ration_glj.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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. me.onContextmenuOpr();
  83. sheetCommonObj.initSheet(me.sheet, me.setting, 30);
  84. me.sheet.bind(GC.Spread.Sheets.Events.ClipboardPasting, me.onClipboardPasting);
  85. me.sheet.bind(GC.Spread.Sheets.Events.ClipboardPasted, me.onClipboardPasted);
  86. me.sheet.bind(GC.Spread.Sheets.Events.EditEnded, me.onCellEditEnd);
  87. me.sheet.bind(GC.Spread.Sheets.Events.RangeChanged, me.onRangeChanged);
  88. });
  89. },
  90. onRangeChanged: function(sender, args) {
  91. if (args.action == GC.Spread.Sheets.RangeChangedAction.clear) {
  92. var me = rationGLJOprObj, updateArr = [], removeArr = [];
  93. if (args.col == 0) {
  94. if (me.cache["_GLJ_" + me.currentRationItem.ID]) {
  95. var cacheArr = me.cache["_GLJ_" + me.currentRationItem.ID];
  96. for (var i = args.rowCount - 1; i >= 0; i--) {
  97. if (args.row + i < cacheArr.length) {
  98. cacheArr.splice(args.row + i, 1);
  99. }
  100. }
  101. me.updateRationItem();
  102. sheetCommonObj.cleanSheet(me.sheet, me.setting, -1);
  103. me.showGljItems(me.currentRationItem.ID);
  104. }
  105. }
  106. }
  107. },
  108. onClipboardPasting: function(sender, args) {
  109. var me = rationGLJOprObj;
  110. if (args.cellRange.colCount != 1 || args.cellRange.col != 0 || !(me.currentRationItem)) {
  111. args.cancel = true;
  112. }
  113. },
  114. onClipboardPasted: function(e, info) {
  115. var me = rationGLJOprObj, repId = storageUtil.getSessionCache("RationGrp","repositoryID");
  116. if (repId) {
  117. if (info.cellRange.col == 0) {
  118. var tmpCodes = sheetCommonObj.analyzePasteData({header:[{dataCode: "code"}] }, info);
  119. var codes = [];
  120. for (var i = 0; i < tmpCodes.length; i++) {
  121. codes.push(tmpCodes[i].code);
  122. }
  123. me.addGljItems(codes, repId);
  124. } else {
  125. //修改用量
  126. }
  127. }
  128. },
  129. onCellEditEnd: function(sender, args){
  130. var me = rationGLJOprObj;
  131. if (args.col != 0) {
  132. var cacheArr = me.cache["_GLJ_" + me.currentRationItem.ID];
  133. if (args.row < cacheArr.length) {
  134. var editGlj = cacheArr[args.row];
  135. if (editGlj["consumeAmt"] != args.editingText) {
  136. let parseNum = parseFloat(args.editingText);
  137. if(isNaN(parseFloat(args.editingText))){
  138. $('#alertModalBtn').click();
  139. $('#alertText').text("定额消耗只能输入数值!");
  140. args.sheet.options.isProtected = true;
  141. $('#alertModalCls').click(function () {
  142. args.sheet.options.isProtected = false;
  143. args.sheet.setValue(args.row, args.col, editGlj['consumeAmt']);
  144. });
  145. $('#alertModalCof').click(function () {
  146. args.sheet.options.isProtected = false;
  147. args.sheet.setValue(args.row, args.col, editGlj['consumeAmt']);
  148. })
  149. }
  150. else{
  151. args.sheet.setValue(args.row, args.col, parseNum);
  152. editGlj["consumeAmt"] = parseNum;
  153. me.updateRationItem();
  154. }
  155. }
  156. }
  157. } else {
  158. //重新更新工料机
  159. if (args.editingText == null || args.editingText.trim() == "") {
  160. //delete
  161. if (me.cache["_GLJ_" + me.currentRationItem.ID]) {
  162. var cacheArr = me.cache["_GLJ_" + me.currentRationItem.ID];
  163. if (args.row < cacheArr.length) {
  164. cacheArr.splice(args.row, 1);
  165. me.updateRationItem();
  166. sheetCommonObj.cleanSheet(me.sheet, me.setting, -1);
  167. me.showGljItems(me.currentRationItem.ID);
  168. }
  169. }
  170. } else {
  171. var repId = storageUtil.getSessionCache("RationGrp","repositoryID");
  172. if (repId) {
  173. var codes = [];
  174. codes.push(args.editingText.trim());
  175. me.addGljItems(codes, repId);
  176. }
  177. }
  178. }
  179. },
  180. onContextmenuOpr: function () {
  181. $.contextMenu({
  182. selector: '#rdSpread',
  183. callback: function(key, options) {
  184. var m = "clicked: " + key;
  185. window.console && console.log(m) || alert(m);
  186. },
  187. items: {
  188. "insert": {name: "插入", callback: function (key, opt) {
  189. }},
  190. "delete": {name: "删除"}
  191. }
  192. });
  193. },
  194. addGljItems: function(codes, repId) {
  195. var me = this;
  196. $.ajax({
  197. type:"POST",
  198. url:"api/getGljItemsByCodes",
  199. data:{"gljCodes": JSON.stringify(codes), repId: repId},
  200. dataType:"json",
  201. cache:false,
  202. timeout:5000,
  203. success:function(result){
  204. sheetCommonObj.cleanSheet(me.sheet, me.setting, -1);
  205. if (result) {
  206. var rstArr = [], dummyR = {gljId: 0, consumeAmt:0}, newAddArr = [];
  207. for (var i = 0; i < result.data.length; i++) {
  208. dummyR.gljId = result.data[i].ID;
  209. rstArr.push(me.createRationGljDisplayItem(dummyR, result.data[i]));
  210. }
  211. if (me.cache["_GLJ_" + me.currentRationItem.ID]) {
  212. var cacheArr = me.cache["_GLJ_" + me.currentRationItem.ID];
  213. for (var i = 0; i < rstArr.length; i++) {
  214. var hasDup = false;
  215. for (var j = 0; j < cacheArr.length; j++) {
  216. if (cacheArr[j].gljId == rstArr[i].gljId) {
  217. hasDup = true;
  218. break;
  219. }
  220. }
  221. if (!hasDup) {
  222. newAddArr.push(rstArr[i]);
  223. }
  224. }
  225. cacheArr.sort(function(a, b) {
  226. var rst = 0;
  227. if (a.code > b.code) rst = 1
  228. else if (a.code < b.code) rst = -1;
  229. return rst;
  230. });
  231. me.cache["_GLJ_" + me.currentRationItem.ID] = cacheArr.concat(newAddArr);
  232. }
  233. me.showGljItems(me.currentRationItem.ID);
  234. if (newAddArr.length > 0) {
  235. me.updateRationItem();
  236. }
  237. }
  238. sheetCommonObj.lockCells(me.sheet, me.setting);
  239. },
  240. error:function(err){
  241. alert(err);
  242. }
  243. })
  244. },
  245. rationCal: function () {
  246. let me = this;
  247. let price = {gljType1: [], gljType2: [], gljType3: []}, rst = {labourPrice: 0, materialPrice: 0, machinePrice: 0}, rationBasePrc = 0;
  248. function round(v,e){
  249. var t=1;
  250. for(;e>0;t*=10,e--);
  251. for(;e<0;t/=10,e++);
  252. return Math.round(v*t)/t;
  253. }
  254. if(me.currentRationItem && me.cache['_GLJ_' + me.currentRationItem.ID]){
  255. let cacheArr = me.cache['_GLJ_' + me.currentRationItem.ID];
  256. cacheArr.forEach(function (gljData) {
  257. if(gljData.gljType && gljData.basePrice && gljData.consumeAmt){
  258. let parent = me.distTypeTree.distTypes[me.distTypeTree.prefix + gljData.gljType].parent;
  259. if(parent && parent.data.ID <= 3){
  260. price['gljType' + parent.data.ID].push(round( gljData.basePrice * gljData.consumeAmt, 3));//取三位
  261. }
  262. if(!parent && gljData.gljType <= 3){
  263. price['gljType' + gljData.gljType].push(round( gljData.basePrice * gljData.consumeAmt, 3));//取三位
  264. }
  265. }
  266. });
  267. if(price.gljType1.length > 0){
  268. let labourPrice = 0;
  269. price.gljType1.forEach(function (singlePrc) {
  270. labourPrice += singlePrc;
  271. });
  272. let roundPrice = round(labourPrice, 2);
  273. rst.labourPrice = roundPrice;
  274. rationBasePrc += roundPrice;
  275. }
  276. if(price.gljType2.length > 0){
  277. let materialPrice = 0;
  278. price.gljType2.forEach(function (singlePrc) {
  279. materialPrice += singlePrc;
  280. });
  281. let roundPrice = round(materialPrice, 2);
  282. rst.materialPrice = roundPrice;
  283. rationBasePrc += roundPrice;
  284. }
  285. if(price.gljType3.length > 0){
  286. let machinePrice = 0;
  287. price.gljType3.forEach(function (singlePrc) {
  288. machinePrice += singlePrc;
  289. });
  290. let roundPrice = round(machinePrice, 2);
  291. rst.machinePrice = roundPrice;
  292. rationBasePrc += roundPrice;
  293. }
  294. rst.rationBasePrc = rationBasePrc;
  295. }
  296. return rst;
  297. },
  298. updateRationItem: function() {
  299. var me = this, updateArr = [];
  300. if (me.currentRationItem) {
  301. me.currentRationItem.rationGljList = me.buildRationItemGlj();
  302. //recalculate ration basePrice
  303. let price = me.rationCal();
  304. me.currentRationItem.labourPrice = price.labourPrice;
  305. me.currentRationItem.materialPrice = price.materialPrice;
  306. me.currentRationItem.machinePrice = price.machinePrice;
  307. me.currentRationItem.basePrice = price.rationBasePrc;
  308. updateArr.push(me.currentRationItem);
  309. rationOprObj.mixUpdateRequest(updateArr, [], []);
  310. }
  311. },
  312. buildRationItemGlj: function(){
  313. var me = this, rst = [];
  314. if (me.currentRationItem && me.cache["_GLJ_" + me.currentRationItem.ID]) {
  315. var cacheArr = me.cache["_GLJ_" + me.currentRationItem.ID];
  316. for (var i = 0; i < cacheArr.length; i++) {
  317. rst.push({gljId: cacheArr[i].gljId, consumeAmt: cacheArr[i].consumeAmt, proportion: 0});
  318. }
  319. }
  320. return rst;
  321. },
  322. createRationGljDisplayItem: function(rItem, repGlj) {
  323. var rst = {};
  324. rst.gljId = rItem.gljId;
  325. rst.consumeAmt = rItem.consumeAmt;
  326. rst.code = repGlj.code;
  327. rst.name = repGlj.name;
  328. rst.specs = repGlj.specs;
  329. rst.unit = repGlj.unit;
  330. rst.basePrice = repGlj.basePrice;
  331. rst.gljType = repGlj.gljType;
  332. return rst;
  333. },
  334. getGljItems: function(rationItem) {
  335. var me = this, rationID = rationItem.ID, rationGljList = rationItem.rationGljList;
  336. me.currentRationItem = rationItem;
  337. if (me.cache["_GLJ_" + rationID]) {
  338. me.showGljItems(rationID);
  339. sheetCommonObj.lockCells(me.sheet, me.setting);
  340. } else {
  341. var gljIds = [];
  342. for (var i = 0; i < rationGljList.length; i++) {
  343. gljIds.push(rationGljList[i].gljId);
  344. }
  345. $.ajax({
  346. type:"POST",
  347. url:"api/getGljItemsByIds",
  348. data:{"gljIds": JSON.stringify(gljIds)},
  349. dataType:"json",
  350. cache:false,
  351. timeout:5000,
  352. success:function(result){
  353. sheetCommonObj.cleanSheet(me.sheet, me.setting, -1);
  354. if (result) {
  355. var cacheArr = [];
  356. for (var i = 0; i < result.data.length; i++) {
  357. for (var j = 0; j < rationGljList.length; j++) {
  358. if (rationGljList[j].gljId == result.data[i].ID) {
  359. cacheArr.push(me.createRationGljDisplayItem(rationGljList[j], result.data[i]));
  360. break;
  361. }
  362. }
  363. }
  364. me.cache["_GLJ_" + rationID] = cacheArr;
  365. me.showGljItems(rationID);
  366. }
  367. sheetCommonObj.lockCells(me.sheet, me.setting);
  368. },
  369. error:function(err){
  370. alert(err);
  371. }
  372. })
  373. }
  374. },
  375. showGljItems: function(rationID) {
  376. var me = this;
  377. if (me.cache["_GLJ_" + rationID]) {
  378. sheetCommonObj.showData(me.sheet, me.setting, me.cache["_GLJ_" + rationID], me.distTypeTree);
  379. }
  380. }
  381. }