import ngModule from '../../module'; import './style.scss'; import Section from 'salix/components/section'; export default class Controller extends Section { saveBuy(buy) { const missingData = !buy.itemFk || !buy.quantity || !buy.packageFk; if (missingData) return; let options; if (buy.id) { options = { query: `Buys/${buy.id}`, method: 'patch' }; } else { options = { query: `Entries/${this.entry.id}/addBuy`, method: 'post' }; } this.$http[options.method](options.query, buy).then(res => { if (!res.data) return; buy = Object.assign(buy, res.data); this.vnApp.showSuccess(this.$t('Data saved!')); }); } /** * Returns checked instances * * @return {Array} Checked instances */ selectedBuys() { if (!this.buys) return; return this.buys.filter(buy => { return buy.checked; }); } deleteBuys() { const buys = this.selectedBuys(); const actualInstances = buys.filter(buy => buy.id); const params = {buys: actualInstances}; if (actualInstances.length) { this.$http.post(`Buys/deleteBuys`, params).then(() => { this.vnApp.showSuccess(this.$t('Data saved!')); }); } buys.forEach(buy => { const index = this.buys.indexOf(buy); this.buys.splice(index, 1); }); } toggleGroupingMode(buy, mode) { const grouping = 1; const packing = 2; const groupingMode = mode === 'grouping' ? grouping : packing; const newGroupingMode = buy.groupingMode === groupingMode ? 0 : groupingMode; const params = { groupingMode: newGroupingMode }; this.$http.patch(`Buys/${buy.id}`, params).then(() => { buy.groupingMode = newGroupingMode; this.vnApp.showSuccess(this.$t('Data saved!')); }); } } ngModule.vnComponent('vnEntryBuyIndex', { template: require('./index.html'), controller: Controller, bindings: { entry: '<' } });