Exemple #1
0
        public void SaveContact(PersonDbModel person, PersonDetailModel personModel)
        {
            try
            {
                _sql.StartTransaction();

                var Id = _sql.SaveDataInTransaction("dbo.spPerson_Insert", person);

                person.Id = Convert.ToInt32(Id);

                foreach (var item in personModel.Phones)
                {
                    item.PersonID = person.Id;

                    _sql.SaveDataInTransaction("dbo.spPhone_Insert", item);
                }

                foreach (var item in personModel.Addresses)
                {
                    item.PersonID = person.Id;

                    _sql.SaveDataInTransaction("dbo.spAddress_Insert", item);
                }

                _sql.CommitTransaction();
            }
            catch
            {
                _sql.RollbackTransaction();
                throw;
            }
        }
Exemple #2
0
        public void Insert(List <AccountingJournalModel> journal)
        {
            try
            {
                _sql.StartTransaction("AccountingConnStr");

                _sql.SaveDataInTransaction("dbo.spAccountingJournal_Insert", journal);

                _sql.CommitTransaction();
            }
            catch (System.Exception)
            {
                _sql.RollBackTransaction();
                throw;
            }
        }
Exemple #3
0
        public void SaveSale(SaleModel saleInfo, string cashierId)
        {
            //make this solid/dryC:\Users\computer\OneDrive\Desktop\Projects\RetailManager\RMDataManager.Library\DataAccess\SaleData.cs
            var saleDetails = new List <SaleDetailDBModel>();
            var taxRate     = ConfigHelper.GetTaxRate() / 100;

            foreach (var item in saleInfo.SaleDetails)
            {
                var detail = new SaleDetailDBModel
                {
                    ProductId = item.ProductId,
                    Quantity  = item.Quantity
                };

                var productInfo = productData.GetProductById(item.ProductId);

                if (productInfo is null)
                {
                    throw new ArgumentNullException($"The Product id of { detail.ProductId } could not be found in the database");
                }
                detail.PurchasePrice = (productInfo.RetailPrice * detail.Quantity);

                if (productInfo.IsTaxable)
                {
                    detail.Tax = (detail.PurchasePrice * taxRate);
                }

                saleDetails.Add(detail);
            }

            var sale = new SaleDbModel
            {
                CashierId = cashierId,
                SubTotal  = saleDetails.Sum(x => x.PurchasePrice),
                Tax       = saleDetails.Sum(x => x.Tax),
            };

            sale.Total = sale.SubTotal + sale.Tax;

            try
            {
                sql.StartTransaction("RMData");
                //sale mode
                sql.SaveDataInTransaction("dbo.spSaleAdd", sale);
                sale.Id = sql.LoadDataInTransaction <int, dynamic>("dbo.spSaleSelect", new { sale.CashierId, sale.SaleDate })
                          .FirstOrDefault();
                foreach (var item in saleDetails)
                {
                    item.SaleId = sale.Id;
                    //sale details
                    sql.SaveDataInTransaction("dbo.spSaleDetailAdd", item);
                }
                sql.CommitTransaction();
            }
            catch
            {
                sql.RollbackTransaction();
                throw;
            }
        }
Exemple #4
0
        public void SaveSale(SaleModel saleInfo, string cashierId)
        {
            List <SaleDetailDBModel> details = new List <SaleDetailDBModel>();
            var taxRate = ConfigHelper.GetTaxRate() / 100;

            foreach (var item in saleInfo.SaleDetails)
            {
                var detail = new SaleDetailDBModel
                {
                    ProductId = item.ProductId,
                    Quantity  = item.Quantity
                };

                var productInfo = _productData.GetProductById(detail.ProductId);
                if (productInfo == null)
                {
                    throw new Exception($"The product Id of {detail.ProductId} could not be found in the database.");
                }

                detail.PurchasePrice = productInfo.RetailPrice * detail.Quantity;

                if (productInfo.IsTaxable)
                {
                    detail.Tax = detail.PurchasePrice * taxRate;
                }

                details.Add(detail);
            }

            SaleDBModel sale = new SaleDBModel
            {
                SubTotal  = details.Sum(x => x.PurchasePrice),
                Tax       = details.Sum(x => x.Tax),
                CashierId = cashierId
            };

            sale.Total = sale.SubTotal + sale.Tax;
            try
            {
                _sql.StartTransaction("KRMData");

                _sql.SaveDataInTransaction("dbo.spSale_Insert", sale);

                sale.Id = _sql.LoadDataInTransaction <int, dynamic>("dbo.spSale_Lookup", new { sale.CashierId, sale.SaleDate }).FirstOrDefault();

                details.ForEach(item =>
                {
                    item.SaleId = sale.Id;
                    _sql.SaveDataInTransaction("dbo.spSaleDetail_Insert", item);
                });

                _sql.CommitTransaction();
            }
            catch
            {
                _sql.RollbackTransaction();
                throw;
            }
        }
Exemple #5
0
        public void SaveSale(SaleModel saleInfo, string cashierId)
        {
            List <SaleDetailsDBModel> details = new List <SaleDetailsDBModel>();

            decimal taxRate = GetTaxRate();

            foreach (var item in saleInfo.SaleDetails)
            {
                var saleDetails = new SaleDetailsDBModel
                {
                    ProductId = item.ProductId,
                    Quantity  = item.Quantity,
                };

                var productInfo = _prodcutData.GetProductById(item.ProductId);
                saleDetails.PurchasePrice = productInfo.RetailPrice * saleDetails.Quantity;
                if (productInfo.IsTaxable)
                {
                    saleDetails.Tax = saleDetails.PurchasePrice * taxRate;
                }

                details.Add(saleDetails);
            }

            var saleToDb = new SaleDBModel()
            {
                CashierId = cashierId,
                SubTotal  = details.Sum(x => x.PurchasePrice),
                Tax       = details.Sum(x => x.Tax)
            };

            saleToDb.Total = saleToDb.SubTotal + saleToDb.Tax;

            try
            {
                _sql.StartTransaction("RMData");
                _sql.SaveDataInTransaction("dbo.spSale_Insert", saleToDb);

                saleToDb.Id = _sql.LoadDataInTransaction <int, dynamic>("dbo.spSale_Lookup", new { saleToDb.CashierId, saleToDb.SaleDate }).FirstOrDefault();

                foreach (var item in details)
                {
                    item.SaleId = saleToDb.Id;
                    _sql.SaveDataInTransaction("dbo.spSaleDetail_Insert", item);
                }

                _sql.CommitTransaction();
            }
            catch
            {
                _sql.RollbackTransaction();
                throw;
            }
        }
Exemple #6
0
        //TODO remove biz-logic
        public void SaveSale(SaleModel saleInfo, string cashierId)
        {
            var details = new List <SaleDetailDbModel>();
            var taxRate = GetTaxRate();

            foreach (var item in saleInfo.SaleDetails)
            {
                var detail = new SaleDetailDbModel
                {
                    ProductId = item.ProductId,
                    Quantity  = item.Quantity
                };
                var productInfo = _productData.GetProductById(detail.ProductId);
                if (_productData is null)
                {
                    // TODO if always false
                    throw new Exception($"The product Id of {detail.ProductId} not found in the Db");
                }
                detail.PurchasePrice = productInfo.RetailPrice * detail.Quantity;
                if (productInfo.IsTaxable)
                {
                    detail.Tax = detail.PurchasePrice * taxRate;
                }
                details.Add(detail);
            }
            var sale = new SaleDbModel
            {
                SubTotal  = details.Sum(x => x.PurchasePrice),
                Tax       = details.Sum(x => x.Tax),
                CashierId = cashierId
            };

            sale.Total = sale.SubTotal + sale.Tax;

            try
            {
                _sql.StartTransaction("RSAData");
                _sql.SaveDataInTransaction("dbo.spSale_Insert", sale);
                sale.Id = _sql
                          .LoadDataInTransaction <int, dynamic>("spSale_Lookup", new { sale.CashierId, sale.SaleDate })
                          .FirstOrDefault();
                foreach (var item in details)
                {
                    item.SaleId = sale.Id;
                    _sql.SaveDataInTransaction("dbo.spSaleDetail_Insert", item);
                }
                _sql.CommitTransaction();
            }
            catch
            {
                _sql.RollbackTransaction();
                throw;
            }
        }
Exemple #7
0
        public void SaveSale(SaleModel sale)
        {
            try
            {
                _sql.StartTransaction("FoodStuffDATA");

                _sql.SaveDataInTransaction("dbo.spSale_Insert", sale);

                _sql.CommitTransaction();
            }
            catch
            {
                _sql.RollbackTransaction();
                throw;
            }
        }
        public static void Seed(IComboBoxData comboBoxData, ISqlDataAccess sql)
        {
            if (!comboBoxData.LoadComboBox().Item1.Any())
            {
                var phoneType      = PhoneType();
                var addressType    = AddressType();
                var contact        = Person();
                var contactPhone   = Phone();
                var contactAddress = Address();

                try
                {
                    sql.StartTransaction();

                    foreach (var item in phoneType)
                    {
                        var Id = sql.SaveDataInTransaction("dbo.spPhoneNumberType_Insert", item);

                        contactPhone.PhoneNumberTypeID = Convert.ToInt32(Id);
                    }

                    foreach (var item in addressType)
                    {
                        var Id = sql.SaveDataInTransaction("dbo.spAddressType_Insert", item);

                        contactAddress.AddressTypeID = Convert.ToInt32(Id);
                    }

                    var contactId = sql.SaveDataInTransaction("dbo.spPerson_Insert", contact);

                    contact.Id              = Convert.ToInt32(contactId);
                    contactPhone.PersonID   = contact.Id;
                    contactAddress.PersonID = contact.Id;

                    sql.SaveDataInTransaction("dbo.spPhone_Insert", contactPhone);
                    sql.SaveDataInTransaction("dbo.spAddress_Insert", contactAddress);

                    sql.CommitTransaction();
                }
                catch
                {
                    sql.RollbackTransaction();
                    throw;
                }
            }
        }
Exemple #9
0
        public void InsertOrderDetails(List <OrderDetailModel> orderDetailModels)
        {
            try
            {
                _sql.StartTransaction("TulipData");

                foreach (var orderDetailModel in orderDetailModels)
                {
                    _sql.SaveDataInTransaction("dbo.spOrderDetail_Insert", orderDetailModel);
                }

                _sql.CommitTransaction();  // important
            }
            catch
            {
                _sql.RollbackTransaction();
                throw;
            }
        }
Exemple #10
0
        public (List <PhoneTypeDbModel>, List <AddressTypeDbModel>) LoadComboBox()
        {
            try
            {
                _sql.StartTransaction();

                PhoneType = _sql.LoadDataInTransaction <PhoneTypeDbModel, dynamic>("spPhoneNumberType_GetAll",
                                                                                   new { });

                AddressType = _sql.LoadDataInTransaction <AddressTypeDbModel, dynamic>("spAddressType_GetAll",
                                                                                       new { });

                _sql.CommitTransaction();
            }
            catch
            {
                _sql.RollbackTransaction();
                throw;
            }

            return(PhoneType, AddressType);
        }
Exemple #11
0
        //public void InsertOrderDetail(OrderDetailModel orderDetail)
        //{
        //    SqlDataAccess sql = new SqlDataAccess();
        //    sql.SaveData("dbo.spOrderDetail_Insert", orderDetail, "TulipData");

        //}


        public void InsertOrderDetails(List <OrderDetailModel> orderDetailModels)
        {
            //using (SqlDataAccess sql = new SqlDataAccess())
            //{
            try
            {
                _sqlDataAccess.StartTransaction("TulipData");

                foreach (var orderDetailModel in orderDetailModels)
                {
                    _sqlDataAccess.SaveDataInTransaction("dbo.spOrderDetail_Insert", orderDetailModel);
                }

                _sqlDataAccess.CommitTransaction();  // important
            }
            catch
            {
                _sqlDataAccess.RollbackTransaction();
                throw;
            }
            //}
        }
        public void SaveSale(SaleModel saleInfo, string cashierId)
        {
            // TODO: make this SOLID/DRY/better
            // Start filling in the sale detail models we will save to the db
            List <SaleDetailDBModel> details = new List <SaleDetailDBModel>();
            var taxRate = ConfigHelper.GetTaxRate() / 100;

            foreach (var item in saleInfo.SaleDetails)
            {
                var detail = (new SaleDetailDBModel
                {
                    ProductId = item.ProductId,
                    Quantity = item.Quantity
                });

                // get the info about this product
                var productInfo = _productData.GetProductById(detail.ProductId);

                if (productInfo == null)
                {
                    throw new Exception($"The product ID of {detail.ProductId} could not be found in the database.");
                }

                detail.PurchasePrice = (productInfo.RetailPrice * detail.Quantity);

                if (productInfo.IsTaxable)
                {
                    detail.Tax = (detail.PurchasePrice * taxRate);
                }

                details.Add(detail);
            }
            // fill in the available info
            // create the sale model
            SaleDBModel sale = new SaleDBModel
            {
                SubTotal  = details.Sum(x => x.PurchasePrice),
                Tax       = details.Sum(x => x.Tax),
                CashierId = cashierId
            };

            sale.Total = sale.SubTotal + sale.Tax;

            // C# TRANSACTIONS ---- NOT SQL (use very rarely or not at all)
            // open transactions = bad for memory and more
            // prefferably SQL will handle the transaction

            try
            {
                _sql.StartTransaction("TRMData");

                // save the sale model
                _sql.SaveDataInTransaction("dbo.spSale_Insert", sale);

                // get the id from the sale model
                sale.Id = _sql.LoadDataInTransaction <int, dynamic>("spSale_Lookup", new { CashierId = sale.CashierId, SaleDate = sale.SaleDate }).FirstOrDefault();

                // finish filling in the sale detail models
                foreach (var item in details)
                {
                    item.SaleId = sale.Id;
                    // save the sale detail models
                    _sql.SaveDataInTransaction("dbo.spSaleDetail_Insert", item);
                }

                _sql.CommitTransaction();
            }
            catch
            {
                _sql.RollbackTransaction();
                throw;
            }
        }