@tomei/finance 0.14.0 → 0.15.0-dev.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (50) hide show
  1. package/dist/document/document.d.ts +3 -2
  2. package/dist/document/document.d.ts.map +1 -1
  3. package/dist/document/document.js +323 -207
  4. package/dist/document/document.js.map +1 -1
  5. package/dist/finance-company/finance-company.d.ts +15 -3
  6. package/dist/finance-company/finance-company.d.ts.map +1 -1
  7. package/dist/finance-company/finance-company.js +145 -122
  8. package/dist/finance-company/finance-company.js.map +1 -1
  9. package/dist/helpers/chrome-browser-pool.d.ts +17 -0
  10. package/dist/helpers/chrome-browser-pool.d.ts.map +1 -0
  11. package/dist/helpers/chrome-browser-pool.js +109 -0
  12. package/dist/helpers/chrome-browser-pool.js.map +1 -0
  13. package/dist/helpers/chrome-setup.d.ts +16 -0
  14. package/dist/helpers/chrome-setup.d.ts.map +1 -0
  15. package/dist/helpers/chrome-setup.js +308 -0
  16. package/dist/helpers/chrome-setup.js.map +1 -0
  17. package/dist/index.d.ts +3 -1
  18. package/dist/index.d.ts.map +1 -1
  19. package/dist/index.js +4 -1
  20. package/dist/index.js.map +1 -1
  21. package/dist/tsconfig.tsbuildinfo +1 -1
  22. package/package.json +3 -1
  23. package/receipt-template/default/assets/css/style.css +2630 -0
  24. package/receipt-template/default/assets/css/style.css.map +13 -0
  25. package/receipt-template/default/assets/css/style.min.css +1 -0
  26. package/receipt-template/default/assets/img/arrow_bg.svg +12 -0
  27. package/receipt-template/default/assets/img/coffy_shop_img.svg +19 -0
  28. package/receipt-template/default/assets/img/ezcash-logo.png +0 -0
  29. package/receipt-template/default/assets/img/header_bg.jpeg +0 -0
  30. package/receipt-template/default/assets/img/header_bg_2.jpeg +0 -0
  31. package/receipt-template/default/assets/img/logo.svg +4 -0
  32. package/receipt-template/default/assets/img/logo_accent.svg +4 -0
  33. package/receipt-template/default/assets/img/logo_white.svg +4 -0
  34. package/receipt-template/default/assets/img/qbar.png +0 -0
  35. package/receipt-template/default/assets/img/shape_1.svg +1 -0
  36. package/receipt-template/default/assets/img/shape_2.svg +1 -0
  37. package/receipt-template/default/assets/img/sign.svg +12 -0
  38. package/receipt-template/default/assets/img/tomei-logo.png +0 -0
  39. package/receipt-template/default/assets/img/tomei-logo.svg +9 -0
  40. package/receipt-template/default/assets/js/html2canvas.min.js +10379 -0
  41. package/receipt-template/default/assets/js/jquery.min.js +2 -0
  42. package/receipt-template/default/assets/js/jspdf.min.js +1 -0
  43. package/receipt-template/default/assets/js/main.js +48 -0
  44. package/receipt-template/default/assets/sass/common/_color_variable.scss +12 -0
  45. package/receipt-template/default/assets/sass/common/_invoice.scss +1802 -0
  46. package/receipt-template/default/assets/sass/common/_normalize.scss +350 -0
  47. package/receipt-template/default/assets/sass/common/_typography.scss +178 -0
  48. package/receipt-template/default/assets/sass/style.scss +12 -0
  49. package/receipt-template/default/index.html +291 -0
  50. package/scripts/postinstall.js +301 -0
@@ -67,14 +67,15 @@ export default class Document extends AccountSystemEntity {
67
67
  static settleByCreditNote(loginUser: LoginUserBase, dbTransaction: any, DocNo: string, AmountToReduce: number): Promise<Document>;
68
68
  static DEFAULT_INVOICE_TEMPLATE_HTML(): Promise<Express.Multer.File>;
69
69
  DEFAULT_INVOICE_TEMPLATE_PDF(customer: FinanceCustomerBase, date?: Date): Promise<Express.Multer.File>;
70
- DEFAULT_RECEIPT_TEMPLATE_PDF(customer: FinanceCustomerBase, date?: Date): Promise<Express.Multer.File>;
70
+ DEFAULT_RECEIPT_TEMPLATE_PDF(customer: FinanceCustomerBase, date?: Date, templateType?: string, customData?: any): Promise<Express.Multer.File>;
71
+ private launchBrowser;
71
72
  get DocumentFileInHTML(): Promise<MediasModel>;
72
73
  get DocumentFileInPDF(): Promise<MediasModel>;
73
74
  generateInvoice(userId?: string, customer?: FinanceCustomerBase, dbTransaction?: any, date?: Date): Promise<{
74
75
  HTMLMedia: MediasModel;
75
76
  PDFMedia: MediasModel;
76
77
  }>;
77
- generateReceipt(userId?: string, customer?: FinanceCustomerBase, transaction?: any, date?: Date): Promise<{
78
+ generateReceipt(userId?: string, customer?: FinanceCustomerBase, transaction?: any, date?: Date, templateType?: string, customData?: any): Promise<{
78
79
  HTMLMedia: MediasModel;
79
80
  PDFMedia: MediasModel;
80
81
  }>;
@@ -1 +1 @@
1
- {"version":3,"file":"document.d.ts","sourceRoot":"","sources":["../../src/document/document.ts"],"names":[],"mappings":"AAIA,OAAO,EAGL,aAAa,IAAI,kBAAkB,EAEnC,WAAW,EAEZ,MAAM,cAAc,CAAC;AACtB,OAAO,EAAE,cAAc,EAAE,OAAO,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AAChE,OAAO,EAAE,aAAa,EAAE,MAAM,sCAAsC,CAAC;AACrE,OAAO,EAAE,gBAAgB,EAAE,MAAM,0CAA0C,CAAC;AAE5E,OAAO,YAAY,MAAM,iBAAiB,CAAC;AAC3C,OAAO,EAAE,mBAAmB,EAAE,MAAM,gDAAgD,CAAC;AACrF,OAAO,EAAE,kBAAkB,EAAE,MAAM,uBAAuB,CAAC;AAC3D,OAAO,mBAAmB,MAAM,sBAAsB,CAAC;AACvD,OAAO,aAAa,MAAM,2BAA2B,CAAC;AAEtD,OAAO,EAAc,aAAa,EAAE,MAAM,gBAAgB,CAAC;AAK3D,OAAO,EAAE,cAAc,EAAE,MAAM,wCAAwC,CAAC;AAExE,MAAM,CAAC,OAAO,OAAO,QAAS,SAAQ,mBAAmB;IACvD,KAAK,SAAS;IACd,OAAO,CAAC,QAAQ,CAAU;IAC1B,OAAO,CAAC,QAAQ,CAAc;IAC9B,QAAQ,SAAS;IACjB,OAAO,CAAC,OAAO,CAAK;IACpB,WAAW,SAAM;IACjB,OAAO,CAAC,OAAO,CAAyB;IACxC,UAAU,SAAM;IAChB,UAAU,SAAM;IAChB,YAAY,SAAM;IAClB,eAAe,SAAM;IACrB,iBAAiB,SAAM;IACvB,WAAW,SAAM;IACjB,SAAS,EAAE,IA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CAAC,EAAE,MAAM,EACf,QAAQ,CAAC,EAAE,mBAAmB,EAC9B,aAAa,CAAC,EAAE,GAAG,EACnB,IAAI,CAAC,EAAE,IAAI,GACV,OAAO,CAAC;QACT,SAAS,EAAE,WAAW,CAAC;QACvB,QAAQ,EAAE,WAAW,CAAC;KACvB,CAAC;IAmFI,eAAe,CACnB,MAAM,CAAC,EAAE,MAAM,EACf,QAAQ,CAAC,EAAE,mBAAmB,EAC9B,WAAW,CAAC,EAAE,GAAG,EACjB,IAAI,CAAC,EAAE,IAAI,EACX,YAAY,CAAC,EAAE,MAAM,EACrB,UAAU,CAAC,EAAE,GAAG,GACf,OAAO,CAAC;QACT,SAAS,EAAE,WAAW,CAAC;QACvB,QAAQ,EAAE,WAAW,CAAC;KACvB,CAAC;IA0EI,kBAAkB,CAAC,MAAM,CAAC,EAAE,MAAM,EAAE,QAAQ,CAAC,EAAE,mBAAmB;IAUlE,eAAe,CAAC,YAAY,CAAC,EAAE,YAAY,GAAG,OAAO,CAAC,YAAY,CAAC;IAsBnE,mBAAmB,CAAC,OAAO,EAAE,OAAO,GAAG,OAAO,CAAC,aAAa,CAAC;WAqB/C,OAAO,CACzB,SAAS,EAAE,aAAa,EACxB,aAAa,EAAE,GAAG,EAClB,IAAI,CAAC,EAAE,MAAM,EACb,GAAG,CAAC,EAAE,MAAM,EACZ,MAAM,CAAC,EAAE,gBAAgB,EACzB,KAAK,CAAC,EAAE,CAAC,MAAM,EAAE,MAAM,CAAC,EAAE,EAC1B,gBAAgB,CAAC,EAAE,cAAc,GAChC,OAAO,CAAC;QAAE,KAAK,EAAE,MAAM,CAAC;QAAC,IAAI,EAAE,aAAa,EAAE,CAAA;KAAE,CAAC;CAwKrD"}
@@ -51,6 +51,7 @@ const config_1 = require("@tomei/config");
51
51
  const sequelize_1 = require("sequelize");
52
52
  const login_user_1 = require("../helpers/login-user");
53
53
  const activity_history_1 = require("@tomei/activity-history");
54
+ const chrome_setup_1 = require("../helpers/chrome-setup");
54
55
  class Document extends account_system_entity_1.AccountSystemEntity {
55
56
  get ObjectType() {
56
57
  return this._ObjectType;
@@ -328,15 +329,15 @@ class Document extends account_system_entity_1.AccountSystemEntity {
328
329
  PostedDateTime: document.PostedDateTime,
329
330
  UseAccSystemDocYN: document.UseAccSystemDocYN,
330
331
  };
332
+ const newAmount = document.Amount - AmountToReduce;
331
333
  let updatedStatus;
332
- if (AmountToReduce == document.Amount) {
334
+ if (AmountToReduce == document.Amount || newAmount == 0) {
333
335
  updatedStatus = enum_1.DocumentStatus.SETTLED;
334
336
  }
335
337
  else {
336
338
  updatedStatus = enum_1.DocumentStatus.PARTIALSETTLED;
337
339
  }
338
340
  await this._RepositoryBase.update({
339
- Amount: AmountToReduce,
340
341
  Status: updatedStatus,
341
342
  UpdatedAt: document.UpdatedAt,
342
343
  UpdatedById: document.UpdatedById,
@@ -409,10 +410,7 @@ class Document extends account_system_entity_1.AccountSystemEntity {
409
410
  const [htmlString] = await Promise.all([
410
411
  readFileContent(templateLocation, 'utf8'),
411
412
  ]);
412
- const browser = await puppeteer.launch({
413
- headless: true,
414
- args: ['--no-sandbox', '--disable-setuid-sandbox'],
415
- });
413
+ const browser = await this.launchBrowser();
416
414
  const page = await browser.newPage();
417
415
  const imagePath = path.resolve('./invoice-template/assets/img/ezcash-logo.svg');
418
416
  const imageBuffer = await readFileContent(imagePath);
@@ -575,182 +573,290 @@ class Document extends account_system_entity_1.AccountSystemEntity {
575
573
  };
576
574
  return pdfStream;
577
575
  }
578
- async DEFAULT_RECEIPT_TEMPLATE_PDF(customer, date = new Date()) {
579
- const templateLocation = path.resolve('./invoice-template/index.html');
580
- const cssLocation = path.resolve('./invoice-template/assets/css/style.css');
581
- const readFileContent = util.promisify(fs.readFile);
582
- const [htmlString] = await Promise.all([
583
- readFileContent(templateLocation, 'utf8'),
584
- ]);
585
- const browser = await puppeteer.launch({
586
- headless: true,
587
- args: ['--no-sandbox', '--disable-setuid-sandbox'],
588
- });
589
- const page = await browser.newPage();
590
- const imagePath = path.resolve('./invoice-template/assets/img/ezcash-logo.svg');
591
- const imageBuffer = await readFileContent(imagePath);
592
- const imageDataUrl = `data:image/svg+xml;base64,${imageBuffer.toString('base64')}`;
593
- const imgTag = `<img src="${imageDataUrl}" alt="Logo">`;
594
- await page.setContent(htmlString);
595
- await page.addStyleTag({ path: cssLocation });
596
- const divHandle = await page.$('.tm_logo');
597
- await divHandle.evaluate((div, imgTag) => {
598
- div.innerHTML = imgTag;
599
- }, imgTag);
600
- const currentDate = date.toLocaleDateString('en-GB');
601
- const receiptNo = this.DocNo;
602
- const receiptTitle = `
603
- <div class="tm_primary_color tm_f50 tm_text_uppercase">
604
- Receipt
576
+ async DEFAULT_RECEIPT_TEMPLATE_PDF(customer, date = new Date(), templateType = 'default', customData) {
577
+ let browser = null;
578
+ try {
579
+ const baseTemplatePath = './receipt-template';
580
+ const templateFolder = templateType || 'default';
581
+ const templateLocation = path.resolve(`${baseTemplatePath}/${templateFolder}/index.html`);
582
+ const cssLocation = path.resolve(`${baseTemplatePath}/${templateFolder}/assets/css/style.css`);
583
+ if (!fs.existsSync(templateLocation)) {
584
+ throw new Error(`Template file not found: ${templateLocation}`);
585
+ }
586
+ if (!fs.existsSync(cssLocation)) {
587
+ throw new Error(`CSS file not found: ${cssLocation}`);
588
+ }
589
+ const readFileContent = util.promisify(fs.readFile);
590
+ const [htmlString] = await Promise.all([
591
+ readFileContent(templateLocation, 'utf8'),
592
+ ]);
593
+ browser = await this.launchBrowser();
594
+ const page = await browser.newPage();
595
+ await page.setContent(htmlString);
596
+ await page.addStyleTag({ path: cssLocation });
597
+ if (templateType === 'default') {
598
+ const imagePath = path.resolve('./invoice-template/assets/img/ezcash-logo.svg');
599
+ const imageBuffer = await readFileContent(imagePath);
600
+ const imageDataUrl = `data:image/svg+xml;base64,${imageBuffer.toString('base64')}`;
601
+ const imgTag = `<img src="${imageDataUrl}" alt="Logo">`;
602
+ const divHandle = await page.$('.tm_logo');
603
+ if (divHandle) {
604
+ await divHandle.evaluate((div, imgTag) => {
605
+ div.innerHTML = imgTag;
606
+ }, imgTag);
607
+ }
608
+ const currentDate = date.toLocaleDateString('en-GB');
609
+ const receiptNo = this.DocNo;
610
+ const receiptTitle = `
611
+ <div class="tm_primary_color tm_f50 tm_text_uppercase">
612
+ Receipt
613
+ </div>
614
+ `;
615
+ const receiptTitleHandle = await page.$('.tm_invoice_title');
616
+ if (receiptTitleHandle) {
617
+ await receiptTitleHandle.evaluate((receipt, receiptTitle) => {
618
+ receipt.innerHTML = receiptTitle;
619
+ }, receiptTitle);
620
+ }
621
+ const receiptTitleInfo = `
622
+ <div class="tm_invoice_seperator tm_gray_bg"></div>
623
+ <div class="tm_invoice_info_list">
624
+ <p class="tm_invoice_number tm_m0">
625
+ Receipt No: <b class="tm_primary_color">${receiptNo}</b>
626
+ </p>
627
+ <p class="tm_invoice_date tm_m0">
628
+ Date: <b class="tm_primary_color">${currentDate}</b>
629
+ </p>
605
630
  </div>
606
- `;
607
- const receiptTitleHandle = await page.$('.tm_invoice_title');
608
- await receiptTitleHandle.evaluate((receipt, receiptTitle) => {
609
- receipt.innerHTML = receiptTitle;
610
- }, receiptTitle);
611
- const receiptTitleInfo = `
612
- <div class="tm_invoice_seperator tm_gray_bg"></div>
613
- <div class="tm_invoice_info_list">
614
- <p class="tm_invoice_number tm_m0">
615
- Receipt No: <b class="tm_primary_color">${receiptNo}</b>
631
+ `;
632
+ const receiptTitleInfoHandle = await page.$('.tm_invoice_info');
633
+ if (receiptTitleInfoHandle) {
634
+ await receiptTitleInfoHandle.evaluate((receipt, receiptTitleInfo) => {
635
+ receipt.innerHTML = receiptTitleInfo;
636
+ }, receiptTitleInfo);
637
+ }
638
+ const customerAddress = customer?.DefaultAddress;
639
+ const receiptCustomerContent = `
640
+ <p class="tm_mb2">
641
+ <b class="tm_primary_color">Issued To:</b>
616
642
  </p>
617
- <p class="tm_invoice_date tm_m0">
618
- Date: <b class="tm_primary_color">${currentDate}</b>
643
+ <p>
644
+ ${customer?.FullName || 'N/A'} <br />
645
+ ${customerAddress?.AddressLine1 || ''}, ${customerAddress?.City || ''} <br />
646
+ ${customerAddress?.Country || ''} <br />
647
+ ${customer?.Email || ''}
619
648
  </p>
620
- </div>
621
- `;
622
- const receiptTitleInfoHandle = await page.$('.tm_invoice_info');
623
- await receiptTitleInfoHandle.evaluate((receipt, receiptTitleInfo) => {
624
- receipt.innerHTML = receiptTitleInfo;
625
- }, receiptTitleInfo);
626
- const receiptCustomerContent = `
627
- <p class="tm_mb2">
628
- <b class="tm_primary_color">Issued To:</b>
629
- </p>
630
- <p>
631
- ${customer.FullName} <br />
632
- ${customer.DefaultAddress.AddressLine1}, ${customer.DefaultAddress.City} <br />
633
- ${customer.DefaultAddress.Country} <br />
634
- ${customer.Email}
635
- </p>
636
- `;
637
- const receiptCustomerHandle = await page.$('.tm_customer_detail');
638
- await receiptCustomerHandle.evaluate((receipt, receiptCustomerContent) => {
639
- receipt.innerHTML = receiptCustomerContent;
640
- }, receiptCustomerContent);
641
- const receiptPayToContent = ``;
642
- const receiptPayToHandle = await page.$('.tm_payto_detail');
643
- await receiptPayToHandle.evaluate((receipt, receiptPayToContent) => {
644
- receipt.innerHTML = receiptPayToContent;
645
- }, receiptPayToContent);
646
- const receiptPaymentInfoContent = ``;
647
- const receiptPaymentInfoHandle = await page.$('.tm_payment_info');
648
- await receiptPaymentInfoHandle.evaluate((receipt, receiptPaymentInfoContent) => {
649
- receipt.innerHTML = receiptPaymentInfoContent;
650
- }, receiptPaymentInfoContent);
651
- const receiptSubtotal = +this.Amount;
652
- const receiptTax = 0;
653
- const receiptRightFooterContent = `
654
- <table>
655
- <tbody>
656
- <tr>
657
- <td
658
- class="tm_width_3 tm_primary_color tm_border_none tm_bold"
659
- >
660
- Subtotal
661
- </td>
662
- <td
663
- class="tm_width_3 tm_primary_color tm_text_right tm_border_none tm_bold"
664
- >
665
- ${this.Currency} ${receiptSubtotal}
666
- </td>
667
- </tr>
649
+ `;
650
+ const receiptCustomerHandle = await page.$('.tm_customer_detail');
651
+ if (receiptCustomerHandle) {
652
+ await receiptCustomerHandle.evaluate((receipt, receiptCustomerContent) => {
653
+ receipt.innerHTML = receiptCustomerContent;
654
+ }, receiptCustomerContent);
655
+ }
656
+ const receiptPayToContent = ``;
657
+ const receiptPayToHandle = await page.$('.tm_payto_detail');
658
+ if (receiptPayToHandle) {
659
+ await receiptPayToHandle.evaluate((receipt, receiptPayToContent) => {
660
+ receipt.innerHTML = receiptPayToContent;
661
+ }, receiptPayToContent);
662
+ }
663
+ const receiptPaymentInfoContent = ``;
664
+ const receiptPaymentInfoHandle = await page.$('.tm_payment_info');
665
+ if (receiptPaymentInfoHandle) {
666
+ await receiptPaymentInfoHandle.evaluate((receipt, receiptPaymentInfoContent) => {
667
+ receipt.innerHTML = receiptPaymentInfoContent;
668
+ }, receiptPaymentInfoContent);
669
+ }
670
+ const receiptSubtotal = +this.Amount;
671
+ const receiptTax = 0;
672
+ const receiptRightFooterContent = `
673
+ <table>
674
+ <tbody>
675
+ <tr>
676
+ <td
677
+ class="tm_width_3 tm_primary_color tm_border_none tm_bold"
678
+ >
679
+ Subtotal
680
+ </td>
681
+ <td
682
+ class="tm_width_3 tm_primary_color tm_text_right tm_border_none tm_bold"
683
+ >
684
+ ${this.Currency} ${receiptSubtotal}
685
+ </td>
686
+ </tr>
687
+ <tr>
688
+ <td
689
+ class="tm_width_3 tm_primary_color tm_border_none tm_pt0"
690
+ >
691
+ Tax <span class="tm_ternary_color">(0%)</span>
692
+ </td>
693
+ <td
694
+ class="tm_width_3 tm_primary_color tm_text_right tm_border_none tm_pt0"
695
+ >
696
+ ${this.Currency} ${receiptTax}
697
+ </td>
698
+ </tr>
699
+ <tr class="tm_border_top tm_border_bottom">
700
+ <td
701
+ class="tm_width_3 tm_border_top_0 tm_bold tm_f16 tm_primary_color"
702
+ >
703
+ Grand Total
704
+ </td>
705
+ <td
706
+ class="tm_width_3 tm_border_top_0 tm_bold tm_f16 tm_primary_color tm_text_right"
707
+ >
708
+ ${this.Currency} ${receiptSubtotal + receiptTax}
709
+ </td>
710
+ </tr>
711
+ </tbody>
712
+ </table>
713
+ `;
714
+ const receiptRightFooterHandle = await page.$('.tm_item_total_section');
715
+ if (receiptRightFooterHandle) {
716
+ await receiptRightFooterHandle.evaluate((receipt, receiptRightFooterContent) => {
717
+ receipt.innerHTML = receiptRightFooterContent;
718
+ }, receiptRightFooterContent);
719
+ }
720
+ const receiptDocumentItems = await this.DocumentItems;
721
+ let receiptItemContent = ``;
722
+ receiptDocumentItems.forEach((documentItem, i) => {
723
+ receiptItemContent += `
668
724
  <tr>
669
- <td
670
- class="tm_width_3 tm_primary_color tm_border_none tm_pt0"
671
- >
672
- Tax <span class="tm_ternary_color">(0%)</span>
673
- </td>
674
- <td
675
- class="tm_width_3 tm_primary_color tm_text_right tm_border_none tm_pt0"
676
- >
677
- ${this.Currency} ${receiptTax}
678
- </td>
679
- </tr>
680
- <tr class="tm_border_top tm_border_bottom">
681
- <td
682
- class="tm_width_3 tm_border_top_0 tm_bold tm_f16 tm_primary_color"
683
- >
684
- Grand Total
685
- </td>
686
- <td
687
- class="tm_width_3 tm_border_top_0 tm_bold tm_f16 tm_primary_color tm_text_right"
688
- >
689
- ${this.Currency} ${receiptSubtotal + receiptTax}
725
+ <td class="tm_width_3">${i + 1}. ${documentItem.Name}</td>
726
+ <td class="tm_width_4">
727
+ ${documentItem.Description}
690
728
  </td>
729
+ <td class="tm_width_2">${documentItem.Currency} ${documentItem.UnitPrice}</td>
730
+ <td class="tm_width_1">${documentItem.Quantity}</td>
731
+ <td class="tm_width_2 tm_text_right">${documentItem.Currency} ${+documentItem.UnitPrice * +documentItem.Quantity}</td>
691
732
  </tr>
692
- </tbody>
693
- </table>
694
- `;
695
- const receiptRightFooterHandle = await page.$('.tm_item_total_section');
696
- await receiptRightFooterHandle.evaluate((receipt, receiptRightFooterContent) => {
697
- receipt.innerHTML = receiptRightFooterContent;
698
- }, receiptRightFooterContent);
699
- const receiptDocumentItems = await this.DocumentItems;
700
- let receiptItemContent = ``;
701
- receiptDocumentItems.forEach((documentItem, i) => {
702
- receiptItemContent += `
703
- <tr>
704
- <td class="tm_width_3">${i + 1}. ${documentItem.Name}</td>
705
- <td class="tm_width_4">
706
- ${documentItem.Description}
707
- </td>
708
- <td class="tm_width_2">${documentItem.Currency} ${documentItem.UnitPrice}</td>
709
- <td class="tm_width_1">${documentItem.Quantity}</td>
710
- <td class="tm_width_2 tm_text_right">${documentItem.Currency} ${+documentItem.UnitPrice * +documentItem.Quantity}</td>
711
- </tr>
712
- `;
713
- });
714
- const receiptDocumentItemHandle = await page.$('.tm_items_content');
715
- await receiptDocumentItemHandle.evaluate((receipt, receiptItemContent) => {
716
- receipt.innerHTML = receiptItemContent;
717
- }, receiptItemContent);
718
- const receiptFooterContent = `
719
- <p class="tm_mb5 tm_text_center tm_f11">
720
- <i>
721
- Thank you for your payment. This is computer generated receipt
722
- - no signature is required.
723
- </i>
724
- </p>
725
- `;
726
- const receiptFooterHandle = await page.$('.tm_footer_section');
727
- await receiptFooterHandle.evaluate((receipt, receiptFooterContent) => {
728
- receipt.innerHTML = receiptFooterContent;
729
- }, receiptFooterContent);
730
- const pdfBuffer = await page.pdf({
731
- format: 'a4',
732
- printBackground: true,
733
- margin: {
734
- left: '0px',
735
- top: '0px',
736
- right: '0px',
737
- bottom: '0px',
738
- },
739
- });
740
- const pdfBufferAsBuffer = Buffer.from(pdfBuffer);
741
- const pdfStream = {
742
- buffer: pdfBufferAsBuffer,
743
- fieldname: 'FileStream',
744
- originalname: 'sample-invoice.pdf',
745
- mimetype: 'application/pdf',
746
- size: pdfBuffer.length,
747
- encoding: '8bit',
748
- filename: 'sample-invoice.pdf',
749
- destination: '',
750
- path: '',
751
- stream: null,
752
- };
753
- return pdfStream;
733
+ `;
734
+ });
735
+ const receiptDocumentItemHandle = await page.$('.tm_items_content');
736
+ if (receiptDocumentItemHandle) {
737
+ await receiptDocumentItemHandle.evaluate((receipt, receiptItemContent) => {
738
+ receipt.innerHTML = receiptItemContent;
739
+ }, receiptItemContent);
740
+ }
741
+ const receiptFooterContent = `
742
+ <p class="tm_mb5 tm_text_center tm_f11">
743
+ <i>
744
+ Thank you for your payment. This is computer generated receipt
745
+ - no signature is required.
746
+ </i>
747
+ </p>
748
+ `;
749
+ const receiptFooterHandle = await page.$('.tm_footer_section');
750
+ if (receiptFooterHandle) {
751
+ await receiptFooterHandle.evaluate((receipt, receiptFooterContent) => {
752
+ receipt.innerHTML = receiptFooterContent;
753
+ }, receiptFooterContent);
754
+ }
755
+ }
756
+ else {
757
+ if (customData) {
758
+ let updatedHtml = await page.content();
759
+ const placeholderPattern = /\{\{([^}]+)\}\}/g;
760
+ updatedHtml = updatedHtml.replace(placeholderPattern, (match, key) => {
761
+ return customData[key] !== undefined ? customData[key] : match;
762
+ });
763
+ await page.setContent(updatedHtml);
764
+ }
765
+ }
766
+ const pdfBuffer = await page.pdf({
767
+ format: 'a4',
768
+ printBackground: true,
769
+ margin: {
770
+ left: '0px',
771
+ top: '0px',
772
+ right: '0px',
773
+ bottom: '0px',
774
+ },
775
+ });
776
+ const pdfBufferAsBuffer = Buffer.from(pdfBuffer);
777
+ const pdfStream = {
778
+ buffer: pdfBufferAsBuffer,
779
+ fieldname: 'FileStream',
780
+ originalname: 'sample-receipt.pdf',
781
+ mimetype: 'application/pdf',
782
+ size: pdfBuffer.length,
783
+ encoding: '8bit',
784
+ filename: 'sample-receipt.pdf',
785
+ destination: '',
786
+ path: '',
787
+ stream: null,
788
+ };
789
+ return pdfStream;
790
+ }
791
+ catch (error) {
792
+ console.error('Error in DEFAULT_RECEIPT_TEMPLATE_PDF:', error);
793
+ const errorMessage = error instanceof Error ? error.message : 'Unknown error occurred';
794
+ throw new Error(`Receipt generation failed: ${errorMessage}`);
795
+ }
796
+ finally {
797
+ if (browser) {
798
+ try {
799
+ await browser.close();
800
+ }
801
+ catch (closeError) {
802
+ console.error('Error closing browser:', closeError);
803
+ }
804
+ }
805
+ }
806
+ }
807
+ async launchBrowser() {
808
+ const chromeSetup = chrome_setup_1.ChromeSetup.getInstance();
809
+ const MAX_RETRIES = 3;
810
+ const RETRY_DELAY = 2000;
811
+ for (let attempt = 1; attempt <= MAX_RETRIES; attempt++) {
812
+ try {
813
+ const launchOptions = await chromeSetup.getBrowserLaunchOptions();
814
+ if (attempt === 1) {
815
+ console.log('Launching browser with options:', JSON.stringify(launchOptions, null, 2));
816
+ }
817
+ else {
818
+ console.log(`Browser launch attempt ${attempt}/${MAX_RETRIES}`);
819
+ }
820
+ return await puppeteer.launch(launchOptions);
821
+ }
822
+ catch (error) {
823
+ const errorMessage = error instanceof Error ? error.message : String(error);
824
+ console.error(`Browser launch attempt ${attempt} failed:`, errorMessage);
825
+ if (errorMessage.includes('Timed out') &&
826
+ errorMessage.includes('WS endpoint URL')) {
827
+ if (attempt < MAX_RETRIES) {
828
+ console.log(`Retrying browser launch in ${RETRY_DELAY}ms due to timeout...`);
829
+ await new Promise((resolve) => setTimeout(resolve, RETRY_DELAY));
830
+ continue;
831
+ }
832
+ else {
833
+ throw new Error('Browser launch timed out after ' +
834
+ MAX_RETRIES +
835
+ ' attempts. ' +
836
+ 'This is a known Puppeteer issue. The system may be under heavy load or Chrome may be slow to start. ' +
837
+ 'Original error: ' +
838
+ errorMessage);
839
+ }
840
+ }
841
+ if (errorMessage.includes('Could not find Chrome') ||
842
+ errorMessage.includes('Failed to launch')) {
843
+ const isReady = await chromeSetup.isReady();
844
+ if (!isReady) {
845
+ throw new Error('Chrome browser is not available for PDF generation. ' +
846
+ 'Please run "npx puppeteer browsers install chrome" or install system Chrome. ' +
847
+ 'On Linux, also ensure dependencies are installed: ' +
848
+ 'sudo apt-get install -y libxfixes3 libnss3 libatk-bridge2.0-0t64');
849
+ }
850
+ }
851
+ if (attempt < MAX_RETRIES) {
852
+ console.log(`Retrying browser launch in ${RETRY_DELAY}ms...`);
853
+ await new Promise((resolve) => setTimeout(resolve, RETRY_DELAY));
854
+ continue;
855
+ }
856
+ throw new Error(`Browser launch failed after ${MAX_RETRIES} attempts: ${errorMessage}`);
857
+ }
858
+ }
859
+ throw new Error('Browser launch failed: Maximum retries exceeded');
754
860
  }
755
861
  get DocumentFileInHTML() {
756
862
  return (async () => {
@@ -796,37 +902,47 @@ class Document extends account_system_entity_1.AccountSystemEntity {
796
902
  PDFMedia: PDFInvoiceMedia,
797
903
  };
798
904
  }
799
- async generateReceipt(userId, customer, transaction, date) {
800
- const media = new media_1.Medias(this.mediaCommonService);
801
- const user = new login_user_1.LoginUser(userId ?? 'System', null, null);
802
- const htmlPayload = {
803
- ObjectId: this.DocNo,
804
- ObjectType: 'Document',
805
- Type: media_1.MediaType.Document,
806
- FileName: `${this.DocType}-${this.DocNo}`,
807
- FileStream: await Document.DEFAULT_INVOICE_TEMPLATE_HTML(),
808
- FileExtension: 'html',
809
- Title: `${this.DocType}-${this.DocNo}.html`,
810
- Description: `HTML ${this.DocType}`,
811
- IsEncryptedYN: 'N',
812
- };
813
- const HTMLInvoiceMedia = await media.postInternal(await Document.DEFAULT_INVOICE_TEMPLATE_HTML(), htmlPayload, user, transaction);
814
- const pdfPayload = {
815
- ObjectId: this.DocNo,
816
- ObjectType: 'Document',
817
- Type: media_1.MediaType.Document,
818
- FileName: `${this.DocType}-${this.DocNo}`,
819
- FileStream: await this.DEFAULT_RECEIPT_TEMPLATE_PDF(customer, date),
820
- FileExtension: 'pdf',
821
- Title: `${this.DocType}-${this.DocNo}.pdf`,
822
- Description: `PDF ${this.DocType}`,
823
- IsEncryptedYN: 'N',
824
- };
825
- const PDFInvoiceMedia = await media.postInternal(await this.DEFAULT_RECEIPT_TEMPLATE_PDF(customer, date), pdfPayload, user, transaction);
826
- return {
827
- HTMLMedia: HTMLInvoiceMedia,
828
- PDFMedia: PDFInvoiceMedia,
829
- };
905
+ async generateReceipt(userId, customer, transaction, date, templateType, customData) {
906
+ try {
907
+ if (!customer) {
908
+ throw new Error('Customer data is required for receipt generation');
909
+ }
910
+ const media = new media_1.Medias(this.mediaCommonService);
911
+ const user = new login_user_1.LoginUser(userId ?? 'System', null, null);
912
+ const htmlPayload = {
913
+ ObjectId: this.DocNo,
914
+ ObjectType: 'Document',
915
+ Type: media_1.MediaType.Document,
916
+ FileName: `${this.DocType}-${this.DocNo}`,
917
+ FileStream: await Document.DEFAULT_INVOICE_TEMPLATE_HTML(),
918
+ FileExtension: 'html',
919
+ Title: `${this.DocType}-${this.DocNo}.html`,
920
+ Description: `HTML ${this.DocType}`,
921
+ IsEncryptedYN: 'N',
922
+ };
923
+ const HTMLInvoiceMedia = await media.postInternal(await Document.DEFAULT_INVOICE_TEMPLATE_HTML(), htmlPayload, user, transaction);
924
+ const pdfPayload = {
925
+ ObjectId: this.DocNo,
926
+ ObjectType: 'Document',
927
+ Type: media_1.MediaType.Document,
928
+ FileName: `${this.DocType}-${this.DocNo}`,
929
+ FileStream: await this.DEFAULT_RECEIPT_TEMPLATE_PDF(customer, date, templateType, customData),
930
+ FileExtension: 'pdf',
931
+ Title: `${this.DocType}-${this.DocNo}.pdf`,
932
+ Description: `PDF ${this.DocType}`,
933
+ IsEncryptedYN: 'N',
934
+ };
935
+ const PDFInvoiceMedia = await media.postInternal(await this.DEFAULT_RECEIPT_TEMPLATE_PDF(customer, date, templateType, customData), pdfPayload, user, transaction);
936
+ return {
937
+ HTMLMedia: HTMLInvoiceMedia,
938
+ PDFMedia: PDFInvoiceMedia,
939
+ };
940
+ }
941
+ catch (error) {
942
+ console.error('Error in generateReceipt:', error);
943
+ const errorMessage = error instanceof Error ? error.message : 'Unknown error occurred';
944
+ throw new Error(`Receipt generation failed: ${errorMessage}`);
945
+ }
830
946
  }
831
947
  async generateCreditNote(userId, customer) {
832
948
  console.log(userId, customer, '<< generateCreditNote');