public static bool ReceiptItem(DB_StoreEntities db, int billId, int type = 1) { try { PaidBill paid = db.PaidBills.First(x => x.BillId == billId); foreach (var r in paid.Receipts) { Item item = db.Items.Find(r.ItemId); item.Quantity += r.Quantity * type; db.Entry(item).State = EntityState.Modified; } db.SaveChanges(); return(true); } catch { return(false); } }
public static bool DeliveryItem(DB_StoreEntities db, int billId, int type = 1) { //type = -1 là trả về try { EarnBill paid = db.EarnBills.First(x => x.BillId == billId); foreach (var r in paid.Deliveries) { Item item = db.Items.Find(r.ItemId); item.QuantitySold += r.Quantity * type; item.Quantity -= r.Quantity * type; db.Entry(item).State = EntityState.Modified; } db.SaveChanges(); return(true); } catch { return(false); } }
public static int ResetBag(DB_StoreEntities db, int itemid, int quantity, string username, bool add = false) { Account user = db.Accounts.Find(username); try { EarnBill bill = user.EarnBills.First(x => x.Status.Name == "Đang Chờ"); try { Delivery delivery = bill.Deliveries.First(x => x.ItemId == itemid); if (delivery.Quantity == quantity && !add) { return(0); } bill.Price -= delivery.Price * delivery.Quantity; delivery.Quantity = (add) ? delivery.Quantity + quantity : (quantity > 0) ? quantity : 0; if (delivery.Quantity == 0) { db.Deliveries.Remove(delivery); Item item = db.Items.Find(itemid); int price = Convert.ToInt32((item.Sale == null) ? item.Price : (item.Price * (100 - item.Sale) / 100)); delivery = new Delivery() { ItemId = itemid, Quantity = 1, Price = price, }; EarnBill billDelete; try { billDelete = user.EarnBills.First(x => x.Status.Name == "Đã Huỷ"); try { delivery = billDelete.Deliveries.First(x => x.ItemId == itemid); } catch { billDelete.Deliveries.Add(delivery); db.Entry(billDelete).State = EntityState.Modified; } } catch { billDelete = new EarnBill(); billDelete.Address = ""; billDelete.Username = user.Username; billDelete.Date = DateTime.Now; billDelete.StatusId = db.Status.First(x => x.Name.Contains("Đã Huỷ")).StatusId; billDelete.Deliveries.Add(delivery); db.EarnBills.Add(billDelete); } } else { bill.Price += delivery.Price * delivery.Quantity; db.Entry(delivery).State = EntityState.Modified; } } catch (Exception e) { Console.WriteLine(e.Message); Item item = db.Items.Find(itemid); int price = Convert.ToInt32((item.Sale == null) ? item.Price : (item.Price * (100 - item.Sale) / 100)); bill.Deliveries.Add(new Delivery() { ItemId = itemid, BillId = bill.BillId, Quantity = 1, Price = price, }); bill.Price += price; } db.Entry(bill).State = EntityState.Modified; db.SaveChanges(); } catch { EarnBill bill = new EarnBill(); bill.Username = user.Username; bill.Date = DateTime.Now; bill.StatusId = db.Status.First(x => x.Name.Contains("Đang Chờ")).StatusId; Item item = db.Items.Find(itemid); int price = Convert.ToInt32((item.Sale == null) ? item.Price : (item.Price * (100 - item.Sale) / 100)); bill.Deliveries.Add(new Delivery() { ItemId = itemid, BillId = bill.BillId, Quantity = quantity, Price = price, }); bill.Address = user.Address; bill.Price = price * quantity; db.EarnBills.Add(bill); db.SaveChanges(); } return(1); }