ration_glj.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. /**
  2. * Created by Tony on 2017/4/28.
  3. */
  4. var rationGLJOprObj = {
  5. sheet: null,
  6. currentRationItem: null,
  7. distTypeTree: null,
  8. cache: {},
  9. setting: {
  10. header:[
  11. {headerName:"编码",headerWidth:120,dataCode:"code", dataType: "String", formatter: "@"},
  12. {headerName:"名称",headerWidth:400,dataCode:"name", dataType: "String"},
  13. {headerName:"单位",headerWidth:160,dataCode:"unit", dataType: "String"},
  14. {headerName:"基价单位",headerWidth:160, dataCode:"basePrice", dataType: "Number", formatter:"0.00", precision: 2},
  15. {headerName:"定额消耗",headerWidth:160, dataCode:"consumeAmt", dataType: "Number", formatter: "0.000", precision: 3},
  16. {headerName:"类型",headerWidth:160,dataCode:"gljType", dataType: "String"}
  17. ],
  18. view:{
  19. comboBox:[],
  20. lockColumns:[1,2,3,5,6]
  21. }
  22. },
  23. getDistTypeTree: function (gljDistType) {
  24. let me = this;
  25. let distType;
  26. let distTypeTree = {
  27. prefix : 'gljDistType',
  28. distTypes: {},
  29. comboDatas: [],
  30. distTypesArr: []
  31. };
  32. gljDistType.forEach(function (typeData) {
  33. let typeObj = {
  34. data: typeData,
  35. children: [],
  36. parent: null
  37. }
  38. distTypeTree.distTypes[distTypeTree.prefix + typeData.ID] = typeObj;
  39. distTypeTree.distTypesArr.push(typeObj);
  40. });
  41. gljDistType.forEach(function (typeData) {
  42. distType = distTypeTree.distTypes[distTypeTree.prefix + typeData.ID];
  43. let parent = distTypeTree.distTypes[distTypeTree.prefix + typeData.ParentID];
  44. if(parent){
  45. distType.parent = parent;
  46. parent.children.push(distType);
  47. }
  48. });
  49. distTypeTree.distTypesArr.forEach(function (distTypeObj) {
  50. if(distTypeObj.children.length === 0 && distTypeObj.data.fullName !== '普通机械' &&distTypeObj.data.fullName !== '机械组成物'
  51. && distTypeObj.data.fullName !== '机上人工'){
  52. distTypeTree.comboDatas.push({text: distTypeObj.data.fullName, value: distTypeObj.data.ID});
  53. }
  54. if(distTypeObj.data.fullName === '机械'){
  55. distTypeTree.comboDatas.push({text: distTypeObj.data.fullName, value: distTypeObj.data.ID});
  56. }
  57. });
  58. //me.distTypeTree = distTypeTree;
  59. return distTypeTree;
  60. //return distTypeTree.comboDatas;
  61. },
  62. getGljDistType: function (callback) {
  63. let me = this;
  64. $.ajax({
  65. type: 'post',
  66. url: "api/getGljDistType",
  67. dataType: 'json',
  68. success: function (result) {
  69. if(!result.error && callback){
  70. me.distTypeTree = me.getDistTypeTree(result.data);
  71. console.log(`me.distTypeTree`);
  72. console.log(me.distTypeTree);
  73. callback();
  74. }
  75. }
  76. })
  77. },
  78. buildSheet: function(sheet) {
  79. var me = this;
  80. me.sheet = sheet;
  81. me.getGljDistType(function () {
  82. sheetCommonObj.initSheet(me.sheet, me.setting, 30);
  83. me.sheet.bind(GC.Spread.Sheets.Events.ClipboardPasting, me.onClipboardPasting);
  84. me.sheet.bind(GC.Spread.Sheets.Events.ClipboardPasted, me.onClipboardPasted);
  85. me.sheet.bind(GC.Spread.Sheets.Events.EditEnded, me.onCellEditEnd);
  86. me.sheet.bind(GC.Spread.Sheets.Events.RangeChanged, me.onRangeChanged);
  87. });
  88. },
  89. onRangeChanged: function(sender, args) {
  90. if (args.action == GC.Spread.Sheets.RangeChangedAction.clear) {
  91. var me = rationGLJOprObj, updateArr = [], removeArr = [];
  92. if (args.col == 0) {
  93. if (me.cache["_GLJ_" + me.currentRationItem.ID]) {
  94. var cacheArr = me.cache["_GLJ_" + me.currentRationItem.ID];
  95. for (var i = args.rowCount - 1; i >= 0; i--) {
  96. if (args.row + i < cacheArr.length) {
  97. cacheArr.splice(args.row + i, 1);
  98. }
  99. }
  100. me.updateRationItem();
  101. sheetCommonObj.cleanSheet(me.sheet, me.setting, -1);
  102. me.showGljItems(me.currentRationItem.ID);
  103. }
  104. }
  105. }
  106. },
  107. onClipboardPasting: function(sender, args) {
  108. var me = rationGLJOprObj;
  109. if (args.cellRange.colCount != 1 || args.cellRange.col != 0 || !(me.currentRationItem)) {
  110. args.cancel = true;
  111. }
  112. },
  113. onClipboardPasted: function(e, info) {
  114. var me = rationGLJOprObj, repId = storageUtil.getSessionCache("RationGrp","repositoryID");
  115. if (repId) {
  116. if (info.cellRange.col == 0) {
  117. var tmpCodes = sheetCommonObj.analyzePasteData({header:[{dataCode: "code"}] }, info);
  118. var codes = [];
  119. for (var i = 0; i < tmpCodes.length; i++) {
  120. codes.push(tmpCodes[i].code);
  121. }
  122. me.addGljItems(codes, repId);
  123. } else {
  124. //修改用量
  125. }
  126. }
  127. },
  128. onCellEditEnd: function(sender, args){
  129. var me = rationGLJOprObj;
  130. if (args.col != 0) {
  131. var cacheArr = me.cache["_GLJ_" + me.currentRationItem.ID];
  132. if (args.row < cacheArr.length) {
  133. var editGlj = cacheArr[args.row];
  134. if (editGlj["consumeAmt"] != args.editingText) {
  135. editGlj["consumeAmt"] = args.editingText;
  136. me.updateRationItem();
  137. }
  138. }
  139. } else {
  140. //重新更新工料机
  141. if (args.editingText == null || args.editingText.trim() == "") {
  142. //delete
  143. if (me.cache["_GLJ_" + me.currentRationItem.ID]) {
  144. var cacheArr = me.cache["_GLJ_" + me.currentRationItem.ID];
  145. if (args.row < cacheArr.length) {
  146. cacheArr.splice(args.row, 1);
  147. me.updateRationItem();
  148. sheetCommonObj.cleanSheet(me.sheet, me.setting, -1);
  149. me.showGljItems(me.currentRationItem.ID);
  150. }
  151. }
  152. } else {
  153. var repId = storageUtil.getSessionCache("RationGrp","repositoryID");
  154. if (repId) {
  155. var codes = [];
  156. codes.push(args.editingText.trim());
  157. me.addGljItems(codes, repId);
  158. }
  159. }
  160. }
  161. },
  162. addGljItems: function(codes, repId) {
  163. var me = this;
  164. $.ajax({
  165. type:"POST",
  166. url:"api/getGljItemsByCodes",
  167. data:{"gljCodes": JSON.stringify(codes), repId: repId},
  168. dataType:"json",
  169. cache:false,
  170. timeout:5000,
  171. success:function(result){
  172. sheetCommonObj.cleanSheet(me.sheet, me.setting, -1);
  173. if (result) {
  174. var rstArr = [], dummyR = {gljId: 0, consumeAmt:0}, newAddArr = [];
  175. for (var i = 0; i < result.data.length; i++) {
  176. dummyR.gljId = result.data[i].ID;
  177. rstArr.push(me.createRationGljDisplayItem(dummyR, result.data[i]));
  178. }
  179. if (me.cache["_GLJ_" + me.currentRationItem.ID]) {
  180. var cacheArr = me.cache["_GLJ_" + me.currentRationItem.ID];
  181. for (var i = 0; i < rstArr.length; i++) {
  182. var hasDup = false;
  183. for (var j = 0; j < cacheArr.length; j++) {
  184. if (cacheArr[j].gljId == rstArr[i].gljId) {
  185. hasDup = true;
  186. break;
  187. }
  188. }
  189. if (!hasDup) {
  190. newAddArr.push(rstArr[i]);
  191. }
  192. }
  193. cacheArr.sort(function(a, b) {
  194. var rst = 0;
  195. if (a.code > b.code) rst = 1
  196. else if (a.code < b.code) rst = -1;
  197. return rst;
  198. });
  199. me.cache["_GLJ_" + me.currentRationItem.ID] = cacheArr.concat(newAddArr);
  200. }
  201. me.showGljItems(me.currentRationItem.ID);
  202. if (newAddArr.length > 0) {
  203. me.updateRationItem();
  204. }
  205. }
  206. sheetCommonObj.lockCells(me.sheet, me.setting);
  207. },
  208. error:function(err){
  209. alert(err);
  210. }
  211. })
  212. },
  213. rationCal: function () {
  214. let me = this;
  215. let price = {gljType1: [], gljType2: [], gljType3: []}, rst = {labourPrice: 0, materialPrice: 0, machinePrice: 0}, rationBasePrc = 0;
  216. function round(v,e){
  217. var t=1;
  218. for(;e>0;t*=10,e--);
  219. for(;e<0;t/=10,e++);
  220. return Math.round(v*t)/t;
  221. }
  222. if(me.currentRationItem && me.cache['_GLJ_' + me.currentRationItem.ID]){
  223. let cacheArr = me.cache['_GLJ_' + me.currentRationItem.ID];
  224. cacheArr.forEach(function (gljData) {
  225. if(gljData.gljType && gljData.basePrice && gljData.consumeAmt){
  226. let parent = me.distTypeTree.distTypes[me.distTypeTree.prefix + gljData.gljType].parent;
  227. if(parent && parent.data.ID <= 3){
  228. price['gljType' + parent.data.ID].push(round( gljData.basePrice * gljData.consumeAmt, 3));//取三位
  229. }
  230. if(!parent && gljData.gljType <= 3){
  231. price['gljType' + gljData.gljType].push(round( gljData.basePrice * gljData.consumeAmt, 3));//取三位
  232. }
  233. }
  234. });
  235. if(price.gljType1.length > 0){
  236. let labourPrice = 0;
  237. price.gljType1.forEach(function (singlePrc) {
  238. labourPrice += singlePrc;
  239. });
  240. let roundPrice = round(labourPrice, 2);
  241. rst.labourPrice = roundPrice;
  242. rationBasePrc += roundPrice;
  243. }
  244. if(price.gljType2.length > 0){
  245. let materialPrice = 0;
  246. price.gljType2.forEach(function (singlePrc) {
  247. materialPrice += singlePrc;
  248. });
  249. let roundPrice = round(materialPrice, 2);
  250. rst.materialPrice = roundPrice;
  251. rationBasePrc += roundPrice;
  252. }
  253. if(price.gljType3.length > 0){
  254. let machinePrice = 0;
  255. price.gljType3.forEach(function (singlePrc) {
  256. machinePrice += singlePrc;
  257. });
  258. let roundPrice = round(machinePrice, 2);
  259. rst.machinePrice = roundPrice;
  260. rationBasePrc += roundPrice;
  261. }
  262. rst.rationBasePrc = rationBasePrc;
  263. }
  264. return rst;
  265. },
  266. getRationUpPrice: function () {
  267. },
  268. updateRationItem: function() {
  269. var me = this, updateArr = [];
  270. if (me.currentRationItem) {
  271. me.currentRationItem.rationGljList = me.buildRationItemGlj();
  272. //recalculate ration basePrice
  273. let price = me.rationCal();
  274. me.currentRationItem.labourPrice = price.labourPrice;
  275. me.currentRationItem.materialPrice = price.materialPrice;
  276. me.currentRationItem.machinePrice = price.machinePrice;
  277. me.currentRationItem.basePrice = price.rationBasePrc;
  278. updateArr.push(me.currentRationItem);
  279. rationOprObj.mixUpdateRequest(updateArr, [], []);
  280. }
  281. },
  282. buildRationItemGlj: function(){
  283. var me = this, rst = [];
  284. if (me.currentRationItem && me.cache["_GLJ_" + me.currentRationItem.ID]) {
  285. var cacheArr = me.cache["_GLJ_" + me.currentRationItem.ID];
  286. for (var i = 0; i < cacheArr.length; i++) {
  287. rst.push({gljId: cacheArr[i].gljId, consumeAmt: cacheArr[i].consumeAmt, proportion: 0});
  288. }
  289. }
  290. return rst;
  291. },
  292. createRationGljDisplayItem: function(rItem, repGlj) {
  293. var rst = {};
  294. rst.gljId = rItem.gljId;
  295. rst.consumeAmt = rItem.consumeAmt;
  296. rst.code = repGlj.code;
  297. rst.name = repGlj.name;
  298. rst.specs = repGlj.specs;
  299. rst.unit = repGlj.unit;
  300. rst.basePrice = repGlj.basePrice;
  301. rst.gljType = repGlj.gljType;
  302. return rst;
  303. },
  304. getGljItems: function(rationItem) {
  305. var me = this, rationID = rationItem.ID, rationGljList = rationItem.rationGljList;
  306. me.currentRationItem = rationItem;
  307. if (me.cache["_GLJ_" + rationID]) {
  308. me.showGljItems(rationID);
  309. sheetCommonObj.lockCells(me.sheet, me.setting);
  310. } else {
  311. var gljIds = [];
  312. for (var i = 0; i < rationGljList.length; i++) {
  313. gljIds.push(rationGljList[i].gljId);
  314. }
  315. $.ajax({
  316. type:"POST",
  317. url:"api/getGljItemsByIds",
  318. data:{"gljIds": JSON.stringify(gljIds)},
  319. dataType:"json",
  320. cache:false,
  321. timeout:5000,
  322. success:function(result){
  323. sheetCommonObj.cleanSheet(me.sheet, me.setting, -1);
  324. if (result) {
  325. var cacheArr = [];
  326. for (var i = 0; i < result.data.length; i++) {
  327. for (var j = 0; j < rationGljList.length; j++) {
  328. if (rationGljList[j].gljId == result.data[i].ID) {
  329. cacheArr.push(me.createRationGljDisplayItem(rationGljList[j], result.data[i]));
  330. break;
  331. }
  332. }
  333. }
  334. me.cache["_GLJ_" + rationID] = cacheArr;
  335. me.showGljItems(rationID);
  336. }
  337. sheetCommonObj.lockCells(me.sheet, me.setting);
  338. },
  339. error:function(err){
  340. alert(err);
  341. }
  342. })
  343. }
  344. },
  345. showGljItems: function(rationID) {
  346. var me = this;
  347. if (me.cache["_GLJ_" + rationID]) {
  348. sheetCommonObj.showData(me.sheet, me.setting, me.cache["_GLJ_" + rationID], me.distTypeTree);
  349. }
  350. }
  351. }