private void btnNewOrder_Click(object sender, EventArgs e) { try { string customerName = cbCustomers.Text; DateTime date = dtpDate.Value; if (!string.IsNullOrEmpty(customerName)) { Payment_Order order = new Payment_Order(); int customerId = (from c in ent.Customers where c.name == customerName select c.id).FirstOrDefault(); order.customer_id = customerId; order.Date = date; ent.Payment_Order.Add(order); ent.SaveChanges(); MessageBox.Show("Order Added!", "", MessageBoxButtons.OK, MessageBoxIcon.Information); PaymentOrder_Load(sender, e); obj = new PaymentOrderInfo(order); obj.Show(); } else { MessageBox.Show("Enter Order Name!", "", MessageBoxButtons.OK, MessageBoxIcon.Error); } } 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); } }
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); } }
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); } }