public ViewResult Create()
        {
            var InvoiceModel = new InvoiceEditModel();

            ViewBag.FormAspAction = "Create";

            return(View("Edit", InvoiceModel));
        }
        public async Task <IActionResult> Create([FromForm] InvoiceEditModel model)
        {
            var Invoice = new Invoice();

            _mapper.Map(model, Invoice);

            await _invoiceRepository.StoreNewAsync(Invoice);

            return(RedirectToAction("Index"));
        }
        public IActionResult CreateInvoice()
        {
            ViewBag.InvoiceSuccess = HttpContext.Session.GetString("InvoiceSuccess");
            InvoiceEditModel invoice = new InvoiceEditModel()
            {
                Company        = new SelectList(CompanyRepository.GetAll(), "Id", "name"),
                ServiceRequest = new SelectList(ServiceRequestImpl.GetRequest(), "Id", "RequestName")
            };

            return(View(invoice));
        }
Example #4
0
 public ActionResult Edit(int id, InvoiceEditModel model)
 {
     if (ModelState.IsValid)
     {
         model.CompanyID = 1;
         Mapper.CreateMap <UserEditModel, Invoice>();
         var dbModel = Mapper.Map <Invoice>(model);
         _invoices.Update(dbModel);
         _unitOfWork.Save();
         return(RedirectToAction("Index"));
     }
     return(View(model));
 }
        public async Task <IActionResult> Edit(int Id, [FromForm] InvoiceEditModel model)
        {
            var Invoice = await _invoiceRepository.GetByIdAsync(Id);

            if (Invoice == null)
            {
                Response.StatusCode = NotFound().StatusCode;
                return(View("Invoice not found!"));
            }

            _mapper.Map(model, Invoice);

            await _invoiceRepository.UpdateAsync(Invoice);

            return(RedirectToAction("Index"));
        }
Example #6
0
        public async Task <IActionResult> Edit([FromBody] InvoiceEditModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState.GetErrorList()));
            }

            if (!EnumerableExtensions.Any(model.Items))
            {
                return(BadRequest("Please select items/services to continue"));
            }

            try
            {
                await _invoiceManager.EditAsync(model);
            }
            catch (Exception ex)
            {
                return(BadRequest(ex.Message));
            }
            return(Ok());
        }
        public IActionResult CreateInvoice(InvoiceEditModel model)
        {
            if (ModelState.IsValid)
            {
                var po = CompanyRepository.Get(model.CompanyID);
                var pk = ServiceRequestImpl.Get(model.ServiceRequestID);
                ServiceTypeRecord pq = ConfigureService.GetServiceType(pk.Service.Id, pk.ServiceType.Id);

                var qz = new Invoice()
                {
                    Company        = po,
                    InvoiceDate    = DateTime.Parse(model.Date),
                    ServiceRequest = pk,
                    InvoiceNo      = GenarateInvoiceNo(),
                    description    = pk.Service.ServiceName,
                    Quantity       = 1,
                    unitPrice      = pq.Price,
                    totalPrice     = pq.Price,
                    SubTotal       = pq.Price,
                    tax            = model.tax,
                    Total          = pq.Price + model.tax
                };

                InvoiceRepository.CreateInvoice(qz);
                InvoiceRepository.Commit();

                HttpContext.Session.SetString("InvoiceSuccess", "Invoice Successfully Created");

                return(RedirectToAction("CreateInvoice"));
            }
            var invoice = new InvoiceEditModel()
            {
                Company        = new SelectList(CompanyRepository.GetAll(), "Id", "Fullname"),
                ServiceRequest = new SelectList(ServiceRequestImpl.GetRequest(), "Id", "RequestName")
            };

            return(View(invoice));
        }
Example #8
0
        public ActionResult Invoice(Guid?id, [ModelBinder(typeof(GuidArrayModelBinder))] Guid[] docs, string extra = null)
        {
            if (docs == null || !docs.Any())
            {
                return(new HttpNotFoundResult());
            }

            var apConfig = ConfigBase.Settings.Applications.SelectForLoggedUser((AuthPrincipal)User, id.Value);

            if (apConfig == null)
            {
                return(new HttpNotFoundResult());
            }

            var model = new InvoiceEditModel
            {
                DocumentId = docs.First(),
                DataJson   = apConfig.GetAppDataJson(),
                AppId      = id.Value
            };

            return(View(model));
        }
        public async Task EditAsync(InvoiceEditModel 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);
            //}
            await _transactionRepository.DeleteTransaction(model.Id);

            var invoice = await _invoiceRepository.GetAsync(model.Id);

            //InvoiceFactory.Create(model, invoice, _userId, items);
            InvoiceFactory.EditInvoice(model, invoice, _userId);

            _invoiceRepository.Edit(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();
                }
            }
        }
Example #10
0
        //public static Invoice Create(InvoiceAddModel model, string userId, List<Item> items)
        //{
        //    var invoice = new Invoice
        //    {
        //        CustomerId = model.CustomerId,
        //        InvoiceNumber = string.Empty,
        //        Tax = model.Tax,
        //        Discount = model.Discount,
        //        TotalAmount = model.TotalAmount,
        //        Remark = model.Remark,
        //        Status = Constants.InvoiceStatus.Pending,
        //        CreatedBy = userId,
        //        CreatedOn = Utility.GetDateTime(),
        //        Services = items.Select(x => new InvoiceService
        //        {
        //            Id = Guid.NewGuid(),
        //            ServiceId = x.Id,
        //            Rate = x.Rate
        //        }).ToList()
        //    };


        //    if (model.Attachments == null || !model.Attachments.Any())
        //    {
        //        return invoice;
        //    }

        //    foreach (var attachment in model.Attachments)
        //    {
        //        invoice.Attachments = new List<InvoiceAttachment>
        //        {
        //            new InvoiceAttachment
        //            {
        //                Title = attachment.Title,
        //                FileName = attachment.FileName,
        //                OriginalFileName = attachment.OriginalFileName,
        //                CreatedBy =userId,
        //                CreatedOn =Utility.GetDateTime()
        //            }
        //        };
        //    }

        //    return invoice;
        //}

        //public static void Create(InvoiceEditModel model, Invoice entity, string userId, List<Item> items)
        //{
        //    entity.CustomerId = model.CustomerId;
        //    entity.Tax = model.Tax;
        //    entity.Discount = model.Discount;
        //    entity.TotalAmount = model.TotalAmount;
        //    entity.Remark = model.Remark;
        //    entity.UpdatedBy = userId;
        //    entity.UpdatedOn = Utility.GetDateTime();

        //    var deletedServices = entity.Services.Where(x => !model.Items.Contains(x.ServiceId)).ToList();

        //    foreach (var deletedService in deletedServices)
        //    {
        //        entity.Services.Remove(deletedService);
        //    }

        //    var addedServices = items
        //        .Where(x => !entity.Services.Select(y => y.ServiceId).Contains(x.Id))
        //        .ToList();

        //    foreach (var service in addedServices)
        //    {
        //        entity.Services.Add(new InvoiceService
        //        {
        //            Id = Guid.NewGuid(),
        //            ServiceId = service.Id,
        //            Rate = service.Rate
        //        });
        //    }

        //    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 invoiceAttachment = entity.Attachments.SingleOrDefault(x => x.FileName.Equals(attachment.FileName));

        //        if (invoiceAttachment == null)
        //        {
        //            invoiceAttachment = new InvoiceAttachment
        //            {
        //                Title = attachment.Title,
        //                FileName = attachment.FileName,
        //                OriginalFileName = attachment.OriginalFileName,
        //                CreatedBy = userId,
        //                CreatedOn = Utility.GetDateTime()
        //            };
        //        }
        //        else
        //        {
        //            invoiceAttachment.Title = attachment.Title;
        //            invoiceAttachment.FileName = attachment.FileName;
        //            invoiceAttachment.OriginalFileName = attachment.OriginalFileName;
        //        }

        //        entity.Attachments.Add(invoiceAttachment);
        //    }
        //}


        public static void EditInvoice(InvoiceEditModel model, Invoice 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.InvoiceDate        = model.InvoiceDate;
            entity.StrInvoiceDate     = model.InvoiceDate.ToString("yyyy-MM-dd");
            entity.DueDate            = model.DueDate;
            entity.StrDueDate         = model.DueDate.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 alreadyExistServices = 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 InvoiceService
                {
                    Id            = Guid.NewGuid(),
                    ServiceId     = service.ServiceId,
                    Rate          = service.Rate,
                    TaxId         = service.TaxId,
                    Price         = service.Price,
                    TaxPrice      = service.TaxPrice,
                    Quantity      = service.Quantity,
                    TaxPercentage = service.TaxPercentage,
                    LineAmount    = service.LineAmount
                });
            }

            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.SingleOrDefault(x => x.FileName.Equals(deletedAttachemntFile));
                entity.Attachments.Remove(attachemnt);
            }

            foreach (var attachment in model.Attachments)
            {
                var invoiceAttachment = entity.Attachments.SingleOrDefault(x => x.FileName.Equals(attachment.FileName));

                if (invoiceAttachment == null)
                {
                    invoiceAttachment = new InvoiceAttachment
                    {
                        Title            = attachment.Title,
                        FileName         = attachment.FileName,
                        OriginalFileName = attachment.OriginalFileName,
                        CreatedBy        = userId ?? "0",
                        CreatedOn        = Utility.GetDateTime()
                    };
                }
                else
                {
                    invoiceAttachment.Title            = attachment.Title;
                    invoiceAttachment.FileName         = attachment.FileName;
                    invoiceAttachment.OriginalFileName = attachment.OriginalFileName;
                }

                entity.Attachments.Add(invoiceAttachment);
            }
        }