Advertisement
Guest User

Untitled

a guest
Mar 30th, 2023
24
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 32.33 KB | None | 0 0
  1. /**
  2. * Created by gporosnicu on 17.10.2022.
  3. */
  4.  
  5. import { LightningElement, api, track, wire } from 'lwc';
  6. import { ShowToastEvent } from 'lightning/platformShowToastEvent';
  7. import { updateRecord } from 'lightning/uiRecordApi';
  8. import userId from '@salesforce/schema/User.Id';
  9.  
  10. //Import Custom Labels
  11. import EL_Account_Balance_Information from '@salesforce/label/c.EL_Account_Balance_Information';
  12. import EL_Account_Balance from '@salesforce/label/c.EL_Account_balance';
  13. import EL_Account_Balance_More_Than_9_Contract_Accounts_Message from '@salesforce/label/c.EL_Account_Balance_More_Than_9_Contract_Accounts_Message';
  14. import EL_Account_Balance_Open_Invoices_Timeout_Message from '@salesforce/label/c.EL_Account_Balance_Open_Invoices_Timeout_Message';
  15. import EL_Contract_Account from '@salesforce/label/c.EL_Contract_Account';
  16. import EL_Contract_Account_with_Highest_Dunning_Level from '@salesforce/label/c.EL_Contract_Account_with_Highest_Dunning_Level';
  17. import EL_Document_Date from '@salesforce/label/c.EL_Document_Date';
  18. import EL_Due_Date from '@salesforce/label/c.EL_Due_Date';
  19. import EL_Reported_Due_Date from '@salesforce/label/c.EL_Reported_Due_Date';
  20. import EL_Dunning_Level from '@salesforce/label/c.EL_Dunning_Level';
  21. import EL_Dunning_Level_Icon from '@salesforce/label/c.EL_Dunning_Level_Icon';
  22. import EL_Invoice_Dunning_Level from '@salesforce/label/c.EL_Invoice_Dunning_Level';
  23. import EL_Invoice_Dunning_Level_Icon from '@salesforce/label/c.EL_Invoice_Dunning_Level_Icon';
  24. import EL_Invoice_Number from '@salesforce/label/c.EL_Invoice_Number';
  25. import EL_Open_Amount from '@salesforce/label/c.EL_Open_Amount';
  26. import EL_Open_Invoices from '@salesforce/label/c.EL_Open_Invoices';
  27. import EL_Open_Invoices_for_Contract_Account from '@salesforce/label/c.EL_Open_Invoices_for_Contract_Account';
  28. import EL_Site_Address from '@salesforce/label/c.EL_Site_Address';
  29. import EL_Total_Open_Amount from '@salesforce/label/c.EL_Total_Open_Amount';
  30. import EL_Total_Open_Amount_Information from '@salesforce/label/c.EL_Total_Open_Amount_Information';
  31.  
  32. //Dunning Level Labels
  33. import BAILIFF from '@salesforce/label/c.EL_Dunning_Level_Bailiff';
  34. import BANKRUPTCY from '@salesforce/label/c.EL_Dunning_Level_Bankruptcy';
  35. import CUTOFF from '@salesforce/label/c.EL_Dunning_Level_Cutoff';
  36. import EXTERNAL_OFFICES from '@salesforce/label/c.EL_Dunning_Level_External_Offices';
  37. import INSOLVENT from '@salesforce/label/c.EL_Dunning_Level_Insolvent';
  38. import LAST_REMINDER from '@salesforce/label/c.EL_Dunning_Level_Last_Reminder';
  39. import OVERDEBTNESS from '@salesforce/label/c.EL_Dunning_Level_Overdebtness';
  40. import PAYMENT_ORDER from '@salesforce/label/c.EL_Dunning_Level_Payment_Order';
  41. import REMINDER_LEVEL_1 from '@salesforce/label/c.EL_Dunning_Level_Reminder_level_01';
  42. import REMINDER_LEVEL_2 from '@salesforce/label/c.EL_Dunning_Level_Reminder_level_02';
  43. import REMINDER_LEVEL_3 from '@salesforce/label/c.EL_Dunning_Level_Reminder_level_03';
  44. import WAGE_GARNISHMENT from '@salesforce/label/c.EL_Dunning_Level_Wage_Garnishment';
  45.  
  46. //Import Apex
  47. import getAccountBalance from '@salesforce/apex/EL_AccountBalance_Controller.getAccountBalance';
  48. import getAccount from '@salesforce/apex/EL_AccountBalance_Controller.getAccount';
  49. import getSubscription from '@salesforce/apex/EL_AccountBalance_Controller.getSubscription';
  50. import getSubscriptionsBySAPContractAccount from '@salesforce/apex/EL_AccountBalance_Controller.getSubscriptionsBySAPContractAccount';
  51. import saveAccountBalanceReportedDate from '@salesforce/apex/EL_AccountBalance_Controller.saveAccountBalanceReportedDate';
  52. import getUserRole from '@salesforce/apex/EL_AccountBalance_Controller.getUserRole';
  53.  
  54. //Import Custom Colors from static resource
  55. import {loadStyle} from 'lightning/platformResourceLoader'
  56. import COLORS from '@salesforce/resourceUrl/EL_Custom_Colors'
  57.  
  58. //Table 1 columns
  59. const columnsTable1 = [
  60. { label: EL_Contract_Account, fieldName: 'contractAccount', type: 'text', sortable: true, wrapText: true, cellAttributes: { alignment: 'center' } },
  61. { label: EL_Site_Address, fieldName: 'siteAddress', type: 'text', sortable: true, wrapText: true, cellAttributes: { alignment: 'center' } },
  62. { label: EL_Total_Open_Amount, fieldName: 'totalOpenAmount', type: 'number', sortable: true, wrapText: true, cellAttributes: { alignment: 'center' } },
  63. { label: EL_Dunning_Level, fieldName: 'dunningLevel', type: 'text', sortable: true, wrapText: true, cellAttributes: { alignment: 'center' } },
  64. { label: 'Indicator', fieldName: '', type: 'text', wrapText: true , initialWidth: 100, cellAttributes: { alignment: 'center', class: {fieldName: 'dunningLevelIcon'}}},
  65. { label: "", fieldName: 'openInvoices', type: 'button', cellAttributes: { alignment: 'center' }, typeAttributes: { label: EL_Open_Invoices, name: 'openInvoices', variant: 'outline-brand'} },
  66. { label: EL_Reported_Due_Date, fieldName: 'reportedDueDate', type: 'date-local',editable: true, sortable: true, wrapText: true , cellAttributes: { alignment: 'center' }}
  67. ];
  68.  
  69. //Table 2 columns
  70. const columnsTable2 = [
  71. { label: EL_Invoice_Number, fieldName: 'invoiceNumber', type: 'text', sortable: true, wrapText: true, initialWidth: 220, cellAttributes: { alignment: 'center' } },
  72. { label: EL_Open_Amount, fieldName: 'amount', type: 'number', editable:true, sortable: true, wrapText: true, cellAttributes: { alignment: 'center' } },
  73. { label: EL_Invoice_Dunning_Level, fieldName: 'invoiceDunningLevel', type: 'text', sortable: true, wrapText: true, cellAttributes: { alignment: 'center' } },
  74. { label: 'Indicator', fieldName: '', type: 'text', sortable: true, wrapText: true, initialWidth: 100, cellAttributes: { alignment: 'center', class: {fieldName: 'invoiceDunningLevelIcon'} } },
  75. { label: EL_Document_Date, fieldName: 'documentDate', type: 'date-local', sortable: true, wrapText: true , cellAttributes: { alignment: 'center' }},
  76. { label: EL_Due_Date, fieldName: 'dueDate', type: 'date-local', sortable: true, wrapText: true , cellAttributes: { alignment: 'center' }},
  77. { label: 'Reported Due Date 2', fieldName: 'invoiceReportedDueDate', type: 'date-local', editable: true, sortable: true, wrapText: true , cellAttributes: { alignment: 'center' }}
  78. ];
  79.  
  80. export default class elAccountBalance extends LightningElement {
  81.  
  82. //Custom Labels
  83. labels = {
  84. EL_Account_Balance,
  85. EL_Account_Balance_Information,
  86. EL_Account_Balance_More_Than_9_Contract_Accounts_Message,
  87. EL_Account_Balance_Open_Invoices_Timeout_Message,
  88. EL_Contract_Account,
  89. EL_Contract_Account_with_Highest_Dunning_Level,
  90. EL_Document_Date,
  91. EL_Due_Date,
  92. EL_Reported_Due_Date,
  93. EL_Dunning_Level,
  94. EL_Dunning_Level_Icon,
  95. EL_Invoice_Dunning_Level,
  96. EL_Invoice_Dunning_Level_Icon,
  97. EL_Invoice_Number,
  98. EL_Open_Amount,
  99. EL_Open_Invoices,
  100. EL_Open_Invoices_for_Contract_Account,
  101. EL_Site_Address,
  102. EL_Total_Open_Amount,
  103. EL_Total_Open_Amount_Information
  104. }
  105.  
  106. //Dunning Level Labels
  107. dunningLevelLabels = {
  108. BAILIFF,
  109. BANKRUPTCY,
  110. CUTOFF,
  111. EXTERNAL_OFFICES,
  112. INSOLVENT,
  113. LAST_REMINDER ,
  114. OVERDEBTNESS,
  115. PAYMENT_ORDER,
  116. REMINDER_LEVEL_1,
  117. REMINDER_LEVEL_2,
  118. REMINDER_LEVEL_3,
  119. WAGE_GARNISHMENT
  120. }
  121.  
  122. //Track Variables
  123. @track data1 = [];
  124. @track data2 = [];
  125. @track columnsTable1 = columnsTable1;
  126. @track columnsTable2 = columnsTable2;
  127. @track defaultSortDirection1 = "desc";
  128. @track defaultSortDirection2 = "asc";
  129. @track sortDirection1;
  130. @track sortDirection2;
  131. @track sortedBy1 = "totalOpenAmount";
  132. @track sortedBy2 = "documentDate";
  133. @track showAccountBalanceInformationSection = false;
  134. @track showTotalOpenAmountInformationSection = false;
  135. @track showOpenInvoicesForContractAccountSection = false;
  136. @track totalOpenAmountOpenInvoice;
  137. @track reportedDueDate;
  138. @track invoiceReportedDueDate;
  139. @track contractAccount;
  140. @track subscriptionSAPContractAccount;
  141. @track listDataInput = [];
  142. @track inputElement;
  143. @track errors = {};
  144. @track wiredInvoicesResult;
  145. @track userRole;
  146. @track hasErrors;
  147.  
  148.  
  149. //Track Variables - Booleans
  150. @track showSpinner = false;
  151. @track showLWC = false;
  152. @track showMoreThan9ContractAccountsWarning = false;
  153. @track showTimeoutWarning = false;
  154. @track isRecordAccount = false;
  155. @track isRecordSubscription = false;
  156.  
  157. //API variables
  158. @api recordId;
  159. @api bpNumber;
  160.  
  161. //Constant Variables
  162. INTEGRATION_MESSAGE_STATUS_IN_ERROR = 'Read timed out';
  163.  
  164. //User Role Variables
  165. USER_ROLE_SLPRO_SUPERVISOR = 'SLPro Supervisor';
  166. USER_ROLE_SLPRO_AGENT = 'SLPro Agent';
  167. USER_ROLE_CSC_SUPERVISOR = 'CSC Supervisor';
  168. USER_ROLE_CSC_AGENT = 'CSC Agent';
  169.  
  170.  
  171. //Other Variables
  172. responseFromSAP = [];
  173. responseSAPContractAccounts = new Map();
  174. isCssLoaded = false;
  175. dunningLevelColor = '';
  176. updatedDueDates = new WeakMap();
  177. draftValues = [];
  178.  
  179. /********************************************************************************************************
  180. *
  181. * @description This is the start method of the LWC when the page loads.
  182. * @date 2022-10-19
  183. * US MP2-1023
  184. *********************************************************************************************************/
  185. connectedCallback(){
  186. //Spinner On
  187. this.showSpinner = true;
  188. //Get user Role
  189. getUserRole()//added via MP2-4111
  190. .then(result => {
  191. this.userRole = result;
  192. }).catch(error => {
  193. console.log('Error while getting the user role ' + JSON.stringify(error));
  194. })
  195.  
  196. //Check if the record is an Account or Subscription
  197. this.checkRecordObjectType();
  198. }
  199.  
  200. /********************************************************************************************************
  201. *
  202. * @description This is the rendering method of the LWC after everything on the page finish loading.
  203. * @date 2022-11-22
  204. * US MP2-1024
  205. *********************************************************************************************************/
  206. renderedCallback(){
  207. //If the page already rendered
  208. if(this.isCssLoaded) {
  209. return;
  210. } else {
  211. //If it's the first time the page finish loading
  212. this.isCssLoaded = true;
  213.  
  214. //Load the COLORS css file
  215. loadStyle(this, COLORS).then(()=>{}).catch(error=>{
  216. //Error handling
  217. this.handleErrorDisplay(error);
  218. });
  219. }
  220. }
  221.  
  222. /********************************************************************************************************
  223. *
  224. * @description This method will check whether the record is an Account or a Subscription
  225. * @date 2022-10-20
  226. * US MP2-1023
  227. *********************************************************************************************************/
  228. checkRecordObjectType() {
  229. if (String(this.recordId).startsWith('001')) {
  230. //Call this method from the controller to get the account record
  231. getAccount({
  232. accountId: this.recordId
  233. }).then(result => {
  234. //Populate fields from accounts to js variables
  235. this.bpNumber = result.EL_BP_Number__c;
  236.  
  237. //Get the Account Balance from SAP
  238. this.getAccountBalanceFromSAP(1); // 1-Account ; 2-Subscription
  239. }).catch(error => {
  240. //Error handling
  241. this.handleErrorDisplay(error);
  242. });
  243.  
  244. //Display components for account
  245. this.isRecordAccount = true;
  246. this.isRecordSubscription = false;
  247.  
  248. } else if (String(this.recordId).startsWith('02i')){
  249. //Call this method from the controller to get the subscription record
  250. getSubscription({
  251. subscriptionId: this.recordId
  252. }).then(result => {
  253. //Populate fields from accounts to js variables
  254. this.bpNumber = result.Account.EL_BP_Number__c;
  255. this.subscriptionSAPContractAccount = result.EL_SAP_Contract_Account__c;
  256.  
  257. //Get the Account Balance from SAP
  258. this.getAccountBalanceFromSAP(2); // 1-Account ; 2-Subscription
  259. }).catch(error => {
  260. //Error handling
  261. this.handleErrorDisplay(error);
  262. });
  263.  
  264. //Display components for subscription
  265. this.isRecordAccount = false;
  266. this.isRecordSubscription = true;
  267. this.showOpenInvoicesForContractAccountSection = true;
  268. }
  269. }
  270.  
  271. /********************************************************************************************************
  272. *
  273. * @description This method will GET the account balance from SAP and populate the front-end tables.
  274. * @date 2022-11-15
  275. * @param recordType: This variable will decide if the record is an Account or Subscription
  276. * @US MP2-1024
  277. *********************************************************************************************************/
  278. getAccountBalanceFromSAP(recordType){
  279. //Call this method from the controller to do the callout and retrieve the account balance
  280. getAccountBalance({
  281. recordId: this.recordId,
  282. bpNumber: this.bpNumber
  283. }).then(result => {
  284. //Move the response in a variable
  285. this.responseFromSAP = result;
  286.  
  287. //Prepare local variables to prepare the table data
  288. let dataTable1 = [];
  289. let dataTable2 = [];
  290.  
  291. //Prepare collection for the SAP Contract accounts
  292. let sapContractAccounts = [];
  293.  
  294. //Check if there are more than 9 contract accounts, if yes - display a warning message
  295. if(this.responseFromSAP.length >= 10){
  296. this.showLWC = false;
  297. this.showMoreThan9ContractAccountsWarning = true;
  298. } else {
  299. //Get the list of contract accounts to query the subscriptions and get the sites
  300. for(let i=0; i < this.responseFromSAP.length; i++){
  301. sapContractAccounts.push(this.responseFromSAP[i].contractAccount);
  302. }
  303.  
  304. getSubscriptionsBySAPContractAccount({
  305. sapContractAccounts: sapContractAccounts
  306. }).then(result => {
  307. //Map the response to a local variable
  308. this.responseSAPContractAccounts = result;
  309.  
  310. //Prepare the SAPContractAccount - Site map local variable
  311. let responseSAPContractAccountsMap = new Map();
  312.  
  313. //Iterate and create the SAPContractNumber - Site map
  314. for (const sapContractNumber in this.responseSAPContractAccounts) {
  315. responseSAPContractAccountsMap.set(sapContractNumber, this.responseSAPContractAccounts[sapContractNumber]);
  316. }
  317.  
  318. //Iterate through the SAP response
  319. for(let i=0; i < this.responseFromSAP.length; i++){
  320.  
  321. //Handle the dunning level colouring
  322. this.handleDunningLevelColor(this.responseFromSAP[i].dunningLevel);
  323.  
  324. //Populate data table 1
  325. dataTable1.push({
  326. id : i,
  327. contractAccount : this.responseFromSAP[i].contractAccount,
  328. siteAddress : responseSAPContractAccountsMap.get(this.responseFromSAP[i].contractAccount),
  329. totalOpenAmount : this.responseFromSAP[i].openAmount,
  330. dunningLevelIcon : this.dunningLevelColor,
  331. dunningLevel : this.responseFromSAP[i].dunningLevel
  332. //reportedDueDate : this.responseFromSAP[i].invoices[j].reportedDueDate
  333. });
  334.  
  335. //Populate the Open Invoices table for the Subscription record
  336. if(this.subscriptionSAPContractAccount != null && this.subscriptionSAPContractAccount == this.responseFromSAP[i].contractAccount){
  337. //Iterate through each invoice and populate the table
  338. for(let j=0; j<this.responseFromSAP[i].invoices.length; j++){
  339.  
  340. //Handle the dunning level colouring
  341. this.handleDunningLevelColor(this.responseFromSAP[i].invoices[j].dunningLevel);
  342.  
  343. //Populate data table 2
  344. dataTable2.push({
  345. id : j,
  346. invoiceNumber : this.responseFromSAP[i].invoices[j].invoiceNumber,
  347. amount : this.responseFromSAP[i].invoices[j].amount,
  348. invoiceDunningLevelIcon : this.dunningLevelColor,
  349. invoiceDunningLevel : this.responseFromSAP[i].invoices[j].dunningLevel,
  350. documentDate : this.responseFromSAP[i].invoices[j].documentDate,
  351. dueDate : this.responseFromSAP[i].invoices[j].dueDate,
  352. invoiceReportedDueDate : this.responseFromSAP[i].invoices[j].duedate
  353. });
  354. }
  355. }
  356. }
  357.  
  358. //Load the data to the front-end tables
  359. this.data1 = dataTable1;
  360. this.data2 = dataTable2;
  361.  
  362. //Default Table Sorting
  363. this.defaultSortingTable();
  364.  
  365. //Spinner Off
  366. this.showSpinner = false;
  367.  
  368. //Show the LWC
  369. this.showLWC = true;
  370. }).catch(error => {
  371. //Error handling
  372. this.handleErrorDisplay(error);
  373. });
  374. }
  375. }).catch(error => {
  376. //Display timeout warning message
  377. if(error.body.message == this.INTEGRATION_MESSAGE_STATUS_IN_ERROR){
  378. this.showTimeoutWarning = true;
  379. }
  380.  
  381. //Error handling
  382. this.handleErrorDisplay(error);
  383. });
  384. }
  385.  
  386. /********************************************************************************************************
  387. *
  388. * @description This method will check the row action (button/drop-down) and call the desired action.
  389. * @date 2022-10-20
  390. * US MP2-1023
  391. *********************************************************************************************************/
  392. callRowAction( event ) {
  393. //Data from the event
  394. const i = event.detail.row.id;
  395. const contractAccountFromRow = event.detail.row.contractAccount;
  396. const totalOpenAmountFromRow = event.detail.row.totalOpenAmount;
  397. const actionName = event.detail.action.name;
  398.  
  399.  
  400.  
  401. //Check what's the desired action
  402. if ( actionName === 'openInvoices' ) {
  403. //Prepare local variables to prepare the table data
  404. let dataTable2 = [];
  405.  
  406. //Display the table 2
  407. this.showOpenInvoicesForContractAccountSection = true;
  408.  
  409. //Variables data to display above the table
  410. this.contractAccount = contractAccountFromRow;
  411. this.totalOpenAmountOpenInvoice = totalOpenAmountFromRow;
  412.  
  413. //Populate through each invoice
  414. for(let j=0; j<this.responseFromSAP[i].invoices.length; j++){
  415.  
  416. //Handle the dunning level colouring
  417. this.handleDunningLevelColor(this.responseFromSAP[i].invoices[j].dunningLevel);
  418.  
  419. //Populate data table 2
  420. dataTable2.push({
  421. id : j,
  422. invoiceNumber : this.responseFromSAP[i].invoices[j].invoiceNumber,
  423. amount : this.responseFromSAP[i].invoices[j].amount,
  424. invoiceDunningLevelIcon : this.dunningLevelColor,
  425. invoiceDunningLevel : this.responseFromSAP[i].invoices[j].dunningLevel,
  426. documentDate : this.responseFromSAP[i].invoices[j].documentDate,
  427. dueDate : this.responseFromSAP[i].invoices[j].dueDate,
  428. invoiceReportedDueDate : this.responseFromSAP[i].invoices[j].dueDate
  429. });
  430. }
  431.  
  432. //Load the data to the front-end tables
  433. this.data2 = dataTable2;
  434. // Log the entire row object
  435. console.log('Row object:', event.detail.row);
  436.  
  437. // Log the contract account and total open amount from the row
  438. console.log('Contract account:', contractAccountFromRow);
  439. console.log('Total open amount:', totalOpenAmountFromRow);
  440. }
  441. }
  442.  
  443. /********************************************************************************************************
  444. *
  445. * @description This method will handle the dunning level cell coloring
  446. * @date 2022-11-22
  447. * US MP2-1024
  448. *********************************************************************************************************/
  449. handleDunningLevelColor(dunningLevelSAP) {
  450.  
  451. //Prepare a map of Dunning Level Text - Color Code
  452. let dunningLevelTextWithColorMap = new Map([
  453. [this.dunningLevelLabels.REMINDER_LEVEL_1, 1],
  454. [this.dunningLevelLabels.REMINDER_LEVEL_2, 2],
  455. [this.dunningLevelLabels.REMINDER_LEVEL_3, 3],
  456. [this.dunningLevelLabels.LAST_REMINDER, 5],
  457. [this.dunningLevelLabels.PAYMENT_ORDER, 6],
  458. [this.dunningLevelLabels.WAGE_GARNISHMENT, 7],
  459. [this.dunningLevelLabels.BAILIFF, 8],
  460. [this.dunningLevelLabels.CUTOFF, 9],
  461. [this.dunningLevelLabels.OVERDEBTNESS, 10],
  462. [this.dunningLevelLabels.EXTERNAL_OFFICES, 11],
  463. [this.dunningLevelLabels.BANKRUPTCY, 12],
  464. [this.dunningLevelLabels.INSOLVENT, 13]
  465. ]);
  466.  
  467. //Find the right color based on the dunning level
  468. switch(dunningLevelTextWithColorMap.get(dunningLevelSAP)) {
  469. case 1: case 2: case 3:
  470. this.dunningLevelColor = "dunning-level-blue dunning-level-blue:hover";
  471. break;
  472. case 5:
  473. this.dunningLevelColor = "dunning-level-orange dunning-level-orange:hover";
  474. break;
  475. case 6:
  476. this.dunningLevelColor = "dunning-level-yellow dunning-level-yellow:hover";
  477. break;
  478. case 7: case 8:
  479. this.dunningLevelColor = "dunning-level-brown dunning-level-brown:hover";
  480. break;
  481. case 9:
  482. this.dunningLevelColor = "dunning-level-red dunning-level-red:hover";
  483. break;
  484. case 10: case 11:
  485. this.dunningLevelColor = "dunning-level-purple dunning-level-purple:hover";
  486. break;
  487. case 12: case 13:
  488. this.dunningLevelColor = "dunning-level-black dunning-level-black:hover";
  489. break;
  490. default:
  491. this.dunningLevelColor = "dunning-level-green dunning-level-green:hover";
  492. }
  493. }
  494.  
  495. /********************************************************************************************************
  496. *
  497. * @description This method will handle the default sorting of the tables.
  498. * @date 2022-11-20
  499. * US MP2-1024
  500. *********************************************************************************************************/
  501. defaultSortingTable() {
  502. //Clone the tables data
  503. const cloneData1 = [...this.data1];
  504. const cloneData2 = [...this.data2];
  505.  
  506. //Sort the cloned data
  507. cloneData1.sort(this.sortBy(this.sortedBy1, this.defaultSortDirection1 === 'asc' ? 1 : -1));
  508. cloneData2.sort(this.sortBy(this.sortedBy2, this.defaultSortDirection2 === 'asc' ? 1 : -1));
  509.  
  510. //Overwrite the tables data with the new sorted one
  511. this.data1 = cloneData1;
  512. this.data2 = cloneData2;
  513. }
  514.  
  515. /********************************************************************************************************
  516. *
  517. * @description This method will handle the sort of the table Total Open Amount Information
  518. * @date 2022-11-20
  519. * US MP2-1024
  520. *********************************************************************************************************/
  521. onHandleSortTotalOpenAmountInformation(event) {
  522. const { fieldName: sortedBy, sortDirection } = event.detail;
  523.  
  524. //Clone the table data
  525. const cloneData = [...this.data1];
  526.  
  527. //Sort the cloned data
  528. cloneData.sort(this.sortBy(sortedBy, sortDirection === 'desc' ? 1 : -1));
  529.  
  530. //Overwrite the table data with the new sorted one
  531. this.data1 = cloneData;
  532. this.sortDirection1 = sortDirection;
  533. this.sortedBy1 = sortedBy;
  534. }
  535.  
  536. /********************************************************************************************************
  537. *
  538. * @description This method will handle the sort of the table Open Invoices for Contract Account
  539. * @date 2022-11-20
  540. * US MP2-1024
  541. *********************************************************************************************************/
  542. onHandleSortOpenInvoicesForContractAccount(event) {
  543. const { fieldName: sortedBy, sortDirection } = event.detail;
  544.  
  545. //Clone the table data
  546. const cloneData = [...this.data2];
  547.  
  548. //Sort the cloned data
  549. cloneData.sort(this.sortBy(sortedBy, sortDirection === 'asc' ? 1 : -1));
  550.  
  551. //Overwrite the table data with the new sorted one
  552. this.data2 = cloneData;
  553. this.sortDirection2 = sortDirection;
  554. this.sortedBy2 = sortedBy;
  555. }
  556.  
  557. /********************************************************************************************************
  558. *
  559. * @description This method is used to sort tables based on a field and a direction
  560. * @date 2022-11-20
  561. * US MP2-1024
  562. *********************************************************************************************************/
  563. sortBy(field, reverse, primer) {
  564. const key = primer
  565. ? function (x) {
  566. return primer(x[field]);
  567. }
  568. : function (x) {
  569. return x[field];
  570. };
  571.  
  572. return function (a, b) {
  573. a = key(a);
  574. b = key(b);
  575. return reverse * ((a > b) - (b > a));
  576. };
  577. }
  578.  
  579. /********************************************************************************************************
  580. *
  581. * @description This method will handle the error display
  582. * @date 2022-11-20
  583. * US MP2-1024
  584. *********************************************************************************************************/
  585. handleErrorDisplay(error) {
  586. //Hide the Tables
  587. this.showLWC = false;
  588.  
  589. //Hide the spinner
  590. this.showSpinner = false;
  591.  
  592. //Show the error message
  593. this.dispatchEvent( new ShowToastEvent({
  594. title: 'Error',
  595. message: error.body.message,
  596. variant: 'error'
  597. })
  598. );
  599. }
  600.  
  601. validationRule(event){
  602.  
  603. let hasErrors = false;
  604. let selectedRow = event.detail.selectedRows;
  605. console.log('tabledata2: ', JSON.stringify(this.data2));
  606. if (this.data2.length > 0) {
  607. const firstRow = this.data2[0];
  608. firstRow.invoiceReportedDueDate = '2023-06-01';
  609. console.log('First selected row:', JSON.stringify(firstRow));
  610.  
  611. // Convert date strings to Date objects
  612. var invoiceReportedDueDate = new Date(firstRow.invoiceReportedDueDate);
  613. var dueDate = new Date(firstRow.dueDate);
  614.  
  615. // Calculate the difference in milliseconds
  616. var dif = Math.abs(invoiceReportedDueDate - dueDate);
  617.  
  618. // Calculate the difference in days
  619. var d = dif / (1000 * 3600 * 24);
  620.  
  621. // Log specific properties from the first selected row
  622. console.log('Color :', JSON.stringify(firstRow.invoiceDunningLevelIcon));
  623. console.log('DueDate:', JSON.stringify(firstRow.dueDate));
  624. console.log('ReportedDueDate:', JSON.stringify(firstRow.invoiceReportedDueDate));
  625. console.log('dif', dif);
  626. console.log('days', d);
  627. }
  628.  
  629. console.log('draft Value: ', event.detail.draftValues);
  630.  
  631.  
  632. const editedRow = [];
  633.  
  634. for(let i = 0; i < selectedRow.length; i++){
  635. console.log('Reported Due Date', selectedRow[i].invoiceReportedDueDate );
  636. console.log('Due Date', selectedRow[i].dueDate);
  637. console.log('Color1 : ', selectedRow[i].invoiceDunningLevelIcon);
  638. console.log('reportedDueDate : ', selectedRow[i].invoiceReportedDueDate);
  639. if((selectedRows[0].JSON.stringify(invoiceDunningLevelIcon).includes("dunning-level-green") || selectedRows[0].JSON.stringify(invoiceDunningLevelIcon).includes("dunning-level-blue")) && d > 30 ){
  640. this.dispatchEvent(
  641. new ShowToastEvent({
  642. title: 'Error',
  643. message: 'Nu ai voie',
  644. variant: 'error'
  645. })
  646. );
  647.  
  648. }
  649. hasErrors = true;
  650. }
  651. return hasErrors;
  652.  
  653. }
  654.  
  655. saveHandleAction(event) {
  656. // Validation
  657. let selectedRow = event.detail.selectedRows;
  658.  
  659. // Validate the data
  660. this.validationRule(event);
  661.  
  662. // Save the data if there are no validation errors
  663. try {
  664. this.inputElement = event.detail.draftValues;
  665. console.log('Input Event.DraftValues', event.detail.draftValues);
  666. console.log('Input element', this.inputElement.length);
  667.  
  668. this.draftValues = [];
  669. console.log('This draftValue: ', this.draftValues);
  670.  
  671. this.dispatchEvent(
  672. new ShowToastEvent({
  673. title: 'Success',
  674. message: 'Records Updated Successfully!!',
  675. variant: 'success'
  676. })
  677. );
  678. } catch (error) {
  679. console.error('Update error:', error);
  680. this.dispatchEvent(
  681. new ShowToastEvent({
  682. title: 'Error',
  683. message: 'An Error Occured!!',
  684. variant: 'error'
  685. })
  686. );
  687. }
  688. }
  689.  
  690. saveHandleActionAccount(event) {
  691. // Validation
  692. let selectedRow = event.detail.draftValues;
  693.  
  694. // Save the data if there are no validation errors
  695. this.draftValues = [];
  696. try {
  697. this.inputElement = event.detail.draftValues;
  698. console.log('Input Event.DraftValues', event.detail.draftValues);
  699. console.log('Input element', this.inputElement.length);
  700.  
  701. console.log('This draftValue: ', this.draftValues);
  702.  
  703. this.dispatchEvent(
  704. new ShowToastEvent({
  705. title: 'Success',
  706. message: 'Records Updated Successfully!!',
  707. variant: 'success'
  708. })
  709. );
  710. } catch (error) {
  711. console.error('Update error:', error);
  712. this.dispatchEvent(
  713. new ShowToastEvent({
  714. title: 'Error',
  715. message: 'An Error Occured!!',
  716. variant: 'error'
  717. })
  718. );
  719. }
  720. }
  721. }
  722.  
  723.  
  724.  
  725.  
  726.  
  727.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement