Exemple #1
0
        public void SaveSale(SaleModel saleInfo, string cashierId)
        {
            //TODO: make this solid/dry
            //start filling in models to be saved to the database
            var details  = new List <SaleDetailDBModel>();
            var 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($"The product Id of {item.ProductId} could net 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
            var 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, "RRMData");

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

            //finish filling it the sale model
            foreach (var item in details)
            {
                item.SaleId = sale.Id;

                //save the sale detail models
                sql.SaveData("dbo.spSaleDetail_Insert", item, "RRMData");
            }
        }
        public void SaveSale(SaleModel saleInfo, string cashierId)
        {
            // TODO: Make this SOLID/DRY/BETTER!
            // Start filling in the models we will save to the DB
            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 == null)
                {
                    throw new Exception($"The product Id of { detail.ProductId } could not be found.");
                }

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

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

                details.Add(detail);
            }

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

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

            // Save the SaleModel
            SqlDataAccess sql = new SqlDataAccess();

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

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

            // Finish 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, "RRMData");
            }
        }