equipment_purchase_view.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. let unitOptions = ['m', 'm2', 'm3', 'km', 't', 'kg', '台班', '工日', '昼夜', '元', '项', '处', '个', '件',
  2. '根', '组', '系统', '台', '套', '株', '丛', '缸', '支', '只', '块', '座', '对', '份', '樘', '攒', '榀']
  3. let equipmentPurchaseObj = {
  4. setting:{
  5. header: [
  6. {headerName: "编号", headerWidth: 160, dataCode: "code", dataType: "String",formatter: "@"},
  7. {headerName: "设备名称", headerWidth: 200, dataCode: "name", dataType: "String"},
  8. {headerName: "设备价格-设备原价", headerWidth: 160, dataCode: "originalPrice", hAlign: "right", dataType: "Number",validator:'number'},
  9. {headerName: "设备运杂费", headerWidth: 160, dataCode: "freight", hAlign: "right", dataType: "Number",validator:'number'},
  10. {headerName: "备品备件费", headerWidth: 160, dataCode: "sparePartCost", hAlign: "right", dataType: "Number",validator:'number'},
  11. {headerName: "单价", headerWidth: 160, dataCode: "unitPrice", hAlign: "right", dataType: "Number",validator:'number'},
  12. {headerName: "单位", headerWidth: 60, dataCode: "unit", dataType: "String",hAlign: "center",cellType:'comboBox',editable:true,options:unitOptions},
  13. {headerName: "数量", headerWidth: 160, dataCode: "quantity", hAlign: "right", dataType: "Number",validator:'number'},
  14. {headerName: "金额", headerWidth: 160, dataCode: "totalPrice", hAlign: "right", dataType: "Number"},
  15. ],
  16. view: {
  17. lockColumns: ["totalPrice"],
  18. rowHeaderWidth:40,
  19. colHeaderHeight:35
  20. }
  21. },
  22. sheet:null,
  23. initSpread:function () {
  24. if(this.sheet == null){
  25. this.spread = SheetDataHelper.createNewSpread($("#equipmentSpread")[0]);
  26. sheetCommonObj.spreadDefaultStyle(this.spread);
  27. this.sheet = this.spread.getSheet(0);
  28. sheetCommonObj.initSheet(this.sheet, this.setting, 0);
  29. this.sheet.bind(GC.Spread.Sheets.Events.ValueChanged,this.onValueChange);
  30. this.sheet.bind(GC.Spread.Sheets.Events.RangeChanged, this.onSheetRangeChange);
  31. this.sheet.bind(GC.Spread.Sheets.Events.SelectionChanged, function (e,args) {
  32. args.sheet.repaint();
  33. });
  34. if (projectReadOnly) {
  35. sheetCommonObj.disableSpread(this.spread);
  36. } else {
  37. this.initRightClick();
  38. }
  39. }
  40. },
  41. showData:function(){
  42. let equipment_purchase = projectObj.project.equipment_purchase;
  43. this.data = equipment_purchase.datas.equipments;
  44. _.sortBy(this.data,['code']);
  45. sheetCommonObj.showData(this.sheet, this.setting,this.data);
  46. this.sheet.setRowCount(this.data.length);
  47. },
  48. onValueChange:function (e,info) {
  49. let me = equipmentPurchaseObj,row = info.row, col = info.col;
  50. let dataCode = me.setting.header[col].dataCode;
  51. let value = info.newValue;
  52. let equipment = me.data[row];
  53. if (value&&! sheetCommonObj.checkData(col,me.setting,value)) {
  54. alert('输入的数据类型不对,请重新输入!');
  55. return me.showData();
  56. }
  57. let data = {doc:{},ID:equipment.ID};
  58. if(dataCode == 'quantity' || dataCode == 'unitPrice'){
  59. me.calcTotalPrice(value,dataCode,data.doc,equipment);
  60. }
  61. if(equipment[dataCode] == value) return me.showData();
  62. data.doc[dataCode] = value;
  63. me.updateEquipments([data]);
  64. },
  65. calcTotalPrice:function(newValue,dataCode,doc,equipment){
  66. let unitPrice = equipment.unitPrice?scMathUtil.roundForObj(equipment.unitPrice,getDecimal('glj.unitPrice')):0;
  67. let quantity = equipment.quantity?scMathUtil.roundForObj(equipment.quantity,getDecimal('glj.quantity')):0;
  68. let quantity = equipment.quantity?scMathUtil.roundForObj(equipment.quantity,getDecimal('glj.quantity')):0;
  69. let quantity = equipment.quantity?scMathUtil.roundForObj(equipment.quantity,getDecimal('glj.quantity')):0;
  70. if(newValue){
  71. if(gljUtil.isDef(doc.unitPrice)) unitPrice = doc.unitPrice;
  72. if(gljUtil.isDef(doc.quantity)) quantity = doc.quantity;
  73. if(dataCode === 'quantity') {
  74. newValue = scMathUtil.roundForObj(newValue,getDecimal('glj.quantity'));
  75. quantity = newValue;
  76. }
  77. if(dataCode === 'unitPrice') {
  78. newValue = scMathUtil.roundForObj(newValue,getDecimal('glj.unitPrice'));
  79. unitPrice = newValue;
  80. }
  81. doc.totalPrice = scMathUtil.roundForObj(quantity * unitPrice,getDecimal('glj.unitPrice'));
  82. }
  83. },
  84. onSheetRangeChange:function(e,args){
  85. let updateMap = {};
  86. let updateData = []
  87. let me = equipmentPurchaseObj;
  88. for(let c of args.changedCells){
  89. let dataCode = me.setting.header[c.col].dataCode;
  90. let value= args.sheet.getCell(c.row, c.col).text();
  91. let equipment = me.data[c.row];
  92. if (value&&!sheetCommonObj.checkData(c.col,me.setting,value)) {
  93. alert('输入的数据类型不对,请重新输入!');
  94. me.showData();
  95. return ;
  96. }
  97. let tem = updateMap[equipment.ID]?updateMap[equipment.ID]:{};
  98. if(dataCode == 'quantity' || dataCode == 'unitPrice'){
  99. me.calcTotalPrice(value,dataCode,tem,equipment);
  100. }
  101. tem[dataCode] = value;
  102. updateMap[equipment.ID] = tem;
  103. }
  104. for(let ID in updateMap){
  105. let data = {doc:updateMap[ID],ID:ID};
  106. updateData.push(data);
  107. }
  108. if(updateData.length > 0) me.updateEquipments(updateData);
  109. },
  110. newEquipment:function(){
  111. return {ID:uuid.v1()}
  112. },
  113. updateEquipments:async function(updateData){
  114. try {
  115. $.bootstrapLoading.start();
  116. let projectID = projectObj.project.ID();
  117. await ajaxPost('/equipmentPurchase/updateEquipments', { projectID, updateData });
  118. for(let data of updateData){
  119. let equipment = _.find(this.data,{ID:data.ID});
  120. if(equipment){
  121. Object.assign(equipment,data.doc);
  122. }
  123. }
  124. } catch (error) {
  125. alert('更新失败,请重试');
  126. }
  127. this.showData();
  128. $.bootstrapLoading.end();
  129. },
  130. insertEquipments:async function(equipments){
  131. try {
  132. $.bootstrapLoading.start();
  133. let projectID = projectObj.project.ID();
  134. await ajaxPost('/equipmentPurchase/insertData', { projectID, equipments });
  135. this.data.push(...equipments)
  136. this.showData();
  137. } catch (error) {
  138. alert('插入失败,请重试');
  139. }
  140. $.bootstrapLoading.end();
  141. },
  142. deleteEquipment:async function(ID){
  143. try {
  144. let projectID = projectObj.project.ID();
  145. await ajaxPost('/equipmentPurchase/deleteEquipment', { projectID, ID });
  146. _.remove(this.data,{ID});
  147. this.showData();
  148. } catch (error) {
  149. alert('删除失败,请重试');
  150. }
  151. },
  152. registerInputContextMenuItem:function(){
  153. const insertEquipmentHtml = `<span>插入&nbsp;&nbsp;<input id='insert-equipment-number' class="menu-input" type="text" value="1" onfocus="this.select()">&nbsp;&nbsp;行</span>`;
  154. let me = this;
  155. return sheetCommonObj.registerInputContextMenuItem('insertEquipment', insertEquipmentHtml, 'fa-sign-in', async function () {
  156. const number = +$('#insert-equipment-number').val();
  157. if (!number) {
  158. return;
  159. }
  160. const newData = [];
  161. for (let i = 0; i < number; i++) {
  162. newData.push(me.newEquipment());
  163. }
  164. me.insertEquipments(newData)
  165. });
  166. },
  167. initRightClick: function () {
  168. let me = this;
  169. $.contextMenu({
  170. selector: '#equipmentSpread',
  171. build: function ($trigger, e) {
  172. me.rightClickTarget = SheetDataHelper.safeRightClickSelection($trigger, e, me.spread);
  173. return me.rightClickTarget.hitTestType === GC.Spread.Sheets.SheetArea.viewport ||
  174. me.rightClickTarget.hitTestType === GC.Spread.Sheets.SheetArea.rowHeader;
  175. },
  176. items: {
  177. "insert": {
  178. type: me.registerInputContextMenuItem(),
  179. disabled: function () {
  180. return false;
  181. },
  182. /* callback: function (key, opt) {
  183. me.insertEquipments([me.newEquipment()])
  184. } */
  185. },
  186. "delete": {
  187. name: "删除",
  188. icon: 'fa-times',
  189. disabled: function () {
  190. return me.rightClickTarget.row === undefined;
  191. },
  192. callback: function (key, opt) {
  193. let row = me.rightClickTarget.row;
  194. me.deleteEquipment(me.data[row].ID);
  195. //me.preApplyInfoPrice(row);
  196. }
  197. }
  198. }
  199. });
  200. },
  201. }
  202. $(function () {
  203. $('#tab_equipment_purchase').on('shown.bs.tab', function (e) {
  204. sessionStorage.setItem('mainTab', '#tab_equipment_purchase');
  205. $(e.relatedTarget.hash).removeClass('active');
  206. equipmentPurchaseObj.initSpread();
  207. equipmentPurchaseObj.showData();
  208. })
  209. })