public Guid Add(Purchase entity) { if (string.IsNullOrEmpty(entity.BillNo)) throw new ArgumentException("Purchase No cannot be empty!"); if (entity.SupplierId == Guid.Empty) throw new ArgumentException("Purchase supplier cannot be empty!"); if (entity.EmployeeId == Guid.Empty) throw new ArgumentException("Purchase Employee cannot be empty!"); if (CheckDuplicateBillNo(entity.Id, entity.BillNo)) throw new ArgumentException("Duplicate Purchase No found!"); using (OpenPOSDbEntities ctx = new OpenPOSDbEntities()) { try { ctx.Purchases.MergeOption = MergeOption.NoTracking; ctx.Purchases.AddObject(entity); ctx.SaveChanges(); var id = entity.Id; ctx.Detach(entity); return id; } catch (Exception ex) { LogService.Error("Error while adding Purchase", ex); throw new ArgumentException("Error while adding new Purchase!", ex); } } }
public void Update(Purchase entity) { if (entity == null) throw new ArgumentNullException("entity"); if (string.IsNullOrEmpty(entity.BillNo)) throw new ArgumentException("Purchase No cannot be empty!"); if (entity.SupplierId == Guid.Empty) throw new ArgumentException("Purchase supplier cannot be empty!"); if (entity.EmployeeId == Guid.Empty) throw new ArgumentException("Purchase Employee cannot be empty!"); if (CheckDuplicateBillNo(entity.Id, entity.BillNo)) throw new ArgumentException("Duplicate Purchase No found!"); try { using (OpenPOSDbEntities ctx = new OpenPOSDbEntities()) { using (var scope = new TransactionScope(TransactionScopeOption.Required)) { ctx.ExecuteStoreCommand("DELETE FROM PurchaseDetails WHERE PurchaseId = {0};", entity.Id); ctx.ExecuteStoreCommand("DELETE FROM PurchasePayments WHERE PurchaseId = {0};", entity.Id); foreach (var od in entity.PurchaseDetails) { ctx.AttachTo("PurchaseDetails", od); ctx.ObjectStateManager.ChangeObjectState(od, System.Data.EntityState.Added); } foreach (var payment in entity.PurchasePayments) { ctx.AttachTo("PurchasePayments", payment); ctx.ObjectStateManager.ChangeObjectState(payment, System.Data.EntityState.Added); } ctx.AttachTo("Purchases", entity); ctx.ObjectStateManager.ChangeObjectState(entity, System.Data.EntityState.Modified); ctx.SaveChanges(); scope.Complete(); } } } catch (Exception ex) { LogService.Error("Error while updating Purchase", ex); throw new ArgumentException("Error while updating Purchase", ex); } }
/// <summary> /// Deprecated Method for adding a new object to the Purchases EntitySet. Consider using the .Add method of the associated ObjectSet<T> property instead. /// </summary> public void AddToPurchases(Purchase purchase) { base.AddObject("Purchases", purchase); }
/// <summary> /// Create a new Purchase object. /// </summary> /// <param name="id">Initial value of the Id property.</param> /// <param name="billNo">Initial value of the BillNo property.</param> /// <param name="totalAmount">Initial value of the TotalAmount property.</param> /// <param name="purchaseDate">Initial value of the PurchaseDate property.</param> /// <param name="status">Initial value of the Status property.</param> /// <param name="supplierId">Initial value of the SupplierId property.</param> /// <param name="employeeId">Initial value of the EmployeeId property.</param> /// <param name="systemId">Initial value of the SystemId property.</param> public static Purchase CreatePurchase(global::System.Guid id, global::System.String billNo, global::System.Double totalAmount, global::System.DateTime purchaseDate, global::System.Boolean status, global::System.Guid supplierId, global::System.Guid employeeId, global::System.String systemId) { Purchase purchase = new Purchase(); purchase.Id = id; purchase.BillNo = billNo; purchase.TotalAmount = totalAmount; purchase.PurchaseDate = purchaseDate; purchase.Status = status; purchase.SupplierId = supplierId; purchase.EmployeeId = employeeId; purchase.SystemId = systemId; return purchase; }