flexbiz-server 12.3.64 → 12.3.65
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
|
@@ -454,7 +454,7 @@ const createSocaiTC = async function(id_app, tu_ngay, den_ngay) {
|
|
|
454
454
|
}
|
|
455
455
|
|
|
456
456
|
// 4. Gọi (hoặc reset) debouncer
|
|
457
|
-
console.log(`[socai] 🔄 Reset bộ đếm cho ${id_app}. Hàng đợi hiện có ${dateSet.size} ngày.`);
|
|
457
|
+
//console.log(`[socai] 🔄 Reset bộ đếm cho ${id_app}. Hàng đợi hiện có ${dateSet.size} ngày.`);
|
|
458
458
|
debouncedProcessorsSocai.get(id_app)();
|
|
459
459
|
|
|
460
460
|
} catch (e) {
|
|
@@ -139,7 +139,7 @@ exports.handlerWithSession = async function (handler, ctrl, req, callback, ...ex
|
|
|
139
139
|
|
|
140
140
|
//xoá session khởi store cho an toán
|
|
141
141
|
(exports.getCurrentStore()||{}).session = null;
|
|
142
|
-
|
|
142
|
+
|
|
143
143
|
attempt++;
|
|
144
144
|
const isTransient =
|
|
145
145
|
err?.errorLabels?.includes("TransientTransactionError") ||
|
|
@@ -177,7 +177,7 @@ exports.onAfterCommit = function (cb) {
|
|
|
177
177
|
if (store && store.session && exports.isSessionActive(store.session)) {
|
|
178
178
|
store.session.afterCommit = store.session.afterCommit || [];
|
|
179
179
|
store.session.afterCommit.push(cb);
|
|
180
|
-
console.log(`[onAfterCommit] added event.. storeId=${store.storeId}, count=${store.session.afterCommit.length}, ctrl=${store.session.ctrl_name}`);
|
|
180
|
+
//console.log(`[onAfterCommit] added event.. storeId=${store.storeId}, count=${store.session.afterCommit.length}, ctrl=${store.session.ctrl_name}`);
|
|
181
181
|
} else {
|
|
182
182
|
// Không có session → chạy ngay
|
|
183
183
|
//console.log("[onAfterCommit] no session, running immediately");
|
package/server/libs/tinhgiatb.js
CHANGED
|
@@ -1,38 +1,713 @@
|
|
|
1
|
-
const ckvt=require(
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
(
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
1
|
+
const ckvt = require('./ckvt');
|
|
2
|
+
const sokho = global.getModel('sokho');
|
|
3
|
+
const socai = global.getModel('socai');
|
|
4
|
+
const dmvt = global.getModel('dmvt');
|
|
5
|
+
const giatb = global.getModel('giatb');
|
|
6
|
+
const dmqddvt = global.getModel('dmqddvt');
|
|
7
|
+
const tinhgiatb1vt = require('./tinhgiatb1vt');
|
|
8
|
+
const async = require('async');
|
|
9
|
+
const _ = require("lodash");
|
|
10
|
+
const Controller = require('../controllers/controller');
|
|
11
|
+
const moment = require('moment');
|
|
12
|
+
const {getCurrentSession} = require("./sessionContext")
|
|
13
|
+
module.exports = async function(condition, fn) {
|
|
14
|
+
//kiem tra dieu kien
|
|
15
|
+
if (!condition || !condition.tu_thang || !condition.den_thang || !condition.nam || !condition.id_app) {
|
|
16
|
+
fn('Lỗi: Tính năng này yêu cầu các tham số: tu_thang,den_thang,nam,id_app');
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
condition.tu_thang = Number(condition.tu_thang);
|
|
20
|
+
condition.den_thang = Number(condition.den_thang);
|
|
21
|
+
//lay dieu kien
|
|
22
|
+
let ma_kho = condition.ma_kho;
|
|
23
|
+
//let tu_ngay = new Date(Date.UTC(condition.nam, condition.tu_thang-1, 1,0,0,0));
|
|
24
|
+
//let den_ngay = new Date(Date.UTC(condition.nam, condition.den_thang,0,23,59, 0));
|
|
25
|
+
let tu_ngay = moment(new Date(condition.nam, condition.tu_thang - 1, 15)).startOf("month").toDate();
|
|
26
|
+
let den_ngay = moment(new Date(condition.nam, condition.den_thang-1, 15)).endOf("month").toDate();
|
|
27
|
+
|
|
28
|
+
//console.log(tu_ngay,den_ngay)
|
|
29
|
+
let id_app = condition.id_app;
|
|
30
|
+
const app = await global.getModel("app").findOne({_id:id_app},{options:1}).lean();
|
|
31
|
+
if(!app) return fn("Công ty này không tồn tại");
|
|
32
|
+
const f_tien = (app.options||{}).f_tien || 0;
|
|
33
|
+
let query_dmvt = {
|
|
34
|
+
id_app: id_app,
|
|
35
|
+
gia_xuat: '1'
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
if (condition.ma_nvt)
|
|
39
|
+
query_dmvt.ma_nvt = condition.ma_nvt
|
|
40
|
+
if (condition.ma_ncc)
|
|
41
|
+
query_dmvt.ma_ncc = condition.ma_ncc
|
|
42
|
+
|
|
43
|
+
if (condition.ma_vt) {
|
|
44
|
+
query_dmvt.ma_vt = condition.ma_vt;
|
|
45
|
+
}
|
|
46
|
+
console.log(`🔥 [tinhgiatb] tính giá trung bình, kho:${condition.ma_kho}, sessionID=${getCurrentSession()?._debugId}`);
|
|
47
|
+
dmvt.find(query_dmvt).lean().then(function(dmvts) {
|
|
48
|
+
async.mapLimit(dmvts,50, function(vt, callback) {
|
|
49
|
+
setImmediate(()=>{
|
|
50
|
+
let query = {
|
|
51
|
+
id_app: id_app,
|
|
52
|
+
tu_ngay: tu_ngay,
|
|
53
|
+
den_ngay: den_ngay,
|
|
54
|
+
ma_vt: vt.ma_vt,
|
|
55
|
+
ma_kho: ma_kho
|
|
56
|
+
};
|
|
57
|
+
tinhgiatb1vt(query, function(error, gia) {
|
|
58
|
+
if (error){
|
|
59
|
+
console.error("[tinhgiatb] Không thể tính giá trung bình cho vật tư",query.ma_vt,error);
|
|
60
|
+
return callback(error);
|
|
61
|
+
}
|
|
62
|
+
//console.log("tinh gia trung binh",{tu_ngay,den_ngay,ma_kho,ma_vt:vt.ma_vt,gia:gia.gia});
|
|
63
|
+
gia.id_app = id_app;
|
|
64
|
+
gia.ma_kho = ma_kho;
|
|
65
|
+
gia.status = true;
|
|
66
|
+
|
|
67
|
+
callback(null, gia);
|
|
68
|
+
});
|
|
69
|
+
})
|
|
70
|
+
}, function(error, bang_gia) {
|
|
71
|
+
if (error){
|
|
72
|
+
return fn(error);
|
|
73
|
+
}
|
|
74
|
+
//Chỉ lấy những vật tư có giá
|
|
75
|
+
/*bang_gia = _.filter(bang_gia, function(r) {
|
|
76
|
+
return r.gia !== 0 || r.tong_sl !== 0 || r.tong_tien !== 0;
|
|
77
|
+
});*/
|
|
78
|
+
|
|
79
|
+
//cap nhat gia vao so sach
|
|
80
|
+
let thangs = [];
|
|
81
|
+
for (let t = condition.tu_thang; t <= condition.den_thang; t++) {
|
|
82
|
+
thangs.push(t);
|
|
83
|
+
}
|
|
84
|
+
async.map(thangs, function(t, callback) {
|
|
85
|
+
async.mapLimit(bang_gia,50, function(gia, callback) {
|
|
86
|
+
setImmediate(()=>{
|
|
87
|
+
gia.thang = t;
|
|
88
|
+
gia.nam = condition.nam;
|
|
89
|
+
async.series({
|
|
90
|
+
savetogiatb: function(callback) {
|
|
91
|
+
//save to giatb
|
|
92
|
+
let query_delete = {
|
|
93
|
+
id_app: id_app,
|
|
94
|
+
ma_vt: gia.ma_vt,
|
|
95
|
+
nam:condition.nam,
|
|
96
|
+
thang:t
|
|
97
|
+
};
|
|
98
|
+
if (ma_kho) {
|
|
99
|
+
query_delete.ma_kho = ma_kho;
|
|
100
|
+
}
|
|
101
|
+
giatb.deleteMany(query_delete, function(error) {
|
|
102
|
+
if (error) return callback(error);
|
|
103
|
+
giatb.create(gia, function(error, result) {
|
|
104
|
+
if (error) {
|
|
105
|
+
console.error('[tinhgiatb] Loi khi luu vao bang gia trung binh thang vat tu ' + gia.ma_vt,error);
|
|
106
|
+
return callback(error);
|
|
107
|
+
}
|
|
108
|
+
callback(null, result);
|
|
109
|
+
});
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
}, function(error) {
|
|
113
|
+
if (error) return callback(error);
|
|
114
|
+
callback(null);
|
|
115
|
+
});
|
|
116
|
+
})
|
|
117
|
+
}, function(error) {
|
|
118
|
+
if (error) return callback(error);
|
|
119
|
+
callback(null);
|
|
120
|
+
});
|
|
121
|
+
}, function(error) {
|
|
122
|
+
if(error) return fn(error);
|
|
123
|
+
//Lấy các chứng từ cần cập nhật sổ sách
|
|
124
|
+
let vouchers_x = {};
|
|
125
|
+
let vouchers_n = {};
|
|
126
|
+
let ma_vts = bang_gia.map(v=>v.ma_vt);
|
|
127
|
+
async.parallel({
|
|
128
|
+
xuat: function(callback) {
|
|
129
|
+
setImmediate(()=>{
|
|
130
|
+
//get phieu xuat
|
|
131
|
+
let query_sokho_x = {
|
|
132
|
+
id_app: id_app,
|
|
133
|
+
ngay_ct: {
|
|
134
|
+
$gte: tu_ngay,
|
|
135
|
+
$lte: den_ngay
|
|
136
|
+
},
|
|
137
|
+
nxt: 2,
|
|
138
|
+
ma_vt: {
|
|
139
|
+
$in: ma_vts
|
|
140
|
+
},
|
|
141
|
+
px_gia_dd: false
|
|
142
|
+
};
|
|
143
|
+
if (ma_kho) {
|
|
144
|
+
query_sokho_x.ma_kho = ma_kho;
|
|
145
|
+
}
|
|
146
|
+
sokho.find(query_sokho_x).lean().then(async function(sks) {
|
|
147
|
+
//console.log("cap nhat gia cho phieu xuat kho",query_sokho_x,sks);
|
|
148
|
+
if (!sks || sks.length === 0) {
|
|
149
|
+
return callback(null, vouchers_x);
|
|
150
|
+
}
|
|
151
|
+
//groupby lại chỉ lấy mỗi chứng từ một dòng
|
|
152
|
+
sks = await sks.asyncGroupBy(["id_ct","so_ct","ma_ct","ngay_ct"],[]);
|
|
153
|
+
//Tìm chứng từ gốc
|
|
154
|
+
async.mapLimit(sks,50, function(sk, callback) {
|
|
155
|
+
let voucher = vouchers_x[sk.id_ct];
|
|
156
|
+
const ctrl = global.controllers[sk.ma_ct.toUpperCase()];
|
|
157
|
+
let ct = ctrl?ctrl.getProperty("model"): mongoose.models[sk.ma_ct.toLowerCase()];
|
|
158
|
+
if (!voucher && ct) {
|
|
159
|
+
ct.findById(sk.id_ct, function(error, v) {
|
|
160
|
+
if (error) return callback(error);
|
|
161
|
+
if (v) {
|
|
162
|
+
voucher = v;
|
|
163
|
+
vouchers_x[sk.id_ct] = voucher;
|
|
164
|
+
} else {
|
|
165
|
+
sokho.deleteMany({id_ct:sk.id_ct.toString()},(e,rs)=>{
|
|
166
|
+
console.log('[tinhgiatb] Khong tim thay phieu xuat. chuong trinh da xoa phieu nay khoi sokho',sk.id_ct,"ma_ct",sk.ma_ct,sk.so_ct,sk.ngay_ct,rs);
|
|
167
|
+
})
|
|
168
|
+
socai.deleteMany({id_ct:sk.id_ct.toString()},(e,rs)=>{
|
|
169
|
+
console.log('[tinhgiatb] Khong tim thay phieu xuat. chuong trinh da xoa phieu nay khoi socai',sk.id_ct,"ma_ct",sk.ma_ct,sk.so_ct,sk.ngay_ct,rs);
|
|
170
|
+
})
|
|
171
|
+
}
|
|
172
|
+
callback();
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
} else {
|
|
176
|
+
callback();
|
|
177
|
+
}
|
|
178
|
+
}, function(error) {
|
|
179
|
+
if (error) {
|
|
180
|
+
console.error('[tinhgiatb] co loi lay phieu xuat',error);
|
|
181
|
+
return callback(error);
|
|
182
|
+
}
|
|
183
|
+
callback(null, vouchers_x);
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
}).catch(e=>{
|
|
187
|
+
callback(e);
|
|
188
|
+
})
|
|
189
|
+
})
|
|
190
|
+
},
|
|
191
|
+
nhap: function(callback) {
|
|
192
|
+
setImmediate(()=>{
|
|
193
|
+
//get phieu nhap gia tb
|
|
194
|
+
let query_sokho_n = {
|
|
195
|
+
id_app: id_app,
|
|
196
|
+
ngay_ct: {
|
|
197
|
+
$gte: tu_ngay,
|
|
198
|
+
$lte: den_ngay
|
|
199
|
+
},
|
|
200
|
+
nxt: 1,
|
|
201
|
+
ma_vt: {
|
|
202
|
+
$in: ma_vts
|
|
203
|
+
},
|
|
204
|
+
pn_gia_tb: true,
|
|
205
|
+
ma_ct: {
|
|
206
|
+
$ne: 'PXC'
|
|
207
|
+
}
|
|
208
|
+
};
|
|
209
|
+
if(ma_kho) query_sokho_n.ma_kho = ma_kho;
|
|
210
|
+
sokho.find(query_sokho_n, async function(error, sks) {
|
|
211
|
+
if (error) return callback(error);
|
|
212
|
+
//console.log("cap nhat gia cho phieu nhap kho",query_sokho_n,sks);
|
|
213
|
+
if (!sks || sks.length === 0) {
|
|
214
|
+
return callback(null, vouchers_n);
|
|
215
|
+
}
|
|
216
|
+
//groupby lại chỉ lấy mỗi chứng từ một dòng
|
|
217
|
+
sks = await sks.asyncGroupBy(["id_ct","so_ct","ma_ct","ngay_ct"],[]);
|
|
218
|
+
//Tìm chứng từ gốc
|
|
219
|
+
async.mapLimit(sks,50, function(sk, callback) {
|
|
220
|
+
let voucher = vouchers_n[sk.id_ct];
|
|
221
|
+
const ctrl = global.controllers[sk.ma_ct.toUpperCase()];
|
|
222
|
+
let ct = ctrl?ctrl.getProperty("model"): mongoose.models[sk.ma_ct.toLowerCase()];
|
|
223
|
+
if (!voucher && ct) {
|
|
224
|
+
ct.findById(sk.id_ct, function(error, v) {
|
|
225
|
+
if (error)
|
|
226
|
+
return callback(error);
|
|
227
|
+
if (v) {
|
|
228
|
+
voucher = v;
|
|
229
|
+
vouchers_n[sk.id_ct] = voucher;
|
|
230
|
+
} else {
|
|
231
|
+
sokho.deleteMany({id_ct:sk.id_ct.toString()},(e,rs)=>{
|
|
232
|
+
console.log('[tinhgiatb] Khong tim thay phieu nhap. chuong trinh da xoa phieu nay khoi sokho',sk.id_ct,"ma_ct",sk.ma_ct,sk.so_ct,sk.ngay_ct,rs);
|
|
233
|
+
})
|
|
234
|
+
socai.deleteMany({id_ct:sk.id_ct.toString()},(e,rs)=>{
|
|
235
|
+
console.log('[tinhgiatb] Khong tim thay phieu nhap. chuong trinh da xoa phieu nay khoi socai',sk.id_ct,"ma_ct",sk.ma_ct,sk.so_ct,sk.ngay_ct,rs);
|
|
236
|
+
})
|
|
237
|
+
}
|
|
238
|
+
callback();
|
|
239
|
+
});
|
|
240
|
+
} else {
|
|
241
|
+
callback();
|
|
242
|
+
}
|
|
243
|
+
}, function(error) {
|
|
244
|
+
if (error) {
|
|
245
|
+
console.error('[tinhgiatb] co loi lay phieu nhap',error);
|
|
246
|
+
return callback(error);
|
|
247
|
+
}
|
|
248
|
+
callback(null, vouchers_n);
|
|
249
|
+
});
|
|
250
|
+
});
|
|
251
|
+
})
|
|
252
|
+
},
|
|
253
|
+
nhap_dc:(callback)=>{
|
|
254
|
+
if(ma_kho){
|
|
255
|
+
let query_sokho_pnc = {
|
|
256
|
+
id_app: id_app,
|
|
257
|
+
ma_ct:"PNC",
|
|
258
|
+
ngay_ct: {
|
|
259
|
+
$gte: tu_ngay,
|
|
260
|
+
$lte: den_ngay
|
|
261
|
+
},
|
|
262
|
+
$or:[
|
|
263
|
+
{ma_kho_x:ma_kho},
|
|
264
|
+
{"details.ma_kho_x":ma_kho}
|
|
265
|
+
],
|
|
266
|
+
"details.px_gia_dd":false
|
|
267
|
+
};
|
|
268
|
+
//console.log("get pnc",query_sokho_pnc);
|
|
269
|
+
global.getModel("pnc").find(query_sokho_pnc).then(pnc=>{
|
|
270
|
+
//console.log("[tinhgiatb] Số phiếu nhập điều chuyển của kho",ma_kho,pnc.length);
|
|
271
|
+
pnc.forEach(p=>{
|
|
272
|
+
vouchers_n[p._id.toString()] = p;
|
|
273
|
+
})
|
|
274
|
+
callback();
|
|
275
|
+
}).catch(e=>{
|
|
276
|
+
console.error('[tinhgiatb] co loi lay phieu nhap dieu chuyen',e);
|
|
277
|
+
return callback(e);
|
|
278
|
+
})
|
|
279
|
+
}else{
|
|
280
|
+
callback();
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
}, function(error) {
|
|
284
|
+
if (error) return fn(error);
|
|
285
|
+
async.waterfall([
|
|
286
|
+
function(callback) {
|
|
287
|
+
//update gia xuat cho cac phieu xuat
|
|
288
|
+
async.mapLimit(_.values(vouchers_x),100, (voucher_x, callback) => {
|
|
289
|
+
setImmediate(()=>{
|
|
290
|
+
(async ()=>{
|
|
291
|
+
//thong thuong
|
|
292
|
+
const details = voucher_x.details.filter(d=>(!condition.ma_vt || condition.ma_vt ===d.ma_vt) && (
|
|
293
|
+
!d.px_gia_dd && (!ma_kho || ma_kho === (d.ma_kho || d.ma_kho_x || voucher_x.ma_kho || voucher_x.ma_kho_x))
|
|
294
|
+
));
|
|
295
|
+
await Promise.all(details.map(d => {
|
|
296
|
+
return (async ()=>{
|
|
297
|
+
let gia = _.find(bang_gia, (vt) => {
|
|
298
|
+
return d.ma_vt === vt.ma_vt;
|
|
299
|
+
})
|
|
300
|
+
//gia chuan
|
|
301
|
+
let gia_chuan = gia? gia.gia: 0;
|
|
302
|
+
//tinh he so quy doi
|
|
303
|
+
let he_so_qd=1;
|
|
304
|
+
let qd = await dmqddvt.findOne({id_app:voucher_x.id_app,ma_vt:d.ma_vt,ma_dvt:d.ma_dvt}).lean();
|
|
305
|
+
if(qd) he_so_qd = (qd.mau?(qd.tu/qd.mau): qd.ty_le_qd);
|
|
306
|
+
//cap nhat gia
|
|
307
|
+
d.gia_von = d.gia_von_nt = gia_chuan * he_so_qd;
|
|
308
|
+
d.tien_xuat = d.tien_xuat_nt = Math.roundBy(d.sl_xuat * d.gia_von, f_tien);
|
|
309
|
+
//console.log("update gia trung binh xuat hang",{ma_ct:voucher_x.ma_ct,so_ct:voucher_x.so_ct,ma_vt:d.ma_vt,gia_von:d.gia_von,gia_tb:gia_chuan,he_so_qd});
|
|
310
|
+
})();
|
|
311
|
+
}))
|
|
312
|
+
//doi doi hang
|
|
313
|
+
if(voucher_x.details_doi){
|
|
314
|
+
const details_doi = voucher_x.details_doi.filter(d=>(!condition.ma_vt || condition.ma_vt ===d.ma_vt) && (
|
|
315
|
+
!d.px_gia_dd && (!ma_kho || ma_kho === (d.ma_kho || voucher_x.ma_kho || voucher_x.ma_kho_x))
|
|
316
|
+
));
|
|
317
|
+
await Promise.all(details_doi.map(d => {
|
|
318
|
+
return (async ()=>{
|
|
319
|
+
let gia = _.find(bang_gia, (vt) => {
|
|
320
|
+
return d.ma_vt === vt.ma_vt;
|
|
321
|
+
})
|
|
322
|
+
//gia chuan
|
|
323
|
+
let gia_chuan = gia? gia.gia: 0;
|
|
324
|
+
//tinh he so quy doi
|
|
325
|
+
let he_so_qd=1;
|
|
326
|
+
let qd = await dmqddvt.findOne({id_app:voucher_x.id_app,ma_vt:d.ma_vt,ma_dvt:d.ma_dvt}).lean();
|
|
327
|
+
if(qd) he_so_qd = (qd.mau?(qd.tu/qd.mau): qd.ty_le_qd);
|
|
328
|
+
//cap nhat gia
|
|
329
|
+
d.gia_von = d.gia_von_nt = gia_chuan * he_so_qd;
|
|
330
|
+
d.tien_xuat = d.tien_xuat_nt = Math.roundBy(d.sl_xuat * d.gia_von, f_tien);
|
|
331
|
+
|
|
332
|
+
//console.log("update gia trung binh doi hang",{ma_ct:voucher_x.ma_ct,so_ct:voucher_x.so_ct,ma_vt:d.ma_vt,gia_von:d.gia_von,gia_tb:gia_chuan,he_so_qd});
|
|
333
|
+
})();
|
|
334
|
+
}))
|
|
335
|
+
}
|
|
336
|
+
//khuyen mai detail
|
|
337
|
+
await Promise.all(voucher_x.details.filter(d=>d.promotion && d.promotion.details_km).map(d=> {
|
|
338
|
+
return (async ()=>{
|
|
339
|
+
const details_km = d.promotion.details_km.filter(d=>(!condition.ma_vt || condition.ma_vt ===d.ma_vt)
|
|
340
|
+
&& !d.px_gia_dd && (!ma_kho || ma_kho === (d.ma_kho || voucher_x.ma_kho|| voucher_x.ma_kho_x))
|
|
341
|
+
);
|
|
342
|
+
await Promise.all(details_km.map(d => {
|
|
343
|
+
return (async ()=>{
|
|
344
|
+
let gia = _.find(bang_gia, (vt) => {
|
|
345
|
+
return d.ma_vt === vt.ma_vt;
|
|
346
|
+
})
|
|
347
|
+
//gia chuan
|
|
348
|
+
let gia_chuan = gia? gia.gia: 0;
|
|
349
|
+
//tinh he so quy doi
|
|
350
|
+
let he_so_qd=1;
|
|
351
|
+
let qd = await dmqddvt.findOne({id_app:voucher_x.id_app,ma_vt:d.ma_vt,ma_dvt:d.ma_dvt}).lean();
|
|
352
|
+
if(qd) he_so_qd = (qd.mau?(qd.tu/qd.mau): qd.ty_le_qd);
|
|
353
|
+
//update gia
|
|
354
|
+
d.gia_von = d.gia_von_nt = gia_chuan * he_so_qd;
|
|
355
|
+
d.tien_xuat = d.tien_xuat_nt = Math.roundBy((d.sl_xuat||d.sl_km||0) * d.gia_von, f_tien);
|
|
356
|
+
//console.log("update gia trung binh khuyen mai",{ma_ct:voucher_x.ma_ct,so_ct:voucher_x.so_ct,ma_vt:d.ma_vt,gia_von:d.gia_von,gia_tb:gia_chuan,he_so_qd});
|
|
357
|
+
})();
|
|
358
|
+
}))
|
|
359
|
+
})();
|
|
360
|
+
}))
|
|
361
|
+
//combo detail
|
|
362
|
+
await Promise.all(voucher_x.details.filter(d=>d.combo && d.combo.length>0 && !d.px_gia_dd).map(detail=> {
|
|
363
|
+
return (async ()=>{
|
|
364
|
+
const combo = detail.combo.filter(c=>(!condition.ma_vt || condition.ma_vt ===c.ma_vt)
|
|
365
|
+
&& (!ma_kho || ma_kho === (detail.ma_kho || detail.ma_kho_x || voucher_x.ma_kho|| voucher_x.ma_kho_x))
|
|
366
|
+
);
|
|
367
|
+
await Promise.all(combo.map(d => {
|
|
368
|
+
return (async ()=>{
|
|
369
|
+
let gia = _.find(bang_gia, (vt) => {
|
|
370
|
+
return d.ma_vt === vt.ma_vt;
|
|
371
|
+
})
|
|
372
|
+
//gia chuan
|
|
373
|
+
let gia_chuan = gia? gia.gia: 0;
|
|
374
|
+
//tinh he so quy doi
|
|
375
|
+
let he_so_qd=1;
|
|
376
|
+
let qd = await dmqddvt.findOne({id_app:voucher_x.id_app,ma_vt:d.ma_vt,ma_dvt:d.ma_dvt}).lean();
|
|
377
|
+
if(qd) he_so_qd = (qd.mau?(qd.tu/qd.mau): qd.ty_le_qd);
|
|
378
|
+
//update gia
|
|
379
|
+
d.gia_von = d.gia_von_nt = gia_chuan * he_so_qd;
|
|
380
|
+
d.tien_xuat = d.tien_xuat_nt = Math.roundBy((d.sl_xuat||0) * d.gia_von, f_tien);
|
|
381
|
+
|
|
382
|
+
/*if(voucher_x.ma_ct==="PXC"){
|
|
383
|
+
console.log(d);
|
|
384
|
+
}*/
|
|
385
|
+
//console.log("update gia trung binh khuyen mai",{ma_ct:voucher_x.ma_ct,so_ct:voucher_x.so_ct,ma_vt:d.ma_vt,gia_von:d.gia_von,gia_tb:gia_chuan,he_so_qd});
|
|
386
|
+
})();
|
|
387
|
+
}))
|
|
388
|
+
//detail.gia_von = detail.gia_von_nt = detail.combo.map(c=>(c.gia_von_nt||0)).reduce((a,b)=>a+b,0);
|
|
389
|
+
detail.tien_xuat = detail.tien_xuat_nt = detail.combo.map(c=>(c.tien_xuat_nt||0)).reduce((a,b)=>a+b,0);
|
|
390
|
+
detail.gia_von = detail.gia_von_nt = Math.roundBy(detail.sl_xuat?detail.tien_xuat_nt/detail.sl_xuat:0,f_tien);
|
|
391
|
+
})();
|
|
392
|
+
}))
|
|
393
|
+
//khuyen mai chung
|
|
394
|
+
if (voucher_x.promotion) {
|
|
395
|
+
const details_kmc = voucher_x.promotion.filter(d=>(!condition.ma_vt || condition.ma_vt ===d.ma_vt)
|
|
396
|
+
&& !d.px_gia_dd && (!ma_kho || ma_kho === (d.ma_kho || voucher_x.ma_kho|| voucher_x.ma_kho_x))
|
|
397
|
+
);
|
|
398
|
+
await Promise.all(details_kmc.map(d => {
|
|
399
|
+
return (async ()=>{
|
|
400
|
+
let gia = _.find(bang_gia, (vt) => {
|
|
401
|
+
return d.ma_vt === vt.ma_vt;
|
|
402
|
+
})
|
|
403
|
+
//gia chuan
|
|
404
|
+
let gia_chuan = gia? gia.gia: 0;
|
|
405
|
+
//tinh he so quy doi
|
|
406
|
+
let he_so_qd=1;
|
|
407
|
+
let qd = await dmqddvt.findOne({id_app:voucher_x.id_app,ma_vt:d.ma_vt,ma_dvt:d.ma_dvt}).lean();
|
|
408
|
+
if(qd) he_so_qd = (qd.mau?(qd.tu/qd.mau): qd.ty_le_qd);
|
|
409
|
+
//update gia
|
|
410
|
+
d.gia_von_nt = gia_chuan * he_so_qd;
|
|
411
|
+
d.gia_von = d.gia_von_nt;
|
|
412
|
+
d.tien_xuat_nt = Math.roundBy((d.sl_xuat||d.sl_km||0) * d.gia_von, f_tien);
|
|
413
|
+
d.tien_xuat = d.tien_xuat_nt;
|
|
414
|
+
|
|
415
|
+
//console.log("update gia trung binh khuyen mai chung",{ma_ct:voucher_x.ma_ct,so_ct:voucher_x.so_ct,ma_vt:d.ma_vt,gia_von:d.gia_von,gia_tb:gia_chuan,he_so_qd});
|
|
416
|
+
})();
|
|
417
|
+
}))
|
|
418
|
+
}
|
|
419
|
+
//save
|
|
420
|
+
let voucher_x_data = voucher_x.toObject();
|
|
421
|
+
try{
|
|
422
|
+
//let _model = mongoose.models[voucher_x.ma_ct.toLowerCase()];
|
|
423
|
+
const ctrl = global.controllers[voucher_x.ma_ct.toUpperCase()];
|
|
424
|
+
let _model = ctrl?ctrl.getProperty("model"): mongoose.models[voucher_x.ma_ct.toLowerCase()];
|
|
425
|
+
|
|
426
|
+
await _model.updateOne({_id:voucher_x._id},voucher_x_data)
|
|
427
|
+
}catch(e){
|
|
428
|
+
return callback(e);
|
|
429
|
+
}
|
|
430
|
+
//post lai so sach
|
|
431
|
+
let ctrl = global.controllers[voucher_x_data.ma_ct.toUpperCase()];
|
|
432
|
+
if (ctrl && ctrl.post) {
|
|
433
|
+
await (async ()=>{
|
|
434
|
+
return new Promise((resolve,reject)=>{
|
|
435
|
+
//console.log("posted book",{ma_ct:voucher_x.ma_ct,so_ct:voucher_x.so_ct,_id:voucher_x_data._id});
|
|
436
|
+
Controller.postData(voucher_x_data,ctrl, (e, rs) => {
|
|
437
|
+
if(e){
|
|
438
|
+
console.error("❌ [tinhgiatb] error repost khi tinh gia trung binh",e.message,voucher_x_data.so_ct,voucher_x_data.ma_ct);
|
|
439
|
+
return reject(e);
|
|
440
|
+
}
|
|
441
|
+
resolve(rs);
|
|
442
|
+
},{kiem_tra_han_muc_cong_no:false});
|
|
443
|
+
})
|
|
444
|
+
})();
|
|
445
|
+
}
|
|
446
|
+
})().then(rs=>{
|
|
447
|
+
callback(null,rs);
|
|
448
|
+
}).catch(e=>{
|
|
449
|
+
callback(e);
|
|
450
|
+
})
|
|
451
|
+
})
|
|
452
|
+
|
|
453
|
+
}, (error) => {
|
|
454
|
+
callback(error,vouchers_x);
|
|
455
|
+
});
|
|
456
|
+
},
|
|
457
|
+
function(_vouchers_x,callback) {
|
|
458
|
+
//update gia nhap cho cac phieu nhap gia trung binh
|
|
459
|
+
async.mapLimit(_.values(vouchers_n),100, (voucher_n, callback)=>{
|
|
460
|
+
setImmediate(()=>{
|
|
461
|
+
(async ()=>{
|
|
462
|
+
const details_pnk = voucher_n.details.filter(d=>(!d.combo || d.combo.length==0) && (!condition.ma_vt || condition.ma_vt ===d.ma_vt)
|
|
463
|
+
&& (d.pn_gia_tb || (voucher_n.ma_ct ==="PNC" && !d.px_gia_dd) || (voucher_n.ma_ct ==="PKK" && !d.px_gia_dd))
|
|
464
|
+
&& (!ma_kho || ma_kho === d.ma_kho || ma_kho === d.ma_kho_x || ma_kho===voucher_n.ma_kho || ma_kho===voucher_n.ma_kho_n)
|
|
465
|
+
);
|
|
466
|
+
await Promise.all(details_pnk.map(d => {
|
|
467
|
+
return (async ()=>{
|
|
468
|
+
let gia = _.find(bang_gia, (vt) => {
|
|
469
|
+
return d.ma_vt === vt.ma_vt;
|
|
470
|
+
})
|
|
471
|
+
//gia chuan
|
|
472
|
+
let gia_chuan = gia? gia.gia: 0;
|
|
473
|
+
//tinh he so quy doi
|
|
474
|
+
let he_so_qd=1;
|
|
475
|
+
let qd = await dmqddvt.findOne({id_app:vouchers_n.id_app,ma_vt:d.ma_vt,ma_dvt:d.ma_dvt}).lean();
|
|
476
|
+
if(qd) he_so_qd = (qd.mau?(qd.tu/qd.mau): qd.ty_le_qd);
|
|
477
|
+
//update gia
|
|
478
|
+
d.gia_von_nt = gia_chuan * he_so_qd;
|
|
479
|
+
d.gia_von = d.gia_von_nt;
|
|
480
|
+
if(voucher_n.ma_ct ==="PNC" || voucher_n.ma_ct ==="PKK"){
|
|
481
|
+
d.tien_xuat_nt = Math.roundBy(d.sl_xuat * d.gia_von, f_tien);
|
|
482
|
+
d.tien_xuat = d.tien_xuat_nt;
|
|
483
|
+
}else{
|
|
484
|
+
d.tien_nhap_nt = Math.roundBy(d.sl_nhap * d.gia_von, f_tien);
|
|
485
|
+
d.tien_nhap = d.tien_nhap_nt;
|
|
486
|
+
}
|
|
487
|
+
//console.log("update gia trung binh cho phieu nhap gia tb",{ma_ct:vouchers_n.ma_ct,so_ct:vouchers_n.so_ct,ma_vt:d.ma_vt,gia_von:d.gia_von,gia_tb:gia_chuan,he_so_qd});
|
|
488
|
+
})();
|
|
489
|
+
}))
|
|
490
|
+
|
|
491
|
+
//combo detail
|
|
492
|
+
await Promise.all(voucher_n.details.filter(d=>d.combo && d.combo.length>0 && (d.pn_gia_tb || (voucher_n.ma_ct ==="PNC" && !d.px_gia_dd) || (voucher_n.ma_ct ==="PKK" && !d.px_gia_dd) ) ).map(detail=> {
|
|
493
|
+
return (async ()=>{
|
|
494
|
+
const combo = detail.combo.filter(c=>(!condition.ma_vt || condition.ma_vt ===c.ma_vt) && (!ma_kho || ma_kho === (detail.ma_kho || voucher_n.ma_kho|| voucher_n.ma_kho_n)));
|
|
495
|
+
await Promise.all(combo.map(d => {
|
|
496
|
+
return (async ()=>{
|
|
497
|
+
let gia = _.find(bang_gia, (vt) => {
|
|
498
|
+
return d.ma_vt === vt.ma_vt;
|
|
499
|
+
})
|
|
500
|
+
//gia chuan
|
|
501
|
+
let gia_chuan = gia? gia.gia: 0;
|
|
502
|
+
//tinh he so quy doi
|
|
503
|
+
let he_so_qd=1;
|
|
504
|
+
let qd = await dmqddvt.findOne({id_app:voucher_n.id_app,ma_vt:d.ma_vt,ma_dvt:d.ma_dvt}).lean();
|
|
505
|
+
if(qd) he_so_qd = (qd.mau?(qd.tu/qd.mau): qd.ty_le_qd);
|
|
506
|
+
//update gia
|
|
507
|
+
d.gia_von = d.gia_von_nt = gia_chuan * he_so_qd;
|
|
508
|
+
|
|
509
|
+
if(voucher_n.ma_ct ==="PNC" || voucher_n.ma_ct ==="PKK"){
|
|
510
|
+
d.tien_xuat_nt = Math.roundBy(d.sl_xuat * d.gia_von, f_tien);
|
|
511
|
+
d.tien_xuat = d.tien_xuat_nt;
|
|
512
|
+
}else{
|
|
513
|
+
d.tien_nhap_nt = Math.roundBy(d.sl_nhap * d.gia_von, f_tien);
|
|
514
|
+
d.tien_nhap = d.tien_nhap_nt;
|
|
515
|
+
}
|
|
516
|
+
//console.log("update gia trung binh cho san pham cua combo",{ma_ct:voucher_n.ma_ct,so_ct:voucher_n.so_ct,ma_vt:d.ma_vt,gia_von:d.gia_von,gia_tb:gia_chuan,he_so_qd});
|
|
517
|
+
})();
|
|
518
|
+
}))
|
|
519
|
+
//detail.gia_von = detail.gia_von_nt = detail.combo.map(c=>(c.gia_von_nt||0)).reduce((a,b)=>a+b,0);
|
|
520
|
+
if(voucher_n.ma_ct ==="PNC" || voucher_n.ma_ct ==="PKK"){
|
|
521
|
+
detail.tien_xuat = detail.tien_xuat_nt = detail.combo.map(c=>(c.tien_xuat_nt||0)).reduce((a,b)=>a+b,0);
|
|
522
|
+
detail.gia_von = detail.gia_von_nt = Math.roundBy(detail.sl_xuat?detail.tien_xuat_nt/detail.sl_xuat:0,f_tien);
|
|
523
|
+
}else{
|
|
524
|
+
detail.tien_nhap = detail.tien_nhap_nt = detail.combo.map(c=>(c.tien_nhap_nt||0)).reduce((a,b)=>a+b,0);
|
|
525
|
+
detail.gia_von = detail.gia_von_nt = Math.roundBy(detail.sl_nhap?detail.tien_nhap_nt/detail.sl_nhap:0,f_tien);
|
|
526
|
+
}
|
|
527
|
+
})();
|
|
528
|
+
}))
|
|
529
|
+
//
|
|
530
|
+
if (voucher_n.details_doi) { //xuat doi hang
|
|
531
|
+
const details_doi =voucher_n.details_doi.filter(d=>(!condition.ma_vt || condition.ma_vt ===d.ma_vt)
|
|
532
|
+
&& (d.pn_gia_tb && (!ma_kho || ma_kho === d.ma_kho))
|
|
533
|
+
);
|
|
534
|
+
await Promise.all(details_doi.map(d => {
|
|
535
|
+
return (async ()=>{
|
|
536
|
+
let gia = _.find(bang_gia, (vt) => {
|
|
537
|
+
return d.ma_vt === vt.ma_vt;
|
|
538
|
+
})
|
|
539
|
+
//gia chuan
|
|
540
|
+
let gia_chuan = gia? gia.gia: 0;
|
|
541
|
+
//tinh he so quy doi
|
|
542
|
+
let he_so_qd=1;
|
|
543
|
+
let qd = await dmqddvt.findOne({id_app:vouchers_n.id_app,ma_vt:d.ma_vt,ma_dvt:d.ma_dvt}).lean();
|
|
544
|
+
if(qd) he_so_qd = (qd.mau?(qd.tu/qd.mau): qd.ty_le_qd);
|
|
545
|
+
//update gia
|
|
546
|
+
d.gia_von_nt = gia_chuan * he_so_qd;
|
|
547
|
+
d.gia_von = d.gia_von_nt;
|
|
548
|
+
d.tien_xuat_nt = Math.roundBy(d.sl_xuat * d.gia_von, f_tien);
|
|
549
|
+
d.tien_xuat = d.tien_xuat_nt;
|
|
550
|
+
|
|
551
|
+
})();
|
|
552
|
+
}))
|
|
553
|
+
}
|
|
554
|
+
let voucher_n_data = voucher_n.toObject();
|
|
555
|
+
try{
|
|
556
|
+
//let _model = mongoose.models[voucher_n_data.ma_ct.toLowerCase()];
|
|
557
|
+
const ctrl = global.controllers[voucher_n_data.ma_ct.toUpperCase()];
|
|
558
|
+
let _model = ctrl?ctrl.getProperty("model"): mongoose.models[voucher_n_data.ma_ct.toLowerCase()];
|
|
559
|
+
|
|
560
|
+
await _model.updateOne({_id:voucher_n_data._id},voucher_n_data);
|
|
561
|
+
}catch(e){
|
|
562
|
+
return callback(e);
|
|
563
|
+
}
|
|
564
|
+
//post lai so sach
|
|
565
|
+
let ctrl = global.controllers[voucher_n_data.ma_ct.toUpperCase()];
|
|
566
|
+
if (ctrl && ctrl.post) {
|
|
567
|
+
await (async ()=>{
|
|
568
|
+
return new Promise((resolve,reject)=>{
|
|
569
|
+
//console.log("posted book",{ma_ct:vouchers_n.ma_ct,so_ct:vouchers_n.so_ct,_id:voucher_n_data._id});
|
|
570
|
+
Controller.postData(voucher_n_data,ctrl, function(e, rs) {
|
|
571
|
+
if(e){
|
|
572
|
+
console.error("❌ [tinhgiatb] error repost khi tinh gia trung binh",e.message,voucher_n_data.so_ct,voucher_n_data.ma_ct);
|
|
573
|
+
return reject(e);
|
|
574
|
+
}
|
|
575
|
+
resolve(rs);
|
|
576
|
+
},{kiem_tra_han_muc_cong_no:false});
|
|
577
|
+
})
|
|
578
|
+
})();
|
|
579
|
+
}
|
|
580
|
+
})().then(rs=>{
|
|
581
|
+
callback(null,rs);
|
|
582
|
+
}).catch(e=>{
|
|
583
|
+
callback(e);
|
|
584
|
+
})
|
|
585
|
+
})
|
|
586
|
+
}, function(error) {
|
|
587
|
+
//console.log("Ket thuc cap nhat gia vao phieu nhap",new Date().getTime()/1000)
|
|
588
|
+
callback(error);
|
|
589
|
+
});
|
|
590
|
+
}
|
|
591
|
+
], function(e) {
|
|
592
|
+
if (e) return fn(e);
|
|
593
|
+
//danh gia chenh lech
|
|
594
|
+
//console.log("Danh gia chenh lech",new Date().getTime()/1000)
|
|
595
|
+
let query_ckvt = {
|
|
596
|
+
id_app: condition.id_app,
|
|
597
|
+
ngay: den_ngay,
|
|
598
|
+
chenh_lech: 1
|
|
599
|
+
};
|
|
600
|
+
if (condition.ma_vt) {
|
|
601
|
+
query_ckvt.ma_vt = condition.ma_vt;
|
|
602
|
+
}
|
|
603
|
+
if (condition.ma_kho) {
|
|
604
|
+
query_ckvt.ma_kho = condition.ma_kho;
|
|
605
|
+
}
|
|
606
|
+
ckvt(query_ckvt, (e, rs)=> {
|
|
607
|
+
if (e) return fn(e);
|
|
608
|
+
//console.log(rs)
|
|
609
|
+
rs = _.filter(rs, (r)=> {
|
|
610
|
+
return r.du00 !== 0 && (r.ton00 == 0 || Math.abs(r.ton00) < 0.001);
|
|
611
|
+
});
|
|
612
|
+
vouchers_x = _.sortBy(_.values(vouchers_x), (v) => {
|
|
613
|
+
return -(new Date(v.ngay_ct)).getTime()
|
|
614
|
+
})
|
|
615
|
+
vouchers_n = _.sortBy(_.values(vouchers_n), (v) => {
|
|
616
|
+
return -(new Date(v.ngay_ct).getTime())
|
|
617
|
+
})
|
|
618
|
+
//console.log("vat tu chenh lech",rs);
|
|
619
|
+
let chung_tu_cap_nhat_chenh_lech ={};
|
|
620
|
+
async.map(rs, (vt, callback)=> {
|
|
621
|
+
setImmediate(()=>{
|
|
622
|
+
let d_voucher = null;
|
|
623
|
+
//xuat
|
|
624
|
+
d_voucher = _.find(vouchers_x, (x) => {
|
|
625
|
+
return _.find(x.details, (vc) => {
|
|
626
|
+
if (vc.ma_vt == vt.ma_vt && !vc.px_gia_dd && (!ma_kho || ma_kho === vc.ma_kho || ma_kho ===x.ma_kho)) {
|
|
627
|
+
vc.tien_xuat_nt = vc.tien_xuat_nt + vt.du00;
|
|
628
|
+
vc.tien_xuat = Math.roundBy(vc.tien_xuat_nt, f_tien);
|
|
629
|
+
|
|
630
|
+
if(vc.sl_xuat) vc.gia_von_nt = vc.gia_von = Math.roundBy(vc.tien_xuat_nt/vc.sl_xuat,0);
|
|
631
|
+
|
|
632
|
+
return true;
|
|
633
|
+
} else {
|
|
634
|
+
return false;
|
|
635
|
+
}
|
|
636
|
+
|
|
637
|
+
})
|
|
638
|
+
})
|
|
639
|
+
//nhap
|
|
640
|
+
if (!d_voucher) {
|
|
641
|
+
d_voucher = _.find(vouchers_n, (n) => {
|
|
642
|
+
return _.find(n.details, (vc) => {
|
|
643
|
+
if (vc.ma_vt == vt.ma_vt && !vc.pn_gia_tb && (!ma_kho || ma_kho === vc.ma_kho || ma_kho===n.ma_kho)) {
|
|
644
|
+
vc.tien_nhap_nt = vc.tien_nhap_nt - vt.du00;
|
|
645
|
+
vc.tien_nhap = Math.roundBy(vc.tien_nhap_nt, f_tien);
|
|
646
|
+
if(vc.sl_nhap) vc.gia_von_nt = vc.gia_von = Math.roundBy(vc.tien_nhap_nt/vc.sl_nhap,0);
|
|
647
|
+
return true;
|
|
648
|
+
} else {
|
|
649
|
+
return false;
|
|
650
|
+
}
|
|
651
|
+
|
|
652
|
+
})
|
|
653
|
+
})
|
|
654
|
+
}
|
|
655
|
+
if(d_voucher){
|
|
656
|
+
chung_tu_cap_nhat_chenh_lech[d_voucher._id.toString()] = d_voucher;
|
|
657
|
+
}
|
|
658
|
+
callback(null, d_voucher);
|
|
659
|
+
})
|
|
660
|
+
|
|
661
|
+
}, (e)=> {
|
|
662
|
+
if (e){
|
|
663
|
+
return fn(e);
|
|
664
|
+
}
|
|
665
|
+
//console.log("Cap nhat phieu chenh lech",new Date().getTime()/1000)
|
|
666
|
+
let _vouchers_chenh_lech = _.values(chung_tu_cap_nhat_chenh_lech);
|
|
667
|
+
//let _vouchers_chenh_lech = vouchers.filter(v=>v);
|
|
668
|
+
async.map(_vouchers_chenh_lech, (voucher, callback)=> {
|
|
669
|
+
setImmediate(async ()=>{
|
|
670
|
+
try{
|
|
671
|
+
let voucher_data = voucher.toObject();
|
|
672
|
+
//let _model = mongoose.models[voucher_data.ma_ct.toLowerCase()];
|
|
673
|
+
const ctrl = global.controllers[voucher_data.ma_ct.toUpperCase()];
|
|
674
|
+
let _model = ctrl?ctrl.getProperty("model"): mongoose.models[voucher_data.ma_ct.toLowerCase()];
|
|
675
|
+
await _model.updateOne({_id:voucher_data._id},voucher_data);
|
|
676
|
+
//post lai so sach
|
|
677
|
+
if (ctrl && ctrl.post) {
|
|
678
|
+
//console.log("repost voucher",voucher_data.ma_ct,voucher_data.so_ct,voucher_data._id,"...")
|
|
679
|
+
Controller.postData(voucher_data,ctrl, (e, rs)=> {
|
|
680
|
+
if(e) console.error("❌ [tinhgiatb] error repost khi tinh gia trung binh",e.message,voucher_data.so_ct,voucher_data.ma_ct);
|
|
681
|
+
callback(e, rs);
|
|
682
|
+
},{kiem_tra_han_muc_cong_no:false});
|
|
683
|
+
} else {
|
|
684
|
+
callback(null);
|
|
685
|
+
}
|
|
686
|
+
}catch(error){
|
|
687
|
+
return callback(error);
|
|
688
|
+
}
|
|
689
|
+
})
|
|
690
|
+
}, async (e)=> {
|
|
691
|
+
if (e) return fn(e);
|
|
692
|
+
bang_gia.joinModel2(id_app, dmvt, [
|
|
693
|
+
{
|
|
694
|
+
where:'ma_vt',
|
|
695
|
+
fields: 'ten_vt'
|
|
696
|
+
}
|
|
697
|
+
], ()=> {
|
|
698
|
+
fn(null, bang_gia);
|
|
699
|
+
});
|
|
700
|
+
});
|
|
701
|
+
|
|
702
|
+
});
|
|
703
|
+
});
|
|
704
|
+
});
|
|
705
|
+
|
|
706
|
+
});
|
|
707
|
+
});
|
|
708
|
+
});
|
|
709
|
+
}).catch(e=>{
|
|
710
|
+
fn(e);
|
|
711
|
+
})
|
|
712
|
+
|
|
713
|
+
};
|