material_checklist.js 72 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459
  1. 'use strict';
  2. /**
  3. * 材料调差 - 调差清单设置
  4. *
  5. * @author EllisRan
  6. * @date 2022/1/7
  7. * @version
  8. */
  9. function getStageId() {
  10. return window.location.pathname.split('/')[5];
  11. }
  12. function findNotJoinLeafXmj(x, type = '') {
  13. if (type === 'index') {
  14. return notJoinList.findIndex(function (item) {
  15. return item.gcl_id === x.gcl_id && item.xmj_id === x.id && (x.mx_id === undefined || (x.mx_id !== undefined && x.mx_id === item.mx_id));
  16. });
  17. }
  18. return notJoinList.find(function (item) {
  19. return item.gcl_id === x.gcl_id && item.xmj_id === x.id && (x.mx_id === undefined || (x.mx_id !== undefined && x.mx_id === item.mx_id));
  20. });
  21. }
  22. function findNotChangeLeafXmj(x, type = '') {
  23. if (type === 'index') {
  24. return notChangeList.findIndex(function (item) {
  25. return item.gcl_id === x.gcl_id && item.xmj_id === x.id && (x.mx_id === undefined || (x.mx_id !== undefined && x.mx_id === item.mx_id));
  26. });
  27. }
  28. return notChangeList.find(function (item) {
  29. return item.gcl_id === x.gcl_id && item.xmj_id === x.id && (x.mx_id === undefined || (x.mx_id !== undefined && x.mx_id === item.mx_id));
  30. });
  31. }
  32. function getPasteHint (str, row = '') {
  33. let returnObj = str;
  34. if (row) {
  35. returnObj.msg = '清单第' + (row+1) + '行' + (str.msg ? str.msg : str);
  36. }
  37. return returnObj;
  38. }
  39. function makeChecklistData(lists, checklists) {
  40. let html = '';
  41. if (lists.length > 0) {
  42. for(const [i,l] of lists.entries()) {
  43. const checklistInfo = _.find(checklists, { b_code: l.b_code, name: l.name, unit: l.unit, unit_price: l.unit_price });
  44. const isChecked = checklistInfo ? ' checked' : '';
  45. const isDisabled = checklistInfo && checklistInfo.had_bills === 1 ? ' disabled' : '';
  46. html += '<tr>\n' +
  47. ' <td><div class="text-center custom-control custom-checkbox mb-2">\n' +
  48. ' <input type="checkbox" id="lists_'+ i +'" value="'+ i +'" name="customCheckbox" class="custom-control-input"'+ isChecked + isDisabled +'>\n' +
  49. ' <label class="custom-control-label" for="lists_'+ i +'"></label>\n' +
  50. ' </div></td>\n' +
  51. ' <td class="text-center">'+ (i+1) +'</td>\n' +
  52. ' <td>'+ l.b_code +'</td>\n' +
  53. ' <td>'+ l.name +'</td>\n' +
  54. ' <td class="text-center">'+ (l.unit ? l.unit : '') +'</td>\n' +
  55. ' <td class="text-right">'+ (l.unit_price ? l.unit_price : '') +'</td>\n' +
  56. ' <td class="text-right">'+ (l.quantity ? l.quantity : '') +'</td>\n' +
  57. ' <td class="text-right">'+ (l.total_price ? l.total_price : '') +'</td>\n' +
  58. ' </tr>';
  59. }
  60. }
  61. $('#lists_data').html(html);
  62. }
  63. // 清单搜索隐藏清单table部分值
  64. function remakeChecklistData(lists, showListData = lists) {
  65. // 先加载台账数据
  66. if (lists.length > 0) {
  67. for (const [index,gcl] of lists.entries()) {
  68. const isShow = _.find(showListData, gcl);
  69. $('#lists_data tr').eq(index).css('display', (isShow ? 'table-row' : 'none'));
  70. }
  71. }
  72. }
  73. $(document).ready(() => {
  74. function TipCellType()
  75. {
  76. }
  77. TipCellType.prototype = new GC.Spread.Sheets.CellTypes.ColumnHeader();
  78. TipCellType.prototype.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  79. return { x: x, y: y, row: context.row, col: context.col, cellRect: cellRect, sheetArea: context.sheetArea, sheet: context.sheet };
  80. };
  81. TipCellType.prototype.processMouseEnter = function (hitInfo){
  82. if (!this._toolTipElement) {
  83. var div = document.createElement("div");
  84. $(div).css("position", "absolute")
  85. .css("border", "1px #C0C0C0 solid")
  86. .css("box-shadow", "1px 2px 5px rgba(0,0,0,0.4)")
  87. .css("font", "9pt Arial")
  88. .css("background", "#fff")
  89. // .css("color", "#fff")
  90. .css("z-index", "1000")
  91. .css("padding", 5);
  92. this._toolTipElement = div;
  93. }
  94. $(this._toolTipElement).text("单位数量:每一单位清单下所需工料消耗量。")
  95. .css("top", hitInfo.y + 15)
  96. .css("left", hitInfo.x - 15);
  97. $(this._toolTipElement).hide();
  98. // document.body.insertBefore(this._toolTipElement, null);
  99. $('#material-spread-div').append(this._toolTipElement, null);
  100. $(this._toolTipElement).show("fast");
  101. };
  102. TipCellType.prototype.processMouseLeave = function (hitInfo) {
  103. if (this._toolTipElement) {
  104. this._toolTipElement.remove();
  105. this._toolTipElement = null;
  106. }
  107. };
  108. autoFlashHeight();
  109. // 清单table
  110. const ledgerSpread = SpreadJsObj.createNewSpread($('#ledger-spread')[0]);
  111. const ledgerSpreadSetting = {
  112. cols: [
  113. {title: '清单编号', colSpan: '1', rowSpan: '2', field: 'b_code', hAlign: 0, width: 80, formatter: '@'},
  114. {title: '名称', colSpan: '1', rowSpan: '2', field: 'name', hAlign: 0, width: 220, formatter: '@'},
  115. {title: '单位', colSpan: '1', rowSpan: '2', field: 'unit', hAlign: 1, width: 60, formatter: '@'},
  116. {title: '单价', colSpan: '1', rowSpan: '2', field: 'unit_price', hAlign: 2, width: 70, type: 'Number'},
  117. {title: '工程量', colSpan: '1', rowSpan: '2', field: 'quantity', hAlign: 2, width: 90, type: 'Number'},
  118. {title: '台账金额', colSpan: '1', rowSpan: '2', field: 'total_price', hAlign: 2, width: 110, type: 'Number'},
  119. ],
  120. emptyRows: 0,
  121. headRows: 1,
  122. headRowHeight: [25, 25],
  123. defaultRowHeight: 21,
  124. headerFont: '12px 微软雅黑',
  125. font: '12px 微软雅黑',
  126. readOnly: true,
  127. };
  128. SpreadJsObj.initSheet(ledgerSpread.getActiveSheet(), ledgerSpreadSetting);
  129. // 加载清单数据 - 暂时统一加载,如有需要,切换成动态加载并缓存
  130. postData(window.location.pathname + '/load', { sid: isStageSelf ? materialStageData[0].sid : null }, function (result) {
  131. ledger = result.ledger;
  132. curLedgerData = result.curLedgerData;
  133. pos = result.pos;
  134. curPosData = result.curPosData;
  135. // materialListData = result.materialListData;
  136. notJoinList = result.materialNotJoinListData;
  137. notChangeList = result.materialNotChangeListData;
  138. materialChecklistData = result.materialChecklistData;
  139. gclList = result.gclList;
  140. if (isStageSelf) {
  141. // updateBillsData(ms_id);
  142. const newGclGatherListData = [];
  143. for (const [index, s] of result.ledgerListData.entries()) {
  144. gclGatherModel.loadLedgerData(_.cloneDeep(ledger), s);
  145. gclGatherModel.loadPosData(_.cloneDeep(pos), result.posListData[index]);
  146. const oneGclGatherData = gclGatherModel.gatherGclData().filter(item => {
  147. return item.qc_qty || item.contract_qty
  148. });
  149. newGclGatherListData.push(oneGclGatherData);
  150. }
  151. gclGatherListData = newGclGatherListData;
  152. }
  153. // 解析清单汇总数据
  154. gclGatherModel.loadLedgerData(ledger, curLedgerData);
  155. gclGatherModel.loadPosData(pos, curPosData);
  156. gclGatherData = gclGatherModel.gatherGclData().filter(function (item) {
  157. return item.unit_price || item.quantity;
  158. });
  159. console.log(gclGatherData);
  160. const hadBillsidList = _.uniq(_.map(gclList, 'gcl_id'));
  161. console.log(hadBillsidList, materialChecklistData);
  162. // 对比清单设置和调差清单,还要和台账对比,显示已选清单列表 不同则更新到清单设置页中
  163. const pushData = [];
  164. const updateData = [];
  165. for (const hb of hadBillsidList) {
  166. const gcl = _.find(gclGatherData, function (item) {
  167. return item.leafXmjs && item.leafXmjs.length > 0 && _.findIndex(item.leafXmjs, { gcl_id : hb }) !== -1;
  168. });
  169. if (gcl) {
  170. const mc = _.find(materialChecklistData, { b_code: gcl.b_code, name: gcl.name, unit: gcl.unit, unit_price: gcl.unit_price });
  171. // const newOrder = _.indexOf(gclGatherData, gcl);
  172. // console.log(newOrder);
  173. if (!mc && _.findIndex(pushData, { b_code: gcl.b_code, name: gcl.name, unit: gcl.unit, unit_price: gcl.unit_price }) === -1) {
  174. pushData.push({ b_code: gcl.b_code, name: gcl.name, unit: gcl.unit, unit_price: gcl.unit_price, quantity: (gcl.quantity ? gcl.quantity : null), total_price: (gcl.total_price ? gcl.total_price : null), had_bills: 1 });
  175. }
  176. }
  177. }
  178. const removeData = [];
  179. for (const mc of materialChecklistData) {
  180. const gcl = _.find(gclGatherData, { b_code: mc.b_code, name: mc.name, unit: mc.unit, unit_price: mc.unit_price });
  181. // 判断是否已不存在工料清单,台账修改过后删除之
  182. if (!gcl) {
  183. removeData.push(mc.id);
  184. } else {
  185. const gcl_ids = gcl.leafXmjs ? _.uniq(_.map(gcl.leafXmjs, 'gcl_id')) : [];
  186. const jiaoji = _.intersection(gcl_ids, hadBillsidList);
  187. // const leafXmjs = gcl.leafXmjs ? gcl.leafXmjs.filter(item => item.gather_qty) : [];
  188. // 更新had_bills值
  189. const updateObj = { id: mc.id };
  190. if (mc.had_bills === 1) {
  191. if (jiaoji.length === 0) {
  192. updateObj.mid = materialID;
  193. updateObj.had_bills = 0;
  194. // updateData.push({ id: mc.id, mid: materialID, had_bills: 0 });
  195. }
  196. } else if (mc.had_bills === 0) {
  197. if (jiaoji.length !== 0) {
  198. updateObj.had_bills = 1;
  199. }
  200. }
  201. // 更新工程量及台账金额
  202. if (mc.quantity !== (gcl.quantity ? gcl.quantity : null)) {
  203. updateObj.quantity = gcl.quantity ? gcl.quantity : null;
  204. updateObj.total_price = gcl.total_price ? gcl.total_price : null;
  205. }
  206. if(!_.isEqual(updateObj,{ id: mc.id })) updateData.push(updateObj);
  207. }
  208. }
  209. console.log(pushData, removeData, updateData);
  210. setChecklistData(pushData, removeData, updateData, true);
  211. });
  212. function setChecklistData(pushData, removeData, updateData = [], sendmsg = false) {
  213. if (pushData.length > 0 || removeData.length > 0 || updateData.length > 0) {
  214. postData(window.location.pathname + '/save', { type: 'resetChecklist', pushData, removeData, updateData }, function (result2) {
  215. if (sendmsg && pushData.length > 0) {
  216. toastr.success('已同步历史调差清单数据至本页中');
  217. }
  218. if (sendmsg && removeData.length > 0) {
  219. toastr.warning('已删除部分与台账清单不匹配的清单数据');
  220. }
  221. materialChecklistData = result2;
  222. showSjsData();
  223. })
  224. } else {
  225. showSjsData();
  226. }
  227. }
  228. function showSjsData() {
  229. SpreadJsObj.loadSheetData(ledgerSpread.getActiveSheet(), SpreadJsObj.DataType.Data, materialChecklistData);
  230. SpreadJsObj.resetTopAndSelect(ledgerSpread.getActiveSheet());
  231. if (materialChecklistData.length > 0) {
  232. const index = _.findIndex(gclGatherData, { b_code: materialChecklistData[0].b_code, name: materialChecklistData[0].name, unit: materialChecklistData[0].unit, unit_price: materialChecklistData[0].unit_price });
  233. loadMaterialData(index, 0);
  234. } else {
  235. loadMaterialData(-1, 0);
  236. }
  237. const sheet = materialSpread.getActiveSheet();
  238. sheet.suspendPaint();
  239. sheet.setCellType(1, 3, new TipCellType(), spreadNS.SheetArea.colHeader);
  240. sheet.resumePaint();
  241. }
  242. // 调差清单工料table
  243. const materialSpread = SpreadJsObj.createNewSpread($('#material-spread')[0]);
  244. const materialSpreadSetting = {
  245. cols: [
  246. {title: '清单工料含量|编号', colSpan: '5|1', rowSpan: '1|1', field: 'code', hAlign: 0, width: 80, formatter: '@', readOnly: true},
  247. {title: '|名称', colSpan: '|1', rowSpan: '|1', field: 'name', hAlign: 0, width: 100, formatter: '@', readOnly: true},
  248. {title: '|单位', colSpan: '|1', rowSpan: '|1', field: 'unit', hAlign: 1, width: 60, formatter: '@', readOnly: true},
  249. {title: '|数量 �', colSpan: '|1', rowSpan: '|1', field: 'quantity', hAlign: 2, width: 80, type: 'Number', readOnly: 'readOnly.isEdit'},
  250. {title: '|计算式', colSpan: '1', rowSpan: '|1', field: 'expr', hAlign: 2, width: 120, formatter: '@', readOnly: 'readOnly.isEdit'},
  251. ],
  252. emptyRows: 0,
  253. headRows: 2,
  254. headRowHeight: [25, 25],
  255. defaultRowHeight: 21,
  256. headerFont: '12px 微软雅黑',
  257. font: '12px 微软雅黑',
  258. };
  259. const materialBase = {
  260. isEdit: function (data) {
  261. // 是否本期添加的工料
  262. return data.order === stage_order;
  263. }
  264. };
  265. const materialCol = {
  266. readOnly: {
  267. isEdit: function (data) {
  268. // return !(!readOnly && materialBase.isEdit(data));
  269. return readOnly;
  270. },
  271. },
  272. };
  273. SpreadJsObj.initSpreadSettingEvents(materialSpreadSetting, materialCol);
  274. // 获取项目节数据
  275. let materialList = [];
  276. function loadMaterialData(iGclRow, iLXmjRow) {
  277. const gcl = gclGatherData[iGclRow];
  278. // const leafXmjs = gcl && gcl.leafXmjs ? gcl.leafXmjs.filter(item => {
  279. // return item.qc_qty || item.contract_qty
  280. // }) : null;
  281. if (gcl && gcl.leafXmjs) {
  282. const gcl_ids = gcl.leafXmjs ? _.uniq(_.map(gcl.leafXmjs, 'gcl_id')) : [];
  283. // const xmj2 = gcl.leafXmjs[iLXmjRow];
  284. materialList = [];
  285. const newMaterialList = _.uniqBy(_.filter(gclList, function (m) {
  286. return _.indexOf(gcl_ids, m.gcl_id) !== -1;
  287. }), 'mb_id');
  288. for(const m of newMaterialList) {
  289. const bills = _.find(materialBillsData, { id: m.mb_id });
  290. if(bills) {
  291. m.code = bills.code;
  292. m.name = bills.name;
  293. m.unit = bills.unit;
  294. }
  295. }
  296. console.log(newMaterialList);
  297. materialList = newMaterialList;
  298. // const leafXmjs = gcl.leafXmjs.filter(item => {
  299. // return item.qc_qty || item.contract_qty
  300. // });
  301. // const xmj = leafXmjs[iLXmjRow];
  302. // materialList = _.filter(materialListData, function (m) {
  303. // return xmj && m.gcl_id === xmj.gcl_id && m.xmj_id === xmj.id && ((xmj.mx_id !==undefined && m.mx_id === xmj.mx_id) || xmj.mx_id === undefined);
  304. // });
  305. // 对清单调差工料table的单位数量进行改变
  306. materialSpreadSetting.cols[materialSpreadSetting.cols.length - 2].title = '|' + '每' + gcl.unit + '数量 �';
  307. SpreadJsObj.initSheet(materialSpread.getActiveSheet(), materialSpreadSetting);
  308. SpreadJsObj.loadSheetData(materialSpread.getActiveSheet(), SpreadJsObj.DataType.Data, materialList);
  309. } else {
  310. materialSpreadSetting.cols[materialSpreadSetting.cols.length - 2].title = '数量 �';
  311. SpreadJsObj.initSheet(materialSpread.getActiveSheet(), materialSpreadSetting);
  312. SpreadJsObj.loadSheetData(materialSpread.getActiveSheet(), SpreadJsObj.DataType.Data, []);
  313. }
  314. SpreadJsObj.resetTopAndSelect(materialSpread.getActiveSheet());
  315. }
  316. // 对添加工料表格赋值
  317. function changeMaterialTable() {
  318. $('#materialBills tr').removeClass('table-secondary');
  319. $('#materialBills').find('input').removeAttr('disabled');
  320. $('#materialBills').find('input').prop('checked', false);
  321. for (const ml of materialList) {
  322. const mbIndex = _.findIndex(materialBillsData, {id : ml.mb_id });
  323. if (mbIndex !== -1) {
  324. $('#materialBills tr').eq(mbIndex).addClass('table-secondary');
  325. $('#materialBills').find('input').eq(mbIndex).attr('disabled', true);
  326. $('#materialBills').find('input').eq(mbIndex).prop('checked', true);
  327. }
  328. }
  329. }
  330. // 选中清单并添加
  331. $('#set_checklist_btn').click(function () {
  332. const select_checklist = $('#lists_data').find('input:checked:not(:disabled)');
  333. const pushData = [];
  334. for (const sc of select_checklist) {
  335. const order = parseInt($(sc).val());
  336. const checklistInfo = _.find(materialChecklistData, { b_code: gclGatherData[order].b_code, name: gclGatherData[order].name, unit: gclGatherData[order].unit, unit_price: gclGatherData[order].unit_price });
  337. if (!checklistInfo) {
  338. pushData.push({
  339. b_code: gclGatherData[order].b_code,
  340. name: gclGatherData[order].name,
  341. unit: gclGatherData[order].unit,
  342. unit_price: gclGatherData[order].unit_price,
  343. quantity: gclGatherData[order].quantity ? gclGatherData[order].quantity : null,
  344. total_price: gclGatherData[order].total_price ? gclGatherData[order].total_price : null,
  345. had_bills: 0,
  346. })
  347. }
  348. }
  349. const notSelect_checklist = $('#lists_data').find('input:not(:checked):not(:disabled)');
  350. const removeData = [];
  351. for (const nsc of notSelect_checklist) {
  352. const order = parseInt($(nsc).val());
  353. // const order = parseInt($(nsc).attr('data-index'));
  354. const checklistInfo = _.find(materialChecklistData, { b_code: gclGatherData[order].b_code, name: gclGatherData[order].name, unit: gclGatherData[order].unit, unit_price: gclGatherData[order].unit_price });
  355. if (checklistInfo) {
  356. removeData.push(checklistInfo.id);
  357. }
  358. }
  359. setChecklistData(pushData, removeData);
  360. $('#addtclist').modal('hide');
  361. });
  362. // 筛选无调差工料清单
  363. $('#notBills_checkList').click(function () {
  364. const isCheck = $(this).is(':checked');
  365. let newMaterialChecklistData = materialChecklistData;
  366. if (isCheck) {
  367. $('#bills0_checkList').prop('checked', false);
  368. newMaterialChecklistData = _.filter(materialChecklistData, { had_bills: 0 });
  369. }
  370. SpreadJsObj.loadSheetData(ledgerSpread.getActiveSheet(), SpreadJsObj.DataType.Data, newMaterialChecklistData);
  371. SpreadJsObj.resetTopAndSelect(ledgerSpread.getActiveSheet());
  372. if (newMaterialChecklistData.length > 0) {
  373. const index = _.findIndex(gclGatherData, { b_code: newMaterialChecklistData[0].b_code, name: newMaterialChecklistData[0].name, unit: newMaterialChecklistData[0].unit, unit_price: newMaterialChecklistData[0].unit_price });
  374. loadMaterialData(index, 0);
  375. } else {
  376. loadMaterialData(-1, 0);
  377. }
  378. });
  379. // 筛选调差工料清单为0
  380. $('#bills0_checkList').click(function () {
  381. const isCheck = $(this).is(':checked');
  382. let newMaterialChecklistData = materialChecklistData;
  383. if (isCheck) {
  384. newMaterialChecklistData = [];
  385. $('#notBills_checkList').prop('checked', false);
  386. const materialList0 = _.uniq(_.map(_.filter(gclList, { quantity: 0 }), 'gcl_id'));
  387. if (materialList0.length > 0) {
  388. const hadMaterialChecklistData = _.filter(materialChecklistData, { had_bills: 1 });
  389. for (const h of hadMaterialChecklistData) {
  390. const gcl = _.find(gclGatherData, { b_code: h.b_code, name: h.name, unit: h.unit, unit_price: h.unit_price });
  391. if (gcl && gcl.leafXmjs.length > 0 && _.indexOf(materialList0, gcl.leafXmjs[0].gcl_id) !== -1) {
  392. newMaterialChecklistData.push(h);
  393. }
  394. }
  395. }
  396. }
  397. SpreadJsObj.loadSheetData(ledgerSpread.getActiveSheet(), SpreadJsObj.DataType.Data, newMaterialChecklistData);
  398. SpreadJsObj.resetTopAndSelect(ledgerSpread.getActiveSheet());
  399. if (newMaterialChecklistData.length > 0) {
  400. const index = _.findIndex(gclGatherData, { b_code: newMaterialChecklistData[0].b_code, name: newMaterialChecklistData[0].name, unit: newMaterialChecklistData[0].unit, unit_price: newMaterialChecklistData[0].unit_price });
  401. loadMaterialData(index, 0);
  402. } else {
  403. loadMaterialData(-1, 0);
  404. }
  405. });
  406. // 添加调差工料
  407. $('#add_material_bill').click(function () {
  408. // 获取已选工料
  409. $('#materialBills').find('input:disabled').prop('checked', false);
  410. const selectList = $('#materialBills').find('input:checked');
  411. if (selectList.length === 0) {
  412. toastr.warning('请选择调差工料');
  413. $('#materialBills').find('input:disabled').prop('checked', true);
  414. return false;
  415. }
  416. const mb_id = [];
  417. for (let s = 0; s < selectList.length; s++) {
  418. mb_id.push($('#materialBills').find('input:checked').eq(s).val());
  419. }
  420. // 获取当前项目节或部位明细id
  421. const sheet = ledgerSpread.getActiveSheet();
  422. const select = SpreadJsObj.getSelectObject(sheet);
  423. const gclIndex = _.findIndex(gclGatherData, { b_code: select.b_code, name: select.name, unit: select.unit, unit_price: select.unit_price });
  424. const gcl = gclGatherData[gclIndex].leafXmjs;
  425. const ms_id = isStageSelf ? materialStageData[0].id : null;
  426. const index = materialChecklistData.indexOf(select);
  427. const datas = [];
  428. for (const xmj of gcl) {
  429. const notx = findNotJoinLeafXmj(xmj);
  430. const notx2 = findNotChangeLeafXmj(xmj);
  431. const data = {
  432. xmj_id: xmj.id,
  433. gcl_id: xmj.gcl_id,
  434. mx_id: xmj.mx_id !== undefined ? xmj.mx_id : '',
  435. gather_qty: notx2 === undefined ? xmj.gather_qty : xmj.contract_qty,
  436. is_join: notx === undefined ? 1 : 0,
  437. };
  438. if (ms_id) data.ms_id = ms_id;
  439. datas.push(data);
  440. }
  441. if (isStageSelf) {
  442. // 取所有的gclGatherData才行,然后获取下的值
  443. const gclData = gclGatherData[gclIndex];
  444. for (const [index, ms] of materialStageData.entries()) {
  445. if (ms.id !== ms_id) {
  446. const gclOther = _.find(gclGatherListData[index], { b_code: gclData.b_code, name: gclData.name, unit: gclData.unit, unit_price: gclData.unit_price });
  447. if (gclOther) {
  448. const leafXmjs = gclOther.leafXmjs.filter(item => item.gather_qty !== null && item.gather_qty !== undefined);
  449. for (const xmj of leafXmjs) {
  450. const notx = findNotJoinLeafXmj(xmj);
  451. const notx2 = findNotChangeLeafXmj(xmj);
  452. const data = {
  453. xmj_id: xmj.id,
  454. gcl_id: xmj.gcl_id,
  455. mx_id: xmj.mx_id ? xmj.mx_id : '',
  456. gather_qty: notx2 === undefined ? xmj.gather_qty : xmj.contract_qty,
  457. is_join: notx === undefined ? 1 : 0,
  458. ms_id: ms.id,
  459. };
  460. datas.push(data);
  461. }
  462. }
  463. }
  464. }
  465. }
  466. // 上传到数据库
  467. console.log(datas, gcl);
  468. postData(window.location.pathname + '/save', {type: 'adds', checklist: { id: select.id, had_bills: 1 }, postData: {xmjs: datas, mbIds: mb_id}}, function (result) {
  469. // materialListData = result;
  470. gclList = result;
  471. materialChecklistData[index].had_bills = 1;
  472. loadMaterialData(gclIndex, 0);
  473. // SpreadJsObj.reLoadRowData(ledgerSpread.getActiveSheet(), index);
  474. $('#addgl').modal('hide');
  475. });
  476. $('#materialBills').find('input:disabled').prop('checked', true);
  477. });
  478. if (!readOnly) {
  479. // material-spread右键功能
  480. const materialSpreadObj = {
  481. del: function () {
  482. const materialSheet = materialSpread.getActiveSheet();
  483. const materialSelect = SpreadJsObj.getSelectObject(materialSheet);
  484. const sheet = ledgerSpread.getActiveSheet();
  485. const select = SpreadJsObj.getSelectObject(sheet);
  486. const index = materialChecklistData.indexOf(select);
  487. const gclIndex = _.findIndex(gclGatherData, { b_code: select.b_code, name: select.name, unit: select.unit, unit_price: select.unit_price });
  488. const gcl = gclGatherData[gclIndex].leafXmjs;
  489. const datas = [];
  490. const ms_id = isStageSelf ? materialStageData[0].id : null;
  491. for (const xmj of gcl) {
  492. const data = {
  493. xmj_id: xmj.id,
  494. gcl_id: xmj.gcl_id,
  495. mx_id: xmj.mx_id !== undefined ? xmj.mx_id : '',
  496. };
  497. if (ms_id) data.ms_id = ms_id;
  498. datas.push(data);
  499. }
  500. if (isStageSelf) {
  501. // 取所有的gclGatherData才行,然后获取下的值
  502. const gclData = gclGatherData[gclIndex];
  503. for (const [index, ms] of materialStageData.entries()) {
  504. if (ms.id !== ms_id) {
  505. const gclOther = _.find(gclGatherListData[index], { b_code: gclData.b_code, name: gclData.name, unit: gclData.unit, unit_price: gclData.unit_price });
  506. if (gclOther) {
  507. const leafXmjs = gclOther.leafXmjs.filter(item => item.gather_qty !== null && item.gather_qty !== undefined);
  508. for (const xmj of leafXmjs) {
  509. const data = {
  510. xmj_id: xmj.id,
  511. gcl_id: xmj.gcl_id,
  512. mx_id: xmj.mx_id ? xmj.mx_id : '',
  513. };
  514. if (_.indexOf(datas, data) === -1) {
  515. datas.push(data);
  516. }
  517. }
  518. }
  519. }
  520. }
  521. }
  522. const xmj = gcl[0];
  523. const materialCount = _.size(_.filter(gclList, function (m) {
  524. return m.gcl_id === xmj.gcl_id;
  525. }));
  526. // const materialCount = _.size(_.filter(materialListData, function (m) {
  527. // return m.gcl_id === xmj.gcl_id && m.xmj_id === xmj.id && ((xmj.mx_id !==undefined && m.mx_id === xmj.mx_id) || xmj.mx_id === undefined);
  528. // }));
  529. let checklist = false;
  530. if (materialCount === 1) {
  531. checklist = {
  532. id: select.id,
  533. had_bills: 0,
  534. }
  535. }
  536. console.log(datas, materialSelect.mb_id, checklist);
  537. postData(window.location.pathname + '/save', {type: 'dels', checklist, postData: { xmjs: datas, mb_id: materialSelect.mb_id }, ms_id: isStageSelf ? materialStageData[0].id : null }, function (result) {
  538. // materialListData = result;
  539. gclList = result;
  540. if (checklist) materialChecklistData[index].had_bills = checklist.had_bills;
  541. loadMaterialData(gclIndex, 0);
  542. // SpreadJsObj.reLoadRowData(ledgerSpread.getActiveSheet(), index);
  543. });
  544. },
  545. deletePress: function (sheet) {
  546. return;
  547. },
  548. editStarting: function (e, info) {
  549. const col = info.sheet.zh_setting.cols[info.col];
  550. const select = SpreadJsObj.getSelectObject(info.sheet);
  551. if (col.field === 'quantity') {
  552. if (select.expr && select.expr !== '') {
  553. info.sheet.getCell(info.row, info.col).text(select.expr);
  554. }
  555. }
  556. },
  557. editEnded: function (e, info) {
  558. if (info.sheet.zh_setting) {
  559. const select = SpreadJsObj.getSelectObject(info.sheet);
  560. const col = info.sheet.zh_setting.cols[info.col];
  561. const validText = info.editingText ? info.editingText.replace('\n', '') : null;
  562. let orgValue;
  563. if (col.field === 'quantity') {
  564. orgValue = validText && validText !== ''
  565. ? _.toNumber(validText) ? select.quantity : select.expr
  566. : (select.expr && select.expr !== '') ? select.expr : select.quantity;
  567. } else {
  568. orgValue = select[col.field];
  569. }
  570. if (orgValue == validText || ((!orgValue || orgValue === '') && (validText === '' || validText === null))) {
  571. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  572. return;
  573. }
  574. const exprQuantity = {
  575. expr: '',
  576. quantity: 0,
  577. };
  578. const [valid, msg] = materialSpreadObj._checkExpr(validText, exprQuantity);
  579. if (!valid) {
  580. toastr.error(msg);
  581. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  582. return;
  583. }
  584. if (isNaN(exprQuantity.quantity)) {
  585. toastr.error('不能输入其它非数字类型字符');
  586. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  587. return;
  588. }
  589. const num = parseFloat(exprQuantity.quantity);
  590. if (num < 0 || !/^\d+(\.\d{1,6})?$/.test(num)) {
  591. toastr.warning('已保留6位小数');
  592. exprQuantity.quantity = ZhCalc.round(num, 6);
  593. }
  594. // 更新至服务器
  595. const ledgerSheet = ledgerSpread.getActiveSheet();
  596. const ledgerSelect = SpreadJsObj.getSelectObject(ledgerSheet);
  597. const gclIndex = _.findIndex(gclGatherData, { b_code: ledgerSelect.b_code, name: ledgerSelect.name, unit: ledgerSelect.unit, unit_price: ledgerSelect.unit_price });
  598. const gcl = gclGatherData[gclIndex].leafXmjs;
  599. const datas = [];
  600. const ms_id = isStageSelf ? materialStageData[0].id : null;
  601. for (const xmj of gcl) {
  602. const data = {
  603. xmj_id: xmj.id,
  604. gcl_id: xmj.gcl_id,
  605. mx_id: xmj.mx_id !== undefined ? xmj.mx_id : '',
  606. };
  607. if (ms_id) data.ms_id = ms_id;
  608. datas.push(data);
  609. }
  610. if (isStageSelf) {
  611. // 取所有的gclGatherData才行,然后获取下的值
  612. const gclData = gclGatherData[gclIndex];
  613. for (const [index, ms] of materialStageData.entries()) {
  614. if (ms.id !== ms_id) {
  615. const gclOther = _.find(gclGatherListData[index], { b_code: gclData.b_code, name: gclData.name, unit: gclData.unit, unit_price: gclData.unit_price });
  616. if (gclOther) {
  617. const leafXmjs = gclOther.leafXmjs.filter(item => item.gather_qty !== null && item.gather_qty !== undefined);
  618. for (const xmj of leafXmjs) {
  619. const data = {
  620. xmj_id: xmj.id,
  621. gcl_id: xmj.gcl_id,
  622. mx_id: xmj.mx_id ? xmj.mx_id : '',
  623. };
  624. if (_.indexOf(datas, data) === -1) {
  625. datas.push(data);
  626. }
  627. }
  628. }
  629. }
  630. }
  631. }
  632. console.log(exprQuantity, datas, select.mb_id);
  633. postData(window.location.pathname + '/save', { type:'updates', updateData: { xmjs: datas, expr: exprQuantity.expr, quantity: exprQuantity.quantity, mb_id: select.mb_id }, ms_id: isStageSelf ? materialStageData[0].id : null }, function (result) {
  634. // materialListData = result;
  635. gclList = result;
  636. loadMaterialData(gclIndex, 0);
  637. materialSpread.getActiveSheet().setSelection(info.row + 1, info.col, 1, 1);
  638. }, function () {
  639. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  640. });
  641. }
  642. },
  643. clipboardPasted(e, info) {
  644. const hint = {
  645. cellError: {type: 'error', msg: '粘贴内容超出了表格范围'},
  646. numberExpr: {type: 'error', msg: '不能粘贴其它非数字类型字符'},
  647. numberCan: {type: 'warning', msg: '已保留6位小数'},
  648. };
  649. const range = info.cellRange;
  650. const sortData = info.sheet.zh_data || [];
  651. if (range.row + range.rowCount > sortData.length) {
  652. toastMessageUniq(hint.cellError);
  653. SpreadJsObj.reLoadSheetHeader(materialSpread.getActiveSheet());
  654. SpreadJsObj.reLoadSheetData(materialSpread.getActiveSheet());
  655. return;
  656. }
  657. if (sortData.length > 0 && range.col + range.colCount > 5) {
  658. toastMessageUniq(hint.cellError);
  659. SpreadJsObj.reLoadSheetHeader(materialSpread.getActiveSheet());
  660. SpreadJsObj.reLoadSheetData(materialSpread.getActiveSheet());
  661. return;
  662. }
  663. const data = [];
  664. for (let iRow = 0; iRow < range.rowCount; iRow++) {
  665. let bPaste = true;
  666. const curRow = range.row + iRow;
  667. const materialData = { id: sortData[curRow].id, mb_id: sortData[curRow].mb_id };
  668. const hintRow = range.rowCount > 1 ? curRow : '';
  669. let sameCol = 0;
  670. for (let iCol = 0; iCol < range.colCount; iCol++) {
  671. const curCol = range.col + iCol;
  672. const colSetting = info.sheet.zh_setting.cols[curCol];
  673. if (!colSetting) continue;
  674. const validText = info.sheet.getText(curRow, curCol).replace('\n', '');
  675. const orgValue = sortData[curRow][colSetting.field];
  676. if (orgValue == validText || ((!orgValue || orgValue === '') && (validText === ''))) {
  677. sameCol++;
  678. if (range.colCount === sameCol) {
  679. bPaste = false;
  680. }
  681. continue;
  682. }
  683. const exprQuantity = {
  684. expr: '',
  685. quantity: 0,
  686. };
  687. const [valid, msg] = materialSpreadObj._checkExpr(validText, exprQuantity);
  688. if (!valid) {
  689. toastMessageUniq(getPasteHint(msg, hintRow));
  690. bPaste = false;
  691. continue;
  692. }
  693. if (isNaN(exprQuantity.quantity)) {
  694. toastMessageUniq(getPasteHint(hint.numberExpr, hintRow));
  695. bPaste = false;
  696. continue;
  697. }
  698. const num = parseFloat(exprQuantity.quantity);
  699. if (num < 0 || !/^\d+(\.\d{1,6})?$/.test(num)) {
  700. toastMessageUniq(getPasteHint(hint.numberCan, hintRow));
  701. exprQuantity.quantity = ZhCalc.round(num, 6);
  702. }
  703. materialData.expr = exprQuantity.expr;
  704. materialData.quantity = exprQuantity.quantity;
  705. }
  706. if (bPaste) {
  707. data.push(materialData);
  708. } else {
  709. SpreadJsObj.reLoadRowData(info.sheet, curRow);
  710. }
  711. }
  712. if (data.length === 0) {
  713. SpreadJsObj.reLoadRowData(info.sheet, info.cellRange.row, info.cellRange.rowCount);
  714. return;
  715. }
  716. const ledgerSheet = ledgerSpread.getActiveSheet();
  717. const ledgerSelect = SpreadJsObj.getSelectObject(ledgerSheet);
  718. const gclIndex = _.findIndex(gclGatherData, { b_code: ledgerSelect.b_code, name: ledgerSelect.name, unit: ledgerSelect.unit, unit_price: ledgerSelect.unit_price });
  719. const gcl = gclGatherData[gclIndex].leafXmjs;
  720. const datas = [];
  721. const ms_id = isStageSelf ? materialStageData[0].id : null;
  722. for (const xmj of gcl) {
  723. const data2 = {
  724. xmj_id: xmj.id,
  725. gcl_id: xmj.gcl_id,
  726. mx_id: xmj.mx_id !== undefined ? xmj.mx_id : '',
  727. };
  728. if (ms_id) data2.ms_id = ms_id;
  729. datas.push(data2);
  730. }
  731. if (isStageSelf) {
  732. // 取所有的gclGatherData才行,然后获取下的值
  733. const gclData = gclGatherData[gclIndex];
  734. for (const [index, ms] of materialStageData.entries()) {
  735. if (ms.id !== ms_id) {
  736. const gclOther = _.find(gclGatherListData[index], { b_code: gclData.b_code, name: gclData.name, unit: gclData.unit, unit_price: gclData.unit_price });
  737. console.log(gclOther);
  738. if (gclOther) {
  739. const leafXmjs = gclOther.leafXmjs.filter(item => item.gather_qty !== null && item.gather_qty !== undefined);
  740. for (const xmj of leafXmjs) {
  741. const data = {
  742. xmj_id: xmj.id,
  743. gcl_id: xmj.gcl_id,
  744. mx_id: xmj.mx_id ? xmj.mx_id : '',
  745. };
  746. if (_.indexOf(datas, data) === -1) {
  747. datas.push(data);
  748. }
  749. }
  750. }
  751. }
  752. }
  753. }
  754. console.log(data, datas);
  755. // 更新至服务器
  756. postData(window.location.pathname + '/save', { type:'pastes', updateData: { xmjs: datas, pasteData: data }, ms_id: isStageSelf ? materialStageData[0].id : null }, function (result) {
  757. // materialListData = result;
  758. gclList = result;
  759. loadMaterialData(gclIndex, 0);
  760. materialSpread.getActiveSheet().setSelection(info.cellRange.row, info.cellRange.col, info.cellRange.rowCount, info.cellRange.colCount);
  761. }, function () {
  762. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  763. });
  764. },
  765. _checkExprValid(expr) {
  766. if (!expr) return [true, null];
  767. const param = [];
  768. let num = '', base = '';
  769. for (let i = 0, iLen = expr.length; i < iLen; i++) {
  770. if (/^[\d\.%]+/.test(expr[i])) {
  771. if (base !== '') {
  772. param.push({type: 'base', value: base});
  773. base = '';
  774. }
  775. num = num + expr[i];
  776. } else if (expr[i] === '(') {
  777. if (num !== '') {
  778. param.push({type: 'num', value: num});
  779. num = '';
  780. }
  781. if (base !== '') {
  782. param.push({type: 'base', value: base});
  783. base = '';
  784. }
  785. param.push({type: 'left', value: '('});
  786. } else if (expr[i] === ')') {
  787. if (num !== '') {
  788. param.push({type: 'num', value: num});
  789. num = '';
  790. }
  791. if (base !== '') {
  792. param.push({type: 'base', value: base});
  793. base = '';
  794. }
  795. param.push({type: 'right', value: ')'});
  796. } else if (/^[\+\-*\/]/.test(expr[i])) {
  797. if (num !== '') {
  798. param.push({type: 'num', value: num});
  799. num = '';
  800. }
  801. if (base !== '') {
  802. param.push({type: 'base', value: base});
  803. base = '';
  804. }
  805. param.push({type: 'calc', value: expr[i]});
  806. } else {
  807. return [false, '输入的表达式含有非法字符: ' + expr[i]];
  808. }
  809. }
  810. if (num !== '') {
  811. param.push({type: 'num', value: num});
  812. num = '';
  813. }
  814. if (base !== '') {
  815. param.push({type: 'base', value: base});
  816. base = '';
  817. }
  818. if (param.length === 0) return true;
  819. if (param.length > 1) {
  820. if (param[0].value === '-') {
  821. param[1].value = '-' + param[1];
  822. }
  823. param.unshift();
  824. }
  825. const iLen = param.length;
  826. let iLeftCount = 0, iRightCount = 0;
  827. for (const [i, p] of param.entries()) {
  828. if (p.type === 'calc') {
  829. if (i === 0 || i === iLen - 1)
  830. return [false, '输入的表达式非法:计算符号' + p.value + '前后应有数字'];
  831. }
  832. if (p.type === 'num') {
  833. num = p.value.replace('%', '');
  834. if (p.value.length - num.length > 1)
  835. return [false, '输入的表达式非法:' + p.value + '不是一个有效的数字'];
  836. num = _.toNumber(num);
  837. if (num === undefined || num === null || _.isNaN(num))
  838. return [false, '输入的表达式非法:' + p.value + '不是一个有效的数字'];
  839. if (i > 0) {
  840. if (param[i - 1].type !== 'calc' && param[i - 1].type !== 'left') {
  841. return [false, '输入的表达式非法:' + p.value + '前应有运算符'];
  842. } else if (param[i - 1].value === '/' && num === 0) {
  843. return [false, '输入的表达式非法:请勿除0'];
  844. }
  845. }
  846. }
  847. if (p.type === 'base') {
  848. if (i > 0 && (param[i - 1].type === 'num' || param[i - 1].type === 'right'))
  849. return [false, '输入的表达式非法:' + p.value + '前应有运算符'];
  850. }
  851. if (p.type === 'left') {
  852. iLeftCount += 1;
  853. if (i !== 0 && param[i-1].type !== 'calc')
  854. return [false, '输入的表达式非法:(前应有运算符'];
  855. }
  856. if (p.type === 'right') {
  857. iRightCount += 1;
  858. if (i !== iLen - 1 && param[i+1].type !== 'calc')
  859. return [false, '输入的表达式非法:)后应有运算符'];
  860. if (iRightCount > iLeftCount)
  861. return [false, '输入的表达式非法:")"前无对应的"("'];
  862. }
  863. }
  864. if (iLeftCount > iRightCount)
  865. return [false, '输入的表达式非法:"("后无对应的")"'];
  866. return [true, ''];
  867. },
  868. _checkExpr: function (text, data) {
  869. if (text) {
  870. const num = _.toNumber(text);
  871. if (num) {
  872. data.quantity = num;
  873. data.expr = '';
  874. } else {
  875. const expr = $.trim(text).replace('\t', '').replace('=', '').toLowerCase();
  876. const [valid, msg] = this._checkExprValid(expr);
  877. if (!valid) return [valid, msg];
  878. data.expr = expr;
  879. data.quantity = ZhCalc.calcExpr.calcExprStrRpn(expr.replace('%', '/100'));
  880. }
  881. } else {
  882. data.quantity = 0;
  883. data.expr = '';
  884. }
  885. return [true, ''];
  886. },
  887. };
  888. materialSpread.bind(spreadNS.Events.EditStarting, materialSpreadObj.editStarting);
  889. materialSpread.bind(spreadNS.Events.EditEnded, materialSpreadObj.editEnded);
  890. materialSpread.bind(spreadNS.Events.ClipboardPasted, materialSpreadObj.clipboardPasted);
  891. SpreadJsObj.addDeleteBind(materialSpread, materialSpreadObj.deletePress);
  892. $.contextMenu({
  893. selector: '#material-spread',
  894. build: function ($trigger, e) {
  895. const target = SpreadJsObj.safeRightClickSelection($trigger, e, materialSpread);
  896. return target.hitTestType === GC.Spread.Sheets.SheetArea.viewport || target.hitTestType === GC.Spread.Sheets.SheetArea.rowHeader;
  897. },
  898. items: {
  899. 'create': {
  900. name: '添加工料',
  901. icon: 'fa-sign-in',
  902. callback: function (key, opt) {
  903. // 获取已选清单
  904. changeMaterialTable();
  905. $('#addgl').modal('show');
  906. },
  907. disabled: function (key, opt) {
  908. const sheet = ledgerSpread.getActiveSheet();
  909. const select = SpreadJsObj.getSelectObject(sheet);
  910. if (!select) {
  911. return true;
  912. }
  913. return readOnly;
  914. }
  915. },
  916. 'delete': {
  917. name: '删除工料',
  918. icon: 'fa-remove',
  919. callback: function (key, opt) {
  920. materialSpreadObj.del(materialSpread.getActiveSheet());
  921. },
  922. disabled: function (key, opt) {
  923. const sheet = materialSpread.getActiveSheet();
  924. const selection = sheet.getSelections();
  925. const sel = selection ? selection[0] : sheet.getSelections()[0];
  926. const select = SpreadJsObj.getSelectObject(sheet);
  927. if (!select) {
  928. return true;
  929. }
  930. if (!readOnly && sel.rowCount === 1 && select && materialBase.isEdit(select)) {
  931. return false;
  932. } else {
  933. return true;
  934. }
  935. }
  936. },
  937. }
  938. });
  939. }
  940. // 切换清单行,读取所属项目节数据
  941. ledgerSpread.getActiveSheet().bind(spreadNS.Events.SelectionChanged, function (e, info) {
  942. if (info.oldSelections !== undefined) {
  943. const iOldRow = info.oldSelections[0].row, iNewRow = info.newSelections[0].row;
  944. if (iNewRow !== iOldRow) {
  945. const sheet = ledgerSpread.getActiveSheet();
  946. const select = SpreadJsObj.getSelectObject(sheet);
  947. const index = _.findIndex(gclGatherData, { b_code: select.b_code, name: select.name, unit: select.unit, unit_price: select.unit_price });
  948. loadMaterialData(index, 0);
  949. }
  950. }
  951. });
  952. $('#open_addtclist').click(function () {
  953. $('#tclist_search').val('');
  954. $('#tclist_search').siblings('a').hide();
  955. makeChecklistData(gclGatherData, materialChecklistData);
  956. $('#addtclist').modal('show');
  957. });
  958. // 回车提交
  959. $('#tclist_search').on('keypress', function () {
  960. if(window.event.keyCode === 13) {
  961. $(this).blur();
  962. }
  963. });
  964. $('#tclist_search').on('blur', function () {
  965. const value = _.trim($(this).val());
  966. let showListData = gclGatherData;
  967. if (value !== '') {
  968. $(this).siblings('a').show();
  969. showListData = _.filter(gclGatherData, function (c) {
  970. return (c.b_code && c.b_code.indexOf(value) !== -1) || (c.name && c.name.indexOf(value) !== -1);
  971. })
  972. } else {
  973. $(this).siblings('a').hide();
  974. }
  975. remakeChecklistData(gclGatherData, showListData);
  976. });
  977. $('.remove-btn').on('click', function () {
  978. $(this).hide();
  979. $(this).siblings('input').val('');
  980. remakeChecklistData(gclGatherData);
  981. });
  982. // 显示有调差工料清单
  983. // $('#show_material_gcl').click(function () {
  984. // if ($(this).is(':checked')) {
  985. // const hadMaterialGclGatherData = [];
  986. // const hadGclIdList = [];
  987. // for (const ml of materialListData) {
  988. // if (hadGclIdList.indexOf(ml.gcl_id) === -1) {
  989. // hadGclIdList.push(ml.gcl_id);
  990. // }
  991. // }
  992. // for (const gcl of gclGatherData) {
  993. // for (const index in gcl.leafXmjs) {
  994. // const gcl_id = gcl.leafXmjs[index].gcl_id;
  995. // if (hadGclIdList.indexOf(gcl_id) !== -1) {
  996. // hadMaterialGclGatherData.push(gcl);
  997. // break;
  998. // }
  999. // }
  1000. // }
  1001. // gclGatherData = hadMaterialGclGatherData;
  1002. // } else {
  1003. // gclGatherModel.loadLedgerData(ledger, curLedgerData);
  1004. // gclGatherModel.loadPosData(pos, curPosData);
  1005. // gclGatherData = gclGatherModel.gatherGclData().filter(item => {
  1006. // return item.qc_qty || item.contract_qty
  1007. // });
  1008. // }
  1009. // SpreadJsObj.loadSheetData(ledgerSpread.getActiveSheet(), SpreadJsObj.DataType.Data, gclGatherData);
  1010. // loadLeafXmjData(0);
  1011. // loadMaterialData(0, 0);
  1012. // SpreadJsObj.resetTopAndSelect(ledgerSpread.getActiveSheet());
  1013. // SpreadJsObj.resetTopAndSelect(leafXmjSpread.getActiveSheet());
  1014. // SpreadJsObj.resetTopAndSelect(materialSpread.getActiveSheet());
  1015. // });
  1016. $.subMenu({
  1017. menu: '#sub-menu', miniMenu: '#sub-mini-menu', miniMenuList: '#mini-menu-list',
  1018. toMenu: '#to-menu', toMiniMenu: '#to-mini-menu',
  1019. key: 'menu.1.0.0',
  1020. miniHint: '#sub-mini-hint', hintKey: 'menu.hint.1.0.1',
  1021. callback: function (info) {
  1022. if (info.mini) {
  1023. $('.panel-title').addClass('fluid');
  1024. $('#sub-menu').removeClass('panel-sidebar');
  1025. } else {
  1026. $('.panel-title').removeClass('fluid');
  1027. $('#sub-menu').addClass('panel-sidebar');
  1028. }
  1029. autoFlashHeight();
  1030. ledgerSpread.refresh();
  1031. materialSpread.refresh();
  1032. }
  1033. });
  1034. $.divResizer({
  1035. select: '#right-spr',
  1036. callback: function () {
  1037. ledgerSpread.refresh();
  1038. materialSpread.refresh();
  1039. const width = (($('#right-view').width()/$('#right-view').parent('div').width())*100).toFixed();
  1040. setLocalCache('material_checklist_' + materialID, width);
  1041. }
  1042. });
  1043. // 展开收起工料并浏览器记住本期展开收起
  1044. $('a', '.right-nav').bind('click', function () {
  1045. const tab = $(this), tabPanel = $(tab.attr('content'));
  1046. if (!tab.hasClass('active')) {
  1047. $('a', '.side-menu').removeClass('active');
  1048. $('.tab-content .tab-select-show').removeClass('active');
  1049. tab.addClass('active');
  1050. tabPanel.addClass('active');
  1051. showSideTools(tab.hasClass('active'));
  1052. if (tab.attr('content') === '#material-tab') {
  1053. const width = (($('#right-view').width()/$('#right-view').parent('div').width())*100).toFixed();
  1054. setLocalCache('material_checklist_' + materialID, width);
  1055. }
  1056. } else {
  1057. removeLocalCache('material_checklist_' + materialID);
  1058. tab.removeClass('active');
  1059. tabPanel.removeClass('active');
  1060. showSideTools(tab.hasClass('active'));
  1061. }
  1062. ledgerSpread.refresh();
  1063. materialSpread.refresh();
  1064. });
  1065. // 根据浏览器记录展开收起
  1066. if (getLocalCache('material_checklist_' + materialID)) {
  1067. const tab = $('.right-nav a[content="#material-tab"]'), tabPanel = $(tab.attr('content'));
  1068. $('a', '.side-menu').removeClass('active');
  1069. $('.tab-content .tab-select-show').removeClass('active');
  1070. tab.addClass('active');
  1071. tabPanel.addClass('active');
  1072. $('#right-view').width(getLocalCache('material_checklist_' + materialID) + '%');
  1073. showSideTools(tab.hasClass('active'));
  1074. ledgerSpread.refresh();
  1075. materialSpread.refresh();
  1076. }
  1077. // 导入功能
  1078. // 上传图片
  1079. let importFile = null;
  1080. $('#upload-list').click(function () {
  1081. if (materialBillsData.length === 0) {
  1082. toastr.error('请添加工料再导入。');
  1083. return
  1084. }
  1085. $("#import").modal('show');
  1086. // $(this).siblings('input').trigger('click');
  1087. });
  1088. $('#upload-list-file').change(function () {
  1089. const file = this.files[0];
  1090. importFile = file;
  1091. });
  1092. $('#import_btn').click(function() {
  1093. if (!importFile) {
  1094. toastr.error('请选择json或excel文件再确定');
  1095. return;
  1096. }
  1097. $('#import').modal('hide');
  1098. const ext = importFile.name.toLowerCase().split('.').splice(-1)[0];
  1099. const imgStr = /(xls|xlsx|json|XLS|XLSX|JSON)$/;
  1100. if (!imgStr.test(ext)) {
  1101. toastr.error('请导入正确格式的json或excel文件。');
  1102. return
  1103. }
  1104. const fileReader = new FileReader();
  1105. fileReader.onload = async function(ev) {
  1106. try{
  1107. const data = ev.target.result;
  1108. let tree = [];
  1109. const ignoreUnitPrice = $('#ignore').is(':checked');
  1110. resetExport();
  1111. $('#okedit').modal('show');
  1112. setProgress($('#export-progress'), 30);
  1113. if (/(xls|xlsx|XLS|XLSX)$/.test(ext)) {
  1114. const workbook = XLSX.read(data, {type: 'binary'}); // 以二进制流方式读取得到整份excel表格对象
  1115. const jsonData = transExcel(XLSX.utils.sheet_to_json(workbook.Sheets[workbook.SheetNames[0]], { defval: null }));
  1116. if (!(jsonData[0] && jsonData[0].b_code !== undefined && jsonData[0].b_code !== null &&
  1117. jsonData[0].gljcode !== undefined && jsonData[0].name !== undefined && jsonData[0].name !== undefined &&
  1118. jsonData[0].unit !== undefined && jsonData[0].unit_price !== undefined)) {
  1119. throw 'excel必须按指定格式内容上传';
  1120. }
  1121. tree = _.filter(jsonData, function (item) {
  1122. return item.b_code !== null;
  1123. });
  1124. let lastIndex = 0;// 防止导入相同的清单导致导入的含量数量增多
  1125. for (const [i,t] of tree.entries()) {
  1126. const jIndex1 = _.findIndex(jsonData, { b_code: t.b_code, name: t.name, unit: t.unit, unit_price: t.unit_price }, lastIndex);
  1127. lastIndex = jIndex1;
  1128. if (i + 1 < tree.length) {
  1129. const jIndex2 = _.findIndex(jsonData, { b_code: tree[i+1].b_code, name: tree[i+1].name, unit: tree[i+1].unit, unit_price: tree[i+1].unit_price }, lastIndex);
  1130. t.children = jsonData.slice(jIndex1 + 1, jIndex2);
  1131. lastIndex = jIndex2;
  1132. } else {
  1133. t.children = jsonData.slice(jIndex1 + 1);
  1134. }
  1135. }
  1136. } else {
  1137. const ascii = jschardet.detect(data.substring(0, 10000));
  1138. iconv.skipDecodeWarning = true
  1139. tree = JSON.parse(iconv.decode(data, ascii.encoding));// 需要转码,否则前端处理中文会出现乱码
  1140. if (!(tree[0] && tree[0].b_code !== undefined && tree[0].b_code !== null &&
  1141. tree[0].gljcode !== undefined && tree[0].name !== undefined && tree[0].name !== undefined &&
  1142. tree[0].unit !== undefined && tree[0].unit_price !== undefined)) {
  1143. throw 'json必须按指定格式内容上传';
  1144. }
  1145. }
  1146. stopProgress($('#export-progress'));
  1147. $('#bill-detail').show();
  1148. setProgress($('#bill-progress'), 30);
  1149. // 导入先生成materialCheckList,再生成materialBillsData,最后生成materialListData
  1150. console.log(tree, gclGatherData, materialChecklistData, materialBillsData);
  1151. const pushChecklist = [];
  1152. const pushBillsData = [];
  1153. const needPushTree = [];
  1154. // 分析materialCheckList和tree,找出相同的清单并插入对应不存在的工料
  1155. for (const t of tree) {
  1156. if(t.children.length === 0) continue;
  1157. const findObject = {
  1158. b_code: t.b_code ? t.b_code.toString() : t.b_code,
  1159. name: t.name ? t.name.toString() : t.name,
  1160. unit: t.unit ? t.unit.toString() : t.unit,
  1161. };
  1162. if (!ignoreUnitPrice) findObject.unit_price = t.unit_price ? parseFloat(t.unit_price) : null;
  1163. const order = _.findIndex(gclGatherData, findObject);
  1164. const mlOrder = _.findIndex(materialChecklistData, findObject);
  1165. const haveQuantity = _.find(t.children, function (item) { return item.quantity && item.quantity !== 0 }) ? 1 : 0;
  1166. if (mlOrder === -1 && order !== -1 && haveQuantity && _.findIndex(pushChecklist, findObject) === -1) {
  1167. pushChecklist.push({
  1168. b_code: gclGatherData[order].b_code,
  1169. name: gclGatherData[order].name,
  1170. unit: gclGatherData[order].unit,
  1171. unit_price: gclGatherData[order].unit_price,
  1172. quantity: gclGatherData[order].quantity ? gclGatherData[order].quantity : null,
  1173. total_price: gclGatherData[order].total_price ? gclGatherData[order].total_price : null,
  1174. had_bills: 0,
  1175. });
  1176. } else if (mlOrder === -1 && order === -1) {
  1177. continue;
  1178. } else if (!haveQuantity) {
  1179. continue;
  1180. }
  1181. needPushTree.push(t);
  1182. // for (const c of t.children) {
  1183. // const mbOrder = _.findIndex(materialBillsData, { code: c.gljcode, name: c.name, unit: c.unit });
  1184. // if (c.b_code !== null && mbOrder === -1 && _.findIndex(pushBillsData, { code: c.gljcode, name: c.name, unit: c.unit }) === -1) {
  1185. // pushBillsData.push({
  1186. // code: c.gljcode,
  1187. // name: c.name,
  1188. // unit: c.unit,
  1189. // spec: c.specs,
  1190. // })
  1191. // }
  1192. // }
  1193. }
  1194. if (needPushTree.length === 0) {
  1195. throw '不存在需要导入的工料清单含量';
  1196. }
  1197. // 先上传需要生成的清单及工料
  1198. if (pushChecklist.length > 0 || pushBillsData.length > 0) {
  1199. postData(window.location.pathname + '/save', { type:'exportCB', addChecklist: pushChecklist, addBillsList: pushBillsData }, async function (result) {
  1200. // materialListData = result;
  1201. materialChecklistData = result.materialChecklistData;
  1202. materialBillsData = result.materialBillsData;
  1203. materialStageBillsData = result.materialStageBillsData;
  1204. stopProgress($('#bill-progress'));
  1205. await pushListData(needPushTree);
  1206. }, function () {
  1207. stop = true;
  1208. clearInterval(interval);
  1209. setTimeout(function () {
  1210. $('#okedit').modal('hide');
  1211. }, 1000);
  1212. });
  1213. } else {
  1214. stopProgress($('#bill-progress'));
  1215. await pushListData(needPushTree);
  1216. }
  1217. } catch (error) {
  1218. console.log(error);
  1219. toastr.error(error);
  1220. stop = true;
  1221. clearInterval(interval);
  1222. setTimeout(function () {
  1223. $('#okedit').modal('hide');
  1224. $('#import').modal('show');
  1225. }, 1000);
  1226. return
  1227. }
  1228. // 分析jsondata,得出每个清单间工料数量,生成新的树结构数组
  1229. // let persons = []; // 存储获取到的数据
  1230. // // 表格的表格范围,可用于判断表头是否数量是否正确
  1231. // let fromTo = '';
  1232. // // 遍历每张表读取
  1233. // for (const sheet in workbook.Sheets) {
  1234. // if (workbook.Sheets.hasOwnProperty(sheet)) {
  1235. // fromTo = workbook.Sheets[sheet]['!ref'];
  1236. // console.log(fromTo);
  1237. // persons = persons.concat(XLSX.utils.sheet_to_json(workbook.Sheets[sheet]));
  1238. // // break; // 如果只取第一张表,就取消注释这行
  1239. // }
  1240. // }
  1241. // console.log(persons);
  1242. };
  1243. // 以二进制方式打开文件
  1244. fileReader.readAsBinaryString(importFile);
  1245. $('#upload-list-file').val('');
  1246. importFile = null;
  1247. });
  1248. function sleep(millisecond) {
  1249. return new Promise(resolve => {
  1250. setTimeout(() => {
  1251. resolve()
  1252. }, millisecond)
  1253. })
  1254. }
  1255. let value = 0;
  1256. let interval;
  1257. let stop = false;
  1258. function setProgress(_this, time = 50) {
  1259. interval = setInterval(function () {
  1260. if (value < 100) {
  1261. value = parseInt(value) + 1;
  1262. _this.css("width", value + "%").text(value + "%");
  1263. } else if (value === 100) {
  1264. value = parseInt(value) + 1;
  1265. value = 30;
  1266. }
  1267. }, time);
  1268. }
  1269. function resetExport() {
  1270. resetProgress($('#export-progress'));
  1271. $('#bill-detail').hide();
  1272. resetProgress($('#bill-progress'));
  1273. $('#list-detail').hide();
  1274. resetProgress($('#list-progress'));
  1275. }
  1276. function resetProgress(_this) {
  1277. _this.removeClass('bg-success');
  1278. _this.css("width", "0%").text("0%").attr('aria-valuenow', '0');
  1279. }
  1280. function stopProgress(_this) {
  1281. if (interval) {
  1282. _this.addClass('bg-success');
  1283. _this.css("width", "100%").text("100%");
  1284. value = 0;
  1285. stop = true;
  1286. clearInterval(interval);
  1287. interval = 0;
  1288. }
  1289. }
  1290. async function pushListData(tree = []) {
  1291. if (tree.length > 0) {
  1292. const ignoreUnitPrice = $('#ignore').is(':checked');
  1293. for (const [i,t] of tree.entries()) {
  1294. $('#list-detail').find('b').text(t.b_code);
  1295. resetProgress($('#list-progress'));
  1296. if (!interval) {
  1297. $('#list-detail').show();
  1298. setProgress($('#list-progress'), 30);
  1299. }
  1300. const mbList = [];
  1301. for (const mb of t.children) {
  1302. if (mb.quantity) {
  1303. const mbInfo = _.find(materialBillsData, { code: mb.gljcode+'', name: mb.name+'', unit: mb.unit+'' });
  1304. if (mbInfo) {
  1305. const num = parseFloat(mb.quantity);
  1306. if (num < 0 || !/^\d+(\.\d{1,6})?$/.test(num)) {
  1307. // toastr.warning('已保留6位小数');
  1308. mb.quantity = ZhCalc.round(num, 6);
  1309. }
  1310. mbList.push({ id: mbInfo.id, quantity: mb.quantity });
  1311. }
  1312. }
  1313. }
  1314. if (mbList.length === 0 && i+1 === tree.length) {
  1315. stopProgress($('#list-progress'));
  1316. toastr.success('导入成功');
  1317. setTimeout(function () { $('#okedit').modal('hide') }, 2000);
  1318. showSjsData();
  1319. return;
  1320. }
  1321. if (mbList.length === 0) {
  1322. continue;
  1323. }
  1324. const findObject = { b_code: t.b_code ? t.b_code.toString() : t.b_code,
  1325. name: t.name ? t.name.toString() : t.name,
  1326. unit: t.unit ? t.unit.toString() : t.unit,
  1327. };
  1328. if (!ignoreUnitPrice) findObject.unit_price = t.unit_price ? parseFloat(t.unit_price) : null;
  1329. const gclIndex = _.findIndex(gclGatherData, findObject);
  1330. const gcl = gclGatherData[gclIndex].leafXmjs;
  1331. const ms_id = isStageSelf ? materialStageData[0].id : null;
  1332. // const index = materialChecklistData.indexOf(select);
  1333. const datas = [];
  1334. for (const xmj of gcl) {
  1335. const notx = findNotJoinLeafXmj(xmj);
  1336. const notx2 = findNotChangeLeafXmj(xmj);
  1337. const data = {
  1338. xmj_id: xmj.id,
  1339. gcl_id: xmj.gcl_id,
  1340. mx_id: xmj.mx_id !== undefined ? xmj.mx_id : '',
  1341. gather_qty: notx2 === undefined ? xmj.gather_qty : xmj.contract_qty,
  1342. is_join: notx === undefined ? 1 : 0,
  1343. };
  1344. if (ms_id) data.ms_id = ms_id;
  1345. datas.push(data);
  1346. }
  1347. if (isStageSelf) {
  1348. // 取所有的gclGatherData才行,然后获取下的值
  1349. const gclData = gclGatherData[gclIndex];
  1350. for (const [index, ms] of materialStageData.entries()) {
  1351. if (ms.id !== ms_id) {
  1352. const gclFindObject = { b_code: gclData.b_code ? gclData.b_code.toString() : gclData.b_code,
  1353. name: gclData.name ? gclData.name.toString() : gclData.name, unit: gclData.unit ? gclData.unit.toString() : gclData.unit };
  1354. if (!ignoreUnitPrice) gclFindObject.unit_price = gclData.unit_price;
  1355. const gclOther = _.find(gclGatherListData[index], gclFindObject);
  1356. if (gclOther) {
  1357. const leafXmjs = gclOther.leafXmjs.filter(item => item.gather_qty !== null && item.gather_qty !== undefined);
  1358. for (const xmj of leafXmjs) {
  1359. const notx = findNotJoinLeafXmj(xmj);
  1360. const notx2 = findNotChangeLeafXmj(xmj);
  1361. const data = {
  1362. xmj_id: xmj.id,
  1363. gcl_id: xmj.gcl_id,
  1364. mx_id: xmj.mx_id ? xmj.mx_id : '',
  1365. gather_qty: notx2 === undefined ? xmj.gather_qty : xmj.contract_qty,
  1366. is_join: notx === undefined ? 1 : 0,
  1367. ms_id: ms.id,
  1368. };
  1369. datas.push(data);
  1370. }
  1371. }
  1372. }
  1373. }
  1374. }
  1375. // 上传到数据库
  1376. const select = _.find(materialChecklistData, findObject);
  1377. console.log(select, datas, mbList);
  1378. if (select) {
  1379. const result = await postDataAsync(window.location.pathname + '/save', {type: 'adds', checklist: { id: select.id, had_bills: 1 }, postData: {xmjs: datas, mbIds: mbList, export: true}});
  1380. // materialListData = result;
  1381. select.had_bills = 1;
  1382. gclList = result;
  1383. stopProgress($('#list-progress'));
  1384. if (i+1 === tree.length) {
  1385. toastr.success('导入成功');
  1386. setTimeout(function () { $('#okedit').modal('hide') }, 2000);
  1387. showSjsData();
  1388. }
  1389. // }, function () {
  1390. // stop = true;
  1391. // clearInterval(interval);
  1392. // setTimeout(function () { $('#okedit').modal('hide') }, 1000);
  1393. // });
  1394. } else {
  1395. stopProgress($('#list-progress'));
  1396. }
  1397. }
  1398. }
  1399. }
  1400. function transExcel(results) {
  1401. const mapInfo = {
  1402. '清单编号': 'b_code',
  1403. '工料编号': 'gljcode',
  1404. '名称': 'name',
  1405. '单位': 'unit',
  1406. '规格': 'specs',
  1407. '单价': 'unit_price',
  1408. '单位耗量': 'quantity',
  1409. };
  1410. return results.map(zhObj => {
  1411. const enObj = {}
  1412. const zhKeys = Object.keys(zhObj);
  1413. zhKeys.forEach(zhKey => {
  1414. const enKey = mapInfo[zhKey];
  1415. enObj[enKey] = zhObj[zhKey] && _.isString(zhObj[zhKey]) ? _.trim(zhObj[zhKey]) : zhObj[zhKey];
  1416. });
  1417. return enObj
  1418. })
  1419. }
  1420. });