material_checklist.js 69 KB

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