// add new  import invoice (this operation will affect on store quantity where the quantity in the store for specific product will increase with every added invoice)
        public async Task <Invoice> AddImportInvoceAsync(InvoiceCartViewModel impoInvoice)
        {
            try
            {
                if (impoInvoice.invoice.Paid < impoInvoice.invoice.FullCost)
                {
                    impoInvoice.invoice.RemainForYamama = impoInvoice.invoice.FullCost - impoInvoice.invoice.Paid;
                }
                else if (impoInvoice.invoice.Paid > impoInvoice.invoice.FullCost)
                {
                    impoInvoice.invoice.RemainForCustomer = impoInvoice.invoice.Paid - impoInvoice.invoice.FullCost;
                }
                else
                {
                    impoInvoice.invoice.RemainForCustomer = impoInvoice.invoice.RemainForYamama = 0;
                }

                // if one of the previous conditions is true then save the invoice in the database
                await _db.Invoice.AddAsync(impoInvoice.invoice);

                //commit the changes
                await _db.SaveChangesAsync();

                //get id for this invoice

                var RecentInvoice = _db.Invoice.OrderByDescending(p => p.Idinvoice).FirstOrDefault();

                int RecentInvoiceID = RecentInvoice.Idinvoice;
                //add cart for this invoice
                await _cart.AddCartAsync(impoInvoice, RecentInvoiceID);

                _db.SaveChanges();
                //get the last addes cart
                var RecentCart = _db.Cart.OrderByDescending(c => c.IdCart).FirstOrDefault();
                //get the quantity from this cart
                Double qty = RecentCart.Qty;
                //get the product id from this cart
                int?id = RecentCart.ProductId;

                //pass the store records and add the quantity for the specific product id
                foreach (var item in _db.Store)
                {
                    if (item.ProId == id)
                    {
                        item.Quantity += qty;
                    }
                }
                //commit changes
                _db.SaveChanges();

                return(impoInvoice.invoice);
            }
            catch
            {
                return(null);
            }
        }
Beispiel #2
0
        public async Task <List <Cart> > AddCartAsync(InvoiceCartViewModel invoiceCart, int id)
        {
            try
            {
                //store invoice full_cost
                //Double fullcost = 0;

                for (int i = 0; i < invoiceCart.listcart.Count; i++)
                {
                    //assign every item in the invoice with it's invoice_id
                    invoiceCart.listcart[i].InvoiceId = id;

                    //fullcost += Convert.ToDouble(invoiceCart.listcart[i].Price);

                    await _yamamadbContext.Cart.AddAsync(invoiceCart.listcart[i]);

                    await _yamamadbContext.SaveChangesAsync();

                    var store = _yamamadbContext.Store.Where(x => x.ProId == invoiceCart.listcart[i].ProductId).SingleOrDefault();
                    if (invoiceCart.invoice.Type == "Purchses" || invoiceCart.invoice.Type == "import")
                    {
                        if (store != null)
                        {
                            store.Quantity += invoiceCart.listcart[i].Qty;
                            _yamamadbContext.Store.Update(store);
                            _yamamadbContext.SaveChanges();
                        }
                        else
                        {
                            Store newStore = new Store
                            {
                                ProId    = invoiceCart.listcart[i].ProductId,
                                Quantity = invoiceCart.listcart[i].Qty,
                                Name     = _yamamadbContext.Product.Where(x => x.Idproduct == invoiceCart.listcart[i].ProductId).Select(x => x.Name).SingleOrDefault()
                            };
                            _yamamadbContext.Store.Add(store);
                            _yamamadbContext.SaveChanges();
                        }
                    }
                    else if (invoiceCart.invoice.Type == "sell" || invoiceCart.invoice.Type == "export")
                    {
                        if (store != null)
                        {
                            store.Quantity -= invoiceCart.listcart[i].Qty;
                            _yamamadbContext.Store.Update(store);
                            _yamamadbContext.SaveChanges();
                        }
                    }
                }
                return(invoiceCart.listcart);
            }
            catch (Exception)
            {
                return(null);
            }
        }
Beispiel #3
0
        // add a new production
        // we use the (StoreViewModel) view model because we will need data from  more than one table
        public async Task <int> AddProductionAsync(Production production)
        {
            // declare a variable
            int result = 0;
            // add the new production object to database
            await _db.Production.AddAsync(production);

            //commit the changes into database
            await _db.SaveChangesAsync();

            //get the added production and store it in variable
            var RecentProduction = _db.Production.OrderByDescending(p => p.Idproduction).FirstOrDefault();
            // get the product id from this added  production from the RecentProduction variable
            int?id = RecentProduction.IdProduct;

            //get the quantity from the added production  from the RecentProduction variable
            int qty = RecentProduction.Quantity.Value;

            // creat a loop to pass the store records and update the product quantity according to the product id
            foreach (var item in _db.Store)
            {
                if (item.ProId == id)
                {
                    item.Quantity += qty;
                }
            }
            //commit the changes into database
            _db.SaveChanges();
            //if the operation succecced return 1
            result += 1;
            //};

            return(result);
        }