public ActionResult AddPurchaseReceipt(int id)
        {
            var po    = _purchasingService.GetPurchaseOrderById(id);
            var model = new Models.ViewModels.Purchases.AddPurchaseReceipt();

            model.PreparePurchaseReceiptViewModel(po);
            return(View(model));
        }
        public ActionResult AddPurchaseReceipt(Models.ViewModels.Purchases.AddPurchaseReceipt model)
        {
            bool hasChanged = false;

            foreach (var line in model.PurchaseReceiptLines)
            {
                if (line.InQty.HasValue && line.InQty.Value != 0)
                {
                    hasChanged = true;
                    break;
                }
            }

            if (!hasChanged)
            {
                return(RedirectToAction("PurchaseOrders"));
            }

            var po = _purchasingService.GetPurchaseOrderById(model.Id);

            var poReceipt = new PurchaseReceiptHeader()
            {
                Date     = DateTime.Now,
                Vendor   = po.Vendor,
                VendorId = po.VendorId,
                PurchaseOrderHeaderId = po.Id,
                CreatedBy             = User.Identity.Name,
                CreatedOn             = DateTime.Now,
                ModifiedBy            = User.Identity.Name,
                ModifiedOn            = DateTime.Now
            };

            foreach (var receipt in model.PurchaseReceiptLines)
            {
                if ((receipt.InQty + receipt.ReceiptQuantity) > receipt.Quantity)
                {
                    return(RedirectToAction("PurchaseOrders"));
                }

                poReceipt.PurchaseReceiptLines.Add(new PurchaseReceiptLine()
                {
                    PurchaseOrderLineId = receipt.PurchaseOrderLineId,
                    ItemId           = receipt.ItemId,
                    MeasurementId    = receipt.UnitOfMeasurementId,
                    Quantity         = receipt.Quantity,
                    ReceivedQuantity = (receipt.InQty.HasValue ? receipt.InQty.Value : 0),
                    Cost             = receipt.Cost.Value,
                    Amount           = receipt.Cost.Value * (receipt.InQty.HasValue ? receipt.InQty.Value : 0),
                    CreatedBy        = User.Identity.Name,
                    CreatedOn        = DateTime.Now,
                    ModifiedBy       = User.Identity.Name,
                    ModifiedOn       = DateTime.Now
                });
            }

            _purchasingService.AddPurchaseOrderReceipt(poReceipt);
            return(RedirectToAction("PurchaseOrders"));
        }
 public ActionResult AddPurchaseReceipt(int id)
 {
     var po = _purchasingService.GetPurchaseOrderById(id);
     var model = new Models.ViewModels.Purchases.AddPurchaseReceipt();
     model.PreparePurchaseReceiptViewModel(po);
     return View(model);
 }