private void potvrdiToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (inspectorsGridView.SelectedRows.Count < 1)
            {
                MessageBox.Show("Odaberite barem jednog inspektora!");
                return;
            }
            if (vehiclesGridView.SelectedRows.Count < 1)
            {
                DialogResult result = MessageBox.Show("Jeste li sigurni da ne želite dodati vozilo?", "Potvrda", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (result == DialogResult.No)
                {
                    noVehicles = false;
                    return;
                }
                else
                {
                    noVehicles = true;
                }
            }
            else
            {
                noVehicles = false;
            }
            //Check if user refreshed responsibilities table
            if (vehiclesGridView.SelectedRows.Count != responsibilitiesGridView.Rows.Count)
            {
                MessageBox.Show("Osvježite tabelu!");
                return;
            }

            if (noVehicles == false)
            {
                //check if all vehicles have responsible inspector
                for (int i = 0; i < responsibilitiesGridView.RowCount; i++)
                {
                    if ((int?)responsibilitiesGridView.Rows[i].Cells["inspector_id_column"].Value == null)
                    {
                        //No responsibility for this vehicle
                        MessageBox.Show("Sva odabrana vozila moraju imati odgovornog inspektora!");
                        return;
                    }
                }
            }
            DatabaseModel db = new DatabaseModel();

            //Insert this control to database
            control c = new control
            {
                company_id  = this.companyId,
                start_date  = controlDatePicker.Value,
                is_finished = 0,
                isActive    = 1,
                is_regular  = regularControl.Checked ? (sbyte)1 : (sbyte)0
            };

            db.controls.Add(c);
            db.SaveChanges();   //Must  be saved to get the auto generated id

            //Insert related complaints if they exist
            if (reclamationIDs != null)
            {
                foreach (var item in reclamationIDs)
                {
                    c.complaints.Add(db.complaints.Find(item));
                }
            }

            //Insert inspectors to control_has_inspectors table
            for (int i = 0; i < inspectorsGridView.SelectedRows.Count; i++)
            {
                c.inspectors.Add(db.inspectors.Find((int)inspectorsGridView.SelectedRows[i].Cells["id"].Value));
            }
            //Insert vehicle responsibilities
            if (this.noVehicles == false)
            {
                for (int i = 0; i < responsibilitiesGridView.Rows.Count; i++)
                {
                    var vr = new vehicle_responsibility();

                    vr.control_id   = c.id;
                    vr.inspector_id = (int)responsibilitiesGridView.Rows[i].Cells["inspector_id_column"].Value;
                    vr.date         = controlDatePicker.Value;
                    string vehId = (string)responsibilitiesGridView.Rows[i].Cells["vehicle_id_column"].Value;
                    vr.vehicle_id = Int32.Parse(vehId);
                    db.vehicle_responsibility.Add(vr);
                }
            }
            db.SaveChanges();
            MessageBox.Show("Kontrola je uspješno dodata.");
            this.Close();
        }
Example #2
0
        private void subjekteIInspektoreToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DateTime dt           = DateTime.Today.AddYears(-1);
            var      queryCompany = from v in db.companies
                                    where v.last_control < dt
                                    select new
            {
                id           = v.id,
                name         = v.name,
                last_control = v.last_control,
                location     = v.location
            };

            var queryInspectors = from v in db.inspectors
                                  select new
            {
                id        = v.id,
                full_name = v.first_name + " " + v.last_name
            };

            var queryVehicles = from v in db.vehicles
                                select new
            {
                id = v.id,
                registration_num = v.registration_num
            };

            var limitedQuery = queryCompany.Take(queryInspectors.Count() * queryVehicles.Count());

            Dictionary <string, Tuple <Tuple <string, string>, string> > triple = new Dictionary <string, Tuple <Tuple <string, string>, string> >();

            DateTime dtt = DateTime.Now;

            while (dtt.DayOfWeek != DayOfWeek.Monday)
            {
                dtt = dtt.AddDays(1);
            }

            for (int i = 0; i < limitedQuery.Count(); i++)//kontrola u bazu
            {
                control c = new control();
                c.company_id        = limitedQuery.ToList()[i].id;
                c.control_justified = 1;
                c.is_finished       = 0;
                c.isActive          = 1;
                int l = i % 5;
                switch (l)
                {
                case 1:
                    c.start_date = dtt;
                    break;

                case 2:
                    c.start_date = dtt.AddDays(1);
                    break;

                case 3:
                    c.start_date = dtt.AddDays(2);
                    break;

                case 4:
                    c.start_date = dtt.AddDays(3);
                    break;

                case 5:
                    c.start_date = dtt.AddDays(4);
                    break;

                default:
                    c.start_date = dtt.AddDays(5);
                    break;
                }
                db.controls.Add(c);
            }
            db.SaveChanges();

            var queryControls = from v in db.controls
                                select new
            {
                id         = v.id,
                company_id = v.company_id
            };

            for (int i = 0; i < queryCompany.Count(); i++)//vehicle_respons u bazu
            {
                vehicle_responsibility vr = new vehicle_responsibility();
                vr.vehicle_id   = queryVehicles.ToList()[i % queryVehicles.Count()].id;
                vr.inspector_id = queryInspectors.ToList()[i % queryInspectors.Count()].id;
                vr.control_id   = queryControls.ToList()[i].id;
                int l = i % 6;
                switch (l)
                {
                case 1:
                    vr.date = dtt;
                    break;

                case 2:
                    vr.date = dtt.AddDays(1);
                    break;

                case 3:
                    vr.date = dtt.AddDays(2);
                    break;

                case 4:
                    vr.date = dtt.AddDays(3);
                    break;

                case 5:
                    vr.date = dtt.AddDays(4);
                    break;

                default:
                    vr.date = dtt.AddDays(5);
                    break;
                }

                triple.Add(queryCompany.ToList()[i].name, new Tuple <Tuple <string, string>, string>(new Tuple <string, string>(queryCompany.ToList()[i].location, queryInspectors.ToList()[i % queryInspectors.Count()].full_name), queryVehicles.ToList()[i % queryVehicles.Count()].registration_num));
                db.vehicle_responsibility.Add(vr);
            }
            db.SaveChanges();

            dayMI.Text = "Dan - Ponedjeljak";
            getControlsByDayInWeek(DayOfWeek.Monday);
        }