Пример #1
0
        public void Update(Order entity)
        {
            if (entity.Id.Equals(Guid.Empty))
                throw new ArgumentException("Order Id cannot be empty!");

            if (entity.BillNo <= 0)
                throw new ArgumentException("Order No cannot be empty!");

            if (entity.CustomerId == Guid.Empty)
                throw new ArgumentException("Order Customer cannot be empty!");

            if (entity.EmployeeId == Guid.Empty)
                throw new ArgumentException("Order Employee cannot be empty!");

            if (CheckDuplicateBillNo(entity.Id, entity.BillNo))
                throw new ArgumentException("Duplicate Order No found!");

            using (OpenPOSDbEntities ctx = new OpenPOSDbEntities())
            {
                try
                {
                    using (var scope = new TransactionScope(TransactionScopeOption.Required))
                    {
                        ctx.ExecuteStoreCommand("DELETE FROM OrderDetails WHERE OrderId = {0};", entity.Id);
                        ctx.ExecuteStoreCommand("DELETE FROM Payments WHERE OrderId = {0};", entity.Id);

                        foreach (var od in entity.OrderDetails)
                        {
                            ctx.AttachTo("OrderDetails", od);
                            ctx.ObjectStateManager.ChangeObjectState(od, System.Data.EntityState.Added);
                        }

                        foreach (var payment in entity.Payments)
                        {
                            ctx.AttachTo("Payments", payment);
                            ctx.ObjectStateManager.ChangeObjectState(payment, System.Data.EntityState.Added);
                        }

                        ctx.AttachTo("Orders", entity);
                        ctx.ObjectStateManager.ChangeObjectState(entity, System.Data.EntityState.Modified);

                        ctx.SaveChanges();

                        scope.Complete();
                    }
                }
                catch (Exception ex)
                {
                    LogService.Error("Error while updating order", ex);
                    throw new ArgumentException("Error while updating order!", ex);
                }
            }
        }
Пример #2
0
        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);
            }
        }