ration_item.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. /**
  2. * Created by Tony on 2017/4/28.
  3. */
  4. let async = require("async");
  5. let moment = require('moment');
  6. let counter = require('../../../public/counter/counter');
  7. let gljDao = require('./glj_repository');
  8. let rationRepositoryDao = require('./repository_map');
  9. const scMathUtil = require('../../../public/scMathUtil').getUtil();
  10. import {rationItemModel} from './schemas';
  11. var rationItemDAO = function(){};
  12. rationItemDAO.prototype.sortToNumber = function (datas) {
  13. for(let i = 0, len = datas.length; i < len; i++){
  14. let data = datas[i]._doc;
  15. if(_exist(data, 'labourPrice')){
  16. data['labourPrice'] = parseFloat(data['labourPrice']);
  17. }
  18. if(_exist(data, 'materialPrice')){
  19. data['materialPrice'] = parseFloat(data['materialPrice']);
  20. }
  21. if(_exist(data, 'machinePrice')){
  22. data['machinePrice'] = parseFloat(data['machinePrice']);
  23. }
  24. if(_exist(data, 'basePrice')){
  25. data['basePrice'] = parseFloat(data['basePrice']);
  26. }
  27. if(_exist(data, 'rationGljList')){
  28. for(let j = 0, jLen = data['rationGljList'].length; j < jLen; j++){
  29. let raGljObj = data['rationGljList'][j]._doc;
  30. if(_exist(raGljObj, 'consumeAmt')){
  31. raGljObj['consumeAmt'] = parseFloat(raGljObj['consumeAmt']);
  32. }
  33. }
  34. }
  35. }
  36. function _exist(data, attr){
  37. return data && data[attr] !== undefined && data[attr];
  38. }
  39. };
  40. rationItemDAO.prototype.getRationItemsBySection = function(rationRepId, sectionId,callback){
  41. let me = this;
  42. rationItemModel.find({"rationRepId": rationRepId, "sectionId": sectionId, "$or": [{"isDeleted": null}, {"isDeleted": false} ]},function(err,data){
  43. if(err) callback(true, "Fail to get items", "");
  44. else {
  45. me.sortToNumber(data);
  46. callback(false,"Get items successfully", data);
  47. }
  48. })
  49. };
  50. rationItemDAO.prototype.mixUpdateRationItems = function(rationLibId, lastOpr, sectionId, updateItems, addItems, rIds, callback){
  51. var me = this;
  52. if (updateItems.length == 0 && rIds.length == 0) {
  53. me.addRationItems(rationLibId, lastOpr, sectionId, addItems, callback);
  54. } else {
  55. me.removeRationItems(rationLibId, lastOpr, rIds, function(err, message, docs) {
  56. if (err) {
  57. callback(true, "Fail to remove", false);
  58. } else {
  59. me.updateRationItems(rationLibId, lastOpr, sectionId, updateItems, function(err, results){
  60. if (err) {
  61. callback(true, "Fail to save", false);
  62. } else {
  63. if (addItems && addItems.length > 0) {
  64. me.addRationItems(rationLibId, lastOpr, sectionId, addItems, callback);
  65. } else {
  66. callback(false, "Save successfully", results);
  67. }
  68. }
  69. });
  70. }
  71. })
  72. }
  73. };
  74. rationItemDAO.prototype.removeRationItems = function(rationLibId, lastOpr, rIds,callback){
  75. if (rIds.length > 0) {
  76. rationItemModel.collection.remove({ID: {$in: rIds}}, null, function(err, docs){
  77. if (err) {
  78. callback(true, "Fail to remove", false);
  79. } else {
  80. rationRepositoryDao.updateOprArr({ID: rationLibId}, lastOpr, moment(Date.now()).format('YYYY-MM-DD HH:mm:ss'), function (err) {
  81. if(!err){
  82. callback(false, "Remove successfully", docs);
  83. }
  84. })
  85. }
  86. })
  87. } else {
  88. callback(false, "No records were deleted!", null);
  89. }
  90. };
  91. rationItemDAO.prototype.getRationItemsByCode = function(repId, code,callback){
  92. rationItemModel.find({"rationRepId": repId, "code": {'$regex': code, $options: '$i'}, "$or": [{"isDeleted": null}, {"isDeleted": false}]},function(err,data){
  93. if(err) callback(true, "Fail to get items", "")
  94. else callback(false,"Get items successfully", data);
  95. })
  96. };
  97. rationItemDAO.prototype.findRation = function (repId, keyword, callback) {
  98. var filter = {
  99. 'rationRepId': repId,
  100. '$and': [{
  101. '$or': [{'code': {'$regex': keyword, $options: '$i'}}, {'name': {'$regex': keyword, $options: '$i'}}]
  102. }, {
  103. '$or': [{'isDeleted': {"$exists":false}}, {'isDeleted': null}, {'isDeleted': false}]
  104. }]
  105. };
  106. rationItemModel.find(filter, function (err, data) {
  107. if (err) {
  108. callback(true, 'Fail to find ration', null);
  109. } else {
  110. callback(false, '', data);
  111. }
  112. })
  113. }
  114. rationItemDAO.prototype.getRationItem = function (repId, code, callback) {
  115. if (callback) {
  116. rationItemModel.findOne({rationRepId: repId, code: code, "$or": [{"isDeleted": null}, {"isDeleted": false}]}, '-_id').exec()
  117. .then(function (result, err) {
  118. if (err) {
  119. callback(1, '找不到定额“' + code +'”' , null);
  120. } else {
  121. callback(0, '', result);
  122. }
  123. });
  124. return null;
  125. } else {
  126. return rationItemModel.findOne({rationRepId: repId, code: code, "$or": [{"isDeleted": null}, {"isDeleted": false}]}, '-_id').exec();
  127. }
  128. };
  129. rationItemDAO.prototype.addRationItems = function(rationLibId, lastOpr, sectionId, items,callback){
  130. if (items && items.length > 0) {
  131. counter.counterDAO.getIDAfterCount(counter.moduleName.rations, items.length, function(err, result){
  132. var maxId = result.value.sequence_value;
  133. var arr = [];
  134. for (var i = 0; i < items.length; i++) {
  135. var obj = new rationItemModel(items[i]);
  136. obj.ID = (maxId - (items.length - 1) + i);
  137. obj.sectionId = sectionId;
  138. obj.rationRepId = rationLibId;
  139. arr.push(obj);
  140. }
  141. rationItemModel.collection.insert(arr, null, function(err, docs){
  142. if (err) {
  143. callback(true, "Fail to save", false);
  144. } else {
  145. rationRepositoryDao.updateOprArr({ID: rationLibId}, lastOpr, moment(Date.now()).format('YYYY-MM-DD HH:mm:ss'), function (err) {
  146. if(err){
  147. callback(true, "Fail to sava operator", false);
  148. }
  149. else{
  150. callback(false, "Save successfully", docs);
  151. }
  152. })
  153. }
  154. })
  155. });
  156. } else {
  157. callback(true, "Source error!", false);
  158. }
  159. };
  160. rationItemDAO.prototype.updateRationItems = function(rationLibId, lastOpr, sectionId, items,callback){
  161. var functions = [];
  162. for (var i=0; i < items.length; i++) {
  163. functions.push((function(doc) {
  164. return function(cb) {
  165. var filter = {};
  166. if (doc.ID) {
  167. filter.ID = doc.ID;
  168. } else {
  169. filter.sectionId = sectionId;
  170. if (rationLibId) filter.rationRepId = rationLibId;
  171. filter.code = doc.code;
  172. }
  173. rationItemModel.update(filter, doc, cb);
  174. };
  175. })(items[i]));
  176. }
  177. functions.push((function () {
  178. return function (cb) {
  179. rationRepositoryDao.updateOprArr({ID: rationLibId}, lastOpr, moment(Date.now()).format('YYYY-MM-DD HH:mm:ss'), function (err) {
  180. if(err){
  181. cb(err);
  182. }
  183. else{
  184. cb(null);
  185. }
  186. });
  187. }
  188. })());
  189. async.parallel(functions, function(err, results) {
  190. callback(err, results);
  191. });
  192. };
  193. //ration round func
  194. function round(v,e){
  195. var t=1;
  196. for(;e>0;t*=10,e--);
  197. for(;e<0;t/=10,e++);
  198. return Math.round(v*t)/t;
  199. }
  200. rationItemDAO.prototype.updateRationBasePrc = function (basePrcArr, callback) {
  201. // let basePrcArr = data.basePrcArr;
  202. // adjGljId = data.gljId, adjBasePrice = data.basePrice, adjGljType = data.gljType,
  203. // repId = data.repId, lastOpr = data.lastOpr;
  204. //
  205. // let updateArr;
  206. async.each(basePrcArr, function (basePrcObj, finalCb) {
  207. let adjGljId = basePrcObj.gljId, adjBasePrice = basePrcObj.basePrice, adjGljType = basePrcObj.gljType;
  208. async.waterfall([
  209. function (cb) {
  210. if(typeof basePrcObj.delete !== 'undefined' && basePrcObj.delete === 1){
  211. rationItemModel.find({'rationGljList.gljId': adjGljId},{ID: 1, rationGljList: 1}, function (err, result) {
  212. if(err){
  213. cb(err);
  214. }
  215. else{
  216. //删除
  217. rationItemModel.update({'rationGljList.gljId': adjGljId}, {$pull: {rationGljList: {gljId: adjGljId}}}, {multi: true}, function (err) {
  218. if(err){
  219. cb(err);
  220. }
  221. else{
  222. cb(null, result);
  223. }
  224. });
  225. }
  226. });
  227. }
  228. else{
  229. rationItemModel.find({'rationGljList.gljId': adjGljId}, function (err, result) {
  230. if(err){
  231. cb(err);
  232. }
  233. else{
  234. cb(null, result);
  235. }
  236. });
  237. }
  238. /* rationItemModel.find({'rationGljList.gljId': adjGljId}, function (err, result) {
  239. if(err){
  240. cb(err);
  241. }
  242. else{
  243. cb(null, result);
  244. }
  245. });*/
  246. },
  247. function (result, cb) {
  248. async.each(result, function (rationItem, ecb) {
  249. let rationGljList = rationItem.rationGljList,
  250. gljIds = [];
  251. rationGljList.forEach(function (rationGlj) {
  252. gljIds.push(rationGlj.gljId);
  253. });
  254. gljDao.getGljItems(gljIds, function(err, gljItems){
  255. if(err){
  256. ecb(err);
  257. }
  258. else{
  259. let gljArr = [];
  260. for(let i=0; i<gljItems.length; i++){
  261. let gljParentType = -1;
  262. if(gljItems[i].ID === adjGljId){
  263. gljItems[i].gljType = adjGljType;
  264. }
  265. if(gljItems[i].gljType <= 3){
  266. gljParentType = gljItems[i].gljType;
  267. }
  268. if(gljItems[i].gljType > 200 && gljItems[i].gljType < 300){
  269. gljParentType = 2;
  270. }
  271. if(gljItems[i].gljType > 300 && gljItems[i].gljType < 400){
  272. gljParentType = 3;
  273. }
  274. if(gljItems[i].ID === adjGljId){
  275. gljArr.push({gljId: gljItems[i].ID, basePrice: adjBasePrice, gljParentType: gljParentType});
  276. }
  277. else {
  278. gljArr.push({gljId: gljItems[i].ID, basePrice: parseFloat(gljItems[i].basePrice), gljParentType: gljParentType});
  279. }
  280. }
  281. gljArr.forEach(function (gljItem) {
  282. rationGljList.forEach(function (rationGlj) {
  283. if(gljItem.gljId === rationGlj.gljId){
  284. gljItem.consumeAmt = parseFloat(rationGlj.consumeAmt);
  285. }
  286. })
  287. });
  288. //recalculate the price of ration
  289. let labourPrc = [], materialPrc = [], machinePrc = [], singlePrc, updatePrc = {labourPrice: 0, materialPrice: 0, machinePrice: 0, basePrice: 0};
  290. gljArr.forEach(function (gljItem) {
  291. if(gljItem.gljParentType !== -1){
  292. singlePrc = scMathUtil.roundTo(gljItem.basePrice * gljItem.consumeAmt, -3);
  293. if(gljItem.gljParentType === 1){
  294. labourPrc.push(singlePrc);
  295. }
  296. else if(gljItem.gljParentType ===2){
  297. materialPrc.push(singlePrc);
  298. }
  299. else{
  300. machinePrc.push(singlePrc);
  301. }
  302. }
  303. });
  304. if(labourPrc.length > 0){
  305. let sumLaP = 0;
  306. for(let i=0; i<labourPrc.length; i++){
  307. sumLaP += labourPrc[i];
  308. }
  309. updatePrc.labourPrice = scMathUtil.roundTo(sumLaP, -2);
  310. }
  311. if(materialPrc.length > 0){
  312. let sumMtP = 0;
  313. for(let i= 0; i<materialPrc.length; i++){
  314. sumMtP += materialPrc[i];
  315. }
  316. updatePrc.materialPrice = scMathUtil.roundTo(sumMtP, -2);
  317. }
  318. if(machinePrc.length > 0){
  319. let sumMaP = 0;
  320. for(let i =0; i< machinePrc.length; i++){
  321. sumMaP += machinePrc[i];
  322. }
  323. updatePrc.machinePrice = scMathUtil.roundTo(sumMaP, -2);
  324. }
  325. updatePrc.basePrice = scMathUtil.roundTo(updatePrc.labourPrice + updatePrc.materialPrice + updatePrc.machinePrice, -2);
  326. //updateDataBase
  327. rationItemModel.update({ID: rationItem.ID}, {$set: {labourPrice: updatePrc.labourPrice.toString(), materialPrice: updatePrc.materialPrice.toString(),
  328. machinePrice: updatePrc.machinePrice.toString(), basePrice: updatePrc.basePrice.toString()}},
  329. function (err, result) {
  330. if(err){
  331. ecb(err);
  332. }
  333. else {
  334. ecb(null);
  335. }
  336. });
  337. }
  338. });
  339. }, function(err){
  340. if(err){
  341. cb(err);
  342. }
  343. else {
  344. cb(null);
  345. }
  346. });
  347. },
  348. ], function (err) {
  349. if(err){
  350. finalCb(err);
  351. }
  352. else{
  353. finalCb(null);
  354. }
  355. });
  356. }, function (err) {
  357. if(err){
  358. callback(err, 'Error');
  359. }
  360. else{
  361. callback(null, '');
  362. }
  363. });
  364. };
  365. rationItemDAO.prototype.getRationGljIds = function (data, callback) {
  366. let repId = data.repId;
  367. rationItemModel.find({rationRepId: repId}, function (err, result) {
  368. if(err){
  369. callback(err, 'Error', null);
  370. }
  371. else{
  372. let rstIds = [], newRst = [];
  373. result.forEach(function (data) {
  374. if(data.rationGljList.length >0){
  375. data.rationGljList.forEach(function (gljObj) {
  376. rstIds.push(gljObj.gljId);
  377. })
  378. }
  379. });
  380. for(let i= 0; i< rstIds.length; i++){
  381. if(newRst.indexOf(rstIds[i]) === -1){
  382. newRst.push(rstIds[i]);
  383. }
  384. }
  385. callback(null, '', newRst);
  386. }
  387. });
  388. };
  389. rationItemDAO.prototype.getRationsCodes = function (data, callback) {
  390. let repId = data.repId;
  391. rationItemModel.find({rationRepId: repId}, function (err, result) {
  392. if(err){
  393. callback(err, 'Error', null);
  394. }
  395. else{
  396. let rstCodes = [];
  397. result.forEach(function (rationItem) {
  398. rstCodes.push(rationItem.code);
  399. });
  400. callback(null, 'get all rationCodes success', rstCodes);
  401. }
  402. })
  403. };
  404. rationItemDAO.prototype.updateJobContent = function (lastOpr, repId, updateArr, callback) {
  405. rationRepositoryDao.updateOprArr({ID: repId}, lastOpr, moment(Date.now()).format('YYYY-MM-DD HH:mm:ss'), function (err) {
  406. async.each(updateArr, function (obj, cb) {
  407. rationItemModel.update({rationRepId: repId, code: obj.code}, {$set: {jobContent: obj.jobContent}}, function (err) {
  408. if(err){
  409. cb(err);
  410. }
  411. else{
  412. cb(null);
  413. }
  414. })
  415. }, function (err) {
  416. if(err){
  417. callback(err);
  418. }
  419. else{
  420. callback(null);
  421. }
  422. });
  423. });
  424. }
  425. rationItemDAO.prototype.updateAnnotation = function (lastOpr, repId, updateArr, callback) {
  426. rationRepositoryDao.updateOprArr({ID: repId}, lastOpr, moment(Date.now()).format('YYYY-MM-DD HH:mm:ss'), function (err) {
  427. async.each(updateArr, function (obj, cb) {
  428. rationItemModel.update({rationRepId: repId, code: obj.code}, {$set: {annotation: obj.annotation}}, function (err) {
  429. if(err){
  430. cb(err);
  431. }
  432. else{
  433. cb(null);
  434. }
  435. })
  436. }, function (err) {
  437. if(err){
  438. callback(err);
  439. }
  440. else{
  441. callback(null);
  442. }
  443. });
  444. });
  445. }
  446. module.exports = new rationItemDAO()