flexbiz-server
Version:
Flexible Server
41 lines (40 loc) • 19.9 kB
JavaScript
'use strict';const PostBook=require("../../libs/post-book"),PostSocai=require("../../libs/post-socai"),utils=require("../../libs/utils"),Sokho=global.getModel("sokho"),Sokhott=global.getModel("sokhott"),Vatra=global.getModel("vatra"),dmvt=global.getModel("dmvt"),dmkho=global.getModel("dmkho"),account=global.getModel("account"),customer=global.getModel("customer"),dmdt=global.getModel("dmdt"),dmphi=global.getModel("dmphi"),contract=global.getModel("contract"),model=global.getModel("hd3"),controller=
require("../../controllers/controller"),async=require("async"),ma_ct="HD3";
module.exports=function($contr_router$$){$contr_router$$=new controller($contr_router$$,model,ma_ct.toLowerCase(),{isVoucher:!0,check_stock:!1,sort:{ngay_ct:-1,so_ct:-1}});$contr_router$$.route();$contr_router$$.post=async function($obj$jscomp$0$$,$fn$$){$fn$$||($fn$$=function(){});$obj$jscomp$0$$.details.forEach($detail$$=>{$detail$$.tien_ct_nt||($detail$$.gia_ban_ct_nt=$detail$$.gia_ban_nt,$detail$$.tien_ct_nt=$detail$$.tien_nt,$detail$$.tien_ct=$detail$$.tien,$detail$$.tien_ck_nt=0,$detail$$.tien_ck=
0,$detail$$.thue_suat=$detail$$.thue_suat||0,$detail$$.tien_thue_nt=$detail$$.tien_thue_nt||0,$detail$$.gia_ban_nt=$detail$$.gia_ban_ct_nt*(1+$detail$$.thue_suat/100))});let $product_details$$=[...$obj$jscomp$0$$.details].map($detail$$=>$detail$$.combo&&0<$detail$$.combo.length?$detail$$.combo.map($item_combo$$=>{$item_combo$$=$item_combo$$.toObject?$item_combo$$.toObject():{...$item_combo$$};$item_combo$$.ma_kho=$detail$$.ma_kho;$item_combo$$.pn_gia_tb=$detail$$.pn_gia_tb;return $item_combo$$}):
[$detail$$]).reduce(($a$$,$b$$)=>$a$$.concat($b$$),[]),$details_thue$$=$obj$jscomp$0$$.details.map($detail$$=>({...$detail$$,thue_suat:($detail$$.ma_thue?$detail$$.thue_suat:$obj$jscomp$0$$.thue_suat)||0,ma_thue:$detail$$.ma_thue||$obj$jscomp$0$$.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$jscomp$0$$.id_app,"vat",{where:"ma_thue",fields:["tk_thue_co"]});
async.series({post_tdttno:$callback$jscomp$0$$=>{setImmediate(async()=>{var $hds_tdttno_tt$$=[...(new Set(($obj$jscomp$0$$.details||[]).filter($d$$=>$d$$.id_hd).map($d$$=>$d$$.id_hd)))];if(0==$hds_tdttno_tt$$.length)return $callback$jscomp$0$$();$hds_tdttno_tt$$=(await global.getModel("tdttno").getBalance({id_app:$obj$jscomp$0$$.id_app,id_hd:{$in:$hds_tdttno_tt$$}})).filter($i$$=>0<$i$$.con_lai_nt).map($invoice$$=>{let $t_invoice$$={sel:!1,id_tdtt:$invoice$$._id,line:$invoice$$.line,id_hd:$invoice$$.id_ct,
so_hd:$invoice$$.so_hd||$invoice$$.so_ct,ngay_hd:$invoice$$.ngay_hd||$invoice$$.ngay_ct,tien_hd_nt:$invoice$$.tien_nt};$t_invoice$$.ma_nt_hd=$invoice$$.ma_nt;$t_invoice$$.ty_gia_hd=$invoice$$.ty_gia;$t_invoice$$.ma_kh=$invoice$$.ma_kh;$t_invoice$$.dien_giai=$invoice$$.dien_giai;$t_invoice$$.tk_co=$invoice$$.tk_no;$t_invoice$$.da_thanh_toan_nt=$invoice$$.da_thanh_toan_nt;$t_invoice$$.da_thanh_toan=$invoice$$.da_thanh_toan;$t_invoice$$.con_lai_nt=$invoice$$.con_lai_nt;$t_invoice$$.con_lai=$invoice$$.con_lai;
$t_invoice$$.tien_nt=$obj$jscomp$0$$.details.filter($d$$=>$d$$.id_hd===$t_invoice$$.id_hd).map($d$$=>$d$$.tien_nt||0).reduce(($a$$,$b$$)=>$a$$+$b$$,0);$t_invoice$$.tien=$t_invoice$$.tien_nt*$obj$jscomp$0$$.ty_gia;$t_invoice$$.thanh_toan_qd=$t_invoice$$.tien/$t_invoice$$.ty_gia_hd;return $t_invoice$$});(new PostBook($obj$jscomp$0$$,$hds_tdttno_tt$$,global.getModel("tdttno_tt"),function($tdttno$$,$callback$$){$callback$$($tdttno$$)})).run(function($e$$,$details$$){if($e$$)return $callback$jscomp$0$$($e$$);
async.mapSeries($details$$,($detail$$,$callback$$)=>{$detail$$.con_lai_nt<=$detail$$.thanh_toan_qd?global.getModel("tdttno").findOneAndUpdate({_id:$detail$$.id_tdtt.toString()},{$set:{tat_toan:!0}},($error$$,$result$$)=>{$callback$$($error$$,$result$$)}):$callback$$(null,$detail$$)},($e$$,$rs$$)=>{$callback$jscomp$0$$($e$$,$rs$$)})})})},post_sokho:function($callback$jscomp$0$$){(new PostBook($obj$jscomp$0$$,$product_details$$,Sokho,async function($detail$$,$callback$$){$detail$$.nxt=1;$detail$$.tk_co=
$obj$jscomp$0$$.tk_co;$callback$$($detail$$)})).run(function($e$$,$rs$$){$callback$jscomp$0$$($e$$,$rs$$)})},post_sokhott:function($callback$jscomp$0$$){(new PostBook($obj$jscomp$0$$,$product_details$$,Sokhott,async function($detail$$,$callback$$){$detail$$.nxt=1;$detail$$.tk_co=$obj$jscomp$0$$.tk_co;$callback$$($detail$$)})).run(function($e$$,$rs$$){$callback$jscomp$0$$($e$$,$rs$$)})},post_vatra:function($callback$jscomp$0$$){(async()=>{$obj$jscomp$0$$.ngay_hd?0==$details_thue$$.length&&$obj$jscomp$0$$.so_hd&&
$obj$jscomp$0$$.ngay_hd&&$obj$jscomp$0$$.ma_thue&&$obj$jscomp$0$$.t_thue_nt?(new PostBook($obj$jscomp$0$$,[{...$obj$jscomp$0$$}],Vatra,function($obj$$,$callback$$){$obj$$.tk_du_thue=$obj$$.tk_co;$obj$$.t_tien_nt=-($obj$$.details.csum("tien_ct_nt")-$obj$$.details.csum("tien_ck_nt"));$obj$$.t_tien=-($obj$$.details.csum("tien_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$jscomp$0$$($e$$,$rs$$)}):$details_thue$$.asyncGroupBy(["ma_thue","thue_suat","ten_vt_vat","tk_thue_co"],"tien_ct_nt tien_ct tien_ck_nt tien_ck tien_thue tien_thue_nt tien_nt tien".split(" ")).then($ds$$=>{if(1==$ds$$.length){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);0==$tien_thue_nt$$&&0==$tien_thue$$&&($tien_thue_nt$$=$obj$jscomp$0$$.t_thue_nt||0,$tien_thue$$=
$obj$jscomp$0$$.t_thue||0);$ds$$[0].tien_thue_nt=$tien_thue_nt$$;$ds$$[0].tien_thue=$tien_thue$$}(new PostBook($obj$jscomp$0$$,$ds$$,Vatra,function($detail$$,$callback$$){try{$detail$$.tk_du_thue=$obj$jscomp$0$$.tk_co,$detail$$.t_tien_nt=-($detail$$.tien_ct_nt-$detail$$.tien_ck_nt),$detail$$.t_tien=-($detail$$.tien_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$jscomp$0$$.ten_vt||$obj$jscomp$0$$.dien_giai}catch($e$$){console.error($e$$,
$obj$jscomp$0$$)}$callback$$($detail$$)})).run(function($e$$,$rs$$){$callback$jscomp$0$$($e$$,$rs$$)})}):$callback$jscomp$0$$()})()},post_socai:function($callback$$){(async()=>{const $details_sc$$=[];if(0===$details_thue$$.length&&$obj$jscomp$0$$.t_thue_nt&&$obj$jscomp$0$$.tk_thue_co){const $o$$={...$obj$jscomp$0$$};$o$$.tk_co=$obj$jscomp$0$$.tk_co;$o$$.tk_no=$obj$jscomp$0$$.tk_thue_co;$o$$.tien_nt=Math.abs($obj$jscomp$0$$.t_thue_nt);$o$$.tien=Math.abs($obj$jscomp$0$$.t_thue);$o$$.dien_giai_thue&&
($o$$.dien_giai=$o$$.dien_giai_thue);$details_sc$$.push($o$$)}else $details_thue$$.forEach($v_vatra$$=>{$v_vatra$$={...$v_vatra$$};$v_vatra$$.tk_co=$obj$jscomp$0$$.tk_co;$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$jscomp$0$$.details.forEach(function($d$jscomp$9_detail$$){$d$jscomp$9_detail$$={...$d$jscomp$9_detail$$};$d$jscomp$9_detail$$.tien_ct_nt||($d$jscomp$9_detail$$.tien_ct_nt=$d$jscomp$9_detail$$.tien_nt,
$d$jscomp$9_detail$$.tien_ct=$d$jscomp$9_detail$$.tien);$d$jscomp$9_detail$$.tk_no=$d$jscomp$9_detail$$.tk_tl||"52131";$d$jscomp$9_detail$$.ma_kh||($d$jscomp$9_detail$$.ma_kh=$obj$jscomp$0$$.ma_kh);$d$jscomp$9_detail$$.tk_co=$obj$jscomp$0$$.tk_co||"1111";$d$jscomp$9_detail$$.tien_nt=$d$jscomp$9_detail$$.tien_ct_nt-($d$jscomp$9_detail$$.tien_ck_nt||0);$d$jscomp$9_detail$$.tien=$d$jscomp$9_detail$$.tien_ct-($d$jscomp$9_detail$$.tien_ck||0);$details_sc$$.push($d$jscomp$9_detail$$)});[...$product_details$$].forEach(function($d$jscomp$10_detail$$){$d$jscomp$10_detail$$=
{...$d$jscomp$10_detail$$};$d$jscomp$10_detail$$.tk_no=$d$jscomp$10_detail$$.tk_vt||"1561";$d$jscomp$10_detail$$.ma_kh||($d$jscomp$10_detail$$.ma_kh=$obj$jscomp$0$$.ma_kh);$d$jscomp$10_detail$$.tk_co=$d$jscomp$10_detail$$.tk_gv||"6321";$d$jscomp$10_detail$$.tien_nt=$d$jscomp$10_detail$$.tien_nhap_nt;$d$jscomp$10_detail$$.tien=$d$jscomp$10_detail$$.tien_nhap;$details_sc$$.push($d$jscomp$10_detail$$)});(await $obj$jscomp$0$$.details.filter($r$$=>$r$$.tien_hoa_hong_ctv&&$r$$.ma_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$$.ma_kh=$r$$.ma_ctv;$tt$$.tien=$tt$$.tien_nt=$r$$.tien_hoa_hong_ctv;$tt$$.tk_no&&$tt$$.tk_co&&$tt$$.tien&&$details_sc$$.push($tt$$)});(await $obj$jscomp$0$$.details.filter($r$$=>$r$$.tien_hoa_hong_ctv2&&$r$$.ma_ctv2).asyncGroupBy(["ma_ctv2","tk_cp_ctv","tk_cn_ctv"],["tien_hoa_hong_ctv2"])).forEach($r$$=>{let $tt$$={};$tt$$.tk_co=$r$$.tk_cp_ctv;$tt$$.tk_no=$r$$.tk_cn_ctv;
$tt$$.ma_kh=$r$$.ma_ctv2;$tt$$.tien=$tt$$.tien_nt=$r$$.tien_hoa_hong_ctv2;$tt$$.tk_no&&$tt$$.tk_co&&$tt$$.tien&&$details_sc$$.push($tt$$)});(await $obj$jscomp$0$$.details.filter($r$$=>$r$$.tien_hoa_hong&&$r$$.ma_kh_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$$.ma_kh=$r$$.ma_kh_hoa_hong;$tt$$.tien=$tt$$.tien_nt=$r$$.tien_hoa_hong;$tt$$.tien=$tt$$.tien_nt*
($obj$jscomp$0$$.ty_gia||1);$tt$$.check_balance=!1;$tt$$.tk_no&&$tt$$.tk_co&&$tt$$.tien&&$details_sc$$.push($tt$$)});(await $obj$jscomp$0$$.details.filter($r$$=>$r$$.tien_hoa_hong_gt&&$r$$.ma_kh_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$$.ma_kh=$r$$.ma_kh_hoa_hong_gt;$tt$$.tien_nt=$r$$.tien_hoa_hong_gt;$tt$$.tien=$tt$$.tien_nt*($obj$jscomp$0$$.ty_gia||
1);$tt$$.check_balance=!1;$tt$$.tk_no&&$tt$$.tk_co&&$tt$$.tien&&$details_sc$$.push($tt$$)});(await $obj$jscomp$0$$.details.filter($r$$=>$r$$.tien_hoa_hong_ql&&$r$$.ma_kh_hoa_hong_ql).asyncGroupBy(["ma_kh_hoa_hong_ql","tk_no_hoa_hong_ql","tk_co_hoa_hong_ql"],["tien_hoa_hong_ql"])).forEach($r$$=>{let $tt$$={};$tt$$.tk_co=$r$$.tk_no_hoa_hong_ql;$tt$$.tk_no=$r$$.tk_co_hoa_hong_ql;$tt$$.ma_kh=$r$$.ma_kh_hoa_hong_ql;$tt$$.tien_nt=$r$$.tien_hoa_hong_ql;$tt$$.tien=$tt$$.tien_nt*($obj$jscomp$0$$.ty_gia||1);
$tt$$.check_balance=!1;$tt$$.tk_no&&$tt$$.tk_co&&$tt$$.tien&&$details_sc$$.push($tt$$)});(await $obj$jscomp$0$$.details.filter($r$$=>$r$$.tien_hoa_hong_kho&&$r$$.ma_kh_hoa_hong_kho).asyncGroupBy(["ma_kh_hoa_hong_kho","tk_no_hoa_hong_kho","tk_co_hoa_hong_kho"],["tien_hoa_hong_kho"])).forEach($r$$=>{let $tt$$={};$tt$$.tk_co=$r$$.tk_no_hoa_hong_kho;$tt$$.tk_no=$r$$.tk_co_hoa_hong_kho;$tt$$.ma_kh=$r$$.ma_kh_hoa_hong_kho;$tt$$.tien_nt=$r$$.tien_hoa_hong_kho;$tt$$.tien=$tt$$.tien_nt*($obj$jscomp$0$$.ty_gia||
1);$tt$$.check_balance=!1;$tt$$.tk_no&&$tt$$.tk_co&&$tt$$.tien&&$details_sc$$.push($tt$$)});(new PostSocai($obj$jscomp$0$$,$details_sc$$)).run(function($e$$,$rs$$){$callback$$($e$$,$rs$$)})})()}},function($e$$,$rs$$){$fn$$($e$$,$rs$$)})};const $valid$$=function($f_tien_nt_i$$,$obj$$,$next$$){for($f_tien_nt_i$$=0;$f_tien_nt_i$$<$obj$$.details.length;$f_tien_nt_i$$++){const $detail$$=$obj$$.details[$f_tien_nt_i$$];$detail$$.tien_ct_nt||($detail$$.gia_ban_ct_nt=$detail$$.gia_ban_nt,$detail$$.tien_ct_nt=
$detail$$.tien_nt,$detail$$.tien_ck_nt=0,$detail$$.thue_suat=$detail$$.thue_suat||0,$detail$$.tien_thue_nt=$detail$$.tien_thue_nt||0,$detail$$.gia_ban_nt=$detail$$.gia_ban_ct_nt*(1+$detail$$.thue_suat/100));$detail$$.tien_ck_nt=$detail$$.tien_ck_nt||0;$detail$$.tien_thue_nt=$detail$$.tien_thue_nt||0;$detail$$.tien_nt=$detail$$.tien_ct_nt+$detail$$.tien_thue_nt-$detail$$.tien_ck_nt;$detail$$.line=$f_tien_nt_i$$;$detail$$.tien_ct=utils.round($detail$$.tien_ct_nt*$obj$$.ty_gia,0);$detail$$.tien=utils.round($detail$$.tien_nt*
$obj$$.ty_gia,0);$detail$$.tien_ck=utils.round($detail$$.tien_ck_nt*$obj$$.ty_gia,0);$detail$$.tien_thue=utils.round($detail$$.tien_thue_nt*$obj$$.ty_gia,0);$detail$$.tien_nhap=$detail$$.tien_nhap_nt}$obj$$.details.find($d$$=>$d$$.tien_thue_nt)?($obj$$.t_thue_nt=$obj$$.details.csum("tien_thue_nt"),$obj$$.t_thue=utils.round($obj$$.t_thue_nt*($obj$$.ty_gia||1),0)):$obj$$.thue_suat&&!$obj$$.t_thue_nt&&($f_tien_nt_i$$="VND"===$obj$$.ma_nt?0:2,$obj$$.t_thue_nt=utils.round(($obj$$.details.csum("tien_ct_nt")-
$obj$$.details.csum("tien_ck_nt"))*$obj$$.thue_suat/100,$f_tien_nt_i$$),$obj$$.t_thue=utils.round($obj$$.t_thue_nt*($obj$$.ty_gia||1),0));$next$$(null,$obj$$)};$contr_router$$.creating=function($user$$,$obj$$,$next$$){$obj$$.details||($obj$$.details=[]);$valid$$($user$$,$obj$$,async function($error$$){if($error$$)return $next$$($error$$);await $obj$$.details.asyncJoinModel2($obj$$.id_app,dmvt,{where:"ma_vt",fields:["combo"]});$obj$$.details.filter($d$$=>$d$$.combo).forEach($detail$$=>{$detail$$.combo=
$detail$$.combo.map($item_combo$$=>{$item_combo$$=$item_combo$$.toObject?$item_combo$$.toObject():{...$item_combo$$};$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=$item_combo$$.tien_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);return $item_combo$$})});$next$$(null,$obj$$)})};$contr_router$$.updating=function($user$$,$data$$,$obj$$,$next$$){$data$$.details=$data$$.details||$obj$$.details;$valid$$($user$$,$data$$,async function($error$$){if($error$$)return $next$$($error$$);await $data$$.details.asyncJoinModel2($obj$$.id_app,dmvt,{where:"ma_vt",setFields:($item$$,$ref$$)=>
{$item$$.combo&&0!=$item$$.combo.length?$ref$$.combo&&$ref$$.combo.forEach($c$$=>{let $cb$$=$item$$.combo.find($ci$$=>$ci$$.ma_vt===$c$$.ma_vt);$cb$$?$cb$$.sl_nhap_1_combo=$c$$.sl_xuat||0:$item$$.combo.push($c$$)}):$item$$.combo=$ref$$.combo}});$data$$.details.filter($d$$=>$d$$.combo).forEach($detail$$=>{$detail$$.combo=$detail$$.combo.map($item_combo$$=>{$item_combo$$=$item_combo$$.toObject?$item_combo$$.toObject():{...$item_combo$$};void 0==$item_combo$$.sl_nhap_1_combo&&($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=$item_combo$$.tien_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);return $item_combo$$})});$next$$(null,$data$$,$obj$$)})};$contr_router$$.view=function($user$$,$items$$,$fn$$){const $id_app$$=$user$$.current_id_app;async.series({tk:function($callback$$){$items$$.filter($item$$=>$item$$.tk_co).joinModel2($id_app$$,account,[{where:{tk_co:"tk"},fields:[{name:"ten_tk_co",value:"ten_tk"}]}],function(){$callback$$()})},kh:function($callback$$){$items$$.filter($item$$=>$item$$.ma_kh).joinModel2($id_app$$,customer,[{where:"ma_kh",fields:[{name:"ten_kh",value:"ten_kh"}]}],
function(){$callback$$()})},t_tien:function($callback$$){$items$$.forEach(function($r$$){$r$$.details||($r$$.details=[]);$r$$.details.forEach($d$$=>{if(!$d$$.tien_ct_nt){$d$$.gia_ban_ct_nt=$d$$.gia_ban_nt;$d$$.tien_ct_nt=$d$$.tien_nt;$d$$.tien_ck_nt=$d$$.tien_ck_nt||0;let $thue_suat$$=$d$$.thue_suat||0;$d$$.tien_thue_nt=$d$$.tien_thue_nt||0;$d$$.gia_ban_nt=$d$$.gia_ban_ct_nt*(1+$thue_suat$$/100);$d$$.tien_nt=$d$$.tien_ct_nt-$d$$.tien_ck_nt+$d$$.tien_thue_nt}});$r$$.t_thue_ra=$r$$.details.csum("tien_thue");
$r$$.t_thue_ra_nt=$r$$.details.csum("tien_thue_nt");$r$$.t_sl=$r$$.details.csum("sl_nhap");$r$$.t_ck=$r$$.details.csum("tien_ck");$r$$.t_ck_nt=$r$$.details.csum("tien_ck_nt");$r$$.t_tien=$r$$.details.csum("tien");$r$$.t_tien_nt=$r$$.details.csum("tien_nt");$r$$.t_tien_ct=$r$$.details.csum("tien_ct");$r$$.t_tien_ct_nt=$r$$.details.csum("tien_ct_nt");$r$$.t_tt=$r$$.t_tien_ct-$r$$.t_ck+$r$$.t_thue_ra;$r$$.t_tt_nt=$r$$.t_tien_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_hd2:function($callback$$){async.mapSeries($items$$,function($r$$,$callback1$$){($r$$.details||[]).filter($d$$=>$d$$.id_hd&&!$d$$.so_ct_hd).joinModel2($id_app$$,"hd2",[{where:{id_hd:"_id"},fields:[{name:"so_ct_hd",value:"so_ct"},{name:"ngay_ct_hd",value:"ngay_ct"},{name:"ma_ct_hd",value:"ma_ct"}]}],function(){$callback1$$()})},function(){$callback$$()})},details_pbl:function($callback$$){async.mapSeries($items$$,function($r$$,
$callback1$$){($r$$.details||[]).filter($d$$=>$d$$.id_hd&&!$d$$.so_ct_hd).joinModel2($id_app$$,"pbl",[{where:{id_hd:"_id"},fields:[{name:"so_ct_hd",value:"so_ct"},{name:"ngay_ct_hd",value:"ngay_ct"},{name:"ma_ct_hd",value:"ma_ct"}]}],function(){$callback1$$()})},function(){$callback$$()})},details_so1:function($callback$$){async.mapSeries($items$$,function($r$$,$callback1$$){($r$$.details||[]).filter($d$$=>$d$$.id_hd&&!$d$$.so_ct_hd).joinModel2($id_app$$,"so1",[{where:{id_hd:"_id"},fields:[{name:"so_ct_hd",
value:"so_ct"},{name:"ngay_ct_hd",value:"ngay_ct"},{name:"ma_ct_hd",value:"ma_ct"}]}],function(){$callback1$$()})},function(){$callback$$()})},details_tk:function($callback$$){async.mapSeries($items$$,function($r$$,$callback1$$){($r$$.details||[]).joinModel($id_app$$,account,[{akey:"tk_vt",bkey:"tk",fields:[{name:"ten_tk_vt",value:"ten_tk"}]},{akey:"tk_gv",bkey:"tk",fields:[{name:"ten_tk_gv",value:"ten_tk"}]},{akey:"tk_tl",bkey:"tk",fields:[{name:"ten_tk_tl",value:"ten_tk"}]}],function(){$callback1$$()})},
function(){$callback$$()})},details_vt:function($callback$$){async.mapSeries($items$$,function($r$$,$callback1$$){($r$$.details||[]).joinModel($id_app$$,dmvt,[{akey:"ma_vt",bkey:"ma_vt",fields:[{name:"ten_vt",value:"ten_vt"}]}],function(){$callback1$$()})},function(){$callback$$()})},details_tt1:function($callback$$){async.mapSeries($items$$,function($r$$,$callback1$$){($r$$.details||[]).filter($d$$=>$d$$.ma_tt1).joinModel2($id_app$$,"dmtt",[{where:{ma_vt:"ma_vt",ma_tt1:"ma_tt"},setFields:($item$$,
$ref$$)=>{$item$$.thuoc_tinh=$ref$$}}],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$$)})}};