material_checklist.js 68 KB

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