public async Task EditAsync(RecInvoiceEditModel model) { //var items = (await _itemRepository.GetAsync(model.Items)).ToList(); //model.TotalAmount = items.Sum(x => x.Rate); //model.Tax = items.Where(x => x.IsTaxable).Sum(x => x.Rate * x.SalesTax.TaxPercentage / 100); //var customer = await _customerRepository.GetAsync(model.CustomerId); //if (customer.Discount != null) //{ // model.Discount = model.TotalAmount * customer.Discount / 100; // model.TotalAmount = model.TotalAmount - (model.Discount ?? 0); //} //if (model.Tax != null) //{ // model.TotalAmount = model.TotalAmount + (model.Tax ?? 0); //} var invoice = await _recInvoiceRepository.GetAsync(model.Id); //InvoiceFactory.Create(model, invoice, _userId, items); RecurringInvoiceFactory.EditInvoice(model, invoice, _userId); _recInvoiceRepository.Edit(invoice); await _unitOfWork.SaveChangesAsync(); }
public async Task <IActionResult> Edit([FromBody] RecInvoiceEditModel model) { if (!ModelState.IsValid) { return(BadRequest(ModelState.GetErrorList())); } if (!EnumerableExtensions.Any(model.Items)) { return(BadRequest("Please select items/services to continue")); } try { await _recInvoiceManager.EditAsync(model); } catch (Exception ex) { return(BadRequest(ex.Message)); } return(Ok()); }
public static void EditInvoice(RecInvoiceEditModel model, RecurringInvoice entity, string userId) { entity.CustomerId = model.CustomerId; entity.Tax = model.Tax; entity.Discount = model.Discount; entity.TotalAmount = model.TotalAmount; entity.Remark = model.Remark; entity.UpdatedBy = userId ?? "0"; entity.UpdatedOn = Utility.GetDateTime(); entity.RecInvoiceDate = model.RecInvoiceDate; entity.StrRecInvoiceDate = model.RecInvoiceDate.ToString("yyyy-MM-dd"); entity.RecDueDate = model.RecDueDate; entity.StrRecDueDate = model.RecDueDate.ToString("yyyy-MM-dd"); entity.PoSoNumber = model.PoSoNumber; entity.SubTotal = model.SubTotal; entity.LineAmountSubTotal = model.LineAmountSubTotal; //int[] arr = new int[100]; ArrayList tempArr = new ArrayList(); //for (int i=0;i<model.Items.Count; i++) //{ // arr[i] = model.Items[i].ServiceId; //} foreach (var item in model.Items) { tempArr.Add(item.ServiceId); var alreadyExistServices = entity.Services.Where(x => item.ServiceId == x.ServiceId).FirstOrDefault(); //entity.Services.Where(x => item.ServiceId == x.ServiceId).Select(c => { c.CreditLimit = 1000; return c; }); if (alreadyExistServices != null) { alreadyExistServices.Price = item.Price; alreadyExistServices.TaxId = item.TaxId; alreadyExistServices.TaxPercentage = item.TaxPercentage; alreadyExistServices.Rate = item.Rate; alreadyExistServices.Quantity = item.Quantity; alreadyExistServices.TaxPrice = item.TaxPrice; alreadyExistServices.LineAmount = item.LineAmount; entity.Services.Add(alreadyExistServices); } } var deletedServices = entity.Services.Where(x => !tempArr.Contains(x.ServiceId)).ToList(); //var resultAll = items.Where(i => filter.All(x => i.Features.Any(f => x == f.Id))); foreach (var deletedService in deletedServices) { entity.Services.Remove(deletedService); } var addedServices = model.Items .Where(x => !entity.Services.Select(y => y.ServiceId).Contains(x.ServiceId)) .ToList(); foreach (var service in addedServices) { entity.Services.Add(new RecurringInvoiceService { Id = Guid.NewGuid(), ServiceId = service.ServiceId, Rate = service.Rate, TaxId = service.TaxId, Price = service.Price, Quantity = service.Quantity, TaxPercentage = service.TaxPercentage, TaxPrice = service.TaxPrice }); } if (model.Attachments == null || !model.Attachments.Any()) { return; } var deletedAttachemntFiles = entity.Attachments.Select(x => x.FileName) .Except(model.Attachments.Select(y => y.FileName)).ToList(); foreach (var deletedAttachemntFile in deletedAttachemntFiles) { var attachemnt = entity.Attachments.Single(x => x.FileName.Equals(deletedAttachemntFile)); entity.Attachments.Remove(attachemnt); } foreach (var attachment in model.Attachments) { var recInvoiceAttachment = entity.Attachments.SingleOrDefault(x => x.FileName.Equals(attachment.FileName)); if (recInvoiceAttachment == null) { recInvoiceAttachment = new RecurringInvoiceAttachment { Title = attachment.Title, FileName = attachment.FileName, OriginalFileName = attachment.OriginalFileName, CreatedBy = userId ?? "0", CreatedOn = Utility.GetDateTime() }; } else { recInvoiceAttachment.Title = attachment.Title; recInvoiceAttachment.FileName = attachment.FileName; recInvoiceAttachment.OriginalFileName = attachment.OriginalFileName; } entity.Attachments.Add(recInvoiceAttachment); } }