ration_item.js 16 KB

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