private void button1_Click(object sender, EventArgs e) { Button oBtn = sender as Button; IList <TLDYE_DyeBatchDetails> DBDetails = new List <TLDYE_DyeBatchDetails>(); IList <TLADM_Departments> Depts = new List <TLADM_Departments>(); bool Add = true; if (oBtn != null && formloaded) { var Selected = (TLDYE_DyeBatch)cmboBatchNo.SelectedItem; if (Selected == null) { MessageBox.Show("Please select a dyebatch from the drop down box"); return; } var SelectedOp = (TLADM_MachineOperators)cmboOperator.SelectedItem; if (SelectedOp == null) { MessageBox.Show("Please select an operator from the drop down list"); return; } using (var context = new TTI2Entities()) { Depts = context.TLADM_Departments.ToList(); var DB = context.TLDYE_DyeBatch.Find(Selected.DYEB_Pk); if (DB != null) { //------------------------------------------------- // This does the dye batch //------------------------------------------------------------ if (rbBtnNo.Checked) { DB.DYEB_Stage1 = true; } var Dept = Depts.Where(x => x.Dep_ShortCode.Contains("DYE")).FirstOrDefault(); if (Dept != null) { TLDYE_DyeTransactions trns = new TLDYE_DyeTransactions(); trns = context.TLDYE_DyeTransactions.Where(x => x.TLDYET_Batch_FK == DB.DYEB_Pk && x.TLDYET_Stage == 3).FirstOrDefault(); if (trns == null) { trns = new TLDYE_DyeTransactions(); } else { Add = false; } trns.TLDYET_BatchNo = DB.DYEB_BatchNo; trns.TLDYET_BatchWeight = DB.DYEB_BatchKG; trns.TLDYET_Date = DateTime.Now; trns.TLDYET_SequenceNo = DB.DYEB_SequenceNo; trns.TLDYET_Batch_FK = DB.DYEB_Pk; trns.TLDYET_Stage = 3; if (rbBtnNo.Checked) { trns.TLDYET_Rejected = false; } else { trns.TLDYET_Rejected = true; trns.TLDYET_RejectDate = DateTime.Now; } var TT = context.TLADM_TranactionType.Where(x => x.TrxT_Department_FK == Dept.Dep_Id && x.TrxT_Number == 400).FirstOrDefault(); if (TT != null) { trns.TLDYET_TransactionType = TT.TrxT_Pk; trns.TLDYET_CurrentStore_FK = (int)TT.TrxT_ToWhse_FK; } if (Add) { context.TLDYE_DyeTransactions.Add(trns); } DBDetails = context.TLDYE_DyeBatchDetails.Where(x => x.DYEBD_DyeBatch_FK == DB.DYEB_Pk).ToList(); foreach (var Record in DBDetails) { Record.DYEBO_CurrentStore_FK = trns.TLDYET_CurrentStore_FK; } } //------------------------------------------------------------------------- // This code assign operators //---------------------------------------------------------------- var selected = (TLADM_MachineOperators)cmboOperator.SelectedItem; if (selected != null) { var allop = context.TLDYE_AllocatedOperator.Where(x => x.DYEOP_BatchNo_FK == DB.DYEB_Pk).FirstOrDefault(); if (allop == null) { allop = new TLDYE_AllocatedOperator(); allop.DYEOP_BatchDate = DateTime.Now; allop.DYEOP_Operator_FK = selected.MachOp_Pk; allop.DYEOP_BatchNo_FK = DB.DYEB_Pk; context.TLDYE_AllocatedOperator.Add(allop); } else { allop.DYEOP_BatchDate = DateTime.Now; allop.DYEOP_Operator_FK = selected.MachOp_Pk; } } //if (DB.DYEB_SequenceNo == 0) //{ //--------------------------------------------------------------------- // 1st thing get the colour of the batch //--------------------------------------------------------------------- var Colour = context.TLADM_Colours.Find(DB.DYEB_Colour_FK); //---------------------------------------------------------------- // Now get the Receipe for this colour //---------------------------------------------------------------- var Receipe = context.TLDYE_RecipeDefinition.Where(x => x.TLDYE_ColorChart_FK == DB.DYEB_Colour_FK).FirstOrDefault(); if (Receipe != null) { var ReceipeDefinition = context.TLDYE_DefinitionDetails.Where(x => x.TLDYED_Receipe_FK == Receipe.TLDYE_DefinePk).ToList(); foreach (var Definition in ReceipeDefinition) { var ConsDC = context.TLADM_ConsumablesDC.Find(Definition.TLDYED_Cosumables_FK); if (ConsDC.ConsDC_Discontinued) { continue; } if (!Definition.TLDYED_LiqCalc) { var Kgs = (Definition.TLDYED_MELFC * DB.DYEB_BatchKG) / 100; //----------------------------------- //Find this Record in the SOH //---------------------------------------------- if (Dept != null) { var SOH = context.TLDYE_ConsumableSOH.Where(x => x.DYCSH_Consumable_FK == Definition.TLDYED_Cosumables_FK && x.DYCSH_DyeKitchen).FirstOrDefault(); //------------------------ // Now there might be NO stock on hand for this item //-------------------------------------------------------- if (SOH == null) { var Whse = context.TLADM_WhseStore.Where(x => x.WhStore_DepartmentFK == Dept.Dep_Id && x.WhStore_DyeKitchen).FirstOrDefault(); TLDYE_ConsumableSOH cons = new TLDYE_ConsumableSOH(); cons.DYCSH_Consumable_FK = Definition.TLDYED_Cosumables_FK; cons.DYCSH_StockOnHand -= Kgs; cons.DYCSH_WhseStore_FK = Whse.WhStore_Id; context.TLDYE_ConsumableSOH.Add(cons); try { context.SaveChanges(); } catch (Exception ex) { MessageBox.Show("Unable to adjust consummable Stock on hand table" + ex.Message); continue; } } else { SOH.DYCSH_StockOnHand -= Kgs; } } } else { //------------------------------------------- // We Now use the the liquidity factor //------------------------------------------------------------ var Kgs = (Definition.TLDYED_MELFC * DB.DYEB_BatchKG * Definition.TLDYED_LiqRatio) / 1000; //----------------------------------- //Find this Record in the SOH //---------------------------------------------- var SOH = context.TLDYE_ConsumableSOH.Where(x => x.DYCSH_Consumable_FK == Definition.TLDYED_Cosumables_FK && x.DYCSH_DyeKitchen).FirstOrDefault(); //------------------------ // Now there might be NO stock on hand for this item in the consumables stock on hand //-------------------------------------------------------- if (SOH == null) { var Whse = context.TLADM_WhseStore.Where(x => x.WhStore_DepartmentFK == Dept.Dep_Id && x.WhStore_DyeKitchen).FirstOrDefault(); TLDYE_ConsumableSOH cons = new TLDYE_ConsumableSOH(); cons.DYCSH_Consumable_FK = Definition.TLDYED_Cosumables_FK; cons.DYCSH_StockOnHand -= Kgs; cons.DYCSH_WhseStore_FK = Whse.WhStore_Id; context.TLDYE_ConsumableSOH.Add(cons); try { context.SaveChanges(); } catch (Exception ex) { MessageBox.Show("Unable to adjust consummable Stock on hand table" + ex.Message); continue; } } else { SOH.DYCSH_StockOnHand -= Kgs; } } } try { context.SaveChanges(); } catch (Exception ex) { MessageBox.Show("Unable to adjust consummable Stock on hand table" + ex.Message); } // } } } try { context.SaveChanges(); SetUp(); MessageBox.Show("Data saved to database successfully"); } catch (Exception ex) { MessageBox.Show(ex.Message); } } } }
private void button1_Click(object sender, EventArgs e) { Button oBtn = sender as Button; if (oBtn != null && formloaded) { var WhseIssue = (TLADM_WhseStore)cmboIssueDepartment.SelectedItem; if (WhseIssue == null) { MessageBox.Show("Please select a Chemical Store from the list provided"); return; } var WhseReceiving = (TLADM_WhseStore)cmboReceivingDepartment.SelectedItem; if (WhseReceiving == null) { MessageBox.Show("Please select a Dye Kitchen from the list provided"); return; } using (var context = new TTI2Entities()) { var LNU = context.TLADM_LastNumberUsed.Find(3); LNU.col12 += 1; foreach (DataGridViewRow row in dataGridView1.Rows) { if (!(bool)row.Cells[1].Value) { continue; } var index = (int)row.Cells[0].Value; //--------------------------------------- // 1st decision //------------------------------------------------------- TLDYE_ConsumableSOH newSOH = new TLDYE_ConsumableSOH(); newSOH = context.TLDYE_ConsumableSOH.Where(x => x.DYCSH_WhseStore_FK == WhseReceiving.WhStore_Id && x.DYCSH_Consumable_FK == index).FirstOrDefault(); if (newSOH == null) { newSOH = new TLDYE_ConsumableSOH(); newSOH.DYCSH_StockOnHand = (decimal)row.Cells[4].Value; newSOH.DYCSH_TransNumber = -1 + LNU.col12; newSOH.DYCSH_Consumable_FK = (int)row.Cells[5].Value; newSOH.DYCSH_WhseStore_FK = WhseReceiving.WhStore_Id; newSOH.DYCSH_DyeKitchen = WhseReceiving.WhStore_DyeKitchen; context.TLDYE_ConsumableSOH.Add(newSOH); } else { newSOH.DYCSH_StockOnHand += (decimal)row.Cells[4].Value; } TLDYE_ConsumableSOH oldSOH = new TLDYE_ConsumableSOH(); oldSOH = context.TLDYE_ConsumableSOH.Find(index); if (oldSOH != null) { oldSOH.DYCSH_StockOnHand -= (Decimal)row.Cells[4].Value; } } try { context.SaveChanges(); MessageBox.Show("Data successfully saved to database"); DyeReportOptions opts = new DyeReportOptions(); opts.fromDate = DateTime.Now; opts.LNU = -1 + LNU.col12; opts.toStore = WhseReceiving.WhStore_Id; TLADM_WhseStore FromWhse = context.TLADM_WhseStore.Where(x => x.WhStore_Code.Contains("CS")).FirstOrDefault(); opts.fromStore = FromWhse.WhStore_Id; frmDyeViewReport vRep = new frmDyeViewReport(30, opts); int h = Screen.PrimaryScreen.WorkingArea.Height; int w = Screen.PrimaryScreen.WorkingArea.Width; vRep.ClientSize = new Size(w, h); vRep.ShowDialog(this); } catch (Exception ex) { MessageBox.Show(ex.Message); } } } }
private void btnSave_Click(object sender, EventArgs e) { Button oBtn = sender as Button; if (oBtn != null) { using (var context = new TTI2Entities()) { var StoreSelected = (TLADM_WhseStore)cmboChemicalStore.SelectedItem; if (StoreSelected == null) { MessageBox.Show("Please select a Chemical Store from the list provided"); return; } foreach (DataGridViewRow row in dataGridView1.Rows) { if (row.Cells[0].Value == null || row.Cells[1].Value == null || row.Cells[2].Value == null) { continue; } TLDYE_ConSummableReceived consReceived = new TLDYE_ConSummableReceived(); consReceived.DYECON_Consumable_FK = (int)row.Cells[0].Value; consReceived.DYECON_Amount = (decimal)row.Cells[1].Value; consReceived.DYECON_TransactionDate = dtpDateReceived.Value; consReceived.DYECON_UOM_FK = (int)row.Cells[2].Value; consReceived.DYECON_WhseStore_FK = StoreSelected.WhStore_Id; context.TLDYE_ConSummableReceived.Add(consReceived); var StockOH = context.TLDYE_ConsumableSOH.Where(x => x.DYCSH_Consumable_FK == consReceived.DYECON_Consumable_FK && !x.DYCSH_DyeKitchen).FirstOrDefault(); if (StockOH == null) { TLDYE_ConsumableSOH soh = new TLDYE_ConsumableSOH(); soh.DYCSH_Consumable_FK = consReceived.DYECON_Consumable_FK; soh.DYCSH_StockOnHand += consReceived.DYECON_Amount; soh.DYCSH_WhseStore_FK = (int)consReceived.DYECON_WhseStore_FK; soh.DYCSH_DyeKitchen = StoreSelected.WhStore_DyeKitchen; context.TLDYE_ConsumableSOH.Add(soh); } else { StockOH.DYCSH_StockOnHand += consReceived.DYECON_Amount; } } try { context.SaveChanges(); MessageBox.Show("Data updated to database successfully"); SetUp(); } catch (System.Data.Entity.Validation.DbEntityValidationException en) { foreach (var eve in en.EntityValidationErrors) { MessageBox.Show("following validation errors: Type" + eve.Entry.Entity.GetType().Name.ToString() + "State " + eve.Entry.State.ToString()); foreach (var ve in eve.ValidationErrors) { MessageBox.Show("- Property" + ve.PropertyName + " Message " + ve.ErrorMessage); } } } catch (Exception ex) { MessageBox.Show(ex.Message); } } } }