Exemple #1
0
        public async Task <IActionResult> Create(CreateViewModel viewModel, string connectionId, bool more)
        {
            //Добавление элемента в базу
            try
            {
                // Проверка номера телефона на кол-во цифр
                Regex regex = new Regex(@"\D+");
                viewModel.Phone = regex.Replace(viewModel.Phone, "");
                if (viewModel.Phone.Length < 7)
                {
                    ModelState.AddModelError("Phone", Messages.phoneMinLengthErrorMessage);
                }

                //Проверка введенных данных
                if (ModelState.IsValid)
                {
                    // Добавление станка при отсутствии в базе
                    var machine = await _machineRepository.GetMachine(viewModel.Machine);

                    if (machine == null)
                    {
                        machine = await _machineRepository.AddMachine(viewModel.Machine, await _machineTypeRepository.AddMachineType("Без группы"));
                    }

                    // Добавляемый элемент
                    var requisition = new Requisition
                    {
                        UserName    = User.Identity.Name,
                        CreateDate  = DateTime.Now,
                        Phone       = viewModel.Phone,
                        ContactName = viewModel.ContactName,
                        Location    = viewModel.Location,
                        MachineId   = machine.Id,
                        Comment     = viewModel.Comment
                    };

                    await _requisitionRepository.AddRequisition(requisition);

                    await _hub.Clients.Client(connectionId).SendAsync("Success", Messages.successCreateMessage);

                    // Добавление дополнительной записи
                    if (more)
                    {
                        return(RedirectToAction("Create", new {
                            ajaxLoad = true,
                            phone = viewModel.Phone,
                            contactName = viewModel.ContactName,
                            location = viewModel.Location
                        }));
                    } // if

                    return(RedirectToAction("Details", new { id = requisition.Id, ajaxLoad = true }));
                } //if
            }
            catch (DbUpdateException ex) { _message = DbErrorsInterpreter.GetDbUpdateExceptionMessage(ex); }
            catch { _message = Messages.createErrorMessage; } // try-catch

            viewModel.Machines = await _machineRepository.GetMachines();

            if (_message != null)
            {
                await _hub.Clients.Client(connectionId).SendAsync("Error", _message);
            }
            return(PartialView(viewModel));
        } // Create
        public HttpResponseMessage Post([FromBody] RequisitionModel RequisitionModel)
        {
            try
            {
                if (string.IsNullOrEmpty(RequisitionModel.RequisitionMasterData.company_id.ToString()) && RequisitionModel.RequisitionMasterData.requisition_type != "B2B Requisition")
                {
                    var partyFormatter = RequestFormat.JsonFormaterString();
                    return(Request.CreateResponse(HttpStatusCode.OK, new Confirmation {
                        output = "warning", msg = "Please Select Company"
                    }, partyFormatter));
                }
                if (string.IsNullOrEmpty(RequisitionModel.RequisitionMasterData.warehouse_from.ToString()))
                {
                    var partyFormatter = RequestFormat.JsonFormaterString();
                    return(Request.CreateResponse(HttpStatusCode.OK, new Confirmation {
                        output = "warning", msg = "Please Select Warehouse"
                    }, partyFormatter));
                }
                if (string.IsNullOrEmpty(RequisitionModel.RequisitionMasterData.party_id.ToString()))
                {
                    var partyFormatter = RequestFormat.JsonFormaterString();
                    return(Request.CreateResponse(HttpStatusCode.OK, new Confirmation {
                        output = "warning", msg = "Please Select Sales Channel"
                    }, partyFormatter));
                }
                if (string.IsNullOrEmpty(RequisitionModel.RequisitionMasterData.credit_term.ToString()))
                {
                    var partyFormatter = RequestFormat.JsonFormaterString();
                    return(Request.CreateResponse(HttpStatusCode.OK, new Confirmation {
                        output = "warning", msg = "Please Select Credit Term"
                    }, partyFormatter));
                }
                if (RequisitionModel.RequisitionDetailsList.Count < 1)
                {
                    var partyFormatter = RequestFormat.JsonFormaterString();
                    return(Request.CreateResponse(HttpStatusCode.OK, new Confirmation {
                        output = "warning", msg = "Please Select Products"
                    }, partyFormatter));
                }

                if (RequisitionModel.RequisitionDetailsList.Count > 0)
                {
                    foreach (var qt in RequisitionModel.RequisitionDetailsList)
                    {
                        var Q          = qt.quantity;
                        var pId        = qt.product_id;
                        var colId      = qt.color_id;
                        var pVerson    = qt.product_version_id;
                        var has_serial = qt.has_serial;
                        if (Q < 1 || Q == null)
                        {
                            var partyFormatter = RequestFormat.JsonFormaterString();
                            return(Request.CreateResponse(HttpStatusCode.OK, new Confirmation {
                                output = "warning", msg = "Check Quantity is not empty"
                            }, partyFormatter));

                            break;
                        }
                        else if (pId == null)
                        {
                            var partyFormatter = RequestFormat.JsonFormaterString();
                            return(Request.CreateResponse(HttpStatusCode.OK, new Confirmation {
                                output = "warning", msg = "Check Product is not empty"
                            }, partyFormatter));

                            break;
                        }
                        else if (has_serial == true && (colId == null || pVerson == null))
                        {
                            var partyFormatter = RequestFormat.JsonFormaterString();
                            return(Request.CreateResponse(HttpStatusCode.OK, new Confirmation {
                                output = "warning", msg = "Check Color or Version is not empty"
                            }, partyFormatter));

                            break;
                        }
                    }
                }

                requisitionRepository.AddRequisition(RequisitionModel);
                var formatter = RequestFormat.JsonFormaterString();
                return(Request.CreateResponse(HttpStatusCode.OK, new Confirmation {
                    output = "success", msg = "Requisition created successfully"
                }, formatter));
            }
            catch (Exception ex)
            {
                var formatter = RequestFormat.JsonFormaterString();
                return(Request.CreateResponse(HttpStatusCode.OK, new Confirmation {
                    output = "error", msg = ex.ToString()
                }, formatter));
            }
        }