UNPKG

kpiplus

Version:

KPI+

1 lines 5.74 kB
var dkvt=require("./dkvt"),ckvt=require("./ckvt"),sokho=require("../models/sokho"),dmvt=require("../models/dmvt"),giatb=require("../models/giatb"),tinhgiatb1vt=require("./tinhgiatb1vt"),arrayfuncs=require("./array-funcs"),async=require("async"),underscore=require("underscore"),moment=require("moment");module.exports=function(n,t){if(!(n&&n.tu_thang&&n.den_thang&&n.nam&&n.id_app))return void t("Lỗi: Tính năng này yêu cầu các tham số: tu_thang,den_thang,nam,id_app");n.tu_thang=Number(n.tu_thang),n.den_thang=Number(n.den_thang);let a=n.ma_kho,e=moment(new Date(n.nam,n.tu_thang-1,1)).startOf("date").toDate(),i=moment(new Date(n.nam,n.den_thang,0)).endOf("date").toDate(),o=n.id_app,_={id_app:o,gia_xuat:"1"};n.ma_vt&&(_.ma_vt=n.ma_vt),dmvt.find(_).lean().exec(function(_,u){async.map(u,function(n,t){let _={id_app:o,tu_ngay:e,den_ngay:i,ma_vt:n.ma_vt,ma_kho:a};tinhgiatb1vt(_,function(n,e){if(n)return t(n);e.id_app=o,e.ma_kho=a,e.status=!0,t(null,e)})},function(_,u){if(_)return t(_);u=underscore.filter(u,function(n){return 0!==n.gia||0!==n.tong_sl||0!==n.tong_tien});let r=[];for(let t=n.tu_thang;t<=n.den_thang;t++)r.push(t);async.map(r,function(t,e){async.map(u,function(e,i){e.thang=t,e.nam=n.nam,async.parallel({savetogiatb:function(n){let t={id_app:o,ma_vt:e.ma_vt};a&&(t.ma_kho=a),giatb.remove(t,function(t){if(t)return n(t);giatb.create(e,function(t,a){if(t)return console.log("Loi khi luu vao bang gia trung binh thang vat tu "+e.ma_vt),console.log(t),n(t);n(null,a)})})}},function(n,t){if(n)return i(n);i(null)})},function(n){if(n)return e(n);e(null)})},function(n){});let c={},l={},d=[];u.forEach(function(n){d.push(n.ma_vt)}),async.parallel({xuat:function(n){let t={id_app:o,ngay_ct:{$gte:e,$lte:i},nxt:2,ma_vt:{$in:d},px_gia_dd:!1};a&&(t.ma_kho=a),sokho.find(t).lean().exec(function(t,a){return t?n(t):a&&0!==a.length?void async.map(a,function(n,t){let a=c[n.id_ct];if(a)t();else{require("../models/"+n.ma_ct.toLowerCase()).findOne({_id:n.id_ct},function(e,i){if(e)return t(e);i?(a=i,c[n.id_ct]=a):console.log("Not found "+n.id_ct),t()})}},function(t){if(t)return console.log("co loi lay phieu xuat"),n(t);n(null,c)}):n(null,c)})},nhap:function(n){let t={id_app:o,ngay_ct:{$gte:e,$lte:i},nxt:1,ma_vt:{$in:d},pn_gia_tb:!0,ma_ct:{$ne:"PXC"}};a&&(t.ma_kho=a),sokho.find(t,function(t,a){return t?n(t):a&&0!==a.length?void async.map(a,function(n,t){let a=l[n.id_ct];if(a)t();else{require("../models/"+n.ma_ct.toLowerCase()).findOne({_id:n.id_ct},function(e,i){if(e)return t(e);i?(a=i,l[n.id_ct]=a):console.log("Not found "+n.id_ct),t()})}},function(t){if(t)return console.log("co loi lay phieu nhap"),n(t);n(null,l)}):n(null,l)})}},function(e,_){if(e)return t(e);async.parallel({update_phieu_xuat:function(n){async.map(underscore.values(c),(n,t)=>{n.details.forEach(t=>{let e=underscore.find(u,n=>!(t.ma_vt!=n.ma_vt||t.px_gia_dd||a&&a!=t.ma_kho));e&&(t.gia_von_nt=e?e.gia:0,t.gia_von=t.gia_von_nt,t.tien_xuat_nt=Math.round(t.sl_xuat*t.gia_von,0),t.tien_xuat=t.tien_xuat_nt),t.promotion&&t.promotion.details_km&&t.promotion.details_km.forEach(t=>{let e=underscore.find(u,e=>!(t.ma_vt!=e.ma_vt||t.px_gia_dd||a&&a!=(t.ma_kho||n.ma_kho)));e&&(t.gia_von_nt=e?e.gia:0,t.gia_von=t.gia_von_nt,t.tien_xuat_nt=Math.round(t.sl_xuat*t.gia_von,0),t.tien_xuat=t.tien_xuat_nt)})}),n.promotion&&n.promotion.forEach(t=>{let e=underscore.find(u,e=>!(t.ma_vt!=e.ma_vt||t.px_gia_dd||a&&a!=(t.ma_kho||n.ma_kho)));e&&(t.gia_von_nt=e?e.gia:0,t.gia_von=t.gia_von_nt,t.tien_xuat_nt=Math.round(t.sl_xuat*t.gia_von,0),t.tien_xuat=t.tien_xuat_nt)}),n.save(a=>{if(a)return t(a);let e=ctrlVouchers[n.ma_ct.toUpperCase()];e&&e.contr.post?e.contr.post(n,(n,a)=>{t(n,a)}):t(null)})},t=>{n(t)})},update_phieu_nhap:function(n){async.map(underscore.values(l),function(n,t){n.details.forEach(function(n){let t=underscore.find(u,t=>n.ma_vt==t.ma_vt&&n.pn_gia_tb&&(!a||a==n.ma_kho));t&&(n.gia_von_nt=t?t.gia:0,n.gia_von=n.gia_von_nt,n.tien_nhap_nt=Math.round(n.sl_nhap*n.gia_von,0),n.tien_nhap=n.tien_nhap_nt)}),n.details_doi&&n.details_doi.forEach(function(n){let t=underscore.find(u,t=>n.ma_vt==t.ma_vt&&n.pn_gia_tb&&(!a||a==n.ma_kho));t&&(n.gia_von_nt=t?t.gia:0,n.gia_von=n.gia_von_nt,n.tien_xuat_nt=Math.round(n.sl_xuat*n.gia_von,0),n.tien_xuat=n.tien_xuat_nt)}),n.save(function(a){if(a)return t(a);let e=ctrlVouchers[n.ma_ct.toUpperCase()];e&&e.contr.post?e.contr.post(n,function(n,a){t(n,a)}):t(null)})},function(t){n(t)})}},function(e,_){if(e)return t(e);let r={id_app:n.id_app,ngay:i,chenh_lech:1};n.ma_vt&&(r.ma_vt=n.ma_vt),n.ma_kho&&(r.ma_kho=n.ma_kho),ckvt(r,function(n,e){if(n)return t(n);e=underscore.filter(e,function(n){return 0!==n.du00&&(0==n.ton00||Math.abs(n.ton00)<.001)}),c=underscore.sortBy(underscore.values(c),n=>-new Date(n.ngay_ct).getTime()),l=underscore.sortBy(underscore.values(l),n=>-new Date(n.ngay_ct).getTime()),async.map(e,function(n,t){let e=null;(e=underscore.find(c,t=>underscore.find(t.details,t=>!(t.ma_vt!=n.ma_vt||t.px_gia_dd||a&&a!=t.ma_kho)&&(t.tien_xuat_nt=t.tien_xuat_nt+n.du00,t.tien_xuat=Math.round(t.tien_xuat_nt,0),!0))))||(e=underscore.find(l,t=>underscore.find(t.details,t=>!(t.ma_vt!=n.ma_vt||t.pn_gia_tb||a&&a!=t.ma_kho)&&(t.tien_nhap_nt=t.tien_nhap_nt-n.du00,t.tien_nhap=Math.round(t.tien_nhap_nt,0),!0)))),t(null,e)},function(n,a){if(n)return t(n);a?async.map(a,function(n,t){n?n.save(function(a){if(a)return t(a);let e=ctrlVouchers[n.ma_ct.toUpperCase()];e&&e.contr.post?e.contr.post(n,function(n,a){t(n,a)}):t(null)}):t(null)},function(n,a){if(n)return t(n);u.joinModel(o,dmvt,[{akey:"ma_vt",bkey:"ma_vt",fields:[{name:"ten_vt",value:"ten_vt"}]}],function(n){t(null,u)})}):u.joinModel(o,dmvt,[{akey:"ma_vt",bkey:"ma_vt",fields:[{name:"ten_vt",value:"ten_vt"}]}],function(n){t(null,u)})})})})})})})};