Changeset f08836b


Ignore:
Timestamp:
01/06/23 15:24:07 (23 months ago)
Author:
DarkoSasanski <darko.sasanski@…>
Branches:
main
Children:
5f69a70
Parents:
e440971
Message:

Added additional checks for order and invoice logic

Location:
FullyStocked/src/main/java/com/bazi/fullystocked
Files:
4 added
6 edited

Legend:

Unmodified
Added
Removed
  • FullyStocked/src/main/java/com/bazi/fullystocked/Repositories/InvoicedArticlesRepository.java

    re440971 rf08836b  
    1212    List<InvoicedArticles> findAllByArticle(Articles article);
    1313    List<InvoicedArticles> findAllByInvoice(Invoices invoice);
    14     List<InvoicedArticles> findAllByInvoiceAndArticle(Invoices invoice, Articles article);
     14    Optional<InvoicedArticles> findByInvoiceAndArticle(Invoices invoice, Articles article);
    1515}
  • FullyStocked/src/main/java/com/bazi/fullystocked/Repositories/OrderedArticlesRepository.java

    re440971 rf08836b  
    1414    List<OrderedArticles> findAllByArticle(Articles article);
    1515    List<OrderedArticles> findAllByLocation(Locations location);
     16    List<OrderedArticles> findAllByLocationAndArticleAndArticlestatus(Locations location, Articles article, ArticleStatus status);
    1617    List<OrderedArticles> findAllByOrder(Orders order);
    17     List<OrderedArticles> findAllByOrderAndArticle(Orders order, Articles article);
     18    Optional<OrderedArticles> findByOrderAndArticle(Orders order, Articles article);
    1819    List<OrderedArticles> findAllByArticlestatus(ArticleStatus status);
    1920    List<OrderedArticles> findAllByLocationAndArticlestatus(Locations location, ArticleStatus status);
  • FullyStocked/src/main/java/com/bazi/fullystocked/Repositories/StoredArticlesRepository.java

    re440971 rf08836b  
    1111    List<StoredArticles> findAllByLocations(Locations location);
    1212    List<StoredArticles> findAllByArticle(Articles article);
    13     List<StoredArticles> findAllByArticleAndLocations(Articles article, Locations location);
     13    Optional<StoredArticles> findByArticleAndLocations(Articles article, Locations location);
    1414    List<StoredArticles> findAllByLocationsAndQuantityIsLessThanEqual(Locations location, int quantity);
    1515    List<StoredArticles> findAllByLocationsAndQuantity(Locations location, int quantity);
  • FullyStocked/src/main/java/com/bazi/fullystocked/Services/Implementations/InvoicesServiceImpl.java

    re440971 rf08836b  
    11package com.bazi.fullystocked.Services.Implementations;
    22
    3 import com.bazi.fullystocked.Models.Articles;
     3import com.bazi.fullystocked.Models.*;
     4import com.bazi.fullystocked.Models.Exceptions.ArticleAlreadyInInvoiceException;
     5import com.bazi.fullystocked.Models.Exceptions.ArticleNotAvailableException;
    46import com.bazi.fullystocked.Models.Exceptions.InvalidArgumentsException;
    5 import com.bazi.fullystocked.Models.InvoicedArticles;
    6 import com.bazi.fullystocked.Models.Invoices;
    7 import com.bazi.fullystocked.Models.Workers;
    8 import com.bazi.fullystocked.Repositories.ArticlesRepository;
    9 import com.bazi.fullystocked.Repositories.InvoicedArticlesRepository;
    10 import com.bazi.fullystocked.Repositories.InvoicesRepository;
    11 import com.bazi.fullystocked.Repositories.WorkersRepository;
     7import com.bazi.fullystocked.Repositories.*;
    128import com.bazi.fullystocked.Services.InvoicesService;
    139import org.springframework.stereotype.Service;
     
    2218    private final WorkersRepository workersRepository;
    2319    private final ArticlesRepository articlesRepository;
     20    private final StoredArticlesRepository storedArticlesRepository;
    2421
    25     public InvoicesServiceImpl(InvoicedArticlesRepository invoicedArticlesRepository, InvoicesRepository invoicesRepository, WorkersRepository workersRepository, ArticlesRepository articlesRepository) {
     22    public InvoicesServiceImpl(InvoicedArticlesRepository invoicedArticlesRepository, InvoicesRepository invoicesRepository, WorkersRepository workersRepository, ArticlesRepository articlesRepository, StoredArticlesRepository storedArticlesRepository) {
    2623        this.invoicedArticlesRepository = invoicedArticlesRepository;
    2724        this.invoicesRepository = invoicesRepository;
    2825        this.workersRepository = workersRepository;
    2926        this.articlesRepository = articlesRepository;
     27        this.storedArticlesRepository = storedArticlesRepository;
    3028    }
    3129
     
    4745        Invoices invoice = invoicesRepository.findById(invoiceId).orElseThrow(InvalidArgumentsException::new);
    4846        Articles article = articlesRepository.findById(articleId).orElseThrow(InvalidArgumentsException::new);
     47        if(invoicedArticlesRepository.findByInvoiceAndArticle(invoice, article).isPresent())
     48        {
     49            throw new ArticleAlreadyInInvoiceException();
     50        }
    4951        if(price<=0 || quantity<=0)
    5052        {
    5153            throw new InvalidArgumentsException();
     54        }
     55        StoredArticles storedArticle=storedArticlesRepository.findByArticleAndLocations(article, invoice.getWorker().getLocation()).orElseThrow(InvalidArgumentsException::new);
     56        if(quantity>storedArticle.getQuantity())
     57        {
     58            throw new ArticleNotAvailableException();
    5259        }
    5360        InvoicedArticles invoicedArticles=new InvoicedArticles(price, quantity, invoice, article);
  • FullyStocked/src/main/java/com/bazi/fullystocked/Services/Implementations/OrdersServiceImpl.java

    re440971 rf08836b  
    55import com.bazi.fullystocked.Models.Enumerations.OrderPriority;
    66import com.bazi.fullystocked.Models.Enumerations.OrderStatus;
     7import com.bazi.fullystocked.Models.Exceptions.ArticleAlreadyInOrderException;
     8import com.bazi.fullystocked.Models.Exceptions.ArticleMaxQuantityException;
    79import com.bazi.fullystocked.Models.Exceptions.InvalidArgumentsException;
    810import com.bazi.fullystocked.Repositories.*;
     
    5557    @Transactional
    5658    public Optional<Orders> addArticleToOrder(int quantity, Integer locationId, Integer articleId, Integer orderId) {
     59        Locations location=locationsRepository.findById(locationId).orElseThrow(InvalidArgumentsException::new);
     60        Articles article=articlesRepository.findById(articleId).orElseThrow(InvalidArgumentsException::new);
     61        Orders order=ordersRepository.findById(orderId).orElseThrow(InvalidArgumentsException::new);
     62        if(orderedArticlesRepository.findByOrderAndArticle(order, article).isPresent())
     63        {
     64            throw new ArticleAlreadyInOrderException();
     65        }
    5766        if(quantity<=0)
    5867        {
    5968            throw new InvalidArgumentsException();
    6069        }
    61         Locations location=locationsRepository.findById(locationId).orElseThrow(InvalidArgumentsException::new);
    62         Articles article=articlesRepository.findById(articleId).orElseThrow(InvalidArgumentsException::new);
    63         Orders order=ordersRepository.findById(orderId).orElseThrow(InvalidArgumentsException::new);
     70        int quantityAlreadyOrdered=orderedArticlesRepository.findAllByLocationAndArticleAndArticlestatus(location, article, ArticleStatus.ORDERED).
     71                stream().mapToInt(OrderedArticles::getQuantity).sum()+
     72                    orderedArticlesRepository.findAllByLocationAndArticleAndArticlestatus(location, article, ArticleStatus.DELIVERED).
     73                        stream().mapToInt(OrderedArticles::getQuantity).sum();
     74        if(article.getMaxquantityperlocation()<quantityAlreadyOrdered+quantity)
     75        {
     76            throw new ArticleMaxQuantityException();
     77        }
    6478        OrderedArticles orderedArticles=orderedArticlesRepository.save(new OrderedArticles(quantity, order, location, article));
    6579        order.getArticlesList().add(orderedArticles);
  • FullyStocked/src/main/java/com/bazi/fullystocked/Services/Implementations/StoredArticlesServiceImpl.java

    re440971 rf08836b  
    4949            throw new InvalidArgumentsException();
    5050        }
    51         StoredArticles storedArticle=storedArticlesRepository.findAllByArticleAndLocations(orderedArticle.getArticle(), orderedArticle.getLocation()).get(0);
     51        StoredArticles storedArticle=storedArticlesRepository.findByArticleAndLocations(orderedArticle.getArticle(), orderedArticle.getLocation()).orElseThrow(InvalidArgumentsException::new);
    5252        storedArticle.setQuantity(storedArticle.getQuantity()+ orderedArticle.getQuantity());
    5353        orderedArticle.setArticlestatus(ArticleStatus.PROCESSED);
Note: See TracChangeset for help on using the changeset viewer.