Пример #1
0
        public bool UpdateSupplierPriceList(SupplierPriceList updateSupplierPriceList, bool newVer = false)
        {
            try
            {
                if (updateSupplierPriceList == null)
                {
                    throw new ArgumentNullException("updateSupplierPriceList");
                }

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

                            SupplierPriceListHistory supplierPriceListHistory = (SupplierPriceListHistory)updateSupplierPriceList;
                            supplierPriceListHistory.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(updateSupplierPriceList).State = EntityState.Modified;

                            db.SuppliersPriceListHistory.Add(supplierPriceListHistory);
                            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;
            }
        }
Пример #2
0
        public bool NewSupplierPriceList(SupplierPriceList newSupplierPriceList)
        {
            try
            {
                if (newSupplierPriceList == null)
                {
                    throw new ArgumentNullException("newSupplierPriceList");
                }

                using (var db = new HKSupplyContext())
                {
                    using (var dbTrans = db.Database.BeginTransaction())
                    {
                        try
                        {
                            var tmpSupplierPriceList = GetSupplierPriceList(newSupplierPriceList.IdItemBcn, newSupplierPriceList.IdSupplier);
                            if (tmpSupplierPriceList != null)
                            {
                                throw new Exception("Supplier Price List already exist");
                            }

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

                            SupplierPriceListHistory supplierPriceListHistory = (SupplierPriceListHistory)newSupplierPriceList;
                            supplierPriceListHistory.User = GlobalSetting.LoggedUser.UserLogin;

                            db.SuppliersPriceList.Add(newSupplierPriceList);
                            db.SuppliersPriceListHistory.Add(supplierPriceListHistory);
                            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;
            }
        }