예제 #1
0
        public static ProductDetail ReadValues(TextBox name, TextBox unit, bool saveInDB)
        {
            string productName = (name.Text != null) ? name.Text.Trim() : String.Empty;
            string productUnit = (unit.Text != null) ? unit.Text.Trim() : String.Empty;

            bool areAllRequiredFieldsFilled = productName != String.Empty && productUnit != String.Empty;

            if (areAllRequiredFieldsFilled)
            {
                ProductDetail productDetail = new ProductDetail();
                productDetail.Name = productName;
                productDetail.Unit = productUnit;

                if (saveInDB)
                {
                    using (var db = new PastryShopDbContext())
                    {
                        db.ProductDetails.Add(productDetail);
                        db.SaveChanges();
                    }
                }

                return(productDetail);
            }

            return(null);
        }
예제 #2
0
        public static Provider ReadValues(TextBox name, bool saveInDB)
        {
            string providerName = (name.Text != null) ? name.Text.Trim() : String.Empty;

            bool areAllRequiredFieldsFilled = providerName != String.Empty;

            if (areAllRequiredFieldsFilled)
            {
                Provider provider = new Provider();
                provider.Name = providerName;

                if (saveInDB)
                {
                    using (var db = new PastryShopDbContext())
                    {
                        db.Providers.Add(provider);
                        db.SaveChanges();
                    }
                }

                return(provider);
            }

            return(null);
        }
예제 #3
0
        public static Dessert ReadValues(DataGridView gridView, TextBox tbDessetName, bool saveInDB)
        {
            if (gridView == null || gridView.RowCount - 1 <= 0)
            {
                return(null);
            }

            Dessert dessert = new Dessert();

            using (var db = new PastryShopDbContext())
            {
                string dessertName = tbDessetName.Text;
                if (dessertName == null || dessertName.Trim().Length == 0)
                {
                    return(null);
                }
                dessert.Name = dessertName.Trim();

                int rowCount = gridView.RowCount - 1;
                for (int i = 0; i < rowCount; i++)
                {
                    var productDetailId = gridView.Rows[i].Cells["Product"].Value;
                    var quantity        = gridView.Rows[i].Cells["Quantity"].Value;

                    bool areAllRequiredFieldsFilled = productDetailId != null && quantity != null;

                    if (areAllRequiredFieldsFilled)
                    {
                        RecipeLine line = new RecipeLine();
                        line.Dessert       = dessert;
                        line.Quantity      = Convert.ToDouble(quantity);
                        line.ProductDetail = db.ProductDetails.First(p => p.Id == (int)productDetailId);
                        dessert.RecipeLines.Add(line);
                    }
                    else
                    {
                        return(null);
                    }
                }
                if (saveInDB)
                {
                    db.Desserts.Add(dessert);
                    db.SaveChanges();
                }
            }
            return(dessert);
        }
예제 #4
0
        private DocumentModel CreateDocumentModel(Dictionary <int, int> idsToQuantities)
        {
            Cursor = Cursors.WaitCursor;
            DocumentModel docModel = new DocumentModel();

            docModel.IssueDate = DateTime.Now;
            //Saving OutputDocument
            OutputDocument outputDoc = new OutputDocument();

            outputDoc.IssueDate = DateTime.Now.Date;

            using (var db = new PastryShopDbContext())
            {
                using (var tr = db.Database.BeginTransaction())
                {
                    foreach (var item in idsToQuantities)
                    {
                        Dessert dessert = db.Desserts.Include("RecipeLines.ProductDetail").FirstOrDefault(d => d.Id == item.Key);

                        DessertInfo dessertInfo = new DessertInfo();
                        dessertInfo.Name     = dessert.Name;
                        dessertInfo.Quantity = item.Value;

                        //Saving OutputDocument
                        OutputDocumentLine outputDocLine = new OutputDocumentLine();
                        outputDocLine.Quantity       = item.Value;
                        outputDocLine.Dessert        = dessert;
                        outputDocLine.OutputDocument = outputDoc;

                        foreach (var recipeLine in dessert.RecipeLines)
                        {
                            var dateNow         = DateTime.Now.Date;
                            var productsInStore = db.Products
                                                  .Where(p => p.ProductDetail.Id == recipeLine.ProductDetail.Id &&
                                                         p.AvailableQuantity > 0 &&
                                                         p.ExpiryDate >= dateNow
                                                         );

                            int productsInStoreCount = productsInStore.Count();

                            double neededQuantity = item.Value * recipeLine.Quantity;
                            var    productInDbSum = productsInStore.Sum(p => (double?)(p.AvailableQuantity)) ?? 0;

                            if (productsInStoreCount == 0 || productInDbSum < neededQuantity)
                            {
                                throw new Exception("Няма достатъчно количество " + recipeLine.ProductDetail.Name + " за " + dessert.Name);
                            }


                            while (neededQuantity > 0)
                            {
                                var product = productsInStore.FirstOrDefault(p => p.AvailableQuantity > 0 && p.ExpiryDate >= dateNow);

                                // product can be null if at the time of execution
                                // the query expiry date become < DateTime.Now.Date
                                if (product == null)
                                {
                                    throw new Exception("Няма достатъчно количество " + recipeLine.ProductDetail.Name + " за " + dessert.Name);
                                }
                                Ingredient ingredient = new Ingredient();
                                ingredient.Name        = recipeLine.ProductDetail.Name;
                                ingredient.Unit        = recipeLine.ProductDetail.Unit;
                                ingredient.BatchNumber = product.BatchNumber;

                                //Saving OutputDocument
                                OutputDocLineProduct outDocLine = new OutputDocLineProduct();
                                outDocLine.OutputDocLine = outputDocLine;
                                outDocLine.Product       = product;

                                if (product.AvailableQuantity >= neededQuantity)
                                {
                                    product.AvailableQuantity    -= neededQuantity;
                                    ingredient.Quantity           = neededQuantity;
                                    outDocLine.CalcualtedQuantity = neededQuantity;
                                    neededQuantity = 0;
                                }
                                else
                                {
                                    neededQuantity           -= product.AvailableQuantity;
                                    ingredient.Quantity       = product.AvailableQuantity;
                                    product.AvailableQuantity = 0;
                                }

                                //Saving OutputDocument
                                outDocLine.CalcualtedQuantity = ingredient.Quantity;
                                outputDocLine.OutputDocLineProducts.Add(outDocLine);
                                dessertInfo.Ingredients.Add(ingredient);
                                db.SaveChanges();
                            }
                        }
                        outputDocLine.OutputDocument = outputDoc;
                        outputDoc.Lines.Add(outputDocLine);

                        docModel.Desserts.Add(dessertInfo);
                    }
                    db.OutputDocuments.Add(outputDoc);
                    db.SaveChanges();
                    tr.Commit();
                }
            }
            Cursor = Cursors.Default;

            return(docModel);
        }
        public static InputDocument ReadValues(DataGridView gridView, bool saveInDB)
        {
            if (gridView == null || gridView.RowCount - 1 <= 0)
            {
                return(null);
            }

            InputDocument inputDocument = new InputDocument();

            inputDocument.CreatedOn = DateTime.Now;

            using (var db = new PastryShopDbContext())
            {
                int rowCount = gridView.RowCount - 1;
                for (int i = 0; i < rowCount; i++)
                {
                    var receivingDate    = gridView.Rows[i].Cells["ReceivingDate"].Value;
                    var providerId       = gridView.Rows[i].Cells["Provider"].Value;
                    var productDetailId  = gridView.Rows[i].Cells["Product"].Value;
                    var receivedQuantity = gridView.Rows[i].Cells["ReceivedQuantity"].Value;
                    var batchNumber      = gridView.Rows[i].Cells["BatchNumber"].Value;
                    var expiryDate       = gridView.Rows[i].Cells["ExpiryDate"].Value;
                    var storageCondition = gridView.Rows[i].Cells["StorageCondition"].Value;
                    var vehicle          = gridView.Rows[i].Cells["Vehicle"].Value;
                    var document         = gridView.Rows[i].Cells["Document"].Value;

                    bool areAllRequiredFieldsFilled = batchNumber != null &&
                                                      receivingDate != null &&
                                                      productDetailId != null &&
                                                      expiryDate != null &&
                                                      receivedQuantity != null &&
                                                      providerId != null;

                    if (areAllRequiredFieldsFilled)
                    {
                        InputDocumentLine line = new InputDocumentLine();
                        line.InputDocument = inputDocument;
                        line.ReceivingDate = Convert.ToDateTime(receivingDate.ToString());
                        line.Provider      = db.Providers.Single(pr => pr.Id == (int)providerId);

                        line.ReceivedQuantity = Convert.ToDouble(receivedQuantity.ToString());
                        line.Vehicle          = (vehicle != null) ? vehicle.ToString() : String.Empty;
                        line.Document         = (document != null) ? document.ToString() : String.Empty;

                        var retrievedProductFromDB = db.Products
                                                     .FirstOrDefault(p => p.ProductDetail.Id == (int)productDetailId &&
                                                                     p.BatchNumber == batchNumber.ToString()
                                                                     );
                        if (retrievedProductFromDB == null)
                        {
                            Product product = new Product();
                            product.AvailableQuantity = line.ReceivedQuantity;
                            product.BatchNumber       = batchNumber.ToString();
                            product.ExpiryDate        = Convert.ToDateTime(expiryDate).Date;
                            product.StorageCondition  = (storageCondition != null) ? storageCondition.ToString() : String.Empty;
                            product.ProductDetail     = db.ProductDetails.Single(p => p.Id == (int)productDetailId);

                            line.Product = product;
                        }
                        else
                        {
                            line.Product = retrievedProductFromDB;
                            line.Product.AvailableQuantity += line.ReceivedQuantity;
                        }

                        inputDocument.Lines.Add(line);
                    }
                    else
                    {
                        return(null);
                    }
                }
                if (saveInDB)
                {
                    db.InputDocuments.Add(inputDocument);
                    db.SaveChanges();
                }
            }

            return(inputDocument);
        }