Exemple #1
0
        public ActionResult Create(sales_collector model, int?branchId, string profession, string employee, HttpPostedFileBase imageFile)
        {
            if (ModelState.IsValid)
            {
                if (branchId == -1 || branchId == null)
                {
                    branchId = _branchService.GetAll().FirstOrDefault().branch_id;
                }

                model.collector_branch_id = branchId;


                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) && e.deleted != true && e.emp_branch_id == branchId)
                          .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.emp_branch_id == branchId && e.deleted != true);
                    ViewBag.Professions = _professionService.GetAll().OrderByDescending(p => p.profession_presidences);
                    return(View(model));
                }

                string extension = Path.GetExtension(imageFile.FileName);
                if (!(extension.Equals(".jpg") || extension.Equals(".JPG")))
                {
                    ModelState.AddModelError(string.Empty, "Not an accepted image type!");
                    ViewBag.SP          = _employeeService.GetAll().Where(e => e.emp_branch_id == branchId && e.deleted != true);
                    ViewBag.Professions = _professionService.GetAll().OrderByDescending(p => p.profession_presidences);
                    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);

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

                _collectorService.Create(model);


                return(RedirectToAction("Index"));
            }

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