material.js 90 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804
  1. 'use strict';
  2. /**
  3. * 材料调差 - 调差工料
  4. *
  5. * @author Mai
  6. * @date 2019/1/16
  7. * @version
  8. */
  9. function loadUpdateMaterials(newMaterial, fields) {
  10. const newMaterials = newMaterial instanceof Array ? newMaterial : [newMaterial];
  11. for (const nm of newMaterials) {
  12. const om = _.find(materialBillsData, {id: nm.id});
  13. for (const prop in nm) {
  14. if (!fields || fields.indexOf(prop) >= 0) {
  15. om[prop] = nm[prop];
  16. }
  17. }
  18. }
  19. }
  20. function DatePickerCellType() {
  21. }
  22. DatePickerCellType.prototype = new GC.Spread.Sheets.CellTypes.Base();
  23. DatePickerCellType.prototype.createEditorElement = function () {
  24. //Create input presenter.
  25. var input = document.createElement("input");
  26. return input;
  27. };
  28. DatePickerCellType.prototype.activateEditor = function (editorContext, cellStyle, cellRect) {
  29. //Initialize input editor.
  30. if (editorContext) {
  31. const $editor = $(editorContext);
  32. GC.Spread.Sheets.CellTypes.Base.prototype.activateEditor.apply(this, arguments);
  33. $editor.datepicker({});
  34. $editor.css("position", "absolute");
  35. $editor.attr("gcUIElement", "gcEditingInput");
  36. $(".ui-datepicker").attr("gcUIElement", "gcEditingInput");
  37. }
  38. }
  39. DatePickerCellType.prototype.deactivateEditor = function (editorContext) {
  40. //Remove input editor when end editor status.
  41. if (editorContext) {
  42. var element = editorContext;
  43. // $(element).datepicker().data('datepicker').hide();
  44. // $(element).datepicker().data('datepicker').destroy();
  45. $(element).datepicker("hide");
  46. $(element).datepicker("destroy");
  47. }
  48. GC.Spread.Sheets.CellTypes.Base.prototype.deactivateEditor.apply(this, arguments)
  49. };
  50. DatePickerCellType.prototype.setEditorValue = function (editor, value) {
  51. //Sync value from Cell value to editor value.
  52. // console.log($(editor), value);
  53. $(editor).datepicker("setDate", value);
  54. // $(editor).datepicker().data('datepicker').selectDate(value ? new Date(value) : new Date());
  55. };
  56. DatePickerCellType.prototype.getEditorValue = function (editor, context) {
  57. // console.log($(editor).datepicker().data('datepicker'), context);
  58. //Sync value from editor value to cell value.
  59. // console.log($(editor).datepicker("getDate"));
  60. // return $(editor).datepicker("getDate");
  61. return $(editor).val();
  62. };
  63. DatePickerCellType.prototype.updateEditor = function (editorContext, cellStyle, cellRect) {
  64. if (editorContext) {
  65. const $editor = $(editorContext);
  66. $editor.css("width", cellRect.width - 1);
  67. $editor.css("height", cellRect.height - 3);
  68. }
  69. };
  70. function resetTpTable() {
  71. $('#tp_set').find('td').eq(1).text(ZhCalc.round(m_tp, materialDecimal.tp));
  72. $('#tp_set').find('td').eq(2).text(ZhCalc.add(pre_tp, ZhCalc.round(m_tp, materialDecimal.tp)));
  73. if (materialTax) {
  74. $('#tax_rate_set').find('td').eq(1).text(ZhCalc.round(m_tax_tp, materialDecimal.tp));
  75. $('#tax_rate_set').find('td').eq(2).text(ZhCalc.add(m_tax_pre_tp, ZhCalc.round(m_tax_tp, materialDecimal.tp)));
  76. } else {
  77. const rate = $('#rateInput').val();
  78. const bqhs = ZhCalc.round(ZhCalc.mul(m_tp, 1+rate/100), materialDecimal.tp);
  79. const jzbqhs = ZhCalc.add(pre_tp_hs, bqhs);
  80. $('#rate_set').find('td').eq(1).text(bqhs !== 0 ? bqhs : '');
  81. $('#rate_set').find('td').eq(2).text(jzbqhs !== 0 ? jzbqhs : '');
  82. }
  83. if (isStageSelf) {
  84. let html = '';
  85. for (const ms of materialStageData) {
  86. const taxHtml = materialTax ? ' <td class="text-center">' + (ms.m_tax_tp !== null ? ms.m_tax_tp : '') + '</td>\n' : '';
  87. html += '<tr><td>第' + ms.order + '期</td><td class="text-center">' + (ms.m_tp !== null ? ms.m_tp : '') + '</td>\n' +
  88. taxHtml +
  89. ' </tr>';
  90. }
  91. const allTaxHtml = materialTax ? ' <td class="text-center">' + m_tax_tp + '</td>\n' : '';
  92. html += '<tr><td>合计</td><td class="text-center">' + m_tp + '</td>\n' +
  93. allTaxHtml +
  94. ' </tr>';
  95. $('#materialStageTable').html(html);
  96. }
  97. }
  98. function getPasteHint (str, row = '') {
  99. let returnObj = str;
  100. if (row) {
  101. returnObj.msg = '工料第' + (row+1) + '行' + str.msg;
  102. }
  103. return returnObj;
  104. }
  105. const is_numeric = (value) => {
  106. if (typeof(value) === 'object') {
  107. return false;
  108. } else {
  109. return !Number.isNaN(Number(value)) && value.toString().trim() !== '';
  110. }
  111. };
  112. $(document).ready(() => {
  113. autoFlashHeight();
  114. const materialSpread = SpreadJsObj.createNewSpread($('#material-spread')[0]);
  115. const materialSpreadSetting = {
  116. emptyRows: 0,
  117. headRows: 2,
  118. headRowHeight: [25, 32],
  119. defaultRowHeight: 21,
  120. headerFont: '12px 微软雅黑',
  121. font: '12px 微软雅黑',
  122. readOnly: readOnly,
  123. };
  124. let materialSpreadSettingCols = [
  125. {title: '调差类型', colSpan: '1', rowSpan: '2', field: 't_type', hAlign: 1, width: 60, formatter: '@', readOnly: 'readOnly.isUsed', cellType: 'customizeCombo', comboItems: materialType.t_type, cellTypeKey: 1},
  126. {title: '编号', colSpan: '1', rowSpan: '2', field: 'code', hAlign: 0, width: 60, formatter: '@', readOnly: 'readOnly.isUsed'},
  127. {title: '名称', colSpan: '1', rowSpan: '2', field: 'name', hAlign: 0, width: 150, formatter: '@', readOnly: 'readOnly.isEdit'},
  128. {title: '单位', colSpan: '1', rowSpan: '2', field: 'unit', hAlign: 1, width: 50, formatter: '@', readOnly: 'readOnly.isEdit'},
  129. {title: '规格', colSpan: '1', rowSpan: '2', field: 'spec', hAlign: 0, width: 150, formatter: '@', readOnly: 'readOnly.isEdit'},
  130. {title: '工料分类', colSpan: '1', rowSpan: '2', field: 'm_type', hAlign: 1, width: 60, readOnly: 'readOnly.isEdit', cellType: 'customizeCombo', comboItems: materialType.m_type, cellTypeKey: 2},
  131. ];
  132. if (materialTax) {
  133. materialSpreadSettingCols.push({title: '税率(%)', colSpan: '1', rowSpan: '2', field: 'm_tax', hAlign: 2, width: 50, type: 'Number', readOnly: editTaxPermission ? 'readOnly.remark' : 'readOnly.isEdit'});
  134. }
  135. materialSpreadSettingCols = _.concat(materialSpreadSettingCols, [
  136. {title: '上涨 幅度(%)', colSpan: '1', rowSpan: '2', field: 'm_up_risk', hAlign: 2, width: 50, type: 'Number', readOnly: 'readOnly.isEdit'},
  137. {title: '下跌 幅度(%)', colSpan: '1', rowSpan: '2', field: 'm_down_risk', hAlign: 2, width: 50, type: 'Number', readOnly: 'readOnly.isEdit'},
  138. {title: '基准价', colSpan: '1', rowSpan: '2', field: 'basic_price', hAlign: 2, width: 60, type: 'Number', readOnly: 'readOnly.isEdit'},
  139. {title: '基准时间', colSpan: '1', rowSpan: '2', field: 'basic_times', hAlign: 0, width: 70, formatter: '@', readOnly: 'readOnly.isEdit'},
  140. {title: '本期信息价|单价', colSpan: '3|1', rowSpan: '1|1', field: 'msg_tp', hAlign: 2, width: 60, type: 'Number', readOnly: 'readOnly.msg_tp'},
  141. {title: '|时间', colSpan: '|1', rowSpan: '|1', field: 'msg_times', hAlign: 0, width: 80, formatter: '@', readOnly: 'readOnly.remark'},
  142. {title: '|单价差', colSpan: '|1', rowSpan: '|1', field: 'msg_spread', hAlign: 2, width: 60, type: 'Number', readOnly: true, getValue: 'getValue.msg_spread'},]
  143. );
  144. if (materialTax) {
  145. materialSpreadSettingCols = _.concat(materialSpreadSettingCols, [
  146. {title: '本期材料调差|有效价差', colSpan: '4|1', rowSpan: '1|1', field: 'm_spread', hAlign: 2, width: 60, type: 'Number', readOnly: true, getValue: 'getValue.m_spread'},
  147. {title: '|本期应耗数量', colSpan: '|1', rowSpan: '|1', field: 'quantity', hAlign: 2, width: 90, type: 'Number', readOnly: true},
  148. {title: '|调差金额', colSpan: '|1', rowSpan: '|1', field: 'm_tp', hAlign: 2, width: 100, type: 'Number', readOnly: true, getValue: 'getValue.m_tp'},
  149. {title: '|调差金额(材料税)', colSpan: '|1', rowSpan: '|1', field: 'm_tax_tp', hAlign: 2, width: 100, type: 'Number', readOnly: true, getValue: 'getValue.m_tax_tp'},
  150. {title: '截止上期|调差金额', colSpan: '2|1', rowSpan: '1|1', field: 'pre_tp', hAlign: 2, width: 100, type: 'Number', readOnly: true},
  151. {title: '|调差金额(材料税)', colSpan: '|1', rowSpan: '|1', field: 'tax_pre_tp', hAlign: 2, width: 100, type: 'Number', readOnly: true},
  152. {title: '备注', colSpan: '1', rowSpan: '2', field: 'remark', hAlign: 0, width: 50, formatter: '@', cellType: 'ellipsisAutoTip', readOnly: 'readOnly.remark'},
  153. {title: '是否汇总', colSpan: '1', rowSpan: '2', field: 'is_summary', hAlign: 1, width: 60, cellType: 'checkbox', readOnly: 'readOnly.isEdit'}
  154. ])
  155. } else {
  156. materialSpreadSettingCols = _.concat(materialSpreadSettingCols, [
  157. {title: '本期材料调差|有效价差', colSpan: '3|1', rowSpan: '1|1', field: 'm_spread', hAlign: 2, width: 60, type: 'Number', readOnly: true, getValue: 'getValue.m_spread'},
  158. {title: '|本期应耗数量', colSpan: '|1', rowSpan: '|1', field: 'quantity', hAlign: 2, width: 90, type: 'Number', readOnly: true},
  159. {title: '|调差金额', colSpan: '|1', rowSpan: '|1', field: 'm_tp', hAlign: 2, width: 100, type: 'Number', readOnly: true, getValue: 'getValue.m_tp'},
  160. {title: '截止上期|调差金额', colSpan: '1|1', rowSpan: '1|1', field: 'pre_tp', hAlign: 2, width: 100, type: 'Number', readOnly: true},
  161. {title: '备注', colSpan: '1', rowSpan: '2', field: 'remark', hAlign: 0, width: 50, formatter: '@', cellType: 'ellipsisAutoTip', readOnly: 'readOnly.remark'},
  162. {title: '是否汇总', colSpan: '1', rowSpan: '2', field: 'is_summary', hAlign: 1, width: 60, cellType: 'checkbox', readOnly: 'readOnly.isEdit'}
  163. ])
  164. }
  165. materialSpreadSetting.cols = materialSpreadSettingCols;
  166. const spCol = _.find(materialSpreadSetting.cols, {field: 'quantity'});
  167. spCol.readOnly = true;
  168. spCol.cellType = 'activeImageBtn';
  169. spCol.normalImg = '#ellipsis-icon';
  170. spCol.indent = 5;
  171. spCol.showImage = function (data) {
  172. // console.log(data, data.t_type);
  173. // return !readOnly && data.t_type === 2 && data.mid === materialID;
  174. return data.t_type === 2;
  175. };
  176. materialSpreadSetting.imageClick = function (data) {
  177. if (data.t_type === 2) {
  178. $('#bcyy').modal('show');
  179. $('#materialbillsId').val(data.id);
  180. $('#expr').val(data.expr);
  181. if (!readOnly) {
  182. $('#expr').attr('readOnly', false);
  183. $('#expr_btn').show();
  184. $('#expr_select').show();
  185. } else {
  186. $('#expr').attr('readOnly', true);
  187. $('#expr_btn').hide();
  188. $('#expr_select').hide();
  189. }
  190. }
  191. };
  192. const materialBase = {
  193. isUsed: function (data) {
  194. if (data.t_type === 2) {
  195. return data.mid === materialID;
  196. } else {
  197. const mlInfo = _.find(materialListData, { mb_id: data.id });
  198. return data.mid === materialID && mlInfo === undefined;
  199. }
  200. },
  201. isEdit: function (data) {
  202. // if (data.t_type === 2) {
  203. // return data.mid === materialID;
  204. // } else {
  205. // const mlInfo = _.find(materialListData, { mb_id: data.id });
  206. // return data.mid === materialID && mlInfo === undefined;
  207. // }
  208. return data.mid === materialID;
  209. },
  210. // isStage: function (data) {
  211. // return data.mid === materialID;
  212. // }
  213. }
  214. const materialCol = {
  215. getValue: {
  216. msg_spread: function (data) {
  217. return ZhCalc.round(ZhCalc.sub(data.msg_tp, data.basic_price), materialDecimal.up);
  218. },
  219. m_spread : function (data) {
  220. const msg_spread = materialCol.getValue.msg_spread(data);
  221. const cor = msg_spread >= 0 ? ZhCalc.mul(data.basic_price, ZhCalc.div(data.m_up_risk, 100)) : ZhCalc.mul(data.basic_price, ZhCalc.div(data.m_down_risk, 100));
  222. return Math.abs(msg_spread) > Math.abs(cor) ? (msg_spread > 0 ? ZhCalc.round(ZhCalc.sub(msg_spread, cor), materialDecimal.up) : ZhCalc.round(ZhCalc.add(msg_spread, cor), materialDecimal.up)) : 0;
  223. },
  224. m_tp: function (data) {
  225. return ZhCalc.round(ZhCalc.mul(materialCol.getValue.m_spread(data), data.quantity), materialDecimal.tp);
  226. },
  227. m_tax_tp: function (data) {
  228. const m_tp = ZhCalc.round(ZhCalc.mul(materialCol.getValue.m_spread(data), data.quantity), materialDecimal.tp);
  229. return data.m_tax ? ZhCalc.round(ZhCalc.mul(m_tp, (1+ZhCalc.div(data.m_tax, 100))), materialDecimal.tp) : m_tp;
  230. }
  231. },
  232. readOnly: {
  233. isEdit: function (data) {
  234. return !(!readOnly && materialBase.isEdit(data));
  235. },
  236. isUsed: function (data) {
  237. return !(!readOnly && materialBase.isUsed(data));
  238. },
  239. remark: function () {
  240. return readOnly;
  241. },
  242. msg_tp: function () {
  243. return !(!readOnly && months.length === 0);
  244. }
  245. },
  246. };
  247. const needUpdateArray = ['quantity', 'expr', 'msg_tp', 'msg_times', 'msg_spread', 'm_spread', 'm_tp', 'm_tax_tp', 'is_summary', 'remark'];
  248. const materialSpreadObj = {
  249. getMaterialBillsData: function () {
  250. if (isStageSelf) {
  251. // 当前选中nav id
  252. const ms_id = $('#myTab').find('.active').data('msid');
  253. const msbList = _.filter(materialStageBillsData, { ms_id });
  254. for (const mb of materialBillsData) {
  255. mb.ms_id = ms_id;
  256. const msbInfo = _.find(msbList, { mb_id: mb.mb_id ? mb.mb_id : mb.id });
  257. if (msbInfo) {
  258. for (const nu of needUpdateArray) {
  259. mb[nu] = msbInfo[nu];
  260. }
  261. }
  262. }
  263. }
  264. return materialBillsData;
  265. },
  266. updateOneMaterialBill: function (data) {
  267. const ms_id = $('#myTab').find('.active').data('msid');
  268. const msbInfo = _.find(materialStageBillsData, { ms_id, mb_id: data.id });
  269. console.log(msbInfo);
  270. data.ms_id = ms_id;
  271. for (const nu of needUpdateArray) {
  272. data[nu] = msbInfo[nu];
  273. }
  274. return data;
  275. },
  276. updateMaterialData: function (datas) {
  277. if (datas.stageData && datas.stageData.length > 0) {
  278. if (datas.stageData.length === materialStageData.length) {
  279. // 全体替换
  280. materialStageData = datas.stageData;
  281. } else {
  282. // 替换对应的stage数据
  283. for (const s of datas.stageData) {
  284. const index = _.findIndex(materialStageData, { id: s.id });
  285. materialStageData.splice(index, 1, s);
  286. }
  287. }
  288. }
  289. if (datas.pushStageBillsData && datas.pushStageBillsData.length > 0) {
  290. materialStageBillsData = _.concat(materialStageBillsData, datas.pushStageBillsData);
  291. }
  292. if (datas.removeStageBillsData && datas.removeStageBillsData.length > 0) {
  293. materialStageBillsData = _.remove(materialStageBillsData, function (item) {
  294. return _.findIndex(datas.removeStageBillsData, { id: item.mb_id }) === -1
  295. })
  296. }
  297. if (datas.stageBillsData && datas.stageBillsData.length > 0) {
  298. if (datas.stageBillsData.length === materialStageBillsData.length) {
  299. // 全体替换
  300. materialStageBillsData = datas.stageBillsData;
  301. } else {
  302. // 替换对应的stage数据
  303. for (const s of datas.stageBillsData) {
  304. const index = _.findIndex(materialStageBillsData, { id: s.id });
  305. materialStageBillsData.splice(index, 1, s);
  306. }
  307. }
  308. }
  309. if (datas.billsData && datas.billsData.length > 0) {
  310. if (datas.billsData.length === materialBillsData.length) {
  311. // 全体替换
  312. materialBillsData = datas.billsData;
  313. materialSpreadObj.getMaterialBillsData();
  314. } else {
  315. for (const b of datas.billsData) {
  316. const index = _.findIndex(materialBillsData, {id: b.id});
  317. materialBillsData.splice(index, 1, b);
  318. materialSpreadObj.updateOneMaterialBill(materialBillsData[index]);
  319. }
  320. }
  321. }
  322. },
  323. materialSheetReset: function (redo = false) {
  324. let newMaterialBillsData = _.cloneDeep(materialBillsData);
  325. if($('#bills0_list').is(':checked')) {
  326. newMaterialBillsData = _.filter(materialBillsData, function (item) {
  327. return item.quantity !== null && item.quantity !== 0;
  328. });
  329. if (redo) {
  330. SpreadJsObj.initSpreadSettingEvents(materialSpreadSetting, materialCol);
  331. SpreadJsObj.initSheet(materialSpread.getActiveSheet(), materialSpreadSetting);
  332. }
  333. }
  334. console.log(newMaterialBillsData);
  335. SpreadJsObj.loadSheetData(materialSpread.getActiveSheet(), SpreadJsObj.DataType.Data, newMaterialBillsData);
  336. materialSpreadObj.refreshActn();
  337. },
  338. refreshActn: function (rowCount = 1) {
  339. const setObjEnable = function (obj, enable) {
  340. if (enable) {
  341. obj.removeClass('disabled');
  342. } else {
  343. obj.addClass('disabled');
  344. }
  345. };
  346. const sheet = materialSpread.getActiveSheet();
  347. const select = SpreadJsObj.getSelectObject(sheet);
  348. const selection = sheet.getSelections();
  349. const sel = selection ? selection[0] : sheet.getSelections()[0];
  350. const row = sel ? sel.row : -1;
  351. const first = materialBillsData[row];
  352. let last = first;
  353. if (sel.rowCount > 1 && first) {
  354. for (let r = 1; r < sel.rowCount; r++) {
  355. const rNode = materialBillsData[sel.row + r];
  356. if (!rNode) break;
  357. last = rNode;
  358. }
  359. }
  360. const preNode = materialBillsData[row - 1];
  361. // 还需判断是否已被调差清单调用
  362. setObjEnable($('#del'), !readOnly && select && materialBase.isUsed(select) && rowCount === 1);
  363. setObjEnable($('#up-move'), !readOnly && first && preNode && materialBillsData.indexOf(last) > 0 && sel.row + sel.rowCount <= materialBillsData.length && !$('#bills0_list').is(':checked'));
  364. setObjEnable($('#down-move'), !readOnly && first && materialBillsData.indexOf(last) < materialBillsData.length - 1 && sel.row + sel.rowCount <= materialBillsData.length && !$('#bills0_list').is(':checked'));
  365. },
  366. add: function () {
  367. const sheet = materialSpread.getActiveSheet();
  368. postData(window.location.pathname + '/save', {type: 'add'}, function (result) {
  369. if (result) {
  370. materialBillsData.push(result.info);
  371. if (isStageSelf) {
  372. materialSpreadObj.updateMaterialData(result);
  373. materialSpreadObj.getMaterialBillsData();
  374. }
  375. // sheet.addRows(materialBillsData.length - 1, 1);
  376. // SpreadJsObj.reLoadRowData(sheet, materialBillsData.length - 1);
  377. let newMaterialBillsData = materialBillsData;
  378. if($('#bills0_list').is(':checked')) {
  379. newMaterialBillsData = _.filter(materialBillsData, function (item) {
  380. return item.quantity !== null && item.quantity !== 0;
  381. });
  382. }
  383. SpreadJsObj.loadSheetData(materialSpread.getActiveSheet(), SpreadJsObj.DataType.Data, newMaterialBillsData);
  384. // materialSpreadObj.materialSheetReset();
  385. sheet.setSelection(newMaterialBillsData.length - 1, 0, 1, 1);
  386. materialSpreadObj.refreshActn();
  387. // 月信息价需要同时添加空白的list
  388. if (months.length > 0) {
  389. const one_month ={ mb_id: result.info.id, code: '', name: null, origin: null, unit: null, order: result.info.order };
  390. for (const m of months) {
  391. one_month[m] = null;
  392. }
  393. monthsList.push(one_month);
  394. // const newMonthsList = _.filter(monthsList, function (item) {
  395. // return _.findIndex(newMaterialBillsData, { code: item.code }) !== -1;
  396. // });
  397. // materialMonthSpread.getActiveSheet().addRows(newMonthsList.length - 1, 1);
  398. // SpreadJsObj.reLoadRowData(materialMonthSpread.getActiveSheet(), newMonthsList.length - 1);
  399. monthFunGather.monthsListSet();
  400. // materialMonthSpreadObj.monthsListSet();
  401. }
  402. }
  403. });
  404. },
  405. del: function () {
  406. const sheet = materialSpread.getActiveSheet();
  407. const select = SpreadJsObj.getSelectObject(sheet);
  408. const sel = sheet.getSelections();
  409. postData(window.location.pathname + '/save', {type: 'del', id: select.id}, function (result) {
  410. m_tp = result.m_tp;
  411. if (materialTax) {
  412. m_tax_tp = result.m_tax_tp;
  413. }
  414. const index = _.findIndex(materialBillsData, { id: select.id });
  415. materialBillsData.splice(index, 1);
  416. if (isStageSelf) {
  417. result.removeStageBillsData = [{ id: select.id }];
  418. materialSpreadObj.updateMaterialData(result);
  419. materialSpreadObj.getMaterialBillsData();
  420. }
  421. let newMaterialBillsData = materialBillsData;
  422. let newIndex = index;
  423. if($('#bills0_list').is(':checked')) {
  424. newMaterialBillsData = _.filter(materialBillsData, function (item) {
  425. return item.quantity !== null && item.quantity !== 0;
  426. });
  427. newIndex = _.findIndex(newMaterialBillsData, { id: select.id });
  428. }
  429. sheet.deleteRows(newIndex, 1);
  430. resetTpTable();
  431. // SpreadJsObj.reLoadSheetData(materialSpread.getActiveSheet());
  432. sheet.setSelection(newIndex > 0 ? newIndex - 1 : 0, sel.length > 0 ? sel[0].col : 0, 1, 1);
  433. materialSpreadObj.refreshActn();
  434. // 月信息价需要同时删除
  435. if (months.length > 0) {
  436. monthsList.splice(index, 1);
  437. materialMonthSpread.getActiveSheet().deleteRows(newIndex, 1);
  438. // SpreadJsObj.reLoadSheetData(materialMonthSpread.getActiveSheet());
  439. }
  440. });
  441. },
  442. upMove: function () {
  443. if($('#bills0_list').is(':checked')) {
  444. return;
  445. }
  446. const data = {
  447. type: 'changeOrder',
  448. postData: [],
  449. };
  450. const sheet = materialSpread.getActiveSheet();
  451. const selection = sheet.getSelections();
  452. const row = selection[0].row, count = selection[0].rowCount;
  453. const first = sheet.zh_data[row];
  454. if (!first) {
  455. materialSpreadObj.refreshActn();
  456. return false;
  457. }
  458. const pre = sheet.zh_data[row - 1], preUpdate = {id: pre.id};
  459. for (let iRow = 0; iRow < count; iRow++) {
  460. const posData = sheet.zh_data[iRow + row];
  461. if (posData) {
  462. data.postData.push({id: posData.id, order: sheet.zh_data[iRow + row - 1].order});
  463. preUpdate.order = posData.order;
  464. }
  465. }
  466. console.log(monthsList);
  467. data.postData.push(preUpdate);
  468. console.log(data);
  469. if (data.postData.length > 0) {
  470. postData(window.location.pathname + '/save', data, function () {
  471. _.forEach(data.postData, function (item) {
  472. const cl = _.find(materialBillsData, { id: item.id });
  473. cl.order = item.order;
  474. });
  475. materialBillsData.sort(function (a, b) {
  476. return a.order - b.order
  477. });
  478. SpreadJsObj.reLoadSheetData(sheet);
  479. const sel = selection[0];
  480. if (sel) {
  481. sheet.setSelection(sheet.zh_data.indexOf(first), sel.col, sel.rowCount, sel.colCount);
  482. }
  483. if (months.length > 0) {
  484. _.forEach(data.postData, function (item) {
  485. const cl = _.find(monthsList, { mb_id: item.id });
  486. cl.order = item.order;
  487. });
  488. monthsList.sort(function (a, b) {
  489. return a.order - b.order
  490. });
  491. const materialMonthSheet = materialMonthSpread.getActiveSheet();
  492. SpreadJsObj.reLoadSheetData(materialMonthSheet);
  493. }
  494. materialSpreadObj.refreshActn();
  495. });
  496. }
  497. },
  498. downMove: function () {
  499. if($('#bills0_list').is(':checked')) {
  500. return;
  501. }
  502. const data = {
  503. type: 'changeOrder',
  504. postData: [],
  505. };
  506. const sheet = materialSpread.getActiveSheet();
  507. const selection = sheet.getSelections();
  508. const row = selection[0].row, count = selection[0].rowCount;
  509. const first = sheet.zh_data[row];
  510. if (!first) {
  511. materialSpreadObj.refreshActn();
  512. return false;
  513. }
  514. const next = sheet.zh_data[row + count], nextUpdate = {id: next.id};
  515. for (let iRow = count - 1; iRow >= 0; iRow--) {
  516. const posData = sheet.zh_data[iRow + row];
  517. if (posData) {
  518. data.postData.push({id: posData.id, order: sheet.zh_data[iRow + row + 1].order});
  519. nextUpdate.order = posData.order;
  520. }
  521. }
  522. data.postData.push(nextUpdate);
  523. console.log(data);
  524. if (data.postData.length > 0) {
  525. postData(window.location.pathname + '/save', data, function () {
  526. _.forEach(data.postData, function (item) {
  527. const cl = _.find(materialBillsData, { id: item.id });
  528. cl.order = item.order;
  529. });
  530. materialBillsData.sort(function (a, b) {
  531. return a.order - b.order
  532. });
  533. SpreadJsObj.reLoadSheetData(sheet);
  534. const sel = selection[0];
  535. if (sel) {
  536. sheet.setSelection(sheet.zh_data.indexOf(first), sel.col, sel.rowCount, sel.colCount);
  537. }
  538. if (months.length > 0) {
  539. _.forEach(data.postData, function (item) {
  540. const cl = _.find(monthsList, { mb_id: item.id });
  541. cl.order = item.order;
  542. });
  543. monthsList.sort(function (a, b) {
  544. return a.order - b.order
  545. });
  546. const materialMonthSheet = materialMonthSpread.getActiveSheet();
  547. SpreadJsObj.reLoadSheetData(materialMonthSheet);
  548. }
  549. materialSpreadObj.refreshActn();
  550. });
  551. }
  552. },
  553. selectionChanged: function (e, info) {
  554. const sel = info.sheet.getSelections()[0];
  555. const col = info.sheet.zh_setting.cols[sel.col];
  556. materialSpreadObj.refreshActn(sel.rowCount);
  557. const data = SpreadJsObj.getSelectObject(info.sheet);
  558. materialSpreadObj.setReadOnly(true);
  559. // console.log(data);
  560. },
  561. editEnded: function (e, info) {
  562. if (info.sheet.zh_setting) {
  563. const select = SpreadJsObj.getSelectObject(info.sheet);
  564. const col = info.sheet.zh_setting.cols[info.col];
  565. if (col.field === 'is_summary') {
  566. return;
  567. }
  568. // 未改变值则不提交
  569. let validText = is_numeric(info.editingText) ? parseFloat(info.editingText) : (info.editingText ? trimInvalidChar(info.editingText) : null);
  570. const orgValue = select[col.field];
  571. if (orgValue == validText || ((!orgValue || orgValue === '') && (validText === ''))) {
  572. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  573. return;
  574. }
  575. // 判断部分值是否输入的是数字判断和数据计算
  576. if (col.field === 'basic_price') {
  577. if (isNaN(validText)) {
  578. toastr.error('不能输入其它非数字类型字符');
  579. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  580. return;
  581. }
  582. let num = parseFloat(validText);
  583. const reg = materialDecimal.up ? new RegExp("^\\d+(\\.\\d{1,"+ materialDecimal.up +"})?$") : new RegExp("^\\d+?$");
  584. if (validText !== null && (num < 0 || !reg.test(num))) {
  585. toastr.warning('已保留'+ materialDecimal.up +'位小数');
  586. validText = ZhCalc.round(num, materialDecimal.up);
  587. }
  588. }
  589. if (col.field === 'msg_tp') {
  590. if (isNaN(validText)) {
  591. toastr.error('不能输入其它非数字类型字符');
  592. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  593. return;
  594. }
  595. const num = parseFloat(validText);
  596. const reg = materialDecimal.up ? new RegExp("^\\d+(\\.\\d{1,"+ materialDecimal.up +"})?$") : new RegExp("^\\d+?$");
  597. if (validText !== null && (num < 0 || !reg.test(num))) {
  598. toastr.warning('已保留'+ materialDecimal.up +'位小数');
  599. validText = ZhCalc.round(num, materialDecimal.up);
  600. }
  601. }
  602. if (col.field === 'm_up_risk' || col.field === 'm_down_risk' || col.field === 'm_tax') {
  603. // 只能输入正整数
  604. if (isNaN(validText)) {
  605. toastr.error('不能输入其它非数字类型字符');
  606. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  607. return;
  608. }
  609. const num = parseFloat(validText);
  610. if (validText !== null && (num < 0 || num > 100 || !/^\d+$/.test(num))) {
  611. toastr.error('只能输入0-100的正整数');
  612. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  613. return;
  614. }
  615. }
  616. if (col.field === 't_type') {
  617. if (validText === 1) {
  618. select.quantity = null;
  619. select.expr = null;
  620. select.m_tp = null;
  621. }
  622. }
  623. select[col.field] = validText;
  624. select.msg_spread = materialCol.getValue.msg_spread(select);
  625. select.m_spread = materialCol.getValue.m_spread(select);
  626. select.m_tp = materialCol.getValue.m_tp(select);
  627. select.m_tax_tp = materialCol.getValue.m_tax_tp(select);
  628. delete select.waitingLoading;
  629. // console.log(select);
  630. // 更新至服务器
  631. postData(window.location.pathname + '/save', { type:'update', updateData: select, ms_id: $('#myTab').find('.active').data('msid') || null }, function (result) {
  632. m_tp = result.m_tp;
  633. if (materialTax) {
  634. m_tax_tp = result.m_tax_tp;
  635. }
  636. if (isStageSelf) {
  637. materialSpreadObj.updateMaterialData(result);
  638. }
  639. resetTpTable();
  640. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  641. // 判断如果是更改了编号,名称,单位,月信息价需要跟着改变值
  642. if (months.length > 0 && (col.field === 'code' || col.field === 'name' || col.field === 'unit')) {
  643. const index = _.findIndex(materialBillsData, { id: select.id });
  644. monthsList[index][col.field] = validText;
  645. monthFunGather.monthsListSet();
  646. }
  647. }, function () {
  648. select[col.field] = orgValue;
  649. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  650. });
  651. }
  652. },
  653. buttonClicked: function (e, info) {
  654. if (info.sheet.zh_setting) {
  655. const select = SpreadJsObj.getSelectObject(info.sheet);
  656. const col = info.sheet.zh_setting.cols[info.col];
  657. if (materialCol.readOnly.isEdit(select)) {
  658. return;
  659. }
  660. if (col.field === 'is_summary') {
  661. if (info.sheet.isEditing()) {
  662. info.sheet.endEdit(true);
  663. }
  664. select.is_summary = info.sheet.getValue(info.row, info.col) ? 1 : 0;
  665. delete select.waitingLoading;
  666. // 更新至服务器
  667. postData(window.location.pathname + '/save', { type:'update', updateData: select, ms_id: $('#myTab').find('.active').data('msid') || null }, function (result) {
  668. m_tp = result.m_tp;
  669. if (materialTax) {
  670. m_tax_tp = result.m_tax_tp;
  671. }
  672. if (isStageSelf) {
  673. materialSpreadObj.updateMaterialData(result);
  674. }
  675. resetTpTable();
  676. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  677. }, function () {
  678. select.is_summary = info.sheet.getValue(info.row, info.col) ? 0 : 1;
  679. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  680. });
  681. }
  682. }
  683. },
  684. deletePress: function (sheet) {
  685. return;
  686. },
  687. clipboardPasted(e, info) {
  688. const hint = {
  689. cellError: {type: 'error', msg: '粘贴内容超出了表格范围'},
  690. codeEmpty: {type: 'error', msg: '请先输入编号,并且不超过15个字符'},
  691. codeUsed: {type: 'error', msg: '该编号已存在,请重新输入。'},
  692. codeError: {type: 'error', msg: '编号为纯数字时,不能为小数'},
  693. numberExpr: {type: 'error', msg: '不能粘贴其它非数字类型字符'},
  694. riskCan: {type: 'error', msg: '只能粘贴0-100的正整数'},
  695. numberCan: {type: 'warning', msg: '已保留'+ materialDecimal.up +'位小数'},
  696. };
  697. const range = info.cellRange;
  698. const sortData = info.sheet.zh_data || [];
  699. if (info.cellRange.row + info.cellRange.rowCount > sortData.length) {
  700. toastMessageUniq(hint.cellError);
  701. // SpreadJsObj.loadSheetData(materialSpread.getActiveSheet(), SpreadJsObj.DataType.Data, materialBillsData);
  702. SpreadJsObj.reLoadSheetHeader(materialSpread.getActiveSheet());
  703. SpreadJsObj.reLoadSheetData(materialSpread.getActiveSheet());
  704. return;
  705. }
  706. if (sortData.length > 0 && range.col + range.colCount > 18) {
  707. toastMessageUniq(hint.cellError);
  708. SpreadJsObj.reLoadSheetHeader(materialSpread.getActiveSheet());
  709. SpreadJsObj.reLoadSheetData(materialSpread.getActiveSheet());
  710. return;
  711. }
  712. const data = [];
  713. // const rowData = [];
  714. for (let iRow = 0; iRow < range.rowCount; iRow++) {
  715. let bPaste = true;
  716. const curRow = range.row + iRow;
  717. // const materialData = JSON.parse(JSON.stringify(sortData[curRow]));
  718. const materialData = { id: sortData[curRow].id };
  719. const hintRow = range.rowCount > 1 ? curRow : '';
  720. let sameCol = 0;
  721. for (let iCol = 0; iCol < range.colCount; iCol++) {
  722. const curCol = range.col + iCol;
  723. const colSetting = info.sheet.zh_setting.cols[curCol];
  724. if (!colSetting) continue;
  725. let validText = info.sheet.getText(curRow, curCol);
  726. validText = is_numeric(validText) ? parseFloat(validText) : (validText ? trimInvalidChar(validText) : null);
  727. const orgValue = sortData[curRow][colSetting.field];
  728. if (orgValue == validText || ((!orgValue || orgValue === '') && (validText === ''))) {
  729. sameCol++;
  730. if (range.colCount === sameCol) {
  731. bPaste = false;
  732. }
  733. continue;
  734. }
  735. if (colSetting.field === 'code' && (validText === '' || validText.length > 15)) {
  736. toastMessageUniq(getPasteHint(hint.codeEmpty, hintRow));
  737. bPaste = false;
  738. continue;
  739. }
  740. if (colSetting.field === 'code' && validText.toString().indexOf('.') !== -1) {
  741. toastMessageUniq(getPasteHint(hint.codeError, hintRow));
  742. bPaste = false;
  743. continue;
  744. }
  745. if (colSetting.field === 'code' && _.find(sortData, { code: validText.toString() })) {
  746. toastMessageUniq(getPasteHint(hint.codeUsed, hintRow));
  747. bPaste = false;
  748. continue;
  749. }
  750. if (colSetting.field === 't_type' || colSetting.field === 'm_type') {
  751. continue;
  752. }
  753. if (colSetting.field === 'basic_price' || colSetting.field === 'msg_tp'
  754. || colSetting.field === 'm_up_risk' || colSetting.field === 'm_down_risk') {
  755. if (isNaN(validText)) {
  756. toastMessageUniq(getPasteHint(hint.numberExpr, hintRow));
  757. bPaste = false;
  758. continue;
  759. }
  760. const num = parseFloat(validText);
  761. if (colSetting.field === 'basic_price' || colSetting.field === 'msg_tp') {
  762. const reg = materialDecimal.up ? new RegExp("^\\d+(\\.\\d{1,"+ materialDecimal.up +"})?$") : new RegExp("^\\d+?$");
  763. if (validText !== null && (num < 0 || !reg.test(num))) {
  764. toastMessageUniq(getPasteHint(hint.numberCan, hintRow));
  765. validText = ZhCalc.round(num, materialDecimal.up);
  766. }
  767. } else if (colSetting.field === 'm_up_risk' || colSetting.field === 'm_down_risk' || colSetting.field === 'm_tax') {
  768. if (validText !== null && (num < 0 || num > 100 || !/^\d+$/.test(num))) {
  769. toastMessageUniq(getPasteHint(hint.riskCan, hintRow));
  770. bPaste = false;
  771. continue;
  772. }
  773. }
  774. }
  775. materialData[colSetting.field] = validText;
  776. sortData[curRow][colSetting.field] = validText;
  777. }
  778. if (materialData.code === '' || materialData.code === null) {
  779. toastMessageUniq(getPasteHint(hint.codeEmpty, hintRow));
  780. bPaste = false;
  781. }
  782. if (bPaste) {
  783. materialData.msg_spread = materialCol.getValue.msg_spread(sortData[curRow]);
  784. materialData.m_spread = materialCol.getValue.m_spread(sortData[curRow]);
  785. materialData.m_tp = materialCol.getValue.m_tp(sortData[curRow]);
  786. materialData.m_tax_tp = materialCol.getValue.m_tax_tp(sortData[curRow]);
  787. data.push(materialData);
  788. // rowData.push(curRow);
  789. } else {
  790. SpreadJsObj.reLoadRowData(info.sheet, curRow);
  791. }
  792. }
  793. if (data.length === 0) {
  794. SpreadJsObj.reLoadRowData(info.sheet, info.cellRange.row, info.cellRange.rowCount);
  795. return;
  796. }
  797. // console.log(data);
  798. // 更新至服务器
  799. postData(window.location.pathname + '/save', { type:'paste', updateData: data, ms_id: $('#myTab').find('.active').data('msid') || null }, function (result) {
  800. materialBillsData = result.info;
  801. m_tp = result.m_tp;
  802. if (materialTax) {
  803. m_tax_tp = result.m_tax_tp;
  804. }
  805. if (isStageSelf) {
  806. materialSpreadObj.updateMaterialData(result);
  807. materialSpreadObj.getMaterialBillsData();
  808. }
  809. // SpreadJsObj.reLoadSheetData(materialSpread.getActiveSheet());
  810. materialSpreadObj.materialSheetReset();
  811. // SpreadJsObj.loadSheetData(materialSpread.getActiveSheet(), SpreadJsObj.DataType.Data, materialBillsData);
  812. // for (const row in rowData) {
  813. // materialBillsData.splice(index, 1, result.info[row]);
  814. // SpreadJsObj.reLoadRowData(info.sheet, row);
  815. // }
  816. if (months.length > 0) {
  817. for (const [i,m] of monthsList.entries()) {
  818. m.code = materialBillsData[i].code;
  819. m.name = materialBillsData[i].name;
  820. m.unit = materialBillsData[i].unit;
  821. m.origin = materialBillsData[i].origin;
  822. }
  823. // SpreadJsObj.loadSheetData(materialMonthSpread.getActiveSheet(), SpreadJsObj.DataType.Data, monthsList);
  824. monthFunGather.monthsListSet();
  825. // SpreadJsObj.reLoadSheetData(materialMonthSpread.getActiveSheet());
  826. }
  827. resetTpTable();
  828. // materialSpreadObj.refreshActn();
  829. }, function () {
  830. // materialSpreadObj.refreshActn();
  831. SpreadJsObj.reLoadRowData(info.sheet, info.cellRange.row, info.cellRange.rowCount);
  832. return;
  833. });
  834. },
  835. setReadOnly: function(readOnly) {
  836. // SpreadJsObj.resetFieldReadOnly(materialSpread.getActiveSheet(), 'msg_spread', 'm_spread', 'm_tp', 'pre_tp', readOnly);
  837. }
  838. };
  839. materialSpread.bind(spreadNS.Events.SelectionChanged, materialSpreadObj.selectionChanged);
  840. materialSpread.bind(spreadNS.Events.ClipboardPasted, materialSpreadObj.clipboardPasted);
  841. SpreadJsObj.addDeleteBind(materialSpread, materialSpreadObj.deletePress);
  842. materialSpread.bind(spreadNS.Events.EditEnded, materialSpreadObj.editEnded);
  843. materialSpread.bind(spreadNS.Events.ButtonClicked, materialSpreadObj.buttonClicked);
  844. const sheet = materialSpread.getActiveSheet();
  845. sheet.suspendPaint();
  846. // materialSpreadObj.refreshActn();
  847. // const basic_range = sheet.getRange(-1, 8, -1, 1);
  848. // basic_range.cellType(new DatePickerCellType());
  849. // basic_range.formatter("yyyy-MM-dd");
  850. // const msg_range = sheet.getRange(-1, 10, -1, 1);
  851. // msg_range.cellType(new DatePickerCellType());
  852. // msg_range.formatter("yyyy-MM-dd");
  853. sheet.resumePaint();
  854. const static_cols = [
  855. {title: '编号', colSpan: '1', rowSpan: '2', field: 'code', hAlign: 0, width: 60, formatter: '@', readOnly: true},
  856. {title: '名称', colSpan: '1', rowSpan: '2', field: 'name', hAlign: 0, width: 100, formatter: '@', readOnly: true},
  857. {title: '单位', colSpan: '1', rowSpan: '2', field: 'unit', hAlign: 1, width: 50, formatter: '@', readOnly: true},
  858. {title: '来源地', colSpan: '1', rowSpan: '2', field: 'origin', hAlign: 0, width: 70, formatter: '@', readOnly: 'readOnly.isEdit'},
  859. {title: '平均单价', colSpan: '1', rowSpan: '2', field: 'average_msg_tp', hAlign: 2, width: 60, type: 'Number', readOnly: true, getValue:'getValue.average_msg_tp'},
  860. ];
  861. // 月信息价方法集合
  862. const monthFunGather = {
  863. _setMonthHtml: function() {
  864. let html = '';
  865. let qihtml = '';
  866. for (const m of months) {
  867. html += '<div class="custom-control custom-checkbox mb-2">\n' +
  868. ' <input type="checkbox" name="del_month" value="' + m + '" class="custom-control-input" id="month_' + m + '">\n' +
  869. ' <label class="custom-control-label" for="month_' + m + '">' + m + '月</label>\n' +
  870. ' </div>';
  871. qihtml += parseInt(m.split('-')[1]) + '月,';
  872. }
  873. if (months.length > 0) {
  874. qihtml = '<span class="mx-2 text-muted">/</span>本期月信息价:' + qihtml;
  875. qihtml = qihtml.substring(0, qihtml.length-1);
  876. }
  877. $('#show_month').html(html);
  878. $('#qi-month').html(qihtml);
  879. },
  880. _monthHeaderSet: function() {
  881. const newMonths = [];
  882. for (const m of months) {
  883. const year = m.split('-')[0];
  884. const month = parseInt(m.split('-')[1]);
  885. let one = _.find(newMonths, { 'year': year });
  886. let oneIndex = _.findIndex(newMonths, { 'year': year });
  887. if (one) {
  888. one.month.push(month);
  889. newMonths.splice(oneIndex, 1, one);
  890. } else {
  891. one = {
  892. year: year,
  893. month: [ month ],
  894. };
  895. newMonths.push(one);
  896. }
  897. }
  898. const pushMonth = [];
  899. for(const mo of newMonths) {
  900. for (let i in mo.month) {
  901. i = parseInt(i);
  902. const newCols = {
  903. title: (i === 0 ? mo.year: '') + '|' + mo.month[i] + '月',
  904. colSpan: (i === 0 ? (mo.month.length === 1 ? '0' : mo.month.length) : '') + '|1',
  905. rowSpan: i === 0 ? '1|1' : '|1',
  906. field: mo.year + '-' + (mo.month[i] < 10 ? '0' + mo.month[i] : mo.month[i]),
  907. hAlign: 2, width: 60, type: '@', readOnly: 'readOnly.isEdit'};
  908. pushMonth.push(newCols);
  909. }
  910. }
  911. return pushMonth;
  912. },
  913. monthSheetReset: function (ml = monthsList) {
  914. const monthCols = monthFunGather._monthHeaderSet();
  915. const newMonthSpreadHeaderCols = static_cols.concat(monthCols);
  916. materialMonthSpreadSetting.cols = newMonthSpreadHeaderCols;
  917. // 表头变化需要重新绘制,不然报错;
  918. materialMonthSpread.getActiveSheet().reset();
  919. SpreadJsObj.initSpreadSettingEvents(materialMonthSpreadSetting, materialMonthCol);
  920. SpreadJsObj.initSheet(materialMonthSpread.getActiveSheet(), materialMonthSpreadSetting);
  921. SpreadJsObj.loadSheetData(materialMonthSpread.getActiveSheet(), SpreadJsObj.DataType.Data, ml);
  922. monthFunGather._setMonthHtml();
  923. },
  924. monthsListSet: function (ml = materialBillsData, checked = true) {
  925. if($('#bills0_list').is(':checked') && checked) {
  926. ml = _.filter(materialBillsData, function (item) {
  927. return item.quantity === null || item.quantity === 0;
  928. });
  929. }
  930. const newMonthsList = _.filter(monthsList, function (item) {
  931. return _.findIndex(ml, { code: item.code }) !== -1;
  932. });
  933. monthFunGather.monthSheetReset(newMonthsList);
  934. }
  935. };
  936. const materialMonthSpread = SpreadJsObj.createNewSpread($('#material-month-spread')[0]);
  937. const materialMonthSpreadSetting = {
  938. cols: static_cols,
  939. emptyRows: 0,
  940. headRows: 2,
  941. headRowHeight: [25, 25],
  942. defaultRowHeight: 21,
  943. headerFont: '12px 微软雅黑',
  944. font: '12px 微软雅黑',
  945. readOnly: readOnly,
  946. };
  947. if (months.length > 0) {
  948. const monthCols = monthFunGather._monthHeaderSet();
  949. const monthSpreadHeaderCols = static_cols.concat(monthCols);
  950. materialMonthSpreadSetting.cols = monthSpreadHeaderCols;
  951. }
  952. const materialMonthCol = {
  953. getValue: {
  954. average_msg_tp: function (data) {
  955. let msg_tp = 0;
  956. let hadnum = 0;
  957. for (const m of months) {
  958. msg_tp += data[m];
  959. if (data[m] !== null && data[m] !== '' && data[m] !== 0) {
  960. hadnum++;
  961. }
  962. }
  963. const average_tp = hadnum !== 0 ? ZhCalc.round(ZhCalc.div(msg_tp, hadnum), materialDecimal.up) : ZhCalc.round(ZhCalc.div(msg_tp, months.length), materialDecimal.up);
  964. return average_tp;
  965. },
  966. },
  967. readOnly: {
  968. isEdit: function (data) {
  969. return readOnly;
  970. },
  971. },
  972. };
  973. materialMonthSpread.getActiveSheet().frozenColumnCount(5);
  974. materialMonthSpread.getActiveSheet().options.frozenlineColor = '#f1f1f1';
  975. SpreadJsObj.initSpreadSettingEvents(materialMonthSpreadSetting, materialMonthCol);
  976. SpreadJsObj.initSheet(materialMonthSpread.getActiveSheet(), materialMonthSpreadSetting);
  977. SpreadJsObj.loadSheetData(materialMonthSpread.getActiveSheet(), SpreadJsObj.DataType.Data, monthsList);
  978. const materialMonthSpreadObj = {
  979. editEnded: function (e, info) {
  980. if (info.sheet.zh_setting) {
  981. const select = SpreadJsObj.getSelectObject(info.sheet);
  982. const col = info.sheet.zh_setting.cols[info.col];
  983. // 未改变值则不提交
  984. let validText = is_numeric(info.editingText) ? parseFloat(info.editingText) : (info.editingText ? trimInvalidChar(info.editingText) : null);
  985. const orgValue = select[col.field];
  986. if (orgValue == validText || ((!orgValue || orgValue === '') && (validText === ''))) {
  987. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  988. return;
  989. }
  990. if (col.field === 'origin') {
  991. select[col.field] = validText;
  992. postData(window.location.pathname + '/month/save', { type:'updateOrigin', updateData: { mb_id: select.mb_id, value: validText } }, function (result) {
  993. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  994. materialBillsData = result.materialBillsData;
  995. materialSpreadObj.materialSheetReset();
  996. // SpreadJsObj.loadSheetData(materialSpread.getActiveSheet(), SpreadJsObj.DataType.Data, materialBillsData);
  997. }, function () {
  998. select[col.field] = orgValue;
  999. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  1000. });
  1001. } else {
  1002. // 判断部分值是否输入的是数字判断和数据计算
  1003. if (isNaN(validText)) {
  1004. toastr.error('不能输入其它非数字类型字符');
  1005. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  1006. return;
  1007. }
  1008. const num = parseFloat(validText);
  1009. const reg = materialDecimal.up ? new RegExp("^\\d+(\\.\\d{1,"+ materialDecimal.up +"})?$") : new RegExp("^\\d+?$");
  1010. if (validText !== null && (num < 0 || !reg.test(num))) {
  1011. toastr.warning('已保留'+ materialDecimal.up +'位小数');
  1012. validText = ZhCalc.round(num, materialDecimal.up);
  1013. }
  1014. select[col.field] = validText;
  1015. // 更新至服务器
  1016. postData(window.location.pathname + '/month/save', { type:'update', updateData: { mb_id: select.mb_id, yearmonth: col.field, value: validText } }, function (result) {
  1017. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  1018. materialBillsData = result.materialBillsData;
  1019. materialSpreadObj.materialSheetReset();
  1020. // SpreadJsObj.loadSheetData(materialSpread.getActiveSheet(), SpreadJsObj.DataType.Data, materialBillsData);
  1021. m_tp = result.m_tp;
  1022. if (materialTax) {
  1023. m_tax_tp = result.m_tax_tp;
  1024. }
  1025. resetTpTable();
  1026. }, function () {
  1027. select[col.field] = orgValue;
  1028. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  1029. });
  1030. }
  1031. }
  1032. },
  1033. deletePress: function (sheet) {
  1034. // if (sheet.zh_setting && sheet.zh_data) {
  1035. // const sel = sheet.getSelections()[0];
  1036. // if (!sel) return;
  1037. //
  1038. // const col = sheet.zh_setting.cols[sel.col];
  1039. // const select = SpreadJsObj.getSelectObject(sheet);
  1040. // const orgValue = select[col.field];
  1041. // if (sel.colCount > 1 || sel.rowCount > 1) {
  1042. // toastr.warning('请勿同时删除多行或多列数据');
  1043. // }
  1044. // console.log(sel, select, col);
  1045. // if (orgValue === null || col.field === 'type' || col.field === 'is_summary') {
  1046. // return;
  1047. // }
  1048. // select[col.field] = null;
  1049. // select.calc_num = materialExponentCol.getValue.calc_num(select);
  1050. // console.log(select);
  1051. // 更新至服务器
  1052. // postData(window.location.pathname + '/save', { type:'update', updateData: select }, function (result) {
  1053. // ex_tp = result.ex_tp;
  1054. // ex_expr = result.ex_expr;
  1055. // resetExTpTable();
  1056. // SpreadJsObj.reLoadRowData(sheet, sel.row);
  1057. // materialExponentData.splice(sel.row, 1, select);
  1058. // }, function () {
  1059. // select[col.field] = orgValue;
  1060. // SpreadJsObj.reLoadRowData(sheet, sel.row);
  1061. // });
  1062. // }
  1063. return;
  1064. },
  1065. clipboardPasted(e, info) {
  1066. const hint = {
  1067. cellError: {type: 'error', msg: '粘贴内容超出了表格范围'},
  1068. numberExpr: {type: 'error', msg: '不能粘贴其它非数字类型字符'},
  1069. numberCan: {type: 'warning', msg: '已保留'+ materialDecimal.up +'位小数'},
  1070. };
  1071. const range = info.cellRange;
  1072. const sortData = info.sheet.zh_data || [];
  1073. if (info.cellRange.row + info.cellRange.rowCount > sortData.length) {
  1074. toastMessageUniq(hint.cellError);
  1075. SpreadJsObj.reLoadSheetHeader(materialMonthSpread.getActiveSheet());
  1076. SpreadJsObj.reLoadSheetData(materialMonthSpread.getActiveSheet());
  1077. return;
  1078. }
  1079. if (sortData.length > 0 && range.col + range.colCount > 5 + months.length) {
  1080. toastMessageUniq(hint.cellError);
  1081. SpreadJsObj.reLoadSheetHeader(materialMonthSpread.getActiveSheet());
  1082. SpreadJsObj.reLoadSheetData(materialMonthSpread.getActiveSheet());
  1083. return;
  1084. }
  1085. const data = [];
  1086. let pasteOrigin = false;
  1087. for (let iRow = 0; iRow < range.rowCount; iRow++) {
  1088. let bPaste = true;
  1089. const curRow = range.row + iRow;
  1090. const materialMonthData = sortData[curRow];
  1091. const hintRow = range.rowCount > 1 ? curRow : '';
  1092. let sameCol = 0;
  1093. for (let iCol = 0; iCol < range.colCount; iCol++) {
  1094. const curCol = range.col + iCol;
  1095. const colSetting = info.sheet.zh_setting.cols[curCol];
  1096. if (!colSetting) continue;
  1097. let validText = info.sheet.getText(curRow, curCol);
  1098. validText = is_numeric(validText) ? parseFloat(validText) : (validText ? trimInvalidChar(validText) : null);
  1099. const orgValue = sortData[curRow][colSetting.field];
  1100. if (orgValue == validText || ((!orgValue || orgValue === '') && (validText === ''))) {
  1101. sameCol++;
  1102. if (range.colCount === sameCol) {
  1103. bPaste = false;
  1104. }
  1105. continue;
  1106. }
  1107. if (colSetting.field === 'origin') {
  1108. pasteOrigin = true;
  1109. } else {
  1110. const num = parseFloat(validText);
  1111. if (isNaN(validText)) {
  1112. toastMessageUniq(getPasteHint(hint.numberExpr, hintRow));
  1113. bPaste = false;
  1114. continue;
  1115. }
  1116. const reg = materialDecimal.up ? new RegExp("^\\d+(\\.\\d{1,"+ materialDecimal.up +"})?$") : new RegExp("^\\d+?$");
  1117. if (validText !== null && (num < 0 || !reg.test(num))) {
  1118. toastMessageUniq(getPasteHint(hint.numberCan, hintRow));
  1119. validText = ZhCalc.round(num, materialDecimal.up);
  1120. }
  1121. }
  1122. materialMonthData[colSetting.field] = validText;
  1123. sortData[curRow][colSetting.field] = validText;
  1124. }
  1125. if (bPaste) {
  1126. data.push(materialMonthData);
  1127. } else {
  1128. SpreadJsObj.reLoadRowData(info.sheet, curRow);
  1129. }
  1130. }
  1131. if (data.length === 0) {
  1132. SpreadJsObj.reLoadRowData(info.sheet, info.cellRange.row, info.cellRange.rowCount);
  1133. return;
  1134. }
  1135. // console.log(data);
  1136. if (pasteOrigin) {
  1137. // 更新至服务器
  1138. postData(window.location.pathname + '/month/save', { type:'pasteOrigin', updateData: data }, function (result) {
  1139. SpreadJsObj.reLoadSheetData(materialMonthSpread.getActiveSheet());
  1140. materialBillsData = result.materialBillsData;
  1141. }, function () {
  1142. SpreadJsObj.reLoadRowData(info.sheet, info.cellRange.row, info.cellRange.rowCount);
  1143. return;
  1144. });
  1145. } else {
  1146. // 更新至服务器
  1147. postData(window.location.pathname + '/month/save', { type:'paste', updateData: data }, function (result) {
  1148. SpreadJsObj.reLoadSheetData(materialMonthSpread.getActiveSheet());
  1149. materialBillsData = result.materialBillsData;
  1150. materialSpreadObj.materialSheetReset();
  1151. // SpreadJsObj.loadSheetData(materialSpread.getActiveSheet(), SpreadJsObj.DataType.Data, materialBillsData);
  1152. m_tp = result.m_tp;
  1153. if (materialTax) {
  1154. m_tax_tp = result.m_tax_tp;
  1155. }
  1156. resetTpTable();
  1157. }, function () {
  1158. SpreadJsObj.reLoadRowData(info.sheet, info.cellRange.row, info.cellRange.rowCount);
  1159. return;
  1160. });
  1161. }
  1162. },
  1163. };
  1164. SpreadJsObj.initSpreadSettingEvents(materialSpreadSetting, materialCol);
  1165. SpreadJsObj.initSheet(materialSpread.getActiveSheet(), materialSpreadSetting);
  1166. materialSpreadObj.getMaterialBillsData();
  1167. SpreadJsObj.loadSheetData(materialSpread.getActiveSheet(), SpreadJsObj.DataType.Data, materialBillsData);
  1168. materialSpreadObj.refreshActn();
  1169. materialMonthSpread.bind(spreadNS.Events.ClipboardPasted, materialMonthSpreadObj.clipboardPasted);
  1170. SpreadJsObj.addDeleteBind(materialMonthSpread, materialMonthSpreadObj.deletePress);
  1171. materialMonthSpread.bind(spreadNS.Events.EditEnded, materialMonthSpreadObj.editEnded);
  1172. // 期切换
  1173. $('#myTab a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
  1174. e.preventDefault();
  1175. showWaitingView();
  1176. setTimeout(function () {
  1177. materialSpreadObj.getMaterialBillsData();
  1178. materialSpreadObj.materialSheetReset(true);
  1179. // SpreadJsObj.reLoadSheetData(materialSpread.getActiveSheet());
  1180. // 消耗量表格更新
  1181. let html = '';
  1182. calcBase = _.find(calcBaseList, { ms_id: $('#myTab').find('.active').data('msid') }).calcBase;
  1183. for (let iBase = 0; iBase < calcBase.length; iBase++) {
  1184. html += '<tr><td>' + calcBase[iBase].name + '</td><td>' + calcBase[iBase].code + '</td><td>' + calcBase[iBase].value + '</td></tr>';
  1185. }
  1186. $('#calcBase').html(html);
  1187. closeWaitingView();
  1188. }, 1000);
  1189. });
  1190. if (!readOnly || editForAudit) {
  1191. $('#expr_select button').on('click', function () {
  1192. const code = $(this).text();
  1193. // $('#expr').val($('#expr').val() + code);
  1194. $('#expr').insertAtCaret(code);
  1195. });
  1196. const ExprObj = {
  1197. _checkExprValid(expr, invalidParam) {
  1198. if (!expr) return [true, null];
  1199. const param = [];
  1200. let num = '', base = '';
  1201. for (let i = 0, iLen = expr.length; i < iLen; i++) {
  1202. if (/^[\d\.%]+/.test(expr[i])) {
  1203. if (base !== '') {
  1204. param.push({type: 'base', value: base});
  1205. base = '';
  1206. }
  1207. num = num + expr[i];
  1208. } else if (/^[a-z]/.test(expr[i])) {
  1209. if (num !== '') {
  1210. param.push({type: 'num', value: num});
  1211. num = '';
  1212. }
  1213. base = base + expr[i];
  1214. } else if (expr[i] === '(') {
  1215. if (num !== '') {
  1216. param.push({type: 'num', value: num});
  1217. num = '';
  1218. }
  1219. if (base !== '') {
  1220. param.push({type: 'base', value: base});
  1221. base = '';
  1222. }
  1223. param.push({type: 'left', value: '('});
  1224. } else if (expr[i] === ')') {
  1225. if (num !== '') {
  1226. param.push({type: 'num', value: num});
  1227. num = '';
  1228. }
  1229. if (base !== '') {
  1230. param.push({type: 'base', value: base});
  1231. base = '';
  1232. }
  1233. param.push({type: 'right', value: ')'});
  1234. } else if (/^[\+\-*\/]/.test(expr[i])) {
  1235. if (num !== '') {
  1236. param.push({type: 'num', value: num});
  1237. num = '';
  1238. }
  1239. if (base !== '') {
  1240. param.push({type: 'base', value: base});
  1241. base = '';
  1242. }
  1243. param.push({type: 'calc', value: expr[i]});
  1244. } else {
  1245. return [false, '输入的表达式含有非法字符: ' + expr[i]];
  1246. }
  1247. }
  1248. if (num !== '') {
  1249. param.push({type: 'num', value: num});
  1250. num = '';
  1251. }
  1252. if (base !== '') {
  1253. param.push({type: 'base', value: base});
  1254. base = '';
  1255. }
  1256. if (param.length === 0) return true;
  1257. if (param.length > 1) {
  1258. if (param[0].value === '-') {
  1259. param[1].value = '-' + param[1];
  1260. }
  1261. param.unshift();
  1262. }
  1263. const iLen = param.length;
  1264. let iLeftCount = 0, iRightCount = 0;
  1265. for (const [i, p] of param.entries()) {
  1266. if (p.type === 'calc') {
  1267. if (i === 0 || i === iLen - 1)
  1268. return [false, '输入的表达式非法:计算符号' + p.value + '前后应有数字或计算基数'];
  1269. }
  1270. if (p.type === 'num') {
  1271. num = p.value.replace('%', '');
  1272. if (p.value.length - num.length > 1)
  1273. return [false, '输入的表达式非法:' + p.value + '不是一个有效的数字'];
  1274. num = _.toNumber(num);
  1275. if (num === undefined || num === null || _.isNaN(num))
  1276. return [false, '输入的表达式非法:' + p.value + '不是一个有效的数字'];
  1277. if (i > 0) {
  1278. if (param[i - 1].type !== 'calc') {
  1279. return [false, '输入的表达式非法:' + p.value + '前应有运算符'];
  1280. } else if (param[i - 1].value === '/' && num === 0) {
  1281. return [false, '输入的表达式非法:请勿除0'];
  1282. }
  1283. }
  1284. }
  1285. if (p.type === 'base') {
  1286. const baseParam = _.find(calcBase, {code: p.value});
  1287. if (!baseParam)
  1288. return [false, '输入的表达式非法:不存在计算基数' + p.value];
  1289. if (invalidParam && invalidParam.indexOf(p.value) >= 0)
  1290. return [false, '不可使用计算基数' + p.value];
  1291. if (i > 0 && (param[i - 1].type === 'num' || param[i - 1].type === 'right'))
  1292. return [false, '输入的表达式非法:' + p.value + '前应有运算符'];
  1293. }
  1294. if (p.type === 'left') {
  1295. iLeftCount += 1;
  1296. if (i !== 0 && param[i-1].type !== 'calc')
  1297. return [false, '输入的表达式非法:(前应有运算符'];
  1298. }
  1299. if (p.type === 'right') {
  1300. iRightCount += 1;
  1301. if (i !== iLen - 1 && param[i+1].type !== 'calc')
  1302. return [false, '输入的表达式非法:)后应有运算符'];
  1303. if (iRightCount > iLeftCount)
  1304. return [false, '输入的表达式非法:")"前无对应的"("'];
  1305. }
  1306. }
  1307. if (iLeftCount > iRightCount)
  1308. return [false, '输入的表达式非法:"("后无对应的")"'];
  1309. return [true, ''];
  1310. },
  1311. _checkExpr: function (text) {
  1312. if (text) {
  1313. const num = _.toNumber(text);
  1314. if (num) {
  1315. console.log(num);
  1316. } else {
  1317. const expr = text.replace('=', '').toLowerCase();
  1318. const [valid, msg] = this._checkExprValid(expr);
  1319. if (!valid) return [valid, msg];
  1320. }
  1321. }
  1322. return [true, ''];
  1323. },
  1324. };
  1325. $('#expr_btn').click(function () {
  1326. const expr = $('#expr').val();
  1327. // 判断表达式格式
  1328. const [valid, msg] = ExprObj._checkExpr(expr);
  1329. if (!valid) {
  1330. toastr.error(msg);
  1331. return false;
  1332. }
  1333. postData(window.location.pathname + '/save', { type:'expr', id: $('#materialbillsId').val(), expr: expr, ms_id: $('#myTab').find('.active').data('msid') || null }, function (result) {
  1334. m_tp = result.m_tp;
  1335. if (materialTax) {
  1336. m_tax_tp = result.m_tax_tp;
  1337. }
  1338. const sheet = materialSpread.getActiveSheet();
  1339. const select = SpreadJsObj.getSelectObject(sheet);
  1340. const index = _.findIndex(materialBillsData, { id: select.id });
  1341. let newIndex = index;
  1342. if (isStageSelf) {
  1343. materialSpreadObj.updateMaterialData(result);
  1344. result.info = materialSpreadObj.updateOneMaterialBill(result.info);
  1345. }
  1346. if($('#bills0_list').is(':checked')) {
  1347. const newMaterialBillsData = _.filter(materialBillsData, function (item) {
  1348. return item.quantity !== null && item.quantity !== 0;
  1349. });
  1350. newIndex = _.findIndex(newMaterialBillsData, { id: select.id });
  1351. // newIndex = newMaterialBillsData.indexOf(select);
  1352. newMaterialBillsData.splice(newIndex, 1, result.info);
  1353. sheet.zh_data[newIndex] = result.info;
  1354. }
  1355. materialBillsData.splice(index, 1, result.info);
  1356. sheet.zh_data.splice(newIndex, 1, result.info);
  1357. SpreadJsObj.reLoadRowData(sheet, newIndex);
  1358. resetTpTable();
  1359. $('#bcyy').modal('hide');
  1360. });
  1361. });
  1362. }
  1363. if (!readOnly && !editForAudit) {
  1364. $('#add').click(materialSpreadObj.add);
  1365. $('#del').click(materialSpreadObj.del);
  1366. $('#up-move').click(materialSpreadObj.upMove);
  1367. $('#down-move').click(materialSpreadObj.downMove);
  1368. // 右键菜单
  1369. $.contextMenu({
  1370. selector: '#material-spread',
  1371. build: function ($trigger, e) {
  1372. const target = SpreadJsObj.safeRightClickSelection($trigger, e, materialSpread);
  1373. return target.hitTestType === GC.Spread.Sheets.SheetArea.viewport || target.hitTestType === GC.Spread.Sheets.SheetArea.rowHeader;
  1374. },
  1375. items: {
  1376. 'create': {
  1377. name: '新增材料',
  1378. icon: 'fa-sign-in',
  1379. callback: function (key, opt) {
  1380. materialSpreadObj.add(materialSpread.getActiveSheet());
  1381. },
  1382. },
  1383. 'delete': {
  1384. name: '删除材料',
  1385. icon: 'fa-remove',
  1386. callback: function (key, opt) {
  1387. materialSpreadObj.del(materialSpread.getActiveSheet());
  1388. },
  1389. disabled: function (key, opt) {
  1390. const sheet = materialSpread.getActiveSheet();
  1391. const select = SpreadJsObj.getSelectObject(sheet);
  1392. const sel = sheet.getSelections()[0];
  1393. materialSpreadObj.refreshActn(sel.rowCount);
  1394. if (!readOnly && select && materialBase.isUsed(select) && sel.rowCount === 1) {
  1395. return false;
  1396. } else {
  1397. return true;
  1398. }
  1399. }
  1400. },
  1401. }
  1402. });
  1403. $('.changeRate').click(function () {
  1404. $('#rateInput').val(parseInt($(this).data('value')));
  1405. $('#rateInput').siblings('.dropdown-menu').hide();
  1406. });
  1407. $('#rateInput').click(function () {
  1408. $(this).siblings('.dropdown-menu').show();
  1409. })
  1410. // 回车提交
  1411. $('#rateInput').on('keypress', function () {
  1412. if(window.event.keyCode === 13) {
  1413. $(this).blur();
  1414. }
  1415. });
  1416. $('#rateInput').blur(function () {
  1417. const _self = $(this);
  1418. setTimeout(function () {
  1419. let rate = parseFloat(_self.val());
  1420. if (_.isNaN(rate)) {
  1421. toastr.error('请输入0-100之前的整数值');
  1422. $('#rateInput').val(materialRate);
  1423. return;
  1424. }
  1425. rate = _.round(rate);
  1426. if(rate < 0 || rate > 100) {
  1427. toastr.error('请输入0-100之前的整数值');
  1428. $('#rateInput').val(materialRate);
  1429. return;
  1430. }
  1431. $('#rateInput').siblings('.dropdown-menu').hide();
  1432. console.log(rate, materialRate);
  1433. if (rate !== materialRate) {
  1434. postData(window.location.pathname + '/save', { type:'rate', rate: rate }, function (result) {
  1435. const bqhs = ZhCalc.round(ZhCalc.mul(m_tp, 1+rate/100), materialDecimal.tp);
  1436. // const exbqhs = ZhCalc.round(ZhCalc.mul(ex_tp, 1+rate/100), materialDecimal.tp);
  1437. const jzbqhs = ZhCalc.round(ZhCalc.add(pre_tp_hs, bqhs), materialDecimal.tp);
  1438. // const exjzbqhs = ZhCalc.round(ZhCalc.add(ex_pre_tp_hs, exbqhs), materialDecimal.tp);
  1439. $('#rate_set').find('td').eq(1).text(bqhs !== 0 ? bqhs : '');
  1440. $('#rate_set').find('td').eq(2).text(jzbqhs !== 0 ? jzbqhs : '');
  1441. // $('#rate_set').find('td').eq(3).text(exbqhs !== 0 ? exbqhs : '');
  1442. // $('#rate_set').find('td').eq(4).text(exjzbqhs !== 0 ? exjzbqhs : '');
  1443. materialRate = rate;
  1444. $('#rateInput').val(rate);
  1445. });
  1446. } else {
  1447. $('#rateInput').val(rate);
  1448. }
  1449. }, 500);
  1450. });
  1451. // $('#changeRate').change(function () {
  1452. // const rate = parseInt($(this).val());
  1453. // postData(window.location.pathname + '/save', { type:'rate', rate: rate }, function (result) {
  1454. // const bqhs = ZhCalc.round(ZhCalc.mul(m_tp, 1+rate/100), materialDecimal.tp);
  1455. // const exbqhs = ZhCalc.round(ZhCalc.mul(ex_tp, 1+rate/100), materialDecimal.tp);
  1456. // const jzbqhs = ZhCalc.round(ZhCalc.add(pre_tp_hs, bqhs), materialDecimal.tp);
  1457. // const exjzbqhs = ZhCalc.round(ZhCalc.add(ex_pre_tp_hs, exbqhs), materialDecimal.tp);
  1458. // $('#rate_set').find('td').eq(1).text(bqhs !== 0 ? bqhs : '');
  1459. // $('#rate_set').find('td').eq(2).text(jzbqhs !== 0 ? jzbqhs : '');
  1460. // $('#rate_set').find('td').eq(3).text(exbqhs !== 0 ? exbqhs : '');
  1461. // $('#rate_set').find('td').eq(4).text(exjzbqhs !== 0 ? exjzbqhs : '');
  1462. // });
  1463. // });
  1464. // 创建月信息价
  1465. $('#make-month').click(function() {
  1466. const yearmonth = $('#months').val();
  1467. const result = yearmonth.match(/^(\d{1,4})(-|\/)(\d{1,2})$/);
  1468. if (result === null) {
  1469. toastr.error('请选择正确的信息价月份');
  1470. return false;
  1471. }
  1472. //判断是否已存在当前月份
  1473. if (months.indexOf(yearmonth) !== -1) {
  1474. toastr.error('调差期已创建过本月的信息价');
  1475. return false;
  1476. }
  1477. postData(window.location.pathname + '/month/save', { type: 'add', updateData: { yearmonth: yearmonth } }, function (data) {
  1478. months.push(yearmonth);
  1479. months.sort();
  1480. monthsList = data.monthsList;
  1481. monthFunGather.monthsListSet();
  1482. // 工料表单价显示也要更新
  1483. materialBillsData = data.materialBillsData;
  1484. materialSpreadObj.materialSheetReset();
  1485. // SpreadJsObj.loadSheetData(materialSpread.getActiveSheet(), SpreadJsObj.DataType.Data, materialBillsData);
  1486. m_tp = data.m_tp;
  1487. if (materialTax) {
  1488. m_tax_tp = data.m_tax_tp;
  1489. }
  1490. resetTpTable();
  1491. $('#add-month').modal('hide');
  1492. });
  1493. });
  1494. // 删除月信息价
  1495. $('#del-month').click(function () {
  1496. const del_month_array = [];
  1497. $("input[name='del_month']:checked").each(function () {
  1498. del_month_array.push($(this).val());
  1499. });
  1500. if (del_month_array.length === 0) {
  1501. toastr.error('请选择要移除的月信息价');
  1502. return false;
  1503. }
  1504. postData(window.location.pathname + '/month/save', { type: 'del', updateData: { del_yearmonth: del_month_array } }, function (data) {
  1505. for (const dm of del_month_array) {
  1506. // _.remove(materialMonthSpreadSetting.cols, function (n) {
  1507. // return n.field === dm;
  1508. // });
  1509. _.remove(months, function (n) {
  1510. return n === dm;
  1511. });
  1512. }
  1513. monthsList = data.monthsList;
  1514. monthFunGather.monthsListSet();
  1515. // 工料表单价显示也要更新
  1516. materialBillsData = data.materialBillsData;
  1517. materialSpreadObj.materialSheetReset();
  1518. // SpreadJsObj.loadSheetData(materialSpread.getActiveSheet(), SpreadJsObj.DataType.Data, materialBillsData);
  1519. m_tp = data.m_tp;
  1520. if (materialTax) {
  1521. m_tax_tp = data.m_tax_tp;
  1522. }
  1523. resetTpTable();
  1524. $('#remove-month').modal('hide');
  1525. });
  1526. });
  1527. } else {
  1528. // SpreadJsObj.forbiddenSpreadContextMenu('#material-spread', materialSpread);
  1529. }
  1530. // 筛选调差工料本期应耗数量为0
  1531. $('#bills0_list').click(function () {
  1532. const isCheck = $(this).is(':checked');
  1533. let newMaterialBillsData = materialBillsData;
  1534. if (isCheck) {
  1535. newMaterialBillsData = _.filter(materialBillsData, function (item) {
  1536. return item.quantity !== null && item.quantity !== 0;
  1537. });
  1538. SpreadJsObj.initSpreadSettingEvents(materialSpreadSetting, materialCol);
  1539. SpreadJsObj.initSheet(materialSpread.getActiveSheet(), materialSpreadSetting);
  1540. }
  1541. SpreadJsObj.loadSheetData(materialSpread.getActiveSheet(), SpreadJsObj.DataType.Data, newMaterialBillsData);
  1542. monthFunGather.monthsListSet(newMaterialBillsData, false);
  1543. materialSpreadObj.refreshActn();
  1544. });
  1545. let gljLib;
  1546. const gljLibCellDoubleClick = function (e, info) {
  1547. if (readOnly || editForAudit) return;
  1548. const gljSheet = info.sheet;
  1549. if (!gljSheet.zh_setting || !gljSheet.zh_tree) { return; }
  1550. const gljTree = gljSheet.zh_tree;
  1551. const gljNode = gljTree.nodes[info.row];
  1552. const sheet = materialSpread.getActiveSheet();
  1553. const select = SpreadJsObj.getSelectObject(sheet);
  1554. if (!gljNode) return;
  1555. if (gljNode && gljNode.is_leaf === 0) {
  1556. toastr.warning('非最底层项目下,不应添加');
  1557. return;
  1558. }
  1559. postData(window.location.pathname + '/save', { type:'add-glj', postData: gljNode, order: select ? select.order : null }, function (result) {
  1560. if (result) {
  1561. // 比order大的+1
  1562. const maxData = select ? _.filter(materialBillsData, (item) => { return item.order > select.order }) : [];
  1563. _.forEach(maxData, function (item) {
  1564. const cl = _.find(materialBillsData, { id: item.id });
  1565. cl.order = item.order + 1;
  1566. });
  1567. materialBillsData.push(result.info);
  1568. materialBillsData.sort(function (a, b) {
  1569. return a.order - b.order
  1570. });
  1571. console.log(materialBillsData);
  1572. if (isStageSelf) {
  1573. materialSpreadObj.updateMaterialData(result);
  1574. materialSpreadObj.getMaterialBillsData();
  1575. }
  1576. // sheet.addRows(materialBillsData.length - 1, 1);
  1577. // SpreadJsObj.reLoadRowData(sheet, materialBillsData.length - 1);
  1578. let newMaterialBillsData = materialBillsData;
  1579. if($('#bills0_list').is(':checked')) {
  1580. newMaterialBillsData = _.filter(materialBillsData, function (item) {
  1581. return item.quantity !== null && item.quantity !== 0;
  1582. });
  1583. }
  1584. SpreadJsObj.loadSheetData(materialSpread.getActiveSheet(), SpreadJsObj.DataType.Data, newMaterialBillsData);
  1585. // materialSpreadObj.materialSheetReset();
  1586. sheet.setSelection(sheet.zh_data.indexOf(select) + 1, 0, 1, 1);
  1587. materialSpreadObj.refreshActn();
  1588. // 月信息价需要同时添加空白的list
  1589. if (months.length > 0) {
  1590. const one_month ={ mb_id: result.info.id, code: result.info.code, name: result.info.name, origin: result.info.origin, unit: result.info.unit, order: result.info.order };
  1591. for (const m of months) {
  1592. one_month[m] = null;
  1593. }
  1594. _.forEach(maxData, function (item) {
  1595. const cl = _.find(monthsList, { mb_id: item.id });
  1596. cl.order = item.order + 1;
  1597. });
  1598. monthsList.push(one_month);
  1599. monthsList.sort(function (a, b) {
  1600. return a.order - b.order
  1601. });
  1602. console.log(monthsList);
  1603. // const newMonthsList = _.filter(monthsList, function (item) {
  1604. // return _.findIndex(newMaterialBillsData, { code: item.code }) !== -1;
  1605. // });
  1606. // materialMonthSpread.getActiveSheet().addRows(newMonthsList.length - 1, 1);
  1607. // SpreadJsObj.reLoadRowData(materialMonthSpread.getActiveSheet(), newMonthsList.length - 1);
  1608. monthFunGather.monthsListSet();
  1609. // materialMonthSpreadObj.monthsListSet();
  1610. }
  1611. }
  1612. });
  1613. };
  1614. const gljLibSetting = {
  1615. selector: '#material-tab',
  1616. stdType: 'glj',
  1617. libs: gljLists,
  1618. treeSetting: {
  1619. id: 'glj_id',
  1620. pid: 'glj_pid',
  1621. order: 'order',
  1622. level: 'level',
  1623. rootId: -1,
  1624. keys: ['id', 'list_id', 'glj_id'],
  1625. },
  1626. spreadSetting: {
  1627. cols: [
  1628. {title: '编号', field: 'code', hAlign: 0, width: 120, formatter: '@', readOnly: true, cellType: 'tree'},
  1629. {title: '名称', field: 'name', hAlign: 0, width: 150, formatter: '@', readOnly: true},
  1630. {title: '单位', field: 'unit', hAlign: 1, width: 50, formatter: '@', readOnly: true},
  1631. {title: '参考上涨幅度', field: 'rise_range', hAlign: 1, width: 50, formatter: '@', readOnly: true},
  1632. {title: '参考下跌幅度', field: 'fall_range', hAlign: 1, width: 50, formatter: '@', readOnly: true},
  1633. ],
  1634. treeCol: 0,
  1635. emptyRows: 0,
  1636. headRows: 1,
  1637. headRowHeight: [32],
  1638. defaultRowHeight: 21,
  1639. headerFont: '12px 微软雅黑',
  1640. font: '12px 微软雅黑',
  1641. headColWidth: [30],
  1642. selectedBackColor: '#fffacd',
  1643. },
  1644. spreadClassCss: 'sjs-material',
  1645. cellDoubleClick: gljLibCellDoubleClick,
  1646. page: 'material',
  1647. tid: window.location.pathname.split('/')[2],
  1648. };
  1649. $.subMenu({
  1650. menu: '#sub-menu', miniMenu: '#sub-mini-menu', miniMenuList: '#mini-menu-list',
  1651. toMenu: '#to-menu', toMiniMenu: '#to-mini-menu',
  1652. key: 'menu.1.0.0',
  1653. miniHint: '#sub-mini-hint', hintKey: 'menu.hint.1.0.1',
  1654. callback: function (info) {
  1655. if (info.mini) {
  1656. $('.panel-title').addClass('fluid');
  1657. $('#sub-menu').removeClass('panel-sidebar');
  1658. } else {
  1659. $('.panel-title').removeClass('fluid');
  1660. $('#sub-menu').addClass('panel-sidebar');
  1661. }
  1662. autoFlashHeight();
  1663. materialSpread.refresh();
  1664. }
  1665. });
  1666. $.divResizer({
  1667. select: '#right-spr',
  1668. callback: function () {
  1669. materialSpread.refresh();
  1670. materialMonthSpread.refresh();
  1671. if (gljLib) gljLib.spread.refresh();
  1672. const width = (($('#right-view').width()/$('#right-view').parent('div').width())*100).toFixed();
  1673. setLocalCache('material_month_' + materialID, width);
  1674. }
  1675. });
  1676. $.divResizer({
  1677. select: '#main-resize',
  1678. callback: function () {
  1679. let bcontent = $(".bcontent-wrap") ? $(".bcontent-wrap").height() : 0;
  1680. $(".sp-wrap").height(bcontent-30);
  1681. const height = $('.bcontent-wrap').height();
  1682. const cutHeight = isStageSelf ? getObjHeight($('#myTab')) : 0;
  1683. $('.sjs-height-material').height($('.sjs-height-1').height() - cutHeight);
  1684. updateSjsHeight();
  1685. materialSpread.refresh();
  1686. materialMonthSpread.refresh();
  1687. if (gljLib) gljLib.spread.refresh();
  1688. setLocalCache('material_bills_' + materialID, height);
  1689. }
  1690. });
  1691. // 展开收起月信息价并浏览器记住本期展开收起
  1692. $('a', '.right-nav').bind('click', function () {
  1693. //const main = $('#main-view'), tool = $('#tools-view');
  1694. const tab = $(this), tabPanel = $(tab.attr('content'));
  1695. if (!tab.hasClass('active')) {
  1696. $('a', '.side-menu').removeClass('active');
  1697. $('.tab-content .tab-pane').removeClass('active');
  1698. tab.addClass('active');
  1699. tabPanel.addClass('active');
  1700. showSideTools(tab.hasClass('active'));
  1701. if (tab.attr('content') === '#month-tab') {
  1702. const width = (($('#right-view').width()/$('#right-view').parent('div').width())*100).toFixed();
  1703. setLocalCache('material_month_' + materialID, width);
  1704. materialMonthSpread.refresh();
  1705. } else if (tab.attr('content') === '#material-tab') {
  1706. if (!gljLib) {
  1707. gljLib = $.gljLib(gljLibSetting);
  1708. }
  1709. updateSjsHeight();
  1710. gljLib.spread.refresh();
  1711. }
  1712. } else {
  1713. removeLocalCache('material_month_' + materialID);
  1714. tab.removeClass('active');
  1715. tabPanel.removeClass('active');
  1716. showSideTools(tab.hasClass('active'));
  1717. }
  1718. materialSpread.refresh();
  1719. // materialMonthSpread.refresh();
  1720. });
  1721. // 根据浏览器记录展开收起
  1722. if (getLocalCache('material_month_' + materialID) && !isStageSelf) {
  1723. const tab = $('.right-nav a[content="#month-tab"]'), tabPanel = $(tab.attr('content'));
  1724. $('a', '.side-menu').removeClass('active');
  1725. $('.tab-content .tab-pane').removeClass('active');
  1726. tab.addClass('active');
  1727. tabPanel.addClass('active');
  1728. $('#right-view').width(getLocalCache('material_month_' + materialID) + '%');
  1729. showSideTools(tab.hasClass('active'));
  1730. materialSpread.refresh();
  1731. materialMonthSpread.refresh();
  1732. }
  1733. const cutHeight = isStageSelf ? getObjHeight($('#myTab')) : 0;
  1734. if (getLocalCache('material_bills_' + materialID)) {
  1735. $('.bcontent-wrap').height(getLocalCache('material_bills_' + materialID));
  1736. const cHeader = getObjHeight($(".c-header"));
  1737. const bcontent = $(".bcontent-wrap") ? $(".bcontent-wrap").height() : 0;
  1738. $(".sp-wrap").height(bcontent-30);
  1739. $('.sjs-height-1').height($(window).height()-cHeader-bcontent-90+53);
  1740. $('.sjs-height-material').height($('.sjs-height-1').height() - cutHeight);
  1741. updateSjsHeight();
  1742. materialSpread.refresh();
  1743. materialMonthSpread.refresh();
  1744. if (gljLib) gljLib.spread.refresh();
  1745. } else {
  1746. $('.sjs-height-material').height($('.sjs-height-1').height() - cutHeight);
  1747. updateSjsHeight();
  1748. materialSpread.refresh();
  1749. materialMonthSpread.refresh();
  1750. if (gljLib) gljLib.spread.refresh();
  1751. }
  1752. function updateSjsHeight() {
  1753. $('.sjs-height-material-month').height($('.sjs-height-1').height() - getObjHeight($('.sjs-bar-1')));
  1754. $('.material-table-height').height($('.bcontent-wrap').height() - getObjHeight($('.bc-bar')));
  1755. $('.sjs-material').height($('.sjs-height-1').height() - getObjHeight($('.sjs-bar')));
  1756. }
  1757. function getObjHeight(select) {
  1758. return select.length > 0 ? select.height() : 0;
  1759. }
  1760. });