private void BindgvPlan(int year, decimal DaysPerFarmer, DateTime start, DateTime end, string role)
    {
        try
        {
            DataTable dtNewPlan = new DataTable();
            dtNewPlan.Columns.Add("FarmerID");
            dtNewPlan.Columns.Add("FarmerName");
            dtNewPlan.Columns.Add("FarmerCode");
            dtNewPlan.Columns.Add("FarmerArea");
            dtNewPlan.Columns.Add("InspectorID");
            dtNewPlan.Columns.Add("InspectorName");
            dtNewPlan.Columns.Add("PlanDate");
            dtNewPlan.Columns.Add("VisitedDate");
            dtNewPlan.Columns.Add("InspectionID");
            List <DateTime> dates      = GetDates(year, start, end);
            DataTable       dtHolidays = InspectionPlan_DL.Holidays(year, start, end);
            foreach (DataRow item in dtHolidays.Rows)
            {
                string stDate = Convert.ToString(item["HolidayDate"]);
                if (stDate.Contains("/"))
                {
                    string[] keys  = stDate.Split('/');
                    DateTime hdate = new DateTime(Convert.ToInt32(keys[2]), Convert.ToInt32(keys[0]), Convert.ToInt32(keys[1]));
                    if (hdate.DayOfWeek != DayOfWeek.Sunday && dates.Contains(hdate))
                    {
                        dates.Remove(new DateTime(Convert.ToInt32(keys[2]), Convert.ToInt32(keys[0]), Convert.ToInt32(keys[1])));
                    }
                }
            }
            dates = dates.OrderBy(m => m).ToList();
            List <int> months = dates.Select(m => m.Month).Distinct().ToList();

            DataTable farmervillages = UnitInformation_DL.FarmersVillageListByIcs("ICS02");
            int       c = farmervillages.Rows.Count;

            List <MudarVillage> villages = new List <MudarVillage>();
            foreach (DataRow item in farmervillages.Rows)
            {
                MudarVillage village = new MudarVillage();
                village.VillageName = Convert.ToString(item["City_Village"]);
                DataTable dtFarmer = Farmer_DL.GetFarmerlistVillagewise(village.VillageName);

                List <MudarFarmer> farmers = new List <MudarFarmer>();
                for (int j = 0; j < dtFarmer.Rows.Count; j++)
                {
                    MudarFarmer farmer = new MudarFarmer();
                    farmer.FarmerID   = Convert.ToString(dtFarmer.Rows[j]["FarmerID"]);
                    farmer.FirstName  = Convert.ToString(dtFarmer.Rows[j]["FirstName"]);
                    farmer.FarmerCode = Convert.ToString(dtFarmer.Rows[j]["FarmerCode"]);
                    farmer.IsVisited  = false;
                    farmers.Add(farmer);
                }
                int[] visitMonths = ShareEqual(farmers.Count, months.Count);
                int   mn          = 0;
                int   skip        = 0;
                foreach (var vm in months)
                {
                    farmers.Skip(skip).Take(visitMonths[mn]).ToList().ForEach(m => m.VisitedMonth = vm);
                    skip = skip + visitMonths[mn];
                    mn   = mn + 1;
                }
                village.Farmers = farmers;
                villages.Add(village);
            }
            DataTable            EmployeeTable = BranchsRolesEmployees_DL.GetEmployeBasedonRoleID(role);
            List <MudarEmployee> emps          = new List <MudarEmployee>();
            foreach (DataRow item in EmployeeTable.Rows)
            {
                MudarEmployee emp = new MudarEmployee();
                emp.EmployeeID   = new Guid(Convert.ToString(item["EmployeeId"]));
                emp.EmployeeName = Convert.ToString(item["EmployeeFristName"]);
                emps.Add(emp);
            }
            List <MudarPlanModel> newPlans = new List <MudarPlanModel>();
            foreach (var mnth in months)
            {
                List <DateTime> monthDates = dates.Where(m => m.Month == mnth && m.Year == year).ToList();
                int             mod        = Convert.ToInt32((monthDates.Count) / c);
                int             l          = Convert.ToInt32((monthDates.Count) % c);
                villages.ForEach(m => m.loopCount = mod);
                for (int i = 0; i < l; i++)
                {
                    villages[i].loopCount += 1;
                }
                int h = 0;
                foreach (var item in villages)
                {
                    List <MudarFarmer> listFarmers = item.Farmers.Where(m => m.VisitedMonth == mnth).ToList();
                    int[] farmerVisitedPerInsp     = ShareEqual(listFarmers.Count, emps.Count);
                    int   f = 0;
                    foreach (var item1 in emps)
                    {
                        int[] timesVisit = ShareEqual(farmerVisitedPerInsp[f], item.loopCount);
                        int   k          = h;
                        foreach (var visit in timesVisit)
                        {
                            List <MudarFarmer> visitFarmers = listFarmers.Where(m => m.IsVisited == false).Take(visit).ToList();
                            foreach (var farm in visitFarmers)
                            {
                                MudarPlanModel plan = new MudarPlanModel();
                                plan.FarmerID      = farm.FarmerID;
                                plan.FarmerName    = farm.FirstName;
                                plan.FarmerCode    = farm.FarmerCode;
                                plan.VillageName   = item.VillageName;
                                plan.InspectorCode = item1.EmployeeID;
                                plan.InspectorName = item1.EmployeeName;
                                plan.PlanDate      = monthDates[k];
                                plan.VisitedDate   = monthDates[k];
                                var empCheck = newPlans.Where(m => m.PlanDate.ToShortDateString() == plan.PlanDate.ToShortDateString() && m.VillageName == plan.VillageName && m.InspectorName != plan.InspectorName).ToList();
                                if (empCheck.Count <= 0)
                                {
                                    newPlans.Add(plan);
                                }
                            }
                            listFarmers.Where(m => m.IsVisited == false).Take(visit).ToList().ForEach(m => m.IsVisited = true);
                            k = k + villages.Count;
                            if (k >= monthDates.Count)
                            {
                                k = 0;
                            }
                        }
                        f = f + 1;
                        h = h + 1;
                    }
                    h = h - 1;
                    item.Farmers.Where(m => m.VisitedMonth == mnth).ToList().ForEach(m => m.IsVisited = true);
                }
            }
            newPlans = newPlans.OrderBy(m => m.PlanDate).ToList();
            foreach (var item in newPlans)
            {
                DataRow newrow = dtNewPlan.NewRow();
                newrow["FarmerID"]      = item.FarmerID;
                newrow["FarmerName"]    = item.FarmerName;
                newrow["FarmerCode"]    = item.FarmerCode;
                newrow["FarmerArea"]    = item.VillageName;
                newrow["InspectorID"]   = item.InspectorCode;
                newrow["InspectorName"] = item.InspectorName;
                newrow["PlanDate"]      = item.PlanDate.ToShortDateString();
                newrow["VisitedDate"]   = item.VisitedDate.ToShortDateString();
                dtNewPlan.Rows.Add(newrow);
            }
            dt = dtNewPlan;
            if (dt.Rows.Count > 0)
            {
                Session["Inspec"]          = string.Empty;
                Session["Inspec"]          = dt;
                lblFarmers.Text            = dt.Rows.Count.ToString();
                divShowPlanDetails.Visible = true;
                btnSubmit.Visible          = true;
                gvPlan.DataSource          = dt;
                gvPlan.DataBind();
            }
            else
            {
                Response.Write("<script>alert('No Data Found for InspectionPlan !!!!'</script>");
            }
        }
        catch (Exception ex)
        {
            Session["ErrorMsg"] = ex.Message;
            Response.Redirect("~/NoAccess.aspx", false);
        }
    }
Beispiel #2
0
 public DataTable FarmersVillageListByIcs(string icsType)
 {
     return(UnitInformation_DL.FarmersVillageListByIcs(icsType));
 }