protected void btnInsert_Click(object sender, EventArgs e) { DateTime inputDate = Convert.ToDateTime(tbDateOfPayment.Text); int insuranceCompanyID = int.Parse(ddlInsuranceCompany.SelectedValue); PolicyItem pi; if (ddlInsuranceSubTypes.Items.Count > 0) { pi = PolicyItem.GetByNumberAndInsuranceSubType(tbPolicyNumber.Text, Convert.ToInt32(ddlInsuranceSubTypes.SelectedValue), insuranceCompanyID); } else { pi = PolicyItem.GetByNumber(tbPolicyNumber.Text, insuranceCompanyID); } if (inputDate.Date > DateTime.Today) { lblFeedback.Text = "Не е можно внесување на датуми поголеми од денешниот"; } else { List <Payment> listPayments = Payment.GetByPolicyItemID(pi.ID); decimal paymentTotalValue = 0; foreach (Payment payment in listPayments) { paymentTotalValue += payment.Value; } if (Convert.ToDecimal(tbValueOfPayment.Text) > (pi.PremiumValue - paymentTotalValue)) { lblFeedback.Text = "Поголем износ од преостанатиот износ за плаќање"; } else { decimal valueFromClient = Convert.ToDecimal(tbValueOfPayment.Text); while (valueFromClient > 0) { Rate currentRate = Rate.GetCurrentRateForPayment(pi.ID); Payment newPayment = new Payment(); newPayment.Date = Convert.ToDateTime(tbDateOfPayment.Text); newPayment.RateID = currentRate.ID; newPayment.IsCashReported = false; Broker.DataAccess.PaymentType pt = Broker.DataAccess.PaymentType.Get(Convert.ToInt32(ddlPaymentTypes.SelectedValue)); newPayment.PaymentTypeID = pt.ID; if (pt.Code == Broker.DataAccess.PaymentType.CREDITCARD) { newPayment.BankCreditCardID = BankCreditCard.GetByBankAndCard(Convert.ToInt32(ddlBank.SelectedValue), Convert.ToInt32(ddlCardTypes.SelectedValue)).ID; } newPayment.UserID = this.PageUser.ID; newPayment.BranchID = this.PageUser.BranchID; if (tbBankslipNumber.Text.Trim() != string.Empty) { newPayment.BankslipNumber = tbBankslipNumber.Text; newPayment.BankslipBankID = Convert.ToInt32(ddlBankslipBanks.SelectedValue); } if (valueFromClient >= currentRate.Value) { newPayment.Value = currentRate.Value - Payment.GetPaidValueForRate(currentRate.ID); newPayment.Insert(); decimal basicValue = newPayment.Value; decimal k = basicValue / newPayment.Rate.PolicyItem.PremiumValue; List <ControlAppropriateInsuranceSubType> listAppropriateIST = Broker.DataAccess.ControlAppropriateInsuranceSubType.Table.ToList(); foreach (ControlAppropriateInsuranceSubType c in listAppropriateIST) { PolicyExtendInformation pei = PolicyExtendInformation.GetByPolicyItemAndControl(pi.ID, c.ControlID); if (pei != null) { decimal peiValue = 0; decimal.TryParse(pei.Value, out peiValue); basicValue -= k * peiValue; if (peiValue > 0) { PaymentsPerInsSubType ppist = new PaymentsPerInsSubType(); ppist.PaymentID = newPayment.ID; ppist.InsuranceSubTypeID = c.InsuranceSubTypeID; ppist.PaidValue = k * peiValue; ppist.BrokerageValue = ppist.PaidValue * pi.BrokeragePercentage / 100; ppist.Insert(); } } } if (basicValue > 0) { PaymentsPerInsSubType ppist = new PaymentsPerInsSubType(); ppist.PaymentID = newPayment.ID; ppist.InsuranceSubTypeID = pi.InsuranceSubTypeID; ppist.PaidValue = basicValue; ppist.BrokerageValue = ppist.PaidValue * pi.BrokeragePercentage / 100; ppist.Insert(); } valueFromClient -= newPayment.Value; currentRate.PaidValue += newPayment.Value; Rate.Table.Context.SubmitChanges(); } else { if (valueFromClient <= (currentRate.Value - currentRate.PaidValue)) { newPayment.Value = valueFromClient; newPayment.Insert(); decimal basicValue = newPayment.Value; decimal k = basicValue / newPayment.Rate.PolicyItem.PremiumValue; List <ControlAppropriateInsuranceSubType> listAppropriateIST = Broker.DataAccess.ControlAppropriateInsuranceSubType.Table.ToList(); foreach (ControlAppropriateInsuranceSubType c in listAppropriateIST) { PolicyExtendInformation pei = PolicyExtendInformation.GetByPolicyItemAndControl(pi.ID, c.ControlID); if (pei != null) { decimal peiValue = 0; decimal.TryParse(pei.Value, out peiValue); basicValue -= k * peiValue; if (peiValue > 0) { PaymentsPerInsSubType ppist = new PaymentsPerInsSubType(); ppist.PaymentID = newPayment.ID; ppist.InsuranceSubTypeID = c.InsuranceSubTypeID; ppist.PaidValue = k * peiValue; ppist.BrokerageValue = ppist.PaidValue * pi.BrokeragePercentage / 100; ppist.Insert(); } } } if (basicValue > 0) { PaymentsPerInsSubType ppist = new PaymentsPerInsSubType(); ppist.PaymentID = newPayment.ID; ppist.InsuranceSubTypeID = pi.InsuranceSubTypeID; ppist.PaidValue = basicValue; ppist.BrokerageValue = ppist.PaidValue * pi.BrokeragePercentage / 100; ppist.Insert(); } currentRate.PaidValue += valueFromClient; Rate.Table.Context.SubmitChanges(); break; } else { newPayment.Value = (currentRate.Value - currentRate.PaidValue); newPayment.Insert(); decimal basicValue = newPayment.Value; decimal k = basicValue / newPayment.Rate.PolicyItem.PremiumValue; List <ControlAppropriateInsuranceSubType> listAppropriateIST = Broker.DataAccess.ControlAppropriateInsuranceSubType.Table.ToList(); foreach (ControlAppropriateInsuranceSubType c in listAppropriateIST) { PolicyExtendInformation pei = PolicyExtendInformation.GetByPolicyItemAndControl(pi.ID, c.ControlID); if (pei != null) { decimal peiValue = 0; decimal.TryParse(pei.Value, out peiValue); basicValue -= k * peiValue; if (peiValue > 0) { PaymentsPerInsSubType ppist = new PaymentsPerInsSubType(); ppist.PaymentID = newPayment.ID; ppist.InsuranceSubTypeID = c.InsuranceSubTypeID; ppist.PaidValue = k * peiValue; ppist.BrokerageValue = ppist.PaidValue * pi.BrokeragePercentage / 100; ppist.Insert(); } } } if (basicValue > 0) { PaymentsPerInsSubType ppist = new PaymentsPerInsSubType(); ppist.PaymentID = newPayment.ID; ppist.InsuranceSubTypeID = pi.InsuranceSubTypeID; ppist.PaidValue = basicValue; ppist.BrokerageValue = ppist.PaidValue * pi.BrokeragePercentage / 100; ppist.Insert(); } //currentRate.PaidValue += valueFromClient; //Rate.Table.Context.SubmitChanges(); currentRate.PaidValue += newPayment.Value; Rate.Table.Context.SubmitChanges(); valueFromClient -= newPayment.Value; } } } Broker.DataAccess.Facture.UpdatePaidStatusForFacture(pi.ID); } } GridViewPayments.DataSource = null; GridViewPayments.DataSourceID = odsPaidPayments.ID; odsPaidPayments.SelectParameters.Clear(); odsPaidPayments.SelectParameters.Add("policyItemID", pi.ID.ToString()); GridViewPayments.DataBind(); GridViewRates.DataSource = null; GridViewRates.DataSourceID = odsRates.ID; odsRates.SelectParameters.Clear(); odsRates.TypeName = "Broker.DataAccess.Rate"; odsRates.DataObjectTypeName = "Broker.DataAccess.Rate"; odsRates.SelectMethod = "GetByPolicyNumberAndInsuranceSubTypeIDAndInsuranceCompanyID"; odsRates.SelectParameters.Add("policyNumber", tbPolicyNumber.Text); odsRates.SelectParameters.Add("insuranceSubTypeID", pi.InsuranceSubType.ID.ToString()); odsRates.SelectParameters.Add("insuranceCompanyID", ddlInsuranceCompany.SelectedValue); //odsRates.SelectMethod = "Broker.DataAccess.PolicyItem.GetByNumberAndInsuranceSubType"; GridViewRates.DataBind(); UpdateTextBoxes(pi); }
void InsertPaymentsForPolicy(PolicyItem pi, decimal newPaidValue, DateTime dateOfNewPaid, string bankslipNumber, int bankslipBankID) { List <Payment> listPayments = Payment.GetByPolicyItemID(pi.ID); decimal paymentTotalValue = 0; foreach (Payment payment in listPayments) { paymentTotalValue += payment.Value; } if (newPaidValue > (pi.PremiumValue - paymentTotalValue)) { RegisterStartupScript("myAlert", "<script>alert('Поголем износ од преостанатиот износ за плаќање!')</script>"); } else { decimal valueFromClient = newPaidValue; while (valueFromClient > 0) { Rate currentRate = Rate.GetCurrentRateForPayment(pi.ID); Payment newPayment = new Payment(); newPayment.Date = dateOfNewPaid; newPayment.RateID = currentRate.ID; newPayment.IsCashReported = false; Broker.DataAccess.PaymentType pt = Broker.DataAccess.PaymentType.GetByCode(Broker.DataAccess.PaymentType.VIRMAN); newPayment.PaymentTypeID = pt.ID; newPayment.UserID = this.PageUser.ID; newPayment.BranchID = this.PageUser.BranchID; newPayment.BankslipNumber = bankslipNumber; newPayment.BankslipBankID = bankslipBankID; if (valueFromClient >= currentRate.Value) { newPayment.Value = currentRate.Value - Payment.GetPaidValueForRate(currentRate.ID); newPayment.Insert(); decimal basicValue = newPayment.Value; decimal k = basicValue / newPayment.Rate.PolicyItem.PremiumValue; List <ControlAppropriateInsuranceSubType> listAppropriateIST = Broker.DataAccess.ControlAppropriateInsuranceSubType.Table.ToList(); foreach (ControlAppropriateInsuranceSubType c in listAppropriateIST) { PolicyExtendInformation pei = PolicyExtendInformation.GetByPolicyItemAndControl(pi.ID, c.ControlID); if (pei != null) { decimal peiValue = 0; decimal.TryParse(pei.Value, out peiValue); basicValue -= k * peiValue; if (peiValue > 0) { PaymentsPerInsSubType ppist = new PaymentsPerInsSubType(); ppist.PaymentID = newPayment.ID; ppist.InsuranceSubTypeID = c.InsuranceSubTypeID; ppist.PaidValue = k * peiValue; ppist.BrokerageValue = ppist.PaidValue * pi.BrokeragePercentage / 100; ppist.Insert(); } } } if (basicValue > 0) { PaymentsPerInsSubType ppist = new PaymentsPerInsSubType(); ppist.PaymentID = newPayment.ID; ppist.InsuranceSubTypeID = pi.InsuranceSubTypeID; ppist.PaidValue = basicValue; ppist.BrokerageValue = ppist.PaidValue * pi.BrokeragePercentage / 100; ppist.Insert(); } valueFromClient -= newPayment.Value; currentRate.PaidValue += newPayment.Value; Rate.Table.Context.SubmitChanges(); } else { if (valueFromClient <= (currentRate.Value - currentRate.PaidValue)) { newPayment.Value = valueFromClient; newPayment.Insert(); decimal basicValue = newPayment.Value; decimal k = basicValue / newPayment.Rate.PolicyItem.PremiumValue; List <ControlAppropriateInsuranceSubType> listAppropriateIST = Broker.DataAccess.ControlAppropriateInsuranceSubType.Table.ToList(); foreach (ControlAppropriateInsuranceSubType c in listAppropriateIST) { PolicyExtendInformation pei = PolicyExtendInformation.GetByPolicyItemAndControl(pi.ID, c.ControlID); if (pei != null) { decimal peiValue = 0; decimal.TryParse(pei.Value, out peiValue); basicValue -= k * peiValue; if (peiValue > 0) { PaymentsPerInsSubType ppist = new PaymentsPerInsSubType(); ppist.PaymentID = newPayment.ID; ppist.InsuranceSubTypeID = c.InsuranceSubTypeID; ppist.PaidValue = k * peiValue; ppist.BrokerageValue = ppist.PaidValue * pi.BrokeragePercentage / 100; ppist.Insert(); } } } if (basicValue > 0) { PaymentsPerInsSubType ppist = new PaymentsPerInsSubType(); ppist.PaymentID = newPayment.ID; ppist.InsuranceSubTypeID = pi.InsuranceSubTypeID; ppist.PaidValue = basicValue; ppist.BrokerageValue = ppist.PaidValue * pi.BrokeragePercentage / 100; ppist.Insert(); } currentRate.PaidValue += valueFromClient; Rate.Table.Context.SubmitChanges(); break; } else { newPayment.Value = (currentRate.Value - currentRate.PaidValue); newPayment.Insert(); decimal basicValue = newPayment.Value; decimal k = basicValue / newPayment.Rate.PolicyItem.PremiumValue; List <ControlAppropriateInsuranceSubType> listAppropriateIST = Broker.DataAccess.ControlAppropriateInsuranceSubType.Table.ToList(); foreach (ControlAppropriateInsuranceSubType c in listAppropriateIST) { PolicyExtendInformation pei = PolicyExtendInformation.GetByPolicyItemAndControl(pi.ID, c.ControlID); if (pei != null) { decimal peiValue = 0; decimal.TryParse(pei.Value, out peiValue); basicValue -= k * peiValue; if (peiValue > 0) { PaymentsPerInsSubType ppist = new PaymentsPerInsSubType(); ppist.PaymentID = newPayment.ID; ppist.InsuranceSubTypeID = c.InsuranceSubTypeID; ppist.PaidValue = k * peiValue; ppist.BrokerageValue = ppist.PaidValue * pi.BrokeragePercentage / 100; ppist.Insert(); } } } if (basicValue > 0) { PaymentsPerInsSubType ppist = new PaymentsPerInsSubType(); ppist.PaymentID = newPayment.ID; ppist.InsuranceSubTypeID = pi.InsuranceSubTypeID; ppist.PaidValue = basicValue; ppist.BrokerageValue = ppist.PaidValue * pi.BrokeragePercentage / 100; ppist.Insert(); } //currentRate.PaidValue += valueFromClient; //Rate.Table.Context.SubmitChanges(); currentRate.PaidValue += newPayment.Value; Rate.Table.Context.SubmitChanges(); valueFromClient -= newPayment.Value; } } } Broker.DataAccess.Facture.UpdatePaidStatusForFacture(pi.ID); } }