Пример #1
0
        public async Task <IActionResult> Edit(Guid id, CancellationToken token)
        {
            var entity = await _context.Agreements
                         .AsNoTracking()
                         .Where(c => c.Id == id)
                         .ProjectTo <AgreementViewModel>(_configurationProvider)
                         .SingleOrDefaultAsync(token);

            if (entity == null)
            {
                throw SheaftException.NotFound();
            }

            var deliveries = await _context.DeliveryModes
                             .Where(d => d.ProducerId == entity.Producer.Id && (int)d.Kind > 4)
                             .ToListAsync(token);

            var catalogs = await _context.Catalogs
                           .Where(d => d.ProducerId == entity.Producer.Id && d.Kind == CatalogKind.Stores)
                           .ToListAsync(token);

            ViewBag.Deliveries = deliveries.Select(d => new SelectListItem(d.Name, d.Id.ToString("D"))).ToList();
            ViewBag.Catalogs   = catalogs.Select(d => new SelectListItem(d.Name, d.Id.ToString("D"))).ToList();

            return(View(entity));
        }
Пример #2
0
        public async Task <IActionResult> Edit(Guid id, CancellationToken token)
        {
            var entity = await _context.BankAccounts
                         .SingleOrDefaultAsync(c => c.Id == id, token);

            if (entity == null)
            {
                throw SheaftException.NotFound();
            }

            ViewBag.Countries = await GetCountries(token);

            return(View(new BankAccountViewModel
            {
                Id = entity.Id,
                Identifier = entity.Identifier,
                BIC = entity.BIC,
                IBAN = entity.IBAN,
                IsActive = entity.IsActive,
                Name = entity.Name,
                Owner = entity.Owner,
                OwnerId = entity.UserId,
                Address = new AddressViewModel
                {
                    Line1 = entity.Line1,
                    Line2 = entity.Line2,
                    City = entity.City,
                    Zipcode = entity.Zipcode,
                    Country = entity.Country
                }
            }));
        }
Пример #3
0
        public async Task <IActionResult> Edit(Guid id, CancellationToken token)
        {
            var requestUser = await GetRequestUserAsync(token);

            if (!requestUser.IsImpersonating())
            {
                return(RedirectToAction("Impersonate", "Account"));
            }

            var entity = await _context.Products
                         .AsNoTracking()
                         .Where(c => c.Id == id)
                         .ProjectTo <ProductViewModel>(_configurationProvider)
                         .SingleOrDefaultAsync(token);

            if (entity == null)
            {
                throw SheaftException.NotFound();
            }

            ViewBag.Tags = await GetTags(token);

            ViewBag.Returnables = await GetReturnables(requestUser, token);

            return(View(entity));
        }
Пример #4
0
        public async Task <IActionResult> Edit(Guid id, CancellationToken token)
        {
            var entity = await _context.Users.OfType <User>()
                         .AsNoTracking()
                         .Where(c => c.Id == id)
                         .SingleOrDefaultAsync(token);

            if (entity == null)
            {
                throw SheaftException.NotFound();
            }

            var controller = string.Empty;

            switch (entity.Kind)
            {
            case ProfileKind.Consumer:
                controller = "Consumers";
                break;

            case ProfileKind.Producer:
                controller = "Producers";
                break;

            case ProfileKind.Store:
                controller = "Stores";
                break;
            }

            return(RedirectToAction("Edit", controller, new { id }));
        }
Пример #5
0
        public async Task <IActionResult> Edit(Guid id, CancellationToken token)
        {
            var entity = await _context.Users
                         .AsNoTracking()
                         .Where(c => c.Id == id)
                         .SingleOrDefaultAsync(token);

            if (entity == null)
            {
                throw SheaftException.NotFound();
            }

            if (entity.Kind == ProfileKind.Consumer)
            {
                return(RedirectToAction("Edit", "Consumers", new { id }));
            }

            if (entity.Kind == ProfileKind.Producer)
            {
                return(RedirectToAction("Edit", "Producers", new { id }));
            }

            if (entity.Kind == ProfileKind.Store)
            {
                return(RedirectToAction("Edit", "Stores", new { id }));
            }

            return(RedirectToAction("Index", "Dashboard"));
        }
Пример #6
0
        public async Task <TResponse> Handle(TRequest request, CancellationToken cancellationToken,
                                             RequestHandlerDelegate <TResponse> next)
        {
            var type = typeof(TRequest).Name;

            try
            {
                return(await next());
            }
            catch (SheaftException sheaftException)
            {
                _logger.LogError(sheaftException, $"Sheaft error on executing {type} : {sheaftException.Message}");
                throw;
            }
            catch (DbUpdateConcurrencyException dbUpdateConcurrency)
            {
                _logger.LogError(dbUpdateConcurrency,
                                 $"DbConcurrency error on executing {type} : {dbUpdateConcurrency.Message}");

                throw SheaftException.Conflict(dbUpdateConcurrency);
            }
            catch (DbUpdateException dbUpdate)
            {
                _logger.LogError(dbUpdate, $"DbUpdate error on executing {type} : {dbUpdate.Message}");

                if (dbUpdate.InnerException != null &&
                    dbUpdate.InnerException.Message.Contains("duplicate key row"))
                {
                    throw SheaftException.AlreadyExists(dbUpdate);
                }

                throw SheaftException.BadRequest(dbUpdate);
            }
            catch (NotSupportedException notSupported)
            {
                _logger.LogError(notSupported, $"Not supported error on executing {type} : {notSupported.Message}");
                throw SheaftException.Unexpected(notSupported);
            }
            catch (InvalidOperationException invalidOperation)
            {
                if (invalidOperation.Source == "Microsoft.EntityFrameworkCore" &&
                    invalidOperation.Message.StartsWith("Enumerator failed to MoveNextAsync"))
                {
                    _logger.LogWarning(invalidOperation, $"Entity not found while processing {type}");
                    throw SheaftException.NotFound(invalidOperation);
                }

                _logger.LogError(invalidOperation,
                                 $"Invalid operation error on executing {type} : {invalidOperation.Message}");

                throw SheaftException.Unexpected(invalidOperation);
            }
            catch (Exception e)
            {
                _logger.LogError(e, $"Unexpected error on executing {type} : {e.Message}");
                throw SheaftException.Unexpected(e);
            }
        }
Пример #7
0
        public void RemovePurchaseOrders(IEnumerable <PurchaseOrder> purchaseOrders)
        {
            if (Status != DeliveryStatus.Waiting)
            {
                throw SheaftException.Validation(
                          "Impossible de modifier les commandes d'une livraison qui n'est pas en attente");
            }

            if (PurchaseOrders == null)
            {
                throw SheaftException.NotFound("Cette livraison ne contient pas de commandes.");
            }

            foreach (var purchaseOrder in purchaseOrders)
            {
                foreach (var purchaseOrderProduct in purchaseOrder.Products)
                {
                    var preparedQuantity = 0;
                    if (purchaseOrder.PickingId.HasValue)
                    {
                        var preparedProduct = purchaseOrder.Picking.PreparedProducts.FirstOrDefault(p =>
                                                                                                    p.ProductId == purchaseOrderProduct.ProductId && p.PurchaseOrderId == purchaseOrder.Id);

                        if (preparedProduct != null)
                        {
                            preparedQuantity = preparedProduct.Quantity;
                        }
                    }
                    else
                    {
                        preparedQuantity = purchaseOrderProduct.Quantity;
                    }

                    var existingProduct = Products.FirstOrDefault(p =>
                                                                  p.ProductId == purchaseOrderProduct.ProductId && p.RowKind == ModificationKind.ToDeliver);

                    if (existingProduct == null)
                    {
                        continue;
                    }

                    existingProduct.RemoveQuantity(preparedQuantity);
                    if (existingProduct.Quantity <= 0)
                    {
                        foreach (var productToRemove in Products
                                 .Where(p => p.ProductId == purchaseOrderProduct.ProductId).ToList())
                        {
                            Products.Remove(productToRemove);
                        }
                    }
                }

                PurchaseOrders.Remove(purchaseOrder);
            }

            Refresh();
        }
Пример #8
0
        public void RemoveClosing(Guid id)
        {
            var closing = Closings.SingleOrDefault(r => r.Id == id);

            if (closing == null)
            {
                throw SheaftException.NotFound("La plage de fermeture pour ce mode de livraison est introuvable.");
            }

            Closings.Remove(closing);
            ClosingsCount = Closings?.Count ?? 0;
        }
Пример #9
0
        public void RemovePurchaseOrders(IEnumerable <PurchaseOrder> purchaseOrders)
        {
            if (Status == PickingStatus.Completed)
            {
                throw SheaftException.Validation(
                          "Impossible de modifier les commandes d'une préparation qui est terminée.");
            }

            if (PurchaseOrders == null)
            {
                throw SheaftException.NotFound("Cette préparation ne contient pas de commandes.");
            }

            if (purchaseOrders == null || !purchaseOrders.Any())
            {
                return;
            }

            foreach (var purchaseOrder in purchaseOrders)
            {
                foreach (var purchaseOrderProduct in purchaseOrder.Products)
                {
                    var product = ProductsToPrepare.FirstOrDefault(p =>
                                                                   p.ProductId == purchaseOrderProduct.ProductId && p.PurchaseOrderId == purchaseOrder.Id);

                    if (product == null)
                    {
                        continue;
                    }

                    ProductsToPrepare.Remove(product);

                    var preparedProduct = PreparedProducts.FirstOrDefault(p =>
                                                                          p.ProductId == purchaseOrderProduct.ProductId && p.PurchaseOrderId == purchaseOrder.Id);

                    if (preparedProduct == null)
                    {
                        continue;
                    }

                    PreparedProducts.Remove(preparedProduct);
                }

                PurchaseOrders.Remove(purchaseOrder);

                if (purchaseOrder.Status != PurchaseOrderStatus.Accepted)
                {
                    purchaseOrder.SetStatus(PurchaseOrderStatus.Accepted, true);
                }
            }

            Refresh();
        }
Пример #10
0
        public async Task <IActionResult> Edit(Guid id, CancellationToken token)
        {
            var entity = await _context.Users.OfType <Consumer>()
                         .Where(c => c.Id == id)
                         .ProjectTo <ConsumerViewModel>(_configurationProvider)
                         .SingleOrDefaultAsync(token);

            if (entity == null)
            {
                throw SheaftException.NotFound();
            }

            ViewBag.LegalsId = (await _context.Legals.FirstOrDefaultAsync(c => c.User.Id == id, token))?.Id;
            return(View(entity));
        }
Пример #11
0
        public async Task <IActionResult> Edit(Guid id, CancellationToken token)
        {
            var entity = await _context.DeliveryBatches
                         .AsNoTracking()
                         .Where(c => c.Id == id)
                         .ProjectTo <DeliveryBatchViewModel>(_configurationProvider)
                         .SingleOrDefaultAsync(token);

            if (entity == null)
            {
                throw SheaftException.NotFound();
            }

            return(View(entity));
        }
Пример #12
0
        public void RemoveDelivery(Delivery delivery)
        {
            if (Deliveries == null)
            {
                throw SheaftException.NotFound("Cette tournée ne contient pas de livraisons.");
            }

            Deliveries.Remove(delivery);
            Refresh();

            if (DeliveriesCount < 1)
            {
                Status = DeliveryBatchStatus.Cancelled;
            }
        }
Пример #13
0
        public void CompleteDelivery(IEnumerable <Tuple <DeliveryProduct, int, ModificationKind> > returnedProducts = null,
                                     IEnumerable <KeyValuePair <Returnable, int> > returnedReturnables = null, string receptionedBy = null,
                                     string comment = null)
        {
            if (Status is DeliveryStatus.Delivered)
            {
                throw SheaftException.Validation("La livraison est déjà validée.");
            }

            ReceptionedBy = receptionedBy;
            Comment       = comment;
            StartedOn ??= DateTimeOffset.UtcNow;
            DeliveredOn = DateTimeOffset.UtcNow;
            Status      = DeliveryStatus.Delivered;

            if (returnedProducts != null && returnedProducts.Any())
            {
                foreach (var productReturned in returnedProducts)
                {
                    var product = Products.FirstOrDefault(p => p.ProductId == productReturned.Item1.ProductId);
                    if (product == null)
                    {
                        throw SheaftException.NotFound(
                                  $"Le produit {productReturned.Item1.Name} retourné est introuvable dans la liste des produits livrés.");
                    }

                    if (productReturned.Item3 != ModificationKind.Excess && product.Quantity < productReturned.Item2)
                    {
                        throw SheaftException.NotFound(
                                  $"Le produit {productReturned.Item1.Name} possède une quantité retournée supérieure à la quantité livrée.");
                    }

                    Products.Add(new DeliveryProduct(product, productReturned.Item2, productReturned.Item3));
                }

                Refresh();
            }

            if (returnedReturnables != null && returnedReturnables.Any())
            {
                SetReturnedReturnables(returnedReturnables);
            }

            foreach (var purchaseOrder in PurchaseOrders)
            {
                purchaseOrder.SetStatus(PurchaseOrderStatus.Delivered, true);
            }
        }
Пример #14
0
        public void EditSetting(Guid settingId, string value)
        {
            if (Settings == null)
            {
                throw SheaftException.NotFound("Aucun paramètre trouvé.");
            }

            var setting = Settings.SingleOrDefault(s => s.SettingId == settingId);

            if (setting == null)
            {
                throw SheaftException.NotFound("Le paramètre est introuvable.");
            }

            setting.SetValue(value);
        }
Пример #15
0
        public async Task <IActionResult> Download(Guid id, CancellationToken token)
        {
            var entity = await _context.Legals
                         .SelectMany(c => c.Documents)
                         .ProjectTo <DocumentViewModel>(_configurationProvider)
                         .SingleOrDefaultAsync(c => c.Id == id, token);

            if (entity == null)
            {
                throw SheaftException.NotFound();
            }

            var data = await DownloadDocumentAsync(id, await GetRequestUserAsync(token), token);

            return(File(data, "application/octet-stream", entity.Name + ".zip"));
        }
Пример #16
0
        public void RemoveSetting(Guid settingId)
        {
            if (Settings == null)
            {
                throw SheaftException.NotFound("Aucun paramètre trouvé.");
            }

            var setting = Settings.SingleOrDefault(s => s.SettingId == settingId);

            if (setting == null)
            {
                throw SheaftException.NotFound("Le paramètre est introuvable.");
            }

            Settings.Remove(setting);
            SettingsCount = Settings?.Count ?? 0;
        }
Пример #17
0
        public void RemovePicture(Guid id)
        {
            if (Pictures == null || Pictures.Any())
            {
                throw SheaftException.NotFound("Cet utilisateur ne contient aucune images.");
            }

            var existingPicture = Pictures.FirstOrDefault(p => p.Id == id);

            if (existingPicture == null)
            {
                throw SheaftException.NotFound("L'image est introuvable.");
            }

            Pictures.Remove(existingPicture);
            PicturesCount = Pictures?.Count ?? 0;
        }
Пример #18
0
        private void RemoveBatches(IEnumerable <Batch> batches)
        {
            if (Batches == null)
            {
                throw SheaftException.NotFound("Ce produit ne contient pas de lots.");
            }

            foreach (var batch in batches)
            {
                var productBatch = Batches.FirstOrDefault(b => b.BatchId == batch.Id);
                if (productBatch == null)
                {
                    throw SheaftException.NotFound("Ce produit ne contient pas ce lot.");
                }

                Batches.Remove(productBatch);
            }
        }
Пример #19
0
        private void RemoveProducts(IEnumerable <Product> products)
        {
            if (Products == null)
            {
                throw SheaftException.NotFound("Cette observation ne contient pas de produits.");
            }

            foreach (var product in products)
            {
                var observationProduct = Products.FirstOrDefault(b => b.ProductId == product.Id);
                if (observationProduct == null)
                {
                    throw SheaftException.NotFound("Cette observation ne contient pas ce produit.");
                }

                Products.Remove(observationProduct);
            }
        }
Пример #20
0
        public async Task <IActionResult> Edit(Guid id, CancellationToken token)
        {
            var entity = await _context.Legals
                         .OfType <BusinessLegal>()
                         .Where(c => c.DeclarationId == id)
                         .ProjectTo <BusinessLegalViewModel>(_configurationProvider)
                         .SingleOrDefaultAsync(token);

            if (entity == null)
            {
                throw SheaftException.NotFound();
            }

            ViewBag.UserId  = entity.UserId;
            ViewBag.LegalId = entity.Id;

            return(View(entity.Declaration));
        }
Пример #21
0
        public async Task <IActionResult> Create(Guid legalId, CancellationToken token)
        {
            var entity = await _context.Legals
                         .Where(c => c.Id == legalId)
                         .ProjectTo <LegalViewModel>(_configurationProvider)
                         .SingleOrDefaultAsync(token);

            if (entity == null)
            {
                throw SheaftException.NotFound();
            }

            ViewBag.Kind    = entity.Kind;
            ViewBag.UserId  = entity.UserId;
            ViewBag.LegalId = entity.Id;

            return(View(new DocumentViewModel()));
        }
Пример #22
0
        private void RemoveClients(IEnumerable <User> clients)
        {
            if (Clients == null)
            {
                throw SheaftException.NotFound("Ce rappel ne contient pas de clients.");
            }

            foreach (var client in clients)
            {
                var observationClient = Clients.FirstOrDefault(b => b.ClientId == client.Id);
                if (observationClient == null)
                {
                    throw SheaftException.NotFound("Ce rappel ne contient pas ce client.");
                }

                Clients.Remove(observationClient);
            }
        }
Пример #23
0
        public async Task <IActionResult> DownloadPage(Guid documentId, Guid pageId, CancellationToken token)
        {
            var entity = await _context.Legals
                         .SelectMany(c => c.Documents)
                         .ProjectTo <DocumentViewModel>(_configurationProvider)
                         .SingleOrDefaultAsync(c => c.Id == documentId && c.Pages.Any(p => p.Id == pageId), token);

            if (entity == null)
            {
                throw SheaftException.NotFound();
            }

            var page = entity.Pages.Single(p => p.Id == pageId);
            var data = await DownloadDocumentPageAsync(documentId, pageId, await GetRequestUserAsync(token),
                                                       token);

            return(File(data, "application/octet-stream", page.FileName + page.Extension));
        }
Пример #24
0
        public async Task <IActionResult> EditLegalConsumer(Guid id, CancellationToken token)
        {
            var entity = await _context.Legals.OfType <ConsumerLegal>()
                         .AsNoTracking()
                         .Where(c => c.Id == id)
                         .ProjectTo <ConsumerLegalViewModel>(_configurationProvider)
                         .SingleOrDefaultAsync(token);

            if (entity == null)
            {
                throw SheaftException.NotFound();
            }

            ViewBag.Countries = await GetCountries(token);

            ViewBag.Nationalities = await GetNationalities(token);

            return(View(entity));
        }
Пример #25
0
        public async Task <IActionResult> AddPage(Guid documentId, CancellationToken token)
        {
            var entity = await _context.Legals
                         .Where(c => c.Documents.Any(d => d.Id == documentId))
                         .ProjectTo <LegalViewModel>(_configurationProvider)
                         .SingleOrDefaultAsync(token);

            if (entity == null)
            {
                throw SheaftException.NotFound();
            }

            ViewBag.Kind   = entity.Kind;
            ViewBag.UserId = entity.UserId;

            var document = entity.Documents.FirstOrDefault(d => d.Id == documentId);

            return(View(document));
        }
Пример #26
0
        public async Task <IActionResult> Edit(Guid id, CancellationToken token)
        {
            var entity = await _context.Rewards
                         .AsNoTracking()
                         .Where(c => c.Id == id)
                         .ProjectTo <RewardViewModel>(_configurationProvider)
                         .SingleOrDefaultAsync(token);

            if (entity == null)
            {
                throw SheaftException.NotFound();
            }

            ViewBag.Levels = await GetLevels(token);

            ViewBag.Departments = await GetDepartments(token);

            return(View(entity));
        }
Пример #27
0
        public async Task <IActionResult> Create(Guid userId, CancellationToken token)
        {
            var entity = await _context.Users
                         .AsNoTracking()
                         .Where(c => c.Id == userId)
                         .SingleOrDefaultAsync(token);

            if (entity == null)
            {
                throw SheaftException.NotFound();
            }

            if (entity.Kind != ProfileKind.Consumer)
            {
                return(RedirectToAction("CreateLegalBusiness", new { userId }));
            }

            return(RedirectToAction("CreateLegalConsumer", new { userId }));
        }
Пример #28
0
        public QuickOrderProduct RemoveProduct(Guid productId)
        {
            if (Products == null)
            {
                throw SheaftException.NotFound("Ce modèle de commande ne contient aucun produits.");
            }

            var productLine = Products.SingleOrDefault(p => p.CatalogProduct.ProductId == productId);

            if (productLine == null)
            {
                throw SheaftException.Validation("Impossible de supprimer le produit du modèle de commande, il est introuvable.");
            }

            Products.Remove(productLine);
            ProductsCount = Products?.Count ?? 0;

            return(productLine);
        }
Пример #29
0
        public async Task <IActionResult> Create(Guid userId, CancellationToken token)
        {
            var entity = await _context.Users.OfType <Domain.Business>()
                         .AsNoTracking()
                         .Where(c => c.Id == userId)
                         .SingleOrDefaultAsync(token);

            if (entity == null)
            {
                throw SheaftException.NotFound();
            }

            ViewBag.Countries = await GetCountries(token);

            return(View(new BankAccountViewModel
            {
                OwnerId = entity.Id,
                IsActive = true
            }));
        }
Пример #30
0
        public void RemoveFromCatalog(Guid catalogId)
        {
            if (CatalogsPrices == null || !CatalogsPrices.Any())
            {
                throw SheaftException.NotFound("Ce produit appartient à aucun catalogue.");
            }

            var existingCatalogPrice = CatalogsPrices.SingleOrDefault(c => c.CatalogId == catalogId);

            if (existingCatalogPrice == null)
            {
                throw SheaftException.NotFound("Ce produit n'est pas présent dans le catalogue.");
            }

            CatalogsPrices.Remove(existingCatalogPrice);
            CatalogsPricesCount = CatalogsPrices?.Count ?? 0;

            existingCatalogPrice.Catalog.DecreaseProductsCount();

            UpdateVisibleToOnRemoveCatalog(existingCatalogPrice);
        }