Пример #1
0
        public ActionResult Approve(int Id)
        {
            ContractDistributor item = controller.Retrieve(Id);

            if (!controller.ResultManager.IsCorrect)
            {
                NotifyUser(resultManager: controller.ResultManager);
                return(RedirectToAction("Index"));
            }

            if (item.AmountGoalTotal < item.AmountGoalTotalPre) //amount indicated by distributor is less than the indicated by bayer?
            {                                                   //yes
                //validate approver is GRV, RTV cannot approve on this case
                if (!Identity.CurrentUser.IsInRole(PSD.Controller.UserRole.AppAdmin + "," + PSD.Controller.UserRole.EmployeeManagerOperation))
                {
                    NotifyUser(messageError: "Los montos indicados por el Distribuidor son menores, se necesita aprobación de GRV");
                    return(RedirectToAction("Detail", new { Id = Id }));
                }
            }

            if (controller.Approve(item) && controller.ResultManager.IsCorrect)
            {
                NotifyUser(messageOk: controller.ResultManager.ToDescriptionString(), messageDebug: controllerTraceId + "Approve.831 Contrato '[" + item.Id + "]:" + item.IdB + "' actualizado");
                return(RedirectToAction("Index"));
            }

            NotifyUser(resultManager: controller.ResultManager);
            return(RedirectToAction("Detail", new { Id = Id }));
        }
        public ContractDistributor Retrieve(int id)
        {
            ResultManager.IsCorrect = false;
            //initial validations
            //-sys validations
            if (id == -1 || id == 0)
            {//no id was received
                ResultManager.Add(ErrorDefault, Trace + "Retrieve.131 No se recibio el id del convenio Bayer-Distribuidor");
                return(null);
            }

            ContractDistributor auxItem = null;

            try
            {
                auxItem = Repository.ContractsDistributor.Get(id);
                if (auxItem == null)
                {
                    ResultManager.Add(ErrorDefault, Trace + "Retrieve.511 No se encontró un convenio Bayer-Distribuidor con id '" + id + "'");
                }
                else
                {
                    ResultManager.IsCorrect = true;
                }
            }
            catch (Exception ex)
            {
                ResultManager.Add(ErrorDefault, Trace + "Retrieve.511 Excepción al obtener el convenio Bayer-Distribuidor a editar: " + ex.Message);
            }

            return(auxItem);
        }
        public bool Delete(int contractId)
        {
            ResultManager.IsCorrect = false;

            // validate bayer contract associated
            ContractDistributor item = Repository.ContractsDistributor.Get(contractId);

            if (item.ContractDistributorStatusId == 1 || item.ContractDistributorStatusId == 2)
            {
                ResultManager.Add(ErrorDefault, Trace + "DeleteContractDistributor.### El Convenio con Distribuidor seleccionado no se puede eliminar, su estatus es activo / vencido.");
                return(false);
            }

            // now proceed with the delete operation
            try
            {
                Repository.ContractsDistributor.Remove(contractId);

                Repository.Complete();
                ResultManager.IsCorrect = true;
            }
            catch (Exception ex)
            {
                ErrorManager.Add(Trace + "", ErrorDefault, "Exception while deleting distributor contract ", ex);
                ResultManager.IsCorrect = false;
            }

            return(ResultManager.IsCorrect);
        }
        public bool Approve(int id)
        {
            ResultManager.IsCorrect = false;
            //initial validations
            //-sys validations
            if (id == -1)
            {
                ResultManager.Add(ErrorDefault, Trace + "Approve.111 No se recibio el parámetro id");
                return(false);
            }
            //-business validations

            ContractDistributor auxContract = null;

            try
            {
                auxContract = Repository.ContractsDistributor.Get(id);
            }
            catch (Exception ex)
            {
                ResultManager.Add(ErrorDefault, Trace + "Approve.511 Excepción al obtener la información del contrato id '" + id + "'" + ex.Message);
                return(false);
            }

            return(Approve(auxContract));
        }
        public bool UpdateAndApprove(ContractDistributor model)
        {
            ResultManager.IsCorrect = false;
            //initial validations
            //-sys validations
            if (model == null)
            {
                ResultManager.Add(ErrorDefault, Trace + "UpdateAndApprove.111 No se recibio el modelo");
                return(false);
            }
            //-business validations

            //approve
            ContractDistributor auxContract = null;
            BayerEmployee       auxRTV      = new BayerEmployee();

            try
            {
                auxContract = Repository.ContractsDistributor.Get(model.Id);

                auxContract.AmountGoalQ1    = model.AmountGoalQ1;
                auxContract.AmountGoalQ2    = model.AmountGoalQ2;
                auxContract.AmountGoalQ3    = model.AmountGoalQ3;
                auxContract.AmountGoalQ4    = model.AmountGoalQ4;
                auxContract.AmountGoalTotal = model.AmountGoalTotal;

                auxContract.ContractDistributorStatusId = 3;

                //set rtv info for email send (will be lost from auxContract after Repository.Complete())
                auxRTV.EMail = auxContract.RTVBayerEmployee.EMail;
                auxRTV.Name  = auxContract.RTVBayerEmployee.NameDisplay;

                Repository.Complete();
                ResultManager.IsCorrect = true;
            }
            catch (Exception ex)
            {
                ResultManager.Add(ErrorDefault, Trace + "UpdateAndApprove.511 Excepción al crear el nuevo elemento" + ex.Message);
            }

            if (ResultManager.IsCorrect)
            {
                ResultManager.Add("El convenio ha sido actualizado", "");

                if (SendEmailContractApproved(auxRTV.Name, auxRTV.EMail, auxContract.Id, auxContract.IdB))
                {
                    ResultManager.Add("Se ha enviado un correo al RTV para la aprobación final", "");
                }
                else
                {
                    ResultManager.Add("No se pudo enviar actualización por correo al RTV");
                }
            }

            return(ResultManager.IsCorrect);
        }
Пример #6
0
        public ActionResult Edit(int Id)
        {
            ContractDistributor itemToEdit = controller.Retrieve(Id);

            if (controller.ResultManager.IsCorrect)
            {
                return(View(itemToEdit));
            }

            NotifyUser(resultManager: controller.ResultManager);
            return(RedirectToAction("Index"));
        }
        public bool SendToDistributor(int id)
        {
            ResultManager.IsCorrect = false;
            //initial validations
            //-sys validations
            //-business validations
            if (id < 1)
            {
                ResultManager.Add(ErrorDefault, Trace + "SendToDistributor.111 No se recibio el parámetro id");
                return(false);
            }

            //update status to 4: review-distributor
            ContractDistributor auxContract = null;

            try
            {
                auxContract = Repository.ContractsDistributor.Get(id);
                auxContract.ContractDistributorStatusId = 4;

                Repository.Complete();
                ResultManager.IsCorrect = true;
            }
            catch (Exception ex)
            {
                ResultManager.Add(ErrorDefault, Trace + "SendToDistributor.511 Excepción al actualizar el elemento" + ex.Message);
            }

            if (ResultManager.IsCorrect)
            {
                ResultManager.Add("El convenio ha sido actualizado", "");
                try
                {
                    //send email to subdistributor
                    DistributorEmployee auxDistributorEmployee = Repository.Distributors.Get(id).DistributorUsers.FirstOrDefault();
                    if (SendEmailToDistributorRequestApproval(auxDistributorEmployee.NameDisplay, auxDistributorEmployee.EMail, auxDistributorEmployee.Distributor.CurrentContract.Id, auxDistributorEmployee.Distributor.CurrentContract.IdB))
                    {
                        ResultManager.Add("Se ha enviado un correo al distribuidor", "");
                    }
                    else
                    {
                        ResultManager.Add("No se pudo enviar actualización por correo al distribuidor");
                    }
                }
                catch (Exception ex)
                {
                    ResultManager.Add(ErrorDefault, Trace + "Create.511 Excepción al crear el convenio: " + ex.Message);
                }
            }

            return(ResultManager.IsCorrect);
        }
Пример #8
0
        public ActionResult Create(ContractDistributor model)
        {
            if (controller.Create(model, true) || controller.ResultManager.IsCorrect)
            {
                NotifyUser(messageOk: "Contracto creado correctamente");
                return(RedirectToAction("Index"));
            }

            PSD.Controller.DistributorController distributorController = new PSD.Controller.DistributorController(Configurations);
            model.Distributor = distributorController.RetrieveDistributor(model.DistributorId);

            NotifyUser(resultManager: controller.ResultManager);
            return(View(model));
        }
        public bool Approve(int contractId)
        {
            ResultManager.IsCorrect = false;
            //initial validations
            //-sys validations
            if (contractId == -1)
            {
                ResultManager.Add(ErrorDefault, Trace + "Approve.111 No se recibio el parámetro id");
                return(false);
            }
            //-business validations

            //approve
            int auxDistributorId            = -1;
            ContractDistributor auxContract = null;

            try
            {
                auxContract = Repository.ContractsDistributor.Get(contractId);
                auxContract.ContractDistributorStatusId = 3;

                auxDistributorId = auxContract.DistributorId;

                Repository.Complete();
                ResultManager.IsCorrect = true;
            }
            catch (Exception ex)
            {
                ResultManager.Add(ErrorDefault, Trace + "Approve.511 Excepción al crear el nuevo elemento" + ex.Message);
            }

            if (ResultManager.IsCorrect)
            {
                ResultManager.Add("El convenio ha sido actualizado", "");
                DistributorEmployee auxDistributorEmployee = Repository.Distributors.Get(auxDistributorId).DistributorUsers.FirstOrDefault();
                if (SendEmailContractApproved(auxDistributorEmployee.NameDisplay, auxDistributorEmployee.EMail, contractId, auxContract.IdB))
                {
                    ResultManager.Add("Se ha enviado un correo al distribuidor", "");
                }
                else
                {
                    ResultManager.Add("No se pudo enviar actualización por correo al distribuidor");
                }
            }

            return(ResultManager.IsCorrect);
        }
Пример #10
0
        public ActionResult Edit(ContractDistributor model)
        {
            //sys validations
            if (model == null)
            {
                NotifyUser(errorDefault, controllerTraceId + "Edit.111 No se recibió el modelo");
                return(RedirectToAction("Index"));
            }

            if (controller.UpdateAndApprove(model) && controller.ResultManager.IsCorrect)
            {
                NotifyUser(messageOk: controller.ResultManager.ToDescriptionString(), messageDebug: controllerTraceId + "Approve.831 Contrato '[" + model.Id + "]:" + model.IdB + "' actualizado");
                return(RedirectToAction("Index"));
            }

            NotifyUser(resultManager: controller.ResultManager);
            return(RedirectToAction("Detail", new { Id = model.Id }));
        }
Пример #11
0
        public ActionResult Create(int Id = -1)
        {
            //sys validations
            if (Id == -1)
            {
                NotifyUser(errorDefault, controllerTraceId + "Create.111 No se recibió el parámetro id del Distribuidor");
                return(RedirectToAction("Index"));
            }

            ContractDistributor item = ContractDistributor.NewEmpty();

            PSD.Controller.DistributorController distributorController = new Controller.DistributorController(Configurations);
            item.Distributor          = distributorController.RetrieveDistributor(Id);
            item.DistributorId        = Id;
            item.RegisteredZoneName   = item.Distributor.Address.AddressColony.AddressPostalCode.AddressMunicipality.Zone.Name;
            item.RegisteredRegionName = item.Distributor.Address.AddressColony.AddressPostalCode.AddressMunicipality.Zone.RegionName;

            return(View(item));
        }
Пример #12
0
        public ActionResult Detail(int Id = -1)
        {
            //sys validations
            if (Id == -1)
            {
                NotifyUser(errorDefault, controllerTraceId + "Detail.111 No se recibió el parámetro Id");
                return(RedirectToAction("Index"));
            }

            ContractDistributor item = controller.Retrieve(Id);

            if (item != null && controller.ResultManager.IsCorrect)
            {
                return(View(item));
            }

            NotifyUser(resultManager: controller.ResultManager);
            return(RedirectToAction("Index"));
        }
        public bool RequestApprovalGRV(int id)
        {
            ResultManager.IsCorrect = false;
            //initial validations
            //-sys validations
            //-business validations
            if (id < 1)
            {
                ResultManager.Add(ErrorDefault, Trace + "SendToDistributor.111 No se recibio el parámetro id");
                return(false);
            }

            //retrieve contract details
            ContractDistributor auxContract = null;

            try
            {
                auxContract = Repository.ContractsDistributor.Get(id);
            }
            catch (Exception ex)
            {
                ResultManager.Add(ErrorDefault, Trace + "SendToDistributor.511 Excepción al actualizar el elemento" + ex.Message);
            }

            if (auxContract != null)
            {
                if (SendEmailToApproveGRV(auxContract.GRVBayerEmployee.NameDisplay, auxContract.GRVBayerEmployee.EMail, id, auxContract.IdB))
                {
                    ResultManager.Add("Se ha enviado un correo al GRV solicitando la aprobación del convenio", "");
                    ResultManager.IsCorrect = true;
                }
                else
                {
                    ResultManager.Add("No se pudo enviar la solicitud por correo al GRV");
                }
            }

            return(ResultManager.IsCorrect);
        }
Пример #14
0
        public ActionResult Edit(ContractDistributor model, string postAction = "")
        {
            if (model == null)
            {
                return(RedirectToError(messageDebug: controllerTraceId + "DistributorCreate[Post].111 No model was received"));
            }

            bool sendToDistributorReview = false;

            switch (postAction)
            {
            case "updateOnly": sendToDistributorReview = false; break;

            case "updateAndSendToDistributor": sendToDistributorReview = true; break;
            }
            if (controller.Update(model, sendToDistributorReview) && controller.ResultManager.IsCorrect)
            {
                NotifyUser(messageOk: controller.ResultManager.ToDescriptionString(), messageDebug: controllerTraceId + "Edit.831 Contrato '[" + model.Id + "]:" + model.IdB + "' actualizado");
                return(RedirectToAction("Index"));
            }

            NotifyUser(resultManager: controller.ResultManager);
            return(View(model));
        }
        public bool Update(ContractDistributor model, bool sendToDistributorReview)
        {
            ResultManager.IsCorrect = false;
            //initial validations
            //-sys validations
            if (model == null)
            {
                ResultManager.Add(ErrorDefault, Trace + "Update.111 No se recibio el modelo");
                return(false);
            }
            //-business validations

            /*if (string.IsNullOrWhiteSpace(model.IdB))
             * {
             *  ResultManager.Add("El id del convenio no puede estar vacio");
             *  return false;
             * }
             * if (model.DistributorId == 0 || model.DistributorId == -1)
             * {
             *  ResultManager.Add("El nombre del cultivo no puede estar vacio");
             *  return false;
             * }
             * if (model.GRVBayerEmployeeId == 0 || model.GRVBayerEmployeeId == -1)
             * {
             *  ResultManager.Add("Se debe tener asignado un GRV");
             *  return false;
             * }
             * if (model.RTVBayerEmployeeId == 0 || model.RTVBayerEmployeeId == -1)
             * {
             *  ResultManager.Add("Se debe tener asignado un RTV");
             *  return false;
             * }
             * if (model.Year == 0 || model.Year == -1)
             * {
             *  ResultManager.Add("Se debe indicar el año del convenio");
             *  return false;
             * }
             * if (string.IsNullOrWhiteSpace(model.RegisteredZoneName))
             * {
             *  ResultManager.Add("Debe haber una zona asignada");
             *  return false;
             * }
             * if (model.AmountGoalQ1Pre == 0 || model.AmountGoalQ1Pre == -1)
             * {
             *  ResultManager.Add("El monto Q1 no puede estar vacio");
             *  return false;
             * }
             * if (model.AmountGoalQ2Pre == 0 || model.AmountGoalQ2Pre == -1)
             * {
             *  ResultManager.Add("El monto Q2 no puede estar vacio");
             *  return false;
             * }
             * if (model.AmountGoalQ3Pre == 0 || model.AmountGoalQ3Pre == -1)
             * {
             *  ResultManager.Add("El monto Q3 no puede estar vacio");
             *  return false;
             * }
             * if (model.AmountGoalQ4Pre == 0 || model.AmountGoalQ4Pre == -1)
             * {
             *  ResultManager.Add("El monto Q4 no puede estar vacio");
             *  return false;
             * }*/
            if (model.AmountGoalTotalPre == 0 || model.AmountGoalTotalPre == -1)
            {
                ResultManager.Add("El total de monto meta no puede estar vacio");
                return(false);
            }

            //update item
            try
            {
                ContractDistributor auxContract = Repository.ContractsDistributor.Get(model.Id);

                auxContract.AmountGoalQ1       = auxContract.AmountGoalQ1Pre = model.AmountGoalQ1Pre;
                auxContract.AmountGoalQ2       = auxContract.AmountGoalQ2Pre = model.AmountGoalQ2Pre;
                auxContract.AmountGoalQ3       = auxContract.AmountGoalQ3Pre = model.AmountGoalQ3Pre;
                auxContract.AmountGoalQ4       = auxContract.AmountGoalQ4Pre = model.AmountGoalQ4Pre;
                auxContract.AmountGoalTotalPre = auxContract.AmountGoalTotal = model.AmountGoalTotalPre;

                //auxContract.ContractFilePath = string.Empty;

                if (sendToDistributorReview)
                {
                    auxContract.ContractDistributorStatusId = 4;
                }

                Repository.Complete();
                ResultManager.IsCorrect = true;
            }
            catch (Exception ex)
            {
                ResultManager.Add(ErrorDefault, Trace + "Update.511 Excepción al crear el nuevo elemento" + ex.Message);
            }

            if (ResultManager.IsCorrect)
            {
                ResultManager.Add("El convenio ha sido actualizado", "");
                if (sendToDistributorReview)
                {
                    DistributorEmployee auxDistributorEmployee = Repository.Distributors.Get(model.DistributorId).DistributorUsers.FirstOrDefault();
                    if (SendEmailToDistributorContractUpdated(auxDistributorEmployee.NameDisplay, auxDistributorEmployee.EMail, model.Id, model.IdB))
                    {
                        ResultManager.Add("Se ha enviado un correo al distribuidor", "");
                    }
                    else
                    {
                        ResultManager.Add("No se pudo enviar actualización por correo al distribuidor");
                    }
                }
            }

            return(ResultManager.IsCorrect);
        }
        public bool Create(ContractDistributor model, bool sendToDistributorReview)
        {
            ResultManager.IsCorrect = false;
            //initial validations
            //-sys validations
            if (model == null)
            {
                ResultManager.Add(ErrorDefault, Trace + "Create.111 No se recibio el modelo");
                return(false);
            }
            //-business validations
            if (model.DistributorId == 0 || model.DistributorId == -1)
            {
                ResultManager.Add("Se debe seleccionar un distribuidor");
                return(false);
            }

            /*
             * if (model.GRVBayerEmployeeId == 0 || model.GRVBayerEmployeeId == -1)
             * {
             *  ResultManager.Add("Se debe tener asignado un GRV");
             *  return false;
             * }
             * if (model.RTVBayerEmployeeId == 0 || model.RTVBayerEmployeeId == -1)
             * {
             *  ResultManager.Add("Se debe tener asignado un RTV");
             *  return false;
             * }
             */
            if (model.Year == 0 || model.Year == -1)
            {
                ResultManager.Add("Se debe indicar el año del convenio");
                return(false);
            }
            if (string.IsNullOrWhiteSpace(model.RegisteredZoneName))
            {
                ResultManager.Add("Debe haber una zona asignada");
                return(false);
            }

            /*deprecated: it can be some of these are not available
             * if (model.AmountGoalQ1Pre == 0 || model.AmountGoalQ1Pre == -1)
             * {
             *  ResultManager.Add("El monto Q1 no puede estar vacio");
             *  return false;
             * }
             * if (model.AmountGoalQ2Pre == 0 || model.AmountGoalQ2Pre == -1)
             * {
             *  ResultManager.Add("El monto Q2 no puede estar vacio");
             *  return false;
             * }
             * if (model.AmountGoalQ3Pre == 0 || model.AmountGoalQ3Pre == -1)
             * {
             *  ResultManager.Add("El monto Q3 no puede estar vacio");
             *  return false;
             * }
             * if (model.AmountGoalQ4Pre == 0 || model.AmountGoalQ4Pre == -1)
             * {
             *  ResultManager.Add("El monto Q4 no puede estar vacio");
             *  return false;
             * }
             */
            if (model.AmountGoalTotalPre == 0 || model.AmountGoalTotalPre == -1)
            {
                ResultManager.Add("El total de monto meta no puede estar vacio");
                return(false);
            }

            //insert new item
            try
            {
                ContractDistributor newContract = new ContractDistributor();

                newContract.IdB = Repository.AppConfigurations.IdBCounterGetNextContractDistributor();
                newContract.GRVBayerEmployeeId   = CurrentUser.Id;
                newContract.RTVBayerEmployeeId   = CurrentUser.Id;
                newContract.RegisteredZoneName   = model.RegisteredZoneName;
                newContract.RegisteredRegionName = model.RegisteredRegionName;
                newContract.Year               = model.Year;
                newContract.AmountGoalQ1       = newContract.AmountGoalQ1Pre = model.AmountGoalQ1Pre;
                newContract.AmountGoalQ2       = newContract.AmountGoalQ2Pre = model.AmountGoalQ2Pre;
                newContract.AmountGoalQ3       = newContract.AmountGoalQ3Pre = model.AmountGoalQ3Pre;
                newContract.AmountGoalQ4       = newContract.AmountGoalQ4Pre = model.AmountGoalQ4Pre;
                newContract.AmountGoalTotalPre = newContract.AmountGoalTotal = model.AmountGoalTotalPre;

                //default values
                newContract.ContractDate     = Common.Dates.Today;
                newContract.ContractFilePath = string.Empty;

                if (sendToDistributorReview)
                {
                    newContract.ContractDistributorStatusId = 4;//4:revision distribuidor
                }
                else
                {
                    newContract.ContractDistributorStatusId = 3;//3:revision bayer
                }

                //relate contract as current distributor contract
                newContract.DistributorId = model.DistributorId;
                newContract.Distributor   = Repository.Distributors.Get(newContract.DistributorId);
                if (newContract.Distributor.CurrentContract == null)
                {//first contract
                    newContract.DiscountType = 0;
                }
                else
                {//contract renovation
                    newContract.DiscountType = 1;
                }
                newContract.Distributor.CurrentContract = newContract;
                //distributorController = null;//release so we can call main Repository.Complete

                Repository.ContractsDistributor.Add(newContract);
                Repository.Complete();
                ResultManager.IsCorrect = true;
            }
            catch (Exception ex)
            {
                ResultManager.Add(ErrorDefault, Trace + "Create.511 Excepción al crear el nuevo elemento", ex);
            }

            if (ResultManager.IsCorrect)
            {
                ResultManager.Add("El convenio ha sido actualizado", "");
                try
                {
                    //send email to subdistributor
                    DistributorEmployee auxDistributorEmployee = Repository.Distributors.Get(model.DistributorId).DistributorUsers.FirstOrDefault();
                    if (SendEmailToDistributorRequestApproval(auxDistributorEmployee.NameDisplay, auxDistributorEmployee.EMail, auxDistributorEmployee.Distributor.CurrentContract.Id, auxDistributorEmployee.Distributor.CurrentContract.IdB))
                    {
                        ResultManager.Add("Se ha enviado un correo al distribuidor", "");
                    }
                    else
                    {
                        ResultManager.Add("No se pudo enviar actualización por correo al distribuidor");
                    }
                }
                catch (Exception ex)
                {
                    ResultManager.Add(ErrorDefault, Trace + "Create.511 Excepción al crear el convenio: " + ex.Message);
                }
            }

            return(ResultManager.IsCorrect);
        }