Exemple #1
0
        public override void Execute(IServiceProvider serviceProvider)
        {
            base.Execute(serviceProvider);

            if (PluginExecutionContext.Depth > 1)
            {
                // Recursive update.
                return;
            }

            try
            {
                lys_agreement        updatedAgreement   = GetTargetAs <Entity>().ToEntity <lys_agreement>();
                IOrganizationService currentUserService = CreateService();

                NavAgreementHandler agreementHandler = new NavAgreementHandler(currentUserService, TraceService);

                // 5.5
                agreementHandler.UpdateFact(updatedAgreement);
            }
            catch (EntityHandlerException e)
            {
                TraceService.Trace(e.ToString());
                throw new InvalidPluginExecutionException(e.Message);
            }
            catch (Exception e)
            {
                TraceService.Trace(e.ToString());
                throw new InvalidPluginExecutionException("Возникла ошибка, см. журнал для подробностей.");
            }
        }
Exemple #2
0
        public override void Execute(IServiceProvider serviceProvider)
        {
            base.Execute(serviceProvider);

            try
            {
                lys_agreement        createdAgreement   = GetTargetAs <Entity>().ToEntity <lys_agreement>();
                IOrganizationService currentUserService = CreateService();

                NavAgreementHandler agreementHandler = new NavAgreementHandler(currentUserService, TraceService);

                // 5.2
                agreementHandler.UpdateRelatedContactDateOnCreate(createdAgreement);
            }
            catch (EntityHandlerException e)
            {
                TraceService.Trace(e.ToString());
                throw new InvalidPluginExecutionException(e.Message);
            }
            catch (Exception e)
            {
                TraceService.Trace(e.ToString());
                throw new InvalidPluginExecutionException("Возникла ошибка, см. журнал для подробностей.");
            }
        }
        /*
         * Updates lys_fact and lys_factsum fields of invoice's related agreement based on all it's related paid invoices.
         *
         * invoice - lys_invoice;
         * invoicesToExclude - IDs of lys_invoices to exclude from the related invoices list.
         *
         * Returns true if updated, false otherwise.
         */
        public bool UpdateRelatedAgreementFactData(lys_invoice invoice, params Guid[] invoicesToExclude)
        {
            Guid relatedAgreementId;
            BaseRepository <lys_invoice>   invoiceRepo   = new BaseRepository <lys_invoice>(Service, lys_invoice.EntityLogicalName);
            BaseRepository <lys_agreement> agreementRepo = new BaseRepository <lys_agreement>(Service, lys_agreement.EntityLogicalName);

            // Checking if invoice contains related agreement ID. If not, obtaining it from CRM.)
            if (invoice.lys_dogovorid != null)
            {
                relatedAgreementId = invoice.lys_dogovorid.Id;
            }
            else
            {
                relatedAgreementId = invoiceRepo.Get(invoice.Id, new ColumnSet(lys_invoice.Fields.lys_dogovorid)).lys_dogovorid.Id;
            }

            TracingService.Trace($"relatedAgreementId={relatedAgreementId}");

            // Obtaining related agreement sum from CRM.
            lys_agreement relatedAgreement = agreementRepo.Get(relatedAgreementId, new ColumnSet(lys_agreement.Fields.lys_summ));

            // Getting total paid invoices sum.
            Decimal totalPaidInvoiceSum = GetRelatedNavInvoicesSum(relatedAgreementId, invoicesToExclude);

            relatedAgreement.lys_factsumma = new Money(totalPaidInvoiceSum);
            relatedAgreement.lys_fact      = relatedAgreement.lys_summ.Value == totalPaidInvoiceSum;

            // Updating related agreement.
            agreementRepo.Update(relatedAgreement);

            TracingService.Trace($"Updated agreement with ID={relatedAgreementId}, totalPaidInvoiceSum={totalPaidInvoiceSum}, lys_fact={relatedAgreement.lys_fact}.");

            return(true);
        }
        /*
         * Checks if sum of all related agreement's paid invoices is bigger than it's own sum.
         *
         * invoice - lys_invoice.
         *
         * Returns true if sum of all related agreement's paid invoices <= agreement sum; false otherwise.
         */
        private bool ValidateRelatedAgreementInvoicesSum(lys_invoice invoice)
        {
            lys_agreement relatedAgreement           = new lys_agreement();
            BaseRepository <lys_invoice> invoiceRepo = new BaseRepository <lys_invoice>(Service, lys_invoice.EntityLogicalName);

            // Checking if invoice contains related agreement ID. If not, obtaining it from CRM.
            if (invoice.lys_dogovorid != null)
            {
                relatedAgreement.Id = invoice.lys_dogovorid.Id;
            }
            else
            {
                relatedAgreement.Id = invoiceRepo.Get(invoice.Id, new ColumnSet(lys_invoice.Fields.lys_dogovorid)).lys_dogovorid.Id;
            }

            // Getting related agreement sum.
            BaseRepository <lys_agreement> agreementRepo = new BaseRepository <lys_agreement>(Service, lys_agreement.EntityLogicalName);

            relatedAgreement = agreementRepo.Get(relatedAgreement.Id, new ColumnSet(lys_agreement.Fields.lys_summ));

            TracingService.Trace($"relatedAgreementId={relatedAgreement.Id}, invoiceId={invoice.Id}");

            // Getting related paid invoices sum.
            Decimal totalPaidInvoiceSum = GetRelatedNavInvoicesSum(relatedAgreement.Id);

            return(totalPaidInvoiceSum <= relatedAgreement.lys_summ.Value);
        }
Exemple #5
0
        /*
         * Sets related contact's first agreement date to this agreement's date if there were no previously related agreements for this contact.
         *
         * agreement - created lys_agreement.
         *
         * Returns true if updated, false otherwise.
         */
        public bool UpdateRelatedContactDateOnCreate(lys_agreement agreement)
        {
            BaseRepository <lys_agreement> agreementRepo = new BaseRepository <lys_agreement>(Service, lys_agreement.EntityLogicalName);

            // Checking if there are already existing related agreements.
            QueryExpression query = new QueryExpression
            {
                ColumnSet = new ColumnSet(false),
                TopCount  = 1
            };

            query.Criteria.AddCondition(lys_agreement.Fields.lys_contact, ConditionOperator.Equal, agreement.lys_contact.Id);

            if (agreementRepo.GetMultiple(query).Entities.Count < 1)
            {
                // Updating contact's date since this is their first related agreement.
                BaseRepository <Contact> contactRepo = new BaseRepository <Contact>(Service, Contact.EntityLogicalName);
                contactRepo.Update(new Contact
                {
                    Id       = agreement.lys_contact.Id,
                    lys_date = ((DateTime)agreement.lys_date).AddDays(1)
                });

                return(true);
            }

            return(false);
        }
        protected override void Execute(CodeActivityContext context)
        {
            Init(context);

            try
            {
                IOrganizationService currentUserService = CreateService();
                NavAgreementHandler  agreementHandler   = new NavAgreementHandler(currentUserService, TraceService);

                lys_agreement agreement = new lys_agreement
                {
                    Id = Agreement.Get(context).Id
                };

                // 6.1
                agreementHandler.CreateFirstRelatedInvoice(agreement, WorkflowContext.UserId);
            }
            catch (EntityHandlerException e)
            {
                TraceService.Trace(e.ToString());
                throw new InvalidWorkflowException(e.Message);
            }
            catch (Exception e)
            {
                TraceService.Trace(e.ToString());
                throw new InvalidWorkflowException("Возникла ошибка, см. журнал для подробностей.");
            }
        }
Exemple #7
0
        /*
         * Checks if agreement has related lys_invoices.
         *
         * agreement - lys_agreement.
         *
         * Returns true if there is at least one related agreement, false otherwise.
         */
        private bool HasRelatedInvoices(lys_agreement agreement)
        {
            TracingService.Trace($"agreementId={agreement.Id}");

            BaseRepository <lys_invoice> invoiceRepo = new BaseRepository <lys_invoice>(Service, lys_invoice.EntityLogicalName);

            // Retrieving the first related lys_invoice.
            QueryExpression query = new QueryExpression();

            query.Criteria.AddCondition(lys_invoice.Fields.lys_dogovorid, ConditionOperator.Equal, agreement.Id);
            query.ColumnSet = new ColumnSet(false);
            query.TopCount  = 1;

            EntityCollection ec = invoiceRepo.GetMultiple(query);

            TracingService.Trace($"ec={ec}, ec.Entities={ec.Entities}, ec.Entities.Count={ec.Entities.Count}");

            return(ec.Entities.Count > 0);
        }
Exemple #8
0
        /*
         * Updates lys_fact based on agreement's lys_summ and lys_factsumma values.
         *
         * agreement - lys_agreement to update;
         * skipDbUpdate - if true, then no data will be written to DB.
         */
        public void UpdateFact(lys_agreement agreement, bool skipDbUpdate = false)
        {
            BaseRepository <lys_agreement> agreementRepo = new BaseRepository <lys_agreement>(Service, lys_agreement.EntityLogicalName);

            // Checking if agreement contains required sum data. If not, obtaining it from CRM.
            if (agreement.lys_summ == null || agreement.lys_factsumma == null)
            {
                agreement = agreementRepo.Get(agreement.Id, new ColumnSet(lys_agreement.Fields.lys_summ, lys_agreement.Fields.lys_factsumma));
            }

            TracingService.Trace($"agreementId={agreement.Id}");

            agreement.lys_fact = Equals(agreement.lys_summ, agreement.lys_factsumma);

            if (skipDbUpdate)
            {
                return;
            }

            agreementRepo.Update(agreement);
            TracingService.Trace($"agreementId={agreement.Id} updated in DB.");
        }
Exemple #9
0
        protected override void Execute(CodeActivityContext context)
        {
            Init(context);

            try
            {
                IOrganizationService currentUserService = CreateService();
                NavAgreementHandler  agreementHandler   = new NavAgreementHandler(currentUserService, TraceService);

                lys_agreement agreement = new lys_agreement
                {
                    Id = Agreement.Get(context).Id
                };

                // 6.2
                agreementHandler.CreateRelatedCreditInvoices(agreement);
            }
            catch (Exception e)
            {
                // Can't display any errors since it's a background activity.
                TraceService.Trace(e.ToString());
            }
        }
Exemple #10
0
        /*
         * Creates new related lys_invoice if there are no other related invoices for the agreement.
         *
         * agreement - lys_agreement.
         */
        public void CreateFirstRelatedInvoice(lys_agreement agreement, Guid emailNotificationSenderId)
        {
            TracingService.Trace($"agreementId={agreement.Id}");

            if (HasRelatedInvoices(agreement))
            {
                return;
            }

            // Checking if agreement contains required data. If not, obtaining it from CRM.
            if (agreement.lys_name == null || agreement.lys_summ == null || agreement.lys_contact == null)
            {
                BaseRepository <lys_agreement> agreementRepo = new BaseRepository <lys_agreement>(Service, lys_agreement.EntityLogicalName);
                agreement = agreementRepo.Get(agreement.Id, new ColumnSet(lys_agreement.Fields.lys_name, lys_agreement.Fields.lys_summ, lys_agreement.Fields.lys_contact));
            }

            // Creating new invoice.
            lys_invoice newRelatedInvoice = new lys_invoice();

            newRelatedInvoice.lys_name      = string.Format("Счет для договора {0}", agreement.lys_name);
            newRelatedInvoice.lys_paydate   = newRelatedInvoice.lys_date = DateTime.Now;
            newRelatedInvoice.lys_dogovorid = new EntityReference(lys_agreement.EntityLogicalName, agreement.Id);
            newRelatedInvoice.lys_fact      = false;
            newRelatedInvoice.lys_type      = lys_invoice_lys_type.Avtomaticheskoe_sozdanie;
            newRelatedInvoice.lys_amount    = agreement.lys_summ;

            BaseRepository <lys_invoice> invoiceRepo = new BaseRepository <lys_invoice>(Service, lys_invoice.EntityLogicalName);
            Guid createdInvoiceId = invoiceRepo.Insert(newRelatedInvoice);

            TracingService.Trace($"Created new related lys_invoice with ID={createdInvoiceId} for agreementId={agreement.Id}");

            // Getting agreement's related contact.
            BaseRepository <Contact> contactRepo = new BaseRepository <Contact>(Service, Contact.EntityLogicalName);
            Contact relatedContact = contactRepo.Get(agreement.lys_contact.Id, new ColumnSet(Contact.Fields.FullName, Contact.Fields.EMailAddress1, Contact.Fields.DoNotBulkEMail));

            // Sending email to related contact if they hasn't opted out of emails and their email address is set.
            if (relatedContact.DoNotBulkEMail.HasValue && !relatedContact.DoNotBulkEMail.Value && relatedContact.EMailAddress1 != null)
            {
                // Forming Email message.
                ActivityParty senderParty = new ActivityParty();
                senderParty.PartyId = new EntityReference(SystemUser.EntityLogicalName, emailNotificationSenderId);

                ActivityParty receiverParty = new ActivityParty();
                receiverParty.PartyId = new EntityReference(Contact.EntityLogicalName, relatedContact.Id);

                Email emailMessage = new Email();
                emailMessage.Subject     = string.Format("Вам выставлен счет для оплаты договора {0}", agreement.lys_name);
                emailMessage.From        = new[] { senderParty };
                emailMessage.To          = new[] { receiverParty };
                emailMessage.Description = string.Format("Уважаемый(ая) {0}, по вашему договору {1} выставлен счет на сумму {2}.", relatedContact.FullName, agreement.lys_name, agreement.lys_summ.Value.ToString("0.##"));

                // Sending Email.
                SendEmailRequest emailRequest = new SendEmailRequest();
                emailRequest.EmailId       = Service.Create(emailMessage); // Creating Email Entity and getting its ID
                emailRequest.IssueSend     = true;                         // Actually sending the email.
                emailRequest.TrackingToken = "";

                Service.Execute(emailRequest);

                TracingService.Trace($"Sent notification email to related contact with ID={relatedContact.Id} at email={relatedContact.EMailAddress1}");
            }
        }
Exemple #11
0
        public void CreateRelatedCreditInvoices(lys_agreement agreement)
        {
            TracingService.Trace($"agreementId={agreement.Id}");

            BaseRepository <lys_agreement> agreementRepo = new BaseRepository <lys_agreement>(Service, lys_agreement.EntityLogicalName);
            BaseRepository <lys_invoice>   invoiceRepo   = new BaseRepository <lys_invoice>(Service, lys_invoice.EntityLogicalName);

            // Retrieving all related invoices.
            QueryExpression query = new QueryExpression();

            query.ColumnSet = new ColumnSet(lys_invoice.Fields.lys_type, lys_invoice.Fields.lys_fact);
            query.Criteria.AddCondition(lys_invoice.Fields.lys_dogovorid, ConditionOperator.Equal, agreement.Id);

            EntityCollection relatedInvoices = invoiceRepo.GetMultiple(query);

            TracingService.Trace($"[lys_invoices] ec={relatedInvoices}, ec.Entities={relatedInvoices.Entities}, ec.Entities.Count={relatedInvoices.Entities.Count}");

            // Checking if there are any paid or manually created related invoices.
            foreach (Entity entity in relatedInvoices.Entities)
            {
                lys_invoice invoice = (lys_invoice)entity;

                if (invoice.lys_fact == true || invoice.lys_type == lys_invoice_lys_type.Ruchnoe_sozdanie)
                {
                    return;
                }
            }

            // Checking if agreement contains required data. If not, obtaining it from CRM.
            if (agreement.lys_creditid == null || agreement.lys_creditperiod == null || agreement.lys_creditamount == null || agreement.lys_name == null)
            {
                agreement = agreementRepo.Get(agreement.Id, new ColumnSet(lys_agreement.Fields.lys_creditid, lys_agreement.Fields.lys_creditperiod, lys_agreement.Fields.lys_creditamount, lys_agreement.Fields.lys_name));

                // Checking if agreement has all required optional fields set.
                if (agreement.lys_creditid == null || agreement.lys_creditperiod == null || agreement.lys_creditamount == null)
                {
                    return;
                }
            }

            // Deleting all existing automatically created related invoices.
            foreach (Entity entity in relatedInvoices.Entities)
            {
                invoiceRepo.Delete(entity.Id);
            }

            // Creating new related invoices based on the credit information.
            int      creditPeriodMonths  = agreement.lys_creditperiod.Value * 12;
            Decimal  monthlyCreditAmount = agreement.lys_creditamount.Value / creditPeriodMonths;
            DateTime nextInvoiceDate     = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).AddMonths(1);

            for (int i = 0; i < creditPeriodMonths; ++i)
            {
                lys_invoice newRelatedInvoice = new lys_invoice();
                newRelatedInvoice.lys_name      = string.Format("Кредитный счет #{0} для договора {1}", i + 1, agreement.lys_name);
                newRelatedInvoice.lys_date      = nextInvoiceDate;
                newRelatedInvoice.lys_paydate   = newRelatedInvoice.lys_date;
                newRelatedInvoice.lys_dogovorid = agreement.ToEntityReference();
                newRelatedInvoice.lys_fact      = false;
                newRelatedInvoice.lys_type      = lys_invoice_lys_type.Avtomaticheskoe_sozdanie;
                newRelatedInvoice.lys_amount    = new Money(monthlyCreditAmount);

                invoiceRepo.Insert(newRelatedInvoice);

                nextInvoiceDate = nextInvoiceDate.AddMonths(1);
            }

            // Updating agreement's payment plan date.
            lys_agreement agreementToUpdate = new lys_agreement();

            agreementToUpdate.Id = agreement.Id;
            agreementToUpdate.lys_paymentplandate = DateTime.Now.AddDays(1);

            agreementRepo.Update(agreementToUpdate);

            TracingService.Trace($"Created {creditPeriodMonths} new lys_invoices related to lys_agreement with ID={agreement.Id}");
        }