protected static FR_Guid Execute(DbConnection Connection, DbTransaction Transaction, P_L3PY_CBfB_0959 Parameter, CSV2Core.SessionSecurity.SessionSecurityTicket securityTicket = null) { //Leave UserCode region to enable user code saving #region UserCode var returnValue = new FR_Guid(); /* * 0. Load current fiscal year; * 1. Load bill header and positions * 2. Load taxes for products on bill positions * 3. Load VAT booking accounts for retrieved taxes from positions * 4. Load customer's account (debtor) -- from BillRecepient_BPID * 5. Load tenant's revenue accounts per taxes * 6. Create one transaction and multiple booking lines * 7. Create link between bill and acc. transaction */ #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. Load bill positions var billPositions = CL1_BIL.ORM_BIL_BillPosition.Query.Search(Connection, Transaction, new CL1_BIL.ORM_BIL_BillPosition.Query { BIL_BilHeader_RefID = Parameter.BillHeaderID }); if (billPositions != null && billPositions.Count > 0) { #endregion #region 2. Load taxes for products on bill positions Guid[] applicableTaxes = billPositions.Select(x => x.ApplicableSalesTax_RefID).Distinct().ToArray(); #endregion #region 3. Load VAT booking accounts for retrieved taxes from positions var taxAccountsParam = new CL3_BookingAccounts.Atomic.Retrieval.P_L3BA_GAfTaFY_1647 { FiscalYearID = currentFiscalYearID, TaxID = applicableTaxes, IsTaxAccount = true, IsRevenueAccount = false }; var taxAccountAssignments = CL3_BookingAccounts.Atomic.Retrieval.cls_Get_Assignment_for_TaxID_List_and_FiscalYearID.Invoke( Connection, Transaction, taxAccountsParam, securityTicket).Result; #endregion #region 4. Load customer's account (debtor) -- from BillRecepient_BPID 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 5. Load tenant's revenue accounts per taxes 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 #region 6. Create one transaction and multiple booking lines var transactionParam = new CL3_BookingAccounts.Complex.Manipulation.P_L3BA_CATwBL_1315 { AccountingTransactionTypeID = Guid.Empty, CurrencyID = billDataWithBookingAccounts.Bill.CurrencyID, DateOfTransaction = DateTime.Now }; var bookingLines = new List <CL3_BookingAccounts.Complex.Manipulation.P_L3BA_CATwBL_1315a>(); CL3_BookingAccounts.Complex.Manipulation.P_L3BA_CATwBL_1315a bookingLine = null; #region VAT Accounts booking lines foreach (var item in taxAccountAssignments) { bookingLine = new CL3_BookingAccounts.Complex.Manipulation.P_L3BA_CATwBL_1315a(); bookingLine.BookingAccountID = item.ACC_BOK_BookingAccount_RefID; // VAT-account: Bill VAT amount is booked with positive sign decimal taxValue = billPositions.Where(x => x.ApplicableSalesTax_RefID == item.ACC_TAX_Tax_RefID).Sum(x => x.PositionValue_IncludingTax - x.PositionValue_BeforeTax); bookingLine.TransactionValue = taxValue; bookingLines.Add(bookingLine); } #endregion #region Customer Account booking line bookingLine = new CL3_BookingAccounts.Complex.Manipulation.P_L3BA_CATwBL_1315a(); bookingLine.BookingAccountID = billDataWithBookingAccounts.CustomerAccount.BookingAccountID; // Debtor account: Bill amount including VAT is booked as receivable with negative sign bookingLine.TransactionValue = -(1) * billDataWithBookingAccounts.Bill.TotalValuee_with_DunningFees_IncludingTax; bookingLines.Add(bookingLine); #endregion #region Tenant's Revenue Account booking line foreach (var revenueAccount in revenueAccountAssignments) { bookingLine = new CL3_BookingAccounts.Complex.Manipulation.P_L3BA_CATwBL_1315a(); bookingLine.BookingAccountID = revenueAccount.ACC_BOK_BookingAccount_RefID; // Revenue account: Bill amount is booked with positive sign decimal value = billPositions.Where(x => x.ApplicableSalesTax_RefID == revenueAccount.ACC_TAX_Tax_RefID).Sum(x => x.PositionValue_BeforeTax); bookingLine.TransactionValue = value; 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 7. Create link between bill and acc. transaction var bill2Transaction = new CL1_BIL.ORM_BIL_BillHeader_2_AccountingTransaction { AssignmentID = Guid.NewGuid(), ACC_BOK_Accounting_Transaction_RefID = accountTransactionID, BIL_BillHeader_RefID = Parameter.BillHeaderID, Creation_Timestamp = DateTime.Now, Tenant_RefID = securityTicket.TenantID }; bill2Transaction.Save(Connection, Transaction); #endregion } return(returnValue); #endregion UserCode }
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 }
protected static FR_Guid Execute(DbConnection Connection, DbTransaction Transaction, P_L3PY_CATfDF_0849 Parameter, CSV2Core.SessionSecurity.SessionSecurityTicket securityTicket = null) { //Leave UserCode region to enable user code saving #region UserCode var returnValue = new FR_Guid(); /* * 0. Load current fiscal year * 1. Find bill recepient and load his customer booking account * 2. Find tenant's "other income" booking account i.e. revenue account for tax that is 0% * 3. Create one transaction and multiple booking lines */ #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. Find bill recepient to load his customer booking account var billHeader = new CL1_BIL.ORM_BIL_BillHeader(); billHeader.Load(Connection, Transaction, Parameter.BillHeaderID); var cbaParam = new CL3_BookingAccounts.Atomic.Retrieval.P_L3BA_GBAPBPAfBP_1717 { FiscalYearID = currentFiscalYearID, IsCustomerAccount = true, BusinessParticipantID_List = new Guid[] { billHeader.BillRecipient_BuisnessParticipant_RefID } }; Guid customerBookingAccountID = CL3_BookingAccounts.Atomic.Retrieval.cls_Get_BookingAccount_Purpose_BPAssignment_for_BPID_List.Invoke( Connection, Transaction, cbaParam, securityTicket).Result.Single().BookingAccount_RefID; #endregion #region 2. Find tenant's "other incomes" booking account i.e. revenue account for tax that is 0% 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 taxZero = CL1_ACC_TAX.ORM_ACC_TAX_Tax.Query.Search(Connection, Transaction, new CL1_ACC_TAX.ORM_ACC_TAX_Tax.Query { IsDeleted = false, Tenant_RefID = securityTicket.TenantID }).SingleOrDefault(x => Double.Equals(x.TaxRate, 0.0d)); if (taxZero == null) { return(returnValue); } var tbaParam = new CL3_BookingAccounts.Atomic.Retrieval.P_L3BA_GAfTaFY_1647 { FiscalYearID = currentFiscalYearID, IsRevenueAccount = true, IsTaxAccount = false, TaxID = new Guid[] { taxZero.ACC_TAX_TaxeID } }; Guid otherIncomeAccountID = CL3_BookingAccounts.Atomic.Retrieval.cls_Get_Assignment_for_TaxID_List_and_FiscalYearID.Invoke( Connection, Transaction, tbaParam, securityTicket).Result.Single().ACC_BOK_BookingAccount_RefID; #endregion #region 3. Create one transaction and multiple booking lines var transactionParam = new CL3_BookingAccounts.Complex.Manipulation.P_L3BA_CATwBL_1315 { AccountingTransactionTypeID = Guid.Empty, CurrencyID = billHeader.Currency_RefID, DateOfTransaction = DateTime.Now }; var bookingLines = new List <CL3_BookingAccounts.Complex.Manipulation.P_L3BA_CATwBL_1315a>(); CL3_BookingAccounts.Complex.Manipulation.P_L3BA_CATwBL_1315a bookingLine = null; #region Booking line for customer account bookingLine = new CL3_BookingAccounts.Complex.Manipulation.P_L3BA_CATwBL_1315a() { BookingAccountID = customerBookingAccountID, // Debtor account: Dunning fee is booked as receivable with negative sign TransactionValue = -1 * Parameter.DunningFee }; bookingLines.Add(bookingLine); #endregion #region Booking line for tenant's "other income" account bookingLine = new CL3_BookingAccounts.Complex.Manipulation.P_L3BA_CATwBL_1315a { BookingAccountID = otherIncomeAccountID, // "Other income account" Dunning Fee is booked with positive sign TransactionValue = Parameter.DunningFee }; 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 returnValue.Result = accountTransactionID; return(returnValue); #endregion UserCode }