UNPKG

flexbiz-server

Version:

Flexible Server

9 lines (8 loc) 3.81 kB
const qts=global.getModel("qts"),sotinhkh=global.getModel("sotinhkh"),tinhkhauhao1ts=require("./tinhkhauhao1ts"),async=require("async"),underscore=require("underscore"),moment=require("moment"); module.exports=async function($condition$$,$fn$$){if($condition$$&&$condition$$.thang&&$condition$$.nam&&$condition$$.id_app){var $ngay_dau_thang_query_check_query_ts$$=moment([$condition$$.nam,$condition$$.thang-1,1]).startOf("month").toDate(),$ngay_cuoi_thang$$=moment($ngay_dau_thang_query_check_query_ts$$).endOf("month").toDate();$ngay_dau_thang_query_check_query_ts$$={id_app:$condition$$.id_app,ngay_cuoi_thang:{$gt:$ngay_cuoi_thang$$}};$condition$$.id_ts&&($ngay_dau_thang_query_check_query_ts$$.id_ts= $condition$$.id_ts);await sotinhkh.findOne($ngay_dau_thang_query_check_query_ts$$)?$fn$$("K\u1ef3 t\u00ednh kh\u1ea5u hao ph\u1ea3i l\u1edbn h\u01a1n ho\u1eb7c b\u1eb1ng k\u1ef3 t\u00ednh tr\u01b0\u1edbc \u0111\u00f3"):($condition$$.tinh_kh_theo_ngay===void 0&&($condition$$.tinh_kh_theo_ngay=!0),$ngay_dau_thang_query_check_query_ts$$={id_app:$condition$$.id_app,ngay_kh:{$lte:$ngay_cuoi_thang$$}},$condition$$.id_ts&&($ngay_dau_thang_query_check_query_ts$$._id=$condition$$.id_ts),qts.find($ngay_dau_thang_query_check_query_ts$$).lean().then(function($tss$$){async.map($tss$$, function($ts$$,$callback$$){setImmediate(()=>{const $c$$={};underscore.extend($c$$,$condition$$);$c$$.id_ts=$ts$$._id;sotinhkh.findOne({id_app:$c$$.id_app,id_ts:$c$$.id_ts,nam:$c$$.nam,thang:$c$$.thang},function($error$$,$stkh$$){if($error$$)return $callback$$($error$$);if($stkh$$&&$stkh$$.sua_kh)return $callback$$(null,$stkh$$);sotinhkh.deleteMany({id_app:$c$$.id_app,id_ts:$c$$.id_ts,nam:$c$$.nam,thang:$c$$.thang},function($error$$){if($error$$)return $callback$$($error$$);tinhkhauhao1ts($c$$,function($error$$, $rs$$){if($error$$)return $callback$$($error$$);if(!$rs$$)return $callback$$(null,null);async.mapLimit($rs$$,50,function($r$$,$callback$$){setImmediate(()=>{$r$$.id_app=$c$$.id_app;$r$$.nam=$c$$.nam;$r$$.thang=$c$$.thang;sotinhkh.create($r$$,function($error$$,$kq$$){if($error$$)return $callback$$($error$$);$kq$$=$kq$$.toObject();$kq$$.ten_ts=$ts$$.ten_ts;$kq$$.ma_ct=$ts$$.ma_ct;$kq$$.tk_cp=$ts$$.tk_cp;$kq$$.tk_kh=$ts$$.tk_kh;$kq$$.ma_bp=$ts$$.ma_bp;$kq$$.ma_phi=$ts$$.ma_phi;$kq$$.ma_dt=$ts$$.ma_dt; $kq$$.ma_hd=$ts$$.ma_hd;$kq$$.ma_kho=$ts$$.ma_kho;$callback$$(null,$kq$$)})})},function($e$$,$rs$$){$callback$$($e$$,$rs$$)})})})})})},function($error$$,$rs$$){if($error$$)return $fn$$($error$$);setImmediate(async()=>{const $result$$=[];$rs$$.forEach(function($r$$){$r$$.forEach(function($r1$$){$result$$.push($r1$$)})});await $result$$.filter($r$$=>$r$$.ma_tang_giam_ts).asyncJoinModel2($condition$$.id_app,"dmtanggiamts",{where:"ma_tang_giam_ts",fields:{kieu_dc:"kieu"}});$result$$.forEach($r$$=>{$r$$.kieu_dc== "2"&&($r$$.nguyen_gia=0-$r$$.nguyen_gia,$r$$.gia_tri_da_kh=0-$r$$.gia_tri_da_kh,$r$$.gia_tri_con_lai=0-$r$$.gia_tri_con_lai,$r$$.gia_tri_kh_ky=0-$r$$.gia_tri_kh_ky)});async.map($result$$,($t$$,$callback$$)=>{setImmediate(async()=>{let $bpsd$$;try{$bpsd$$=await qts.bophansudung($t$$.id_app,$t$$.id_ts,$ngay_cuoi_thang$$)}catch($e$$){$bpsd$$=[{ma_bp:$t$$.ma_bp,so_luong:$t$$.so_luong||1}]}$bpsd$$=await $bpsd$$.asyncGroupBy(["ma_bp"],["so_luong"]);$bpsd$$=$bpsd$$.map($bp$$=>{let $he_so$$=$bp$$.so_luong/ ($t$$.so_luong||1);$bp$$={...$t$$,...$bp$$};$bp$$.nguyen_gia*=$he_so$$;$bp$$.gia_tri_da_kh*=$he_so$$;$bp$$.gia_tri_con_lai*=$he_so$$;$bp$$.gia_tri_kh_ky*=$he_so$$;return $bp$$});$callback$$(null,$bpsd$$)})},async($e$$,$rs$$)=>{if($e$$)return $fn$$($e$$);$e$$=$rs$$.reduce(($a$$,$b$$)=>$a$$.concat($b$$),[]);$fn$$(null,$e$$)})})})}).catch($e$$=>{$fn$$($e$$)}))}else $fn$$("L\u1ed7i: B\u00e1o c\u00e1o n\u00e0y y\u00eau c\u1ea7u c\u00e1c tham s\u1ed1: so_the_ts,thang,nam,id_app")};