protected static FR_Guid Execute(DbConnection Connection, DbTransaction Transaction, P_L3PY_CPfB_1506 Parameter, CSV2Core.SessionSecurity.SessionSecurityTicket securityTicket = null)
        {
            #region UserCode
            var returnValue = new FR_Guid();

            /*
             * 0. Get current fiscal year
             * 1. Get current's account business participant
             * 2. Get booking accounts for bill's revenue participant and debtor participant
             * 3. Find unpayed amount for bill
             * 4. Create accounting transaction with booking lines
             * 5. Create new bill header assigned payment entry and assign accunting transaction to installments
             * 6. Update dunning process if needed
             */

            #region 0. Load current fiscal year

            Guid currentFiscalYearID = CL2_FiscalYear.Complex.Retrieval.cls_Get_Current_FiscalYear.Invoke(Connection, Transaction, securityTicket).Result.ACC_FiscalYearID;

            #endregion

            #region 1. Get current's account business participant

            Guid accountBPID = CL1_USR.ORM_USR_Account.Query.Search(Connection, Transaction,
                                                                    new CL1_USR.ORM_USR_Account.Query
            {
                USR_AccountID = securityTicket.AccountID,
                IsDeleted     = false,
                Tenant_RefID  = securityTicket.TenantID
            }).Single().BusinessParticipant_RefID;

            #endregion

            #region 2. Get booking accounts for bill's revenue participant and debtor participant

            var param = new CL3_Payments.Complex.Retrieval.P_L3PY_GBAfBP_1521
            {
                FiscalYearID = currentFiscalYearID,
                BillHeaderID = Parameter.BillHeaderID
            };

            var billDataWithBookingAccounts = CL3_Payments.Complex.Retrieval.cls_Get_Booking_Accounts_for_Bill_Participants.Invoke(Connection, Transaction, param, securityTicket).Result;

            #endregion

            #region 3. Find unpayed amount for bill

            var payedParam = new CL3_Payments.Atomic.Retrieval.P_L3PY_GPAfB_1312
            {
                BillHeaderID_List = new Guid[] { Parameter.BillHeaderID }
            };
            var payed = CL3_Payments.Atomic.Retrieval.cls_Get_Payed_Amount_for_Bills.Invoke(Connection, Transaction, payedParam, securityTicket).Result;

            decimal amountLeftToBePayed = billDataWithBookingAccounts.Bill.TotalValuee_with_DunningFees_IncludingTax - payed.Single(x => x.BillHeaderID == Parameter.BillHeaderID).TotalPayedValue;

            // check if amount left to be payed is zero or if the amount for the current payment is larger than amount that is left to pay and send error status
            if (Decimal.Compare(amountLeftToBePayed, Decimal.Zero) == 0)
            {
                throw new Exception("Amount left to be payed is zero.");
            }
            else if (Decimal.Compare((amountLeftToBePayed - Parameter.Amount), Decimal.Zero) < 0)
            {
                throw new Exception("Amount for the current payment is larger than amount that is left to pay for this bill");
            }

            #endregion

            #region 4. Create accounting transaction with booking lines

            var transactionParam = new CL3_BookingAccounts.Complex.Manipulation.P_L3BA_CATwBL_1315
            {
                AccountingTransactionTypeID = Guid.Empty,
                CurrencyID        = billDataWithBookingAccounts.Bill.CurrencyID,
                DateOfTransaction = Parameter.PaymentDate
            };

            var bookingLines = new List <CL3_BookingAccounts.Complex.Manipulation.P_L3BA_CATwBL_1315a>();

            CL3_BookingAccounts.Complex.Manipulation.P_L3BA_CATwBL_1315a bookingLine = null;

            #region Customer Account booking line

            bookingLine = new CL3_BookingAccounts.Complex.Manipulation.P_L3BA_CATwBL_1315a();
            bookingLine.BookingAccountID = billDataWithBookingAccounts.CustomerAccount.BookingAccountID;
            // Debtor account: Arrived amount is booked with positive sign
            bookingLine.TransactionValue = Parameter.Amount;
            bookingLines.Add(bookingLine);

            #endregion

            Guid tenantBPID = CL1_CMN_BPT.ORM_CMN_BPT_BusinessParticipant.Query.Search(Connection, Transaction,
                                                                                       new CL1_CMN_BPT.ORM_CMN_BPT_BusinessParticipant.Query
            {
                IfTenant_Tenant_RefID = securityTicket.TenantID,
                IsDeleted             = false,
                Tenant_RefID          = securityTicket.TenantID
            }).Single().CMN_BPT_BusinessParticipantID;

            var bankAccountParam = new CL3_BookingAccounts.Atomic.Retrieval.P_L3BA_GBAPBPAfBP_1717
            {
                BusinessParticipantID_List = new Guid[] { tenantBPID },
                FiscalYearID  = currentFiscalYearID,
                IsBankAccount = true
            };
            var bankAccount = CL3_BookingAccounts.Atomic.Retrieval.cls_Get_BookingAccount_Purpose_BPAssignment_for_BPID_List.Invoke(
                Connection, Transaction, bankAccountParam, securityTicket).Result.SingleOrDefault();

            if (bankAccount != null)
            {
                #region Tenant's Bank Account booking line

                bookingLine = new CL3_BookingAccounts.Complex.Manipulation.P_L3BA_CATwBL_1315a();
                bookingLine.BookingAccountID = bankAccount.BookingAccount_RefID;
                // On the tenants bank account the arrived amount is booked with negative sign
                bookingLine.TransactionValue = -1 * Parameter.Amount;
                bookingLines.Add(bookingLine);

                #endregion

                transactionParam.BookingLines = bookingLines.ToArray();

                Guid accountTransactionID = CL3_BookingAccounts.Complex.Manipulation.cls_Create_Accounting_Transaction_with_BookingLines.Invoke(
                    Connection, Transaction, transactionParam, securityTicket).Result;

                #endregion

                #region 5. Create new bill header assigned payment entry and assign accunting transaction to installments

                bool isFullyPaid = Decimal.Compare((amountLeftToBePayed - Parameter.Amount), Decimal.Zero) == 0;
                // if the whole bill is payed before -- stop
                if (Decimal.Compare(amountLeftToBePayed, Decimal.Zero) == 0)
                {
                    return(returnValue);
                }
                // if this transaction will pay the rest of the bill set flag on the bill
                else if (isFullyPaid)
                {
                    var billHeader = new CL1_BIL.ORM_BIL_BillHeader();
                    billHeader.Load(Connection, Transaction, Parameter.BillHeaderID);

                    billHeader.IsFullyPaid = true;
                    billHeader.Save(Connection, Transaction);
                }

                var assignParam = new P_L3PY_AATtB_1107
                {
                    AccountTransactionID = accountTransactionID,
                    AssignedAmount       = Parameter.Amount,
                    BillHeaderID         = Parameter.BillHeaderID,
                    PaymentDate          = Parameter.PaymentDate
                };

                cls_Assign_AccountingTransaction_to_Bill.Invoke(Connection, Transaction, assignParam, securityTicket);

                #region Load taxes and bill positions for taxes per position

                var billPositions = CL1_BIL.ORM_BIL_BillPosition.Query.Search(Connection, Transaction,
                                                                              new CL1_BIL.ORM_BIL_BillPosition.Query
                {
                    BIL_BilHeader_RefID = Parameter.BillHeaderID
                });

                Guid[] applicableTaxes = billPositions.Select(x => x.ApplicableSalesTax_RefID).Distinct().ToArray();

                var revenueAccountsParam = new CL3_BookingAccounts.Atomic.Retrieval.P_L3BA_GAfTaFY_1647
                {
                    FiscalYearID     = currentFiscalYearID,
                    TaxID            = applicableTaxes,
                    IsTaxAccount     = false,
                    IsRevenueAccount = true
                };

                var revenueAccountAssignments = CL3_BookingAccounts.Atomic.Retrieval.cls_Get_Assignment_for_TaxID_List_and_FiscalYearID.Invoke(
                    Connection, Transaction, revenueAccountsParam, securityTicket).Result;

                #endregion

                var balanceParam = new P_L3PY_CBCfAT_1329
                {
                    AccountingTransactionID = accountTransactionID,
                    BookingAccountID        = bankAccount.BookingAccount_RefID,
                    Amount = Parameter.Amount
                };
                cls_Create_BalanceChange_for_AccountingTransaction.Invoke(Connection, Transaction, balanceParam, securityTicket);

                #endregion

                #region 6. Update dunning process if needed

                var dunningProcessMemberBill = CL1_ACC_DUN.ORM_ACC_DUN_DunningProcess_MemberBill.Query.Search(Connection, Transaction,
                                                                                                              new CL1_ACC_DUN.ORM_ACC_DUN_DunningProcess_MemberBill.Query
                {
                    BIL_BillHeader_RefID = Parameter.BillHeaderID,
                    IsDeleted            = false,
                    Tenant_RefID         = securityTicket.TenantID
                }).SingleOrDefault();


                if (dunningProcessMemberBill != null)
                {
                    dunningProcessMemberBill.CurrentUnpaidBillFraction = amountLeftToBePayed;
                    dunningProcessMemberBill.Save(Connection, Transaction);

                    var dunningProcess = CL1_ACC_DUN.ORM_ACC_DUN_DunningProcess.Query.Search(Connection, Transaction,
                                                                                             new CL1_ACC_DUN.ORM_ACC_DUN_DunningProcess.Query
                    {
                        ACC_DUN_DunningProcessID = dunningProcessMemberBill.ACC_DUN_DunningProcess_RefID,
                        IsDeleted    = false,
                        Tenant_RefID = securityTicket.TenantID
                    }).Single();

                    if (isFullyPaid)
                    {
                        dunningProcess.IsDunningProcessClosed      = true;
                        dunningProcess.DunningProcessClosedAt_Date = DateTime.Now;
                        dunningProcess.DunningProcessClosedBy_BusinessParticipnant_RefID = accountBPID;
                        dunningProcess.Save(Connection, Transaction);
                    }
                }
            }
            #endregion

            return(returnValue);

            #endregion UserCode
        }
Ejemplo n.º 2
0
        protected static FR_Guid Execute(DbConnection Connection, DbTransaction Transaction, P_L3PY_AFVfATtB_0934 Parameter, CSV2Core.SessionSecurity.SessionSecurityTicket securityTicket = null)
        {
            //Leave UserCode region to enable user code saving
            #region UserCode
            var returnValue = new FR_Guid();

            /*
             * 0. Find current fiscal year if none is given by the parameter
             * 1. Load bill to find recepient's BPID
             * 2. Find all values from accounting transactions that are not fully assigned to other bills
             * 3. Assign maximum free amount that needs to be assigned to bill
             */

            #region 0. Find current fiscal year if none is given by the parameter

            Guid currentFiscalYearID = Parameter.FiscalYearID;
            if (currentFiscalYearID == Guid.Empty)
            {
                currentFiscalYearID = CL2_FiscalYear.Complex.Retrieval.cls_Get_Current_FiscalYear.Invoke(
                    Connection, Transaction, securityTicket).Result.ACC_FiscalYearID;
            }

            #endregion

            #region 1. Load bill to find recepient's BPID

            var billHeader = new CL1_BIL.ORM_BIL_BillHeader();
            billHeader.Load(Connection, Transaction, Parameter.BillHeaderID);

            #endregion

            #region 2. Find all values from accounting transactions that are not fully assigned to other bills

            var unassignedParam = new CL3_Payments.Atomic.Retrieval.P_L3PY_GAATNFAtP_0846
            {
                FiscalYearID          = currentFiscalYearID,
                BusinessParticipantID = billHeader.BillRecipient_BuisnessParticipant_RefID
            };

            var transactions = CL3_Payments.Atomic.Retrieval.cls_Get_All_AccountingTransactions_Not_Fully_Assigned_to_Payments.Invoke(
                Connection, Transaction, unassignedParam, securityTicket).Result.Where(x => x.UnassignedTransactionValue > 0);

            #endregion

            #region 3. Assign maximum free amount that needs to be assigned to bill

            var payedParam = new CL3_Payments.Atomic.Retrieval.P_L3PY_GPAfB_1312
            {
                BillHeaderID_List = new Guid[] { Parameter.BillHeaderID }
            };
            var payed = CL3_Payments.Atomic.Retrieval.cls_Get_Payed_Amount_for_Bills.Invoke(Connection, Transaction, payedParam, securityTicket).Result;

            decimal amountLeftToBill = billHeader.TotalValue_IncludingTax - payed.Single(x => x.BillHeaderID == Parameter.BillHeaderID).TotalPayedValue;
            foreach (var item in transactions)
            {
                // if the whole bill is payed with transaction - stop the iteration
                if (Decimal.Compare(amountLeftToBill, Decimal.Zero) == 0)
                {
                    break;
                }

                decimal assignedAmount = item.UnassignedTransactionValue;
                if (amountLeftToBill > item.UnassignedTransactionValue)
                {
                    assignedAmount = amountLeftToBill;
                }

                var assignParam = new P_L3PY_AATtB_1107
                {
                    AccountTransactionID = item.AccountingTransactionID,
                    AssignedAmount       = assignedAmount,
                    BillHeaderID         = Parameter.BillHeaderID
                };

                cls_Assign_AccountingTransaction_to_Bill.Invoke(Connection, Transaction, assignParam, securityTicket);
            }

            #endregion

            return(returnValue);

            #endregion UserCode
        }