Vue.component('change-status-modal', {
props: {
orderId: {type: Number, required: true},
type: {type: String, default: 'accept'}
},
data() {
return {
window: window,
order: null,
newStatus: 'noChanges',
note: '',
file: null,
uploadedFiles: [],
sendEmail: false,
sendEmailViewedPDF: false,
sendEmailMail: '',
submitLoading: false
};
},
async mounted() {
const response = await axios.get(`${window.TT_CONFIG["BASE_PATH"]}/WarehouseOrder/getById`, {params: {id: this.orderId}});
// if order.status is canceled emit close event and window.notify('error', 'Bestellung wurde storniert')
if (response.data.status === 'cancelled') {
this.$emit('close');
window.notify('error', 'Bestellung wurde storniert');
}
this.order = response.data;
},
computed: {
availableStatuses() {
switch (this.order.status) {
case 'new':
return [
{value: 'noChanges', text: 'Keine Änderungen'},
{value: 'accepted', text: 'Akzeptiert'},
{value: 'cancelled', text: 'Storniert'},
];
case 'accepted':
return [
{value: 'noChanges', text: 'Keine Änderungen'},
{value: 'ordered', text: 'Bestellt'},
{value: 'cancelled', text: 'Storniert'},
];
case 'ordered':
return [
{value: 'noChanges', text: 'Keine Änderungen'},
{value: 'sent', text: 'Versendet'},
{value: 'fullyDelivered', text: 'Geliefert'},
{value: 'cancelled', text: 'Storniert'},
{value: 'cancelled', text: 'Storniert'},
];
case 'sent':
return [
{value: 'noChanges', text: 'Keine Änderungen'},
{value: 'partiallyDelivered', text: 'Teilweise geliefert'},
{value: 'fullyDelivered', text: 'Geliefert'},
{value: 'cancelled', text: 'Storniert'},
];
case 'partiallyDelivered':
return [
{value: 'noChanges', text: 'Keine Änderungen'},
{value: 'fullyDelivered', text: 'Geliefert'},
{value: 'cancelled', text: 'Storniert'},
];
case 'fullyDelivered':
return [
{value: 'noChanges', text: 'Keine Änderungen'},
{value: 'cancelled', text: 'Storniert'},
];
}
}
},
methods: {
async handleFileUpload(event) {
const files = event.target.files;
if (!files.length) return;
for (let i = 0; i < files.length; i++) {
const file = files[i];
const formData = new FormData();
formData.append('file', file);
try {
const response = await axios.post(`${window.TT_CONFIG["BASE_PATH"]}/WarehouseOrder/uploadFile`, formData, {
headers: {
'Content-Type': 'multipart/form-data'
}
});
if (response.data.success) {
this.uploadedFiles.push({
id: response.data.fileId,
name: file.name
});
window.notify('success', `File "${file.name}" uploaded successfully`);
} else {
window.notify('error', `File "${file.name}" upload failed: ${response.data.error || 'Unknown error'}`);
}
} catch (error) {
window.notify('error', `Error uploading file "${file.name}"`);
}
}
// Clear the file input
event.target.value = '';
},
removeFile: index => this.uploadedFiles.splice(index, 1),
async submit() {
this.submitLoading = true;
if (this.newStatus === 'accepted' && this.sendEmail && !this.sendEmailMail) {
window.notify('error', 'Bitte geben Sie eine E-Mail-Adresse ein');
this.submitLoading = false;
return;
} else if (this.newStatus === 'accepted' && this.sendEmail && !this.sendEmailViewedPDF) {
window.notify('error', 'Bitte öffnen Sie das PDF bevor Sie die E-Mail senden');
this.submitLoading = false;
return;
} else if (this.newStatus === 'accepted' && this.sendEmail && this.sendEmailMail) {
if (this.order.sendShippingNote > 0) {
const shippingNoteId = await this.generateShippingNote();
await this.submitEmail(shippingNoteId);
} else await this.submitEmail();
}
const fileIds = this.uploadedFiles.map(file => file.id);
const response = await axios.post(`${window.TT_CONFIG["BASE_PATH"]}/WarehouseOrder/createNewLogAction`, {
orderId: this.order.id,
status: this.newStatus,
note: this.note,
fileIds: JSON.stringify(fileIds),
});
if (response.data.success) {
this.$emit('close');
window.notify('success', response.data.message ?? 'Status erfolgreich geändert');
} else {
window.notify('error',
response.data.errors ? Object.values(response.data.errors).join('
') : response.data.message || 'Ein Fehler ist aufgetreten');
}
this.submitLoading = false;
},
async generateShippingNote() {
const positions = this.order.positions.map(position => ({
article: position.article,
amount: position.amount,
price: position.buyPrice
}));
const response = await axios.post(`${window.TT_CONFIG["BASE_PATH"]}/WarehouseShippingNote/create`,
{
"billingAddressId": this.order.sendShippingNote,
"deliveryAddressName": this.order.delAddrName,
"deliveryAddressLine": this.order.delAddrLine,
"deliveryAddressPLZ": this.order.delAddrPLZ,
"deliveryAddressCity": this.order.delAddrCity,
"deliveryAddressEMail": this.order.delAddrEMail,
"textElements": [],
"hoursEntries": [],
"positions": positions,
"note": "Bestellung #" + this.order.orderNumber,
"status": "new"
});
return response.data.id;
},
async submitEmail(shippingNote = null) {
const response = await axios.get(`${window.TT_CONFIG["BASE_PATH"]}/WarehouseOrder/sendEmail?id=${this.order.id}&email=${this.sendEmailMail}${shippingNote ? '&shippingNote=' + shippingNote : ''}`);
if (response.data.success) {
window.notify('success', response.data.message);
} else {
window.notify('error', response.data.message || 'Ein Fehler ist aufgetreten');
}
}
},
template: `
Positionen
E-Mail verschicken?
Bestelldetails
Positionen
Lieferadresse
') : response.data.message || 'Ein Fehler ist aufgetreten');
}
} else {
const response = await axios.post(`${window.TT_CONFIG["BASE_PATH"]}/WarehouseOrder/update`, this.order);
if (response.data.success) {
this.$emit('close');
window.notify('success', response.data.message ?? 'Bestellung erfolgreich aktualisiert');
} else window.notify('error',
response.data.errors ? Object.values(response.data.errors).join('
') : response.data.message || 'Ein Fehler ist aufgetreten');
}
},
async deleteOrder() {
if (!window.confirm('Bestellung wirklich löschen?')) return;
const response = await axios.post(`${window.TT_CONFIG["BASE_PATH"]}/WarehouseOrder/delete`, {id: this.id});
if (response.data.success) {
this.$emit('close');
window.notify('success', response.data.message || 'Bestellung erfolgreich gelöscht');
} else window.notify('error', response.data.message || 'Ein Fehler ist aufgetreten');
},
async fetchDistributors(article) {
const url = `${window.TT_CONFIG["BASE_PATH"]}/WarehouseOrder/getArticleDistributorData`;
const params = typeof article === 'string' ? {allDistributor: true} : {articleId: article};
if (JSON.stringify(params) === JSON.stringify(this.lastDistributorFetch)) return;
this.lastDistributorFetch = params;
const response = await axios.get(url, {params});
this.positionsConfig.fields.distributorId.options = response.data.map(distributor => ({
value: distributor.id,
text: distributor.name,
externalArticleNumber: distributor.externalArticleNumber || null,
purchasePrice: distributor.purchasePrice || null,
}));
},
async fetchDistributorData(distributorId) {
if (distributorId && typeof this.$refs.positionsManager.formData.article === 'number') {
const distributor = this.positionsConfig.fields.distributorId.options.find(distributor => parseInt(distributor.value) ===
parseInt(distributorId));
this.$refs.positionsManager.updateField('distributorArticleNumber', distributor.externalArticleNumber);
this.$refs.positionsManager.updateField('buyPrice', distributor.purchasePrice);
}
},
},
watch: {
'order.positions': {
handler(newPositions) {
if (this.id !== 'create' && new Set(newPositions.map(p => p.distributorId)).size > 1) {
window.notify('error', 'Eine bestehende Bestellung kann nur Positionen vom gleichen Lieferanten enthalten.');
this.order.positions = newPositions.filter(p => p.distributorId === this.order.distributorId);
}
},
deep: true
}
}
,
});
Vue.component('tt-file', {
props: ['id'],
data: () => ({file: null}),
async mounted() {
const response = await axios.get(`${window.TT_CONFIG.BASE_PATH}/File/getById`, {params: {id: this.id}});
this.file = response.data;
},
template: `