private void PaymentOrderInfo_Load(object sender, EventArgs e) { txtOrderNum.Text = order.number.ToString(); txtCustomer.Text = order.Customer.name; dtpOrderDate.Value = order.Date; cbWharehouses.Items.Clear(); cbSupplyingOrders.Items.Clear(); cbItems.Items.Clear(); cbUnits.Items.Clear(); nudItemQuantity.Value = 0; // load warehouses foreach (Wharehouse warehouse in ent.Wharehouses) { cbWharehouses.Items.Add(warehouse.name); } // load data grid view foreach (Inovice_Items info in ent.Inovice_Items) { // load data grid view Items // item name(item), quantity(), // (production date, expiration date) (supplyOrderItems) // (warehouse, supplier) (supplyingOrder) // select supplyOrderItems where orderId, ProductId if (info.order_number == order.number) { SupplyingOrder_Items supplyOrderItem = ( from sOrder in ent.SupplyingOrder_Items where sOrder.order_number == info.supplyingOrder_id && sOrder.item_code == info.product_id select sOrder ).FirstOrDefault(); dgvOrderItems.Rows.Add( info.Item.Name, info.quantity, supplyOrderItem.Production_Date, supplyOrderItem.expiration_date, info.SupplyingOrder.Wharehouse.name, info.SupplyingOrder.Provider.name ); } } }
bool DeleteItemFromOrder(object sender, EventArgs e) { // delete item from order // get item id Item item = (from ite in ent.Items where ite.Name == cbItems.Text select ite).FirstOrDefault(); // get warehouse id Wharehouse wharehouse = (from warehouse in ent.Wharehouses where warehouse.name == txtWarehouse.Text select warehouse).FirstOrDefault(); // get quantity in supplyingOrderItems SupplyingOrder_Items order_Items = (from so in ent.SupplyingOrder_Items where so.order_number == orderNum && so.item_code == item.Code select so).FirstOrDefault(); // step 1: check if item still in warehouse with initial quantity // get quantity in warehouse WarehouseItem warehouseItem = (from warehouse in ent.WarehouseItems where warehouse.warehouse_id == wharehouse.ID && warehouse.order_id == order_Items.order_number && warehouse.item_id == item.Code select warehouse ).FirstOrDefault(); // get transaction buy item Transaction transaction = (from tr in ent.Transactions where tr.type_id == 1 && tr.order_id == orderNum && tr.wharehouse_id == wharehouse.ID && tr.item_id == item.Code select tr ).FirstOrDefault(); // compare quantity if (order_Items.quantity == warehouseItem.quantity) { // step 2: in items: set quantity -= old value item.quantity -= order_Items.quantity; // step 3: in warehouse items: delete ent.WarehouseItems.Remove(warehouseItem); // step 4: in supplying order items: delete ent.SupplyingOrder_Items.Remove(order_Items); // step 5: in Transactions: delete ent.Transactions.Remove(transaction); ent.SaveChanges(); return(true); } else { MessageBox.Show("Can't edit/delete this Item Because it was moved or sold", "", MessageBoxButtons.OK, MessageBoxIcon.Error); return(false); } }
void InsertItemInOrder(object sender, EventArgs e) { try { selectedItemName = cbItems.Text; unit = cbUnits.Text; itemQuantity = (int)nudItemQuantity.Value; date = dtpItemProductionDate.Value; itemExpirationDuration = (int)nudExpirationDuration.Value; if (!string.IsNullOrEmpty(selectedItemName) && !string.IsNullOrEmpty(unit) && itemQuantity > 0 && itemExpirationDuration > 0) { int unitNumber = (from u in ent.Units where u.Name == unit select u.quantity).FirstOrDefault(); // select item Item selectedItem = (from item in ent.Items where item.Name == selectedItemName select item).FirstOrDefault(); // select warehouse_id int warehouseId = (from warehouse in ent.Wharehouses where warehouse.name == txtWarehouse.Text select warehouse.ID).FirstOrDefault(); // order number int orderNumber = int.Parse(txtOrderNum.Text); // calculate expiration date DateTime expirationDate = date.AddDays(itemExpirationDuration); // check if item exist in current order, show error"Item already exists". SupplyingOrder_Items exists = (from order_items in ent.SupplyingOrder_Items where order_items.item_code == selectedItem.Code && order_items.order_number == orderNumber select order_items).FirstOrDefault(); if (exists != null) { MessageBox.Show("Item already exists in current order!"); } else { // add to supply order item SupplyingOrder_Items order_Items = new SupplyingOrder_Items(); order_Items.item_code = selectedItem.Code; order_Items.order_number = orderNumber; order_Items.quantity = itemQuantity * unitNumber; order_Items.Production_Date = date; order_Items.expiration_date = expirationDate; //order_Items.Item = selectedItem; ent.SupplyingOrder_Items.Add(order_Items); ent.SaveChanges(); // add to warehouse items WarehouseItem warehouseItem = new WarehouseItem(); warehouseItem.item_id = selectedItem.Code; warehouseItem.order_id = orderNumber; warehouseItem.warehouse_id = warehouseId; warehouseItem.quantity = itemQuantity * unitNumber; ent.WarehouseItems.Add(warehouseItem); ent.SaveChanges(); // update item quantity selectedItem.quantity += (itemQuantity * unitNumber); ent.SaveChanges(); // add to transaction, buy = 1 Transaction buyItem = new Transaction(); buyItem.date = DateTime.Now; buyItem.type_id = 1; buyItem.order_id = orderNumber; buyItem.item_id = selectedItem.Code; buyItem.wharehouse_id = warehouseId; buyItem.quantity = itemQuantity * unitNumber; // total item quantity in all warehouses buyItem.total_quantity_for_item_in_wharehouse = (int)selectedItem.quantity; // total item quantity in current warehouse buyItem.total_quantity_for_product_in_wharehouse = itemQuantity * unitNumber; ent.Transactions.Add(buyItem); ent.SaveChanges(); MessageBox.Show("Item added to Order!", "", MessageBoxButtons.OK, MessageBoxIcon.Information); SupplyOrderInfo_Load(sender, e); } } else { MessageBox.Show("Enter all data!"); } } catch (Exception ex) { MessageBox.Show(ex.Message); } }