payfrit-works/api/New folder/save.cfm

136 lines
5.2 KiB
Text

<cfsetting showdebugoutput="false">
<cfsetting enablecfoutputonly="true">
<cfcontent type="application/json; charset=utf-8" reset="true">
<cfheader name="Cache-Control" value="no-store">
<cfscript>
function apiAbort(obj){ writeOutput(serializeJSON(obj)); abort; }
function readJsonBody(){
raw = toString(getHttpRequestData().content);
if (isNull(raw) || len(trim(raw)) EQ 0) return {};
try { parsed = deserializeJSON(raw); }
catch(any e){ apiAbort({ OK=false, ERROR="bad_json", MESSAGE="Invalid JSON body" }); }
if (!isStruct(parsed)) return {};
return parsed;
}
function normStr(v){
if (isNull(v)) return "";
return trim(toString(v));
}
data = readJsonBody();
if (!structKeyExists(request,"UserID") || !isNumeric(request.UserID) || request.UserID LTE 0) apiAbort({OK=false,ERROR="not_logged_in"});
if (!structKeyExists(request,"BusinessID") || !isNumeric(request.BusinessID) || request.BusinessID LTE 0) apiAbort({OK=false,ERROR="no_business_selected"});
if (!structKeyExists(data,"ServicePointName") || len(normStr(data.ServicePointName)) EQ 0) {
apiAbort({OK=false,ERROR="missing_servicepoint_name",MESSAGE="ServicePointName is required"});
}
spid = 0;
if (structKeyExists(data,"ServicePointID") && isNumeric(data.ServicePointID) && int(data.ServicePointID) GT 0) {
spid = int(data.ServicePointID);
}
spName = normStr(data.ServicePointName);
spTypeID = (structKeyExists(data,"ServicePointTypeID") && isNumeric(data.ServicePointTypeID)) ? int(data.ServicePointTypeID) : 0;
spCode = structKeyExists(data,"ServicePointCode") ? normStr(data.ServicePointCode) : "";
descr = structKeyExists(data,"Description") ? normStr(data.Description) : "";
sortOrd = (structKeyExists(data,"SortOrder") && isNumeric(data.SortOrder)) ? int(data.SortOrder) : 0;
isActive = 1;
if (structKeyExists(data,"IsActive")) {
if (isBoolean(data.IsActive)) isActive = (data.IsActive ? 1 : 0);
else if (isNumeric(data.IsActive)) isActive = int(data.IsActive);
else if (isSimpleValue(data.IsActive)) isActive = (lcase(trim(toString(data.IsActive))) EQ "true" ? 1 : 0);
}
</cfscript>
<cfif spid GT 0>
<cfquery datasource="#application.datasource#">
UPDATE ServicePoints
SET
ServicePointName = <cfqueryparam cfsqltype="cf_sql_varchar" value="#spName#">,
ServicePointTypeID = <cfqueryparam cfsqltype="cf_sql_integer" value="#spTypeID#">,
ServicePointCode = <cfqueryparam cfsqltype="cf_sql_varchar" value="#spCode#" null="#(len(spCode) EQ 0)#">,
Description = <cfqueryparam cfsqltype="cf_sql_varchar" value="#descr#" null="#(len(descr) EQ 0)#">,
SortOrder = <cfqueryparam cfsqltype="cf_sql_integer" value="#sortOrd#">,
IsActive = <cfqueryparam cfsqltype="cf_sql_tinyint" value="#isActive#">
WHERE ServicePointID = <cfqueryparam cfsqltype="cf_sql_integer" value="#spid#">
AND BusinessID = <cfqueryparam cfsqltype="cf_sql_integer" value="#request.BusinessID#">
</cfquery>
<cfquery name="qCheck" datasource="#application.datasource#">
SELECT ServicePointID
FROM ServicePoints
WHERE ServicePointID = <cfqueryparam cfsqltype="cf_sql_integer" value="#spid#">
AND BusinessID = <cfqueryparam cfsqltype="cf_sql_integer" value="#request.BusinessID#">
LIMIT 1
</cfquery>
<cfif qCheck.recordCount EQ 0>
<cfoutput>#serializeJSON({ OK=false, ERROR="not_found" })#</cfoutput>
<cfabort>
</cfif>
<cfelse>
<cfquery datasource="#application.datasource#">
INSERT INTO ServicePoints (
BusinessID,
ServicePointName,
ServicePointTypeID,
ServicePointCode,
Description,
SortOrder,
IsActive
) VALUES (
<cfqueryparam cfsqltype="cf_sql_integer" value="#request.BusinessID#">,
<cfqueryparam cfsqltype="cf_sql_varchar" value="#spName#">,
<cfqueryparam cfsqltype="cf_sql_integer" value="#spTypeID#">,
<cfqueryparam cfsqltype="cf_sql_varchar" value="#spCode#" null="#(len(spCode) EQ 0)#">,
<cfqueryparam cfsqltype="cf_sql_varchar" value="#descr#" null="#(len(descr) EQ 0)#">,
<cfqueryparam cfsqltype="cf_sql_integer" value="#sortOrd#">,
<cfqueryparam cfsqltype="cf_sql_tinyint" value="#isActive#">
)
</cfquery>
<cfquery name="qId" datasource="#application.datasource#">
SELECT LAST_INSERT_ID() AS ServicePointID
</cfquery>
<cfset spid = qId.ServicePointID>
</cfif>
<cfquery name="qOut" datasource="#application.datasource#">
SELECT
ServicePointID,
BusinessID,
ServicePointName,
ServicePointTypeID,
ServicePointCode,
Description,
SortOrder,
IsActive,
CreatedAt,
UpdatedAt
FROM ServicePoints
WHERE ServicePointID = <cfqueryparam cfsqltype="cf_sql_integer" value="#spid#">
AND BusinessID = <cfqueryparam cfsqltype="cf_sql_integer" value="#request.BusinessID#">
LIMIT 1
</cfquery>
<cfset sp = {
"ServicePointID"=qOut.ServicePointID,
"BusinessID"=qOut.BusinessID,
"ServicePointName"=qOut.ServicePointName,
"ServicePointTypeID"=qOut.ServicePointTypeID,
"ServicePointCode"=qOut.ServicePointCode,
"Description"=qOut.Description,
"SortOrder"=qOut.SortOrder,
"IsActive"=qOut.IsActive,
"CreatedAt"=(qOut.CreatedAt & ""),
"UpdatedAt"=(qOut.UpdatedAt & "")
}>
<cfoutput>#serializeJSON({ OK=true, ERROR="", SERVICEPOINT=sp })#</cfoutput>