material.js 108 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158
  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. // return !readOnly && data.t_type === 2 && data.mid === materialID;
  173. return data && data.t_type === 2;
  174. };
  175. materialSpreadSetting.imageClick = function (data) {
  176. if (data.t_type === 2) {
  177. $('#bcyy').modal('show');
  178. $('#materialbillsId').val(data.id);
  179. $('#expr').val(data.expr);
  180. if (!readOnly) {
  181. $('#expr').attr('readOnly', false);
  182. $('#expr_btn').show();
  183. $('#expr_select').show();
  184. } else {
  185. $('#expr').attr('readOnly', true);
  186. $('#expr_btn').hide();
  187. $('#expr_select').hide();
  188. }
  189. }
  190. };
  191. const materialBase = {
  192. isUsed: function (data) {
  193. if (data.t_type === 2) {
  194. return data.mid === materialID;
  195. } else {
  196. const mlInfo = _.find(materialListData, { mb_id: data.id });
  197. return data.mid === materialID && mlInfo === undefined;
  198. }
  199. },
  200. isEdit: function (data) {
  201. // if (data.t_type === 2) {
  202. // return data.mid === materialID;
  203. // } else {
  204. // const mlInfo = _.find(materialListData, { mb_id: data.id });
  205. // return data.mid === materialID && mlInfo === undefined;
  206. // }
  207. return data.mid === materialID;
  208. },
  209. // isStage: function (data) {
  210. // return data.mid === materialID;
  211. // }
  212. }
  213. const materialCol = {
  214. getValue: {
  215. msg_spread: function (data) {
  216. return ZhCalc.round(ZhCalc.sub(data.msg_tp, data.basic_price), materialDecimal.up);
  217. },
  218. m_spread : function (data) {
  219. const msg_spread = materialCol.getValue.msg_spread(data);
  220. 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));
  221. 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;
  222. },
  223. m_tp: function (data) {
  224. return ZhCalc.round(ZhCalc.mul(materialCol.getValue.m_spread(data), data.quantity), materialDecimal.tp);
  225. },
  226. m_tax_tp: function (data) {
  227. const m_tp = ZhCalc.round(ZhCalc.mul(materialCol.getValue.m_spread(data), data.quantity), materialDecimal.tp);
  228. return data.m_tax ? ZhCalc.round(ZhCalc.mul(m_tp, (1+ZhCalc.div(data.m_tax, 100))), materialDecimal.tp) : m_tp;
  229. }
  230. },
  231. readOnly: {
  232. isEdit: function (data) {
  233. return !(!readOnly && materialBase.isEdit(data));
  234. },
  235. isUsed: function (data) {
  236. return !(!readOnly && materialBase.isUsed(data));
  237. },
  238. remark: function () {
  239. return readOnly;
  240. },
  241. msg_tp: function () {
  242. return !(!readOnly && months.length === 0);
  243. }
  244. },
  245. };
  246. const needUpdateArray = ['quantity', 'expr', 'msg_tp', 'msg_times', 'msg_spread', 'm_spread', 'm_tp', 'm_tax_tp', 'is_summary', 'remark'];
  247. const materialSpreadObj = {
  248. getMaterialBillsData: function () {
  249. if (isStageSelf) {
  250. // 当前选中nav id
  251. const ms_id = $('#myTab').find('.active').data('msid');
  252. const msbList = _.filter(materialStageBillsData, { ms_id });
  253. for (const mb of materialBillsData) {
  254. mb.ms_id = ms_id;
  255. const msbInfo = _.find(msbList, { mb_id: mb.mb_id ? mb.mb_id : mb.id });
  256. if (msbInfo) {
  257. for (const nu of needUpdateArray) {
  258. mb[nu] = msbInfo[nu];
  259. }
  260. }
  261. }
  262. }
  263. return materialBillsData;
  264. },
  265. updateOneMaterialBill: function (data) {
  266. const ms_id = $('#myTab').find('.active').data('msid');
  267. const msbInfo = _.find(materialStageBillsData, { ms_id, mb_id: data.id });
  268. console.log(msbInfo);
  269. data.ms_id = ms_id;
  270. for (const nu of needUpdateArray) {
  271. data[nu] = msbInfo[nu];
  272. }
  273. return data;
  274. },
  275. updateMaterialData: function (datas) {
  276. if (datas.stageData && datas.stageData.length > 0) {
  277. if (datas.stageData.length === materialStageData.length) {
  278. // 全体替换
  279. materialStageData = datas.stageData;
  280. } else {
  281. // 替换对应的stage数据
  282. for (const s of datas.stageData) {
  283. const index = _.findIndex(materialStageData, { id: s.id });
  284. materialStageData.splice(index, 1, s);
  285. }
  286. }
  287. }
  288. if (datas.pushStageBillsData && datas.pushStageBillsData.length > 0) {
  289. materialStageBillsData = _.concat(materialStageBillsData, datas.pushStageBillsData);
  290. }
  291. if (datas.removeStageBillsData && datas.removeStageBillsData.length > 0) {
  292. materialStageBillsData = _.remove(materialStageBillsData, function (item) {
  293. return _.findIndex(datas.removeStageBillsData, { id: item.mb_id }) === -1
  294. })
  295. }
  296. if (datas.stageBillsData && datas.stageBillsData.length > 0) {
  297. if (datas.stageBillsData.length === materialStageBillsData.length) {
  298. // 全体替换
  299. materialStageBillsData = datas.stageBillsData;
  300. } else {
  301. // 替换对应的stage数据
  302. for (const s of datas.stageBillsData) {
  303. const index = _.findIndex(materialStageBillsData, { id: s.id });
  304. materialStageBillsData.splice(index, 1, s);
  305. }
  306. }
  307. }
  308. if (datas.billsData && datas.billsData.length > 0) {
  309. if (datas.billsData.length === materialBillsData.length) {
  310. // 全体替换
  311. materialBillsData = datas.billsData;
  312. materialSpreadObj.getMaterialBillsData();
  313. } else {
  314. for (const b of datas.billsData) {
  315. const index = _.findIndex(materialBillsData, {id: b.id});
  316. materialBillsData.splice(index, 1, b);
  317. materialSpreadObj.updateOneMaterialBill(materialBillsData[index]);
  318. }
  319. }
  320. }
  321. },
  322. materialSheetReset: function (redo = false) {
  323. let newMaterialBillsData = _.cloneDeep(materialBillsData);
  324. if($('#bills0_list').is(':checked')) {
  325. newMaterialBillsData = _.filter(materialBillsData, function (item) {
  326. return item.quantity !== null && item.quantity !== 0;
  327. });
  328. if (redo) {
  329. materialSpread.getActiveSheet().reset();
  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. console.log(sheet);
  387. materialSpreadObj.refreshActn();
  388. // 月信息价需要同时添加空白的list
  389. if (months.length > 0) {
  390. const one_month ={ mb_id: result.info.id, code: '', name: null, origin: null, unit: null, order: result.info.order };
  391. for (const m of months) {
  392. one_month[m] = null;
  393. }
  394. monthsList.push(one_month);
  395. // const newMonthsList = _.filter(monthsList, function (item) {
  396. // return _.findIndex(newMaterialBillsData, { code: item.code }) !== -1;
  397. // });
  398. // materialMonthSpread.getActiveSheet().addRows(newMonthsList.length - 1, 1);
  399. // SpreadJsObj.reLoadRowData(materialMonthSpread.getActiveSheet(), newMonthsList.length - 1);
  400. monthFunGather.monthsListSet();
  401. // materialMonthSpreadObj.monthsListSet();
  402. }
  403. }
  404. });
  405. },
  406. del: function () {
  407. const sheet = materialSpread.getActiveSheet();
  408. const select = SpreadJsObj.getSelectObject(sheet);
  409. const sel = sheet.getSelections();
  410. postData(window.location.pathname + '/save', {type: 'del', id: select.id}, function (result) {
  411. m_tp = result.m_tp;
  412. if (materialTax) {
  413. m_tax_tp = result.m_tax_tp;
  414. }
  415. const index = _.findIndex(materialBillsData, { id: select.id });
  416. materialBillsData.splice(index, 1);
  417. if (isStageSelf) {
  418. result.removeStageBillsData = [{ id: select.id }];
  419. materialSpreadObj.updateMaterialData(result);
  420. materialSpreadObj.getMaterialBillsData();
  421. }
  422. let newMaterialBillsData = materialBillsData;
  423. let newIndex = index;
  424. if($('#bills0_list').is(':checked')) {
  425. newMaterialBillsData = _.filter(materialBillsData, function (item) {
  426. return item.quantity !== null && item.quantity !== 0;
  427. });
  428. newIndex = _.findIndex(newMaterialBillsData, { id: select.id });
  429. }
  430. // sheet.deleteRows(newIndex, 1);
  431. // sheet.zh_data.splice(newIndex, 1);
  432. materialSpreadObj.materialSheetReset(true);
  433. resetTpTable();
  434. // SpreadJsObj.reLoadSheetData(materialSpread.getActiveSheet());
  435. sheet.setSelection(newIndex > 0 ? newIndex - 1 : 0, sel.length > 0 ? sel[0].col : 0, 1, 1);
  436. materialSpreadObj.refreshActn();
  437. // 月信息价需要同时删除
  438. if (months.length > 0) {
  439. monthsList.splice(index, 1);
  440. materialMonthSpread.getActiveSheet().deleteRows(newIndex, 1);
  441. // SpreadJsObj.reLoadSheetData(materialMonthSpread.getActiveSheet());
  442. }
  443. });
  444. },
  445. upMove: function () {
  446. if($('#bills0_list').is(':checked')) {
  447. return;
  448. }
  449. const data = {
  450. type: 'changeOrder',
  451. postData: [],
  452. };
  453. const sheet = materialSpread.getActiveSheet();
  454. const selection = sheet.getSelections();
  455. const row = selection[0].row, count = selection[0].rowCount;
  456. const first = sheet.zh_data[row];
  457. if (!first) {
  458. materialSpreadObj.refreshActn();
  459. return false;
  460. }
  461. const pre = sheet.zh_data[row - 1], preUpdate = {id: pre.id};
  462. for (let iRow = 0; iRow < count; iRow++) {
  463. const posData = sheet.zh_data[iRow + row];
  464. if (posData) {
  465. data.postData.push({id: posData.id, order: sheet.zh_data[iRow + row - 1].order});
  466. preUpdate.order = posData.order;
  467. }
  468. }
  469. console.log(monthsList);
  470. data.postData.push(preUpdate);
  471. console.log(data);
  472. if (data.postData.length > 0) {
  473. postData(window.location.pathname + '/save', data, function () {
  474. _.forEach(data.postData, function (item) {
  475. const cl = _.find(materialBillsData, { id: item.id });
  476. cl.order = item.order;
  477. });
  478. materialBillsData.sort(function (a, b) {
  479. return a.order - b.order
  480. });
  481. SpreadJsObj.reLoadSheetData(sheet);
  482. const sel = selection[0];
  483. if (sel) {
  484. sheet.setSelection(sheet.zh_data.indexOf(first), sel.col, sel.rowCount, sel.colCount);
  485. }
  486. if (months.length > 0) {
  487. _.forEach(data.postData, function (item) {
  488. const cl = _.find(monthsList, { mb_id: item.id });
  489. cl.order = item.order;
  490. });
  491. monthsList.sort(function (a, b) {
  492. return a.order - b.order
  493. });
  494. const materialMonthSheet = materialMonthSpread.getActiveSheet();
  495. SpreadJsObj.reLoadSheetData(materialMonthSheet);
  496. }
  497. materialSpreadObj.refreshActn();
  498. });
  499. }
  500. },
  501. downMove: function () {
  502. if($('#bills0_list').is(':checked')) {
  503. return;
  504. }
  505. const data = {
  506. type: 'changeOrder',
  507. postData: [],
  508. };
  509. const sheet = materialSpread.getActiveSheet();
  510. const selection = sheet.getSelections();
  511. const row = selection[0].row, count = selection[0].rowCount;
  512. const first = sheet.zh_data[row];
  513. if (!first) {
  514. materialSpreadObj.refreshActn();
  515. return false;
  516. }
  517. const next = sheet.zh_data[row + count], nextUpdate = {id: next.id};
  518. for (let iRow = count - 1; iRow >= 0; iRow--) {
  519. const posData = sheet.zh_data[iRow + row];
  520. if (posData) {
  521. data.postData.push({id: posData.id, order: sheet.zh_data[iRow + row + 1].order});
  522. nextUpdate.order = posData.order;
  523. }
  524. }
  525. data.postData.push(nextUpdate);
  526. console.log(data);
  527. if (data.postData.length > 0) {
  528. postData(window.location.pathname + '/save', data, function () {
  529. _.forEach(data.postData, function (item) {
  530. const cl = _.find(materialBillsData, { id: item.id });
  531. cl.order = item.order;
  532. });
  533. materialBillsData.sort(function (a, b) {
  534. return a.order - b.order
  535. });
  536. SpreadJsObj.reLoadSheetData(sheet);
  537. const sel = selection[0];
  538. if (sel) {
  539. sheet.setSelection(sheet.zh_data.indexOf(first), sel.col, sel.rowCount, sel.colCount);
  540. }
  541. if (months.length > 0) {
  542. _.forEach(data.postData, function (item) {
  543. const cl = _.find(monthsList, { mb_id: item.id });
  544. cl.order = item.order;
  545. });
  546. monthsList.sort(function (a, b) {
  547. return a.order - b.order
  548. });
  549. const materialMonthSheet = materialMonthSpread.getActiveSheet();
  550. SpreadJsObj.reLoadSheetData(materialMonthSheet);
  551. }
  552. materialSpreadObj.refreshActn();
  553. });
  554. }
  555. },
  556. selectionChanged: function (e, info) {
  557. const sel = info.sheet.getSelections()[0];
  558. const col = info.sheet.zh_setting.cols[sel.col];
  559. materialSpreadObj.refreshActn(sel.rowCount);
  560. const data = SpreadJsObj.getSelectObject(info.sheet);
  561. materialSpreadObj.setReadOnly(true);
  562. // console.log(data);
  563. setCurBillSourceList(data ? (data.mb_id || data.id) : null, $('#myTab').find('.active').data('msid') || null);
  564. },
  565. editEnded: function (e, info) {
  566. if (info.sheet.zh_setting) {
  567. const select = SpreadJsObj.getSelectObject(info.sheet);
  568. const col = info.sheet.zh_setting.cols[info.col];
  569. if (col.field === 'is_summary') {
  570. return;
  571. }
  572. // 未改变值则不提交
  573. let validText = (col.type === 'Number' || col.field === 'm_type' || col.field === 't_type') && is_numeric(info.editingText) ? parseFloat(info.editingText) : (info.editingText ? trimInvalidChar(info.editingText) : null);
  574. const orgValue = select[col.field];
  575. if (orgValue == validText || ((!orgValue || orgValue === '') && (validText === ''))) {
  576. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  577. return;
  578. }
  579. // 判断部分值是否输入的是数字判断和数据计算
  580. if (col.field === 'basic_price') {
  581. if (isNaN(validText)) {
  582. toastr.error('不能输入其它非数字类型字符');
  583. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  584. return;
  585. }
  586. let num = parseFloat(validText);
  587. const reg = materialDecimal.up ? new RegExp("^\\d+(\\.\\d{1,"+ materialDecimal.up +"})?$") : new RegExp("^\\d+?$");
  588. if (validText !== null && (num < 0 || !reg.test(num))) {
  589. toastr.warning('已保留'+ materialDecimal.up +'位小数');
  590. validText = ZhCalc.round(num, materialDecimal.up);
  591. }
  592. }
  593. if (col.field === 'msg_tp') {
  594. if (isNaN(validText)) {
  595. toastr.error('不能输入其它非数字类型字符');
  596. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  597. return;
  598. }
  599. const num = parseFloat(validText);
  600. const reg = materialDecimal.up ? new RegExp("^\\d+(\\.\\d{1,"+ materialDecimal.up +"})?$") : new RegExp("^\\d+?$");
  601. if (validText !== null && (num < 0 || !reg.test(num))) {
  602. toastr.warning('已保留'+ materialDecimal.up +'位小数');
  603. validText = ZhCalc.round(num, materialDecimal.up);
  604. }
  605. }
  606. if (col.field === 'm_up_risk' || col.field === 'm_down_risk' || col.field === 'm_tax') {
  607. // 只能输入正整数
  608. if (isNaN(validText)) {
  609. toastr.error('不能输入其它非数字类型字符');
  610. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  611. return;
  612. }
  613. const num = parseFloat(validText);
  614. if (validText !== null && (num < 0 || num > 100 || !/^\d+$/.test(num))) {
  615. toastr.error('只能输入0-100的正整数');
  616. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  617. return;
  618. }
  619. }
  620. if (col.field === 't_type') {
  621. if (validText === 1) {
  622. select.quantity = null;
  623. select.expr = null;
  624. select.m_tp = null;
  625. }
  626. }
  627. select[col.field] = validText;
  628. select.msg_spread = materialCol.getValue.msg_spread(select);
  629. select.m_spread = materialCol.getValue.m_spread(select);
  630. select.m_tp = materialCol.getValue.m_tp(select);
  631. select.m_tax_tp = materialCol.getValue.m_tax_tp(select);
  632. delete select.waitingLoading;
  633. // console.log(select);
  634. // 更新至服务器
  635. postData(window.location.pathname + '/save', { type:'update', updateData: select, ms_id: $('#myTab').find('.active').data('msid') || null }, function (result) {
  636. m_tp = result.m_tp;
  637. if (materialTax) {
  638. m_tax_tp = result.m_tax_tp;
  639. }
  640. if (isStageSelf) {
  641. materialSpreadObj.updateMaterialData(result);
  642. }
  643. resetTpTable();
  644. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  645. // 判断如果是更改了编号,名称,单位,月信息价需要跟着改变值
  646. if (months.length > 0 && (col.field === 'code' || col.field === 'name' || col.field === 'unit')) {
  647. const index = _.findIndex(materialBillsData, { id: select.id });
  648. monthsList[index][col.field] = validText;
  649. monthFunGather.monthsListSet();
  650. }
  651. }, function () {
  652. select[col.field] = orgValue;
  653. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  654. });
  655. }
  656. },
  657. buttonClicked: function (e, info) {
  658. if (info.sheet.zh_setting) {
  659. const select = SpreadJsObj.getSelectObject(info.sheet);
  660. const col = info.sheet.zh_setting.cols[info.col];
  661. if (materialCol.readOnly.isEdit(select)) {
  662. return;
  663. }
  664. if (col.field === 'is_summary') {
  665. if (info.sheet.isEditing()) {
  666. info.sheet.endEdit(true);
  667. }
  668. select.is_summary = info.sheet.getValue(info.row, info.col) ? 1 : 0;
  669. delete select.waitingLoading;
  670. // 更新至服务器
  671. postData(window.location.pathname + '/save', { type:'update', updateData: select, ms_id: $('#myTab').find('.active').data('msid') || null }, function (result) {
  672. m_tp = result.m_tp;
  673. if (materialTax) {
  674. m_tax_tp = result.m_tax_tp;
  675. }
  676. if (isStageSelf) {
  677. materialSpreadObj.updateMaterialData(result);
  678. }
  679. resetTpTable();
  680. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  681. }, function () {
  682. select.is_summary = info.sheet.getValue(info.row, info.col) ? 0 : 1;
  683. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  684. });
  685. }
  686. }
  687. },
  688. deletePress: function (sheet) {
  689. return;
  690. },
  691. clipboardPasted(e, info) {
  692. const hint = {
  693. cellError: {type: 'error', msg: '粘贴内容超出了表格范围'},
  694. codeEmpty: {type: 'error', msg: '请先输入编号,并且不超过15个字符'},
  695. codeUsed: {type: 'error', msg: '该编号已存在,请重新输入。'},
  696. codeError: {type: 'error', msg: '编号为纯数字时,不能为小数'},
  697. numberExpr: {type: 'error', msg: '不能粘贴其它非数字类型字符'},
  698. riskCan: {type: 'error', msg: '只能粘贴0-100的正整数'},
  699. numberCan: {type: 'warning', msg: '已保留'+ materialDecimal.up +'位小数'},
  700. };
  701. const range = info.cellRange;
  702. const sortData = info.sheet.zh_data || [];
  703. if (info.cellRange.row + info.cellRange.rowCount > sortData.length) {
  704. toastMessageUniq(hint.cellError);
  705. // SpreadJsObj.loadSheetData(materialSpread.getActiveSheet(), SpreadJsObj.DataType.Data, materialBillsData);
  706. SpreadJsObj.reLoadSheetHeader(materialSpread.getActiveSheet());
  707. SpreadJsObj.reLoadSheetData(materialSpread.getActiveSheet());
  708. return;
  709. }
  710. if (sortData.length > 0 && range.col + range.colCount > 18) {
  711. toastMessageUniq(hint.cellError);
  712. SpreadJsObj.reLoadSheetHeader(materialSpread.getActiveSheet());
  713. SpreadJsObj.reLoadSheetData(materialSpread.getActiveSheet());
  714. return;
  715. }
  716. const data = [];
  717. // const rowData = [];
  718. for (let iRow = 0; iRow < range.rowCount; iRow++) {
  719. let bPaste = true;
  720. const curRow = range.row + iRow;
  721. // const materialData = JSON.parse(JSON.stringify(sortData[curRow]));
  722. const materialData = { id: sortData[curRow].id };
  723. const hintRow = range.rowCount > 1 ? curRow : '';
  724. let sameCol = 0;
  725. for (let iCol = 0; iCol < range.colCount; iCol++) {
  726. const curCol = range.col + iCol;
  727. const colSetting = info.sheet.zh_setting.cols[curCol];
  728. if (!colSetting) continue;
  729. let validText = info.sheet.getText(curRow, curCol);
  730. validText = (colSetting.type === 'Number' || colSetting.field === 'm_type' || colSetting.field === 't_type') && is_numeric(validText) ? parseFloat(validText) : (validText ? trimInvalidChar(validText) : null);
  731. const orgValue = sortData[curRow][colSetting.field];
  732. if (orgValue == validText || ((!orgValue || orgValue === '') && (validText === ''))) {
  733. sameCol++;
  734. if (range.colCount === sameCol) {
  735. bPaste = false;
  736. }
  737. continue;
  738. }
  739. if (colSetting.field === 'code' && (validText === '' || validText.length > 15)) {
  740. toastMessageUniq(getPasteHint(hint.codeEmpty, hintRow));
  741. bPaste = false;
  742. continue;
  743. }
  744. if (colSetting.field === 'code' && validText.toString().indexOf('.') !== -1) {
  745. toastMessageUniq(getPasteHint(hint.codeError, hintRow));
  746. bPaste = false;
  747. continue;
  748. }
  749. if (colSetting.field === 'code' && _.find(sortData, { code: validText.toString() })) {
  750. toastMessageUniq(getPasteHint(hint.codeUsed, hintRow));
  751. bPaste = false;
  752. continue;
  753. }
  754. if (colSetting.field === 't_type' || colSetting.field === 'm_type') {
  755. continue;
  756. }
  757. if (colSetting.field === 'basic_price' || colSetting.field === 'msg_tp'
  758. || colSetting.field === 'm_up_risk' || colSetting.field === 'm_down_risk') {
  759. if (isNaN(validText)) {
  760. toastMessageUniq(getPasteHint(hint.numberExpr, hintRow));
  761. bPaste = false;
  762. continue;
  763. }
  764. const num = parseFloat(validText);
  765. if (colSetting.field === 'basic_price' || colSetting.field === 'msg_tp') {
  766. const reg = materialDecimal.up ? new RegExp("^\\d+(\\.\\d{1,"+ materialDecimal.up +"})?$") : new RegExp("^\\d+?$");
  767. if (validText !== null && (num < 0 || !reg.test(num))) {
  768. toastMessageUniq(getPasteHint(hint.numberCan, hintRow));
  769. validText = ZhCalc.round(num, materialDecimal.up);
  770. }
  771. } else if (colSetting.field === 'm_up_risk' || colSetting.field === 'm_down_risk' || colSetting.field === 'm_tax') {
  772. if (validText !== null && (num < 0 || num > 100 || !/^\d+$/.test(num))) {
  773. toastMessageUniq(getPasteHint(hint.riskCan, hintRow));
  774. bPaste = false;
  775. continue;
  776. }
  777. }
  778. }
  779. materialData[colSetting.field] = validText;
  780. sortData[curRow][colSetting.field] = validText;
  781. }
  782. if (materialData.code === '' || materialData.code === null) {
  783. toastMessageUniq(getPasteHint(hint.codeEmpty, hintRow));
  784. bPaste = false;
  785. }
  786. if (bPaste) {
  787. materialData.msg_spread = materialCol.getValue.msg_spread(sortData[curRow]);
  788. materialData.m_spread = materialCol.getValue.m_spread(sortData[curRow]);
  789. materialData.m_tp = materialCol.getValue.m_tp(sortData[curRow]);
  790. materialData.m_tax_tp = materialCol.getValue.m_tax_tp(sortData[curRow]);
  791. data.push(materialData);
  792. // rowData.push(curRow);
  793. } else {
  794. SpreadJsObj.reLoadRowData(info.sheet, curRow);
  795. }
  796. }
  797. if (data.length === 0) {
  798. SpreadJsObj.reLoadRowData(info.sheet, info.cellRange.row, info.cellRange.rowCount);
  799. return;
  800. }
  801. // console.log(data);
  802. // 更新至服务器
  803. postData(window.location.pathname + '/save', { type:'paste', updateData: data, ms_id: $('#myTab').find('.active').data('msid') || null }, function (result) {
  804. materialBillsData = result.info;
  805. m_tp = result.m_tp;
  806. if (materialTax) {
  807. m_tax_tp = result.m_tax_tp;
  808. }
  809. if (isStageSelf) {
  810. materialSpreadObj.updateMaterialData(result);
  811. materialSpreadObj.getMaterialBillsData();
  812. }
  813. // SpreadJsObj.reLoadSheetData(materialSpread.getActiveSheet());
  814. materialSpreadObj.materialSheetReset();
  815. // SpreadJsObj.loadSheetData(materialSpread.getActiveSheet(), SpreadJsObj.DataType.Data, materialBillsData);
  816. // for (const row in rowData) {
  817. // materialBillsData.splice(index, 1, result.info[row]);
  818. // SpreadJsObj.reLoadRowData(info.sheet, row);
  819. // }
  820. if (months.length > 0) {
  821. for (const [i,m] of monthsList.entries()) {
  822. m.code = materialBillsData[i].code;
  823. m.name = materialBillsData[i].name;
  824. m.unit = materialBillsData[i].unit;
  825. m.origin = materialBillsData[i].origin;
  826. }
  827. // SpreadJsObj.loadSheetData(materialMonthSpread.getActiveSheet(), SpreadJsObj.DataType.Data, monthsList);
  828. monthFunGather.monthsListSet();
  829. // SpreadJsObj.reLoadSheetData(materialMonthSpread.getActiveSheet());
  830. }
  831. resetTpTable();
  832. // materialSpreadObj.refreshActn();
  833. }, function () {
  834. // materialSpreadObj.refreshActn();
  835. SpreadJsObj.reLoadRowData(info.sheet, info.cellRange.row, info.cellRange.rowCount);
  836. return;
  837. });
  838. },
  839. setReadOnly: function(readOnly) {
  840. // SpreadJsObj.resetFieldReadOnly(materialSpread.getActiveSheet(), 'msg_spread', 'm_spread', 'm_tp', 'pre_tp', readOnly);
  841. }
  842. };
  843. materialSpread.bind(spreadNS.Events.SelectionChanged, materialSpreadObj.selectionChanged);
  844. materialSpread.bind(spreadNS.Events.ClipboardPasted, materialSpreadObj.clipboardPasted);
  845. SpreadJsObj.addDeleteBind(materialSpread, materialSpreadObj.deletePress);
  846. materialSpread.bind(spreadNS.Events.EditEnded, materialSpreadObj.editEnded);
  847. materialSpread.bind(spreadNS.Events.ButtonClicked, materialSpreadObj.buttonClicked);
  848. const sheet = materialSpread.getActiveSheet();
  849. sheet.suspendPaint();
  850. // materialSpreadObj.refreshActn();
  851. // const basic_range = sheet.getRange(-1, 8, -1, 1);
  852. // basic_range.cellType(new DatePickerCellType());
  853. // basic_range.formatter("yyyy-MM-dd");
  854. // const msg_range = sheet.getRange(-1, 10, -1, 1);
  855. // msg_range.cellType(new DatePickerCellType());
  856. // msg_range.formatter("yyyy-MM-dd");
  857. sheet.resumePaint();
  858. const static_cols = [
  859. {title: '编号', colSpan: '1', rowSpan: '2', field: 'code', hAlign: 0, width: 60, formatter: '@', readOnly: true},
  860. {title: '名称', colSpan: '1', rowSpan: '2', field: 'name', hAlign: 0, width: 100, formatter: '@', readOnly: true},
  861. {title: '单位', colSpan: '1', rowSpan: '2', field: 'unit', hAlign: 1, width: 50, formatter: '@', readOnly: true},
  862. {title: '来源地', colSpan: '1', rowSpan: '2', field: 'origin', hAlign: 0, width: 70, formatter: '@', readOnly: 'readOnly.isEdit'},
  863. {title: '平均单价', colSpan: '1', rowSpan: '2', field: 'average_msg_tp', hAlign: 2, width: 60, type: 'Number', readOnly: true, getValue:'getValue.average_msg_tp'},
  864. ];
  865. // 月信息价方法集合
  866. const monthFunGather = {
  867. _setMonthHtml: function() {
  868. let html = '';
  869. let qihtml = '';
  870. for (const m of months) {
  871. html += '<div class="custom-control custom-checkbox mb-2">\n' +
  872. ' <input type="checkbox" name="del_month" value="' + m + '" class="custom-control-input" id="month_' + m + '">\n' +
  873. ' <label class="custom-control-label" for="month_' + m + '">' + m + '月</label>\n' +
  874. ' </div>';
  875. qihtml += parseInt(m.split('-')[1]) + '月,';
  876. }
  877. if (months.length > 0) {
  878. qihtml = '<span class="mx-2 text-muted">/</span>本期月信息价:' + qihtml;
  879. qihtml = qihtml.substring(0, qihtml.length-1);
  880. }
  881. $('#show_month').html(html);
  882. $('#qi-month').html(qihtml);
  883. },
  884. _monthHeaderSet: function() {
  885. const newMonths = [];
  886. for (const m of months) {
  887. const year = m.split('-')[0];
  888. const month = parseInt(m.split('-')[1]);
  889. let one = _.find(newMonths, { 'year': year });
  890. let oneIndex = _.findIndex(newMonths, { 'year': year });
  891. if (one) {
  892. one.month.push(month);
  893. newMonths.splice(oneIndex, 1, one);
  894. } else {
  895. one = {
  896. year: year,
  897. month: [ month ],
  898. };
  899. newMonths.push(one);
  900. }
  901. }
  902. const pushMonth = [];
  903. for(const mo of newMonths) {
  904. for (let i in mo.month) {
  905. i = parseInt(i);
  906. const newCols = {
  907. title: (i === 0 ? mo.year: '') + '|' + mo.month[i] + '月',
  908. colSpan: (i === 0 ? (mo.month.length === 1 ? '0' : mo.month.length) : '') + '|1',
  909. rowSpan: i === 0 ? '1|1' : '|1',
  910. field: mo.year + '-' + (mo.month[i] < 10 ? '0' + mo.month[i] : mo.month[i]),
  911. hAlign: 2, width: 60, type: '@', readOnly: 'readOnly.isEdit'};
  912. pushMonth.push(newCols);
  913. }
  914. }
  915. return pushMonth;
  916. },
  917. monthSheetReset: function (ml = monthsList) {
  918. const monthCols = monthFunGather._monthHeaderSet();
  919. const newMonthSpreadHeaderCols = static_cols.concat(monthCols);
  920. materialMonthSpreadSetting.cols = newMonthSpreadHeaderCols;
  921. // 表头变化需要重新绘制,不然报错;
  922. materialMonthSpread.getActiveSheet().reset();
  923. SpreadJsObj.initSpreadSettingEvents(materialMonthSpreadSetting, materialMonthCol);
  924. SpreadJsObj.initSheet(materialMonthSpread.getActiveSheet(), materialMonthSpreadSetting);
  925. SpreadJsObj.loadSheetData(materialMonthSpread.getActiveSheet(), SpreadJsObj.DataType.Data, ml);
  926. monthFunGather._setMonthHtml();
  927. },
  928. monthsListSet: function (ml = materialBillsData, checked = true) {
  929. if($('#bills0_list').is(':checked') && checked) {
  930. ml = _.filter(materialBillsData, function (item) {
  931. return item.quantity === null || item.quantity === 0;
  932. });
  933. }
  934. const newMonthsList = _.filter(monthsList, function (item) {
  935. return _.findIndex(ml, { code: item.code }) !== -1;
  936. });
  937. monthFunGather.monthSheetReset(newMonthsList);
  938. }
  939. };
  940. const materialMonthSpread = SpreadJsObj.createNewSpread($('#material-month-spread')[0]);
  941. const materialMonthSpreadSetting = {
  942. cols: static_cols,
  943. emptyRows: 0,
  944. headRows: 2,
  945. headRowHeight: [25, 25],
  946. defaultRowHeight: 21,
  947. headerFont: '12px 微软雅黑',
  948. font: '12px 微软雅黑',
  949. readOnly: readOnly,
  950. };
  951. if (months.length > 0) {
  952. const monthCols = monthFunGather._monthHeaderSet();
  953. const monthSpreadHeaderCols = static_cols.concat(monthCols);
  954. materialMonthSpreadSetting.cols = monthSpreadHeaderCols;
  955. }
  956. const materialMonthCol = {
  957. getValue: {
  958. average_msg_tp: function (data) {
  959. let msg_tp = 0;
  960. let hadnum = 0;
  961. for (const m of months) {
  962. msg_tp += data[m];
  963. if (data[m] !== null && data[m] !== '' && data[m] !== 0) {
  964. hadnum++;
  965. }
  966. }
  967. const average_tp = hadnum !== 0 ? ZhCalc.round(ZhCalc.div(msg_tp, hadnum), materialDecimal.up) : ZhCalc.round(ZhCalc.div(msg_tp, months.length), materialDecimal.up);
  968. return average_tp;
  969. },
  970. },
  971. readOnly: {
  972. isEdit: function (data) {
  973. return readOnly;
  974. },
  975. },
  976. };
  977. materialMonthSpread.getActiveSheet().frozenColumnCount(5);
  978. materialMonthSpread.getActiveSheet().options.frozenlineColor = '#f1f1f1';
  979. SpreadJsObj.initSpreadSettingEvents(materialMonthSpreadSetting, materialMonthCol);
  980. SpreadJsObj.initSheet(materialMonthSpread.getActiveSheet(), materialMonthSpreadSetting);
  981. SpreadJsObj.loadSheetData(materialMonthSpread.getActiveSheet(), SpreadJsObj.DataType.Data, monthsList);
  982. const materialMonthSpreadObj = {
  983. editEnded: function (e, info) {
  984. if (info.sheet.zh_setting) {
  985. const select = SpreadJsObj.getSelectObject(info.sheet);
  986. const col = info.sheet.zh_setting.cols[info.col];
  987. // 未改变值则不提交
  988. let validText = is_numeric(info.editingText) ? parseFloat(info.editingText) : (info.editingText ? trimInvalidChar(info.editingText) : null);
  989. const orgValue = select[col.field];
  990. if (orgValue == validText || ((!orgValue || orgValue === '') && (validText === ''))) {
  991. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  992. return;
  993. }
  994. if (col.field === 'origin') {
  995. select[col.field] = validText;
  996. postData(window.location.pathname + '/month/save', { type:'updateOrigin', updateData: { mb_id: select.mb_id, value: validText } }, function (result) {
  997. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  998. materialBillsData = result.materialBillsData;
  999. materialSpreadObj.materialSheetReset();
  1000. // SpreadJsObj.loadSheetData(materialSpread.getActiveSheet(), SpreadJsObj.DataType.Data, materialBillsData);
  1001. }, function () {
  1002. select[col.field] = orgValue;
  1003. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  1004. });
  1005. } else {
  1006. // 判断部分值是否输入的是数字判断和数据计算
  1007. if (isNaN(validText)) {
  1008. toastr.error('不能输入其它非数字类型字符');
  1009. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  1010. return;
  1011. }
  1012. const num = parseFloat(validText);
  1013. const reg = materialDecimal.up ? new RegExp("^\\d+(\\.\\d{1,"+ materialDecimal.up +"})?$") : new RegExp("^\\d+?$");
  1014. if (validText !== null && (num < 0 || !reg.test(num))) {
  1015. toastr.warning('已保留'+ materialDecimal.up +'位小数');
  1016. validText = ZhCalc.round(num, materialDecimal.up);
  1017. }
  1018. select[col.field] = validText;
  1019. // 更新至服务器
  1020. postData(window.location.pathname + '/month/save', { type:'update', updateData: { mb_id: select.mb_id, yearmonth: col.field, value: validText } }, function (result) {
  1021. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  1022. materialBillsData = result.materialBillsData;
  1023. materialSpreadObj.materialSheetReset();
  1024. // SpreadJsObj.loadSheetData(materialSpread.getActiveSheet(), SpreadJsObj.DataType.Data, materialBillsData);
  1025. m_tp = result.m_tp;
  1026. if (materialTax) {
  1027. m_tax_tp = result.m_tax_tp;
  1028. }
  1029. resetTpTable();
  1030. }, function () {
  1031. select[col.field] = orgValue;
  1032. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  1033. });
  1034. }
  1035. }
  1036. },
  1037. deletePress: function (sheet) {
  1038. // if (sheet.zh_setting && sheet.zh_data) {
  1039. // const sel = sheet.getSelections()[0];
  1040. // if (!sel) return;
  1041. //
  1042. // const col = sheet.zh_setting.cols[sel.col];
  1043. // const select = SpreadJsObj.getSelectObject(sheet);
  1044. // const orgValue = select[col.field];
  1045. // if (sel.colCount > 1 || sel.rowCount > 1) {
  1046. // toastr.warning('请勿同时删除多行或多列数据');
  1047. // }
  1048. // console.log(sel, select, col);
  1049. // if (orgValue === null || col.field === 'type' || col.field === 'is_summary') {
  1050. // return;
  1051. // }
  1052. // select[col.field] = null;
  1053. // select.calc_num = materialExponentCol.getValue.calc_num(select);
  1054. // console.log(select);
  1055. // 更新至服务器
  1056. // postData(window.location.pathname + '/save', { type:'update', updateData: select }, function (result) {
  1057. // ex_tp = result.ex_tp;
  1058. // ex_expr = result.ex_expr;
  1059. // resetExTpTable();
  1060. // SpreadJsObj.reLoadRowData(sheet, sel.row);
  1061. // materialExponentData.splice(sel.row, 1, select);
  1062. // }, function () {
  1063. // select[col.field] = orgValue;
  1064. // SpreadJsObj.reLoadRowData(sheet, sel.row);
  1065. // });
  1066. // }
  1067. return;
  1068. },
  1069. clipboardPasted(e, info) {
  1070. const hint = {
  1071. cellError: {type: 'error', msg: '粘贴内容超出了表格范围'},
  1072. numberExpr: {type: 'error', msg: '不能粘贴其它非数字类型字符'},
  1073. numberCan: {type: 'warning', msg: '已保留'+ materialDecimal.up +'位小数'},
  1074. };
  1075. const range = info.cellRange;
  1076. const sortData = info.sheet.zh_data || [];
  1077. if (info.cellRange.row + info.cellRange.rowCount > sortData.length) {
  1078. toastMessageUniq(hint.cellError);
  1079. SpreadJsObj.reLoadSheetHeader(materialMonthSpread.getActiveSheet());
  1080. SpreadJsObj.reLoadSheetData(materialMonthSpread.getActiveSheet());
  1081. return;
  1082. }
  1083. if (sortData.length > 0 && range.col + range.colCount > 5 + months.length) {
  1084. toastMessageUniq(hint.cellError);
  1085. SpreadJsObj.reLoadSheetHeader(materialMonthSpread.getActiveSheet());
  1086. SpreadJsObj.reLoadSheetData(materialMonthSpread.getActiveSheet());
  1087. return;
  1088. }
  1089. const data = [];
  1090. let pasteOrigin = false;
  1091. for (let iRow = 0; iRow < range.rowCount; iRow++) {
  1092. let bPaste = true;
  1093. const curRow = range.row + iRow;
  1094. const materialMonthData = sortData[curRow];
  1095. const hintRow = range.rowCount > 1 ? curRow : '';
  1096. let sameCol = 0;
  1097. for (let iCol = 0; iCol < range.colCount; iCol++) {
  1098. const curCol = range.col + iCol;
  1099. const colSetting = info.sheet.zh_setting.cols[curCol];
  1100. if (!colSetting) continue;
  1101. let validText = info.sheet.getText(curRow, curCol);
  1102. validText = is_numeric(validText) ? parseFloat(validText) : (validText ? trimInvalidChar(validText) : null);
  1103. const orgValue = sortData[curRow][colSetting.field];
  1104. if (orgValue == validText || ((!orgValue || orgValue === '') && (validText === ''))) {
  1105. sameCol++;
  1106. if (range.colCount === sameCol) {
  1107. bPaste = false;
  1108. }
  1109. continue;
  1110. }
  1111. if (colSetting.field === 'origin') {
  1112. pasteOrigin = true;
  1113. } else {
  1114. const num = parseFloat(validText);
  1115. if (isNaN(validText)) {
  1116. toastMessageUniq(getPasteHint(hint.numberExpr, hintRow));
  1117. bPaste = false;
  1118. continue;
  1119. }
  1120. const reg = materialDecimal.up ? new RegExp("^\\d+(\\.\\d{1,"+ materialDecimal.up +"})?$") : new RegExp("^\\d+?$");
  1121. if (validText !== null && (num < 0 || !reg.test(num))) {
  1122. toastMessageUniq(getPasteHint(hint.numberCan, hintRow));
  1123. validText = ZhCalc.round(num, materialDecimal.up);
  1124. }
  1125. }
  1126. materialMonthData[colSetting.field] = validText;
  1127. sortData[curRow][colSetting.field] = validText;
  1128. }
  1129. if (bPaste) {
  1130. data.push(materialMonthData);
  1131. } else {
  1132. SpreadJsObj.reLoadRowData(info.sheet, curRow);
  1133. }
  1134. }
  1135. if (data.length === 0) {
  1136. SpreadJsObj.reLoadRowData(info.sheet, info.cellRange.row, info.cellRange.rowCount);
  1137. return;
  1138. }
  1139. // console.log(data);
  1140. if (pasteOrigin) {
  1141. // 更新至服务器
  1142. postData(window.location.pathname + '/month/save', { type:'pasteOrigin', updateData: data }, function (result) {
  1143. SpreadJsObj.reLoadSheetData(materialMonthSpread.getActiveSheet());
  1144. materialBillsData = result.materialBillsData;
  1145. }, function () {
  1146. SpreadJsObj.reLoadRowData(info.sheet, info.cellRange.row, info.cellRange.rowCount);
  1147. return;
  1148. });
  1149. } else {
  1150. // 更新至服务器
  1151. postData(window.location.pathname + '/month/save', { type:'paste', updateData: data }, function (result) {
  1152. SpreadJsObj.reLoadSheetData(materialMonthSpread.getActiveSheet());
  1153. materialBillsData = result.materialBillsData;
  1154. materialSpreadObj.materialSheetReset();
  1155. // SpreadJsObj.loadSheetData(materialSpread.getActiveSheet(), SpreadJsObj.DataType.Data, materialBillsData);
  1156. m_tp = result.m_tp;
  1157. if (materialTax) {
  1158. m_tax_tp = result.m_tax_tp;
  1159. }
  1160. resetTpTable();
  1161. }, function () {
  1162. SpreadJsObj.reLoadRowData(info.sheet, info.cellRange.row, info.cellRange.rowCount);
  1163. return;
  1164. });
  1165. }
  1166. },
  1167. };
  1168. // 应耗来源sjs
  1169. const material_source_cols = [
  1170. {title: '清单编号', colSpan: '1', rowSpan: '2', field: 'code', hAlign: 0, width: 100, formatter: '@'},
  1171. {title: '名称', colSpan: '1', rowSpan: '2', field: 'name', hAlign: 0, width: 150, formatter: '@'},
  1172. {title: '单位', colSpan: '1', rowSpan: '2', field: 'unit', hAlign: 1, width: 70, formatter: '@'},
  1173. {title: '本期应耗', colSpan: '1', rowSpan: '2', field: 'quantity', hAlign: 2, width: 100, formatter: '@'},
  1174. ];
  1175. const materialSourceSpread = SpreadJsObj.createNewSpread($('#material-source-spread')[0]);
  1176. const materialSourceSpreadSetting = {
  1177. cols: material_source_cols,
  1178. emptyRows: 0,
  1179. headRows: 1,
  1180. headRowHeight: [32],
  1181. defaultRowHeight: 21,
  1182. headerFont: '12px 微软雅黑',
  1183. font: '12px 微软雅黑',
  1184. readOnly: true,
  1185. };
  1186. SpreadJsObj.initSheet(materialSourceSpread.getActiveSheet(), materialSourceSpreadSetting);
  1187. SpreadJsObj.loadSheetData(materialSourceSpread.getActiveSheet(), SpreadJsObj.DataType.Data, []);
  1188. let first = true;
  1189. function setListsData(sid = null, ms_id = null) {
  1190. if (first) {
  1191. // 加载清单数据 - 暂时统一加载,如有需要,切换成动态加载并缓存
  1192. postData(window.location.pathname + '/list/load', {sid}, async function (result) {
  1193. ledger = result.ledger;
  1194. curLedgerData = result.curLedgerData;
  1195. pos = result.pos;
  1196. curPosData = result.curPosData;
  1197. materialListData2 = result.materialListData;
  1198. gclList = result.gclList;
  1199. notJoinList = result.materialNotJoinListData;
  1200. selfList = result.materialSelfListData;
  1201. materialChecklistData = result.materialChecklistData;
  1202. if (isStageSelf) {
  1203. const newGclGatherListData = [];
  1204. for (const [index, s] of result.ledgerListData.entries()) {
  1205. gclGatherModel.loadLedgerData(_.cloneDeep(ledger), s);
  1206. gclGatherModel.loadPosData(_.cloneDeep(pos), result.posListData[index]);
  1207. const oneGclGatherData = gclGatherModel.gatherGclData().filter(item => {
  1208. return item.qc_qty || item.contract_qty || item.qc_minus_qty
  1209. });
  1210. newGclGatherListData.push(oneGclGatherData);
  1211. }
  1212. gclGatherListData = newGclGatherListData;
  1213. }
  1214. // 解析清单汇总数据
  1215. gclGatherModel.loadLedgerData(_.cloneDeep(ledger), curLedgerData);
  1216. gclGatherModel.loadPosData(_.cloneDeep(pos), curPosData);
  1217. gclGatherData = gclGatherModel.gatherGclData();
  1218. gclGatherData = gclGatherData.filter(item => {
  1219. return item.qc_qty || item.contract_qty || item.qc_minus_qty
  1220. });
  1221. if (openMaterialChecklist) {
  1222. // 取交集
  1223. const selfListGcl = _.uniq(_.map(selfList, 'gcl_id'));
  1224. gclGatherData = _.filter(gclGatherData, function (item) {
  1225. if (_.find(materialChecklistData, {
  1226. b_code: item.b_code,
  1227. name: item.name,
  1228. unit: item.unit,
  1229. unit_price: item.unit_price
  1230. })) {
  1231. return true;
  1232. } else {
  1233. const gcl_ids = item.leafXmjs ? _.uniq(_.map(item.leafXmjs, 'gcl_id')) : [];
  1234. const jiaoji = _.intersection(gcl_ids, selfListGcl);
  1235. if (jiaoji.length > 0) {
  1236. return true;
  1237. }
  1238. }
  1239. });
  1240. }
  1241. // calculateJiaCha(gclGatherData);
  1242. console.log(gclGatherData);
  1243. setCurBillSourceList(materialBillsData[0] ? materialBillsData[0].mb_id || materialBillsData[0].id : null, ms_id);
  1244. first = false;
  1245. // 判断是否需要更新contract_qty、qc_qty和qc_minus_qty
  1246. if (!materialIsNewQty && !readOnly && !editForAudit) {
  1247. let needUpdateList = [];
  1248. if (materialListData2.length > 0) {
  1249. if (isStageSelf) {
  1250. for (const msIndex in materialStageData) {
  1251. const materialStageList = _.filter(materialListData2, { ms_id: materialStageData[msIndex].id });
  1252. const gclIdList = _.uniq(_.map(materialListData2, 'gcl_id'));
  1253. let leafXmjList = [];
  1254. for (const id of gclIdList) {
  1255. const gcl = _.find(gclGatherListData[msIndex], function (item) {
  1256. return item.leafXmjs && item.leafXmjs.length > 0 && _.findIndex(item.leafXmjs, {gcl_id: id}) !== -1;
  1257. });
  1258. if (gcl) leafXmjList = [...leafXmjList, ...gcl.leafXmjs];
  1259. }
  1260. for (const ml of materialStageList) {
  1261. const leafXmjInfo = _.find(leafXmjList, function (item) {
  1262. return item.gcl_id === ml.gcl_id && item.id === ml.xmj_id && (item.mx_id === undefined || (item.mx_id !== undefined && item.mx_id === ml.mx_id));
  1263. });
  1264. if (leafXmjInfo) {
  1265. needUpdateList.push({ id: ml.id, contract_qty: leafXmjInfo.contract_qty, qc_qty: leafXmjInfo.qc_qty, qc_minus_qty: leafXmjInfo.qc_minus_qty });
  1266. }
  1267. }
  1268. }
  1269. } else {
  1270. // 找出所有gcl相同的,方便搜索gclGatherData值
  1271. const gclIdList = _.uniq(_.map(materialListData2, 'gcl_id'));
  1272. let leafXmjList = [];
  1273. for (const id of gclIdList) {
  1274. const gcl = _.find(gclGatherData, function (item) {
  1275. return item.leafXmjs && item.leafXmjs.length > 0 && _.findIndex(item.leafXmjs, {gcl_id: id}) !== -1;
  1276. });
  1277. if (gcl) leafXmjList = [...leafXmjList, ...gcl.leafXmjs];
  1278. }
  1279. for (const ml of materialListData2) {
  1280. const leafXmjInfo = _.find(leafXmjList, function (item) {
  1281. return item.gcl_id === ml.gcl_id && item.id === ml.xmj_id && (item.mx_id === undefined || (item.mx_id !== undefined && item.mx_id === ml.mx_id));
  1282. });
  1283. if (leafXmjInfo) {
  1284. needUpdateList.push({ id: ml.id, contract_qty: leafXmjInfo.contract_qty, qc_qty: leafXmjInfo.qc_qty, qc_minus_qty: leafXmjInfo.qc_minus_qty });
  1285. }
  1286. }
  1287. }
  1288. }
  1289. console.log(needUpdateList);
  1290. // postData(window.location.pathname + '/save', needUpdateList, function (result) {
  1291. // materialListData2 = result.materialListData;
  1292. // });
  1293. }
  1294. });
  1295. } else {
  1296. const i = _.findIndex(materialStageData, { sid });
  1297. gclGatherData = gclGatherListData[i];
  1298. gclGatherData = gclGatherData.filter(item => {
  1299. return item.qc_qty || item.contract_qty || item.qc_minus_qty
  1300. });
  1301. if (openMaterialChecklist) {
  1302. // 取交集
  1303. const selfListGcl = _.uniq(_.map(selfList, 'gcl_id'));
  1304. gclGatherData = _.filter(gclGatherData, function (item) {
  1305. if (_.find(materialChecklistData, {
  1306. b_code: item.b_code,
  1307. name: item.name,
  1308. unit: item.unit,
  1309. unit_price: item.unit_price
  1310. })) {
  1311. return true;
  1312. } else {
  1313. const gcl_ids = item.leafXmjs ? _.uniq(_.map(item.leafXmjs, 'gcl_id')) : [];
  1314. const jiaoji = _.intersection(gcl_ids, selfListGcl);
  1315. if (jiaoji.length > 0) {
  1316. return true;
  1317. }
  1318. }
  1319. });
  1320. }
  1321. // calculateJiaCha(gclGatherData);
  1322. console.log(gclGatherData);
  1323. }
  1324. }
  1325. setListsData(isStageSelf ? materialStageData[0].sid : null, isStageSelf ? materialStageData[0].id : null);
  1326. SpreadJsObj.initSpreadSettingEvents(materialSpreadSetting, materialCol);
  1327. SpreadJsObj.initSheet(materialSpread.getActiveSheet(), materialSpreadSetting);
  1328. materialSpreadObj.getMaterialBillsData();
  1329. SpreadJsObj.loadSheetData(materialSpread.getActiveSheet(), SpreadJsObj.DataType.Data, materialBillsData);
  1330. materialSpreadObj.refreshActn();
  1331. materialMonthSpread.bind(spreadNS.Events.ClipboardPasted, materialMonthSpreadObj.clipboardPasted);
  1332. SpreadJsObj.addDeleteBind(materialMonthSpread, materialMonthSpreadObj.deletePress);
  1333. materialMonthSpread.bind(spreadNS.Events.EditEnded, materialMonthSpreadObj.editEnded);
  1334. // 期切换
  1335. $('#myTab a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
  1336. e.preventDefault();
  1337. showWaitingView();
  1338. setTimeout(function () {
  1339. materialSpreadObj.getMaterialBillsData();
  1340. materialSpreadObj.materialSheetReset(true);
  1341. // SpreadJsObj.reLoadSheetData(materialSpread.getActiveSheet());
  1342. // 消耗量表格更新
  1343. let html = '';
  1344. calcBase = _.find(calcBaseList, { ms_id: $('#myTab').find('.active').data('msid') }).calcBase;
  1345. for (let iBase = 0; iBase < calcBase.length; iBase++) {
  1346. if (!calcBase[iBase].cType || (calcBase[iBase].cType && calcBase[iBase].cType === 1 && calcBase[iBase].value)) {
  1347. html += '<tr><td>' + calcBase[iBase].name + '</td><td>' + calcBase[iBase].code + '</td><td>' + calcBase[iBase].value + '</td></tr>';
  1348. }
  1349. }
  1350. $('#calcBase').html(html);
  1351. const ms_id = parseInt($('#myTab').find('.active').data('msid'));
  1352. const msInfo = _.find(materialStageData, { id: ms_id });
  1353. setListsData(msInfo.sid, ms_id);
  1354. const data = SpreadJsObj.getSelectObject(materialSpread.getActiveSheet());
  1355. setCurBillSourceList(data ? (data.mb_id || data.id) : null, ms_id);
  1356. closeWaitingView();
  1357. }, 1000);
  1358. });
  1359. // 本条工料应耗来源数据信息
  1360. function setCurBillSourceList(mb_id, ms_id = null) {
  1361. const showSourceList = [];
  1362. if (mb_id) {
  1363. const list = _.filter(materialListData2, function (item) {
  1364. return item.mb_id === mb_id && item.ms_id === ms_id && (item.is_join === 1 || item.is_join === 2);
  1365. });
  1366. console.log(list);
  1367. if (list.length > 0) {
  1368. for (const l of list) {
  1369. const gcl = _.find(gclGatherData, function (item) {
  1370. return item.leafXmjs && item.leafXmjs.length > 0 && _.findIndex(item.leafXmjs, { gcl_id: l.gcl_id }) !== -1;
  1371. });
  1372. if (gcl) {
  1373. const index = _.findIndex(showSourceList, { code: gcl.b_code, name: gcl.name, unit: gcl.unit });
  1374. if (index === -1) {
  1375. showSourceList.push({
  1376. code: gcl.b_code,
  1377. name: gcl.name,
  1378. unit: gcl.unit,
  1379. quantity: ZhCalc.mul(calcQty(l), l.quantity),
  1380. });
  1381. } else {
  1382. showSourceList[index].quantity = ZhCalc.add(showSourceList[index].quantity, ZhCalc.mul(calcQty(l), l.quantity));
  1383. }
  1384. }
  1385. }
  1386. }
  1387. }
  1388. SpreadJsObj.loadSheetData(materialSourceSpread.getActiveSheet(), SpreadJsObj.DataType.Data, showSourceList);
  1389. }
  1390. function calcQty(info) {
  1391. let qty = '';
  1392. switch (qtySource) {
  1393. case qtySourceValueConst.gather_qty: qty = info.gather_qty; break;
  1394. case qtySourceValueConst.contract_qty: qty = info.contract_qty; break;
  1395. case qtySourceValueConst.gather_minus_qty: qty = ZhCalc.add(info.gather_qty, info.qc_minus_qty); break;
  1396. default: throw '未配置计量来源出错';
  1397. }
  1398. if (qtySource !== qtySourceValueConst.contract_qty && info.is_join === 2) {
  1399. qty = ZhCalc.sub(qty, info.qc_qty);
  1400. }
  1401. return qty;
  1402. }
  1403. if (!readOnly || editForAudit) {
  1404. $('#expr_select button').on('click', function () {
  1405. const code = $(this).text();
  1406. // $('#expr').val($('#expr').val() + code);
  1407. $('#expr').insertAtCaret(code);
  1408. });
  1409. const ExprObj = {
  1410. _checkExprValid(expr, invalidParam) {
  1411. if (!expr) return [true, null];
  1412. const param = [];
  1413. let num = '', base = '';
  1414. for (let i = 0, iLen = expr.length; i < iLen; i++) {
  1415. if (/^[\d\.%]+/.test(expr[i])) {
  1416. if (base !== '') {
  1417. param.push({type: 'base', value: base});
  1418. base = '';
  1419. }
  1420. num = num + expr[i];
  1421. } else if (/^[a-z]/.test(expr[i])) {
  1422. if (num !== '') {
  1423. param.push({type: 'num', value: num});
  1424. num = '';
  1425. }
  1426. base = base + expr[i];
  1427. } else if (expr[i] === '(') {
  1428. if (num !== '') {
  1429. param.push({type: 'num', value: num});
  1430. num = '';
  1431. }
  1432. if (base !== '') {
  1433. param.push({type: 'base', value: base});
  1434. base = '';
  1435. }
  1436. param.push({type: 'left', value: '('});
  1437. } else if (expr[i] === ')') {
  1438. if (num !== '') {
  1439. param.push({type: 'num', value: num});
  1440. num = '';
  1441. }
  1442. if (base !== '') {
  1443. param.push({type: 'base', value: base});
  1444. base = '';
  1445. }
  1446. param.push({type: 'right', value: ')'});
  1447. } else if (/^[\+\-*\/]/.test(expr[i])) {
  1448. if (num !== '') {
  1449. param.push({type: 'num', value: num});
  1450. num = '';
  1451. }
  1452. if (base !== '') {
  1453. param.push({type: 'base', value: base});
  1454. base = '';
  1455. }
  1456. param.push({type: 'calc', value: expr[i]});
  1457. } else {
  1458. return [false, '输入的表达式含有非法字符: ' + expr[i]];
  1459. }
  1460. }
  1461. if (num !== '') {
  1462. param.push({type: 'num', value: num});
  1463. num = '';
  1464. }
  1465. if (base !== '') {
  1466. param.push({type: 'base', value: base});
  1467. base = '';
  1468. }
  1469. if (param.length === 0) return true;
  1470. if (param.length > 1) {
  1471. if (param[0].value === '-') {
  1472. param[1].value = '-' + param[1];
  1473. }
  1474. param.unshift();
  1475. }
  1476. const iLen = param.length;
  1477. let iLeftCount = 0, iRightCount = 0;
  1478. for (const [i, p] of param.entries()) {
  1479. if (p.type === 'calc') {
  1480. if (i === 0 || i === iLen - 1)
  1481. return [false, '输入的表达式非法:计算符号' + p.value + '前后应有数字或计算基数'];
  1482. }
  1483. if (p.type === 'num') {
  1484. num = p.value.replace('%', '');
  1485. if (p.value.length - num.length > 1)
  1486. return [false, '输入的表达式非法:' + p.value + '不是一个有效的数字'];
  1487. num = _.toNumber(num);
  1488. if (num === undefined || num === null || _.isNaN(num))
  1489. return [false, '输入的表达式非法:' + p.value + '不是一个有效的数字'];
  1490. if (i > 0) {
  1491. if (param[i - 1].type !== 'calc') {
  1492. return [false, '输入的表达式非法:' + p.value + '前应有运算符'];
  1493. } else if (param[i - 1].value === '/' && num === 0) {
  1494. return [false, '输入的表达式非法:请勿除0'];
  1495. }
  1496. }
  1497. }
  1498. if (p.type === 'base') {
  1499. const baseParam = _.find(calcBase, {code: p.value});
  1500. if (!baseParam)
  1501. return [false, '输入的表达式非法:不存在计算基数' + p.value];
  1502. if (invalidParam && invalidParam.indexOf(p.value) >= 0)
  1503. return [false, '不可使用计算基数' + p.value];
  1504. if (i > 0 && (param[i - 1].type === 'num' || param[i - 1].type === 'right'))
  1505. return [false, '输入的表达式非法:' + p.value + '前应有运算符'];
  1506. }
  1507. if (p.type === 'left') {
  1508. iLeftCount += 1;
  1509. if (i !== 0 && param[i-1].type !== 'calc')
  1510. return [false, '输入的表达式非法:(前应有运算符'];
  1511. }
  1512. if (p.type === 'right') {
  1513. iRightCount += 1;
  1514. if (i !== iLen - 1 && param[i+1].type !== 'calc')
  1515. return [false, '输入的表达式非法:)后应有运算符'];
  1516. if (iRightCount > iLeftCount)
  1517. return [false, '输入的表达式非法:")"前无对应的"("'];
  1518. }
  1519. }
  1520. if (iLeftCount > iRightCount)
  1521. return [false, '输入的表达式非法:"("后无对应的")"'];
  1522. return [true, ''];
  1523. },
  1524. _checkExpr: function (text) {
  1525. if (text) {
  1526. const num = _.toNumber(text);
  1527. if (num) {
  1528. console.log(num);
  1529. } else {
  1530. const expr = text.replace('=', '').toLowerCase();
  1531. const [valid, msg] = this._checkExprValid(expr);
  1532. if (!valid) return [valid, msg];
  1533. }
  1534. }
  1535. return [true, ''];
  1536. },
  1537. };
  1538. $('#expr_btn').click(function () {
  1539. const expr = $('#expr').val();
  1540. // 判断表达式格式
  1541. const [valid, msg] = ExprObj._checkExpr(expr);
  1542. if (!valid) {
  1543. toastr.error(msg);
  1544. return false;
  1545. }
  1546. postData(window.location.pathname + '/save', { type:'expr', id: $('#materialbillsId').val(), expr: expr, ms_id: $('#myTab').find('.active').data('msid') || null }, function (result) {
  1547. m_tp = result.m_tp;
  1548. if (materialTax) {
  1549. m_tax_tp = result.m_tax_tp;
  1550. }
  1551. const sheet = materialSpread.getActiveSheet();
  1552. const select = SpreadJsObj.getSelectObject(sheet);
  1553. const index = _.findIndex(materialBillsData, { id: select.id });
  1554. let newIndex = index;
  1555. if (isStageSelf) {
  1556. materialSpreadObj.updateMaterialData(result);
  1557. result.info = materialSpreadObj.updateOneMaterialBill(result.info);
  1558. }
  1559. if($('#bills0_list').is(':checked')) {
  1560. const newMaterialBillsData = _.filter(materialBillsData, function (item) {
  1561. return item.quantity !== null && item.quantity !== 0;
  1562. });
  1563. newIndex = _.findIndex(newMaterialBillsData, { id: select.id });
  1564. // newIndex = newMaterialBillsData.indexOf(select);
  1565. newMaterialBillsData.splice(newIndex, 1, result.info);
  1566. sheet.zh_data[newIndex] = result.info;
  1567. }
  1568. materialBillsData.splice(index, 1, result.info);
  1569. sheet.zh_data.splice(newIndex, 1, result.info);
  1570. SpreadJsObj.reLoadRowData(sheet, newIndex);
  1571. resetTpTable();
  1572. $('#bcyy').modal('hide');
  1573. });
  1574. });
  1575. }
  1576. if (!readOnly && !editForAudit) {
  1577. $('#add').click(materialSpreadObj.add);
  1578. $('#del').click(materialSpreadObj.del);
  1579. $('#up-move').click(materialSpreadObj.upMove);
  1580. $('#down-move').click(materialSpreadObj.downMove);
  1581. // 右键菜单
  1582. $.contextMenu({
  1583. selector: '#material-spread',
  1584. build: function ($trigger, e) {
  1585. const target = SpreadJsObj.safeRightClickSelection($trigger, e, materialSpread);
  1586. return target.hitTestType === GC.Spread.Sheets.SheetArea.viewport || target.hitTestType === GC.Spread.Sheets.SheetArea.rowHeader;
  1587. },
  1588. items: {
  1589. 'create': {
  1590. name: '新增材料',
  1591. icon: 'fa-sign-in',
  1592. callback: function (key, opt) {
  1593. materialSpreadObj.add(materialSpread.getActiveSheet());
  1594. },
  1595. },
  1596. 'delete': {
  1597. name: '删除材料',
  1598. icon: 'fa-remove',
  1599. callback: function (key, opt) {
  1600. materialSpreadObj.del(materialSpread.getActiveSheet());
  1601. },
  1602. disabled: function (key, opt) {
  1603. const sheet = materialSpread.getActiveSheet();
  1604. const select = SpreadJsObj.getSelectObject(sheet);
  1605. const sel = sheet.getSelections()[0];
  1606. materialSpreadObj.refreshActn(sel.rowCount);
  1607. if (!readOnly && select && materialBase.isUsed(select) && sel.rowCount === 1) {
  1608. return false;
  1609. } else {
  1610. return true;
  1611. }
  1612. }
  1613. },
  1614. }
  1615. });
  1616. $('.changeRate').click(function () {
  1617. $('#rateInput').val(parseInt($(this).data('value')));
  1618. $('#rateInput').siblings('.dropdown-menu').hide();
  1619. });
  1620. $('#rateInput').click(function () {
  1621. $(this).siblings('.dropdown-menu').show();
  1622. })
  1623. // 回车提交
  1624. $('#rateInput').on('keypress', function () {
  1625. if(window.event.keyCode === 13) {
  1626. $(this).blur();
  1627. }
  1628. });
  1629. $('#rateInput').blur(function () {
  1630. const _self = $(this);
  1631. setTimeout(function () {
  1632. let rate = parseFloat(_self.val());
  1633. if (_.isNaN(rate)) {
  1634. toastr.error('请输入0-100之前的整数值');
  1635. $('#rateInput').val(materialRate);
  1636. return;
  1637. }
  1638. rate = _.round(rate);
  1639. if(rate < 0 || rate > 100) {
  1640. toastr.error('请输入0-100之前的整数值');
  1641. $('#rateInput').val(materialRate);
  1642. return;
  1643. }
  1644. $('#rateInput').siblings('.dropdown-menu').hide();
  1645. console.log(rate, materialRate);
  1646. if (rate !== materialRate) {
  1647. postData(window.location.pathname + '/save', { type:'rate', rate: rate }, function (result) {
  1648. const bqhs = ZhCalc.round(ZhCalc.mul(m_tp, 1+rate/100), materialDecimal.tp);
  1649. // const exbqhs = ZhCalc.round(ZhCalc.mul(ex_tp, 1+rate/100), materialDecimal.tp);
  1650. const jzbqhs = ZhCalc.round(ZhCalc.add(pre_tp_hs, bqhs), materialDecimal.tp);
  1651. // const exjzbqhs = ZhCalc.round(ZhCalc.add(ex_pre_tp_hs, exbqhs), materialDecimal.tp);
  1652. $('#rate_set').find('td').eq(1).text(bqhs !== 0 ? bqhs : '');
  1653. $('#rate_set').find('td').eq(2).text(jzbqhs !== 0 ? jzbqhs : '');
  1654. // $('#rate_set').find('td').eq(3).text(exbqhs !== 0 ? exbqhs : '');
  1655. // $('#rate_set').find('td').eq(4).text(exjzbqhs !== 0 ? exjzbqhs : '');
  1656. materialRate = rate;
  1657. $('#rateInput').val(rate);
  1658. });
  1659. } else {
  1660. $('#rateInput').val(rate);
  1661. }
  1662. }, 500);
  1663. });
  1664. // $('#changeRate').change(function () {
  1665. // const rate = parseInt($(this).val());
  1666. // postData(window.location.pathname + '/save', { type:'rate', rate: rate }, function (result) {
  1667. // const bqhs = ZhCalc.round(ZhCalc.mul(m_tp, 1+rate/100), materialDecimal.tp);
  1668. // const exbqhs = ZhCalc.round(ZhCalc.mul(ex_tp, 1+rate/100), materialDecimal.tp);
  1669. // const jzbqhs = ZhCalc.round(ZhCalc.add(pre_tp_hs, bqhs), materialDecimal.tp);
  1670. // const exjzbqhs = ZhCalc.round(ZhCalc.add(ex_pre_tp_hs, exbqhs), materialDecimal.tp);
  1671. // $('#rate_set').find('td').eq(1).text(bqhs !== 0 ? bqhs : '');
  1672. // $('#rate_set').find('td').eq(2).text(jzbqhs !== 0 ? jzbqhs : '');
  1673. // $('#rate_set').find('td').eq(3).text(exbqhs !== 0 ? exbqhs : '');
  1674. // $('#rate_set').find('td').eq(4).text(exjzbqhs !== 0 ? exjzbqhs : '');
  1675. // });
  1676. // });
  1677. // 创建月信息价
  1678. $('#make-month').click(function() {
  1679. const yearmonth = $('#months').val();
  1680. const result = yearmonth.match(/^(\d{1,4})(-|\/)(\d{1,2})$/);
  1681. if (result === null) {
  1682. toastr.error('请选择正确的信息价月份');
  1683. return false;
  1684. }
  1685. //判断是否已存在当前月份
  1686. if (months.indexOf(yearmonth) !== -1) {
  1687. toastr.error('调差期已创建过本月的信息价');
  1688. return false;
  1689. }
  1690. postData(window.location.pathname + '/month/save', { type: 'add', updateData: { yearmonth: yearmonth } }, function (data) {
  1691. months.push(yearmonth);
  1692. months.sort();
  1693. monthsList = data.monthsList;
  1694. monthFunGather.monthsListSet();
  1695. // 工料表单价显示也要更新
  1696. materialBillsData = data.materialBillsData;
  1697. materialSpreadObj.materialSheetReset();
  1698. // SpreadJsObj.loadSheetData(materialSpread.getActiveSheet(), SpreadJsObj.DataType.Data, materialBillsData);
  1699. m_tp = data.m_tp;
  1700. if (materialTax) {
  1701. m_tax_tp = data.m_tax_tp;
  1702. }
  1703. resetTpTable();
  1704. $('#add-month').modal('hide');
  1705. });
  1706. });
  1707. // 删除月信息价
  1708. $('#del-month').click(function () {
  1709. const del_month_array = [];
  1710. $("input[name='del_month']:checked").each(function () {
  1711. del_month_array.push($(this).val());
  1712. });
  1713. if (del_month_array.length === 0) {
  1714. toastr.error('请选择要移除的月信息价');
  1715. return false;
  1716. }
  1717. postData(window.location.pathname + '/month/save', { type: 'del', updateData: { del_yearmonth: del_month_array } }, function (data) {
  1718. for (const dm of del_month_array) {
  1719. // _.remove(materialMonthSpreadSetting.cols, function (n) {
  1720. // return n.field === dm;
  1721. // });
  1722. _.remove(months, function (n) {
  1723. return n === dm;
  1724. });
  1725. }
  1726. monthsList = data.monthsList;
  1727. monthFunGather.monthsListSet();
  1728. // 工料表单价显示也要更新
  1729. materialBillsData = data.materialBillsData;
  1730. materialSpreadObj.materialSheetReset();
  1731. // SpreadJsObj.loadSheetData(materialSpread.getActiveSheet(), SpreadJsObj.DataType.Data, materialBillsData);
  1732. m_tp = data.m_tp;
  1733. if (materialTax) {
  1734. m_tax_tp = data.m_tax_tp;
  1735. }
  1736. resetTpTable();
  1737. $('#remove-month').modal('hide');
  1738. });
  1739. });
  1740. // 导入功能
  1741. let importFile = null;
  1742. $('#upload-xls-file').change(function () {
  1743. const file = this.files[0];
  1744. importFile = file;
  1745. });
  1746. $('#import-bills-btn').click(function() {
  1747. if (!importFile) {
  1748. toastr.error('请选择excel文件再确定');
  1749. return;
  1750. }
  1751. const ext = importFile.name.toLowerCase().split('.').splice(-1)[0];
  1752. const imgStr = /(xls|xlsx|XLS|XLSX)$/;
  1753. if (!imgStr.test(ext)) {
  1754. toastr.error('请导入正确格式的excel文件。');
  1755. return
  1756. }
  1757. const fileReader = new FileReader();
  1758. fileReader.onload = async function(ev) {
  1759. try{
  1760. const data = ev.target.result;
  1761. let tree = [];
  1762. const includeSpec = $('#xls-spec').is(':checked');
  1763. if (/(xls|xlsx|XLS|XLSX)$/.test(ext)) {
  1764. const workbook = XLSX.read(data, {type: 'binary'}); // 以二进制流方式读取得到整份excel表格对象
  1765. const jsonData = transExcel(XLSX.utils.sheet_to_json(workbook.Sheets[workbook.SheetNames[0]], { defval: null }));
  1766. console.log(jsonData);
  1767. if (!(jsonData[0] && jsonData[0].code !== undefined && jsonData[0].code !== null && jsonData[0].unit !== undefined && jsonData[0].name !== undefined &&
  1768. jsonData[0].spec !== undefined && jsonData[0].msg_tp !== undefined && jsonData[0].msg_times !== undefined)) {
  1769. throw 'excel必须按指定格式内容上传';
  1770. }
  1771. // 去重
  1772. tree = _.unionWith(jsonData, function (item1, item2) {
  1773. return item1.code === item2.code && item1.name === item2.name && item1.unit === item2.unit && (!includeSpec || (includeSpec && item1.spec === item2.spec));
  1774. });
  1775. }
  1776. if (tree.length === 0) {
  1777. throw '该excel不存在导入的数据';
  1778. }
  1779. // 判断msg_tp格式,不符合则无法导入,还有excel日期转换问题
  1780. for (const t of tree) {
  1781. t.code = t.code.toString();
  1782. if (t.msg_tp) {
  1783. if (_.isNumber(t.msg_tp)) {
  1784. const num = parseFloat(t.msg_tp);
  1785. const reg = materialDecimal.up ? new RegExp("^\\d+(\\.\\d{1,"+ materialDecimal.up +"})?$") : new RegExp("^\\d+?$");
  1786. if (num < 0 || !reg.test(num)) {
  1787. t.msg_tp = ZhCalc.round(num, materialDecimal.up);
  1788. }
  1789. } else {
  1790. throw '信息价数据存在非数字字符,无法导入';
  1791. }
  1792. }
  1793. if (_.isNumber(t.msg_times)) {
  1794. t.msg_times = formatDate(t.msg_times, '/');
  1795. }
  1796. t.spec = t.spec === null ? '' : t.spec;
  1797. }
  1798. console.log(tree);
  1799. postData(window.location.pathname + '/save', { type:'export', postData: tree, includeSpec, ms_id: $('#myTab').find('.active').data('msid') || null }, function (result) {
  1800. m_tp = result.m_tp;
  1801. if (materialTax) {
  1802. m_tax_tp = result.m_tax_tp;
  1803. }
  1804. materialBillsData = result.billsData;
  1805. if (isStageSelf) {
  1806. materialStageData = result.stageData;
  1807. materialStageBillsData = result.stageBillsData;
  1808. }
  1809. materialSpreadObj.getMaterialBillsData();
  1810. materialSpreadObj.materialSheetReset(true);
  1811. if (months.length > 0) {
  1812. monthsList = result.monthsList;
  1813. monthFunGather.monthsListSet();
  1814. }
  1815. resetTpTable();
  1816. if (result.addNum || result.updateNum) {
  1817. let msg = [];
  1818. if (result.addNum !== undefined && result.addNum !== 0) {
  1819. msg.push('成功导入工料 ' + result.addNum + ' 条');
  1820. }
  1821. if (result.updateNum !== undefined && result.updateNum !== 0) {
  1822. msg.push('成功更新工料 ' + result.updateNum + ' 条');
  1823. }
  1824. toastr.success(msg.length > 1 ? msg.join(',并') : msg[0]);
  1825. } else {
  1826. toastr.warning('不存在可导入的信息价数据');
  1827. }
  1828. $('#import-info').modal('hide');
  1829. });
  1830. } catch (error) {
  1831. console.log(error);
  1832. toastr.error(error);
  1833. return
  1834. }
  1835. };
  1836. // 以二进制方式打开文件
  1837. fileReader.readAsBinaryString(importFile);
  1838. $('#upload-xls-file').val('');
  1839. importFile = null;
  1840. });
  1841. } else {
  1842. // SpreadJsObj.forbiddenSpreadContextMenu('#material-spread', materialSpread);
  1843. }
  1844. // 筛选调差工料本期应耗数量为0
  1845. $('#bills0_list').click(function () {
  1846. const isCheck = $(this).is(':checked');
  1847. let newMaterialBillsData = materialBillsData;
  1848. if (isCheck) {
  1849. newMaterialBillsData = _.filter(materialBillsData, function (item) {
  1850. return item.quantity !== null && item.quantity !== 0;
  1851. });
  1852. SpreadJsObj.initSpreadSettingEvents(materialSpreadSetting, materialCol);
  1853. SpreadJsObj.initSheet(materialSpread.getActiveSheet(), materialSpreadSetting);
  1854. }
  1855. SpreadJsObj.loadSheetData(materialSpread.getActiveSheet(), SpreadJsObj.DataType.Data, newMaterialBillsData);
  1856. monthFunGather.monthsListSet(newMaterialBillsData, false);
  1857. materialSpreadObj.refreshActn();
  1858. });
  1859. let gljLib;
  1860. const gljLibCellDoubleClick = function (e, info) {
  1861. if (readOnly || editForAudit) return;
  1862. const gljSheet = info.sheet;
  1863. if (!gljSheet.zh_setting || !gljSheet.zh_tree) { return; }
  1864. const gljTree = gljSheet.zh_tree;
  1865. const gljNode = gljTree.nodes[info.row];
  1866. const sheet = materialSpread.getActiveSheet();
  1867. const select = SpreadJsObj.getSelectObject(sheet);
  1868. if (!gljNode) return;
  1869. if (gljNode && gljNode.is_leaf === 0) {
  1870. toastr.warning('非最底层项目下,不应添加');
  1871. return;
  1872. }
  1873. postData(window.location.pathname + '/save', { type:'add-glj', postData: gljNode, order: select ? select.order : null }, function (result) {
  1874. if (result) {
  1875. // 比order大的+1
  1876. const maxData = select ? _.filter(materialBillsData, (item) => { return item.order > select.order }) : [];
  1877. _.forEach(maxData, function (item) {
  1878. const cl = _.find(materialBillsData, { id: item.id });
  1879. cl.order = item.order + 1;
  1880. });
  1881. materialBillsData.push(result.info);
  1882. materialBillsData.sort(function (a, b) {
  1883. return a.order - b.order
  1884. });
  1885. console.log(materialBillsData);
  1886. if (isStageSelf) {
  1887. materialSpreadObj.updateMaterialData(result);
  1888. materialSpreadObj.getMaterialBillsData();
  1889. }
  1890. // sheet.addRows(materialBillsData.length - 1, 1);
  1891. // SpreadJsObj.reLoadRowData(sheet, materialBillsData.length - 1);
  1892. let newMaterialBillsData = materialBillsData;
  1893. if($('#bills0_list').is(':checked')) {
  1894. newMaterialBillsData = _.filter(materialBillsData, function (item) {
  1895. return item.quantity !== null && item.quantity !== 0;
  1896. });
  1897. }
  1898. SpreadJsObj.loadSheetData(materialSpread.getActiveSheet(), SpreadJsObj.DataType.Data, newMaterialBillsData);
  1899. // materialSpreadObj.materialSheetReset();
  1900. sheet.setSelection(sheet.zh_data.indexOf(select) + 1, 0, 1, 1);
  1901. materialSpreadObj.refreshActn();
  1902. // 月信息价需要同时添加空白的list
  1903. if (months.length > 0) {
  1904. 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 };
  1905. for (const m of months) {
  1906. one_month[m] = null;
  1907. }
  1908. _.forEach(maxData, function (item) {
  1909. const cl = _.find(monthsList, { mb_id: item.id });
  1910. cl.order = item.order + 1;
  1911. });
  1912. monthsList.push(one_month);
  1913. monthsList.sort(function (a, b) {
  1914. return a.order - b.order
  1915. });
  1916. console.log(monthsList);
  1917. // const newMonthsList = _.filter(monthsList, function (item) {
  1918. // return _.findIndex(newMaterialBillsData, { code: item.code }) !== -1;
  1919. // });
  1920. // materialMonthSpread.getActiveSheet().addRows(newMonthsList.length - 1, 1);
  1921. // SpreadJsObj.reLoadRowData(materialMonthSpread.getActiveSheet(), newMonthsList.length - 1);
  1922. monthFunGather.monthsListSet();
  1923. // materialMonthSpreadObj.monthsListSet();
  1924. }
  1925. }
  1926. });
  1927. };
  1928. const gljLibSetting = {
  1929. selector: '#material-tab',
  1930. stdType: 'glj',
  1931. libs: gljLists,
  1932. treeSetting: {
  1933. id: 'glj_id',
  1934. pid: 'glj_pid',
  1935. order: 'order',
  1936. level: 'level',
  1937. rootId: -1,
  1938. keys: ['id', 'list_id', 'glj_id'],
  1939. },
  1940. spreadSetting: {
  1941. cols: [
  1942. {title: '编号', field: 'code', hAlign: 0, width: 120, formatter: '@', readOnly: true, cellType: 'tree'},
  1943. {title: '名称', field: 'name', hAlign: 0, width: 150, formatter: '@', readOnly: true},
  1944. {title: '单位', field: 'unit', hAlign: 1, width: 50, formatter: '@', readOnly: true},
  1945. {title: '参考上涨幅度', field: 'rise_range', hAlign: 1, width: 50, formatter: '@', readOnly: true},
  1946. {title: '参考下跌幅度', field: 'fall_range', hAlign: 1, width: 50, formatter: '@', readOnly: true},
  1947. ],
  1948. treeCol: 0,
  1949. emptyRows: 0,
  1950. headRows: 1,
  1951. headRowHeight: [32],
  1952. defaultRowHeight: 21,
  1953. headerFont: '12px 微软雅黑',
  1954. font: '12px 微软雅黑',
  1955. headColWidth: [30],
  1956. selectedBackColor: '#fffacd',
  1957. },
  1958. spreadClassCss: 'sjs-material',
  1959. cellDoubleClick: gljLibCellDoubleClick,
  1960. page: 'material',
  1961. tid: window.location.pathname.split('/')[2],
  1962. };
  1963. $.subMenu({
  1964. menu: '#sub-menu', miniMenu: '#sub-mini-menu', miniMenuList: '#mini-menu-list',
  1965. toMenu: '#to-menu', toMiniMenu: '#to-mini-menu',
  1966. key: 'menu.1.0.0',
  1967. miniHint: '#sub-mini-hint', hintKey: 'menu.hint.1.0.1',
  1968. callback: function (info) {
  1969. if (info.mini) {
  1970. $('.panel-title').addClass('fluid');
  1971. $('#sub-menu').removeClass('panel-sidebar');
  1972. } else {
  1973. $('.panel-title').removeClass('fluid');
  1974. $('#sub-menu').addClass('panel-sidebar');
  1975. }
  1976. autoFlashHeight();
  1977. materialSpread.refresh();
  1978. }
  1979. });
  1980. $.divResizer({
  1981. select: '#right-spr',
  1982. callback: function () {
  1983. materialSpread.refresh();
  1984. materialMonthSpread.refresh();
  1985. if (gljLib) gljLib.spread.refresh();
  1986. const width = (($('#right-view').width()/$('#right-view').parent('div').width())*100).toFixed();
  1987. setLocalCache('material_month_' + materialID, width);
  1988. }
  1989. });
  1990. $.divResizer({
  1991. select: '#main-resize',
  1992. callback: function () {
  1993. let bcontent = $(".bcontent-wrap") ? $(".bcontent-wrap").height() : 0;
  1994. $(".sp-wrap").height(bcontent-30);
  1995. const height = $('.bcontent-wrap').height();
  1996. const cutHeight = isStageSelf ? getObjHeight($('#myTab')) : 0;
  1997. $('.sjs-height-material').height($('.sjs-height-1').height() - cutHeight);
  1998. updateSjsHeight();
  1999. materialSpread.refresh();
  2000. materialMonthSpread.refresh();
  2001. if (gljLib) gljLib.spread.refresh();
  2002. setLocalCache('material_bills_' + materialID, height);
  2003. }
  2004. });
  2005. // 展开收起月信息价并浏览器记住本期展开收起
  2006. $('a', '.right-nav').bind('click', function () {
  2007. //const main = $('#main-view'), tool = $('#tools-view');
  2008. const tab = $(this), tabPanel = $(tab.attr('content'));
  2009. if (!tab.hasClass('active')) {
  2010. $('a', '.side-menu').removeClass('active');
  2011. $('#right-view .tab-content .tab-pane').removeClass('active');
  2012. tab.addClass('active');
  2013. tabPanel.addClass('active');
  2014. showSideTools(tab.hasClass('active'));
  2015. if (tab.attr('content') === '#month-tab') {
  2016. const width = (($('#right-view').width()/$('#right-view').parent('div').width())*100).toFixed();
  2017. setLocalCache('material_month_' + materialID, width);
  2018. materialMonthSpread.refresh();
  2019. } else if (tab.attr('content') === '#material-tab') {
  2020. if (!gljLib) {
  2021. gljLib = $.gljLib(gljLibSetting);
  2022. }
  2023. updateSjsHeight();
  2024. gljLib.spread.refresh();
  2025. }
  2026. } else {
  2027. removeLocalCache('material_month_' + materialID);
  2028. tab.removeClass('active');
  2029. tabPanel.removeClass('active');
  2030. showSideTools(tab.hasClass('active'));
  2031. }
  2032. materialSpread.refresh();
  2033. // materialMonthSpread.refresh();
  2034. });
  2035. // 根据浏览器记录展开收起
  2036. if (getLocalCache('material_month_' + materialID) && !isStageSelf) {
  2037. const tab = $('.right-nav a[content="#month-tab"]'), tabPanel = $(tab.attr('content'));
  2038. $('a', '.side-menu').removeClass('active');
  2039. $('.tab-content .tab-pane').removeClass('active');
  2040. tab.addClass('active');
  2041. tabPanel.addClass('active');
  2042. $('#right-view').width(getLocalCache('material_month_' + materialID) + '%');
  2043. showSideTools(tab.hasClass('active'));
  2044. materialSpread.refresh();
  2045. materialMonthSpread.refresh();
  2046. }
  2047. const cutHeight = isStageSelf ? getObjHeight($('#myTab')) : 0;
  2048. if (getLocalCache('material_bills_' + materialID)) {
  2049. $('.bcontent-wrap').height(getLocalCache('material_bills_' + materialID));
  2050. const cHeader = getObjHeight($(".c-header"));
  2051. const bcontent = $(".bcontent-wrap") ? $(".bcontent-wrap").height() : 0;
  2052. $(".sp-wrap").height(bcontent-30);
  2053. $('.sjs-height-1').height($(window).height()-cHeader-bcontent-90+53);
  2054. $('.sjs-height-material').height($('.sjs-height-1').height() - cutHeight);
  2055. updateSjsHeight();
  2056. materialSpread.refresh();
  2057. materialMonthSpread.refresh();
  2058. if (gljLib) gljLib.spread.refresh();
  2059. } else {
  2060. $('.sjs-height-material').height($('.sjs-height-1').height() - cutHeight);
  2061. updateSjsHeight();
  2062. materialSpread.refresh();
  2063. materialMonthSpread.refresh();
  2064. if (gljLib) gljLib.spread.refresh();
  2065. }
  2066. function updateSjsHeight() {
  2067. $('.sjs-height-material-month').height($('.sjs-height-1').height() - getObjHeight($('.sjs-bar-1')));
  2068. $('.material-table-height').height($('.bcontent-wrap').height() - getObjHeight($('.bc-bar')));
  2069. $('.sjs-material').height($('.sjs-height-1').height() - getObjHeight($('.sjs-bar')));
  2070. }
  2071. function getObjHeight(select) {
  2072. return select.length > 0 ? select.height() : 0;
  2073. }
  2074. function formatDate(numb, format) {
  2075. const old = numb - 1;
  2076. const t = Math.round((old - Math.floor(old)) * 24 * 60 * 60);
  2077. const time = new Date(1900, 0, old, 0, 0, t);
  2078. const year = time.getFullYear();
  2079. const month = time.getMonth() + 1;
  2080. const date = time.getDate();
  2081. return year + format + (month < 10 ? '0' + month : month) + format + (date < 10 ? '0' + date : date);
  2082. }
  2083. function transExcel(results) {
  2084. const mapInfo = {
  2085. '编号': 'code',
  2086. '名称': 'name',
  2087. '单位': 'unit',
  2088. '规格': 'spec',
  2089. '信息价': 'msg_tp',
  2090. '时间': 'msg_times',
  2091. };
  2092. return results.map(zhObj => {
  2093. const enObj = {}
  2094. const zhKeys = Object.keys(zhObj);
  2095. zhKeys.forEach(zhKey => {
  2096. const enKey = mapInfo[zhKey];
  2097. enObj[enKey] = zhObj[zhKey] && _.isString(zhObj[zhKey]) ? _.trim(zhObj[zhKey]) : zhObj[zhKey];
  2098. });
  2099. return enObj
  2100. })
  2101. }
  2102. });