Exemplo n.º 1
0
        private void LoadRouteAssignment()
        {
            using (var c = NestedContainer)
            {
                AssignedRoutesList.Clear();
                UnassignedRoutesList.Clear();
                AllRoutesCache.Clear();

                var query = new QueryPurchasingClerkRoute();
                query.ShowInactive = false;

                _assignedRoutes = Using<IPurchasingClerkRouteRepository>(c).Query(query).Where(
                   n => n.PurchasingClerkRef.Id == User.CostCentre);

                //_assignedRoutes = Using<IPurchasingClerkRouteRepository>(c).GetAll().Where(
                //    n => n.PurchasingClerkRef.Id == User.CostCentre);
                _assignedRoutes.OrderBy(n => n.Route.Name).ToList().ForEach(
                    n => AssignedRoutesList.Add(new VMRouteItem() {IsSelected = false, Route = n.Route}));

                var allRoutesCache =
                    Using<IRouteRepository>(c).GetAll().OrderBy(n => n.Name)
                        .ToList();
                allRoutesCache.ForEach(n => AllRoutesCache.Add(new VMRouteItem {Route = n, IsSelected = false}));

                var unassignedRoutes =
                    AllRoutesCache.Where(n => AssignedRoutesList.All(p => p.Route.Id != n.Route.Id)).ToList();
                unassignedRoutes.ForEach(UnassignedRoutesList.Add);
            }
        }
        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);

        }