protected static FR_Guid Execute(DbConnection Connection, DbTransaction Transaction, P_L3IP_SIP_1718 Parameter, CSV2Core.SessionSecurity.SessionSecurityTicket securityTicket = null) { #region UserCode var returnValue = new FR_Guid(); returnValue.Result = Guid.Empty; bool isNew = Parameter.InstallmentPlanID == Guid.Empty; Guid currencyID = CL2_Currency.Atomic.Retrieval.cls_Get_DefaultCurrency_for_Tenant.Invoke(Connection, Transaction, securityTicket).Result.CMN_CurrencyID; var installmentPlan = new CL1_ACC_IPL.ORM_ACC_IPL_InstallmentPlan(); var statusHistory = new CL1_ACC_IPL.ORM_ACC_IPL_InstallmentPlan_StatusHistory(); var firstDate = DateTime.Now; if (isNew) { #region Create new Installment Plan installmentPlan.ACC_IPL_InstallmentPlanID = Guid.NewGuid(); installmentPlan.InstallmentPeriodType = 0; installmentPlan.InstallmentStart = firstDate; installmentPlan.Currency_RefID = currencyID; installmentPlan.Creation_Timestamp = DateTime.Now; installmentPlan.Tenant_RefID = securityTicket.TenantID; Guid statusID = CL1_ACC_IPL.ORM_ACC_IPL_InstallmentPlan_Status.Query.Search(Connection, Transaction, new CL1_ACC_IPL.ORM_ACC_IPL_InstallmentPlan_Status.Query { GlobalPropertyMatchingID = EnumUtils.GetEnumDescription(EInstallmentPlanStatus.Created), IsDeleted = false, Tenant_RefID = securityTicket.TenantID }).Single().ACC_IPL_InstallmentPlan_StatusID; var BusinessParticipantID = 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; statusHistory = new CL1_ACC_IPL.ORM_ACC_IPL_InstallmentPlan_StatusHistory { ACC_IPL_InstallmentPlan_StatusHistoryID = Guid.NewGuid(), ACC_IPL_InstallmentPlan_RefID = installmentPlan.ACC_IPL_InstallmentPlanID, ACC_IPL_InstallmentPlan_Status_RefID = statusID, PerformedBy_BusinessParticipant_RefID = BusinessParticipantID, StatusHistoryComment = Parameter.Comment, Creation_Timestamp = DateTime.Now, Tenant_RefID = securityTicket.TenantID }; statusHistory.Save(Connection, Transaction); installmentPlan.Current_InstallmentPlanStatus_RefID = statusID; #endregion } else { #region Load existing InstallmentPlan var fetched = installmentPlan.Load(Connection, Transaction, Parameter.InstallmentPlanID); if (fetched.Status != FR_Status.Success || installmentPlan.ACC_IPL_InstallmentPlanID != Parameter.InstallmentPlanID) { returnValue.Status = FR_Status.Error_Internal; return(returnValue); } #endregion } installmentPlan.NominalValue = Parameter.NominalValue; installmentPlan.Save(Connection, Transaction); #region Save Installments CL1_ACC_IPL.ORM_ACC_IPL_Installment installment = null; foreach (var item in Parameter.Installments) { if (item.InstallmentID == Guid.Empty) { installment = new CL1_ACC_IPL.ORM_ACC_IPL_Installment { ACC_IPL_InstallmentID = Guid.NewGuid(), InstallmentPlan_RefID = installmentPlan.ACC_IPL_InstallmentPlanID, IsFullyPaid = false, Creation_Timestamp = DateTime.Now, Tenant_RefID = securityTicket.TenantID }; } else { installment = new CL1_ACC_IPL.ORM_ACC_IPL_Installment(); var fetched = installment.Load(Connection, Transaction, item.InstallmentID); if (fetched.Status != FR_Status.Success || installment.ACC_IPL_InstallmentID != item.InstallmentID) { returnValue.Status = FR_Status.Error_Internal; return(returnValue); } } installment.PaymentDeadline = item.PaymentDeadline; installment.Amount = item.Amount; installment.Save(Connection, Transaction); } #endregion returnValue.Result = installmentPlan.ACC_IPL_InstallmentPlanID; return(returnValue); #endregion UserCode }
protected static FR_Guids Execute(DbConnection Connection, DbTransaction Transaction, P_L3IP_AATfIP_1331 Parameter, CSV2Core.SessionSecurity.SessionSecurityTicket securityTicket = null) { #region UserCode var returnValue = new FR_Guids(); /* * 0. Load current's account business participant ID * 1. Find all not fully payed installments * 2. Assign payment to open installments * 3. Change installment plan status history * */ List <Guid> updatedInstallmentIDs = new List <Guid>(); #region 0. Load current's account business participant ID var account = 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(); #endregion #region 1. Find all not fully payed installments var installments = CL1_ACC_IPL.ORM_ACC_IPL_Installment.Query.Search(Connection, Transaction, new CL1_ACC_IPL.ORM_ACC_IPL_Installment.Query { InstallmentPlan_RefID = Parameter.InstallmentPlanID, IsFullyPaid = false, IsDeleted = false, Tenant_RefID = securityTicket.TenantID }).OrderBy(x => x.PaymentDeadline); #endregion #region 2. Assign payment to open installments and set to fully paid if needed CL1_ACC_IPL.ORM_ACC_IPL_Installment_2_AssignedPayment installment2AssignedPayment = null; CL1_ACC_IPL.ORM_ACC_IPL_Installment_2_AccountTransactionPairBatch installment2AccountTransactionPairBatch = null; decimal unassignedAmount = Parameter.TransactionAmount; foreach (var item in installments) { // stop if unassigned amount has reached zero if (Decimal.Compare(unassignedAmount, 0M) == 0) { break; } /** * assigned value is amount on installment by default, * but if it is less than unassigned amount it receives the value of unassigned amount */ bool isFullyPaid = (item.Amount <= unassignedAmount); decimal amountAssignedToInstallment = isFullyPaid ? item.Amount : unassignedAmount; installment2AssignedPayment = new CL1_ACC_IPL.ORM_ACC_IPL_Installment_2_AssignedPayment { ACC_IPL_Installment_2_AssignedPaymentID = Guid.NewGuid(), ACC_BOK_Accounting_Transaction_RefID = Parameter.AccountingTransactionID, ACC_IPL_Installment_RefID = item.ACC_IPL_InstallmentID, AssignedValue = amountAssignedToInstallment, AssignedBy_BusinessParticipant_RefID = account.BusinessParticipant_RefID, Creation_Timestamp = DateTime.Now, Tenant_RefID = securityTicket.TenantID }; installment2AssignedPayment.Save(Connection, Transaction); installment2AccountTransactionPairBatch = new CL1_ACC_IPL.ORM_ACC_IPL_Installment_2_AccountTransactionPairBatch { AssignmentID = Guid.NewGuid(), ACC_BOK_Accounting_Transaction_RefID = Parameter.AccountingTransactionID, ACC_IPL_Installment_RefID = item.ACC_IPL_InstallmentID, Creation_Timestamp = DateTime.Now, Tenant_RefID = securityTicket.TenantID }; installment2AccountTransactionPairBatch.Save(Connection, Transaction); #region Set current as fully paid and adjust amount if it is lower than the part of the payment can cover if (item.Amount < amountAssignedToInstallment) { item.Amount = amountAssignedToInstallment; } item.IsFullyPaid = true; item.Save(Connection, Transaction); #endregion if (isFullyPaid == false) { #region Create new installment with the amount that is left on current installment var newInstallment = new CL1_ACC_IPL.ORM_ACC_IPL_Installment { ACC_IPL_InstallmentID = Guid.NewGuid(), InstallmentPlan_RefID = Parameter.InstallmentPlanID, PaymentDeadline = item.PaymentDeadline, IsFullyPaid = false, Amount = item.Amount - unassignedAmount, Tenant_RefID = securityTicket.TenantID }; newInstallment.IsFullyPaid = false; newInstallment.Save(Connection, Transaction); #endregion } unassignedAmount -= amountAssignedToInstallment; updatedInstallmentIDs.Add(item.ACC_IPL_InstallmentID); } #endregion #region 3. Change installment plan status history var statusHistoryParam = new P_L3IP_CIPSH_1447 { InstallmentPlanID = Parameter.InstallmentPlanID, DoTerminate = false }; cls_Change_InstallmentPlan_StatusHistory.Invoke(Connection, Transaction, statusHistoryParam, securityTicket); #endregion returnValue.Result = updatedInstallmentIDs.ToArray(); return(returnValue); #endregion UserCode }