示例#1
0
 /// <summary>
 /// Handles the Click event of the ViewJobIInfo_button control.
 /// </summary>
 /// <param name="sender">The source of the event.</param>
 /// <param name="e">The <see cref="RoutedEventArgs"/> instance containing the event data.</param>
 private void ViewJobIInfo_button_Click(object sender, RoutedEventArgs e)
 {
     try
     {
         DataRowView row = (DataRowView)dataGrid1.SelectedItems[0];
         string selected = row["מספר עבודה"].ToString();
         ManagerJobInfoGui MJIG = new ManagerJobInfoGui(selected);
         MJIG.Show();
         Login.close = 1;
         this.Close();
     }//end try
     catch { MessageBox.Show("לא נבחרה עבודה לצפיה", "!שים לב", MessageBoxButton.OK, MessageBoxImage.Error); }
 }
        /// <summary>
        /// Handles the Click event of the Add_Existing_button control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="RoutedEventArgs"/> instance containing the event data.</param>
        private void Add_Existing_button_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                DataTable changedRecordsItemsTable = dt.GetChanges();
                int sizeofItemsnewtable = changedRecordsItemsTable.Rows.Count;
                int count = 0; // will count the number of rows with cell "" in the  changedRecordsItemsTable.
                Console.WriteLine("sizeofItemsnewtable = " + sizeofItemsnewtable);

                // test the input
                foreach (DataRow testrow in changedRecordsItemsTable.Rows)// for every row in the updateds table.
                {
                    string q = testrow["כמות"].ToString();
                    Console.WriteLine("q = " + q);
                    try
                    {

                        if (q != "") // in case the user deleted a cell in the item and now it have a string of-  "" .
                        {
                            int new_item_quantity = Convert.ToInt32(q);
                            if (new_item_quantity > 0)
                            {
                                Console.WriteLine("הקלט היה טוב במקט מספר =" + testrow["מקט פריט"].ToString());
                            }//end if (item_quantity > 0)
                            else
                            {
                                MessageBox.Show("שדה הכמות מכיל כמות שלילית או 0 במקט פריט - " + testrow["מקט פריט"].ToString() + "", "!שים לב", MessageBoxButton.OK, MessageBoxImage.Error);
                                return;
                            }

                        } // if (q != "")
                        else { count++; }

                    }// end try
                    catch
                    {
                        MessageBox.Show("שדה הכמות לא כולל רק מספרים במקט פריט - " + testrow["מקט פריט"].ToString() + "", "!שים לב", MessageBoxButton.OK, MessageBoxImage.Error);
                        return;
                    }

                } // end of foreach to test the input
                if (count == changedRecordsItemsTable.Rows.Count)
                {
                    MessageBox.Show("  לא נבחרו פריטים מהטבלה ", "!שים לב", MessageBoxButton.OK, MessageBoxImage.Error);
                    return;
                }

                // if the unpute was OK then we can add the items.
                count = 0;
                foreach (DataRow dri in changedRecordsItemsTable.Rows)// for every row in the updateds table.
                {
                    string q = dri["כמות"].ToString();
                    Console.WriteLine("q = " + q);
                    string itemid = dri["מקט פריט"].ToString();
                    Console.WriteLine("itemid = " + itemid);
                    //string cost_itemid = dri["מקט לקוח"].ToString();
                    try
                    {

                        if (q != "") // in case the user deleted a cell in the item and now it have a string of-  "" .
                        {
                            int new_item_quantity = Convert.ToInt32(q) , maxItemNum=0;
                            if (new_item_quantity > 0)
                            {
                                string  itemStatus = "רישום", itemStageOrder = "1";
                                try
                                {
                                    MySqlConnection MySqlConn = new MySqlConnection(Login.Connectionstring);
                                    MySqlConn.Open();
                                    string Query1 = "(SELECT *, MAX(itemNum) FROM project.jobs WHERE jobid='" + jobID + "' AND itemid='" + itemid + "')";
                                    Console.WriteLine("שורה 267 Query1 = " + Query1);
                                    MySqlCommand crcommand1 = new MySqlCommand(Query1, MySqlConn);
                                    crcommand1.ExecuteNonQuery();
                                    MySqlDataReader dr1 = crcommand1.ExecuteReader();
                                    int count1 = 0, expectedItemQuantity=0;
                                    string costumerid = "", itemsDescription = "", job_status = "", jobdescription = "", startDate = "", expectedFinishDate = "", contact_id = "", orderid = "", group_Status = "", group_StageOrder = "", deliveryid = "", invoiceNumber = "", group_costomer_itemid = "", group_itemToFixStageOrder = "", reg_date="";

                                    while (dr1.Read())
                                    {
                                        if (!dr1.IsDBNull(0))
                                        {
                                            count1++; // inc count only if there are already items with this itemid for this jobid.
                                            Console.WriteLine("שורה 279 count1 = " + count1);
                                            maxItemNum = dr1.GetInt32(25); // get the added MAX(itemNum) column
                                            Console.WriteLine("שורה 281 maxItemNum = " + maxItemNum);
                                            expectedItemQuantity = dr1.GetInt32(5);
                                            Console.WriteLine("שורה 283 expectedItemQuantity = " + expectedItemQuantity);
                                            costumerid = dr1.GetString(6);
                                            Console.WriteLine("שורה 285 costumerid = " + costumerid);
                                            itemsDescription = dr1.GetString(8);
                                            Console.WriteLine("שורה 287 itemsDescription = " + itemsDescription);
                                            job_status = dr1.GetString(10);
                                            Console.WriteLine("שורה 289 job_status = " + job_status);
                                            jobdescription = dr1.GetString(11);
                                            Console.WriteLine("שורה 291 jobdescription = " + jobdescription);
                                            startDate = dr1.GetString(12);
                                            Console.WriteLine("שורה 293 startDate = " + startDate);
                                            startDate = Convert.ToDateTime(startDate).ToString("yyyy-MM-dd");
                                            Console.WriteLine("שורה 295 startDate = " + startDate);
                                            expectedFinishDate = dr1.GetString(13);
                                            Console.WriteLine("שורה 297 expectedFinishDate = " + expectedFinishDate);
                                            expectedFinishDate = Convert.ToDateTime(expectedFinishDate).ToString("yyyy-MM-dd");
                                            Console.WriteLine("שורה 299 expectedFinishDate = " + expectedFinishDate);
                                            contact_id = dr1.GetString(15);
                                            Console.WriteLine("שורה 301 contact_id = " + contact_id);
                                            orderid = dr1.GetString(16);
                                            Console.WriteLine("שורה 303 orderid = " + orderid);
                                            group_Status = dr1.GetString(17);
                                            Console.WriteLine("שורה 305 group_Status = " + group_Status);
                                            group_StageOrder = dr1.GetString(18);
                                            Console.WriteLine("שורה 307 group_StageOrder = " + group_StageOrder);
                                            deliveryid = dr1.GetString(19);
                                            Console.WriteLine("שורה 309 deliveryid = " + deliveryid);
                                            invoiceNumber = dr1.GetString(20);
                                            Console.WriteLine("שורה 311 invoiceNumber = " + invoiceNumber);
                                            group_costomer_itemid = dr1.GetString(21);
                                            Console.WriteLine("שורה 312 group_costomer_itemid = " + group_costomer_itemid);
                                            group_itemToFixStageOrder= dr1.GetString(22);
                                            Console.WriteLine("שורה 315 group_itemToFixStageOrder = " + group_itemToFixStageOrder);
                                            reg_date = dr1.GetString(24);
                                            Console.WriteLine("שורה 317 reg_date = " + reg_date);
                                            reg_date = Convert.ToDateTime(reg_date).ToString("yyyy-MM-dd");
                                            Console.WriteLine("שורה 319 reg_date = " + reg_date);
                                        }
                                    }
                                    MySqlConn.Close();
                                    Console.WriteLine("שורה 323 לפני בדיקה האם המקט כבר קיים");
                                    if (count1 == 1)// if an itemid already exist in this job.
                                    {
                                      //  int new_expected = new_item_quantity + expectedItemQuantity;
                                        for (int i = 1; i <= new_item_quantity; i++)
                                        {
                                            Console.WriteLine("שורה 329 if (count1 == 1)// if an itemid already exist in this job.");
                                            maxItemNum++;
                                          //  string itemid = dri["מקט פריט"].ToString();
                                            try
                                            {
                                                MySqlConnection MySqlConn1 = new MySqlConnection(Login.Connectionstring);
                                                MySqlConn1.Open();
                                                CheckSingleQuotationMark CSQ = new CheckSingleQuotationMark();
                                                string Query2 = ("INSERT INTO project.jobs (jobid, itemid,itemNum,itemStatus,itemStageOrder, expectedItemQuantity,costumerid, itemsDescription, job_status, jobdescription, startDate, expectedFinishDate, contact_id , orderid , group_Status , group_StageOrder , deliveryid , invoiceNumber , group_costomer_itemid, group_itemToFixStageOrder, reg_date ) VALUES ('" + jobID + "','" + CSQ.checkForSingleQuotationMark(itemid) + "','" + maxItemNum + "','" + itemStatus + "','" + itemStageOrder + "','" + expectedItemQuantity + "','" + costumerid + "','" + CSQ.checkForSingleQuotationMark(itemsDescription) + "','" + job_status + "','" + CSQ.checkForSingleQuotationMark(jobdescription) + "','" + startDate + "','" + expectedFinishDate + "','" + contact_id + "','" + orderid + "','" + group_Status + "','" + group_StageOrder + "','" + deliveryid + "','" + invoiceNumber + "','" + group_costomer_itemid + "','" + group_itemToFixStageOrder + "','" + reg_date + "')");
                                                Console.WriteLine("שורה 377 Query2 = " + Query2);
                                                MySqlCommand MSQLcrcommand2 = new MySqlCommand(Query2, MySqlConn1);
                                                MSQLcrcommand2.ExecuteNonQuery();
                                                MySqlDataAdapter mysqlDAdp1 = new MySqlDataAdapter(MSQLcrcommand2);
                                                MySqlConn1.Close();
                                            }
                                            catch (Exception ex)
                                            {
                                                MessageBox.Show(ex.Message);
                                                Console.WriteLine("if (for (int i = 1; i <= new_item_quantity; i++))");
                                                return;
                                            }

                                        }//end for (int i = 1; i <= new_item_quantity; i++)

                                    } //  end of if (count1 == 1)// if an itemid already exist in this job.

                                    else // cound1==0 so no such itemid for this jobid.
                                    {
                                        try
                                        {
                                            Console.WriteLine("שורה 373 cound1==0 so no such itemid for this jobid. ");
                                            MySqlConnection MySqlConn5 = new MySqlConnection(Login.Connectionstring);
                                            MySqlConn5.Open();
                                            string Query5 = "(SELECT *, MAX(itemNum) FROM project.jobs WHERE jobid='" + jobID + "')";
                                            Console.WriteLine("שורה 377 Query5 = " + Query5);
                                            MySqlCommand crcommand5 = new MySqlCommand(Query5, MySqlConn5);
                                            crcommand5.ExecuteNonQuery();
                                            MySqlDataReader dr5 = crcommand5.ExecuteReader();
                                            while (dr5.Read())
                                            {
                                                expectedItemQuantity = dr5.GetInt32(5);
                                                Console.WriteLine("שורה 384 expectedItemQuantity = " + expectedItemQuantity);
                                                costumerid = dr5.GetString(6);
                                                Console.WriteLine("שורה 386 costumerid = " + costumerid);
                                                itemsDescription = dr5.GetString(8);
                                                Console.WriteLine("שורה 388 itemsDescription = " + itemsDescription);
                                                job_status = dr5.GetString(10);
                                                Console.WriteLine("שורה 390 job_status = " + job_status);
                                                jobdescription = dr5.GetString(11);
                                                Console.WriteLine("שורה 392 jobdescription = " + jobdescription);
                                                startDate = dr5.GetString(12);
                                                Console.WriteLine("שורה 394 startDate = " + startDate);
                                                startDate = Convert.ToDateTime(startDate).ToString("yyyy-MM-dd");
                                                Console.WriteLine("שורה 396 startDate = " + startDate);
                                                expectedFinishDate = dr5.GetString(13);
                                                Console.WriteLine("שורה 398 expectedFinishDate = " + expectedFinishDate);
                                                expectedFinishDate = Convert.ToDateTime(expectedFinishDate).ToString("yyyy-MM-dd");
                                                Console.WriteLine("שורה 400 expectedFinishDate = " + expectedFinishDate);
                                                contact_id = dr5.GetString(15);
                                                Console.WriteLine("שורה 402 contact_id = " + contact_id);

                                                orderid = dr5.GetString(16);
                                                Console.WriteLine("שורה 405 orderid = " + orderid);
                                                deliveryid = dr5.GetString(19);
                                                Console.WriteLine("שורה 407 deliveryid = " + deliveryid);
                                                invoiceNumber = dr5.GetString(20);
                                                Console.WriteLine("שורה 409 invoiceNumber = " + invoiceNumber);
                                                reg_date = dr5.GetString(24);
                                                Console.WriteLine("שורה 411 reg_date = " + reg_date);
                                                reg_date = Convert.ToDateTime(reg_date).ToString("yyyy-MM-dd");
                                                Console.WriteLine("שורה 413 reg_date = " + reg_date);
                                            }
                                            MySqlConn5.Close();
                                        }
                                        catch (Exception ex)
                                        {
                                            MessageBox.Show(ex.Message);
                                            Console.WriteLine("שורה 423");
                                            return;
                                        }

                                        int itemNum = 0;
                                        for (int i = 1; i <= new_item_quantity; i++)
                                        {
                                            itemNum++;
                                            Console.WriteLine("שורה 430 itemNum = " + itemNum);
                                            try
                                            {
                                                MySqlConnection MySqlConn4 = new MySqlConnection(Login.Connectionstring);
                                                MySqlConn4.Open();
                                                CheckSingleQuotationMark CSQ = new CheckSingleQuotationMark();
                                                string Query4 = ("INSERT INTO project.jobs (jobid, itemid,itemNum,itemStatus,itemStageOrder, expectedItemQuantity,costumerid, itemsDescription, job_status, jobdescription, startDate, expectedFinishDate, contact_id , orderid  , deliveryid , invoiceNumber , reg_date) VALUES ('" + jobID + "','" + CSQ.checkForSingleQuotationMark(itemid) + "','" + itemNum + "','" + itemStatus + "','" + itemStageOrder + "','" + new_item_quantity + "','" + costumerid + "','" + CSQ.checkForSingleQuotationMark(itemsDescription) + "','" + job_status + "','" + CSQ.checkForSingleQuotationMark(jobdescription) + "','" + startDate + "','" + expectedFinishDate + "','" + contact_id + "','" + orderid + "','" + deliveryid + "','" + invoiceNumber + "','" + reg_date + "')");
                                                Console.WriteLine("שורה 436 Query4 = " + Query4);
                                                MySqlCommand MSQLcrcommand4 = new MySqlCommand(Query4, MySqlConn4);
                                                MSQLcrcommand4.ExecuteNonQuery();
                                                MySqlDataAdapter mysqlDAdp4 = new MySqlDataAdapter(MSQLcrcommand4);
                                                MySqlConn4.Close();

                                            }
                                            catch (Exception ex)
                                            {
                                                MessageBox.Show(ex.Message);
                                                Console.WriteLine("שורה 445");
                                                return;
                                            }

                                        } // end for (int i = 1; i <= new_item_quantity; i++)
                                    }// end else // cound1==0 so no such itemid for this jobid.

                                }// end of first try.
                                catch (Exception ex)
                                {
                                    MessageBox.Show(ex.Message);
                                    Console.WriteLine("Query1");
                                    return;
                                }

                            }//end if (item_quantity > 0)
                            else
                            {
                                MessageBox.Show("שדה הכמות מכיל כמות שלילית או 0 בפריט מספר - " + dri["מקט פריט"].ToString() + "" , "!שים לב", MessageBoxButton.OK, MessageBoxImage.Error);
                                return;
                            }

                        } // if (q != "")
                        else { count++; }

                    }// end try
                    catch
                    {
                        MessageBox.Show("שדה הכמות לא כולל רק מספרים בפריט מספר - " + dri["מקט פריט"].ToString() + "", "!שים לב", MessageBoxButton.OK, MessageBoxImage.Error);
                        return;
                    }

                }// end foreach (DataRow dri in changedRecordsTable.Rows)}

                if (count == changedRecordsItemsTable.Rows.Count)
                {
                    MessageBox.Show("  לא נבחרו פריטים מהטבלה ", "!שים לב", MessageBoxButton.OK, MessageBoxImage.Error); return;
                }
                else
                {
                    MessageBox.Show(".!הפריט/ים נוסף/ו לעבודה", "!הצלחה", MessageBoxButton.OK, MessageBoxImage.Information);
                    ManagerJobInfoGui MJIG = new ManagerJobInfoGui(jobID);
                    MJIG.Show();
                    Login.close = 1;
                    this.Close();
                }
            }
            catch
            {
                MessageBox.Show("לא נבחרו פריטים", "!שים לב", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
        /*
        private void ADD_Btn_Click(object sender, RoutedEventArgs e)
        {
          //  ManagerAddNewItemGUI MANIG = new ManagerAddNewItemGUI(jobID);
          //  MANIG.Show();
          //  this.Close();
        }

        */
        // go to previous screen.
        /// <summary>
        /// Handles the Click event of the Back_Btn control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="RoutedEventArgs"/> instance containing the event data.</param>
        private void Back_Btn_Click(object sender, RoutedEventArgs e)
        {
            ManagerJobInfoGui MJIG = new ManagerJobInfoGui(jobID);
            MJIG.Show();
            Login.close = 1;
            this.Close();
        }
        /// <summary>
        /// Handles the Click event of the DeleteBtn control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="RoutedEventArgs"/> instance containing the event data.</param>
        private void DeleteBtn_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                //System.Collections.IList rows = dataGrid1.SelectedItems;
                DataRowView row = (DataRowView)dataGrid1.SelectedItems[0];
                if (MessageBox.Show("?האם אתה בטוח שברצונך למחוק פריט זה", "וידוא מחיקה", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.No)
                {
                    return;
                }
                else // if the user clicked on "Yes" so he wants to Delete.
                {
                    string itemnum = row["מספר פריט בסט"].ToString();
                    int thecountinitemid = 0;
                    int thecountinjobid = 0;

                    // see if this is the last item in this itemid
                    try
                    {
                        MySqlConnection MySqlConn = new MySqlConnection(Login.Connectionstring);
                        MySqlConn.Open();
                        string Query1 = "select COUNT(itemNum) from jobs where itemid='" + itemID + "' and   jobid='" + jobID + "' ";
                        MySqlCommand MSQLcrcommand1 = new MySqlCommand(Query1, MySqlConn);
                        MSQLcrcommand1.ExecuteNonQuery();
                        MySqlDataAdapter mysqlDAdp = new MySqlDataAdapter(MSQLcrcommand1);
                        MySqlDataReader dr = MSQLcrcommand1.ExecuteReader();

                        while (dr.Read())
                        {
                            if (!dr.IsDBNull(0))
                            {
                                thecountinitemid = dr.GetInt32(0);
                            }

                        }
                        MySqlConn.Close();
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("נפל בשורה מספר 222");
                        MessageBox.Show(ex.Message);
                    }
                    Console.WriteLine("שורה  225 thecountinitemid=" + thecountinitemid);
                    if (thecountinitemid > 1)
                    {
                        try
                        {
                            MySqlConnection MySqlConn = new MySqlConnection(Login.Connectionstring);
                            MySqlConn.Open();
                            string Query1 = "DELETE FROM jobs WHERE jobid='" + jobID + "' and itemid='" + itemID + "'and itemNum='" + itemnum + "'";
                            MySqlCommand MSQLcrcommand1 = new MySqlCommand(Query1, MySqlConn);
                            MSQLcrcommand1.ExecuteNonQuery();
                            MySqlDataAdapter mysqlDAdp = new MySqlDataAdapter(MSQLcrcommand1);
                            MySqlConn.Close();
                            refreashandclear();
                            MessageBox.Show("!הפריט נמחק מהעבודה", "!הצלחה", MessageBoxButton.OK, MessageBoxImage.Information);
                            return;
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message);
                        }
                    }

                    // if it is the last from the itemid then check if this is the last from the jobid.
                    else
                    {
                        try
                        {
                            MySqlConnection MySqlConn = new MySqlConnection(Login.Connectionstring);
                            MySqlConn.Open();
                            string Query1 = "select COUNT(itemNum) from jobs where jobid='" + jobID + "' ";
                            MySqlCommand MSQLcrcommand1 = new MySqlCommand(Query1, MySqlConn);
                            MSQLcrcommand1.ExecuteNonQuery();
                            MySqlDataAdapter mysqlDAdp = new MySqlDataAdapter(MSQLcrcommand1);
                            MySqlDataReader dr = MSQLcrcommand1.ExecuteReader();

                            while (dr.Read())
                            {
                                if (!dr.IsDBNull(0))
                                {
                                    thecountinjobid = dr.GetInt32(0);
                                }

                            }
                            MySqlConn.Close();
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine("נפל בשורה מספר 272");
                            MessageBox.Show(ex.Message);
                        }
                        Console.WriteLine("שורה  275 thecountinjobid=" + thecountinjobid);
                        if (thecountinjobid > 1)
                        {
                            try
                            {
                                MySqlConnection MySqlConn = new MySqlConnection(Login.Connectionstring);
                                MySqlConn.Open();
                                string Query1 = "DELETE FROM jobs WHERE jobid='" + jobID + "' and itemid='" + itemID + "'and itemNum='" + itemnum + "'";
                                MySqlCommand MSQLcrcommand1 = new MySqlCommand(Query1, MySqlConn);
                                MSQLcrcommand1.ExecuteNonQuery();
                                MySqlDataAdapter mysqlDAdp = new MySqlDataAdapter(MSQLcrcommand1);
                                MySqlConn.Close();
                                refreashandclear();
                                MessageBox.Show("!הפריט נמחק מהעבודה", "!הצלחה", MessageBoxButton.OK, MessageBoxImage.Information);
                                ManagerJobInfoGui MJIG = new ManagerJobInfoGui(jobID);
                                MJIG.Show();
                                Login.close = 1;
                                this.Close();
                            }
                            catch (Exception ex)
                            {
                                MessageBox.Show(ex.Message);
                            }
                        }
                        else // if this is the last item in this job then just update the job status to בוטלה
                        {
                            try
                            {
                                string Query1 = "UPDATE jobs SET job_status='בוטלה' WHERE jobid='" + jobID + "'";
                                MySqlConnection MySqlConn = new MySqlConnection(Login.Connectionstring);
                                MySqlConn.Open();
                                MySqlCommand MSQLcrcommand1 = new MySqlCommand(Query1, MySqlConn);
                                MSQLcrcommand1.ExecuteNonQuery();
                                MySqlDataAdapter mysqlDAdp = new MySqlDataAdapter(MSQLcrcommand1);
                                MySqlConn.Close();
                                MessageBox.Show("בגלל שזהו הפריט האחרון בעבודה\n .הפריט לא נמחק אך סטטוס העבודה שונה לבוטלה", "!שים לב - שינוי סטטוס עבודה", MessageBoxButton.OK, MessageBoxImage.Warning);
                                return;
                            }
                            catch (Exception ex)
                            {
                                MessageBox.Show(ex.Message);
                            }

                        }

                    }// end else -  if it is the last from the itemid then check if this is the last from the jobid.

                }//end else

            }//end try
            catch
            {
                MessageBox.Show("לא נבחר פריט למחיקה", "!שים לב", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }