UNPKG

flexbiz-server

Version:

Flexible Server

9 lines (8 loc) 3.43 kB
const async=require("async"),_=require("lodash"),so1=global.getModel("so1"),hd3=global.getModel("hd3"),dmkh=global.getModel("customer"),rptobject=global.getModel("rptobject"),controller=require("../../controllers/controllerRPT"); module.exports=function($router$$){new controller($router$$,"ptdonhang",function($req$$,$callback$$){const $condition$$=$req$$.query;if(!$condition$$.tu_ngay||!$condition$$.den_ngay||!$condition$$.bao_cao_theo)return $callback$$("B\u00e1o c\u00e1o n\u00e0y y\u00eau c\u1ea7u c\u00e1c tham s\u1ed1:tu_ngay,den_ngay,bao_cao_theo");const $query$$={id_app:$condition$$.id_app},$bao_cao_theo$$=$condition$$.bao_cao_theo;$query$$.ngay_ct={$gte:$condition$$.tu_ngay,$lte:$condition$$.den_ngay};$condition$$.ma_dvcs&& ($query$$.ma_dvcs={$regex:$condition$$.ma_dvcs,$options:"i"});$condition$$.ma_kho&&($query$$.ma_kho={$regex:$condition$$.ma_kho,$options:"i"});async.series({rptobject:function($callback$$){rptobject.findOne({code_rpt:"ptdonhang",ma_obj:$bao_cao_theo$$}).lean().exec(function($e$$,$rptobj$$){if($e$$)return $callback$$($e$$);if(!$rptobj$$)return $callback$$("Kh\u00f4ng th\u1ec3 b\u00e1o c\u00e1o theo \u0111\u1ed1i t\u01b0\u1ee3ng n\u00e0y");$callback$$(null,$rptobj$$)})},dskhach:function($callback$$){$condition$$.nh_kh? dmkh.find({id_app:$query$$.id_app,status:!0,nh_kh:$condition$$.nh_kh}).lean().exec(function($e$$,$dskh$$){if($e$$)return $callback$$($e$$);$callback$$(null,$dskh$$.map($k$$=>$k$$.ma_kh))}):$callback$$(null,null)}},function($e$$,$rs$$){if($e$$)return $callback$$($e$$);const $rptobj$$=$rs$$.rptobject;$rs$$.dskhach&&($query$$.ma_kh={$in:$rs$$.dskhach});async.series({tralai:function($callback$$){const $q$$={id_app:$query$$.id_app,ngay_ct:$query$$.ngay_ct};$condition$$.ma_dvcs&&($q$$.ma_dvcs={$regex:$condition$$.ma_dvcs, $options:"i"});$condition$$.ma_kho&&($q$$.ma_kho={$regex:$condition$$.ma_kho,$options:"i"});$rs$$.dskhach&&($q$$.ma_kh={$in:$rs$$.dskhach});hd3.find($q$$).lean().exec(function($e$$,$rs$$){if($e$$)return $callback$$($e$$);$callback$$(null,$rs$$)})},donhang:function($callback$$){so1.find($query$$).lean().exec(function($e$$,$rs$$){if($e$$)return $callback$$($e$$);$callback$$(null,$rs$$)})}},function($e$$,$rs$$){if($e$$)return Logger.info($e$$),$callback$$($e$$);$e$$=$rs$$.tralai;$rs$$=$rs$$.donhang; $rs$$.forEach(function($r$$){$r$$.dat_hang=1;Number($r$$.trang_thai)>=3&&($r$$.da_phat=1);Number($r$$.trang_thai)==4&&($r$$.thanh_cong=1);Number($r$$.trang_thai)==5&&($r$$.huy=1)});$e$$.forEach(function($tl$$){$tl$$.tra_lai=1;$rs$$.push($tl$$)});$rs$$.groupBy($bao_cao_theo$$,[{name:"dat_hang",value:"dat_hang"},{name:"da_phat",value:"da_phat"},{name:"thanh_cong",value:"thanh_cong"},{name:"huy",value:"huy"},{name:"tra_lai",value:"tra_lai"}],function($error$$,$rs$$){if($error$$)return Logger.info($error$$), $callback$$($error$$);const $report$$=_.sortBy($rs$$,function($r$$){return $r$$[$bao_cao_theo$$]});let $i$$=1;$report$$.forEach(function($r$$){$r$$.stt=$i$$;$r$$.bold=!1;$i$$++});$error$$=global.getModel(""+$rptobj$$.model_obj);$report$$.joinModel($condition$$.id_app,$error$$,[{akey:$bao_cao_theo$$,bkey:$rptobj$$.value_field,fields:[{name:"chi_tieu",value:$rptobj$$.header_field}]}],function(){$report$$.push({chi_tieu:"T\u1ed5ng c\u1ed9ng",dat_hang:$report$$.csum("dat_hang"),da_phat:$report$$.csum("da_phat"), thanh_cong:$report$$.csum("thanh_cong"),huy:$report$$.csum("huy"),bold:!0});$callback$$(null,$report$$)})})})})})};