material_checklist.js 70 KB

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