// GET: /ProductMaster/Edit/5

        public ActionResult Edit(int id)
        {
            PurchaseWaybillViewModel pt = _PurchaseWaybillService.GetPurchaseWaybill(id);

            PrepareViewBag();
            if (pt == null)
            {
                return(HttpNotFound());
            }
            return(View("Create", pt));
        }
        // GET: /ProductMaster/Create

        public ActionResult Create()
        {
            PrepareViewBag();

            PurchaseWaybillViewModel vm = new PurchaseWaybillViewModel();

            vm.DocDate    = DateTime.Now;
            vm.DivisionId = (int)System.Web.HttpContext.Current.Session["DivisionId"];
            vm.SiteId     = (int)System.Web.HttpContext.Current.Session["SiteId"];
            vm.EntryNo    = _PurchaseWaybillService.GetMaxDocNo();
            return(View("Create", vm));
        }
        public ActionResult Post(PurchaseWaybillViewModel vm)
        {
            PurchaseWaybill pt = AutoMapper.Mapper.Map <PurchaseWaybillViewModel, PurchaseWaybill>(vm);

            if (ModelState.IsValid)
            {
                if (vm.PurchaseWaybillId <= 0)
                {
                    pt.CreatedDate  = DateTime.Now;
                    pt.ModifiedDate = DateTime.Now;
                    pt.CreatedBy    = User.Identity.Name;
                    pt.ModifiedBy   = User.Identity.Name;
                    pt.ObjectState  = Model.ObjectState.Added;
                    _PurchaseWaybillService.Create(pt);

                    ActivityLog log = new ActivityLog()
                    {
                        ActivityType = (int)(ActivityTypeContants.Added),
                        CreatedBy    = User.Identity.Name,
                        CreatedDate  = DateTime.Now,
                        DocId        = pt.PurchaseWaybillId,
                        Narration    = "A new PurchaseWaybill is created with the id" + pt.PurchaseWaybillId,
                    };
                    new ActivityLogService(_unitOfWork).Create(log);

                    try
                    {
                        _unitOfWork.Save();
                    }
                    catch (Exception ex)
                    {
                        string message = _exception.HandleException(ex);
                        ModelState.AddModelError("", message);
                        PrepareViewBag();
                        return(View("Create", vm));
                    }
                    PrepareViewBag();
                    return(RedirectToAction("Create").Success("Data saved successfully"));
                }

                else
                {
                    PurchaseWaybill temp = _PurchaseWaybillService.Find(pt.PurchaseWaybillId);

                    temp.EntryNo               = pt.EntryNo;
                    temp.DocNo                 = pt.DocNo;
                    temp.DocDate               = pt.DocDate;
                    temp.ConsignerId           = pt.ConsignerId;
                    temp.ReferenceDocNo        = pt.ReferenceDocNo;
                    temp.ShipMethodId          = pt.ShipMethodId;
                    temp.DeliveryPoint         = pt.DeliveryPoint;
                    temp.EstimatedDeliveryDate = pt.EstimatedDeliveryDate;
                    temp.FreightType           = pt.FreightType;
                    temp.FromCityId            = pt.FromCityId;
                    temp.ToCityId              = pt.ToCityId;
                    temp.ProductDescription    = pt.ProductDescription;
                    temp.PrivateMark           = pt.PrivateMark;
                    temp.NoOfPackages          = pt.NoOfPackages;
                    temp.ActualWeight          = pt.ActualWeight;
                    temp.ChargedWeight         = pt.ChargedWeight;
                    temp.ContainerNo           = pt.ContainerNo;
                    temp.FreightAmt            = pt.FreightAmt;
                    temp.OtherCharges          = pt.OtherCharges;
                    temp.ServiceTax            = pt.ServiceTax;
                    temp.ServiceTaxPer         = pt.ServiceTaxPer;
                    temp.TotalAmount           = pt.TotalAmount;
                    temp.Remark                = pt.Remark;
                    temp.IsDoorDelivery        = pt.IsDoorDelivery;
                    temp.IsPOD                 = pt.IsPOD;

                    _PurchaseWaybillService.Update(temp);

                    ActivityLog log = new ActivityLog()
                    {
                        ActivityType = (int)(ActivityTypeContants.Added),
                        CreatedBy    = User.Identity.Name,
                        CreatedDate  = DateTime.Now,
                        Narration    = "PurchaseWaybill is modified with the name " + temp.DocNo,
                        DocId        = pt.PurchaseWaybillId,
                    };
                    new ActivityLogService(_unitOfWork).Create(log);

                    try
                    {
                        _unitOfWork.Save();
                    }

                    catch (Exception ex)
                    {
                        string message = _exception.HandleException(ex);
                        ModelState.AddModelError("", message);
                        PrepareViewBag();
                        return(View("Create", pt));
                    }
                    PrepareViewBag();
                    return(RedirectToAction("Index").Success("Data saved successfully"));
                }
            }
            PrepareViewBag();
            return(View("Create", vm));
        }