|
@@ -0,0 +1,604 @@
|
|
|
+using System;
|
|
|
+using System.Collections.Generic;
|
|
|
+using System.ComponentModel;
|
|
|
+using System.IO;
|
|
|
+using System.Linq;
|
|
|
+using System.Net.Http;
|
|
|
+using System.Net.Http.Json;
|
|
|
+using System.Text;
|
|
|
+using System.Threading.Tasks;
|
|
|
+using System.Xml.Linq;
|
|
|
+
|
|
|
+namespace TCCInvoice
|
|
|
+{
|
|
|
+ internal class InvoiceGenerator
|
|
|
+ {
|
|
|
+ #region Fields
|
|
|
+ private string dataNumber;
|
|
|
+ private DateTime dataDate = DateTime.Today;
|
|
|
+ private string sellerId = SELLER_ID;
|
|
|
+ private string buyerId;
|
|
|
+ private string buyerName;
|
|
|
+ private string customsClearanceMark;
|
|
|
+ private int salesAmount;
|
|
|
+ private int freeTaxSalesAmount = FREE_ZERO_TAX_SALES_AMOUNT;
|
|
|
+ private int zeroTaxSalesAmount = FREE_ZERO_TAX_SALES_AMOUNT;
|
|
|
+ private string invoiceType = INVOICE_TYPE;
|
|
|
+ private string randomNumber;
|
|
|
+ private int taxType = TAX_TYPE;
|
|
|
+ private double taxRate = TAX_RATE;
|
|
|
+ private int taxAmount;
|
|
|
+ private int totalAmount;
|
|
|
+ private string printMark = DEFAULT_PRINT_MARK;
|
|
|
+ private string carrierType;
|
|
|
+ private string carrierId1;
|
|
|
+ private string carrierId2;
|
|
|
+ private string mainRemark;
|
|
|
+ private int donateMark;
|
|
|
+ private string nPOBAN;
|
|
|
+ private string contactEmail;
|
|
|
+ private string contactAddress;
|
|
|
+ private string contactPhone;
|
|
|
+ private List<InvoiceItem> invoiceItems = new List<InvoiceItem>();
|
|
|
+ private Dictionary<string, string> DataRandomNoPair = new Dictionary<string, string>();
|
|
|
+
|
|
|
+ private FileSystemWatcher preInvoiceResponseWatcher;
|
|
|
+ private FileSystemWatcher sellerInvoiceResponseWatcher;
|
|
|
+ private FileSystemWatcher sellerInvoiceFailureWatcher;
|
|
|
+ private List<InvoiceResponseItem> invoiceResponseItemList = new List<InvoiceResponseItem>();
|
|
|
+ private string responseDataNumber;
|
|
|
+ private string responseInvoiceError;
|
|
|
+ private int totalQuantity = 0;
|
|
|
+ private int count = 0;
|
|
|
+ #endregion
|
|
|
+
|
|
|
+ #region Constants
|
|
|
+ private const string SELLER_ID = "83196607";
|
|
|
+ private const string DEFAULT_BUYER_ID = "0000000000";
|
|
|
+ private const int FREE_ZERO_TAX_SALES_AMOUNT = 0;
|
|
|
+ private const string INVOICE_TYPE = "07";
|
|
|
+ private const int TAX_TYPE = 1;
|
|
|
+ private const double TAX_RATE = 0.05;
|
|
|
+ private const string DEFAULT_PRINT_MARK = "N";
|
|
|
+ /// <summary>
|
|
|
+ /// WEB : https://gcis.nat.g0v.tw/id/30435973
|
|
|
+ /// API : https://gcis.nat.g0v.tw/api/show/30435973
|
|
|
+ /// </summary>
|
|
|
+ private const string BUSINESS_NAME_API = "https://gcis.nat.g0v.tw/api/show/";
|
|
|
+ private const string CARRIER_TYPE_BARCODE = "3J0002";
|
|
|
+ private const string DEFAULT_LOVECODE = "4997276";
|
|
|
+
|
|
|
+ private const string PREINVOICE_PATH = @"C:\UXB2B_EIVO\PreInvoice\";
|
|
|
+ private const string PREINVOICE_RESPONSE_PATH = @"C:\UXB2B_EIVO\PreInvoice(Response)\";
|
|
|
+ private const string SELLER_INVOICE_RESPONSE_PATH = @"C:\Gateway-83196607\logs\InvoiceNoInspector\SellerInvoice(Response)\";
|
|
|
+ private const string SELLER_INVOICE_PATH = @"C:\Gateway-83196607\logs\InvoiceNoInspector\SellerInvoice\";
|
|
|
+ private const string SELLER_INVOICE_FAILURE_PATH = @"C:\Gateway-83196607\logs\InvoiceNoInspector\SellerInvoice(Failure)\";
|
|
|
+ private const string RESPONSE_BACKUP_PATH = @"C:\UXB2B_EIVO\ResponseBackup\";
|
|
|
+ #endregion
|
|
|
+
|
|
|
+ #region Properties
|
|
|
+ /// <summary>
|
|
|
+ /// (必填)單據號碼,須為唯一值,長度上限20
|
|
|
+ /// </summary>
|
|
|
+ public string DataNumber
|
|
|
+ {
|
|
|
+ set
|
|
|
+ {
|
|
|
+ Reset();
|
|
|
+ dataNumber = value;
|
|
|
+ DataRandomNoPair.Add(value, this.randomNumber);
|
|
|
+ Console.WriteLine("DataNumber = " + dataNumber);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ /// <summary>
|
|
|
+ /// (必填)發票日期
|
|
|
+ /// </summary>
|
|
|
+ public DateTime DataDate
|
|
|
+ {
|
|
|
+ set => dataDate = value;
|
|
|
+ }
|
|
|
+ /// <summary>
|
|
|
+ /// (選填)賣方統編,預設值83196607
|
|
|
+ /// </summary>
|
|
|
+ public string SellerId
|
|
|
+ {
|
|
|
+ set => sellerId = value;
|
|
|
+ }
|
|
|
+ /// <summary>
|
|
|
+ /// (必填)買方統編,若無統編請填null或空字串,系統自動帶入預設值0000000000
|
|
|
+ /// </summary>
|
|
|
+ public string BuyerId
|
|
|
+ {
|
|
|
+ set
|
|
|
+ {
|
|
|
+ buyerId = string.IsNullOrEmpty(value) ? DEFAULT_BUYER_ID : value;
|
|
|
+
|
|
|
+ if (buyerId.Equals(DEFAULT_BUYER_ID)) // 一般消費者
|
|
|
+ {
|
|
|
+ buyerName = GenerateRandom4Digit();
|
|
|
+ }
|
|
|
+ else // 有統編
|
|
|
+ {
|
|
|
+ GetBuyerNameFromBuyerIdAsync().Wait();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ /// <summary>
|
|
|
+ /// (必填)含稅總額
|
|
|
+ /// </summary>
|
|
|
+ public int TotalAmount
|
|
|
+ {
|
|
|
+ set
|
|
|
+ {
|
|
|
+ totalAmount = value;
|
|
|
+
|
|
|
+ if (string.IsNullOrEmpty(buyerId))
|
|
|
+ {
|
|
|
+ BuyerId = null;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (buyerId.Equals(DEFAULT_BUYER_ID)) // 一般消費者
|
|
|
+ {
|
|
|
+ salesAmount = totalAmount;
|
|
|
+ taxAmount = 0;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ salesAmount = (int)Math.Round((double)totalAmount / (1 + taxRate), 0, MidpointRounding.AwayFromZero);
|
|
|
+ taxAmount = totalAmount - salesAmount;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ /// <summary>
|
|
|
+ /// (必填)手機條碼載具,若無手機條碼請填null
|
|
|
+ /// </summary>
|
|
|
+ public string CarrierId1
|
|
|
+ {
|
|
|
+ set
|
|
|
+ {
|
|
|
+ carrierId1 = value;
|
|
|
+ carrierId2 = carrierId1;
|
|
|
+ carrierType = String.IsNullOrEmpty(carrierId1) ? null : CARRIER_TYPE_BARCODE;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ /// <summary>
|
|
|
+ /// (選填)總備註,最多200字
|
|
|
+ /// </summary>
|
|
|
+ public string MainRemark
|
|
|
+ {
|
|
|
+ set => mainRemark = value;
|
|
|
+ }
|
|
|
+ /// <summary>
|
|
|
+ /// (選填)0代表不捐贈;1代表捐贈。預設值為0
|
|
|
+ /// </summary>
|
|
|
+ public int DonateMark
|
|
|
+ {
|
|
|
+ set
|
|
|
+ {
|
|
|
+ donateMark = value;
|
|
|
+
|
|
|
+ donateMark = donateMark != 0 ? 1 : 0;
|
|
|
+ nPOBAN = donateMark == 1 ? DEFAULT_LOVECODE : null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ /// <summary>
|
|
|
+ /// (選填)消費者的email address
|
|
|
+ /// </summary>
|
|
|
+ public string ContactEmail
|
|
|
+ {
|
|
|
+ set => contactEmail = value;
|
|
|
+ }
|
|
|
+ /// <summary>
|
|
|
+ /// (選填)消費者的聯絡地址
|
|
|
+ /// </summary>
|
|
|
+ public string ContactAddress
|
|
|
+ {
|
|
|
+ set => contactAddress = value;
|
|
|
+ }
|
|
|
+ /// <summary>
|
|
|
+ /// (選填)消費者的聯絡電話
|
|
|
+ /// </summary>
|
|
|
+ public string ContactPhone
|
|
|
+ {
|
|
|
+ set => contactPhone = value;
|
|
|
+ }
|
|
|
+ #endregion
|
|
|
+
|
|
|
+ #region Constructors
|
|
|
+ public InvoiceGenerator(int quantity)
|
|
|
+ {
|
|
|
+ totalQuantity = quantity;
|
|
|
+ preInvoiceResponseWatcher = new FileSystemWatcher()
|
|
|
+ {
|
|
|
+ Path = PREINVOICE_RESPONSE_PATH,
|
|
|
+ Filter = "*.xml",
|
|
|
+ NotifyFilter = NotifyFilters.LastWrite,
|
|
|
+ IncludeSubdirectories = false,
|
|
|
+ EnableRaisingEvents = true,
|
|
|
+ };
|
|
|
+ preInvoiceResponseWatcher.Changed += PreInvoiceResponse_Changed;
|
|
|
+
|
|
|
+ sellerInvoiceResponseWatcher = new FileSystemWatcher()
|
|
|
+ {
|
|
|
+ Path = SELLER_INVOICE_RESPONSE_PATH,
|
|
|
+ Filter = "*.xml",
|
|
|
+ NotifyFilter = NotifyFilters.LastWrite,
|
|
|
+ IncludeSubdirectories = false,
|
|
|
+ EnableRaisingEvents = true,
|
|
|
+ };
|
|
|
+ sellerInvoiceResponseWatcher.Changed += SellerInvoiceResponse_Changed;
|
|
|
+
|
|
|
+ sellerInvoiceFailureWatcher = new FileSystemWatcher()
|
|
|
+ {
|
|
|
+ Path = SELLER_INVOICE_FAILURE_PATH,
|
|
|
+ Filter = "*.xml",
|
|
|
+ NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.LastAccess,
|
|
|
+ IncludeSubdirectories = false,
|
|
|
+ EnableRaisingEvents = true,
|
|
|
+ };
|
|
|
+ sellerInvoiceFailureWatcher.Changed += SellerInvoiceFailure_Changed;
|
|
|
+ }
|
|
|
+ #endregion
|
|
|
+
|
|
|
+ #region Events & EventHandlers
|
|
|
+ public delegate void InvoiceCompleteEventHandler(List<InvoiceResponseItem> responseItems);
|
|
|
+ public event InvoiceCompleteEventHandler InvoiceCompleted;
|
|
|
+
|
|
|
+ private void PreInvoiceResponse_Changed(object sender, FileSystemEventArgs e)
|
|
|
+ {
|
|
|
+ XDocument xDoc = XDocument.Load(e.FullPath);
|
|
|
+
|
|
|
+ string status = xDoc.Descendants("Status").ElementAt(0).Value;
|
|
|
+ if (status == "1") // 配號成功
|
|
|
+ {
|
|
|
+ string invoicenumber = xDoc.Descendants("InvoiceNumber").ElementAt(0).Value;
|
|
|
+ string datanumber = xDoc.Descendants("DataNumber").ElementAt(0).Value;
|
|
|
+ string invoicedate = xDoc.Descendants("InvoiceDate").ElementAt(0).Value;
|
|
|
+ string invoicetime = xDoc.Descendants("InvoiceTime").ElementAt(0).Value;
|
|
|
+ string invoiceError = null;
|
|
|
+ string value;
|
|
|
+ if (DataRandomNoPair.TryGetValue(datanumber, out value))
|
|
|
+ {
|
|
|
+ Console.WriteLine("Fetched value: {0}", value);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ value = "7878";
|
|
|
+ Console.WriteLine("No such key: {0}", datanumber);
|
|
|
+ }
|
|
|
+ invoiceResponseItemList.Add(new InvoiceResponseItem
|
|
|
+ {
|
|
|
+ ResponseStatus = "配號成功",
|
|
|
+ ResponseInvoiceNumber = invoicenumber,
|
|
|
+ ResponseDataNumber = datanumber,
|
|
|
+ ResponseInvoiceDate = invoicedate,
|
|
|
+ ResponseInvoiceTime = invoicetime,
|
|
|
+ ResponseRandomNumber = value,
|
|
|
+ ResponseInvoiceError = invoiceError
|
|
|
+ });
|
|
|
+ }
|
|
|
+ else // 配號失敗
|
|
|
+ {
|
|
|
+ responseInvoiceError = xDoc.Descendants("Description").ElementAt(0).Value;
|
|
|
+ responseDataNumber = xDoc.Descendants("DataNumber").ElementAt(0).Value;
|
|
|
+ randomNumber = null;
|
|
|
+
|
|
|
+ invoiceResponseItemList.Add(new InvoiceResponseItem
|
|
|
+ {
|
|
|
+ ResponseStatus = "配號失敗",
|
|
|
+ ResponseInvoiceNumber = null,
|
|
|
+ ResponseDataNumber = responseDataNumber,
|
|
|
+ ResponseInvoiceDate = null,
|
|
|
+ ResponseInvoiceTime = null,
|
|
|
+ ResponseRandomNumber = null,
|
|
|
+ ResponseInvoiceError = responseInvoiceError
|
|
|
+ });
|
|
|
+
|
|
|
+ count++;
|
|
|
+ if (count == totalQuantity)
|
|
|
+ {
|
|
|
+ InvoiceCompleted?.Invoke(invoiceResponseItemList);
|
|
|
+ TurnOffFileSystemWatcher();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void SellerInvoiceResponse_Changed(object sender, FileSystemEventArgs e)
|
|
|
+ {
|
|
|
+ XDocument xDoc = XDocument.Load(e.FullPath);
|
|
|
+
|
|
|
+ int index = -1;
|
|
|
+ string status = xDoc.Descendants("Status").ElementAt(0).Value;
|
|
|
+ string invoicenumber = xDoc.Descendants("InvoiceNumber").ElementAt(0).Value;
|
|
|
+ foreach (InvoiceResponseItem item in invoiceResponseItemList)
|
|
|
+ {
|
|
|
+ if (item.ResponseInvoiceNumber == invoicenumber)
|
|
|
+ {
|
|
|
+ index = invoiceResponseItemList.IndexOf(item);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (index == -1)
|
|
|
+ {
|
|
|
+ return; // something wrong
|
|
|
+ }
|
|
|
+
|
|
|
+ if (status == "1") // 上傳成功
|
|
|
+ {
|
|
|
+ invoiceResponseItemList[index].ResponseStatus = "上傳成功";
|
|
|
+
|
|
|
+ count++;
|
|
|
+ if (count == totalQuantity)
|
|
|
+ {
|
|
|
+ InvoiceCompleted?.Invoke(invoiceResponseItemList);
|
|
|
+ TurnOffFileSystemWatcher();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else // 上傳失敗
|
|
|
+ {
|
|
|
+ string invoiceerror = xDoc.Descendants("Description").ElementAt(0).Value;
|
|
|
+
|
|
|
+ invoiceResponseItemList[index].ResponseStatus = "上傳失敗";
|
|
|
+ invoiceResponseItemList[index].ResponseInvoiceError = invoiceerror;
|
|
|
+
|
|
|
+ count++;
|
|
|
+ if (count == totalQuantity)
|
|
|
+ {
|
|
|
+ InvoiceCompleted?.Invoke(invoiceResponseItemList);
|
|
|
+ TurnOffFileSystemWatcher();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void SellerInvoiceFailure_Changed(object sender, FileSystemEventArgs e)
|
|
|
+ {
|
|
|
+ string failurePath = e.FullPath;
|
|
|
+ string responsePath = SELLER_INVOICE_RESPONSE_PATH + e.Name;
|
|
|
+ string sellerinvoicePath = SELLER_INVOICE_PATH + e.Name;
|
|
|
+
|
|
|
+ // 當SELLER_INVOICE_FAILURE_PATH出現檔案時,先等待500毫秒,
|
|
|
+ // 再去檢查SELLER_INVOICE_RESPONSE_PATH是否也出現同一個檔名的檔案,
|
|
|
+ // 如果沒有出現,代表應該是網優SQL連線失敗造成的發票上傳失敗,
|
|
|
+ // 此時會先等待180秒,再把該檔案複製到SELLER_INVOICE_PATH,讓Gateway自動重新上傳
|
|
|
+ Task.Run(async delegate
|
|
|
+ {
|
|
|
+ await Task.Delay(500);
|
|
|
+ if (!File.Exists(responsePath)) // 可能是網優SQL連線失敗,需要嘗試重新上傳
|
|
|
+ {
|
|
|
+ await Task.Delay(30000);
|
|
|
+ File.Copy(failurePath, sellerinvoicePath);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+ #endregion
|
|
|
+
|
|
|
+ #region Instance Methods
|
|
|
+ /// <summary>
|
|
|
+ /// 加入消費品項明細
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="sequenceNumber">發票明細之排列序號</param>
|
|
|
+ /// <param name="description">產品名稱</param>
|
|
|
+ /// <param name="quantity">數量 decimal(16,4)</param>
|
|
|
+ /// <param name="unit">單位,若不填請輸入null</param>
|
|
|
+ /// <param name="unitPrice">單價 decimal(16,4)</param>
|
|
|
+ /// <param name="amount">總金額 decimal(16,4)</param>
|
|
|
+ /// <param name="remark">單一欄位備註,若不填請輸入null</param>
|
|
|
+ public void AddInvoiceItem(int sequenceNumber, string description, double quantity,
|
|
|
+ string unit, double unitPrice, double amount, string remark)
|
|
|
+ {
|
|
|
+ InvoiceItem item = new InvoiceItem()
|
|
|
+ {
|
|
|
+ SequenceNumber = sequenceNumber,
|
|
|
+ Description = description,
|
|
|
+ Quantity = quantity,
|
|
|
+ Unit = unit,
|
|
|
+ UnitPrice = unitPrice,
|
|
|
+ Amount = amount,
|
|
|
+ Remark = remark
|
|
|
+ };
|
|
|
+
|
|
|
+ invoiceItems.Add(item);
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 產生Preinvoice檔案,交由網優Gateway開立發票,並重置InvoiceGenerator物件的欄位值
|
|
|
+ /// </summary>
|
|
|
+ public void GetInvoiceResponse()
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ GeneratePreinvoiceXML();
|
|
|
+ }
|
|
|
+ catch (Exception)
|
|
|
+ {
|
|
|
+ ;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void GeneratePreinvoiceXML()
|
|
|
+ {
|
|
|
+ XDocument xDoc = new XDocument();
|
|
|
+ xDoc.Declaration = new XDeclaration("1.0", "UTF-8", "");
|
|
|
+
|
|
|
+ XElement root = new XElement("InvoiceRoot");
|
|
|
+ root.Add(new XElement("Invoice",
|
|
|
+ new XElement("DataNumber", dataNumber),
|
|
|
+ new XElement("DataDate", dataDate.ToString("yyyy/MM/dd")),
|
|
|
+ new XElement("SellerId", sellerId),
|
|
|
+ new XElement("BuyerId", buyerId),
|
|
|
+ new XElement("BuyerName", buyerName),
|
|
|
+ new XElement("CustomsClearanceMark", customsClearanceMark),
|
|
|
+ new XElement("SalesAmount", salesAmount),
|
|
|
+ new XElement("FreeTaxSalesAmount", freeTaxSalesAmount),
|
|
|
+ new XElement("ZeroTaxSalesAmount", zeroTaxSalesAmount),
|
|
|
+ new XElement("RandomNumber", randomNumber),
|
|
|
+ new XElement("InvoiceType", invoiceType),
|
|
|
+ new XElement("TaxType", taxType),
|
|
|
+ new XElement("TaxRate", taxRate),
|
|
|
+ new XElement("TaxAmount", taxAmount),
|
|
|
+ new XElement("TotalAmount", totalAmount),
|
|
|
+ new XElement("PrintMark", printMark),
|
|
|
+ new XElement("CarrierType", carrierType),
|
|
|
+ new XElement("CarrierId1", carrierId1),
|
|
|
+ new XElement("CarrierId2", carrierId2),
|
|
|
+ new XElement("MainRemark", mainRemark),
|
|
|
+ new XElement("DonateMark", donateMark),
|
|
|
+ new XElement("NPOBAN", nPOBAN),
|
|
|
+ new XElement("Contact",
|
|
|
+ new XElement("Email", contactEmail),
|
|
|
+ new XElement("Address", contactAddress),
|
|
|
+ new XElement("TEL", contactPhone)
|
|
|
+ )
|
|
|
+ )
|
|
|
+ );
|
|
|
+
|
|
|
+ foreach (InvoiceItem item in invoiceItems)
|
|
|
+ {
|
|
|
+ XElement invoice = root.Element("Invoice");
|
|
|
+ invoice.Add(new XElement("InvoiceItem",
|
|
|
+ new XElement("SequenceNumber", item.SequenceNumber),
|
|
|
+ new XElement("Description", item.Description),
|
|
|
+ new XElement("Quantity", item.Quantity),
|
|
|
+ new XElement("Unit", item.Unit),
|
|
|
+ new XElement("UnitPrice", item.UnitPrice),
|
|
|
+ new XElement("Amount", item.Amount),
|
|
|
+ new XElement("Remark", item.Remark)
|
|
|
+ )
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
+ xDoc.Add(root);
|
|
|
+ xDoc.Save(PREINVOICE_PATH + dataNumber + ".xml");
|
|
|
+ Console.WriteLine(PREINVOICE_PATH + dataNumber + ".xml");
|
|
|
+ }
|
|
|
+
|
|
|
+ private void Reset()
|
|
|
+ {
|
|
|
+ dataNumber = null;
|
|
|
+ dataDate = DateTime.Today;
|
|
|
+ buyerId = null;
|
|
|
+ buyerName = null;
|
|
|
+ customsClearanceMark = null;
|
|
|
+ randomNumber = GenerateRandom4Digit();
|
|
|
+ salesAmount = 0;
|
|
|
+ taxAmount = 0;
|
|
|
+ totalAmount = 0;
|
|
|
+ carrierType = null;
|
|
|
+ carrierId1 = null;
|
|
|
+ carrierId2 = null;
|
|
|
+ mainRemark = null;
|
|
|
+ donateMark = 0;
|
|
|
+ nPOBAN = null;
|
|
|
+ contactEmail = null;
|
|
|
+ contactAddress = null;
|
|
|
+ contactPhone = null;
|
|
|
+ invoiceItems.Clear();
|
|
|
+
|
|
|
+ responseDataNumber = null;
|
|
|
+ responseInvoiceError = null;
|
|
|
+ }
|
|
|
+
|
|
|
+ private async Task GetBuyerNameFromBuyerIdAsync()
|
|
|
+ {
|
|
|
+ using (var client = new HttpClient())
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ var result = await client.GetFromJsonAsync<BusinessName>(BUSINESS_NAME_API + buyerId);
|
|
|
+
|
|
|
+ //buyerName = GenerateRandom4Digit();
|
|
|
+ if (result.data != null && result.data.財政部 != null)
|
|
|
+ {
|
|
|
+ buyerName = result.data.財政部.營業人名稱; // 1st try
|
|
|
+ if (string.IsNullOrEmpty(buyerName))
|
|
|
+ {
|
|
|
+ buyerName = result.data.財政部.單位名稱; // 2nd try
|
|
|
+ if (string.IsNullOrEmpty(buyerName))
|
|
|
+ {
|
|
|
+ buyerName = result.data.名稱; // last try
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (string.IsNullOrEmpty(buyerName))
|
|
|
+ {
|
|
|
+ buyerName = GenerateRandom4Digit();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ catch (Exception)
|
|
|
+ {
|
|
|
+ buyerName = GenerateRandom4Digit();
|
|
|
+ }
|
|
|
+ };
|
|
|
+ }
|
|
|
+
|
|
|
+ private string GenerateRandom4Digit()
|
|
|
+ {
|
|
|
+ int min = 0;
|
|
|
+ int max = 9999;
|
|
|
+ Random rdm = new Random(Guid.NewGuid().GetHashCode());
|
|
|
+ return rdm.Next(min, max).ToString().PadLeft(4, '0');
|
|
|
+ }
|
|
|
+
|
|
|
+ private void TurnOffFileSystemWatcher()
|
|
|
+ {
|
|
|
+ if (preInvoiceResponseWatcher != null)
|
|
|
+ {
|
|
|
+ preInvoiceResponseWatcher.EnableRaisingEvents = false;
|
|
|
+ preInvoiceResponseWatcher.Dispose();
|
|
|
+ preInvoiceResponseWatcher = null;
|
|
|
+ }
|
|
|
+ if (sellerInvoiceResponseWatcher != null)
|
|
|
+ {
|
|
|
+ sellerInvoiceResponseWatcher.EnableRaisingEvents = false;
|
|
|
+ sellerInvoiceResponseWatcher.Dispose();
|
|
|
+ sellerInvoiceResponseWatcher = null;
|
|
|
+ }
|
|
|
+ if (sellerInvoiceFailureWatcher != null)
|
|
|
+ {
|
|
|
+ sellerInvoiceFailureWatcher.EnableRaisingEvents = false;
|
|
|
+ sellerInvoiceFailureWatcher.Dispose();
|
|
|
+ sellerInvoiceFailureWatcher = null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ #endregion
|
|
|
+
|
|
|
+ #region Nested Classes
|
|
|
+ internal class InvoiceResponseItem
|
|
|
+ {
|
|
|
+ public string ResponseStatus { get; set; }
|
|
|
+ public string ResponseInvoiceNumber { get; set; }
|
|
|
+ public string ResponseDataNumber { get; set; }
|
|
|
+ public string ResponseInvoiceDate { get; set; }
|
|
|
+ public string ResponseInvoiceTime { get; set; }
|
|
|
+ public string ResponseRandomNumber { get; set; }
|
|
|
+ public string ResponseInvoiceError { get; set; }
|
|
|
+ }
|
|
|
+ internal class InvoiceItem
|
|
|
+ {
|
|
|
+ public int SequenceNumber { get; set; }
|
|
|
+ public string Description { get; set; }
|
|
|
+ public double Quantity { get; set; }
|
|
|
+ public string Unit { get; set; }
|
|
|
+ public double UnitPrice { get; set; }
|
|
|
+ public double Amount { get; set; }
|
|
|
+ public string Remark { get; set; }
|
|
|
+ }
|
|
|
+ internal class BusinessName
|
|
|
+ {
|
|
|
+ public Data data { get; set; }
|
|
|
+ }
|
|
|
+
|
|
|
+ internal class Data
|
|
|
+ {
|
|
|
+ public string 名稱 { get; set; }
|
|
|
+ public 財政部 財政部 { get; set; }
|
|
|
+ }
|
|
|
+
|
|
|
+ internal class 財政部
|
|
|
+ {
|
|
|
+ public string 營業人名稱 { get; set; }
|
|
|
+ public string 單位名稱 { get; set; }
|
|
|
+ }
|
|
|
+ #endregion
|
|
|
+ }
|
|
|
+}
|