UNPKG

flexbiz-server

Version:

Flexible Server

25 lines (24 loc) 12.3 kB
const SocaiModel=global.getModel("socai"),dmtk=global.getModel("account"),async=require("async"),cktk=require("./cktk"),ckcn=require("./ckcn"),moment=require("moment"),numeral=require("numeral"),asyncCkcn=async($condition$$,$options$$={model:"socai"})=>new Promise(($resolve$$,$reject$$)=>{ckcn($condition$$,($du_e$$,$rs$$)=>{if($du_e$$)return $reject$$($du_e$$);$du_e$$=$rs$$.map($r$$=>$r$$.du_no00-$r$$.du_co00).reduce(($a$$,$b$$)=>$a$$+$b$$,0);$resolve$$($du_e$$)},$options$$)}),asyncCheckHMCN=async($hmcnct_id_app$$, $obj$$,$tk$$,$ma_kh$$,$ps_no_tien_tien$$,$ps_co$$,$du_ck_ma_nv$$)=>{let $ex_id_ct$$=$obj$$?._id?.toString();var $acc_hm_query_query_hm_chung$$=await dmtk.findOne({id_app:$hmcnct_id_app$$,tk:$tk$$,loai_tk:1}).lean();if(!$acc_hm_query_query_hm_chung$$)throw"Kh\u00f4ng t\u1ed3n t\u1ea1i t\u00e0i kho\u1ea3n "+$tk$$;if($acc_hm_query_query_hm_chung$$.tk_cn){$acc_hm_query_query_hm_chung$$={id_app:$hmcnct_id_app$$,tk:$tk$$,ma_kh:$ma_kh$$,status:!0};$du_ck_ma_nv$$&&($acc_hm_query_query_hm_chung$$.ma_nv=$du_ck_ma_nv$$); let $ngay_ct$$=Math.max(new Date($obj$$.ngay_ghi_so||$obj$$.ngay_ct),new Date($obj$$.ngay_ct));$acc_hm_query_query_hm_chung$$.$and=[{$or:[{hieu_luc_tu:null},{hieu_luc_tu:{$lte:moment($ngay_ct$$).endOf("date").toDate()}}]},{$or:[{hieu_luc_den:null},{hieu_luc_den:{$gte:moment($ngay_ct$$).startOf("date").toDate()}}]}];$acc_hm_query_query_hm_chung$$=await global.getModel("hmcn").findOne($acc_hm_query_query_hm_chung$$).lean();$acc_hm_query_query_hm_chung$$||($acc_hm_query_query_hm_chung$$={id_app:$hmcnct_id_app$$, tk:$tk$$,status:!0,ma_kh:{$in:[null,""]}},$du_ck_ma_nv$$&&($acc_hm_query_query_hm_chung$$.ma_nv=$du_ck_ma_nv$$),$acc_hm_query_query_hm_chung$$=await global.getModel("hmcn").findOne($acc_hm_query_query_hm_chung$$).lean());if($acc_hm_query_query_hm_chung$$){$du_ck_ma_nv$$=await asyncCkcn({id_app:$hmcnct_id_app$$,ex_id_ct:$ex_id_ct$$,tk:$tk$$,ma_kh:$ma_kh$$,ma_nv:$du_ck_ma_nv$$,bu_tru:!0,ngay:moment($ngay_ct$$).endOf("date").toDate()},{id_ct:$obj$$._id?.toString()});if($acc_hm_query_query_hm_chung$$.loai== "2"){$du_ck_ma_nv$$=0-$du_ck_ma_nv$$;$ps_no_tien_tien$$=$ps_co$$-$ps_no_tien_tien$$;if($ps_no_tien_tien$$<0)return;if($acc_hm_query_query_hm_chung$$.hm==0){if($du_ck_ma_nv$$>$acc_hm_query_query_hm_chung$$.hm)throw`Nh\u00e0 cung c\u1ea5p n\u00e0y ch\u1ec9 \u0111\u01b0\u1ee3c n\u1ee3 \u1edf \u0111\u01a1n \u0111\u1ea7u ti\u00ean. C\u00f4ng n\u1ee3 hi\u1ec7n t\u1ea1i ${numeral($du_ck_ma_nv$$).format()} VND, ph\u00e1t sinh ${$ps_no_tien_tien$$<0?"-":""} ${numeral(Math.abs($ps_no_tien_tien$$)).format()} VND`; }else if($du_ck_ma_nv$$+$ps_no_tien_tien$$>$acc_hm_query_query_hm_chung$$.hm)throw`Nh\u00e0 cung c\u1ea5p n\u00e0y \u0111\u00e3 v\u01b0\u1ee3t h\u1ea1n m\u1ee9c c\u00f4ng n\u1ee3 ${numeral($acc_hm_query_query_hm_chung$$.hm).format()} VND, c\u00f4ng n\u1ee3 hi\u1ec7n t\u1ea1i ${numeral($du_ck_ma_nv$$).format()} VND, ph\u00e1t sinh ${$ps_no_tien_tien$$<0?"-":""} ${numeral(Math.abs($ps_no_tien_tien$$)).format()} VND`;}else{$ps_no_tien_tien$$-=$ps_co$$;if($ps_no_tien_tien$$<0)return;if($acc_hm_query_query_hm_chung$$.hm== 0){if($du_ck_ma_nv$$>$acc_hm_query_query_hm_chung$$.hm)throw`Kh\u00e1ch h\u00e0ng n\u00e0y ch\u1ec9 \u0111\u01b0\u1ee3c n\u1ee3 \u1edf \u0111\u01a1n \u0111\u1ea7u ti\u00ean. C\u00f4ng n\u1ee3 hi\u1ec7n t\u1ea1i ${numeral($du_ck_ma_nv$$).format()} VND, ph\u00e1t sinh ${$ps_no_tien_tien$$<0?"-":""} ${numeral(Math.abs($ps_no_tien_tien$$)).format()} VND`;}else if($du_ck_ma_nv$$+$ps_no_tien_tien$$>$acc_hm_query_query_hm_chung$$.hm)throw`Kh\u00e1ch h\u00e0ng n\u00e0y \u0111\u00e3 v\u01b0\u1ee3t h\u1ea1n m\u1ee9c c\u00f4ng n\u1ee3 ${numeral($acc_hm_query_query_hm_chung$$.hm).format()} VND, c\u00f4ng n\u1ee3 hi\u1ec7n t\u1ea1i ${numeral($du_ck_ma_nv$$).format()} VND, ph\u00e1t sinh ${$ps_no_tien_tien$$< 0?"-":""} ${numeral(Math.abs($ps_no_tien_tien$$)).format()} VND`;}$hmcnct_id_app$$=await global.getModel("hmcnct").findOne({id_app:$hmcnct_id_app$$,tk:$tk$$,ma_kh:$ma_kh$$,id_ct:$obj$$._id.toString()}).lean();$hmcnct_id_app$$||($hmcnct_id_app$$={...$acc_hm_query_query_hm_chung$$},$hmcnct_id_app$$.id_ct=$obj$$._id.toString(),delete $hmcnct_id_app$$._id,await global.getModel("hmcnct").create($hmcnct_id_app$$))}}},checkAcc=function($id_app$$,$data$$,$tk$$,$ma_kh$$,$callback$$){dmtk.findOne({id_app:$id_app$$, tk:$tk$$,loai_tk:1}).lean().then(async $acc$$=>{if(!$acc$$)return $callback$$(`T\u00e0i kho\u1ea3n ${$tk$$} kh\u00f4ng t\u1ed3n t\u1ea1i ho\u1eb7c kh\u00f4ng ph\u1ea3i l\u00e0 t\u00e0i kho\u1ea3n chi ti\u1ebft.`);if($acc$$.tk_cn&&!$ma_kh$$)return $callback$$(`T\u00e0i kho\u1ea3n ${$tk$$} y\u00eau c\u1ea7u nh\u1eadp kh\u00e1ch h\u00e0ng`);if($acc$$.tk_cp_yn&&!$data$$.ma_phi)return $callback$$(`T\u00e0i kho\u1ea3n ${$tk$$} y\u00eau c\u1ea7u nh\u1eadp doanh thu/chi ph\u00ed`);if($acc$$.tk_kho_yn&&!$data$$.ma_kho)return $callback$$(`T\u00e0i kho\u1ea3n ${$tk$$} y\u00eau c\u1ea7u nh\u1eadp kho/c\u1eeda h\u00e0ng`); if($acc$$.tk_bp_yn&&!$data$$.ma_bp)return $callback$$(`T\u00e0i kho\u1ea3n ${$tk$$} y\u00eau c\u1ea7u nh\u1eadp b\u1ed9 ph\u1eadn`);if($acc$$.tk_dt_yn&&!$data$$.ma_dt)return $callback$$(`T\u00e0i kho\u1ea3n ${$tk$$} y\u00eau c\u1ea7u nh\u1eadp v\u1ee5 vi\u1ec7c, d\u1ef1 \u00e1n`);if($acc$$.tk_hd_yn&&!$data$$.ma_hd)return $callback$$(`T\u00e0i kho\u1ea3n ${$tk$$} y\u00eau c\u1ea7u nh\u1eadp h\u1ee3p \u0111\u1ed3ng`);if($acc$$.tk_nv_yn&&!$data$$.ma_nv)return $callback$$(`T\u00e0i kho\u1ea3n ${$tk$$} y\u00eau c\u1ea7u nh\u1eadp nh\u00e2n vi\u00ean`); if($acc$$.tk_td1_yn&&!$data$$.ma_td1)return $callback$$(`T\u00e0i kho\u1ea3n ${$tk$$} y\u00eau c\u1ea7u nh\u1eadp ma_td1`);if($acc$$.tk_td2_yn&&!$data$$.ma_td2)return $callback$$(`T\u00e0i kho\u1ea3n ${$tk$$} y\u00eau c\u1ea7u nh\u1eadp ma_td2`);if($acc$$.tk_td3_yn&&!$data$$.ma_td3)return $callback$$(`T\u00e0i kho\u1ea3n ${$tk$$} y\u00eau c\u1ea7u nh\u1eadp ma_td3`);$callback$$(null,$acc$$)}).catch($e$$=>{$callback$$($e$$)})},checkData=function($id_app$$,$ngay_ct$$,$_data$$,$callback$$){async.parallel({tk_no:$callback$$=> {checkAcc($id_app$$,$_data$$,$_data$$.tk_no,$_data$$.ma_kh_no,($e$$,$acc$$)=>{if($e$$)return $callback$$($e$$);$acc$$.not_du_no_yn&&$_data$$.check_balance!=0?cktk({id_app:$id_app$$,tk:$_data$$.tk_no,bu_tru:!0,ngay:moment($ngay_ct$$).endOf("date").toDate()},($du_no$jscomp$2_e$$,$rs$$)=>{if($du_no$jscomp$2_e$$)return $callback$$($du_no$jscomp$2_e$$);$du_no$jscomp$2_e$$=$rs$$.map($r$$=>$r$$.du_no00-$r$$.du_co00).reduce(($a$$,$b$$)=>$a$$+$b$$,0)+$_data$$.tien;$du_no$jscomp$2_e$$>0?$callback$$(`T\u00e0i kho\u1ea3n ${$_data$$.tk_no} kh\u00f4ng \u0111\u01b0\u1ee3c ph\u00e9p c\u00f3 s\u1ed1 d\u01b0 b\u00ean n\u1ee3 : ${$du_no$jscomp$2_e$$}`): $callback$$()}):$callback$$()})},tk_co:$callback$$=>{checkAcc($id_app$$,$_data$$,$_data$$.tk_co,$_data$$.ma_kh_co,($e$$,$acc$$)=>{if($e$$)return $callback$$($e$$);$acc$$.not_du_co_yn&&$_data$$.check_balance!=0?cktk({id_app:$id_app$$,tk:$_data$$.tk_co,bu_tru:!0,ngay:moment($ngay_ct$$).endOf("date").toDate()},($du_co$jscomp$2_e$$,$rs$$)=>{if($du_co$jscomp$2_e$$)return $callback$$($du_co$jscomp$2_e$$);$du_co$jscomp$2_e$$=$rs$$.map($r$$=>$r$$.du_co00-$r$$.du_no00).reduce(($a$$,$b$$)=>$a$$+$b$$,0)+$_data$$.tien; $du_co$jscomp$2_e$$>0?$callback$$(`T\u00e0i kho\u1ea3n ${$_data$$.tk_co} kh\u00f4ng \u0111\u01b0\u1ee3c ph\u00e9p c\u00f3 s\u1ed1 d\u01b0 b\u00ean c\u00f3: ${$du_co$jscomp$2_e$$}`):$callback$$()}):$callback$$()})}},$e$$=>{$callback$$($e$$,$_data$$)})},post=function($master$$,$details$$,$prepare$$,$model$$=SocaiModel,$_options$$={}){$details$$&&(this.master={...$master$$},this.details=$details$$.map($detail$$=>({...$detail$$})),this.model=$model$$,this._options=$_options$$,this.prepare=$prepare$$)}; post.prototype.runPrepare=async($prepare$$,$details$$)=>{$details$$=$details$$.map($d$$=>{$d$$.toObject&&($d$$=$d$$.toObject());return{...$d$$}});return new Promise($resolve$$=>{$prepare$$?async.map($details$$,($detail$$,$callback$$)=>{setImmediate(()=>{$detail$$.toObject=function(){return Object.assign({},$detail$$)};$prepare$$($detail$$,$detail$$=>{$detail$$.toObject=function(){return Object.assign({},$detail$$)};$callback$$(null,$detail$$)})})},($e$$,$details$$)=>{$resolve$$($details$$)}):$resolve$$($details$$)})}; post.prototype.run=async function($callback$$=()=>{}){if(!this.details)return $callback$$(null,null);const $Socai$$=this.model||SocaiModel;let $master$$=this.master;if($master$$.trang_thai&&$master$$.ma_ct){var $details$$=await global.getModel("trangthai").findOne({ma_trang_thai:$master$$.trang_thai,ma_ct:$master$$.ma_ct}).lean();if($details$$?.post_books&&$details$$?.post_books?.indexOf($Socai$$.modelName.toLowerCase())<0||$details$$?.not_post_books&&$details$$?.not_post_books?.indexOf($Socai$$.modelName.toLowerCase())>= 0)return $callback$$(null,null)}$details$$=await this.runPrepare(this.prepare,this.details);let $ngay_ct$$=Math.max(new Date($master$$.ngay_ghi_so||$master$$.ngay_ct),new Date($master$$.ngay_ct));if(!$ngay_ct$$)return $callback$$("ngay_ct is not null");$details$$.forEach($d$$=>{$d$$.tk_no=$d$$.tk_no||$master$$.tk_no;$d$$.tk_co=$d$$.tk_co||$master$$.tk_co;$d$$.ma_ct=$master$$.ma_ct;$d$$.so_ct=$master$$.so_ct;$d$$.ngay_ct=$ngay_ct$$});$details$$=$details$$.filter($d$$=>$d$$.tk_no&&$d$$.tk_co&&($d$$.tk_no!= $d$$.tk_co||$d$$.ma_kh_no!=$d$$.ma_kh_co));if(this._options.kiem_tra_han_muc_cong_no){let $check_hmcn$$=$details$$.map($d$$=>[{tk:$d$$.tk_no,ps_no:$d$$.tien,ps_co:0,ma_kh:$d$$.ma_kh_no||$master$$.ma_kh_no||$d$$.ma_kh||$master$$.ma_kh,ma_nv:$d$$.ma_nv||$master$$.ma_nv},{tk:$d$$.tk_co,ps_no:0,ps_co:$d$$.tien,ma_kh:$d$$.ma_kh_co||$master$$.ma_kh_co||$d$$.ma_kh||$master$$.ma_kh,ma_nv:$d$$.ma_nv||$master$$.ma_nv}]).reduce(($a$$,$b$$)=>$a$$.concat($b$$),[]).filter($a$$=>$a$$.ma_kh&&($a$$.ps_no||$a$$.ps_co)), $groupBy$$=["tk","ma_kh"];(await global.getModel("app").findOne({_id:$master$$.id_app},{options:1}))?.options?.kiem_tra_han_muc_cong_no_kh_nv&&$groupBy$$.push("ma_nv");$check_hmcn$$=await $check_hmcn$$.asyncGroupBy($groupBy$$,["ps_no","ps_co"]);if($check_hmcn$$.length>0)try{await Promise.all($check_hmcn$$.map($d$$=>asyncCheckHMCN($master$$.id_app,$master$$,$d$$.tk,$d$$.ma_kh,$d$$.ps_no,$d$$.ps_co,$d$$.ma_nv)))}catch($e$$){return $callback$$($e$$.message||$e$$.error||$e$$)}}async.map($details$$,($detail$$, $callback$$)=>{$detail$$.ma_kh_no||($detail$$.ma_kh_no=$master$$.ma_kh_no||$detail$$.ma_kh||$master$$.ma_kh);$detail$$.ma_kh_co||($detail$$.ma_kh_co=$master$$.ma_kh_co||$detail$$.ma_kh||$master$$.ma_kh);checkData($master$$.id_app,$ngay_ct$$,$detail$$,($e$$,$detail$$)=>{$callback$$($e$$,$detail$$)})},($but_toan_loi_e$$,$details$$)=>{if($but_toan_loi_e$$)return $callback$$($but_toan_loi_e$$);$details$$.forEach($detail$$=>{!isNaN($detail$$.tien)&&$detail$$.tien||isNaN($detail$$.tien_nt)||($detail$$.tien= $detail$$.tien_nt*($master$$.ty_gia||1))});$but_toan_loi_e$$=$details$$.filter($detail$$=>isNaN($detail$$.tien_nt)||isNaN($detail$$.tien));$but_toan_loi_e$$.length>0&&Logger.error("[postsocai] B\u00fat to\u00e1n kh\u00f4ng h\u1ee3p l\u1ec7 v\u00ec tien_nt ho\u1eb7c tien l\u00e0 isNaN:",{so_ct:$master$$.so_ct,ma_ct:$master$$.ma_ct,chi_tiet:$but_toan_loi_e$$},", Ch\u01b0\u01a1ng tr\u00ecnh s\u1ebd b\u1ecf qua c\u1eb7p \u0111\u1ecbnh kho\u1ea3n n\u00e0y");let $dks$$=$details$$.filter($detail$$=>!($detail$$.tien_nt=== 0&&$detail$$.tien===0)&&!isNaN($detail$$.tien_nt)&&!isNaN($detail$$.tien)).map(($detail$$,$nh_dk$$)=>{for(let $attr$$ in $Socai$$.schema.paths)$attr$$!="id_ct"&&$attr$$!="_id"&&$attr$$!="__v"&&$attr$$!="date_updated"&&$attr$$!="date_created"&&($detail$$[$attr$$]!==void 0&&$detail$$[$attr$$]!==null&&$detail$$[$attr$$]!==""||$detail$$[$attr$$]===0||$master$$[$attr$$]===void 0||($detail$$[$attr$$]=$master$$[$attr$$]));$detail$$.nh_dk=$nh_dk$$;$detail$$.id_ct=$master$$._id.toString();delete $detail$$.toObject; delete $detail$$._id;delete $detail$$.__v;delete $detail$$.listinfo_code;return $detail$$});$details$$={id_app:$master$$.id_app,id_ct:$master$$._id.toString()};this._options.deleteQuery&&($details$$={...$details$$,...this._options.deleteQuery,id_app:$master$$.id_app});$Socai$$.deleteMany($details$$,$error$$=>{if($error$$)return $callback$$("Don't remove socai \n"+$error$$);if($dks$$.length==0)return $callback$$(null,[]);$Socai$$.create($dks$$,async($e$$,$rs$$)=>{$e$$?$callback$$($e$$):$callback$$(null, $rs$$)})})})};module.exports=post;