UNPKG

flexbiz-server

Version:

Flexible Server

35 lines (34 loc) 16.9 kB
'use strict';const PostBook=require("../../libs/post-book"),PostSocai=require("../../libs/post-socai"),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("xhd"),controller=require("../../controllers/controller"),trangthai=global.getModel("trangthai"), utils=require("../../libs/utils"),async=require("async"),ma_ct="XHD",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,check_stock:!1,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$$=[],$o$$={...$obj$$};$o$$.tk_co=$o$$.tk_thue_co;$o$$.tien_nt=$o$$.t_thue_nt;$o$$.tien=$o$$.t_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_vatra:function($callback$jscomp$0$$){$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_hang_nt tien_hang 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_hang_nt-$detail$$.tien_ck_nt,$detail$$.t_tien=$detail$$.tien_hang- $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$$=function($f_tien_nt_i_t_thue_nt_user$$,$obj$$,$next$$){for($f_tien_nt_i_t_thue_nt_user$$=0;$f_tien_nt_i_t_thue_nt_user$$<$obj$$.details.length;$f_tien_nt_i_t_thue_nt_user$$++){var $detail$$= $obj$$.details[$f_tien_nt_i_t_thue_nt_user$$];let $thue_suat$$=($detail$$.ma_thue?$detail$$.thue_suat:$obj$$.thue_suat)||0;$detail$$.line=$f_tien_nt_i_t_thue_nt_user$$;$detail$$.gia_ban=$detail$$.gia_ban_nt;$detail$$.tien_hang_nt=$detail$$.tien_hang=$detail$$.sl_xuat*$detail$$.gia_ban_nt;$detail$$.tien_nt=$detail$$.tien=$detail$$.tien_hang_nt-$detail$$.tien_ck_nt;$detail$$.tien_thue=$detail$$.tien_thue_nt=Math.roundBy($detail$$.tien*$thue_suat$$/100)}if(!$obj$$.details.find($r$$=>$r$$.ma_thue)&&void 0!= $obj$$.thue_suat&&($f_tien_nt_i_t_thue_nt_user$$="VND"==$obj$$.ma_nt?0:2,$obj$$.t_thue_nt=utils.round($obj$$.details.csum("tien_nt")*$obj$$.thue_suat/100,$f_tien_nt_i_t_thue_nt_user$$),$obj$$.t_thue=utils.round($obj$$.t_thue_nt*($obj$$.ty_gia||1),0),$f_tien_nt_i_t_thue_nt_user$$=$obj$$.details.csum("tien_thue_nt"),$detail$$=$obj$$.details.csum("tien_thue"),$f_tien_nt_i_t_thue_nt_user$$!==$obj$$.t_thue_nt||$detail$$!==$obj$$.t_thue)){let $chenh_lech$$=$obj$$.t_thue-$detail$$,$chenh_lech_nt$$=$obj$$.t_thue_nt- $f_tien_nt_i_t_thue_nt_user$$;for(;0!==$chenh_lech$$;)$obj$$.details.forEach($d$$=>{if($chenh_lech$$&&$chenh_lech_nt$$){let $dc_nt$$=0>$chenh_lech_nt$$?-1:1,$dc$$=0>$chenh_lech$$?-1:1;$d$$.tien_thue_nt+=$dc_nt$$;$d$$.tien_thue+=$dc$$;$chenh_lech$$-=$dc$$;$chenh_lech_nt$$-=$dc_nt$$}})}$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$$)})};$contr$$.updating=function($user$$,$data$$,$obj$$,$next$$){$valid$$($user$$,$data$$,function($error$$){if($error$$)return $next$$($error$$);$next$$(null,$data$$,$obj$$)})};$contr$$.view=function($user$$,$items$$,$fn$$){let $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"]}],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_hang=$r$$.details.csum("tien_hang"),$r$$.t_tien_hang_nt=$r$$.details.csum("tien_hang_nt"),$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_thue=$r$$.details.csum("tien_thue"),$r$$.t_thue_nt=$r$$.details.csum("tien_thue_nt"),$r$$.t_tt=$r$$.t_tien+$r$$.t_thue,$r$$.t_tt_nt=$r$$.t_tien_nt+$r$$.t_thue_nt,$r$$.details.forEach($detail$$=>{$detail$$.dien_giai||($detail$$.dien_giai=$r$$.dien_giai)}))});$callback$$()},details_ct:function($callback$$){async.mapSeries($items$$,($r$$,$callback1$$)=>{$r$$.details.filter($detail$$=>$detail$$.code_ct&&mongoose.models[$detail$$.code_ct.toLowerCase()]).joinModel2($id_app$$,$detail$$=>{$detail$$= $detail$$.code_ct.toLowerCase();return mongoose.models[$detail$$]},{where:{id_ct:"_id"},fields:["so_ct","ngay_ct"]},()=>{$callback1$$()})},()=>{$callback$$()})},details_dt:function($callback$$){async.mapSeries($items$$,function($r$$,$callback1$$){($r$$.details||[]).filter($item$$=>$item$$.ma_dt).joinModel2($id_app$$,dmdt,[{where:"ma_dt",fields:[{name:"ten_dt",value:"ten_dt"}]}],()=>{$callback1$$()})},()=>{$callback$$()})},details_contract:function($callback$$){async.mapSeries($items$$,function($r$$, $callback1$$){($r$$.details||[]).filter($item$$=>$item$$.ma_hd).joinModel2($id_app$$,contract,[{where:{ma_hd:"so_hd"},fields:["ten_hd"]}],function(){$callback1$$()})},function(){$callback$$()})},details_phi:function($callback$$){async.mapSeries($items$$,function($r$$,$callback1$$){($r$$.details||[]).filter($item$$=>$item$$.ma_phi).joinModel2($id_app$$,dmphi,[{where:"ma_phi",fields:["ten_phi"]}],function(){$callback1$$()})},function(){$callback$$()})}},function(){$fn$$(null,$items$$)})};$contr$$.createRoute("export2viettel/:id_ct", async($data$jscomp$0$$,$next$$)=>{var $id_app$$=$data$jscomp$0$$.user.current_id_app,$app_msg_e$$=await App.findOne({_id:$id_app$$});if(!$app_msg_e$$)return $next$$("Kh\u00f4ng t\u00ecm th\u1ea5y c\u00f4ng ty n\u00e0y");if(!$app_msg_e$$.viettel_url_service||!$app_msg_e$$.viettel_user||!$app_msg_e$$.viettel_password)return $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_msg_e$$.ma_so_thue)return $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:$data$jscomp$0$$.params.id_ct}).lean();if(!$ct$$)return $next$$("Ch\u1ee9ng t\u1eeb kh\u00f4ng h\u1ee3p l\u1ec7");$data$jscomp$0$$=$ct$$._id.toString();try{if($auth_result_export_einvoice_t_tien_hang$$=await utils.getInvoice($app_msg_e$$,$data$jscomp$0$$)){if($auth_result_export_einvoice_t_tien_hang$$.errorCode)return $next$$($auth_result_export_einvoice_t_tien_hang$$.data||$auth_result_export_einvoice_t_tien_hang$$.description);console.log("\u0110\u1ed3ng b\u1ed9 hoa \u0111\u01a1n viettel"); $ct$$.result_export_einvoice=$auth_result_export_einvoice_t_tien_hang$$;await utils.updateInvoice($contr$$,$ct$$);return $next$$(null,$auth_result_export_einvoice_t_tien_hang$$)}}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);$id_app$$=await customer.findOne({id_app:$id_app$$,ma_kh:$ct$$.ma_kh})||{};var $auth_result_export_einvoice_t_tien_hang$$= $ct$$.details.map($d$$=>$d$$.tien_hang).reduce(($a$$,$b$$)=>$a$$+$b$$,0);const $t_ck$$=$ct$$.details.map($d$$=>$d$$.tien_ck).reduce(($a$$,$b$$)=>$a$$+$b$$,0),$t_tien$$=$ct$$.details.map($d$$=>$d$$.tien).reduce(($a$$,$b$$)=>$a$$+$b$$,0),$t_thue$$=$ct$$.details.map($d$$=>$d$$.tien_thue).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"]);$data$jscomp$0$$= {generalInvoiceInfo:{invoiceType:$ct$$.ma_hoa_don||$app_msg_e$$.ma_hoa_don||"1",templateCode:$ct$$.ky_hieu_hoa_don||$app_msg_e$$.ky_hieu_hoa_don||"1/001",invoiceSeries:$ct$$.so_seri,transactionUuid:$data$jscomp$0$$,currencyCode:"VND",adjustmentType:"1",paymentStatus:!0,cusGetInvoiceRight:!0,userName:$ct$$.user_created},buyerInfo:{buyerName:$ct$$.ong_ba||"",buyerLegalName:$ct$$.ten_kh||$id_app$$.ten_kh||"",buyerTaxCode:$ct$$.ma_so_thue||$id_app$$.ma_so_thue||"",buyerAddressLine:$ct$$.dia_chi||$id_app$$.dia_chi|| "",buyerPostalCode:"",buyerDistrictName:"",buyerCityName:"",buyerCountryCode:"",buyerPhoneNumber:$ct$$.dien_thoai||$id_app$$.dien_thoai||"",buyerFaxNumber:"",buyerEmail:$id_app$$.email||"",buyerBankName:"",buyerBankAccount:"",buyerIdType:"",buyerIdNo:"",buyerCode:"",buyerBirthDay:""},sellerInfo:{sellerLegalName:$app_msg_e$$.name,sellerTaxCode:$app_msg_e$$.ma_so_thue,sellerAddressLine:$app_msg_e$$.dia_chi_xuat_hoa_don||$app_msg_e$$.address,sellerPhoneNumber:$app_msg_e$$.phone||"",sellerEmail:$app_msg_e$$.email|| "",sellerBankName:$app_msg_e$$.ten_ngan_hang||"",sellerBankAccount:$app_msg_e$$.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,$JSCompiler_temp_const$$=0>$detail$$.tien?3:1,$JSCompiler_temp_const$jscomp$9$$=$detail$$.ma_vt,$JSCompiler_temp_const$jscomp$8$$=$detail$$.ten_vt_vat||$detail$$.ten_vt,$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_hang_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);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_thue_nt),customTaxAmount:"0",batchNo:"",expDate:""}}),summarizeInfo:{sumOfTotalLineAmountWithoutTax:$auth_result_export_einvoice_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 $viettel_token$$;$auth_result_export_einvoice_t_tien_hang$$={username:$app_msg_e$$.viettel_user.trim(),password:$app_msg_e$$.viettel_password.trim()};if("1"!=$app_msg_e$$.viettel_version){$data$jscomp$0$$.generalInvoiceInfo.invoiceIssuedDate=(new Date($ct$$.ngay_hd)).getTime(); var $headers_url_token$$=`${$app_msg_e$$.viettel_url_service.trim()}/auth/login`;$id_app$$=$app_msg_e$$.viettel_direct_issue?`${$app_msg_e$$.viettel_url_service.trim()}/services/einvoiceapplication/api/InvoiceAPI/InvoiceWS/createInvoice/${$app_msg_e$$.ma_so_thue.trim()}`:`${$app_msg_e$$.viettel_url_service.trim()}/services/einvoiceapplication/api/InvoiceAPI/InvoiceWS/createOrUpdateInvoiceDraft/${$app_msg_e$$.ma_so_thue.trim()}`;try{$viettel_token$$=(await axios({method:"post",url:$headers_url_token$$, data:$auth_result_export_einvoice_t_tien_hang$$})).data.access_token}catch($e$$){return console.error("error when get token viettel",$auth_result_export_einvoice_t_tien_hang$$,$headers_url_token$$),($app_msg_e$$=$e$$.response?$e$$.response.data:$e$$.message)&&$app_msg_e$$.detail&&($app_msg_e$$.message=$app_msg_e$$.detail),$next$$($app_msg_e$$||$e$$.message||$e$$.error||$e$$)}$headers_url_token$$={Cookie:`"access_token":"${$viettel_token$$}"; access_token=${$viettel_token$$}; session_token=${$viettel_token$$}`, "Content-Type":"application/json"};$auth_result_export_einvoice_t_tien_hang$$=void 0}else $data$jscomp$0$$.generalInvoiceInfo.invoiceIssuedDate=Moment($ct$$.ngay_hd).format("YYYY-MM-DD"),$id_app$$=$app_msg_e$$.viettel_direct_issue?`${$app_msg_e$$.viettel_url_service.trim()}/InvoiceAPI/InvoiceWS/createInvoice/${$app_msg_e$$.ma_so_thue.trim()}`:`${$app_msg_e$$.viettel_url_service.trim()}/InvoiceAPI/InvoiceWS/createOrUpdateInvoiceDraft/${$app_msg_e$$.ma_so_thue.trim()}`;axios({method:"post",url:$id_app$$, data:"1"!=$app_msg_e$$.viettel_version?JSON.stringify($data$jscomp$0$$):$data$jscomp$0$$,auth:$auth_result_export_einvoice_t_tien_hang$$,headers:$headers_url_token$$}).then(async $data$$=>{$data$$=$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$$);$ct$$.so_hd&& await Vatra.updateMany({id_ct:$ct$$._id.toString()},{so_hd:$ct$$.so_hd});controller.postData($ct$$,$contr$$,$e$$=>{if($e$$)return console.error($e$$);controller.pushNotification($contr$$,$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$$)})},{method:"GET"})};