Esempio n. 1
0
        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);
            }
        }
        /// <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);
        }
        /// <exception cref="InvalidPluginExecutionException">Thrown when agreement full paid</exception>
        public void ValidatePaidInvoice(Entities.new_invoice target, Entities.new_invoice entityWithData)
        {
            if (IsPaid(target))
            {
                if (IsAgreementFullPaid(entityWithData))
                {
                    throw new InvalidPluginExecutionException("Данный договор уже полностью оплачен!");
                }

                SetPrePayDate(target, DateTime.Now);
            }
        }
        public void RecalculateAndChangeAgreementPaid(
            RecalculateType type,
            Entities.new_invoice target,
            Entities.new_invoice entityWithData,
            Entities.new_invoice entityWithOldData = null)
        {
            Entities.new_invoice entityToCheckPaid = target.new_fact == null ? entityWithData : target;

            if (IsPaid(entityToCheckPaid))
            {
                RecalculateAgreementAmount(type, entityWithData, entityWithOldData);

                ChangeAgreementPaidState(entityWithData, IsAgreementFullPaid(entityWithData));
            }
        }
        /// <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);
        }
Esempio n. 6
0
        public override void PluginExecuted(PluginData pluginData)
        {
            try
            {
                InvoiceService service = new InvoiceService(pluginData.OrganizationService);

                Entities.new_invoice target = pluginData.Target.ToEntity <Entities.new_invoice>();

                service.RecalculateAndChangeAgreementPaid(RecalculateType.Create, target, target);
            }
            catch (Exception ex)
            {
                pluginData.TracingService.Trace(ex.ToString());

                throw new InvalidPluginExecutionException(ex.Message);
            }
        }
Esempio n. 7
0
        public override void PluginExecuted(PluginData pluginData)
        {
            try
            {
                InvoiceService service  = new InvoiceService(pluginData.OrganizationService);
                Entity         preImage = pluginData.PluginExecutionContext.PreEntityImages["preImage"];

                Entities.new_invoice target     = pluginData.Target.ToEntity <Entities.new_invoice>();
                Entities.new_invoice dataEntity = preImage.ToEntity <Entities.new_invoice>();

                service.ValidatePaidInvoice(target, dataEntity);
            }
            catch (Exception ex)
            {
                pluginData.TracingService.Trace(ex.ToString());

                throw new InvalidPluginExecutionException(ex.Message);
            }
        }
        public override void PluginExecuted(PluginData pluginData)
        {
            try
            {
                InvoiceService service = new InvoiceService(pluginData.OrganizationService);

                Entities.new_invoice target = pluginData.Target.ToEntity <Entities.new_invoice>();

                service.SetPreTypeIfEmpty(target);

                service.ValidatePaidInvoice(target, target);
            }
            catch (Exception ex)
            {
                pluginData.TracingService.Trace(ex.ToString());

                throw new InvalidPluginExecutionException(ex.Message);
            }
        }
Esempio n. 9
0
        public override void PluginExecuted(PluginData pluginData)
        {
            try
            {
                InvoiceService service   = new InvoiceService(pluginData.OrganizationService);
                Entity         preImage  = pluginData.PluginExecutionContext.PreEntityImages["preImage"];
                Entity         postImage = pluginData.PluginExecutionContext.PostEntityImages["postImage"];

                Entities.new_invoice target        = pluginData.Target.ToEntity <Entities.new_invoice>();
                Entities.new_invoice dataEntity    = postImage.ToEntity <Entities.new_invoice>();
                Entities.new_invoice oldDataEntity = preImage.ToEntity <Entities.new_invoice>();

                service.RecalculateAndChangeAgreementPaid(RecalculateType.Update, target, dataEntity, oldDataEntity);
            }
            catch (Exception ex)
            {
                pluginData.TracingService.Trace(ex.ToString());

                throw new InvalidPluginExecutionException(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);
        }
 private bool IsPaid(Entities.new_invoice targetEntity)
 {
     return(targetEntity.new_fact ?? false);
 }
 public void SetPreTypeIfEmpty(Entities.new_invoice target)
 {
     target.new_type = target.new_type ?? Entities.new_invoice_new_type.__100000000;
 }
 public void SetPrePayDate(Entities.new_invoice target, DateTime date)
 {
     target.new_paydate = date;
 }