/* Suggested Ingredients dialog (#dlgIngredients in menu_import_dialog.php) * Entry points: * menuImportSuggestForSaved() - items just saved by the Import Menu from Image dialog * menuImportSuggestForCurrentItem() - the item open in the New/Edit POS Item dialog (inputs grid) * Both call ingOpen(posNo, items) where items = [{res_item, name, description}]. * Endpoint: menu_import_ingredients.php (stock / suggest / save). * * Grid rows: {res_item, dish, item (stock item_no or null), item_name, uom, uom_id, kind, * category_id, category_name, sub_category_id, sub_category_name, * quantity, costing_type, note, status} * A row with item = null is a NEW store item (created on save). The Stock item cell * accepts either a pick from the outlet's stock list or a typed new name; the Unit, * Category and Sub-category cells are editable for new items only (existing stock * items keep what inventory already says). A typed sub-category name is created * in inv_sub_category on save. */ var ingGridReady = false; var ingEditIndex; var ingEditOrig = null; // snapshot of the row being edited (to detect ingredient changes) var ingPosNo = ''; var ingStock = []; // [{item_no, item_name, item_uom, category_id, category_name, sub_category_id, sub_category_name}] var ingStockById = {}; var ingUoms = []; // [{id, description}] var ingUomById = {}; var ingDefaultUomId = 0; var ingCategories = []; // [{category_id, category_name}] var ingCategoryById = {}; var ingSubCategories = []; // [{id, sub_category_name, category_id}] var ingSubById = {}; var ingBatchSize = 20; var ingCostingTypes = [{ value: 'none', label: 'none' }, { value: 'Buffet', label: 'Buffet' }, { value: 'Alarcate', label: 'Alarcate' }]; function menuImportSuggestForSaved() { if (!miSavedItems.length) { $.messager.alert('Info', 'Save some items first, then ask for ingredient suggestions.', 'info'); return; } ingOpen(miPosNo(), miSavedItems.slice(), { steps: true }); } function menuImportSuggestForCurrentItem() { var resItem = $('#item_no').val(); var name = $('#item_name').val(); if (!resItem || !name) { $.messager.alert('Info', 'Save the item first, then ask for ingredient suggestions.', 'info'); return; } ingOpen(miPagePosNo(), [{ res_item: resItem, name: name, description: $('#item_desc').val() || '' }]); } var ingShowSteps = false; function ingOpen(posNo, items, opts) { ingPosNo = posNo; ingShowSteps = !!(opts && opts.steps); ingInitGrid(); $('#ingGrid').datagrid('loadData', []); $('#ingStatus').text('Loading the stock list...'); $('#dlgIngredients').dialog('open'); try { $('#dlgIngredients').dialog('resize'); } catch (e) { } // auto-height dialog: fit the grid without a scrollbar if (ingShowSteps) { miRenderSteps('miSteps2', 3, 2); miRenderSteps('miSteps1', 3, 2); } else { $('#miSteps2').hide(); } ingLoadStock(function (ok) { if (!ok) { $('#ingStatus').text('Could not load the stock list.'); return; } if (!ingStock.length) { $('#ingStatus').text('This P.O.S has no stock items yet: every ingredient will be proposed as a new store item and created on save.'); } ingSuggest(items); }); } function ingLoadStock(done) { $.getJSON('menu_import_ingredients.php', { action: 'stock', pos_no: ingPosNo }).done(function (r) { ingStock = (r && r.items) || []; ingStockById = {}; ingStock.forEach(function (s) { ingStockById[String(s.item_no)] = s; }); ingUoms = (r && r.uoms) || []; ingUomById = {}; ingUoms.forEach(function (u) { ingUomById[String(u.id)] = u.description; }); ingDefaultUomId = (r && r.default_uom_id) || (ingUoms[0] ? ingUoms[0].id : 0); ingCategories = (r && r.categories) || []; ingCategoryById = {}; ingCategories.forEach(function (c) { ingCategoryById[String(c.category_id)] = c.category_name; }); ingSubCategories = (r && r.sub_categories) || []; ingSubById = {}; ingSubCategories.forEach(function (s) { ingSubById[String(s.id)] = s; }); $('#ingGrid').datagrid('getColumnOption', 'item').editor.options.data = ingStock; $('#ingGrid').datagrid('getColumnOption', 'uom_id').editor.options.data = ingUoms; $('#ingGrid').datagrid('getColumnOption', 'category_id').editor.options.data = ingCategories; done(true); }).fail(function () { done(false); }); } /* Ask for suggestions in batches so long imports do not hit request timeouts. */ /* Base name of a menu item without its serving, so "Jameson (Bottle)", "Jameson (Tot)" * and "Jameson - Tot" all map to "jameson". */ function ingBaseName(name) { return String(name || '').toLowerCase() .replace(/\([^)]*\)/g, ' ') .replace(/[-–,\/]\s*(bottle|btl|tots?|shots?|glass(es)?|single|half|quarter|full)\b.*$/, ' ') .replace(/\b(per\s+)?(bottle|btl|tots?|shots?|by the glass|glass(es)?)\b/g, ' ') .replace(/\s+/g, ' ').trim(); } /* Batches of up to ingBatchSize dishes that never split the servings of one drink or dish. */ function ingMakeBatches(items) { var groups = {}; var order = []; items.forEach(function (it) { var k = ingBaseName(it.name) || String(it.name || '').toLowerCase(); if (!groups[k]) { groups[k] = []; order.push(k); } groups[k].push(it); }); var batches = []; var cur = []; order.forEach(function (k) { var g = groups[k]; if (cur.length && cur.length + g.length > ingBatchSize) { batches.push(cur); cur = []; } cur = cur.concat(g); }); if (cur.length) { batches.push(cur); } return batches; } function ingSuggest(items) { var batches = ingMakeBatches(items); var known = {}; // new store items proposed by earlier batches, by lower-cased name var all = []; var done = 0; var failed = []; $.messager.progress({ title: 'Suggesting ingredients', msg: 'Reading the stock list and proposing recipes for ' + items.length + ' item(s)...' }); function next() { if (done >= batches.length) { $.messager.progress('close'); $('#ingGrid').datagrid('loadData', all); var newOnes = all.filter(function (r) { return !r.item; }).length; var msg = all.length + ' ingredient row(s) proposed for ' + items.length + ' item(s). Check the quantities, then save.'; if (newOnes) { msg += ' ' + newOnes + ' row(s) are new store items that will be created on save.'; } if (failed.length) { msg += ' ' + failed.length + ' batch(es) failed: ' + failed.join(' | '); } $('#ingStatus').text(msg); if (ingShowSteps && all.length) { miRenderSteps('miSteps2', 4, 3); miRenderSteps('miSteps1', 4, 3); } return; } if (batches.length > 1) { $.messager.progress('bar').progressbar('setValue', Math.round(done / batches.length * 100)); } $.ajax({ url: 'menu_import_ingredients.php?action=suggest', type: 'POST', contentType: 'application/json', dataType: 'json', timeout: 320000, data: JSON.stringify({ pos_no: ingPosNo, items: batches[done], known_new_items: Object.keys(known).map(function (k) { return known[k]; }) }) }).done(function (r) { if (r.success) { all = all.concat(r.rows || []); (r.rows || []).forEach(function (row) { var k = String(row.item_name || '').trim().toLowerCase(); if (!row.item && k && !known[k]) { known[k] = { name: row.item_name, uom: row.uom, category: row.category_name || '', sub_category: row.sub_category_name || '' }; } }); } else { failed.push(r.msg || 'error'); } }).fail(function (xhr) { var m = 'request failed'; try { m = JSON.parse(xhr.responseText).msg || m; } catch (e) { } failed.push(m); }).always(function () { done++; next(); }); } next(); } /* ------------------------------------------------------------ helpers */ /* Identity of an ingredient across dishes: the stock item id, or the new item's name. */ function ingKey(row) { if (row.item) { return 'id:' + row.item; } return 'name:' + String(row.item_name || '').trim().toLowerCase(); } function ingLabel(row) { if (row.item && ingStockById[String(row.item)]) { return ingStockById[String(row.item)].item_name; } return row.item_name || ''; } function ingUnitLabel(row) { if (row.item) { return row.uom || ''; } return ingUomById[String(row.uom_id)] || row.uom || ''; } function ingCategoryLabel(row) { return ingCategoryById[String(row.category_id)] || row.category_name || ''; } function ingSubLabel(row) { if (row.sub_category_id && ingSubById[String(row.sub_category_id)]) { return ingSubById[String(row.sub_category_id)].sub_category_name; } return row.sub_category_name || ''; } function ingSubsForCategory(catId) { return ingSubCategories.filter(function (s) { return String(s.category_id) === String(catId); }); } /* Other grid rows that carry the same ingredient as `key` (excluding `index`). */ function ingSiblings(key, index) { var out = []; $('#ingGrid').datagrid('getRows').forEach(function (r, i) { if (i !== index && ingKey(r) === key) { out.push(i); } }); return out; } function ingReload(rows) { $('#ingGrid').datagrid('loadData', rows.slice()); } function ingIsNew(row) { return !row.item; } /* ---------------------------------------------------------------- grid */ function ingInitGrid() { if (ingGridReady) { return; } ingGridReady = true; $('#ingGrid').datagrid({ width: 1116, height: 400, singleSelect: true, fitColumns: true, rownumbers: true, nowrap: false, columns: [[ { field: 'dish', title: 'Menu / POS item', width: 140, formatter: function (v) { return miEsc(v); } }, { field: 'item', title: 'Stock item (pick or type new)', width: 200, editor: { type: 'combobox', options: { valueField: 'item_no', textField: 'item_name', editable: true, panelHeight: 200, data: [] } }, formatter: function (v, row) { if (!v) { return '' + miEsc(row.item_name) + ' (new)'; } return miEsc(ingLabel(row)); } }, { field: 'quantity', title: 'Qty', width: 65, align: 'right', editor: { type: 'numberbox', options: { precision: 3, min: 0 } }, formatter: function (v) { return (v == null || v === '') ? '' : Number(v); } }, { field: 'uom_id', title: 'Unit', width: 70, editor: { type: 'combobox', options: { valueField: 'id', textField: 'description', editable: false, panelHeight: 200, data: [] } }, formatter: function (v, row) { return miEsc(ingUnitLabel(row)); } }, { field: 'category_id', title: 'Stock category', width: 120, editor: { type: 'combobox', options: { valueField: 'category_id', textField: 'category_name', editable: false, panelHeight: 'auto', data: [] } }, formatter: function (v, row) { return miEsc(ingCategoryLabel(row)); } }, { field: 'sub_category_id', title: 'Sub-category (pick or type new)', width: 150, editor: { type: 'combobox', options: { valueField: 'id', textField: 'sub_category_name', editable: true, panelHeight: 200, data: [] } }, formatter: function (v, row) { var label = ingSubLabel(row); if (!label) { return ''; } return miEsc(label) + (row.sub_category_id ? '' : ' (new)'); } }, { field: 'costing_type', title: 'Type', width: 70, editor: { type: 'combobox', options: { valueField: 'value', textField: 'label', editable: false, panelHeight: 'auto', data: ingCostingTypes } }, formatter: function (v) { return miEsc(v || 'none'); } }, { field: 'note', title: 'Note', width: 90, editor: 'text', formatter: function (v) { return miEsc(v); } }, { field: 'status', title: 'Check', width: 130, formatter: function (v) { return miEsc(v); } }, { field: 'action', title: 'Action', width: 95, align: 'center', formatter: function (v, row, index) { return ingActionHtml(index); } } ]], onClickCell: function (index, field) { if (field === 'action' || field === 'dish') { return; } if (ingEditIndex === index) { return; // already editing this row } if (ingEditIndex !== undefined) { $('#ingGrid').datagrid('selectRow', ingEditIndex); $('#ingStatus').text('Save or cancel the row you are editing first.'); return; } ingBeginEdit(index, field); }, onBeginEdit: function (index, row) { ingEditOrig = $.extend({}, row); var isNew = ingIsNew(row); var get = function (field) { var ed = $('#ingGrid').datagrid('getEditor', { index: index, field: field }); return ed ? $(ed.target) : null; }; var itemEd = get('item'); if (itemEd && isNew && row.item_name) { itemEd.combobox('setText', row.item_name); } var uomEd = get('uom_id'); if (uomEd) { if (isNew) { uomEd.combobox('readonly', false).combobox('setValue', row.uom_id || ingDefaultUomId); } else { uomEd.combobox('setValue', '').combobox('setText', row.uom || '').combobox('readonly', true); } } var catEd = get('category_id'); if (catEd) { if (isNew) { catEd.combobox('readonly', false).combobox('setValue', row.category_id || ''); catEd.combobox({ onSelect: function (rec) { var subEd = get('sub_category_id'); if (subEd) { subEd.combobox('loadData', ingSubsForCategory(rec.category_id)).combobox('setValue', '').combobox('setText', ''); } } }); } else { catEd.combobox('setValue', '').combobox('setText', ingCategoryLabel(row)).combobox('readonly', true); } } var subEd = get('sub_category_id'); if (subEd) { if (isNew) { subEd.combobox('readonly', false).combobox('loadData', ingSubsForCategory(row.category_id)); if (row.sub_category_id) { subEd.combobox('setValue', row.sub_category_id); } else { subEd.combobox('setValue', '').combobox('setText', row.sub_category_name || ''); } } else { subEd.combobox('setValue', '').combobox('setText', ingSubLabel(row)).combobox('readonly', true); } } }, onAfterEdit: function (index, row) { var orig = ingEditOrig || {}; ingEditOrig = null; ingResolveRow(row, orig); var oldKey = ingKey(orig); var newKey = ingKey(row); var ingredientChanged = oldKey !== newKey; var detailChanged = ingIsNew(row) && ( String(row.uom_id) !== String(orig.uom_id) || String(row.category_id) !== String(orig.category_id) || String(row.sub_category_id || '') !== String(orig.sub_category_id || '') || (row.sub_category_name || '') !== (orig.sub_category_name || '')); $('#ingGrid').datagrid('refreshRow', index); if (!ingredientChanged && !detailChanged) { return; } var siblings = ingSiblings(oldKey, index); if (!siblings.length) { return; } var what = ingredientChanged ? 'Replace "' + ingLabel(orig) + '" with "' + ingLabel(row) + '"' : 'Apply the new unit / category / sub-category of "' + ingLabel(row) + '"'; var total = siblings.length + 1; miChoice('Apply to all menu / POS items?', miEsc(what) + '.

"' + miEsc(ingLabel(orig)) + '" is used by ' + total + ' menu / POS items. Apply this change to all of them, or only to "' + miEsc(row.dish) + '"?', [ { text: 'All ' + total + ' items', iconCls: 'icon-ok', handler: function () { applyToSiblings(); } }, { text: 'Only ' + row.dish, handler: null }, { text: 'Cancel', iconCls: 'icon-cancel', handler: function () { ingUndoRow(index, orig); } } ]); function applyToSiblings() { var rows = $('#ingGrid').datagrid('getRows'); siblings.forEach(function (i) { var r = rows[i]; ['item', 'item_name', 'uom', 'uom_id', 'kind', 'category_id', 'category_name', 'sub_category_id', 'sub_category_name'].forEach(function (f) { r[f] = row[f]; }); if (!/^saved/.test(r.status || '')) { r.status = row.status; } }); ingReload(rows); } } }); } /* Normalise a row after editing: picked stock item vs typed new name, its unit and categories. */ function ingResolveRow(row, orig) { var val = row.item; var s = ingStockById[String(val)]; if (s) { row.item = s.item_no; row.item_name = s.item_name; row.uom = s.item_uom; row.uom_id = null; row.category_id = s.category_id; row.category_name = s.category_name; row.sub_category_id = s.sub_category_id; row.sub_category_name = s.sub_category_name; if (/new store item|not in store|pick a stock item/.test(row.status || '')) { row.status = ''; } return; } var typed = String(val == null ? '' : val).trim(); row.item = null; if (typed !== '') { row.item_name = typed; } if (orig && orig.item && !typed) { // the user cleared an existing pick without typing: keep the old item $.extend(row, orig); return; } if (!row.uom_id || !ingUomById[String(row.uom_id)]) { row.uom_id = ingDefaultUomId; } row.uom = ingUomById[String(row.uom_id)] || ''; if (!row.kind) { row.kind = 'other'; } // category: keep a valid one, else the first available if (!row.category_id || !ingCategoryById[String(row.category_id)]) { row.category_id = ingCategories[0] ? ingCategories[0].category_id : null; } row.category_name = ingCategoryById[String(row.category_id)] || ''; // sub-category: an id from the list, or a typed new name var subVal = row.sub_category_id; var sub = ingSubById[String(subVal)]; if (sub && String(sub.category_id) === String(row.category_id)) { row.sub_category_id = sub.id; row.sub_category_name = sub.sub_category_name; } else { var typedSub = String(subVal == null ? '' : subVal).trim(); if (sub) { typedSub = sub.sub_category_name; // picked before the category changed: keep the name, recreate under the new category } var match = ingSubsForCategory(row.category_id).filter(function (x) { return x.sub_category_name.toLowerCase() === typedSub.toLowerCase(); })[0]; if (match) { row.sub_category_id = match.id; row.sub_category_name = match.sub_category_name; } else { row.sub_category_id = null; row.sub_category_name = typedSub !== '' ? typedSub : (orig && !orig.sub_category_id ? (orig.sub_category_name || '') : ''); } } if (String(row.item_name || '').trim() === '') { row.status = 'pick a stock item or type a name'; } else if (!/^saved/.test(row.status || '')) { row.status = 'new store item - will be created on save'; } } /* Links shown in the Action column: Edit / Remove normally, Save / Cancel while the row is being edited. */ function ingActionHtml(index) { if (index === ingEditIndex) { return 'Save  ' + 'Cancel'; } return 'Edit  ' + 'Remove'; } /* Re-render only the Action cell of a row (refreshRow would destroy the editors). */ function ingSwapActionCell(index) { var cell = $('#ingGrid').datagrid('getPanel').find('tr[datagrid-row-index="' + index + '"] td[field="action"] div'); if (cell.length) { cell.html(ingActionHtml(index)); } } function ingBeginEdit(index, field) { if (ingEditIndex !== undefined && ingEditIndex !== index) { $('#ingGrid').datagrid('selectRow', ingEditIndex); $('#ingStatus').text('Save or cancel the row you are editing first.'); return; } if (ingEditIndex === index) { return; } $('#ingGrid').datagrid('selectRow', index).datagrid('beginEdit', index); ingEditIndex = index; ingSwapActionCell(index); var ed = $('#ingGrid').datagrid('getEditor', { index: index, field: field || 'item' }); if (ed) { $(ed.target).focus(); } } /* Save link: commit the row (onAfterEdit then asks about other menu items if the ingredient changed). */ function ingSaveRow(index) { if (ingEditIndex !== index) { return; } if (!$('#ingGrid').datagrid('validateRow', index)) { $('#ingStatus').text('Please correct the highlighted field first.'); return; } ingEditIndex = undefined; $('#ingGrid').datagrid('endEdit', index); } /* Cancel link: drop the edits and restore the row as it was. */ function ingCancelRow(index) { if (ingEditIndex !== index) { return; } ingEditIndex = undefined; ingEditOrig = null; $('#ingGrid').datagrid('cancelEdit', index); $('#ingGrid').datagrid('refreshRow', index); } /* true when no row is mid-edit; otherwise points the user at the Save / Cancel links. */ function ingEndEditing() { if (ingEditIndex === undefined) { return true; } $('#ingGrid').datagrid('selectRow', ingEditIndex); $('#ingStatus').text('Save or cancel the row you are editing first.'); return false; } function ingRemoveRow(index) { if (ingEditIndex !== undefined) { $('#ingGrid').datagrid('cancelEdit', ingEditIndex); ingEditIndex = undefined; ingEditOrig = null; } var rows = $('#ingGrid').datagrid('getRows'); var row = rows[index]; if (!row) { return; } var key = ingKey(row); var siblings = ingSiblings(key, index); var removeIdx = function (indexes) { var drop = {}; indexes.forEach(function (i) { drop[i] = true; }); ingReload(rows.filter(function (r, i) { return !drop[i]; })); }; if (!siblings.length || !String(row.item_name || '').trim() && !row.item) { removeIdx([index]); return; } var total = siblings.length + 1; miChoice('Remove ingredient', '"' + miEsc(ingLabel(row)) + '" is used by ' + total + ' menu / POS items.

Remove it from all of them, or only from "' + miEsc(row.dish) + '"?', [ { text: 'All ' + total + ' items', iconCls: 'icon-remove', handler: function () { removeIdx(siblings.concat([index])); } }, { text: 'Only ' + row.dish, handler: function () { removeIdx([index]); } }, { text: 'Cancel', iconCls: 'icon-cancel', handler: null } ]); } /* Put a row back to its pre-edit state (used when the "apply to all" question is cancelled). */ function ingUndoRow(index, orig) { var rows = $('#ingGrid').datagrid('getRows'); if (rows[index]) { Object.keys(orig).forEach(function (k) { rows[index][k] = orig[k]; }); $('#ingGrid').datagrid('refreshRow', index); } } function ingAddRow() { if (!ingEndEditing()) { return; } var sel = $('#ingGrid').datagrid('getSelected'); var rows = $('#ingGrid').datagrid('getRows'); var base = sel || rows[rows.length - 1]; if (!base) { $.messager.alert('Info', 'There is no menu / POS item in the list to add an ingredient to.', 'info'); return; } var cat = ingCategories[0] ? ingCategories[0].category_id : null; var row = { res_item: base.res_item, dish: base.dish, item: null, item_name: '', uom: ingUomById[String(ingDefaultUomId)] || '', uom_id: ingDefaultUomId, kind: 'other', category_id: cat, category_name: ingCategoryById[String(cat)] || '', sub_category_id: null, sub_category_name: '', quantity: 0, costing_type: 'none', note: '', status: 'pick a stock item or type a name' }; var index = sel ? $('#ingGrid').datagrid('getRowIndex', sel) + 1 : rows.length; $('#ingGrid').datagrid('insertRow', { index: index, row: row }); ingReload($('#ingGrid').datagrid('getRows')); ingBeginEdit(index, 'item'); } /* ---------------------------------------------------------------- save */ function ingSave() { if (!ingEndEditing()) { $.messager.alert('Info', 'Save or cancel the row you are editing first.', 'info'); return; } var rows = $('#ingGrid').datagrid('getRows').filter(function (r) { return !/^saved/.test(r.status || ''); }); var ready = rows.filter(function (r) { return (r.item || String(r.item_name || '').trim()) && Number(r.quantity) > 0; }); if (!ready.length) { $.messager.alert('Info', 'No rows are ready to save. Each row needs a stock item (or a new item name) and a quantity above 0.', 'info'); return; } var notReady = rows.length - ready.length; var toCreate = {}; ready.forEach(function (r) { if (!r.item) { toCreate[ingKey(r)] = true; } }); var createCount = Object.keys(toCreate).length; var go = function () { $.messager.progress({ title: 'Saving', msg: createCount ? 'Creating ' + createCount + ' store item(s) and saving ingredients...' : 'Saving ingredients...' }); $.ajax({ url: 'menu_import_ingredients.php?action=save', type: 'POST', contentType: 'application/json', dataType: 'json', data: JSON.stringify({ pos_no: ingPosNo, rows: ready.map(function (r) { return { res_item: r.res_item, item: r.item, item_name: r.item_name, uom_id: r.uom_id, kind: r.kind, category_id: r.category_id, sub_category_id: r.sub_category_id, sub_category_name: r.sub_category_name, quantity: r.quantity, costing_type: r.costing_type || 'none' }; }) }) }).done(function (r) { $.messager.progress('close'); if (!r.success) { $.messager.alert('Not saved', miEsc(r.msg), 'error'); return; } // merge statuses (and ids of newly created store items) back by position var returned = r.rows || []; var all = $('#ingGrid').datagrid('getRows'); var k = 0; all.forEach(function (row) { if (ready.indexOf(row) === -1) { return; } var o = returned[k++]; if (!o) { return; } row.status = o.status; if (o.item) { row.item = o.item; row.item_name = o.item_name || row.item_name; row.uom = o.uom || row.uom; row.uom_id = null; if (o.category_id) { row.category_id = o.category_id; row.category_name = o.category_name; } if (o.sub_category_id) { row.sub_category_id = o.sub_category_id; row.sub_category_name = o.sub_category_name; } } }); var finish = function () { ingReload(all); $('#ingStatus').text(r.msg); if (ingShowSteps && r.saved > 0) { miRenderSteps('miSteps2', 5, 4); miRenderSteps('miSteps1', 5, 4); } $.messager.show({ title: 'Ingredients', msg: miEsc(r.msg), timeout: 8000 }); try { if ($('#tt').data('datagrid')) { $('#tt').datagrid('reload'); } } catch (e) { } if (typeof window.ingAfterSaveHook === 'function') { try { window.ingAfterSaveHook(r); } catch (e) { } } }; if ((r.created || []).length || (r.created_sub_categories || []).length) { ingLoadStock(function () { finish(); }); } else { finish(); } }).fail(function (xhr) { $.messager.progress('close'); var msg = 'The request failed.'; try { msg = JSON.parse(xhr.responseText).msg || msg; } catch (e) { } $.messager.alert('Not saved', miEsc(msg), 'error'); }); }; if (notReady) { $.messager.confirm('Confirm', notReady + ' row(s) have no item name or no quantity and will be left out. Save the rest?', function (ok) { if (ok) { go(); } }); } else { go(); } }