Эх сурвалжийг харах

1.預設捐贈,若有填統編或手機條碼就不捐贈; 2.未知的營業人名稱改為隨機四位數字; 3.發票開立事件通知中的carriernumber欄位,若有填手機條碼就回傳手機條碼,否則回傳null

Snoopy 3 жил өмнө
parent
commit
def549bf47

+ 30 - 23
TCCInvoice/InvoiceGenerator.cs

@@ -33,8 +33,8 @@ namespace TCCInvoice
         private string carrierId1;
         private string carrierId2;
         private string mainRemark;
-        private int donateMark;
-        private string nPOBAN;
+        private int donateMark = DEFAULT_DONATE_MARK;
+        private string nPOBAN = DEFAULT_LOVECODE;
         private string contactEmail;
         private string contactAddress;
         private string contactPhone;
@@ -45,7 +45,6 @@ namespace TCCInvoice
 
         #region Constants
         private const string SELLER_ID = "83196607";
-        private const string UNKNOWN_BUYER_NAME = "----"; // 未查詢到營業人名稱或一般消費者
         private const string DEFAULT_BUYER_ID = "0000000000";
         private const int FREE_ZERO_TAX_SALES_AMOUNT = 0;
         private const string INVOICE_TYPE = "07";
@@ -58,7 +57,8 @@ namespace TCCInvoice
         /// </summary>
         private const string BUSINESS_NAME_API = "http://gcis.nat.g0v.tw/api/show/";
         private const string CARRIER_TYPE_BARCODE = "3J0002";
-        private const string DEFAULT_LOVECODE = "";
+        private const int DEFAULT_DONATE_MARK = 1;
+        private const string DEFAULT_LOVECODE = "591"; // 待台泥提供愛心碼
 
         private const string PREINVOICE_PATH = @"C:\UXB2B_EIVO\PreInvoice\";
         private const string INVOICE_RESPONSE_PATH = @"C:\UXB2B_EIVO\PreInvoice(Response)\";
@@ -98,11 +98,15 @@ namespace TCCInvoice
 
                 if (buyerId.Equals(DEFAULT_BUYER_ID)) // 一般消費者
                 {
-                    buyerName = UNKNOWN_BUYER_NAME;
+                    buyerName = GenerateRandom4Digit().ToString();
                 }
                 else // 有統編
                 {
                     GetBuyerNameFromBuyerIdAsync().Wait();
+
+                    // 有填統編,不捐贈
+                    donateMark = 0;
+                    nPOBAN = null;
                 }
             }
         }
@@ -142,6 +146,13 @@ namespace TCCInvoice
                 carrierId1 = value;
                 carrierId2 = carrierId1;
                 carrierType = String.IsNullOrEmpty(carrierId1) ? null : CARRIER_TYPE_BARCODE;
+
+                // 如果有填手機條碼,就改為不捐贈
+                if (!String.IsNullOrEmpty(carrierId1))
+                {
+                    donateMark = 0;
+                    nPOBAN = null;
+                }
             }
         }
         /// <summary>
@@ -152,17 +163,6 @@ namespace TCCInvoice
             set => mainRemark = value; 
         }
         /// <summary>
-        /// (必填)不捐贈發票填0,捐贈發票填1
-        /// </summary>
-        public int DonateMark 
-        {
-            set
-            {
-                donateMark = value;
-                nPOBAN = donateMark == 1 ? DEFAULT_LOVECODE : null;
-            }
-        }
-        /// <summary>
         /// (選填)消費者的email address
         /// </summary>
         public string ContactEmail 
@@ -216,8 +216,7 @@ namespace TCCInvoice
                 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 carriernumber = null; 
-                // string carriernumber = xDoc.Descendants("CarrierId1").ElementAt(0).Value;
+                string carriernumber = carrierId1; 
 
                 InvoiceGenerated?.Invoke(datanumber, invoicenumber, invoicedate, invoicetime, carriernumber);
             }
@@ -344,10 +343,10 @@ namespace TCCInvoice
             taxAmount = 0;
             totalAmount = 0;
             carrierType = null;
-            carrierId1 = null;
-            carrierId2 = null;
+            //carrierId1 = null; // 不重置,保留到發票開立後的事件通知時回傳載具號碼
+            //carrierId2 = null; // 與carrierId1同進退
             mainRemark = null;
-            donateMark = 0;
+            donateMark = 1;
             nPOBAN = null;
             contactEmail = null;
             contactAddress = null;
@@ -363,7 +362,7 @@ namespace TCCInvoice
                 {
                     var result = await client.GetFromJsonAsync<BusinessName>(BUSINESS_NAME_API + buyerId);
 
-                    buyerName = UNKNOWN_BUYER_NAME;
+                    buyerName = GenerateRandom4Digit().ToString();
                     if (result.data != null && result.data.財政部 != null)
                     {
                         buyerName = result.data.財政部.營業人名稱; // 1st try
@@ -379,10 +378,18 @@ namespace TCCInvoice
                 }
                 catch (Exception)
                 {
-                    buyerName = UNKNOWN_BUYER_NAME;
+                    buyerName = GenerateRandom4Digit().ToString();
                 }
             };
         }
+
+        private int GenerateRandom4Digit()
+        {
+            int min = 1000;
+            int max = 9999;
+            Random rdm = new Random();
+            return rdm.Next(min, max);
+        }
         #endregion
 
         #region Nested Classes

+ 5 - 7
TCCInvoice/Program.cs

@@ -19,16 +19,15 @@ namespace TCCInvoice
             myInvoice.InvoiceGenerationFailed += MyInvoice_InvoiceGenerationFailed;
 
             // 填具待開立發票的相關資料(Preinvoice)
-            myInvoice.DataNumber = "20220205123456222";
+            myInvoice.DataNumber = "202202080058";
             myInvoice.DataDate = DateTime.Today;
             myInvoice.BuyerId = null; // 一般消費者
             myInvoice.TotalAmount = 524;
-            myInvoice.CarrierId1 = null;
-            myInvoice.DonateMark = 0;
+            myInvoice.CarrierId1 = "/CPDF.O2";
             myInvoice.ContactEmail = "snoopy.h.huang@outlook.com";
             myInvoice.ContactPhone = "0929168960";
-            myInvoice.AddInvoiceItem(0, "充電服務費", 41.5793, "度", 12, 499, "每度12元");
-            myInvoice.AddInvoiceItem(1, "占用費", 0.5, "小時", 50, 25, "每小時50元");
+            myInvoice.AddInvoiceItem(0, "充電服務費", 41.5793, "度", 12, 499, "充電服務費每度12元");
+            myInvoice.AddInvoiceItem(1, "占用費", 0.5, "小時", 50, 25, "占用費每小時50元");
             //開立發票
             myInvoice.GetInvoiceResponse();
 
@@ -37,8 +36,7 @@ namespace TCCInvoice
             //myInvoice.DataDate = DateTime.Today;
             //myInvoice.BuyerId = "30435973"; //營業人
             //myInvoice.TotalAmount = 1204;
-            //myInvoice.CarrierId1 = "/CPDF.O2";
-            //myInvoice.DonateMark = 0;
+            //myInvoice.CarrierId1 = null;
             //myInvoice.ContactEmail = "snoopy_huang@phihong.com.tw";
             //myInvoice.ContactPhone = "0972637981";
             //myInvoice.AddInvoiceItem(0, "充電服務費", 120.4130, "度", 10, 1204, "每度10元");

+ 1 - 0
TCCInvoice/TCCInvoice.csproj

@@ -24,6 +24,7 @@
     <DefineConstants>DEBUG;TRACE</DefineConstants>
     <ErrorReport>prompt</ErrorReport>
     <WarningLevel>4</WarningLevel>
+    <DocumentationFile>bin\Debug\TCCInvoice.xml</DocumentationFile>
   </PropertyGroup>
   <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
     <PlatformTarget>AnyCPU</PlatformTarget>