Esempio n. 1
0
        public void PostSale(SaleModel sale, string cashierId)
        {
            List <SaleDetailDataModel> saleDetails = new List <SaleDetailDataModel>();
            ProductData productData = new ProductData();
            var         taxRate     = ConfigHelper.GetTaxRate();

            foreach (var item in sale.SaleDetails)
            {
                var details = new SaleDetailDataModel
                {
                    ProductId = item.ProductId,
                    Quantity  = item.Quantity
                };
                var productInfo = productData.GetProductById(item.ProductId);
                if (productInfo == null)
                {
                    throw new Exception($"The product WHERE id = {item.ProductId} is null");
                }
                details.PurchasePrice = productInfo.RetailPrice * details.Quantity;
                if (productInfo.isTaxable)
                {
                    details.Tax = details.PurchasePrice * taxRate;
                }
                saleDetails.Add(details);
            }


            SaleDataModel saleData = new SaleDataModel
            {
                SubTotal  = saleDetails.Sum(x => x.PurchasePrice),
                Tax       = saleDetails.Sum(x => x.Tax),
                CashierId = cashierId,
            };

            saleData.Total = saleData.SubTotal + saleData.Tax;

            using (SqlDataAccess sql = new SqlDataAccess())
            {
                try
                {
                    sql.StartTransaction("RMData");
                    sql.SaveDataInTransaction("dbo.spSaleInsert", saleData);
                    saleData.Id = sql.LoadDataInTransaction <int, dynamic>("dbo.spSaleLookup", new { saleData.CashierId, saleData.SaleDate }, "RMData").FirstOrDefault();
                    foreach (var item in saleDetails)
                    {
                        item.SaleId = saleData.Id;
                        sql.SaveDataInTransaction("dbo.spSaleDetailInsert", item);
                    }
                }
                catch
                {
                    sql.RollBackTransaction();
                    throw;
                }
            }
        }
Esempio n. 2
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
                };

                //Get the info about product
                var productInfo = products.GetProductById(item.ProductId);

                if (productInfo == null)
                {
                    throw new System.Exception($"The product Id of {item.ProductId} is not found 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;

            using (SqlDataAccess sql = new SqlDataAccess())
            {
                sql.StartTransaction("RMData");
                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);
                }
            }
        }
Esempio n. 3
0
        public void SaveSale(SaleModel saleInfo, string cashierId)
        {
            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 info about the product
                var productInfo = product.GetProductById(detail.ProductId);

                if (productInfo == null)
                {
                    throw new Exception($"The product Id of { detail.ProductId } could nor 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;

            using (SqlDataAccess sql = new SqlDataAccess())
            {
                try
                {
                    sql.StartTransaction("TRMData");
                    // Save the sale model
                    sql.SaveDataInTransaction("dbo.spSale_Insert", sale);

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

                    // Finish filling in the 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;
                }
            }
        }
Esempio n. 4
0
        public void SaveSale(SaleModel saleInfo, string cashierId)
        {
            // ToDo: Make this SOLID/DRY/Better

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

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

            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 model
                    sale.Id = sql
                              .LoadDataInTransaction <int, dynamic>("spSale_Lookup", new { CashierId = sale.CashierId, SaleDate = sale.SaleDate })
                              .FirstOrDefault();

                    // Finish filling in the sale detail models
                    details.ForEach(item =>
                    {
                        item.SaleId = sale.Id;

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

                    sql.CommitTransaction();
                }
                catch
                {
                    sql.RollbackTransaction();
                    throw;
                }
            }
        }
Esempio n. 5
0
        public void SaveSale(SaleModel saleInfo, string cashierId)
        {
            // Todo: Make this S.O.L.I.D!!!

            // 1.) Create the models to be saved to the database and
            // fill in the available information.
            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 the products from the database
                var productInfo = products.GetProductById(item.ProductId);

                if (productInfo == null)
                {
                    throw new Exception($"The product Id of {item.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);
            }

            // 3.) 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;

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

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

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

                    // 6.) Finish filling in the SaleDetailModel and Save the sale detail models
                    foreach (var item in details)
                    {
                        item.SaleId = sale.Id;
                        // In cases where the database is going to be called thousands of times,
                        // it's possible to use "table value parameters" here instead and save the whole table.
                        sql.SaveDataInTransaction("dbo.spSaleDetail_Insert", item);
                    }

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