public static Invoice Create(InvoiceAddModel model, string userId, int count) { var invoice = new Invoice { CustomerId = model.CustomerId, InvoiceNumber = "INV" + "-" + model.InvoiceDate.ToString("yy") + "-" + (count + 1).ToString("000"), Tax = model.Tax, Discount = model.Discount, TotalAmount = model.TotalAmount, Remark = model.Remark, Status = Constants.InvoiceStatus.Pending, CreatedBy = userId ?? "0", CreatedOn = Utility.GetDateTime(), InvoiceDate = model.InvoiceDate, StrInvoiceDate = model.InvoiceDate.ToString("yyyy-MM-dd"), DueDate = model.DueDate, StrDueDate = model.DueDate.ToString("yyyy-MM-dd"), PoSoNumber = model.PoSoNumber, SubTotal = model.SubTotal, LineAmountSubTotal = model.LineAmountSubTotal, Services = model.Items.Select(x => new InvoiceService { Id = Guid.NewGuid(), ServiceId = x.ServiceId, Rate = x.Rate, Quantity = x.Quantity, Price = x.Price, TaxId = x.TaxId, TaxPrice = x.TaxPrice, TaxPercentage = x.TaxPercentage, LineAmount = x.LineAmount }).ToList() }; if (model.Attachments == null || !model.Attachments.Any()) { return(invoice); } invoice.Attachments = model.Attachments.Select(x => new InvoiceAttachment { Title = x.Title, FileName = x.FileName, OriginalFileName = x.OriginalFileName, CreatedBy = userId ?? "0", CreatedOn = Utility.GetDateTime() }).ToList(); return(invoice); }
public async Task <IActionResult> Add([FromBody] InvoiceAddModel model) { if (!ModelState.IsValid) { return(BadRequest(ModelState.GetErrorList())); } if (!EnumerableExtensions.Any(model.Items)) { return(BadRequest("Please select items/services to continue")); } try { await _invoiceManager.AddAsync(model); } catch (Exception ex) { return(BadRequest(ex.Message)); } return(Ok()); }
public async Task AddAsync(InvoiceAddModel 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); //} model.LineAmountSubTotal = model.Items.Sum(x => x.LineAmount); var count = await _invoiceRepository.getCount(); //await _invoiceRepository.AddAsync(InvoiceFactory.Create(model, _userId, items)); var invoice = InvoiceFactory.Create(model, _userId, count); await _invoiceRepository.AddAsync(invoice); await _unitOfWork.SaveChangesAsync(); var transaction = TransactionFactory.CreateByInvoice(invoice); await _transactionRepository.AddAsync(transaction); await _unitOfWork.SaveChangesAsync(); var itemsList = (model.Items.GroupBy(l => l.BankAccountId, l => new { l.BankAccountId, l.LineAmount }) .Select(g => new { GroupId = g.Key, Values = g.ToList() })).ToList(); foreach (var item in itemsList) { var id = item.GroupId; var amount = item.Values.Sum(x => x.LineAmount); var itemsData = TransactionFactory.CreateByInvoiceItemsAndTax(invoice, id, amount); await _transactionRepository.AddAsync(itemsData); await _unitOfWork.SaveChangesAsync(); } var taxlistList = (model.Items.GroupBy(l => l.TaxBankAccountId, l => new { l.TaxBankAccountId, l.TaxPrice }) .Select(g => new { GroupId = g.Key, Values = g.ToList() })).ToList(); foreach (var tax in taxlistList) { if (tax.GroupId > 0) { var id = tax.GroupId; var amount = tax.Values.Sum(x => x.TaxPrice); var taxData = TransactionFactory.CreateByInvoiceItemsAndTax(invoice, id, amount); await _transactionRepository.AddAsync(taxData); await _unitOfWork.SaveChangesAsync(); } } }