flexbiz-server
Version:
Flexible Server
26 lines (25 loc) • 12.3 kB
JavaScript
const axios=require("axios"),moment=require("moment"),querystring=require("querystring"),exportInvoice=async($app_msg_e_msg_e$$,$ct$$,$check_data$$=!1)=>{if(!$app_msg_e_msg_e$$.viettel_url_service||!$app_msg_e_msg_e$$.viettel_user||!$app_msg_e_msg_e$$.viettel_password)throw"Th\u00f4ng tin xu\u1ea5t ho\u00e1 \u0111\u01a1n Viettel ch\u01b0a \u0111\u01b0\u1ee3c khai b\u00e1o \u0111\u1ea7y \u0111\u1ee7";var $data_kh$$=await global.getModel("customer").findOne({id_app:$app_msg_e_msg_e$$._id,ma_kh:$ct$$.ma_kh})||
{};await $ct$$.details.filter($d$$=>$d$$.ma_vt).asyncJoinModel2($app_msg_e_msg_e$$._id.toString(),global.getModel("dmvt"),{where:"ma_vt",fields:"ten_vt"});var $transactionUuid_url$$=$ct$$._id.toString(),$auth_t_tien_hang$$=Math.abs($ct$$.details.map($d$$=>$d$$.tien_ct_nt).reduce(($a$$,$b$$)=>$a$$+$b$$,0));const $t_ck$$=Math.abs($ct$$.details.map($d$$=>$d$$.tien_ck_nt||0).reduce(($a$$,$b$$)=>$a$$+$b$$,0)),$t_tien$$=Math.abs($ct$$.details.map($d$$=>$d$$.tien_ct_nt||0-$d$$.tien_ck_nt||0).reduce(($a$$,
$b$$)=>$a$$+$b$$,0));let $t_thue$$=Math.abs($ct$$.details.map($d$$=>$d$$.tien_thue_nt||0).reduce(($a$$,$b$$)=>$a$$+$b$$,0));const $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($taxBreakdowns$$.length===0)throw"B\u1ea1n ch\u01b0a khai b\u00e1o thu\u1ebf";const $getThueSuat$$=($ma_thue$$,$thue_suat$$,$tien_ct_nt$$)=>
{switch($ma_thue$$){case "KT":return $tien_ct_nt$$<0?void 0:-2;case "KK":return $tien_ct_nt$$<0?void 0:-1;default:return $thue_suat$$}};$taxBreakdowns$$=$taxBreakdowns$$.filter($d$$=>$d$$.ma_thue!="KT"&&$d$$.ma_thue!="KK").map($detail$$=>({taxPercentage:$detail$$.thue_suat,taxableAmount:Math.abs($detail$$.tien_ct_nt-$detail$$.tien_ck_nt),taxAmount:Math.abs($detail$$.tien_thue_nt)}));$taxBreakdowns$$=await $taxBreakdowns$$.asyncGroupBy("taxPercentage",["taxableAmount","taxAmount"]);var $pattern$jscomp$3_thue_suat$$=
[...(new Set($ct$$.details.map($detail$$=>$getThueSuat$$($detail$$.ma_thue||$ct$$.ma_thue,($detail$$.ma_thue?$detail$$.thue_suat:$ct$$.thue_suat)||0,$detail$$.tien_ct_nt))))];let $itemInfo$$;if($pattern$jscomp$3_thue_suat$$.length==1||$ct$$.details.find($d$$=>$d$$.tien_ct_nt<0)){$itemInfo$$=$ct$$.details.map(($detail$$,$index$$)=>{var $taxAmount$jscomp$1_thue_suat$$=($detail$$.ma_thue?$detail$$.thue_suat:$ct$$.thue_suat)||0;let $unitPrice$$=$detail$$.gia_ban_ct_nt,$quantity$$=$detail$$.sl_xuat,$itemTotalAmountWithoutTax$$=
$detail$$.tien_ct_nt,$itemTotalAmountAfterDiscount$$=$itemTotalAmountWithoutTax$$-0,$taxPercentage$$=$getThueSuat$$($detail$$.ma_thue||$ct$$.ma_thue,$taxAmount$jscomp$1_thue_suat$$,$detail$$.tien_ct_nt);$taxAmount$jscomp$1_thue_suat$$=Math.round($detail$$.tien_ct_nt*$taxAmount$jscomp$1_thue_suat$$/100);return{lineNumber:$index$$+1,selection:1,itemCode:$detail$$.ma_vt,itemName:$detail$$.ten_vt_vat||$detail$$.ten_vt,unitName:$detail$$.ma_dvt,itemNote:"",unitPrice:$unitPrice$$,quantity:$quantity$$,itemTotalAmountWithoutTax:$itemTotalAmountWithoutTax$$,
discount:0,itemDiscount:0,itemTotalAmountAfterDiscount:$itemTotalAmountAfterDiscount$$,taxPercentage:$taxPercentage$$,taxAmount:$taxAmount$jscomp$1_thue_suat$$,itemTotalAmountWithTax:$itemTotalAmountAfterDiscount$$+$taxAmount$jscomp$1_thue_suat$$,customTaxAmount:"0",batchNo:"",expDate:""}});$pattern$jscomp$3_thue_suat$$=$pattern$jscomp$3_thue_suat$$[0];var $so_seri_tong_tien_ck$$=$ct$$.details.map($d$$=>$d$$.tien_ck_nt).reduce(($a$$,$b$$)=>$a$$+$b$$);let $tien_thue_ck$$=Math.round($so_seri_tong_tien_ck$$*
Math.max($pattern$jscomp$3_thue_suat$$,0)/100,0);$tien_thue_ck$$&&($t_thue$$=$itemInfo$$.map($i$$=>$i$$.taxAmount).reduce(($a$$,$b$$)=>$a$$+$b$$,0)-$tien_thue_ck$$);$so_seri_tong_tien_ck$$>0&&$itemInfo$$.push({lineNumber:$ct$$.details.length,selection:3,itemCode:"discount",itemName:"Ti\u1ec1n chi\u1ebft kh\u1ea5u",unitName:"",itemNote:"",unitPrice:void 0,quantity:void 0,itemTotalAmountWithoutTax:$so_seri_tong_tien_ck$$,discount:0,itemDiscount:0,itemTotalAmountAfterDiscount:$so_seri_tong_tien_ck$$,
taxPercentage:$pattern$jscomp$3_thue_suat$$,taxAmount:$tien_thue_ck$$,itemTotalAmountWithTax:$so_seri_tong_tien_ck$$+$tien_thue_ck$$,customTaxAmount:"0",batchNo:"",expDate:""})}else $itemInfo$$=$ct$$.details.map(($detail$$,$index$$)=>{let $unitPrice$$=$detail$$.tien_ct_nt<0?void 0:$detail$$.gia_ban_ct_nt,$quantity$$=$detail$$.tien_ct_nt<0?void 0:$detail$$.sl_xuat,$itemTotalAmountWithoutTax$$=Math.abs($detail$$.tien_ct_nt),$discount$$=$detail$$.ty_le_ck,$itemDiscount$$=Math.abs($detail$$.tien_ck_nt),
$itemTotalAmountAfterDiscount$$=Math.abs($detail$$.tien_ct_nt-$detail$$.tien_ck_nt),$taxPercentage$$=$getThueSuat$$($detail$$.ma_thue||$ct$$.ma_thue,($detail$$.ma_thue?$detail$$.thue_suat:$ct$$.thue_suat)||0,$detail$$.tien_ct_nt);return{lineNumber:$index$$+1,selection:$detail$$.tien_ct_nt<0?3:1,itemCode:$detail$$.ma_vt,itemName:$detail$$.ten_vt_vat||$detail$$.ten_vt,unitName:$detail$$.tien_ct_nt<0?"":$detail$$.ma_dvt,itemNote:"",unitPrice:$unitPrice$$,quantity:$quantity$$,itemTotalAmountWithoutTax:$itemTotalAmountWithoutTax$$,
discount:$discount$$,itemDiscount:$itemDiscount$$,itemTotalAmountAfterDiscount:$itemTotalAmountAfterDiscount$$,taxPercentage:$taxPercentage$$,taxAmount:Math.abs($detail$$.tien_thue_nt),itemTotalAmountWithTax:Math.abs($detail$$.tien_ct_nt-$detail$$.tien_ck_nt+$detail$$.tien_thue_nt),customTaxAmount:"0",batchNo:"",expDate:""}});$pattern$jscomp$3_thue_suat$$=$ct$$.ky_hieu_hoa_don||$app_msg_e_msg_e$$.ky_hieu_hoa_don||"1/001";$so_seri_tong_tien_ck$$=$ct$$.so_seri||$app_msg_e_msg_e$$.so_serial||"";$data_kh$$=
{generalInvoiceInfo:{invoiceType:$ct$$.ma_hoa_don||$app_msg_e_msg_e$$.ma_hoa_don||"1",templateCode:$pattern$jscomp$3_thue_suat$$,invoiceSeries:$so_seri_tong_tien_ck$$,transactionUuid:$transactionUuid_url$$,currencyCode:"VND",adjustmentType:"1",paymentStatus:!0,cusGetInvoiceRight:!0,userName:$ct$$.user_created},buyerInfo:{buyerName:$ct$$.ong_ba||"",buyerLegalName:$ct$$.ten_kh||$data_kh$$.ten_kh||"",buyerTaxCode:$ct$$.ma_so_thue||$data_kh$$.ma_so_thue||"",buyerAddressLine:$ct$$.dia_chi||$data_kh$$.dia_chi||
"",buyerPostalCode:"",buyerDistrictName:"",buyerCityName:"",buyerCountryCode:"",buyerPhoneNumber:$ct$$.dien_thoai||$data_kh$$.dien_thoai||"",buyerFaxNumber:"",buyerEmail:$ct$$.email||$data_kh$$.email||"",buyerBankName:"",buyerBankAccount:"",buyerIdType:"",buyerIdNo:"",buyerCode:"",buyerBirthDay:""},sellerInfo:{sellerLegalName:$app_msg_e_msg_e$$.name,sellerTaxCode:$app_msg_e_msg_e$$.ma_so_thue,sellerAddressLine:$app_msg_e_msg_e$$.dia_chi_xuat_hoa_don||$app_msg_e_msg_e$$.address,sellerPhoneNumber:$app_msg_e_msg_e$$.phone||
"",sellerEmail:$app_msg_e_msg_e$$.email||"",sellerBankName:$app_msg_e_msg_e$$.ten_ngan_hang||"",sellerBankAccount:$app_msg_e_msg_e$$.tai_khoan_ngan_hang||""},itemInfo:$itemInfo$$,summarizeInfo:{sumOfTotalLineAmountWithoutTax:$auth_t_tien_hang$$,discountAmount:$t_ck$$,settlementDiscountAmount:$tien_ck_hd$$,totalAmountWithoutTax:$t_tien$$,totalTaxAmount:$t_thue$$,totalAmountWithTax:$auth_t_tien_hang$$-$t_ck$$+$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_t_tien_hang$$={username:$app_msg_e_msg_e$$.viettel_user.trim(),password:$app_msg_e_msg_e$$.viettel_password.trim()};if($app_msg_e_msg_e$$.viettel_version!="1"){$data_kh$$.generalInvoiceInfo.invoiceIssuedDate=(new Date($ct$$.ngay_hd)).getTime();$transactionUuid_url$$=$app_msg_e_msg_e$$.viettel_direct_issue?`${$app_msg_e_msg_e$$.viettel_url_service.trim()}/services/einvoiceapplication/api/InvoiceAPI/InvoiceWS/createInvoice/${$app_msg_e_msg_e$$.ma_so_thue.trim()}`:
`${$app_msg_e_msg_e$$.viettel_url_service.trim()}/services/einvoiceapplication/api/InvoiceAPI/InvoiceWS/createOrUpdateInvoiceDraft/${$app_msg_e_msg_e$$.ma_so_thue.trim()}`;var $headers_url_token$$=`${$app_msg_e_msg_e$$.viettel_url_service.trim()}/auth/login`;try{$viettel_token$$=(await axios({method:"post",url:$headers_url_token$$,data:$auth_t_tien_hang$$})).data.access_token}catch($e$$){throw Logger.error("[utils] [exportInvoice] error when get token viettel",$auth_t_tien_hang$$,$headers_url_token$$),
($app_msg_e_msg_e$$=$e$$.response?$e$$.response.data:$e$$.message)&&$app_msg_e_msg_e$$.detail&&($app_msg_e_msg_e$$.message=$app_msg_e_msg_e$$.detail),$app_msg_e_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_t_tien_hang$$=void 0}else $data_kh$$.generalInvoiceInfo.invoiceIssuedDate=moment($ct$$.ngay_hd).format("YYYY-MM-DD"),$transactionUuid_url$$=
$app_msg_e_msg_e$$.viettel_direct_issue?`${$app_msg_e_msg_e$$.viettel_url_service.trim()}/InvoiceAPI/InvoiceWS/createInvoice/${$app_msg_e_msg_e$$.ma_so_thue.trim()}`:`${$app_msg_e_msg_e$$.viettel_url_service.trim()}/InvoiceAPI/InvoiceWS/createOrUpdateInvoiceDraft/${$app_msg_e_msg_e$$.ma_so_thue.trim()}`;if($check_data$$)return $data_kh$$;try{let $data_rs$$=(await axios({method:"post",url:$transactionUuid_url$$,data:$app_msg_e_msg_e$$.viettel_version!="1"?JSON.stringify($data_kh$$):$data_kh$$,auth:$auth_t_tien_hang$$,
headers:$headers_url_token$$})).data;if(!$data_rs$$)throw"Kh\u00f4ng th\u1ec3 xu\u1ea5t ho\u00e1 \u0111\u01a1n";if($data_rs$$.errorCode)throw $data_rs$$.description;$data_rs$$.so_hd=$data_rs$$?.result?.invoiceNo;$data_rs$$.ky_hieu_hoa_don=$pattern$jscomp$3_thue_suat$$;$data_rs$$.so_seri=$so_seri_tong_tien_ck$$;return $data_rs$$}catch($e$$){throw Logger.error("[utils] [exportInvoice] error invocie viettel",$e$$.response),$app_msg_e_msg_e$$=$e$$.response?$e$$.response.data:$e$$.message,$app_msg_e_msg_e$$.data&&
($app_msg_e_msg_e$$.message=$app_msg_e_msg_e$$.data),$app_msg_e_msg_e$$.detail&&($app_msg_e_msg_e$$.message=$app_msg_e_msg_e$$.detail),$app_msg_e_msg_e$$||$e$$.message||$e$$.error||$e$$;}};exports.exportInvoice=exportInvoice;
const getInvoice=async($app$jscomp$1_data$$,$transactionUuid$$)=>{if(!$app$jscomp$1_data$$)throw"Thi\u1ebfu th\u00f4ng tin c\u00f4ng ty";if(!$app$jscomp$1_data$$.viettel_url_service||!$app$jscomp$1_data$$.viettel_user||!$app$jscomp$1_data$$.viettel_password)throw"Ch\u01b0a khai b\u00e1o th\u00f4ng tin k\u1ebft n\u1ed1i invoice";if(!$app$jscomp$1_data$$.ma_so_thue)throw"Ch\u01b0a khai b\u00e1o m\u00e3 s\u1ed1 thu\u1ebf c\u00f4ng ty";var $auth$jscomp$1_url$$={username:$app$jscomp$1_data$$.viettel_user.trim(),
password:$app$jscomp$1_data$$.viettel_password.trim()};try{var $headers$jscomp$1_viettel_token$$=(await axios({method:"post",url:`${$app$jscomp$1_data$$.viettel_url_service.trim()}/auth/login`,data:$auth$jscomp$1_url$$})).data.access_token}catch($e$$){throw Logger.error("[utils] [getInvoice] error when get token viettel"),"Th\u00f4ng tin truy c\u1eadp Viettel invoice kh\u00f4ng \u0111\u00fang";}$auth$jscomp$1_url$$=`${$app$jscomp$1_data$$.viettel_url_service.trim()}/services/einvoiceapplication/api/InvoiceAPI/InvoiceWS/searchInvoiceByTransactionUuid`;
$headers$jscomp$1_viettel_token$$={Cookie:`"access_token":"${$headers$jscomp$1_viettel_token$$}"; access_token=${$headers$jscomp$1_viettel_token$$}; session_token=${$headers$jscomp$1_viettel_token$$}`,"Content-Type":"application/x-www-form-urlencoded"};$app$jscomp$1_data$$={supplierTaxCode:$app$jscomp$1_data$$.ma_so_thue,transactionUuid:$transactionUuid$$};try{let $rs$$=await axios.post($auth$jscomp$1_url$$,querystring.stringify($app$jscomp$1_data$$),{headers:$headers$jscomp$1_viettel_token$$});Logger.info("[utils] [getInvoice] invoice info",
$rs$$.data);let $invoice$$=$rs$$.data;$invoice$$.result&&$invoice$$.result.length==1&&($invoice$$.result=$invoice$$.result[0]);return $invoice$$}catch($e$$){if((($e$$.response||{}).data||{}).message==="NOT_FOUND_DATA")return null;Logger.error("[utils] [getInvoice] error get invoice",($e$$.response||{}).data,", data:",$app$jscomp$1_data$$);throw(($e$$.response||{}).data||{}).data||"Kh\u00f4ng th\u1ec3 l\u1ea5y th\u00f4ng tin ho\u00e1 \u0111\u01a1n n\u00e0y c\u1ee7a transactionUuid "+$transactionUuid$$;
}};exports.getInvoice=getInvoice;