import ngModule from '../module'; import Section from 'salix/components/section'; import './style.scss'; export default class Controller extends Section { constructor($element, $) { super($element, $); this.smartTableOptions = { activeButtons: { search: true }, defaultSearch: true, columns: [ { field: 'warehouseFk', autocomplete: { url: 'Warehouses', showField: 'name', valueField: 'id', } }, { field: 'started', searchable: false }, { field: 'ended', searchable: false } ] }; } add() { if (!this.$.model.data || this.$.model.data.length == 0) { this.$.model.data = []; this.$.model.proxiedData = []; this.$.model.insert({}); return; } const lastIndex = this.$.model.data.length - 1; const lastItem = this.$.model.data[lastIndex]; this.$.model.insert({ itemFk: lastItem.itemFk, name: lastItem.name, subName: lastItem.subName, value5: lastItem.value5, value6: lastItem.value6, value7: lastItem.value7, value8: lastItem.value8, value9: lastItem.value9, value10: lastItem.value10, warehouseFk: lastItem.warehouseFk, rate2: lastItem.rate2, rate3: lastItem.rate3, hasMinPrice: lastItem.hasMinPrice, minPrice: lastItem.minPrice, started: lastItem.started, ended: lastItem.ended, }); } upsertPrice(price, resetMinPrice) { if (resetMinPrice) delete price['minPrice']; price.hasMinPrice = price.minPrice ? true : false; let requiredFields = ['itemFk', 'started', 'ended', 'rate2', 'rate3']; for (let field of requiredFields) if (price[field] == undefined) return; const query = 'FixedPrices/upsertFixedPrice'; this.$http.patch(query, price) .then(res => { this.vnApp.showSuccess(this.$t('Data saved!')); Object.assign(price, res.data); }); } removePrice($index) { const price = this.$.model.data[$index]; if (price.id) { this.$http.delete(`FixedPrices/${price.id}`) .then(() => { this.$.model.remove($index); this.vnApp.showSuccess(this.$t('Data saved!')); }); } else this.$.model.remove($index); } itemSearchFunc($search) { return /^\d+$/.test($search) ? {id: $search} : {name: {like: '%' + $search + '%'}}; } exprBuilder(param, value) { switch (param) { case 'name': return {'i.name': {like: `%${value}%`}}; case 'itemFk': case 'warehouseFk': case 'rate2': case 'rate3': param = `fp.${param}`; return {[param]: value}; case 'minPrice': param = `i.${param}`; return {[param]: value}; } } recalculateRate2(price) { if (!price.id || !price.rate3) return; const query = 'FixedPrices/getRate2'; const params = { fixedPriceId: price.id, rate3: price.rate3 }; this.$http.get(query, {params}) .then(res => { const rate2 = res.data.rate2; if (rate2) { price.rate2 = rate2; this.upsertPrice(price); } }); } } ngModule.vnComponent('vnFixedPrice', { template: require('./index.html'), controller: Controller });