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

155 lines
4.2 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 companyName = invoice.tx_company;
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,
companyName: companyName,
2021-01-26 06:21:05 +00:00
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 + '%'}};
}
2021-07-15 11:41:29 +00:00
showFilterDialog(buy) {
this.activeBuy = buy;
this.itemFilterParams = {};
this.itemFilter = {
include: [
{
relation: 'producer',
scope: {
fields: ['name']
}
},
{
relation: 'ink',
scope: {
fields: ['name']
}
}
]
};
this.$.filterDialog.show();
}
selectItem(id) {
this.activeBuy['itemFk'] = id;
this.$.filterDialog.hide();
}
filter() {
const filter = this.itemFilter;
const params = this.itemFilterParams;
const where = {};
for (let key in params) {
const value = params[key];
if (!value) continue;
switch (key) {
case 'name':
where[key] = {like: `%${value}%`};
break;
case 'producerFk':
case 'typeFk':
case 'size':
2021-11-29 09:11:47 +00:00
case 'inkFk':
2021-07-15 11:41:29 +00:00
where[key] = value;
}
}
filter.where = where;
this.$.itemsModel.applyFilter(filter);
}
2021-01-26 06:21:05 +00:00
}
Controller.$inject = ['$element', '$scope'];
ngModule.vnComponent('vnEntryBuyImport', {
template: require('./index.html'),
controller: Controller,
bindings: {
worker: '<'
}
});