public override async Task ProcessAsync(ProcessEntityParametrs parameters)
        {
            await _invoiceManager.DeleteAsync(parameters.ProjectId, InvoiceType.Partial);

            var invoices = new List<ProjectInvoice>();
            var data = parameters.StateModel as PartialInvoices;
            foreach(var invoice in data.Invoices)
            {
                var item = new ProjectInvoice
                {
                    Id = 0,
                    CustomerId = parameters.CustomerId,
                    InvoiceAmount = invoice.Amount,
                    InvoiceDate = invoice.Date,
                    InvoiceNumber = invoice.Number,
                    PaymentDate = invoice.PaymentDate,
                    ProjectId = parameters.ProjectId,
                    Status = invoice.IsPaid ? InvoiceStatus.Paid : InvoiceStatus.Open,
                    TransitionId = parameters.TransitionId,
                    Type = InvoiceType.Partial,
                };
                invoices.Add(item);
            }
            var result = _invoiceManager.AddBunch(invoices);
        }
 private int CalcLateDays(ProjectInvoice invoice)
 {
     return (int)(invoice.Status == InvoiceStatus.Paid ?
         invoice.PaymentDate.Value.Subtract(invoice.InvoiceDate).TotalDays :
         DateTime.UtcNow.Subtract(invoice.InvoiceDate).TotalDays);
 }
        public async Task<Infrastructure.ActionResult> SaveAsync(ProjectInvoice invoice, AppUser currentUser)
        {
            try
            {
                int customerId = 0;
                string customerIdValue = (from pt in _dbContext.ProjectTransitions
                                          join ptv in _dbContext.ProjectTransitionValues on pt.Id equals ptv.TransitionId
                                          where pt.ProjectId == invoice.ProjectId && ptv.ParamName == "Customer"
                                          select ptv.ParamValue).FirstOrDefault();

                if (!int.TryParse(customerIdValue, out customerId))
                {
                    throw new ArgumentNullException("Customer not found");
                }

                if (invoice.Id == 0)
                {
                    invoice.CustomerId = customerId;
                    _dbContext.ProjectInvoices.Add(invoice);

                    if (invoice.Status == InvoiceStatus.PartiallyPaid && invoice.PartialPayments != null)
                    {
                        foreach (var partialPayment in invoice.PartialPayments)
                        {
                            partialPayment.Invoice = invoice;
                            _dbContext.ProjectInvoicePartialPayments.Add(partialPayment);
                        }

                        if (invoice.LeftToPayByPartialPayments == 0)
                        {
                            invoice.Status = InvoiceStatus.Paid;
                            invoice.PaymentDate = invoice.PartialPayments.LastOrDefault()?.Date;
                        }
                    }

                    _historyManager.LogInfo(invoice.ProjectId, invoice.TransitionId, invoice, EventType.CreateInvoice, currentUser);
                }
                else
                {
                    var item = await _dbContext.ProjectInvoices.Include(i => i.PartialPayments).FirstOrDefaultAsync(i => i.Id == invoice.Id);
                    if (item != null)
                    {
                        item.Comment = invoice.Comment;
                        item.CustomerId = customerId;
                        item.InvoiceAmount = invoice.InvoiceAmount;
                        item.InvoiceDate = invoice.InvoiceDate;
                        item.InvoiceNumber = invoice.InvoiceNumber;
                        //item.LateDays = CalcLateDays(invoice);
                        item.PaymentDate = invoice.PaymentDate;
                        item.ProjectId = invoice.ProjectId;
                        item.Status = invoice.Status;
                        item.Transition = invoice.Transition;
                        item.TransitionId = invoice.TransitionId;
                        item.Type = invoice.Type;

                        var dbItems = item.PartialPayments ?? new List<ProjectInvoicePartialPayment>();
                        if (invoice.PartialPayments == null || !invoice.PartialPayments.Any())
                        {
                            if (dbItems != null)
                                _dbContext.ProjectInvoicePartialPayments.RemoveRange(dbItems);
                        }
                        else
                        {
                            var deletedItems = dbItems.Where(r => !invoice.PartialPayments.Any(rr => rr.Id == r.Id)).ToList();
                            var addedItems = invoice.PartialPayments.Where(r => !dbItems.Any(rr => rr.Id == r.Id)).ToList();
                            _dbContext.ProjectInvoicePartialPayments.RemoveRange(deletedItems);
                            _dbContext.ProjectInvoicePartialPayments.AddRange(addedItems);
                            foreach (var edit in dbItems.Except(deletedItems))
                            {
                                var property = invoice.PartialPayments.FirstOrDefault(p => p.Id == edit.Id);
                                edit.Amount = property.Amount;
                                edit.Date = property.Date;
                            }

                            if (invoice.Status == InvoiceStatus.PartiallyPaid && invoice.LeftToPayByPartialPayments == 0)
                            {
                                item.Status = InvoiceStatus.Paid;
                                item.PaymentDate = invoice.PartialPayments.LastOrDefault()?.Date;
                            }
                            else if (invoice.Status == InvoiceStatus.Paid && invoice.LeftToPayByPartialPayments > 0)
                            {
                                item.Status = InvoiceStatus.PartiallyPaid;
                                item.PaymentDate = null;
                            }
                        }
                        _historyManager.LogInfo(invoice.ProjectId, invoice.TransitionId, invoice, EventType.ModifyInvoice, currentUser);
                    }
                }
                _dbContext.SaveChanges();

                await _alertsManager.CreateScheduledAlertForInvoice(invoice);

                return Infrastructure.ActionResult.Success(invoice);
            }
            catch (Exception ex)
            {
                return Infrastructure.ActionResult.Failed(ex);
            }
        }
Example #4
0
 public async Task CreateScheduledAlertForInvoice(ProjectInvoice invoice)
 {
     if (_dbContext.ProjectTransitions.Any(t => t.ProjectId == invoice.ProjectId))
     {
         var invoiceAlertLinksId = await _dbContext.AlertToProjectLinkConditions.Where(c => c.PropertyName.Contains("Invoice.")).Select(ac => ac.AlertLinkId).Distinct().ToListAsync();
         var invoiceAlertLinks = await _dbContext.AlertToProjectLinks.Where(a => invoiceAlertLinksId.Contains(a.Id)).ToListAsync();
         var transitionId = invoice.TransitionId ?? _dbContext.ProjectTransitions.First(t => t.ProjectId == invoice.ProjectId).Id;
         foreach (var link in invoiceAlertLinks)
         {
             var alertScheduled = _dbContext.ProjectAlertsScheduled.Any(a => a.AlertLinkId == link.Id.ToString() && a.ProjectId == invoice.ProjectId && a.StateId == link.StateId && a.TransitionId == transitionId && a.InvoiceId == invoice.Id);
             if (!alertScheduled)
             {
                 _dbContext.ProjectAlertsScheduled.Add(new ProjectAlertScheduled
                 {
                     AlertLinkId = link.Id.ToString(),
                     ProjectId = invoice.ProjectId,
                     StateId = link.StateId,
                     TransitionId = transitionId,
                     InvoiceId = invoice.Id
                 });
             }
         }
         _dbContext.SaveChanges();
     }
 }