flexbiz-server
Version:
Flexible Server
30 lines (29 loc) • 14.1 kB
JavaScript
const databanle=require("./databanle"),async=require("async"),moment=require("moment");
module.exports=async function($condition$$,$callback$$){let $field_group_by$$=$condition$$.group_by||"ngay_ct",$group_by$$;$group_by$$=$field_group_by$$!="ngay_ct"&&$field_group_by$$!="nam"&&$field_group_by$$!="ma_kho"&&$field_group_by$$!="ma_kh"&&$field_group_by$$!="ma_nv"?["nam",$field_group_by$$]:[$field_group_by$$];if(!$condition$$.tu_ngay||!$condition$$.den_ngay)return $callback$$("B\u00e1o c\u00e1o n\u00e0y y\u00eau c\u1ea7u c\u00e1c tham s\u1ed1:tu_ngay,den_ngay");const $query$$={id_app:$condition$$.id_app};
$query$$.ngay_ct={$gte:$condition$$.tu_ngay,$lte:$condition$$.den_ngay};$condition$$.ma_dvcs&&($query$$.ma_dvcs=$condition$$.ma_dvcs);$condition$$.ma_kho&&($query$$.$or=[{ma_kho:$condition$$.ma_kho},{"details.ma_kho":$condition$$.ma_kho}]);if($condition$$.ma_kh)$query$$.ma_kh=$condition$$.ma_kh;else{var $condition_kh_custs$$={};if($condition$$.nh_kh){var $nh_khs_nh_khs2_nh_khs3_nh_khs4_nh_khs5$$=await global.getModel("group").getAllChildrenGroup($condition$$.id_app,$condition$$.nh_kh,{status:!0});
$condition_kh_custs$$.nh_kh={$in:$nh_khs_nh_khs2_nh_khs3_nh_khs4_nh_khs5$$}}$condition$$.nh_kh2&&($nh_khs_nh_khs2_nh_khs3_nh_khs4_nh_khs5$$=await global.getModel("group").getAllChildrenGroup($condition$$.id_app,$condition$$.nh_kh2,{status:!0}),$condition_kh_custs$$.nh_kh2={$in:$nh_khs_nh_khs2_nh_khs3_nh_khs4_nh_khs5$$});$condition$$.nh_kh3&&($nh_khs_nh_khs2_nh_khs3_nh_khs4_nh_khs5$$=await global.getModel("group").getAllChildrenGroup($condition$$.id_app,$condition$$.nh_kh3,{status:!0}),$condition_kh_custs$$.nh_kh3=
{$in:$nh_khs_nh_khs2_nh_khs3_nh_khs4_nh_khs5$$});$condition$$.nh_kh4&&($nh_khs_nh_khs2_nh_khs3_nh_khs4_nh_khs5$$=await global.getModel("group").getAllChildrenGroup($condition$$.id_app,$condition$$.nh_kh4,{status:!0}),$condition_kh_custs$$.nh_kh4={$in:$nh_khs_nh_khs2_nh_khs3_nh_khs4_nh_khs5$$});$condition$$.nh_kh5&&($nh_khs_nh_khs2_nh_khs3_nh_khs4_nh_khs5$$=await global.getModel("group").getAllChildrenGroup($condition$$.id_app,$condition$$.nh_kh5,{status:!0}),$condition_kh_custs$$.nh_kh5={$in:$nh_khs_nh_khs2_nh_khs3_nh_khs4_nh_khs5$$});
Object.keys($condition_kh_custs$$).length>0&&($condition_kh_custs$$.id_app=$condition$$.id_app,$condition_kh_custs$$=await global.getModel("customer").find($condition_kh_custs$$,{ma_kh:1}).lean(),$query$$.ma_kh={$in:$condition_kh_custs$$.map($c$$=>$c$$.ma_kh)})}$condition$$.ma_ct&&($query$$.ma_ct=$condition$$.ma_ct);$condition$$.user_hoa_hong&&($query$$.$or?$query$$.$and=[{$or:[{user_nv:$condition$$.user_hoa_hong},{user_ctv:$condition$$.user_hoa_hong},{user_ctv2:$condition$$.user_hoa_hong},{user_kho:$condition$$.user_hoa_hong},
{user_gt:$condition$$.user_hoa_hong},{user_ql:$condition$$.user_hoa_hong},{"details.user_nv":$condition$$.user_hoa_hong},{"details.user_ctv":$condition$$.user_hoa_hong},{"details.user_ctv2":$condition$$.user_hoa_hong},{"details.user_kho":$condition$$.user_hoa_hong},{"details.user_gt":$condition$$.user_hoa_hong},{"details.user_ql":$condition$$.user_hoa_hong}]}]:$query$$.$or=[{user_nv:$condition$$.user_hoa_hong},{user_ctv:$condition$$.user_hoa_hong},{user_ctv2:$condition$$.user_hoa_hong},{user_kho:$condition$$.user_hoa_hong},
{user_gt:$condition$$.user_hoa_hong},{user_ql:$condition$$.user_hoa_hong},{"details.user_nv":$condition$$.user_hoa_hong},{"details.user_ctv":$condition$$.user_hoa_hong},{"details.user_ctv2":$condition$$.user_hoa_hong},{"details.user_kho":$condition$$.user_hoa_hong},{"details.user_gt":$condition$$.user_hoa_hong},{"details.user_ql":$condition$$.user_hoa_hong}]);databanle($query$$,async function($error$$,$rs$$,$ptthanhtoans$$){if($error$$)return $callback$$($error$$);const $thanh_toan$$=($ptthanhtoans$$||
[]).map($pt$$=>({name:$pt$$.ma,value:$pt$$.ma,ma:$pt$$.ma,ten:$pt$$.ten}));$rs$$.forEach(function($r$$){$r$$.sl_dh=1;$r$$.thang=$r$$.ngay_ct.getMonth()+1;$r$$.nam=moment($r$$.ngay_ct).year();$r$$.quy=moment($r$$.ngay_ct).quarter()});async.mapLimit($rs$$,20,function($r$$,$callback$$){$r$$.t_sl_xuat=$r$$.details.csum("sl_xuat");$r$$.t_tien_hang=$r$$.details.csum("tien_hang");$r$$.t_tien_hang_ct=$r$$.details.csum("tien_hang_ct");$r$$.t_thue=$r$$.details.csum("tien_thue");$r$$.t_tien_ck=$r$$.details.csum("tien_ck");
$r$$.t_tien=$r$$.details.csum("tien");$r$$.t_tien_cp=$r$$.details.csum("tien_cp");$r$$.tien_ck_hd=$r$$.tien_ck_hd||0;$r$$.t_tien_xuat=$r$$.details.csum("tien_xuat");$r$$.t_tien_hoa_hong=$r$$.details.csum("tien_hoa_hong");$r$$.t_tien_hoa_hong_gt=$r$$.details.csum("tien_hoa_hong_gt");$r$$.t_tien_hoa_hong_ctv=$r$$.details.csum("tien_hoa_hong_ctv");$r$$.t_tien_hoa_hong_ql=$r$$.details.csum("tien_hoa_hong_ql");$r$$.tien_freeship=$r$$.tien_freeship||0;$r$$.tien_phi_vc=$r$$.tien_phi_vc||0;$r$$.details_tl?
($r$$.t_tien_tl=$r$$.details_tl.csum("tien_hang_ct")-$r$$.details_tl.csum("tien_ck"),$r$$.t_tien_thue_tl=$r$$.details_tl.csum("tien_thue"),$r$$.t_tien_nhap=$r$$.details_tl.csum("tien_nhap"),$r$$.t_sl_nhap=$r$$.details_tl.csum("sl_nhap"),$r$$.t_tien_hoa_hong-=$r$$.details_tl.csum("tien_hoa_hong"),$r$$.t_tien_hoa_hong_gt-=$r$$.details_tl.csum("tien_hoa_hong_gt"),$r$$.t_tien_hoa_hong_ctv-=$r$$.details_tl.csum("tien_hoa_hong_ctv"),$r$$.t_tien_hoa_hong_ql-=$r$$.details_tl.csum("tien_hoa_hong_ql")):($r$$.t_tien_tl=
0,$r$$.t_sl_nhap=0,$r$$.t_tien_nhap=0);setImmediate(async()=>{try{if($r$$._id){const $no$$=await global.getModel("tdttno").findOne({id_ct:$r$$._id,id_app:$condition$$.id_app}).lean();if($no$$){$r$$.t_tien_no_nt=$no$$.tien_nt;$r$$.t_tien_no=$no$$.tien;const $da_thus$$=await global.getModel("tdttno_tt").find({id_hd:$r$$._id,id_app:$condition$$.id_app}).lean();$r$$.t_tien_da_thu=$da_thus$$.map($d$$=>$d$$.tien).reduce(($a$$,$b$$)=>$a$$+$b$$,0);$r$$.t_tien_da_thu_nt=$da_thus$$.map($d$$=>$d$$.tien_nt).reduce(($a$$,
$b$$)=>$a$$+$b$$,0)}$r$$.t_tien_con_no=$r$$.t_tien_no-$r$$.t_tien_da_thu;$r$$.t_tien_con_no_nt=$r$$.t_tien_no_nt-$r$$.t_tien_da_thu_nt}$callback$$(null,$r$$)}catch($e$$){$callback$$($e$$)}})},async function($error$$,$rs$$){if($error$$)return $callback$$($error$$);$error$$=[{name:"sl_dh",value:"sl_dh"},{name:"t_sl_xuat",value:"t_sl_xuat"},{name:"t_sl_nhap",value:"t_sl_nhap"},{name:"t_tien_hang",value:"t_tien_hang"},{name:"t_tien_hang_ct",value:"t_tien_hang_ct"},{name:"t_thue",value:"t_thue"},{name:"t_tien_cp",
value:"t_tien_cp"},{name:"t_tien_ck",value:"t_tien_ck"},{name:"t_tien",value:"t_tien"},{name:"tien_ck_hd",value:"tien_ck_hd"},{name:"tien_freeship",value:"tien_freeship"},{name:"tien_phi_vc",value:"tien_phi_vc"},{name:"tien_evoucher",value:"tien_evoucher"},{name:"tien_thu",value:"tien_thu"},{name:"tien_thu2",value:"tien_thu2"},{name:"tien_paymentByPoints",value:"tien_paymentByPoints"},{name:"t_tien_xuat",value:"t_tien_xuat"},{name:"t_tien_tl",value:"t_tien_tl"},{name:"t_tien_thue_tl",value:"t_tien_thue_tl"},
{name:"t_tien_nhap",value:"t_tien_nhap"},{name:"phai_tra",value:"phai_tra"},{name:"t_tien_hoa_hong",value:"t_tien_hoa_hong"},{name:"t_tien_hoa_hong_gt",value:"t_tien_hoa_hong_gt"},{name:"t_tien_hoa_hong_ctv",value:"t_tien_hoa_hong_ctv"},{name:"t_tien_hoa_hong_ql",value:"t_tien_hoa_hong_ql"},{name:"t_tien_da_thu",value:"t_tien_da_thu"},{name:"t_tien_con_no",value:"t_tien_con_no"},...$thanh_toan$$];let $data$$=await $rs$$.asyncGroupBy([...(new Set([...$group_by$$,"ma_kh"]))],$error$$);await $data$$.filter($r$$=>
$r$$.ma_kh).asyncJoinModel2($condition$$.id_app,"customer",{where:"ma_kh",fields:["ngay_mua_dau_tien",{cust_date_created:"date_created"}]});$data$$.forEach($r$$=>{$r$$.t_kh=1;if($r$$.cust_date_created){let $tu_ngay$$;switch($field_group_by$$){case "nam":var $date$jscomp$1_date$jscomp$2_date$jscomp$3_date$$=moment(new Date(Number($r$$.nam),1,1));$tu_ngay$$=$date$jscomp$1_date$jscomp$2_date$jscomp$3_date$$.startOf("year").toDate().getTime();$date$jscomp$1_date$jscomp$2_date$jscomp$3_date$$=$date$jscomp$1_date$jscomp$2_date$jscomp$3_date$$.endOf("year").toDate().getTime();
break;case "quy":$date$jscomp$1_date$jscomp$2_date$jscomp$3_date$$=moment(new Date(Number($r$$.nam),1,1));$date$jscomp$1_date$jscomp$2_date$jscomp$3_date$$=$date$jscomp$1_date$jscomp$2_date$jscomp$3_date$$.quarters(Number($r$$.quy));$tu_ngay$$=$date$jscomp$1_date$jscomp$2_date$jscomp$3_date$$.startOf("quarters").toDate().getTime();$date$jscomp$1_date$jscomp$2_date$jscomp$3_date$$=$date$jscomp$1_date$jscomp$2_date$jscomp$3_date$$.endOf("quarters").toDate().getTime();break;case "thang":$date$jscomp$1_date$jscomp$2_date$jscomp$3_date$$=
moment(new Date(Number($r$$.nam),0,1));$date$jscomp$1_date$jscomp$2_date$jscomp$3_date$$=$date$jscomp$1_date$jscomp$2_date$jscomp$3_date$$.month(Number($r$$.thang)-1);$tu_ngay$$=$date$jscomp$1_date$jscomp$2_date$jscomp$3_date$$.startOf("months").toDate().getTime();$date$jscomp$1_date$jscomp$2_date$jscomp$3_date$$=$date$jscomp$1_date$jscomp$2_date$jscomp$3_date$$.endOf("months").toDate().getTime();break;case "ngay_ct":$date$jscomp$1_date$jscomp$2_date$jscomp$3_date$$=moment($r$$.ngay_ct);$tu_ngay$$=
$date$jscomp$1_date$jscomp$2_date$jscomp$3_date$$.startOf("dates").toDate().getTime();$date$jscomp$1_date$jscomp$2_date$jscomp$3_date$$=$date$jscomp$1_date$jscomp$2_date$jscomp$3_date$$.endOf("dates").toDate().getTime();break;default:$tu_ngay$$=(new Date($condition$$.tu_ngay)).getTime(),$date$jscomp$1_date$jscomp$2_date$jscomp$3_date$$=(new Date($condition$$.den_ngay)).getTime()}var $cust_created_ngay_mua_dau_tien$$=(new Date($r$$.ngay_mua_dau_tien)).getTime();$r$$.mua_lan_dau=$r$$.ngay_mua_dau_tien&&
$cust_created_ngay_mua_dau_tien$$>=$tu_ngay$$&&$cust_created_ngay_mua_dau_tien$$<=$date$jscomp$1_date$jscomp$2_date$jscomp$3_date$$?1:0;$r$$.kh_cu=$r$$.ngay_mua_dau_tien&&$cust_created_ngay_mua_dau_tien$$<$tu_ngay$$?1:0;$cust_created_ngay_mua_dau_tien$$=(new Date($r$$.cust_date_created)).getTime();$r$$.kh_moi=$r$$.ngay_mua_dau_tien&&$cust_created_ngay_mua_dau_tien$$>=$tu_ngay$$&&$cust_created_ngay_mua_dau_tien$$<=$date$jscomp$1_date$jscomp$2_date$jscomp$3_date$$?1:0}else $r$$.mua_lan_dau=0,$r$$.kh_moi=
0,$r$$.kh_cu=0});switch($condition$$.loai_kh){case "MUA_LAN_DAU":$data$$=$data$$.filter($c$$=>$c$$.mua_lan_dau);$rs$$=$rs$$.filter($r$$=>$data$$.find($c$$=>$c$$.ma_kh===$r$$.ma_kh));break;case "KH_MOI":$data$$=$data$$.filter($c$$=>$c$$.kh_moi);$rs$$=$rs$$.filter($r$$=>$data$$.find($c$$=>$c$$.ma_kh===$r$$.ma_kh));break;case "KH_CU":$data$$=$data$$.filter($c$$=>$c$$.kh_cu),$rs$$=$rs$$.filter($r$$=>$data$$.find($c$$=>$c$$.ma_kh===$r$$.ma_kh))}let $report$$=await $data$$.asyncGroupBy($group_by$$,[...$error$$,
"kh_moi","kh_cu","t_kh","sl_dh","mua_lan_dau"]);$report$$=$report$$.sort(($a$$,$b$$)=>{for(let $i$$=0;$i$$<$group_by$$.length;$i$$++){let $field$$=$group_by$$[$i$$],$a_v$$=$a$$[$field$$],$b_v$$=$b$$[$field$$];$field$$=="ngay_ct"&&($a_v$$=moment($a_v$$).format("YYYY-MM-DD"),$b_v$$=moment($b_v$$).format("YYYY-MM-DD"));if($a_v$$<$b_v$$)return-1;if($a_v$$>$b_v$$)return 1}return 0});$report$$.forEach(function($r$$,$i$$){$r$$.stt=$i$$+1;$r$$.bold=!1;$r$$.t_phai_thu=$r$$.t_tien_hang_ct-($r$$.t_tien_ck||
0)+($r$$.t_thue||0)+($r$$.tien_phi_vc||0)-($r$$.tien_freeship||0)-($r$$.tien_ck_hd||0)-($r$$.tien_evoucher||0);$r$$.t_doanh_thu=$r$$.t_tien_hang_ct-($r$$.t_tien_ck||0)+($r$$.tien_phi_vc||0)-($r$$.tien_freeship||0)-($r$$.tien_ck_hd||0)-($r$$.tien_evoucher||0);$r$$.t_lai=$r$$.t_doanh_thu-$r$$.t_tien_xuat-($r$$.t_tien_tl-$r$$.t_tien_nhap)-($r$$.t_tien_cp||0);$r$$.t_doanh_thu_thuan=$r$$.t_doanh_thu-$r$$.t_tien_tl;$r$$.t_tien_thu=$thanh_toan$$.map($pt$$=>$r$$[$pt$$.name]||0).reduce(($a$$,$b$$)=>$a$$+$b$$,
0);$r$$.t_tien_paymentByPoints=$r$$.tien_paymentByPoints||0;$r$$.nam?($i$$=moment(new Date($r$$.nam,0,1)),$r$$.quy?($i$$=$i$$.quarter($r$$.quy),$r$$.tu_ngay=$i$$.startOf("quarter").toDate(),$r$$.den_ngay=$i$$.endOf("quarter").toDate()):$r$$.thang?($i$$=$i$$.month($r$$.thang-1),$r$$.tu_ngay=$i$$.startOf("month").toDate(),$r$$.den_ngay=$i$$.endOf("month").toDate()):($r$$.tu_ngay=$i$$.startOf("year").toDate(),$r$$.den_ngay=$i$$.endOf("year").toDate())):($r$$.tu_ngay=$condition$$.tu_ngay,$r$$.den_ngay=
$condition$$.den_ngay)});await $report$$.filter($r$$=>$r$$.ma_kh).asyncJoinModel2($condition$$.id_app,"customer",{where:"ma_kh",fields:"ten_kh"});await $report$$.filter($r$$=>$r$$.ma_kho).asyncJoinModel2($condition$$.id_app,"dmkho",{where:"ma_kho",fields:"ten_kho"});const $tong_cong$$={[$group_by$$[0]]:"T\u1ed5ng c\u1ed9ng",mua_lan_dau:$report$$.csum("mua_lan_dau"),kh_moi:$report$$.csum("kh_moi"),kh_cu:$report$$.csum("kh_cu"),t_kh:$report$$.csum("t_kh"),sl_dh:$report$$.csum("sl_dh"),t_sl_xuat:$report$$.csum("t_sl_xuat"),
t_sl_nhap:$report$$.csum("t_sl_nhap"),t_tien_hang:$report$$.csum("t_tien_hang"),t_tien_hang_ct:$report$$.csum("t_tien_hang_ct"),t_thue:$report$$.csum("t_thue"),t_tien_cp:$report$$.csum("t_tien_cp"),t_tien_ck:$report$$.csum("t_tien_ck"),t_tien:$report$$.csum("t_tien"),tien_ck_hd:$report$$.csum("tien_ck_hd"),tien_phi_vc:$report$$.csum("tien_phi_vc"),tien_freeship:$report$$.csum("tien_freeship"),t_tien_tl:$report$$.csum("t_tien_tl"),t_tien_thue_tl:$report$$.csum("t_tien_thue_tl"),t_tien_xuat:$report$$.csum("t_tien_xuat"),
t_tien_nhap:$report$$.csum("t_tien_nhap"),t_phai_thu:$report$$.csum("t_phai_thu"),t_doanh_thu:$report$$.csum("t_doanh_thu"),t_doanh_thu_thuan:$report$$.csum("t_doanh_thu_thuan"),tien_evoucher:$report$$.csum("tien_evoucher"),tien_thu:$report$$.csum("tien_thu"),tien_thu2:$report$$.csum("tien_thu2"),t_tien_thu:$report$$.csum("t_tien_thu"),phai_tra:$report$$.csum("phai_tra"),t_tien_paymentByPoints:$report$$.csum("tien_paymentByPoints"),t_lai:$report$$.csum("t_lai"),t_tien_hoa_hong:$report$$.csum("t_tien_hoa_hong"),
t_tien_hoa_hong_gt:$report$$.csum("t_tien_hoa_hong_gt"),t_tien_hoa_hong_ctv:$report$$.csum("t_tien_hoa_hong_ctv"),t_tien_hoa_hong_ql:$report$$.csum("t_tien_hoa_hong_ql"),t_tien_da_thu:$report$$.csum("t_tien_da_thu"),t_tien_con_no:$report$$.csum("t_tien_con_no"),bold:!0};$thanh_toan$$.forEach($pt$$=>{$pt$$.tong_cong=$tong_cong$$[$pt$$.name]=$report$$.csum($pt$$.name)});$report$$.push($tong_cong$$);$report$$.forEach($r$$=>{$r$$.t_tien_phai_tl=$r$$.t_tien_tl+$r$$.t_tien_thue_tl});$callback$$(null,{data:$report$$,
thanh_toan:$thanh_toan$$.filter($t$$=>$t$$.tong_cong)})})})};