flexbiz-server 12.4.6 → 12.4.7

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
@@ -2,7 +2,7 @@
2
2
  "name": "flexbiz-server",
3
3
  "main": "./server/app.js",
4
4
  "description": "Flexible Server",
5
- "version": "12.4.6",
5
+ "version": "12.4.7",
6
6
  "author": {
7
7
  "name": "Van Truong Pham",
8
8
  "email": "invncur@gmail.com"
@@ -1,6 +1,79 @@
1
- const dkvt=require("./dkvt"),sokho=global.getModel("sokho"),async=require("async"),{getCurrentSession}=require("./sessionContext");
2
- module.exports=function($condition$$,$fn$$){if(!($condition$$&&$condition$$.ma_vt&&$condition$$.tu_ngay&&$condition$$.den_ngay&&$condition$$.id_app))return $fn$$("L\u1ed7i: B\u00e1o c\u00e1o n\u00e0y y\u00eau c\u1ea7u c\u00e1c tham s\u1ed1: tu_ngay,den_ngay,id_app,ma_vt");let $ma_vt$$=$condition$$.ma_vt,$tu_ngay$$=$condition$$.tu_ngay,$den_ngay$$=$condition$$.den_ngay,$id_app$$=$condition$$.id_app;async.parallel({dn:function($callback$$){let $query$$={ngay:$tu_ngay$$,id_app:$id_app$$};$condition$$.ma_kho&&
3
- ($query$$.ma_kho=$condition$$.ma_kho);$query$$.ma_vt=$condition$$.ma_vt;dkvt($query$$,function($error$$,$du_dau_result$$){if($error$$)return $callback$$($error$$);console.log(`\u2705 [tinhgiatb] t\u00ednh gi\u00e1 trung b\u00ecnh cho v\u1eadt t\u01b0=${$condition$$.ma_vt}, t\u00ednh \u0111\u1ea7u k\u1ef3, sessionID=${getCurrentSession()?._debugId}`);$error$$=$du_dau_result$$.csum("ton00");$du_dau_result$$=$du_dau_result$$.csum("du00");$callback$$(null,{ton_dau:$error$$,du_dau:$du_dau_result$$})})},
4
- ps:function($callback$$){let $query$$={id_app:$id_app$$,ngay_ct:{$gte:$tu_ngay$$,$lt:$den_ngay$$},ma_vt:$ma_vt$$,$or:[{nxt:1,pn_gia_tb:!1},{nxt:2,px_gia_dd:!0}]};$condition$$.ma_kho&&($query$$.ma_kho=$condition$$.ma_kho,$query$$.$or.push({nxt:1,pn_gia_tb:!0,ma_ct:"PNC"}));sokho.find($query$$).lean().then(function($pss_tien_nhap$$){$pss_tien_nhap$$=$pss_tien_nhap$$.map($ps$$=>{$ps$$.sl_nhap=($ps$$.sl_nhap_qd||0)-($ps$$.sl_xuat_qd||0);return $ps$$});let $sl_nhap$$=$pss_tien_nhap$$.csum("sl_nhap");$pss_tien_nhap$$=
5
- $pss_tien_nhap$$.csum("tien_nhap")-$pss_tien_nhap$$.csum("tien_xuat");$callback$$(null,{sl_nhap:$sl_nhap$$,tien_nhap:$pss_tien_nhap$$})}).catch($e$$=>{$callback$$($e$$)})}},function($error$$,$results$$){if($error$$)$fn$$($error$$);else{$error$$=$results$$.ps.sl_nhap+$results$$.dn.ton_dau;var $tong_tien$$=$results$$.ps.tien_nhap+$results$$.dn.du_dau,$gia$$=$error$$?Math.roundBy($tong_tien$$/$error$$,0):0;return $fn$$(null,{ma_vt:$ma_vt$$,ton_dau:$results$$.dn.ton_dau,du_dau:$results$$.dn.du_dau,sl_nhap:$results$$.ps.sl_nhap,
6
- tien_nhap:$results$$.ps.tien_nhap,tong_sl:$error$$,tong_tien:$tong_tien$$,gia:$gia$$})}})};
1
+ const dkvt = require('./dkvt');
2
+ const sokho = global.getModel('sokho');
3
+ const async = require('async');
4
+ //const {getCurrentSession} = require("./sessionContext")
5
+ module.exports = function(condition, fn) {
6
+ //kiem tra dieu kien
7
+ if (!condition || !condition.ma_vt || !condition.tu_ngay || !condition.den_ngay || !condition.id_app) {
8
+ return fn('Lỗi: Báo cáo này yêu cầu các tham số: tu_ngay,den_ngay,id_app,ma_vt');
9
+ }
10
+ //console.log(`✅ [tinhgiatb] tính giá trung bình cho vật tư=${condition.ma_vt}, sessionID=${getCurrentSession()?._debugId}`);
11
+ //lay dieu kien
12
+ /////kho
13
+ ////vat tu
14
+ let ma_vt = condition.ma_vt;
15
+ /////dieu kien khac
16
+ let tu_ngay = condition.tu_ngay;
17
+ let den_ngay = condition.den_ngay;
18
+ let id_app = condition.id_app;
19
+ async.parallel({
20
+ //dau ky
21
+ dn: function(callback) {
22
+ let query = {ngay: tu_ngay, id_app: id_app};
23
+ if (condition.ma_kho) {
24
+ query.ma_kho = condition.ma_kho;
25
+ }
26
+ query.ma_vt = condition.ma_vt;
27
+ dkvt(query, function(error, result) {
28
+ if (error) return callback(error);
29
+ //console.log(`✅ [tinhgiatb] tính giá trung bình cho vật tư=${condition.ma_vt}, tính đầu kỳ, sessionID=${getCurrentSession()?._debugId}`);
30
+ //console.log("tinh gia trung binh","tinh dau ky",query,result)
31
+ let ton_dau = result.csum('ton00');
32
+ let du_dau = result.csum('du00');
33
+ callback(null, {ton_dau: ton_dau, du_dau: du_dau});
34
+ });
35
+ },
36
+ //phat sinh trong ky
37
+ ps: function(callback) {
38
+ let query = {
39
+ id_app: id_app,
40
+ ngay_ct: {$gte: tu_ngay, $lt: den_ngay},
41
+ ma_vt: ma_vt,
42
+ $or:[
43
+ {nxt:1,pn_gia_tb:false},
44
+ {nxt:2,px_gia_dd:true}
45
+ ]
46
+ };
47
+
48
+ if (condition.ma_kho) {
49
+ query.ma_kho = condition.ma_kho;
50
+ //tinh riêng từng kho thì cần lấy cả các phiếu nhập điều chuyển giá trung bình
51
+ query.$or.push({nxt:1,pn_gia_tb:true,ma_ct:"PNC"});
52
+ }
53
+ //console.log(`✅ [tinhgiatb] tính giá trung bình cho vật tư=${condition.ma_vt}, tính phát sinh, sessionID=${getCurrentSession()?._debugId}`);
54
+ sokho.find(query).lean().then(function(pss) {
55
+ //lay theo so luong quy doi
56
+ pss = pss.map(ps=>{
57
+ ps.sl_nhap = (ps.sl_nhap_qd||0) - (ps.sl_xuat_qd||0);
58
+ return ps;
59
+ })
60
+ //console.log("tinh gia trung binh","tinh phat sinh",query,pss)
61
+ let sl_nhap = pss.csum('sl_nhap');
62
+ let tien_nhap = pss.csum('tien_nhap') - pss.csum('tien_xuat');
63
+ callback(null, {sl_nhap: sl_nhap, tien_nhap: tien_nhap});
64
+ }).catch(e=>{
65
+ callback(e);
66
+ })
67
+ }
68
+ },function(error, results) {
69
+ if (error) {
70
+ fn(error);
71
+ return;
72
+ }
73
+ let tong_sl = results.ps.sl_nhap + results.dn.ton_dau;
74
+ let tong_tien = results.ps.tien_nhap + results.dn.du_dau;
75
+ let gia = tong_sl?Math.roundBy(tong_tien / tong_sl, 0):0;
76
+ return fn(null, {ma_vt: ma_vt, ton_dau: results.dn.ton_dau, du_dau: results.dn.du_dau, sl_nhap: results.ps.sl_nhap, tien_nhap: results.ps.tien_nhap, tong_sl: tong_sl, tong_tien: tong_tien, gia: gia});
77
+ }
78
+ );
79
+ };