const defaultCrudModalData = { billingAddressId: '', deliveryAddressName: '', deliveryAddressLine: '', deliveryAddressPLZ: '', deliveryAddressCity: '', status: 'new', positions: [], textElements: {} } window.crudModalStatusOptions = [{value: 'new', text: 'Neu'}, {value: 'accepted', text: 'Akzeptiert'}, {value: 'invoiced', text: 'In Rechnung gestellt', disabled: true}] // create a additional vue component for showing positions in the table with lazy loading for article titles and description Vue.component('warehouse-shipping-note-positions', { //language=Vue props: { positions: Array }, data() { return { articleData: {}, loading: false, articlePacketData: {} } }, template: `
`, async mounted() { this.loading = true; for (const position of this.positions) { if (position.article) { const response = await axios.get(window.TT_CONFIG["BASE_PATH"] + '/WarehouseArticle/autoComplete?searchedID=' + position.article); this.$set(this.articleData, position.article, response.data[0]); } else if (position.articlePacket) { const response = await axios.get(window.TT_CONFIG["BASE_PATH"] + '/WarehouseArticlePacket/autoComplete?searchedID=' + position.articlePacket); this.$set(this.articlePacketData, position.articlePacket, response.data[0]); } } this.loading = false; } }) // noinspection JSUnusedLocalSymbols Vue.component('warehouse-shipping-note', { //language=Vue template: `

Positionen

`, data() { return { window: window, historyModal: false, historyModalId: null, crudModal: false, crudModalSelectDeliveryAddressModeItems: [{text: 'Wie Rechnungsadresse', value: 'billing'}, {text: 'Bestehende Lieferadresse', value: 'existing'}, {text: 'Neue Lieferadresse', value: 'new'}], crudModalSelectDeliveryAddressMode: 'billing', crudModalDataDeliveryAddressOptions: [], crudModalDataDeliveryAddressSelected: '', crudModalVerifyMode: false, crudModalId: null, crudModalData: defaultCrudModalData, crudModalAddPositionArticle: '', crudModalAddPositionAmount: '', crudModalAddPositionPrice: '', articleNames: {}, articlePacketNames: {}, textElements: [], } }, async mounted() { const response = await axios.get(window.TT_CONFIG["BASE_PATH"] + '/WarehouseShippingNote/getAllTextElements'); this.textElements = response.data; }, methods: { async openVerifyModal() { const unverifiedShippingNotes = await axios.post(window.TT_CONFIG['BASE_PATH'] + '/WarehouseShippingNote/get', { "pagination": {"page": 1, "per_page": 1}, "filters": { "status": "new" }, "order": {"key": null, "order": "asc"} }); if (unverifiedShippingNotes.data.rows.length === 0) { this.window.notify('warning', 'Keine Lieferscheine zum Freigeben gefunden'); return; } await this.openCrudModal(unverifiedShippingNotes.data.rows[0]); this.crudModalVerifyMode = true; }, resetCrudModalData() { this.crudModalData.billingAddressId = ''; this.crudModalData.deliveryAddressName = ''; this.crudModalData.deliveryAddressLine = ''; this.crudModalData.deliveryAddressPLZ = ''; this.crudModalData.deliveryAddressCity = ''; this.crudModalAddPositionArticle = ''; this.crudModalAddPositionAmount = ''; this.crudModalAddPositionPrice = ''; this.crudModalSelectDeliveryAddressMode = 'billing'; this.crudModalDataDeliveryAddressSelected = ''; this.crudModal = false; }, async openCrudModal(data) { this.resetCrudModalData(); this.crudModalVerifyMode = false; if (data === 'create') { this.crudModalId = 'create' this.crudModalData = defaultCrudModalData this.crudModal = true } else { this.crudModalSelectDeliveryAddressMode = 'new'; const disconnectedData = JSON.parse(JSON.stringify(data)); if (disconnectedData.status !== 'new') { this.window.notify('warning', 'Lieferschein kann nicht bearbeitet werden, da er bereits genehmigt wurde'); return; } disconnectedData.textElements = JSON.parse(disconnectedData.textElements); disconnectedData.positions = JSON.parse(disconnectedData.positions); for (const position of disconnectedData.positions) { if (position.article) await this.fetchArticleNames(position.article); if (position.articlePacket) await this.fetchArticlePacketNames(position.articlePacket); } this.crudModalId = 'update' this.crudModalData = disconnectedData this.crudModal = true } }, async addPosition() { const missingFields = []; // ---------- Check Required Fields ---------- if (!this.crudModalAddPositionArticle) missingFields.push('Artikel'); if (!this.crudModalAddPositionAmount) missingFields.push('Menge'); if (!this.crudModalAddPositionPrice) missingFields.push('Preis-Überschreibung'); if (missingFields.length > 0) { window.notify('error', 'Bitte füllen Sie die folgenden Felder aus: ' + missingFields.join(', ')); return; } // ---------- Check if same article is already in positions ---------- const articleAlreadyInPositions = this.crudModalData.positions.find(position => position.article === this.crudModalAddPositionArticle); if (articleAlreadyInPositions) { window.notify('error', 'Artikel ist bereits in den Positionen enthalten'); return; } await this.fetchArticleNames(this.crudModalAddPositionArticle); this.crudModalData.positions.push({ article: this.crudModalAddPositionArticle, amount: this.crudModalAddPositionAmount, price: parseFloat(this.crudModalAddPositionPrice) }); //TODO: post to server }, async fetchArticleNames(articleId) { const response = await axios.get(window.TT_CONFIG["BASE_PATH"] + '/WarehouseArticle/autoComplete?searchedID=' + articleId); this.$set(this.articleNames, articleId, response.data[0].text); }, async fetchArticlePacketNames(articlePacketId) { const response = await axios.get(window.TT_CONFIG["BASE_PATH"] + '/WarehouseArticlePacket/autoComplete?searchedID=' + articlePacketId); this.$set(this.articlePacketNames, articlePacketId, response.data[0].text); }, async createOrUpdate() { const response = await axios.post(this.crudModalId === 'create' ? window['TT_CONFIG']['CREATE_URL'] : window['TT_CONFIG']['UPDATE_URL'], this.crudModalData); if (response.data.success) { this.$refs.table.$refs.table.refreshTable(); this.resetCrudModalData(); this.window.notify('success', response.data.message || 'Erfolgreich gespeichert'); } else { this.window.notify('error', response.data.errors ? Object.values(response.data.errors).join('
') : response.data.message || 'Ein Fehler ist aufgetreten'); } }, async fetchDeliveryAddresses() { if (!this.crudModalData.billingAddressId || this.crudModalSelectDeliveryAddressMode !== 'existing' && this.crudModalSelectDeliveryAddressMode !== 'billing') return; if (this.crudModalSelectDeliveryAddressMode === 'billing') { const response = await axios.get(window.TT_CONFIG["BASE_PATH"] + '/Address/api?do=getAddress&id=' + this.crudModalData.billingAddressId); if (response.data.status !== 'OK' || !response.data.result.address) { window.notify('error', 'Rechnungsadresse konnte nicht gefunden werden'); return; } this.crudModalData.deliveryAddressName = response.data.result.address.company || response.data.result.address.firstname + ' ' + response.data.result.address.lastname; this.crudModalData.deliveryAddressLine = response.data.result.address.street; this.crudModalData.deliveryAddressPLZ = response.data.result.address.zip; this.crudModalData.deliveryAddressCity = response.data.result.address.city; } if (!this.crudModalData.billingAddressId || this.crudModalSelectDeliveryAddressMode !== 'existing') return; const response = await axios.get(window.TT_CONFIG["BASE_PATH"] + '/WarehouseShippingNote/getDeliveryAddresses?billingAddressId=' + this.crudModalData.billingAddressId); this.crudModalDataDeliveryAddressOptions = response.data.map(address => { address.value = address.id; address.text = `${address.deliveryAddressName} - ${address.deliveryAddressLine}, ${address.deliveryAddressPLZ} ${address.deliveryAddressCity}`; return address; }); } }, watch: { crudModalAddPositionArticle: async function (newValue) { if (!newValue) return; const url = `${window.TT_CONFIG["BASE_PATH"]}/WarehouseShippingNote/getArticleAddressPrice?articleId=${newValue}&addressId=${this.crudModalData.billingAddressId}`; const response = await axios.get(url); this.crudModalAddPositionPrice = response.data.price; }, crudModalData: {handler: 'fetchDeliveryAddresses', deep: true}, crudModalSelectDeliveryAddressMode: {handler: 'fetchDeliveryAddresses', deep: true}, crudModalDataDeliveryAddressSelected: function (newValue) { if (!newValue) return; const selectedAddress = this.crudModalDataDeliveryAddressOptions.find(address => address.id === parseInt(newValue)); if (!selectedAddress) { window.notify('error', 'Lieferadresse konnte nicht gefunden werden'); return; } this.crudModalData.deliveryAddressName = selectedAddress.deliveryAddressName; this.crudModalData.deliveryAddressLine = selectedAddress.deliveryAddressLine; this.crudModalData.deliveryAddressPLZ = selectedAddress.deliveryAddressPLZ; this.crudModalData.deliveryAddressCity = selectedAddress.deliveryAddressCity; } } })