public void CreatePaymentSchedule(Entities.new_agreement entity) { int creditPeriod = entity.new_creditperiod ?? throw new ArgumentNullException(nameof(entity.new_creditperiod)); Money creditAmount = entity.new_creditamount ?? throw new ArgumentNullException(nameof(entity.new_creditamount)); DateTime agreementStartDate = entity.new_date ?? throw new ArgumentNullException(nameof(entity.new_date)); DateTime currDate = DateTime.Now; // определяем дату первого счета DateTime nextMonth = currDate.AddMonths(1); DateTime startDate = new DateTime(nextMonth.Year, nextMonth.Month, 1); // дата окончания кредита DateTime creditEndDate = agreementStartDate.AddYears(creditPeriod); // сумма ежемесячного платежа по счету decimal amountPerMonth = Math.Ceiling(creditAmount.Value / (creditPeriod * 12)); while (startDate < creditEndDate) { Entities.new_invoice newInvoice = new Entities.new_invoice { new_name = "Счет на оплату договора " + entity.new_name, new_date = currDate, new_paydate = startDate, new_dogovorid = entity.ToEntityReference(), new_fact = false, new_type = Entities.new_invoice_new_type.__100000001, new_amount = new Money(amountPerMonth) }; _OrganizationService.Create(newInvoice); startDate = startDate.AddMonths(1); } }
public void CheckForFirstAgreement(Entities.new_agreement target) { if (IsFirstAgreementForContact(target.new_contact)) { DateTime date = target.new_date ?? throw new NullReferenceException("Agreement date not setted!"); UpdateContactForNewDate(target.new_contact, date); } }
/// <summary> /// /// </summary> /// <param name="target">Entity with agremeent</param> private void ChangeAgreementPaidState(Entities.new_invoice target, bool state) { Entities.new_agreement updatedAgreement = new Entities.new_agreement { Id = target.new_dogovorid.Id, new_fact = state }; _OrganizationService.Update(updatedAgreement); }
/// <summary> /// /// </summary> /// <param name="target">Entity with agremeent</param> private bool IsAgreementFullPaid(Entities.new_invoice target) { Entities.new_agreement agreement = _OrganizationService.Retrieve( target.new_dogovorid.LogicalName, target.new_dogovorid.Id, new ColumnSet( Entities.new_agreement.Fields.new_factsumma, Entities.new_agreement.Fields.new_summa) ).ToEntity <Entities.new_agreement>(); Money paid = agreement.new_factsumma; Money total = agreement.new_summa; return(paid.Value >= total.Value); }
public override void PluginExecuted(PluginData pluginData) { try { AgreementService service = new AgreementService(pluginData.OrganizationService); Entities.new_agreement target = pluginData.Target.ToEntity <Entities.new_agreement>(); service.CheckForFirstAgreement(target); } catch (Exception ex) { pluginData.TracingService.Trace(ex.ToString()); throw new InvalidPluginExecutionException(ex.Message); } }
public override void Executed(WorkflowData data) { try { EntityReference agreementRef = AgreementReference.Get(data.Context); Entities.new_agreement entity = data.Target.ToEntity <Entities.new_agreement>(); AgreementService service = new AgreementService(data.OrganizationService); service.DeleteRelatedInvoicesWithAutoType(agreementRef); service.CreatePaymentSchedule(entity); service.UpdateAgreementScheduleDate(entity); } catch (Exception ex) { data.TracingService.Trace(ex.ToString()); throw new InvalidWorkflowException(ex.Message); } }
/// <summary> /// /// </summary> /// <param name="entity">Entity with agremeent and amount fields</param> private void RecalculateAgreementAmount(RecalculateType type, Entities.new_invoice entity, Entities.new_invoice oldEntity) { Entities.new_agreement agreement = _OrganizationService.Retrieve( entity.new_dogovorid.LogicalName, entity.new_dogovorid.Id, new ColumnSet( Entities.new_agreement.Fields.new_factsumma) ).ToEntity <Entities.new_agreement>(); Money agreementMoney = agreement.new_factsumma; Money currentMoney = entity.new_amount; Money oldMoney = oldEntity == null ? new Money(0) : oldEntity.new_amount; Money recalculatedMoney = GetRecalculatedMoneyByType(type, agreementMoney, currentMoney, oldMoney); Entities.new_agreement updatedAgreement = new Entities.new_agreement { Id = agreement.Id, new_factsumma = recalculatedMoney }; _OrganizationService.Update(updatedAgreement); }
public void UpdateAgreementScheduleDate(Entities.new_agreement entity) { entity.new_paymentplandate = DateTime.Now.AddDays(1); _OrganizationService.Update(entity); }