Exemplo n.º 1
0
        public ActionResult Edit(sales_collector model, string branch, string profession, string employee, HttpPostedFileBase imageFile)
        {
            if (ModelState.IsValid)
            {
                if (imageFile != null)
                {
                    string extension = Path.GetExtension(imageFile.FileName);

                    if (!(extension.Equals(".jpg") || extension.Equals(".JPG")))
                    {
                        ModelState.AddModelError(string.Empty, "Not an accepted image type!");
                        return(View(model));
                    }

                    string fileName = model.collector_code + extension;

                    model.collector_image = "~/File/Collector/" + fileName;

                    fileName = Path.Combine(Server.MapPath("~/File/Collector/"), fileName);

                    imageFile.SaveAs(fileName);
                }

                var pro = (hr_profession)_professionService.GetAll().Where(p => p.profession_name.Contains(profession)).SingleOrDefault();
                if (pro != null)
                {
                    model.collector_profession_id = pro.profession_id;
                }
                else
                {
                    _professionService.Create(new hr_profession()
                    {
                        profession_name = profession,
                    });
                    pro = (hr_profession)_professionService.GetAll().Where(p => p.profession_name.Contains(profession)).SingleOrDefault();

                    model.collector_profession_id = pro.profession_id;
                }

                var sal = (hr_employee)_employeeService.GetAll().Where(e => e.emp_code.Contains(employee))
                          .SingleOrDefault();
                if (sal != null)
                {
                    model.collector_sales_person_id = sal.emp_id;
                }

                else
                {
                    ModelState.AddModelError(string.Empty, "Sales Person Code not correct!");

                    ViewBag.SP          = _employeeService.GetAll().Where(e => e.deleted != true);
                    ViewBag.Branchs     = _branchService.GetAll();
                    ViewBag.Professions = _professionService.GetAll().OrderByDescending(p => p.profession_presidences);

                    return(View(model));
                }


                var br = _branchService.GetAll().Where(b => b.branch_name.Contains(branch)).SingleOrDefault();
                if (br != null)
                {
                    model.collector_branch_id = br.branch_id;
                }

                else
                {
                    ModelState.AddModelError(string.Empty, "Branch Name not correct!");

                    ViewBag.SP          = _employeeService.GetAll().Where(e => e.deleted != true);
                    ViewBag.Branchs     = _branchService.GetAll();
                    ViewBag.Professions = _professionService.GetAll().OrderByDescending(p => p.profession_presidences);

                    return(View(model));
                }

                pro.profession_presidences += 1;
                _professionService.Edit(pro);


                _collectorService.Edit(model);



                return(RedirectToAction("Index"));
            }

            ViewBag.SP          = _employeeService.GetAll().Where(e => e.deleted != true);
            ViewBag.Branchs     = _branchService.GetAll();
            ViewBag.Professions = _professionService.GetAll().OrderByDescending(p => p.profession_presidences);
            return(View(model));
        }