import { TrUtils } from "../../utils/tr-utils"; import { SharedPDFService } from "../../shared/shared-pdf.service"; import JsBarcode from "jsbarcode"; import { Add } from "../../shared/math-operations"; export class PosReceiptPrintService { static GetPrint(InvoicePDFData: any, size: string) { // console.log('InvoicePDFData', InvoicePDFData); if (TrUtils.IsNull(InvoicePDFData.Name)) { InvoicePDFData.Name = ''; } let dd: any = { // info: { // title: this.GetFileName(ROPrintData), // }, info: { title: 'CounterSale', }, background: function (currentPage: any, pageSize: any) { // if (size !== 'full') { // pageSize.height = pageSize.height - 435.945; // } return SharedPDFService.GetWatermarkImage(InvoicePDFData.Image, pageSize, InvoicePDFData.Entity.Wmark); }, header: function (currentPage: any, pageCount: any, pageSize: any) { return { text: currentPage.toString() + ' of ' + pageCount, alignment: 'right', marginRight: 25, fontSize: 8, marginTop: 2 }; }, pageMargins: this.GetMarginsBasedOnPaperSize(size), pageSize: 'A4', // pageSize: { // width: 595.28, // height: 420.945 // }, // pageOrientation: 'portrait', content: [ this.GetMainHeaderInfo(InvoicePDFData), // { text: InvoicePDFData.Entity.CName, style: ['headerstyle'], alignment: 'center', fontSize: 20 }, // { text: this.GetAddress(InvoicePDFData.Entity), alignment: 'center', style: ['headerstyle'] }, // { // columns: this.GetHeaderInfo(InvoicePDFData) // }, // this.CustomerAndVehicleDetailsAfterLine(size), // { // columns: [{ // width: '*', // // marginTop: 4, // marginLeft: 5, // stack: [{ text: 'Name : ' + InvoicePDFData.Customer.Name, style: ['headerstyle'] }, // { text: 'Doctor : ' + InvoicePDFData.Name, style: ['headerstyle'] }] // }, { // width: 'auto', // // marginTop: 4, // marginRight: 5, // stack: [{ text: 'Bill No : ' + InvoicePDFData._id, style: ['headerstyle'] }, // { text: 'Date : ' + InvoicePDFData.CrDate, style: ['headerstyle'] }] // }], // columnGap: 10, // marginTop: 5 // }, { style: 'tableExample', table: { widths: ['*', 'auto', '*'], body: [ [{ // marginTop: 4, marginLeft: 5, // stack: [{ text: 'UHID : ' + InvoicePDFData.Customer.Code, style: ['headerstyle'] }, // { text: 'Name : ' + InvoicePDFData.Customer.Name, style: ['headerstyle'] }, // { text: 'Doctor : ' + InvoicePDFData.Name, style: ['headerstyle'] }], table: { lineHeight: 0.5, body: this.CustomerInfo(InvoicePDFData) }, layout: 'noBorders' }, this.GetBarCode(InvoicePDFData.Customer?.Code, InvoicePDFData.Entity.PrCustBar), { // marginTop: 4, marginRight: 5, marginLeft: 70, // alignment: 'right', // stack: [{ text: 'Bill No : ' + InvoicePDFData._id, style: ['headerstyle'] }, // { text: 'Date : ' + InvoicePDFData.CrDate, style: ['headerstyle'] }] table: { lineHeight: 0.5, body: [ [{ text: 'Bill No', style: ['headerstyle'] }, { text: ':', style: ['headerstyle'] }, { text: InvoicePDFData._id, style: ['headerstyle'] }], [{ text: 'Date', style: ['headerstyle'] }, { text: ':', style: ['headerstyle'] }, { text: InvoicePDFData.CrDate, style: ['headerstyle'] }], // [{text:'Doctor', style: ['headerstyle']}, {text:':', style: ['headerstyle']}, {text: InvoicePDFData.Name, style: ['headerstyle']}] ] }, layout: 'noBorders' }] ] }, layout: this.HeaderLayOut() }, this.GetItemsTable(InvoicePDFData.Items, InvoicePDFData.ShowTaxColumn, size), // { text: 'SubTotal : ' + InvoicePDFData.FixedSubTotal, style: ['headerstyle'], alignment: 'right' }, // { text: 'Discount : -' + InvoicePDFData.FixedDisc, style: ['headerstyle'], alignment: 'right' }, // { text: 'Adjust : ' + this.GetAdjustValue(InvoicePDFData.Adj), style: ['headerstyle'], alignment: 'right' }, { columns: [{ text: '', width: '*' }, { text: this.finalReturnTotal(InvoicePDFData), width: '*', bold: true, fontSize: 10, alignment: 'center' }, this.GetTotalDetails(InvoicePDFData, size)] }, // { text: '' , fontSize: 8, alignment: 'left' }, { stack: ['* Goods once sold will not be taken back or exchanged', '* PLEASE GET YOUR MEDICINES CHECKED BY YOUR DOCTOR BEFORE USE *'], fontSize: 7, marginLeft: 5 }, this.GetSignatures(InvoicePDFData.Entity.CName, InvoicePDFData.Type, InvoicePDFData.For), ], styles: { headerstyle: { fontFamily: 'Calibri', fontSize: 9 }, Sign: { fontSize: 8, margin: [0, 30, 0, 5] }, forCompany: { // margin: [0, 10, 30, 0], marginRight: 5, marginTop: 10, fontSize: 9, alignment: 'right' }, tableheader: { bold: true, fontFamily: 'Calibri', margin: [0, 1, 0, 5], alignment: 'center' }, tableheader1: { bold: true, fontFamily: 'Calibri', margin: [0, 2, 0, 2], alignment: 'center' }, tableExample: { fontSize: 9 } } }; return dd; } static CustomerInfo(InvoicePDFData: any) { let CustomerTable: any = [ [{ text: 'Name', style: ['headerstyle'] }, { text: ':', style: ['headerstyle'] }, { text: InvoicePDFData.Customer.Name, style: ['headerstyle'] }], [{ text: 'Doctor', style: ['headerstyle'] }, { text: ':', style: ['headerstyle'] }, { text: InvoicePDFData.Name, style: ['headerstyle'] }] ]; if (InvoicePDFData.Customer.Code) { CustomerTable.unshift([{ text: 'UHID', style: ['headerstyle'] }, { text: ':', style: ['headerstyle'] }, { text: InvoicePDFData.Customer.Code, style: ['headerstyle'] }]); } return CustomerTable; } static GetBarCode(code: string, PrCustBar: boolean) { if (!TrUtils.IsNull(code) && PrCustBar) { return { svg: this.textToSvgBarcode(code).svg } } else { return {}; } } static textToSvgBarcode(text: string): any { const svgElement = document.createElementNS("http://www.w3.org/2000/svg", "svg"); JsBarcode(svgElement, text, { format: "CODE128", width: 1.25, height: 25, margin: 0, marginTop: 8, displayValue: true, fontSize: 7, textMargin: 0, fontOptions: 'bold' }); return { svg: svgElement.outerHTML }; } static HeaderLayOut() { return { hLineWidth: function (i: any, node: any) { return (i === 0 || i === 1 || i === node.table.body.length) ? 1 : 0; }, vLineWidth: function (i: any, node: any) { return (i === 0 || i === node.table.widths.length) ? 1 : 0; }, // hLineStyle: function (i, node) { // return (i === 0 || i === 1) ? { dash: { length: 5, space: 5 } } : null; // }, hLineColor: function (i: any, node: any) { return 'gray'; }, vLineColor: function (i: any, node: any) { return 'gray'; }, }; } static GetMainHeaderInfo(InvoicePDFData: any) { if (!TrUtils.IsNull(InvoicePDFData.Image)) { return { columns: [{ image: InvoicePDFData.Image, width: 60, height: 60, marginBottom: 5 }, { stack: [ { text: InvoicePDFData.Entity.CName, style: ['headerstyle'], alignment: 'left', fontSize: 15 }, { text: this.GetAddress(InvoicePDFData.Entity), alignment: 'left', style: ['headerstyle'] }, { columns: this.GetHeaderInfo(InvoicePDFData, 0) }, { text: InvoicePDFData.HeaderName, alignment: 'center', style: ['Receiptheader1'], marginTop: 5 } ] } ], columnGap: 10 } } else { return { stack: [ { text: InvoicePDFData.Entity.CName, style: ['headerstyle'], alignment: 'center', fontSize: 15 }, { text: this.GetAddress(InvoicePDFData.Entity), alignment: 'center', style: ['headerstyle'] }, { columns: this.GetHeaderInfo(InvoicePDFData, 5) }, { text: InvoicePDFData.HeaderName, alignment: 'center', style: ['Receiptheader1'] } ] } } } static GetHeaderInfo(InvoicePDFData: any, marginleft: number) { let TaxInfo: any = []; if (!TrUtils.IsEmpty(InvoicePDFData.Entity.GSTIN)) { TaxInfo.push({ text: 'GSTIN : ' + InvoicePDFData.Entity.GSTIN, marginTop: 2, marginLeft: marginleft, alignment: 'left', fontSize: 9, width: 'auto' }, ); } if (!TrUtils.IsEmpty(InvoicePDFData.Entity.DLNo)) { TaxInfo.push({ text: 'D.L.NO : ' + InvoicePDFData.Entity.DLNo, marginTop: 2, marginLeft: 5, alignment: 'left', fontSize: 9, width: '*' }, ); } TaxInfo.push(this.GetBillofSupplyName(InvoicePDFData.Settings)); return TaxInfo; } // static GetHeaderInfo(InvoicePDFData: any) { // let TaxInfo: any = []; // if (!TrUtils.IsEmpty(InvoicePDFData.Entity.GSTIN)) { // TaxInfo.push({ text: 'GSTIN : ' + InvoicePDFData.Entity.GSTIN, marginTop: 2, marginLeft: 5, alignment: 'left', fontSize: 9, width: 'auto' }, // ); // } // // if (!TrUtils.IsEmpty(InvoicePDFData.Entity.PAN)) { // // TaxInfo.push({ text: 'PAN : ' + InvoicePDFData.Entity.PAN, marginTop: 2, marginLeft: 5, alignment: 'left', fontSize: 9, width: 'auto' }, // // ); // // } // if (!TrUtils.IsEmpty(InvoicePDFData.Entity.DLNo)) { // TaxInfo.push({ text: 'D.L.NO : ' + InvoicePDFData.Entity.DLNo, marginTop: 2, marginLeft: 5, alignment: 'left', fontSize: 9, width: '*' }, // ); // } // TaxInfo.push(this.GetBillofSupplyName(InvoicePDFData.Settings)); // return TaxInfo; // } static finalReturnTotal(InvoicePDFData: any) { // if (!TrUtils.IsFixedZero(InvoicePDFData.CustRetRoundedTotal)) { // return 'Returns Total : ' + InvoicePDFData.CustRetRoundedTotal // } else { return ''; // } } static GetMarginsBasedOnPaperSize(size: any) { if (size === 'full') { return [25, 15, 25, 15]; } else { return [25, 15, 25, 435.945]; } } static GetTotalDetails(InvoicePDFData: any, size: any) { let TotalData: any = []; if (!InvoicePDFData.Consolidate && !TrUtils.IsFixedZero(InvoicePDFData.STotal)) { // TotalData.push({ text: 'Sub Total : ' + InvoicePDFData.STotal, style: ['headerstyle'], alignment: 'right', marginRight: 5, }) TotalData.push({ name: 'Sub Total', value: InvoicePDFData.STotal }); } else { TotalData.push({ name: 'Sub Total', value: TrUtils.FixPriceValue(Add(InvoicePDFData.CustNonRetRoundedTotal, InvoicePDFData.CustPartsDiscTotal)) }); } if (!TrUtils.IsFixedZero(this.GetDiscountOverall(InvoicePDFData))) { // TotalData.push({ text: 'Discount : -' + this.GetDiscountOverall(InvoicePDFData), style: ['headerstyle'], alignment: 'right', marginRight: 5, }); TotalData.push({ name: 'Discount', value: this.GetDiscountOverall(InvoicePDFData) }); } if (!InvoicePDFData.Consolidate && !TrUtils.IsFixedZero(InvoicePDFData.CustPartITax) && TrUtils.IsFixedZero(InvoicePDFData.CustRetRoundedTotal)) { TotalData.push({ name: 'Tax Amount', value: TrUtils.FixPriceValue(InvoicePDFData.CustPartITax) }); } if (!TrUtils.IsFixedZero(InvoicePDFData.Round) && TrUtils.IsFixedZero(InvoicePDFData.CustRetRoundedTotal)) { TotalData.push({ name: 'Round off', value: TrUtils.FixPriceValue(InvoicePDFData.Round) }); } if (!TrUtils.IsFixedZero(InvoicePDFData.CustRetRoundedTotal)) { // TotalData.push({ text: 'Total : ' + InvoicePDFData.CustRoundedTotal, style: ['headerstyle'], alignment: 'right', marginRight: 5, }); TotalData.push({ name: 'Total', value: InvoicePDFData.CustNonRetRoundedTotal }); TotalData.push({ name: 'Returns Total', value: InvoicePDFData.CustRetRoundedTotal }); } if (!TrUtils.IsFixedZero(InvoicePDFData.CustRoundedTotal)) { // TotalData.push({ text: 'Total : ' + InvoicePDFData.CustRoundedTotal, style: ['headerstyle'], alignment: 'right', marginRight: 5, }); TotalData.push({ name: 'Final Total', value: InvoicePDFData.CustRoundedTotal }); } if (!TrUtils.IsFixedZero(InvoicePDFData.Due)) { // TotalData.push({ text: 'Total : ' + InvoicePDFData.CustRoundedTotal, style: ['headerstyle'], alignment: 'right', marginRight: 5, }); TotalData.push({ name: 'Due', value: InvoicePDFData.Due }); } return { style: ['columnheader', 'TotalsStyles'], lineHeight: 0.7, marginBottom: 5, width: 'auto', table: { body: this.GrandTotalTable(TotalData) }, layout: 'noBorders' // layout: { // hLineWidth: function (i: any, node: any) { // return (i === 0) ? 1 : 0; // }, // vLineWidth: function (i: any, node: any) { // return (i === 0 || i === node.table.body.length) ? 0 : 0; // } // }, }; // return TotalData; } static GrandTotalTable(data: any) { var body: any = []; data.forEach((row: any) => { if (!TrUtils.IsFixedZero(row.value) || row.name === 'Total') { var dataRow: any = []; dataRow.push({ text: (row.name).toString(), noWrap: true, bold: true, style: ['headerstyle'], fontSize: 10 }); dataRow.push({ text: ':', style: ['headerstyle'], bold: true, fontSize: 10 }); if (!TrUtils.IsNull(row.value)) { dataRow.push({ text: (row.value).toString(), noWrap: true, alignment: 'right', bold: true, style: ['headerstyle'], fontSize: 10, marginRight: 5 }); } else { dataRow.push({ text: '', noWrap: true, alignment: 'right', style: ['headerstyle'] }); } body.push(dataRow); } }); return body; } static GetBillofSupplyName(Settings: any) { return { text: Settings.Tax === 'BS' ? ' Bill of Supply' : null, alignment: 'center', marginLeft: 15, marginTop: 2, marginBottom: 3, fontSize: 9, width: 'auto' }; } static GetDiscountOverall(InvoicePDFData: any) { // let OveralDisc: number = TrUtils.SetValueToZeroIfNull(Number(InvoicePDFData.Disc)) + // TrUtils.SetValueToZeroIfNull(Number(InvoicePDFData.CustPartsDiscTotal)); // console.log(InvoicePDFData.Disc,InvoicePDFData.CustPartsDiscTotal); return TrUtils.FixPriceValue(Number(InvoicePDFData.CustPartsDiscTotal)); } static CustomerAndVehicleDetailsAfterLine(size: any) { let length: any = 575; if (size === 'A3') { length = 820; } else if (size === 'Custom') { length = 575; } return { canvas: [ { type: 'line', lineColor: 'gray', x1: 0, y1: 0, x2: 540, y2: 0, lineWidth: 1 } ] }; } static GetSignatures(CName: any, For: any, Type: any) { return { columns: [{ stack: [ this.CompanyName(CName), // { columns: [this.Authorizedsignature(), this.SurveyorSignature(For, Type), this.CustomerSignature()] } ] }], }; } static CompanyName(CName: any) { return { style: 'forCompany', text: ['For ', { text: CName, }], }; } static Authorizedsignature() { return { style: 'Sign', text: ['Authorized Signatory'], }; } static CustomerSignature() { return { style: 'Sign', text: ['Customer Signature'], marginLeft: 90 }; } static GetItemsTable(Items: any, ShowTaxColumn: boolean, size: any) { return { style: 'tableExample', marginTop: 3, marginBottom: 5, table: { widths: this.GetWidths(ShowTaxColumn), headerRows: 1, body: this.BuildTableBodyForLaborAndParts(Items, ShowTaxColumn, size) }, layout: { hLineWidth: function (i: any, node: any) { // || (i === 20 && size !== 'full') return (i === 0 || i === 1 || i === node.table.body.length) ? 1 : 0; }, vLineWidth: function (i: any, node: any) { return (i === 0 || i === node.table.widths.length) ? 1 : 0.5; }, // hLineStyle: function (i, node) { // return (i === 0 || i === 1) ? { dash: { length: 5, space: 5 } } : null; // }, hLineColor: function (i: any, node: any) { return 'gray'; }, vLineColor: function (i: any, node: any) { return 'gray'; }, } }; } static GetWidths(ShowTaxColumn: any) { if (ShowTaxColumn) { return [18, '*', 40, 50, 25, 25, 18, 20, 45, 45, 50]; } else { return [22, '*', 60, 30, 25, 18, 20, 50, 50]; } } static GetAddress(Entity: any) { return Entity.Adrs1 + ', ' + Entity.Adrs2 + ', ' + Entity.City + '-' + Entity.PIN + ', Cell : ' + Entity.Phone; } static BuildTableBodyForLaborAndParts(Items: any, ShowTaxColumn: any, size: any) { let body: any = this.GetHeaderNames(ShowTaxColumn); let j = 1; Items.forEach((item: any, index: any) => { let dataRow: any = []; dataRow.push({ text: index + 1, }); dataRow.push({ text: item.Desc, }); if (ShowTaxColumn) { dataRow.push({ text: item.HSN, alignment: 'center', style: ['headerstyle'], }); } dataRow.push({ text: item.BN, alignment: 'center', style: ['headerstyle'], }); dataRow.push({ text: item.ExDt, alignment: 'center', style: ['headerstyle'], }); dataRow.push({ text: item.Make, alignment: 'center', style: ['headerstyle'], }); dataRow.push({ text: item.Sch, alignment: 'center', style: ['headerstyle'], }); dataRow.push({ text: item.Ret ? -item.Qty : item.Qty, alignment: 'right', style: ['headerstyle'], }); dataRow.push({ text: TrUtils.FixPriceValue(item.UnPr), alignment: 'right', style: ['headerstyle'], }); if (ShowTaxColumn) { dataRow.push({ text: item.TaxName, alignment: 'center', style: ['headerstyle'], }); } dataRow.push({ text: item.Ret ? '-' + TrUtils.FixPriceValue(item.LineTotal) : TrUtils.FixPriceValue(item.LineTotal), alignment: 'right', style: ['headerstyle'], }); body.push(dataRow); if (index + 1 === j * 9) { j++; if (size === 'diff') { // let dataRow1: any = [{ text: '' }, '', '', '', '', '']; // if (ShowTaxColumn) { // dataRow1.push(''); // dataRow1.push(''); // } // body.push(dataRow1); // let dataRow2: any = [{ text: '' }, '', '', '', '', '']; // if (ShowTaxColumn) { // dataRow2.push(''); // dataRow2.push(''); // } // body.push(dataRow2); // let dataRow3: any = [{ text: '' }, '', '', '', '', '']; // if (ShowTaxColumn) { // dataRow3.push(''); // dataRow3.push(''); // } // body.push(dataRow3); // let dataRow4: any = [{ text: '' }, '', '', '', '', '']; // if (ShowTaxColumn) { // dataRow4.push(''); // dataRow4.push(''); // } // body.push(dataRow4); // let dataRow5: any = [{ text: '' }, '', '', '', '', '']; // if (ShowTaxColumn) { // dataRow5.push(''); // dataRow5.push(''); // } // body.push(dataRow5); // body.push(this.GetHeaderNames(ShowTaxColumn)[0]); // } else { let dataRow1: any = [{ text: '', pageBreak: 'after' }, '', '', '', '', '']; if (ShowTaxColumn) { dataRow1.push(''); dataRow1.push(''); } body.push(dataRow1); } } }); return body; } static GetHeaderNames(ShowTaxColumn: any) { let HeadingNames: any; if (ShowTaxColumn) { HeadingNames = [[{ text: 'S.No', style: 'tableheader1', Field: 'SNo', alignment: 'left', fontSize: 8 }, { text: 'Item Name', style: 'tableheader1', Field: 'Desc', alignment: 'left', line: true }, { text: 'HSN', style: 'tableheader1', Field: 'HSN' }, { text: 'Batch No.', style: 'tableheader1', Field: 'Batches' }, { text: 'Ex.Dt', style: 'tableheader1', Field: 'ExDate' }, { text: 'Manf', style: 'tableheader1', Field: 'Manf' }, { text: 'Sch', style: 'tableheader1', Field: 'Sch' }, { text: 'Qty', style: 'tableheader1', Field: 'Qty', alignment: 'right' }, { text: 'Price', style: 'tableheader1', Field: 'UnPr', alignment: 'right' }, { text: 'Tax', style: 'tableheader1', Field: 'TaxAmount' }, { text: 'Line Total', style: 'tableheader1', Field: 'LineTotal', alignment: 'right' }]]; } else { HeadingNames = [[{ text: 'S.No', style: 'tableheader1', Field: 'SNo', alignment: 'left' }, { text: 'Item Name', style: 'tableheader1', Field: 'Desc', alignment: 'left', line: true }, { text: 'Batch No.', style: 'tableheader1', Field: 'Batches', alignment: 'left' }, { text: 'Ex.Dt', style: 'tableheader1', Field: 'ExDate', alignment: 'left' }, { text: 'Manf', style: 'tableheader1', Field: 'Manf', alignment: 'right' }, { text: 'Sch', style: 'tableheader1', Field: 'Sch', alignment: 'right' }, { text: 'Qty', style: 'tableheader1', Field: 'Qty', alignment: 'right' }, { text: 'Price', style: 'tableheader1', Field: 'UnPr', alignment: 'right' }, { text: 'Line Total', style: 'tableheader1', Field: 'LineTotal', alignment: 'right' }]]; } return HeadingNames; } }