private static InvoiceDetail getEntityByModel(InvoiceDetailModel model)
        {
            if (model == null) return null;
            InvoiceDetail entity = new InvoiceDetail();

            if (model.Id == 0)
            {
                entity.CreateBy = AuthenticationHelper.UserId;
                entity.CreateDate = DateTime.Now;
            }
            else
            {
                entity.CreateBy = model.CreateBy;
                entity.CreateDate = model.CreateDate;
            }

            entity.Amount = model.Amount;
            entity.Id = model.Id;
            entity.InvoiceId = model.InvoiceId;
            entity.InvoiceSourceId = model.InvoiceSourceId;
            entity.ItemId = model.ItemId;
            entity.Quantity = model.Quantity;
            entity.Rate = model.Rate;
            entity.TaxAmount = model.TaxAmount;
            entity.TaxId = model.TaxId;
            entity.UpdateBy = AuthenticationHelper.UserId;
            entity.UpdateDate = DateTime.Now;
            return entity;
        }
        public ActionResult AddNewPartial(InvoiceDetailModel model)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    if (SessionHelper.Invoice != null)
                    {
                        if (SessionHelper.Invoice.InvoiceDetail != null && SessionHelper.Invoice.InvoiceDetail.Count() > 0)
                            model.Id = SessionHelper.Invoice.InvoiceDetail.LastOrDefault().Id + 1;
                        else
                            model.Id = 1;
                    }
                    else
                        model.Id = 1;

                    if (model.ItemId == null && model.InvoiceSourceId == null)
                    {
                        ViewData["EditError"] = "Either Invoice Source or Item is required.";
                    }
                    else if (model.ItemId != null && model.InvoiceSourceId != null)
                    {
                        ViewData["EditError"] = "Only Invoice Source or Item can be entered at a time.";
                    }
                    else
                    {
                        InvoiceHelper.Insert(model);
                    }
                }
                catch (Exception e)
                {
                    ViewData["EditError"] = e.Message;
                }
            }
            else
                ViewData["EditError"] = "Please, correct all errors.";
            return PartialView("_Detail", InvoiceHelper.GetInvoiceDetail());
        }
 public ActionResult UpdatePartial(InvoiceDetailModel model)
 {
     if (ModelState.IsValid)
     {
         try
         {
             if (model.ItemId == null && model.InvoiceSourceId == null)
             {
                 ViewData["EditError"] = "Either Invoice Source or Item is required.";
             }
             else if (model.ItemId != null && model.InvoiceSourceId != null)
             {
                 ViewData["EditError"] = "Only Invoice Source or Item can be entered at a time.";
             }
             else
             {
                 InvoiceHelper.UpdateInvoiceDetail(model);
             }
         }
         catch (Exception e)
         {
             ViewData["EditError"] = e.Message;
         }
     }
     else
         ViewData["EditError"] = "Please, correct all errors.";
     return PartialView("_Detail", InvoiceHelper.GetInvoiceDetail());
 }
 public ActionResult DeletePartial(InvoiceDetailModel model)
 {
     if (ModelState.IsValid)
     {
         try
         {
             InvoiceModel invoice = SessionHelper.Invoice;
             InvoiceHelper.DeleteInvoiceDetail(model);
             SessionHelper.Invoice = invoice;
             IList<InvoiceDetailModel> invoiceDetail = InvoiceHelper.GetInvoiceDetail();
             return PartialView("_Detail", invoiceDetail);
         }
         catch (Exception e)
         {
             ViewData["EditError"] = e.Message;
         }
     }
     else
         ViewData["EditError"] = "Please, correct all errors.";
     return PartialView("_Detail");
     //return PartialView("_Detail", InvoiceHelper.GetInvoiceDetail());
 }
 public static void DeleteInvoiceDetail(InvoiceDetailModel model)
 {
     InvoiceModel invoice = SessionHelper.Invoice;
     InvoiceDetailModel invoiceDetail = invoice.InvoiceDetail.FirstOrDefault(x => x.Id == model.Id);
     invoice.InvoiceDetail.Remove(invoiceDetail);
 }
        public static void UpdateInvoiceDetail(InvoiceDetailModel model)
        {
            InvoiceModel invoice = SessionHelper.Invoice;

            invoice.InvoiceDetail.FirstOrDefault(x => x.Id == model.Id).InvoiceSourceId = model.InvoiceSourceId;
            invoice.InvoiceDetail.FirstOrDefault(x => x.Id == model.Id).ItemId = model.ItemId;
            invoice.InvoiceDetail.FirstOrDefault(x => x.Id == model.Id).Quantity = model.Quantity;
            invoice.InvoiceDetail.FirstOrDefault(x => x.Id == model.Id).Rate = model.Rate;
            invoice.InvoiceDetail.FirstOrDefault(x => x.Id == model.Id).TaxId = model.TaxId;
            invoice.InvoiceDetail.FirstOrDefault(x => x.Id == model.Id).Amount = model.Quantity * model.Rate;

            if (model.TaxId != null)
            {
                TaxDetailModel taxDetail = TaxHelper.GetTaxDetail(invoice.InvoiceDetail.FirstOrDefault(x => x.Id == model.Id).TaxId.ToString()).FirstOrDefault(x => x.StartDate <= SessionHelper.Invoice.InvoiceDate && x.EndDate >= SessionHelper.Invoice.InvoiceDate);

                if (taxDetail != null)
                    invoice.InvoiceDetail.FirstOrDefault(x => x.Id == model.Id).TaxAmount = invoice.InvoiceDetail.FirstOrDefault(x => x.Id == model.Id).Amount * taxDetail.Rate / 100;
                else
                    invoice.InvoiceDetail.FirstOrDefault(x => x.Id == model.Id).TaxAmount = 0;
            }
        }
        public static void Insert(InvoiceDetailModel model)
        {
            model.Amount = model.Quantity * model.Rate;

            TaxDetailModel taxDetail = TaxHelper.GetTaxDetail(model.TaxId.ToString()).FirstOrDefault(x => x.StartDate <= SessionHelper.Invoice.InvoiceDate && x.EndDate >= SessionHelper.Invoice.InvoiceDate);

            if (taxDetail != null)
                model.TaxAmount = model.Amount * taxDetail.Rate / 100;
            else
                model.TaxAmount = 0;

            InvoiceModel invoice = SessionHelper.Invoice;
            invoice.InvoiceDetail.Add(model);
        }