search and export from invoice archive (#2408)

This commit is contained in:
Kevin Papst
2021-03-10 01:50:27 +01:00
committed by GitHub
parent c34e9f576d
commit db1344573f
22 changed files with 544 additions and 72 deletions

View File

@@ -16,7 +16,7 @@ use App\Entity\User;
use App\Repository\Loader\InvoiceLoader;
use App\Repository\Paginator\LoaderPaginator;
use App\Repository\Paginator\PaginatorInterface;
use App\Repository\Query\InvoiceQuery;
use App\Repository\Query\InvoiceArchiveQuery;
use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\QueryBuilder;
use Pagerfanta\Pagerfanta;
@@ -91,7 +91,7 @@ class InvoiceRepository extends EntityRepository
return $this->getCounterFor($start, $end, $customer);
}
public function getCounterForAllTime(\DateTime $date, ?Customer $customer = null): int
public function getCounterForAllTime(?Customer $customer = null): int
{
if (null !== $customer) {
return $this->count(['customer' => $customer]);
@@ -137,7 +137,7 @@ class InvoiceRepository extends EntityRepository
$qb->setParameter('teams', $ids);
}
private function getQueryBuilderForQuery(InvoiceQuery $query): QueryBuilder
private function getQueryBuilderForQuery(InvoiceArchiveQuery $query): QueryBuilder
{
$qb = $this->getEntityManager()->createQueryBuilder();
@@ -146,21 +146,79 @@ class InvoiceRepository extends EntityRepository
->from(Invoice::class, 'i')
;
if ($query->getBegin() !== null) {
$qb->andWhere($qb->expr()->gte('i.createdAt', ':begin'));
$qb->setParameter('begin', $query->getBegin());
}
if ($query->getEnd() !== null) {
$qb->andWhere($qb->expr()->lte('i.createdAt', ':end'));
$qb->setParameter('end', $query->getEnd());
}
if ($query->hasCustomers()) {
$qb->andWhere($qb->expr()->in('i.customer', ':customer'));
$qb->setParameter('customer', $query->getCustomers());
}
if ($query->hasStatus()) {
$qb->andWhere($qb->expr()->in('i.status', ':status'));
$qb->setParameter('status', $query->getStatus());
}
$orderBy = $query->getOrderBy();
switch ($orderBy) {
case 'date':
$orderBy = 'i.createdAt';
break;
case 'customer':
$orderBy = 'i.customer';
break;
case 'total':
$orderBy = 'i.total';
break;
}
$qb->addOrderBy($orderBy, $query->getOrder());
$this->addPermissionCriteria($qb, $query->getCurrentUser());
if ($query->hasSearchTerm()) {
$qb->leftJoin('i.customer', 'customer');
$searchAnd = $qb->expr()->andX();
$searchTerm = $query->getSearchTerm();
foreach ($searchTerm->getSearchFields() as $metaName => $metaValue) {
$qb->leftJoin('customer.meta', 'meta');
$searchAnd->add(
$qb->expr()->andX(
$qb->expr()->eq('meta.name', ':metaName'),
$qb->expr()->like('meta.value', ':metaValue')
)
);
$qb->setParameter('metaName', $metaName);
$qb->setParameter('metaValue', '%' . $metaValue . '%');
}
if ($searchTerm->hasSearchTerm()) {
$searchAnd->add(
$qb->expr()->orX(
$qb->expr()->like('customer.name', ':searchTerm'),
$qb->expr()->like('customer.company', ':searchTerm')
)
);
$qb->setParameter('searchTerm', '%' . $searchTerm->getSearchTerm() . '%');
}
if ($searchAnd->count() > 0) {
$qb->andWhere($searchAnd);
}
}
return $qb;
}
public function countInvoicesForQuery(InvoiceQuery $query): int
public function countInvoicesForQuery(InvoiceArchiveQuery $query): int
{
$qb = $this->getQueryBuilderForQuery($query);
$qb
@@ -173,7 +231,20 @@ class InvoiceRepository extends EntityRepository
return (int) $qb->getQuery()->getSingleScalarResult();
}
protected function getPaginatorForQuery(InvoiceQuery $query): PaginatorInterface
/**
* @param InvoiceArchiveQuery $query
* @return Invoice[]
*/
public function getInvoicesForQuery(InvoiceArchiveQuery $query): iterable
{
// this is using the paginator internally, as it will load all joined entities into the working unit
// do not "optimize" to use the query directly, as it would results in hundreds of additional lazy queries
$paginator = $this->getPaginatorForQuery($query);
return $paginator->getAll();
}
protected function getPaginatorForQuery(InvoiceArchiveQuery $query): PaginatorInterface
{
$counter = $this->countInvoicesForQuery($query);
$qb = $this->getQueryBuilderForQuery($query);
@@ -181,7 +252,7 @@ class InvoiceRepository extends EntityRepository
return new LoaderPaginator(new InvoiceLoader($qb->getEntityManager()), $qb, $counter);
}
public function getPagerfantaForQuery(InvoiceQuery $query): Pagerfanta
public function getPagerfantaForQuery(InvoiceArchiveQuery $query): Pagerfanta
{
$paginator = new Pagerfanta($this->getPaginatorForQuery($query));
$paginator->setMaxPerPage($query->getPageSize());

View File

@@ -0,0 +1,58 @@
<?php
/*
* This file is part of the Kimai time-tracking app.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace App\Repository\Query;
use App\Form\Model\DateRange;
trait DateRangeTrait
{
/**
* @var DateRange
*/
protected $dateRange;
public function getBegin(): ?\DateTime
{
if (null === $this->dateRange) {
return null;
}
return $this->dateRange->getBegin();
}
public function setBegin(\DateTime $begin): void
{
$this->dateRange->setBegin($begin);
}
public function getEnd(): ?\DateTime
{
if (null === $this->dateRange) {
return null;
}
return $this->dateRange->getEnd();
}
public function setEnd(\DateTime $end): void
{
$this->dateRange->setEnd($end);
}
public function getDateRange(): ?DateRange
{
return $this->dateRange;
}
public function setDateRange(DateRange $dateRange): void
{
$this->dateRange = $dateRange;
}
}

View File

@@ -0,0 +1,101 @@
<?php
/*
* This file is part of the Kimai time-tracking app.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace App\Repository\Query;
use App\Entity\Customer;
use App\Entity\Invoice;
use App\Form\Model\DateRange;
/**
* Query for created invoices.
*/
class InvoiceArchiveQuery extends BaseQuery
{
use DateRangeTrait;
public const INVOICE_ARCHIVE_ORDER_ALLOWED = [
'date', 'customer', 'total'
];
/**
* Filter for invoice status (by default all)
* @var string[]
*/
private $status = [];
/**
* @var Customer[]
*/
private $customers = [];
public function __construct()
{
$this->setDefaults([
'orderBy' => 'date',
'order' => self::ORDER_DESC,
'dateRange' => new DateRange(),
]);
}
public function addCustomer(Customer $customer): void
{
$this->customers[] = $customer;
}
public function setCustomers(array $customers): void
{
foreach ($customers as $customer) {
$this->addCustomer($customer);
}
}
/**
* @return Customer[]
*/
public function getCustomers(): array
{
return $this->customers;
}
public function hasCustomers(): bool
{
return !empty($this->customers);
}
public function hasStatus(): bool
{
return !empty($this->status);
}
public function getStatus(): array
{
return $this->status;
}
/**
* @param string[] $status
*/
public function setStatus(array $status): void
{
foreach ($status as $s) {
$this->addStatus($s);
}
}
public function addStatus(string $status): void
{
if (!\in_array($status, [Invoice::STATUS_NEW, Invoice::STATUS_PENDING, Invoice::STATUS_PAID])) {
throw new \InvalidArgumentException('Unknown invoice status given.');
}
if (!\in_array($status, $this->status)) {
$this->status[] = $status;
}
}
}

View File

@@ -11,6 +11,9 @@ namespace App\Repository\Query;
use App\Entity\InvoiceTemplate;
/**
* Find items (eg timesheets) for creating a new invoice.
*/
class InvoiceQuery extends TimesheetQuery
{
/**

View File

@@ -20,6 +20,7 @@ use App\Form\Model\DateRange;
class TimesheetQuery extends ActivityQuery implements BillableInterface
{
use BillableTrait;
use DateRangeTrait;
public const STATE_ALL = 1;
public const STATE_RUNNING = 2;
@@ -49,10 +50,6 @@ class TimesheetQuery extends ActivityQuery implements BillableInterface
* @var \DateTime|null
*/
private $modifiedAfter;
/**
* @var DateRange
*/
protected $dateRange;
/**
* @var iterable
*/
@@ -230,42 +227,6 @@ class TimesheetQuery extends ActivityQuery implements BillableInterface
return $this;
}
public function getBegin(): ?\DateTime
{
return $this->dateRange->getBegin();
}
public function setBegin(\DateTime $begin): TimesheetQuery
{
$this->dateRange->setBegin($begin);
return $this;
}
public function getEnd(): ?\DateTime
{
return $this->dateRange->getEnd();
}
public function setEnd(\DateTime $end): TimesheetQuery
{
$this->dateRange->setEnd($end);
return $this;
}
public function getDateRange(): DateRange
{
return $this->dateRange;
}
public function setDateRange(DateRange $dateRange): TimesheetQuery
{
$this->dateRange = $dateRange;
return $this;
}
public function getTags(bool $allowUnknown = false): iterable
{
if (empty($this->tags)) {