equipment_purchase_view.js 8.3 KB

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