ration_item.js 13 KB

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