material_checklist.js 73 KB

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