public ActionResult DeleteConfirmed(int id)
        {
            InternalSale internalSale = db.InternalSales.Find(id);

            db.InternalSales.Remove(internalSale);
            db.SaveChanges();
            return(RedirectToAction("Index"));
        }
 public ActionResult Edit([Bind(Include = "InternalSaleID,InvoiceID,ItemID,Price,OrderQnty,IsDeleted")] InternalSale internalSale)
 {
     if (ModelState.IsValid)
     {
         db.Entry(internalSale).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     ViewBag.InvoiceID = new SelectList(db.Invoices, "InvoiceID", "PartNumber", internalSale.InvoiceID);
     ViewBag.ItemID    = new SelectList(db.Items, "ItemID", "ItemID", internalSale.ItemID);
     return(View(internalSale));
 }
        // GET: InternalSale/Details/5
        public ActionResult Details(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            InternalSale internalSale = db.InternalSales.Find(id);

            if (internalSale == null)
            {
                return(HttpNotFound());
            }
            return(View(internalSale));
        }
        // GET: InternalSale/Edit/5
        public ActionResult Edit(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            InternalSale internalSale = db.InternalSales.Find(id);

            if (internalSale == null)
            {
                return(HttpNotFound());
            }
            ViewBag.InvoiceID = new SelectList(db.Invoices, "InvoiceID", "PartNumber", internalSale.InvoiceID);
            ViewBag.ItemID    = new SelectList(db.Items, "ItemID", "ItemID", internalSale.ItemID);
            return(View(internalSale));
        }
        public JsonResult Create(string custName, decimal invDiscount, string pmntType, string phone, string order)
        {
            if (Session["OutletID"] != null)
            {
                var    lastInv = db.Invoices.OrderByDescending(e => e.InvoiceID).FirstOrDefault();
                int    sid     = Convert.ToInt32(Session["OutletID"].ToString());
                string year    = DateTime.Now.Year.ToString();
                //InternalInvoice
                Invoice inv = new Invoice();
                if (lastInv == null)
                {
                    inv.PartNumber = "LINV" + year + "00001";
                }
                else
                {
                    inv.PartNumber = "LINV" + year + (Convert.ToInt32(lastInv.PartNumber.Substring(8, lastInv.PartNumber.Length - 8)) + 1).ToString("D5");
                }
                inv.CustomerName    = custName;
                inv.ContactNumber   = phone;
                inv.PaymentType     = pmntType;
                inv.MadyBy          = HttpContext.User.Identity.Name;
                inv.OutletID        = sid;
                inv.InvoiceDiscount = invDiscount;
                inv.IsPaid          = true;
                inv.IsDeleted       = false;
                inv.InvoiceDate     = DateTime.Now;

                db.Invoices.Add(inv);
                db.SaveChanges();

                int lastInvID = inv.InvoiceID;

                InternalSale[] a = JsonConvert.DeserializeObject <InternalSale[]>(order);
                foreach (var item in a)
                {
                    InternalSale sale = new InternalSale();
                    sale.ItemID    = item.ItemID;
                    sale.OrderQnty = item.OrderQnty;
                    sale.Price     = item.Price;
                    sale.InvoiceID = lastInvID;
                    sale.ItemSize  = item.ItemSize;
                    db.InternalSales.Add(sale);

                    var updateItem = db.Items.FirstOrDefault(i => i.ItemID == item.ItemID);
                    if (updateItem != null)
                    {
                        updateItem.AvailableQnty  -= item.OrderQnty;
                        db.Entry(updateItem).State = EntityState.Modified;
                        db.SaveChanges();

                        if (updateItem.AvailableQnty <= 5)
                        {
                            updateItem.IsAvailable     = false;
                            db.Entry(updateItem).State = EntityState.Modified;
                            db.SaveChanges();
                        }
                    }
                }
                db.SaveChanges();

                return(Json("saved", JsonRequestBehavior.AllowGet));
            }
            else
            {
                return(Json(null, JsonRequestBehavior.AllowGet));
            }
        }