material_checklist.js 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972
  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', {}, 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. // 解析清单汇总数据
  130. gclGatherModel.loadLedgerData(ledger, curLedgerData);
  131. gclGatherModel.loadPosData(pos, curPosData);
  132. gclGatherData = gclGatherModel.gatherGclData();
  133. console.log(gclGatherData);
  134. const hadBillsidList = _.uniq(_.map(gclList, 'gcl_id'));
  135. console.log(hadBillsidList, materialChecklistData);
  136. // 对比清单设置和调差清单,还要和台账对比,显示已选清单列表 不同则更新到清单设置页中
  137. const pushData = [];
  138. const updateData = [];
  139. for (const hb of hadBillsidList) {
  140. const gcl = _.find(gclGatherData, function (item) {
  141. return item.leafXmjs && item.leafXmjs.length > 0 && _.findIndex(item.leafXmjs, { gcl_id : hb }) !== -1;
  142. });
  143. if (gcl) {
  144. const mc = _.find(materialChecklistData, { b_code: gcl.b_code, name: gcl.name, unit: gcl.unit, unit_price: gcl.unit_price });
  145. // const newOrder = _.indexOf(gclGatherData, gcl);
  146. // console.log(newOrder);
  147. if (!mc && _.findIndex(pushData, { b_code: gcl.b_code, name: gcl.name, unit: gcl.unit, unit_price: gcl.unit_price }) === -1) {
  148. 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 });
  149. }
  150. }
  151. }
  152. const removeData = [];
  153. for (const mc of materialChecklistData) {
  154. const gcl = _.find(gclGatherData, { b_code: mc.b_code, name: mc.name, unit: mc.unit, unit_price: mc.unit_price });
  155. console.log(gcl);
  156. // 判断是否已不存在工料清单,台账修改过后删除之
  157. if (!gcl) {
  158. removeData.push(mc.id);
  159. } else {
  160. const gcl_ids = gcl.leafXmjs ? _.uniq(_.map(gcl.leafXmjs, 'gcl_id')) : [];
  161. const jiaoji = _.intersection(gcl_ids, hadBillsidList);
  162. // const leafXmjs = gcl.leafXmjs ? gcl.leafXmjs.filter(item => item.gather_qty) : [];
  163. // 更新had_bills值
  164. const updateObj = { id: mc.id };
  165. if (mc.had_bills === 1) {
  166. if (jiaoji.length === 0) {
  167. updateObj.mid = materialID;
  168. updateObj.had_bills = 0;
  169. // updateData.push({ id: mc.id, mid: materialID, had_bills: 0 });
  170. }
  171. } else if (mc.had_bills === 0) {
  172. if (jiaoji.length !== 0) {
  173. updateObj.had_bills = 1;
  174. }
  175. }
  176. // 更新工程量及台账金额
  177. if (mc.quantity !== (gcl.quantity ? gcl.quantity : null)) {
  178. updateObj.quantity = gcl.quantity ? gcl.quantity : null;
  179. updateObj.total_price = gcl.total_price ? gcl.total_price : null;
  180. }
  181. if(!_.isEqual(updateObj,{ id: mc.id })) updateData.push(updateObj);
  182. }
  183. }
  184. console.log(pushData, removeData, updateData);
  185. setChecklistData(pushData, removeData, updateData, true);
  186. });
  187. function setChecklistData(pushData, removeData, updateData = [], sendmsg = false) {
  188. if (pushData.length > 0 || removeData.length > 0 || updateData.length > 0) {
  189. postData(window.location.pathname + '/save', { type: 'resetChecklist', pushData, removeData, updateData }, function (result2) {
  190. if (sendmsg && pushData.length > 0) {
  191. toastr.success('已同步历史调差清单数据至本页中');
  192. }
  193. if (sendmsg && removeData.length > 0) {
  194. toastr.warning('已删除部分与台账清单不匹配的清单数据');
  195. }
  196. materialChecklistData = result2;
  197. showSjsData();
  198. })
  199. } else {
  200. showSjsData();
  201. }
  202. }
  203. function showSjsData() {
  204. SpreadJsObj.loadSheetData(ledgerSpread.getActiveSheet(), SpreadJsObj.DataType.Data, materialChecklistData);
  205. SpreadJsObj.resetTopAndSelect(ledgerSpread.getActiveSheet());
  206. if (materialChecklistData.length > 0) {
  207. const index = _.findIndex(gclGatherData, { b_code: materialChecklistData[0].b_code, name: materialChecklistData[0].name, unit: materialChecklistData[0].unit, unit_price: materialChecklistData[0].unit_price });
  208. loadMaterialData(index, 0);
  209. } else {
  210. loadMaterialData(-1, 0);
  211. }
  212. const sheet = materialSpread.getActiveSheet();
  213. sheet.suspendPaint();
  214. sheet.setCellType(1, 3, new TipCellType(), spreadNS.SheetArea.colHeader);
  215. sheet.resumePaint();
  216. }
  217. // 调差清单工料table
  218. const materialSpread = SpreadJsObj.createNewSpread($('#material-spread')[0]);
  219. const materialSpreadSetting = {
  220. cols: [
  221. {title: '清单工料含量|编号', colSpan: '5|1', rowSpan: '1|1', field: 'code', hAlign: 0, width: 80, formatter: '@', readOnly: true},
  222. {title: '|名称', colSpan: '|1', rowSpan: '|1', field: 'name', hAlign: 0, width: 100, formatter: '@', readOnly: true},
  223. {title: '|单位', colSpan: '|1', rowSpan: '|1', field: 'unit', hAlign: 1, width: 60, formatter: '@', readOnly: true},
  224. {title: '|数量 �', colSpan: '|1', rowSpan: '|1', field: 'quantity', hAlign: 2, width: 80, type: 'Number', readOnly: 'readOnly.isEdit'},
  225. {title: '|计算式', colSpan: '1', rowSpan: '|1', field: 'expr', hAlign: 2, width: 120, formatter: '@', readOnly: 'readOnly.isEdit'},
  226. ],
  227. emptyRows: 0,
  228. headRows: 2,
  229. headRowHeight: [25, 25],
  230. defaultRowHeight: 21,
  231. headerFont: '12px 微软雅黑',
  232. font: '12px 微软雅黑',
  233. };
  234. const materialBase = {
  235. isEdit: function (data) {
  236. // 是否本期添加的工料
  237. return data.order === stage_order;
  238. }
  239. };
  240. const materialCol = {
  241. readOnly: {
  242. isEdit: function (data) {
  243. // return !(!readOnly && materialBase.isEdit(data));
  244. return readOnly;
  245. },
  246. },
  247. };
  248. SpreadJsObj.initSpreadSettingEvents(materialSpreadSetting, materialCol);
  249. // 获取项目节数据
  250. let materialList = [];
  251. function loadMaterialData(iGclRow, iLXmjRow) {
  252. const gcl = gclGatherData[iGclRow];
  253. // const leafXmjs = gcl && gcl.leafXmjs ? gcl.leafXmjs.filter(item => {
  254. // return item.qc_qty || item.contract_qty
  255. // }) : null;
  256. if (gcl && gcl.leafXmjs) {
  257. const gcl_ids = gcl.leafXmjs ? _.uniq(_.map(gcl.leafXmjs, 'gcl_id')) : [];
  258. // const xmj2 = gcl.leafXmjs[iLXmjRow];
  259. materialList = [];
  260. const newMaterialList = _.uniqBy(_.filter(gclList, function (m) {
  261. return _.indexOf(gcl_ids, m.gcl_id) !== -1;
  262. }), 'mb_id');
  263. for(const m of newMaterialList) {
  264. const bills = _.find(materialBillsData, { id: m.mb_id });
  265. if(bills) {
  266. m.code = bills.code;
  267. m.name = bills.name;
  268. m.unit = bills.unit;
  269. }
  270. }
  271. console.log(newMaterialList);
  272. materialList = newMaterialList;
  273. // const leafXmjs = gcl.leafXmjs.filter(item => {
  274. // return item.qc_qty || item.contract_qty
  275. // });
  276. // const xmj = leafXmjs[iLXmjRow];
  277. // materialList = _.filter(materialListData, function (m) {
  278. // 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);
  279. // });
  280. // 对清单调差工料table的单位数量进行改变
  281. materialSpreadSetting.cols[materialSpreadSetting.cols.length - 2].title = '|' + gcl.unit + '数量 �';
  282. SpreadJsObj.initSheet(materialSpread.getActiveSheet(), materialSpreadSetting);
  283. SpreadJsObj.loadSheetData(materialSpread.getActiveSheet(), SpreadJsObj.DataType.Data, materialList);
  284. } else {
  285. materialSpreadSetting.cols[materialSpreadSetting.cols.length - 2].title = '数量 �';
  286. SpreadJsObj.initSheet(materialSpread.getActiveSheet(), materialSpreadSetting);
  287. SpreadJsObj.loadSheetData(materialSpread.getActiveSheet(), SpreadJsObj.DataType.Data, []);
  288. }
  289. SpreadJsObj.resetTopAndSelect(materialSpread.getActiveSheet());
  290. }
  291. // 对添加工料表格赋值
  292. function changeMaterialTable() {
  293. $('#materialBills tr').removeClass('table-secondary');
  294. $('#materialBills').find('input').removeAttr('disabled');
  295. $('#materialBills').find('input').prop('checked', false);
  296. for (const ml of materialList) {
  297. const mbIndex = _.findIndex(materialBillsData, {id : ml.mb_id });
  298. if (mbIndex !== -1) {
  299. $('#materialBills tr').eq(mbIndex).addClass('table-secondary');
  300. $('#materialBills').find('input').eq(mbIndex).attr('disabled', true);
  301. $('#materialBills').find('input').eq(mbIndex).prop('checked', true);
  302. }
  303. }
  304. }
  305. // 选中清单并添加
  306. $('#set_checklist_btn').click(function () {
  307. const select_checklist = $('#lists_data').find('input:checked:not(:disabled)');
  308. const pushData = [];
  309. for (const sc of select_checklist) {
  310. const order = parseInt($(sc).val());
  311. const checklistInfo = _.find(materialChecklistData, { b_code: gclGatherData[order].b_code, name: gclGatherData[order].name, unit: gclGatherData[order].unit, unit_price: gclGatherData[order].unit_price });
  312. if (!checklistInfo) {
  313. pushData.push({
  314. b_code: gclGatherData[order].b_code,
  315. name: gclGatherData[order].name,
  316. unit: gclGatherData[order].unit,
  317. unit_price: gclGatherData[order].unit_price,
  318. quantity: gclGatherData[order].quantity ? gclGatherData[order].quantity : null,
  319. total_price: gclGatherData[order].total_price ? gclGatherData[order].total_price : null,
  320. had_bills: 0,
  321. })
  322. }
  323. }
  324. const notSelect_checklist = $('#lists_data').find('input:not(:checked):not(:disabled)');
  325. const removeData = [];
  326. for (const nsc of notSelect_checklist) {
  327. const order = parseInt($(nsc).val());
  328. // const order = parseInt($(nsc).attr('data-index'));
  329. const checklistInfo = _.find(materialChecklistData, { b_code: gclGatherData[order].b_code, name: gclGatherData[order].name, unit: gclGatherData[order].unit, unit_price: gclGatherData[order].unit_price });
  330. if (checklistInfo) {
  331. removeData.push(checklistInfo.id);
  332. }
  333. }
  334. setChecklistData(pushData, removeData);
  335. $('#addtclist').modal('hide');
  336. });
  337. // 筛选无调差工料清单
  338. $('#notBills_checkList').click(function () {
  339. const isCheck = $(this).is(':checked');
  340. let newMaterialChecklistData = materialChecklistData;
  341. if (isCheck) {
  342. $('#bills0_checkList').prop('checked', false);
  343. newMaterialChecklistData = _.filter(materialChecklistData, { had_bills: 0 });
  344. }
  345. SpreadJsObj.loadSheetData(ledgerSpread.getActiveSheet(), SpreadJsObj.DataType.Data, newMaterialChecklistData);
  346. SpreadJsObj.resetTopAndSelect(ledgerSpread.getActiveSheet());
  347. if (newMaterialChecklistData.length > 0) {
  348. const index = _.findIndex(gclGatherData, { b_code: newMaterialChecklistData[0].b_code, name: newMaterialChecklistData[0].name, unit: newMaterialChecklistData[0].unit, unit_price: newMaterialChecklistData[0].unit_price });
  349. loadMaterialData(index, 0);
  350. } else {
  351. loadMaterialData(-1, 0);
  352. }
  353. });
  354. // 筛选调差工料清单为0
  355. $('#bills0_checkList').click(function () {
  356. const isCheck = $(this).is(':checked');
  357. let newMaterialChecklistData = materialChecklistData;
  358. if (isCheck) {
  359. newMaterialChecklistData = [];
  360. $('#notBills_checkList').prop('checked', false);
  361. const materialList0 = _.uniq(_.map(_.filter(gclList, { quantity: 0 }), 'gcl_id'));
  362. if (materialList0.length > 0) {
  363. const hadMaterialChecklistData = _.filter(materialChecklistData, { had_bills: 1 });
  364. for (const h of hadMaterialChecklistData) {
  365. const gcl = _.find(gclGatherData, { b_code: h.b_code, name: h.name, unit: h.unit, unit_price: h.unit_price });
  366. if (gcl && gcl.leafXmjs.length > 0 && _.indexOf(materialList0, gcl.leafXmjs[0].gcl_id) !== -1) {
  367. newMaterialChecklistData.push(h);
  368. }
  369. }
  370. }
  371. }
  372. SpreadJsObj.loadSheetData(ledgerSpread.getActiveSheet(), SpreadJsObj.DataType.Data, newMaterialChecklistData);
  373. SpreadJsObj.resetTopAndSelect(ledgerSpread.getActiveSheet());
  374. if (newMaterialChecklistData.length > 0) {
  375. const index = _.findIndex(gclGatherData, { b_code: newMaterialChecklistData[0].b_code, name: newMaterialChecklistData[0].name, unit: newMaterialChecklistData[0].unit, unit_price: newMaterialChecklistData[0].unit_price });
  376. loadMaterialData(index, 0);
  377. } else {
  378. loadMaterialData(-1, 0);
  379. }
  380. });
  381. // 添加调差工料
  382. $('#add_material_bill').click(function () {
  383. // 获取已选工料
  384. $('#materialBills').find('input:disabled').prop('checked', false);
  385. const selectList = $('#materialBills').find('input:checked');
  386. if (selectList.length === 0) {
  387. toastr.warning('请选择调差工料');
  388. $('#materialBills').find('input:disabled').prop('checked', true);
  389. return false;
  390. }
  391. const mb_id = [];
  392. for (let s = 0; s < selectList.length; s++) {
  393. mb_id.push($('#materialBills').find('input:checked').eq(s).val());
  394. }
  395. // 获取当前项目节或部位明细id
  396. const sheet = ledgerSpread.getActiveSheet();
  397. const select = SpreadJsObj.getSelectObject(sheet);
  398. const gclIndex = _.findIndex(gclGatherData, { b_code: select.b_code, name: select.name, unit: select.unit, unit_price: select.unit_price });
  399. const gcl = gclGatherData[gclIndex].leafXmjs;
  400. const index = materialChecklistData.indexOf(select);
  401. const datas = [];
  402. for (const xmj of gcl) {
  403. const notx = findNotJoinLeafXmj(xmj);
  404. const data = {
  405. xmj_id: xmj.id,
  406. gcl_id: xmj.gcl_id,
  407. mx_id: xmj.mx_id !== undefined ? xmj.mx_id : '',
  408. gather_qty: xmj.gather_qty,
  409. is_join: notx === undefined ? 1 : 0,
  410. };
  411. datas.push(data);
  412. }
  413. // 上传到数据库
  414. console.log(datas, gcl);
  415. postData(window.location.pathname + '/save', {type: 'adds', checklist: { id: select.id, had_bills: 1 }, postData: {xmjs: datas, mbIds: mb_id}}, function (result) {
  416. // materialListData = result;
  417. gclList = result;
  418. materialChecklistData[index].had_bills = 1;
  419. loadMaterialData(gclIndex, 0);
  420. // SpreadJsObj.reLoadRowData(ledgerSpread.getActiveSheet(), index);
  421. $('#addgl').modal('hide');
  422. });
  423. $('#materialBills').find('input:disabled').prop('checked', true);
  424. });
  425. if (!readOnly) {
  426. // material-spread右键功能
  427. const materialSpreadObj = {
  428. del: function () {
  429. const materialSheet = materialSpread.getActiveSheet();
  430. const materialSelect = SpreadJsObj.getSelectObject(materialSheet);
  431. const sheet = ledgerSpread.getActiveSheet();
  432. const select = SpreadJsObj.getSelectObject(sheet);
  433. const index = materialChecklistData.indexOf(select);
  434. const gclIndex = _.findIndex(gclGatherData, { b_code: select.b_code, name: select.name, unit: select.unit, unit_price: select.unit_price });
  435. const gcl = gclGatherData[gclIndex].leafXmjs;
  436. const datas = [];
  437. for (const xmj of gcl) {
  438. const data = {
  439. xmj_id: xmj.id,
  440. gcl_id: xmj.gcl_id,
  441. mx_id: xmj.mx_id !== undefined ? xmj.mx_id : '',
  442. };
  443. datas.push(data);
  444. }
  445. const xmj = gcl[0];
  446. const materialCount = _.size(_.filter(gclList, function (m) {
  447. return m.gcl_id === xmj.gcl_id;
  448. }));
  449. // const materialCount = _.size(_.filter(materialListData, function (m) {
  450. // 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);
  451. // }));
  452. let checklist = false;
  453. if (materialCount === 1) {
  454. checklist = {
  455. id: select.id,
  456. had_bills: 0,
  457. }
  458. }
  459. console.log(datas, materialSelect.mb_id, checklist);
  460. postData(window.location.pathname + '/save', {type: 'dels', checklist, postData: { xmjs: datas, mb_id: materialSelect.mb_id }}, function (result) {
  461. // materialListData = result;
  462. gclList = result;
  463. if (checklist) materialChecklistData[index].had_bills = checklist.had_bills;
  464. loadMaterialData(gclIndex, 0);
  465. // SpreadJsObj.reLoadRowData(ledgerSpread.getActiveSheet(), index);
  466. });
  467. },
  468. deletePress: function (sheet) {
  469. return;
  470. },
  471. editStarting: function (e, info) {
  472. const col = info.sheet.zh_setting.cols[info.col];
  473. const select = SpreadJsObj.getSelectObject(info.sheet);
  474. if (col.field === 'quantity') {
  475. if (select.expr && select.expr !== '') {
  476. info.sheet.getCell(info.row, info.col).text(select.expr);
  477. }
  478. }
  479. },
  480. editEnded: function (e, info) {
  481. if (info.sheet.zh_setting) {
  482. const select = SpreadJsObj.getSelectObject(info.sheet);
  483. const col = info.sheet.zh_setting.cols[info.col];
  484. const validText = info.editingText ? info.editingText.replace('\n', '') : null;
  485. let orgValue;
  486. if (col.field === 'quantity') {
  487. orgValue = validText && validText !== ''
  488. ? _.toNumber(validText) ? select.quantity : select.expr
  489. : (select.expr && select.expr !== '') ? select.expr : select.quantity;
  490. } else {
  491. orgValue = select[col.field];
  492. }
  493. if (orgValue == validText || ((!orgValue || orgValue === '') && (validText === '' || validText === null))) {
  494. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  495. return;
  496. }
  497. const exprQuantity = {
  498. expr: '',
  499. quantity: 0,
  500. };
  501. const [valid, msg] = materialSpreadObj._checkExpr(validText, exprQuantity);
  502. if (!valid) {
  503. toastr.error(msg);
  504. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  505. return;
  506. }
  507. if (isNaN(exprQuantity.quantity)) {
  508. toastr.error('不能输入其它非数字类型字符');
  509. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  510. return;
  511. }
  512. const num = parseFloat(exprQuantity.quantity);
  513. if (num < 0 || !/^\d+(\.\d{1,6})?$/.test(num)) {
  514. toastr.warning('已保留6位小数');
  515. exprQuantity.quantity = ZhCalc.round(num, 6);
  516. }
  517. // 更新至服务器
  518. const ledgerSheet = ledgerSpread.getActiveSheet();
  519. const ledgerSelect = SpreadJsObj.getSelectObject(ledgerSheet);
  520. const gclIndex = _.findIndex(gclGatherData, { b_code: ledgerSelect.b_code, name: ledgerSelect.name, unit: ledgerSelect.unit, unit_price: ledgerSelect.unit_price });
  521. const gcl = gclGatherData[gclIndex].leafXmjs;
  522. const datas = [];
  523. for (const xmj of gcl) {
  524. const data = {
  525. xmj_id: xmj.id,
  526. gcl_id: xmj.gcl_id,
  527. mx_id: xmj.mx_id !== undefined ? xmj.mx_id : '',
  528. };
  529. datas.push(data);
  530. }
  531. console.log(exprQuantity, datas, select.mb_id);
  532. postData(window.location.pathname + '/save', { type:'updates', updateData: { xmjs: datas, expr: exprQuantity.expr, quantity: exprQuantity.quantity, mb_id: select.mb_id } }, function (result) {
  533. // materialListData = result;
  534. gclList = result;
  535. loadMaterialData(gclIndex, 0);
  536. materialSpread.getActiveSheet().setSelection(info.row + 1, info.col, 1, 1);
  537. }, function () {
  538. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  539. });
  540. }
  541. },
  542. clipboardPasted(e, info) {
  543. const hint = {
  544. cellError: {type: 'error', msg: '粘贴内容超出了表格范围'},
  545. numberExpr: {type: 'error', msg: '不能粘贴其它非数字类型字符'},
  546. numberCan: {type: 'warning', msg: '已保留6位小数'},
  547. };
  548. const range = info.cellRange;
  549. const sortData = info.sheet.zh_data || [];
  550. if (range.row + range.rowCount > sortData.length) {
  551. toastMessageUniq(hint.cellError);
  552. SpreadJsObj.reLoadSheetHeader(materialSpread.getActiveSheet());
  553. SpreadJsObj.reLoadSheetData(materialSpread.getActiveSheet());
  554. return;
  555. }
  556. if (sortData.length > 0 && range.col + range.colCount > 5) {
  557. toastMessageUniq(hint.cellError);
  558. SpreadJsObj.reLoadSheetHeader(materialSpread.getActiveSheet());
  559. SpreadJsObj.reLoadSheetData(materialSpread.getActiveSheet());
  560. return;
  561. }
  562. const data = [];
  563. for (let iRow = 0; iRow < range.rowCount; iRow++) {
  564. let bPaste = true;
  565. const curRow = range.row + iRow;
  566. const materialData = { id: sortData[curRow].id, mb_id: sortData[curRow].mb_id };
  567. const hintRow = range.rowCount > 1 ? curRow : '';
  568. let sameCol = 0;
  569. for (let iCol = 0; iCol < range.colCount; iCol++) {
  570. const curCol = range.col + iCol;
  571. const colSetting = info.sheet.zh_setting.cols[curCol];
  572. if (!colSetting) continue;
  573. const validText = info.sheet.getText(curRow, curCol).replace('\n', '');
  574. const orgValue = sortData[curRow][colSetting.field];
  575. if (orgValue == validText || ((!orgValue || orgValue === '') && (validText === ''))) {
  576. sameCol++;
  577. if (range.colCount === sameCol) {
  578. bPaste = false;
  579. }
  580. continue;
  581. }
  582. const exprQuantity = {
  583. expr: '',
  584. quantity: 0,
  585. };
  586. const [valid, msg] = materialSpreadObj._checkExpr(validText, exprQuantity);
  587. if (!valid) {
  588. toastMessageUniq(getPasteHint(msg, hintRow));
  589. bPaste = false;
  590. continue;
  591. }
  592. if (isNaN(exprQuantity.quantity)) {
  593. toastMessageUniq(getPasteHint(hint.numberExpr, hintRow));
  594. bPaste = false;
  595. continue;
  596. }
  597. const num = parseFloat(exprQuantity.quantity);
  598. if (num < 0 || !/^\d+(\.\d{1,6})?$/.test(num)) {
  599. toastMessageUniq(getPasteHint(hint.numberCan, hintRow));
  600. exprQuantity.quantity = ZhCalc.round(num, 6);
  601. }
  602. materialData.expr = exprQuantity.expr;
  603. materialData.quantity = exprQuantity.quantity;
  604. }
  605. if (bPaste) {
  606. data.push(materialData);
  607. } else {
  608. SpreadJsObj.reLoadRowData(info.sheet, curRow);
  609. }
  610. }
  611. if (data.length === 0) {
  612. SpreadJsObj.reLoadRowData(info.sheet, info.cellRange.row, info.cellRange.rowCount);
  613. return;
  614. }
  615. const ledgerSheet = ledgerSpread.getActiveSheet();
  616. const ledgerSelect = SpreadJsObj.getSelectObject(ledgerSheet);
  617. const gclIndex = _.findIndex(gclGatherData, { b_code: ledgerSelect.b_code, name: ledgerSelect.name, unit: ledgerSelect.unit, unit_price: ledgerSelect.unit_price });
  618. const gcl = gclGatherData[gclIndex].leafXmjs;
  619. const datas = [];
  620. for (const xmj of gcl) {
  621. const data2 = {
  622. xmj_id: xmj.id,
  623. gcl_id: xmj.gcl_id,
  624. mx_id: xmj.mx_id !== undefined ? xmj.mx_id : '',
  625. };
  626. datas.push(data2);
  627. }
  628. console.log(data, datas);
  629. // 更新至服务器
  630. postData(window.location.pathname + '/save', { type:'pastes', updateData: { xmjs: datas, pasteData: data } }, function (result) {
  631. // materialListData = result;
  632. gclList = result;
  633. loadMaterialData(gclIndex, 0);
  634. materialSpread.getActiveSheet().setSelection(info.cellRange.row, info.cellRange.col, info.cellRange.rowCount, info.cellRange.colCount);
  635. }, function () {
  636. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  637. });
  638. },
  639. _checkExprValid(expr) {
  640. if (!expr) return [true, null];
  641. const param = [];
  642. let num = '', base = '';
  643. for (let i = 0, iLen = expr.length; i < iLen; i++) {
  644. if (/^[\d\.%]+/.test(expr[i])) {
  645. if (base !== '') {
  646. param.push({type: 'base', value: base});
  647. base = '';
  648. }
  649. num = num + expr[i];
  650. } else if (expr[i] === '(') {
  651. if (num !== '') {
  652. param.push({type: 'num', value: num});
  653. num = '';
  654. }
  655. if (base !== '') {
  656. param.push({type: 'base', value: base});
  657. base = '';
  658. }
  659. param.push({type: 'left', value: '('});
  660. } else if (expr[i] === ')') {
  661. if (num !== '') {
  662. param.push({type: 'num', value: num});
  663. num = '';
  664. }
  665. if (base !== '') {
  666. param.push({type: 'base', value: base});
  667. base = '';
  668. }
  669. param.push({type: 'right', value: ')'});
  670. } else if (/^[\+\-*\/]/.test(expr[i])) {
  671. if (num !== '') {
  672. param.push({type: 'num', value: num});
  673. num = '';
  674. }
  675. if (base !== '') {
  676. param.push({type: 'base', value: base});
  677. base = '';
  678. }
  679. param.push({type: 'calc', value: expr[i]});
  680. } else {
  681. return [false, '输入的表达式含有非法字符: ' + expr[i]];
  682. }
  683. }
  684. if (num !== '') {
  685. param.push({type: 'num', value: num});
  686. num = '';
  687. }
  688. if (base !== '') {
  689. param.push({type: 'base', value: base});
  690. base = '';
  691. }
  692. if (param.length === 0) return true;
  693. if (param.length > 1) {
  694. if (param[0].value === '-') {
  695. param[1].value = '-' + param[1];
  696. }
  697. param.unshift();
  698. }
  699. const iLen = param.length;
  700. let iLeftCount = 0, iRightCount = 0;
  701. for (const [i, p] of param.entries()) {
  702. if (p.type === 'calc') {
  703. if (i === 0 || i === iLen - 1)
  704. return [false, '输入的表达式非法:计算符号' + p.value + '前后应有数字'];
  705. }
  706. if (p.type === 'num') {
  707. num = p.value.replace('%', '');
  708. if (p.value.length - num.length > 1)
  709. return [false, '输入的表达式非法:' + p.value + '不是一个有效的数字'];
  710. num = _.toNumber(num);
  711. if (num === undefined || num === null || _.isNaN(num))
  712. return [false, '输入的表达式非法:' + p.value + '不是一个有效的数字'];
  713. if (i > 0) {
  714. if (param[i - 1].type !== 'calc' && param[i - 1].type !== 'left') {
  715. return [false, '输入的表达式非法:' + p.value + '前应有运算符'];
  716. } else if (param[i - 1].value === '/' && num === 0) {
  717. return [false, '输入的表达式非法:请勿除0'];
  718. }
  719. }
  720. }
  721. if (p.type === 'base') {
  722. if (i > 0 && (param[i - 1].type === 'num' || param[i - 1].type === 'right'))
  723. return [false, '输入的表达式非法:' + p.value + '前应有运算符'];
  724. }
  725. if (p.type === 'left') {
  726. iLeftCount += 1;
  727. if (i !== 0 && param[i-1].type !== 'calc')
  728. return [false, '输入的表达式非法:(前应有运算符'];
  729. }
  730. if (p.type === 'right') {
  731. iRightCount += 1;
  732. if (i !== iLen - 1 && param[i+1].type !== 'calc')
  733. return [false, '输入的表达式非法:)后应有运算符'];
  734. if (iRightCount > iLeftCount)
  735. return [false, '输入的表达式非法:")"前无对应的"("'];
  736. }
  737. }
  738. if (iLeftCount > iRightCount)
  739. return [false, '输入的表达式非法:"("后无对应的")"'];
  740. return [true, ''];
  741. },
  742. _checkExpr: function (text, data) {
  743. if (text) {
  744. const num = _.toNumber(text);
  745. if (num) {
  746. data.quantity = num;
  747. data.expr = '';
  748. } else {
  749. const expr = $.trim(text).replace('\t', '').replace('=', '').toLowerCase();
  750. const [valid, msg] = this._checkExprValid(expr);
  751. if (!valid) return [valid, msg];
  752. data.expr = expr;
  753. data.quantity = ZhCalc.calcExpr.calcExprStrRpn(expr);
  754. }
  755. } else {
  756. data.quantity = 0;
  757. data.expr = '';
  758. }
  759. return [true, ''];
  760. },
  761. };
  762. materialSpread.bind(spreadNS.Events.EditStarting, materialSpreadObj.editStarting);
  763. materialSpread.bind(spreadNS.Events.EditEnded, materialSpreadObj.editEnded);
  764. materialSpread.bind(spreadNS.Events.ClipboardPasted, materialSpreadObj.clipboardPasted);
  765. SpreadJsObj.addDeleteBind(materialSpread, materialSpreadObj.deletePress);
  766. $.contextMenu({
  767. selector: '#material-spread',
  768. build: function ($trigger, e) {
  769. const target = SpreadJsObj.safeRightClickSelection($trigger, e, materialSpread);
  770. return target.hitTestType === GC.Spread.Sheets.SheetArea.viewport || target.hitTestType === GC.Spread.Sheets.SheetArea.rowHeader;
  771. },
  772. items: {
  773. 'create': {
  774. name: '添加工料',
  775. icon: 'fa-sign-in',
  776. callback: function (key, opt) {
  777. // 获取已选清单
  778. changeMaterialTable();
  779. $('#addgl').modal('show');
  780. },
  781. disabled: function (key, opt) {
  782. const sheet = ledgerSpread.getActiveSheet();
  783. const select = SpreadJsObj.getSelectObject(sheet);
  784. if (!select) {
  785. return true;
  786. }
  787. return readOnly;
  788. }
  789. },
  790. 'delete': {
  791. name: '删除工料',
  792. icon: 'fa-remove',
  793. callback: function (key, opt) {
  794. materialSpreadObj.del(materialSpread.getActiveSheet());
  795. },
  796. disabled: function (key, opt) {
  797. const sheet = materialSpread.getActiveSheet();
  798. const selection = sheet.getSelections();
  799. const sel = selection ? selection[0] : sheet.getSelections()[0];
  800. const select = SpreadJsObj.getSelectObject(sheet);
  801. if (!select) {
  802. return true;
  803. }
  804. if (!readOnly && sel.rowCount === 1 && select && materialBase.isEdit(select)) {
  805. return false;
  806. } else {
  807. return true;
  808. }
  809. }
  810. },
  811. }
  812. });
  813. }
  814. // 切换清单行,读取所属项目节数据
  815. ledgerSpread.getActiveSheet().bind(spreadNS.Events.SelectionChanged, function (e, info) {
  816. if (info.oldSelections !== undefined) {
  817. const iOldRow = info.oldSelections[0].row, iNewRow = info.newSelections[0].row;
  818. if (iNewRow !== iOldRow) {
  819. const sheet = ledgerSpread.getActiveSheet();
  820. const select = SpreadJsObj.getSelectObject(sheet);
  821. const index = _.findIndex(gclGatherData, { b_code: select.b_code, name: select.name, unit: select.unit, unit_price: select.unit_price });
  822. loadMaterialData(index, 0);
  823. }
  824. }
  825. });
  826. $('#open_addtclist').click(function () {
  827. $('#tclist_search').val('');
  828. $('#tclist_search').siblings('a').hide();
  829. makeChecklistData(gclGatherData, materialChecklistData);
  830. $('#addtclist').modal('show');
  831. });
  832. // 回车提交
  833. $('#tclist_search').on('keypress', function () {
  834. if(window.event.keyCode === 13) {
  835. $(this).blur();
  836. }
  837. });
  838. $('#tclist_search').on('blur', function () {
  839. const value = _.trim($(this).val());
  840. let showListData = gclGatherData;
  841. if (value !== '') {
  842. $(this).siblings('a').show();
  843. showListData = _.filter(gclGatherData, function (c) {
  844. return (c.b_code && c.b_code.indexOf(value) !== -1) || (c.name && c.name.indexOf(value) !== -1);
  845. })
  846. } else {
  847. $(this).siblings('a').hide();
  848. }
  849. remakeChecklistData(gclGatherData, showListData);
  850. });
  851. $('.remove-btn').on('click', function () {
  852. $(this).hide();
  853. $(this).siblings('input').val('');
  854. remakeChecklistData(gclGatherData);
  855. });
  856. // 显示有调差工料清单
  857. // $('#show_material_gcl').click(function () {
  858. // if ($(this).is(':checked')) {
  859. // const hadMaterialGclGatherData = [];
  860. // const hadGclIdList = [];
  861. // for (const ml of materialListData) {
  862. // if (hadGclIdList.indexOf(ml.gcl_id) === -1) {
  863. // hadGclIdList.push(ml.gcl_id);
  864. // }
  865. // }
  866. // for (const gcl of gclGatherData) {
  867. // for (const index in gcl.leafXmjs) {
  868. // const gcl_id = gcl.leafXmjs[index].gcl_id;
  869. // if (hadGclIdList.indexOf(gcl_id) !== -1) {
  870. // hadMaterialGclGatherData.push(gcl);
  871. // break;
  872. // }
  873. // }
  874. // }
  875. // gclGatherData = hadMaterialGclGatherData;
  876. // } else {
  877. // gclGatherModel.loadLedgerData(ledger, curLedgerData);
  878. // gclGatherModel.loadPosData(pos, curPosData);
  879. // gclGatherData = gclGatherModel.gatherGclData().filter(item => {
  880. // return item.qc_qty || item.contract_qty
  881. // });
  882. // }
  883. // SpreadJsObj.loadSheetData(ledgerSpread.getActiveSheet(), SpreadJsObj.DataType.Data, gclGatherData);
  884. // loadLeafXmjData(0);
  885. // loadMaterialData(0, 0);
  886. // SpreadJsObj.resetTopAndSelect(ledgerSpread.getActiveSheet());
  887. // SpreadJsObj.resetTopAndSelect(leafXmjSpread.getActiveSheet());
  888. // SpreadJsObj.resetTopAndSelect(materialSpread.getActiveSheet());
  889. // });
  890. $.subMenu({
  891. menu: '#sub-menu', miniMenu: '#sub-mini-menu', miniMenuList: '#mini-menu-list',
  892. toMenu: '#to-menu', toMiniMenu: '#to-mini-menu',
  893. key: 'menu.1.0.0',
  894. miniHint: '#sub-mini-hint', hintKey: 'menu.hint.1.0.1',
  895. callback: function (info) {
  896. if (info.mini) {
  897. $('.panel-title').addClass('fluid');
  898. $('#sub-menu').removeClass('panel-sidebar');
  899. } else {
  900. $('.panel-title').removeClass('fluid');
  901. $('#sub-menu').addClass('panel-sidebar');
  902. }
  903. autoFlashHeight();
  904. ledgerSpread.refresh();
  905. materialSpread.refresh();
  906. }
  907. });
  908. $.divResizer({
  909. select: '#right-spr',
  910. callback: function () {
  911. ledgerSpread.refresh();
  912. materialSpread.refresh();
  913. const width = (($('#right-view').width()/$('#right-view').parent('div').width())*100).toFixed();
  914. setLocalCache('material_checklist_' + materialID, width);
  915. }
  916. });
  917. // 展开收起工料并浏览器记住本期展开收起
  918. $('a', '.right-nav').bind('click', function () {
  919. const tab = $(this), tabPanel = $(tab.attr('content'));
  920. if (!tab.hasClass('active')) {
  921. $('a', '.side-menu').removeClass('active');
  922. $('.tab-content .tab-select-show').removeClass('active');
  923. tab.addClass('active');
  924. tabPanel.addClass('active');
  925. showSideTools(tab.hasClass('active'));
  926. if (tab.attr('content') === '#material-tab') {
  927. const width = (($('#right-view').width()/$('#right-view').parent('div').width())*100).toFixed();
  928. setLocalCache('material_checklist_' + materialID, width);
  929. }
  930. } else {
  931. removeLocalCache('material_checklist_' + materialID);
  932. tab.removeClass('active');
  933. tabPanel.removeClass('active');
  934. showSideTools(tab.hasClass('active'));
  935. }
  936. ledgerSpread.refresh();
  937. materialSpread.refresh();
  938. });
  939. // 根据浏览器记录展开收起
  940. if (getLocalCache('material_checklist_' + materialID)) {
  941. const tab = $('.right-nav a[content="#material-tab"]'), tabPanel = $(tab.attr('content'));
  942. $('a', '.side-menu').removeClass('active');
  943. $('.tab-content .tab-select-show').removeClass('active');
  944. tab.addClass('active');
  945. tabPanel.addClass('active');
  946. $('#right-view').width(getLocalCache('material_checklist_' + materialID) + '%');
  947. showSideTools(tab.hasClass('active'));
  948. ledgerSpread.refresh();
  949. materialSpread.refresh();
  950. }
  951. });