Click here to Skip to main content
15,892,674 members
Articles / Desktop Programming / Windows Forms

Internal Supply Chain Management System, Visibility

Rate me:
Please Sign up or sign in to vote.
4.78/5 (30 votes)
9 Oct 2009CPOL7 min read 106.1K   16.5K   108  
Internal supply chain management system's objective is to visualize organization's activities and events spawn during its work flow and offers panoramic view of upstream and downstream activities
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace ISCMS.Dialog
{
    public partial class Job : Hasan.FormCustom
    {
        MainForm mainForm;
        public DataSet dsTransaction;
        public Job()
        {
            InitializeComponent();
            CreateTables();
        }


        public Job(MainForm m_mainForm)
        {
            InitializeComponent();
            mainForm = m_mainForm;
            CreateTables();
            this.SetStyle(ControlStyles.UserPaint |
              ControlStyles.AllPaintingInWmPaint |
              ControlStyles.ResizeRedraw |
              ControlStyles.ContainerControl |
              ControlStyles.OptimizedDoubleBuffer |
              ControlStyles.SupportsTransparentBackColor
              , true);
           
        }

        void CreateTables()
        {
            // Transaction
            dsTransaction = new DataSet();
            dsTransaction.Tables.Add("tblTransaction");
            dsTransaction.Tables["tblTransaction"].Columns.Add("Goods_ID", System.Type.GetType("System.Int32"));
            dsTransaction.Tables["tblTransaction"].Columns.Add("Is_Raw_Material", System.Type.GetType("System.Boolean"));
            dsTransaction.Tables["tblTransaction"].Columns.Add("Date", System.Type.GetType("System.String"));
            dsTransaction.Tables["tblTransaction"].Columns.Add("Last_Transaction_ID", System.Type.GetType("System.Int32"));
            dsTransaction.Tables["tblTransaction"].Columns.Add("Transaction_Status_ID", System.Type.GetType("System.Int32"));
            dsTransaction.Tables["tblTransaction"].Columns.Add("Transaction_Type_ID", System.Type.GetType("System.Int32"));
            dsTransaction.Tables["tblTransaction"].Columns.Add("Is_Job", System.Type.GetType("System.Boolean"));
            dsTransaction.Tables["tblTransaction"].Columns.Add("Job_ID", System.Type.GetType("System.Int32"));
            dsTransaction.Tables["tblTransaction"].Columns.Add("Unit", System.Type.GetType("System.Int32"));
            dsTransaction.Tables["tblTransaction"].Columns.Add("Comments", System.Type.GetType("System.String"));
            dsTransaction.Tables["tblTransaction"].Columns.Add("Job_Transaction_ID", System.Type.GetType("System.Int32"));
            dsTransaction.Tables["tblTransaction"].Columns.Add("Is_Active", System.Type.GetType("System.Boolean"));
            dsTransaction.Tables["tblTransaction"].Columns.Add("Cost", System.Type.GetType("System.Double"));
            dsTransaction.Tables["tblTransaction"].Columns.Add("Unit_Cost", System.Type.GetType("System.Double"));
            


        }


        void CalculateCost()
        {
            double totalCost = Math.Round( (txtMaterialCost.Text_AsDouble 
                + txtLabour_Cost.Text_AsDouble 
                + txtManufactureing_Overhead.Text_AsDouble),2);
            string unitFinished_Goods = "0" +   dsTransaction.Tables["tblTransaction"].Compute(" sum(Unit) ", "Is_Raw_Material = false ") ;
            txtUnit_Cost.Text = string.Format("{0:c}", ( totalCost / Convert.ToUInt32( unitFinished_Goods )  )  );
            txtTotal_Cost.Text = string.Format("{0:c}", totalCost);
        }


        public void ReloadGoods()
        {
            DataRow[] rows = dsTransaction.Tables["tblTransaction"].Select("Is_Raw_Material ");
            if (dsTransaction.Tables["tblTransaction"].Select().Length != 0)
            {
                cmbStatus.Enabled = false;
            }
            else
            {
                cmbStatus.Enabled = true;
            }

            txtMaterialCost.Text = string.Format("{0:c}",dsTransaction.Tables["tblTransaction"].Compute(" sum(cost) "  ,"Is_Raw_Material ") );
            CalculateCost();
            int i =0;
            string goods = "";
            listRaw_Material.Items.Clear();
            for (i = 0; i < rows.Length; i++)
            {
                goods = MainForm.dsData.Tables["Goods"].Select(" Goods_ID = " + rows[i]["Goods_ID"].ToString())[0]["Name"].ToString(); 
                listRaw_Material.Items.Add( goods );
            }

            rows = dsTransaction.Tables["tblTransaction"].Select(" Is_Raw_Material = false ");
            listFinished_Goods.Items.Clear();
            for (i = 0; i < rows.Length; i++)
            {
                goods = MainForm.dsData.Tables["Goods"].Select(" Goods_ID = " + rows[i]["Goods_ID"].ToString())[0]["Name"].ToString();
                listFinished_Goods.Items.Add(goods);
            }

            if (rows.Length == 1)
                btnAddFinished_Goods.Enabled = false;
            else
                btnAddFinished_Goods.Enabled = true;
        }

        private void SetDuration()
        {
            
            string sStart_Date = Start_Date.Value.ToShortDateString() + "  " + Start_Time.Value.ToShortTimeString();
            string sEnd_Date = End_Date.Value.ToShortDateString() + "  " +  End_Timer.Value.ToShortTimeString();
            TimeSpan ts = DateTime.Parse(sEnd_Date) - DateTime.Parse(sStart_Date);
            lblDuration.Text = "";
            if (ts.Days != 0)
            {
                lblDuration.Text += ts.Days.ToString() + " days ";
            }

            if (ts.Hours != 0)
            {
                lblDuration.Text += ts.Hours.ToString() + " hour ";
            }

            if (ts.Minutes != 0)
            {
                lblDuration.Text += ts.Minutes.ToString() + " min.";
            }

            if( ts.Days == 0 && ts.Hours == 0 && ts.Minutes == 0 )
                lblDuration.Text = "#######";
        }

        void EnableSave(bool bol)
        {
            Save.Enabled = bol;
            Cancel.Enabled = bol;
        }


        private void Save_Click(object sender, EventArgs e)
        {
            EnableSave(false);
            string sStart_Date = Start_Date.Value.ToShortDateString() + "  " + Start_Time.Value.ToShortTimeString();
            string sEnd_Date = End_Date.Value.ToShortDateString() + "  " + End_Timer.Value.ToShortTimeString();
            TimeSpan ts = DateTime.Parse(sEnd_Date) - DateTime.Parse(sStart_Date)  ;

            if (ts.Days < 0)
            {
                lblErrorEnd_Date.Visible = true;
                End_Date.Focus();
                EnableSave(true);
                return;
            }
            
            if (ts.Days == 0)
            {
                // no date and time difference
                if (ts.Hours == 0)
                {
                    lblErrorEnd_Date_Time.Visible = true;
                    End_Timer.Focus();
                    EnableSave(true);
                    return;
                }
                if (ts.Hours < 0)
                {
                    lblErrorEnd_Date_Time.Visible = true;
                    End_Timer.Focus();
                    EnableSave(true);
                    return;
                }
                
            }

            if (Convert.ToUInt32(cmbStatus.SelectedValue) == 0)
            {
                lblErrorStatus.Visible = true;
                cmbStatus.Focus();
                EnableSave(true);
                return;
            }

            if (txtComments.Text.Trim().Length < 3)
            {
                lblErrorComments.Visible = true;
                txtComments.Focus();
                EnableSave(true);
                return;
            }

            if (listRaw_Material.Items.Count == 0)
            {
                lblErrorRaw_Material.Visible = true;
                EnableSave(true);
                return;
            }

            if (listFinished_Goods.Items.Count == 0)
            {
                lblErrorFinished_Goods.Visible = true;
                EnableSave(true);
                return;
            }

            int job_ID = 0;
            DataAccess.Access ac = new ISCMS.DataAccess.Access();
            ac.InsertJob(sStart_Date, sEnd_Date, Convert.ToInt32(cmbStatus.SelectedValue), 
                txtComments.Text,Convert.ToInt32(txtLabour_Cost.Text_AsDouble),
                Convert.ToInt32(txtManufactureing_Overhead.Text_AsDouble ) );
            job_ID = ac.GetLastJobID(); 

            DataRow[] rows = dsTransaction.Tables["tblTransaction"].Select("Is_Raw_Material");
            for(int i=0;i<rows.Length;i++)
            {
                ac.InsertTransaction(Convert.ToInt32(rows[i]["Goods_ID"]), Convert.ToBoolean(rows[i]["Is_Raw_Material"]), sStart_Date, 0,
                    Convert.ToInt32(rows[i]["Transaction_Status_ID"]), Convert.ToInt32(rows[i]["Transaction_Type_ID"]), true, job_ID,
                    Convert.ToInt32(rows[i]["Unit"]), rows[i]["Comments"].ToString(), 0, true, Convert.ToInt32(rows[i]["Cost"]), Convert.ToSingle(rows[i]["Unit_Cost"]));
            }

            rows = dsTransaction.Tables["tblTransaction"].Select("Is_Raw_Material = false ");
            for (int i = 0; i < rows.Length; i++)
            {
                ac.InsertTransaction(Convert.ToInt32(rows[i]["Goods_ID"]), Convert.ToBoolean(rows[i]["Is_Raw_Material"]), sStart_Date, 0,
                    Convert.ToInt32(rows[i]["Transaction_Status_ID"]), Convert.ToInt32(rows[i]["Transaction_Type_ID"]), true, job_ID,
                    Convert.ToInt32(rows[i]["Unit"]), rows[i]["Comments"].ToString(), 0, true, Convert.ToInt32(txtTotal_Cost.Text_AsDouble), Convert.ToSingle(txtUnit_Cost.Text_AsDouble));
            }




            string str = Hasan.CMessageBoxContinueOk.ShowBox("Job has been saved\n to continue press continue otherwise Done", (int)Hasan.CMessageBox.Type.Done);

            if (str == "Continue")
            {
                Job_Load(new object(), new EventArgs());
                ResetForm();
                EnableSave(true);
                Start_Date.Focus();
                return;
            }
            else
            {
                mainForm.Load_Click(new object(), new EventArgs());
                this.Close();
            }
        }


        void ResetForm()
        {
            Start_Date.Value = DateTime.Now;
            Start_Time.Value = DateTime.Now;
            End_Date.Value = DateTime.Now;
            End_Timer.Value = DateTime.Now;

            cmbStatus.SelectedIndex = 0;
            txtComments.Text = "";
            txtLabour_Cost.Text = string.Format("{0:c}", 0);
            txtManufactureing_Overhead.Text = string.Format("{0:c}", 0);

            dsTransaction.Tables["tblTransaction"].Clear();
            ReloadGoods();
        }



        private void Cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnAddRaw_Material_Click(object sender, EventArgs e)
        {
            lblErrorRaw_Material.Visible = false;
            if (cmbStatus.SelectedIndex == 0)
            {
                Hasan.CMessageBoxSmall.ShowBox("Please, select job's status", (int)Hasan.CMessageBox.Type.Exclamation,1,1);
                cmbStatus.Focus();
                return;
            }

            int m_Transaction_Type_ID = 0;
            int m_Transaction_Status_ID = 0;

            // Job Status Is Expected
            if (Convert.ToInt32(cmbStatus.SelectedValue) == 2)
            {
                m_Transaction_Type_ID = 2;
                m_Transaction_Status_ID = 2;
            }
            else
            {
                m_Transaction_Type_ID = 2;
                m_Transaction_Status_ID = 3;
            }

            ISCMS.Dialog.Transaction dl = new ISCMS.Dialog.Transaction(mainForm, m_Transaction_Status_ID, m_Transaction_Type_ID, true, true, 0, this,Start_Date.Value.ToShortDateString());
            DisableBackGround();
            dl.ShowDialog();
            EnableBackGround();
        }

        private void btnAddFinished_Goods_Click(object sender, EventArgs e)
        {
            lblErrorFinished_Goods.Visible = false;
            if (cmbStatus.SelectedIndex == 0)
            {
                Hasan.CMessageBoxSmall.ShowBox("Please, select job's status", (int)Hasan.CMessageBox.Type.Exclamation, 1, 1);
                cmbStatus.Focus();
                return;
            }

            int m_Transaction_Type_ID = 0;
            int m_Transaction_Status_ID = 0;


            // Job Status Is Expected
            if (Convert.ToInt32(cmbStatus.SelectedValue) == 2)
            {
                m_Transaction_Type_ID = 1;
                m_Transaction_Status_ID = 2;
            }
            else
            {
                m_Transaction_Type_ID = 1;
                m_Transaction_Status_ID = 3;
            }

            ISCMS.Dialog.Transaction dl = new ISCMS.Dialog.Transaction(mainForm, m_Transaction_Status_ID, m_Transaction_Type_ID, false, true, 0, this, Start_Date.Value.ToShortDateString());
            DisableBackGround();
            dl.ShowDialog();
            EnableBackGround();
        }

        private void Job_Load(object sender, EventArgs e)
        {
            cmbStatus.Init(MainForm.dsData, "Transaction_Status", "Transaction_Status_Name", "Transaction_Status_ID", 3, " Transaction_Status_ID = 2 or Transaction_Status_ID = 3 ");
            this.cmbStatus.SelectedValueChanged += new System.EventHandler(this.cmbStatus_SelectedValueChanged);
        }

        protected void DisableBackGround()
        {
           // panel1.BackColor = System.Drawing.SystemColors.AppWorkspace;
        }

        protected void EnableBackGround()
        {
            //panel1.BackColor = System.Drawing.SystemColors.Control;
        }

        private void Clear_All_Click(object sender, EventArgs e)
        {
            dsTransaction.Tables["tblTransaction"].Clear();
            ReloadGoods();
        }

        private void Clear_Raw_Material_Click(object sender, EventArgs e)
        {
            DataRow[] rows = dsTransaction.Tables["tblTransaction"].Select("Is_Raw_Material ");
            for( int i=0;i<rows.Length;i++)
                dsTransaction.Tables["tblTransaction"].Rows.Remove(rows[i]);
            dsTransaction.AcceptChanges();
            ReloadGoods();
        }

        private void Clear_Finished_Goods_Click(object sender, EventArgs e)
        {
            DataRow[] rows = dsTransaction.Tables["tblTransaction"].Select("Is_Raw_Material = False or Is_Dual  ");
            for (int i = 0; i < rows.Length; i++)
                dsTransaction.Tables["tblTransaction"].Rows.Remove(rows[i]);
            dsTransaction.AcceptChanges();
            ReloadGoods();
        }

        private void Start_Date_ValueChanged(object sender, EventArgs e)
        {
            lblErrorStart_Date.Visible = false;
            SetDuration();
        }

        private void Start_Time_ValueChanged(object sender, EventArgs e)
        {
            lblErrorStart_Date_Time.Visible = false;
            SetDuration();
        }

        private void End_Date_ValueChanged(object sender, EventArgs e)
        {
            lblErrorEnd_Date.Visible = false;
            SetDuration();
        }

        private void End_Time_ValueChanged(object sender, EventArgs e)
        {
            lblErrorEnd_Date_Time.Visible = false;
            SetDuration();
        }

        private void cmbStatus_SelectedValueChanged(object sender, EventArgs e)
        {
            lblErrorStatus.Visible = false;
            SetDuration();

            if( cmbStatus.SelectedValue.ToString().Length == 0 )
                return;

            switch (Convert.ToInt32(cmbStatus.SelectedValue))
            {
                case 0:
                    Header.Text = "##########";
                    Header.BackColor = System.Drawing.SystemColors.Control;
                    Header.ForeColor = Color.White;
                    this.BackColor = Color.DarkGray;
                    break;

                case 2:
                    Header.Text = "Pending Job";
                    Header.BackColor = Color.Orange;
                    Header.ForeColor = Color.White;
                    this.BackColor = Color.Orange;
                    break;

                case 3:
                    Header.Text = "Initiate Job";
                    Header.BackColor = Color.Green;
                    Header.ForeColor = Color.White;
                    this.BackColor = Color.Green;
                    break;
            }

        }

        private void txtComments_TextChanged(object sender, EventArgs e)
        {
            lblErrorComments.Visible = false;
        }

        private void txtLabour_Cost_TextChanged(object sender, EventArgs e)
        {
            CalculateCost();
        }

        private void txtManufactureing_Overhead_TextChanged(object sender, EventArgs e)
        {
            CalculateCost();
        }

        private void Job_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == System.Windows.Forms.Keys.Escape)
            {
              //  this.Close();
                return;
            }
        }

     
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Pakistan Pakistan
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.
This is a Organisation

33 members

Comments and Discussions