public ActionResult CreateDocument() { var c = new Document(); c.CodDocument = documentRepository.GetNewCode(c); documentRepository.Add(c); documentRepository.Save(); Session["CodDocument"] = c.CodDocument; //view name is needed for reach right view because to using more than one submit we have to use "Action" in action method name ViewBag.ActionMethod = "EditDocument"; return View("EditDocument", c); }
public ActionResult CreateProduct(ProductViewModel pv) { bool doControl = true; // var qts = pv.Quantities; var product = pv.Product; foreach (var item in product.ProductParts) { if (item.Format == "0x0" && item.FormatPersonalized != String.Empty) { item.Format = item.FormatPersonalized; } if (item.Format == "0x0") { item.Format = String.Empty; } if (item.TypeOfProductPart == ProductPart.ProductPartType.ProductPartDoubleLabelRoll) { doControl = false; } } //CHECK IF EACH PRINTABLE ARTICLE IS IN LIST foreach (var item in product.ProductParts) { foreach (var pArticle in item.ProductPartPrintableArticles) { if (!pArticle.IsInList(articleRepository.GetAll())) { ModelState.AddModelError("PersError", "ProductPartPrintableArticleListError"); } } } var ok = true; var taskList = typeOfTaskRepository.GetAll().ToList(); var productParts = pv.Product.ProductParts; foreach (var pPart in productParts) { //per tutte le lavorazioni voglio controllare che esista almeno una macchina e almeno il formato giusto var lstTask = pPart.ProductPartTasks.Where(x => !x.CodOptionTypeOfTask.Contains("_NO")); var lstTaskExecutor = taskExecuteRepository.GetAll().ToList(); foreach (var item in lstTask) { var t = taskList.FirstOrDefault(y => y.OptionTypeOfTasks.Where(x => x.CodOptionTypeOfTask == item.CodOptionTypeOfTask).Count() > 0); var s = TaskExecutor.FilterByTask(lstTaskExecutor.AsQueryable(), t.CodTypeOfTask); Console.WriteLine(s); //controllo se esiste una macchina ok = ok && (s.Count() > 0); foreach (var tsk in s) { ok = ok || SheetCut.IsValidOnMax(tsk.FormatMax, tsk.FormatMin, pPart.Format); } if (t.CodTypeOfTask.StartsWith("STAMPA")) { Console.WriteLine(""); } } if (!ok && doControl) { ModelState.AddModelError("PersError", "FormatOrTaskExecutorError"); } } //END CHECK if (ModelState.IsValid) { // try { //il cliente var cust = customerSupplierRepository.GetAll().OfType<Customer>().Where(x => x.BusinessName == pv.Customer).FirstOrDefault(); if (cust == null) { cust = new Customer(); cust.CodCustomerSupplier = customerSupplierRepository.GetNewCode(cust); cust.BusinessName = pv.Customer; cust.VatNumber = "00000000000"; customerSupplierRepository.Add(cust); customerSupplierRepository.Save(); } product.ProductRefName = pv.ProductRefName; product.CodProduct = productRepository.GetNewCode(product); product.CheckConsistency(); //save the product productRepository.Add(product); productRepository.Save(); documentRepository.SetDbName(""); documentRepository.SetDbName(CurrentDatabase); //se ho un documento attivo in sessione salvo anche la relazione //oppure se non c'è un domcumento attivo lo creo e salvo la relazione //dopodiche devo saltare al documento oppure alla view model del prodottodocumento if (Session["CodDocument"] == null || documentRepository.GetSingle((string)Session["CodDocument"]) == null) { var d = new Document(); d.CodDocument = documentRepository.GetNewCode(d); Session["CodDocument"] = d.CodDocument; documentRepository.Add(d); documentRepository.Save(); } var document = documentRepository.GetSingle((string)Session["CodDocument"]); document.DocumentName = pv.DocumentName; document.CustomerSupplier = cust; document.Customer = cust.BusinessName; document.CodCustomer = cust.CodCustomerSupplier; documentRepository.Edit(document); documentRepository.Save(); DocumentProduct dp; DocumentProduct firstDocumentProduct = null; if (pv.Quantity != 0) { dp = new DocumentProduct(); //use first document product to lead each tecnical choice if (firstDocumentProduct == null) { firstDocumentProduct = dp; } dp.Document = null; dp.CodProduct = pv.Product.CodProduct; dp.Product = pv.Product; dp.Quantity = pv.Quantity; dp.InitCost(); document.DocumentProducts.Add(dp); documentRepository.Edit(document); documentRepository.Save(); } //OK questo funziona ma riporta alla lista dei costi //TODO: Sending singlaR notification to client to reload basket product //return Json(new { redirectUrl = Url.Action("EditDocumentProducts", "Document", new { id = document.DocumentProducts.LastOrDefault().CodProduct }) }); if (firstDocumentProduct.Costs.FirstOrDefault() != null) { Session["codProduct"] = document.DocumentProducts.LastOrDefault().CodProduct; return Json(new { redirectUrl = Url.Action("EditAndCreateAllCost", "Document", new { id = firstDocumentProduct.Costs.FirstOrDefault().CodDocumentProduct }) }); // return Json(new { redirectUrl = Url.Action("EditCost", "Document", new { id = firstDocumentProduct.Costs.FirstOrDefault().CodCost }) }); } else { return Json(new { redirectUrl = Url.Action("EditDocumentProducts", "Document", new { id = document.DocumentProducts.LastOrDefault().CodProduct }) }); } } // catch (Exception ex) //{ // ModelState.AddModelError(string.Empty, "Something went wrong. Message: " + ex.Message); //} } //Carico i nomi dei formati perchè se la validazione non va a buon fine devo ripresentarli product.FormatsName = formatsRepository.GetAllById(product.CodMenuProduct); product.SystemTaskList = typeOfTaskRepository.GetAll().ToList(); //reload option object for productTask and productPartTask // var taskList = this.typeOfTaskRepository.GetAll(); foreach (var item in product.ProductTasks) { item.OptionTypeOfTask = typeOfTaskRepository.GetSingleOptionTypeOfTask(item.CodOptionTypeOfTask); } foreach (var item in product.ProductParts) { foreach (var item2 in item.ProductPartTasks) { item2.OptionTypeOfTask = typeOfTaskRepository.GetSingleOptionTypeOfTask(item2.CodOptionTypeOfTask); } } //-----end reloding //???? //view name is needed for reach right view because to using more than one submit we have to use "Action" in action method name ViewBag.ActionMethod = "CreateProduct"; return PartialView("_EditAndCreateProduct", pv); }