salix/modules/entry/front/buy/import/index.js

100 lines
2.9 KiB
JavaScript
Raw Normal View History

2021-01-26 06:21:05 +00:00
import ngModule from '../../module';
import Section from 'salix/components/section';
import './style.scss';
class Controller extends Section {
constructor($element, $) {
super($element, $);
this.import = {
file: '',
invoice: null,
buys: []
};
}
onFileChange($event) {
const input = $event.target;
const file = input.files[0];
const reader = new FileReader();
reader.onload = event =>
this.fillData(event.target.result);
reader.readAsText(file, 'UTF-8');
}
fillData(raw) {
const data = JSON.parse(raw);
const [invoice] = data.invoices;
this.$.$applyAsync(() => {
2021-02-09 06:59:55 +00:00
this.import.observation = invoice.tx_awb;
2021-01-26 06:21:05 +00:00
const boxes = invoice.boxes;
const buys = [];
for (let box of boxes) {
2021-02-03 08:03:03 +00:00
const boxVolume = box.nu_length * box.nu_width * box.nu_height;
2021-01-26 06:21:05 +00:00
for (let product of box.products) {
const packing = product.nu_stems_bunch * product.nu_bunches;
buys.push({
description: product.nm_product,
size: product.nu_length,
packing: packing,
grouping: product.nu_stems_bunch,
2021-02-03 08:03:03 +00:00
buyingValue: parseFloat(product.mny_rate_stem),
volume: boxVolume
2021-01-26 06:21:05 +00:00
});
}
}
2021-02-09 06:59:55 +00:00
2021-03-09 09:01:40 +00:00
const boxesId = boxes.map(box => box.id_box);
this.import.ref = boxesId.join(', ');
this.fetchBuys(buys);
});
}
fetchBuys(buys) {
const params = {buys};
const query = `Entries/${this.$params.id}/importBuysPreview`;
2021-03-09 09:01:40 +00:00
this.$http.get(query, {params}).then(res => {
this.import.buys = res.data;
2021-01-26 06:21:05 +00:00
});
}
onSubmit() {
2021-03-12 07:17:56 +00:00
try {
const params = this.import;
const hasAnyEmptyRow = params.buys.some(buy => {
return buy.itemFk == null;
});
if (hasAnyEmptyRow)
throw new Error(`Some of the imported buys doesn't have an item`);
const query = `Entries/${this.$params.id}/importBuys`;
2021-03-12 07:17:56 +00:00
return this.$http.post(query, params)
.then(() => this.vnApp.showSuccess(this.$t('Data saved!')))
.then(() => this.$state.go('entry.card.buy.index'));
} catch (e) {
this.vnApp.showError(this.$t(e.message));
return false;
}
2021-01-26 06:21:05 +00:00
}
itemSearchFunc($search) {
return /^\d+$/.test($search)
? {id: $search}
: {name: {like: '%' + $search + '%'}};
}
}
Controller.$inject = ['$element', '$scope'];
ngModule.vnComponent('vnEntryBuyImport', {
template: require('./index.html'),
controller: Controller,
bindings: {
worker: '<'
}
});