Ejemplo n.º 1
0
        private void button2_Click(object sender, EventArgs e)
        {
            Button oBtn = sender as Button;

            if (oBtn != null)
            {
                using (var context = new TTI2Entities())
                {
                    foreach (DataGridViewRow rw in dataGridView1.Rows)
                    {
                        if (!(bool)rw.Cells[1].Value)
                        {
                            continue;
                        }

                        TLSPN_CottonReceivedBales bales = context.TLSPN_CottonReceivedBales.Find((int)rw.Cells[0].Value);
                        if (bales != null)
                        {
                            if ((bool)rw.Cells[1].EditedFormattedValue)
                            {
                                bales.CoBales_CottonSequence   = Convert.ToInt32(txtIssueToProdNo.Text);
                                bales.CoBales_BlowRoomPosition = (int)rw.Cells[2].Value;
                            }
                            else
                            {
                                bales.CoBales_CottonSequence   = 0;
                                bales.CoBales_BlowRoomPosition = 0;
                            }

                            try
                            {
                                context.SaveChanges();
                            }
                            catch (Exception ex)
                            {
                                MessageBox.Show(ex.Message);
                            }
                        }
                    }
                    //-----------------------------------------------------------------------
                    //Now we need to print the report
                    //-------------------------------------------------------------------
                    frmViewReport vRep = new frmViewReport(1);
                    int           h    = Screen.PrimaryScreen.WorkingArea.Height;
                    int           w    = Screen.PrimaryScreen.WorkingArea.Width;
                    vRep.ClientSize = new Size(w, h);
                    vRep.ShowDialog(this);
                }
            }
        }
Ejemplo n.º 2
0
        private void btnSave_Click(object sender, EventArgs e)
        {
            Button oBtn    = sender as Button;
            bool   success = true;

            if (oBtn != null)
            {
                using (var context = new TTI2Entities())
                {
                    foreach (DataGridViewRow dr in dataGridView1.Rows)
                    {
                        if ((int)dr.Cells[1].Value == 0)
                        {
                            continue;
                        }

                        TLSPN_CottonReceivedBales bales = new TLSPN_CottonReceivedBales();
                        //---------------------------------------------------------------------------
                        bales.CotBales_BaleNo        = (int)dr.Cells[1].Value;
                        bales.CotBales_Mic           = (decimal)dr.Cells[2].Value;
                        bales.CotBales_Weight_Nett   = (decimal)dr.Cells[3].Value;
                        bales.CotBales_Staple        = (decimal)dr.Cells[4].Value;
                        bales.CotBales_LotNo         = _CTrns.cotrx_LotNo;
                        bales.CotBales_Weight_Gross  = (decimal)dr.Cells[5].Value;
                        bales.CoBales_CottonSequence = _CTrns.cotrx_Return_No;
                        //------------------------------------------------------------------------------
                        context.TLSPN_CottonReceivedBales.Add(bales);
                    }

                    try
                    {
                        context.SaveChanges();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                        success = false;
                    }
                }

                if (success)
                {
                    MessageBox.Show("Rocords stored to database successfully");
                    this.Close();
                }
            }
        }
Ejemplo n.º 3
0
        private void btnSave_Click(object sender, EventArgs e)
        {
            Button oBtn    = sender as Button;
            bool   success = true;

            if (oBtn != null)
            {
                using (var context = new TTI2Entities())
                {
                    foreach (DataGridViewRow rw in dataGridView1.Rows)
                    {
                        if ((bool)rw.Cells[1].Value == false)
                        {
                            continue;
                        }

                        TLSPN_CottonReceivedBales bales = context.TLSPN_CottonReceivedBales.Find((int)rw.Cells[0].Value);
                        if (bales != null)
                        {
                            bales.CotBales_ConfirmedByQA = true;

                            try
                            {
                                context.SaveChanges();
                            }
                            catch (Exception ex)
                            {
                                MessageBox.Show(ex.Message);
                                success = false;
                            }
                        }
                    }
                }

                if (success)
                {
                    dataGridView1.Rows.Clear();
                    MessageBox.Show("Records stored successfully to database");
                }
            }
        }
Ejemplo n.º 4
0
        private void btnImport_Click(object sender, EventArgs e)
        {
            Button oBtn     = sender as Button;
            int    RowCount = 0;

            if (oBtn != null)
            {
                // Create an instance of the open file dialog box.
                OpenFileDialog openFileDialog1 = new OpenFileDialog();

                // Set filter options and filter index.
                openFileDialog1.Filter      = "Text Files (.txt)|*.txt|All Files (*.*)|*.*";
                openFileDialog1.FilterIndex = 1;

                openFileDialog1.Multiselect = false;

                // Call the ShowDialog method to show the dialog box.
                DialogResult res = openFileDialog1.ShowDialog(this);

                // Process input if the user clicked OK.
                if (res == DialogResult.OK)
                {
                    // Open the selected file to read.
                    try
                    {
                        System.IO.Stream fileStream = openFileDialog1.OpenFile();
                        using (System.IO.StreamReader reader = new System.IO.StreamReader(fileStream))
                        {
                            using (var context = new TTI2Entities())
                            {
                                // Read the first line from the file and write it the textbox.
                                string[] input = reader.ReadToEnd().Split('\r');
                                foreach (var line in input)
                                {
                                    string[] row = line.Split(',');

                                    if (row[0].Contains("Bale"))
                                    {
                                        continue;
                                    }



                                    TLSPN_CottonReceivedBales BalesRec = new TLSPN_CottonReceivedBales();
                                    //-------------------------------------------------------------------------------------

                                    int     Bale   = Convert.ToInt32(row[0]);
                                    decimal Micro  = Math.Round(Convert.ToDecimal(row[1]), 1);
                                    decimal Staple = Math.Round(Convert.ToDecimal(row[3]), 1);
                                    //-----------------------------------------------------------------------------
                                    BalesRec.CotBales_BaleNo         = Bale;
                                    BalesRec.CotBales_CotReceived_FK = _CTrns.cotrx_pk;
                                    BalesRec.CotBales_LotNo          = _CTrns.cotrx_LotNo;
                                    BalesRec.CoBales_CottonSequence  = _CTrns.cotrx_Return_No;
                                    BalesRec.CotBales_Mic            = Micro;
                                    BalesRec.CotBales_Staple         = Staple;
                                    BalesRec.CotBales_Weight_Nett    = _CTrns.cottrx_NettAveBaleWeight;
                                    BalesRec.CotBales_Weight_Gross   = _CTrns.cotrx_GrossAveBaleWeight;

                                    var index = dataGridView1.Rows.Add();
                                    dataGridView1.Rows[index].Cells[1].Value = Bale;                            //Bale No
                                    dataGridView1.Rows[index].Cells[2].Value = Micro;                           // MIC Decimal
                                    dataGridView1.Rows[index].Cells[3].Value = _CTrns.cottrx_NettAveBaleWeight; // Kgs (NETT)decimal
                                    dataGridView1.Rows[index].Cells[4].Value = Staple;                          // staple decimal
                                    dataGridView1.Rows[index].Cells[5].Value = _CTrns.cotrx_GrossAveBaleWeight; // kgs (GROSS) decimal

                                    RowCount += 1;
                                }

                                dataGridView1.Refresh();
                            }
                        }
                        fileStream.Close();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                        return;
                    }

                    if (RowCount != _CTrns.cotrx_NoBales)
                    {
                        MessageBox.Show("The number of records read in does not correspond to the number entered" + Environment.NewLine + "Please investigate" + Environment.NewLine + "Number of Rows read in " + RowCount.ToString());
                        btnSave.Enabled = false;
                    }
                }
            }
        }
Ejemplo n.º 5
0
        private void btnSave_Click(object sender, EventArgs e)
        {
            TLSPN_CottonReceivedBales bales;

            Button oBtn = sender as Button;

            if (oBtn != null & formloaded)
            {
                if (RowsSelected != null && rbWriteOff.Checked)
                {
                    var cnt = RowsSelected.Where(x => x == false).Count();
                    if (cnt == RowsSelected.Count())
                    {
                        MessageBox.Show("Please select at least one row from the grid as shown");
                        return;
                    }
                }

                var errorM = core.returnMessage(MandSelected, true, MandatoryFields);
                if (!string.IsNullOrEmpty(errorM))
                {
                    MessageBox.Show(errorM);
                    return;
                }

                var LotDetails = (TLSPN_CottonTransactions)cmbLotNo.SelectedItem;
                //dont forget last number used
                //-------------------------------------------

                using (var context = new TTI2Entities())
                {
                    var LastNumber = context.TLADM_LastNumberUsed.Find(1);
                    if (LastNumber != null)
                    {
                        LastNumber.col3 += 1;
                    }

                    // 0
                    // 1 Selected
                    // 2 Bales Numeric
                    // 3 MIC Decimal
                    // 4 kgs (NETT) Decimal
                    // 5 Staple Decimal
                    // 6 kgs (GROSS) decimal
                    int     NoBales         = 0;
                    decimal NettMass        = 0M;
                    decimal GrossMass       = 0M;
                    var     ContractDetails = (TLADM_CottonContracts)cmbContractNo.SelectedItem;

                    foreach (DataGridViewRow row in dataGridView1.Rows)
                    {
                        if (row.Cells[1].Value == null || (bool)row.Cells[1].Value == false)
                        {
                            continue;
                        }

                        NoBales   += 1;
                        NettMass  += (decimal)row.Cells[4].Value;
                        GrossMass += (decimal)row.Cells[6].Value;

                        if (rbWriteOn.Checked)
                        {
                            TLSPN_CottonReceivedBales Bales = new TLSPN_CottonReceivedBales();
                            Bales.CotBales_BaleNo        = (int)row.Cells[2].Value;
                            Bales.CotBales_Mic           = (decimal)row.Cells[3].Value;
                            Bales.CotBales_Staple        = (decimal)row.Cells[5].Value;
                            Bales.CotBales_Weight_Nett   = (decimal)row.Cells[4].Value;
                            Bales.CotBales_LotNo         = LotDetails.cotrx_LotNo;
                            Bales.CotBales_Weight_Gross  = (decimal)row.Cells[6].Value;
                            Bales.CoBales_CottonSequence = Convert.ToInt32(txtAdjustmentNumber.Text);
                            context.TLSPN_CottonReceivedBales.Add(Bales);
                        }
                        else
                        {
                            bales = context.TLSPN_CottonReceivedBales.Find((int)row.Cells[0].Value);
                            if (bales != null)
                            {
                                if ((bool)row.Cells[1].Value == true)
                                {
                                    bales.CoBales_CottonReturned = true;
                                    bales.CoBales_CottonSequence = Convert.ToInt32(txtAdjustmentNumber.Text);

                                    try
                                    {
                                        context.SaveChanges();
                                    }
                                    catch (Exception ex)
                                    {
                                        MessageBox.Show(ex.Message);
                                    }
                                }
                            }
                        }
                    }

                    //----------------------------------------------------------------------------
                    //
                    //------------------------------------------------------------------------------

                    TLSPN_CottonTransactions cotTrans = new TLSPN_CottonTransactions();

                    cotTrans.cotrx_TransDate     = dateTimePicker1.Value;
                    cotTrans.cotrx_ContractNo_Fk = ContractDetails.CottonCon_Pk;
                    cotTrans.cotrx_LotNo         = Convert.ToInt32(cmbLotNo.SelectedValue);
                    cotTrans.cotrx_NetWeight     = NettMass;
                    cotTrans.cotrx_GrossWeight   = GrossMass;
                    cotTrans.cotrx_NoBales       = NoBales;
                    //cotTrans.cotrx_VehReg_FK = null;
                    cotTrans.cotrx_WeighBridgeFull  = 0;
                    cotTrans.cotrx_WeighBridgeEmpty = 0;
                    cotTrans.cotrx_NettPerWB        = 0;
                    cotTrans.cotrx_Return_No        = Convert.ToInt32(txtAdjustmentNumber.Text);
                    cotTrans.cotrx_Supplier_FK      = ContractDetails.CottonCon_ConSupplier_FK;
                    cotTrans.cotrx_Notes            = rtbNotes.Text;
                    //-------------------------------------------------------------------------
                    // Consult Table TLADM_TranTypes
                    //----------------------------------------------------------------------------
                    var DepDetail = context.TLADM_Departments.Where(x => x.Dep_ShortCode == "SPIN").FirstOrDefault();
                    if (DepDetail != null)
                    {
                        var Trantype = context.TLADM_TranactionType.Where(x => x.TrxT_Number == 400 && x.TrxT_Department_FK == DepDetail.Dep_Id).FirstOrDefault();
                        if (Trantype != null)
                        {
                            cotTrans.cotrx_TranType = Trantype.TrxT_Pk;
                        }
                    }
                    if (rbWriteOff.Checked)
                    {
                        cotTrans.cotrx_WriteOff = true;
                    }
                    else
                    {
                        cotTrans.cotrx_WriteOff = false;
                    }
                    //------------------------------------------------------------------------

                    context.TLSPN_CottonTransactions.Add(cotTrans);

                    //-----------------------------------------------------------
                    //
                    //------------------------------------------------------------

                    string Mach_IP = Dns.GetHostEntry(Dns.GetHostName())
                                     .AddressList.First(f => f.AddressFamily == AddressFamily.InterNetwork)
                                     .ToString();


                    TLADM_DailyLog DailyLog = new TLADM_DailyLog();
                    DailyLog.TLDL_IPAddress    = Mach_IP;
                    DailyLog.TLDL_Dept_Fk      = DepDetail.Dep_Id;
                    DailyLog.TLDL_Date         = DateTime.Now;
                    DailyLog.TLDL_TransDetail  = "Cotton Adjustment";
                    DailyLog.TLDL_AuthorisedBy = txtAdjustmentNumber.Text;
                    DailyLog.TLDL_Comments     = txtAdjustmentNumber.Text;

                    context.TLADM_DailyLog.Add(DailyLog);

                    try
                    {
                        context.SaveChanges();
                        var           Retno = Convert.ToInt32(txtAdjustmentNumber.Text);
                        frmViewReport vRep  = new frmViewReport(4, Retno);
                        int           h     = Screen.PrimaryScreen.WorkingArea.Height;
                        int           w     = Screen.PrimaryScreen.WorkingArea.Width;
                        vRep.ClientSize = new Size(w, h);
                        vRep.ShowDialog(this);
                        SetUp();
                        MessageBox.Show("Data saved to database successfully");
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                }
            }
        }
Ejemplo n.º 6
0
        private void btnSave_Click(object sender, EventArgs e)
        {
            Button oBtn    = sender as Button;
            bool   success = true;

            if (oBtn != null)
            {
                var CottonRec = (TLSPN_CottonTransactions)cmbLotNo.SelectedItem;

                using (var context = new TTI2Entities())
                {
                    //Hard Coded at the moment
                    // See Table TLADM_TranactionType for a complete List of the Transaction Type Per Department
                    //--------------------------------------------------------------------------------------------------
                    var DeptDetail = context.TLADM_Departments.Where(x => x.Dep_ShortCode == "SPIN").FirstOrDefault();
                    var TranType   = context.TLADM_TranactionType.Where(x => x.TrxT_Number == 500 && x.TrxT_Department_FK == DeptDetail.Dep_Id).FirstOrDefault();

                    var LastNumber = context.TLADM_LastNumberUsed.Find(1);
                    if (LastNumber != null)
                    {
                        LastNumber.col5 += 1;
                    }

                    var NoBales     = 0;
                    var GrossWeight = 0M;
                    var NettWeight  = 0M;

                    foreach (DataGridViewRow rw in dataGridView1.Rows)
                    {
                        if (rw.Cells[1].Value == null || (bool)rw.Cells[1].Value == false)
                        {
                            continue;
                        }

                        TLSPN_CottonReceivedBales bales = context.TLSPN_CottonReceivedBales.Find((int)rw.Cells[0].Value);
                        if (bales != null)
                        {
                            bales.CoBales_IssuedToProd     = true;
                            bales.CoBales_BlowRoomPosition = 0;
                            bales.CoBales_CottonSequence   = Convert.ToInt32(txtIssueToProdNo.Text);

                            NoBales     += 1;
                            NettWeight  += (decimal)rw.Cells[5].Value;
                            GrossWeight += (decimal)rw.Cells[7].Value;
                            try
                            {
                                context.SaveChanges();
                            }
                            catch (Exception ex)
                            {
                                MessageBox.Show(ex.Message);
                                success = false;
                            }
                        }
                    }

                    //------------------------------------------------------------------
                    TLSPN_CottonTransactions cotTrans = new TLSPN_CottonTransactions();
                    cotTrans.cotrx_TransDate          = dtpTransDate.Value;
                    cotTrans.cotrx_Supplier_FK        = CottonRec.cotrx_Supplier_FK;
                    cotTrans.cotrx_Return_No          = Convert.ToInt32(txtIssueToProdNo.Text);
                    cotTrans.cotrx_ContractNo_Fk      = CottonRec.cotrx_ContractNo_Fk;
                    cotTrans.cotrx_LotNo              = CottonRec.cotrx_LotNo;
                    cotTrans.cotrx_NoBales            = NoBales;
                    cotTrans.cotrx_GrossWeight        = GrossWeight;
                    cotTrans.cotrx_NetWeight          = NettWeight;
                    cotTrans.cotrx_WeighBridgeFull    = 0M;
                    cotTrans.cotrx_WeighBridgeEmpty   = 0M;
                    cotTrans.cotrx_NettPerWB          = 0M;
                    cotTrans.cotrx_GrossAveBaleWeight = (GrossWeight / NoBales);
                    cotTrans.cottrx_NettAveBaleWeight = (NettWeight / NoBales);
                    cotTrans.cotrx_TranType           = TranType.TrxT_Pk;
                    cotTrans.cotrx_WriteOff           = true;

                    //---------------------------------------------
                    // Need to store this to the laydown file
                    //-----------------------------------------------------------
                    TLSPN_YarnOrderLayDown ld = new TLSPN_YarnOrderLayDown();
                    ld.YarnLD_BaleAvgWeight = cotTrans.cottrx_NettAveBaleWeight;
                    ld.YarnLD_Date          = dtpTransDate.Value;
                    ld.YarnLD_LayDownNo     = Convert.ToInt32(txtIssueToProdNo.Text);
                    ld.YarnLD_LotNo         = CottonRec.cotrx_LotNo;
                    ld.YarnLD_NoOfBales     = NoBales;
                    ld.YarnLD_WeightKg      = NettWeight;

                    try
                    {
                        context.TLSPN_CottonTransactions.Add(cotTrans);
                        context.TLSPN_YarnOrderLayDown.Add(ld);

                        context.SaveChanges();
                        SetUp();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                        success = false;
                    }
                }

                if (success)
                {
                    MessageBox.Show("Records stored successfully to database");
                }
            }
        }
Ejemplo n.º 7
0
        private void btnSave_Click(object sender, EventArgs e)
        {
            Button oBtn = sender as Button;

            if (oBtn != null && formloaded)
            {
                var errorM = core.returnMessage(MandSelected, true, MandatoryFields);
                if (!string.IsNullOrEmpty(errorM))
                {
                    MessageBox.Show(errorM);
                    return;
                }

                var cnt = RowsSelected.Where(x => x == false).Count();
                if (cnt == RowsSelected.Count())
                {
                    MessageBox.Show("Please select at least one row from the grid as shown");
                    return;
                }

                var CottonRec       = (TLSPN_CottonTransactions)cmbLotNo.SelectedItem;
                var ContractDetails = (TLADM_CottonContracts)cmbContractNo.SelectedItem;
                var CustomerDetails = (TLADM_CustomerFile)cmbCustomerName.SelectedItem;

                //Dont forget last number used
                //----------------------------------------------
                using (var context = new TTI2Entities())
                {
                    //Hard Coded at the moment
                    // See Table TLADM_TranactionType for a complete List of the Transaction Type Per Department
                    //--------------------------------------------------------------------------------------------------

                    var SequenceNo = Convert.ToInt32(txtDeliveryNo.Text);
                    var LastNumber = context.TLADM_LastNumberUsed.Find(1);
                    if (LastNumber != null)
                    {
                        LastNumber.col4 += 1;
                    }
                    // 0
                    // 1 Selected
                    // 2 Bales Numeric
                    // 3 MIC Decimal
                    // 4 kgs Decimal
                    // 5 Staple Decimal
                    int     NoBales = 0;
                    decimal Mass    = 0.00M;
                    decimal Gross   = 0.00M;

                    foreach (DataGridViewRow row in dataGridView1.Rows)
                    {
                        if (row.Cells[1].Value == null || (bool)row.Cells[1].Value == false)
                        {
                            continue;
                        }

                        TLSPN_CottonReceivedBales bales = context.TLSPN_CottonReceivedBales.Find((int)row.Cells[0].Value);
                        if (bales != null)
                        {
                            bales.CoBales_CottonSold     = true;
                            bales.CoBales_CottonSequence = SequenceNo;
                            NoBales += 1;
                            Mass    += (decimal)row.Cells[4].Value;
                            Gross   += (decimal)row.Cells[6].Value;
                            try
                            {
                                context.SaveChanges();
                            }
                            catch (Exception ex)
                            {
                                MessageBox.Show(ex.Message);
                            }
                        }
                    }

                    TLSPN_CottonTransactions cotTrans = new TLSPN_CottonTransactions();
                    var haul = (TLADM_CottonHauliers)comboTransporter.SelectedItem;

                    cotTrans.cotrx_TransDate          = dtpDateDelivered.Value;
                    cotTrans.cotrx_ContractNo_Fk      = ContractDetails.CottonCon_Pk;
                    cotTrans.cotrx_LotNo              = Convert.ToInt32(cmbLotNo.SelectedValue);
                    cotTrans.cotrx_NetWeight          = Mass;
                    cotTrans.cotrx_GrossWeight        = Mass;
                    cotTrans.cotrx_NoBales            = NoBales;
                    cotTrans.cotrx_Haulier_FK         = haul.Haul_Pk;
                    cotTrans.cotrx_VehReg             = txtVehReg.Text;
                    cotTrans.cotrx_WeighBridgeFull    = Convert.ToDecimal(txtWeighBridgeFull.Text);
                    cotTrans.cotrx_WeighBridgeEmpty   = Convert.ToDecimal(txtWeighBridgeEmpty.Text);
                    cotTrans.cotrx_NettPerWB          = cotTrans.cotrx_WeighBridgeFull - cotTrans.cotrx_WeighBridgeEmpty;
                    cotTrans.cottrx_NettAveBaleWeight = Mass / NoBales;
                    cotTrans.cotrx_GrossAveBaleWeight = Gross / NoBales;
                    cotTrans.cotrx_Return_No          = SequenceNo;
                    cotTrans.cotrx_Customer_FK        = CustomerDetails.Cust_Pk;
                    cotTrans.cotrx_Supplier_FK        = ContractDetails.CottonCon_ConSupplier_FK;
                    cotTrans.cotrx_Notes              = txtCustOrderNo.Text;
                    cotTrans.cotrx_WriteOff           = true;
                    //-------------------------------------------------------------------------
                    // Consult Table TLADM_TranTypes
                    //----------------------------------------------------------------------------
                    var DeptDetails = context.TLADM_Departments.Where(x => x.Dep_ShortCode == "SPIN").FirstOrDefault();
                    var Trantype    = context.TLADM_TranactionType.Where(x => x.TrxT_Number == 300 && x.TrxT_Department_FK == DeptDetails.Dep_Id).FirstOrDefault();
                    if (Trantype != null)
                    {
                        cotTrans.cotrx_TranType = Trantype.TrxT_Pk;
                    }

                    try
                    {
                        if (!_EditMode)
                        {
                            context.TLSPN_CottonTransactions.Add(cotTrans);
                        }

                        context.SaveChanges();
                        Setup();
                        frmViewReport vRep = new frmViewReport(5, SequenceNo);
                        int           h    = Screen.PrimaryScreen.WorkingArea.Height;
                        int           w    = Screen.PrimaryScreen.WorkingArea.Width;
                        vRep.ClientSize = new Size(w, h);
                        vRep.ShowDialog(this);
                        MessageBox.Show("Data saved to database successfully");
                        btnPickList.Enabled = false;
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                }

                btnSave.Enabled = false;
            }
        }
Ejemplo n.º 8
0
        private void btnSave_Click(object sender, EventArgs e)
        {
            bool   success = true;
            Button oBtn    = sender as Button;


            if (oBtn != null && formloaded)
            {
                var ErrorM = core.returnMessage(MandSelected, true, MandatoryFields);

                if (!string.IsNullOrEmpty(ErrorM))
                {
                    MessageBox.Show(ErrorM);
                    return;
                }

                using (var context = new TTI2Entities())
                {
                    TLSPN_CottonReceived cotrec = new TLSPN_CottonReceived();

                    var Contract  = (TLADM_CottonContracts)cmbCottonContracts.SelectedItem;
                    var Supplier  = (TLADM_Cotton)cmbCottonSuppliers.SelectedItem;
                    var Haulier   = (TLADM_CottonHauliers)cmbHaulier.SelectedItem;
                    var NoOfBales = Convert.ToInt32(txtNoOfBales.Text);


                    //---------------------------------------------------------------------------------------------------
                    //Unfortunately hard coded at the moment
                    //--------------------------------------------------------------------------------------------------
                    var DeptDetail = context.TLADM_Departments.Where(x => x.Dep_ShortCode == "SPIN").FirstOrDefault();
                    var TranType   = context.TLADM_TranactionType.Where(x => x.TrxT_Number == 100 && x.TrxT_Department_FK == DeptDetail.Dep_Id).FirstOrDefault();
                    if (TranType != null)
                    {
                        cotrec.CotReC_TranType_FK = TranType.TrxT_Pk;

                        var CountTrx = context.TLSPN_CottonTransactions.Count();
                        if (CountTrx == 0)
                        {
                            //--------------------------------------------
                            //this means that this transaction is being run for the first time ie add a record of zeros to the Opening Balance table
                            // This table will cater for future yearend
                            //--------------------------------------------------------------------------------
                            TLSPN_OpenBalance openBal = new TLSPN_OpenBalance();
                            openBal.OpenBal_Store_FK        = (int)TranType.TrxT_FromWhse_FK;
                            openBal.OpenBal_GrossBaleWeight = 0M;
                            openBal.OpenBal_NettBaleWeight  = 0M;
                            openBal.OpenBal_NoOfBales       = 0;

                            context.TLSPN_OpenBalance.Add(openBal);
                        }
                    }

                    //----------------------------------------------------------------
                    // Now onto the main transaction
                    //-------------------------------------------------------------------
                    TLSPN_CottonTransactions CTS = new TLSPN_CottonTransactions();
                    CTS.cotrx_ContractNo_Fk      = Contract.CottonCon_Pk;
                    CTS.cotrx_Customer_FK        = null;
                    CTS.cotrx_GrossWeight        = Convert.ToDecimal(txtSuppplierGrossWeight.Text);
                    CTS.cotrx_LotNo              = Convert.ToInt32(txtLotNo.Text);
                    CTS.cotrx_NettPerWB          = Convert.ToDecimal(txtCottonNettWeight.Text);
                    CTS.cotrx_NetWeight          = Convert.ToDecimal(txtSupplierNettWeight.Text);
                    CTS.cotrx_NoBales            = NoOfBales;
                    CTS.cotrx_Notes              = string.Empty;
                    CTS.cotrx_Return_No          = Convert.ToInt32(txtGrnNumber.Text);
                    CTS.cotrx_Supplier_FK        = Supplier.Cotton_Pk;
                    CTS.cotrx_TransDate          = dtpDateReceived.Value;
                    CTS.cotrx_TranType           = TranType.TrxT_Pk;
                    CTS.cotrx_VehReg             = txtVehReg.Text;
                    CTS.cotrx_Haulier_FK         = Haulier.Haul_Pk;
                    CTS.cotrx_WeighBridgeEmpty   = Convert.ToDecimal(txtWeighBridgeNett.Text);
                    CTS.cotrx_WeighBridgeFull    = Convert.ToDecimal(txtWeighBridgeGross.Text);
                    CTS.cotrx_WriteOff           = false;
                    CTS.cottrx_NettAveBaleWeight = Convert.ToDecimal(txtNetAvgBaleWeight.Text);
                    CTS.cotrx_GrossAveBaleWeight = Convert.ToDecimal(txtGrossAvgBaleWeight.Text);

                    context.TLSPN_CottonTransactions.Add(CTS);
                    //--------------------------------------------------------------------------------

                    var lastNumber = context.TLADM_LastNumberUsed.Find(1);
                    if (lastNumber != null)
                    {
                        lastNumber.col1  += 1;
                        lastNumber.col12 += 1;
                    }

                    try
                    {
                        context.SaveChanges();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }

                    if (microAvailNo.Checked)
                    {
                        decimal micraFrom  = 0.00M;
                        decimal micraTo    = 0.00M;
                        decimal micraAvg   = 0.00M;
                        decimal staplefrom = 0.00M;
                        decimal stapleto   = 0.00M;
                        decimal stapleAvg  = 0.00M;

                        var CottonContract = (TLADM_CottonContracts)cmbCottonContracts.SelectedItem;

                        if (CottonContract != null)
                        {
                            micraFrom = CottonContract.CottonCon_MicraFrom;
                            micraTo   = CottonContract.CottonCon_MicraTo;

                            staplefrom = CottonContract.CottonCon_StapleFrom;
                            stapleto   = CottonContract.CottonCon_StapleTo;

                            if (micraFrom > 0 && micraTo > 0)
                            {
                                micraAvg = (micraFrom + micraTo) / 2;
                            }
                            else
                            {
                                micraAvg = 1;
                            }
                            if (staplefrom != 0 && stapleto != 0)
                            {
                                stapleAvg = (staplefrom + stapleto) / 2;
                            }
                            else
                            {
                                stapleAvg = 1;
                            }
                        }
                        var n = 0;

                        while (++n <= CTS.cotrx_NoBales)
                        {
                            TLSPN_CottonReceivedBales recBales = new TLSPN_CottonReceivedBales();
                            recBales.CotBales_BaleNo         = n;
                            recBales.CotBales_CotReceived_FK = cotrec.CotRec_Pk;
                            recBales.CotBales_LotNo          = Convert.ToInt32(txtLotNo.Text);
                            recBales.CotBales_Mic            = micraAvg;
                            recBales.CotBales_Staple         = stapleAvg;
                            recBales.CotBales_Weight_Gross   = Convert.ToDecimal(txtGrossAvgBaleWeight.Text);
                            recBales.CotBales_Weight_Nett    = Convert.ToDecimal(txtNetAvgBaleWeight.Text);
                            recBales.CoBales_CottonSequence  = Convert.ToInt32(txtGrnNumber.Text);

                            context.TLSPN_CottonReceivedBales.Add(recBales);

                            try
                            {
                                context.SaveChanges();
                            }
                            catch (Exception ex)
                            {
                                MessageBox.Show(ex.Message);
                            }
                        }
                    }
                }
                if (success)
                {
                    MessageBox.Show("Data stored to the database successfully");
                    int           returnno = Convert.ToInt32(txtGrnNumber.Text);
                    frmViewReport vRep     = new frmViewReport(2, returnno);
                    int           h        = Screen.PrimaryScreen.WorkingArea.Height;
                    int           w        = Screen.PrimaryScreen.WorkingArea.Width;
                    vRep.ClientSize = new Size(w, h);
                    vRep.ShowDialog(this);
                    MandSelected = core.PopulateArray(MandatoryFields.Length, false);
                    Setup();
                }
            }
        }