kpiplus
Version:
KPI+
1 lines • 4.49 kB
JavaScript
var socai=require("../../models/socai"),arrayfuncs=require("../../libs/array-funcs"),kbmpttct=require("../../models/kbm_pttct"),rptform=require("../../models/rptform"),vatvao=require("../../models/vatvao"),vatra=require("../../models/vatra"),sokho=require("../../models/sokho"),dmvt=require("../../models/dmvt"),async=require("async"),underscore=require("underscore"),stp=require("stp-utils"),utils=require("../../libs/utils"),moment=require("moment"),controller=require("../../controllers/controllerRPT"),getRegString=function(t){return t.reg()};module.exports=function(t){new controller(t,"ptcttct",function(t,n){let e=t.query;if(!e.den_ngay||!e.tu_ngay)return n("Báo cáo này yêu cầu tham số từ ngày(tu_ngay), đến ngày(den_ngay)");p=new Promise((t,n)=>{if(e.id_rptform)return t(e.id_rptform);rptform.findOne({id_app:e.id_app,rptform_type:"PTCTTCT"}).lean().exec((e,_)=>{if(e||!_)return n(e);t(_._id.toString())})}).then(t=>{e.tu_ngay=moment(new Date(e.tu_ngay)).startOf("date").toDate(),e.den_ngay=moment(new Date(e.den_ngay)).endOf("date").toDate();let _;kbmpttct.find({id_app:e.id_app,id_rptform:t}).lean().exec(function(t,a){if(t)return n(t);_=a,async.map(_,function(t,n){"9"===t.cach_tinh?(t.cong_thuc="",async.map(ds_obj,function(n,e){utils.getInfoFromQuery(n,t.query,(n,_)=>{if(n)return e();underscore.isNumber(_)?(t.so_kn=_,t.so_kn_nt=_):t.value_string=_,e()})},(e,_)=>{n(e,t)})):"2"===t.cach_tinh?(t.cong_thuc="",async.parallel({ky_nay:function(n){let _={ngay_ct:{$gte:e.tu_ngay,$lte:e.den_ngay},id_app:e.id_app};e.ma_dvcs&&(_.ma_dvcs={$regex:"^"+e.ma_dvcs,$options:"i"}),e.ma_bp&&(_.ma_bp={$regex:"^"+e.ma_bp,$options:"i"}),e.ma_hd&&(_.ma_hd={$regex:"^"+e.ma_hd,$options:"i"}),e.ma_dt&&(_.ma_dt={$regex:"^"+e.ma_dt,$options:"i"}),e.ma_sp&&(_.ma_sp={$regex:"^"+e.ma_sp,$options:"i"}),e.ma_nv&&(_.ma_nv={$regex:"^"+e.ma_nv,$options:"i"}),t.ma_phi&&(_.ma_phi={$regex:"^"+t.ma_phi,$options:"i"}),t.ma_kho&&(_.ma_kho={$regex:"^"+t.ma_kho,$options:"i"});let a=getRegString(t.tk_no);a&&(t.giam_tru_no?_.tk_no={$not:new RegExp(a)}:_.tk_no={$regex:a,$options:"i"});let o=getRegString(t.tk_co);if(o&&(t.giam_tru_co&&o?_.tk_co={$not:new RegExp(o)}:_.tk_co={$regex:o,$options:"i"}),t.dieu_kien_khac)try{"{"!==t.dieu_kien_khac.substring(0,1)&&(t.dieu_kien_khac="{"+t.dieu_kien_khac+"}");let e=JSON.parse(t.dieu_kien_khac);underscore(_,e)}catch(t){n(t.toString())}socai.find(_,function(e,_){if(e)return n(e);t.so_kn=_.csum("tien"),t.so_kn_nt=_.csum("tien_nt"),t.so_ct=_.cmax("so_ct"),t.ngay_ct=_.cmax("ngay_ct"),t.dien_giai=_.cmax("dien_giai");let a=underscore.pluck(_,"id_ct");async.parallel({vatra:t=>{vatra.find({id_ct:{$in:a}}).lean().exec((n,e)=>{if(n)return t(n);t(null,e)})},vatvao:t=>{vatvao.find({id_ct:{$in:a}}).lean().exec((n,e)=>{if(n)return t(n);t(null,e)})},sokho:t=>{sokho.find({id_ct:{$in:a}}).lean().exec((n,e)=>{if(n)return t(n);t(null,e)})},socai:t=>{_.groupBy(["id_ct"],["tien","tien_nt"],(n,e)=>{t(n,e)},{fieldmaxs:["so_ct","ngay_ct","dien_giai","ma_ct"]})}},(e,_)=>{t.vatra=_.vatra.csum("t_thue"),t.vatvao=_.vatvao.csum("t_thue"),t.vatra_nt=_.vatra.csum("t_thue_nt"),t.vatvao_nt=_.vatvao.csum("t_thue_nt"),t.thue=t.vatra+t.vatvao,t.thue_nt=t.vatra_nt+t.vatvao_nt,t.sl_nhap=_.sokho.csum("sl_nhap"),t.sl_xuat=_.sokho.csum("sl_xuat"),t.so_luong=t.sl_nhap+t.sl_xuat,t.t_tt=t.thue+t.so_kn,t.t_tt_nt=t.thue_nt+t.so_kn_nt,t.sokho=_.sokho,t.ct_socai&&(t.socai=_.socai,t.socai.forEach(t=>{t.thue=_.vatra.csum("t_thue",{id_ct:t.id_ct})+_.vatvao.csum("t_thue",{id_ct:t.id_ct}),t.thue_nt=_.vatra.csum("t_thue_nt",{id_ct:t.id_ct})+_.vatvao.csum("t_thue_nt",{id_ct:t.id_ct}),t.so_luong=_.sokho.csum("sl_nhap",{id_ct:t.id_ct})+_.sokho.csum("sl_xuat",{id_ct:t.id_ct}),t.gia=0!==t.so_luong?Math.round(t.tien/t.so_luong,0):0,t.gia_nt=0!==t.so_luong?Math.round(t.tien_nt/t.so_luong,2):0,t.t_tt=t.thue+t.tien,t.t_tt_nt=t.thue_nt+t.tien_nt})),n(e,t)})})}},function(t,e){n(t,e)})):n(null,t)},function(t,e){if(t)return n(t);let a=function(t,n){n.chi_tinh_cac_cot&&n.chi_tinh_cac_cot.length>0&&("so_kn,so_kn_nt,vatra,vatvao,vatra_nt,vatvao_nt,thue,thue_nt,sl_nhap,sl_xuat,so_luong,t_tt,t_tt_nt".split(",").forEach(t=>{n.chi_tinh_cac_cot.indexOf(t)<0&&(n[t]=0)}),n.chi_tinh_cac_cot.indexOf("t_tt")<0&&(n.t_tt=n.thue+n.so_kn))},o={so_kn:a,so_kn_nt:a,vatra:a,vatvao:a,vatra_nt:a,vatvao_nt:a,thue:a,thue_nt:a,sl_nhap:a,sl_xuat:a,so_luong:a,t_tt:a,t_tt_nt:a};stp.calcGrid(_,o,function(t){n(null,t)})})})}).catch(t=>{n(t||"Không có mẫu báo cáo nào cho báo cáo này")})})};