Guest User

Untitled

a guest
Feb 24th, 2019
553
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <cfsilent>
  2. <!---  THE CONTAINS THE LOGIC FOR APPLY_CONING
  3. -----  THE LOGIC CONTAINED WITHIN THIS FILE, IS INCLUDED IN THE FILE, APPLY_CONING.cfm---->
  4.  
  5. <cfset VARIABLES.unique_id = CreateUUID()>
  6. <cfset VARIABLES.finClassUtil = server.javaLoader.create("com.powerhealth.pbrcae.fin.FinancialClassUtil").init()>
  7.  
  8. <cfif isDefined('arguments.sb_invoice_id')>
  9.     <cfquery name="Check_Disable_Costing" datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#">
  10.         SELECT ISNULL(invoice_type.SB_DISABLE_COSTING, 'N') AS SB_DISABLE_COSTING
  11.         FROM SB_INVOICE invoice
  12.             INNER JOIN SB_INVOICE_TYPE invoice_type ON invoice_type.SB_INVOICE_TYPE_CODE =  invoice.SB_INVOICE_TYPE_CODE
  13.             AND invoice_type.SB_CURRENT = 'Y'
  14.         WHERE invoice.SB_INVOICE_ID = <cfqueryparam cfsqltype="cf_sql_numeric" value="#arguments.sb_invoice_id#">
  15.     </cfquery>
  16.  
  17.     <cfif Check_Disable_Costing.SB_DISABLE_COSTING eq "Y">
  18.         <!--- Recheck potential Errors --->
  19.         <cfinvoke component="allocate_chart_of_acc" method="allocate" returnvariable="none">
  20.             <cfinvokeargument name="alloc_coac_mrn" value="#ARGUMENTS.mrn#">
  21.             <cfinvokeargument name="alloc_coac_hl7_source" value="#ARGUMENTS.source#">
  22.             <cfinvokeargument name="imaging_install" value="0">
  23.             <cfinvokeargument name="alloc_coac_invoice_id" value="#arguments.sb_invoice_id#">
  24.         </cfinvoke>
  25.         <cfreturn>
  26.     </cfif>
  27.  
  28. <cfelse>
  29.     <cfquery name="Invoices" datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#">
  30.         SELECT ISNULL(invoice_type.SB_DISABLE_COSTING, 'N') AS SB_DISABLE_COSTING, SB_INVOICE_ID
  31.         FROM SB_INVOICE invoice
  32.             left outer JOIN SB_INVOICE_TYPE invoice_type ON invoice_type.SB_INVOICE_TYPE_CODE =  invoice.SB_INVOICE_TYPE_CODE
  33.             AND invoice_type.SB_CURRENT = 'Y'
  34.         WHERE invoice.SB_PATIENT_MRN = <cfqueryparam cfsqltype="cf_sql_varchar" value="#ARGUMENTS.mrn#">
  35.             AND invoice.SB_HL7_SOURCE = <cfqueryparam cfsqltype="cf_sql_varchar" value="#ARGUMENTS.source#">
  36.     </cfquery>
  37.  
  38.     <cfscript>
  39.  
  40.         invoiceList = server.javaLoader.create("com.powerhealth.pbrcae.util.StringListBuilder").init(",");
  41.  
  42.     </cfscript>
  43.  
  44.         <cfloop query="Invoices">
  45.             <cfif SB_DISABLE_COSTING neq 'Y'>
  46.                 <cfscript>
  47.                     invoice = "";
  48.                     invoice = SB_INVOICE_ID;
  49.                     invoiceList.append("" & invoice & "");
  50.                 </cfscript>
  51.             </cfif>
  52.         </cfloop>
  53.  
  54.     <cfset arguments.sb_invoice_id = invoiceList.toString()>
  55.  
  56. </cfif>
  57.  
  58. <!---
  59. Check to see if emergency dept compensable invoices can be raised on the same day as an inpatient episode.
  60. If so, SDCOM parameter will be set to 'Y' --->
  61. <cfscript> 
  62.     same_day_compensable = application.cache.getSysParam("SDCOM");
  63. </cfscript>
  64.  
  65. <!--- >> CACHED_QUERY << Find the (or a) 100% mbs item cost table.
  66. Used as a back-up price if price not defined for health fund no gaps --->
  67. <cfinvoke component="pbrc-ae.components.apply_coning" method="MBS100" returnvariable="MBS100">
  68. <cfif MBS100.SB_COST_TYPE_ID EQ "">
  69.     <cfset session.errormsg = "The system administrator must set up which cost type is MBS 100%">
  70.     <cflocation url="../sber01.cfm" addtoken="No">
  71. </cfif>
  72.  
  73. <!--- >> CACHED_QUERY << --->
  74. <cfinvoke component="pbrc-ae.components.apply_coning" method="NO_SPECIALIST" returnvariable="NO_SPECIALIST">
  75. <cfset item_types_not_requiring_specialist = "">
  76. <cfoutput query="NO_SPECIALIST">
  77.     <cfset item_types_not_requiring_specialist
  78.          = item_types_not_requiring_specialist & NO_SPECIALIST.SB_ITEM_TYPE_CODE & ",">
  79. </cfoutput>
  80.  
  81. <!--- Check if any records about to be coned have an item_status of 'ER' - Error.
  82. If they do, and the error has been resolved, then the item status is not reset.
  83. To avoid this, run allocate_bill_mech against any invoices with 'ER'.
  84. Mostly CF_allocate_bill_mech is called before apply_coning, but I suspect there
  85. are times when it is skipped.
  86. --->
  87. <cfif IsDefined("ARGUMENTS.call_allocate_bill_mech") and ARGUMENTS.call_allocate_bill_mech eq "N">
  88. <!---   Do not call allocate bill mech if called from sbbp34 --->
  89. <cfelse>
  90.     <cfquery datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#" name="Errorcheck">
  91.         SELECT
  92.             DISTINCT SB_INVOICE.SB_INVOICE_ID, SB_DISABLE_COSTING
  93.         FROM
  94.             SB_INVOICE_ITEM
  95.                 INNER JOIN
  96.             SB_INVOICE  ON SB_INVOICE.SB_INVOICE_ID = SB_INVOICE_ITEM.SB_INVOICE_ID
  97.                 INNER JOIN
  98.             SB_INVOICE_TYPE ON SB_INVOICE.SB_INVOICE_TYPE_CODE = SB_INVOICE_TYPE.SB_INVOICE_TYPE_CODE
  99.             WHERE   (   SB_INVOICE_ITEM.SB_ITEM_STATUS_CODE = 'ER'
  100.                      or (SB_INVOICE.SB_INVOICE_DATE IS NULL AND SB_INVOICE_ITEM.SB_ITEM_STATUS_CODE in ('03','1C')))
  101.               and   SB_INVOICE_ITEM.sb_voucher_id is null
  102.               and   SB_INVOICE.SB_PATIENT_MRN = <cfqueryparam cfsqltype="cf_sql_varchar" value="#ARGUMENTS.mrn#">
  103.               and   SB_INVOICE.SB_HL7_SOURCE = <cfqueryparam cfsqltype="cf_sql_varchar" value="#ARGUMENTS.source#">
  104.     </cfquery>
  105.  
  106.     <cfoutput query="Errorcheck">
  107.         <!--- Set appropriate billing mechanism, invoice status and invoice item status.
  108.             No processing it done is invoice has a date. --->
  109.         <cfinvoke component="allocate_bill_mech"
  110.         method="allocate"
  111.         returnvariable="none">
  112.         <cfif SB_DISABLE_COSTING eq 'Y'>
  113.             <cfinvokeargument name="apply_bill_mech" value="N">
  114.         </cfif>
  115.  
  116.         <cfinvokeargument name="sb_invoice_id" value="#Errorcheck.SB_INVOICE_ID#">
  117.         </cfinvoke>
  118.  
  119.     </cfoutput>
  120. </cfif>
  121.  
  122. <cfif arguments.sb_invoice_id eq ''>
  123.         <cfreturn>
  124. </cfif>
  125.  
  126. <!---
  127. Phil : 11-Jan-2008
  128.  
  129. When Medicare updated their end to Eclipse 6, the field previously used for number of fields in DVA rad onc claims vaporized.
  130. Up until then, Rad Onc claims were sent as a base item, with another item specifying the number of fields. Payment for the two items was paid against one item only.
  131. An adjustment process (SBBP46) swapped the payments around.
  132.  
  133. Post Eclipse 6 at medicare (with p2 on eclipse 4), in order to send multiple fields through to DVA, the field item needs to be repeated multiple times.
  134. When payment is returned, only one of the items gets the full payment.
  135. To further complicate matters, the amount paid by DVA on the fields is "Derived", but it does not publish how the figure is derived. It appears to be a straight payment per field.
  136. Rather than use the coning rules, it is easier just to define the cost for the item.
  137.  
  138. apply_coning splits DVA online items with multiple fields to be multiple items with one field in each.
  139. Sets SB_MIGRATION_P1_ITEM_ID to be the item id of the original item.
  140.  
  141. sbbp46 spreads payment of items with the same SB_MIGRATION_P1_ITEM_ID value.
  142. --->
  143.  
  144.  
  145.     <cfquery datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#" name="PharmacyErrorCheck">
  146.         SELECT
  147.             DISTINCT
  148.             SB_INVOICE.SB_INVOICE_ID,
  149.             SB_DEBTOR.SB_DEBTOR_ADDRESS_1,
  150.             SB_DEBTOR.SB_DEBTOR_SUBURB,
  151.             SB_DEBTOR.SB_DEBTOR_POSTCODE
  152.         FROM
  153.             SB_INVOICE_ITEM     INNER JOIN
  154.             SB_INVOICE  ON
  155.                     SB_INVOICE.SB_INVOICE_ID = SB_INVOICE_ITEM.SB_INVOICE_ID INNER JOIN
  156.             SB_DEBTOR  ON
  157.                 SB_INVOICE.SB_DEBTOR_ID = SB_DEBTOR.SB_DEBTOR_ID
  158.             WHERE
  159.                 (SB_INVOICE.SB_INVOICE_DATE IS NULL
  160.                  OR SB_INVOICE_ITEM.SB_ITEM_STATUS_CODE in ('03','1C'))
  161.               AND   SB_INVOICE_ITEM.SB_ITEM_STATUS_CODE < '50'
  162.               and   SB_INVOICE_ITEM.sb_claim_id is null
  163.               and   SB_INVOICE_ITEM.sb_voucher_id is null
  164.               and   SB_INVOICE.SB_BILL_MECH_CODE = 'P'
  165.               and   SB_INVOICE.SB_PATIENT_MRN = <cfqueryparam cfsqltype="cf_sql_varchar" value="#ARGUMENTS.mrn#">
  166.               and   SB_INVOICE.SB_HL7_SOURCE = <cfqueryparam cfsqltype="cf_sql_varchar" value="#ARGUMENTS.source#">
  167.               and   SB_INVOICE.SB_INVOICE_ID in (#ARGUMENTS.sb_invoice_id#)
  168.     </cfquery>
  169.  
  170.  
  171.  
  172.  
  173. <!---
  174. Populate table SB_CONING_RUN with invoices that do not have an invoice date
  175. for the selected MRN and HL7 Source
  176.  
  177. Similar code exists in allocate_bill_mech
  178.  --->
  179.  <cfquery datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#" name="INSERT_CONING_RECORDS">
  180.     SET DATEFORMAT DMY
  181.     INSERT INTO SB_CONING_RUN
  182.     (
  183.         SB_CONING_RUN_NO,
  184.         SB_ITEM_NUMBER,
  185.         SB_SCHEDULE_FEE,
  186.         SB_ACTUAL_FEE,
  187.         SB_DERIVED_NOGAP_PERCENT,
  188.         SB_CONING_STATUS_CODE,
  189.         SB_SERVICE_DATE,
  190.         SB_CONING_ITEM_ID,
  191.         SB_ACTUAL_FEE_75,
  192.         SB_MBS_100_COST,
  193.         SB_CONING_PROVIDER,
  194.         SB_CONING_PATIENT_AGE,
  195.         SB_CONING_MSG_TO_HIC,
  196.         SB_CONING_QTY,
  197.         SB_GST_PERCENT,
  198.         SB_APPLY_CONING,
  199.         SB_CONING_SPECIALTY_CODE,
  200.         SB_CONING_HOSP_CODE,
  201.         SB_CONING_FIN_CLASS,
  202.         SB_CONING_BILL_MECH,
  203.         SB_CONING_EPISODE,
  204.         SB_REQUEST_DATE,
  205.         SB_CONING_ITEM_TYPE,
  206.         SB_CONING_REQUEST_PROVIDER,
  207.         SB_CONING_NOT_DUP_SERVICE,
  208.         SB_CONING_MULTIPLE_OVERRIDE,
  209.         SB_CONING_REQUIREMENT_CODE,
  210.         SB_CONING_ADMIT_DATE,
  211.         SB_CONING_DISCH_DATE,
  212.         SB_CONING_INVOICE_ID,
  213.         SB_CONING_REQ_PROV_START,
  214.         SB_CONING_REQ_PROV_END,
  215.         SB_CONING_FIN_CLASS_VALID,
  216.         SB_CONING_HOSP_VALID,
  217.         SB_CONING_SPEC_PROV_DEFINED,
  218.         SB_SERV_PROV_VALID,
  219.         SB_CONING_LSPN,
  220.         SB_CONING_FUND_GROUP_ID,
  221.         SB_CONING_REFERRAL_OVERRIDE,
  222.         SB_CONING_DURATION,
  223.         SB_PRIV_BILLING_END_DATE,
  224.         SB_CONING_FIN_CLASS_TYPE_CODE,
  225.         SB_CONING_DEFENCE,
  226.         CURRENT_DEDUCTIONS
  227.     )
  228.     SELECT
  229.         /* THIS CF VARIABLE CONTAINS A GUID */
  230.         '#VARIABLES.unique_id#',
  231.         MBS_ITEM.SB_MBS_ITEM_NUMBER,
  232.         /* INSERT A ZERO SO THE CONING ERROR DOESN'T APPEAR WHEN PROSTHESIS ITEMS ARE USED THAT DO NOT HAVE A PRICE.*/
  233.         CASE WHEN MBS_ITEM.SB_ITEM_TYPE_CODE <> 'R' THEN
  234.             ITEM_COST.SB_MBS_ITEM_FEE
  235.         ELSE
  236.             ISNULL(ITEM_COST.SB_MBS_ITEM_FEE, 0)
  237.         END AS SB_MBS_ITEM_FEE,
  238.  /* if price entered, then use it.
  239.     if price defined in table, then use it
  240.     if nogap, and price not in fund table, use mbs cost */
  241.         CASE WHEN  ITEM.SB_PRICE_OVERRIDE = 'Y' --AND ITEM_COST.SB_MBS_ITEM_FEE IS NULL
  242.                         THEN
  243.                             CASE WHEN ITEM.SB_GST_AMOUNT_CLAIMED IS NOT NULL
  244.                                 THEN ITEM.SB_INVOICE_COST - ITEM.SB_GST_AMOUNT_CLAIMED
  245.                                 ELSE ITEM.SB_INVOICE_COST
  246.                             END
  247.             WHEN  ISNULL(ITEM.SB_PRICE_OVERRIDE, 'N') = 'N' AND ITEM_COST.SB_MBS_ITEM_FEE IS NOT NULL and ITEM_COST.SB_ITEM_QTY IS NULL
  248.                         THEN ITEM_COST.SB_MBS_ITEM_FEE * ITEM.SB_ITEM_QTY
  249.             WHEN  ISNULL(ITEM.SB_PRICE_OVERRIDE, 'N') = 'N' AND ITEM_COST.SB_MBS_ITEM_FEE IS NOT NULL and ITEM_COST.SB_ITEM_QTY IS NOT NULL
  250.                         THEN ITEM_COST.SB_MBS_ITEM_FEE
  251.              WHEN ITEM_COST.SB_MBS_ITEM_FEE IS NOT NULL AND ITEM_COST.SB_MBS_ITEM_FEE > 0
  252.                         THEN ITEM_COST.SB_MBS_ITEM_FEE
  253.              WHEN HEALTH_FUND.SB_FUND_NO_GAPS = 'Y'
  254.               AND SCHEME.SB_FUND_PROVIDER_ID IS NOT NULL
  255.               AND   ((SB_SERVICE_DATE BETWEEN COVERAGE.SB_NOGAP_START_DATE AND ISNULL(COVERAGE.SB_NOGAP_END_DATE, GETDATE()) ) OR EPISODE.SB_COVERAGE_ID IS NULL)
  256.                         THEN DEFAULT_ITEM_COST.SB_MBS_ITEM_FEE
  257.               ELSE 0
  258.         END,
  259.         ITEM_COST.SB_DERIVED_NOGAP_PERCENT,
  260.         MECH.SB_INITIAL_ITEM_STATUS,
  261.         ITEM.SB_SERVICE_DATE,
  262.         ITEM.SB_INVOICE_ITEM_ID,
  263.         ITEM_COST.SB_MBS_ITEM_FEE_75,
  264.         DEFAULT_ITEM_COST.SB_MBS_ITEM_FEE,
  265.         INVOICE.SB_PROVIDER_NUMBER,
  266.         abs(Case when datepart(dy, PATIENT_REGISTER.SB_PATIENT_DOB) > datepart(dy, ITEM.SB_SERVICE_DATE)
  267.             then year(ITEM.SB_SERVICE_DATE) - year(PATIENT_REGISTER.SB_PATIENT_DOB) - 1
  268.             else year(ITEM.SB_SERVICE_DATE) - year(PATIENT_REGISTER.SB_PATIENT_DOB)
  269.         end),
  270.         '',
  271.         ITEM.SB_ITEM_QTY,
  272.         GST.SB_GST_PERCENT,
  273.         CRITERIA.SB_APPLY_CONING_RULES,
  274.         INVOICE.SB_SPECIALTY_CODE,
  275.         EPISODE.SB_HOSPITAL_CODE,
  276.         EFC.SB_FIN_CLASS_CODE,
  277. -- Pharmacy is not represented in cost_type_criteria as Pharmacy episodes do not have any episode fin class records.
  278.         Case
  279.         when EPISODE.SB_EPISODE_TYPE_CODE = 'P'
  280.              then 'P'
  281.         WHEN Invoice.SB_BILL_MECH_CODE = 'B' AND EPISODE.SB_MC_FORM_SIGNED = 'Y' AND SB_MEDICARE_IMAGE_FILENAME IS NOT NULL THEN
  282.             'B'
  283.         ELSE
  284.             IsNull(Invoice.SB_RERAISE_BILL_MECH, CRITERIA.SB_BILL_MECH_CODE)
  285.         end,
  286.         EPISODE.SB_EPISODE_NUMBER,
  287.         ITEM.SB_REQUEST_DATE,
  288.         MBS_ITEM.SB_ITEM_TYPE_CODE,
  289.         ITEM.SB_PROVIDER_NUMBER,
  290.         IsNull(ITEM.SB_HICOL_NOT_DUP_SERVICE,'N'),
  291.         IsNull(ITEM.SB_MULTIPLE_PROCEDURE_OVERRIDE,'N'),
  292.         CASE WHEN ITEM.SB_INV_ITEM_COMMENT LIKE 'Split by Pbrc-ae for Eclipse 4%' THEN '' ELSE IsNull(ITEM.SB_REQUIREMENT_CODE,'N/A') END,
  293.         EPISODE.SB_ADMISSION_DATE,
  294.         EPISODE.SB_DISCHARGE_DATE,
  295.         INVOICE.SB_INVOICE_ID,
  296.         IsNull(REQUESTING_PROVIDER.SB_PROVIDER_START_DATE,'31-dec-1900') AS SB_PROVIDER_START_DATE,
  297.         IsNull(REQUESTING_PROVIDER.SB_PROVIDER_END_DATE,'31-dec-3000') AS SB_PROVIDER_END_DATE,
  298.         CASE WHEN (FIN_CLASS.SB_FIN_CLASS_DESC = 'Undefined Fin Class' or FIN_CLASS.SB_FIN_CURRENT <> 'Y')
  299.              THEN 'N'
  300.              WHEN FIN_CLASS_TYPE.SB_COMPENSABLE = 'Y'
  301.              THEN 'C'
  302.              ELSE 'Y'
  303.         END,
  304.         CASE WHEN HOSPITAL.SB_HOSPITAL_NAME = 'Unknown' or HOSPITAL.SB_HOSPITAL_CURRENT <> 'Y'
  305.              THEN 'N'
  306.              ELSE 'Y'
  307.         END,
  308.         CASE WHEN SB_DR_SPECIALTY.sb_specialty_code IS NULL
  309.             THEN 'N'
  310.             ELSE 'Y'
  311.         END,
  312.         CASE WHEN PROVIDER.SB_PROVIDER_SURNAME = 'Unknown'
  313.             THEN 'N'
  314.             ELSE 'Y'
  315.         END,
  316.         IsNull(SB_DR_SPECIALTY.SB_LSPN, HOSPITAL.SB_LSPN) AS SB_LSPN,
  317.         CRITERIA.SB_FUND_GROUP_ID,
  318.         Case when item.SB_HICOL_SELF_DEEMED = 'D' then 'D'
  319.              else ITEM.SB_REFERRAL_OVERRIDE
  320.         End,
  321.         ITEM.SB_DURATION,
  322.         SB_ROOM_MODALITY_MAPPING.SB_PRIVATE_BILLING_END_DATE,
  323.         FIN_CLASS_TYPE.SB_FIN_CLASS_TYPE_CODE,
  324.         FIN_CLASS_TYPE.SB_DEFENSE,
  325.         0
  326.     FROM
  327.         SB_PATIENT_EPISODE EPISODE   INNER JOIN
  328.         SB_PATIENT_REGISTER PATIENT_REGISTER  ON
  329.                 EPISODE.SB_PATIENT_MRN = PATIENT_REGISTER.SB_PATIENT_MRN AND
  330.                 EPISODE.SB_HL7_SOURCE = PATIENT_REGISTER.SB_HL7_SOURCE INNER JOIN
  331.         SB_INVOICE INVOICE  ON
  332.             EPISODE.SB_EPISODE_NUMBER = INVOICE.SB_EPISODE_NUMBER INNER JOIN
  333.         SB_INVOICE_ITEM ITEM  ON
  334.             INVOICE.SB_INVOICE_ID = ITEM.SB_INVOICE_ID INNER JOIN
  335.         SB_GST_CATEGORY GST with (nolock) ON
  336.             ITEM.SB_GST_CAT_ID = GST.SB_GST_CAT_ID  INNER JOIN
  337.         SB_MBS_ITEM MBS_ITEM  with (nolock) ON
  338.             ITEM.SB_MBS_ITEM_NUMBER = MBS_ITEM.SB_MBS_ITEM_NUMBER INNER JOIN
  339.         SB_ITEM_TYPE ITEM_TYPE with (nolock) ON
  340.             MBS_ITEM.SB_ITEM_TYPE_CODE = ITEM_TYPE.SB_ITEM_TYPE_CODE INNER JOIN
  341.         /* SELECT THE FINANCNIAL CLASS WHERE THE SERVICE DATE IS BETWEEN THE EPISODE FIN CLASS */
  342.     SB_EPISODE_FIN_CLASS EFC   ON
  343.         EPISODE.SB_EPISODE_NUMBER = EFC.SB_EPISODE_NUMBER AND
  344.         (
  345.         CONVERT(DATETIME, CONVERT(VARCHAR, ITEM.SB_SERVICE_DATE, 103)) = EFC.SB_FIN_DATE  OR
  346.             (
  347.             MBS_ITEM.SB_ITEM_TYPE_CODE = 'T' AND
  348.             CONVERT(DATETIME, CONVERT(VARCHAR, EPISODE.SB_DISCHARGE_DATE, 103)) = EFC.SB_FIN_DATE
  349.             )
  350.             OR
  351.             (
  352.                 EPISODE.SB_EPISODE_TYPE_CODE = 'P'
  353.             )
  354.         )
  355.         INNER JOIN
  356.         SB_FINANCIAL_CLASSIFICATION FIN_CLASS with (nolock) ON
  357.             EFC.SB_FIN_CLASS_CODE = FIN_CLASS.SB_FIN_CLASS_CODE INNER JOIN
  358.         SB_HOSPITAL HOSPITAL  with (nolock) ON
  359.             EPISODE.SB_HOSPITAL_CODE = HOSPITAL.SB_HOSPITAL_CODE
  360.         LEFT OUTER JOIN
  361.         SB_HEALTH_FUND HEALTH_FUND with (nolock) ON
  362.             EPISODE.SB_HEALTH_FUND_CODE = HEALTH_FUND.SB_HEALTH_FUND_CODE  LEFT OUTER JOIN
  363.         SB_PROVIDER REQUESTING_PROVIDER with (nolock) ON
  364.             REQUESTING_PROVIDER.SB_PROVIDER_NUMBER = ITEM.SB_PROVIDER_NUMBER    LEFT OUTER JOIN
  365.         SB_DR_SPECIALTY with (nolock) ON
  366.                 SB_DR_SPECIALTY.sb_specialty_code = INVOICE.sb_specialty_code AND
  367.                 SB_DR_SPECIALTY.sb_provider_number = INVOICE.sb_provider_number LEFT OUTER JOIN
  368.         SB_PROVIDER PROVIDER with (nolock) ON
  369.             INVOICE.SB_PROVIDER_NUMBER = PROVIDER.SB_PROVIDER_NUMBER LEFT OUTER JOIN
  370.         SB_FUND_PROVIDER_SCHEME SCHEME with (nolock) ON
  371.             INVOICE.SB_PROVIDER_NUMBER = SCHEME.SB_PROVIDER_NUMBER AND
  372.             EPISODE.SB_HEALTH_FUND_CODE = SCHEME.SB_HEALTH_FUND_CODE AND
  373.             ITEM.SB_SERVICE_DATE BETWEEN SCHEME.SB_FUND_PROVIDER_START_DATE AND ISNULL(SCHEME.SB_FUND_PROVIDER_END_DATE, GETDATE())
  374.         LEFT OUTER JOIN
  375.         SB_HEALTH_FUND_COVERAGE COVERAGE with (nolock) ON
  376.             EPISODE.SB_COVERAGE_ID = COVERAGE.SB_COVERAGE_ID AND
  377.             COVERAGE.SB_NOGAP_START_DATE  <= (SELECT MIN(SB_SERVICE_DATE) FROM SB_INVOICE_ITEM WHERE SB_INVOICE_ID = INVOICE.SB_INVOICE_ID)  AND
  378.             ( COVERAGE.SB_NOGAP_END_DATE IS NULL OR COVERAGE.SB_NOGAP_END_DATE >= (SELECT MAX(SB_SERVICE_DATE) FROM SB_INVOICE_ITEM WHERE SB_INVOICE_ID = INVOICE.SB_INVOICE_ID))
  379.         LEFT OUTER JOIN
  380.         SB_COST_TYPE_CRITERIA CRITERIA with (nolock) ON
  381.             CRITERIA.SB_COST_TYPE_CRITERIA_ID = INVOICE.SB_COST_TYPE_CRITERIA_ID
  382.             LEFT OUTER JOIN
  383.         /* SELECT THE ITEM'S COST WHERE THE SERVICE DATE IS BEWTEEN THE COST START AND END DATE */
  384.         SB_MBS_ITEM_COST ITEM_COST  with (nolock) ON
  385.             MBS_ITEM.SB_MBS_ITEM_NUMBER = ITEM_COST.SB_MBS_ITEM_NUMBER AND
  386.             CRITERIA.SB_COST_TYPE_ID = ITEM_COST.SB_COST_TYPE_ID AND
  387.             ITEM.SB_SERVICE_DATE BETWEEN ITEM_COST.SB_MBS_COST_START AND ISNULL(ITEM_COST.SB_MBS_COST_END,'31-DEC-3100') AND
  388. -- item qty used only for rad onc items. On other item cost records it is null.
  389.             (ITEM_COST.SB_ITEM_QTY IS NULL OR ITEM.SB_ITEM_QTY = ITEM_COST.SB_ITEM_QTY)
  390.             LEFT OUTER JOIN
  391.         /* SELECT THE DEFAULT MBS ITEM COST IN CASE FUND NO GAP PRICE IS MISSING */
  392.         SB_MBS_ITEM_COST DEFAULT_ITEM_COST with (nolock) ON
  393.             MBS_ITEM.SB_MBS_ITEM_NUMBER = DEFAULT_ITEM_COST.SB_MBS_ITEM_NUMBER AND
  394.             DEFAULT_ITEM_COST.SB_COST_TYPE_ID = #MBS100.SB_COST_TYPE_ID# AND
  395.             ITEM.SB_SERVICE_DATE BETWEEN DEFAULT_ITEM_COST.SB_MBS_COST_START AND ISNULL(DEFAULT_ITEM_COST.SB_MBS_COST_END,'31-DEC-3100')
  396.             LEFT OUTER JOIN
  397.         SB_FIN_CLASS_TYPE FIN_CLASS_TYPE  with (nolock) ON
  398.             FIN_CLASS.SB_FIN_CLASS_TYPE_CODE = FIN_CLASS_TYPE.SB_FIN_CLASS_TYPE_CODE LEFT OUTER JOIN
  399.         SB_BILLING_MECHANISM MECH  with (nolock) ON
  400.             IsNull(Invoice.SB_RERAISE_BILL_MECH, CRITERIA.SB_BILL_MECH_CODE) = MECH.SB_BILL_MECH_CODE
  401.         LEFT OUTER JOIN SB_INV_BATCH
  402.             ON SB_INV_BATCH.SB_INV_BATCH_ID = ITEM.SB_INV_BATCH_ID
  403.         LEFT OUTER JOIN SB_ROOM_MODALITY_MAPPING
  404.             ON ITEM.SB_RMM_ID = SB_ROOM_MODALITY_MAPPING.SB_RMM_ID
  405. -- If an invoice has been reraised, it will have an RT flag. It should therefore be ignored in any coning checks etc.
  406.         LEFT OUTER JOIN SB_INVOICE_ERROR
  407.             ON INVOICE.SB_INVOICE_ID = SB_INVOICE_ERROR.SB_INVOICE_ID
  408.             AND SB_INVOICE_ERROR.SB_INVOICE_FLAG_CODE = 'RT'
  409.         WHERE
  410.             SB_INVOICE_ERROR.SB_INVOICE_FLAG_CODE IS NULL AND
  411. <!--- If allocate bill mech set the status to 'ER, then don't check further.
  412. This was added by Phil to prevent overwriting (and clearing) of error code and text
  413. if it was set by allocate_bill_mech.
  414. --->
  415.             SB_EPISODE_DELETION_DATE IS NULL AND
  416.             ITEM.SB_ITEM_STATUS_CODE <> 'ER' AND
  417.             ITEM.SB_ITEM_STATUS_CODE <> 'MI' AND  -- Items migrated to another system should be ignored.
  418.             (SB_INV_BATCH.SB_BATCH_STATUS IS NULL OR SB_INV_BATCH.SB_BATCH_STATUS <> 'C') AND
  419.             (INVOICE.SB_INVOICE_DATE IS NULL
  420.              OR ITEM.SB_ITEM_STATUS_CODE in ('03','1C'))
  421.             AND ITEM.SB_ITEM_STATUS_CODE < '50'
  422.             and ITEM.sb_claim_id is null
  423.             and ITEM.sb_voucher_id is null
  424.             AND
  425.             EPISODE.SB_PATIENT_MRN = <cfqueryparam cfsqltype="cf_sql_varchar" value="#ARGUMENTS.mrn#"> and
  426.             EPISODE.SB_HL7_SOURCE = <cfqueryparam cfsqltype="cf_sql_varchar" value="#ARGUMENTS.source#">
  427. <!--- The db4 clinic is for printing db4 certificates only. No invoices will ever be processed from this pseudo clinic --->
  428.              and (episode.sb_clinic_code is null or episode.sb_clinic_code <> 'DB4')
  429. <!--- Only Cone internal invoices - ignore externally generated invoices. --->
  430.             and (INVOICE.SB_SOURCE_CODE = 'I')
  431.             and INVOICE.SB_INVOICE_ID in (#ARGUMENTS.sb_invoice_id#)
  432.         ORDER BY
  433.             EPISODE.SB_PATIENT_MRN,
  434.             ITEM.SB_SERVICE_DATE,
  435.             ITEM.SB_MBS_ITEM_NUMBER
  436.  </cfquery>
  437.  
  438.  
  439. <!--- Tidy coning - the matching to cost type criteria on health fund
  440. should be "If Fund group matches and no gaps select record,
  441.            else if criteria fund is null select record"
  442. However, this code does not work as desired, in that if the fund does match, the 'else'
  443. match also is selected. Hence a clean up afterwards to delete these duplications. --->
  444.  
  445. <!--- If there are two records for the same invoice item, and one has a null cost, then delete it --->
  446. <cfquery datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#" name="Find_Dup1">
  447.     SELECT  SB_CONING_ITEM_ID,
  448.             SB_CONING_ID,
  449.             IsNull(SB_SCHEDULE_FEE,-1) as SB_SCHEDULE_FEE
  450.     FROM    SB_CONING_RUN
  451.     WHERE   SB_CONING_RUN_NO = '#VARIABLES.unique_id#'
  452.     ORDER BY SB_CONING_ITEM_ID, SB_SCHEDULE_FEE DESC, SB_CONING_FUND_GROUP_ID desc
  453. </cfquery>
  454. <cfset last_item = "">
  455.  
  456. <cfoutput query="Find_Dup1">
  457.     <cfif last_item neq Find_Dup1.SB_CONING_ITEM_ID>
  458.         <cfset last_item = Find_Dup1.SB_CONING_ITEM_ID>
  459.     <cfelse>
  460.         <cfif Find_Dup1.SB_SCHEDULE_FEE EQ -1>
  461.             <cfquery datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#" name="Kill_Dup1">
  462.                 DELETE FROM SB_CONING_RUN
  463.                 WHERE   SB_CONING_ID = #Find_Dup1.SB_CONING_ID#
  464.             </cfquery>
  465.         </cfif>
  466.     </cfif>
  467. </cfoutput>
  468.  
  469. <!--- If there are still two records for the same invoice item,
  470. it should be caused by one having a null fund group. Delete any duplicates,
  471. but give survival preference to a record with a valid fund group. --->
  472. <cfquery datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#" name="Find_Dup2">
  473.     SELECT  SB_CONING_ITEM_ID,
  474.             SB_CONING_ID,
  475.             IsNull(SB_CONING_FUND_GROUP_ID,'') as SB_CONING_FUND_GROUP_ID
  476.     FROM    SB_CONING_RUN
  477.     WHERE   SB_CONING_RUN_NO = '#VARIABLES.unique_id#'
  478.     ORDER BY SB_CONING_ITEM_ID, SB_CONING_FUND_GROUP_ID desc
  479. </cfquery>
  480. <cfset last_item = "">
  481.  
  482. <cfoutput query="Find_Dup2">
  483.     <cfif last_item neq Find_Dup2.SB_CONING_ITEM_ID>
  484.         <cfset last_item = Find_Dup2.SB_CONING_ITEM_ID>
  485.     <cfelse>
  486.         <cfquery datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#" name="Kill_Dup2">
  487.             DELETE FROM SB_CONING_RUN
  488.             WHERE   SB_CONING_ID = #Find_Dup2.SB_CONING_ID#
  489.         </cfquery>
  490.     </cfif>
  491. </cfoutput>
  492.  
  493. <!---
  494.     Process records that do not require coning
  495. --->
  496. <!--- Find any records that do not have a cost and update the coning_record with an error message --->
  497. <cfquery datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#" name="MISSING_COST">
  498.     SELECT  CR.SB_CONING_ID,
  499.             CR.SB_CONING_ITEM_ID,
  500.             CR.SB_CONING_FIN_CLASS,
  501.             CR.SB_ITEM_NUMBER,
  502.             CR.SB_ACTUAL_FEE,
  503.             CTC.SB_COST_TYPE_ID,
  504.             CR.SB_CONING_SPECIALTY_CODE,
  505.             CR.SB_CONING_PROVIDER,
  506.             CR.SB_CONING_INVOICE_ID,
  507.             E.SB_EPISODE_TYPE_CODE,
  508.             I.SB_EPISODE_NUMBER,
  509.             II.SB_INV_BATCH_ID
  510.     FROM    SB_CONING_RUN CR
  511.             INNER JOIN SB_PATIENT_EPISODE E with (nolock) ON E.SB_EPISODE_NUMBER = CR.SB_CONING_EPISODE
  512.             INNER JOIN SB_INVOICE_ITEM II with (nolock) ON II.SB_INVOICE_ITEM_ID = CR.SB_CONING_ITEM_ID
  513.             INNER JOIN SB_INVOICE I with (nolock) ON I.SB_INVOICE_ID = II.SB_INVOICE_ID
  514.             INNER JOIN SB_COST_TYPE_CRITERIA CTC with (nolock) ON CTC.SB_COST_TYPE_CRITERIA_ID = I.SB_COST_TYPE_CRITERIA_ID
  515.     WHERE   (CR.SB_SCHEDULE_FEE IS NULL OR CR.SB_SCHEDULE_FEE = 0.00)
  516.             AND SB_CONING_RUN_NO = <cfqueryparam cfsqltype="cf_sql_varchar" value="#VARIABLES.unique_id#">
  517. </cfquery>
  518.  
  519.  
  520. <cfif MISSING_COST.RECORDCOUNT NEQ 0>
  521.     <cfset ORG_MISSING_COST_LIST_CONING_ID = VALUELIST(MISSING_COST.SB_CONING_ID)>
  522.     <!--- Perform item cost determination and item number transformation based on the COST_DEFAULT_MAPPING table --->
  523.     <cfoutput query="MISSING_COST">
  524.         <!--- Retrieves alternate costs by looking up the Cost Type Default;
  525.             Considers the costs based on the alternate Item Number (SB_ALTERNATE_ITEM_CODE/ALT_MIC),
  526.             as well as costs based on the original Item Number (SB_MBS_ITEM_NUMBER/DEF_ALT_MIC)
  527.             Orders by the Cost Type Default priority and only considers non-zero costs. --->
  528.         <cfquery name="ALTERNATE_ITEM_FEE" datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#">
  529.             DECLARE @ITEM_NUMBER  VARCHAR(10)   = <cfif MISSING_COST.SB_ITEM_NUMBER neq "">'#MISSING_COST.SB_ITEM_NUMBER#'<cfelse>NULL</cfif>
  530.             DECLARE @ITEM_ID NUMERIC(18,0)      = <cfif MISSING_COST.SB_CONING_ITEM_ID neq "">'#MISSING_COST.SB_CONING_ITEM_ID#'<cfelse>NULL</cfif>
  531.             DECLARE @FIN_CLASS VARCHAR(8)       = <cfif MISSING_COST.SB_CONING_FIN_CLASS neq "">'#MISSING_COST.SB_CONING_FIN_CLASS#'<cfelse>NULL</cfif>
  532.             DECLARE @EPISODE_TYPE VARCHAR(1)    = <cfif MISSING_COST.SB_EPISODE_TYPE_CODE neq "">'#MISSING_COST.SB_EPISODE_TYPE_CODE#'<cfelse>NULL</cfif>
  533.             DECLARE @ACTUAL_FEE NUMERIC(18,2)   = <cfif MISSING_COST.SB_ACTUAL_FEE neq "">'#MISSING_COST.SB_ACTUAL_FEE#'<cfelse>NULL</cfif>;
  534.  
  535.             WITH ORIGINAL AS ( 
  536.                 SELECT  
  537.                     MIC.SB_COST_TYPE_ID,
  538.                     MIC.SB_MBS_ITEM_NUMBER,
  539.                     MIC.SB_MBS_ITEM_FEE,
  540.                     MIC.SB_MBS_COST_START,
  541.                     MIC.SB_MBS_ITEM_FEE_75,
  542.                     CDM.SB_DEFAULT_COST_TYPE_ID,
  543.                     CDM.SB_PRIORITY,
  544.                     MIC.SB_DERIVED_NOGAP_PERCENT,
  545.                     II.SB_PRICE_OVERRIDE,
  546.                     II.SB_ITEM_QTY,
  547.                     II.SB_SERVICE_DATE,
  548.                     EPISODE.SB_COVERAGE_ID,
  549.                     CTC.SB_FIN_CLASS_TYPE_CODE,
  550.                     CASE WHEN HEALTH_FUND.SB_FUND_NO_GAPS = 'Y' AND SCHEME.SB_FUND_PROVIDER_ID IS NOT NULL
  551.                             AND ((II.SB_SERVICE_DATE BETWEEN COVERAGE.SB_NOGAP_START_DATE AND ISNULL(COVERAGE.SB_NOGAP_END_DATE, GETDATE()) ) OR COVERAGE.SB_COVERAGE_ID IS NULL)
  552.                     THEN 1 ELSE 0 END AS SCHEMEPROVIDER_NO_GAPS
  553.                 FROM SB_INVOICE_ITEM II
  554.                 INNER JOIN SB_INVOICE INV with (nolock) ON II.SB_INVOICE_ID = INV.SB_INVOICE_ID
  555.                 INNER JOIN SB_PATIENT_EPISODE EPISODE with (nolock) ON EPISODE.SB_EPISODE_NUMBER = INV.SB_EPISODE_NUMBER
  556.                 INNER JOIN SB_MBS_ITEM_COST MIC with (nolock) ON MIC.SB_MBS_ITEM_NUMBER = II.SB_MBS_ITEM_NUMBER
  557.                     AND II.SB_SERVICE_DATE BETWEEN MIC.SB_MBS_COST_START AND ISNULL(MIC.SB_MBS_COST_END, DATEADD(Y,100,GETDATE()))
  558.                 INNER JOIN SB_COST_DEFAULT_MAPPING CDM with (nolock) ON CDM.SB_COST_TYPE_ID = MIC.SB_COST_TYPE_ID
  559.                 LEFT OUTER JOIN SB_HEALTH_FUND HEALTH_FUND with (nolock) ON EPISODE.SB_HEALTH_FUND_CODE = HEALTH_FUND.SB_HEALTH_FUND_CODE  
  560.                 LEFT OUTER JOIN SB_FUND_PROVIDER_SCHEME SCHEME with (nolock) ON INV.SB_PROVIDER_NUMBER = SCHEME.SB_PROVIDER_NUMBER
  561.                     AND EPISODE.SB_HEALTH_FUND_CODE = SCHEME.SB_HEALTH_FUND_CODE
  562.                     AND II.SB_SERVICE_DATE BETWEEN SCHEME.SB_FUND_PROVIDER_START_DATE AND ISNULL(SCHEME.SB_FUND_PROVIDER_END_DATE, GETDATE())
  563.                 LEFT OUTER JOIN SB_HEALTH_FUND_COVERAGE COVERAGE with (nolock) ON EPISODE.SB_COVERAGE_ID = COVERAGE.SB_COVERAGE_ID
  564.                     AND COVERAGE.SB_NOGAP_START_DATE <= (SELECT MIN(MIN_II.SB_SERVICE_DATE) FROM SB_INVOICE_ITEM MIN_II WHERE MIN_II.SB_INVOICE_ID = INV.SB_INVOICE_ID)  AND
  565.                         ( COVERAGE.SB_NOGAP_END_DATE IS NULL OR COVERAGE.SB_NOGAP_END_DATE >= (SELECT MAX(MAX_II.SB_SERVICE_DATE) FROM SB_INVOICE_ITEM MAX_II WHERE MAX_II.SB_INVOICE_ID = INV.SB_INVOICE_ID))
  566.                 LEFT OUTER JOIN SB_COST_TYPE_CRITERIA CTC with (nolock) ON CTC.SB_COST_TYPE_ID = MIC.SB_COST_TYPE_ID
  567.                 WHERE II.SB_INVOICE_ITEM_ID = @ITEM_ID
  568.                     AND CDM.SB_FIN_CLASS_CODE = @FIN_CLASS
  569.                     AND CDM.SB_EPISODE_TYPE_CODE = @EPISODE_TYPE
  570.                    
  571.             ) SELECT TOP 1
  572.                     SB_PRIORITY,
  573.                     SB_COST_TYPE_ID,
  574.                     SB_MBS_ITEM_NUMBER,
  575.                     SB_SCHEDULE_FEE,
  576.                     SB_ACTUAL_FEE,
  577.                     SB_ACTUAL_FEE_75,
  578.                     SB_ITEM_TYPE_CODE,
  579.                     SB_FIN_CLASS_TYPE_CODE,
  580.                     ITEM_VIEW_ID,
  581.                     SB_MBS_COST_START,
  582.                     SB_APPLY_CONING_RULES,
  583.                     SB_DERIVED_NOGAP_PERCENT
  584.                 FROM ( SELECT
  585.                     <!--- ORIGINAL ITEM FEE--->
  586.                     ORIGINAL.SB_PRIORITY,
  587.                     ORIGINAL.SB_COST_TYPE_ID,
  588.                     ORIGINAL.SB_MBS_ITEM_NUMBER,
  589.                     CASE WHEN CTC.SB_ITEM_TYPE_CODE <> 'R' THEN ORIGINAL.SB_MBS_ITEM_FEE ELSE ISNULL(ORIGINAL.SB_MBS_ITEM_FEE, 0) END AS SB_SCHEDULE_FEE,
  590.                     CASE
  591.                         WHEN ORIGINAL.SB_PRICE_OVERRIDE = 'Y' AND @ACTUAL_FEE IS NOT NULL  
  592.                             THEN @ACTUAL_FEE
  593.                         WHEN  ISNULL(ORIGINAL.SB_PRICE_OVERRIDE, 'N') = 'N' AND ORIGINAL.SB_MBS_ITEM_FEE IS NOT NULL and ORIGINAL.SB_ITEM_QTY IS NULL
  594.                             THEN ORIGINAL.SB_MBS_ITEM_FEE * ORIGINAL.SB_ITEM_QTY
  595.                         WHEN  ISNULL(ORIGINAL.SB_PRICE_OVERRIDE, 'N') = 'N' AND ORIGINAL.SB_MBS_ITEM_FEE IS NOT NULL and ORIGINAL.SB_ITEM_QTY IS NOT NULL
  596.                             THEN ORIGINAL.SB_MBS_ITEM_FEE
  597.                         WHEN ORIGINAL.SB_MBS_ITEM_FEE IS NOT NULL AND ORIGINAL.SB_MBS_ITEM_FEE > 0
  598.                             THEN ORIGINAL.SB_MBS_ITEM_FEE
  599.                         WHEN ORIGINAL.SCHEMEPROVIDER_NO_GAPS = 1
  600.                             THEN ORIG_DEFAULT_ITEM_COST.SB_MBS_ITEM_FEE
  601.                         ELSE 0 END AS SB_ACTUAL_FEE,
  602.                     ORIGINAL.SB_MBS_ITEM_FEE_75 AS SB_ACTUAL_FEE_75,
  603.                     CTC.SB_ITEM_TYPE_CODE,
  604.                     CTC.SB_FIN_CLASS_TYPE_CODE,
  605.                     ITYPE.ITEM_VIEW_ID,
  606.                     ORIGINAL.SB_MBS_COST_START,
  607.                     CTC.SB_APPLY_CONING_RULES,
  608.                     ORIGINAL.SB_DERIVED_NOGAP_PERCENT AS SB_DERIVED_NOGAP_PERCENT
  609.                 FROM ORIGINAL
  610.                 LEFT OUTER JOIN SB_COST_TYPE_CRITERIA CTC with (nolock) ON CTC.SB_COST_TYPE_ID = ORIGINAL.SB_COST_TYPE_ID
  611.                 LEFT OUTER JOIN SB_ITEM_TYPE ITYPE with (nolock) ON CTC.SB_ITEM_TYPE_CODE = ITYPE.SB_ITEM_TYPE_CODE
  612.                 LEFT OUTER JOIN SB_MBS_ITEM_COST ORIG_DEFAULT_ITEM_COST with (nolock)
  613.                     ON  ORIGINAL.SB_MBS_ITEM_NUMBER = ORIG_DEFAULT_ITEM_COST.SB_MBS_ITEM_NUMBER
  614.                     AND ORIG_DEFAULT_ITEM_COST.SB_COST_TYPE_ID = #MBS100.SB_COST_TYPE_ID#
  615.                     AND ORIGINAL.SB_SERVICE_DATE BETWEEN ORIG_DEFAULT_ITEM_COST.SB_MBS_COST_START AND ISNULL(ORIG_DEFAULT_ITEM_COST.SB_MBS_COST_END,'31-DEC-3100')
  616.                 WHERE ISNULL(ORIGINAL.SB_MBS_ITEM_FEE,0) > 0
  617.                 UNION ALL
  618.                 SELECT 
  619.                     <!--- ALTERNATE ITEM FEE--->   
  620.                     ORIGINAL.SB_PRIORITY,
  621.                     ALT_MIC.SB_COST_TYPE_ID,
  622.                     ALT_MIC.SB_MBS_ITEM_NUMBER,
  623.                     CASE WHEN ALT_CTC.SB_ITEM_TYPE_CODE <> 'R' THEN ALT_MIC.SB_MBS_ITEM_FEE ELSE ISNULL(ALT_MIC.SB_MBS_ITEM_FEE, 0) END AS SB_SCHEDULE_FEE,
  624.                     CASE
  625.                         WHEN ORIGINAL.SB_PRICE_OVERRIDE = 'Y' AND @ACTUAL_FEE IS NOT NULL  
  626.                             THEN @ACTUAL_FEE
  627.                         WHEN  ISNULL(ORIGINAL.SB_PRICE_OVERRIDE, 'N') = 'N' AND ALT_MIC.SB_MBS_ITEM_FEE IS NOT NULL and ALT_MIC.SB_ITEM_QTY IS NULL
  628.                             THEN ALT_MIC.SB_MBS_ITEM_FEE * ORIGINAL.SB_ITEM_QTY
  629.                         WHEN  ISNULL(ORIGINAL.SB_PRICE_OVERRIDE, 'N') = 'N' AND ALT_MIC.SB_MBS_ITEM_FEE IS NOT NULL and ALT_MIC.SB_ITEM_QTY IS NOT NULL
  630.                             THEN ALT_MIC.SB_MBS_ITEM_FEE
  631.                         WHEN ALT_MIC.SB_MBS_ITEM_FEE IS NOT NULL AND ALT_MIC.SB_MBS_ITEM_FEE > 0
  632.                             THEN ALT_MIC.SB_MBS_ITEM_FEE
  633.                         WHEN ORIGINAL.SCHEMEPROVIDER_NO_GAPS = 1
  634.                             THEN ALT_DEFAULT_ITEM_COST.SB_MBS_ITEM_FEE
  635.                         ELSE 0 END AS SB_ACTUAL_FEE,
  636.                     ALT_MIC.SB_MBS_ITEM_FEE_75 AS SB_ACTUAL_FEE_75,
  637.                     ALT_CTC.SB_ITEM_TYPE_CODE,
  638.                     ALT_CTC.SB_FIN_CLASS_TYPE_CODE,
  639.                     ALT_ITYPE.ITEM_VIEW_ID,
  640.                     ALT_MIC.SB_MBS_COST_START,
  641.                     ALT_CTC.SB_APPLY_CONING_RULES,
  642.                     ALT_MIC.SB_DERIVED_NOGAP_PERCENT AS SB_DERIVED_NOGAP_PERCENT
  643.                 FROM ORIGINAL
  644.                 INNER JOIN SB_MBS_ITEM ALTERNATE_CODE with (nolock) ON ALTERNATE_CODE.SB_ALTERNATE_ITEM_CODE = @ITEM_NUMBER
  645.                 INNER JOIN  SB_MBS_ITEM_COST ALT_MIC with (nolock) ON ALT_MIC.SB_MBS_ITEM_NUMBER = ALTERNATE_CODE.SB_MBS_ITEM_NUMBER
  646.                     AND ORIGINAL.SB_DEFAULT_COST_TYPE_ID = ALT_MIC.SB_COST_TYPE_ID
  647.                     AND ORIGINAL.SB_SERVICE_DATE BETWEEN ALT_MIC.SB_MBS_COST_START AND ISNULL(ALT_MIC.SB_MBS_COST_END, DATEADD(Y,100,GETDATE()))
  648.                 INNER JOIN SB_COST_TYPE_CRITERIA ALT_CTC with (nolock) ON ALT_CTC.SB_COST_TYPE_ID = ALT_MIC.SB_COST_TYPE_ID
  649.                 INNER JOIN SB_ITEM_TYPE ALT_ITYPE with (nolock) ON ALT_CTC.SB_ITEM_TYPE_CODE = ALT_ITYPE.SB_ITEM_TYPE_CODE
  650.                 LEFT OUTER JOIN SB_MBS_ITEM_COST ALT_DEFAULT_ITEM_COST with (nolock)
  651.                     ON  ALT_DEFAULT_ITEM_COST.SB_MBS_ITEM_NUMBER = ALT_MIC.SB_MBS_ITEM_NUMBER
  652.                     AND ALT_DEFAULT_ITEM_COST.SB_COST_TYPE_ID = #MBS100.SB_COST_TYPE_ID#
  653.                     AND ORIGINAL.SB_SERVICE_DATE BETWEEN ALT_DEFAULT_ITEM_COST.SB_MBS_COST_START AND ISNULL(ALT_DEFAULT_ITEM_COST.SB_MBS_COST_END,'31-DEC-3100')
  654.                 WHERE ALT_CTC.SB_FIN_CLASS_TYPE_CODE = ORIGINAL.SB_FIN_CLASS_TYPE_CODE
  655.                     AND ISNULL(ALT_MIC.SB_MBS_ITEM_FEE,0) > 0
  656.                 UNION ALL
  657.                 SELECT 
  658.                     <!--- DEFAULT ALTERNATE ITEM FEE--->       
  659.                     ORIGINAL.SB_PRIORITY,
  660.                     DEF_ALT_MIC.SB_COST_TYPE_ID,
  661.                     DEF_ALT_MIC.SB_MBS_ITEM_NUMBER,
  662.                     CASE WHEN DEF_ALT_CTC.SB_ITEM_TYPE_CODE <> 'R' THEN DEF_ALT_MIC.SB_MBS_ITEM_FEE ELSE ISNULL(DEF_ALT_MIC.SB_MBS_ITEM_FEE, 0) END AS SB_SCHEDULE_FEE,
  663.                     CASE
  664.                         WHEN ORIGINAL.SB_PRICE_OVERRIDE = 'Y' AND @ACTUAL_FEE IS NOT NULL  
  665.                             THEN @ACTUAL_FEE
  666.                         WHEN  ISNULL(ORIGINAL.SB_PRICE_OVERRIDE, 'N') = 'N' AND DEF_ALT_MIC.SB_MBS_ITEM_FEE IS NOT NULL and DEF_ALT_MIC.SB_ITEM_QTY IS NULL
  667.                             THEN DEF_ALT_MIC.SB_MBS_ITEM_FEE * ORIGINAL.SB_ITEM_QTY
  668.                         WHEN  ISNULL(ORIGINAL.SB_PRICE_OVERRIDE, 'N') = 'N' AND DEF_ALT_MIC.SB_MBS_ITEM_FEE IS NOT NULL and DEF_ALT_MIC.SB_ITEM_QTY IS NOT NULL
  669.                             THEN DEF_ALT_MIC.SB_MBS_ITEM_FEE
  670.                         WHEN DEF_ALT_MIC.SB_MBS_ITEM_FEE IS NOT NULL AND DEF_ALT_MIC.SB_MBS_ITEM_FEE > 0
  671.                             THEN DEF_ALT_MIC.SB_MBS_ITEM_FEE
  672.                         WHEN ORIGINAL.SCHEMEPROVIDER_NO_GAPS = 1
  673.                             THEN ALT_DEFAULT_ITEM_COST2.SB_MBS_ITEM_FEE
  674.                         ELSE 0 END AS SB_ACTUAL_FEE,
  675.                     DEF_ALT_MIC.SB_MBS_ITEM_FEE_75 AS SB_ACTUAL_FEE_75,
  676.                     DEF_ALT_CTC.SB_ITEM_TYPE_CODE,
  677.                     DEF_ALT_CTC.SB_FIN_CLASS_TYPE_CODE,
  678.                     DEF_ALT_ITYPE.ITEM_VIEW_ID,
  679.                     DEF_ALT_MIC.SB_MBS_COST_START,
  680.                     DEF_ALT_CTC.SB_APPLY_CONING_RULES,
  681.                     DEF_ALT_MIC.SB_DERIVED_NOGAP_PERCENT AS SB_DERIVED_NOGAP_PERCENT
  682.                 FROM ORIGINAL
  683.                 INNER JOIN SB_MBS_ITEM_COST DEF_ALT_MIC with (nolock) ON DEF_ALT_MIC.SB_MBS_ITEM_NUMBER = ORIGINAL.SB_MBS_ITEM_NUMBER
  684.                     AND ORIGINAL.SB_DEFAULT_COST_TYPE_ID = DEF_ALT_MIC.SB_COST_TYPE_ID
  685.                     AND ORIGINAL.SB_SERVICE_DATE BETWEEN DEF_ALT_MIC.SB_MBS_COST_START AND ISNULL(DEF_ALT_MIC.SB_MBS_COST_END, DATEADD(Y,100,GETDATE()))
  686.                 INNER JOIN SB_COST_TYPE_CRITERIA DEF_ALT_CTC with (nolock) ON DEF_ALT_CTC.SB_COST_TYPE_ID = DEF_ALT_MIC.SB_COST_TYPE_ID
  687.                 INNER JOIN SB_ITEM_TYPE DEF_ALT_ITYPE with (nolock) ON DEF_ALT_CTC.SB_ITEM_TYPE_CODE = DEF_ALT_ITYPE.SB_ITEM_TYPE_CODE
  688.                 LEFT OUTER JOIN SB_MBS_ITEM_COST ALT_DEFAULT_ITEM_COST2 with (nolock)
  689.                     ON  ALT_DEFAULT_ITEM_COST2.SB_MBS_ITEM_NUMBER = ORIGINAL.SB_MBS_ITEM_NUMBER
  690.                     AND ALT_DEFAULT_ITEM_COST2.SB_COST_TYPE_ID = #MBS100.SB_COST_TYPE_ID#
  691.                     AND ORIGINAL.SB_SERVICE_DATE BETWEEN ALT_DEFAULT_ITEM_COST2.SB_MBS_COST_START AND ISNULL(ALT_DEFAULT_ITEM_COST2.SB_MBS_COST_END,'31-DEC-3100')
  692.                 WHERE DEF_ALT_CTC.SB_FIN_CLASS_TYPE_CODE = ORIGINAL.SB_FIN_CLASS_TYPE_CODE
  693.                     AND DEF_ALT_MIC.SB_MBS_ITEM_FEE > 0
  694.                 ) FEES
  695.             ORDER BY SB_PRIORITY, SB_MBS_COST_START DESC
  696.         </cfquery>
  697.        
  698.         <cfif ALTERNATE_ITEM_FEE.RECORDCOUNT GT 0>
  699.             <cfquery name="UPDATE_ITEM_COST" datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#">
  700.                 UPDATE SB_CONING_RUN
  701.                 SET SB_SCHEDULE_FEE = <cfif ALTERNATE_ITEM_FEE.SB_SCHEDULE_FEE eq "">NULL<cfelse>#ALTERNATE_ITEM_FEE.SB_SCHEDULE_FEE#</cfif>,
  702.                     SB_ACTUAL_FEE = <cfif ALTERNATE_ITEM_FEE.SB_ACTUAL_FEE eq "">NULL<cfelse>#ALTERNATE_ITEM_FEE.SB_ACTUAL_FEE#</cfif>,
  703.                     SB_ACTUAL_FEE_75 = <cfif ALTERNATE_ITEM_FEE.SB_ACTUAL_FEE_75 eq "">NULL<cfelse>#ALTERNATE_ITEM_FEE.SB_ACTUAL_FEE_75#</cfif>,
  704.                     SB_DERIVED_NOGAP_PERCENT = <cfif ALTERNATE_ITEM_FEE.SB_DERIVED_NOGAP_PERCENT eq "">NULL<cfelse>#ALTERNATE_ITEM_FEE.SB_DERIVED_NOGAP_PERCENT#</cfif>,
  705.                     SB_ITEM_NUMBER = '#ALTERNATE_ITEM_FEE.SB_MBS_ITEM_NUMBER#',
  706.                     SB_CONING_ITEM_TYPE = '#ALTERNATE_ITEM_FEE.SB_ITEM_TYPE_CODE#',
  707.                     SB_APPLY_CONING = <cfif ALTERNATE_ITEM_FEE.SB_APPLY_CONING_RULES eq "">NULL<cfelse>'#ALTERNATE_ITEM_FEE.SB_APPLY_CONING_RULES#'</cfif>
  708.                 FROM SB_CONING_RUN
  709.                 WHERE SB_CONING_ID = #MISSING_COST.SB_CONING_ID#
  710.             </cfquery>
  711.            
  712.             <cfquery name="UPDATE_INVOICE_ITEM_VIEW" datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#">
  713.                 UPDATE SB_INVOICE_ITEM
  714.                 SET SB_INVOICE_ITEM.ITEM_VIEW_ID = #ALTERNATE_ITEM_FEE.ITEM_VIEW_ID#,
  715.                     SB_INVOICE_ITEM.SB_MBS_ITEM_NUMBER = SB_CONING_RUN.SB_ITEM_NUMBER,
  716.                     SB_INVOICE_ITEM.SB_ORIG_MBS_ITEM_NUMBER = ISNULL(SB_INVOICE_ITEM.SB_ORIG_MBS_ITEM_NUMBER,SB_INVOICE_ITEM.SB_MBS_ITEM_NUMBER),
  717.                     SB_INVOICE_ITEM.SB_UPDATED_BY =
  718.                         <cfif isDefined("SESSION.SB_LOGIN_CODE")>
  719.                             <cfqueryparam cfsqltype="cf_sql_varchar" value="#SESSION.SB_LOGIN_CODE#">
  720.                         <cfelse>
  721.                             'pbrc-ae:ACL'
  722.                         </cfif>,
  723.                     SB_INVOICE_ITEM.SB_UPDATE_DATE = getDate(),
  724.                     SB_UPDATED_FROM = 'apply_coning_logic UPDATE_INVOICE_ITEM_VIEW'
  725.                     FROM
  726.                     SB_CONING_RUN
  727.                     INNER JOIN SB_INVOICE_ITEM ON SB_CONING_RUN.SB_CONING_ITEM_ID = SB_INVOICE_ITEM.SB_INVOICE_ITEM_ID
  728.                     INNER JOIN SB_INVOICE with (nolock) ON SB_INVOICE_ITEM.SB_INVOICE_ID = SB_INVOICE.SB_INVOICE_ID
  729.                 WHERE
  730.                     SB_CONING_ID = #MISSING_COST.SB_CONING_ID#
  731.             </cfquery>
  732.         </cfif>
  733.      </cfoutput> <!--- Finished assessing and transforming CONING_RUN records in MISSING_COST --->
  734.      
  735.     <!--- If another invoice already exists with the right item type, move the invoice --->
  736.     <!--- Check to see if there is an existing invoice matching the episode, provider, specialty and item type and batch--->
  737.     <cfquery datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#" name="MISSING_COST_RECORDS">
  738.          SELECT DISTINCT
  739.             CR.SB_CONING_ID,
  740.             I.SB_INVOICE_ID AS ORIG_INV_ID,
  741.             CASE WHEN MAX(REL_INV.SB_INVOICE_ID) IS NOT NULL AND I.SB_INVOICE_ID <> MAX(REL_INV.SB_INVOICE_ID) THEN 1 ELSE 0 END AS REL_INV_FOUND,
  742.             MAX(REL_INV.SB_INVOICE_ID) AS REL_INV_ID
  743.         FROM
  744.             SB_CONING_RUN CR
  745.             INNER JOIN SB_INVOICE_ITEM II with (nolock) ON II.SB_INVOICE_ITEM_ID = CR.SB_CONING_ITEM_ID
  746.             INNER JOIN SB_INVOICE I with (nolock) ON I.SB_INVOICE_ID = II.SB_INVOICE_ID
  747.             LEFT OUTER JOIN SB_INVOICE REL_INV with (nolock) ON REL_INV.SB_EPISODE_NUMBER = I.SB_EPISODE_NUMBER
  748.                 AND REL_INV.SB_INVOICE_DATE IS NULL
  749.                 AND REL_INV.SB_ITEM_TYPE_CODE LIKE CR.SB_CONING_ITEM_TYPE
  750.                 AND REL_INV.SB_PROVIDER_NUMBER LIKE CR.SB_CONING_PROVIDER
  751.                 AND REL_INV.SB_SPECIALTY_CODE LIKE CR.SB_CONING_SPECIALTY_CODE
  752.             LEFT OUTER JOIN SB_INVOICE_ITEM REL_ITEM with (nolock) ON REL_INV.SB_INVOICE_ID = REL_ITEM.SB_INVOICE_ID   
  753.                 AND REL_ITEM.SB_INV_BATCH_ID = II.SB_INV_BATCH_ID
  754.         WHERE
  755.             CR.SB_CONING_ID IN (#ORG_MISSING_COST_LIST_CONING_ID#)
  756.         GROUP BY CR.SB_CONING_ID,I.SB_INVOICE_ID
  757.     </cfquery>
  758.      
  759.     <cfloop query="MISSING_COST_RECORDS">
  760.         <cfset ATTACHED_INVOICE_ID = MISSING_COST_RECORDS.ORIG_INV_ID> 
  761.         <cfif MISSING_COST_RECORDS.REL_INV_FOUND eq 1>
  762.             <!--- If a relevant invoice exist, move the item to the new invoice --->
  763.             <cfset ATTACHED_INVOICE_ID = MISSING_COST_RECORDS.REL_INV_ID>  
  764.         <cfelse>
  765.             <!--- If no relavant invoice was found, check if there are any item type conflicts --->
  766.             <cfquery datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#" name="ITEM_TYPE_CONFLICTS">
  767.                 SELECT DISTINCT INV.SB_EPISODE_NUMBER,
  768.                 CR.SB_CONING_ITEM_TYPE
  769.                 FROM SB_CONING_RUN CR
  770.                 INNER JOIN SB_INVOICE INV ON INV.SB_INVOICE_ID = CR.SB_CONING_INVOICE_ID
  771.                 INNER JOIN SB_INVOICE_ITEM INVI on INVI.SB_INVOICE_ID = CR.SB_CONING_INVOICE_ID
  772.                 WHERE INV.SB_INVOICE_ID IN (
  773.                     SELECT INVOICE.SB_INVOICE_ID
  774.                     FROM SB_INVOICE INVOICE
  775.                     INNER JOIN SB_INVOICE_ITEM ITEM ON ITEM.SB_INVOICE_ID = INVOICE.SB_INVOICE_ID
  776.                     INNER JOIN SB_MBS_ITEM MBSITEM ON MBSITEM.SB_MBS_ITEM_NUMBER = ITEM.SB_MBS_ITEM_NUMBER
  777.                     WHERE INVOICE.SB_INVOICE_ID = #MISSING_COST_RECORDS.ORIG_INV_ID#
  778.                     GROUP BY INVOICE.SB_INVOICE_ID HAVING COUNT(DISTINCT MBSITEM.SB_ITEM_TYPE_CODE) > 1
  779.                 )
  780.             </cfquery>
  781.            
  782.             <cfif ITEM_TYPE_CONFLICTS.recordcount neq 0>
  783.                 <!--- If item type conflict within invoice is found, create new invoice and move the items, update the invoice_ref  --->
  784.                 <!--- Calculate the new invoice number --->
  785.                 <cfinvoke component="pbrc-ae.components.Invoice" method="PreInvoiceNumber" returnvariable="VARIABLES.INVOICE_NUMBER">
  786.                 <cfinvokeargument name="SB_EPISODE_NUMBER" value='#ITEM_TYPE_CONFLICTS.SB_EPISODE_NUMBER#'/>
  787.                 </cfinvoke>
  788.                
  789.                 <cfquery datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#" name="INSERT_INVOICE">
  790.                     set nocount on
  791.                     INSERT INTO SB_INVOICE
  792.                     (
  793.                         SB_EPISODE_NUMBER,
  794.                         SB_INVOICE_TYPE_CODE,
  795.                         SB_INVOICE_NUMBER,
  796.                         SB_INVOICE_DATE,
  797.                         SB_INVOICE_RECEIVED,
  798.                         SB_PROVIDER_NUMBER,
  799.                         SB_PATIENT_MRN,
  800.                         SB_HL7_SOURCE,
  801.                         SB_INVOICE_STATUS_CODE,
  802.                         SB_DEBTOR_ID,
  803.                         SB_BILL_MECH_CODE,
  804.                         SB_INVOICE_TOTAL_AMT,
  805.                         SB_SPECIALTY_CODE,
  806.                         SB_CHART_OF_ACC_ID,
  807.                         SB_ITEM_TYPE_CODE,
  808.                         SB_SOURCE_CODE,
  809.                         SB_UPDATED_BY,
  810.                         SB_UPDATE_DATE,
  811.                         SB_UPDATED_FROM
  812.                     )
  813.                     SELECT
  814.                         SB_EPISODE_NUMBER,
  815.                         SB_INVOICE_TYPE_CODE,
  816.                         '#VARIABLES.INVOICE_NUMBER#',
  817.                         null,
  818.                         SB_INVOICE_RECEIVED,
  819.                         SB_PROVIDER_NUMBER,
  820.                         SB_PATIENT_MRN,
  821.                         SB_HL7_SOURCE,
  822.                         SB_INVOICE_STATUS_CODE,
  823.                         SB_DEBTOR_ID,
  824.                         SB_BILL_MECH_CODE,
  825.                         SB_INVOICE_TOTAL_AMT,
  826.                         SB_SPECIALTY_CODE,
  827.                         SB_CHART_OF_ACC_ID,
  828.                         SB_ITEM_TYPE_CODE,
  829.                         SB_SOURCE_CODE,
  830.                         SB_UPDATED_BY,
  831.                         GETDATE(),
  832.                         'apply_coning_logic INSERT_INVOICE'
  833.                     FROM SB_INVOICE
  834.                     WHERE SB_INVOICE_ID = #MISSING_COST_RECORDS.ORIG_INV_ID#
  835.                    
  836.                     select SCOPE_IDENTITY() as SB_INVOICE_ID
  837.                 </cfquery>
  838.                 <cfset ATTACHED_INVOICE_ID = INSERT_INVOICE.SB_INVOICE_ID> 
  839.             </cfif>
  840.         </cfif>
  841.                
  842.             <!--- Copy invoice error flags from the original; only if the attached invoice_id does not match the original invoice_id  --->
  843.             <cfquery datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#" name="copy_invoice_flags">
  844.                 INSERT INTO SB_INVOICE_ERROR
  845.                 (SB_INVOICE_FLAG_CODE,
  846.                 SB_INVOICE_ID,
  847.                 SB_ERROR_DATE,
  848.                 SB_FINALISED_DATE,
  849.                 SB_ERROR_COMMENT,
  850.                 SB_UPDATED_BY,
  851.                 SB_UPDATE_DATE)
  852.                 SELECT SB_INVOICE_FLAG_CODE,
  853.                 <CFQUERYPARAM CFSQLTYPE="CF_SQL_NUMERIC" VALUE="#ATTACHED_INVOICE_ID#">,
  854.                 SB_ERROR_DATE,
  855.                 SB_FINALISED_DATE,
  856.                 SB_ERROR_COMMENT,
  857.                 SB_UPDATED_BY,
  858.                 SB_UPDATE_DATE
  859.                 FROM SB_INVOICE_ERROR
  860.                 WHERE 1=1
  861.                 AND SB_INVOICE_ID = <cfqueryparam cfsqltype="cf_sql_numeric" value="#MISSING_COST_RECORDS.ORIG_INV_ID#">
  862.                 AND <cfqueryparam cfsqltype="cf_sql_numeric" value="#MISSING_COST_RECORDS.ORIG_INV_ID#"> <> <cfqueryparam cfsqltype="cf_sql_numeric" value="#ATTACHED_INVOICE_ID#">
  863.             </cfquery>
  864.    
  865.             <!--- Update the INVOICE ITEM's Linked INVOICE details; only if the original invoice_id does not match the attached invoice_id --->
  866.             <cfquery name="UPDATE_ITEM_INVOICE_ID" datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#">
  867.                 UPDATE SB_INVOICE_ITEM
  868.                     SET SB_INVOICE_ID =  <cfqueryparam cfsqltype="cf_sql_numeric" value="#ATTACHED_INVOICE_ID#">,
  869.                     SB_INVOICE_ITEM.SB_ORIG_MBS_ITEM_NUMBER = ISNULL(SB_INVOICE_ITEM.SB_ORIG_MBS_ITEM_NUMBER,SB_INVOICE_ITEM.SB_MBS_ITEM_NUMBER),
  870.                     SB_INVOICE_ITEM.SB_UPDATED_BY =
  871.                     <cfif isDefined("SESSION.SB_LOGIN_CODE")>
  872.                         <cfqueryparam cfsqltype="cf_sql_varchar" value="#SESSION.SB_LOGIN_CODE#">
  873.                     <cfelse>
  874.                         'pbrc-ae:ACL'
  875.                     </cfif>,
  876.                     SB_INVOICE_ITEM.SB_UPDATE_DATE = getDate(),
  877.                     SB_UPDATED_FROM = 'apply_coning_logic UPDATE_ITEM_INVOICE_ID'
  878.                 FROM
  879.                     SB_CONING_RUN
  880.                     INNER JOIN SB_INVOICE_ITEM ON SB_CONING_RUN.SB_CONING_ITEM_ID = SB_INVOICE_ITEM.SB_INVOICE_ITEM_ID
  881.                     INNER JOIN SB_INVOICE with (nolock) ON SB_INVOICE_ITEM.SB_INVOICE_ID = SB_INVOICE.SB_INVOICE_ID
  882.                 WHERE
  883.                     SB_CONING_ID = <cfqueryparam cfsqltype="cf_sql_numeric" value="#MISSING_COST_RECORDS.SB_CONING_ID#">
  884.                     AND SB_CONING_INVOICE_ID <> <cfqueryparam cfsqltype="cf_sql_numeric" value="#ATTACHED_INVOICE_ID#">
  885.             </cfquery>
  886.                        
  887.             <!--- Delete invoice error flags of orphaned invoices;the invoices with no items --->
  888.             <cfquery datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#" name="DELETE_INVOICE_FLAG">
  889.                 DELETE INV_ERROR
  890.                 FROM SB_INVOICE_ERROR INV_ERROR
  891.                 LEFT OUTER JOIN SB_INVOICE_ITEM ITEM with (nolock) ON ITEM.SB_INVOICE_ID = INV_ERROR.SB_INVOICE_ID
  892.                 LEFT OUTER JOIN SB_INVOICE_ITEM_DELETED ITEM_DEL with (nolock) ON ITEM_DEL.SB_INVOICE_ID = INV_ERROR.SB_INVOICE_ID
  893.                 WHERE INV_ERROR.SB_INVOICE_ID = <cfqueryparam cfsqltype="cf_sql_numeric" value="#MISSING_COST_RECORDS.ORIG_INV_ID#">
  894.                 AND ITEM.SB_INVOICE_ITEM_ID IS NULL
  895.                 AND ITEM_DEL.SB_INVOICE_ITEM_ID2 IS NULL
  896.             </cfquery>
  897.            
  898.             <!--- Delete orphaned invoices; the invoices with no items---> 
  899.             <cfquery datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#" name="DELETE_INVOICE">
  900.                 DELETE INV
  901.                 FROM SB_INVOICE INV
  902.                 LEFT OUTER JOIN SB_INVOICE_ITEM ITEM with (nolock) ON ITEM.SB_INVOICE_ID = INV.SB_INVOICE_ID
  903.                 LEFT OUTER JOIN SB_INVOICE_ITEM_DELETED ITEM_DEL with (nolock) ON ITEM_DEL.SB_INVOICE_ID = INV.SB_INVOICE_ID
  904.                 WHERE INV.SB_INVOICE_ID = <cfqueryparam cfsqltype="cf_sql_numeric" value="#MISSING_COST_RECORDS.ORIG_INV_ID#">
  905.                 AND ITEM.SB_INVOICE_ITEM_ID IS NULL
  906.                 AND ITEM_DEL.SB_INVOICE_ITEM_ID2 IS NULL
  907.             </cfquery>
  908.            
  909.             <!--- Update the linked invoice_id reference of CONING_RUN record --->
  910.             <cfquery datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#" name="UPDATE_INVOICE_REF">
  911.                 UPDATE SB_CONING_RUN
  912.                 SET SB_CONING_INVOICE_ID = <cfqueryparam cfsqltype="cf_sql_numeric" value="#ATTACHED_INVOICE_ID#">
  913.                 FROM SB_CONING_RUN
  914.                 WHERE 1=1
  915.                 AND SB_CONING_ID = <cfqueryparam cfsqltype="cf_sql_numeric" value="#MISSING_COST_RECORDS.SB_CONING_ID#">
  916.                 AND SB_CONING_INVOICE_ID <> <cfqueryparam cfsqltype="cf_sql_numeric" value="#ATTACHED_INVOICE_ID#">
  917.             </cfquery>
  918.     </cfloop>
  919.     <!--- Update the invoice type if the attached INVOICE needs updating --->
  920.     <cfquery name="UPDATE_INVOICE_TYPE" datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#">
  921.         UPDATE INV
  922.             SET INV.SB_ITEM_TYPE_CODE = MBSITEM.SB_ITEM_TYPE_CODE,
  923.             INV.SB_UPDATED_BY =
  924.                 <cfif isDefined("SESSION.SB_LOGIN_CODE") and SESSION.SB_LOGIN_CODE neq "">
  925.                     <cfqueryparam cfsqltype="cf_sql_varchar" value="#SESSION.SB_LOGIN_CODE#">
  926.                 <cfelse>
  927.                     'pbrc-ae:ACL'
  928.                 </cfif>,
  929.             INV.SB_UPDATE_DATE = getDate()
  930.         FROM SB_INVOICE INV
  931.         INNER JOIN SB_CONING_RUN CR ON INV.SB_INVOICE_ID = CR.SB_CONING_INVOICE_ID
  932.         INNER JOIN SB_INVOICE_ITEM ITEM ON ITEM.SB_INVOICE_ITEM_ID = CR.SB_CONING_ITEM_ID
  933.         INNER JOIN SB_MBS_ITEM MBSITEM ON MBSITEM.SB_MBS_ITEM_NUMBER = ITEM.SB_MBS_ITEM_NUMBER
  934.         WHERE 1=1
  935.         AND CR.SB_CONING_ID IN (#ORG_MISSING_COST_LIST_CONING_ID#)  
  936.         AND MBSITEM.SB_ITEM_TYPE_CODE NOT LIKE INV.SB_ITEM_TYPE_CODE
  937.     </cfquery>
  938.  
  939.     <!--- Review the bill mech all coning records with missing cost --->
  940.     <cfquery name="MISSING_COST_REVIEW_BILL_MECH" datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#">
  941.         SELECT SB_INVOICE_TYPE.SB_DISABLE_COSTING, INV.SB_INVOICE_ID
  942.         FROM SB_CONING_RUN CR
  943.         INNER JOIN SB_INVOICE INV ON INV.SB_INVOICE_ID = CR.SB_CONING_INVOICE_ID
  944.         INNER JOIN SB_INVOICE_TYPE with (nolock) ON INV.SB_INVOICE_TYPE_CODE = SB_INVOICE_TYPE.SB_INVOICE_TYPE_CODE
  945.         WHERE CR.SB_CONING_ID IN (#ORG_MISSING_COST_LIST_CONING_ID#)  
  946.     </cfquery>
  947.    
  948.     <cfoutput query="MISSING_COST_REVIEW_BILL_MECH">       
  949.         <cfinvoke component="pbrc-ae.components.allocate_bill_mech"
  950.             method="allocate"
  951.             returnvariable="none">
  952.                 <cfinvokeargument name="sb_invoice_id" value="#MISSING_COST_REVIEW_BILL_MECH.SB_INVOICE_ID#">
  953.             <cfif MISSING_COST_REVIEW_BILL_MECH.SB_DISABLE_COSTING eq 'Y'>
  954.                 <cfinvokeargument name="apply_bill_mech" value="N">
  955.             </cfif>
  956.         </cfinvoke>
  957.      </cfoutput>
  958.      
  959.     <!--- Update the Bill Mech related values in the CONING_RUN record --->
  960.     <cfquery name="UPDATE_ITEM_COST" datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#">
  961.         UPDATE SB_CONING_RUN
  962.         SET SB_CONING_STATUS_CODE = MECH.SB_INITIAL_ITEM_STATUS,
  963.         SB_CONING_BILL_MECH =
  964.         CASE WHEN EPISODE.SB_EPISODE_TYPE_CODE = 'P'
  965.          THEN 'P'
  966.         WHEN INVOICE.SB_BILL_MECH_CODE = 'B' AND EPISODE.SB_MC_FORM_SIGNED = 'Y' AND SB_MEDICARE_IMAGE_FILENAME IS NOT NULL THEN
  967.             'B'
  968.         ELSE
  969.             ISNULL(INVOICE.SB_RERAISE_BILL_MECH, CRITERIA.SB_BILL_MECH_CODE)
  970.         END
  971.         FROM SB_CONING_RUN
  972.         INNER JOIN SB_INVOICE INVOICE with (nolock) ON INVOICE.SB_INVOICE_ID =  SB_CONING_RUN.SB_CONING_INVOICE_ID
  973.         INNER JOIN SB_PATIENT_EPISODE EPISODE with (nolock) ON EPISODE.SB_EPISODE_NUMBER = INVOICE.SB_EPISODE_NUMBER
  974.         LEFT OUTER JOIN SB_COST_TYPE_CRITERIA CRITERIA with (nolock) ON CRITERIA.SB_COST_TYPE_CRITERIA_ID = INVOICE.SB_COST_TYPE_CRITERIA_ID
  975.         LEFT OUTER JOIN SB_BILLING_MECHANISM MECH  with (nolock) ON IsNull(Invoice.SB_RERAISE_BILL_MECH, CRITERIA.SB_BILL_MECH_CODE) = MECH.SB_BILL_MECH_CODE
  976.         WHERE SB_CONING_ID IN (#ORG_MISSING_COST_LIST_CONING_ID#)  
  977.     </cfquery>
  978.      
  979.     <!--- Flag error status if scheduled fee is still missing --->
  980.     <cfquery name="STILL_MISSING" datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#">
  981.          SELECT SB_CONING_ID, SB_CONING_ITEM_ID
  982.          FROM   SB_CONING_RUN
  983.          WHERE
  984.          SB_SCHEDULE_FEE IS NULL AND
  985.          SB_APPLY_CONING = 'N' AND
  986.          SB_CONING_RUN_NO = <cfqueryparam cfsqltype="cf_sql_varchar" value="#VARIABLES.unique_id#">
  987.     </cfquery>
  988.  
  989.     <cfoutput query="STILL_MISSING">
  990.          <cfset ARGUMENTS.coning_error_count = ARGUMENTS.coning_error_count + 1>
  991.          <cfset VARIABLES.ERROR_TEXT = 'Item Cost could not be determined'>
  992.  
  993.          <cfquery datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#" name="UPDATE_CONING_RECORDS">
  994.             UPDATE  SB_CONING_RUN
  995.             SET     SB_CONING_STATUS_CODE = 'ER',
  996.                     SB_CONING_ERROR = LEFT('#VARIABLES.ERROR_TEXT#',3000)
  997.             WHERE   SB_CONING_ITEM_ID = <cfqueryparam cfsqltype="cf_sql_numeric" value="#STILL_MISSING.SB_CONING_ITEM_ID#">
  998.                 AND SB_CONING_ID = <cfqueryparam cfsqltype="cf_sql_numeric" value="#STILL_MISSING.SB_CONING_ID#">
  999.          </cfquery>
  1000.     </cfoutput>
  1001. </cfif><!--- END default cost mapping transformation --->
  1002.  
  1003.  
  1004.   <!---
  1005.     Find any AMA  records that have a zero cost and update the coning_record with an error message --->
  1006.   <cfquery datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#" name="MISSING_COST_CRITERIA">
  1007.      SELECT
  1008.         SB_CONING_ID, SB_CONING_ITEM_ID
  1009.      FROM
  1010.         SB_CONING_RUN   INNER JOIN
  1011.         SB_MBS_ITEM with (nolock) ON
  1012.             SB_CONING_RUN.SB_ITEM_NUMBER  = SB_MBS_ITEM.SB_MBS_ITEM_NUMBER
  1013.      WHERE
  1014.      SB_SCHEDULE_FEE = 0 AND
  1015.      (SB_ACTUAL_FEE = 0 OR SB_ACTUAL_FEE IS NULL) AND
  1016.      SB_MBS_ITEM.SB_ITEM_TYPE_CODE = 'A' AND
  1017.      SB_CONING_RUN_NO = <cfqueryparam cfsqltype="cf_sql_varchar" value="#VARIABLES.unique_id#">
  1018.  </cfquery>
  1019.  
  1020.  <cfif MISSING_COST_CRITERIA.RECORDCOUNT NEQ 0>
  1021.  
  1022.      <cfoutput query="MISSING_COST_CRITERIA">
  1023.  
  1024.          <cfset ARGUMENTS.coning_error_count = ARGUMENTS.coning_error_count + 1>
  1025.          <cfset VARIABLES.ERROR_TEXT = 'The AMA Item Price needs to be over written.'>
  1026.  
  1027.          <cfquery datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#" name="UPDATE_CONING_RECORDS">
  1028.          UPDATE SB_CONING_RUN
  1029.             SET
  1030.                 SB_CONING_STATUS_CODE = 'ER',
  1031.                 SB_CONING_ERROR = LEFT('#VARIABLES.ERROR_TEXT#',3000)
  1032.             WHERE
  1033.                 SB_CONING_ID = <cfqueryparam cfsqltype="cf_sql_numeric" value="#MISSING_COST_CRITERIA.SB_CONING_ID#">
  1034.          </cfquery>
  1035.      </cfoutput>
  1036.  </cfif>
  1037.  
  1038.  
  1039.  
  1040.  
  1041.  
  1042. <!--- .............................................. --->
  1043. <!--- Edit checks originally specified by Imaging are included here.
  1044. These are not so much coning rules as sensible edits.
  1045.  
  1046.  
  1047. srt, 04-dec-2007 added request date
  1048. --->
  1049.  
  1050. <cfquery datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#" name="EditChecks">
  1051.     SELECT  SB_CONING_RUN.*,
  1052.             Case when sb_mbs_item.sb_mbs_item_number between '65060' and '79999'
  1053.                 then 'P'
  1054.              when sb_mbs_item.sb_hicol_service_type = 'O'
  1055.                 then 'O'
  1056.              when SB_HIC_SPECIALTY.SB_HIC_SPECIALIST = 'Y'
  1057.                 then 'S'
  1058.              else 'O'
  1059.         end as service_type,
  1060.         EPISODE.SB_EPISODE_TYPE_CODE,
  1061.         EPISODE.SB_REFFERAL_DATE_ON_LETTER,
  1062.         EPISODE.SB_REFERRAL_DATE,
  1063.  
  1064.         ITEM.SB_REQUEST_DATE,
  1065.         ISNULL(CLINIC.SB_REQUEST_OR_REFERRAL, 'R') AS SB_REQUEST_OR_REFERRAL,
  1066.         ISNULL(SB_ITEM_TYPE.SB_SERVICE_PROV_REQUIRED, 'N') AS SB_SERVICE_PROV_REQUIRED
  1067.     FROM
  1068.         SB_CONING_RUN   INNER JOIN
  1069.         sb_mbs_item with (nolock) ON
  1070.             SB_CONING_RUN.SB_ITEM_NUMBER = sb_mbs_item.SB_MBS_ITEM_NUMBER INNER JOIN
  1071.         SB_INVOICE_ITEM ITEM  ON
  1072.             SB_CONING_RUN.SB_CONING_ITEM_ID = ITEM.SB_INVOICE_ITEM_ID INNER JOIN
  1073.         SB_INVOICE INVOICE  ON
  1074.             ITEM.SB_INVOICE_ID = INVOICE.SB_INVOICE_ID INNER JOIN
  1075.         SB_PATIENT_EPISODE EPISODE  ON
  1076.             INVOICE.SB_EPISODE_NUMBER = EPISODE.SB_EPISODE_NUMBER LEFT OUTER JOIN
  1077.         sb_provider with (nolock) ON
  1078.             SB_CONING_RUN.SB_CONING_PROVIDER = sb_provider.SB_PROVIDER_NUMBER LEFT OUTER JOIN
  1079.         SB_HIC_SPECIALTY with (nolock) ON
  1080.             sb_provider.SB_HIC_SPECIALTY_CODE = SB_HIC_SPECIALTY.SB_HIC_SPECIALTY_CODE LEFT OUTER JOIN
  1081.         SB_CLINIC CLINIC ON
  1082.             EPISODE.SB_CLINIC_CODE = CLINIC.SB_CLINIC_CODE
  1083.         LEFT OUTER JOIN SB_ITEM_TYPE on SB_ITEM_TYPE.SB_ITEM_TYPE_CODE = sb_mbs_item.SB_ITEM_TYPE_CODE
  1084.     WHERE
  1085.         SB_CONING_RUN_NO = '#VARIABLES.unique_id#'
  1086.     ORDER BY
  1087.             SB_CONING_INVOICE_ID
  1088.  
  1089. </cfquery>
  1090.  
  1091.  
  1092. <cfquery datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#" name="PatDem">
  1093.     select  SB_PATIENT_REGISTER.SB_PATIENT_SURNAME,
  1094.             SB_PATIENT_REGISTER.SB_PATIENT_OTHER_NAMES,
  1095.             SB_PATIENT_REGISTER.SB_PATIENT_SEX,
  1096.             SB_PATIENT_REGISTER.SB_PATIENT_DOB,
  1097.             LTRIM(RTRIM(SB_PATIENT_REGISTER.SB_PATIENT_MEDICARE_NUMBER)) AS SB_PATIENT_MEDICARE_NUMBER,
  1098.             LTRIM(RTRIM(SB_PATIENT_REGISTER.SB_MEDICARE_NO_REF)) AS SB_MEDICARE_NO_REF,
  1099.             SB_PATIENT_REGISTER.SB_DVA_NUMBER
  1100.     from    SB_PATIENT_REGISTER
  1101.     where   SB_PATIENT_REGISTER.sb_patient_mrn = <cfqueryparam cfsqltype="cf_sql_varchar" value="#ARGUMENTS.mrn#">
  1102.       and   SB_PATIENT_REGISTER.SB_HL7_SOURCE = <cfqueryparam cfsqltype="cf_sql_varchar" value="#ARGUMENTS.source#">
  1103.  
  1104. </cfquery>
  1105.  
  1106.  
  1107.     <cfloop query="PharmacyErrorCheck">
  1108.  
  1109.         <cfset inv_related_error = "">
  1110.  
  1111.             <!--- SCOTT....... CHECK PHARMACY DEBTOR --->
  1112.             <cfif PharmacyErrorCheck.RECORDCOUNT GT 0 AND (PharmacyErrorCheck.SB_DEBTOR_ADDRESS_1 eq ""
  1113.                or PharmacyErrorCheck.SB_DEBTOR_SUBURB eq ""
  1114.                or PharmacyErrorCheck.SB_DEBTOR_POSTCODE eq "")>
  1115.  
  1116.                   <cfquery datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#" name="UPDATE_INVOICE_ERROR">
  1117.                     UPDATE SB_INVOICE_ITEM
  1118.                         SET
  1119.                             SB_ITEM_STATUS_CODE = 'ER',
  1120.                             SB_OUTPAT_ERROR = LEFT(SB_OUTPAT_ERROR + ' Patient debtor address not fully specified.', 300),
  1121.                             SB_UPDATED_BY =
  1122.                                 <cfif isDefined("SESSION.SB_LOGIN_CODE")>
  1123.                                     <cfqueryparam cfsqltype="cf_sql_varchar" value="#SESSION.SB_LOGIN_CODE#">
  1124.                                 <cfelse>
  1125.                                     'pbrc-ae:ACL'
  1126.                                 </cfif>,
  1127.                             SB_UPDATE_DATE = getDate(),
  1128.                             SB_UPDATED_FROM = 'apply_coning_logic UPDATE_INVOICE_ERROR'
  1129.                     WHERE
  1130.                         SB_INVOICE_ID = #PharmacyErrorCheck.SB_INVOICE_ID#
  1131.                   </cfquery>
  1132.  
  1133.  
  1134.                </cfif>
  1135.  
  1136.  
  1137.     </cfloop>
  1138.  
  1139.  
  1140.  
  1141.  
  1142. <cfset this_invoice_id = "first">
  1143. <cfset coning_hist_date = '1-Jan-2200'>
  1144.  
  1145.  
  1146.  
  1147.  
  1148.  
  1149. <cfoutput query="EditChecks">
  1150.     <cfset pat_error = "">
  1151.  
  1152.     <!--- Patient demographics include an address line, suburb and postcode
  1153.           Patient sex has been specified
  1154.           Patient has first name and surname
  1155.           Medicare number is valid (bulk bill bill mech)
  1156.           Vet Affairs number exists (vet affairs bill mech)
  1157.      --->
  1158.  
  1159.     <cfif PatDem.SB_PATIENT_SURNAME neq 'FORENSIC' and PatDem.SB_PATIENT_SURNAME neq 'FORENSICS'>
  1160.         <cfif PatDem.SB_PATIENT_SURNAME eq "" or PatDem.SB_PATIENT_OTHER_NAMES eq "">
  1161.             <cfset pat_error = Trim(pat_error & " Patient name not fully specified.")>
  1162.         </cfif>
  1163.         <cfif ListfindNoCase('M,F',PatDem.SB_PATIENT_SEX) eq 0>
  1164.             <cfset pat_error = Trim(pat_error & " Patient gender not specified.")>
  1165.         </cfif>
  1166.     </cfif>
  1167.     <!---
  1168.         Medicare number is valid
  1169.  
  1170.         srt, 13-07-2006, made use of cfelseif to reduce number of errors
  1171.  
  1172.     --->
  1173.     <cfif ListfindNoCase('B,BM,AG,SC,M',EditChecks.SB_CONING_BILL_MECH) neq 0>
  1174.         <cfif PatDem.SB_PATIENT_MEDICARE_NUMBER eq "">
  1175.             <cfset pat_error = Trim(pat_error & " No medicare number.")>
  1176.         <cfelseif not isnumeric(PatDem.SB_MEDICARE_NO_REF) or PatDem.SB_MEDICARE_NO_REF eq "0" >
  1177.             <cfset pat_error = Trim(pat_error & " Invalid medicare ref no.")>
  1178.         <cfelseif isnumeric(PatDem.SB_PATIENT_MEDICARE_NUMBER) and len(PatDem.SB_PATIENT_MEDICARE_NUMBER) eq 10 >
  1179.  
  1180.             <cfset check_dig = (Mid(PatDem.SB_PATIENT_MEDICARE_NUMBER,1,1)
  1181.                              +  Mid(PatDem.SB_PATIENT_MEDICARE_NUMBER,2,1) * 3
  1182.                              +  Mid(PatDem.SB_PATIENT_MEDICARE_NUMBER,3,1) * 7
  1183.                              +  Mid(PatDem.SB_PATIENT_MEDICARE_NUMBER,4,1) * 9
  1184.                              +  Mid(PatDem.SB_PATIENT_MEDICARE_NUMBER,5,1)
  1185.                              +  Mid(PatDem.SB_PATIENT_MEDICARE_NUMBER,6,1) * 3
  1186.                              +  Mid(PatDem.SB_PATIENT_MEDICARE_NUMBER,7,1) * 7
  1187.                              +  Mid(PatDem.SB_PATIENT_MEDICARE_NUMBER,8,1) * 9) mod 10>
  1188.             <cfif check_dig neq Mid(PatDem.SB_PATIENT_MEDICARE_NUMBER,9,1)>
  1189.                 <cfset pat_error = Trim(pat_error & " Invalid medicare check digit.")>
  1190.             </cfif>
  1191.         <cfelseif NOT isnumeric(PatDem.SB_PATIENT_MEDICARE_NUMBER) >
  1192.             <cfset pat_error = Trim(pat_error & " Medicare Number invalid.")>
  1193.         </cfif>
  1194.     </cfif>
  1195.     <!---     DVA number exists --->
  1196.     <cfif ListfindNoCase('DV,DM',EditChecks.SB_CONING_BILL_MECH) neq 0>
  1197.         <cfset dva_ok = "N">
  1198.         <cfif PatDem.SB_DVA_NUMBER neq "" and PatDem.SB_PATIENT_OTHER_NAMES neq ""
  1199.                 and PatDem.SB_PATIENT_SURNAME neq "" and PatDem.SB_PATIENT_SEX neq ""
  1200.                 and PatDem.SB_PATIENT_DOB neq "">
  1201.             <cfset dva_ok = "Y">
  1202.         </cfif>
  1203.  
  1204.         <cfif dva_ok eq "N">
  1205.             <cfset pat_error = pat_error & "DVA claim requires DVA No + first name + surname + dob + gender.">
  1206.         </cfif>
  1207.  
  1208.         <cfif PatDem.SB_DVA_NUMBER neq "">
  1209.             <cfif Len(PatDem.SB_DVA_NUMBER) lt 5>
  1210.                 <cfset pat_error = Trim(pat_error & " Invalid DVA number.")>
  1211.             <cfelseif Left(UCase(PatDem.SB_DVA_NUMBER),1) lt 'A' or Left(UCase(PatDem.SB_DVA_NUMBER),1) gt 'Z'>
  1212.                 <cfset pat_error = Trim(pat_error & " Invalid DVA number.")>
  1213.             </cfif>
  1214.         </cfif>
  1215.     </cfif>
  1216.  
  1217. <!--- Find the minimum service day being coned, which will be assumed to be the date that determines which
  1218. historical version of a coning rule will be applied. --->
  1219.     <cfif APPLICATION.dateFunctions.parseDateTimeWithNULL(EditChecks.SB_SERVICE_DATE) lt APPLICATION.dateFunctions.parseDateTimeWithNULL(coning_hist_date)>
  1220.         <cfset coning_hist_date = DateFormat(EditChecks.SB_SERVICE_DATE,"dd-mmm-yyyy")>
  1221.     </cfif>
  1222.     <cfset edit_error_text = pat_error>
  1223. <!--- Billing Mechanism is defined --->
  1224.     <cfif EditChecks.SB_CONING_BILL_MECH eq "">
  1225.         <cfset EditChecks.SB_CONING_BILL_MECH = "N">
  1226.  
  1227.  
  1228.         <cfset edit_error_text = Trim(edit_error_text & " Billing Mechanism is not defined.")>
  1229.     </cfif>
  1230.  
  1231. <!---   Provider is not required for prosthesis and pharmacy items --->
  1232.     <cfif FindNoCase(EditChecks.SB_CONING_ITEM_TYPE, item_types_not_requiring_specialist) eq 0 and EditChecks.SB_SERVICE_PROV_REQUIRED eq 'Y'>
  1233.     <!---   Provider and modality combination is valid --->
  1234.         <cfif EditChecks.SB_CONING_SPEC_PROV_DEFINED EQ "N">
  1235.             <cfset edit_error_text = Trim(edit_error_text & " Provider / Specialty Combination is invalid.")>
  1236.  
  1237.         </cfif>
  1238.     <!--- Radiologist provider number exists in platypus --->
  1239.         <cfif EditChecks.SB_SERV_PROV_VALID eq "N">
  1240.             <cfset edit_error_text = Trim(edit_error_text & " Provider is invalid - " & EditChecks.SB_CONING_PROVIDER)>
  1241.         </cfif>
  1242.     </cfif>
  1243.  
  1244. <!--- Hospital is valid --->
  1245.     <cfif EditChecks.SB_CONING_HOSP_VALID eq "N">
  1246.         <cfset edit_error_text = Trim(edit_error_text & " Hospital is invalid.")>
  1247.     </cfif>
  1248. <!--- Financial Classification is valid --->
  1249.     <cfif EditChecks.SB_CONING_FIN_CLASS_VALID eq "N">
  1250.         <cfset edit_error_text = Trim(edit_error_text & " Financial Classification is invalid.")>
  1251.     </cfif>
  1252.        
  1253.     <cfif isnull(inv_related_error)>
  1254.         <cfset inv_related_error = "">
  1255.     </cfif>
  1256.  
  1257.     <cfif this_invoice_id neq EditChecks.SB_CONING_INVOICE_ID
  1258.       and (EditChecks.SB_CONING_BILL_MECH eq "DD") >
  1259.         <cfset this_invoice_id = EditChecks.SB_CONING_INVOICE_ID>
  1260.         <cfset inv_related_error = "">
  1261.         <cfquery datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#" name="DebtorAddr">
  1262.             SELECT  SB_DEBTOR.SB_DEBTOR_ADDRESS_1,
  1263.                     SB_DEBTOR.SB_DEBTOR_SUBURB,
  1264.                     SB_DEBTOR.SB_DEBTOR_POSTCODE
  1265.             FROM    dbo.SB_DEBTOR
  1266.                         INNER JOIN
  1267.                     dbo.SB_INVOICE   ON SB_DEBTOR.SB_DEBTOR_ID = SB_INVOICE.SB_DEBTOR_ID
  1268.                         INNER JOIN
  1269.                     dbo.SB_INVOICE_ITEM   ON SB_INVOICE.SB_INVOICE_ID = SB_INVOICE_ITEM.SB_INVOICE_ID
  1270.             WHERE
  1271.                 SB_INVOICE_ITEM.SB_INVOICE_ITEM_ID = <cfqueryparam cfsqltype="cf_sql_numeric" value="#EditChecks.SB_CONING_ITEM_ID#">
  1272.         </cfquery>
  1273.  
  1274.  
  1275.         <cfif DebtorAddr.RECORDCOUNT EQ 0
  1276.            or DebtorAddr.SB_DEBTOR_ADDRESS_1 eq ""
  1277.            or DebtorAddr.SB_DEBTOR_SUBURB eq ""
  1278.            or DebtorAddr.SB_DEBTOR_POSTCODE eq "">
  1279.  
  1280.             <cfset inv_related_error = Trim(edit_error_text & " Invoice debtor address not fully specified.")>
  1281.  
  1282.         </cfif>
  1283.  
  1284.     </cfif>
  1285.     <cfset edit_error_text = Trim(edit_error_text & inv_related_error)>
  1286.  
  1287.  
  1288. <!--- Exam dates are within admission dates --->
  1289.     <!--- The following dates could be null and there for can not be parsed through APPLICATION.dateFunctions.parseDateTimeWithNULL() this needs to be handled. --->
  1290.     <cfif EditChecks.SB_CONING_ADMIT_DATE eq ''>
  1291.         <cfset sb_coning_admit_date_comparison = ''>
  1292.     <cfelse>
  1293.         <cfset sb_coning_admit_date_comparison = APPLICATION.dateFunctions.parseDateTimeWithNULL(dateFormat(EditChecks.SB_CONING_ADMIT_DATE,'YYYY-MM-DD'))>
  1294.     </cfif>
  1295.     <cfif EditChecks.SB_SERVICE_DATE eq ''>
  1296.         <cfset sb_service_date_comparison = ''>
  1297.     <cfelse>
  1298.         <cfset sb_service_date_comparison = APPLICATION.dateFunctions.parseDateTimeWithNULL(EditChecks.SB_SERVICE_DATE)>
  1299.     </cfif>
  1300.     <cfif EditChecks.SB_SERVICE_DATE eq ''>
  1301.         <cfset sb_service_date_ymd_comparison = ''>
  1302.     <cfelse>
  1303.         <cfset sb_service_date_ymd_comparison = APPLICATION.dateFunctions.parseDateTimeWithNULL(dateFormat(EditChecks.SB_SERVICE_DATE,'YYYY-MM-DD'))>
  1304.     </cfif>
  1305.     <cfif EditChecks.SB_REQUEST_DATE eq ''>
  1306.         <cfset sb_request_date_comparison = ''>
  1307.     <cfelse>
  1308.         <cfset sb_request_date_comparison = APPLICATION.dateFunctions.parseDateTimeWithNULL(dateFormat(EditChecks.SB_REQUEST_DATE,'YYYY-MM-DD'))>
  1309.     </cfif>
  1310.  
  1311.  
  1312.     <cfif sb_coning_admit_date_comparison gt sb_service_date_comparison>
  1313.         <cfset edit_error_text = Trim(edit_error_text & " Service prior to admission.")>
  1314.     </cfif>
  1315.     <cfif
  1316.         EditChecks.SB_CONING_ITEM_TYPE   NEQ "T" and<!--- not a medical report --->
  1317.         EditChecks.SB_CONING_DISCH_DATE neq ""
  1318.       and APPLICATION.dateFunctions.parseDateTimeWithNULL(dateFormat(EditChecks.SB_CONING_DISCH_DATE,'YYYY-MM-DD')) lt APPLICATION.dateFunctions.parseDateTimeWithNULL(dateFormat(EditChecks.SB_SERVICE_DATE,'YYYY-MM-DD'))>
  1319.         <cfset edit_error_text = Trim(edit_error_text & " Service after discharge.")>
  1320.     </cfif>
  1321.     <!---
  1322.         Exam code is on or after request date
  1323.     --->
  1324.  
  1325.     <cfif sb_service_date_ymd_comparison lt sb_request_date_comparison>
  1326.         <cfset edit_error_text = Trim(edit_error_text & " Service date before request date.")>
  1327.     </cfif>
  1328.  
  1329.  
  1330.  
  1331. <!--- Item number is valid for medicare (for bulk bill and vet affairs) --->
  1332.     <cfif ListfindNoCase('B,BM,AG,SC,M,E,DV,DM',EditChecks.SB_CONING_BILL_MECH) neq 0>
  1333.         <cfif EditChecks.SB_CONING_ITEM_TYPE NEQ 'M'>
  1334.             <cfset edit_error_text = Trim(edit_error_text & " Item code must be MBS.")>
  1335.         </cfif>
  1336.     </cfif>
  1337.  
  1338. <!---
  1339. If not interhospital, corp or private inpatient, check referring provider is valid
  1340. and not a prosthesis item
  1341. --->
  1342.  
  1343.     <cfif EditChecks.SB_CONING_DEFENCE neq "y" and ListfindNoCase('H,C,PI',EditChecks.SB_CONING_BILL_MECH) eq 0
  1344.       AND EditChecks.SB_CONING_REFERRAL_OVERRIDE EQ ""
  1345.       and EditChecks.SB_CONING_ITEM_TYPE neq "r"
  1346.       and EditChecks.SB_CONING_ITEM_TYPE neq "o"
  1347.       and EditChecks.SB_CONING_ITEM_TYPE neq "d"
  1348.       and EditChecks.SB_CONING_ITEM_TYPE neq "e"
  1349.       and EditChecks.SB_CONING_ITEM_TYPE neq "i"
  1350.       and EditChecks.SB_CONING_ITEM_TYPE neq "t"
  1351.       and EditChecks.service_type neq "O">
  1352.         <cfset ALLOW_PROVIDER_VALIDATION = application.cache.getSysParam("ALLOW_PROVIDER_VALIDATION")>
  1353.         <cfif ALLOW_PROVIDER_VALIDATION EQ "Y">
  1354.             <!---
  1355.                 Requesting dr has a start date
  1356.                 Request date is inside the requesting dr start and end date
  1357.  
  1358.                 srt, 04-12-2007 added request date portion to below cfelseif statment
  1359.                 srt, 05-12-2004 job 2106, separated referral and request
  1360.  
  1361.  
  1362.                 SB_REQUEST_OR_REFERRAL = (r = refferal) (q = request)
  1363.               --->
  1364.             <cfif EditChecks.SB_EPISODE_TYPE_CODE eq 'O' and
  1365.                 EditChecks.SB_REFFERAL_DATE_ON_LETTER neq "" and EditChecks.SB_REQUEST_OR_REFERRAL EQ "R" and
  1366.                     (APPLICATION.dateFunctions.parseDateTimeWithNULL(EditChecks.SB_CONING_REQ_PROV_START) gt APPLICATION.dateFunctions.parseDateTimeWithNULL(EditChecks.SB_REFFERAL_DATE_ON_LETTER)
  1367.                    or APPLICATION.dateFunctions.parseDateTimeWithNULL(EditChecks.SB_CONING_REQ_PROV_END) lt APPLICATION.dateFunctions.parseDateTimeWithNULL(EditChecks.SB_REFFERAL_DATE_ON_LETTER))
  1368.             >
  1369.  
  1370.                 <cfset edit_error_text = Trim(edit_error_text & ' Requesting Provider ' & EditChecks.SB_CONING_REQUEST_PROVIDER & ' start / end dates (#DateFormat(EditChecks.SB_CONING_REQ_PROV_START,"dd-mmm-yyyy")# - #DateFormat(EditChecks.SB_CONING_REQ_PROV_END,"dd-mmm-yyyy")#) do not match date on Letter #DateFormat(EditChecks.SB_REFFERAL_DATE_ON_LETTER,"dd-mmm-yyyy")#')>
  1371.  
  1372.  
  1373.             <cfelseif EditChecks.SB_EPISODE_TYPE_CODE eq 'O' and  EditChecks.SB_REQUEST_DATE neq "" and EditChecks.SB_REQUEST_OR_REFERRAL EQ "Q" AND
  1374.                     (APPLICATION.dateFunctions.parseDateTimeWithNULL(EditChecks.SB_CONING_REQ_PROV_START) gt APPLICATION.dateFunctions.parseDateTimeWithNULL(EditChecks.SB_REQUEST_DATE)
  1375.                    or APPLICATION.dateFunctions.parseDateTimeWithNULL(EditChecks.SB_CONING_REQ_PROV_END) lt APPLICATION.dateFunctions.parseDateTimeWithNULL(EditChecks.SB_REQUEST_DATE))
  1376.             >
  1377.  
  1378.                 <cfset edit_error_text = Trim(edit_error_text & ' Requesting Provider ' & EditChecks.SB_CONING_REQUEST_PROVIDER & ' start / end dates (#DateFormat(EditChecks.SB_CONING_REQ_PROV_START,"dd-mmm-yyyy")# - #DateFormat(EditChecks.SB_CONING_REQ_PROV_END,"dd-mmm-yyyy")#) do not match request date #DateFormat(EditChecks.SB_REQUEST_DATE,"dd-mmm-yyyy")#')>
  1379.  
  1380.  
  1381.             </cfif>
  1382.         </cfif>
  1383.     </cfif>
  1384.  
  1385. <!---   For items that are ED Comp/Inelig or OPD Comp/Ineli, if there is an inpatient episode
  1386.     on the same day, they cannot be claimed
  1387.  
  1388.     SRT, 10-06-2008. ADDED EPISODE DELTION DATE TO WHERE CLAUSE.
  1389.  
  1390. --->
  1391.  
  1392.     <cfif same_day_compensable neq "Y"
  1393.      and (EditChecks.SB_CONING_ITEM_TYPE eq "E"
  1394.        or EditChecks.SB_CONING_ITEM_TYPE eq "O")>
  1395.         <cfquery datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#" name="InpatEp">
  1396.             set dateformat dmy
  1397.             SELECT
  1398.                 Count(*) as cnt
  1399.             FROM
  1400.                 dbo.SB_PATIENT_EPISODE
  1401.             WHERE
  1402.                 SB_EPISODE_DELETION_DATE IS NULL
  1403.               AND   SB_PATIENT_MRN = '#ARGUMENTS.mrn#'
  1404.               and   SB_HL7_SOURCE = '#ARGUMENTS.source#'
  1405.               AND   '#DateFormat(EditChecks.SB_SERVICE_DATE,"dd-mmm-yyyy")#'
  1406.                     between CONVERT(DATETIME, CONVERT(VARCHAR, SB_ADMISSION_DATE,103))
  1407.                         and IsNull(CONVERT(DATETIME, CONVERT(VARCHAR, SB_ADMISSION_DATE,103)),GetDate())
  1408.                         and SB_EPISODE_TYPE_CODE = 'I'
  1409.         </cfquery>
  1410.         <cfif InpatEp.CNT GT 0>
  1411.             <cfset edit_error_text = Trim(edit_error_text & ' Compensable item cannot be raised when an inpatient stay occurred on the same day')>
  1412.         </cfif>
  1413.  
  1414.     </cfif>
  1415. <!--- The Fin Class from RIS does not conflict with that from PIMS.
  1416. i.e. If RIS specifies an inpatient fin class code, but there is no inpatient episode from PIMS, then an error will be flagged.
  1417.  
  1418. This edit check is not required, as it is covered by the cost determination functionality.
  1419. i.e. Entries in SB_COST_TYPE_CRITERIA specify valid combinations of fin class and episode type to give a billin mechanism
  1420. and cost type. If no match, then an error of "No Bill Mech", and "No Cost" will be given.
  1421. --->
  1422.  
  1423. <!--- Update any errors into the coning run table. --->
  1424.  
  1425.     <cfif edit_error_text neq "">
  1426.          <cfquery datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#" name="UPDATE_EDIT_ERROR">
  1427.          UPDATE SB_CONING_RUN
  1428.             SET
  1429.                 SB_CONING_STATUS_CODE = 'ER',
  1430.                 SB_CONING_ERROR = LEFT('#edit_error_text#',3000)
  1431.             WHERE
  1432.                 SB_CONING_ID = #EditChecks.SB_CONING_ID#
  1433.          </cfquery>
  1434.     </cfif>
  1435. </cfoutput>
  1436. <!--- .............................................. --->
  1437. <!---               End of edit checks               --->
  1438. <!--- .............................................. --->
  1439.  
  1440. <!---
  1441. slip this AMA coning rule in prior to exiting this procedure with SB_APPLY_CONING = 'N'.
  1442. Some diagnostic items cannot be raised for compensable patients --->
  1443. <cfif EditChecks.SB_CONING_FIN_CLASS_VALID eq "C">
  1444.     <cfquery datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#" name="CompItem">
  1445.         select  SB_CONING_RULE_ITEM.SB_MBS_ITEM_NUMBER,
  1446.                 SB_CONING_RULE_ITEM.SB_MBS_ITEM_NUMBER_TO
  1447.         from    SB_CONING_RULE_ITEM ,
  1448.                 SB_CONING_RULE
  1449.         where   SB_RULE_START_DATE < '#coning_hist_date#'
  1450.           and   (SB_RULE_END_DATE IS NULL OR SB_RULE_END_DATE > '#coning_hist_date#')
  1451.           and   SB_CONING_RULE_ITEM.SB_CONING_RULE_ID = SB_CONING_RULE.SB_CONING_RULE_ID
  1452.           and   SB_RULE_CODE = 'COMP'
  1453.     </cfquery>
  1454.  
  1455.     <cfquery datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#" name="Compensable">
  1456.         SELECT
  1457.             SB_CONING_ID,
  1458.             SB_CONING_ERROR
  1459.          FROM   SB_CONING_RUN
  1460.          WHERE  SB_CONING_RUN_NO = <cfqueryparam cfsqltype="cf_sql_varchar" value="#VARIABLES.unique_id#">
  1461. -- Applies to AMA codes only.
  1462.            AND  SB_CONING_ITEM_TYPE = 'A'
  1463.            <cfif application.cache.getSysParam("COMPENSABLE_APPLY_CONING") eq 'Y'>
  1464.                AND  SB_APPLY_CONING = 'Y'      
  1465.            </cfif>
  1466.            AND  SB_CONING_FIN_CLASS_VALID = 'C' and (1 = 2
  1467.                 <cfoutput query="CompItem">
  1468.                     <cfif CompItem.SB_MBS_ITEM_NUMBER_TO eq "">
  1469.                         or SB_ITEM_NUMBER = '#CompItem.SB_MBS_ITEM_NUMBER#'
  1470.                     <cfelse>
  1471.                         or SB_ITEM_NUMBER BETWEEN '#CompItem.SB_MBS_ITEM_NUMBER#' and '#CompItem.SB_MBS_ITEM_NUMBER_TO#'
  1472.                     </cfif>
  1473.                 </cfoutput>)
  1474.     </cfquery>
  1475.  
  1476.     <cfoutput query="Compensable">
  1477.  
  1478.         <cfset VARIABLES.ERROR_TEXT = #Compensable.SB_CONING_ERROR# & " Unable to raise AMA diagnostic accounts for compensable patients">
  1479.         <cfset ARGUMENTS.coning_error_count = ARGUMENTS.coning_error_count + 1>
  1480.  
  1481.         <cfquery  datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#" name="Comp_error">
  1482.             UPDATE SB_CONING_RUN
  1483.                 SET SB_CONING_ERROR = LEFT('#VARIABLES.ERROR_TEXT#',3000),
  1484.                 SB_CONING_STATUS_CODE = <cfqueryparam cfsqltype="cf_sql_varchar" value="ER">
  1485.             WHERE
  1486.                 SB_CONING_ID = <cfqueryparam cfsqltype="cf_sql_numeric" value="#Compensable.SB_CONING_ID#">
  1487.         </cfquery>
  1488.     </cfoutput>
  1489. </cfif>
  1490.  
  1491. <!--- Update Invoice Item with the price by decided by the coning procedures .......  --->
  1492.  
  1493. <!--- The SQL command Round(x, decimal places) is equivalent to cf Fix()
  1494. Any costs are assumed to be GST exclusive.
  1495. If GST inclusive, use the following instead:
  1496.         SB_INVOICE_COST = ISNULL(SB_ACTUAL_FEE, 0),
  1497.         SB_GST_AMOUNT_CLAIMED = ISNULL(SB_ACTUAL_FEE, 0) - Round((ISNULL(SB_ACTUAL_FEE, 0) * (100.0 / (SB_GST_PERCENT + 100))),2),
  1498.  
  1499.  
  1500.  
  1501. SRT, 10-APR-2008 modified query UPDATE_SB_INVOICE_ITEM to only changes price is there is no invoice date
  1502. --->
  1503.  
  1504.  
  1505. <cfquery datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#" name="UPDATE_SB_INVOICE_ITEM">
  1506.     UPDATE SB_INVOICE_ITEM
  1507.     SET
  1508.         SB_INVOICE_COST     =
  1509.         CASE WHEN SB_INVOICE.SB_INVOICE_DATE IS NULL THEN
  1510.             CEILING((ISNULL(SB_ACTUAL_FEE,0) * (SB_GST_PERCENT + 100.0) / 100.0 ) / 0.05) * 0.05
  1511.         ELSE
  1512.             SB_INVOICE_COST
  1513.         END,
  1514.         SB_GST_AMOUNT_CLAIMED   =
  1515.         CASE WHEN SB_INVOICE.SB_INVOICE_DATE IS NULL THEN
  1516.             ISNULL(SB_ACTUAL_FEE,0) * SB_GST_PERCENT / 100.0
  1517.         ELSE
  1518.             SB_GST_AMOUNT_CLAIMED
  1519.         END,
  1520.         SB_OUTPAT_ERROR     = left(SB_CONING_ERROR,300),
  1521.  
  1522.         SB_CONING_MESSAGE_TO_HIC    = Left(Case     when SB_CONING_BILL_MECH = 'DV' AND
  1523.                                         (SB_CONING_NOT_DUP_SERVICE = 'Y' OR SB_CONING_MULTIPLE_OVERRIDE = 'Y')
  1524.                                     then  RIGHT(CONVERT(VARCHAR, SB_CONING_RUN.sb_service_date ),7) + ' ' + SB_CONING_MSG_TO_HIC
  1525.                                     else  SB_CONING_MSG_TO_HIC end,50),
  1526.         SB_ORIG_MBS_ITEM_NUMBER = ISNULL(SB_INVOICE_ITEM.SB_ORIG_MBS_ITEM_NUMBER,SB_INVOICE_ITEM.SB_MBS_ITEM_NUMBER),
  1527.         SB_ITEM_QTY         = SB_CONING_QTY,
  1528.         SB_FIN_CLASS_CODE   = SB_CONING_FIN_CLASS,
  1529.         SB_ITEM_STATUS_CODE     = Case when SB_CONING_STATUS_CODE = 'ER' then 'ER' else SB_ITEM_STATUS_CODE end,
  1530.         SB_INVOICE_COST_75  =
  1531.         CASE WHEN SB_INVOICE.SB_INVOICE_DATE IS NULL THEN
  1532.             (
  1533.             CASE WHEN SB_SCHEDULE_FEE <> SB_ACTUAL_FEE OR (SB_ACTUAL_FEE_75 IS NULL ) THEN
  1534.                 CEILING((SB_ACTUAL_FEE * 0.75 ) / 0.05) * 0.05
  1535.             ELSE
  1536.                 SB_ACTUAL_FEE_75
  1537.             END
  1538.             )
  1539.         ELSE
  1540.             SB_INVOICE_COST_75
  1541.         END,
  1542.         SB_INVOICE_ITEM.SB_UPDATED_BY =
  1543.             <cfif isDefined("SESSION.SB_LOGIN_CODE")>
  1544.                 <cfqueryparam cfsqltype="cf_sql_varchar" value="#SESSION.SB_LOGIN_CODE#">
  1545.             <cfelse>
  1546.                 'pbrc-ae:ACL'
  1547.             </cfif>,
  1548.         SB_INVOICE_ITEM.SB_UPDATE_DATE = getDate(),
  1549.         SB_UPDATED_FROM = 'apply_coning_logic UPDATE_SB_INVOICE_ITEM'
  1550.     FROM
  1551.         SB_CONING_RUN INNER JOIN
  1552.         SB_INVOICE_ITEM ON
  1553.             SB_CONING_RUN.SB_CONING_ITEM_ID = SB_INVOICE_ITEM.SB_INVOICE_ITEM_ID INNER JOIN
  1554.         SB_INVOICE ON
  1555.             SB_INVOICE_ITEM.SB_INVOICE_ID = SB_INVOICE.SB_INVOICE_ID
  1556.     WHERE
  1557.         SB_CONING_RUN.SB_CONING_RUN_NO = '#VARIABLES.unique_id#'
  1558.         and SB_CONING_RUN.SB_APPLY_CONING = 'N'
  1559. </cfquery>
  1560.  
  1561. <cfinvoke component="pbrc-ae.components.item" method="Bulid_Message_To_Medicare" returnvariable="none">
  1562.     <cfinvokeargument name="Coning_Run" value="#VARIABLES.unique_id#">
  1563. </cfinvoke>
  1564.  
  1565.  
  1566.     <cfquery datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#" name="DELETE">
  1567.      DELETE
  1568.      FROM   SB_CONING_RUN
  1569.      WHERE SB_CONING_RUN_NO = <cfqueryparam cfsqltype="cf_sql_varchar" value="#VARIABLES.unique_id#">
  1570.        and SB_CONING_RUN.SB_APPLY_CONING = 'N'
  1571. </cfquery>
  1572.  
  1573. <!--- This QUERY will be used throughout this program as a CTE --->
  1574. <cfset CONING_RECORDS_QUERY = '
  1575.      SELECT
  1576.         SB_CONING_ID,
  1577.         SB_CONING_RUN_NO,
  1578.         SB_ITEM_NUMBER,
  1579.         SB_SCHEDULE_FEE,
  1580.         SB_ACTUAL_FEE,
  1581.         SB_CONING_STATUS_CODE,
  1582.         SB_SERVICE_DATE,
  1583.         SB_CONING_ITEM_ID,
  1584.         SB_ACTUAL_FEE_75,
  1585.         SB_CONING_ERROR,
  1586.         SB_CONING_PROVIDER,
  1587.         SB_CONING_PATIENT_AGE,
  1588.         SB_CONING_MSG_TO_HIC,
  1589.         SB_CONING_QTY,
  1590.         SB_GST_PERCENT,
  1591.         SB_APPLY_CONING,
  1592.         SB_CONING_SPECIALTY_CODE,
  1593.         SB_CONING_HOSP_CODE,
  1594.         SB_CONING_FIN_CLASS,
  1595.         SB_CONING_BILL_MECH,
  1596.         SB_CONING_EPISODE,
  1597.         SB_REQUEST_DATE,
  1598.         SB_CONING_ITEM_TYPE,
  1599.         SB_CONING_REQUEST_PROVIDER,
  1600.         SB_CONING_CHART_OF_ACC_ID,
  1601.         SB_CONING_NOT_DUP_SERVICE,
  1602.         SB_CONING_MULTIPLE_OVERRIDE,
  1603.         SB_CONING_ADMIT_DATE,
  1604.         SB_CONING_DISCH_DATE,
  1605.         SB_CONING_INVOICE_ID,
  1606.         SB_CONING_REQ_PROV_START,
  1607.         SB_CONING_REQ_PROV_END,
  1608.         SB_CONING_FIN_CLASS_VALID,
  1609.         SB_CONING_HOSP_VALID,
  1610.         SB_CONING_SPEC_PROV_DEFINED,
  1611.         SB_SERV_PROV_VALID,
  1612.         SB_CONING_LSPN,
  1613.         SB_CONING_FUND_GROUP_ID,
  1614.         SB_CONING_REQUIREMENT_CODE,
  1615.         SB_DERIVED_NOGAP_PERCENT,
  1616.         SB_CONING_REFERRAL_OVERRIDE,
  1617.         SB_CONING_DURATION,
  1618.         SB_MBS_100_COST,
  1619.         SB_PRIV_BILLING_END_DATE,
  1620.         SB_CONING_FIN_CLASS_TYPE_CODE,
  1621.         SB_CONING_DEFENCE,
  1622.         CONVERT(DATETIME, CONVERT(VARCHAR, SB_SERVICE_DATE,103)) AS SERVICE_DAY
  1623.      FROM
  1624.         SB_CONING_RUN
  1625.      WHERE
  1626.         SB_CONING_RUN_NO = ''' & #VARIABLES.unique_id# & ''''
  1627. >
  1628.  
  1629. <!--- This QUERY will be used throughout this program to reduce the number of queries against the database
  1630.     It will be used as a QUERY of QUERY
  1631. --->
  1632. <cfquery datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#" name="DATE_RANGE">
  1633.     SET DATEFORMAT DMY
  1634.     SELECT
  1635.         DISTINCT CONVERT(DATETIME, CONVERT(VARCHAR, SB_SERVICE_DATE,103)) AS SERVICE_DAY
  1636.     FROM
  1637.         SB_CONING_RUN
  1638.     WHERE
  1639.         SB_CONING_RUN_NO = <cfqueryparam cfsqltype="cf_sql_varchar" value="#VARIABLES.unique_id#">
  1640.     ORDER BY
  1641.         SERVICE_DAY
  1642.  </cfquery>
  1643.    <!--- This QUERY will be used throughout this program to reduce the number of queries against the database
  1644.         It will be used as a QUERY of QUERY
  1645.   --->
  1646.  <cfquery datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#" name="PROVIDER_RANGE">
  1647.     SET DATEFORMAT DMY
  1648.     SELECT
  1649.         DISTINCT SB_CONING_PROVIDER
  1650.     FROM
  1651.         SB_CONING_RUN
  1652.     WHERE
  1653.         SB_CONING_RUN_NO = <cfqueryparam cfsqltype="cf_sql_varchar" value="#VARIABLES.unique_id#">
  1654.     ORDER BY SB_CONING_PROVIDER
  1655.  </cfquery>
  1656.  
  1657. <!--- Start of Coning Rules --->
  1658.  
  1659. <!--- Check to see if the item number already exists for the selected date and time on the invoice item.
  1660. This is OK if there is HIOC text, a dup service, or mult proc override set. --->
  1661.  
  1662. <cfquery datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#" name="check_dup_proc">
  1663.     SET DATEFORMAT DMY;
  1664.     #preserveSingleQuotes(CONING_RECORDS_QUERY)#
  1665. </cfquery>
  1666.  
  1667. <cfif check_dup_proc.RECORDCOUNT NEQ 0>
  1668.     <cfquery datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#" name="Dup_Proc">
  1669.         SELECT
  1670.             Min(SB_CONING_ID) as dup_1,
  1671.             Max(SB_CONING_ID) as dup_2
  1672.         FROM
  1673.             SB_CONING_RUN
  1674.         WHERE   SB_CONING_NOT_DUP_SERVICE   = 'N'
  1675.           AND   SB_CONING_MULTIPLE_OVERRIDE = 'N'
  1676.           AND   SB_CONING_REQUIREMENT_CODE = 'N/A'
  1677.           AND   SB_CONING_RUN_NO = <cfqueryparam cfsqltype="cf_sql_varchar" value="#VARIABLES.unique_id#">
  1678.         GROUP BY SB_ITEM_NUMBER,
  1679.                  SB_SERVICE_DATE
  1680.         HAVING COUNT(*) > 1
  1681.     </cfquery>
  1682.     <cfif Dup_Proc.recordcount gt 0>
  1683.         <!--- Loop through the DUP_PROC record set. --->
  1684.         <cfloop query="Dup_Proc">
  1685.             <cfquery datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#" name="Dup_error_text">
  1686.                 UPDATE SB_CONING_RUN
  1687.                     SET SB_ACTUAL_FEE = 0,
  1688.                         SB_CONING_STATUS_CODE = 'ER',
  1689.                         SB_CONING_ERROR = 'Item already exists for the specified date and time. If correct please enter another time, select duplicate service, multiple override or add a requirement code.'
  1690.                 WHERE
  1691.                     SB_CONING_ID in (#Dup_Proc.dup_1#,#Dup_Proc.dup_1#)
  1692.             </cfquery>
  1693.         </cfloop>
  1694.     </cfif>
  1695. </cfif>
  1696.  
  1697. <!---
  1698.     Check to see which coning rules are required.
  1699.     SRT, 14-JUL-2006, reformatted query
  1700. --->
  1701.  
  1702. <cfquery datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#" name="Find_Coning">
  1703.     SET DATEFORMAT DMY
  1704.     select distinct
  1705.         SB_CONING_RULE.SB_RULE_CODE,
  1706.         SB_CONING_RULE.SB_CONING_RULE_ID,
  1707.         CONVERT(DATETIME, CONVERT(VARCHAR, sb_coning_run.SB_SERVICE_DATE,103)) AS SERVICE_DAY
  1708.     from
  1709.         SB_CONING_RULE with (nolock) LEFT OUTER JOIN
  1710.         SB_CONING_RULE_ITEM with (nolock) on
  1711.             SB_CONING_RULE_ITEM.SB_CONING_RULE_ID = SB_CONING_RULE.SB_CONING_RULE_ID LEFT OUTER JOIN
  1712.         sb_coning_run  on
  1713.             sb_coning_run.SB_ITEM_NUMBER = SB_CONING_RULE.SB_MBS_ITEM_NUMBER
  1714.             or sb_coning_run.SB_ITEM_NUMBER = SB_CONING_RULE_ITEM.SB_MBS_ITEM_NUMBER
  1715.             or sb_coning_run.SB_ITEM_NUMBER between SB_CONING_RULE_ITEM.SB_MBS_ITEM_NUMBER
  1716.                                             and SB_CONING_RULE_ITEM.SB_MBS_ITEM_NUMBER_TO
  1717.             or
  1718.             (SB_CONING_RULE.SB_RULE_CODE = 'OUTP' and (SB_CONING_BILL_MECH = 'B' or SB_CONING_BILL_MECH = 'BM' ))
  1719.     where
  1720.         SB_CONING_RUN.SB_SERVICE_DATE between SB_RULE_START_DATE    and IsNull(SB_RULE_END_DATE,'31-dec-3000')
  1721.       and SB_CONING_RUN_NO = '#VARIABLES.unique_id#'
  1722. </cfquery>
  1723. <!--- Establish a list of required coning rules --->
  1724. <cfset required_rules = valuelist(Find_Coning.SB_RULE_CODE)>
  1725.  
  1726. <!--- PAE-2263: -Kieran Wood
  1727.       Implementation of DIQ and DIQ1 outpatient serverices rules.
  1728.       Must be charged at a different rate if bulk billed.
  1729.       This is put for the case where there is a single outpatient service on its own,
  1730.       in which case DRC and DIAGI combinaion rules would not run on it.--->
  1731. <cfif ListFind(required_rules,'DIQ') gt 0 or ListFind(required_rules,'DIQ1') gt 0>
  1732.     <!--- get each item in the coning run, and check each one to find the ones that belong in DIQ and DIQ1 rules. --->
  1733.     <cfquery datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#" name="get_coning_runs">
  1734.         SET DATEFORMAT DMY
  1735.         select distinct
  1736.             SB_CONING_ID, SB_CONING_BILL_MECH, SB_MBS_100_COST
  1737.         from
  1738.             sb_coning_run
  1739.         where
  1740.           SB_CONING_RUN_NO = '#VARIABLES.unique_id#'
  1741.     </cfquery>
  1742.  
  1743.     <cfloop query="get_coning_runs">
  1744.         <!--- gets the rules required for this individual item. --->
  1745.         <cfquery datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#" name="Find_item_coning">
  1746.             SET DATEFORMAT DMY
  1747.             select distinct
  1748.                 SB_CONING_RULE.SB_RULE_CODE,
  1749.                 SB_CONING_RULE.SB_CONING_RULE_ID,
  1750.                 CONVERT(DATETIME, CONVERT(VARCHAR, sb_coning_run.SB_SERVICE_DATE,103)) AS SERVICE_DAY
  1751.             from
  1752.                 SB_CONING_RULE with (nolock) INNER JOIN
  1753.                 SB_CONING_RULE_ITEM with (nolock) on
  1754.                     SB_CONING_RULE_ITEM.SB_CONING_RULE_ID = SB_CONING_RULE.SB_CONING_RULE_ID INNER JOIN
  1755.                 sb_coning_run  on
  1756.                     sb_coning_run.SB_ITEM_NUMBER = SB_CONING_RULE.SB_MBS_ITEM_NUMBER
  1757.                     or sb_coning_run.SB_ITEM_NUMBER = SB_CONING_RULE_ITEM.SB_MBS_ITEM_NUMBER
  1758.                     or sb_coning_run.SB_ITEM_NUMBER between SB_CONING_RULE_ITEM.SB_MBS_ITEM_NUMBER
  1759.                                                     and SB_CONING_RULE_ITEM.SB_MBS_ITEM_NUMBER_TO
  1760.                     or
  1761.                     (SB_CONING_RULE.SB_RULE_CODE = 'OUTP' and (SB_CONING_BILL_MECH = 'B' or SB_CONING_BILL_MECH = 'BM' ))
  1762.             where
  1763.                 SB_CONING_RUN.SB_SERVICE_DATE between SB_RULE_START_DATE    and IsNull(SB_RULE_END_DATE,'31-dec-3000')
  1764.               and SB_CONING_ID = '#get_coning_runs.SB_CONING_ID#'
  1765.         </cfquery>
  1766.         <cfset this_items_rules = valuelist(Find_item_coning.SB_RULE_CODE)>
  1767.         <!--- DIQ or DIQ1 required for this rule. --->
  1768.         <cfif ListFind(this_items_rules,'DIQ') gt 0 or ListFind(this_items_rules,'DIQ1') gt 0>
  1769.             <cfif get_coning_runs.SB_CONING_BILL_MECH eq 'B'
  1770.                or get_coning_runs.SB_CONING_BILL_MECH eq 'BM'>
  1771.                 <!--- set the reduction amount --->
  1772.                 <cfif ListFind(this_items_rules,'DIQ') gt 0>
  1773.                     <cfset reduction_percent = 0.95>
  1774.                     <cfset rule_used = 'DIQ'>
  1775.                 <cfelseif ListFind(this_items_rules,'DIQ1') gt 0>
  1776.                     <cfset reduction_percent = 1>
  1777.                     <cfset rule_used = 'DIQ1'>
  1778.                 </cfif>
  1779.  
  1780.                 <!--- now update the actual fee to have the correct cost for this item. --->
  1781.                 <cfquery datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#" name="UPDATE_CONING_RECORDS">
  1782.                     UPDATE SB_CONING_RUN
  1783.                         SET SB_ACTUAL_FEE = CEILING(#get_coning_runs.SB_MBS_100_COST# * #reduction_percent# / 0.05 ) * 0.05,
  1784.                             CONING_RULES_APPLIED = CASE WHEN LEN(ISNULL(CONING_RULES_APPLIED,'') + ',' + '#rule_used#') <= 100
  1785.                                                     THEN (CASE WHEN LEN(ISNULL(CONING_RULES_APPLIED,'')) > 0 THEN CONING_RULES_APPLIED + ',' + '#rule_used#' ELSE '#rule_used#' END)
  1786.                                                     ELSE CONING_RULES_APPLIED END
  1787.                     WHERE
  1788.                         SB_CONING_ID = <cfqueryparam cfsqltype="cf_sql_numeric" value="#get_coning_runs.SB_CONING_ID#">
  1789.                 </cfquery>
  1790.  
  1791.             </cfif>
  1792.         </cfif>
  1793.     </cfloop>
  1794. </cfif>
  1795.  
  1796.  
  1797.  
  1798.  <!--- Start of Multiple Operations Forumala
  1799.  
  1800. For multiple operations (MBS items 30001 to 51318) that is not an amputation (44325 - 44376) performed by the
  1801. same dr on the same day , the most expensive is to be 100% of cost, 2nd most expensive is to be 50%,
  1802. and subsequent operations charged at 25%.
  1803.  
  1804. Assume all items are on the one invoice
  1805.  
  1806.  --->
  1807.  
  1808. <cfif ListFind(required_rules,'MOP') gt 0>
  1809.     <cfloop query="Find_Coning">
  1810.         <cfif Find_Coning.SB_RULE_CODE eq 'MOP'>
  1811.             <cfset this_service_day = DateFormat(find_coning.SERVICE_DAY,"dd/mm/yyyy")>
  1812.             <cfset this_service_day_mmm = DateFormat(find_coning.SERVICE_DAY,"dd-mmm-yyyy")>
  1813.             <cfset this_rule_id = find_coning.SB_CONING_RULE_ID>
  1814.             <!--- Multiple Operations means more than one record in MULTIPLE_OPERATIONS.
  1815.             Tables are accessed as query of queries.
  1816.              --->
  1817.             <cfquery datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#" name="MOP">
  1818.                 select  SB_RULE_PERCENT1,
  1819.                         SB_RULE_PERCENT2,
  1820.                         SB_RULE_PERCENT3,
  1821.                         SB_CONING_RULE_ID
  1822.                 from    SB_CONING_RULE with (nolock)
  1823.                 where   SB_CONING_RULE_ID = #this_rule_id#
  1824.             </cfquery>
  1825.  
  1826.             <cfquery datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#" name="MOPITEM">
  1827.                 select  SB_MBS_ITEM_NUMBER,
  1828.                         SB_MBS_ITEM_NUMBER_TO
  1829.                 from    SB_CONING_RULE_ITEM  with (nolock)
  1830.                 where   SB_CONING_RULE_ID = #this_rule_id#
  1831.             </cfquery>
  1832.             <cfif mopitem.recordcount eq 0>
  1833.                 The coning run requires the MOP (multiple operations) item records to be defined.
  1834.                 <cfabort>
  1835.             </cfif>
  1836.  
  1837.             <cfquery datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#" name="AMPITEM">
  1838.                 select  SB_CONING_RULE_ITEM.SB_MBS_ITEM_NUMBER,
  1839.                         SB_CONING_RULE_ITEM.SB_MBS_ITEM_NUMBER_TO
  1840.                 from    SB_CONING_RULE_ITEM with (nolock),
  1841.                         SB_CONING_RULE with (nolock)
  1842.                 where   '#this_service_day_mmm#' between SB_RULE_START_DATE
  1843.                                                  and IsNull(SB_RULE_END_DATE,'31-dec-3000')
  1844.                   and   SB_CONING_RULE_ITEM.SB_CONING_RULE_ID = SB_CONING_RULE.SB_CONING_RULE_ID
  1845.                   and   SB_RULE_CODE = 'AMP'
  1846.             </cfquery>
  1847.             <cfif ampitem.recordcount eq 0>
  1848.                 The coning run requires the AMP (amputation operations) item records to be defined.
  1849.                 <cfabort>
  1850.             </cfif>
  1851.  
  1852.             <cfquery datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#" name="MULTIPLE_OPERATIONS">
  1853.                 SET DATEFORMAT DMY;
  1854.                 WITH CONING_RECORDS
  1855.                 AS (
  1856.                     #preserveSingleQuotes(CONING_RECORDS_QUERY)#
  1857.                 )
  1858.                 SELECT
  1859.                     SB_CONING_ID,
  1860.                     SERVICE_DAY,
  1861.                     SB_CONING_PROVIDER
  1862.                 FROM
  1863.                     CONING_RECORDS
  1864.                 WHERE
  1865.                     service_day = '#DateFormat(this_service_day_mmm, "dd-mmm-yyyy")#' and
  1866.                     SB_CONING_MULTIPLE_OVERRIDE = 'N' and
  1867.                     (1 = 2
  1868.                 <cfoutput query="MOPITEM">
  1869.                     <cfif MOPITEM.SB_MBS_ITEM_NUMBER_TO eq "">
  1870.                         or SB_ITEM_NUMBER = '#MOPITEM.SB_MBS_ITEM_NUMBER#'
  1871.                     <cfelse>
  1872.                         or SB_ITEM_NUMBER BETWEEN '#MOPITEM.SB_MBS_ITEM_NUMBER#' and '#MOPITEM.SB_MBS_ITEM_NUMBER_TO#'
  1873.                     </cfif>
  1874.                 </cfoutput>)
  1875.                 <cfoutput query="AMPITEM">
  1876.                     <cfif AMPITEM.SB_MBS_ITEM_NUMBER_TO eq "">
  1877.                         and SB_ITEM_NUMBER <> '#AMPITEM.SB_MBS_ITEM_NUMBER#'
  1878.                     <cfelse>
  1879.                         and SB_ITEM_NUMBER NOT BETWEEN '#AMPITEM.SB_MBS_ITEM_NUMBER#' and '#AMPITEM.SB_MBS_ITEM_NUMBER_TO#'
  1880.                     </cfif>
  1881.                 </cfoutput>
  1882.  
  1883.                 ORDER BY
  1884.                     SERVICE_DAY,
  1885.                     SB_CONING_PROVIDER,
  1886.                     SB_SCHEDULE_FEE DESC,
  1887.                     SB_ITEM_NUMBER ASC,
  1888.                     SB_ACTUAL_FEE DESC
  1889.             </cfquery>
  1890.  
  1891.             <cfif MULTIPLE_OPERATIONS.recordcount gt 0>
  1892.  
  1893.                 <cfset VARIABLES.SERVICE_DATE = "">
  1894.                 <cfset VARIABLES.PROVIDER = "">
  1895.  
  1896.                 <!--- Loop through the MULTIPLE_OPERATIONS record set. --->
  1897.                 <cfloop query="MULTIPLE_OPERATIONS">
  1898.  
  1899.                     <!--- For the first record, the SERVICE_DATE and  PROVIDER VARIABLES will be blank
  1900.                         The second and following records may be on the same date with same provider
  1901.                     --->
  1902.                     <cfif APPLICATION.dateFunctions.parseDateTimeWithNULL(VARIABLES.SERVICE_DATE) EQ APPLICATION.dateFunctions.parseDateTimeWithNULL(MULTIPLE_OPERATIONS.SERVICE_DAY)
  1903.                         AND VARIABLES.PROVIDER EQ MULTIPLE_OPERATIONS.SB_CONING_PROVIDER>
  1904.  
  1905.                         <!--- A second or subsequent record has been processed for the same day and provider --->
  1906.                         <cfset VARIABLES.item_count = VARIABLES.item_count + 1>
  1907.  
  1908.                         <cfif VARIABLES.item_count eq 1>
  1909.                             <cfset VARIABLES.reduced_rate = MOP.SB_RULE_PERCENT1 / 100>
  1910.                         <cfelseif VARIABLES.item_count eq 2>
  1911.                             <cfset VARIABLES.reduced_rate = MOP.SB_RULE_PERCENT2 / 100>
  1912.                         <cfelse>
  1913.                             <cfset VARIABLES.reduced_rate = MOP.SB_RULE_PERCENT3 / 100>
  1914.                         </cfif>
  1915.  
  1916.                         <cfquery datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#" name="UPDATE_CONING_RECORDS">
  1917.                             UPDATE SB_CONING_RUN
  1918.                                 SET SB_ACTUAL_FEE = CEILING((ISNULL(SB_ACTUAL_FEE, 0) * #VARIABLES.reduced_rate#) / 0.05) * 0.05,
  1919.                                     CONING_RULES_APPLIED = CASE WHEN LEN(ISNULL(CONING_RULES_APPLIED,'') + ',' + 'MOP') <= 100
  1920.                                                             THEN (CASE WHEN LEN(ISNULL(CONING_RULES_APPLIED,'')) > 0 THEN CONING_RULES_APPLIED + ',' + 'MOP' ELSE 'MOP' END)
  1921.                                                             ELSE CONING_RULES_APPLIED END
  1922.                             WHERE
  1923.                                 SB_CONING_ID = <cfqueryparam cfsqltype="cf_sql_numeric" value="#MULTIPLE_OPERATIONS.SB_CONING_ID#">
  1924.                         </cfquery>
  1925.                     <cfelse>
  1926.                         <!---  A record with a different date and provider has been processed  --->
  1927.                         <cfset VARIABLES.item_count = 1>
  1928.                         <!--- Update the service date and provider VARIABLES--->
  1929.                         <cfset VARIABLES.SB_CONING_ID = MULTIPLE_OPERATIONS.SB_CONING_ID>
  1930.                         <cfset VARIABLES.SERVICE_DATE = MULTIPLE_OPERATIONS.SERVICE_DAY>
  1931.                         <cfset VARIABLES.PROVIDER = MULTIPLE_OPERATIONS.SB_CONING_PROVIDER>
  1932.                     </cfif>
  1933.  
  1934.                 </cfloop>
  1935.  
  1936.             </cfif>
  1937.         </cfif>
  1938.     </cfloop>
  1939. </cfif>
  1940.  
  1941.  
  1942. <!--- End Of Multiple Operations Forumala --->
  1943.  
  1944. <!--- Start of Diagnostic Rule A
  1945.  
  1946. Where there are two or more diagnostic imaging services (55028 to 63946) by the same dr on the same day,
  1947. then the higher schedule amount is unchanged, and any others are reduced by $5.00
  1948.  
  1949. Medicare do this in a very bizarre fashion. For bulk bill, the $5 should become $4.25 (85% of 5).
  1950. However, some mbs 85% list prices are not exactly 85% of the schedule fee. In these cases,
  1951. medicare take 100%, then subtract $5, then multiply by 85%
  1952. This is not the same as the 85% list price, less $4.25
  1953. Hence for bulk bill, find 100% cost, subtract the 5, multiply by .85 and round up / down to
  1954. nearest 5 cents.
  1955.  
  1956. --->
  1957. <cfif ListFind(required_rules,'DIAGI') gt 0>
  1958.     <cfloop query="Find_Coning">
  1959.         <cfif Find_Coning.SB_RULE_CODE eq 'DIAGI'>
  1960.  
  1961.             <!---
  1962.                Generates the list of coning id's on this run that relate to items on invoices which contain any non-VULT items.
  1963.                You would expect this to be the case most of the time, so this would usually contain all the coning id's in this run.
  1964.                However, items on invoices with ONLY VULT items should not receive the -5 dollar DRA reduction further down in the code,
  1965.                so this helps us later, where we can make sure to only apply the -5 dollar reduction to the items on this list.  
  1966.            --->  
  1967.             <cfquery datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#" name="VULT_CHECK">
  1968.                WITH vult
  1969.                      AS (SELECT c_item.sb_mbs_item_number,
  1970.                                 c_item.sb_mbs_item_number_to,
  1971.                                 c_rule.sb_coning_rule_id
  1972.                          FROM   sb_coning_rule_item c_item
  1973.                                 INNER JOIN sb_coning_rule c_rule
  1974.                                         ON c_rule.sb_coning_rule_id = c_item.sb_coning_rule_id
  1975.                                            AND c_rule.sb_rule_code = 'VULT')
  1976.                                            
  1977.                 SELECT DISTINCT sb_coning_id,
  1978.                                 item.*
  1979.                 FROM   sb_coning_run cr
  1980.                        INNER JOIN sb_invoice_item item
  1981.                                ON cr.sb_coning_item_id = item.sb_invoice_item_id
  1982.                        LEFT OUTER JOIN sb_invoice_item item2
  1983.                                     ON item2.sb_invoice_id = item.sb_invoice_id
  1984.                        LEFT OUTER JOIN vult
  1985.                                     ON vult.sb_mbs_item_number = item2.sb_mbs_item_number
  1986.                                         OR item2.sb_mbs_item_number BETWEEN
  1987.                                            vult.sb_mbs_item_number AND
  1988.                                            vult.sb_mbs_item_number_to
  1989.                        LEFT OUTER JOIN sb_coning_rule c_rule
  1990.                                     ON c_rule.sb_coning_rule_id = vult.sb_coning_rule_id
  1991.                                        AND c_rule.sb_rule_code = 'VULT'
  1992.                 WHERE  cr.sb_coning_run_no = '#VARIABLES.unique_id#'
  1993.                        AND c_rule.sb_coning_rule_id IS NULL
  1994.             </cfquery>
  1995.  
  1996.             <!---
  1997.                Invoices with only MMUS items should also not have -5 dollar DRA reduction applied, so this is selected for in the same way as VULT_CHECK above.  
  1998.            --->
  1999.             <cfquery datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#" name="MMUS_CHECK">
  2000.                 WITH mmus
  2001.                      AS (SELECT c_item.sb_mbs_item_number,
  2002.                                 c_item.sb_mbs_item_number_to,
  2003.                                 c_rule.sb_coning_rule_id
  2004.                          FROM   sb_coning_rule_item c_item
  2005.                                 INNER JOIN sb_coning_rule c_rule
  2006.                                         ON c_rule.sb_coning_rule_id = c_item.sb_coning_rule_id
  2007.                                            AND c_rule.sb_rule_code = 'MMUS')
  2008.                                            
  2009.                 SELECT DISTINCT sb_coning_id,
  2010.                                 item.*
  2011.                 FROM   sb_coning_run cr
  2012.                        INNER JOIN sb_invoice_item item
  2013.                                ON cr.sb_coning_item_id = item.sb_invoice_item_id
  2014.                        LEFT OUTER JOIN sb_invoice_item item2
  2015.                                     ON item2.sb_invoice_id = item.sb_invoice_id
  2016.                        LEFT OUTER JOIN mmus
  2017.                                     ON mmus.sb_mbs_item_number = item2.sb_mbs_item_number
  2018.                                         OR item2.sb_mbs_item_number BETWEEN
  2019.                                            mmus.sb_mbs_item_number AND
  2020.                                            mmus.sb_mbs_item_number_to
  2021.                        LEFT OUTER JOIN sb_coning_rule c_rule
  2022.                                     ON c_rule.sb_coning_rule_id = mmus.sb_coning_rule_id
  2023.                                        AND c_rule.sb_rule_code = 'MMUS'
  2024.                 WHERE  cr.sb_coning_run_no = '#VARIABLES.unique_id#'
  2025.                        AND c_rule.sb_coning_rule_id IS NULL
  2026.             </cfquery>
  2027.            
  2028.             <cfset this_service_day = DateFormat(find_coning.SERVICE_DAY,"dd/mm/yyyy")>
  2029.             <cfset this_service_day_mmm = DateFormat(find_coning.SERVICE_DAY,"dd-mmm-yyyy")>
  2030.             <cfset this_rule_id = find_coning.SB_CONING_RULE_ID>
  2031.  
  2032.             <cfquery datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#" name="DRA">
  2033.                 select  SB_REDUCE_SUB_BY
  2034.                 from    SB_CONING_RULE with (nolock)
  2035.                 where   '#this_service_day_mmm#' between SB_RULE_START_DATE
  2036.                                                  and IsNull(SB_RULE_END_DATE,'31-dec-3000')
  2037.                   and   SB_RULE_CODE = 'DRA'
  2038.             </cfquery>
  2039.             <cfif DRA.recordcount eq 0>
  2040.                 The coning run requires the DRA (Diagnostic Rule A) record to be defined.
  2041.                 <cfabort>
  2042.             </cfif>
  2043.  
  2044.             <cfquery datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#" name="DIAGIITEM">
  2045.                 select  SB_CONING_RULE_ITEM.SB_MBS_ITEM_NUMBER,
  2046.                         SB_CONING_RULE_ITEM.SB_MBS_ITEM_NUMBER_TO
  2047.                 from    SB_CONING_RULE_ITEM with (nolock),
  2048.                         SB_CONING_RULE with (nolock)
  2049.                 where   SB_CONING_RULE.SB_CONING_RULE_ID = #this_rule_id#
  2050.                   and   SB_CONING_RULE_ITEM.SB_CONING_RULE_ID = SB_CONING_RULE.SB_CONING_RULE_ID
  2051.             </cfquery>
  2052.             <cfif DIAGIITEM.recordcount eq 0>
  2053.                 The coning run requires the DIAGI (Diagnostic Imaging Definition) item records to be defined.
  2054.                 <cfabort>
  2055.             </cfif>
  2056.  
  2057.             <cfquery datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#" name="DIAGNOSTIC_RULE_A">
  2058.                 SET DATEFORMAT DMY;
  2059.                 WITH CONING_RECORDS
  2060.                 AS (
  2061.                     #preserveSingleQuotes(CONING_RECORDS_QUERY)#
  2062.                 )
  2063.                 SELECT
  2064.                     SB_CONING_ID,
  2065.                     SB_SERVICE_DATE,
  2066.                     SERVICE_DAY,
  2067.                     SB_CONING_PROVIDER,
  2068.                     SB_CONING_BILL_MECH,
  2069.                     SB_ACTUAL_FEE,
  2070.                     SB_MBS_100_COST
  2071.                 FROM
  2072.                     CONING_RECORDS
  2073.                 WHERE
  2074.                     service_day = '#DateFormat(this_service_day_mmm, "dd-mmm-yyyy")#' and
  2075.                     (1 = 2
  2076.                 <cfoutput query="DIAGIITEM">
  2077.                     <cfif DIAGIITEM.SB_MBS_ITEM_NUMBER_TO eq "">
  2078.                         or SB_ITEM_NUMBER = '#DIAGIITEM.SB_MBS_ITEM_NUMBER#'
  2079.                     <cfelse>
  2080.                         or SB_ITEM_NUMBER BETWEEN '#DIAGIITEM.SB_MBS_ITEM_NUMBER#' and '#DIAGIITEM.SB_MBS_ITEM_NUMBER_TO#'
  2081.                     </cfif>
  2082.                 </cfoutput>)
  2083.                 ORDER BY
  2084.                     SERVICE_DAY,
  2085.                     SB_CONING_PROVIDER,
  2086.                     SB_SCHEDULE_FEE DESC,
  2087.                     SB_ACTUAL_FEE DESC
  2088.             </cfquery>
  2089.  
  2090.             <!--- Two or more diagnostic imaging services --->
  2091.             <cfif DIAGNOSTIC_RULE_A.recordcount GTE 2>
  2092.  
  2093.                 <cfset VARIABLES.SB_CONING_ID = "">
  2094.                 <cfset VARIABLES.SERVICE_DATE = "">
  2095.                 <cfset VARIABLES.PROVIDER = "">
  2096.  
  2097.                 <!--- Loop through the DIAGNOSTIC_RULE_A record set. --->
  2098.                 <cfloop query="DIAGNOSTIC_RULE_A">
  2099.                     <!--- For the first record, the SERVICE_DATE and  PROVIDER VARIABLES will be blank
  2100.                         The second and following records may be on the same date with same provider
  2101.                     --->
  2102.                     <cfif APPLICATION.dateFunctions.parseDateTimeWithNULL(VARIABLES.SERVICE_DATE) EQ APPLICATION.dateFunctions.parseDateTimeWithNULL(DIAGNOSTIC_RULE_A.SERVICE_DAY)
  2103.                         AND VARIABLES.PROVIDER EQ DIAGNOSTIC_RULE_A.SB_CONING_PROVIDER>
  2104.  
  2105.                         <!--- A second or subsequent record has been processed for the same day and provider --->
  2106.                         <cfset VARIABLES.item_count = VARIABLES.item_count + 1>
  2107.  
  2108.                         <cfif VARIABLES.item_count GT 1>
  2109.             <!---           For bulk bill, reduction is 85% of normal reduction --->
  2110.                             <cfif DIAGNOSTIC_RULE_A.SB_CONING_BILL_MECH eq 'B'
  2111.                                or DIAGNOSTIC_RULE_A.SB_CONING_BILL_MECH eq 'BM'>
  2112.  
  2113.                                <cfquery datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#" name="Find_item_coning">
  2114.                                     SET DATEFORMAT DMY
  2115.                                     select distinct
  2116.                                         SB_CONING_RULE.SB_RULE_CODE,
  2117.                                         SB_CONING_RULE.SB_CONING_RULE_ID,
  2118.                                         CONVERT(DATETIME, CONVERT(VARCHAR, sb_coning_run.SB_SERVICE_DATE,103)) AS SERVICE_DAY
  2119.                                     from
  2120.                                         SB_CONING_RULE with (nolock) INNER JOIN
  2121.                                         SB_CONING_RULE_ITEM with (nolock) on
  2122.                                             SB_CONING_RULE_ITEM.SB_CONING_RULE_ID = SB_CONING_RULE.SB_CONING_RULE_ID INNER JOIN
  2123.                                         sb_coning_run  on
  2124.                                             sb_coning_run.SB_ITEM_NUMBER = SB_CONING_RULE.SB_MBS_ITEM_NUMBER
  2125.                                             or sb_coning_run.SB_ITEM_NUMBER = SB_CONING_RULE_ITEM.SB_MBS_ITEM_NUMBER
  2126.                                             or sb_coning_run.SB_ITEM_NUMBER between SB_CONING_RULE_ITEM.SB_MBS_ITEM_NUMBER
  2127.                                                                             and SB_CONING_RULE_ITEM.SB_MBS_ITEM_NUMBER_TO
  2128.                                             or
  2129.                                             (SB_CONING_RULE.SB_RULE_CODE = 'OUTP' and (SB_CONING_BILL_MECH = 'B' or SB_CONING_BILL_MECH = 'BM' ))
  2130.                                     where
  2131.                                         SB_CONING_RUN.SB_SERVICE_DATE between SB_RULE_START_DATE    and IsNull(SB_RULE_END_DATE,'31-dec-3000')
  2132.                                       and SB_CONING_ID = '#DIAGNOSTIC_RULE_A.SB_CONING_ID#'
  2133.                                 </cfquery>
  2134.                                <cfset this_items_rules = valuelist(Find_item_coning.SB_RULE_CODE)>
  2135.  
  2136.                                <cfif ListFind(this_items_rules,'DIQ') gt 0>
  2137.                                     <cfset reduction_percent = 0.95>
  2138.                                 <cfelseif ListFind(this_items_rules,'DIQ1') gt 0>
  2139.                                     <cfset reduction_percent = 1>
  2140.                                 <cfelse>
  2141.                                     <cfset reduction_percent = 0.85>
  2142.                                 </cfif>
  2143.                                <cfset item_cost = DIAGNOSTIC_RULE_A.SB_MBS_100_COST>
  2144.                             <cfelse>
  2145.                                <cfset reduction_percent = 1>
  2146.                                <cfset item_cost = DIAGNOSTIC_RULE_A.SB_ACTUAL_FEE>
  2147.                             </cfif>
  2148.                            
  2149.                             <!--- The -5 dollar DRA reduction does not apply to invoices that contain only VULT items or to invoices that contain only MMUS items --->
  2150.                             <cfset VULT_LIST = ValueList(VULT_CHECK.SB_CONING_ID)>
  2151.                             <cfset MMUS_LIST = ValueList(MMUS_CHECK.SB_CONING_ID)>
  2152.                             <cfif ListFind(VULT_LIST, DIAGNOSTIC_RULE_A.SB_CONING_ID) eq 0 or ListFind(MMUS_LIST, DIAGNOSTIC_RULE_A.SB_CONING_ID) eq 0>
  2153.                                 <cfset VARIABLES.reduced_rate = 0>
  2154.                             <cfelse>
  2155.                                 <cfset VARIABLES.reduced_rate = DRA.SB_REDUCE_SUB_BY>
  2156.                             </cfif>
  2157.                            
  2158. <!---
  2159. Phil : 30-Oct-2007
  2160. Had a problem with item 57715,
  2161. where select Round((60.9 - 5) * .85 / 5,2) * 5 gives 47.50
  2162. but medicare pay 47.55
  2163. If you ignore rounding to nearest 5 cents, it is 47.515
  2164. I assume medicare are rounding up to nearest 5 cents.
  2165. Therefore changed formula to be
  2166. select ceiling((60.9 - 5) * .85 * 100 / 5) * 5 / 100
  2167. which give 47.55
  2168. Changed
  2169.     SET SB_ACTUAL_FEE = Round((#item_cost# - #VARIABLES.reduced_rate#) * #reduction_percent# / 5,2) * 5
  2170. to be
  2171.     SET SB_ACTUAL_FEE = ceiling((#item_cost# - #VARIABLES.reduced_rate#) * #reduction_percent# * 100 / 5) * 5 / 100
  2172. --->
  2173.  
  2174.                             <cfquery datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#" name="UPDATE_CONING_RECORDS">
  2175.                                 UPDATE SB_CONING_RUN
  2176.                                     SET SB_ACTUAL_FEE = ceiling((#item_cost# - #VARIABLES.reduced_rate# - CURRENT_DEDUCTIONS) * #reduction_percent# * 100 / 5) * 5 / 100,
  2177.                                         CURRENT_DEDUCTIONS = CURRENT_DEDUCTIONS + #VARIABLES.reduced_rate#,
  2178.                                         CONING_RULES_APPLIED = CASE WHEN LEN(ISNULL(CONING_RULES_APPLIED,'') + ',' + 'DRA') <= 100
  2179.                                                                 THEN (CASE WHEN LEN(ISNULL(CONING_RULES_APPLIED,'')) > 0 THEN CONING_RULES_APPLIED + ',' + 'DRA' ELSE 'DRA' END)
  2180.                                                                 ELSE CONING_RULES_APPLIED END
  2181.                                 WHERE
  2182.                                     SB_CONING_ID = <cfqueryparam cfsqltype="cf_sql_numeric" value="#DIAGNOSTIC_RULE_A.SB_CONING_ID#">
  2183.                             </cfquery>
  2184.                         </cfif>
  2185.  
  2186.                     <cfelse>
  2187.                         <!---  A record with a different date and provider has been processed  --->
  2188.                         <cfset VARIABLES.item_count = 1>
  2189.                         <!--- Update the service date and provider VARIABLES--->
  2190.                         <cfset VARIABLES.SB_CONING_ID = DIAGNOSTIC_RULE_A.SB_CONING_ID>
  2191.                         <cfset VARIABLES.SERVICE_DATE = DIAGNOSTIC_RULE_A.SERVICE_DAY>
  2192.                         <cfset VARIABLES.PROVIDER = DIAGNOSTIC_RULE_A.SB_CONING_PROVIDER>
  2193.                     </cfif>
  2194.  
  2195.                 </cfloop>
  2196.  
  2197.             </cfif>
  2198.         </cfif>
  2199.     </cfloop>
  2200. </cfif>
  2201. <!--- End of Diagnostic Rule A --->
  2202. <!--- Start of Diagnostic Rule B
  2203.  
  2204. Where there is at least one R_Type service (see list below), and at least one consultation (00001 through 10816)
  2205. by the same dr on the same day, then the highest charged R-Type service only is reduced as follows:
  2206. if schedule fee >= 40, then reduce by 35
  2207. if schedule fee <40 and > 15 reduce by 15
  2208. if schedule fee <= 15 reduce to zero.
  2209.  
  2210. R-Type diagnostic services are as follows :
  2211. 55029,55031,55033,55037,55039,55045, 55049,55073, 55079,55703,55705, 55709, 55715,
  2212. 55723, 55725,55733,55739,55762,55766, 55770,55774,55802,55806,55810,55814,55818,55822,55826,55830,55834,
  2213. 55838,55842,55846, 55854,57506,57512,57518,57524,57700,57706,58500,58900,60072,60075,60078,60918,60927
  2214.  
  2215. --->
  2216. <cfif ListFind(required_rules,'CONS') gt 0>
  2217.     <cfloop query="Find_Coning">
  2218.         <cfif Find_Coning.SB_RULE_CODE eq 'CONS'>
  2219.             <cfset this_service_day = DateFormat(find_coning.SERVICE_DAY,"dd/mm/yyyy")>
  2220.             <cfset this_service_day_mmm = DateFormat(find_coning.SERVICE_DAY,"dd-mmm-yyyy")>
  2221.             <cfset this_rule_id = find_coning.SB_CONING_RULE_ID>
  2222.  
  2223.             <cfquery datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#" name="DRB">
  2224.                 select  SB_CONSULT_FEE_1,
  2225.                     SB_REDUCTION_1,
  2226.                     SB_CONSULT_FEE_2,
  2227.                     SB_REDUCTION_2
  2228.                 from    SB_CONING_RULE with (nolock)
  2229.                 where   '#this_service_day_mmm#' between SB_RULE_START_DATE
  2230.                                                  and IsNull(SB_RULE_END_DATE,'31-dec-3000')
  2231.                   and   SB_RULE_CODE = 'DRB'
  2232.             </cfquery>
  2233.             <cfif DRB.recordcount eq 0>
  2234.                 The coning run requires the DRB (Diagnostic Rule B) record to be defined.
  2235.                 <cfabort>
  2236.             </cfif>
  2237.  
  2238.             <cfquery datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#" name="CONSITEM">
  2239.                 select  SB_CONING_RULE_ITEM.SB_MBS_ITEM_NUMBER,
  2240.                         SB_CONING_RULE_ITEM.SB_MBS_ITEM_NUMBER_TO
  2241.                 from    SB_CONING_RULE_ITEM with (nolock),
  2242.                         SB_CONING_RULE with (nolock)
  2243.                 where   SB_CONING_RULE.SB_CONING_RULE_ID = #this_rule_id#
  2244.                   and   SB_CONING_RULE_ITEM.SB_CONING_RULE_ID = SB_CONING_RULE.SB_CONING_RULE_ID
  2245.             </cfquery>
  2246.             <cfif CONSITEM.recordcount eq 0>
  2247.                 The coning run requires the CONS (Consultation Item Definition) item records to be defined.
  2248.                 <cfabort>
  2249.             </cfif>
  2250.  
  2251.             <cfquery datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#" name="RTYPEITEM">
  2252.                 select  SB_CONING_RULE_ITEM.SB_MBS_ITEM_NUMBER,
  2253.                         SB_CONING_RULE_ITEM.SB_MBS_ITEM_NUMBER_TO
  2254.                 from    SB_CONING_RULE_ITEM with (nolock),
  2255.                         SB_CONING_RULE with (nolock)
  2256.                 where   '#this_service_day_mmm#' between SB_RULE_START_DATE
  2257.                                                  and IsNull(SB_RULE_END_DATE,'31-dec-3000')
  2258.                   and   SB_CONING_RULE_ITEM.SB_CONING_RULE_ID = SB_CONING_RULE.SB_CONING_RULE_ID
  2259.                   and   SB_RULE_CODE = 'RTYPE'
  2260.             </cfquery>
  2261.             <cfif RTYPEITEM.recordcount eq 0>
  2262.                 The coning run requires the RTYPE (RType Item Definition) item records to be defined.
  2263.                 <cfabort>
  2264.             </cfif>
  2265.  
  2266.             <cfquery datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#" name="DIAGNOSTIC_RULE_B_R_TYPE">
  2267.                 SET DATEFORMAT DMY;
  2268.                 WITH CONING_RECORDS
  2269.                 AS (
  2270.                     #preserveSingleQuotes(CONING_RECORDS_QUERY)#
  2271.                 )
  2272.                 SELECT
  2273.                     COUNT(SB_CONING_ID) AS R_TYPE
  2274.                 FROM
  2275.                     CONING_RECORDS
  2276.                 WHERE service_day = '#DateFormat(this_service_day_mmm, "dd-mmm-yyyy")#' and
  2277.                     (1 = 2
  2278.                 <cfoutput query="RTYPEITEM">
  2279.                     <cfif RTYPEITEM.SB_MBS_ITEM_NUMBER_TO eq "">
  2280.                         or SB_ITEM_NUMBER = '#RTYPEITEM.SB_MBS_ITEM_NUMBER#'
  2281.                     <cfelse>
  2282.                         or SB_ITEM_NUMBER BETWEEN '#RTYPEITEM.SB_MBS_ITEM_NUMBER#' and '#RTYPEITEM.SB_MBS_ITEM_NUMBER_TO#'
  2283.                     </cfif>
  2284.                 </cfoutput>)
  2285.             </cfquery>
  2286.  
  2287.             <cfquery datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#" name="DIAGNOSTIC_RULE_B_CONSULTATION">
  2288.                 SET DATEFORMAT DMY;
  2289.                 WITH CONING_RECORDS
  2290.                 AS (
  2291.                     #preserveSingleQuotes(CONING_RECORDS_QUERY)#
  2292.                 )
  2293.                 SELECT          COUNT(SB_CONING_ID) AS CONSULTATION
  2294.                 FROM            CONING_RECORDS
  2295.                 WHERE service_day = '#DateFormat(this_service_day_mmm, "dd-mmm-yyyy")#' and
  2296.                     (1 = 2
  2297.                 <cfoutput query="CONSITEM">
  2298.                     <cfif CONSITEM.SB_MBS_ITEM_NUMBER_TO eq "">
  2299.                         or SB_ITEM_NUMBER = '#CONSITEM.SB_MBS_ITEM_NUMBER#'
  2300.                     <cfelse>
  2301.                         or SB_ITEM_NUMBER BETWEEN '#CONSITEM.SB_MBS_ITEM_NUMBER#' and '#CONSITEM.SB_MBS_ITEM_NUMBER_TO#'
  2302.                     </cfif>
  2303.                 </cfoutput>)
  2304.             </cfquery>
  2305.  
  2306.             <cfif DIAGNOSTIC_RULE_B_R_TYPE.R_TYPE GT 0 AND DIAGNOSTIC_RULE_B_CONSULTATION.CONSULTATION GT 0>
  2307.             <!--- Originally tried this as query of query, but it couldn't handle the case in the select statement --->
  2308.                 <cfquery  datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#" name="DIAGNOSTIC_RULE_B_COMBINED">
  2309.                     SET DATEFORMAT DMY
  2310.                     SELECT
  2311.                         SB_MBS_100_COST,
  2312.                         SB_CONING_ID,
  2313.                         SB_ITEM_NUMBER,
  2314.                         CONVERT(DATETIME, CONVERT(VARCHAR, SB_SERVICE_DATE,103)) AS SERVICE_DAY,
  2315.                         SB_CONING_PROVIDER,
  2316.                         SB_ACTUAL_FEE,
  2317.                         SB_SCHEDULE_FEE,
  2318.                         CASE
  2319.                             when  1 = 2
  2320.                         <cfoutput query="CONSITEM">
  2321.                             <cfif CONSITEM.SB_MBS_ITEM_NUMBER_TO eq "">
  2322.                                 or SB_ITEM_NUMBER = '#CONSITEM.SB_MBS_ITEM_NUMBER#'
  2323.                             <cfelse>
  2324.                                 or SB_ITEM_NUMBER BETWEEN '#CONSITEM.SB_MBS_ITEM_NUMBER#' and '#CONSITEM.SB_MBS_ITEM_NUMBER_TO#'
  2325.                             </cfif>
  2326.                         </cfoutput>
  2327.                             then 0
  2328.                             else 1
  2329.                         end as consult_item,
  2330.                         SB_CONING_BILL_MECH
  2331.                     FROM
  2332.                         SB_CONING_RUN
  2333.                 WHERE SB_CONING_RUN_NO = <cfqueryparam cfsqltype="cf_sql_varchar" value="#VARIABLES.unique_id#">
  2334.                 AND CONVERT(DATETIME, CONVERT(VARCHAR, SB_SERVICE_DATE,103)) = '#this_service_day#' and
  2335.                 (   1 = 2
  2336.                 <cfoutput query="RTYPEITEM">
  2337.                     <cfif RTYPEITEM.SB_MBS_ITEM_NUMBER_TO eq "">
  2338.                         or SB_ITEM_NUMBER = '#RTYPEITEM.SB_MBS_ITEM_NUMBER#'
  2339.                     <cfelse>
  2340.                         or SB_ITEM_NUMBER BETWEEN '#RTYPEITEM.SB_MBS_ITEM_NUMBER#' and '#RTYPEITEM.SB_MBS_ITEM_NUMBER_TO#'
  2341.                     </cfif>
  2342.                 </cfoutput>
  2343.                 <cfoutput query="CONSITEM">
  2344.                     <cfif CONSITEM.SB_MBS_ITEM_NUMBER_TO eq "">
  2345.                         or SB_ITEM_NUMBER = '#CONSITEM.SB_MBS_ITEM_NUMBER#'
  2346.                     <cfelse>
  2347.                         or SB_ITEM_NUMBER BETWEEN '#CONSITEM.SB_MBS_ITEM_NUMBER#' and '#CONSITEM.SB_MBS_ITEM_NUMBER_TO#'
  2348.                     </cfif>
  2349.                 </cfoutput>
  2350.                 )
  2351.                         ORDER BY CONVERT(DATETIME, CONVERT(VARCHAR, SB_SERVICE_DATE,103)),
  2352.                                  SB_CONING_PROVIDER,
  2353.                                  consult_item,
  2354.                                  SB_SCHEDULE_FEE DESC,
  2355.                                  SB_ACTUAL_FEE DESC
  2356.                 </cfquery>
  2357.  
  2358.                 <!--- Two or more diagnostic imaging services --->
  2359.                 <cfif DIAGNOSTIC_RULE_B_COMBINED.recordcount GTE 2>
  2360.  
  2361.                     <cfset VARIABLES.SERVICE_DATE = "">
  2362.                     <cfset VARIABLES.PROVIDER = "">
  2363.                     <cfset consult_sched_fee = "">
  2364.  
  2365.                     <!--- Loop through the DIAGNOSTIC_RULE_A record set. --->
  2366.                     <cfloop query="DIAGNOSTIC_RULE_B_COMBINED">
  2367.  
  2368.                         <!--- Each time the day and provider changes, insert that record (in addition to the first record .. which has the highest fee)--->
  2369.  
  2370.                         <cfif APPLICATION.dateFunctions.parseDateTimeWithNULL(VARIABLES.SERVICE_DATE) NEQ APPLICATION.dateFunctions.parseDateTimeWithNULL(DIAGNOSTIC_RULE_B_COMBINED.SERVICE_DAY)
  2371.                            OR VARIABLES.PROVIDER NEQ DIAGNOSTIC_RULE_B_COMBINED.SB_CONING_PROVIDER>
  2372.  
  2373.                             <cfset VARIABLES.SERVICE_DATE = DIAGNOSTIC_RULE_B_COMBINED.SERVICE_DAY>
  2374.                             <cfset VARIABLES.PROVIDER = DIAGNOSTIC_RULE_B_COMBINED.SB_CONING_PROVIDER>
  2375.                             <cfset consult_sched_fee = "">
  2376.  
  2377.                         <!--- If first consult item for the day, the store the sched fee amount --->
  2378.                             <cfif DIAGNOSTIC_RULE_B_COMBINED.consult_item eq 0 and consult_sched_fee eq "">
  2379.                                 <cfset consult_sched_fee = DIAGNOSTIC_RULE_B_COMBINED.SB_MBS_100_COST>
  2380.                             </cfif>
  2381.                         <cfelse>
  2382.                             <!--- same day - if first diag item then update --->
  2383.                             <cfif DIAGNOSTIC_RULE_B_COMBINED.consult_item eq 1 and consult_sched_fee neq "">
  2384.  
  2385.                                 <!---   For bulk bill, reduction is 85% of normal reduction --->
  2386.                                 <cfif DIAGNOSTIC_RULE_B_COMBINED.SB_CONING_BILL_MECH eq 'B'
  2387.                                    or DIAGNOSTIC_RULE_B_COMBINED.SB_CONING_BILL_MECH eq 'BM'>
  2388.  
  2389.                                     <cfquery datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#" name="Find_item_coning">
  2390.                                         SET DATEFORMAT DMY
  2391.                                         select distinct
  2392.                                             SB_CONING_RULE.SB_RULE_CODE,
  2393.                                             SB_CONING_RULE.SB_CONING_RULE_ID,
  2394.                                             CONVERT(DATETIME, CONVERT(VARCHAR, sb_coning_run.SB_SERVICE_DATE,103)) AS SERVICE_DAY
  2395.                                         from
  2396.                                             SB_CONING_RULE with (nolock) INNER JOIN
  2397.                                             SB_CONING_RULE_ITEM with (nolock) on
  2398.                                                 SB_CONING_RULE_ITEM.SB_CONING_RULE_ID = SB_CONING_RULE.SB_CONING_RULE_ID INNER JOIN
  2399.                                             sb_coning_run  on
  2400.                                                 sb_coning_run.SB_ITEM_NUMBER = SB_CONING_RULE.SB_MBS_ITEM_NUMBER
  2401.                                                 or sb_coning_run.SB_ITEM_NUMBER = SB_CONING_RULE_ITEM.SB_MBS_ITEM_NUMBER
  2402.                                                 or sb_coning_run.SB_ITEM_NUMBER between SB_CONING_RULE_ITEM.SB_MBS_ITEM_NUMBER
  2403.                                                                                 and SB_CONING_RULE_ITEM.SB_MBS_ITEM_NUMBER_TO
  2404.                                                 or
  2405.                                                 (SB_CONING_RULE.SB_RULE_CODE = 'OUTP' and (SB_CONING_BILL_MECH = 'B' or SB_CONING_BILL_MECH = 'BM' ))
  2406.                                         where
  2407.                                             SB_CONING_RUN.SB_SERVICE_DATE between SB_RULE_START_DATE    and IsNull(SB_RULE_END_DATE,'31-dec-3000')
  2408.                                           and SB_CONING_ID = '#DIAGNOSTIC_RULE_B_COMBINED.SB_CONING_ID#'
  2409.                                     </cfquery>
  2410.  
  2411.                                     <cfset this_items_rules = valuelist(Find_item_coning.SB_RULE_CODE)>
  2412.  
  2413.                                     <cfif ListFind(this_items_rules,'DIQ') gt 0>
  2414.                                         <cfset reduction_percent = 0.95>
  2415.                                     <cfelseif ListFind(this_items_rules,'DIQ1') gt 0>
  2416.                                         <cfset reduction_percent = 1>
  2417.                                     <cfelse>
  2418.                                         <cfset reduction_percent = 0.85>
  2419.                                     </cfif>
  2420.  
  2421.                                 <cfelse>
  2422.                                    <cfset reduction_percent = 1>
  2423.                                 </cfif>
  2424.  
  2425.                                 <cfquery datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#" name="UPDATE_CONING_RECORDS">
  2426.                                     UPDATE SB_CONING_RUN
  2427.                                         SET SB_ACTUAL_FEE = CASE WHEN #consult_sched_fee# >= #drb.SB_CONSULT_FEE_1# THEN
  2428.                                                                      CEILING(((#DIAGNOSTIC_RULE_B_COMBINED.SB_MBS_100_COST# - #drb.SB_REDUCTION_1# - CURRENT_DEDUCTIONS) * #reduction_percent#) / 0.05) * 0.05
  2429.                                                                  WHEN #consult_sched_fee# > #drb.SB_CONSULT_FEE_2#  THEN
  2430.                                                                      CEILING(((#DIAGNOSTIC_RULE_B_COMBINED.SB_MBS_100_COST# - #drb.SB_REDUCTION_2# - CURRENT_DEDUCTIONS) * #reduction_percent#) / 0.05) * 0.05
  2431.                                                                  ELSE
  2432.                                                                      CEILING(((#DIAGNOSTIC_RULE_B_COMBINED.SB_MBS_100_COST# - #consult_sched_fee# - CURRENT_DEDUCTIONS) * #reduction_percent#) / 0.05) * 0.05
  2433.                                                             END,
  2434.                                             CURRENT_DEDUCTIONS = CURRENT_DEDUCTIONS +
  2435.                                                             CASE WHEN #consult_sched_fee# >= #drb.SB_CONSULT_FEE_1# THEN
  2436.                                                                      #drb.SB_REDUCTION_1#
  2437.                                                                  WHEN #consult_sched_fee# > #drb.SB_CONSULT_FEE_2#  THEN
  2438.                                                                      #drb.SB_REDUCTION_2#
  2439.                                                                  ELSE
  2440.                                                                      #consult_sched_fee#
  2441.                                                             END,
  2442.                                             CONING_RULES_APPLIED = CASE WHEN LEN(ISNULL(CONING_RULES_APPLIED,'') + ',' + 'DRB') <= 100
  2443.                                                                     THEN (CASE WHEN LEN(ISNULL(CONING_RULES_APPLIED,'')) > 0 THEN CONING_RULES_APPLIED + ',' + 'DRB' ELSE 'DRB' END)
  2444.                                                                     ELSE CONING_RULES_APPLIED END
  2445.                                     WHERE
  2446.                                         SB_CONING_ID = <cfqueryparam cfsqltype="cf_sql_numeric" value="#DIAGNOSTIC_RULE_B_COMBINED.SB_CONING_ID#">
  2447.                                 </cfquery>
  2448.                             <cfset consult_sched_fee = "">
  2449.                             </cfif>
  2450.                         </cfif>
  2451.  
  2452.  
  2453.                     </cfloop>
  2454.                 </cfif>
  2455.             </cfif>
  2456.         </cfif>
  2457.     </cfloop>
  2458. </cfif>
  2459. <!--- End of Diagnostic Rule B --->
  2460.  
  2461. <!--- Start of Diagnostic Rule C
  2462.  
  2463. Where there is at least one R_Type service (see list below), and at least one non consultation
  2464. by the same dr on the same day, then the highest charged R-Type service only is reduced by $5.00
  2465.  
  2466. R-Type diagnostic services are as follows :
  2467. 55029,55031,55033,55037,55039,55045, 55049,55073, 55079,55703,55705, 55709, 55715,
  2468. 55723, 55725,55733,55739,55762,55766, 55770,55774,55802,55806,55810,55814,55818,55822,55826,55830,55834,
  2469. 55838,55842,55846, 55854,57506,57512,57518,57524,57700,57706,58500,58900,60072,60075,60078,60918,60927
  2470.  
  2471. --->
  2472.  
  2473. <cfif ListFind(required_rules,'DRC') gt 0>
  2474.     <cfloop query="Find_Coning">
  2475.         <cfif Find_Coning.SB_RULE_CODE eq 'DRC'>
  2476.             <cfset this_service_day = DateFormat(find_coning.SERVICE_DAY,"dd/mm/yyyy")>
  2477.             <cfset this_service_day_mmm = DateFormat(find_coning.SERVICE_DAY,"dd-mmm-yyyy")>
  2478.             <cfset this_rule_id = find_coning.SB_CONING_RULE_ID>
  2479.  
  2480.             <cfquery datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#" name="DRC">
  2481.                 select  SB_REDUCTION_1
  2482.                 from    SB_CONING_RULE with (nolock)
  2483.                 where   SB_CONING_RULE.SB_CONING_RULE_ID = #this_rule_id#
  2484.             </cfquery>
  2485.             <cfif DRC.recordcount eq 0>
  2486.                 The coning run requires the DRC (Diagnostic Rule C) record to be defined.
  2487.                 <cfabort>
  2488.             </cfif>
  2489.  
  2490.             <cfquery datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#" name="NCONSITEM">
  2491.                 select  SB_CONING_RULE_ITEM.SB_MBS_ITEM_NUMBER,
  2492.                         SB_CONING_RULE_ITEM.SB_MBS_ITEM_NUMBER_TO
  2493.                 from    SB_CONING_RULE_ITEM with (nolock),
  2494.                         SB_CONING_RULE with (nolock)
  2495.                 where   SB_CONING_RULE.SB_CONING_RULE_ID = #this_rule_id#
  2496.                   and   SB_CONING_RULE_ITEM.SB_CONING_RULE_ID = SB_CONING_RULE.SB_CONING_RULE_ID
  2497.             </cfquery>
  2498.             <cfif NCONSITEM.recordcount eq 0>
  2499.                 The coning run requires the DRC (Diagnostic Rule C) item records to be defined.
  2500.                 <cfabort>
  2501.             </cfif>
  2502.  
  2503.             <cfquery datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#" name="RTYPEITEM">
  2504.                 select  SB_CONING_RULE_ITEM.SB_MBS_ITEM_NUMBER,
  2505.                         SB_CONING_RULE_ITEM.SB_MBS_ITEM_NUMBER_TO
  2506.                 from    SB_CONING_RULE_ITEM with (nolock),
  2507.                         SB_CONING_RULE with (nolock)
  2508.                 where   '#this_service_day_mmm#' between SB_RULE_START_DATE
  2509.                                                  and IsNull(SB_RULE_END_DATE,'31-dec-3000')
  2510.                   and   SB_CONING_RULE_ITEM.SB_CONING_RULE_ID = SB_CONING_RULE.SB_CONING_RULE_ID
  2511.                   and   SB_RULE_CODE = 'RTYPE'
  2512.             </cfquery>
  2513.             <cfif RTYPEITEM.recordcount eq 0>
  2514.                 The coning run requires the RTYPE (RType Item Definition) item records to be defined.
  2515.                 <cfabort>
  2516.             </cfif>
  2517.  
  2518.             <cfquery datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#" name="DIAGNOSTIC_RULE_C_R_TYPE">
  2519.                 SET DATEFORMAT DMY;
  2520.                 WITH CONING_RECORDS
  2521.                 AS (
  2522.                     #preserveSingleQuotes(CONING_RECORDS_QUERY)#
  2523.                 )
  2524.                 SELECT
  2525.                     COUNT(SB_CONING_ID) AS R_TYPE
  2526.                 FROM
  2527.                     CONING_RECORDS
  2528.                 WHERE service_day = '#DateFormat(this_service_day_mmm, "dd-mmm-yyyy")#' and
  2529.                     (1 = 2
  2530.                 <cfoutput query="RTYPEITEM">
  2531.                     <cfif RTYPEITEM.SB_MBS_ITEM_NUMBER_TO eq "">
  2532.                         or SB_ITEM_NUMBER = '#RTYPEITEM.SB_MBS_ITEM_NUMBER#'
  2533.                     <cfelse>
  2534.                         or SB_ITEM_NUMBER BETWEEN '#RTYPEITEM.SB_MBS_ITEM_NUMBER#' and '#RTYPEITEM.SB_MBS_ITEM_NUMBER_TO#'
  2535.                     </cfif>
  2536.                 </cfoutput>)
  2537.             </cfquery>
  2538.  
  2539.             <cfquery datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#" name="DIAGNOSTIC_RULE_C_CONSULTATION">
  2540.                 SET DATEFORMAT DMY;
  2541.                 WITH CONING_RECORDS
  2542.                 AS (
  2543.                     #preserveSingleQuotes(CONING_RECORDS_QUERY)#
  2544.                 )
  2545.                 SELECT          COUNT(SB_CONING_ID) AS NONCONSULTATION
  2546.                 FROM            CONING_RECORDS
  2547.                 WHERE service_day = '#DateFormat(this_service_day_mmm, "dd-mmm-yyyy")#' and
  2548.                     (1 = 2
  2549.                 <cfoutput query="NCONSITEM">
  2550.                     <cfif NCONSITEM.SB_MBS_ITEM_NUMBER_TO eq "">
  2551.                         or SB_ITEM_NUMBER = '#NCONSITEM.SB_MBS_ITEM_NUMBER#'
  2552.                     <cfelse>
  2553.                         or SB_ITEM_NUMBER BETWEEN '#NCONSITEM.SB_MBS_ITEM_NUMBER#' and '#NCONSITEM.SB_MBS_ITEM_NUMBER_TO#'
  2554.                     </cfif>
  2555.                 </cfoutput>)
  2556.             </cfquery>
  2557.  
  2558.             <cfif DIAGNOSTIC_RULE_C_R_TYPE.R_TYPE GT 0 AND DIAGNOSTIC_RULE_C_CONSULTATION.NONCONSULTATION GT 0>
  2559.             <!--- Originally tried this as query of query, but it couldn't handle the case in the select statement --->
  2560.                 <cfquery  datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#" name="DIAGNOSTIC_RULE_C_COMBINED">
  2561.                     SET DATEFORMAT DMY
  2562.                     SELECT
  2563.                         SB_MBS_100_COST,
  2564.                         SB_CONING_ID,
  2565.                         SB_ITEM_NUMBER,
  2566.                         CONVERT(DATETIME, CONVERT(VARCHAR, SB_SERVICE_DATE,103)) AS SERVICE_DAY,
  2567.                         SB_CONING_PROVIDER,
  2568.                         SB_ACTUAL_FEE,
  2569.                         CASE
  2570.                             when  1 = 2
  2571.                         <cfoutput query="NCONSITEM">
  2572.                             <cfif NCONSITEM.SB_MBS_ITEM_NUMBER_TO eq "">
  2573.                                 or SB_ITEM_NUMBER = '#NCONSITEM.SB_MBS_ITEM_NUMBER#'
  2574.                             <cfelse>
  2575.                                 or SB_ITEM_NUMBER BETWEEN '#NCONSITEM.SB_MBS_ITEM_NUMBER#' and '#NCONSITEM.SB_MBS_ITEM_NUMBER_TO#'
  2576.                             </cfif>
  2577.                         </cfoutput>
  2578.                             then 0
  2579.                             else 1
  2580.                         end as nonconsult_item,
  2581.                         SB_CONING_BILL_MECH
  2582.                     FROM
  2583.                         SB_CONING_RUN
  2584.                 WHERE SB_CONING_RUN_NO = <cfqueryparam cfsqltype="cf_sql_varchar" value="#VARIABLES.unique_id#">
  2585.                 and CONVERT(DATETIME, CONVERT(VARCHAR, SB_SERVICE_DATE,103)) = '#this_service_day_MMM#'
  2586.                 AND (   1 = 2
  2587.                 <cfoutput query="RTYPEITEM">
  2588.                     <cfif RTYPEITEM.SB_MBS_ITEM_NUMBER_TO eq "">
  2589.                         or SB_ITEM_NUMBER = '#RTYPEITEM.SB_MBS_ITEM_NUMBER#'
  2590.                     <cfelse>
  2591.                         or SB_ITEM_NUMBER BETWEEN '#RTYPEITEM.SB_MBS_ITEM_NUMBER#' and '#RTYPEITEM.SB_MBS_ITEM_NUMBER_TO#'
  2592.                     </cfif>
  2593.                 </cfoutput>
  2594.                 <cfoutput query="NCONSITEM">
  2595.                     <cfif NCONSITEM.SB_MBS_ITEM_NUMBER_TO eq "">
  2596.                         or SB_ITEM_NUMBER = '#NCONSITEM.SB_MBS_ITEM_NUMBER#'
  2597.                     <cfelse>
  2598.                         or SB_ITEM_NUMBER BETWEEN '#NCONSITEM.SB_MBS_ITEM_NUMBER#' and '#NCONSITEM.SB_MBS_ITEM_NUMBER_TO#'
  2599.                     </cfif>
  2600.                 </cfoutput>
  2601.                 )
  2602.                         ORDER BY CONVERT(DATETIME, CONVERT(VARCHAR, SB_SERVICE_DATE,103)),
  2603.                                  SB_CONING_PROVIDER,
  2604.                                  nonconsult_item,
  2605.                                  SB_SCHEDULE_FEE DESC,
  2606.                                  SB_ACTUAL_FEE DESC
  2607.                 </cfquery>
  2608.  
  2609.                 <!--- Two or more diagnostic imaging services --->
  2610.                 <cfif DIAGNOSTIC_RULE_C_COMBINED.recordcount GTE 2>
  2611.  
  2612.                     <cfset VARIABLES.SERVICE_DATE = "">
  2613.                     <cfset VARIABLES.PROVIDER = "">
  2614.                     <cfset consult_sched_fee = "">
  2615.  
  2616.                     <!--- Loop through the DIAGNOSTIC_RULE_C record set. --->
  2617.                     <cfloop query="DIAGNOSTIC_RULE_C_COMBINED">
  2618.  
  2619.                         <!--- Each time the day and provider changes, insert that record (in addition to the first record .. which has the highest fee)--->
  2620.  
  2621.                         <cfif VARIABLES.SERVICE_DATE eq "">
  2622.                             <cfset parsed_date = "">
  2623.                         <cfelse>
  2624.                             <cfset parsed_date = APPLICATION.dateFunctions.parseDateTimeWithNULL(VARIABLES.SERVICE_DATE)>
  2625.                         </cfif>
  2626.  
  2627.                         <cfif parsed_date NEQ APPLICATION.dateFunctions.parseDateTimeWithNULL(DIAGNOSTIC_RULE_C_COMBINED.SERVICE_DAY)
  2628.                            OR VARIABLES.PROVIDER NEQ DIAGNOSTIC_RULE_C_COMBINED.SB_CONING_PROVIDER>
  2629.  
  2630.                             <cfset VARIABLES.SERVICE_DATE = DIAGNOSTIC_RULE_C_COMBINED.SERVICE_DAY>
  2631.                             <cfset VARIABLES.PROVIDER = DIAGNOSTIC_RULE_C_COMBINED.SB_CONING_PROVIDER>
  2632.                             <cfset consult_sched_fee = "">
  2633.  
  2634.                         <!--- If first consult item for the day, the store the sched fee amount --->
  2635.                             <cfif DIAGNOSTIC_RULE_C_COMBINED.nonconsult_item eq 0 and consult_sched_fee eq "">
  2636.                                 <cfset consult_sched_fee = DIAGNOSTIC_RULE_C_COMBINED.SB_ACTUAL_FEE>
  2637.                             </cfif>
  2638.                         <cfelse>
  2639.  
  2640.                             <!---   For bulk bill, reduction is 85% of normal reduction --->
  2641.                             <cfif DIAGNOSTIC_RULE_C_COMBINED.SB_CONING_BILL_MECH eq 'B'
  2642.                                or DIAGNOSTIC_RULE_C_COMBINED.SB_CONING_BILL_MECH eq 'BM'>
  2643.  
  2644.                                 <cfquery datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#" name="Find_item_coning">
  2645.                                     SET DATEFORMAT DMY
  2646.                                     select distinct
  2647.                                         SB_CONING_RULE.SB_RULE_CODE,
  2648.                                         SB_CONING_RULE.SB_CONING_RULE_ID,
  2649.                                         CONVERT(DATETIME, CONVERT(VARCHAR, sb_coning_run.SB_SERVICE_DATE,103)) AS SERVICE_DAY
  2650.                                     from
  2651.                                         SB_CONING_RULE with (nolock) INNER JOIN
  2652.                                         SB_CONING_RULE_ITEM with (nolock) on
  2653.                                             SB_CONING_RULE_ITEM.SB_CONING_RULE_ID = SB_CONING_RULE.SB_CONING_RULE_ID INNER JOIN
  2654.                                         sb_coning_run  on
  2655.                                             sb_coning_run.SB_ITEM_NUMBER = SB_CONING_RULE.SB_MBS_ITEM_NUMBER
  2656.                                             or sb_coning_run.SB_ITEM_NUMBER = SB_CONING_RULE_ITEM.SB_MBS_ITEM_NUMBER
  2657.                                             or sb_coning_run.SB_ITEM_NUMBER between SB_CONING_RULE_ITEM.SB_MBS_ITEM_NUMBER
  2658.                                                                             and SB_CONING_RULE_ITEM.SB_MBS_ITEM_NUMBER_TO
  2659.                                             or
  2660.                                             (SB_CONING_RULE.SB_RULE_CODE = 'OUTP' and (SB_CONING_BILL_MECH = 'B' or SB_CONING_BILL_MECH = 'BM' ))
  2661.                                     where
  2662.                                         SB_CONING_RUN.SB_SERVICE_DATE between SB_RULE_START_DATE    and IsNull(SB_RULE_END_DATE,'31-dec-3000')
  2663.                                       and SB_CONING_ID = '#DIAGNOSTIC_RULE_C_COMBINED.SB_CONING_ID#'
  2664.                                 </cfquery>
  2665.  
  2666.                                 <cfset this_items_rules = valuelist(Find_item_coning.SB_RULE_CODE)>
  2667.  
  2668.                                 <cfif ListFind(this_items_rules,'DIQ') gt 0>
  2669.                                     <cfset reduction_percent = 0.95>
  2670.                                 <cfelseif ListFind(this_items_rules,'DIQ1') gt 0>
  2671.                                     <cfset reduction_percent = 1>
  2672.                                 <cfelse>
  2673.                                     <cfset reduction_percent = 0.85>
  2674.                                 </cfif>
  2675.  
  2676.                             <cfelse>
  2677.                                <cfset reduction_percent = 1>
  2678.                             </cfif>
  2679.  
  2680.                             <!--- same day - if first diag item then update --->
  2681.                             <cfif DIAGNOSTIC_RULE_C_COMBINED.nonconsult_item eq 1 and consult_sched_fee neq "">
  2682.                                 <cfquery datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#" name="UPDATE_CONING_RECORDS">
  2683.                                     UPDATE SB_CONING_RUN
  2684.                                 <cfif DIAGNOSTIC_RULE_C_COMBINED.SB_ACTUAL_FEE gt drc.SB_REDUCTION_1>
  2685.                                         SET SB_ACTUAL_FEE = CEILING(((#DIAGNOSTIC_RULE_C_COMBINED.SB_MBS_100_COST# - #drc.SB_REDUCTION_1# - CURRENT_DEDUCTIONS ) * #reduction_percent#) / 0.05) * 0.05,
  2686.                                             CURRENT_DEDUCTIONS = CURRENT_DEDUCTIONS + #drc.SB_REDUCTION_1#
  2687.                                 <cfelse>
  2688.                                         SET SB_ACTUAL_FEE = 0
  2689.                                 </cfif>,
  2690.                                         CONING_RULES_APPLIED = CASE WHEN LEN(ISNULL(CONING_RULES_APPLIED,'') + ',' + 'DRC') <= 100
  2691.                                                                 THEN (CASE WHEN LEN(ISNULL(CONING_RULES_APPLIED,'')) > 0 THEN CONING_RULES_APPLIED + ',' + 'DRC' ELSE 'DRC' END)
  2692.                                                                 ELSE CONING_RULES_APPLIED END
  2693.                                     WHERE
  2694.                                         SB_CONING_ID = <cfqueryparam cfsqltype="cf_sql_numeric" value="#DIAGNOSTIC_RULE_C_COMBINED.SB_CONING_ID#">
  2695.                                 </cfquery>
  2696.                             <cfset consult_sched_fee = "">
  2697.                             </cfif>
  2698.                         </cfif>
  2699.  
  2700.  
  2701.                     </cfloop>
  2702.                 </cfif>
  2703.             </cfif>
  2704.         </cfif>
  2705.     </cfloop>
  2706. </cfif>
  2707.  
  2708. <!--- End of Diagnostic Rule C --->
  2709.  
  2710. <!--- Start Of Mutliple Services - ECG
  2711.  
  2712. 11700 only allowed once every hour
  2713. --->
  2714. <cfif ListFind(required_rules,'ECG') gt 0>
  2715.     <cfloop query="Find_Coning">
  2716.         <cfif Find_Coning.SB_RULE_CODE eq 'ECG'>
  2717.             <cfset this_service_day = DateFormat(find_coning.SERVICE_DAY,"dd/mm/yyyy")>
  2718.             <cfset this_service_day_mmm = DateFormat(find_coning.SERVICE_DAY,"dd-mmm-yyyy")>
  2719.             <cfset this_rule_id = find_coning.SB_CONING_RULE_ID>
  2720.  
  2721.             <cfquery datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#" name="ECG">
  2722.                 select  SB_HOURS_BETWEEN
  2723.                 from    SB_CONING_RULE with (nolock)
  2724.                 where   SB_CONING_RULE_ID = #this_rule_id#
  2725.             </cfquery>
  2726.             <cfif ECG.recordcount eq 0>
  2727.                 The coning run requires the ECG (ECG multiple Service rule) record to be defined.
  2728.                 <cfabort>
  2729.             </cfif>
  2730.  
  2731.             <cfquery datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#" name="ECGITEM">
  2732.                 select  SB_MBS_ITEM_NUMBER,
  2733.                         SB_MBS_ITEM_NUMBER_TO
  2734.                 from    SB_CONING_RULE_ITEM with (nolock)
  2735.                 where   SB_CONING_RULE_ID = #this_rule_id#
  2736.             </cfquery>
  2737.             <cfif ECGITEM.recordcount eq 0>
  2738.                 The coning run requires the ECG (ECG multiple Service rule) item records to be defined.
  2739.                 <cfabort>
  2740.             </cfif>
  2741.  
  2742.             <cfquery datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#" name="MULTIPLE_SERVICES_ECG">
  2743.                 SET DATEFORMAT DMY;
  2744.                 WITH CONING_RECORDS
  2745.                 AS (
  2746.                     #preserveSingleQuotes(CONING_RECORDS_QUERY)#
  2747.                 )
  2748.                 SELECT
  2749.                     SB_CONING_ID,
  2750.                     SB_SERVICE_DATE,
  2751.                     SB_CONING_PROVIDER,
  2752.                     SB_CONING_ERROR
  2753.                 FROM CONING_RECORDS
  2754.                 WHERE service_day = '#DateFormat(this_service_day_mmm, "dd-mmm-yyyy")#' and
  2755.                     (1 = 2
  2756.                 <cfoutput query="ECGITEM">
  2757.                     <cfif ECGITEM.SB_MBS_ITEM_NUMBER_TO eq "">
  2758.                         or SB_ITEM_NUMBER = '#ECGITEM.SB_MBS_ITEM_NUMBER#'
  2759.                     <cfelse>
  2760.                         or SB_ITEM_NUMBER BETWEEN '#ECGITEM.SB_MBS_ITEM_NUMBER#' and '#ECGITEM.SB_MBS_ITEM_NUMBER_TO#'
  2761.                     </cfif>
  2762.                 </cfoutput>)
  2763.                 ORDER BY
  2764.                     SB_SERVICE_DATE,
  2765.                     SB_CONING_PROVIDER
  2766.             </cfquery>
  2767.  
  2768.             <!--- If there is more than one ECG service, continue.. --->
  2769.             <cfif MULTIPLE_SERVICES_ECG.RECORDCOUNT GT 1>
  2770.  
  2771.                 <cfset VARIABLES.SERVICE_DATE = "">
  2772.                 <cfset VARIABLES.PROVIDER = "">
  2773.                 <cfset VARIABLES.LAST_CONING_ID = "">
  2774.                 <cfset MINIMUM_MINUTES = 60 * ECG.SB_HOURS_BETWEEN>
  2775.                 <!--- Loop through each record in MUTLIPLE_SERVICES_ECG --->
  2776.                 <cfloop query="MULTIPLE_SERVICES_ECG">
  2777.  
  2778.                     <!---  For the same provider on the same day --->
  2779.                     <cfif (VARIABLES.SERVICE_DATE EQ "" AND VARIABLES.PROVIDER EQ "") OR
  2780.                             (APPLICATION.dateFunctions.parseDateTimeWithNULL(dateFormat(VARIABLES.SERVICE_DATE,'YYYY-MM-DD')) EQ APPLICATION.dateFunctions.parseDateTimeWithNULL(dateFormat(MULTIPLE_SERVICES_ECG.SB_SERVICE_DATE,'YYYY-MM-DD')) AND
  2781.                              VARIABLES.PROVIDER EQ MULTIPLE_SERVICES_ECG.SB_CONING_PROVIDER)>
  2782.  
  2783.                              <cfif MULTIPLE_SERVICES_ECG.CurrentRow eq 1>
  2784.                                 <!--- This var is the date and time at which the first service has occured --->
  2785.                                 <cfset VARIABLES.charged_item_time = MULTIPLE_SERVICES_ECG.SB_SERVICE_DATE>
  2786.                              </cfif>
  2787.  
  2788.                              <!--- If a subsequent ECG occurs less than 1 hours after the charged item, it costs 0 --->
  2789.                              <cfif DATEDIFF("N", VARIABLES.charged_item_time, MULTIPLE_SERVICES_ECG.SB_SERVICE_DATE) LT MINIMUM_MINUTES and
  2790.                                     MULTIPLE_SERVICES_ECG.CurrentRow neq 1>
  2791.                                     <cfset VARIABLES.ERROR_TEXT = #MULTIPLE_SERVICES_ECG.SB_CONING_ERROR# & " Item can only be claimed once every #ECG.SB_HOURS_BETWEEN# hours">
  2792.  
  2793.                                     <cfset ARGUMENTS.coning_error_count = ARGUMENTS.coning_error_count + 1>
  2794.                                     <!--- Set the error message every record that is not the first in the series for a
  2795.                                         item and date --->
  2796.                                     <cfquery datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#" name="MULTIPLE_SERVICES_ECG_error_text">
  2797.                                         UPDATE SB_CONING_RUN
  2798.                                             SET SB_ACTUAL_FEE = 0,
  2799.                                                 SB_CONING_STATUS_CODE = 'ER',
  2800.                                                 SB_CONING_ERROR = LEFT('#VARIABLES.ERROR_TEXT#',3000)
  2801.                                         WHERE
  2802.                                             SB_CONING_ID IN (#MULTIPLE_SERVICES_ECG.SB_CONING_ID#, #VARIABLES.LAST_CONING_ID#)
  2803.                                     </cfquery>
  2804.                              <cfelse>
  2805.                                 <!--- Update this variable to the date and time at which the last item was charged at` --->
  2806.                                  <cfset VARIABLES.charged_item_time = MULTIPLE_SERVICES_ECG.SB_SERVICE_DATE>
  2807.                              </cfif>
  2808.                     </cfif>
  2809.  
  2810.                     <cfset VARIABLES.SERVICE_DATE = MULTIPLE_SERVICES_ECG.SB_SERVICE_DATE>
  2811.                     <cfset VARIABLES.PROVIDER = MULTIPLE_SERVICES_ECG.SB_CONING_PROVIDER>
  2812.                     <cfset VARIABLES.LAST_CONING_ID = MULTIPLE_SERVICES_ECG.SB_CONING_ID>
  2813.                 </cfloop>
  2814.  
  2815.             </cfif>
  2816.         </cfif>
  2817.     </cfloop>
  2818. </cfif>
  2819.  
  2820.  <!---  End Of Mutliple Services - ECG rule --->
  2821. <!--- Start Of Mutliple Services - Ultrasound rule
  2822.  
  2823. If an item number in the following ranges has already been entered by the same dr for the same date
  2824. they must be 3hrs apart 55113 to 55130, 55238 to 55296, 55600 to 55603, 55700 to 55774, 55800 to 55854.
  2825. Where an item is within 3 hours of a charged item, then set the revised fee to zero.
  2826. If ultrasounds occurred at 9am, 11am and 1pm, then only the 11am fee would be set to zero.
  2827.  
  2828. --->
  2829. <cfif ListFind(required_rules,'ULT') gt 0>
  2830.     <cfloop query="Find_Coning">
  2831.         <cfif Find_Coning.SB_RULE_CODE eq 'ULT'>
  2832.             <cfset this_service_day = DateFormat(find_coning.SERVICE_DAY,"dd/mm/yyyy")>
  2833.             <cfset this_service_day_mmm = DateFormat(find_coning.SERVICE_DAY,"dd-mmm-yyyy")>
  2834.             <cfset this_rule_id = find_coning.SB_CONING_RULE_ID>
  2835.  
  2836.             <cfquery datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#" name="ULT">
  2837.                 select  SB_HOURS_BETWEEN
  2838.                 from    SB_CONING_RULE with (nolock)
  2839.                 where   SB_CONING_RULE_ID = #this_rule_id#
  2840.             </cfquery>
  2841.             <cfif ULT.recordcount eq 0>
  2842.                 The coning run requires the ULT (ultrasound multiple Service rule) record to be defined.
  2843.                 <cfabort>
  2844.             </cfif>
  2845.  
  2846.             <cfquery datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#" name="ULTITEM">
  2847.                 select  SB_CONING_RULE_ITEM.SB_MBS_ITEM_NUMBER,
  2848.                         SB_CONING_RULE_ITEM.SB_MBS_ITEM_NUMBER_TO
  2849.                 from    SB_CONING_RULE_ITEM with (nolock)
  2850.                 where   SB_CONING_RULE_ID = #this_rule_id#
  2851.             </cfquery>
  2852.             <cfif ULTITEM.recordcount eq 0>
  2853.                 The coning run requires the ULT (ultrasound multiple Service rule) item records to be defined.
  2854.                 <cfabort>
  2855.             </cfif>
  2856.  
  2857.             <cfquery datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#" name="MULTIPLE_SERVICES_ULTRASOUND">
  2858.                 SET DATEFORMAT DMY;
  2859.                 WITH CONING_RECORDS
  2860.                 AS (
  2861.                     #preserveSingleQuotes(CONING_RECORDS_QUERY)#
  2862.                 )
  2863.                 SELECT
  2864.                     SB_CONING_ID,
  2865.                     SB_SERVICE_DATE,
  2866.                     SB_CONING_PROVIDER,
  2867.                     SB_CONING_ERROR,
  2868.                     SB_CONING_NOT_DUP_SERVICE
  2869.                 FROM CONING_RECORDS
  2870.                 WHERE service_day = '#DateFormat(this_service_day_mmm, "dd-mmm-yyyy")#' and
  2871.                     (1 = 2
  2872.                 <cfoutput query="ULTITEM">
  2873.                     <cfif ULTITEM.SB_MBS_ITEM_NUMBER_TO eq "">
  2874.                         or SB_ITEM_NUMBER = '#ULTITEM.SB_MBS_ITEM_NUMBER#'
  2875.                     <cfelse>
  2876.                         or SB_ITEM_NUMBER BETWEEN '#ULTITEM.SB_MBS_ITEM_NUMBER#' and '#ULTITEM.SB_MBS_ITEM_NUMBER_TO#'
  2877.                     </cfif>
  2878.                 </cfoutput>)
  2879.                 ORDER BY
  2880.                     SB_SERVICE_DATE,
  2881.                     SB_CONING_PROVIDER
  2882.             </cfquery>
  2883.  
  2884.             <!--- If there is more than one ultrasound service, continue.. --->
  2885.             <cfif MULTIPLE_SERVICES_ULTRASOUND.RECORDCOUNT GT 1>
  2886.  
  2887.                 <cfset VARIABLES.SERVICE_DATE = "">
  2888.                 <cfset VARIABLES.PROVIDER = "">
  2889.                 <cfset MINIMUM_MINUTES = 60 * ult.SB_HOURS_BETWEEN>
  2890.                 <!--- Loop through each record in MUTLIPLE_SERVICES_ULTRASOUND --->
  2891.                 <cfloop query="MULTIPLE_SERVICES_ULTRASOUND">
  2892.  
  2893.                     <!---  For the same provider on the same day --->
  2894.                     <cfif (VARIABLES.SERVICE_DATE EQ "" AND VARIABLES.PROVIDER EQ "") OR
  2895.                             (APPLICATION.dateFunctions.parseDateTimeWithNULL(DateFormat(VARIABLES.SERVICE_DATE,'YYYY-MM-DD')) EQ APPLICATION.dateFunctions.parseDateTimeWithNULL(DateFormat(MULTIPLE_SERVICES_ULTRASOUND.SB_SERVICE_DATE,'YYYY-MM-DD')) AND
  2896.                              VARIABLES.PROVIDER EQ MULTIPLE_SERVICES_ULTRASOUND.SB_CONING_PROVIDER)>
  2897.  
  2898.                              <cfif MULTIPLE_SERVICES_ULTRASOUND.CurrentRow eq 1>
  2899.                                 <!--- This var is the date and time at which the first service has occured --->
  2900.                                 <cfset VARIABLES.charged_item_time = MULTIPLE_SERVICES_ULTRASOUND.SB_SERVICE_DATE>
  2901.                              </cfif>
  2902.  
  2903.                              <!--- If a subsequent ultrasound occurs less than 3 hours after the charged item, it costs 0 --->
  2904.                              <!--- PAU-442 ULT Coning rule should not produce the error "item can only be claimed once every 3 hrs" for multiple service ultrasound if not dup service set to 'Y'.--->
  2905.                              <cfif DATEDIFF("N", VARIABLES.charged_item_time, MULTIPLE_SERVICES_ULTRASOUND.SB_SERVICE_DATE) LT MINIMUM_MINUTES and
  2906.                                     MULTIPLE_SERVICES_ULTRASOUND.CurrentRow neq 1
  2907.                                     AND MULTIPLE_SERVICES_ULTRASOUND.SB_CONING_NOT_DUP_SERVICE NEQ 'Y'>
  2908.                                     <cfset VARIABLES.ERROR_TEXT = #MULTIPLE_SERVICES_ULTRASOUND.SB_CONING_ERROR# & " Item can only be claimed once every #ult.SB_HOURS_BETWEEN# hours">
  2909.                                     <cfset ARGUMENTS.coning_error_count = ARGUMENTS.coning_error_count + 1>
  2910.                                     <!--- Set the error message every record that is not the first in the series for a
  2911.                                         item and date --->
  2912.                                     <cfquery datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#" name="MULTIPLE_SERVICES_ULTRASOUND_error_text">
  2913.                                         UPDATE SB_CONING_RUN
  2914.                                             SET SB_ACTUAL_FEE = 0,
  2915.                                                 SB_CONING_STATUS_CODE = 'ER',
  2916.                                                 SB_CONING_ERROR = LEFT('#VARIABLES.ERROR_TEXT#',3000)
  2917.                                         WHERE
  2918.                                             SB_CONING_ID = <cfqueryparam cfsqltype="cf_sql_numeric" value="#MULTIPLE_SERVICES_ULTRASOUND.SB_CONING_ID#">
  2919.                                     </cfquery>
  2920.                              <cfelse>
  2921.                                 <!--- Update this variable to the date and time at which the last item was charged at` --->
  2922.                                  <cfset VARIABLES.charged_item_time = MULTIPLE_SERVICES_ULTRASOUND.SB_SERVICE_DATE>
  2923.                              </cfif>
  2924.                     </cfif>
  2925.  
  2926.                     <cfset VARIABLES.SERVICE_DATE = MULTIPLE_SERVICES_ULTRASOUND.SB_SERVICE_DATE>
  2927.                     <cfset VARIABLES.PROVIDER = MULTIPLE_SERVICES_ULTRASOUND.SB_CONING_PROVIDER>
  2928.  
  2929.                 </cfloop>
  2930.  
  2931.             </cfif>
  2932.  
  2933.         </cfif>
  2934.     </cfloop>
  2935. </cfif>
  2936.   <!---  End Of Mutliple Services - Ultrasound rule --->
  2937.  
  2938.  <!--- Start of  Multiple Services - Vascular Ultrasound
  2939.  
  2940. Where more than one vascular ultrasound service (55238 to 55296) is provided to the same patient by the same practitioner on
  2941. the same date of service, the following formula applies to the Schedule fee for each service:
  2942. - 100% for the item with the greatest Schedule fee
  2943. - plus 60% for the item with the next greatest Schedule fee
  2944. - plus 50% for each other item
  2945.  
  2946. When the Schedule fee for some of the items are the same, the reduction is calculated in the following order:
  2947. - 100% for the item with the greatest Schedule fee and the lowest item number
  2948. - plus 60% for the item with the greatest Schedule fee and the second lowest item number
  2949. - plus 50% for each other item
  2950.  
  2951.  --->
  2952. <cfif ListFind(required_rules,'VULT') gt 0>
  2953.     <cfloop query="Find_Coning">
  2954.         <cfif Find_Coning.SB_RULE_CODE eq 'VULT'>
  2955.             <cfset this_service_day = DateFormat(find_coning.SERVICE_DAY,"dd/mm/yyyy")>
  2956.             <cfset this_service_day_mmm = DateFormat(find_coning.SERVICE_DAY,"dd-mmm-yyyy")>
  2957.             <cfset this_rule_id = find_coning.SB_CONING_RULE_ID>
  2958.  
  2959.             <cfquery datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#" name="VULT">
  2960.                 select  SB_RULE_PERCENT1,
  2961.                         SB_RULE_PERCENT2,
  2962.                         SB_RULE_PERCENT3,
  2963.                         SB_CONING_RULE_ID
  2964.                 from    SB_CONING_RULE with (nolock)
  2965.                 where   SB_CONING_RULE_ID = #this_rule_id#
  2966.             </cfquery>
  2967.             <cfif VULT.recordcount eq 0>
  2968.                 The coning run requires the VULT (Vascular ultrasound multiple Service rule) record to be defined.
  2969.                 <cfabort>
  2970.             </cfif>
  2971.  
  2972.             <cfquery datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#" name="VULTITEM">
  2973.                 select  SB_CONING_RULE_ITEM.SB_MBS_ITEM_NUMBER,
  2974.                         SB_CONING_RULE_ITEM.SB_MBS_ITEM_NUMBER_TO
  2975.                 from    SB_CONING_RULE_ITEM with (nolock)
  2976.                 where   SB_CONING_RULE_ID = #this_rule_id#
  2977.             </cfquery>
  2978.             <cfif VULTITEM.recordcount eq 0>
  2979.                 The coning run requires the VULT (Vascular ultrasound multiple Service rule) item records to be defined.
  2980.                 <cfabort>
  2981.             </cfif>
  2982.  
  2983.             <cfquery datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#" name="MULTIPLE_SERVICES_VASCULAR_ULTRASOUND">
  2984.                 set dateformat dmy
  2985.                 SELECT
  2986.                     SB_CONING_ID,
  2987.                     SB_SERVICE_DATE,
  2988.                     SB_CONING_PROVIDER,
  2989.                     SB_ACTUAL_FEE,
  2990.                     SB_SCHEDULE_FEE,
  2991.                     SB_ITEM_NUMBER,
  2992.                     CONVERT(DATETIME, CONVERT(VARCHAR, SB_SERVICE_DATE, 103))
  2993.                 FROM    SB_CONING_RUN
  2994.                 WHERE   SB_CONING_RUN_NO = '#VARIABLES.unique_id#'
  2995.                 AND CONVERT(DATETIME, CONVERT(VARCHAR, SB_SERVICE_DATE, 103)) = '#this_service_day#'
  2996.                 AND (1 = 2
  2997.                 <cfoutput query="VULTITEM">
  2998.                     <cfif VULTITEM.SB_MBS_ITEM_NUMBER_TO eq "">
  2999.                         or SB_ITEM_NUMBER = '#VULTITEM.SB_MBS_ITEM_NUMBER#'
  3000.                     <cfelse>
  3001.                         or SB_ITEM_NUMBER BETWEEN '#VULTITEM.SB_MBS_ITEM_NUMBER#' and '#VULTITEM.SB_MBS_ITEM_NUMBER_TO#'
  3002.                     </cfif>
  3003.                 </cfoutput>
  3004.                 )
  3005.                 ORDER BY
  3006.                     CONVERT(DATETIME, CONVERT(VARCHAR, SB_SERVICE_DATE, 103)),
  3007.                     SB_CONING_PROVIDER,
  3008.                     SB_SCHEDULE_FEE DESC,
  3009.                     SB_ITEM_NUMBER,
  3010.                     SB_ACTUAL_FEE DESC
  3011.             </cfquery>
  3012.  
  3013.             <cfif MULTIPLE_SERVICES_VASCULAR_ULTRASOUND.RECORDCOUNT GT 1>
  3014.  
  3015.                 <cfset VARIABLES.SERVICE_DATE = "">
  3016.                 <cfset VARIABLES.PROVIDER = "">
  3017.                 <cfset VARIABLES.item_count = 0>
  3018.  
  3019.                 <cfloop query="MULTIPLE_SERVICES_VASCULAR_ULTRASOUND">
  3020.  
  3021.                     <cfif VARIABLES.SERVICE_DATE eq "">
  3022.                         <cfset parsed_date = "">
  3023.                     <cfelse>
  3024.                         <cfset parsed_date = APPLICATION.dateFunctions.parseDateTimeWithNULL(VARIABLES.SERVICE_DATE)>
  3025.                     </cfif>
  3026.  
  3027.                     <!--- For the first record, the SERVICE_DATE and  PROVIDER VARIABLES will be blank
  3028.                         The second and following records may be on the same date with same provider
  3029.                     --->
  3030.                     <cfset SERVICE_DAY = DateFormat(MULTIPLE_SERVICES_VASCULAR_ULTRASOUND.SB_SERVICE_DATE,"dd-mmm-yyyy")>
  3031.                     <cfif parsed_date EQ APPLICATION.dateFunctions.parseDateTimeWithNULL(SERVICE_DAY)
  3032.                         AND VARIABLES.PROVIDER EQ MULTIPLE_SERVICES_VASCULAR_ULTRASOUND.SB_CONING_PROVIDER>
  3033.  
  3034.                         <!--- A second or subsequent record has been processed for the same day and provider --->
  3035.                         <cfset VARIABLES.item_count = VARIABLES.item_count + 1>
  3036.  
  3037.                         <cfif VARIABLES.item_count eq 1>
  3038.                             <cfset VARIABLES.reduced_rate = VULT.SB_RULE_PERCENT1 / 100>
  3039.                         <cfelseif VARIABLES.item_count eq 2>
  3040.                             <cfset VARIABLES.reduced_rate = VULT.SB_RULE_PERCENT2 / 100>
  3041.                         <cfelse>
  3042.                             <cfset VARIABLES.reduced_rate = VULT.SB_RULE_PERCENT3 / 100>
  3043.                         </cfif>
  3044.  
  3045.                         <cfquery datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#" name="UPDATE_CONING_RECORDS">
  3046.                             UPDATE SB_CONING_RUN
  3047.                                 SET SB_ACTUAL_FEE = CEILING((ISNULL(SB_ACTUAL_FEE, 0) * #VARIABLES.reduced_rate#) / 0.05) * 0.05,
  3048.                                     CONING_RULES_APPLIED = CASE WHEN LEN(ISNULL(CONING_RULES_APPLIED,'') + ',' + 'VULT') <= 100
  3049.                                                             THEN (CASE WHEN LEN(ISNULL(CONING_RULES_APPLIED,'')) > 0 THEN CONING_RULES_APPLIED + ',' + 'VULT' ELSE 'VULT' END)
  3050.                                                             ELSE CONING_RULES_APPLIED END
  3051.                             WHERE
  3052.                                 SB_CONING_ID = <cfqueryparam cfsqltype="cf_sql_numeric" value="#MULTIPLE_SERVICES_VASCULAR_ULTRASOUND.SB_CONING_ID#">
  3053.                         </cfquery>
  3054.                     <cfelse>
  3055.                         <!---  A record with a different date and provider has been processed  --->
  3056.                         <cfset VARIABLES.item_count = 1>
  3057.                         <!--- Update the service date and provider VARIABLES--->
  3058.                         <cfset VARIABLES.SB_CONING_ID = MULTIPLE_SERVICES_VASCULAR_ULTRASOUND.SB_CONING_ID>
  3059.                         <cfset VARIABLES.SERVICE_DATE = SERVICE_DAY>
  3060.                         <cfset VARIABLES.PROVIDER = MULTIPLE_SERVICES_VASCULAR_ULTRASOUND.SB_CONING_PROVIDER>
  3061.                     </cfif>
  3062.                 </cfloop>
  3063.  
  3064.             </cfif>
  3065.         </cfif>
  3066.     </cfloop>
  3067. </cfif>
  3068.  <!--- End of Multiple Services - Vascular Ultrasound --->
  3069.  
  3070.  <!--- Start of  Multiple Services - Musculo Skeletal
  3071.  
  3072. Where more than one MRI Musculo Skeletal service (59700 to 60078) is provided to the same patient by the same practitioner on
  3073. the same date of service, the following formula applies to the Schedule fee for each service:
  3074. - 100% for the item with the greatest Schedule fee
  3075. - plus 50% for each other item
  3076.  
  3077.  --->
  3078. <cfif ListFind(required_rules,'MMUS') gt 0>
  3079.     <cfloop query="Find_Coning">
  3080.         <cfif Find_Coning.SB_RULE_CODE eq 'MMUS'>
  3081.             <cfset this_service_day = DateFormat(find_coning.SERVICE_DAY,"dd/mm/yyyy")>
  3082.             <cfset this_service_day_mmm = DateFormat(find_coning.SERVICE_DAY,"dd-mmm-yyyy")>
  3083.             <cfset this_rule_id = find_coning.SB_CONING_RULE_ID>
  3084.  
  3085.             <cfquery datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#" name="MMUS">
  3086.                 select  SB_RULE_PERCENT1,
  3087.                         SB_RULE_PERCENT2,
  3088.                         SB_RULE_PERCENT3,
  3089.                         SB_CONING_RULE_ID
  3090.                 from    SB_CONING_RULE with (nolock)
  3091.                 where   SB_CONING_RULE_ID = #this_rule_id#
  3092.             </cfquery>
  3093.             <cfif MMUS.recordcount eq 0>
  3094.                 The coning run requires the MMUS (Musulo Skeletal multiple Service rule) record to be defined.
  3095.                 <cfabort>
  3096.             </cfif>
  3097.  
  3098.             <cfquery datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#" name="MMUSITEM">
  3099.                 select  SB_CONING_RULE_ITEM.SB_MBS_ITEM_NUMBER,
  3100.                         SB_CONING_RULE_ITEM.SB_MBS_ITEM_NUMBER_TO
  3101.                 from    SB_CONING_RULE_ITEM with (nolock)
  3102.                 where   SB_CONING_RULE_ID = #this_rule_id#
  3103.             </cfquery>
  3104.             <cfif MMUSITEM.recordcount eq 0>
  3105.                 The coning run requires the MMUS (Musulo Skeletal multiple Service rule) item records to be defined.
  3106.                 <cfabort>
  3107.             </cfif>
  3108.  
  3109.             <cfquery datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#" name="MULTIPLE_SERVICES_MRI_MS">
  3110.                 set dateformat dmy
  3111.                 SELECT
  3112.                     SB_CONING_ID,
  3113.                     SB_SERVICE_DATE,
  3114.                     SB_CONING_PROVIDER,
  3115.                     SB_ACTUAL_FEE,
  3116.                     SB_SCHEDULE_FEE,
  3117.                     SB_ITEM_NUMBER,
  3118.                     CONVERT(DATETIME, CONVERT(VARCHAR, SB_SERVICE_DATE, 103))
  3119.                 FROM    SB_CONING_RUN    with (nolock)
  3120.                 WHERE   SB_CONING_RUN_NO = '#VARIABLES.unique_id#'
  3121.                 AND CONVERT(DATETIME, CONVERT(VARCHAR, SB_SERVICE_DATE, 103)) = '#this_service_day#'
  3122.                 AND (1 = 2
  3123.                 <cfoutput query="MMUSITEM">
  3124.                     <cfif MMUSITEM.SB_MBS_ITEM_NUMBER_TO eq "">
  3125.                         or SB_ITEM_NUMBER = '#MMUSITEM.SB_MBS_ITEM_NUMBER#'
  3126.                     <cfelse>
  3127.                         or SB_ITEM_NUMBER BETWEEN '#MMUSITEM.SB_MBS_ITEM_NUMBER#' and '#MMUSITEM.SB_MBS_ITEM_NUMBER_TO#'
  3128.                     </cfif>
  3129.                 </cfoutput>
  3130.                 )
  3131.                 ORDER BY
  3132.                     CONVERT(DATETIME, CONVERT(VARCHAR, SB_SERVICE_DATE, 103)),
  3133.                     SB_CONING_PROVIDER,
  3134.                     SB_SCHEDULE_FEE DESC,
  3135.                     SB_ITEM_NUMBER,
  3136.                     SB_ACTUAL_FEE DESC
  3137.             </cfquery>
  3138.  
  3139.             <cfif MULTIPLE_SERVICES_MRI_MS.RECORDCOUNT GT 1>
  3140.  
  3141.                 <cfset VARIABLES.SERVICE_DATE = "">
  3142.                 <cfset VARIABLES.PROVIDER = "">
  3143.                 <cfset VARIABLES.item_count = 0>
  3144.  
  3145.                 <cfloop query="MULTIPLE_SERVICES_MRI_MS">
  3146.  
  3147.  
  3148.                     <!--- For the first record, the SERVICE_DATE and  PROVIDER VARIABLES will be blank
  3149.                         The second and following records may be on the same date with same provider
  3150.                     --->
  3151.                     <cfset SERVICE_DAY = DateFormat(MULTIPLE_SERVICES_MRI_MS.SB_SERVICE_DATE,"dd-mmm-yyyy")>
  3152.                     <cfif VARIABLES.SERVICE_DATE EQ SERVICE_DAY
  3153.                         AND VARIABLES.PROVIDER EQ MULTIPLE_SERVICES_MRI_MS.SB_CONING_PROVIDER>
  3154.  
  3155.                         <!--- A second or subsequent record has been processed for the same day and provider --->
  3156.                         <cfset VARIABLES.item_count = VARIABLES.item_count + 1>
  3157.  
  3158.                         <cfif VARIABLES.item_count eq 1>
  3159.                             <cfset VARIABLES.reduced_rate = mmus.SB_RULE_PERCENT1 / 100>
  3160.                         <cfelseif VARIABLES.item_count eq 2>
  3161.                             <cfset VARIABLES.reduced_rate = mmus.SB_RULE_PERCENT2 / 100>
  3162.                         <cfelse>
  3163.                             <cfset VARIABLES.reduced_rate = mmus.SB_RULE_PERCENT3 / 100>
  3164.                         </cfif>
  3165.  
  3166.                         <cfquery datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#" name="UPDATE_CONING_RECORDS">
  3167.                             UPDATE SB_CONING_RUN
  3168.                                 SET SB_ACTUAL_FEE = CEILING((ISNULL(SB_ACTUAL_FEE, 0) * #VARIABLES.reduced_rate#) / 0.05) * 0.05,
  3169.                                     CONING_RULES_APPLIED = CASE WHEN LEN(ISNULL(CONING_RULES_APPLIED,'') + ',' + 'MMUS') <= 100
  3170.                                                         THEN (CASE WHEN LEN(ISNULL(CONING_RULES_APPLIED,'')) > 0 THEN CONING_RULES_APPLIED + ',' + 'MMUS' ELSE 'MMUS' END)
  3171.                                                         ELSE CONING_RULES_APPLIED END
  3172.                             WHERE
  3173.                                 SB_CONING_ID = <cfqueryparam cfsqltype="cf_sql_numeric" value="#MULTIPLE_SERVICES_MRI_MS.SB_CONING_ID#">
  3174.                         </cfquery>
  3175.                     <cfelse>
  3176.                         <!---  A record with a different date and provider has been processed  --->
  3177.                         <cfset VARIABLES.item_count = 1>
  3178.                         <!--- Update the service date and provider VARIABLES--->
  3179.                         <cfset VARIABLES.SB_CONING_ID = MULTIPLE_SERVICES_MRI_MS.SB_CONING_ID>
  3180.                         <cfset VARIABLES.SERVICE_DATE = SERVICE_DAY>
  3181.                         <cfset VARIABLES.PROVIDER = MULTIPLE_SERVICES_MRI_MS.SB_CONING_PROVIDER>
  3182.                     </cfif>
  3183.                 </cfloop>
  3184.  
  3185.             </cfif>
  3186.         </cfif>
  3187.     </cfloop>
  3188. </cfif>
  3189.  <!--- End of Multiple Services - MRI Musculoskeletal --->
  3190.  
  3191.  
  3192.  <!--- Start of Musculoskeletal Ultrasound
  3193.  
  3194. Single rebate per day
  3195. Items 55800 to 55854 apply once a day for each patient regardless of the number of regions scanned.
  3196. Sort by dr  by item number. For each subsequent scan on the same day,
  3197. set the error status.
  3198. --->
  3199. <cfif ListFind(required_rules,'MULT') gt 0>
  3200.     <cfloop query="Find_Coning">
  3201.         <cfif Find_Coning.SB_RULE_CODE eq 'MULT'>
  3202.             <cfset this_service_day = DateFormat(find_coning.SERVICE_DAY,"dd/mm/yyyy")>
  3203.             <cfset this_service_day_mmm = DateFormat(find_coning.SERVICE_DAY,"dd-mmm-yyyy")>
  3204.             <cfset this_rule_id = find_coning.SB_CONING_RULE_ID>
  3205.  
  3206.             <cfquery datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#" name="MULTITEM">
  3207.                 select  SB_CONING_RULE_ITEM.SB_MBS_ITEM_NUMBER,
  3208.                         SB_CONING_RULE_ITEM.SB_MBS_ITEM_NUMBER_TO
  3209.                 from    SB_CONING_RULE_ITEM
  3210.                 where   SB_CONING_RULE_ID = #this_rule_id#
  3211.             </cfquery>
  3212.             <cfif MULTITEM.recordcount eq 0>
  3213.                 The coning run requires the MULT (Musculoskeletal Ultrasound) item records to be defined.
  3214.                 <cfabort>
  3215.             </cfif>
  3216.  
  3217.             <cfquery datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#" name="MUSCULOSKELETAL_ULTRASOUND">
  3218.                 SET DATEFORMAT DMY;
  3219.                 WITH CONING_RECORDS
  3220.                 AS (
  3221.                     #preserveSingleQuotes(CONING_RECORDS_QUERY)#
  3222.                 )
  3223.                 SELECT
  3224.                     SB_CONING_ID,
  3225.                     SERVICE_DAY,
  3226.                     SB_ITEM_NUMBER,
  3227.                     SB_CONING_ERROR
  3228.                 FROM CONING_RECORDS
  3229.                 WHERE service_day = '#DateFormat(this_service_day_mmm, "dd-mmm-yyyy")#' and
  3230.                 (1 = 2
  3231.                 <cfoutput query="MULTITEM">
  3232.                     <cfif MULTITEM.SB_MBS_ITEM_NUMBER_TO eq "">
  3233.                         or SB_ITEM_NUMBER = '#MULTITEM.SB_MBS_ITEM_NUMBER#'
  3234.                     <cfelse>
  3235.                         or SB_ITEM_NUMBER BETWEEN '#MULTITEM.SB_MBS_ITEM_NUMBER#' and '#MULTITEM.SB_MBS_ITEM_NUMBER_TO#'
  3236.                     </cfif>
  3237.                 </cfoutput>)
  3238.                 ORDER BY
  3239.                     SERVICE_DAY,
  3240.                     SB_ITEM_NUMBER
  3241.             </cfquery>
  3242.  
  3243.             <cfif MUSCULOSKELETAL_ULTRASOUND.RECORDCOUNT GT 1>
  3244.  
  3245.                 <cfset VARIABLES.SERVICE_DATE = "">
  3246.                 <cfset VARIABLES.ERROR_TEXT = "">
  3247.                 <cfset VARIABLES.SB_ITEM_NUMBER = "">
  3248.  
  3249.                 <cfloop query="MUSCULOSKELETAL_ULTRASOUND">
  3250.  
  3251.                     <cfif VARIABLES.SERVICE_DATE eq "">
  3252.                         <cfset parsed_date = "">
  3253.                     <cfelse>
  3254.                         <cfset parsed_date = APPLICATION.dateFunctions.parseDateTimeWithNULL(VARIABLES.SERVICE_DATE)>
  3255.                     </cfif>
  3256.  
  3257.                     <cfif parsed_date EQ APPLICATION.dateFunctions.parseDateTimeWithNULL(MUSCULOSKELETAL_ULTRASOUND.SERVICE_DAY)
  3258.                         and VARIABLES.SB_ITEM_NUMBER EQ MUSCULOSKELETAL_ULTRASOUND.SB_ITEM_NUMBER>
  3259.  
  3260.                         <cfset VARIABLES.ERROR_TEXT = #MUSCULOSKELETAL_ULTRASOUND.SB_CONING_ERROR# & " Item can only be claimed once per day.">
  3261.                         <cfset ARGUMENTS.coning_error_count = ARGUMENTS.coning_error_count + 1>
  3262.                         <!--- Set the error message every record that is not the first in the series for a
  3263.                         item and date --->
  3264.                         <cfquery datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#" name="MUSCULOSKELETAL_ULTRASOUND_error_text">
  3265.                             UPDATE SB_CONING_RUN
  3266.                                 SET SB_CONING_STATUS_CODE = 'ER',
  3267.                                     SB_CONING_ERROR = LEFT('#VARIABLES.ERROR_TEXT#',3000)
  3268.                             WHERE
  3269.                                 SB_CONING_ID = <cfqueryparam cfsqltype="cf_sql_numeric" value="#MUSCULOSKELETAL_ULTRASOUND.SB_CONING_ID#">
  3270.                         </cfquery>
  3271.  
  3272.                     </cfif>
  3273.                     <cfset VARIABLES.SERVICE_DATE = MUSCULOSKELETAL_ULTRASOUND.SERVICE_DAY>
  3274.                     <cfset VARIABLES.SB_ITEM_NUMBER = MUSCULOSKELETAL_ULTRASOUND.SB_ITEM_NUMBER>
  3275.  
  3276.                 </cfloop>
  3277.  
  3278.             </cfif>
  3279.  
  3280.         </cfif>
  3281.     </cfloop>
  3282. </cfif>
  3283.   <!--- End of Multiple Services - Musculoskeletal Ultrasound --->
  3284.  
  3285.   <!--- Start of CT Spine
  3286.  
  3287. If item 56233 exists, then two of 56220, 56221 and 56223 must exist for the same dr same day.
  3288. The fee for these two items will be set to zero. If two of these items do not exist, the update the
  3289. 56233 record so that sb_invoice_item.sb_outpat_error = sb_invoice_item.sb_outpat_error & " Item requires
  3290. two of 56220, 56221 and 56223", and set sb_item_status_code to 'ER'.
  3291.  
  3292. If item 56234 exists, then two of 56224, 56225 and 56226 must exist for the same dr same day.
  3293. The fee for these two items will be set to zero. If two of these items do not exist, the update the
  3294. 56234 record so that sb_invoice_item.sb_outpat_error = sb_invoice_item.sb_outpat_error & " Item requires
  3295. two of 56224, 56225 and 56226", and set sb_item_status_code to 'ER'.
  3296.  
  3297. If item 56235 exists, then two of 56227, 56228 and 56229 must exist for the same dr same day.
  3298. The fee for these two items will be set to zero. If two of these items do not exist, the update the
  3299. 56235 record so that sb_invoice_item.sb_outpat_error = sb_invoice_item.sb_outpat_error & " Item requires
  3300. two of 56227, 56228 and 56229", and set sb_item_status_code to 'ER'.
  3301.  
  3302. If item 56236 exists, then two of 56230, 56231 and 56232 must exist for the same dr same day.
  3303. The fee for these two items will be set to zero. If two of these items do not exist, the update the
  3304. 56236 record so that sb_invoice_item.sb_outpat_error = sb_invoice_item.sb_outpat_error & " Item requires
  3305. two of 56230, 56231 and 56232", and set sb_item_status_code to 'ER'.
  3306.  
  3307. =========================================================
  3308.  
  3309. Also does Radiographic Examination of the Spine (RSA and RSB)
  3310.  
  3311. If item 58112 exists, then two of 58100, 58103, 58106, 58109 must exist for the same dr same day.
  3312. The fee for these two items will be set to zero. If two of these items do not exist, then update the
  3313. 58112 record so that sb_invoice_item.sb_outpat_error = sb_invoice_item.sb_outpat_error & "
  3314. Item requires two of 58100, 58103, 58106, 58109", and set sb_item_status_code to 'ER'.
  3315. Where an item has the required dependant items, then the dependant item numbers are entered into
  3316. the message to HIC, as 'Items x, y performed'.
  3317.  
  3318. If item 58115 exists, then three of 58100, 58103, 58106, 58109 must exist for the same dr same day.
  3319. The fee for these three items will be set to zero. If three of these items do not exist, then update
  3320. the 58115 record so that sb_invoice_item.sb_outpat_error = sb_invoice_item.sb_outpat_error & "
  3321. Item requires three of 58100, 58103, 58106, 58109 ", and set sb_item_status_code to 'ER'.
  3322. Where an item has the required dependant items, then the dependant item numbers are entered into
  3323. the message to HIC, as 'Items x, y, z performed'.
  3324.  --->
  3325.  
  3326. <cfloop index="CTItem" list="CTSPA,CTSPB,CTSPC,CTSPD,RSA,RSB,C112A,C115A,C108A">
  3327.     <cfif ListFind(required_rules,CTItem) gt 0>
  3328.         <cfloop query="Find_Coning">
  3329.             <cfif Find_Coning.SB_RULE_CODE eq CTItem>
  3330.                 <cfset this_service_day = DateFormat(find_coning.SERVICE_DAY,"dd/mm/yyyy")>
  3331.                 <cfset this_service_day_mmm = DateFormat(find_coning.SERVICE_DAY,"dd-mmm-yyyy")>
  3332.                 <cfset this_rule_id = find_coning.SB_CONING_RULE_ID>
  3333.  
  3334.  
  3335.                 <!--- If item 56233 exists, then two of 56220, 56221 and 56223 must exist for the same dr same day.
  3336.             The fee for these two items will be set to zero. If two of these items do not exist, the update the
  3337.             56233 record so that sb_invoice_item.sb_outpat_error = sb_invoice_item.sb_outpat_error & " Item requires
  3338.             two of 56220, 56221 and 56223", and set sb_item_status_code to 'ER'. --->
  3339.                 <cfquery datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#" name="CTSP">
  3340.                     select  SB_MBS_ITEM_NUMBER,
  3341.                             SB_CONING_RULE_ID,
  3342.                             SB_ITEM_COUNT
  3343.                     from    SB_CONING_RULE
  3344.                     where   SB_CONING_RULE_ID = #this_rule_id#
  3345.                     and     '#this_service_day_mmm#' between SB_RULE_START_DATE
  3346.                                                  and IsNull(SB_RULE_END_DATE,'31-dec-3000')
  3347.                 </cfquery>
  3348.                 <cfif CTSP.recordcount gt 0>
  3349.  
  3350.                     <cfquery datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#" name="CTSPITEM">
  3351.                         select  SB_CONING_RULE_ITEM.SB_MBS_ITEM_NUMBER,
  3352.                                 SB_CONING_RULE_ITEM.SB_MBS_ITEM_NUMBER_TO
  3353.                         from    SB_CONING_RULE_ITEM
  3354.                         where   SB_CONING_RULE_ID = #this_rule_id#
  3355.                     </cfquery>
  3356.                     <cfif CTSPITEM.recordcount eq 0>
  3357.                         The coning run requires the <cfoutput>#CTItem#</cfoutput> (CT Spine Rule A,B,C,D) item records to be defined.
  3358.                         <cfabort>
  3359.                     </cfif>
  3360.  
  3361.                     <cfquery datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#" name="CT_SPINE_56233">
  3362.                         SET DATEFORMAT DMY;
  3363.                         WITH CONING_RECORDS
  3364.                         AS (
  3365.                             #preserveSingleQuotes(CONING_RECORDS_QUERY)#
  3366.                         )
  3367.                         SELECT
  3368.                             SB_CONING_ID,
  3369.                             SERVICE_DAY,
  3370.                             SB_CONING_PROVIDER,
  3371.                             SB_CONING_ERROR,
  3372.                             SB_CONING_INVOICE_ID
  3373.                         FROM CONING_RECORDS
  3374.                         WHERE
  3375.                             service_day = '#DateFormat(this_service_day_mmm, "dd-mmm-yyyy")#' and
  3376.                             SB_ITEM_NUMBER = '#CTSP.SB_MBS_ITEM_NUMBER#'
  3377.                         ORDER BY
  3378.                             SERVICE_DAY,
  3379.                             SB_CONING_PROVIDER
  3380.                     </cfquery>
  3381.  
  3382.                     <!--- If records exists continue processing CT Spine --->
  3383.                     <cfif CT_SPINE_56233.RECORDCOUNT GT 0 >
  3384.  
  3385.                         <cfloop query="CT_SPINE_56233">
  3386.                             <!---Modify query to check that the two combining item are in the sb_invoice_item_deleted table --->
  3387.                             <!--- Two of 56220, 56221 and 56223 must exists for the same doctor on same day --->
  3388.                             <cfquery name="CT_SPINE_56233_CHECK" datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#">
  3389.                                 SET DATEFORMAT dmy
  3390.                                 SELECT
  3391.                                     SB_MBS_ITEM_NUMBER
  3392.                                 FROM
  3393.                                     SB_INVOICE_ITEM_DELETED
  3394.                                 WHERE
  3395.                                     CONVERT(DATETIME, CONVERT(VARCHAR, SB_SERVICE_DATE, 103)) = CONVERT(DATETIME, '#DateFormat(CT_SPINE_56233.SERVICE_DAY, 'dd/MMM/yyyy')#') AND
  3396.                                     SB_INVOICE_ID = #CT_SPINE_56233.sb_coning_invoice_id# AND
  3397.                                     (1 = 2
  3398.                             <cfoutput query="CTSPITEM">
  3399.                                 <cfif CTSPITEM.SB_MBS_ITEM_NUMBER_TO eq "">
  3400.                                     or SB_MBS_ITEM_NUMBER = '#CTSPITEM.SB_MBS_ITEM_NUMBER#'
  3401.                                 <cfelse>
  3402.                                     or SB_MBS_ITEM_NUMBER BETWEEN '#CTSPITEM.SB_MBS_ITEM_NUMBER#' and '#CTSPITEM.SB_MBS_ITEM_NUMBER_TO#'
  3403.                                 </cfif>
  3404.                             </cfoutput>
  3405.                                     )
  3406.                             </cfquery>
  3407.  
  3408.                             <cfif CT_SPINE_56233_CHECK.RECORDCOUNT NEQ ctsp.SB_ITEM_COUNT>
  3409.                                 <!--- Increment the Error Count --->
  3410.                                 <cfset ARGUMENTS.coning_error_count = ARGUMENTS.coning_error_count + 1>
  3411.  
  3412.                                 <!--- Update with Error Message --->
  3413.                                 <cfset items_reqd = ''>
  3414.                                 <cfoutput query="CTSPITEM">
  3415.                                     <cfset items_reqd = Trim(items_reqd & " " & CTSPITEM.SB_MBS_ITEM_NUMBER)>
  3416.                                 </cfoutput>
  3417.                                 <cfset VARIABLES.ERROR_TEXT = CT_SPINE_56233.SB_CONING_ERROR & " Item requires #ctsp.SB_ITEM_COUNT# of #items_reqd# Rule #CTItem#">
  3418.  
  3419.                                 <cfquery datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#">
  3420.                                     UPDATE SB_CONING_RUN
  3421.                                         SET SB_CONING_ERROR = LEFT('#VARIABLES.ERROR_TEXT#',3000),
  3422.                                         SB_CONING_STATUS_CODE = <cfqueryparam cfsqltype="cf_sql_varchar" value="ER">
  3423.                                     WHERE
  3424.                                         SB_CONING_ID = <cfqueryparam cfsqltype="cf_sql_numeric" value="#CT_SPINE_56233.SB_CONING_ID#">
  3425.                                 </cfquery>
  3426.                             <cfelse>
  3427.                                 <cfset VARIABLES.ITEM_LIST = "">
  3428.                                 <cfset item_number_count = '0'>
  3429.                                 <!--- Set Price to zero --->
  3430.                                 <cfloop query="CT_SPINE_56233_CHECK">
  3431.                                     <cfif  CT_SPINE_56233_CHECK.RECORDCOUNT neq item_number_count>
  3432.                                         <cfset item_number_count = item_number_count + 1>
  3433.                                         <cfset VARIABLES.ITEM_LIST = VARIABLES.ITEM_LIST & CT_SPINE_56233_CHECK.SB_MBS_ITEM_NUMBER & ", ">
  3434.                                     <cfelse>
  3435.                                         <cfset VARIABLES.ITEM_LIST = VARIABLES.ITEM_LIST & CT_SPINE_56233_CHECK.SB_MBS_ITEM_NUMBER>
  3436.                                     </cfif>
  3437.  
  3438.                                 </cfloop>
  3439.  
  3440.                                 <!--- Update message to HIC for the Item --->
  3441.                                 <cfquery datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#">
  3442.                                     UPDATE SB_CONING_RUN
  3443.                                         SET SB_CONING_MSG_TO_HIC = 'Items ' + '#VARIABLES.ITEM_LIST#' + ' performed'
  3444.                                     WHERE
  3445.                                         SB_CONING_ID = <cfqueryparam cfsqltype="cf_sql_numeric" value="#CT_SPINE_56233.SB_CONING_ID#">
  3446.                                 </cfquery>
  3447.                             </cfif>
  3448.                         </cfloop>
  3449.                     </cfif>
  3450.                 </cfif>
  3451.             </cfif>
  3452.         </cfloop>
  3453.     </cfif>
  3454. </cfloop>
  3455.  
  3456. <!--- End of CT Spine / Radiographic Examination of the Spine Rules --->
  3457.  
  3458. <!--- Start of Myocardial Perfusion Studies
  3459.  
  3460. If items 61302 and 61303 exist for the same day, then items 61306 or 61307 should not
  3461. also exist for the same day. If 61306 or 61307 also exist, then update the 61302 record
  3462. so that sb_invoice_item.sb_outpat_error = sb_invoice_item.sb_outpat_error & " For single myocardial
  3463. perfusion studies, items 61302 and 61303 cannot coexist with 6130 or 61307", and set sb_item_status_code
  3464. to 'ER'.
  3465. Item 42731 not payable in association with items 42551 to 42566, 42569, 42698 to 42707, 42716, 42734, 42743 to 42746,
  3466. 42761 to 42767, 42815, 42857 for same dr same day.
  3467. Flag as an error, status = 'ER', message is 'Item 42731 cannot be claimed with items 42551 to 42566, 42569, 42698 to 42707, 42716,
  3468. 42734, 42743 to 42746, 42761 to 42767, 42815, 42857'.
  3469. --->
  3470.  
  3471. <cfloop index="MPSItem" list="MPS1,MPS2,MEX2,MEX3">
  3472.     <cfif ListFind(required_rules,MPSItem) gt 0>
  3473.         <cfloop query="Find_Coning">
  3474.             <cfif Find_Coning.SB_RULE_CODE eq MPSItem>
  3475.                 <cfset this_service_day = DateFormat(find_coning.SERVICE_DAY,"dd/mm/yyyy")>
  3476.                 <cfset this_service_day_mmm = DateFormat(find_coning.SERVICE_DAY,"dd-mmm-yyyy")>
  3477.                 <cfset this_rule_id = find_coning.SB_CONING_RULE_ID>
  3478.  
  3479.                 <cfquery datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#" name="MPS">
  3480.                     select  SB_MBS_ITEM_NUMBER,
  3481.                             SB_CONING_RULE_ID,
  3482.                             SB_ITEM_COUNT
  3483.                     from    SB_CONING_RULE
  3484.                     where   SB_CONING_RULE_ID = #this_rule_id#
  3485.                 </cfquery>
  3486.                 <cfif MPS.recordcount eq 0>
  3487.                     The coning run requires the <cfoutput>#MPSItem#</cfoutput> record to be defined.
  3488.                     <cfabort>
  3489.                 </cfif>
  3490.  
  3491.                 <cfquery datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#" name="MPSExclude">
  3492.                     select  SB_CONING_RULE_ITEM.SB_MBS_ITEM_NUMBER,
  3493.                             SB_CONING_RULE_ITEM.SB_MBS_ITEM_NUMBER_TO
  3494.                     from    SB_CONING_RULE_ITEM
  3495.                     where   SB_CONING_RULE_ID = #this_rule_id#
  3496.                 </cfquery>
  3497.                 <cfif MPSExclude.recordcount eq 0>
  3498.                     The coning run requires the <cfoutput>#MPSItem#</cfoutput> item records to be defined.
  3499.                     <cfabort>
  3500.                 </cfif>
  3501.  
  3502.                 <cfquery datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#" name="MPS_ITEMS">
  3503.                     SET DATEFORMAT DMY;
  3504.                     WITH CONING_RECORDS
  3505.                     AS (
  3506.                         #preserveSingleQuotes(CONING_RECORDS_QUERY)#
  3507.                     )
  3508.                     SELECT
  3509.                         SB_CONING_ID,
  3510.                         SERVICE_DAY,
  3511.                         SB_CONING_PROVIDER,
  3512.                         SB_CONING_ERROR
  3513.                     FROM CONING_RECORDS
  3514.                     WHERE
  3515.                         service_day = '#DateFormat(this_service_day_mmm, "dd-mmm-yyyy")#' and
  3516.                     (1 = 2
  3517.                         <cfoutput query="MPSExclude">
  3518.                             <cfif MPSExclude.SB_MBS_ITEM_NUMBER_TO eq "">
  3519.                                 or SB_ITEM_NUMBER = '#MPSExclude.SB_MBS_ITEM_NUMBER#'
  3520.                             <cfelse>
  3521.                                 or SB_ITEM_NUMBER BETWEEN '#MPSExclude.SB_MBS_ITEM_NUMBER#' and '#MPSExclude.SB_MBS_ITEM_NUMBER_TO#'
  3522.                             </cfif>
  3523.                         </cfoutput>)
  3524.                     ORDER BY
  3525.                         SERVICE_DAY,
  3526.                         SB_CONING_PROVIDER
  3527.                 </cfquery>
  3528.  
  3529.                 <!--- If records exists continue processing CT Spine --->
  3530.                 <cfif MPS_ITEMS.RECORDCOUNT GT 0 >
  3531.  
  3532.                     <cfloop query="MPS_ITEMS">
  3533.                         <!--- Two of 56220, 56221 and 56223 must exists for the same doctor on same day --->
  3534.                         <cfquery name="MPS_ITEMS_CHECK" datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#">
  3535.                             SET DATEFORMAT dmy
  3536.                             SELECT
  3537.                                 SB_CONING_ID,
  3538.                                 SB_ITEM_NUMBER
  3539.                             FROM
  3540.                                 SB_CONING_RUN
  3541.                             WHERE
  3542.                                 CONVERT(DATETIME, CONVERT(VARCHAR, SB_SERVICE_DATE, 103)) = CONVERT(DATETIME, '#DateFormat(MPS_ITEMS.SERVICE_DAY, 'dd/MMM/yyyy')#') AND
  3543.                                 SB_CONING_PROVIDER ='#MPS_ITEMS.SB_CONING_PROVIDER#' AND
  3544.                                 SB_ITEM_NUMBER = '#MPS.SB_MBS_ITEM_NUMBER#' AND
  3545.                                 SB_CONING_RUN_NO = <cfqueryparam cfsqltype="cf_sql_varchar" value="#VARIABLES.unique_id#">
  3546.                         </cfquery>
  3547.  
  3548.                         <cfif MPS_ITEMS_CHECK.RECORDCOUNT NEQ 0>
  3549.                             <!--- Increment the Error Count --->
  3550.                             <cfset ARGUMENTS.coning_error_count = ARGUMENTS.coning_error_count + 1>
  3551.  
  3552.                             <!--- Update with Error Message --->
  3553.                             <cfset items_excl = ''>
  3554.                             <cfoutput query="MPSExclude">
  3555.                                 <cfif MPSExclude.SB_MBS_ITEM_NUMBER_TO eq "">
  3556.                                     <cfset items_excl = trim(items_excl & " " & MPSExclude.SB_MBS_ITEM_NUMBER)>
  3557.                                 <cfelse>
  3558.                                     <cfset items_excl = trim(items_excl & " " & MPSExclude.SB_MBS_ITEM_NUMBER & " - " & MPSExclude.SB_MBS_ITEM_NUMBER_TO)>
  3559.                                 </cfif>
  3560.                             </cfoutput>
  3561.                             <cfset VARIABLES.ERROR_TEXT = MPS_ITEMS.SB_CONING_ERROR & " Item #MPS.SB_MBS_ITEM_NUMBER# cannot co-exist with #items_excl#">
  3562.  
  3563.                             <cfquery datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#">
  3564.                                 UPDATE SB_CONING_RUN
  3565.                                     SET SB_CONING_ERROR = LEFT('#VARIABLES.ERROR_TEXT#',3000),
  3566.                                     SB_CONING_STATUS_CODE = <cfqueryparam cfsqltype="cf_sql_varchar" value="ER">
  3567.                                 WHERE
  3568.                                     SB_CONING_ID = #MPS_ITEMS.SB_CONING_ID#
  3569.                             </cfquery>
  3570.                             <cfquery datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#">
  3571.                                 UPDATE SB_CONING_RUN
  3572.                                     SET SB_CONING_ERROR = LEFT('#VARIABLES.ERROR_TEXT#',3000),
  3573.                                     SB_CONING_STATUS_CODE = <cfqueryparam cfsqltype="cf_sql_varchar" value="ER">
  3574.                                 WHERE
  3575.                                     SB_CONING_ID = #MPS_ITEMS_CHECK.SB_CONING_ID#
  3576.                             </cfquery>
  3577.                         </cfif>
  3578.                     </cfloop>
  3579.                 </cfif>
  3580.             </cfif>
  3581.         </cfloop>
  3582.     </cfif>
  3583. </cfloop>
  3584.  
  3585. <!---   End of Myocardial Perfusion Studies --->
  3586.  
  3587. <!--- Start of Magnetic Resonance Imaging
  3588.  
  3589. Only one benefit can be claimed in a 12 month period for the following items:
  3590. 63000 to 63162, 63300 to 63574, 63600 to 63721, 63750 to 63756, 63870, 63900 to 63930
  3591. It does not matter which performed the service. This rule applies to each item no separately,
  3592. not as a group. E.g. It is OK for a patient to have a 63000 and 63001 in the same year.
  3593. Where a subsequent item occurs within the 12 month period, the fee for the subsequent item will be set
  3594. to zero. When doing the comparison, ignore any previous items that have zero sb_invoice_cost..
  3595.  
  3596. Only two benefits can be claimed in a 12 month period for the following items:
  3597. 63200 to 63256, 63745, 63800 to 63868.
  3598. It does not matter which performed the service. This rule applies to each item no separately,
  3599. not as a group. E.g. It is OK for a patient to have two 63200 and two 63201 in the same year.
  3600. Where a second subsequent item occurs within the 12 month period, the fee for the second subsequent
  3601. item will be set to zero. When doing the comparison, ignore any previous items that have
  3602. zero sb_invoice_cost.
  3603.  
  3604. --->
  3605. <!--- Only one benefit can be claimed in a 24 month period for the following items: 12306, 12309, 12315 and 12318. It does not matter
  3606. which Dr performed the service. This rule applies to each item no separately, not as a group.
  3607. E.g. It is OK for a patient to have
  3608. a 12306 and 12309 in the same 2 year period.
  3609.  
  3610. Where a subsequent item occurs within the 24 month period, flag it as an error,
  3611. status = 'ER', message is 'Item can only be claimed once per 24 months. Item was previously claimed on dd-mmm-yyyy' --->
  3612. <!--- Only one benefit can be claimed in a 12 month period for the following items: 12312 and 12321. It does not matter
  3613. which Dr performed the service. This rule applies to each item no separately, not as a group. E.g. It is OK for a patient to have
  3614. a 12312 and 12321in the same 12 month period. Where a subsequent item occurs within the 12 month period, flag it as an error,
  3615. status = 'ER', message is 'Item can only be claimed once per 12 months. Item was previously claimed on dd-mmm-yyyy' --->
  3616.  
  3617. <!--- >> CACHED_QUERY << --->
  3618. <cfinvoke component="pbrc-ae.components.apply_coning" method="PRDRules" returnvariable="PRDRules">
  3619. <cfset derived_list = "MRI1,MRI2,MRI3,PER24,PER12,PER4,PR242,PR244">
  3620. <cfoutput query="PRDRules">
  3621.     <cfif !listfindnocase(derived_list, #PRDRules.SB_RULE_CODE#)>
  3622.         <cfset derived_list = derived_list & ",#PRDRules.SB_RULE_CODE#">
  3623.     </cfif>
  3624. </cfoutput>
  3625.  
  3626. <cfloop index="MRIRule" list="#derived_list#">
  3627.     <cfif ListFind(required_rules,MRIRule) gt 0>
  3628.         <cfloop query="Find_Coning">
  3629.             <cfif Find_Coning.SB_RULE_CODE eq MRIRule>
  3630.                 <cfset this_service_day = DateFormat(find_coning.SERVICE_DAY,"dd/mm/yyyy")>
  3631.                 <cfset this_service_day_mmm = DateFormat(find_coning.SERVICE_DAY,"dd-mmm-yyyy")>
  3632.                 <cfset this_rule_id = find_coning.SB_CONING_RULE_ID>
  3633.  
  3634.                 <cfquery datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#" name="QMRI">
  3635.                     select  SB_MAX_ITEMS_PER_PERIOD,
  3636.                             SB_DAYS_IN_PERIOD
  3637.                     from    SB_CONING_RULE
  3638.                     where   SB_CONING_RULE_ID = #this_rule_id#
  3639.                 </cfquery>
  3640.                 <cfif QMRI.recordcount eq 0>
  3641.                     The coning run requires the <cfoutput>#MRIRule#</cfoutput> record to be defined.
  3642.                     <cfabort>
  3643.                 </cfif>
  3644.  
  3645.                 <cfquery datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#" name="QMRIItem">
  3646.                     select  SB_CONING_RULE_ITEM.SB_MBS_ITEM_NUMBER,
  3647.                             SB_CONING_RULE_ITEM.SB_MBS_ITEM_NUMBER_TO
  3648.                     from    SB_CONING_RULE_ITEM
  3649.                     where   SB_CONING_RULE_ID = #this_rule_id#
  3650.                 </cfquery>
  3651.                 <cfif QMRIItem.recordcount eq 0>
  3652.                     The coning run requires the <cfoutput>#MRIRule#</cfoutput> item records to be defined.
  3653.                     <cfabort>
  3654.                 </cfif>
  3655.                 <cfquery datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#" name="MRI">
  3656.                     SELECT
  3657.                         RUN.SB_CONING_ITEM_ID,
  3658.                         RUN.SB_ITEM_NUMBER
  3659.                     FROM
  3660.                         SB_CONING_RUN RUN INNER JOIN
  3661.                         SB_INVOICE_ITEM ITEM ON
  3662.                             RUN.SB_CONING_ITEM_ID = ITEM.SB_INVOICE_ITEM_ID INNER JOIN
  3663.                         SB_INVOICE INVOICE ON
  3664.                             ITEM.SB_INVOICE_ID = INVOICE.SB_INVOICE_ID INNER JOIN
  3665.                         SB_INVOICE INVOICE2 ON
  3666.                             INVOICE.SB_PATIENT_MRN = INVOICE2.SB_PATIENT_MRN AND
  3667.                             INVOICE.SB_HL7_SOURCE = INVOICE2.SB_HL7_SOURCE AND
  3668.                             INVOICE2.SB_BILL_MECH_CODE NOT IN ('c','co','h') INNER JOIN
  3669.                         SB_INVOICE_ITEM ITEM2 ON
  3670.                             INVOICE2.SB_INVOICE_ID = ITEM2.SB_INVOICE_ID AND
  3671.                             RUN.SB_ITEM_NUMBER = ITEM2.SB_MBS_ITEM_NUMBER AND
  3672.                             -- DO NOT COMPARE THE RECORD TO ITSELF
  3673.                             RUN.SB_CONING_ITEM_ID <> ITEM2.SB_INVOICE_ITEM_ID AND
  3674.                             -- INCLUDE ONLY CLAIMED ITEMS
  3675.                             (ITEM2.SB_ITEM_STATUS_CODE <> '98' AND ITEM2.SB_ITEM_STATUS_CODE <> '99')
  3676.                             -- INCLUDE ONLY THOSE WHERE THE DIFFERENCE IN YEARS IS LESS THAN 1
  3677.                             AND (ITEM2.SB_ITEM_STATUS_CODE < '50' OR IsNull(ITEM2.SB_HIC_AMOUNT_PAID,0) + IsNull(ITEM2.SB_FUND_AMOUNT_PAID,0) > 0)
  3678.                             AND ABS(DATEDIFF(DAY,  CONVERT(DATETIME, CONVERT(VARCHAR, ITEM2.SB_SERVICE_DATE,103)),  CONVERT(DATETIME, CONVERT(VARCHAR, RUN.SB_SERVICE_DATE,103)))) < #QMRI.SB_DAYS_IN_PERIOD#
  3679.                     WHERE
  3680.                             CONVERT(DATETIME, CONVERT(VARCHAR,run.SB_SERVICE_DATE,103)) = '#this_service_day#'
  3681.                             and     (1 = 2
  3682.                             <cfoutput query="QMRIItem">
  3683.                                 <cfif QMRIItem.SB_MBS_ITEM_NUMBER_TO eq "">
  3684.                                     or RUN.SB_ITEM_NUMBER = '#QMRIItem.SB_MBS_ITEM_NUMBER#'
  3685.                                 <cfelse>
  3686.                                     or RUN.SB_ITEM_NUMBER BETWEEN '#QMRIItem.SB_MBS_ITEM_NUMBER#' and '#QMRIItem.SB_MBS_ITEM_NUMBER_TO#'
  3687.                                 </cfif>
  3688.                             </cfoutput>
  3689.                                     ) and
  3690.                             SB_CONING_RUN_NO = <cfqueryparam cfsqltype="cf_sql_varchar" value="#VARIABLES.unique_id#">
  3691.                     GROUP BY
  3692.                         RUN.SB_CONING_ITEM_ID,
  3693.                         RUN.SB_ITEM_NUMBER
  3694.                     HAVING COUNT(ITEM2.SB_INVOICE_ITEM_ID) >= #QMRI.SB_MAX_ITEMS_PER_PERIOD#
  3695.                 </cfquery>
  3696.  
  3697.  
  3698.  
  3699.                 <cfif MRI.RECORDCOUNT NEQ 0>
  3700.                     <cfoutput query="MRI">
  3701.  
  3702.                         <cfquery datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#" name="MRI_CHECK">
  3703.                             SELECT
  3704.                                 SB_CONING_ID,
  3705.                                 SB_CONING_ERROR
  3706.                             FROM
  3707.                                 SB_CONING_RUN
  3708.                             WHERE
  3709.                                 SB_CONING_ITEM_ID = #MRI.SB_CONING_ITEM_ID# and
  3710.                                 SB_CONING_RUN_NO = '#VARIABLES.unique_id#'
  3711.                         </cfquery>
  3712.  
  3713.                         <cfset VARIABLES.ERROR_TEXT = MRI_CHECK.SB_CONING_ERROR & " Item can only be claimed #QMRI.SB_MAX_ITEMS_PER_PERIOD# times per #QMRI.SB_DAYS_IN_PERIOD# days.">
  3714.                         <cfset ARGUMENTS.coning_error_count = ARGUMENTS.coning_error_count + 1>
  3715.  
  3716.                         <cfquery datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#" name="MRI_error">
  3717.                             UPDATE SB_CONING_RUN
  3718.                                 SET SB_CONING_ERROR = LEFT('#VARIABLES.ERROR_TEXT#',3000),
  3719.                                 SB_CONING_STATUS_CODE = 'ER'
  3720.                             WHERE
  3721.                                 SB_CONING_ITEM_ID = #MRI.SB_CONING_ITEM_ID# and
  3722.                                 SB_CONING_RUN_NO = '#VARIABLES.unique_id#'
  3723.                         </cfquery>
  3724.                     </cfoutput>
  3725.                 </cfif>
  3726.             </cfif>
  3727.         </cfloop>
  3728.     </cfif>
  3729. </cfloop>
  3730.  
  3731. <cfset VARIABLES.ERROR_TEXT = "">
  3732.  
  3733. <cfif ListFind(required_rules,'MRI4') gt 0>
  3734.     <cfloop query="Find_Coning">
  3735.         <cfif Find_Coning.SB_RULE_CODE eq 'MRI4'>
  3736.             <cfset this_service_day = DateFormat(find_coning.SERVICE_DAY,"dd/mm/yyyy")>
  3737.             <cfset this_service_day_mmm = DateFormat(find_coning.SERVICE_DAY,"dd-mmm-yyyy")>
  3738.             <cfset this_rule_id = find_coning.SB_CONING_RULE_ID>
  3739.             <!--- If an item number in the range 63491 - 63497 is entered
  3740.                     an item in the range 63001 - 63473 must also be entered
  3741.          --->
  3742.  
  3743.             <cfquery datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#" name="MRI4">
  3744.                 select  SB_CONING_RULE_ITEM.SB_MBS_ITEM_NUMBER,
  3745.                         SB_CONING_RULE_ITEM.SB_MBS_ITEM_NUMBER_TO
  3746.                 from    SB_CONING_RULE_ITEM
  3747.                 where   SB_CONING_RULE_ID = #this_rule_id#
  3748.             </cfquery>
  3749.             <cfif MRI4.recordcount eq 0>
  3750.                 The coning run requires the MRI4 item records to be defined.
  3751.                 <cfabort>
  3752.             </cfif>
  3753.  
  3754.             <cfquery datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#" name="MRI5">
  3755.                 select  SB_CONING_RULE_ITEM.SB_MBS_ITEM_NUMBER,
  3756.                         SB_CONING_RULE_ITEM.SB_MBS_ITEM_NUMBER_TO
  3757.                 from    SB_CONING_RULE_ITEM,
  3758.                         SB_CONING_RULE
  3759.                 where   '#this_service_day_mmm#' between SB_RULE_START_DATE
  3760.                                                  and IsNull(SB_RULE_END_DATE,'31-dec-3000')
  3761.                   and   SB_CONING_RULE_ITEM.SB_CONING_RULE_ID = SB_CONING_RULE.SB_CONING_RULE_ID
  3762.                   and   SB_RULE_CODE = 'MRI5'
  3763.             </cfquery>
  3764.             <cfif MRI5.recordcount eq 0>
  3765.                 The coning run requires the MRI5 item records to be defined.
  3766.                 <cfabort>
  3767.             </cfif>
  3768.             <!--- RANGE 20100 TO 21997 --->
  3769.             <cfquery datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#" name="MRI4_BILLING_RANGE_1">
  3770.                 set dateformat dmy
  3771.  
  3772.                 SELECT
  3773.                     R1.SB_CONING_ID,
  3774.                     R1.SB_ITEM_NUMBER,
  3775.                     R1.SB_CONING_ERROR,
  3776.                     R2.SB_CONING_ID as required_item
  3777.                 FROM
  3778.                     SB_CONING_RUN R1
  3779.                     LEFT OUTER JOIN SB_CONING_RUN R2
  3780.                     ON R1.SB_CONING_RUN_NO = R2.SB_CONING_RUN_NO
  3781.                     AND CONVERT(DATETIME, CONVERT(VARCHAR, R1.SB_SERVICE_DATE,103))
  3782.                         = CONVERT(DATETIME, CONVERT(VARCHAR, R2.SB_SERVICE_DATE,103))
  3783.                     and
  3784.                     (1 = 2
  3785.                 <cfoutput query="MRI5">
  3786.                     <cfif MRI5.SB_MBS_ITEM_NUMBER_TO eq "">
  3787.                         or R2.SB_ITEM_NUMBER = '#MRI5.SB_MBS_ITEM_NUMBER#'
  3788.                     <cfelse>
  3789.                         or R2.SB_ITEM_NUMBER BETWEEN '#MRI5.SB_MBS_ITEM_NUMBER#' and '#MRI5.SB_MBS_ITEM_NUMBER_TO#'
  3790.                     </cfif>
  3791.                 </cfoutput>)
  3792.                 WHERE
  3793.                         R1.SB_CONING_RUN_NO = '#VARIABLES.unique_id#'
  3794.                     AND CONVERT(DATETIME, CONVERT(VARCHAR, R1.SB_SERVICE_DATE,103)) = '#this_service_day_mmm#'
  3795.                     AND (1 = 2
  3796.                 <cfoutput query="MRI4">
  3797.                     <cfif MRI4.SB_MBS_ITEM_NUMBER_TO eq "">
  3798.                         or R1.SB_ITEM_NUMBER = '#MRI4.SB_MBS_ITEM_NUMBER#'
  3799.                     <cfelse>
  3800.                         or R1.SB_ITEM_NUMBER BETWEEN '#MRI4.SB_MBS_ITEM_NUMBER#' and '#MRI4.SB_MBS_ITEM_NUMBER_TO#'
  3801.                     </cfif>
  3802.                 </cfoutput>)
  3803.             </cfquery>
  3804.  
  3805.  
  3806.             <cfset VARIABLES.ERROR_TEXT = "">
  3807.  
  3808.             <!--- Record an error for range 1 --->
  3809.             <cfloop query="MRI4_BILLING_RANGE_1">
  3810.  
  3811.                 <cfif MRI4_BILLING_RANGE_1.required_item eq "">
  3812.                     <cfset VARIABLES.ERROR_TEXT = #MRI4_BILLING_RANGE_1.SB_CONING_ERROR# & " MRI procedure requires an item from 63001 to 63473">
  3813.                     <cfset ARGUMENTS.coning_error_count = ARGUMENTS.coning_error_count + 1>
  3814.  
  3815.                     <cfquery datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#" name="MRI4_BILLING_RANGE_1_error">
  3816.                         UPDATE SB_CONING_RUN
  3817.                             SET SB_CONING_ERROR = LEFT('#VARIABLES.ERROR_TEXT#',3000),
  3818.                             SB_CONING_STATUS_CODE = <cfqueryparam cfsqltype="cf_sql_varchar" value="ER">
  3819.                         WHERE
  3820.                             SB_CONING_ID = <cfqueryparam cfsqltype="cf_sql_numeric" value="#MRI4_BILLING_RANGE_1.SB_CONING_ID#">
  3821.                     </cfquery>
  3822.                 </cfif>
  3823.             </cfloop>
  3824.  
  3825.             <cfset VARIABLES.ERROR_TEXT = "">
  3826.  
  3827.         </cfif>
  3828.     </cfloop>
  3829. </cfif>
  3830.  
  3831. <!--- Get list of user defined rules, of type LIG - Limit Services for group
  3832. Items are restricted as a group to a defined number within a given period.
  3833. Each item counts towards the total - they do not apply individually to each item. --->
  3834. <!--- >> CACHED_QUERY << --->
  3835. <cfinvoke component="pbrc-ae.components.apply_coning" method="LIGRules" returnvariable="LIGRules">
  3836. <cfset derived_list = "!">
  3837. <cfoutput query="LIGRules">
  3838.     <cfif !listfindnocase(derived_list, #LIGRules.SB_RULE_CODE#)>
  3839.         <cfset derived_list = derived_list & ",#LIGRules.SB_RULE_CODE#">
  3840.     </cfif>
  3841. </cfoutput>
  3842.  
  3843. <cfloop index="LIGRule" list="#derived_list#">
  3844.     <cfif ListFind(required_rules,LIGRule) gt 0>
  3845.         <cfloop query="Find_Coning">
  3846.             <cfif Find_Coning.SB_RULE_CODE eq LIGRule>
  3847.                 <cfset VARIABLES.ERROR_TEXT = "">
  3848.                 <cfset this_service_day = DateFormat(find_coning.SERVICE_DAY,"dd/mm/yyyy")>
  3849.                 <cfset this_service_day_mmm = DateFormat(find_coning.SERVICE_DAY,"dd-mmm-yyyy")>
  3850.                 <cfset this_rule_id = find_coning.SB_CONING_RULE_ID>
  3851.                 <cfquery datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#" name="LIG">
  3852.                     select  SB_MAX_ITEMS_PER_PERIOD,
  3853.                             SB_DAYS_IN_PERIOD
  3854.                     from    SB_CONING_RULE
  3855.                     where   SB_CONING_RULE_ID = #this_rule_id#
  3856.                 </cfquery>
  3857.                 <cfif LIG.recordcount eq 0>
  3858.                     The coning run requires the <cfoutput>#LIGRule#</cfoutput> record to be defined.
  3859.                     <cfabort>
  3860.                 </cfif>
  3861.  
  3862.                 <cfquery datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#" name="LIGItem">
  3863.                     select  SB_CONING_RULE_ITEM.SB_MBS_ITEM_NUMBER,
  3864.                             SB_CONING_RULE_ITEM.SB_MBS_ITEM_NUMBER_TO
  3865.                     from    SB_CONING_RULE_ITEM
  3866.                     where   SB_CONING_RULE_ID = #this_rule_id#
  3867.                 </cfquery>
  3868.                 <cfif LIGItem.recordcount eq 0>
  3869.                     The coning run requires the <cfoutput>#LIGRule#</cfoutput> item records to be defined.
  3870.                     <cfabort>
  3871.                 </cfif>
  3872.  
  3873. <!--- Find all items that are relevant to the edit check --->
  3874.                 <cfquery datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#" name="LIGCheck">
  3875.                     SET DATEFORMAT DMY
  3876.                     SELECT  ITEM.SB_SERVICE_DATE,
  3877.                             ITEM.sb_invoice_item_id,
  3878.                             invoice.sb_invoice_date
  3879.                     FROM
  3880.                         SB_INVOICE_ITEM ITEM
  3881.                             INNER JOIN
  3882.                         SB_INVOICE INVOICE ON
  3883.                             ITEM.SB_INVOICE_ID = INVOICE.SB_INVOICE_ID
  3884.                     WHERE
  3885.                             ITEM.SB_ITEM_STATUS_CODE <> '98'
  3886.                         AND ITEM.SB_ITEM_STATUS_CODE <> '99'
  3887.                         AND (ITEM.SB_ITEM_STATUS_CODE < '50' OR IsNull(ITEM.SB_HIC_AMOUNT_PAID,0) + IsNull(ITEM.SB_FUND_AMOUNT_PAID,0) > 0)
  3888.                         AND INVOICE.SB_PATIENT_MRN = <cfqueryparam cfsqltype="cf_sql_varchar" value="#ARGUMENTS.mrn#">
  3889.                         AND INVOICE.SB_HL7_SOURCE = <cfqueryparam cfsqltype="cf_sql_varchar" value="#ARGUMENTS.source#">
  3890.                         AND INVOICE.SB_INVOICE_ID in (#ARGUMENTS.sb_invoice_id#)
  3891.                         AND ABS(DATEDIFF(DAY,  CONVERT(DATETIME, CONVERT(VARCHAR, ITEM.SB_SERVICE_DATE,103)),  '#this_service_day#')) < #LIG.SB_DAYS_IN_PERIOD#
  3892.                         and     (1 = 2
  3893.                             <cfoutput query="LIGItem">
  3894.                                 <cfif LIGItem.SB_MBS_ITEM_NUMBER_TO eq "">
  3895.                                     or ITEM.SB_MBS_ITEM_NUMBER = '#LIGItem.SB_MBS_ITEM_NUMBER#'
  3896.                                 <cfelse>
  3897.                                     or ITEM.SB_MBS_ITEM_NUMBER BETWEEN '#LIGItem.SB_MBS_ITEM_NUMBER#' and '#LIGItem.SB_MBS_ITEM_NUMBER_TO#'
  3898.                                 </cfif>
  3899.                             </cfoutput>
  3900.                                     )
  3901.                     ORDER BY item.SB_SERVICE_DATE
  3902.                 </cfquery>
  3903. <!--- For each of these items, count the number of related items that proceed them in
  3904. the edit period concerned --->
  3905.                 <cfloop query="LIGCheck">
  3906.                     <cfquery datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#" name="LIGCount">
  3907.                         select count(*) as cnt
  3908.                         FROM
  3909.                             SB_INVOICE_ITEM ITEM
  3910.                                 INNER JOIN
  3911.                             SB_INVOICE INVOICE ON
  3912.                                 ITEM.SB_INVOICE_ID = INVOICE.SB_INVOICE_ID
  3913.                         WHERE
  3914.                                 ITEM.SB_ITEM_STATUS_CODE <> '98'
  3915.                             AND ITEM.SB_ITEM_STATUS_CODE <> '99'
  3916.                             AND (ITEM.SB_ITEM_STATUS_CODE < '50' OR IsNull(ITEM.SB_HIC_AMOUNT_PAID,0) + IsNull(ITEM.SB_FUND_AMOUNT_PAID,0) > 0)
  3917.                             AND INVOICE.SB_PATIENT_MRN = <cfqueryparam cfsqltype="cf_sql_varchar" value="#ARGUMENTS.mrn#">
  3918.                             AND INVOICE.SB_HL7_SOURCE = <cfqueryparam cfsqltype="cf_sql_varchar" value="#ARGUMENTS.source#">
  3919.                             AND INVOICE.SB_INVOICE_ID in (#ARGUMENTS.sb_invoice_id#)
  3920.                             AND ITEM.SB_SERVICE_DATE >= '#DateFormat(Dateadd("d",-1 * LIG.SB_DAYS_IN_PERIOD,LIGCheck.SB_SERVICE_DATE),"dd-mmm-yyyy")#'
  3921.                             and ITEM.SB_SERVICE_DATE <= '#DateFormat(LIGCheck.SB_SERVICE_DATE,"dd-mmm-yyyy")# 23:59:59'
  3922.                             and     (1 = 2
  3923.                                 <cfoutput query="LIGItem">
  3924.                                     <cfif LIGItem.SB_MBS_ITEM_NUMBER_TO eq "">
  3925.                                         or ITEM.SB_MBS_ITEM_NUMBER = '#LIGItem.SB_MBS_ITEM_NUMBER#'
  3926.                                     <cfelse>
  3927.                                         or ITEM.SB_MBS_ITEM_NUMBER BETWEEN '#LIGItem.SB_MBS_ITEM_NUMBER#' and '#LIGItem.SB_MBS_ITEM_NUMBER_TO#'
  3928.                                     </cfif>
  3929.                                 </cfoutput>
  3930.                                         )
  3931.                     </cfquery>
  3932.                     <cfif LIGCount.cnt gt lig.SB_MAX_ITEMS_PER_PERIOD>
  3933.                         <cfset VARIABLES.ERROR_TEXT = "Item is part of group that can only be claimed #LIG.SB_MAX_ITEMS_PER_PERIOD# times per #LIG.SB_DAYS_IN_PERIOD# days.">
  3934.                         <cfquery datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#" name="LIG_error">
  3935.                             UPDATE SB_CONING_RUN
  3936.                                 SET SB_CONING_ERROR = LEFT('#VARIABLES.ERROR_TEXT#',3000),
  3937.                                 SB_CONING_STATUS_CODE = 'ER'
  3938.                             WHERE
  3939.                                 SB_CONING_RUN_NO = '#VARIABLES.unique_id#' AND
  3940.                                 SB_CONING_STATUS_CODE <> 'ER'
  3941.                             AND (1 = 2
  3942.                                 <cfoutput query="LIGItem">
  3943.                                     <cfif LIGItem.SB_MBS_ITEM_NUMBER_TO eq "">
  3944.                                         or SB_ITEM_NUMBER = '#LIGItem.SB_MBS_ITEM_NUMBER#'
  3945.                                     <cfelse>
  3946.                                         or SB_ITEM_NUMBER BETWEEN '#LIGItem.SB_MBS_ITEM_NUMBER#' and '#LIGItem.SB_MBS_ITEM_NUMBER_TO#'
  3947.                                     </cfif>
  3948.                                 </cfoutput>
  3949.                                 )
  3950.                         </cfquery>
  3951.  
  3952.                         <cfbreak>
  3953.                     </cfif>
  3954.                 </cfloop>
  3955.             </cfif>
  3956.         </cfloop>
  3957.     </cfif>
  3958. </cfloop>
  3959. <!--- End of Magnetic Resonance Imaging --->
  3960.  
  3961. <!--- Start of  Anaesthetic Billing Rules
  3962.  
  3963. If at least 1 item in the range 20100 to 21997 is entered, then at least one item in the range 23010 to 24136 must also exist,
  3964. and vice versa. If an item in one range exists without an item in the other range,  then update the item so that
  3965. sb_invoice_item.sb_outpat_error = sb_invoice_item.sb_outpat_error & "Anaesthetic accounts require an item from 20100 to 21997,
  3966. and from 23010 to 24136", and set sb_item_status_code to 'ER'.
  3967.  
  3968. If an item number in the range 18213 - 18370 is entered and an item between 20100 - 25205 has already been raised/entered for the
  3969. same servicing provider,  then update the item so that sb_invoice_item.sb_outpat_error = sb_invoice_item.sb_outpat_error &
  3970. "Associated item already raised'", and set sb_item_status_code to 'ER'.
  3971.  
  3972. Check that no more than one item exists in the range 25000 - 25020. If more than one exists, flag both as an error, status = 'ER',
  3973. message is 'Only one item in the range 25000 - 25020 may be entered'.
  3974.  
  3975. Check that no more than one item exists in the range 23010 - 24136. If more than one exists, flag both as an error, status = 'ER',
  3976. message is 'Only one item in the range 23010 - 24136 may be entered'.
  3977. --->
  3978. <cfset VARIABLES.ERROR_TEXT = "">
  3979.  
  3980. <cfif ListFind(required_rules,'ANAE1') gt 0>
  3981.     <cfloop query="Find_Coning">
  3982.         <cfif Find_Coning.SB_RULE_CODE eq 'ANAE1'>
  3983.             <cfset this_service_day = DateFormat(find_coning.SERVICE_DAY,"dd/mm/yyyy")>
  3984.             <cfset this_service_day_mmm = DateFormat(find_coning.SERVICE_DAY,"dd-mmm-yyyy")>
  3985.             <cfset this_rule_id = find_coning.SB_CONING_RULE_ID>
  3986.             <!--- If an item number in the range 18213 - 18370 is entered and an item between 20100 - 25205 has already been raised/entered for the
  3987.         same servicing provider,  then update the item so that sb_invoice_item.sb_outpat_error = sb_invoice_item.sb_outpat_error &
  3988.         "Associated item already raised'", and set sb_item_status_code to 'ER'. --->
  3989.  
  3990.             <cfquery datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#" name="ANAE1">
  3991.                 select  SB_CONING_RULE_ITEM.SB_MBS_ITEM_NUMBER,
  3992.                         SB_CONING_RULE_ITEM.SB_MBS_ITEM_NUMBER_TO
  3993.                 from    SB_CONING_RULE_ITEM
  3994.                 where   SB_CONING_RULE_ID = #this_rule_id#
  3995.             </cfquery>
  3996.             <cfif ANAE1.recordcount eq 0>
  3997.                 The coning run requires the ANAE1 item records to be defined.
  3998.                 <cfabort>
  3999.             </cfif>
  4000.  
  4001.             <cfquery datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#" name="ANAE2">
  4002.                 select  SB_CONING_RULE_ITEM.SB_MBS_ITEM_NUMBER,
  4003.                         SB_CONING_RULE_ITEM.SB_MBS_ITEM_NUMBER_TO
  4004.                 from    SB_CONING_RULE_ITEM,
  4005.                         SB_CONING_RULE
  4006.                 where   '#this_service_day_mmm#' between SB_RULE_START_DATE
  4007.                                                  and IsNull(SB_RULE_END_DATE,'31-dec-3000')
  4008.                   and   SB_CONING_RULE_ITEM.SB_CONING_RULE_ID = SB_CONING_RULE.SB_CONING_RULE_ID
  4009.                   and   SB_RULE_CODE = 'ANAE2'
  4010.             </cfquery>
  4011.             <cfif ANAE2.recordcount eq 0>
  4012.                 The coning run requires the ANAE2 item records to be defined.
  4013.                 <cfabort>
  4014.             </cfif>
  4015.             <!--- RANGE 20100 TO 21997 --->
  4016.             <cfquery datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#" name="ANAESTHETIC_BILLING_RANGE_1">
  4017.                 set dateformat dmy
  4018.  
  4019.                 SELECT
  4020.                     R1.SB_CONING_ID,
  4021.                     R1.SB_ITEM_NUMBER,
  4022.                     R1.SB_CONING_ERROR,
  4023.                     R2.SB_CONING_ID as required_item
  4024.                 FROM
  4025.                     SB_CONING_RUN R1
  4026.                     LEFT OUTER JOIN SB_CONING_RUN R2
  4027.                     ON R1.SB_CONING_RUN_NO = R2.SB_CONING_RUN_NO
  4028.                     AND CONVERT(DATETIME, CONVERT(VARCHAR, R1.SB_SERVICE_DATE,103))
  4029.                         = CONVERT(DATETIME, CONVERT(VARCHAR, R2.SB_SERVICE_DATE,103))
  4030.                     and
  4031.                     (1 = 2
  4032.                 <cfoutput query="ANAE2">
  4033.                     <cfif ANAE2.SB_MBS_ITEM_NUMBER_TO eq "">
  4034.                         or R2.SB_ITEM_NUMBER = '#ANAE2.SB_MBS_ITEM_NUMBER#'
  4035.                     <cfelse>
  4036.                         or R2.SB_ITEM_NUMBER BETWEEN '#ANAE2.SB_MBS_ITEM_NUMBER#' and '#ANAE2.SB_MBS_ITEM_NUMBER_TO#'
  4037.                     </cfif>
  4038.                 </cfoutput>)
  4039.                 WHERE
  4040.                         R1.SB_CONING_RUN_NO = '#VARIABLES.unique_id#'
  4041.                     AND CONVERT(DATETIME, CONVERT(VARCHAR, R1.SB_SERVICE_DATE,103)) = '#this_service_day_mmm#'
  4042.                     AND (1 = 2
  4043.                 <cfoutput query="ANAE1">
  4044.                     <cfif ANAE1.SB_MBS_ITEM_NUMBER_TO eq "">
  4045.                         or R1.SB_ITEM_NUMBER = '#ANAE1.SB_MBS_ITEM_NUMBER#'
  4046.                     <cfelse>
  4047.                         or R1.SB_ITEM_NUMBER BETWEEN '#ANAE1.SB_MBS_ITEM_NUMBER#' and '#ANAE1.SB_MBS_ITEM_NUMBER_TO#'
  4048.                     </cfif>
  4049.                 </cfoutput>)
  4050.             </cfquery>
  4051.  
  4052.  
  4053.             <cfset VARIABLES.ERROR_TEXT = "">
  4054.  
  4055.             <!--- Record an error for range 1 --->
  4056.             <cfloop query="ANAESTHETIC_BILLING_RANGE_1">
  4057.  
  4058.                 <cfif ANAESTHETIC_BILLING_RANGE_1.required_item eq "">
  4059.                     <cfset VARIABLES.ERROR_TEXT = #ANAESTHETIC_BILLING_RANGE_1.SB_CONING_ERROR# & " Anaesthetic accounts require an item from 20100 to 21997, and from 23010  to 24136" >
  4060.                     <cfset ARGUMENTS.coning_error_count = ARGUMENTS.coning_error_count + 1>
  4061.  
  4062.                     <cfquery datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#" name="ANAESTHETIC_BILLING_RANGE_1_error">
  4063.                         UPDATE SB_CONING_RUN
  4064.                             SET SB_CONING_ERROR = LEFT('#VARIABLES.ERROR_TEXT#',3000),
  4065.                             SB_CONING_STATUS_CODE = <cfqueryparam cfsqltype="cf_sql_varchar" value="ER">
  4066.                         WHERE
  4067.                             SB_CONING_ID = <cfqueryparam cfsqltype="cf_sql_numeric" value="#ANAESTHETIC_BILLING_RANGE_1.SB_CONING_ID#">
  4068.                     </cfquery>
  4069.                 </cfif>
  4070.             </cfloop>
  4071.  
  4072.             <cfset VARIABLES.ERROR_TEXT = "">
  4073.  
  4074.         </cfif>
  4075.     </cfloop>
  4076. </cfif>
  4077.  
  4078. <cfset VARIABLES.ERROR_TEXT = "">
  4079.  
  4080. <cfif ListFind(required_rules,'ANAE2') gt 0>
  4081.     <cfloop query="Find_Coning">
  4082.         <cfif Find_Coning.SB_RULE_CODE eq 'ANAE2'>
  4083.             <cfset this_service_day = DateFormat(find_coning.SERVICE_DAY,"dd/mm/yyyy")>
  4084.             <cfset this_service_day_mmm = DateFormat(find_coning.SERVICE_DAY,"dd-mmm-yyyy")>
  4085.             <cfset this_rule_id = find_coning.SB_CONING_RULE_ID>
  4086.  
  4087.             <cfquery datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#" name="ANAE2">
  4088.                 select  SB_CONING_RULE_ITEM.SB_MBS_ITEM_NUMBER,
  4089.                         SB_CONING_RULE_ITEM.SB_MBS_ITEM_NUMBER_TO
  4090.                 from    SB_CONING_RULE_ITEM
  4091.                 where   SB_CONING_RULE_ID = #this_rule_id#
  4092.             </cfquery>
  4093.             <cfif ANAE2.recordcount eq 0>
  4094.                 The coning run requires the ANAE2 item records to be defined.
  4095.                 <cfabort>
  4096.             </cfif>
  4097.  
  4098.             <cfquery datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#" name="ANAE1">
  4099.                 select  SB_CONING_RULE_ITEM.SB_MBS_ITEM_NUMBER,
  4100.                         SB_CONING_RULE_ITEM.SB_MBS_ITEM_NUMBER_TO
  4101.                 from    SB_CONING_RULE_ITEM,
  4102.                         SB_CONING_RULE
  4103.                 where   '#this_service_day_mmm#' between SB_RULE_START_DATE
  4104.                                                  and IsNull(SB_RULE_END_DATE,'31-dec-3000')
  4105.                   and   SB_CONING_RULE_ITEM.SB_CONING_RULE_ID = SB_CONING_RULE.SB_CONING_RULE_ID
  4106.                   and   SB_RULE_CODE = 'ANAE1'
  4107.             </cfquery>
  4108.             <cfif ANAE1.recordcount eq 0>
  4109.                 The coning run requires the ANAE1 item records to be defined.
  4110.                 <cfabort>
  4111.             </cfif>
  4112.  
  4113.             <cfquery datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#" name="ANAESTHETIC_BILLING_RANGE_2">
  4114.                 SELECT
  4115.                     R1.SB_CONING_ID,
  4116.                     R1.SB_ITEM_NUMBER,
  4117.                     R1.SB_CONING_ERROR,
  4118.                     R2.SB_CONING_ID as required_item
  4119.                 FROM
  4120.                     SB_CONING_RUN R1
  4121.                     LEFT OUTER JOIN SB_CONING_RUN R2
  4122.                     ON R1.SB_CONING_RUN_NO = R2.SB_CONING_RUN_NO
  4123.                     AND CONVERT(DATETIME, CONVERT(VARCHAR, R1.SB_SERVICE_DATE,103))
  4124.                         = CONVERT(DATETIME, CONVERT(VARCHAR, R2.SB_SERVICE_DATE,103))
  4125.                     AND (1 = 2
  4126.                 <cfoutput query="ANAE1">
  4127.                     <cfif ANAE1.SB_MBS_ITEM_NUMBER_TO eq "">
  4128.                         or R2.SB_ITEM_NUMBER = '#ANAE1.SB_MBS_ITEM_NUMBER#'
  4129.                     <cfelse>
  4130.                         or R2.SB_ITEM_NUMBER BETWEEN '#ANAE1.SB_MBS_ITEM_NUMBER#' and '#ANAE1.SB_MBS_ITEM_NUMBER_TO#'
  4131.                     </cfif>
  4132.                 </cfoutput>)
  4133.                 WHERE
  4134.                         R1.SB_CONING_RUN_NO = '#VARIABLES.unique_id#'
  4135.                     AND CONVERT(DATETIME, CONVERT(VARCHAR, R1.SB_SERVICE_DATE,103)) = '#this_service_day_mmm#'
  4136.                     AND (1 = 2
  4137.                 <cfoutput query="ANAE2">
  4138.                     <cfif ANAE2.SB_MBS_ITEM_NUMBER_TO eq "">
  4139.                         or R1.SB_ITEM_NUMBER = '#ANAE2.SB_MBS_ITEM_NUMBER#'
  4140.                     <cfelse>
  4141.                         or R1.SB_ITEM_NUMBER BETWEEN '#ANAE2.SB_MBS_ITEM_NUMBER#' and '#ANAE2.SB_MBS_ITEM_NUMBER_TO#'
  4142.                     </cfif>
  4143.                 </cfoutput>)
  4144.             </cfquery>
  4145.  
  4146.             <!--- Record an error for range 2 --->
  4147.             <cfloop query="ANAESTHETIC_BILLING_RANGE_2">
  4148.  
  4149.                 <cfif ANAESTHETIC_BILLING_RANGE_2.required_item eq "">
  4150.  
  4151.                     <cfset VARIABLES.ERROR_TEXT = #ANAESTHETIC_BILLING_RANGE_2.SB_CONING_ERROR# & " Anaesthetic accounts require an item from 20100 to 21997, and from 23010  to 24136" >
  4152.                     <cfset ARGUMENTS.coning_error_count = ARGUMENTS.coning_error_count + 1>
  4153.  
  4154.                     <cfquery datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#" name="ANAESTHETIC_BILLING_RANGE_2_error">
  4155.                         UPDATE SB_CONING_RUN
  4156.                             SET SB_CONING_ERROR = LEFT('#VARIABLES.ERROR_TEXT#',3000),
  4157.                             SB_CONING_STATUS_CODE = <cfqueryparam cfsqltype="cf_sql_varchar" value="ER">
  4158.                         WHERE
  4159.                             SB_CONING_ID = <cfqueryparam cfsqltype="cf_sql_numeric" value="#ANAESTHETIC_BILLING_RANGE_2.SB_CONING_ID#">
  4160.                     </cfquery>
  4161.                 </cfif>
  4162.             </cfloop>
  4163.         </cfif>
  4164.     </cfloop>
  4165. </cfif>
  4166.  
  4167. <cfset VARIABLES.ERROR_TEXT = "">
  4168.  
  4169. <cfif ListFind(required_rules,'ANAE3') gt 0>
  4170.     <cfloop query="Find_Coning">
  4171.         <cfif Find_Coning.SB_RULE_CODE eq 'ANAE3'>
  4172.             <cfset this_service_day = DateFormat(find_coning.SERVICE_DAY,"dd/mm/yyyy")>
  4173.             <cfset this_service_day_mmm = DateFormat(find_coning.SERVICE_DAY,"dd-mmm-yyyy")>
  4174.             <cfset this_rule_id = find_coning.SB_CONING_RULE_ID>
  4175.  
  4176.             <cfquery datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#" name="ANAE3">
  4177.                 select  SB_CONING_RULE_ITEM.SB_MBS_ITEM_NUMBER,
  4178.                         SB_CONING_RULE_ITEM.SB_MBS_ITEM_NUMBER_TO
  4179.                 from    SB_CONING_RULE_ITEM
  4180.                 where   SB_CONING_RULE_ID = #this_rule_id#
  4181.             </cfquery>
  4182.             <cfif ANAE3.recordcount eq 0>
  4183.                 The coning run requires the ANAE3 item records to be defined.
  4184.                 <cfabort>
  4185.             </cfif>
  4186.  
  4187.             <cfquery datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#" name="ANAE4">
  4188.                 select  SB_CONING_RULE_ITEM.SB_MBS_ITEM_NUMBER,
  4189.                         SB_CONING_RULE_ITEM.SB_MBS_ITEM_NUMBER_TO
  4190.                 from    SB_CONING_RULE_ITEM,
  4191.                         SB_CONING_RULE
  4192.                 where   '#this_service_day_mmm#' between SB_RULE_START_DATE
  4193.                                                  and IsNull(SB_RULE_END_DATE,'31-dec-3000')
  4194.                   and   SB_CONING_RULE_ITEM.SB_CONING_RULE_ID = SB_CONING_RULE.SB_CONING_RULE_ID
  4195.                   and   SB_RULE_CODE = 'ANAE4'
  4196.             </cfquery>
  4197.             <cfif ANAE4.recordcount eq 0>
  4198.                 The coning run requires the ANAE4 item records to be defined.
  4199.                 <cfabort>
  4200.             </cfif>
  4201.  
  4202.  
  4203.             <!--- If an item number in the range 18213 - 18370 is entered and an item between 20100 - 25205 has already been raised/entered for the
  4204.         same servicing provider,  then update the item so that sb_invoice_item.sb_outpat_error = sb_invoice_item.sb_outpat_error &
  4205.         "Associated item already raised'", and set sb_item_status_code to 'ER'. --->
  4206.             <!--- RANGE 18213 TO 18370 --->
  4207.             <cfquery datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#" name="ANAESTHETIC_BILLING_RANGE_3">
  4208.                 SET DATEFORMAT DMY;
  4209.                 WITH CONING_RECORDS
  4210.                 AS (
  4211.                     #preserveSingleQuotes(CONING_RECORDS_QUERY)#
  4212.                 )
  4213.                 SELECT
  4214.                     SB_CONING_ID,
  4215.                     SB_CONING_ERROR,
  4216.                     SB_CONING_PROVIDER,
  4217.                     SERVICE_DAY
  4218.                 FROM
  4219.                     CONING_RECORDS
  4220.                 WHERE
  4221.                     service_day = '#DateFormat(this_service_day_mmm, "dd-mmm-yyyy")#'
  4222.                     and (1 = 2
  4223.                 <cfoutput query="ANAE3">
  4224.                     <cfif ANAE3.SB_MBS_ITEM_NUMBER_TO eq "">
  4225.                         or SB_ITEM_NUMBER = '#ANAE3.SB_MBS_ITEM_NUMBER#'
  4226.                     <cfelse>
  4227.                         or SB_ITEM_NUMBER BETWEEN '#ANAE3.SB_MBS_ITEM_NUMBER#' and '#ANAE3.SB_MBS_ITEM_NUMBER_TO#'
  4228.                     </cfif>
  4229.                 </cfoutput>)
  4230.             </cfquery>
  4231.  
  4232.             <cfset VARIABLES.ERROR_TEXT = "">
  4233.  
  4234.             <cfif   ANAESTHETIC_BILLING_RANGE_3.RECORDCOUNT GT 0>
  4235.                 <cfloop query="ANAESTHETIC_BILLING_RANGE_3">
  4236.                     <!--- Check to see if there is an item in the range 20100 to 25205 --->
  4237.                     <cfquery name="ANAESTHETIC_BILLING_RANGE_3_CHECK"  datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#">
  4238.                         SELECT
  4239.                             COUNT(SB_CONING_ID) AS ALREADY_RAISED
  4240.                         FROM
  4241.                             SB_CONING_RUN
  4242.                         WHERE SB_CONING_RUN_NO = <cfqueryparam cfsqltype="cf_sql_varchar" value="#VARIABLES.unique_id#">
  4243.                         and   CONVERT(DATETIME, CONVERT(VARCHAR, SB_SERVICE_DATE,103)) = '#DateFormat(ANAESTHETIC_BILLING_RANGE_3.SERVICE_DAY,"dd-mmm-yyyy")#'
  4244.                         and   CONVERT(DATETIME, CONVERT(VARCHAR, SB_SERVICE_DATE,103)) = '#this_service_day_mmm#'
  4245.                         AND (1 = 2
  4246.                         <cfoutput query="ANAE4">
  4247.                             <cfif ANAE4.SB_MBS_ITEM_NUMBER_TO eq "">
  4248.                                 or SB_ITEM_NUMBER = '#ANAE4.SB_MBS_ITEM_NUMBER#'
  4249.                             <cfelse>
  4250.                                 or SB_ITEM_NUMBER BETWEEN '#ANAE4.SB_MBS_ITEM_NUMBER#' and '#ANAE4.SB_MBS_ITEM_NUMBER_TO#'
  4251.                             </cfif>
  4252.                         </cfoutput>
  4253.                         ) and
  4254.                             SB_CONING_PROVIDER = '#ANAESTHETIC_BILLING_RANGE_3.SB_CONING_PROVIDER#'
  4255.                     </cfquery>
  4256.  
  4257.                     <cfif ANAESTHETIC_BILLING_RANGE_3_CHECK.ALREADY_RAISED GT 0>
  4258.  
  4259.                         <cfset VARIABLES.ERROR_TEXT = #ANAESTHETIC_BILLING_RANGE_3.SB_CONING_ERROR# & " Associated items already raised. ( 20100 - 25205 ) cannot be used with (18213 - 18370)." >
  4260.                         <cfset ARGUMENTS.coning_error_count = ARGUMENTS.coning_error_count + 1>
  4261.  
  4262.                         <cfquery datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#" name="ANAESTHETIC_BILLING_RANGE_3_error">
  4263.                             UPDATE SB_CONING_RUN
  4264.                                 SET SB_CONING_ERROR = LEFT('#VARIABLES.ERROR_TEXT#',3000),
  4265.                                 SB_CONING_STATUS_CODE = <cfqueryparam cfsqltype="cf_sql_varchar" value="ER">
  4266.                             WHERE
  4267.                                 SB_CONING_ID = <cfqueryparam cfsqltype="cf_sql_numeric" value="#ANAESTHETIC_BILLING_RANGE_3.SB_CONING_ID#">
  4268.                         </cfquery>
  4269.                     </cfif>
  4270.                 </cfloop>
  4271.             </cfif>
  4272.         </cfif>
  4273.     </cfloop>
  4274. </cfif>
  4275.  
  4276. <cfset VARIABLES.ERROR_TEXT = "">
  4277.  
  4278. <cfif ListFind(required_rules,'ANAE4') gt 0>
  4279.     <cfloop query="Find_Coning">
  4280.         <cfif Find_Coning.SB_RULE_CODE eq 'ANAE4'>
  4281.             <cfset this_service_day = DateFormat(find_coning.SERVICE_DAY,"dd/mm/yyyy")>
  4282.             <cfset this_service_day_mmm = DateFormat(find_coning.SERVICE_DAY,"dd-mmm-yyyy")>
  4283.             <cfset this_rule_id = find_coning.SB_CONING_RULE_ID>
  4284.  
  4285.             <cfquery datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#" name="ANAE4">
  4286.                 select  SB_CONING_RULE_ITEM.SB_MBS_ITEM_NUMBER,
  4287.                         SB_CONING_RULE_ITEM.SB_MBS_ITEM_NUMBER_TO
  4288.                 from    SB_CONING_RULE_ITEM
  4289.                 where   SB_CONING_RULE_ID = #this_rule_id#
  4290.             </cfquery>
  4291.             <cfif ANAE4.recordcount eq 0>
  4292.                 The coning run requires the ANAE4 item records to be defined.
  4293.                 <cfabort>
  4294.             </cfif>
  4295.  
  4296.             <cfquery datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#" name="ANAE3">
  4297.                 select  SB_CONING_RULE_ITEM.SB_MBS_ITEM_NUMBER,
  4298.                         SB_CONING_RULE_ITEM.SB_MBS_ITEM_NUMBER_TO
  4299.                 from    SB_CONING_RULE_ITEM,
  4300.                         SB_CONING_RULE
  4301.                 where   '#this_service_day_mmm#' between SB_RULE_START_DATE
  4302.                                                  and IsNull(SB_RULE_END_DATE,'31-dec-3000')
  4303.                   and   SB_CONING_RULE_ITEM.SB_CONING_RULE_ID = SB_CONING_RULE.SB_CONING_RULE_ID
  4304.                   and   SB_RULE_CODE = 'ANAE3'
  4305.             </cfquery>
  4306.             <cfif ANAE3.recordcount eq 0>
  4307.                 The coning run requires the ANAE3 item records to be defined.
  4308.                 <cfabort>
  4309.             </cfif>
  4310.  
  4311.             <cfquery datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#" name="ANAESTHETIC_BILLING_RANGE_4">
  4312.                 SET DATEFORMAT DMY;
  4313.                 WITH CONING_RECORDS
  4314.                 AS (
  4315.                     #preserveSingleQuotes(CONING_RECORDS_QUERY)#
  4316.                 )
  4317.                 SELECT
  4318.                     SB_CONING_ID,
  4319.                     SB_CONING_ERROR,
  4320.                     SB_CONING_PROVIDER,
  4321.                     SERVICE_DAY
  4322.                 FROM
  4323.                     CONING_RECORDS
  4324.                 WHERE
  4325.                     service_day = '#DateFormat(this_service_day_mmm, "dd-mmm-yyyy")#'
  4326.                     and (1 = 2
  4327.                 <cfoutput query="ANAE4">
  4328.                     <cfif ANAE4.SB_MBS_ITEM_NUMBER_TO eq "">
  4329.                         or SB_ITEM_NUMBER = '#ANAE4.SB_MBS_ITEM_NUMBER#'
  4330.                     <cfelse>
  4331.                         or SB_ITEM_NUMBER BETWEEN '#ANAE4.SB_MBS_ITEM_NUMBER#' and '#ANAE4.SB_MBS_ITEM_NUMBER_TO#'
  4332.                     </cfif>
  4333.                 </cfoutput>)
  4334.             </cfquery>
  4335.  
  4336.             <cfset VARIABLES.ERROR_TEXT = "">
  4337.  
  4338.             <cfif   ANAESTHETIC_BILLING_RANGE_4.RECORDCOUNT GT 0>
  4339.                 <cfloop query="ANAESTHETIC_BILLING_RANGE_4">
  4340.                     <!--- Check to see if there is an item in the range 20100 to 25205 --->
  4341.                     <cfquery name="ANAESTHETIC_BILLING_RANGE_4_CHECK"  datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#">
  4342.                         SELECT
  4343.                             COUNT(SB_CONING_ID) AS ALREADY_RAISED
  4344.                         FROM
  4345.                             SB_CONING_RUN
  4346.                         WHERE SB_CONING_RUN_NO = <cfqueryparam cfsqltype="cf_sql_varchar" value="#VARIABLES.unique_id#">
  4347.                         and   CONVERT(DATETIME, CONVERT(VARCHAR, SB_SERVICE_DATE,103)) = '#DateFormat(ANAESTHETIC_BILLING_RANGE_4.SERVICE_DAY,"dd-mmm-yyyy")#'
  4348.                         and   CONVERT(DATETIME, CONVERT(VARCHAR, SB_SERVICE_DATE,103)) = '#this_service_day_mmm#'
  4349.                         AND (1 = 2
  4350.                         <cfoutput query="ANAE3">
  4351.                             <cfif ANAE3.SB_MBS_ITEM_NUMBER_TO eq "">
  4352.                                 or SB_ITEM_NUMBER = '#ANAE3.SB_MBS_ITEM_NUMBER#'
  4353.                             <cfelse>
  4354.                                 or SB_ITEM_NUMBER BETWEEN '#ANAE3.SB_MBS_ITEM_NUMBER#' and '#ANAE3.SB_MBS_ITEM_NUMBER_TO#'
  4355.                             </cfif>
  4356.                         </cfoutput>
  4357.                         ) and
  4358.                             SB_CONING_PROVIDER = '#ANAESTHETIC_BILLING_RANGE_4.SB_CONING_PROVIDER#'
  4359.                     </cfquery>
  4360.  
  4361.                     <cfif ANAESTHETIC_BILLING_RANGE_4_CHECK.ALREADY_RAISED GT 0>
  4362.  
  4363.                         <cfset VARIABLES.ERROR_TEXT = #ANAESTHETIC_BILLING_RANGE_4.SB_CONING_ERROR# & " Associated item ( 20100 - 25205 ) already raised. " >
  4364.                         <cfset ARGUMENTS.coning_error_count = ARGUMENTS.coning_error_count + 1>
  4365.  
  4366.                         <cfquery datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#" name="ANAESTHETIC_BILLING_RANGE_4_error">
  4367.                             UPDATE SB_CONING_RUN
  4368.                                 SET SB_CONING_ERROR = LEFT('#VARIABLES.ERROR_TEXT#',3000),
  4369.                                 SB_CONING_STATUS_CODE = <cfqueryparam cfsqltype="cf_sql_varchar" value="ER">
  4370.                             WHERE
  4371.                                 SB_CONING_ID = <cfqueryparam cfsqltype="cf_sql_numeric" value="#ANAESTHETIC_BILLING_RANGE_4.SB_CONING_ID#">
  4372.                         </cfquery>
  4373.                     </cfif>
  4374.                 </cfloop>
  4375.             </cfif>
  4376.         </cfif>
  4377.     </cfloop>
  4378. </cfif>
  4379.  
  4380. <cfset VARIABLES.ERROR_TEXT = "">
  4381.  
  4382. <cfif ListFind(required_rules,'ANAE5') gt 0>
  4383.     <cfloop query="Find_Coning">
  4384.         <cfif Find_Coning.SB_RULE_CODE eq 'ANAE5'>
  4385.             <cfset this_service_day = DateFormat(find_coning.SERVICE_DAY,"dd/mm/yyyy")>
  4386.             <cfset this_service_day_mmm = DateFormat(find_coning.SERVICE_DAY,"dd-mmm-yyyy")>
  4387.             <cfset this_rule_id = find_coning.SB_CONING_RULE_ID>
  4388.  
  4389.             <cfquery datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#" name="ANAE5">
  4390.                 select  SB_CONING_RULE_ITEM.SB_MBS_ITEM_NUMBER,
  4391.                         SB_CONING_RULE_ITEM.SB_MBS_ITEM_NUMBER_TO
  4392.                 from    SB_CONING_RULE_ITEM
  4393.                 where   SB_CONING_RULE_ID = #this_rule_id#
  4394.             </cfquery>
  4395.             <cfif ANAE5.recordcount eq 0>
  4396.                 The coning run requires the ANAE5 item records to be defined.
  4397.                 <cfabort>
  4398.             </cfif>
  4399.  
  4400.             <cfquery datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#" name="ANAE6">
  4401.                 select  SB_CONING_RULE_ITEM.SB_MBS_ITEM_NUMBER,
  4402.                         SB_CONING_RULE_ITEM.SB_MBS_ITEM_NUMBER_TO
  4403.                 from    SB_CONING_RULE_ITEM,
  4404.                         SB_CONING_RULE
  4405.                 where   '#this_service_day_mmm#' between SB_RULE_START_DATE
  4406.                                                  and IsNull(SB_RULE_END_DATE,'31-dec-3000')
  4407.                   and   SB_CONING_RULE_ITEM.SB_CONING_RULE_ID = SB_CONING_RULE.SB_CONING_RULE_ID
  4408.                   and   SB_RULE_CODE = 'ANAE6'
  4409.             </cfquery>
  4410.             <cfif ANAE6.recordcount eq 0>
  4411.                 The coning run requires the ANAE6 item records to be defined.
  4412.                 <cfabort>
  4413.             </cfif>
  4414.  
  4415.         <!--- Check that no more than one item exists in the range 25000 - 25020. If more than one exists, flag both as an error, status = 'ER',
  4416.         message is 'Only one item in the range 25000 - 25020 may be entered'.
  4417.         srt 02-03-2006 added sb_coning_not_dup_service = 'n' for chad
  4418.         --->
  4419.             <cfquery datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#" name="ANAESTHETIC_BILLING_RANGE_5">
  4420.                 SET DATEFORMAT DMY;
  4421.                 WITH CONING_RECORDS
  4422.                 AS (
  4423.                     #preserveSingleQuotes(CONING_RECORDS_QUERY)#
  4424.                 )
  4425.                 SELECT
  4426.                     SERVICE_DAY
  4427.                 FROM
  4428.                     CONING_RECORDS
  4429.                 WHERE
  4430.                     service_day = '#DateFormat(this_service_day_mmm, "dd-mmm-yyyy")#' and
  4431.                     (sb_coning_not_dup_service = 'n' or sb_coning_not_dup_service = 'N') and
  4432.                     (   1 = 2
  4433.                 <cfoutput query="ANAE5">
  4434.                     <cfif ANAE5.SB_MBS_ITEM_NUMBER_TO eq "">
  4435.                         or SB_ITEM_NUMBER = '#ANAE5.SB_MBS_ITEM_NUMBER#'
  4436.                     <cfelse>
  4437.                         or SB_ITEM_NUMBER BETWEEN '#ANAE5.SB_MBS_ITEM_NUMBER#' and '#ANAE5.SB_MBS_ITEM_NUMBER_TO#'
  4438.                     </cfif>
  4439.                 </cfoutput>)
  4440.                 GROUP BY SERVICE_DAY
  4441.                 HAVING COUNT(*) > 1
  4442.             </cfquery>
  4443.  
  4444.             <!--- If more than one exists flag them as errors --->
  4445.             <cfif ANAESTHETIC_BILLING_RANGE_5.recordcount gt 0>
  4446.  
  4447.                 <cfquery name="ANAESTHETIC_BILLING_RANGE_5A" datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#">
  4448.                     SELECT
  4449.                         SB_CONING_ID,
  4450.                         SB_CONING_ERROR
  4451.                         FROM
  4452.                             SB_CONING_RUN
  4453.                         WHERE SB_CONING_RUN_NO = <cfqueryparam cfsqltype="cf_sql_varchar" value="#VARIABLES.unique_id#">
  4454.                         and   CONVERT(DATETIME, CONVERT(VARCHAR, SB_SERVICE_DATE,103)) = '#DateFormat(ANAESTHETIC_BILLING_RANGE_5.SERVICE_DAY,"dd-mmm-yyyy")#'
  4455.                         AND CONVERT(DATETIME, CONVERT(VARCHAR, SB_SERVICE_DATE,103)) = '#this_service_day_mmm#'
  4456.                         AND (       1 = 2
  4457.                     <cfoutput query="ANAE5">
  4458.                         <cfif ANAE5.SB_MBS_ITEM_NUMBER_TO eq "">
  4459.                             or SB_ITEM_NUMBER = '#ANAE5.SB_MBS_ITEM_NUMBER#'
  4460.                         <cfelse>
  4461.                             or SB_ITEM_NUMBER BETWEEN '#ANAE5.SB_MBS_ITEM_NUMBER#' and '#ANAE5.SB_MBS_ITEM_NUMBER_TO#'
  4462.                         </cfif>
  4463.                     </cfoutput>)
  4464.                 </cfquery>
  4465.  
  4466.                 <cfloop query="ANAESTHETIC_BILLING_RANGE_5A">
  4467.  
  4468.                     <cfset VARIABLES.ERROR_TEXT = #ANAESTHETIC_BILLING_RANGE_5a.SB_CONING_ERROR# & "Only one item in the range 25000 to 25020 may be entered">
  4469.                     <cfset ARGUMENTS.coning_error_count = ARGUMENTS.coning_error_count + 1>
  4470.  
  4471.                     <cfquery datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#" name="ANAESTHETIC_BILLING_RANGE_5_error">
  4472.                         UPDATE SB_CONING_RUN
  4473.                             SET SB_CONING_ERROR = LEFT('#VARIABLES.ERROR_TEXT#',3000),
  4474.                             SB_CONING_STATUS_CODE = <cfqueryparam cfsqltype="cf_sql_varchar" value="ER">
  4475.                         WHERE
  4476.                             SB_CONING_ID = <cfqueryparam cfsqltype="cf_sql_numeric" value="#ANAESTHETIC_BILLING_RANGE_5a.SB_CONING_ID#">
  4477.                     </cfquery>
  4478.  
  4479.                 </cfloop>
  4480.  
  4481.             </cfif>
  4482.         </cfif>
  4483.     </cfloop>
  4484. </cfif>
  4485.  
  4486. <cfset VARIABLES.ERROR_TEXT = "">
  4487.  
  4488. <cfif ListFind(required_rules,'ANAE6') gt 0>
  4489.     <cfloop query="Find_Coning">
  4490.         <cfif Find_Coning.SB_RULE_CODE eq 'ANAE6'>
  4491.             <cfset this_service_day = DateFormat(find_coning.SERVICE_DAY,"dd/mm/yyyy")>
  4492.             <cfset this_service_day_mmm = DateFormat(find_coning.SERVICE_DAY,"dd-mmm-yyyy")>
  4493.             <cfset this_rule_id = find_coning.SB_CONING_RULE_ID>
  4494.  
  4495.             <cfquery datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#" name="ANAE6">
  4496.                 select  SB_CONING_RULE_ITEM.SB_MBS_ITEM_NUMBER,
  4497.                         SB_CONING_RULE_ITEM.SB_MBS_ITEM_NUMBER_TO
  4498.                 from    SB_CONING_RULE_ITEM
  4499.                 where   SB_CONING_RULE_ID = #this_rule_id#
  4500.             </cfquery>
  4501.             <cfif ANAE6.recordcount eq 0>
  4502.                 The coning run requires the ANAE6 item records to be defined.
  4503.                 <cfabort>
  4504.             </cfif>
  4505.  
  4506.             <cfquery datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#" name="ANAE5">
  4507.                 select  SB_CONING_RULE_ITEM.SB_MBS_ITEM_NUMBER,
  4508.                         SB_CONING_RULE_ITEM.SB_MBS_ITEM_NUMBER_TO
  4509.                 from    SB_CONING_RULE_ITEM,
  4510.                         SB_CONING_RULE
  4511.                 where   '#this_service_day_mmm#' between SB_RULE_START_DATE
  4512.                                                  and IsNull(SB_RULE_END_DATE,'31-dec-3000')
  4513.                   and   SB_CONING_RULE_ITEM.SB_CONING_RULE_ID = SB_CONING_RULE.SB_CONING_RULE_ID
  4514.                   and   SB_RULE_CODE = 'ANAE5'
  4515.             </cfquery>
  4516.             <cfif ANAE5.recordcount eq 0>
  4517.                 The coning run requires the ANAE5 item records to be defined.
  4518.                 <cfabort>
  4519.             </cfif>
  4520.  
  4521.             <cfset VARIABLES.ERROR_TEXT = "">
  4522.  
  4523.         <!--- Check that no more than one item exists in the range 25000 - 25020. If more than one exists, flag both as an error, status = 'ER',
  4524.         message is 'Only one item in the range 25000 - 25020 may be entered'. --->
  4525.             <cfquery datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#" name="ANAESTHETIC_BILLING_RANGE_6">
  4526.                 SET DATEFORMAT DMY;
  4527.                 WITH CONING_RECORDS
  4528.                 AS (
  4529.                     #preserveSingleQuotes(CONING_RECORDS_QUERY)#
  4530.                 )
  4531.                 SELECT
  4532.                     SERVICE_DAY
  4533.                 FROM
  4534.                     CONING_RECORDS
  4535.                 WHERE
  4536.                     service_day = '#DateFormat(this_service_day_mmm, "dd-mmm-yyyy")#' and
  4537.                     (SB_CONING_NOT_DUP_SERVICE = 'n' or SB_CONING_NOT_DUP_SERVICE = 'N' ) and
  4538.                     (   1 = 2
  4539.                 <cfoutput query="ANAE6">
  4540.                     <cfif ANAE6.SB_MBS_ITEM_NUMBER_TO eq "">
  4541.                         or SB_ITEM_NUMBER = '#ANAE6.SB_MBS_ITEM_NUMBER#'
  4542.                     <cfelse>
  4543.                         or SB_ITEM_NUMBER BETWEEN '#ANAE6.SB_MBS_ITEM_NUMBER#' and '#ANAE6.SB_MBS_ITEM_NUMBER_TO#'
  4544.                     </cfif>
  4545.                 </cfoutput>)
  4546.                 GROUP BY SERVICE_DAY
  4547.                 HAVING COUNT(*) > 1
  4548.             </cfquery>
  4549.  
  4550.             <!--- If more than one exists flag them as errors --->
  4551.             <cfif ANAESTHETIC_BILLING_RANGE_6.recordcount gt 0>
  4552.  
  4553.                 <cfquery datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#" name="ANAESTHETIC_BILLING_RANGE_6A">
  4554.                     SET DATEFORMAT DMY
  4555.                     SELECT
  4556.                         SB_CONING_ID,
  4557.                         SB_CONING_ERROR
  4558.                     FROM
  4559.                         SB_CONING_RUN
  4560.                     WHERE SB_CONING_RUN_NO = <cfqueryparam cfsqltype="cf_sql_varchar" value="#VARIABLES.unique_id#">
  4561.                     and   CONVERT(DATETIME, CONVERT(VARCHAR, SB_SERVICE_DATE,103)) = '#DateFormat(ANAESTHETIC_BILLING_RANGE_6.SERVICE_DAY,"dd-mmm-yyyy")#'
  4562.                     and   CONVERT(DATETIME, CONVERT(VARCHAR, SB_SERVICE_DATE,103)) = '#this_service_day_mmm#'
  4563.                     AND (       1 = 2
  4564.                     <cfoutput query="ANAE6">
  4565.                         <cfif ANAE6.SB_MBS_ITEM_NUMBER_TO eq "">
  4566.                             or SB_ITEM_NUMBER = '#ANAE6.SB_MBS_ITEM_NUMBER#'
  4567.                         <cfelse>
  4568.                             or SB_ITEM_NUMBER BETWEEN '#ANAE6.SB_MBS_ITEM_NUMBER#' and '#ANAE6.SB_MBS_ITEM_NUMBER_TO#'
  4569.                         </cfif>
  4570.                     </cfoutput>)
  4571.                 </cfquery>
  4572.  
  4573.  
  4574.                 <cfloop query="ANAESTHETIC_BILLING_RANGE_6A">
  4575.  
  4576.                     <cfset VARIABLES.ERROR_TEXT = #ANAESTHETIC_BILLING_RANGE_6a.SB_CONING_ERROR# & "Only one item in the range 23010 to 24136 may be entered">
  4577.                     <cfset ARGUMENTS.coning_error_count = ARGUMENTS.coning_error_count + 1>
  4578.  
  4579.                     <cfquery datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#" name="ANAESTHETIC_BILLING_RANGE_6_error">
  4580.                         UPDATE SB_CONING_RUN
  4581.                             SET SB_CONING_ERROR = LEFT('#VARIABLES.ERROR_TEXT#',3000),
  4582.                             SB_CONING_STATUS_CODE = <cfqueryparam cfsqltype="cf_sql_varchar" value="ER">
  4583.                         WHERE
  4584.                             SB_CONING_ID = <cfqueryparam cfsqltype="cf_sql_numeric" value="#ANAESTHETIC_BILLING_RANGE_6a.SB_CONING_ID#">
  4585.                     </cfquery>
  4586.  
  4587.                 </cfloop>
  4588.  
  4589.             </cfif>
  4590.         </cfif>
  4591.     </cfloop>
  4592. </cfif>
  4593.  
  4594.  
  4595. <cfset VARIABLES.ERROR_TEXT = "">
  4596.  
  4597. <cfif ListFind(required_rules,'PER99') gt 0>
  4598.     <cfloop query="Find_Coning">
  4599.         <cfif Find_Coning.SB_RULE_CODE eq 'PER99'>
  4600.             <cfset this_service_day = DateFormat(find_coning.SERVICE_DAY,"dd/mm/yyyy")>
  4601.             <cfset this_service_day_mmm = DateFormat(find_coning.SERVICE_DAY,"dd-mmm-yyyy")>
  4602.             <cfset this_rule_id = find_coning.SB_CONING_RULE_ID>
  4603.  
  4604.             <cfquery datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#" name="PER99">
  4605.                 select  SB_CONING_RULE_ITEM.SB_MBS_ITEM_NUMBER,
  4606.                         SB_CONING_RULE_ITEM.SB_MBS_ITEM_NUMBER_TO
  4607.                 from    SB_CONING_RULE_ITEM
  4608.                 where   SB_CONING_RULE_ID = #this_rule_id#
  4609.             </cfquery>
  4610.             <cfif PER99.recordcount eq 0>
  4611.                 The coning run requires the PER99 item records to be defined.
  4612.                 <cfabort>
  4613.             </cfif>
  4614.  
  4615.             <cfquery datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#" name="per99_services">
  4616.                 SELECT
  4617.                     RUN.SB_CONING_ID,
  4618.                     RUN.SB_CONING_ERROR
  4619.                 FROM
  4620.                     SB_CONING_RUN RUN INNER JOIN
  4621.                     SB_INVOICE_ITEM ITEM ON
  4622.                         RUN.SB_CONING_ITEM_ID = ITEM.SB_INVOICE_ITEM_ID INNER JOIN
  4623.                     SB_INVOICE INVOICE ON
  4624.                         ITEM.SB_INVOICE_ID = INVOICE.SB_INVOICE_ID INNER JOIN
  4625.                     SB_INVOICE INVOICE2 ON
  4626.                         INVOICE.SB_PATIENT_MRN = INVOICE2.SB_PATIENT_MRN AND
  4627.                         INVOICE.SB_HL7_SOURCE = INVOICE2.SB_HL7_SOURCE  INNER JOIN
  4628.                     SB_INVOICE_ITEM ITEM2 ON
  4629.                         INVOICE2.SB_INVOICE_ID = ITEM2.SB_INVOICE_ID AND
  4630.                         (   1 = 2
  4631.                             <cfoutput query="PER99">
  4632.                                 <cfif PER99.SB_MBS_ITEM_NUMBER_TO eq "">
  4633.                                     or ITEM2.SB_MBS_ITEM_NUMBER = '#PER99.SB_MBS_ITEM_NUMBER#'
  4634.                                 <cfelse>
  4635.                                     or ITEM2.SB_MBS_ITEM_NUMBER BETWEEN '#PER99.SB_MBS_ITEM_NUMBER#' and '#PER99.SB_MBS_ITEM_NUMBER_TO#'
  4636.                                 </cfif>
  4637.                             </cfoutput>
  4638.                         ) AND
  4639.                         -- DO NOT COMPARE THE RECORD TO ITSELF
  4640.                         RUN.SB_CONING_ITEM_ID <> ITEM2.SB_INVOICE_ITEM_ID AND
  4641.                         -- INCLUDE ONLY CLAIMED ITEMS
  4642.                         (ITEM2.SB_ITEM_STATUS_CODE <> '98' AND ITEM2.SB_ITEM_STATUS_CODE <> '99')
  4643.                         AND (ITEM2.SB_ITEM_STATUS_CODE < '50' OR IsNull(ITEM2.SB_HIC_AMOUNT_PAID,0) + IsNull(ITEM2.SB_FUND_AMOUNT_PAID,0) > 0)
  4644.                 WHERE
  4645.                             CONVERT(DATETIME, CONVERT(VARCHAR,run.SB_SERVICE_DATE,103)) = '#this_service_day#'
  4646.                   AND   (   1 = 2
  4647.                             <cfoutput query="PER99">
  4648.                                 <cfif PER99.SB_MBS_ITEM_NUMBER_TO eq "">
  4649.                                     or RUN.SB_ITEM_NUMBER = '#PER99.SB_MBS_ITEM_NUMBER#'
  4650.                                 <cfelse>
  4651.                                     or RUN.SB_ITEM_NUMBER BETWEEN '#PER99.SB_MBS_ITEM_NUMBER#' and '#PER99.SB_MBS_ITEM_NUMBER_TO#'
  4652.                                 </cfif>
  4653.                             </cfoutput>
  4654.                         ) AND
  4655.                         SB_CONING_RUN_NO = <cfqueryparam cfsqltype="cf_sql_varchar" value="#VARIABLES.unique_id#">
  4656.             </cfquery>
  4657.  
  4658.             <cfif per99_services.RECORDCOUNT GT 0>
  4659.                 <cfset error_code_list = "">
  4660.                 <cfoutput query="PER99">
  4661.                     <cfif PER99.SB_MBS_ITEM_NUMBER_TO eq "">
  4662.                         <cfset error_code_list = trim(error_code_list & " " & PER99.SB_MBS_ITEM_NUMBER)>
  4663.                     <cfelse>
  4664.                         <cfset error_code_list = trim(error_code_list & " " & PER99.SB_MBS_ITEM_NUMBER & " - " & PER99.SB_MBS_ITEM_NUMBER_TO)>
  4665.                     </cfif>
  4666.                 </cfoutput>
  4667.  
  4668.                 <cfloop query="per99_services">
  4669.                     <cfset VARIABLES.ERROR_TEXT = #per99_services.SB_CONING_ERROR# & " Only one of items #error_code_list# can be claimed once per lifetime.">
  4670.                     <cfset ARGUMENTS.coning_error_count = ARGUMENTS.coning_error_count + 1>
  4671.  
  4672.                     <cfquery  datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#" name="per99_error">
  4673.                         UPDATE SB_CONING_RUN
  4674.                             SET SB_CONING_ERROR = LEFT('#VARIABLES.ERROR_TEXT#',3000),
  4675.                             SB_CONING_STATUS_CODE = <cfqueryparam cfsqltype="cf_sql_varchar" value="ER">
  4676.                         WHERE
  4677.                             SB_CONING_ID = <cfqueryparam cfsqltype="cf_sql_numeric" value="#per99_services.SB_CONING_ID#">
  4678.                     </cfquery>
  4679.                 </cfloop>
  4680.             </cfif>
  4681.         </cfif>
  4682.     </cfloop>
  4683. </cfif>
  4684.  
  4685. <cfset VARIABLES.ERROR_TEXT = "">
  4686.  
  4687.  
  4688. <!--- End of   Anaesthetic Billing Rules --->
  4689.  
  4690. <!--- Start of  Anaesthetic Derived Fee Items
  4691.  
  4692. Calculate the fee for item 25025 as follows:
  4693. 50% of the total sum of all items in the ranges 20100-21997, 22900, 23010-24136, 25000-25015, 22001-22050.
  4694.  
  4695. Calculate the fee for item 25030 as follows:
  4696. 50% of the total sum of all items in the ranges 25200-25205, 23010-24136, 25000-25015, 22001-22050
  4697.  
  4698. Calculate the fee for item 25050 as follows:
  4699. 50% of the total sum of all items in the ranges 22060, 23010-24136, 25000-25015, 22001-22050, 22065-22075
  4700.  
  4701. Calculate the fee for item 25200 as follows:
  4702. $82.50 plus the total sum of all items in the range 23010-24136 and 25000-25020.
  4703.  
  4704. Calculate the fee for item 25205 as follows:
  4705. $82.50 plus the total sum of all items in the range 23010-24136 and 25000-25020.
  4706. --->
  4707.  
  4708.  
  4709.     <!--- Calculate the fee for item 25200 as follows:
  4710. $82.50 plus the total sum of all items in the range 23010-24136 and 25000-25020.
  4711. DFAN4 processing needs to occur before DFAN2, as 25200 is a derived fee, and
  4712. can be used in the derivation of 25025.
  4713. --->
  4714. <cfloop index="DFARule" list="DFAN4,DFAN5">
  4715.     <cfif ListFind(required_rules,DFARule) gt 0>
  4716.         <cfloop query="Find_Coning">
  4717.             <cfif Find_Coning.SB_RULE_CODE eq DFARule>
  4718.                 <cfset this_service_day = DateFormat(find_coning.SERVICE_DAY,"dd/mm/yyyy")>
  4719.                 <cfset this_service_day_mmm = DateFormat(find_coning.SERVICE_DAY,"dd-mmm-yyyy")>
  4720.                 <cfset this_rule_id = find_coning.SB_CONING_RULE_ID>
  4721.  
  4722.                 <cfquery datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#" name="QDFAN2">
  4723.                     set dateformat YMD;
  4724.                     select  CR.SB_MBS_ITEM_NUMBER,
  4725.                             isnull(CR.SB_BASE_AMOUNT,IC.SB_MBS_ITEM_FEE) SB_BASE_AMOUNT
  4726.                     from    SB_CONING_RULE CR left outer join
  4727.                             SB_MBS_ITEM_COST IC on IC.SB_MBS_ITEM_NUMBER = CR.SB_BASED_ON_MBS_ITEM and
  4728.                                 IC.SB_COST_TYPE_ID = #MBS100.SB_COST_TYPE_ID# and
  4729.                                 '#DateFormat(this_service_day_mmm, "yyyy-MM-dd")#' >= IC.SB_MBS_COST_START and
  4730.                                 '#DateFormat(this_service_day_mmm, "yyyy-MM-dd")#' < isnull(IC.SB_MBS_COST_END,dateadd(year,100,getdate()))
  4731.                     where   CR.SB_CONING_RULE_ID = #this_rule_id#
  4732.                    
  4733.                     set dateformat dmy;
  4734.                 </cfquery>
  4735.                 <cfif QDFAN2.recordcount eq 0>
  4736.                     The coning run requires the <cfoutput>#DFARule#</cfoutput> record to be defined.
  4737.                     <cfabort>
  4738.                 </cfif>
  4739.  
  4740.                 <cfquery datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#" name="DFANItem2">
  4741.                     select  SB_CONING_RULE_ITEM.SB_MBS_ITEM_NUMBER,
  4742.                             SB_CONING_RULE_ITEM.SB_MBS_ITEM_NUMBER_TO
  4743.                     from    SB_CONING_RULE_ITEM
  4744.                     where   SB_CONING_RULE_ID = #this_rule_id#
  4745.                     ORDER BY SB_CONING_RULE_ITEM.SB_MBS_ITEM_NUMBER
  4746.                 </cfquery>
  4747.                 <cfif DFANItem2.recordcount eq 0>
  4748.                     The coning run requires the <cfoutput>#DFARule#</cfoutput> item records to be defined.
  4749.                     <cfabort>
  4750.                 </cfif>
  4751.  
  4752.             <!--- Calculate the fee for item 25025 as follows:
  4753.         50% of the total sum of all items in the ranges 20100-21997, 22900, 23010-24136, 25000-25015, 22001-22050. --->
  4754.                 <cfquery datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#" name="ANAE_DERIVED_FEE2">
  4755.                     SET DATEFORMAT DMY;
  4756.                     WITH CONING_RECORDS
  4757.                     AS (
  4758.                         #preserveSingleQuotes(CONING_RECORDS_QUERY)#
  4759.                     )
  4760.                     SELECT
  4761.                         SB_CONING_ID,
  4762.                         SB_CONING_ERROR,
  4763.                         SB_SERVICE_DATE,
  4764.                         SB_DERIVED_NOGAP_PERCENT
  4765.                     FROM
  4766.                         CONING_RECORDS
  4767.                     WHERE
  4768.                         service_day = '#DateFormat(this_service_day_mmm, "dd-mmm-yyyy")#' and
  4769.                         SB_ITEM_NUMBER = '#QDFAN2.SB_MBS_ITEM_NUMBER#'
  4770.                 </cfquery>
  4771. <!--- If derived fee calculation with a no-gap percentage, and component does not
  4772. have a percentage specified and component is not derived, then the component has
  4773. the percentage built in to it, and must not be applied twice. Hence reduce component
  4774. by inverse of no gpa percentage, so when the percentage is reapplied it is only applied
  4775. once, not twice. --->
  4776.                 <cfloop query="ANAE_DERIVED_FEE2">
  4777.                     <cfquery datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#" name="ANAE_DERIVED_FEE2_TOTAL">
  4778.                         SELECT
  4779.                         <cfif ANAE_DERIVED_FEE2.SB_DERIVED_NOGAP_PERCENT eq "">
  4780.                             SUM(SB_ACTUAL_FEE) as total,
  4781.                         <cfelse>
  4782.                             SUM(
  4783.                             Case when SB_CONING_RUN.SB_DERIVED_NOGAP_PERCENT is null
  4784.                                   and SB_CONING_RUN.SB_SCHEDULE_FEE IS NOT NULL
  4785.                                  then SB_ACTUAL_FEE * 100 / #ANAE_DERIVED_FEE2.SB_DERIVED_NOGAP_PERCENT#
  4786.                                  else SB_ACTUAL_FEE end)
  4787.                             as total,
  4788.                         </cfif>
  4789.                             SUM(CASE WHEN 1=2
  4790.                         <cfoutput query="DFANItem2" startrow=1 maxrows=1>
  4791.                             <cfif DFANItem2.SB_MBS_ITEM_NUMBER_TO eq "">
  4792.                                 OR SB_ITEM_NUMBER = '#DFANItem2.SB_MBS_ITEM_NUMBER#'
  4793.                                 <cfset reqd_range = DFANItem2.SB_MBS_ITEM_NUMBER>
  4794.                             <cfelse>
  4795.                                 OR SB_ITEM_NUMBER BETWEEN '#DFANItem2.SB_MBS_ITEM_NUMBER#' and '#DFANItem2.SB_MBS_ITEM_NUMBER_TO#'
  4796.                                 <cfset reqd_range = DFANItem2.SB_MBS_ITEM_NUMBER & " - " & DFANItem2.SB_MBS_ITEM_NUMBER_TO>
  4797.                             </cfif>
  4798.                         </cfoutput>
  4799.                                     THEN 1 ELSE 0 END) AS FIRST_RANGE_COUNT
  4800.                         FROM
  4801.                             SB_CONING_RUN
  4802.                         WHERE
  4803.                              SB_CONING_RUN_NO = <cfqueryparam cfsqltype="cf_sql_varchar" value="#VARIABLES.unique_id#">
  4804.                         and CONVERT(DATETIME, CONVERT(VARCHAR, SB_CONING_RUN.SB_SERVICE_DATE, 103)) = '#DateFormat(ANAE_DERIVED_FEE2.SB_SERVICE_DATE,"dd-mmm-yyyy")#'
  4805.                         AND (1 = 2
  4806.                         <cfoutput query="DFANItem2">
  4807.                             <cfif DFANItem2.SB_MBS_ITEM_NUMBER_TO eq "">
  4808.                                 or SB_ITEM_NUMBER = '#DFANItem2.SB_MBS_ITEM_NUMBER#'
  4809.                             <cfelse>
  4810.                                 or SB_ITEM_NUMBER BETWEEN '#DFANItem2.SB_MBS_ITEM_NUMBER#' and '#DFANItem2.SB_MBS_ITEM_NUMBER_TO#'
  4811.                             </cfif>
  4812.                         </cfoutput>
  4813.                         )
  4814.                     </cfquery>
  4815.  
  4816.  
  4817.                     <cfif ANAE_DERIVED_FEE2_TOTAL.FIRST_RANGE_COUNT eq "" or ANAE_DERIVED_FEE2_TOTAL.FIRST_RANGE_COUNT eq "0">
  4818.                         <cfset VARIABLES.ERROR_TEXT = #ANAE_DERIVED_FEE2.SB_CONING_ERROR# & " Requires at least one item from #reqd_range#">
  4819.                         <cfset ARGUMENTS.coning_error_count = ARGUMENTS.coning_error_count + 1>
  4820.                     </cfif>
  4821.                     <cfquery datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#" name="ANAE_DERIVED_FEE2_TOTAL_update">
  4822.                         UPDATE SB_CONING_RUN
  4823.                         <cfif ANAE_DERIVED_FEE2_TOTAL.FIRST_RANGE_COUNT eq "" or ANAE_DERIVED_FEE2_TOTAL.FIRST_RANGE_COUNT eq "0">
  4824.                             SET SB_ACTUAL_FEE = #qdfan2.SB_BASE_AMOUNT#,
  4825.                                 SB_CONING_ERROR = '#ERROR_TEXT#',
  4826.                                 SB_CONING_STATUS_CODE = 'ER'
  4827.                         <cfelse>
  4828.                             SET SB_ACTUAL_FEE = CEILING((#qdfan2.SB_BASE_AMOUNT# + #ANAE_DERIVED_FEE2_TOTAL.total#) / 0.05) * 0.05,
  4829.                                 CONING_RULES_APPLIED = CASE WHEN LEN(ISNULL(CONING_RULES_APPLIED,'') + ',' + '#DFARule#') <= 100
  4830.                                                         THEN (CASE WHEN LEN(ISNULL(CONING_RULES_APPLIED,'')) > 0 THEN CONING_RULES_APPLIED + ',' + '#DFARule#' ELSE '#DFARule#' END)
  4831.                                                         ELSE CONING_RULES_APPLIED END
  4832.                         </cfif>
  4833.                         WHERE
  4834.                             SB_CONING_ID = <cfqueryparam cfsqltype="cf_sql_numeric" value="#ANAE_DERIVED_FEE2.SB_CONING_ID#">
  4835.                     </cfquery>
  4836.  
  4837.                 </cfloop>
  4838.             </cfif>
  4839.         </cfloop>
  4840.     </cfif>
  4841. </cfloop>
  4842.  
  4843.  
  4844. <cfinvoke component="pbrc-ae.components.apply_coning" method="DRPRules" returnvariable="DRPRules">
  4845. <cfset derived_list = "DFAN1,DFAN2,DFAN3,AMP">
  4846. <cfoutput query="DRPRules">
  4847.     <cfif !listfindnocase(derived_list, #DRPRules.SB_RULE_CODE#)>
  4848.         <cfset derived_list = derived_list & ",#DRPRules.SB_RULE_CODE#">
  4849.     </cfif>
  4850. </cfoutput>
  4851.  
  4852. <cfloop index="DFARule" list="#derived_list#">
  4853.     <cfif ListFind(required_rules,DFARule) gt 0>
  4854.         <cfloop query="Find_Coning">
  4855.             <cfif Find_Coning.SB_RULE_CODE eq DFARule>
  4856.                 <cfset this_service_day = DateFormat(find_coning.SERVICE_DAY,"dd/mm/yyyy")>
  4857.                 <cfset this_service_day_mmm = DateFormat(find_coning.SERVICE_DAY,"dd-mmm-yyyy")>
  4858.                 <cfset this_rule_id = find_coning.SB_CONING_RULE_ID>
  4859.  
  4860.                 <cfif DFARule eq "AMP">
  4861.                     <cfset item_type = 'amputation'>
  4862.                 <cfelse>
  4863.                     <cfset item_type = 'anaesthetic'>
  4864.                 </cfif>
  4865.  
  4866.                 <cfquery datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#" name="QDFAN">
  4867.                     select  SB_MBS_ITEM_NUMBER,
  4868.                             SB_RULE_PERCENT1
  4869.                     from    SB_CONING_RULE
  4870.                     where   SB_CONING_RULE_ID = #this_rule_id#
  4871.                 </cfquery>
  4872.                 <cfif QDFAN.recordcount eq 0>
  4873.                     The coning run requires the <cfoutput>#DFARule#</cfoutput> record to be defined.
  4874.                     <cfabort>
  4875.                 </cfif>
  4876.  
  4877.                 <cfquery datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#" name="DFANItem">
  4878.                     select  SB_CONING_RULE_ITEM.SB_MBS_ITEM_NUMBER,
  4879.                             SB_CONING_RULE_ITEM.SB_MBS_ITEM_NUMBER_TO
  4880.                     from    SB_CONING_RULE_ITEM
  4881.                     where   SB_CONING_RULE_ID = #this_rule_id#
  4882.                 </cfquery>
  4883.                 <cfif DFANItem.recordcount eq 0>
  4884.                     The coning run requires the <cfoutput>#DFARule#</cfoutput> item records to be defined.
  4885.                     <cfabort>
  4886.                 </cfif>
  4887.  
  4888.             <!--- Calculate the fee for item 25025 as follows:
  4889.         50% of the total sum of all items in the ranges 20100-21997, 22900, 23010-24136, 25000-25015, 22001-22050. --->
  4890.                 <cfquery datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#" name="ANAE_DERIVED_FEE1">
  4891.                     SET DATEFORMAT DMY;
  4892.                     WITH CONING_RECORDS
  4893.                     AS (
  4894.                         #preserveSingleQuotes(CONING_RECORDS_QUERY)#
  4895.                     )
  4896.                     SELECT
  4897.                         SB_CONING_ID,
  4898.                         SB_CONING_ERROR,
  4899.                         SB_SERVICE_DATE,
  4900.                         SB_DERIVED_NOGAP_PERCENT
  4901.                     FROM
  4902.                         CONING_RECORDS
  4903.                     WHERE
  4904.                         SB_ITEM_NUMBER = '#QDFAN.SB_MBS_ITEM_NUMBER#'
  4905.                         and service_day = '#DateFormat(this_service_day_mmm, "dd-mmm-yyyy")#'
  4906.                 </cfquery>
  4907.  
  4908.  
  4909. <!--- If derived fee calculation with a no-gap percentage, and component does not
  4910. have a percentage specified and component is not derived, then the component has
  4911. the percentage built in to it, and must not be applied twice. Hence reduce component
  4912. by inverse of no gpa percentage, so when the percentage is reapplied it is only applied
  4913. once, not twice. --->
  4914.  
  4915.                 <cfloop query="ANAE_DERIVED_FEE1">
  4916.                     <cfquery datasource="#Application.DSNdatasource#" username="#Application.DSNuser#" password="#Application.DSNpass#" name="ANAE_DERIVED_FEE1_TOTAL">
  4917.                         SELECT
  4918.                         <cfif ANAE_DERIVED_FEE1.SB_DERIVED_NOGAP_PERCENT eq "">
  4919.                             SUM(SB_ACTUAL_FEE) as total
  4920.                         <cfelse>
  4921.                             SUM(
  4922.                             Case when SB_CONING_RUN.SB_DERIVED_NOGAP_PERCENT is null
  4923.                                   and SB_CONING_RUN.SB_SCHEDULE_FEE IS NOT NULL
  4924.                                  then SB_ACTUAL_FEE * 100 / #ANAE_DERIVED_FEE1.SB_DERIVED_NOGAP_PERCENT#
  4925.                                  else SB_ACTUAL_FEE end)
  4926.                             as total
  4927.                         </cfif>
  4928.                         FROM
  4929.                             SB_CONING_RUN
  4930.                         WHERE
  4931.                              SB_CONING_RUN_NO = <cfqueryparam cfsqltype="cf_sql_varchar" value="#VARIABLES.unique_id#">
  4932.                         AND DATEDIFF(DAY,  SB_CONING_RUN.SB_SERVICE_DATE, '#DateFormat(ANAE_DERIVED_FEE1.SB_SERVICE_DATE,"dd-mmm-yyyy")#' ) = 0
  4933.                         AND (1 = 2
  4934.                         <cfoutput query="DFANItem">
  4935.                             <cfif DFANItem.SB_MBS_ITEM_NUMBER_TO eq "">
  4936.                               &n