{"version":3,"file":"document-NqGF2GaO.js","sources":["../../../app/frontend/entrypoints/controllers/erp/order/overview/show/document.ts"],"sourcesContent":["import jQuery from 'jquery/dist/jquery';\nimport * as bootstrap from 'bootstrap'\nimport Utils from '../../../../../utils'\nimport ErpDocumentEntity from \"../../../document/entity\";\nimport * as pdfjsLib from \"pdfjs-dist\"\nimport SignaturePad from \"signature_pad\"\n\nexport default class ErpOrderDocument{\n private parent: any;\n private entity = \"erp/documents\";\n private toastr: any;\n private datatable: any;\n private newItemsDatatable: any;\n\n private pdfDocumentId = \"\";\n private pdfDownloadUrl = \"\";\n private pdfModal: bootstrap.Modal;\n private pdfSignModal: bootstrap.Modal;\n private signaturePad: SignaturePad | undefined;\n\n constructor(parent: any) {\n this.parent = parent\n this.toastr = parent.toastr\n this.pdfModal = new bootstrap.Modal((document.querySelector(\"#erpOrderDocumentPdfModal\") as HTMLElement));\n this.pdfSignModal = new bootstrap.Modal((document.querySelector(\"#erpOrderDocumentPdfSignModal\") as HTMLElement));\n this.setupPdfReader()\n this.bindListeners();\n }\n\n async setupPdfReader() {\n pdfjsLib.GlobalWorkerOptions.workerSrc = '/pdf.worker.min.js';\n }\n\n async setupSigning() {\n const canvas = document.querySelector('#erpOrderDocumentPdfSignCanvas') as HTMLCanvasElement | null;\n if (canvas) {\n if (!this.signaturePad) {\n this.signaturePad = new SignaturePad(canvas);\n } else {\n this.signaturePad.clear();\n }\n }\n }\n\n async saveSignature() {\n if (this.signaturePad) {\n const image = this.signaturePad.toDataURL();\n await Utils.erp.saveSignature(this.pdfDocumentId, this.pdfDownloadUrl, image)\n }\n }\n\n async renderPdfPage(page) {\n console.log(\"Render\", page)\n const canvas = document.createElement(\"canvas\")\n const scale = 1.5;\n const viewport = page.getViewport({scale: scale});\n const context = canvas.getContext('2d');\n if (context) {\n canvas.height = viewport.height;\n canvas.width = viewport.width;\n const renderContext = {\n canvasContext: context,\n viewport: viewport\n };\n const renderTask = page.render(renderContext);\n await renderTask.promise\n }\n canvas.style.maxWidth = \"100%\"\n return canvas\n }\n async loadPdf(downloadUrl: string) {\n pdfjsLib.getDocument(`${document.location.origin}/${downloadUrl}`).promise.then(async (pdf) => {\n const canvasHolder = document.querySelector('#erpOrderDocumentPdfCanvases') as HTMLCanvasElement | null;\n if (canvasHolder) {\n for (let i = 1; i <= pdf.numPages; i++) {\n const c = await this.renderPdfPage((await pdf.getPage(i)))\n canvasHolder.appendChild(c)\n }\n }\n this.pdfModal.show();\n const button = document.querySelector('#erpOrderDocumentPdfModalSignButton') as HTMLButtonElement;\n\n button.style.bottom = `${window.siteConfig.document.y}px`\n button.style.right = `${window.siteConfig.document.x}px`\n button.style.width = window.siteConfig.document.width;\n button.style.height = window.siteConfig.document.height;\n })\n }\n\n getEntityData(elem: any) {\n return {...ErpDocumentEntity.getEntityData(elem), order_id: this.parent.id}\n }\n\n bindListeners() {\n\n jQuery(\".datatables-erp-order-document tbody\").delegate(\".sign-record\", \"click\", async (e: JQuery.TriggeredEvent) => {\n this.pdfDownloadUrl = e.currentTarget.getAttribute(\"data-url\");\n this.pdfDocumentId = e.currentTarget.getAttribute(\"data-document-id\");\n await this.loadPdf(this.pdfDownloadUrl);\n\n });\n (document.querySelector('#erpOrderDocumentPdfModalSignButton') as HTMLButtonElement).addEventListener(\"click\", async (e) => {\n e.preventDefault();\n await this.setupSigning();\n this.pdfSignModal.show();\n });\n\n (document.querySelector('#erpOrderDocumentPdfSignSave') as HTMLButtonElement).addEventListener(\"click\", async (e) => {\n e.preventDefault();\n await this.saveSignature();\n this.pdfSignModal.hide();\n this.pdfModal.hide();\n await this.parent.getEntity();\n });\n (document.querySelector(\"#addErpOrderDocumentForm\") as HTMLFormElement).addEventListener(\"submit\", async (e: any) => {\n e.preventDefault();\n const elem = document.querySelector(\"#addErpOrderDocumentForm\") as HTMLFormElement;\n const valid = elem.checkValidity();\n if (valid) {\n let itemConfig: any = []\n let failed = false //This is not pretty, but there is no way of breaking of DTs every() I'm afraid\n if (this.newItemsDatatable) {\n const itemRows = this.newItemsDatatable.rows({selected: true})\n itemRows.every((rowIndex: any) => {\n const row = this.newItemsDatatable.row(rowIndex).node()\n const data = this.newItemsDatatable.row(rowIndex).data()\n const selectedQuantity = parseInt(row.querySelector(\".erp_document_new_quantity\").value);\n // @ts-ignore\n const selectedSerials = Array.from(row.querySelectorAll(\"select.form-select option:checked\")).map((o: HTMLInputElement) => {\n return o.value\n });\n if (data.serials.length > 0 && selectedQuantity !== selectedSerials.length) {\n this.toastr.error(`Serials are not matching quantity!`, `${Utils.translate('generic.failed')}`)\n failed = true;\n }\n let mapIds: any[] = [];\n console.log(\"D\", data)\n selectedSerials.forEach((serial: string, i: number) => {\n const map = data.data.data.filter((d: { serial: string; }) => d.serial === serial)[0]\n mapIds.push(map.id)\n })\n itemConfig.push({\n map_ids: mapIds,\n })\n })\n } else {\n this.parent.data.items.forEach((item: any) => {\n itemConfig.push({\n mapIds: item.data.map((d: { id: any; }) => d.id)\n })\n })\n }\n const warehouseId = (document.querySelector(\"#erp_document_warehouse_id option:checked\") as HTMLOptionElement).value\n if (!failed) {\n await Utils.showLoader();\n await Utils.entity.upsert({...this.getEntityData(elem), items: itemConfig, warehouse_id: warehouseId}, this.entity)\n this.toastr.success(`${Utils.translate('erp.document.name')} ${Utils.translate('generic.saved')}`, `${Utils.translate('generic.success')}`)\n\n\n const bsElem = bootstrap.Offcanvas.getInstance((document.querySelector(\"#offcanvasAddErpOrderDocument\") as HTMLElement))\n if (bsElem) {\n bsElem.hide();\n }\n await Utils.hideLoader();\n await this.parent.getEntity()\n }\n }\n });\n\n jQuery(\"#erp_document_document_type_id\").on(\"select2:select\", (e: any) => {\n (document.querySelector(\"#erp_document_document_type_name\") as HTMLInputElement).value = e.params.data.data.technicalName;\n })\n }\n\n\n createDataTable() {\n this.datatable = new DataTable(\".datatables-erp-order-document\", {\n processing: true,\n layout: {\n topEnd: ['search', 'buttons']\n },\n language: {\n sLengthMenu: '_MENU_',\n search: '',\n searchPlaceholder: `${Utils.translate('generic.search')}...`,\n \"zeroRecords\": `${Utils.translate('generic.datatable.no_results')}`,\n \"emptyTable\": `${Utils.translate('generic.datatable.no_results')}`,\n \"paginate\": {\n \"first\": `${Utils.translate('generic.datatable.pagination.first')}`,\n \"last\": `${Utils.translate('generic.datatable.pagination.last')}`,\n \"next\": `${Utils.translate('generic.datatable.pagination.next')}`,\n \"previous\": `${Utils.translate('generic.datatable.pagination.previous')}`\n },\n \"info\": `${Utils.translate('generic.datatable.info.info')}`,\n \"infoEmpty\": `${Utils.translate('generic.datatable.info.empty')}`,\n \"infoFiltered\": `${Utils.translate('generic.datatable.info.filtered')}`,\n },\n buttons: [\n {\n text: `${Utils.translate('generic.add')}`,\n className: 'dt-button add-new btn btn-primary m-2',\n attr: {\n 'data-bs-toggle': 'offcanvas',\n 'data-bs-target': '#offcanvasAddErpOrderDocument'\n }\n }\n ]\n });\n }\n\n async generateWarehouseList() {\n let itemConfig: any = []\n let failed = false //This is not pretty, but there is no way of breaking of DTs every() I'm afraid\n if (this.newItemsDatatable) {\n const itemRows = this.newItemsDatatable.rows({selected: true})\n itemRows.every((rowIndex: any) => {\n const row = this.newItemsDatatable.row(rowIndex).node()\n const data = this.newItemsDatatable.row(rowIndex).data()\n const selectedQuantity = parseInt(row.querySelector(\".erp_document_new_quantity\").value);\n // @ts-ignore\n const selectedSerials = Array.from(row.querySelectorAll(\"select.form-select option:checked\")).map((o: HTMLInputElement) => {\n return o.value\n });\n if (data.serials.length > 0 && selectedQuantity !== selectedSerials.length) {\n this.toastr.error(`Serials are not matching quantity!`, `${Utils.translate('generic.failed')}`)\n failed = true;\n }\n let mapIds: any[] = [];\n selectedSerials.forEach((serial: string, i: number) => {\n const map = data.data.data.filter((d: { serial: string; }) => d.serial === serial)[0]\n mapIds.push(map.id)\n })\n itemConfig.push({\n map_ids: mapIds,\n })\n })\n } else {\n this.parent.data.items.forEach((item: any) => {\n itemConfig.push({\n mapIds: item.data.map((d: { id: any; }) => d.id)\n })\n })\n }\n const warehouses = await Utils.erp.generateDocumentWarehouse(itemConfig)\n if (warehouses.status === 200) {\n const warehouseData = warehouses.data\n const select = document.querySelector(\"#erp_document_warehouse_id\") as HTMLSelectElement\n if (warehouseData.length === 0) {\n select.setAttribute(\"disabled\", \"disabled\")\n select.value = \"\"\n } else {\n select.removeAttribute(\"disabled\")\n let html = \"\"\n warehouseData.forEach((d: any, i: number) => {\n html += ``\n })\n select.innerHTML = html;\n }\n }\n }\n async update(data: any) {\n if (this.datatable) {\n this.datatable.destroy();\n }\n\n await Utils.updateElements(data, '', (document.querySelector(\"#erp_order_document\") as HTMLElement))\n const table = document.querySelector(\".datatables-erp-order-document tbody\") as HTMLElement;\n table.innerHTML = \"\";\n data.documents.forEach((document: any) => {\n let tr = `