예제 #1
0
        //IncomeHandler-BatchGenReceiptAfterPayment
        /// <summary>
        /// Batch Process to generate tax invoice pdf report, receipt pdf report after payment on shared report folder
        /// </summary>
        /// <param name="UserId">employee no.</param>
        /// <param name="BatchDate">process datetime</param>
        /// <returns></returns>
        public doBatchProcessResult ICP010_BatchGenReceiptAfterPaymentProcess(string UserId, DateTime BatchDate)
        {
            #region Prepare
            //Handler
            IBillingHandler         billingHandler         = ServiceContainer.GetService <IBillingHandler>() as IBillingHandler;
            IBillingDocumentHandler billingDocumentHandler = ServiceContainer.GetService <IBillingDocumentHandler>() as IBillingDocumentHandler;
            IIncomeHandler          incomeHandler          = ServiceContainer.GetService <IIncomeHandler>() as IIncomeHandler;
            IIncomeDocumentHandler  incomeDocumentHandler  = ServiceContainer.GetService <IIncomeDocumentHandler>() as IIncomeDocumentHandler;

            //Get data
            List <doPaidInvoiceNoReceipt> paidInvoices = this.GetPaidInvoiceNoReceipt();

            //Initial batch result
            doBatchProcessResult result = new doBatchProcessResult();
            result.Result       = FlagType.C_FLAG_ON;
            result.BatchStatus  = null;
            result.Total        = paidInvoices.Count;
            result.Failed       = 0;
            result.Complete     = 0;
            result.ErrorMessage = string.Empty;
            result.BatchUser    = UserId;
            #endregion

            if (paidInvoices.Count > 0)
            {
                foreach (doPaidInvoiceNoReceipt doPaidInvoice in paidInvoices)
                {
                    using (TransactionScope scope = new TransactionScope())
                    {
                        try
                        {
                            doInvoice doInvoice = billingHandler.GetInvoice(doPaidInvoice.InvoiceNo);

                            if (doPaidInvoice.BillingTypeGroup != BillingTypeGroup.C_BILLING_TYPE_GROUP_SALE &&
                                (doPaidInvoice.IssueReceiptTiming == IssueRecieptTime.C_ISSUE_REC_TIME_NOT_ISSUE ||
                                 doPaidInvoice.IssueReceiptTiming == IssueRecieptTime.C_ISSUE_REC_TIME_AFTER_PAYMENT)
                                )
                            {
                                #region Issue tax invoice

                                tbt_TaxInvoice doCheckTaxInvoice = billingHandler.GetTaxInvoiceData(doInvoice.InvoiceNo, doInvoice.InvoiceOCC); //Add by Jutarat A. on 04072013

                                if (doCheckTaxInvoice != null)                                                                                  //Add by Jutarat A. on 14112013
                                {
                                    //Add by Jutarat A. on 17102013
                                    List <tbt_TaxInvoice> doCheckTaxInvoiceList = new List <tbt_TaxInvoice>();
                                    doCheckTaxInvoiceList.Add(doCheckTaxInvoice);

                                    doCheckTaxInvoiceList = (from t in doCheckTaxInvoiceList
                                                             where t.TaxInvoiceCanceledFlag == false
                                                             select t).ToList <tbt_TaxInvoice>();

                                    if (doCheckTaxInvoiceList != null && doCheckTaxInvoiceList.Count > 0)
                                    {
                                        doCheckTaxInvoice = CommonUtil.CloneObject <tbt_TaxInvoice, tbt_TaxInvoice>(doCheckTaxInvoiceList[0]);
                                    }
                                    else
                                    {
                                        doCheckTaxInvoice = null;
                                    }
                                    //End Add
                                }

                                if (doCheckTaxInvoice == null) //Add by Jutarat A. on 04072013 (Check not exist TaxInvoice)
                                {
                                    tbt_TaxInvoice doTaxInvoice = billingHandler.IssueTaxInvoice(
                                        doInvoice,
                                        doPaidInvoice.PaymentDate,
                                        ProcessID.C_PROCESS_ID_GENERATE_RECEIPT_AFTER_PAYMENT,
                                        BatchDate);
                                    if (doTaxInvoice != null)
                                    {
                                        // Comment by Jirawat Jannet
                                        //BLR020 TaxInvoice report
                                        //billingDocumentHandler.GenerateBLR020FilePath(
                                        //    doTaxInvoice.TaxInvoiceNo
                                        //    , ProcessID.C_PROCESS_ID_GENERATE_RECEIPT_AFTER_PAYMENT
                                        //    , BatchDate);
                                        throw new Exception("กำลังดำเนินการแก้ไข report BLR020");
                                    }
                                    else
                                    {
                                        throw new Exception("Error generate tax invoice");
                                    }
                                }

                                #endregion

                                #region Issue receipt
                                tbt_Receipt doReceipt = incomeHandler.IssueReceipt(doInvoice, doPaidInvoice.PaymentDate, ProcessID.C_PROCESS_ID_GENERATE_RECEIPT_AFTER_PAYMENT, BatchDate, false); //Add (isWriteTransLog) by Jutarat A. on 07062013
                                if (doReceipt != null)
                                {
                                    //ICR010 Receipt report
                                    incomeDocumentHandler.GenerateICR010FilePath(doReceipt.ReceiptNo
                                                                                 , ProcessID.C_PROCESS_ID_GENERATE_RECEIPT_AFTER_PAYMENT
                                                                                 , BatchDate);

                                    if (doInvoice.BillingTypeCode == BillingType.C_BILLING_TYPE_DEPOSIT)
                                    {
                                        bool isSuccess = billingHandler.UpdateReceiptNoDepositFee(doPaidInvoice.InvoiceNo, doReceipt.ReceiptNo, ProcessID.C_PROCESS_ID_GENERATE_RECEIPT_AFTER_PAYMENT, BatchDate);
                                        if (!isSuccess)
                                        {
                                            throw new Exception("Error update receipt no to deposit table");
                                        }
                                    }
                                }
                                else
                                {
                                    throw new Exception("Error generate receipt");
                                }
                                #endregion
                            }
                            else
                            {
                                #region Issued receipt only

                                DateTime receiptDate = doPaidInvoice.PaymentDate;

                                if (doPaidInvoice.BillingTypeGroup != BillingTypeGroup.C_BILLING_TYPE_GROUP_SALE &&
                                    doPaidInvoice.IssueReceiptTiming == IssueRecieptTime.C_ISSUE_REC_TIME_SAME_INV &&
                                    (doPaidInvoice.PaymentMethod == PaymentMethod.C_PAYMENT_METHOD_BANK_TRANSFER ||
                                     doPaidInvoice.PaymentMethod == PaymentMethodType.C_PAYMENT_METHOD_MESSENGER))
                                {
                                    receiptDate = doPaidInvoice.IssueInvDate.Value;
                                }
                                tbt_Receipt doReceipt = incomeHandler.IssueReceipt(doInvoice, receiptDate, ProcessID.C_PROCESS_ID_GENERATE_RECEIPT_AFTER_PAYMENT, BatchDate, false); //Add (isWriteTransLog) by Jutarat A. on 07062013
                                if (doReceipt != null)
                                {
                                    //ICR010 Receipt report
                                    incomeDocumentHandler.GenerateICR010FilePath(doReceipt.ReceiptNo
                                                                                 , ProcessID.C_PROCESS_ID_GENERATE_RECEIPT_AFTER_PAYMENT
                                                                                 , BatchDate);

                                    if (doInvoice.BillingTypeCode == BillingType.C_BILLING_TYPE_DEPOSIT)
                                    {
                                        bool isSuccess = billingHandler.UpdateReceiptNoDepositFee(doPaidInvoice.InvoiceNo, doReceipt.ReceiptNo, ProcessID.C_PROCESS_ID_GENERATE_RECEIPT_AFTER_PAYMENT, BatchDate);
                                        if (!isSuccess)
                                        {
                                            throw new Exception("Error update receipt no to deposit table");
                                        }
                                    }
                                }
                                else
                                {
                                    throw new Exception("Error generate receipt");
                                }
                                #endregion
                            }

                            //Success
                            scope.Complete();
                            result.Complete++;
                        }
                        catch (Exception ex)
                        {
                            scope.Dispose();
                            result.Failed++;
                            result.ErrorMessage += string.Format("Invoice no. {0} has Error : {1} {2}\n", doPaidInvoice.InvoiceNo, ex.Message, ex.InnerException != null ? ex.InnerException.Message : string.Empty);
                        }
                    }
                }
                //Update batch result,     at lease one transaction fail => batch fail
                result.Result = (result.Failed == 0);
            }
            return(result);
        }