//sisteme giris eden sexsin id-sine gonderilen mesajdaki is planlamasinin bitmesini yerine yetiren emeliyyat. is planlamasinin statusunu deyisir
        public ActionResult WorkPlanComplated(int id)
        {
            MESSAGENOTIFICATION compilitedWp = db.MESSAGENOTIFICATIONs.Find(id);

            WORK_PLAN complatedWorkPlan = db.WORK_PLAN.Find(compilitedWp.WORKPLANID);

            complatedWorkPlan.STATUS          = false;
            db.Entry(complatedWorkPlan).State = EntityState.Modified;
            db.SaveChanges();
            return(Json("ok", JsonRequestBehavior.AllowGet));
        }
        //create work plan
        public ActionResult CreateWorkPlan(mainWorkPlan workplan)
        {
            User logineduser = (User)Session["UserLogin"];

            WORK_PLAN newworkplan = new WORK_PLAN();

            newworkplan.START_DATE   = workplan.mainstartdate;
            newworkplan.END_DATE     = workplan.mainenddate;
            newworkplan.PARCEL_ID    = workplan.parcelId;
            newworkplan.RESPONDER_ID = workplan.responderTd;
            newworkplan.WORKS_ID     = workplan.WorkId;
            newworkplan.STATUS       = true;
            db.WORK_PLAN.Add(newworkplan);
            db.SaveChanges();


            User reciver = db.Users.Where(s => s.WorkerID == newworkplan.RESPONDER_ID).FirstOrDefault();

            if (reciver != null)
            {
                MESSAGENOTIFICATION new_message = new MESSAGENOTIFICATION();
                new_message.WORKPLANID = newworkplan.OBJECTID;
                new_message.STATUS     = true;
                new_message.RECIVERID  = (int)reciver.ID;
                new_message.SENDERID   = logineduser.ID;
                new_message.SENDEDTIME = DateTime.Now;
                db.MESSAGENOTIFICATIONs.Add(new_message);
                db.SaveChanges();
            }



            if (workplan.dailyWorkPlanArray != null && workplan.dailyWorkPlanArray.Count != 0)
            {
                for (int i = 0; i < workplan.dailyWorkPlanArray.Count; i++)
                {
                    DAYLY_WORK_PLAN newdailyPlan = new DAYLY_WORK_PLAN();
                    newdailyPlan.WORKPLAN_ID = newworkplan.OBJECTID;
                    newdailyPlan.STARTDATE   = workplan.dailyWorkPlanArray[i].startdate;
                    newdailyPlan.ENDDATE     = workplan.dailyWorkPlanArray[i].enddate;
                    db.DAYLY_WORK_PLAN.Add(newdailyPlan);
                    db.SaveChanges();

                    if (workplan.dailyWorkPlanArray[i].queueArray != null && workplan.dailyWorkPlanArray[i].queueArray.Count != 0)
                    {
                        for (int q = 0; q < workplan.dailyWorkPlanArray[i].queueArray.Count; q++)
                        {
                            QueuePlan newqueueplan = new QueuePlan();
                            newqueueplan.dailyPlanId = newdailyPlan.OBJECTID;
                            newqueueplan.startdate   = DateTime.Now;
                            newqueueplan.enddate     = DateTime.Now;
                            newqueueplan.note        = workplan.dailyWorkPlanArray[i].queueArray[q].note;
                            db.QueuePlans.Add(newqueueplan);
                            db.SaveChanges();

                            if (workplan.dailyWorkPlanArray[i].queueArray[q].workerID != null && workplan.dailyWorkPlanArray[i].queueArray[q].workerID.Count != 0)
                            {
                                for (int w = 0; w < workplan.dailyWorkPlanArray[i].queueArray[q].workerID.Count; w++)
                                {
                                    WORK_PLAN_WORKERS planworkers = new WORK_PLAN_WORKERS();
                                    planworkers.DAYLY_WORK_PLAN_ID = newqueueplan.OBJECTID;
                                    planworkers.WORKER_ID          = workplan.dailyWorkPlanArray[i].queueArray[q].workerID[w];
                                    db.WORK_PLAN_WORKERS.Add(planworkers);
                                    db.SaveChanges();
                                }
                            }

                            if (workplan.dailyWorkPlanArray[i].queueArray[q].technialArray != null && workplan.dailyWorkPlanArray[i].queueArray[q].technialArray.Count != 0)
                            {
                                for (int t = 0; t < workplan.dailyWorkPlanArray[i].queueArray[q].technialArray.Count; t++)
                                {
                                    WORK_PLAN_TECHNIQUE plantechicals = new WORK_PLAN_TECHNIQUE();
                                    plantechicals.DAYLY_WORK_PLAN_ID = newqueueplan.OBJECTID;
                                    plantechicals.TECHNIQUE_ID       = workplan.dailyWorkPlanArray[i].queueArray[q].technialArray[t].tehcnical;
                                    db.WORK_PLAN_TECHNIQUE.Add(plantechicals);
                                    db.SaveChanges();

                                    if (workplan.dailyWorkPlanArray[i].queueArray[q].technialArray[t].trailerid != null && workplan.dailyWorkPlanArray[i].queueArray[q].technialArray[t].trailerid.Count != 0)
                                    {
                                        for (int y = 0; y < workplan.dailyWorkPlanArray[i].queueArray[q].technialArray[t].trailerid.Count; y++)
                                        {
                                            TECHNIQUE_TRAILER plantrailer = new TECHNIQUE_TRAILER();
                                            plantrailer.WORK_PLAN_TECHNIQUE_ID = plantechicals.OBJECTID;
                                            plantrailer.TECHNIQUE_ID           = workplan.dailyWorkPlanArray[i].queueArray[q].technialArray[t].trailerid[y];
                                            db.TECHNIQUE_TRAILER.Add(plantrailer);
                                            db.SaveChanges();
                                        }
                                    }
                                }
                            }

                            if (workplan.dailyWorkPlanArray[i].queueArray[q].feltilizerArray != null && workplan.dailyWorkPlanArray[i].queueArray[q].feltilizerArray.Count != 0)
                            {
                                for (int f = 0; f < workplan.dailyWorkPlanArray[i].queueArray[q].feltilizerArray.Count; f++)
                                {
                                    FERTILIZER_WORK_PLAN planfertilizer = new FERTILIZER_WORK_PLAN();
                                    planfertilizer.DAYLY_WORK_PLAN_ID = newqueueplan.OBJECTID;
                                    planfertilizer.FERTILIZER_ID      = workplan.dailyWorkPlanArray[i].queueArray[q].feltilizerArray[f].feltilizerid;
                                    planfertilizer.WATER_UNIT         = Convert.ToInt32(workplan.dailyWorkPlanArray[i].queueArray[q].feltilizerArray[f].watherquantity);
                                    planfertilizer.FERTILIZER_UNIT    = Convert.ToInt32(workplan.dailyWorkPlanArray[i].queueArray[q].feltilizerArray[f].feltilizerquantity);
                                    db.FERTILIZER_WORK_PLAN.Add(planfertilizer);
                                    db.SaveChanges();
                                }
                            }
                        }
                    }
                }
            }

            return(Json("ok", JsonRequestBehavior.AllowGet));
        }