material_checklist.js 45 KB

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