Пример #1
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
            //Fill in the available information
            List <SaleDetail> details     = new List <SaleDetail>();
            ProductData       productData = new ProductData();
            var taxRate = ConfigHelper.GetTaxRate() / 100;

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

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

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

            sale.Total = sale.SubTotal + sale.Tax;
            User user = _db.Users.FirstOrDefault(x => x.Id == cashierId);

            sale.User = user;
            user.Sales.Add(sale);

            //Save the sale model
            _db.Entry(sale.User).State = EntityState.Modified;
            _db.SaveChanges();

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

                Product product = _db.Products.FirstOrDefault(x => x.Id == item.ProductId);
                product.QuantityInStock -= item.Quantity;
                item.Product             = product;
                product.SaleDetails.Add(item);
                _db.Entry(item.Product).State = EntityState.Modified;

                item.Sale = sale;
                sale.SaleDetails.Add(item);
                _db.Entry(item.Sale).State = EntityState.Modified;

                _db.SaveChanges();
            }
        }
Пример #2
0
        public void SaveSale(SaleModel saleInfo, string cashierId)
        {
            //TODO: Make this SOLID/ DRY/ Better
            //Start filling in the sale detail models i 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 info about this product
                var productInfo = products.GetProductById(detail.ProductId);

                if (productInfo == null)
                {
                    throw new Exception($"The prouduct 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("GRMData");
                    //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 { sale.CashierId, sale.SaleDate }).FirstOrDefault();

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

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