flexbiz-server
Version:
Flexible Server
8 lines (7 loc) • 2.86 kB
JavaScript
const sokho=global.getModel("sokho"),dmkh=global.getModel("customer"),rptobject=global.getModel("rptobject"),async=require("async"),underscore=require("underscore"),controller=require("../../controllers/controllerRPT");
module.exports=function($router$$){new controller($router$$,"ptbitralai",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,ma_ct:{$in:["HD3"]}},$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_vt&&($query$$.ma_vt={$regex:$condition$$.ma_vt,$options:"i"});$condition$$.ma_kho&&($query$$.ma_kho={$regex:$condition$$.ma_kho,$options:"i"});async.series({rptobject:function($callback$$){rptobject.findOne({code_rpt:"ptbitralai",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});sokho.find($query$$).lean().exec(function($error$$,$rs$$){if($error$$)return Logger.info($error$$),
$callback$$($error$$);$rs$$.groupBy($bao_cao_theo$$,[{name:"sl_nhap",value:"sl_nhap"},{name:"tien",value:"tien"},{name:"tien_ck",value:"tien_ck"}],function($error$$,$rs$$){if($error$$)return Logger.info($error$$),$callback$$($error$$);var $report$$=underscore.sortBy($rs$$,function($r$$){return $r$$[$bao_cao_theo$$]});let $i$$=1;$report$$.forEach(function($r$$){$r$$.stt=$i$$;$r$$.bold=!1;$r$$.tien_tl=$r$$.tien-$r$$.tien_ck;$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(){const $t_sl$$=$report$$.csum("sl_nhap"),$t_tien_tl$$=$report$$.csum("tien_tl"),$t_tien$$=$report$$.csum("tien");$report$$.push({chi_tieu:"T\u1ed5ng c\u1ed9ng",sl_nhap:$t_sl$$,tien:$t_tien$$,tien_ck:$report$$.csum("tien_ck"),tien_tl:$t_tien_tl$$,bold:!0});$t_sl$$!==0&&$report$$.forEach(function($r$$){$r$$.ty_trong=Math.roundBy($r$$.sl_nhap/$t_sl$$,2)*100});$callback$$(null,
$report$$)})})})})})};