Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.73 KB | None | 0 0
  1. public List<DME_WrapperCheck> doGenerate() {
  2.  
  3. List<DME_Service_Item__c> itemList = new List<DME_Service_Item__c>();
  4. this.checkItemMap = new Map<String, DME_WrapperCheck>();
  5. this.billCheckMap = new Map<Id, String>();
  6. this.checkItemList = new List<DME_WrapperCheck>();
  7.  
  8. if(!this.showError) {
  9.  
  10. String day;
  11. String month;
  12. String formattedStringDate;
  13.  
  14. //format date to correct SOQL format
  15. if(this.checkDate != null) {
  16. if(this.checkDate.day()<10) {
  17. day = '0' + String.valueOf(checkDate.day());
  18. } else {
  19. day = String.valueOf(checkDAte.day());
  20. }
  21. if(checkDate.month()<10) {
  22. month = '0' + String.valueOf(checkDate.month());
  23. } else {
  24. month = String.valueOf(checkDate.month());
  25. }
  26. formattedStringDate = checkDate.year() + '-' + month + '-' + day;
  27. }
  28.  
  29. Boolean run = false;
  30.  
  31. //general query string for paid items without filter criteria by check date or number
  32. String queryString = 'SELECT Order__r.DME_Injury__r.Person__r.LastName, Order__r.DME_Injury__r.Person__r.FirstName, '
  33. + 'Bill_Line_Item__r.Bill__r.ProviderInvoice__c, DOS_From__c, Product__r.ProductCode, Product__r.Name, '
  34. + 'Quantity__c, Bill_Line_Item__r.Linecharge__c, Discount_Amount__c, APR__c, Bill_Line_Item__r.Name, '
  35. + 'Bill_Line_Item__r.Denial_Reason__c, Bill_Line_Item__r.Bill__r.Id, Bill_Line_Item__r.Bill__r.Trading_Partner__r.Name, '
  36. + 'Check_Number__c, Check_Paid_Amount__c, Check_Paid_Date__c, Bill_Line_Item__r.Bill__r.Trading_Partner__r.Id, '
  37. + 'Bill_Line_Item__r.Bill__r.Trading_Partner__r.RA_Doc_Type_Preference__c, Bill_Line_Item__c, '
  38. + 'Bill_Line_Item__r.Compliance_Reason__c, Bill_Line_Item__r.Duplicate_Check_Amount__c, Bill_Line_Item__r.Duplicate_Check_Number__c, '
  39. + 'Bill_Line_Item__r.Bill__r.Trading_Partner__r.RA_Delivery_Preference__c, Bill_Line_Item__r.Reasoncode__c '
  40. + 'FROM DME_Service_Item__c '
  41. + 'WHERE (Paid__c = TRUE) AND (Check_Paid_Amount__c <> null AND Check_Paid_Amount__c <> 0 AND Check_Number__c <> '') '
  42. + 'AND ';
  43.  
  44. //append check date and number filter to query string
  45. if(this.checkDate != null) {
  46.  
  47. if(this.checkNumber != '' && this.checkNumber != null) {
  48.  
  49. queryString += '(Check_Paid_Date__c = ' + formattedStringDate + ' AND Check_Number__c = ''+ checkNumber + '') ';
  50. run = true;
  51.  
  52. } else {
  53.  
  54. queryString += '(Check_Paid_Date__c = ' + formattedStringDate + ') ';
  55. run = true;
  56.  
  57. }
  58.  
  59. } else if(this.checkNumber != '' && this.checkNumber != null) {
  60.  
  61. queryString += '(Check_Number__c = '' + checkNumber + '') ';
  62. run = true;
  63.  
  64. } else {
  65.  
  66. showError = true;
  67. errorMsg = 'Please enter at least one search criteria';
  68.  
  69. }
  70.  
  71. if(!this.showError) {
  72. //get records
  73. if(run) {
  74. System.debug(queryString);
  75. itemList = Database.query(queryString);
  76. this.runReport = true;
  77. }
  78.  
  79. Set<Id> tempBillIdList = new Set<Id>();
  80. List<DME_Service_Item__c> tempDeniedItemList = new List<DME_Service_Item__c>();
  81.  
  82. //get bill ids of paid bills
  83. for(DME_Service_Item__c si : itemList) {
  84. if(si.Bill_Line_Item__r.Bill__r.Id != null) {
  85. tempBillIdList.add(si.Bill_Line_Item__r.Bill__r.Id);
  86. }
  87. }
  88.  
  89.  
  90. //get unpaid items of the bills which have been paid for (partial denied)
  91. tempDeniedItemList = [ SELECT Order__r.DME_Injury__r.Person__r.LastName, Order__r.DME_Injury__r.Person__r.FirstName,
  92. Bill_Line_Item__r.Bill__r.ProviderInvoice__c, DOS_From__c, Product__r.ProductCode, Product__r.Name,
  93. Quantity__c, Bill_Line_Item__r.Linecharge__c, Discount_Amount__c, APR__c, Bill_Line_Item__r.Name,
  94. Bill_Line_Item__r.Bill__r.Trading_Partner__r.Id, Bill_Line_Item__r.Denial_Reason__c,
  95. Bill_Line_Item__r.Bill__r.Id, Bill_Line_Item__r.Bill__r.Trading_Partner__r.Name, Bill_Line_Item__c,
  96. Check_Number__c, Check_Paid_Amount__c, Check_Paid_Date__c, Bill_Line_Item__r.Bill__r.Trading_Partner__r.RA_Doc_Type_Preference__c,
  97. Bill_Line_Item__r.Bill__r.Trading_Partner__r.RA_Delivery_Preference__c, Bill_Line_Item__r.Reasoncode__c,
  98. Bill_Line_Item__r.Compliance_Reason__c, Bill_Line_Item__r.Duplicate_Check_Amount__c, Bill_Line_Item__r.Duplicate_Check_Number__c
  99. FROM DME_Service_Item__c
  100. WHERE Status__c = :DME_Constants.SERVICE_ITEM_STATUS_DENIED_SI AND Bill_Line_Item__r.Bill__r.Id IN :tempBillIdList AND Id NOT IN :itemList ];
  101.  
  102. //remove denial reason string and get only denial code
  103. for(DME_Service_Item__c si : tempDeniedItemList) {
  104. if(si.Bill_Line_Item__r.Denial_Reason__c != null) {
  105. si.Bill_Line_Item__r.Denial_Reason__c = si.Bill_Line_Item__r.Denial_Reason__c.substring(0, 2);
  106. }
  107. }
  108.  
  109. itemList.addAll(tempDeniedItemList);
  110.  
  111. Set<Id> bliIdList = new Set<Id>();
  112.  
  113. for(DME_Service_Item__c si : itemList) {
  114.  
  115. if(si.Discount_Amount__c == null) {
  116. si.Discount_Amount__c = 0.00;
  117. }
  118.  
  119. if(si.Check_Paid_Amount__c == null) {
  120. si.Discount_Amount__c = 0.00;
  121. }
  122.  
  123. if(si.Bill_Line_Item__r.Linecharge__c == null) {
  124. si.Bill_Line_Item__r.Linecharge__c = 0.00;
  125. }
  126.  
  127. bliIdList.add(si.Bill_Line_Item__c);
  128. }
  129.  
  130. List<DME_Bill_Line_Item__c> tempDeniedBliList = new List<DME_Bill_Line_Item__c>();
  131.  
  132. tempDeniedBliList = [ SELECT Id, Bill__c, Bill__r.Patient_FirstName__c, Bill__r.Patient_LastName__c,
  133. Bill__r.ProviderInvoice__c, Servicefrom__c, Procedure_Code__c, Procedure_Description__c, Units__c, Linecharge__c, Name,
  134. Reasoncode__c, Bill__r.Check_Number__c, Compliance_Reason__c, Duplicate_Check_Amount__c, Duplicate_Check_Number__c
  135. FROM DME_Bill_Line_Item__c WHERE Status__c = :DME_Constants.BILL_LINE_ITEM_STATUS_DENIED AND Bill__c IN :tempBillIdList
  136. AND Id NOT IN :bliIdList ];
  137.  
  138. //map bill id to check number
  139. for(DME_Service_Item__c si : itemList) {
  140. if(si.Bill_Line_Item__r.Bill__r.Id != null) {
  141. if(billCheckMap.get(si.Bill_Line_Item__r.Bill__r.Id) == null) {
  142. billCheckMap.put(si.Bill_Line_Item__r.Bill__r.Id, si.Check_Number__c);
  143. }
  144. }
  145. }
  146.  
  147. String tempCheckNo = '';
  148. //map all items to the check number which maps to its bill
  149. for(DME_Service_Item__c si : itemList) {
  150.  
  151. tempCheckNo = billCheckMap.get(si.Bill_Line_Item__r.Bill__r.Id);
  152.  
  153. if(checkItemMap.get(tempCheckNo) == null) {
  154.  
  155. DME_WrapperCheck wc = new DME_WrapperCheck(tempCheckNo, si.Check_Paid_Amount__c,
  156. si.Check_Paid_Date__c, si.Bill_Line_Item__r.Bill__r.Trading_Partner__r.Name,
  157. si.Bill_Line_Item__r.Bill__r.Trading_Partner__r.RA_Doc_Type_Preference__c,
  158. si.Bill_Line_Item__r.Bill__r.Trading_Partner__r.RA_Delivery_Preference__c,
  159. si.Bill_Line_Item__r.Bill__r.Trading_Partner__r.Id,
  160. null );
  161. wc.siList.add(si);
  162. checkItemMap.put(tempCheckNo, wc);
  163.  
  164. } else {
  165.  
  166. checkItemMap.get(tempCheckNo).siList.add(si);
  167. if(si.Check_Paid_Amount__c!=null) {
  168. checkItemMap.get(tempCheckNo).checkAmount += si.Check_Paid_Amount__c;
  169. }
  170.  
  171. }
  172. }
  173.  
  174. for(DME_Bill_Line_Item__c bli : tempDeniedBliList) {
  175. checkItemMap.get(bli.Bill__r.Check_Number__c).bliList.add(bli);
  176. }
  177.  
  178. for(DME_WrapperCheck wc : checkItemMap.values()) {
  179.  
  180. // get the count of display items in the check items
  181. counter += wc.sortLists(counter);
  182. System.debug('counter' + counter);
  183.  
  184. system.debug('wc.displayList - ' + wc.displayList);
  185. system.debug('wc.senditemList - ' + wc.sendDisplayItemList);
  186. checkItemList.add(wc);
  187.  
  188. }
  189.  
  190. // If limit exceeds show the error popup and send all records to the logged in user
  191. if(counter > 4) {
  192. this.limitExceedError = true;
  193.  
  194. // Check whether the email is not sent
  195. if(!this.emailSent) {
  196. this.sendFiltered(checkItemList);
  197. }
  198.  
  199. }
  200.  
  201. }
  202. }
  203. for(DME_WrapperCheck wc : checkItemList) {
  204.  
  205. String formattedStringDate;
  206. String day;
  207. String month;
  208.  
  209. if(wc.checkDate.day()<10) {
  210. day = '0' + String.valueOf(wc.checkDate.day());
  211. } else {
  212. day = String.valueOf(wc.checkDate.day());
  213. }
  214. if(wc.checkDate.month()<10) {
  215. month = '0' + String.valueOf(wc.checkDate.month());
  216. } else {
  217. month = String.valueOf(wc.checkDate.month());
  218. }
  219. wc.dlcdate = month + '/' + day + '/' + wc.checkDate.year();
  220.  
  221.  
  222. }
  223.  
  224. return checkItemList;
  225.  
  226.  
  227. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement