Ejemplo n.º 1
0
        public bool UpdateCustomerPriceList(CustomerPriceList updateCustomerPriceList, bool newVer = false)
        {
            try
            {
                if (updateCustomerPriceList == null)
                {
                    throw new ArgumentNullException("updateCustomerPriceList");
                }

                using (var db = new HKSupplyContext())
                {
                    using (var dbTrans = db.Database.BeginTransaction())
                    {
                        try
                        {
                            updateCustomerPriceList.IdSubVer += 1;
                            if (newVer == true)
                            {
                                updateCustomerPriceList.IdVer   += 1;
                                updateCustomerPriceList.IdSubVer = 0;
                            }
                            updateCustomerPriceList.Timestamp = DateTime.Now;

                            CustomerPriceListHistory customerPriceListHistory = (CustomerPriceListHistory)updateCustomerPriceList;
                            customerPriceListHistory.User = GlobalSetting.LoggedUser.UserLogin;

                            //Con esto marcaremos todo el objeto como modificado y actualizará todos los campos.
                            //En este caso nos interesa porque la mayoría de los campos de supplier se pueden modificar
                            db.Entry(updateCustomerPriceList).State = EntityState.Modified;

                            db.CustomersPriceListHistory.Add(customerPriceListHistory);
                            db.SaveChanges();

                            dbTrans.Commit();
                            return(true);
                        }
                        catch (SqlException sqlex)
                        {
                            dbTrans.Rollback();

                            for (int i = 0; i < sqlex.Errors.Count; i++)
                            {
                                _log.Error("Index #" + i + "\n" +
                                           "Message: " + sqlex.Errors[i].Message + "\n" +
                                           "Error Number: " + sqlex.Errors[i].Number + "\n" +
                                           "LineNumber: " + sqlex.Errors[i].LineNumber + "\n" +
                                           "Source: " + sqlex.Errors[i].Source + "\n" +
                                           "Procedure: " + sqlex.Errors[i].Procedure + "\n");

                                switch (sqlex.Errors[i].Number)
                                {
                                case -1:     //connection broken
                                case -2:     //timeout
                                    throw new DBServerConnectionException(GlobalSetting.ResManager.GetString("DBServerConnectionError"));
                                }
                            }
                            throw sqlex;
                        }
                        catch (Exception ex)
                        {
                            dbTrans.Rollback();
                            _log.Error(ex.Message, ex);
                            throw ex;
                        }
                    }
                }
            }
            catch (ArgumentNullException anex)
            {
                _log.Error(anex.Message, anex);
                throw anex;
            }
        }
Ejemplo n.º 2
0
        public bool NewCustomerPriceList(CustomerPriceList newCustomerPriceList)
        {
            try
            {
                if (newCustomerPriceList == null)
                {
                    throw new ArgumentNullException("newSupplierPriceList");
                }

                using (var db = new HKSupplyContext())
                {
                    using (var dbTrans = db.Database.BeginTransaction())
                    {
                        try
                        {
                            var tmpCustomerPriceList = GetCustomerPriceList(newCustomerPriceList.IdItemBcn, newCustomerPriceList.IdCustomer);
                            if (tmpCustomerPriceList != null)
                            {
                                throw new Exception("Customer Price List already exist");
                            }

                            newCustomerPriceList.IdVer     = 1;
                            newCustomerPriceList.IdSubVer  = 0;
                            newCustomerPriceList.Timestamp = DateTime.Now;

                            CustomerPriceListHistory customerPriceListHistory = (CustomerPriceListHistory)newCustomerPriceList;
                            customerPriceListHistory.User = GlobalSetting.LoggedUser.UserLogin;

                            db.CustomersPriceList.Add(newCustomerPriceList);
                            db.CustomersPriceListHistory.Add(customerPriceListHistory);
                            db.SaveChanges();

                            dbTrans.Commit();
                            return(true);
                        }
                        catch (SqlException sqlex)
                        {
                            dbTrans.Rollback();

                            for (int i = 0; i < sqlex.Errors.Count; i++)
                            {
                                _log.Error("Index #" + i + "\n" +
                                           "Message: " + sqlex.Errors[i].Message + "\n" +
                                           "Error Number: " + sqlex.Errors[i].Number + "\n" +
                                           "LineNumber: " + sqlex.Errors[i].LineNumber + "\n" +
                                           "Source: " + sqlex.Errors[i].Source + "\n" +
                                           "Procedure: " + sqlex.Errors[i].Procedure + "\n");

                                switch (sqlex.Errors[i].Number)
                                {
                                case -1:     //connection broken
                                case -2:     //timeout
                                    throw new DBServerConnectionException(GlobalSetting.ResManager.GetString("DBServerConnectionError"));
                                }
                            }
                            throw sqlex;
                        }
                        catch (Exception ex)
                        {
                            dbTrans.Rollback();
                            _log.Error(ex.Message, ex);
                            throw ex;
                        }
                    }
                }
            }
            catch (ArgumentNullException anex)
            {
                _log.Error(anex.Message, anex);
                throw anex;
            }
        }