public static bool ChangePaidBill(DB_StoreEntities db, int billId, int state = 2) { var s = db.Status.First(x => x.StatusId == state); try { PaidBill bill = db.PaidBills.First(x => x.BillId == billId); if (bill.Status != s && s.Name == "Đã Hoàn Thành") { ReceiptItem(db, billId); } else if (bill.Status.Name == "Đang Vận Chuyển" && s.Name == "Đã Huỷ") { ReceiptItem(db, billId, -1); return(DeletePaidBill(db, billId)); } bill.Status = s; db.Entry(bill).State = EntityState.Modified; db.SaveChanges(); return(true); } catch { return(false); } }
public static bool DeletePaidBill(DB_StoreEntities db, int billId) { try { PaidBill bill = db.PaidBills.First(x => x.BillId == billId); if (bill.Status.Name == "Đã Huỷ") { var listDeliverty = bill.Receipts.ToList(); for (int i = 0; i < listDeliverty.Count; i++) { bill.Receipts.Remove(listDeliverty[i]); } db.PaidBills.Remove(bill); } else { try { PaidBill deleteBill = bill.Publisher.PaidBills.First(x => x.Status.Name == "Đã Huỷ"); var listReceipt = bill.Receipts.ToList(); for (int i = 0; i < listReceipt.Count; i++) { listReceipt[i].Quantity = 1; bill.Receipts.Remove(listReceipt[i]); if (deleteBill.Receipts.Count(x => x.ItemId == listReceipt[i].ItemId) == 0) { db.Receipts.Add(new Receipt() { ItemId = listReceipt[i].ItemId, BillId = deleteBill.BillId, Quantity = 1, Price = listReceipt[i].Price, }); } } db.PaidBills.Remove(bill); } catch { bill.Status = db.Status.First(x => x.Name == "Đã Huỷ"); bill.Price = 0; db.Entry(bill).State = EntityState.Modified; } } db.SaveChanges(); return(true); } catch { return(false); } }
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); } }