<commit_before>/*
 * Axelor Business Solutions
 *
 * Copyright (C) 2019 Axelor (<http://axelor.com>).
 *
 * This program is free software: you can redistribute it and/or  modify
 * it under the terms of the GNU Affero General Public License, version 3,
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.axelor.apps.portal.service;

import com.axelor.apps.account.db.Invoice;
import com.axelor.apps.account.db.repo.InvoiceRepository;
import com.axelor.apps.base.service.user.UserService;
import com.axelor.apps.helpdesk.db.Ticket;
import com.axelor.apps.helpdesk.db.repo.TicketRepository;
import com.axelor.apps.project.db.Project;
import com.axelor.apps.project.db.repo.ProjectRepository;
import com.axelor.apps.sale.db.SaleOrder;
import com.axelor.apps.sale.db.repo.SaleOrderRepository;
import com.axelor.apps.stock.db.StockMove;
import com.axelor.apps.stock.db.repo.StockMoveRepository;
import com.axelor.auth.db.User;
import com.axelor.i18n.I18n;
import com.axelor.inject.Beans;
import com.axelor.team.db.TeamTask;
import com.axelor.team.db.repo.TeamTaskRepository;
import com.google.inject.Inject;
import java.math.BigDecimal;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ClientViewServiceImpl implements ClientViewService {

  protected SaleOrderRepository saleOrderRepo;
  protected StockMoveRepository stockMoveRepo;
  protected ProjectRepository projectRepo;
  protected TicketRepository ticketRepo;
  protected InvoiceRepository invoiceRepo;
  protected TeamTaskRepository teamTaskRepo;

  protected static final DateTimeFormatter DATE_FORMATTER =
      DateTimeFormatter.ofPattern("dd/MM/yyyy");

  static final String CLIENT_PORTAL_NO_DATE = /*$$(*/ "None" /*)*/;

  @Inject
  public ClientViewServiceImpl(
      SaleOrderRepository saleOrderRepo,
      StockMoveRepository stockMoveRepo,
      ProjectRepository projectRepo,
      TicketRepository ticketRepo,
      InvoiceRepository invoiceRepo,
      TeamTaskRepository teamTaskRepo) {
    this.saleOrderRepo = saleOrderRepo;
    this.stockMoveRepo = stockMoveRepo;
    this.projectRepo = projectRepo;
    this.ticketRepo = ticketRepo;
    this.invoiceRepo = invoiceRepo;
    this.teamTaskRepo = teamTaskRepo;
  }

  @Override
  public User getClientUser() {
    return Beans.get(UserService.class).getUser();
  }

  @Override
  public Map<String, Object> updateClientViewIndicators() {
    Map<String, Object> map = new HashMap<>();
    User user = getClientUser();
    /* SaleOrder */
    map.put("$ordersInProgress", getOrdersInProgressIndicator(user));
    map.put("$myQuotation", getQuotationsIndicator(user));
    map.put("$lastOrder", getLastOrderIndicator(user));
    /* StockMove */
    map.put("$lastDelivery", getLastDeliveryIndicator(user));
    map.put("$nextDelivery", getNextDeliveryIndicator(user));
    map.put("$plannedDeliveries", getPlannedDeliveriesIndicator(user));
    map.put("$myReversions", getReversionsIndicator(user));
    /* Invoice */
    map.put("$overdueInvoices", getOverdueInvoicesIndicator(user));
    map.put("$awaitingInvoices", getAwaitingInvoicesIndicator(user));
    map.put("$totalRemaining", getTotalRemainingIndicator(user));
    map.put("$myRefund", getRefundIndicator(user));
    /* Helpdesk */
    map.put("$customerTickets", getCustomerTicketsIndicator(user));
    map.put("$companyTickets", getCompanyTicketsIndicator(user));
    map.put("$resolvedTickets", getResolvedTicketsIndicator(user));
    map.put("$lateTickets", getLateTicketsIndicator(user));
    /* Project */
    map.put("$totalProjects", getTotalProjectsIndicator(user));
    map.put("$newTasks", getNewTasksIndicator(user));
    map.put("$tasksInProgress", getTasksInProgressIndicator(user));
    map.put("$tasksDue", getTasksDueIndicator(user));
    return map;
  }

  /* SaleOrder Indicators */
  protected Integer getOrdersInProgressIndicator(User user) {
    List<SaleOrder> saleOrderList =
        saleOrderRepo.all().filter(getOrdersInProgressOfUser(user)).fetch();
    return !saleOrderList.isEmpty() ? saleOrderList.size() : 0;
  }

  protected Integer getQuotationsIndicator(User user) {
    List<SaleOrder> saleOrderList = saleOrderRepo.all().filter(getQuotationsOfUser(user)).fetch();
    return !saleOrderList.isEmpty() ? saleOrderList.size() : 0;
  }

  protected String getLastOrderIndicator(User user) {
    SaleOrder saleOrder = saleOrderRepo.all().filter(getLastOrderOfUser(user)).fetchOne();
    return saleOrder != null
        ? saleOrder.getConfirmationDateTime().format(DATE_FORMATTER)
        : I18n.get(CLIENT_PORTAL_NO_DATE);
  }

  /* StockMove Indicators */
  protected String getLastDeliveryIndicator(User user) {
    StockMove stockMove = stockMoveRepo.all().filter(getLastDeliveryOfUser(user)).fetchOne();
    return stockMove != null
        ? stockMove.getRealDate().format(DATE_FORMATTER)
        : I18n.get(CLIENT_PORTAL_NO_DATE);
  }

  protected String getNextDeliveryIndicator(User user) {
    StockMove stockMove = stockMoveRepo.all().filter(getNextDeliveryOfUser(user)).fetchOne();
    return stockMove != null
        ? stockMove.getEstimatedDate().format(DATE_FORMATTER)
        : I18n.get(CLIENT_PORTAL_NO_DATE);
  }

  protected Integer getPlannedDeliveriesIndicator(User user) {
    List<StockMove> stockMoveList =
        stockMoveRepo.all().filter(getPlannedDeliveriesOfUser(user)).fetch();
    return !stockMoveList.isEmpty() ? stockMoveList.size() : 0;
  }

  protected Integer getReversionsIndicator(User user) {
    List<StockMove> stockMoveList = stockMoveRepo.all().filter(getReversionsOfUser(user)).fetch();
    return !stockMoveList.isEmpty() ? stockMoveList.size() : 0;
  }

  /* Invoice Indicators */
  protected Integer getOverdueInvoicesIndicator(User user) {
    List<Invoice> invoiceList = invoiceRepo.all().filter(getOverdueInvoicesOfUser(user)).fetch();
    return !invoiceList.isEmpty() ? invoiceList.size() : 0;
  }

  protected Integer getAwaitingInvoicesIndicator(User user) {
    List<Invoice> invoiceList = invoiceRepo.all().filter(getAwaitingInvoicesOfUser(user)).fetch();
    return !invoiceList.isEmpty() ? invoiceList.size() : 0;
  }

  protected String getTotalRemainingIndicator(User user) {<commit_msg>RM16955 : fix blank indicator of client portal<commit_after>/*
 * Axelor Business Solutions
 *
 * Copyright (C) 2019 Axelor (<http://axelor.com>).
 *
 * This program is free software: you can redistribute it and/or  modify
 * it under the terms of the GNU Affero General Public License, version 3,
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.axelor.apps.portal.service;

import com.axelor.apps.account.db.Invoice;
import com.axelor.apps.account.db.repo.InvoiceRepository;
import com.axelor.apps.base.service.user.UserService;
import com.axelor.apps.helpdesk.db.Ticket;
import com.axelor.apps.helpdesk.db.repo.TicketRepository;
import com.axelor.apps.project.db.Project;
import com.axelor.apps.project.db.repo.ProjectRepository;
import com.axelor.apps.sale.db.SaleOrder;
import com.axelor.apps.sale.db.repo.SaleOrderRepository;
import com.axelor.apps.stock.db.StockMove;
import com.axelor.apps.stock.db.repo.StockMoveRepository;
import com.axelor.auth.db.User;
import com.axelor.i18n.I18n;
import com.axelor.inject.Beans;
import com.axelor.team.db.TeamTask;
import com.axelor.team.db.repo.TeamTaskRepository;
import com.google.inject.Inject;
import java.math.BigDecimal;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ClientViewServiceImpl implements ClientViewService {

  protected SaleOrderRepository saleOrderRepo;
  protected StockMoveRepository stockMoveRepo;
  protected ProjectRepository projectRepo;
  protected TicketRepository ticketRepo;
  protected InvoiceRepository invoiceRepo;
  protected TeamTaskRepository teamTaskRepo;

  protected static final DateTimeFormatter DATE_FORMATTER =
      DateTimeFormatter.ofPattern("dd/MM/yyyy");

  static final String CLIENT_PORTAL_NO_DATE = /*$$(*/ "None" /*)*/;

  @Inject
  public ClientViewServiceImpl(
      SaleOrderRepository saleOrderRepo,
      StockMoveRepository stockMoveRepo,
      ProjectRepository projectRepo,
      TicketRepository ticketRepo,
      InvoiceRepository invoiceRepo,
      TeamTaskRepository teamTaskRepo) {
    this.saleOrderRepo = saleOrderRepo;
    this.stockMoveRepo = stockMoveRepo;
    this.projectRepo = projectRepo;
    this.ticketRepo = ticketRepo;
    this.invoiceRepo = invoiceRepo;
    this.teamTaskRepo = teamTaskRepo;
  }

  @Override
  public User getClientUser() {
    return Beans.get(UserService.class).getUser();
  }

  @Override
  public Map<String, Object> updateClientViewIndicators() {
    Map<String, Object> map = new HashMap<>();
    User user = getClientUser();
    /* SaleOrder */
    map.put("$ordersInProgress", getOrdersInProgressIndicator(user));
    map.put("$myQuotation", getQuotationsIndicator(user));
    map.put("$lastOrder", getLastOrderIndicator(user));
    /* StockMove */
    map.put("$lastDelivery", getLastDeliveryIndicator(user));
    map.put("$nextDelivery", getNextDeliveryIndicator(user));
    map.put("$plannedDeliveries", getPlannedDeliveriesIndicator(user));
    map.put("$myReversions", getReversionsIndicator(user));
    /* Invoice */
    map.put("$overdueInvoices", getOverdueInvoicesIndicator(user));
    map.put("$awaitingInvoices", getAwaitingInvoicesIndicator(user));
    map.put("$totalRemaining", getTotalRemainingIndicator(user));
    map.put("$myRefund", getRefundIndicator(user));
    /* Helpdesk */
    map.put("$customerTickets", getCustomerTicketsIndicator(user));
    map.put("$companyTickets", getCompanyTicketsIndicator(user));
    map.put("$resolvedTickets", getResolvedTicketsIndicator(user));
    map.put("$lateTickets", getLateTicketsIndicator(user));
    /* Project */
    map.put("$totalProjects", getTotalProjectsIndicator(user));
    map.put("$newTasks", getNewTasksIndicator(user));
    map.put("$tasksInProgress", getTasksInProgressIndicator(user));
    map.put("$tasksDue", getTasksDueIndicator(user));
    return map;
  }

  /* SaleOrder Indicators */
  protected Integer getOrdersInProgressIndicator(User user) {
    List<SaleOrder> saleOrderList =
        saleOrderRepo.all().filter(getOrdersInProgressOfUser(user)).fetch();
    return !saleOrderList.isEmpty() ? saleOrderList.size() : 0;
  }

  protected Integer getQuotationsIndicator(User user) {
    List<SaleOrder> saleOrderList = saleOrderRepo.all().filter(getQuotationsOfUser(user)).fetch();
    return !saleOrderList.isEmpty() ? saleOrderList.size() : 0;
  }

  protected String getLastOrderIndicator(User user) {
    SaleOrder saleOrder = saleOrderRepo.all().filter(getLastOrderOfUser(user)).fetchOne();
    if (saleOrder == null) {
      return I18n.get(CLIENT_PORTAL_NO_DATE);
    }
    return saleOrder.getConfirmationDateTime() != null
        ? saleOrder.getConfirmationDateTime().format(DATE_FORMATTER)
        : I18n.get(CLIENT_PORTAL_NO_DATE);
  }

  /* StockMove Indicators */
  protected String getLastDeliveryIndicator(User user) {
    StockMove stockMove = stockMoveRepo.all().filter(getLastDeliveryOfUser(user)).fetchOne();
    if (stockMove == null) {
      return I18n.get(CLIENT_PORTAL_NO_DATE);
    }
    return stockMove.getRealDate() != null
        ? stockMove.getRealDate().format(DATE_FORMATTER)
        : I18n.get(CLIENT_PORTAL_NO_DATE);
  }

  protected String getNextDeliveryIndicator(User user) {
    StockMove stockMove = stockMoveRepo.all().filter(getNextDeliveryOfUser(user)).fetchOne();
    if (stockMove == null) {
      return I18n.get(CLIENT_PORTAL_NO_DATE);
    }
    return stockMove.getEstimatedDate() != null
        ? stockMove.getEstimatedDate().format(DATE_FORMATTER)
        : I18n.get(CLIENT_PORTAL_NO_DATE);
  }

  protected Integer getPlannedDeliveriesIndicator(User user) {
    List<StockMove> stockMoveList =
        stockMoveRepo.all().filter(getPlannedDeliveriesOfUser(user)).fetch();
    return !stockMoveList.isEmpty() ? stockMoveList.size() : 0;
  }

  protected Integer getReversionsIndicator(User user) {
    List<StockMove> stockMoveList = stockMoveRepo.all().filter(getReversionsOfUser(user)).fetch();
    return !stockMoveList.isEmpty() ? stockMoveList.size() : 0;
  }

  /* Invoice Indicators */
  protected Integer getOverdueInvoicesIndicator(User user) {
    List<Invoice> invoiceList = invoiceRepo.all().filter(getOverdueInvoicesOfUser(user)).fetch();
    return !invoiceList.isEmpty() ? invoiceList.size() : 0;
  }

  protected Integer getAwaitingInvoicesIndicator(User user) {
    List<Invoice> invoiceList = invoiceRepo.all().filter(getAwaitingInvoicesOfUser(user)).fetch();
    return !invoiceList.isEmpty() ? invoiceList.size() : 0;
  }

  protected String getTotalRemainingIndicator(User user) {
{'fraction_non_alphanumeric': 0.11452002636010837, 'fraction_numerical': 0.00388079373215201, 'mean_word_length': 4.856775300171527, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4632135', 'n_tokens_mistral': 4219, 'n_tokens_neox': 4196, 'n_words': 787}