composition_spread.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. /**
  2. * 组成物Spread
  3. *
  4. * @author CaiAoLin
  5. * @date 2017/7/18
  6. * @version
  7. */
  8. /**
  9. * 构造函数
  10. *
  11. * @return {void}
  12. */
  13. function CompositionSpread () {
  14. this.isChanging = false;
  15. this.sheetObj = null;
  16. this.successCallback = null;
  17. this.rightClickTarget = null;
  18. }
  19. /**
  20. * 初始化
  21. *
  22. * @param {String} target
  23. * @return {Object}
  24. */
  25. CompositionSpread.prototype.init = function(target) {
  26. let name = target === 'machine' ? '用量' : '消耗量';
  27. let header = [
  28. {name: '编码', field: 'code', visible: true},
  29. {name: '名称', field: 'name', visible: true},
  30. {name: '单位', field: 'unit', visible: true},
  31. {name: 'ID', field: 'id', visible: false},
  32. {name: '类型', field: 'unit_price.type', visible: false},
  33. {name: '定额价', field: "base_price", visible: true,decimalField:"glj.unitPrice"},
  34. {name: '调整价', field: 'adjust_price', visible: true,decimalField:"glj.unitPrice"},
  35. {name: '市场价', field: "unit_price.market_price", visible: true,decimalField:"glj.unitPrice"},
  36. {name: name, field: 'consumption', visible: true, validator: 'number',decimalField:'glj.quantity'},
  37. {name: 'CID', field: 'mix_ratio_id', visible: false},
  38. ];
  39. this.sheetObj = new CommonSpreadJs(header);
  40. this.sheetObj.init(target);
  41. // 获取列号
  42. let codeColumn = this.sheetObj.getFieldColumn('code');
  43. let unitColumn = this.sheetObj.getFieldColumn('unit');
  44. let consumptionColumn = this.sheetObj.getFieldColumn('consumption');
  45. let basePriceCol = this.sheetObj.getFieldColumn('base_price');
  46. let adjustPriceCol = this.sheetObj.getFieldColumn('adjust_price');
  47. let marketPriceCol = this.sheetObj.getFieldColumn('unit_price.market_price');
  48. // 居中样式
  49. let centerStyleSetting = {hAlign: 1};
  50. this.sheetObj.setStyle(-1, codeColumn, centerStyleSetting);
  51. this.sheetObj.setStyle(-1, unitColumn, centerStyleSetting);
  52. //靠右设置
  53. let rightStyleSetting={hAlign: 2};
  54. this.sheetObj.setStyle(-1, basePriceCol, rightStyleSetting);
  55. this.sheetObj.setStyle(-1, adjustPriceCol, rightStyleSetting);
  56. this.sheetObj.setStyle(-1, marketPriceCol, rightStyleSetting);
  57. this.sheetObj.setStyle(-1, consumptionColumn, rightStyleSetting);
  58. // 设置可编辑列
  59. this.sheetObj.setColumnEditable(consumptionColumn);
  60. // 绑定事件
  61. let self = this;
  62. this.sheetObj.bind(GC.Spread.Sheets.Events.ValueChanged, function(element, info) {
  63. self.updateConsumption(info, self.successCallback);
  64. });
  65. return this.sheetObj;
  66. };
  67. /**
  68. * 初始化右键
  69. *
  70. * @param {String} target
  71. * @return {void}
  72. */
  73. CompositionSpread.prototype.initRightClick = function(target) {
  74. let activeSheet = this.sheetObj.getSheet();
  75. let self = this;
  76. $.contextMenu({
  77. selector: '#' + target,
  78. build: function ($trigger, e) {
  79. self.rightClickTarget = SheetDataHelper.safeRightClickSelection($trigger, e, self.sheetObj.spread);
  80. return self.rightClickTarget.hitTestType === GC.Spread.Sheets.SheetArea.viewport ||
  81. self.rightClickTarget.hitTestType === GC.Spread.Sheets.SheetArea.rowHeader;
  82. },
  83. items: {
  84. "deleteMixRatio": {
  85. name: "删除",
  86. icon: 'fa-trash-o',
  87. disabled: function () {
  88. return self.rightClickTarget.row === undefined;
  89. },
  90. callback: function (key, opt) {
  91. let row = self.rightClickTarget.row;
  92. let idColumn = self.sheetObj.getFieldColumn('mix_ratio_id');
  93. let deleteId = activeSheet.getValue(row, idColumn);
  94. self.deleteComposition(deleteId, row, self.successCallback);
  95. }
  96. },
  97. }
  98. });
  99. };
  100. /**
  101. * 获取组成物数据
  102. *
  103. * @param {Number} projectGLJid
  104. * @return {void | boolean}
  105. */
  106. CompositionSpread.prototype.getRatioData = function(projectGLJid) {
  107. projectGLJid = parseInt(projectGLJid);
  108. let self = this;
  109. if (isNaN(projectGLJid) || projectGLJid <= 0) {
  110. this.sheetObj.setData(null);
  111. return false;
  112. }
  113. $.ajax({
  114. url: '/glj/get-ratio',
  115. type: 'post',
  116. data: {id: projectGLJid, project_id: scUrlUtil.GetQueryString('project')},
  117. error: function() {
  118. self.sheetObj.setData(null);
  119. },
  120. beforeSend: function() {
  121. },
  122. success: function(response) {
  123. if (response.err === 0) {
  124. response.data = JSON.parse(response.data);
  125. // 设置数据
  126. self.sheetObj.setData(response.data);
  127. self.specialColumn(response.data);
  128. } else {
  129. self.sheetObj.setData(null);
  130. console.log('不存在对应数据');
  131. }
  132. }
  133. });
  134. };
  135. /**
  136. * 设置特殊单元格数据
  137. *
  138. * @param {object} sourceData
  139. * @return {void}
  140. */
  141. CompositionSpread.prototype.specialColumn = function(sourceData) {
  142. let rowCounter = 0;
  143. // 获取市场单价列号
  144. let consumptionColumn = this.sheetObj.getFieldColumn('consumption');
  145. let basePriceColumn = this.sheetObj.getFieldColumn('base_price');
  146. let adjustPriceColumn = this.sheetObj.getFieldColumn('adjust_price');
  147. let idColumn = this.sheetObj.getFieldColumn('mix_ratio_id');
  148. let activeSheet = this.sheetObj.getSheet();
  149. for(let data of sourceData) {
  150. // 把消耗量从对象中抽离出来
  151. if (data.ratio_data.consumption !== undefined) {
  152. activeSheet.setValue(rowCounter, consumptionColumn, data.ratio_data.consumption);
  153. activeSheet.setValue(rowCounter, idColumn, data.ratio_data.id);
  154. }
  155. data=this.sheetObj.setProjectGLJDiffPrice(data);
  156. activeSheet.setValue(rowCounter, basePriceColumn, data.base_price);
  157. activeSheet.setValue(rowCounter, adjustPriceColumn, data.adjust_price);
  158. rowCounter++;
  159. }
  160. };
  161. /**
  162. * 更新组成物消耗量或用量
  163. *
  164. * @param {Object} info
  165. * @param {function} callback
  166. * return {void}
  167. */
  168. CompositionSpread.prototype.updateConsumption = function(info, callback) {
  169. // 获取修改的数据
  170. let column = info.col;
  171. let row = info.row;
  172. let field = this.sheetObj.getColumnField(column);
  173. if (field === '') {
  174. return false;
  175. }
  176. // 防止快速同时提交
  177. if (this.isChanging) {
  178. return false;
  179. }
  180. let activeSheet = this.sheetObj.getSheet();
  181. // 校验数据
  182. let value = info.newValue;
  183. if (!this.sheetObj.checkData(column, value)) {
  184. alert('数据格式错误,请重新输入!');
  185. activeSheet.setValue(row, column, info.oldValue);
  186. return false;
  187. }
  188. // 获取id
  189. let idColumn = this.sheetObj.getFieldColumn('mix_ratio_id');
  190. if (idColumn < 0) {
  191. return false;
  192. }
  193. let id = activeSheet.getValue(row, idColumn);
  194. let [parentMarketPrice, parentBasePrice] = this.getCompositionSumPrice('modify', row, info.newValue);
  195. let self = this;
  196. $.ajax({
  197. url: '/glj/update',
  198. type: 'post',
  199. data: {id: id, field: 'mix_ratio.' + field, value: value, market_price: parentMarketPrice, base_price: parentBasePrice},
  200. dataType: 'json',
  201. error: function() {
  202. alert('数据传输有误!');
  203. self.isChanging = false;
  204. activeSheet.setValue(row, column, info.oldValue);
  205. },
  206. beforeSend: function() {
  207. self.isChanging = true;
  208. },
  209. success: function(response) {
  210. self.isChanging = false;
  211. // 修改失败则恢复原值
  212. if (response.err !== 0) {
  213. activeSheet.setValue(row, column, info.oldValue);
  214. alert('更改数据失败!');
  215. } else {
  216. info.id = id;
  217. info.field = field;
  218. info.parentMarketPrice = parentMarketPrice;
  219. info.parentBasePrice = parentBasePrice;
  220. info.change = info.newValue - info.oldValue;
  221. callback(info);
  222. }
  223. }
  224. });
  225. };
  226. /**
  227. * 获取当前所有组成物累积的市场单价和基价单价
  228. * 用于同步修改父级市场单价和基价单价
  229. *
  230. * @param {String} scene
  231. * @param {Number} affectRow
  232. * @param {Number} newValue
  233. * @return {Array}
  234. */
  235. CompositionSpread.prototype.getCompositionSumPrice = function(scene, affectRow, newValue = 0) {
  236. let activeSheet = this.sheetObj.getSheet();
  237. // 计算父级3个价格
  238. let maxRow = activeSheet.getRowCount();
  239. // 获取对应列的列号
  240. let marketPriceColumn = this.sheetObj.getFieldColumn('unit_price.market_price');
  241. let consumptionColumn = this.sheetObj.getFieldColumn('consumption');
  242. let basePriceColumn = this.sheetObj.getFieldColumn('base_price');
  243. let parentMarketPrice = 0;
  244. let parentBasePrice = 0;
  245. for(let i = 0; i < maxRow; i++) {
  246. // 获取市场单价
  247. let marketPrice = activeSheet.getValue(i, marketPriceColumn);
  248. // 获取基价单价
  249. let basePrice = activeSheet.getValue(i, basePriceColumn);
  250. // 如果是删除则忽略即将被删除的行数据
  251. if (scene === 'delete' && affectRow === i) {
  252. continue;
  253. }
  254. // 获取消耗量(如果是当前修改的行则替换数据)
  255. let consumption = i === affectRow ? newValue : activeSheet.getValue(i, consumptionColumn);
  256. parentMarketPrice += operationWithRound(consumption,marketPrice,"glj.unitPrice","*");
  257. parentBasePrice += operationWithRound(consumption,basePrice,"glj.unitPrice","*");
  258. }
  259. parentMarketPrice = parentMarketPrice.toDecimal(getDecimal("glj.unitPrice"));
  260. parentBasePrice = parentBasePrice.toDecimal(getDecimal("glj.unitPrice"));
  261. return [parentMarketPrice, parentBasePrice]
  262. };
  263. /**
  264. * 删除组成物
  265. *
  266. * @param {Number} id
  267. * @param {Number} row
  268. * @param {function} callback
  269. * @return {void | boolean}
  270. */
  271. CompositionSpread.prototype.deleteComposition = function (id, row, callback) {
  272. id = parseInt(id);
  273. if (isNaN(id) || id <= 0) {
  274. alert('参数错误!');
  275. }
  276. if (isDeleting) {
  277. return false;
  278. }
  279. let activeSheet = this.sheetObj.getSheet();
  280. // 获取当前行的消耗量
  281. let consumptionColumn = this.sheetObj.getFieldColumn('consumption');
  282. let consumption = activeSheet.getValue(row, consumptionColumn);
  283. let self = this;
  284. $.ajax({
  285. url: '/glj/delete-ratio',
  286. type: 'post',
  287. data: {id: id},
  288. dataType: 'json',
  289. error: function() {
  290. isDeleting = false;
  291. alert('服务器繁忙');
  292. },
  293. beforeSend: function() {
  294. isDeleting = true;
  295. },
  296. success: function(response) {
  297. if (response.err === 0) {
  298. // 计算同级的市场单价和基价单价
  299. let [parentMarketPrice, parentBasePrice] = self.getCompositionSumPrice('delete', row);
  300. let info = {
  301. parentMarketPrice: parentMarketPrice,
  302. parentBasePrice: parentBasePrice,
  303. change: -consumption
  304. };
  305. activeSheet.setValue(row, consumptionColumn, 0);
  306. callback(info);
  307. }
  308. }
  309. });
  310. };