flexbiz-server
Version:
Flexible Server
42 lines (41 loc) • 20.9 kB
JavaScript
'use strict';const PostBook=require("../../libs/post-book"),PostSocai=require("../../libs/post-socai"),Tdttno=global.getModel("tdttno"),Tdttno_tt=global.getModel("tdttno_tt"),Vatra=global.getModel("vatra"),dvcs=global.getModel("dvcs"),account=global.getModel("account"),customer=global.getModel("customer"),dmnv=global.getModel("dmnv"),dmdt=global.getModel("dmdt"),dmphi=global.getModel("dmphi"),contract=global.getModel("contract"),salecontract=global.getModel("sale_contract"),model=global.getModel("hde"),
controller=require("../../controllers/controller"),trangthai=global.getModel("trangthai"),utils=require("../../libs/utils"),async=require("async"),ma_ct="HDE",App=global.getModel("app"),axios=require("axios"),Moment=require("moment");
module.exports=function($router$$){const $contr$$=new controller($router$$,model,ma_ct.toLowerCase(),{isVoucher:!0,sort:{ngay_ct:-1,so_ct:-1}});$contr$$.route();$contr$$.post=function($obj$$,$fn$$){$fn$$||($fn$$=function(){});async.series({post_socai:function($callback$$){const $details_sc$$=[];$obj$$.details.forEach(function($d_detail$$){$d_detail$$={...$d_detail$$};$d_detail$$.tk_co=$d_detail$$.tk_dt;$d_detail$$.ma_kh||($d_detail$$.ma_kh=$obj$$.ma_kh);$d_detail$$.tk_no=$obj$$.tk_no;$details_sc$$.push($d_detail$$)});
$obj$$.details.forEach(function($d$jscomp$1_detail$$){$d$jscomp$1_detail$$={...$d$jscomp$1_detail$$};$d$jscomp$1_detail$$.tk_no=$d$jscomp$1_detail$$.tk_ck;$d$jscomp$1_detail$$.ma_kh||($d$jscomp$1_detail$$.ma_kh=$obj$$.ma_kh);$d$jscomp$1_detail$$.tk_co=$obj$$.tk_no;$d$jscomp$1_detail$$.tien_nt=$d$jscomp$1_detail$$.tien_ck_nt;$d$jscomp$1_detail$$.tien=$d$jscomp$1_detail$$.tien_ck;$d$jscomp$1_detail$$.check_balance=!1;$details_sc$$.push($d$jscomp$1_detail$$)});const $o$$={...$obj$$};$o$$.tk_co=$o$$.tk_thue_co;
$o$$.tien_nt=$obj$$.details.csum("tien_thue_nt");$o$$.tien=$obj$$.details.csum("tien_thue");$o$$.dien_giai_thue&&($o$$.dien_giai=$o$$.dien_giai_thue);$details_sc$$.push($o$$);(new PostSocai($obj$$,$details_sc$$)).run(function($e$$,$rs$$){$callback$$($e$$,$rs$$)})},post_tdttno:function($callback$jscomp$0$$){account.findOne({tk:$obj$$.tk_no,id_app:$obj$$.id_app},{tk_cn:1},function($error$jscomp$0$$,$tk$$){if($error$jscomp$0$$)return $callback$jscomp$0$$($error$jscomp$0$$);$tk$$&&!0===$tk$$.tk_cn?(new PostBook($obj$$,
[{...$obj$$}],Tdttno,function($detail$$,$callback$$){try{$detail$$.so_hd||($detail$$.so_hd=$obj$$.so_ct),$detail$$.ngay_hd||($detail$$.ngay_hd=$obj$$.ngay_ct),$detail$$.tien_nt=$obj$$.details.csum("tien_nt")-$obj$$.details.csum("tien_ck_nt")+$obj$$.details.csum("tien_thue_nt"),$detail$$.tien=$obj$$.details.csum("tien")-$obj$$.details.csum("tien_ck")+$obj$$.details.csum("tien_thue")}catch($e$$){console.error($e$$,$obj$$)}$callback$$($detail$$)})).run(function($e$$,$rs$$){$callback$jscomp$0$$($e$$,
$rs$$)}):Tdttno.deleteMany({id_ct:$obj$$._id},function($error$$){$callback$jscomp$0$$($error$$)})})},post_vatra:function($callback$jscomp$0$$){$obj$$.so_hd&&$obj$$.ngay_hd?$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})).filter($d$$=>$d$$.ma_thue).asyncGroupBy(["ma_thue","thue_suat","ten_vt_vat"],"tien_nt tien tien_ck tien_ck_nt tien_thue tien_thue_nt".split(" ")).then($ds$$=>{if(1==$ds$$.length){let $tien_thue_nt$$=
$ds$$.map($d$$=>$d$$.tien_thue_nt||0).reduce(($a$$,$b$$)=>$a$$+$b$$,0),$tien_thue$$=$ds$$.map($d$$=>$d$$.tien_thue||0).reduce(($a$$,$b$$)=>$a$$+$b$$,0);0==$tien_thue_nt$$&&0==$tien_thue$$&&($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_no,$detail$$.t_tien_nt=$detail$$.tien_nt-$detail$$.tien_ck_nt,$detail$$.t_tien=
$detail$$.tien-$detail$$.tien_ck,$detail$$.t_thue_nt=$detail$$.tien_thue_nt,$detail$$.t_thue=$detail$$.tien_thue,$detail$$.ten_vt=$detail$$.ten_vt_vat||$obj$$.ten_vt||$obj$$.dien_giai}catch($e$$){console.error($e$$,$obj$$)}$callback$$($detail$$)})).run(function($e$$,$rs$$){$callback$jscomp$0$$($e$$,$rs$$)})}):$callback$jscomp$0$$()}},function($e$$,$rs$$){$fn$$($e$$,$rs$$)})};const $valid$$=async($t_thue_nt_user$$,$obj$$,$next$$,$old_obj_t_thue$$)=>{for(var $i_row_dieu_chinh$$=0;$i_row_dieu_chinh$$<
$obj$$.details.length;$i_row_dieu_chinh$$++){const $detail$$=$obj$$.details[$i_row_dieu_chinh$$];if(!$detail$$.ma_evoucher)return $next$$("B\u1ea1n ch\u01b0a nh\u1eadp m\u00e3 evoucher");var $e$jscomp$0$$=await global.getModel("evoucher").findOne({ma:$detail$$.ma_evoucher,for_sell:!0});if(!$e$jscomp$0$$)return $next$$("M\u00e3 evoucher "+$detail$$.ma_evoucher+" kh\u00f4ng t\u1ed3n t\u1ea1i ho\u1eb7c kh\u00f4ng \u0111\u1ec3 b\u00e1n");if($e$jscomp$0$$.id_ct_used||$e$jscomp$0$$.id_ct_useds&&0<$e$jscomp$0$$.id_ct_useds.length)return $next$$("M\u00e3 evoucher "+
$detail$$.ma_evoucher+" \u0111\u00e3 \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng");if(($e$jscomp$0$$=await model.findOne({id_app:$t_thue_nt_user$$.current_id_app,"details.ma_evoucher":$detail$$.ma_evoucher}).lean())&&$e$jscomp$0$$._id!=$old_obj_t_thue$$._id)return $next$$("M\u00e3 evoucher "+$detail$$.ma_evoucher+" \u0111\u00e3 \u0111\u01b0\u1ee3c b\u00e1n");$detail$$.line=$i_row_dieu_chinh$$;"VND"===$obj$$.ma_nt&&($detail$$.tien=$detail$$.tien_nt,$detail$$.tien_ck=$detail$$.tien_ck_nt);$detail$$.tien_ck=
$detail$$.tien_ck||0;$detail$$.tien_ck_nt=$detail$$.tien_ck_nt||0;$detail$$.tien=$detail$$.tien||0;$detail$$.tien_nt=$detail$$.tien_nt||0}let $f_tien_nt$$="VND"===$obj$$.ma_nt?0:2;$obj$$.thue_suat=$obj$$.thue_suat||0;$obj$$.details.forEach($detail$$=>{let $thue_suat$$=($detail$$.ma_thue?$detail$$.thue_suat:$obj$$.thue_suat)||0;$detail$$.tien_thue=utils.round(($detail$$.tien-$detail$$.tien_ck)*$thue_suat$$/100,0);$detail$$.tien_thue_nt=utils.round(($detail$$.tien_nt-$detail$$.tien_ck_nt)*$thue_suat$$/
100,$f_tien_nt$$)});$obj$$.details.find($r$$=>$r$$.ma_thue)||void 0==$obj$$.thue_suat||($obj$$.t_thue_nt=utils.round(($obj$$.details.csum("tien_nt")-$obj$$.details.csum("tien_ck_nt"))*$obj$$.thue_suat/100,$f_tien_nt$$),$obj$$.t_thue=utils.round($obj$$.t_thue_nt*($obj$$.ty_gia||1),0),$t_thue_nt_user$$=$obj$$.details.csum("tien_thue_nt"),$old_obj_t_thue$$=$obj$$.details.csum("tien_thue"),($t_thue_nt_user$$!==$obj$$.t_thue_nt||$old_obj_t_thue$$!==$obj$$.t_thue)&&0<$obj$$.details.length&&($i_row_dieu_chinh$$=
$obj$$.details[$obj$$.details.length-1],$i_row_dieu_chinh$$.tien_thue_nt+=$obj$$.t_thue_nt-$t_thue_nt_user$$,$i_row_dieu_chinh$$.tien_thue+=$obj$$.t_thue-$old_obj_t_thue$$));$obj$$.id_contract?salecontract.findOne({_id:$obj$$.id_contract}).lean().exec(($e$$,$c$$)=>{$c$$&&($obj$$.ma_hd=$c$$.ma_hd);$next$$(null,$obj$$)}):$next$$(null,$obj$$)};$contr$$.creating=function($user$$,$obj$$,$next$$){$obj$$.details||($obj$$.details=[]);$valid$$($user$$,$obj$$,function($error$$){if($error$$)return $next$$($error$$);
$obj$$.result_export_einvoice=null;$next$$(null,$obj$$)},$obj$$)};$contr$$.created=async function($user$$,$obj$$,$next$$){let $of_user$$="";$obj$$.ma_kh&&($of_user$$=(await customer.findOne({id_app:$obj$$.id_app,ma_kh:$obj$$.ma_kh}).lean()||{}).of_user||"");$obj$$.details.forEach($d$$=>{global.getModel("evoucher").findOneAndUpdate({id_app:$user$$.current_id_app,ma:$d$$.ma_evoucher},{id_sell:$obj$$._id.toString(),user:$of_user$$})});$next$$(null,$obj$$)};$contr$$.updating=function($user$$,$data$$,
$obj$$,$next$$){Tdttno_tt.findOne({id_hd:$obj$$._id},function($error$jscomp$0$$,$ct_tt$$){if($error$jscomp$0$$)return $next$$($error$jscomp$0$$);$ct_tt$$?$next$$(`Kh\u00f4ng th\u1ec3 c\u1eadp nh\u1eadt phi\u1ebfu n\u00e0y do \u0111\u00e3 \u0111\u01b0\u1ee3c thanh to\u00e1n b\u1edfi ${$ct_tt$$.ma_ct||"ch\u1ee9ng t\u1eeb"}: ${$ct_tt$$.so_ct}, ng\u00e0y ${Moment($ct_tt$$.ngay_ct).format("DD/MM/YYYY")}`):$valid$$($user$$,$data$$,function($error$$){if($error$$)return $next$$($error$$);$next$$(null,$data$$,
$obj$$)},$obj$$)})};$contr$$.updated=async function($user$$,$obj$$,$next$$){global.getModel("evoucher").updateMany({id_app:$user$$.current_id_app,id_sell:$obj$$._id.toString()},{id_sell:"",user:""});let $of_user$$="";$obj$$.ma_kh&&($of_user$$=(await customer.findOne({id_app:$obj$$.id_app,ma_kh:$obj$$.ma_kh}).lean()||{}).of_user||"");$obj$$.details.forEach($d$$=>{global.getModel("evoucher").findOneAndUpdate({id_app:$user$$.current_id_app,ma:$d$$.ma_evoucher},{id_sell:$obj$$._id.toString(),user:$of_user$$})});
$next$$(null,$obj$$)};$contr$$.deleting=function($user$$,$obj$$,$next$$){Tdttno_tt.findOne({id_hd:$obj$$._id},function($error$$,$ct_tt$$){if($error$$)return $next$$($error$$);$ct_tt$$?$next$$(`Kh\u00f4ng th\u1ec3 xo\u00e1 phi\u1ebfu n\u00e0y do \u0111\u00e3 \u0111\u01b0\u1ee3c thanh to\u00e1n b\u1edfi ${$ct_tt$$.ma_ct||"ch\u1ee9ng t\u1eeb"}: ${$ct_tt$$.so_ct}, ng\u00e0y ${Moment($ct_tt$$.ngay_ct).format("DD/MM/YYYY")}`):$next$$(null,$obj$$)})};$contr$$.deleted=($user$$,$obj$$,$next$$)=>{global.getModel("evoucher").updateMany({id_app:$user$$.current_id_app,
id_sell:$obj$$._id.toString()},{id_sell:"",user:""});$next$$(null,$obj$$)};$contr$$.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$$()})},tk:function($callback$$){$items$$.filter($item$$=>$item$$.tk_no).joinModel($id_app$$,account,[{akey:"tk_no",bkey:"tk",fields:[{name:"ten_tk_no",
value:"ten_tk"}]}],function(){$callback$$()})},dt:function($callback$$){$items$$.filter($item$$=>$item$$.ma_dt).joinModel($id_app$$,dmdt,[{akey:"ma_dt",bkey:"ma_dt",fields:[{name:"ten_dt",value:"ten_dt"}]}],function(){$callback$$()})},kh:function($callback$$){$items$$.filter($item$$=>$item$$.ma_kh).joinModel2($id_app$$,customer,[{where:"ma_kh",fields:["ten_kh","email","nh_kh"]}],function(){$callback$$()})},nv:function($callback$$){$items$$.filter($item$$=>$item$$.ma_nv).joinModel($id_app$$,dmnv,[{akey:"ma_nv",
bkey:"ma_nv",fields:[{name:"ten_nv",value:"ten_nv"}]}],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$$()})},contract:function($callback$$){$items$$.filter($item$$=>$item$$.ma_hd).joinModel2($id_app$$,contract,[{where:{ma_hd:"so_hd"},fields:["ten_hd","ten_hd"]}],function(){$callback$$()})},t_tien:function($callback$$){$items$$.forEach(function($r$$){$r$$.details&&
($r$$.t_tien=$r$$.details.csum("tien"),$r$$.t_tien_nt=$r$$.details.csum("tien_nt"),$r$$.t_ck=$r$$.details.csum("tien_ck"),$r$$.t_ck_nt=$r$$.details.csum("tien_ck_nt"),$r$$.t_thue=$r$$.details.csum("tien_thue"),$r$$.t_thue_nt=$r$$.details.csum("tien_thue_nt"),$r$$.t_tt=$r$$.t_tien-$r$$.t_ck+$r$$.t_thue,$r$$.t_tt_nt=$r$$.t_tien_nt-$r$$.t_ck_nt+$r$$.t_thue_nt,$r$$.details.forEach($detail$$=>{$detail$$.dien_giai||($detail$$.dien_giai=$r$$.dien_giai)}))});$callback$$()},details_tk:function($callback$$){async.each($items$$,
function($r$$,$callback1$$){($r$$.details||[]).joinModel($id_app$$,account,[{akey:"tk_dt",bkey:"tk",fields:[{name:"ten_tk_dt",value:"ten_tk"}]},{akey:"tk_ck",bkey:"tk",fields:[{name:"ten_tk_ck",value:"ten_tk"}]}],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$$)})};$contr$$.createRoute("export2viettel/:id_ct",async($req$$,$next$$)=>{try{console.log("Export to viettel....");const $id_app$$=$req$$.user.current_id_app,$app$$=await App.findOne({_id:$id_app$$});if(!$app$$)return console.log("Kh\u00f4ng tim th\u1ea5y th\u00f4ng tin coogn ty"),$next$$("Kh\u00f4ng t\u00ecm th\u1ea5y c\u00f4ng ty n\u00e0y");
if(!$app$$.viettel_url_service||!$app$$.viettel_user||!$app$$.viettel_password)return console.log("Chua khai bao thong tin ket noi viettel"),$next$$("Th\u00f4ng tin xu\u1ea5t ho\u00e1 \u0111\u01a1n Viettel ch\u01b0a \u0111\u01b0\u1ee3c khai b\u00e1o \u0111\u1ea7y \u0111\u1ee7");if(!$app$$.ma_so_thue)return console.log("Chua nhap ma so thue cua cong ty"),$next$$("B\u1ea1n ch\u01b0a nh\u1eadp m\u00e3 s\u1ed1 thu\u1ebf c\u1ee7a c\u00f4ng ty");const $ct$$=await model.findOne({id_app:$id_app$$,_id:$req$$.params.id_ct}).lean();
if(!$ct$$)return console.log("Khong tim thay chung tu"),$next$$("Ch\u1ee9ng t\u1eeb kh\u00f4ng h\u1ee3p l\u1ec7");let $transactionUuid$$=$ct$$._id.toString();try{let $result_export_einvoice$$=await utils.getInvoice($app$$,$transactionUuid$$);if($result_export_einvoice$$){if($result_export_einvoice$$.errorCode)return $next$$($result_export_einvoice$$.data||$result_export_einvoice$$.description);console.log("\u0110\u1ed3ng b\u1ed9 hoa \u0111\u01a1n viettel");$ct$$.result_export_einvoice=$result_export_einvoice$$;
await utils.updateInvoice($contr$$,$ct$$);return $next$$(null,$result_export_einvoice$$)}}catch($e$$){return console.error("error export to viettel",$e$$),$next$$($e$$)}if($ct$$.result_export_einvoice&&$ct$$.result_export_einvoice.result&&$ct$$.result_export_einvoice.result.invoiceNo)return $next$$(null,$ct$$.result_export_einvoice);const $kh$$=await customer.findOne({id_app:$id_app$$,ma_kh:$ct$$.ma_kh})||{},$t_tien_hang$$=Math.abs($ct$$.details.map($d$$=>$d$$.tien_nt).reduce(($a$$,$b$$)=>$a$$+$b$$,
0)),$t_ck$$=Math.abs($ct$$.details.map($d$$=>$d$$.tien_ck_nt).reduce(($a$$,$b$$)=>$a$$+$b$$,0)),$t_tien$$=Math.abs($ct$$.details.map($d$$=>$d$$.tien_nt-$d$$.tien_ck_nt).reduce(($a$$,$b$$)=>$a$$+$b$$,0)),$t_thue$$=Math.abs($ct$$.details.map($d$$=>$d$$.tien_thue_nt).reduce(($a$$,$b$$)=>$a$$+$b$$,0)),$tien_ck_hd$$=$ct$$.tien_ck_hd||0;let $taxBreakdowns$$=$ct$$.details.map($detail$$=>({...$detail$$,ma_thue:$detail$$.ma_thue||$ct$$.ma_thue,thue_suat:($detail$$.ma_thue?$detail$$.thue_suat:$ct$$.thue_suat)||
0})).filter($d$$=>$d$$.ma_thue);if(0===$taxBreakdowns$$.length)return $next$$("B\u1ea1n ch\u01b0a khai b\u00e1o thu\u1ebf");$taxBreakdowns$$=$taxBreakdowns$$.filter($d$$=>"KT"!==$d$$.ma_thue&&"KK"!=$d$$.ma_thue).map($detail$$=>({taxPercentage:$detail$$.thue_suat,taxableAmount:Math.abs($detail$$.tien_nt-$detail$$.tien_ck_nt),taxAmount:Math.abs($detail$$.tien_thue_nt)}));$taxBreakdowns$$=await $taxBreakdowns$$.asyncGroupBy("taxPercentage",["taxableAmount","taxAmount"]);const $data$jscomp$0$$={generalInvoiceInfo:{invoiceType:$ct$$.ma_hoa_don||
$app$$.ma_hoa_don||"1",templateCode:$ct$$.ky_hieu_hoa_don||$app$$.ky_hieu_hoa_don||"1/001",invoiceSeries:$ct$$.so_seri,transactionUuid:$transactionUuid$$,currencyCode:"VND",adjustmentType:"1",paymentStatus:!0,cusGetInvoiceRight:!0,userName:$ct$$.user_created},buyerInfo:{buyerName:$ct$$.ong_ba||"",buyerLegalName:$ct$$.ten_kh||$kh$$.ten_kh||"",buyerTaxCode:$ct$$.ma_so_thue||$kh$$.ma_so_thue||"",buyerAddressLine:$ct$$.dia_chi||$kh$$.dia_chi||"",buyerPostalCode:"",buyerDistrictName:"",buyerCityName:"",
buyerCountryCode:"",buyerPhoneNumber:$ct$$.dien_thoai||$kh$$.dien_thoai||"",buyerFaxNumber:"",buyerEmail:$kh$$.email||"",buyerBankName:"",buyerBankAccount:"",buyerIdType:"",buyerIdNo:"",buyerCode:"",buyerBirthDay:""},sellerInfo:{sellerLegalName:$app$$.name,sellerTaxCode:$app$$.ma_so_thue,sellerAddressLine:$app$$.address,sellerPhoneNumber:$app$$.phone||"",sellerEmail:$app$$.email||"",sellerBankName:$app$$.ten_ngan_hang||"",sellerBankAccount:$app$$.tai_khoan_ngan_hang||""},itemInfo:$ct$$.details.map(($detail$$,
$index$$)=>{var $JSCompiler_inline_result$jscomp$10_thue_suat$$=($detail$$.ma_thue?$detail$$.thue_suat:$ct$$.thue_suat)||0,$JSCompiler_temp_const$$=0>$detail$$.tien_nt?3:1,$JSCompiler_temp_const$jscomp$9$$=($index$$+1).toString(),$JSCompiler_temp_const$jscomp$8$$=$detail$$.dien_giai,$JSCompiler_temp_const$jscomp$7$$=0>$detail$$.tien_nt?"":$detail$$.ma_dvt,$JSCompiler_temp_const$jscomp$6$$=0>$detail$$.tien_nt?void 0:$detail$$.gia_ban_nt,$JSCompiler_temp_const$jscomp$5$$=0>$detail$$.tien_nt?void 0:
$detail$$.sl_xuat,$JSCompiler_temp_const$jscomp$4$$=Math.abs($detail$$.tien_nt),$JSCompiler_temp_const$jscomp$3$$=$detail$$.ty_le_ck,$JSCompiler_temp_const$jscomp$2$$=Math.abs($detail$$.tien_ck_nt),$JSCompiler_temp_const$jscomp$1$$=Math.abs($detail$$.tien_nt-$detail$$.tien_ck_nt);a:{var $tien_ct_nt$$=$detail$$.tien_nt;switch($detail$$.ma_thue||$ct$$.ma_thue){case "KT":$JSCompiler_inline_result$jscomp$10_thue_suat$$=0>$tien_ct_nt$$?void 0:-2;break a;case "KK":$JSCompiler_inline_result$jscomp$10_thue_suat$$=
0>$tien_ct_nt$$?void 0:-1}}return{lineNumber:$index$$+1,selection:$JSCompiler_temp_const$$,itemCode:$JSCompiler_temp_const$jscomp$9$$,itemName:$JSCompiler_temp_const$jscomp$8$$,unitName:$JSCompiler_temp_const$jscomp$7$$,itemNote:"",unitPrice:$JSCompiler_temp_const$jscomp$6$$,quantity:$JSCompiler_temp_const$jscomp$5$$,itemTotalAmountWithoutTax:$JSCompiler_temp_const$jscomp$4$$,discount:$JSCompiler_temp_const$jscomp$3$$,itemDiscount:$JSCompiler_temp_const$jscomp$2$$,itemTotalAmountAfterDiscount:$JSCompiler_temp_const$jscomp$1$$,
taxPercentage:$JSCompiler_inline_result$jscomp$10_thue_suat$$,taxAmount:Math.abs($detail$$.tien_thue_nt),itemTotalAmountWithTax:Math.abs($detail$$.tien_nt-$detail$$.tien_ck_nt+$detail$$.tien_thue_nt),customTaxAmount:"0",batchNo:"",expDate:""}}),summarizeInfo:{sumOfTotalLineAmountWithoutTax:$t_tien_hang$$,discountAmount:$t_ck$$,settlementDiscountAmount:$tien_ck_hd$$,totalAmountWithoutTax:$t_tien$$,totalTaxAmount:$t_thue$$,totalAmountWithTax:$t_tien$$+$t_thue$$},taxBreakdowns:$taxBreakdowns$$,payments:[{paymentMethodName:$ct$$.hinh_thuc_tt||
"TM"}],extAttribute:[],deliveryInfo:{},metadata:[]};$ct$$.ngay_hd||($ct$$.ngay_hd=new Date);let $headers$$,$url$$,$viettel_token$$,$auth$$={username:$app$$.viettel_user.trim(),password:$app$$.viettel_password.trim()};if("1"!=$app$$.viettel_version){$data$jscomp$0$$.generalInvoiceInfo.invoiceIssuedDate=(new Date($ct$$.ngay_hd)).getTime();let $url_token$$=`${$app$$.viettel_url_service.trim()}/auth/login`;$url$$=$app$$.viettel_direct_issue?`${$app$$.viettel_url_service.trim()}/services/einvoiceapplication/api/InvoiceAPI/InvoiceWS/createInvoice/${$app$$.ma_so_thue.trim()}`:
`${$app$$.viettel_url_service.trim()}/services/einvoiceapplication/api/InvoiceAPI/InvoiceWS/createOrUpdateInvoiceDraft/${$app$$.ma_so_thue.trim()}`;try{$viettel_token$$=(await axios({method:"post",url:$url_token$$,data:$auth$$})).data.access_token}catch($e$$){console.error("error when get token viettel",$auth$$,$url_token$$);let $msg_e$$=$e$$.response?$e$$.response.data:$e$$.message;$msg_e$$&&$msg_e$$.detail&&($msg_e$$.message=$msg_e$$.detail);return $next$$($msg_e$$||$e$$.message||$e$$.error||$e$$)}$headers$$=
{Cookie:`"access_token":"${$viettel_token$$}"; access_token=${$viettel_token$$}; session_token=${$viettel_token$$}`,"Content-Type":"application/json"};$auth$$=void 0}else $data$jscomp$0$$.generalInvoiceInfo.invoiceIssuedDate=Moment($ct$$.ngay_hd).format("YYYY-MM-DD"),$url$$=$app$$.viettel_direct_issue?`${$app$$.viettel_url_service.trim()}/InvoiceAPI/InvoiceWS/createInvoice/${$app$$.ma_so_thue.trim()}`:`${$app$$.viettel_url_service.trim()}/InvoiceAPI/InvoiceWS/createOrUpdateInvoiceDraft/${$app$$.ma_so_thue.trim()}`;
console.log("post to viettel...");axios({method:"post",url:$url$$,data:"1"!=$app$$.viettel_version?JSON.stringify($data$jscomp$0$$):$data$jscomp$0$$,auth:$auth$$,headers:$headers$$}).then(async $data$$=>{$data$$=$data$$.data;console.error("result data",$data$$);if(!$data$$)return $next$$("Kh\u00f4ng th\u1ec3 xu\u1ea5t ho\u00e1 \u0111\u01a1n");if($data$$.errorCode)return $next$$($data$$.description);$ct$$.result_export_einvoice=$data$$;$ct$$.so_hd=$ct$$.result_export_einvoice.result.invoiceNo;await model.findOneAndUpdate({_id:$ct$$._id},
$ct$$);$next$$(null,$data$$)}).catch($e$$=>{console.error("error when export invocie viettel",$e$$.response);let $msg_e$$=$e$$.response?$e$$.response.data:$e$$.message;$msg_e$$.data&&($msg_e$$.message=$msg_e$$.data);$msg_e$$.detail&&($msg_e$$.message=$msg_e$$.detail);$next$$($msg_e$$||$e$$.message||$e$$.error||$e$$)})}catch($e$$){console.log($e$$),$next$$($e$$.message)}},{method:"GET"})};