public ActionResult Manage(int?id)
        {
            var rec = base.BaseCreateEdit <SiteTransasction>(id, "SiteTransID");

            //Since UserID and QtyRemoved is [required] we have to set it before loading the form
            if (rec == null) //We are in create mode
            {
                //rec = new SiteTransasction { UserID = User.Identity.GetUserId(), QtyRemoved = 0 };
                ViewBag.OriginalQty = 0;

                ViewBag.SupplierID = new SelectList(db.Fetch <Supplier>("Select SupplierID,SupplierName from Supplier"), "SupplierID", "SupplierName");
            }
            else
            {
                //In edit mode We need to fetch the name values of the autocomplete boxes
                ViewBag.FromSiteName = db.SingleOrDefault <string>("Select SiteName from Sites where SiteID = @0", rec.SiteID);
                ViewBag.ItemName     = db.SingleOrDefault <string>("Select ItemName from Item where ItemID = @0", rec.ItemID);
                ViewBag.SupplierName = db.SingleOrDefault <string>("Select SupplierName from Supplier where SupplierID = @0", rec.SupplierID);
                ViewBag.OriginalQty  = rec.QtyAdded;
                ViewBag.SupplierID   = new SelectList(db.Fetch <Supplier>("Select SupplierID,SupplierName from Supplier"), "SupplierID", "SupplierName");
            }

            if (id != null)
            {
                ChallanImg ci = new ChallanImg()
                {
                    ItemID      = rec.ItemID.Value,
                    Price       = (decimal)rec.Price,
                    QtyAdded    = (int)rec.QtyAdded,
                    QtyRemoved  = (int)rec.QtyRemoved,
                    Remarks     = rec.Remarks,
                    SiteID      = (int)rec.SiteID,
                    SiteTransID = rec.SiteTransID,
                    SupplierID  = (int)rec.SupplierID,
                    Tdate       = (DateTime)rec.Tdate,
                    UserID      = User.Identity.GetUserId(),
                    path        = rec.path
                };
                return(View(ci));
            }
            else
            {
                ChallanImg ci = new ChallanImg()
                {
                    UserID = User.Identity.GetUserId(), QtyRemoved = 0
                };
                return(View(ci));
            }
        }
        public ActionResult Manage([Bind(Include = "SiteTransID,UserID,Tdate,SiteID,SupplierID,Path,UploadedFile,ItemID,Price,QtyAdded,Remarks,QtyRemoved")] ChallanImg siteTransaction, int OriginalQty)
        {
            siteTransaction.Tdate = DateTime.Now;
            var currentStock = db.SingleOrDefault <SiteCurrentStock>("select * from SiteCurrentStock where SiteID = @0 and ItemID= @1", siteTransaction.SiteID, siteTransaction.ItemID);

            using (var transaction = db.GetTransaction())
            {
                try
                {
                    //on 14 Nov 2017 stanley said suppliers would make their own bills, hence the below was commented out
                    //var sup = db.FirstOrDefault<SupplierBill>("Select SBillID,SupplierID from SupplierBill Where SupplierID = @0", siteTransaction.SupplierID);
                    //var getSupBill = db.FirstOrDefault<SupplierBillDetail>("Select SBillDetailID,SBillID,ItemID,Qty,UnitPrice,QtyRec from SupplierBillDetail Where ItemID= @0 and SBillID = @1", siteTransaction.ItemID, sup.SBillID);
                    //if (siteTransaction.SiteTransID>0)
                    //{
                    //    if (getSupBill != null)
                    //    {
                    //        db.Update("SupplierBillDetail", "SBillDetailID", new { QtyRec =  siteTransaction.QtyAdded + getSupBill.QtyRec - OriginalQty }, getSupBill.SBillDetailID);
                    //        siteTransaction.SBillDetailID = getSupBill.SBillDetailID;
                    //    }
                    //}
                    //else
                    //{
                    //  if (getSupBill != null)
                    //    {
                    //        if (getSupBill.Qty != getSupBill.QtyRec)
                    //        {
                    //            db.Update("SupplierBillDetail", "SBillDetailID", new { QtyRec =siteTransaction.QtyAdded+getSupBill.QtyRec}, getSupBill.SBillDetailID);
                    //        }

                    //        siteTransaction.SBillDetailID = getSupBill.SBillDetailID;
                    //    }

                    //}


                    if (currentStock == null)    //this is the first purchase of the item at thissite
                    {
                        // var getSupBill = db.FirstOrDefault<SupplierBillDetail>("SBillDetailID,SBillID,ItemID,Qty,UnitPrice,QtyRec","Supplier as sp inner join SupplierBillDetail as sbd on sp.SBillID = sbd.SBillID where Supplier Where ItemID= @0 and SupplierID=@0",siteTransaction.ItemID,siteTransaction.SupplierID);


                        var item = new SiteCurrentStock {
                            SiteID = siteTransaction.SiteID, ItemID = siteTransaction.ItemID, Qty = siteTransaction.QtyAdded
                        };
                        db.Save(item);
                    }
                    else
                    {
                        if (siteTransaction.SiteTransID > 0)     //Edit mode
                        {
                            db.Update("SiteCurrentStock", "SiteStockID", new { Qty = currentStock.Qty - OriginalQty + siteTransaction.QtyAdded }, currentStock.SiteStockID);
                        }
                        else
                        {
                            db.Update("SiteCurrentStock", "SiteStockID", new { Qty = currentStock.Qty + siteTransaction.QtyAdded }, currentStock.SiteStockID);
                        }
                    }


                    SiteTransasction res = new SiteTransasction
                    {
                        ItemID      = siteTransaction.ItemID,
                        Price       = siteTransaction.Price,
                        QtyAdded    = siteTransaction.QtyAdded,
                        QtyRemoved  = siteTransaction.QtyRemoved,
                        Remarks     = siteTransaction.Remarks,
                        SiteID      = siteTransaction.SiteID,
                        SiteTransID = siteTransaction.SiteTransID,
                        SupplierID  = siteTransaction.SupplierID,
                        Tdate       = siteTransaction.Tdate,
                        UserID      = siteTransaction.UserID
                    };

                    if (siteTransaction.UploadedFile != null)
                    {
                        string fn = siteTransaction.UploadedFile.FileName.Substring(siteTransaction.UploadedFile.FileName.LastIndexOf('\\') + 1);
                        fn = siteTransaction.SiteID + "_" + fn;
                        string SavePath = System.IO.Path.Combine(Server.MapPath("~/Images"), fn);
                        siteTransaction.UploadedFile.SaveAs(SavePath);

                        //System.Drawing.Bitmap upimg = new System.Drawing.Bitmap(siteTransaction.UploadedFile.InputStream);
                        //System.Drawing.Bitmap svimg = MyExtensions.CropUnwantedBackground(upimg);
                        //svimg.Save(System.IO.Path.Combine(Server.MapPath("~/Images"), fn));

                        res.path = fn;
                    }
                    else
                    {
                        res.path = siteTransaction.path;
                    }

                    base.BaseSave <SiteTransasction>(res, siteTransaction.SiteTransID > 0);
                    transaction.Complete();
                    return(RedirectToAction("Index"));
                }
                catch (Exception ex)
                {
                    db.AbortTransaction();
                    throw ex;
                }
            }
        }