<cfsetting showdebugoutput="false">
<cfsetting enablecfoutputonly="true">
<cfcontent type="application/json; charset=utf-8" reset="true">

<!--- Debug: Show La Serenata business and service points --->
<cfquery name="qBiz" datasource="payfrit">
  SELECT ID, Name, BeaconShardID, BeaconMajor
  FROM Businesses
  WHERE Name LIKE '%serenata%'
</cfquery>

<cfif qBiz.recordCount EQ 0>
  <cfoutput>#serializeJSON({ "OK": false, "ERROR": "No business found" })#</cfoutput>
  <cfabort>
</cfif>

<cfif val(qBiz.BeaconShardID) GT 0>
  <cfquery name="qShard" datasource="payfrit">
    SELECT ID, UUID, IsActive
    FROM BeaconShards
    WHERE ID = <cfqueryparam cfsqltype="cf_sql_integer" value="#qBiz.BeaconShardID#">
  </cfquery>
<cfelse>
  <cfset qShard = queryNew("ID,UUID,IsActive")>
</cfif>

<cfquery name="qSP" datasource="payfrit">
  SELECT ID, Name, BeaconMinor, IsActive
  FROM ServicePoints
  WHERE BusinessID = <cfqueryparam cfsqltype="cf_sql_integer" value="#qBiz.ID#">
  ORDER BY SortOrder, Name
</cfquery>

<cfset spList = []>
<cfloop query="qSP">
  <cfset arrayAppend(spList, {
    "ID": qSP.ID,
    "Name": qSP.Name,
    "BeaconMinor": isNull(qSP.BeaconMinor) ? "NULL" : val(qSP.BeaconMinor),
    "IsActive": qSP.IsActive
  })>
</cfloop>

<cfoutput>#serializeJSON({
  "OK": true,
  "Business": {
    "ID": qBiz.ID,
    "Name": qBiz.Name,
    "BeaconShardID": val(qBiz.BeaconShardID),
    "BeaconMajor": val(qBiz.BeaconMajor)
  },
  "Shard": qShard.recordCount GT 0 ? {
    "ID": qShard.ID,
    "UUID": qShard.UUID,
    "IsActive": qShard.IsActive
  } : "NOT FOUND",
  "ServicePoints": spList
})#</cfoutput>
