flexbiz-server
Version:
Flexible Server
30 lines (29 loc) • 14.5 kB
JavaScript
const PostBook=require("../../libs/post-book"),PostSocai=require("../../libs/post-socai"),Options=global.getModel("options"),Sokho=global.getModel("sokho"),Sokhott=global.getModel("sokhott"),Vatra=global.getModel("vatra"),dvcs=global.getModel("dvcs"),dmvt=global.getModel("dmvt"),dmkho=global.getModel("dmkho"),customer=global.getModel("customer"),trangthai=global.getModel("trangthai"),dmdt=global.getModel("dmdt"),dmphi=global.getModel("dmphi"),contract=global.getModel("contract"),model=global.getModel("hd7"),
controller=require("../../controllers/controller"),ptthanhtoan=global.getModel("ptthanhtoan"),async=require("async"),ma_ct="HD7";
module.exports=function($contr_router$$){$contr_router$$=new controller($contr_router$$,model,ma_ct.toLowerCase(),{isVoucher:!0,sort:{ngay_ct:-1,so_ct:-1}});$contr_router$$.route();$contr_router$$.post=async function($_obj_details$$,$fn$$){let $obj$$={...$_obj_details$$};$fn$$||=function(){};$_obj_details$$=[...$obj$$.details];await $_obj_details$$.asyncJoinModel2($obj$$.id_app,dmvt,{where:"ma_vt",fields:["combo"]});let $data_post_kho$$=$_obj_details$$.map($detail$$=>$detail$$.combo&&$detail$$.combo.length>
0?$detail$$.combo.map($item_combo$$=>{$item_combo$$=$item_combo$$.toObject?$item_combo$$.toObject():{...$item_combo$$};$item_combo$$.sl_nhap_1_combo==void 0&&($item_combo$$.sl_nhap_1_combo=$item_combo$$.sl_xuat||0);delete $item_combo$$.sl_xuat;delete $item_combo$$.tien_xuat;delete $item_combo$$.tien_xuat_nt;delete $item_combo$$.px_gia_dd;$item_combo$$.sl_nhap=$item_combo$$.sl_nhap_1_combo*$detail$$.sl_nhap;$item_combo$$.tien_nhap=$item_combo$$.tien_nhap_nt=$item_combo$$.sl_nhap*($item_combo$$.gia_von_nt||
0);$item_combo$$.tien_hang=$item_combo$$.tien_hang_nt=$item_combo$$.sl_nhap*($item_combo$$.gia_ban_nt||0);$item_combo$$.tien_ck=$item_combo$$.tien_ck_nt=$item_combo$$.sl_nhap*($item_combo$$.tien_ck_nt||0);$item_combo$$.tien=$item_combo$$.tien_nt=$item_combo$$.tien_hang_nt-($item_combo$$.tien_ck_nt||0);$item_combo$$.ma_kho=$detail$$.ma_kho;return $item_combo$$}):[$detail$$]).reduce(($a$$,$b$$)=>$a$$.concat($b$$),[]),$details_thue$$=$obj$$.details.map($detail$$=>({...$detail$$,thue_suat:($detail$$.ma_thue?
$detail$$.thue_suat:$obj$$.thue_suat)||0,ma_thue:$detail$$.ma_thue||$obj$$.ma_thue,tien_thue_nt:$detail$$.tien_thue_nt||0,tien_thue:$detail$$.tien_thue||0})).filter($d$$=>$d$$.ma_thue);await $details_thue$$.filter($r$$=>!$r$$.tk_thue_co&&$r$$.ma_thue).asyncJoinModel2($obj$$.id_app,"vat",{where:"ma_thue",fields:["tk_thue_co"]});async.series({post_sokho:function($callback$$){(new PostBook($obj$$,$data_post_kho$$,Sokho,async function($detail$$,$callback$$){$detail$$.nxt=1;$detail$$.tk_co=$obj$$.tk_co;
$callback$$($detail$$)})).run(function($e$$,$rs$$){$callback$$($e$$,$rs$$)})},post_sokhott:function($callback$$){(new PostBook($obj$$,$data_post_kho$$,Sokhott,async function($detail$$,$callback$$){$detail$$.nxt=1;$detail$$.tk_co=$obj$$.tk_co;$callback$$($detail$$)})).run(function($e$$,$rs$$){$callback$$($e$$,$rs$$)})},post_vatra:function($callback$$){(async()=>{$obj$$.ngay_hd?$details_thue$$.length==0&&$obj$$.so_hd&&$obj$$.ngay_hd&&$obj$$.ma_thue&&$obj$$.t_thue_nt?(new PostBook($obj$$,[{...$obj$$}],
Vatra,function($obj$$,$callback$$){$obj$$.tk_du_thue=$obj$$.tk_co;$obj$$.t_tien_nt=-($obj$$.details.csum("tien_hang_ct_nt")-$obj$$.details.csum("tien_ck_nt"));$obj$$.t_tien=-($obj$$.details.csum("tien_hang_ct")-$obj$$.details.csum("tien_ck"));$obj$$.t_thue_nt=0-$obj$$.t_thue_nt;$obj$$.t_thue=0-$obj$$.t_thue;$obj$$.ten_vt||($obj$$.ten_vt=$obj$$.dien_giai);$callback$$($obj$$)})).run(function($e$$,$rs$$){$callback$$($e$$,$rs$$)}):$details_thue$$.asyncGroupBy(["ma_thue","thue_suat","ten_vt_vat","tk_thue_co"],
"tien_hang_ct_nt tien_hang_ct tien_ck_nt tien_ck tien_thue tien_thue_nt tien_nt tien".split(" ")).then($ds$$=>{if($ds$$.length==1){let $tien_thue_nt$$=$ds$$.map($d$$=>$d$$.tien_thue_nt).reduce(($a$$,$b$$)=>$a$$+$b$$,0),$tien_thue$$=$ds$$.map($d$$=>$d$$.tien_thue).reduce(($a$$,$b$$)=>$a$$+$b$$,0);$tien_thue_nt$$==0&&$tien_thue$$==0&&($tien_thue_nt$$=$obj$$.t_thue_nt||0,$tien_thue$$=$obj$$.t_thue||0);$ds$$[0].tien_thue_nt=$tien_thue_nt$$;$ds$$[0].tien_thue=$tien_thue$$}(new PostBook($obj$$,$ds$$,Vatra,
function($detail$$,$callback$$){try{$detail$$.tk_du_thue=$obj$$.tk_co,$detail$$.t_tien_nt=-($detail$$.tien_hang_ct_nt-$detail$$.tien_ck_nt),$detail$$.t_tien=-($detail$$.tien_hang_ct-$detail$$.tien_ck),$detail$$.t_thue_nt=0-$detail$$.tien_thue_nt,$detail$$.t_thue=0-$detail$$.tien_thue,$detail$$.ten_vt=$detail$$.ten_vt_vat||$obj$$.ten_vt||$obj$$.dien_giai}catch($e$$){Logger.error($e$$,$obj$$)}$callback$$($detail$$)})).run(function($e$$,$rs$$){$callback$$($e$$,$rs$$)})}):$callback$$()})()},post_socai:function($callback$$){async.series({options:$callback$$=>
{Options.findOne({id_app:$obj$$.id_app,id_func:ma_ct.toLowerCase()}).lean().exec(function($error$$,$rs$$){$callback$$(null,$rs$$||{})})},kho:$callback$$=>{dmkho.findOne({id_app:$obj$$.id_app,ma_kho:$obj$$.ma_kho}).lean().exec(($e$$,$kho$$)=>{$callback$$(null,$kho$$||{})})},tk:$callback$$=>{$obj$$.details.joinModel2($obj$$.id_app,dmvt,[{where:"ma_vt",fields:[{tk_vt_def:"tk_vt"},{tk_gv_def:"tk_gv"},{tk_tl_def:"tk_tl"},{tk_dt_def:"tk_dt"}]}],function(){$obj$$.details.forEach($d$$=>{$d$$.toObject&&delete $d$$.toObject});
$callback$$()})},ptthanhtoan:$callback$$=>{ptthanhtoan.findOne({id_app:$obj$$.id_app,_id:$obj$$.pt_thanh_toan}).lean().exec(($e$$,$pt$$)=>{$callback$$(null,$pt$$||{})})}},async($e$jscomp$7_kho$$,$ma_kh_o_pt$jscomp$1_rs$$)=>{let $option$$=$ma_kh_o_pt$jscomp$1_rs$$.options;$e$jscomp$7_kho$$=$ma_kh_o_pt$jscomp$1_rs$$.kho;$ma_kh_o_pt$jscomp$1_rs$$=$ma_kh_o_pt$jscomp$1_rs$$.ptthanhtoan;$option$$.tk_tien=$ma_kh_o_pt$jscomp$1_rs$$.tk_tien||$e$jscomp$7_kho$$.tk_tien||$option$$.tk_tien;$option$$.tk_cn=$ma_kh_o_pt$jscomp$1_rs$$.tk_cn||
$e$jscomp$7_kho$$.tk_cn||$option$$.tk_cn;$option$$.tk_dt=$ma_kh_o_pt$jscomp$1_rs$$.tk_dt||$e$jscomp$7_kho$$.tk_dt||$option$$.tk_dt;$option$$.tk_tl=$ma_kh_o_pt$jscomp$1_rs$$.tk_tl||$e$jscomp$7_kho$$.tk_tl||$option$$.tk_tl;$option$$.tk_ck=$ma_kh_o_pt$jscomp$1_rs$$.tk_ck||$e$jscomp$7_kho$$.tk_ck||$option$$.tk_ck;$option$$.tk_gv=$ma_kh_o_pt$jscomp$1_rs$$.tk_gv||$e$jscomp$7_kho$$.tk_gv||$option$$.tk_gv;$option$$.tk_vt=$ma_kh_o_pt$jscomp$1_rs$$.tk_vt||$e$jscomp$7_kho$$.tk_vt||$option$$.tk_vt;if($ma_kh_o_pt$jscomp$1_rs$$=
$ma_kh_o_pt$jscomp$1_rs$$.ma_kh||$e$jscomp$7_kho$$.ma_kh)$option$$.ma_kh=$ma_kh_o_pt$jscomp$1_rs$$,$option$$.tk_tien=$option$$.tk_cn;const $details_sc$$=[];$details_thue$$.length===0&&$obj$$.t_thue_nt&&$obj$$.tk_thue_co?($ma_kh_o_pt$jscomp$1_rs$$={...$obj$$},$ma_kh_o_pt$jscomp$1_rs$$.tk_co=$obj$$.tk_co||$option$$.tk_tien,$ma_kh_o_pt$jscomp$1_rs$$.tk_no=$obj$$.tk_thue_co,$ma_kh_o_pt$jscomp$1_rs$$.tien_nt=Math.abs($obj$$.t_thue_nt),$ma_kh_o_pt$jscomp$1_rs$$.tien=Math.abs($obj$$.t_thue),$ma_kh_o_pt$jscomp$1_rs$$.dien_giai_thue&&
($ma_kh_o_pt$jscomp$1_rs$$.dien_giai=$ma_kh_o_pt$jscomp$1_rs$$.dien_giai_thue),$details_sc$$.push($ma_kh_o_pt$jscomp$1_rs$$)):$details_thue$$.forEach($v_vatra$$=>{$v_vatra$$={...$v_vatra$$};$v_vatra$$.tk_co=$obj$$.tk_co||$option$$.tk_tien;$v_vatra$$.tk_no=$v_vatra$$.tk_thue_co;$v_vatra$$.tien_nt=$v_vatra$$.tien_thue_nt;$v_vatra$$.tien=$v_vatra$$.tien_thue;$details_sc$$.push($v_vatra$$)});$obj$$.details.forEach(function($d$jscomp$6_detail$$){$d$jscomp$6_detail$$=Object.assign({},$d$jscomp$6_detail$$);
$d$jscomp$6_detail$$.tk_no=$option$$.tk_tl||$d$jscomp$6_detail$$.tk_tl_def;$d$jscomp$6_detail$$.ma_kh||($d$jscomp$6_detail$$.ma_kh=$obj$$.ma_kh);$d$jscomp$6_detail$$.tk_co=$obj$$.tk_co||$option$$.tk_tien;$d$jscomp$6_detail$$.tien_nt=$d$jscomp$6_detail$$.tien_hang_ct_nt-$d$jscomp$6_detail$$.tien_ck_nt;$d$jscomp$6_detail$$.tien=$d$jscomp$6_detail$$.tien_hang_ct-$d$jscomp$6_detail$$.tien_ck;$details_sc$$.push($d$jscomp$6_detail$$)});(await $obj$$.details.filter($r$$=>$r$$.tien_hoa_hong_ctv).asyncGroupBy(["ma_ctv",
"tk_cp_ctv","tk_cn_ctv"],["tien_hoa_hong_ctv"])).forEach($r$$=>{let $tt$$={};$tt$$.tk_co=$r$$.tk_cp_ctv;$tt$$.tk_no=$r$$.tk_cn_ctv;$tt$$.tien=$tt$$.tien_nt=$r$$.tien_hoa_hong_ctv;$tt$$.ma_kh=$tt$$.ma_ctv;$tt$$.tk_no&&$tt$$.tk_co&&$tt$$.tien&&$details_sc$$.push($tt$$)});(await $obj$$.details.filter($r$$=>$r$$.tien_hoa_hong).asyncGroupBy(["ma_kh_hoa_hong","tk_no_hoa_hong","tk_co_hoa_hong"],["tien_hoa_hong"])).forEach($r$$=>{let $tt$$={};$tt$$.tk_co=$r$$.tk_no_hoa_hong;$tt$$.tk_no=$r$$.tk_co_hoa_hong;
$tt$$.tien=$tt$$.tien_nt=$r$$.tien_hoa_hong;$tt$$.tien=$tt$$.tien_nt*($obj$$.ty_gia||1);$tt$$.ma_kh=$tt$$.ma_kh_hoa_hong;$tt$$.check_balance=!1;$tt$$.tk_no&&$tt$$.tk_co&&$tt$$.tien&&$details_sc$$.push($tt$$)});(await $obj$$.details.filter($r$$=>$r$$.tien_hoa_hong_gt).asyncGroupBy(["ma_kh_hoa_hong_gt","tk_no_hoa_hong_gt","tk_co_hoa_hong_gt"],["tien_hoa_hong_gt"])).forEach($r$$=>{let $tt$$={};$tt$$.tk_co=$r$$.tk_no_hoa_hong_gt;$tt$$.tk_no=$r$$.tk_co_hoa_hong_gt;$tt$$.tien_nt=$r$$.tien_hoa_hong_gt;$tt$$.tien=
$tt$$.tien_nt*($obj$$.ty_gia||1);$tt$$.ma_kh=$tt$$.ma_kh_hoa_hong_gt;$tt$$.check_balance=!1;$tt$$.tk_no&&$tt$$.tk_co&&$tt$$.tien&&$details_sc$$.push($tt$$)});$obj$$.details.forEach(function($d$jscomp$7_detail$$){$d$jscomp$7_detail$$=Object.assign({},$d$jscomp$7_detail$$);$d$jscomp$7_detail$$.tk_no=$option$$.tk_vt||$d$jscomp$7_detail$$.tk_vt_def;$d$jscomp$7_detail$$.ma_kh||($d$jscomp$7_detail$$.ma_kh=$obj$$.ma_kh);$d$jscomp$7_detail$$.tk_co=$option$$.tk_gv||$d$jscomp$7_detail$$.tk_gv_def;$d$jscomp$7_detail$$.tien_nt=
$d$jscomp$7_detail$$.tien_nhap_nt;$d$jscomp$7_detail$$.tien=$d$jscomp$7_detail$$.tien_nhap;$details_sc$$.push($d$jscomp$7_detail$$)});$option$$.ma_kh&&$details_sc$$.forEach($d$$=>{$d$$.ma_kh=$option$$.ma_kh});(new PostSocai($obj$$,$details_sc$$)).run(function($e$$,$rs$$){$callback$$($e$$,$rs$$)})})}},function($e$$,$rs$$){$fn$$($e$$,$rs$$)})};const $valid$$=function($i_user$$,$obj$$,$next$$){for($i_user$$=0;$i_user$$<$obj$$.details.length;$i_user$$++){let $detail$$=$obj$$.details[$i_user$$];$detail$$.line=
$i_user$$;$detail$$.tien_nt=$detail$$.tien_hang_ct_nt-$detail$$.tien_ck_nt+($detail$$.tien_thue_nt||0);$detail$$.tien=$detail$$.tien_hang-$detail$$.tien_ck+($detail$$.tien_thue||0);$obj$$.ma_nt=="VND"&&($detail$$.tien_hang_ct=$detail$$.tien_hang_ct_nt=Math.roundBy($detail$$.tien_hang_ct_nt||0),$detail$$.tien_hang=$detail$$.tien_hang_nt=Math.roundBy($detail$$.tien_hang_nt||0),$detail$$.tien=$detail$$.tien_nt=Math.roundBy($detail$$.tien_nt||0),$detail$$.tien_ck=$detail$$.tien_ck_nt=Math.roundBy($detail$$.tien_ck_nt||
0),$detail$$.tien_thue=$detail$$.tien_thue_nt=Math.roundBy($detail$$.tien_thue_nt||0),$detail$$.tien_nhap=$detail$$.tien_nhap_nt=Math.roundBy($detail$$.tien_nhap_nt||0))}$next$$(null,$obj$$)};$contr_router$$.creating=function($user$$,$obj$$,$next$$){$obj$$.details||($obj$$.details=[]);$valid$$($user$$,$obj$$,function($error$$){if($error$$)return $next$$($error$$);$next$$(null,$obj$$)})};$contr_router$$.updating=function($user$$,$data$$,$obj$$,$next$$){$valid$$($user$$,$data$$,function($error$$){if($error$$)return $next$$($error$$);
$next$$(null,$data$$,$obj$$)})};$contr_router$$.view=function($user$$,$items$$,$fn$$){const $id_app$$=$user$$.current_id_app;async.series({dv:function($callback$$){$items$$.filter($item$$=>$item$$.ma_dvcs).joinModel($id_app$$,dvcs,[{akey:"ma_dvcs",bkey:"_id",fields:[{name:"ten_dvcs",value:"ten_dvcs"}]}],function(){$callback$$()})},trangthai:function($callback$$){$items$$.filter($item$$=>$item$$.trang_thai).joinModel2(null,trangthai,[{where:{trang_thai:"ma_trang_thai",ma_ct:"ma_ct"},fields:["ten_trang_thai",
"color"]}],function(){$callback$$()})},kh:function($callback$$){$items$$.filter($item$$=>$item$$.ma_kh).joinModel($id_app$$,customer,[{akey:"ma_kh",bkey:"ma_kh",fields:[{name:"ten_kh",value:"ten_kh"}]}],function(){$callback$$()})},dich_vu_giao_hang:function($callback$$){$items$$.filter($item$$=>$item$$.dich_vu_giao_hang).joinModel($id_app$$,customer,[{akey:"dich_vu_giao_hang",bkey:"ma_kh",fields:[{name:"ten_dich_vu_giao_hang",value:"ten_kh"}]}],function(){$callback$$()})},ptthanhtoan:function($callback$$){$items$$.filter($item$$=>
$item$$.pt_thanh_toan).joinModel2($id_app$$,ptthanhtoan,[{where:{pt_thanh_toan:"_id"},fields:{ten_pt_thanh_toan:"ten"}}],function(){$callback$$()})},t_tien:function($callback$$){$items$$.forEach(function($r$$){$r$$.details&&($r$$.t_sl=$r$$.details.csum("sl_nhap"),$r$$.t_tien_hang=$r$$.details.csum("tien_hang"),$r$$.t_tien_hang_nt=$r$$.details.csum("tien_hang_nt"),$r$$.t_tien_hang_ct=$r$$.details.csum("tien_hang_ct"),$r$$.t_tien_hang_ct_nt=$r$$.details.csum("tien_hang_ct_nt"),$r$$.t_ck=$r$$.details.csum("tien_ck"),
$r$$.t_ck_nt=$r$$.details.csum("tien_ck_nt"),$r$$.t_thue_ra=$r$$.details.csum("tien_thue"),$r$$.t_thue_ra_nt=$r$$.details.csum("tien_thue_nt"),$r$$.t_tt=$r$$.t_tien_hang_ct-$r$$.t_ck+$r$$.t_thue_ra,$r$$.t_tt_nt=$r$$.t_tien_hang_ct_nt-$r$$.t_ck_nt+$r$$.t_thue_ra_nt,$r$$.t_tien_nhap=$r$$.details.csum("tien_nhap"),$r$$.t_tien_nhap_nt=$r$$.details.csum("tien_nhap_nt"))});$callback$$()},details_vt:function($callback$$){async.mapSeries($items$$,function($r$$,$callback1$$){($r$$.details||[]).joinModel2($id_app$$,
dmvt,[{where:"ma_vt",fields:"ten_vt picture chieu_cao chieu_rong chieu_sau trong_luong".split(" ")}],function(){$callback1$$()})},function(){$callback$$()})},details_kho:function($callback$$){async.mapSeries($items$$,function($r$$,$callback1$$){($r$$.details||[]).filter($item$$=>$item$$.ma_kho).joinModel($id_app$$,dmkho,[{akey:"ma_kho",bkey:"ma_kho",fields:[{name:"ten_kho",value:"ten_kho"}]}],function(){$callback1$$()})},function(){$callback$$()})},details_dt:function($callback$$){async.mapSeries($items$$,
function($r$$,$callback1$$){($r$$.details||[]).filter($item$$=>$item$$.ma_dt).joinModel($id_app$$,dmdt,[{akey:"ma_dt",bkey:"ma_dt",fields:[{name:"ten_dt",value:"ten_dt"}]}],function(){$callback1$$()})},function(){$callback$$()})},details_contract:function($callback$$){async.mapSeries($items$$,function($r$$,$callback1$$){($r$$.details||[]).filter($item$$=>$item$$.ma_hd).joinModel($id_app$$,contract,[{akey:"ma_hd",bkey:"so_hd",fields:[{name:"ten_hd",value:"ten_hd"}]}],function(){$callback1$$()})},function(){$callback$$()})},
details_phi:function($callback$$){async.mapSeries($items$$,function($r$$,$callback1$$){($r$$.details||[]).filter($item$$=>$item$$.ma_phi).joinModel($id_app$$,dmphi,[{akey:"ma_phi",bkey:"ma_phi",fields:[{name:"ten_phi",value:"ten_phi"}]}],function(){$callback1$$()})},function(){$callback$$()})}},function(){$fn$$(null,$items$$)})}};