private async Task<ImportValidationResultInfo> MapAndValidate(CommoditySupplierDTO dto, int index)
        {
            return await Task.Run(() =>
            {
                var entity = _mappingService.Map(dto);
                var exist =
                    _ctx.tblCostCentre.FirstOrDefault(
                        p =>p.CostCentreType==(int)CostCentreType.CommoditySupplier &&
                        p.Name.ToLower() == dto.Name.ToLower() ||
                        p.Cost_Centre_Code != null && p.Cost_Centre_Code.Equals(dto.CostCentreCode, StringComparison.CurrentCultureIgnoreCase));

                entity.Id = exist == null ? Guid.NewGuid() : exist.Id;

                var res = _repository.Validate(entity);
                var vResult = new ImportValidationResultInfo()
                {
                    Results = res.Results,
                    Description =
                        string.Format("Row-{0} name or code=>{1}", index,
                                      entity.Name ?? entity.CostCentreCode),
                    Entity = entity
                };
                return vResult;

            });

        }
 private CommoditySupplierDTO Map(tblCostCentre tbl)
 {
     var dto = new CommoditySupplierDTO
     {
         MasterId = tbl.Id,
         DateCreated = tbl.IM_DateCreated,
         DateLastUpdated = tbl.IM_DateLastUpdated,
         StatusId = tbl.IM_Status,
         Name = tbl.Name,
         CostCentreCode = tbl.Cost_Centre_Code,                
         ParentCostCentreId = tbl.ParentCostCentreId ?? Guid.Empty,
         CostCentreTypeId = tbl.CostCentreType ?? 0,
         CommoditySupplierTypeId = tbl.CostCentreType2,
         JoinDate = tbl.JoinDate ?? new DateTime(),
         AccountNo = tbl.AccountNumber,
         PinNo = tbl.Revenue_PIN,
         BankId = tbl.BankId.HasValue? tbl.BankId.Value:Guid.Empty,
         BankBranchId = tbl.BankBranchId.HasValue? tbl.BankBranchId.Value:Guid.Empty,
         AccountName=tbl.AccountName
     };
     return dto;
 }
        private IEnumerable<CommoditySupplierDTO> ConstructDtOs(IEnumerable<ImportEntity> entities)
        {
            var items = new List<CommoditySupplierDTO>();
          
            items.AddRange(entities.Select(n => n.Fields).Select(row =>
                                                                     {
                                                                         var name = SetFieldValue(row, 1);
                                                                         var code = SetFieldValue(row, 2);
                                                                         var typ = SetFieldValue(row, 3);
                                                                         var hubcodeOrName = GetParentCostCentreId(SetFieldValue(row, 4));
                                                                         var jDate = GetDatetime(SetFieldValue(row, 5));
                                                                         var acName = SetFieldValue(row, 6);
                                                                         var acNo = SetFieldValue(row, 7);
                                                                         var pin = SetFieldValue(row, 8);
                                                                         var bnk = GetBank(SetFieldValue(row, 9));
                                                                         var branch =
                                                                             GetBankBranch(SetFieldValue(row, 10),SetFieldValue(row,9));
                                                                         
                                                                         
                                                                         if (string.IsNullOrEmpty(name))
                                                                         {
                                                                             var res = new List<ValidationResult>
                                                                                           {
                                                                                               new ValidationResult(
                                                                                                   string.Format(
                                                                                                       "Commodity supplier name cannot be empty"))
                                                                                           };
                                                                             validationResultInfos.Add(new ImportValidationResultInfo
                                                                                                           ()
                                                                             {
                                                                                 Results = res
                                                                             });
                                                                             return null;
                                                                         }
                                                                         
                                                                         if (string.IsNullOrEmpty(code))
                                                                         {
                                                                             var res = new List<ValidationResult>
                                                                                           {
                                                                                               new ValidationResult(
                                                                                                   string.Format(
                                                                                                       "Commodity supplier code cannot be empty"))
                                                                                           };
                                                                             validationResultInfos.Add(new ImportValidationResultInfo
                                                                                                           ()
                                                                             {
                                                                                 Results = res
                                                                             });
                                                                             return null;
                                                                         }

                                                                        
                                                                         if (hubcodeOrName==Guid.Empty)
                                                                         {
                                                                             var res = new List<ValidationResult>
                                                                                           {
                                                                                               new ValidationResult(
                                                                                                   string.Format(
                                                                                                       "Commodity Supplier Hub cannot be found in the DB"))
                                                                                           };
                                                                             validationResultInfos.Add(new ImportValidationResultInfo
                                                                                                           ()
                                                                             {
                                                                                 Results = res
                                                                             });
                                                                             return null;
                                                                         }
                                                                         CommoditySupplierType type = 0;
                                                                         try
                                                                         {

                                                                             Enum.TryParse(typ, true, out type);
                                                                         }
                                                                         catch
                                                                         {

                                                                         }
                                                                        var sup= new CommoditySupplierDTO()
                                                                                    {
                                                                                        Name = name,
                                                                                        CostCentreCode = code,
                                                                                        CommoditySupplierTypeId =(int)type,
                                                                                        JoinDate = jDate,
                                                                                        AccountName = acName,
                                                                                        AccountNo = acNo,
                                                                                        PinNo = pin,
                                                                                        ParentCostCentreId = hubcodeOrName
                                                                                    };
                                                                         if(bnk !=null)
                                                                             sup.BankId = bnk.Id;
                                                                         if (branch != null)
                                                                             sup.BankBranchId = branch.Id;

                                                                         return sup;
                                                                     }));
           
            return items.Where(p => p != null);

        }
예제 #4
0
        public async Task<ResponseBool> CommoditySupplierAddAsync(CommoditySupplierDTO commoditySupplierdto)
        {
            ResponseBool _response = new ResponseBool { Success = false, ErrorInfo = "" };
            string url = string.Format("api/pushmasterdata/commoditysupplier/save");
            var httpClient = setupHttpClient();
            httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            try
            {
                var response = await httpClient.PostAsJsonAsync(url, commoditySupplierdto);
                var _responseBasic = await response.Content.ReadAsAsync<ResponseBasic>();
                if (_responseBasic != null && _responseBasic.ErrorInfo == "OK")
                {
                    _response = new ResponseBool { Success = true };

                }
                else
                {
                    _response = new ResponseBool { Success = false, ErrorInfo = _responseBasic.ErrorInfo ?? "" };
                }
            }
            catch (Exception ex)
            {
                _response.ErrorInfo = "Error: An error occurred when editing the commodity supplier.\nCause: " +
                                      ex.Message;
                _log.Error("Failed to edit commodity supplier.", ex);
            }
            return _response;
        }
예제 #5
0
 public CommoditySupplier Map(CommoditySupplierDTO dto)
 {
     if (dto == null) return null;
     var commoditySupplier = Mapper.Map<CommoditySupplierDTO, CommoditySupplier>(dto);
     return commoditySupplier;
 }
 public void Save(CommoditySupplierDTO model)
 {
     CommoditySupplier commoditySupplier = new CommoditySupplier(model.MasterId);
     commoditySupplier.AccountName = model.Name;
     commoditySupplier.Name = model.Name;
     commoditySupplier.CostCentreCode = model.CostCentreCode;
     commoditySupplier.CommoditySupplierType =
         (CommoditySupplierType)model.CommoditySupplierTypeId;
     commoditySupplier.JoinDate = DateTime.Now;
     commoditySupplier.AccountNo = model.AccountNo;
     commoditySupplier.PinNo = model.PinNo;
     commoditySupplier.BankId = model.BankId;
     commoditySupplier.BankBranchId = model.BankBranchId;
     commoditySupplier._Status = EntityStatus.Active;
     commoditySupplier.ParentCostCentre = new CostCentreRef { Id = model.ParentCostCentreId };
     commoditySupplier.CostCentreType = CostCentreType.CommoditySupplier;
     _commoditySupplierRepository.Save(commoditySupplier);
 }
        public ActionResult CreateCommoditySupplier(CommoditySupplierViewModel vm)
        {
            
            ViewBag.CommoditySupplierTypeList = _commoditySupplierViewModelBuilder.CommoditySupplierType();
            ViewBag.ParentCostCentreList = _commoditySupplierViewModelBuilder.ParentCostCentre();
            ViewBag.Banks = _commoditySupplierViewModelBuilder.GetBanks();
            ViewBag.GenderList = _commodityOwnerViewModelBuilder.Gender();
            ViewBag.MaritalStatusList = _commodityOwnerViewModelBuilder.MaritalStatus();
            ViewBag.CommodityOwnerTypeList = _commodityOwnerViewModelBuilder.CommodityOwnerType();
            ViewBag.UnassignedCentresList = _centreRepository.GetAll().Select(r => new { r.Id, r.Name }).ToDictionary(d => d.Id, d => d.Name);
            LoadBranches(vm.BankId);

            if (!ModelState.IsValid)
            {
                return View(vm);
            }

            var allocatedCenter = _centreRepository.GetById(vm.SelectedCentreId);
            try
            {
                using (var tran = new TransactionScope())
                {
                    var commoditySupplierDto = new CommoditySupplierDTO
                    {
                            MasterId = vm.CommoditySupplierId,
                            Name = vm.Name,
                            AccountName = vm.AccountName,
                            AccountNo = vm.AccountNo,
                            PinNo = vm.PinNo,
                            BankId = vm.BankId,
                            BankBranchId = vm.BankBranchId,
                            CommoditySupplierTypeId = vm.CommoditySupplierType,
                            CostCentreCode = vm.CostCentreCode,
                            ParentCostCentreId = vm.ParentCostCentreId,
                        
                        };
                    _commoditySupplierViewModelBuilder.Save(commoditySupplierDto);
                    var commodityOwnerViewModel = new CommodityOwnerViewModel
                        {
                            Id=vm.CommodityOwnerId,
                            Code=vm.OwnerCode,
                            CommodityOwnerType=vm.CommodityOwnerType,
                            DateOfBirth=vm.DateOfBirth,
                            BusinessNumber=vm.BusinessNumber,
                            Description=vm.Description,
                            Email=vm.Email,
                            FaxNumber=vm.FaxNumber,
                            FirstName=vm.FirstName,
                            LastName=vm.LastName,
                            Surname=vm.Surname,
                            Gender=vm.Gender,
                            IdNo=vm.IdNo,
                            MaritalStatus=vm.MaritalStatus,
                            OfficeNumber=vm.OfficeNumber,
                            PhoneNumber=vm.PhoneNumber,
                            PhysicalAddress=vm.PhysicalAddress,
                            PinNo=vm.OwnerPinNo,
                            PostalAddress=vm.PostalAddress,
                            CommoditySupplier=vm.CommoditySupplierId
                        };

                    _commodityOwnerViewModelBuilder.Save(commodityOwnerViewModel);
                    var commodityProducerViewModel = new CommodityProducerViewModel
                        {
                            Id=vm.CommodityProducerId,
                            Code=vm.FarmCode,
                            Acrage=vm.Acrage,
                            Name=vm.FarmName,
                            RegNo=vm.RegNo,
                            PhysicalAddress=vm.FarmPhysicalAddress,
                            Description=vm.FarmDescription,
                            CommoditySupplierId=vm.CommoditySupplierId,
                            AssignedFarmCentres = new List<Centre>(){allocatedCenter},

                        };
                    _commodityProducerViewModelBuilder.Save(commodityProducerViewModel);

                    AssignCenter(vm.SelectedCentreId, vm.CommodityProducerId);

                    TempData["msg"] = "Commodity supplier Successfully Created";
                    tran.Complete();
                }


                return RedirectToAction("ListCommoditySuppliers");
            }
            catch (DomainValidationException ve)
            {
                ValidationSummary.DomainValidationErrors(ve, ModelState);
                _log.Debug("Failed to create commodity suppliers " + ve.Message);
                _log.Error("Failed to create commodity suppliers" + ve.ToString());

                return View(vm);
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("", ex.Message);
                _log.Debug("Failed to create Commodity suppliers " + ex.Message);
                _log.Error("Failed to create Commodity suppliers" + ex.ToString());

                return View(vm);
            }

        }
        public ActionResult EditCommoditySupplier(CommoditySupplierDTO vm)
        {
            ViewBag.CommoditySupplierTypeList = _commoditySupplierViewModelBuilder.CommoditySupplierType();
            ViewBag.ParentCostCentreList = _commoditySupplierViewModelBuilder.ParentCostCentre();
            ViewBag.Banks = _commoditySupplierViewModelBuilder.GetBanks();
            try
            {
                LoadBranches(vm.BankId);
                _commoditySupplierViewModelBuilder.Save(vm);
                TempData["msg"] = "Commodity supplier Successfully Edited";
                return RedirectToAction("ListCommoditySuppliers");
            }
            catch (DomainValidationException ve)
            {
                ValidationSummary.DomainValidationErrors(ve, ModelState);
                _log.Debug("Failed to edit Commodity producer " + ve.Message);
                _log.Error("Failed to edit Commodity producer" + ve.ToString());

                return View();
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("", ex.Message);
                _log.Debug("Failed to edit Commodity producer " + ex.Message);
                _log.Error("Failed to edit Commodity producer" + ex.ToString());
                return View();
            }
        }