{"version":3,"file":"new-tIDg4QC7.js","sources":["../../../app/frontend/entrypoints/controllers/erp/order/overview/new.ts"],"sourcesContent":["import * as bootstrap from 'bootstrap'\nimport Utils from '../../../../utils'\nimport DefaultController from \"../../../defaultController\";\nimport ErpOrderEntity from \"../entity\";\nimport ErpOrderGeneral from \"./show/general\";\nimport ErpCustomerEntity from \"../../customer/entity\";\nimport UtilsErp from \"../../../../utils/erp\";\n\nexport default class ErpOrderNew extends DefaultController {\n orderData: any = {}\n private currentProductUuid: any = null\n private currentProductName: any = null\n\n\n async init() {\n this.entity = \"erp/orders\"\n this.childs = {\n general: new ErpOrderGeneral(this, \"new\")\n }\n await Utils.createSelects(this.data, (elem: any) => {\n return this.onSelectGetFilterOptions(elem)\n }, (data: any, entity: any, element: any) => {\n return this.onSelectFilterResults(data, entity, element)\n });\n\n this.bindListeners();\n this.childs.general.update({items: []})\n\n await this.checkParams()\n await Utils.hideLoader();\n }\n\n protected async addLineItem(entityData: any) {\n if (!this.orderData.items) {\n this.orderData.items = [];\n }\n const stockUuids = []\n for (const data of entityData) {\n console.log(data, \"data\")\n let stockItem = this.orderData.items.filter(item => item.product.id === this.currentProductUuid)[0]\n let index = null;\n if (stockItem) {\n index = this.orderData.items.findIndex((obj: any) => obj.product.id === this.currentProductUuid)\n } else {\n stockItem = {\n product: {\n id: this.currentProductUuid,\n name: this.currentProductName\n },\n data: []\n }\n }\n for (const serial of data.serials) {\n const stockData = await Utils.entity.find('erp/stocks', {\n serial, warehouse_location_id: data.warehouse_location_id, warehouse_pallet_id: data.warehouse_pallet_id\n })\n if (stockData.data) {\n const existingStockItem = stockItem.data.filter((d: { id: any; }) => d.id === stockData.data.uuid)[0]\n if (!existingStockItem) {\n stockItem.data.push({\n id: stockData.data.uuid,\n serial: serial,\n payload: stockData.data.payload\n })\n stockUuids.push(stockData.data.uuid)\n } else {\n console.warn(\"Stock exists already!\", serial, data)\n }\n } else {\n console.warn(\"Stock not found!\", serial, data)\n }\n }\n if (index !== null) {\n this.orderData.items[index] = stockItem\n } else {\n this.orderData.items.push(stockItem)\n }\n }\n await Utils.entity.upsert({reserved: true, uuid: stockUuids}, 'erp/stocks')\n // @ts-ignore\n await this.childs.general.update(this.orderData)\n if(this.orderData.items.length > 0) {\n (document.querySelector(\"#erp_order_save\") as HTMLButtonElement).removeAttribute(\"disabled\")\n } else {\n (document.querySelector(\"#erp_order_save\") as HTMLButtonElement).setAttribute(\"disabled\", \"disabled\")\n }\n }\n\n protected async addLinePallet(data: any, orderId = null) {\n if (!this.orderData.items) {\n this.orderData.items = [];\n }\n const stockUuids = []\n const pallet = await Utils.entity.get(data.warehousePalletId, 'erp/warehouse_location_pallets')\n if (pallet && pallet.status === 200) {\n const data = pallet.data[0]\n for (const stock of data.stock) {\n if (stock.available && !stock.reserved) {\n let stockItem = this.orderData.items.filter((item: { product: { id: any; }; }) => item.product.id === stock.product.id)[0]\n let index = null;\n if (stockItem) {\n index = this.orderData.items.findIndex((obj: any) => obj.product.id === stock.product.id)\n } else {\n stockItem = {\n product: {\n id: stock.product.id,\n name: stock.product.name\n },\n data: []\n }\n }\n const existingStockItem = stockItem.data.filter((d: { id: any; }) => d.id === stock.uuid)[0]\n if (!existingStockItem) {\n stockItem.data.push({\n id: stock.uuid,\n serial: stock.serial,\n payload: stock.payload\n })\n stockUuids.push(stock.uuid)\n } else {\n console.warn(\"Stock exists already!\", stock, stock)\n }\n if (index !== null) {\n this.orderData.items[index] = stockItem\n } else {\n this.orderData.items.push(stockItem)\n }\n }\n }\n await Utils.entity.upsert({reserved: true, uuid: stockUuids}, 'erp/stocks')\n }\n // @ts-ignore\n await this.childs.general.update(this.orderData)\n if(this.orderData.items.length > 0) {\n (document.querySelector(\"#erp_order_save\") as HTMLButtonElement).removeAttribute(\"disabled\")\n } else {\n (document.querySelector(\"#erp_order_save\") as HTMLButtonElement).setAttribute(\"disabled\", \"disabled\")\n }\n }\n\n protected async removeLineItem(ids: any) {\n const deletedIds = ids.split(\",\");\n const stockUuids = []\n let deleteIndex = null\n this.orderData.items.forEach((items: any, i: number) => {\n const stockIds = items.data.map((d: { id: any; }) => d.id);\n if (stockIds.sort().join(\",\") === deletedIds.sort().join(\",\")) {\n deleteIndex = i;\n }\n })\n if (deleteIndex !== null) {\n this.orderData.items.splice(deleteIndex, 1);\n for (const id of deletedIds) {\n stockUuids.push(id)\n }\n }\n // @ts-ignore\n await this.childs.general.update(this.orderData)\n await Utils.entity.upsert({reserved: false, uuid: stockUuids}, 'erp/stocks')\n if(this.orderData.items.length > 0) {\n (document.querySelector(\"#erp_order_save\") as HTMLButtonElement).removeAttribute(\"disabled\")\n } else {\n (document.querySelector(\"#erp_order_save\") as HTMLButtonElement).setAttribute(\"disabled\", \"disabled\")\n }\n }\n\n protected getEntityData(elem: any) {\n return ErpOrderEntity.getEntityData(elem)\n }\n\n bindListeners() {\n jQuery(\"#new_erp_order_line_item_product_id\").on(\"select2:ajax_done\", async (e, data: any) => {\n console.log(\"data\", data)\n if (data && data.status === 200 && data.data.length === 1 && data.data[0].is_serial_match === true) {\n const mapData = data.data[0]\n this.currentProductName = mapData.product.name\n this.currentProductUuid = mapData.product.id\n await this.addLineItem([{\n quantity: 1,\n serials: [mapData.serial],\n warehouse_id: null,\n warehouse_location_id: mapData.warehouse_location.id,\n warehouse_pallet_id: mapData.warehouse_pallet ? mapData.warehouse_pallet.id : null\n }])\n const bsElem = bootstrap.Offcanvas.getInstance((document.querySelector(\"#offcanvasAddErpOrderLineItem\") as HTMLElement))\n if (bsElem) {\n bsElem.hide();\n }\n const elem = document.querySelector(\"#addErpOrderLineItemForm\") as HTMLFormElement;\n jQuery(\"#new_erp_order_line_item_product_id\").val(\"\").trigger(\"change\").html(\"\");\n elem.reset();\n }\n });\n\n jQuery(\"#new_erp_order_line_item_product_id\").on(\"select2:select\", async (e, data: any) => {\n this.currentProductName = e.params.data.data.name\n this.currentProductUuid = e.params.data.data.uuid\n await this.addWarehouseInfo(e);\n });\n\n (document.querySelector(\"#erp_order_save\") as HTMLButtonElement).addEventListener(\"click\", async (e: any) => {\n\n await Utils.showLoader()\n const r = await Utils.entity.upsert(this.orderData, 'erp/orders')\n if (r.status === 200) {\n this.toastr.success(`${Utils.translate('erp.order.name')} ${Utils.translate('generic.messages.duplicated')}`, `${Utils.translate('generic.success')}`)\n setTimeout(() => {\n document.location.href = `/${(window as any).currentLocale}/erp/orders/${r.data[0].id}`\n }, 1000)\n }\n });\n\n //@ts-ignore\n jQuery(\"#order_customer_id\").on(\"select2:select\", async (e: any) => {\n const customerData = e.params.data.data\n const addresses = customerData.customerLocations\n jQuery('#order_customer_address_id').empty().trigger('change');\n jQuery('#order_customer_address_id').append(new Option(\"Addresse wählen!\", \"NONE\", false, false));\n addresses.forEach((address: any) => {\n const newOption = new Option(address.name_formatted, address.uuid, false, false);\n jQuery('#order_customer_address_id').append(newOption);\n })\n jQuery('#order_customer_address_id').trigger(\"change\");\n jQuery('#order_customer_address_id').removeAttr(\"disabled\")\n this.orderData.customer_id = customerData.uuid\n })\n\n //@ts-ignore\n jQuery(\"#order_customer_address_id\").on(\"select2:select\", async (e: any) => {\n const generalTab = document.querySelector(\".erp_order_new_row #general\") as HTMLElement;\n this.orderData.customer_location_id = e.target.value\n generalTab.style.opacity = \"1\";\n generalTab.style.pointerEvents = \"all\";\n })\n\n jQuery(\"body\").delegate(\".line_item_warehouse_map\", \"keyup change\", (e) => {\n e.preventDefault();\n const target = e.target;\n const maxQuantity = parseInt(target.getAttribute(\"data-max-quantity\"));\n const currentQuantity = parseInt(target.value);\n const fullQuantity = parseInt((document.querySelector(\"#erp_order_line_item_quantity\") as HTMLInputElement).value);\n if (maxQuantity < currentQuantity) {\n target.value = String(maxQuantity);\n }\n let usedQuantity = 0;\n const inputs = document.querySelectorAll(\".line_item_warehouse_map\");\n inputs.forEach((input: any) => {\n usedQuantity += parseInt(input.value)\n })\n const saveBtn = document.querySelector(\"#erp_line_item_add_save_button\") as HTMLButtonElement\n if (fullQuantity === usedQuantity) {\n saveBtn.removeAttribute(\"disabled\");\n } else {\n saveBtn.setAttribute(\"disabled\", \"disabled\");\n }\n })\n\n\n jQuery(\"body\").delegate(\"#erp_order_line_item_quantity\", \"keyup change\", (e) => {\n e.preventDefault();\n let usedQuantity = 0;\n const fullQuantity = parseInt((document.querySelector(\"#erp_order_line_item_quantity\") as HTMLInputElement).value);\n document.querySelectorAll(\".line_item_warehouse_map\").forEach((input: any) => {\n const maxQuantity = parseInt(input.getAttribute(\"data-max-quantity\"));\n const currentQuantity = parseInt(input.value);\n if (maxQuantity < currentQuantity) {\n usedQuantity += maxQuantity\n } else {\n usedQuantity += currentQuantity\n }\n })\n const saveBtn = document.querySelector(\"#erp_line_item_add_save_button\") as HTMLButtonElement\n if (fullQuantity === usedQuantity) {\n saveBtn.removeAttribute(\"disabled\");\n } else {\n saveBtn.setAttribute(\"disabled\", \"disabled\");\n }\n })\n }\n\n async addWarehouseInfo(data: any) {\n if (data.params.data.id) {\n console.log(this.data, data)\n const stockInfo = await Utils.entity.getAll('erp/stocks', null, {product_id: data.params.data.id, customer_id: this.orderData.customer_id});\n let html = '';\n const tbody = document.querySelector(\"#erp_order_line_item_add_warehouse_list_table tbody\") as HTMLElement;\n tbody.innerHTML = \"\";\n stockInfo.data.forEach((stock: any) => {\n let name = `${stock.warehouse.name}, ${stock.warehouse_location.name}`\n if (stock.warehouse_pallet) {\n name += `, ${stock.warehouse_pallet.name}`\n }\n let options = ``\n stock.serials.filter(n => n).forEach((sn: string) => {\n options += ``\n })\n let availableStock = stock.stock\n if (availableStock > 0) {\n\n html += `