示例#1
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;
            }
        }
        public void SaveSale(SaleModel saleInfo, string cashierId)
        {
            //todo : make this respect solid principles // Dry
            //start filling in the sale detail models we will save to the database
            var details           = new List <SaleDetailDbModel>();
            var productDataAccess = new ProductDataAccess();
            var taxRate           = ConfigHelper.GetTaxRate();

            saleInfo.SaleDetails.ForEach(saleDetail =>
            {
                var detail = new SaleDetailDbModel
                {
                    ProductId = saleDetail.ProductId,
                    Quantity  = saleDetail.Quantity
                };

                //Get information about the current procduct
                var product = productDataAccess.GetProductById(detail.ProductId);
                if (product == null)
                {
                    throw new Exception($"the product id of {detail.ProductId} could not be found");
                }

                detail.PurchasePrice = product.RetailPrice * detail.Quantity;
                detail.Tax           = product.IsTaxable ? detail.PurchasePrice * (taxRate / 100) : 0;

                details.Add(detail);
            });
            //create sale model
            var sale = new SaleDbModel
            {
                SubTotal  = details.Sum(detail => detail.PurchasePrice),
                Tax       = details.Sum(detail => detail.Tax),
                CashierId = cashierId
            };

            sale.Total = sale.SubTotal + sale.Tax;
            using (SqlDataAccess sql = new SqlDataAccess())
            {
                try
                {
                    sql.StartTransaction("RRMData");
                    //save the sale model
                    //get the id from the sale model
                    var saleId = sql.SaveDataInTransaction <int, SaleDbModel>("[dbo].[spInsertSale]", sale);
                    // finish filling sale details models with id
                    foreach (var detail in details)
                    {
                        detail.SaleId = saleId;
                        //save the sale detail models
                        sql.SaveDataInTransaction <int, SaleDetailDbModel>("[dbo].[spInsertSaleDetail]", detail);
                    }
                }
                catch
                {
                    sql.RollbackTransaction();
                    throw;
                }
            }
        }
        public void SaveSale(SaleModel sale, string cashierId)
        {
            ProductData productData          = new ProductData();
            List <SaleDetailDbModel> details = new List <SaleDetailDbModel>();

            //Create a model for insertion in DB for each sale detail model that was posted to the api
            foreach (SaleDetailModel item in sale.saleDetails)
            {
                ProductModel productInfo = productData.GetProductById(item.Id);
                if (productInfo == null)
                {
                    throw new System.Exception($"The product with id {item.Id} could not be found in database");
                }
                decimal tax           = 0;
                decimal purchasePrice = productInfo.RetailPrice * item.Quantity;
                if (productInfo.IsTaxable)
                {
                    tax = purchasePrice * Convert.ToDecimal(ConfigHelper.GetTaxRate());
                }
                details.Add(new SaleDetailDbModel
                {
                    ProductId     = item.Id,
                    Quantity      = item.Quantity,
                    PurchasePrice = purchasePrice,
                    Tax           = tax,
                });
            }
            //create the sale model
            SaleDbModel saleDbModel = new SaleDbModel
            {
                SubTotal  = details.Sum(x => x.PurchasePrice),
                Tax       = details.Sum(x => x.Tax),
                CashierId = cashierId,
            };

            saleDbModel.Total = saleDbModel.SubTotal + saleDbModel.Tax;
            using (SqlDataAccess dataAccess = new SqlDataAccess())
            {
                try
                {
                    dataAccess.StartTransaction("TRMData");
                    dataAccess.SaveDataInTransaction("dbo.spSale_Insert", saleDbModel);

                    saleDbModel.Id = dataAccess.LoadDataInTransaction <int, dynamic>("dbo.spLookUp", new { CashierId = cashierId, saleDbModel.SaleDate }).FirstOrDefault();
                    //Finish filling in the sale detail models
                    foreach (SaleDetailDbModel item in details)
                    {
                        item.SaleId = saleDbModel.Id;
                        dataAccess.SaveDataInTransaction("dbo.spSaleDetail_Insert", item);
                    }
                    ;
                    dataAccess.CommitTransaction();
                }
                catch (Exception e)
                {
                    dataAccess.RollBackTransaction();
                    throw;
                }
            }
        }
示例#4
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;
            }
        }
示例#5
0
        public void SaveSale(SaleModel saleInfo, string cashierId)
        {
            List <SaleDetailDBModel> details = new List <SaleDetailDBModel>();
            ProductData products             = new ProductData();
            var         taxRate = ConfigHelper.GetTaxRate() / 100;

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

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

                if (productInfo == null)
                {
                    throw new Exception("No product in DB");
                }

                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;


            SqlDataAccess sql = new SqlDataAccess();

            sql.SaveData("dbo.spSale_Insert", sale, "RSTData");


            sale.Id = sql.LoadData <int, dynamic>("spSale_Lookup", new { CashierId = sale.CashierId, SaleDate = sale.SaleDate }, "RSTData").FirstOrDefault();
            foreach (var item in details)
            {
                item.SaleId = sale.Id;
                sql.SaveData("dbo.spSaleDetail_Insert", item, "RSTData");
            }
        }
示例#6
0
        public void SaveSale(SaleModel saleInfo, string cashierId)
        {
            var details  = new List <SaleDetailDbModel>();
            var products = new ProductData();
            var taxRate  = ConfigHelper.TaxRate;

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

                var productInfo = products.GetById(detail.ProductId);

                if (productInfo == null)
                {
                    throw new Exception($"Product with id of {detail.ProductId} was not found in DB !");
                }

                detail.PurchasePrice = productInfo.RetailPrice * detail.Quantity;
                detail.Tax           = productInfo.IsTaxable ? detail.PurchasePrice * taxRate : 0;

                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;

            var sql = new SqlDataAccess();

            sql.SaveData("dbo.spSale_Insert", sale, "RM.Database");

            sale.Id = sql.LoadData <int, dynamic>("dbo.spSale_Lookup", new { sale.CashierId, sale.SaleDate }, "RM.Database").Single();

            foreach (var item in details)
            {
                item.SaleId = sale.Id;
                sql.SaveData("dbo.spSaleDetail_Insert", item, "RM.Database");
            }
        }
示例#7
0
        public void SaveSale(SaleModel saleInfo, string cashierId)
        {
            var saleDetails = new List <SaleDetailDbModel>();

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

                var productInfo = _productData.GetProductById(detail.ProductId);
                detail.PurchasePrice = productInfo.RetailPrice * detail.Quantity;
                detail.Tax           = productInfo.IsTaxable ? detail.PurchasePrice * ConfigHelper.GetTaxRate() : detail.Tax;

                saleDetails.Add(detail);
            }

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

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


            try
            {
                _sql.StartTransaction("DKRData");
                int saleId = _sql.SaveDataScalarInTransaction("dbo.spSale_Insert", sale);

                saleDetails.ForEach(item =>
                {
                    item.SaleId = saleId;
                    _sql.SaveDataInTransaction("dbo.spSaleDetail_Insert", item);
                });
            }
            catch
            {
                _sql.RollBackTransaction();
                throw;
            }
        }
示例#8
0
        public void SaveSale(SaleModel saleInfo, string cashierId)
        {
            //TODO: Make this SOLID/DRY/Better

            // Start filling in sale detail models we will save to database
            List <SaleDetailDbModel> details = new List <SaleDetailDbModel>();

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

                // Get information 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;
                detail.Tax           = productInfo.RetailPrice * (productInfo.TaxRate / 100);

                details.Add(detail);
            }

            // Create sale model
            SaleDbModel sale = new SaleDbModel
            {
                CashierId = cashierId,
                SubTotal  = details.Sum(x => x.PurchasePrice),
                Tax       = details.Sum(x => x.Tax)
            };

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

            try
            {
                _sql.StartTransaction("TRMData");

                // save sale model
                _sql.SaveDataInTransaction <SaleDbModel>("dbo.spSale_Insert", sale);

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

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

                _sql.CommitTransaction();
            }
            catch
            {
                _sql.RollbackTransaction();
                throw;
            }
        }
示例#9
0
        public void SaveSale(SaleModel saleInfo, string userId)
        {
            // TODO: Make this SOLID/DRY/Better
            List <SaleDetailDbModel> saleDetails = new List <SaleDetailDbModel>();
            ProductData product = new ProductData();
            var         taxRate = ConfigHelper.GetTaxRate();

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

                //TODO: Additional SaleDetail information
                var productInfo = product.GetProductById(detail.ProductId);

                if (productInfo == null)
                {
                    throw new Exception($"The 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) / 100;
                }

                saleDetails.Add(detail);
            }


            //SaleDbModel to save to database
            SaleDbModel sale = new SaleDbModel
            {
                SubTotal  = saleDetails.Sum(x => x.PurchasePrice),
                Tax       = saleDetails.Sum(x => x.Tax),
                CashierId = userId,
            };

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


            //Save Sales record
            SqlDataAcces sql = new SqlDataAcces();

            sql.SaveData("dbo.spSales_Insert", sale, "RMDatabase");


            //Get the ID from Sales table
            sale.Id = sql.LoadData <int, dynamic>("dbo.spSales_Latest",
                                                  new { CashierId = sale.CashierId, SaleDate = sale.SaleDate },
                                                  "RMDatabase").FirstOrDefault();


            //Finish filling Sales database model for insertion
            foreach (var item in saleDetails)
            {
                item.SaleId = sale.Id;

                //Save SaleDetails records.
                //If SaveData is being called too much (eg: 1000 calls/time), you can create table parameter and pass all values in 1 table.
                sql.SaveData("dbo.spSaleDetails_Insert", item, "RMDatabase");
            }
        }
示例#10
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(item.ProductId);

                if (productInfo == null)
                {
                    throw new System.Exception($"El producto con Id {item.ProductId} no se encuentra en la base de datos.");
                }

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

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

                details.Add(detail);
            }

            SaleDbModel sale = new SaleDbModel
            {
                Subtotal  = details.Sum(d => d.PurchasePrice),
                Tax       = details.Sum(d => d.Tax),
                CashierId = cashierId
            };

            sale.Total = sale.Subtotal + sale.Tax;


            try
            {
                _sql.StartTransaction("VPMData");
                _sql.SaveDataInTransaction("dbo.spSale_Insert", sale);

                var p = new { sale.CashierId, sale.SaleDate };

                sale.Id = _sql.LoadDataInTransaction <int, dynamic>("dbo.spSale_Lookup", p).FirstOrDefault();

                foreach (var item in details)
                {
                    item.SaleId = sale.Id;

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

                _sql.CommitTransaction();
            }
            catch
            {
                _sql.RollbackTransaction();
                throw;
            }
        }
示例#11
0
        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 database
            List <SaleDetailDbModel> details = new List <SaleDetailDbModel>();
            ProductData products             = new ProductData();
            var         taxRate = ConfigHelper.GetTaxRate() / 100;

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

                // Get the information about this product
                var productInfo = products.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);
            }

            // 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;

            // Save the sale model
            SqlDataAccess sql = new SqlDataAccess();

            sql.SaveData("dbo.spSale_Insert", sale, "RMData");

            // Get the ID from the sale mode
            sale.Id = sql.LoadData <int, dynamic>("spSale_Lookup", new { sale.CashierId, sale.SaleDate }, "RMData")
                      .FirstOrDefault();

            // Finsh filling in the sale detail models
            foreach (var item in details)
            {
                item.SaleId = sale.Id;
                // Save the sale detail models
                sql.SaveData("dbo.spSaleDetail_Insert", item, "RMData");
            }
        }
        public void SaveSale(SaleModel saleInfo, string cashierId)
        {
            //TODO: Make this solid/dry/better

            // Start filling in the models we will save to the database
            List <SaleDetailDBModel> details = new List <SaleDetailDBModel>();
            ProductData products             = new ProductData(_config);
            var         taxRate = ConfigHelper.getTaxRate() / 100;

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

                var productInfo = products.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);
            }

            // 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;

            // save the sale model

            using (SqlDataAccess sql = new SqlDataAccess(_config))
            {
                try
                {
                    sql.StartTransaction("TRMData");
                    sql.SaveDataInTransaction <SaleDbModel>("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;

                        // save the sale detail modesl
                        sql.SaveDataInTransaction("dbo.spSaleDetail_Insert", item);
                    }

                    sql.CommitTransaction();
                }
                catch
                {
                    sql.RollbackTransaction();
                    throw;
                }
            }
        }
        public void SaveSale(SaleModel saleInfo, string userId)
        {
            // TODO: Make this SOLID/DRY/Btter
            // Start filling in the sale detail models we will save to the database
            List <SaleDetailDbModel> details = new List <SaleDetailDbModel>();
            ProductData product = new ProductData(config);
            var         taxRate = ConfigHelper.GetTaxRate() / 100;

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

                // Get the information about this product
                var productInfo = product.GetProductById(detail.ProductId);

                if (productInfo == null)
                {
                    throw new Exception($"El producto con Id { detail.ProductId } no se encuentra en la base de datos.");
                }

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

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

                details.Add(detail);
            }

            // Create the sale model
            SaleDbModel sale = new SaleDbModel
            {
                SubTotal  = details.Sum(x => x.PurchasePrice),
                Tax       = details.Sum(x => x.Tax),
                CashierId = userId
            };

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

            using (SqlDataAccess sql = new SqlDataAccess(config))
            {
                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>("dbo.spSale_Lookup", new
                                                                       { CashierId = sale.CashierId, SaleDate = sale.SaleDate }).FirstOrDefault();

                    // Finish filling in the sale details model
                    foreach (var item in details)
                    {
                        item.SaleId = sale.Id;
                        // Save the sale detail model
                        sql.SaveDataInTransaction("dbo.spSaleDetail_Insert", item);
                    }

                    sql.CommitTransaction();
                }
                catch
                {
                    sql.RollBackTransaction();
                    throw;
                }
            }
        }
示例#14
0
        public void SaveSale(SaleModel saleInfo, string userId)
        {
            // TODO:: Make this SOLID/DRY/BETTER

            List <SaleDetailDBModel> details = new List <SaleDetailDBModel>();
            ProductData product = new ProductData();
            var         taxRate = ConfigHelper.GetTaxRate() / 100;

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

                //Get information about this product
                var productInfo = product.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);
            }

            // Create the Sale model

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

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

            using (SqlDataAccess sql = new SqlDataAccess())
            {
                try
                {
                    sql.StartTransaction("RMData");

                    // Save the sale model
                    sql.SaveDataInTransaction("dbo.spSale_Insert", sale);

                    // Get the ID from the sale mode
                    sale.Id = sql.LoadDataInTransaction <int, dynamic>(
                        "dbo.spSale_Lookup",
                        new
                    {
                        sale.CashierId,
                        sale.SaleDate
                    }).FirstOrDefault();

                    // Finish filling int the sale detail models
                    foreach (var item in details)
                    {
                        item.SaleId = sale.Id;

                        // Save the sale detail model
                        sql.SaveDataInTransaction("dbo.spSaleDetail_Insert", item);
                    }

                    sql.CommitTransaction();
                }
                catch (Exception)
                {
                    sql.RollBackTransaction();
                    throw;
                }
            }
        }
示例#15
0
        public async Task <int> CreateSaleAsync(string userId, SaleModel saleInfo)
        {
            decimal taxRate     = 0.0875M;//new ConfigHelper().GetTaxRate();
            var     saleDetails = new List <SaleDetailDbModel>();

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

                var productInfo = await _productRepository.GetProductByIdAsync(item.ProductId);

                if (productInfo == null)
                {
                    throw new Exception($"The product Id {item.ProductId} could not be found");
                }

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

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

                saleDetails.Add(detail);
            }

            var subtotal = saleDetails.Sum(d => d.PurchasePrice);
            var tax      = saleDetails.Sum(d => d.Tax);
            var total    = subtotal + tax;

            var saleModel = new SaleDbModel()
            {
                Subtotal = subtotal,
                Tax      = tax,
                Total    = total,
                UserId   = userId
            };

            var saleId = -1;

            using (var sql = new SqlDataAccess())
            {
                try
                {
                    sql.StartTransaction(_connectionStringName);
                    saleId = await sql.ExecuteWithOutputInTransactionAsync(
                        $@"
                        INSERT INTO [dbo].[Sale](UserId, Subtotal, Tax, Total)
                        OUTPUT INSERTED.[Id]
                        VALUES(
                            @{nameof(saleModel.UserId)}, 
                            @{nameof(saleModel.Subtotal)}, 
                            @{nameof(saleModel.Tax)}, 
                            @{nameof(saleModel.Total)})
                    ",
                        new
                    {
                        saleModel.UserId,
                        saleModel.Subtotal,
                        saleModel.Tax,
                        saleModel.Total
                    });

                    foreach (var detail in saleDetails)
                    {
                        detail.SaleId = saleId;
                        // TODO: Single Call? TVP
                        await sql.ExecuteInTransactionAsync(
                            $@"
                            INSERT INTO [dbo].[SaleDetail](SaleId, ProductId, Quantity, PurchasePrice, Tax)
                            VALUES(
                                @{nameof(detail.SaleId)}, 
                                @{nameof(detail.ProductId)}, 
                                @{nameof(detail.Quantity)}, 
                                @{nameof(detail.PurchasePrice)}, 
                                @{nameof(detail.Tax)})
                        ",
                            new
                        {
                            detail.SaleId,
                            detail.ProductId,
                            detail.Quantity,
                            detail.PurchasePrice,
                            detail.Tax
                        });
                    }

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

            return(saleId);
        }
        public void SaveSale(SaleModel saleInfo, string cashierId)
        {
            // TODO: make this SOLID / DRY / better

            // Start filling in the saleInfo detail models we will persist
            var     details  = new List <SaleDetailDbModel>();
            var     products = new ProductData();
            decimal taxRate  = (decimal)ConfigHelper.GetTaxRate() / 100;

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

                // Get product information
                var productInfo = products.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);
            }

            // Create the SaleModel
            SaleDbModel sale = new SaleDbModel
            {
                SubTotal  = details.Sum(x => x.PurchasePrice),
                TaxTotal  = details.Sum(x => x.Tax),
                CashierId = cashierId
            };

            sale.Total = sale.SubTotal + sale.TaxTotal;

            // Save the SaleModel
            using (var sql = new SqlDataAccess())
            {
                try
                {
                    sql.StartTransaction("ARMData");

                    sql.SaveDataInTransaction <SaleDbModel>("dbo.spSale_Insert", sale);

                    // Get the id for the new sale
                    sale.Id = sql
                              .LoadDataInTransaction <int, dynamic>("dbo.spSale_Lookup", new { sale.CashierId, sale.SaleDate })
                              .FirstOrDefault();

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

                    sql.CommitTransaction();
                }
                catch
                {
                    // TODO: notify the user
                    sql.RollbackTransaction();
                    throw;
                }
            }
        }