flexbiz-server
Version:
Flexible Server
25 lines (24 loc) • 12.1 kB
JavaScript
'use strict';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$$=>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$$)})}),asyncCheckHMCN=async($hmcnct_id_app$$,$obj$$,
$tk$$,$ma_kh$$,$ps_no_tien_tien$$,$ps_co$$)=>{var $acc_hm_query$$=await dmtk.findOne({id_app:$hmcnct_id_app$$,tk:$tk$$,loai_tk:1}).lean();if(!$acc_hm_query$$)throw"Kh\u00f4ng t\u1ed3n t\u1ea1i t\u00e0i kho\u1ea3n "+$tk$$;if($acc_hm_query$$.tk_cn){$acc_hm_query$$={id_app:$hmcnct_id_app$$,tk:$tk$$,ma_kh:$ma_kh$$,status:!0};var $du_ck_ngay_ct$$=new Date($obj$$.ngay_ct);$acc_hm_query$$.$and=[{$or:[{hieu_luc_tu:null},{hieu_luc_tu:{$lte:moment($du_ck_ngay_ct$$).endOf("date").toDate()}}]},{$or:[{hieu_luc_den:null},
{hieu_luc_den:{$gte:moment($du_ck_ngay_ct$$).startOf("date").toDate()}}]}];($acc_hm_query$$=await global.getModel("hmcn").findOne($acc_hm_query$$).lean())||($acc_hm_query$$=await global.getModel("hmcn").findOne({id_app:$hmcnct_id_app$$,tk:$tk$$,status:!0,ma_kh:{$in:[null,""]}}).lean());if($acc_hm_query$$){$du_ck_ngay_ct$$=await asyncCkcn({id_app:$hmcnct_id_app$$,tk:$tk$$,ma_kh:$ma_kh$$,bu_tru:!0,ngay:moment($obj$$.ngay_ct).endOf("year").toDate()});if("2"==$acc_hm_query$$.loai){$du_ck_ngay_ct$$=0-
$du_ck_ngay_ct$$;$ps_no_tien_tien$$=$ps_co$$-$ps_no_tien_tien$$;if(0>$ps_no_tien_tien$$)return;if(0==$acc_hm_query$$.hm){if($du_ck_ngay_ct$$>$acc_hm_query$$.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$$.hm).format()} VND, c\u00f4ng n\u1ee3 hi\u1ec7n t\u1ea1i ${numeral($du_ck_ngay_ct$$).format()} VND, ph\u00e1t sinh ${0>$ps_no_tien_tien$$?"-":""} ${numeral(Math.abs($ps_no_tien_tien$$)).format()} VND`;}else if($du_ck_ngay_ct$$+
$ps_no_tien_tien$$>$acc_hm_query$$.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$$.hm).format()} VND, c\u00f4ng n\u1ee3 hi\u1ec7n t\u1ea1i ${numeral($du_ck_ngay_ct$$).format()} VND, ph\u00e1t sinh ${0>$ps_no_tien_tien$$?"-":""} ${numeral(Math.abs($ps_no_tien_tien$$)).format()} VND`;}else{$ps_no_tien_tien$$-=$ps_co$$;if(0>$ps_no_tien_tien$$)return;if(0==$acc_hm_query$$.hm){if($du_ck_ngay_ct$$>$acc_hm_query$$.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$$.hm).format()} VND, c\u00f4ng n\u1ee3 hi\u1ec7n t\u1ea1i ${numeral($du_ck_ngay_ct$$).format()} VND, ph\u00e1t sinh ${0>
$ps_no_tien_tien$$?"-":""} ${numeral(Math.abs($ps_no_tien_tien$$)).format()} VND`;}else if($du_ck_ngay_ct$$+$ps_no_tien_tien$$>$acc_hm_query$$.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$$.hm).format()} VND, c\u00f4ng n\u1ee3 hi\u1ec7n t\u1ea1i ${numeral($du_ck_ngay_ct$$).format()} VND, ph\u00e1t sinh ${0>$ps_no_tien_tien$$?"-":""} ${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$$},$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().exec(async($e$$,$acc$$)=>{if($e$$||!$acc$$)return $callback$$($e$$||`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$$)})},checkData=function($id_app$$,$ngay_ct$$,$_data$$,$callback$jscomp$0$$){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&&
0!=$_data$$.check_balance?cktk({id_app:$id_app$$,tk:$_data$$.tk_no,bu_tru:!0,ngay:moment($ngay_ct$$).endOf("year").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;0<$du_no$jscomp$2_e$$?$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&&0!=$_data$$.check_balance?cktk({id_app:$id_app$$,tk:$_data$$.tk_co,bu_tru:!0,ngay:moment($ngay_ct$$).endOf("year").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;
0<$du_co$jscomp$2_e$$?$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$jscomp$0$$($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$jscomp$0$$)=>{$details$jscomp$0$$=$details$jscomp$0$$.map($d$$=>{$d$$.toObject&&($d$$=$d$$.toObject());return{...$d$$}});return new Promise($resolve$$=>{$prepare$$?async.map($details$jscomp$0$$,($detail$jscomp$0$$,$callback$$)=>{setImmediate(()=>{$detail$jscomp$0$$.toObject=function(){return Object.assign({},$detail$jscomp$0$$)};$prepare$$($detail$jscomp$0$$,$detail$$=>{$detail$$.toObject=function(){return Object.assign({},$detail$$)};$callback$$(null,
$detail$$)})})},($e$$,$details$$)=>{$resolve$$($details$$)}):$resolve$$($details$jscomp$0$$)})};
post.prototype.run=async function($callback$jscomp$0$$=()=>{}){if(!this.details)return $callback$jscomp$0$$(null,null);const $Socai$$=this.model||SocaiModel;let $master$$=this.master,$details$jscomp$0$$=await this.runPrepare(this.prepare,this.details);$details$jscomp$0$$.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=$master$$.ngay_ct});$details$jscomp$0$$=$details$jscomp$0$$.filter($d$$=>
$d$$.tk_no&&$d$$.tk_co);if(!$master$$.ngay_ct)return $callback$jscomp$0$$("ngay_ct is not null");if(this._options.kiem_tra_han_muc_cong_no){console.log("Ki\u1ec3m tra h\u1ea1n m\u1ee9c c\u00f4ng n\u1ee3",$master$$.ma_kh,$master$$.ma_ct);let $check_hmcn$$=$details$jscomp$0$$.map($d$$=>[{tk:$d$$.tk_no,ps_no:$d$$.tien,ps_co:0,ma_kh:$d$$.ma_kh_no||$d$$.ma_kh||$master$$.ma_kh},{tk:$d$$.tk_co,ps_no:0,ps_co:$d$$.tien,ma_kh:$d$$.ma_kh_co||$d$$.ma_kh||$master$$.ma_kh}]).reduce(($a$$,$b$$)=>$a$$.concat($b$$),
[]).filter($a$$=>$a$$.ma_kh&&($a$$.ps_no||$a$$.ps_co));$check_hmcn$$=await $check_hmcn$$.asyncGroupBy(["tk","ma_kh"],["ps_no","ps_co"]);if(0<$check_hmcn$$.length)try{await Promise.all($check_hmcn$$.map($d$$=>asyncCheckHMCN($master$$.id_app,$master$$,$d$$.tk,$d$$.ma_kh,$d$$.ps_no,$d$$.ps_co)))}catch($e$$){return $callback$jscomp$0$$($e$$.message||$e$$.error||$e$$)}}async.map($details$jscomp$0$$,($detail$jscomp$0$$,$callback$$)=>{setImmediate(()=>{$detail$jscomp$0$$.ma_kh_no||($detail$jscomp$0$$.ma_kh_no=
$detail$jscomp$0$$.ma_kh||$master$$.ma_kh);$detail$jscomp$0$$.ma_kh_co||($detail$jscomp$0$$.ma_kh_co=$detail$jscomp$0$$.ma_kh||$master$$.ma_kh);checkData($master$$.id_app,$master$$.ngay_ct,$detail$jscomp$0$$,($e$$,$detail$$)=>{$callback$$($e$$,$detail$$)})})},($but_toan_loi_e$$,$details$$)=>{if($but_toan_loi_e$$)return $callback$jscomp$0$$($but_toan_loi_e$$);$but_toan_loi_e$$=$details$$.filter($detail$$=>isNaN($detail$$.tien_nt)||isNaN($detail$$.tien));0<$but_toan_loi_e$$.length&&console.error("Cac but toan khong hop ly",
$but_toan_loi_e$$,"cua chung tu",$master$$._id);let $dks$$=$details$$.filter($detail$$=>!(0===$detail$$.tien_nt&&0===$detail$$.tien)&&!isNaN($detail$$.tien_nt)&&!isNaN($detail$$.tien)).map(($detail$$,$nh_dk$$)=>{for(let $attr$$ in $Socai$$.schema.paths)"id_ct"!=$attr$$&&"_id"!=$attr$$&&"__v"!=$attr$$&&"date_updated"!=$attr$$&&"date_created"!=$attr$$&&(void 0!==$detail$$[$attr$$]&&null!==$detail$$[$attr$$]&&""!==$detail$$[$attr$$]||0===$detail$$[$attr$$]||void 0===$master$$[$attr$$]||($detail$$[$attr$$]=
$master$$[$attr$$]));$detail$$.nh_dk=$nh_dk$$;$detail$$.id_ct=$master$$._id.toString();delete $detail$$.toObject;delete $detail$$._id;delete $detail$$.__v;return $detail$$});$Socai$$.deleteMany({id_ct:$master$$._id.toString()},$error$$=>{if($error$$)return $callback$jscomp$0$$("Don't remove socai \n"+$error$$);if(0==$dks$$.length)return $callback$jscomp$0$$(null,[]);$Socai$$.create($dks$$,async($e$$,$msg_error_rs$$)=>{if($e$$)if($msg_error_rs$$=$e$$.message||$e$$.error||$e$$.toString(),0<=$msg_error_rs$$.indexOf("E11000 duplicate")&&
0<=$msg_error_rs$$.indexOf("id_ct_1_nh_dk_1"))if($msg_error_rs$$=await $Socai$$.find({id_ct:$master$$._id.toString()}),0<$msg_error_rs$$.length)$callback$jscomp$0$$(null,$msg_error_rs$$);else{console.error("Error save socai",$e$$,$master$$);let $q_remove$$={id_app:$master$$.id_app,id_ct:$master$$._id.toString()};$Socai$$.deleteMany($q_remove$$,($_e$$,$rs$$)=>{console.error("delete socai because error when posting",$master$$._id.toString(),JSON.stringify($rs$$),$q_remove$$);$callback$jscomp$0$$($e$$)})}else{console.error("Error save socai",
$e$$,$master$$);let $q_remove$$={id_app:$master$$.id_app,id_ct:$master$$._id.toString()};$Socai$$.deleteMany($q_remove$$,($_e$$,$rs$$)=>{console.error("delete socai because error when posting",$master$$._id.toString(),JSON.stringify($rs$$),$q_remove$$);$callback$jscomp$0$$($e$$)})}else if($master$$.ma_ct){let $contr$$=global.controllers[$master$$.ma_ct.toUpperCase()];$contr$$?await $contr$$.model.findById($master$$._id)?$callback$jscomp$0$$(null,$msg_error_rs$$):$Socai$$.deleteMany({id_ct:$master$$._id.toString()},
($_e$$,$rs$$)=>{console.error("Ch\u1ee9ng t\u1eeb kh\u00f4ng t\u1ed3n t\u1ea1i. X\u00f3a socai",$master$$._id,$rs$$);$callback$jscomp$0$$($e$$)}):(console.log("not found controller",$master$$.ma_ct.toUpperCase()),$callback$jscomp$0$$(null,$msg_error_rs$$))}else $callback$jscomp$0$$(null,$msg_error_rs$$)})})})};module.exports=post;