Exemplo n.º 1
0
 public async Task<ResponseBool> PurchasingCerkAddAsync(PurchasingClerkItem purchasingClerkItem)
 {
     ResponseBool _response = new ResponseBool { Success = false, ErrorInfo = "" };
     string url = string.Format("api/distributorservices/purchasingclerkadd");
     var httpClient = setupHttpClient();
     httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
     try
     {
         var response = await httpClient.PostAsJsonAsync(url, purchasingClerkItem);
         _response = await response.Content.ReadAsAsync<ResponseBool>();
     }
     catch (Exception ex)
     {
         _response.ErrorInfo = "Error: An error occurred when adding purchasing clerk.\n" + ex.Message;
         _log.Error("Error: An error occurred when adding purchasing clerk.", ex);
     }
     return _response;
 }
Exemplo n.º 2
0
        private async Task<ResponseBool> SavePurchasingClerkCostCentre()
        {
            ResponseBool response = new ResponseBool {Success = false};
            using (var c = NestedContainer)
            {
                _proxy = Using<IDistributorServiceProxy>(c);

                Guid ccId = User._Status == EntityStatus.New ? Guid.NewGuid() : User.CostCentre;
                PurchasingClerk pClerk =
                    Using<CostCentreFactory>(c).CreateCostCentre(ccId, CostCentreType.PurchasingClerk,
                                                                 Using<ICostCentreRepository>(c).GetById(
                                                                     GetConfigParams().CostCentreId)) as PurchasingClerk;
                pClerk.Name = User.Username;
                pClerk.CostCentreCode = Code;


                User.CostCentre = pClerk.Id;
                pClerk.User = User;
                pClerk.PurchasingClerkRoutes = GetRoutesAssigned(pClerk);

                PurchasingClerkItem pClerkItem = new PurchasingClerkItem()
                {
                    MasterId = pClerk.Id,
                    Name = pClerk.Name,
                    ParentCostCentreId = pClerk.ParentCostCentre.Id,
                    StatusId = (int)EntityStatus.Active,
                    CostCentreTypeId = (int)pClerk.CostCentreType,
                    CostCentreCode = pClerk.CostCentreCode
                };
                pClerkItem.UserItem = CreateUserItem(pClerk.Id);
                pClerkItem.PurchasingClerkRouteItems = pClerk.PurchasingClerkRoutes.Select(Map).ToList();

                AuditLogEntry = string.Format("Created purchasing clerk costcentre for user: {0};", User.Username);
                Using<IAuditLogWFManager>(c).AuditLogEntry("User Administration", AuditLogEntry);
                response = await _proxy.PurchasingCerkAddAsync(pClerkItem);
                if(response.Success)
                {
                   ChangeAllocation(pClerkItem);
                }
                MessageBox.Show(response.ErrorInfo, "Distributr: Manage purchasing clerk", MessageBoxButton.OK,
                                MessageBoxImage.Information);
            }
            return response;
        }
Exemplo n.º 3
0
        private void ChangeAllocation(PurchasingClerkItem purchasingClerkItem)
        {
            using (var container=NestedContainer)
            {
               // Route route = null;
                var _purchasingClerkRouteRepository = Using<IPurchasingClerkRouteRepository>(container);
               // var _routeRepository = Using<IRouteRepository>(container);
                //var 
                //var purchasingClerk = _costCentreFactory.CreateCostCentre(purchasingClerkItem.MasterId, CostCentreType.PurchasingClerk, hub)
                //        as PurchasingClerk;

                var existingAssignedRoutes = _purchasingClerkRouteRepository.GetAll(true)
                           .Where(n => n.PurchasingClerkRef.Id == purchasingClerkItem.MasterId);
                var deletedRouteAssignments = existingAssignedRoutes
                    .Where(n => purchasingClerkItem.PurchasingClerkRouteItems.Select(x => x.RouteId).All(x => x != n.Route.Id));
                //foreach (var item in purchasingClerkItem.PurchasingClerkRouteItems)
                //{
                //    route = _routeRepository.GetById(item.RouteId);
                //    var pcr = new PurchasingClerkRoute(item.MasterId)
                //    {
                //        Route = route,
                //        PurchasingClerkRef = new CostCentreRef { Id = purchasingClerkItem.MasterId }
                //    };
                //    _routeRepository.Save(route);
                //    purchasingClerk.PurchasingClerkRoutes.Add(pcr);
                //}

                foreach (var item in deletedRouteAssignments)
                {
                    _purchasingClerkRouteRepository.SetAsDeleted(item);
                }
            }
        }
        public HttpResponseMessage PurchasingClerkAdd(PurchasingClerkItem purchasingClerkItem)
        {
            var response = new ResponseBool { Success = false };
            using (TransactionScope scope = TransactionUtils.CreateTransactionScope())
            {
                try
                {
                    Route route = null;
                    var hub = _costCentreRepository.GetById(purchasingClerkItem.ParentCostCentreId);
                    var purchasingClerk = _costCentreFactory.CreateCostCentre(purchasingClerkItem.MasterId, CostCentreType.PurchasingClerk, hub)
                        as PurchasingClerk;
                    if (purchasingClerk == null) throw new NullReferenceException();
                    purchasingClerk.Name = purchasingClerkItem.Name;
                    purchasingClerk.CostCentreCode = purchasingClerkItem.CostCentreCode;
                    purchasingClerk.User = new User(purchasingClerkItem.UserItem.MasterId)
                                               {
                                                   CostCentre = purchasingClerkItem.MasterId,
                                                   Username = purchasingClerkItem.UserItem.Username,
                                                   Password = purchasingClerkItem.UserItem.Password,
                                                   PIN = purchasingClerkItem.UserItem.PIN,
                                                   Mobile = purchasingClerkItem.UserItem.Mobile,
                                                   UserType = (UserType) purchasingClerkItem.UserItem.UserType,
                                                   TillNumber = purchasingClerkItem.UserItem.TillNumber,
                                               };
                    var query = new QueryPurchasingClerkRoute();
                    query.ShowInactive = true;
                    var existingAssignedRoutes = _purchasingClerkRouteRepository.Query(query)
                        .Where(n => n.PurchasingClerkRef.Id == purchasingClerkItem.MasterId);

                    //var existingAssignedRoutes = _purchasingClerkRouteRepository.GetAll(true)
                    //    .Where(n => n.PurchasingClerkRef.Id == purchasingClerkItem.MasterId);
                    var deletedRouteAssignments = existingAssignedRoutes
                        .Where(n => purchasingClerkItem.PurchasingClerkRouteItems.Select(x => x.RouteId).All(x => x != n.Route.Id));
                    foreach (var item in purchasingClerkItem.PurchasingClerkRouteItems)
                    {
                        route = _routeRepository.GetById(item.RouteId);
                        if(existingAssignedRoutes.Any(p=>p.Route==route))
                        {
                            continue;
                        }
                        var pcr = new PurchasingClerkRoute(item.MasterId)
                                                       {
                                                           Route = route,
                                                           PurchasingClerkRef = new CostCentreRef {Id = purchasingClerkItem.MasterId}
                                                       };
                        //_routeRepository.Save(route);
                        purchasingClerk.PurchasingClerkRoutes.Add(pcr);
                    }

                    foreach (var item in deletedRouteAssignments)
                    {
                        _purchasingClerkRouteRepository.SetAsDeleted(item);
                    }

                    purchasingClerk._SetStatus(EntityStatus.Active);
                    _purchasingClerkRepository.Save(purchasingClerk);

                    response.Success = true;
                    response.ErrorInfo = "Successfully added purchasing clerk.";
                    scope.Complete();
                }
                catch (DomainValidationException dve)
                {
                    string errorMsg = dve.ValidationResults.Results.Aggregate("Error: Invalid fields.\n",
                                                                              (current, msg) =>
                                                                              current +
                                                                              ("\t- " + msg.ErrorMessage + "\n"));
                    response.ErrorInfo = errorMsg;
                    _log.Error(errorMsg, dve);
                }
                catch (Exception ex) //any other
                {
                    response.ErrorInfo = "Error: An error occurred when creating or updating purchasing clerk.";
                    _log.Error("Error: An error occurred when creating or updating the purchasing clerk.", ex);
                }
            }
            return Request.CreateResponse(HttpStatusCode.OK, response);

        }