Пример #1
0
        private void saveStocks(SMEntities db)
        {
            // Update Stocks
            foreach (var item in saleOrder.SaleOrderItems)
            {
                Product prod = prodsList.Where(p => p.prod_id.Equals(item.prod_id))
                        .First();
                prod.Stocks.OrderBy(s => s.base_price_by_stock);

                // Proceed stocks
                if (item.quantity_by_stock > 0)
                {
                    int remainStockNeedProceed = item.quantity_by_stock;
                    // Take lowest price stock first
                    foreach (var stock in prod.Stocks)
                    {
                        if (stock.quantity_by_stock == 0)// Proceed next stock
                            continue;
                        if (remainStockNeedProceed <= 0)// Proceed complete
                            break;
                        remainStockNeedProceed = remainStockNeedProceed - stock.quantity_by_stock;
                        if (remainStockNeedProceed < 0)
                        {
                            stock.quantity_by_stock = -remainStockNeedProceed;
                            db.Entry(stock).State = EntityState.Modified;
                            break;// Proceed complete
                        }
                        else
                        {
                            // Proceed the stock
                            stock.quantity_by_stock = 0;
                            db.Entry(stock).State = EntityState.Modified;
                        }
                    }
                }

                // Proceed units
                if (item.quantity_by_unit > 0 &&
                    item.quantity_by_unit < item.quantity_control)
                {
                    int remainUnitNeedProceed = item.quantity_by_unit;
                    // Take lowest price stock first
                    foreach (var stock in prod.Stocks)
                    {
                        if (stock.quantity_by_unit == 0)// Proceed next stock
                            continue;
                        if (remainUnitNeedProceed <= 0)// Proceed complete
                            break;
                        remainUnitNeedProceed = remainUnitNeedProceed - stock.quantity_by_unit;
                        if (remainUnitNeedProceed <= 0)
                        {
                            stock.quantity_by_unit = -remainUnitNeedProceed;
                            db.Entry(stock).State = EntityState.Modified;
                            break;// Proceed complete
                        }
                        else
                        {
                            // Subtract from availabe stock
                            if (stock.quantity_by_stock >= 1)
                            {
                                stock.quantity_by_stock -= 1;
                                stock.quantity_by_unit = stock.quantity_control - remainUnitNeedProceed;
                                db.Entry(stock).State = EntityState.Modified;
                            }
                            //else continue proceed next stock
                        }
                    }
                }

                // Update quantity the product
                prod.quantity_by_stock -= item.quantity_by_stock;
                if (prod.quantity_by_unit >= item.quantity_by_unit)
                    prod.quantity_by_unit -= item.quantity_by_unit;
                else
                {
                    prod.quantity_by_stock -= 1;
                    prod.quantity_by_unit = prod.quantity_control + prod.quantity_by_unit
                        - item.quantity_by_unit;
                }
                db.Entry(prod).State = EntityState.Modified;
                if (prod.quantity_by_stock < 0 || prod.quantity_by_unit < 0)
                {
                    MessageBox.Show("Lỗi thực thi.");
                }
            }
        }
Пример #2
0
        private void saveStock(SMEntities db)
        {
            // Save to Stocks
                var stockList = db.Stocks;
                foreach (var item in importOrder.ImportOrderItems)
                {
                    // Get stock has same prod_id & base_price_by_unit to item
                    var stock = stockList.Where(s => s.prod_id.Equals(item.prod_id)
                        && s.base_price_by_unit.Equals(item.base_price_by_unit));
                    if (stock.Any())// Exist
                    {
                        Stock st = stock.First();
                        st.quantity_by_stock += item.quantity_by_stock;
                        st.quantity_by_unit += item.quantity_by_unit;
                    }
                    else// Create new stock for the product
                    {
                        Stock newStock = db.Stocks.Create();
                        newStock.prod_id = item.prod_id;
                        newStock.Product = item.Product;
                        newStock.quantity_by_stock = item.quantity_by_stock;
                        newStock.quantity_by_unit = item.quantity_by_unit;
                        newStock.base_price_by_stock = item.base_price_by_stock;
                        newStock.base_price_by_unit = item.base_price_by_unit;
                        newStock.quantity_control = item.quantity_control;
                        newStock.date_expired = item.date_expired;

                        // Save to Stocks
                        db.Stocks.Add(newStock);
                    }

                    // Update quantity on Product
                    var prod = db.Products.Find(item.prod_id);
                    prod.quantity_by_stock += item.quantity_by_stock;
                    prod.quantity_by_unit += item.quantity_by_unit;
                    prod.date_modified = DateTime.Now;

                    //db.Products.Attach(prod);
                    db.Entry(prod).State = EntityState.Modified;
                }
        }