/* * Commodity page scripts. */ (function (win, doc, undef) { "use strict"; let co = sbm.mdl.commodities = { env: {} }; //-------------------------------------------------------------- co.refreshConfig = function (env, delay) { env.ajax.refreshConfig = env.ajax.refreshConfig || { url: sbm.url.base + 'Commodity/Ajax/Config', formSel: env.$frm.filter, callbackFn: function (response) { co.doRefreshConfig(env, response); } }; sbm.doAjaxRequest(env.ajax.refreshConfig, delay || 50); } co.doRefreshConfig = function (env, response) { //sbm.debug('doRefreshConfig', env, response); if (!response.success) return; Object.assign(env.data, response.data); } //-------------------------------------------------------------- co.refreshResults = function (env, delay) { if (!co.awaitingRefreshResponse) { env.$pnl.bgr.invisible(); env.$pnl.table.find('tbody tr').remove(); env.$pnl.msgs.hide().empty().append($('
')).fadeIn(200); } env.ajax.refreshResults = env.ajax.refreshResults || { url: sbm.url.base + 'Commodity/Ajax/List', formSel: env.$frm.filter, $msgs: env.$pnl.msgs, callbackFn: function (response) { co.doRefreshResults(env, response); } }; sbm.doAjaxRequest(env.ajax.refreshResults, delay || 50); co.awaitingRefreshResponse = true; } co.doRefreshResults = function (env, response) { //sbm.debug('doRefreshResults', env, response); delete co.awaitingRefreshResponse; env.$pnl.msgs.hide().empty(); let $table = $('table', env.$pnl.table).find('tbody tr').remove().end(); sbm.refreshPaging(response, env.$frm.filter, function () { co.refreshResults(env); }); if (!response.success) return; co.doRefreshConfig(env, response); env.mode = response.mode; // Menu. if (env.$pnl.bgr.length) { env.$pnl.bgr.invisible(); env.$pnl.mnu.empty(); env.$pnl.mnu.append(co.getFilterMenuOption(env, sbm.lbl.refresh, function () { co.refreshResults(env); }, 'sbm-refresh')); if (env.data.cfgList.action.allow.create) env.$pnl.mnu.append(co.getFilterMenuOption(env, sbm.lbl.create, function () { co.createItem(env, env.data.cfgItemNew); }, 'sbm-add')); env.$pnl.bgr.toggleVisibility(!!$('.sbm-dropdown-item', env.$pnl.mnu).length); } // Items. var items = response.commodities || []; if (!items.length) return env.$pnl.msgs.append($('')).fadeIn(500); for (const item of items) $table.append(co.populateItem(env, co.getItemTemplate(env), item)); sbm.configureItemTable(env.data.cfgList, $table); } co.populateItem = function (env, $item, item) { if (!item) return $item; $item.prop('id', co.getItemId(item)); let cfg = env.data.cfgList; if (cfg) sbm.populateItemValues(cfg, $item, item.field.value); let prefix = cfg.column.prefix; $('.' + prefix + 'Status', $item).text(item.statusDesc).removeStatusClasses().addClass('sbm-status-' + item.statusCode.toLowerCase()); if (!env.mode.search) { // Actions. var $bgr = $('.sbm-burger', $item).hide(); let $mnu = $('ul.sbm-dropdown-menu', $bgr).empty(); if (!item.deleted) { let fnUpd = function () { co.updateItem(env, item.id) }; $mnu.append(co.getItemMenuOption(env, sbm.lbl.edit, fnUpd, 'sbm-edit')); $('.CommodityCode,.CommodityDesc', $item).off('click').on('click', fnUpd).addClass('sbm-action-text'); $item.off('dblclick').on('dblclick', fnUpd); } $bgr.toggle(!!$('.sbm-dropdown-item', $mnu).length); } else { $item.off('click').on('click', function () { co.selectItemRow(env, $item, item); }); $item.off('dblclick').on('dblclick', function () { co.selectItem(env); }); } return $item; } co.getItemId = function (item) { return 'Cmd_' + (item || {}).id; } //-------------------------------------------------------------- co.getFilterDropdownTemplate = function (env) { return $('.sbm-dropdown-menu.CommodityList li.sbm-dropdown-item', env.$pnl.tmpl).clone(); } co.getItemTemplate = function (env) { return $('table tr.sbm-row-item.Commodity', env.$pnl.tmpl).clone(); } co.getItemDropdownTemplate = function (env) { return $('.sbm-dropdown-menu.Commodity li.sbm-dropdown-item', env.$pnl.tmpl).clone(); } //-------------------------------------------------------------- co.getFilterMenuOption = function (env, lbl, fn, cls) { return co.getMenuOption(co.getFilterDropdownTemplate(env), lbl, fn, cls); } co.getItemMenuOption = function (env, lbl, fn, cls) { return co.getMenuOption(co.getItemDropdownTemplate(env), lbl, fn, cls); } co.getMenuOption = function ($opt, lbl, fn, cls) { return $opt.find('div.sbm-label').text(lbl).end().on('click', fn).addClass(cls); } //-------------------------------------------------------------- co.createItem = function (env, cfg) { co.clearItem(env, cfg); let $frm = env.$frm.item; $frm.find('.CommodityType').val(env.data.commodityTypeDefault); $frm.find('.CommodityStatus').val(env.data.defaultStatusCode); co.openItemDlg(env, cfg, sbm.dlg.title.createCommodity); } co.clearItem = function (env, cfg) { sbm.clearItemForm(env.$frm.item); sbm.populateSelectCodeDesc('#cbCommodityStatus', env.data.statuses); sbm.populateSelectCodeDesc(env.$ctrl.saleTaxCode, env.data.taxCodes.map(function (tax) { return { code: tax.code, desc: tax.caption }; })); env.$ctrl.saleTaxCode.val(env.data.taxCodeDefault); sbm.populateSelectCodeDesc(env.$ctrl.saleUomCat, env.data.uomCats); co.populateSaleUomType(env, env.data.uomTypeDefault); } co.openItemDlg = function (env, cfg, title) { let $dlg = env.$dlg.item; let $frm = env.$frm.item; env.$ctrl.saleTaxCode.off('change').on('change', function () { co.onSaleTaxCodeSelected(env); }); env.$ctrl.saleUomCat.off('change').on('change', function () { co.onSaleUomCatSelected(env); }) env.$ctrl.saleUomTyp.off('change').on('change', function () { co.onSaleUomSelected(env); }); sbm.configureItemControls(cfg, $frm); var btns = {}; btns[sbm.lbl.ok] = function () { co.storeItem(env); }; btns[sbm.lbl.cancel] = function () { delete env.prmEdit; $(this).dialog('close'); }; $dlg.dialog('option', 'buttons', btns); if (title) $dlg.dialog('option', 'title', title); sbm.openDialog($dlg, 720, 640); } //-------------------------------------------------------------- co.updateItem = function (env, id) { let $frm = env.$frm.itemId; $('.CommodityId', $frm).val(id); $('.CommodityDataGroups', $frm).val('Sales'); env.ajax.updateItem = env.ajax.updateItem || { url: sbm.url.base + 'Commodity/Ajax/GetItem', formSel: $frm, callbackFn: function (response) { co.onUpdateItem(env, response); } }; sbm.doAjaxRequest(env.ajax.updateItem, 50); } co.onUpdateItem = function (env, response) { //sbm.debug('onUpdateItem', env, response); if (!response.success) return; let cfg = response.config || {}; co.clearItem(env, cfg); let item = response.commodity; let $frm = env.$frm.item; sbm.populateFormValues(cfg, $frm, item.field.value); let $ctrls = $frm.find('.sbm-input,.sbm-textarea,.sbm-select'); $ctrls.filter('.CommodityId').val(item.id); $ctrls.filter('.CommodityExists').val(1); sbm.selectOrAdd($ctrls.filter('.CommodityType'), item.typeCode, item.typeDesc); co.populateSaleUomType(env, item.salesUnitType); $ctrls.filter('.CommoditySalePackQty').off('blur').on('blur', function () { sbm.formatNumber($(this), cfg.decimals.packQty); }).trigger('blur'); sbm.selectOrAdd($ctrls.filter('.CommodityStatus'), item.statusCode, item.field.value.Status); co.openItemDlg(env, cfg, sbm.dlg.title.updateCommodity); } //-------------------------------------------------------------- co.onTaxCodeSelected = function (env, $taxCode) { } co.onSaleTaxCodeSelected = function (env) { co.onTaxCodeSelected(env.$ctrl.saleTaxCode); } //-------------------------------------------------------------- co.onUomCatSelected = function (env, $saleUomCat, $saleUomTyp) { let uomCat = $saleUomCat.val(); let uom = $saleUomTyp.val(); let uoms = env.data.uoms.filter(function (uom) { return uom.category == uomCat; }).map(function (uom) { return { code: uom.type, desc: uom.caption }; }); sbm.populateSelectCodeDesc($saleUomTyp, uoms); $saleUomTyp.val(uom); } co.onSaleUomCatSelected = function (env) { co.onUomCatSelected(env, env.$ctrl.saleUomCat, env.$ctrl.saleUomTyp); } co.populateUomType = function (env, $saleUomCat, $saleUomTyp, type) { //sbm.debug('populateUomType'); let uom = env.data.uoms.find(function (uom) { return uom.type == type; }); if (!uom) return; $saleUomCat.val(uom.category).trigger('change'); co.onUomCatSelected(env, $saleUomCat, $saleUomTyp); $saleUomTyp.val(uom.type); } co.populateSaleUomType = function (env, type) { co.populateUomType(env, env.$ctrl.saleUomCat, env.$ctrl.saleUomTyp, type); } co.onUomSelected = function (env, $saleUomCat, $saleUomTyp) { } co.onSaleUomSelected = function (env) { co.onUomSelected(env, env.$ctrl.saleUomCat, env.$ctrl.saleUomTyp); } //-------------------------------------------------------------- co.storeItem = function (env) { env.ajax.storeItem = env.ajax.storeItem || { url: sbm.url.base + 'Commodity/Ajax/StoreItem', formSel: env.$frm.item, callbackFn: function (response) { co.onStoreItem(env, response); } }; sbm.doAjaxRequest(env.ajax.storeItem, 50); } co.onStoreItem = function (env, response) { //sbm.debug('onStoreItem', env, response); sbm.refreshControlValues(response.controlValues); if (!response.success) return; env.$dlg.item.dialog('close'); var item = response.commodity; if (env.prmEdit) { co.onItemEdited(env, item); } else if (env.$pnl.table.length) { env.$pnl.msgs.hide().empty(); var $item = $('#' + co.getItemId(item), env.$pnl.table); if (!$item.length) { $item = co.getItemTemplate(env); $('table tbody', env.$pnl.table).prepend($item).show(); } co.populateItem(env, $item, item); sbm.configureItemTable(env.data.cfgList, env.$pnl.table); } } //-------------------------------------------------------------- /* Params: type, search, fnSuccess. */ sbm.fn.selectCommodity = function (prm) { //sbm.debug('selectCommodity', prm); let env = co.env.select; if (!env.$pnl.items.length) return; env.prmSelect = prm; env.$frm.filter.find('.FilterCommodityType').val(''); env.$frm.filter.find('.FilterSearch').val(prm.search || ''); env.$pnl.table.find('tbody tr').remove(); sbm.openDialog(env.$dlg.select, 1000, 800); setTimeout(function () { co.refreshResults(env); }, 250); } co.selectItemRow = function (env, $item, item) { //sbm.debug('selectItemRow', env, $item, item); env.$pnl.table.find('tr').removeClass('sbm-sel'); $item.addClass('sbm-sel'); env.prmSelect.item = item; } co.selectItem = function (env) { //sbm.debug('selectItem', env, env.prmSelect.item); env.$dlg.select.dialog('close'); env.prmSelect.fnSuccess(env.prmSelect.item); delete env.prmSelect; } /* Params: id, fnSuccess. */ sbm.fn.editCommodity = function (prm) { //sbm.debug('editCommodity', prm); let env = co.env.select; env.prmEdit = prm; //sbm.debug(env, prm); co.updateItem(env, prm.id); } co.onItemEdited = function (env, item) { //sbm.debug('co.onItemEdited', env, item); env.prmEdit.fnSuccess(item); delete env.prmEdit; } //-------------------------------------------------------------- co.initEnv = function ($pnlItems) { let env = { mode: { search: 1 }, data: {}, ajax: {}, $pnl: {}, $frm: {}, $ctrl: {}, $dlg: {} }; let $items = env.$pnl.items = $($pnlItems); if (!$items.length) return env; env.$pnl.hdr = $items.find('.sbm-panel-header'); env.$pnl.bgr = env.$pnl.hdr.find('.sbm-burger').invisible(); env.$pnl.mnu = env.$pnl.bgr.find('ul.sbm-dropdown-menu'); env.$pnl.table = $items.find('.sbm-panel-item-table'); env.$pnl.msgs = env.$pnl.table.find('.sbm-panel-messages'); env.$pnl.tmpl = $items.find('.sbm-panel-item-template'); env.$frm.filter = $items.find('.frmCommodityFilter'); env.$frm.filter.on('input', '.FilterSearch', function () { co.refreshResults(env, 250); }).on('change', '.FilterCommodityType,.FilterStatusCat', function () { co.refreshResults(env, 50); }); env.$frm.itemId = $items.find('#frmCommodityId'); var opt = sbm.dlgOpts(); opt.dialogClass += " sbm-Commodity"; env.$dlg.item = $items.find('#dlgCommodity').dialog(opt); env.$frm.item = env.$dlg.item.find('#frmCommodity').off('input').on('input', '.sbm-input, .sbm-textarea', function () { sbm.clearControlError(this); }).off('change').on('change', '.sbm-select', function () { sbm.clearControlError(this); }); env.$frm.item.off('blur').on('blur', '.sbm-input.sbm-format-integer', function () { sbm.formatInteger($(this)); }).off('blur').on('blur', '.sbm-input.sbm-format-currency', function () { sbm.formatCurrency($(this)); }); env.$ctrl.saleTaxCode = env.$frm.item.find('.sbm-select.CommoditySaleTaxCode'); env.$ctrl.saleUomCat = env.$frm.item.find('.sbm-select.CommoditySaleUnitCat'); env.$ctrl.saleUomTyp = env.$frm.item.find('.sbm-select.CommoditySaleUnitType'); var opt = sbm.dlgOpts(); opt.dialogClass += " sbm-Commodity"; opt.buttons[sbm.lbl.ok] = function () { co.selectItem(env); }; opt.buttons[sbm.lbl.cancel] = function () { delete env.prmSelect; $(this).dialog('close'); }; env.$dlg.select = $('#dlgCommoditySelect').dialog(opt); return env; } //-------------------------------------------------------------- $(function () { co.env.maint = co.initEnv('.sbm-panel.Commodities'); if (co.env.maint.$pnl.items.length) { $('body').on('keydown', function (e) { let env = co.env.maint; if (e.which == 45/*Insert*/ && env.data.cfgList.action.allow.create) co.createItem(env, env.data.cfgItemNew); }); setTimeout(function () { co.refreshResults(co.env.maint); }, 250); } co.env.select = co.initEnv('#dlgCommoditySelect'); if (co.env.select.$pnl.items.length) setTimeout(function () { co.refreshConfig(co.env.select); }, 250); }); //-------------------------------------------------------------- }(window, window.document)); /* The End */