private void btnEditItems_Click(object sender, EventArgs e)
        {
            try
            {
                string warehouse, supplier, date;
                warehouse = cbWarehouses.Text;
                supplier  = cbSuppliers.Text;
                date      = dtpDate.Text;

                if (!string.IsNullOrEmpty(txtOrderNumber.Text))
                {
                    int            id    = int.Parse(txtOrderNumber.Text);
                    SupplyingOrder order = (from c in ent.SupplyingOrders
                                            where c.number == id
                                            select c).FirstOrDefault();

                    obj = new SupplyOrderInfo(order.number);
                    obj.Show();
                }
                else
                {
                    MessageBox.Show("Error!\n" +
                                    "the Order you are trying to update doesn't exist.\n" +
                                    "hint: try \"New\" instaed");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
Esempio n. 2
0
 public SupplyOrderInfo(int orderNumber)
 {
     InitializeComponent();
     order    = new SupplyingOrder();
     orderNum = orderNumber;
     ent      = new WarehouseDB();
 }
        private void btnEditOrder_Click(object sender, EventArgs e)
        {
            try
            {
                string warehouse, supplier, date;
                warehouse = cbWarehouses.Text;
                supplier  = cbSuppliers.Text;
                date      = dtpDate.Text;

                if (!string.IsNullOrEmpty(txtOrderNumber.Text))
                {
                    int            id    = int.Parse(txtOrderNumber.Text);
                    SupplyingOrder order = (from c in ent.SupplyingOrders
                                            where c.number == id
                                            select c).FirstOrDefault();

                    if (!string.IsNullOrEmpty(warehouse) && !string.IsNullOrEmpty(supplier) && !string.IsNullOrEmpty(date))
                    {
                        int warehouseId = (from w in ent.Wharehouses
                                           where w.name == warehouse
                                           select w.ID).FirstOrDefault();

                        int supplierId = (from s in ent.Providers
                                          where s.name == supplier
                                          select s.id).FirstOrDefault();
                        var d = Convert.ToDateTime(date);
                        order.supplier_id  = supplierId;
                        order.warehouse_id = warehouseId;
                        order.date         = d;

                        ent.SaveChanges();
                        MessageBox.Show("Order Updated!\n", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        SupplingOrders_Load(sender, e);
                    }
                    else
                    {
                        MessageBox.Show("All Fields Required!");
                    }
                }
                else
                {
                    MessageBox.Show("Error!\n" +
                                    "the field you are trying to update doesn't exist.\n" +
                                    "hint: try \"New\" instaed");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void cbUnits_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                nudItemQuantity.Value = 0;
                if (cbItems.SelectedIndex != -1)
                {
                    // set max quantity
                    // get avalible quantity in warehouse
                    string unitName = cbUnits.Text;
                    Unit   unit     = (from u in ent.Units
                                       where u.Name == unitName
                                       select u).FirstOrDefault();

                    string     warehouseName = cbWharehouses.Text;
                    Wharehouse warehouse     = (from w in ent.Wharehouses
                                                where w.name == warehouseName
                                                select w).FirstOrDefault();

                    int            orderNum = int.Parse(cbSupplyingOrders.Text);
                    SupplyingOrder order    = (from sOrder in ent.SupplyingOrders
                                               where sOrder.number == orderNum
                                               select sOrder).FirstOrDefault();

                    string itemName = cbItems.Text;
                    Item   item     = (from i in ent.Items
                                       where i.Name == itemName
                                       select i).FirstOrDefault();

                    WarehouseItem warehouseItem =
                        (from wi in ent.WarehouseItems
                         where wi.warehouse_id == warehouse.ID &&
                         wi.order_id == order.number &&
                         wi.item_id == item.Code
                         select wi
                        ).FirstOrDefault();

                    nudItemQuantity.Maximum = warehouseItem.quantity / unit.quantity;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void cbSupplyingOrders_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                cbItems.Items.Clear();
                cbItems.Text = "";
                cbUnits.Items.Clear();
                cbUnits.Text          = "";
                nudItemQuantity.Value = 0;
                if (cbSupplyingOrders.SelectedIndex != -1)
                {
                    // get Order
                    int            orderNum = int.Parse(cbSupplyingOrders.Text);
                    SupplyingOrder order    = (from sOrder in ent.SupplyingOrders
                                               where sOrder.number == orderNum
                                               select sOrder).FirstOrDefault();
                    // get all order Items
                    //foreach (SupplyingOrder_Items order_Items in ent.SupplyingOrder_Items)
                    //{
                    //    if (order_Items.order_number == orderNum)
                    //    {
                    //        cbItems.Items.Add(order_Items.Item.Name);
                    //    }
                    //}

                    string     warehouseName = cbWharehouses.Text;
                    Wharehouse warehouse     = (from w in ent.Wharehouses
                                                where w.name == warehouseName
                                                select w).FirstOrDefault();
                    foreach (WarehouseItem wi in ent.WarehouseItems)
                    {
                        if (wi.order_id == orderNum && wi.warehouse_id == warehouse.ID && wi.quantity > 0)
                        {
                            cbItems.Items.Add(wi.Item.Name);
                        }
                    }

                    cbItems.SelectedIndex = -1;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void btnNew_Click(object sender, EventArgs e)
        {
            try
            {
                string warehouse, supplier, date;
                warehouse = cbWarehouses.Text;
                supplier  = cbSuppliers.Text;
                date      = dtpDate.Text;

                if (!string.IsNullOrEmpty(warehouse) && !string.IsNullOrEmpty(supplier) && !string.IsNullOrEmpty(date))
                {
                    int warehouseId = (from w in ent.Wharehouses
                                       where w.name == warehouse
                                       select w.ID).FirstOrDefault();

                    int supplierId = (from s in ent.Providers
                                      where s.name == supplier
                                      select s.id).FirstOrDefault();
                    var            d     = Convert.ToDateTime(date);
                    SupplyingOrder order = new SupplyingOrder
                    {
                        supplier_id  = supplierId,
                        warehouse_id = warehouseId,
                        date         = d
                    };
                    ent.SupplyingOrders.Add(order);
                    ent.SaveChanges();
                    MessageBox.Show("Order added!\n", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    //SupplingOrders_Load(sender, e);
                    obj = new SupplyOrderInfo(order.number);
                    obj.Show();
                }
                else
                {
                    MessageBox.Show("All Fields Required!", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
Esempio n. 7
0
        private void SupplyOrderInfo_Load(object sender, System.EventArgs e)
        {
            try
            {
                order = (from ord in ent.SupplyingOrders
                         where ord.number == orderNum
                         select ord).FirstOrDefault();
                txtOrderNum.Text   = order.number.ToString();
                txtWarehouse.Text  = order.Wharehouse.name;
                txtSupplier.Text   = order.Provider.name;
                dtpOrderDate.Value = order.date;

                cbItems.Items.Clear();
                cbUnits.Items.Clear();
                nudItemQuantity.Value       = 0;
                nudExpirationDuration.Value = 0;

                dgvOrderItems.Rows.Clear();

                // load order items
                foreach (var order_item in order.SupplyingOrder_Items)
                {
                    dgvOrderItems.Rows.Add(order_item.Item.Name, order_item.quantity,
                                           order_item.Production_Date, order_item.expiration_date);
                }
                // load all items in combobox
                foreach (var item in ent.Items)
                {
                    cbItems.Items.Add(item.Name);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void btnAddItem_Click(object sender, EventArgs e)
        {
            try
            {
                string warehouseName, supplyingOrderNumber, itemName, unitName;
                int    quantity           = (int)nudItemQuantity.Value,
                       paymentOrderNumber = int.Parse(txtOrderNum.Text);
                warehouseName        = cbWharehouses.Text;
                supplyingOrderNumber = cbSupplyingOrders.Text;
                itemName             = cbItems.Text;
                unitName             = cbUnits.Text;
                if (!string.IsNullOrEmpty(warehouseName) && !string.IsNullOrEmpty(warehouseName) &&
                    !string.IsNullOrEmpty(warehouseName) && !string.IsNullOrEmpty(warehouseName) &&
                    quantity > 0)
                {
                    int supplyingOrderId = int.Parse(supplyingOrderNumber);
                    // prepare data
                    Item item = (from i in ent.Items
                                 where i.Name == itemName
                                 select i).FirstOrDefault();

                    Unit unit = (from u in ent.Units
                                 where u.Name == unitName
                                 select u).FirstOrDefault();

                    Wharehouse warehouse = (from w in ent.Wharehouses
                                            where w.name == warehouseName
                                            select w).FirstOrDefault();

                    SupplyingOrder order = (from sOrder in ent.SupplyingOrders
                                            where sOrder.number == supplyingOrderId
                                            select sOrder).FirstOrDefault();

                    WarehouseItem warehouseItem =
                        (from wi in ent.WarehouseItems
                         where wi.warehouse_id == warehouse.ID &&
                         wi.order_id == order.number &&
                         wi.item_id == item.Code
                         select wi
                        ).FirstOrDefault();

                    // check if item exist in current payment order
                    Inovice_Items inovice_Item = (from i in ent.Inovice_Items
                                                  where i.product_id == item.Code &&
                                                  i.order_number == paymentOrderNumber &&
                                                  i.supplyingOrder_id == order.number
                                                  select i).FirstOrDefault();
                    if (inovice_Item == null)
                    {
                        quantity *= unit.quantity;
                        // step 1: insert into inovice_items(order_number, item_number,
                        // supplyOrder_number, quantity)
                        inovice_Item = new Inovice_Items();
                        inovice_Item.order_number      = paymentOrderNumber;
                        inovice_Item.product_id        = item.Code;
                        inovice_Item.supplyingOrder_id = order.number;
                        inovice_Item.quantity          = quantity;
                        ent.Inovice_Items.Add(inovice_Item);

                        // update item, warehouse_items
                        // step 2: update item_quantity -= order_quantity
                        item.quantity -= quantity;
                        // step 3: update Warehouse_item_quantity -= order_quantity
                        warehouseItem.quantity -= quantity;
                        // insert into transaction sell = 2
                        // insert into transaction(2, supOrderId, itemId, quantity,
                        // warehouseId, WarehouseItemsQuantity, itemQuantity, date, paymentOrderId)
                        Transaction sellTransaction = new Transaction();
                        sellTransaction.type_id       = 2;
                        sellTransaction.order_id      = order.number;
                        sellTransaction.item_id       = item.Code;
                        sellTransaction.quantity      = quantity;
                        sellTransaction.wharehouse_id = warehouse.ID;
                        sellTransaction.total_quantity_for_item_in_wharehouse    = (int)item.quantity;
                        sellTransaction.total_quantity_for_product_in_wharehouse = (int)warehouseItem.quantity;
                        sellTransaction.date           = DateTime.Now;
                        sellTransaction.paymentOrderId = paymentOrderNumber;
                        ent.Transactions.Add(sellTransaction);
                        ent.SaveChanges();
                        MessageBox.Show("Item Added!", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        PaymentOrderInfo_Load(sender, e);
                    }
                    else
                    {
                        MessageBox.Show("Item already exist in this order!", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
                else
                {
                    MessageBox.Show("All Data Required!", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                string warehouse, supplier, date;
                warehouse = cbWarehouses.Text;
                supplier  = cbSuppliers.Text;
                date      = dtpDate.Text;

                if (!string.IsNullOrEmpty(txtOrderNumber.Text))
                {
                    int            id    = int.Parse(txtOrderNumber.Text);
                    SupplyingOrder order = (from c in ent.SupplyingOrders
                                            where c.number == id
                                            select c).FirstOrDefault();
                    if (order.Inovice_Items.Count > 0)
                    {
                        MessageBox.Show("Can't delete this order\nSome of the items in order were sold!");
                    }
                    else
                    {
                        // foreach item in this order, decrease Item.quantity
                        // delete from transctions, warehouse items, suppling order items , supplying order

                        foreach (Transaction transaction in ent.Transactions)
                        {
                            if (transaction.order_id == order.number)
                            {
                                ent.Transactions.Remove(transaction);
                            }
                        }

                        foreach (WarehouseItem warehouseItem in ent.WarehouseItems)
                        {
                            if (warehouseItem.order_id == order.number)
                            {
                                //get item, decrease quantity, delete warehouseItem
                                int  itemId = warehouseItem.item_id;
                                Item item   = (from i in ent.Items
                                               where i.Code == itemId
                                               select i).FirstOrDefault();
                                item.quantity -= warehouseItem.quantity;
                                ent.WarehouseItems.Remove(warehouseItem);
                            }
                        }

                        foreach (SupplyingOrder_Items i in ent.SupplyingOrder_Items)
                        {
                            if (i.order_number == order.number)
                            {
                                ent.SupplyingOrder_Items.Remove(i);
                            }
                        }

                        ent.SupplyingOrders.Remove(order);

                        ent.SaveChanges();

                        MessageBox.Show("Order Deleted!\n", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        SupplingOrders_Load(sender, e);
                    }
                }
                else
                {
                    MessageBox.Show("Select Order to Delete!");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }