public void changesupplier(int itemcode,int purchaseid,string comname,string userid )
 {
     var n = from a in ctx.purchase_item
             from b in ctx.purchases
             where (a.itemId == itemcode) && (b.purchaserId == purchaseid)
             &&(a.purchaseId==b.purchaserId)
             select a;
     purchase_item pt=n.SingleOrDefault();
     int x = pt.itemId;
     ctx.purchase_item.Remove(pt);
     var l = from a in ctx.suppliers
             where a.compName == comname
             select a.supplierId;
     int ls = l.FirstOrDefault();
     purchase po = new purchase();
     po.supplierId = ls;
     po.purchaseDate = DateTime.Today.Date;
     po.expectedDeliveryDate = DateTime.Today.Date.AddDays(14);
     po.status = "waiting";
     po.userId = userid;
     ctx.purchases.Add(po);
     var xo = from a in ctx.items
              where a.itemId == x
              select a;
     item xx = xo.SingleOrDefault();
     purchase_item npt = new purchase_item();
     npt.purchaseId = po.purchaserId;
     npt.itemId = xx.itemId;
     npt.requestQty = xx.reorderQty;
     npt.supplierid = ls;
     ctx.purchase_item.Add(npt);
     ctx.SaveChanges();
 }
 public void additems(int itemcode, int supplierid,int purchaseid,int qt)
 {
     purchase_item pitem = new purchase_item();
     pitem.purchaseId = purchaseid;
     pitem.itemId = itemcode;
     pitem.requestQty = qt;
     pitem.supplierid = supplierid;
     ctx.purchase_item.Add(pitem);
     ctx.SaveChanges();
 }
 //
 public void formorder(User u)
 {
     var n = from a in ctx.items
             where (a.balance < a.reorderlevel) && (a.status == "stockout")
             from c in ctx.item_supplier
             where a.itemId == c.itemId
             from b in ctx.suppliers
             where b.supplierId == c.supplierId && (c.priority == 1)
             select b;
     var d = from a in ctx.items
             where (a.balance < a.reorderlevel) && (a.status == "stockout")
             from c in ctx.item_supplier
             where a.itemId == c.itemId
             from b in ctx.suppliers
             where b.supplierId == c.supplierId && (c.priority == 1)
             select c;
     List<supplier> s = n.ToList();
     List<item_supplier> ls = d.ToList();
     foreach (supplier x in s)
     {
         purchase po = new purchase();
         po.supplierId = x.supplierId;
         po.purchaseDate = DateTime.Today.Date;
         po.userId = u.UserId;
         po.expectedDeliveryDate = DateTime.Today.Date.AddDays(14);
         po.status = "waiting";
         ctx.purchases.Add(po);
         ctx.SaveChanges();
         foreach (item_supplier i in ls)
         {
             if (i.supplierId == x.supplierId)
             {
                 var y = from a in ctx.items
                         where a.itemId == i.itemId
                         select a;
                 item item = y.SingleOrDefault();
                 item.status = "ordering";
                 purchase_item pitem = new purchase_item();
                 pitem.purchaseId = po.purchaserId;
                 pitem.itemId = i.itemId.Value;
                 pitem.requestQty = item.reorderlevel;
                 pitem.supplierid = po.supplierId;
                 ctx.purchase_item.Add(pitem);
                 ctx.SaveChanges();
             }
         }
     }
 }