1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 05:02:36 +01:00

Updates for reports

This commit is contained in:
David Bomba 2024-03-05 13:58:26 +11:00
parent e7ae3f81cb
commit a43bd9c6de
18 changed files with 781 additions and 436 deletions

View File

@ -826,8 +826,15 @@ class BaseExport
return '';
}
public function applyFilters(Builder $query): Builder
/**
* Apply Product Filters
*
* @param Builder $query
*
* @return Builder
*/
public function applyProductFilters(Builder $query): Builder
{
if(isset($this->input['product_key'])) {
@ -844,8 +851,16 @@ class BaseExport
return $query;
}
protected function addClientFilter($query, $clients): Builder
/**
* Add Client Filter
*
* @param Builder $query
* @param string $clients
*
* @return Builder
*/
protected function addClientFilter(Builder $query, string $clients): Builder
{
if(is_string($clients)) {
$clients = explode(',', $clients);
@ -862,8 +877,16 @@ class BaseExport
return $query;
}
protected function addVendorFilter($query, $vendors): Builder
/**
* Add Vendor Filter
*
* @param Builder $query
* @param string $vendors
*
* @return Builder
*/
protected function addVendorFilter(Builder$query, string $vendors): Builder
{
if(is_string($vendors)) {
@ -878,8 +901,16 @@ class BaseExport
return $query;
}
protected function addProjectFilter($query, $projects): Builder
/**
* AddProjectFilter
*
* @param Builder $query
* @param string $projects
*
* @return Builder
*/
protected function addProjectFilter(Builder $query, string $projects): Builder
{
if(is_string($projects)) {
@ -894,8 +925,16 @@ class BaseExport
return $query;
}
protected function addCategoryFilter($query, $expense_categories): Builder
/**
* Add Category Filter
*
* @param Builder $query
* @param string $expense_categories
*
* @return Builder
*/
protected function addCategoryFilter(Builder $query, string $expense_categories): Builder
{
if(is_string($expense_categories)) {
@ -911,13 +950,230 @@ class BaseExport
return $query;
}
protected function addInvoiceStatusFilter($query, $status): Builder
/**
* Add Payment Status Filters
*
* @param Builder $query
* @param string $status
*
* @return Builder
*/
protected function addPaymentStatusFilters(Builder $query, string $status): Builder
{
$status_parameters = explode(',', $status);
if(in_array('all', $status_parameters)) {
if(in_array('all', $status_parameters) || count($status_parameters) == 0) {
return $query;
}
$query->where(function ($query) use ($status_parameters) {
$payment_filters = [];
if (in_array('pending', $status_parameters)) {
$payment_filters[] = Payment::STATUS_PENDING;
}
if (in_array('cancelled', $status_parameters)) {
$payment_filters[] = Payment::STATUS_CANCELLED;
}
if (in_array('failed', $status_parameters)) {
$payment_filters[] = Payment::STATUS_FAILED;
}
if (in_array('completed', $status_parameters)) {
$payment_filters[] = Payment::STATUS_COMPLETED;
}
if (in_array('partially_refunded', $status_parameters)) {
$payment_filters[] = Payment::STATUS_PARTIALLY_REFUNDED;
}
if (in_array('refunded', $status_parameters)) {
$payment_filters[] = Payment::STATUS_REFUNDED;
}
if (count($payment_filters) > 0) {
$query->whereIn('status_id', $payment_filters);
}
if(in_array('partially_unapplied', $status_parameters)) {
$query->whereColumn('amount', '>', 'applied')->where('refunded', 0);
}
});
return $query;
}
/**
* Add RecurringInvoice Status Filter
*
* @param Builder $query
* @param string $status
*
* @return Builder
*/
protected function addRecurringInvoiceStatusFilter(Builder $query, string $status): Builder
{
$status_parameters = explode(',', $status);
if (in_array('all', $status_parameters) || count($status_parameters) == 0){
return $query;
}
$recurring_filters = [];
if (in_array('active', $status_parameters)) {
$recurring_filters[] = RecurringInvoice::STATUS_ACTIVE;
}
if (in_array('paused', $status_parameters)) {
$recurring_filters[] = RecurringInvoice::STATUS_PAUSED;
}
if (in_array('completed', $status_parameters)) {
$recurring_filters[] = RecurringInvoice::STATUS_COMPLETED;
}
if (count($recurring_filters) >= 1) {
return $query->whereIn('status_id', $recurring_filters);
}
return $query;
}
/**
* Add QuoteStatus Filter
*
* @param Builder $query
* @param string $status
*
* @return Builder
*/
protected function addQuoteStatusFilter(Builder $query, string $status): Builder
{
$status_parameters = explode(',', $status);
if (in_array('all', $status_parameters)) {
return $query;
}
$query->where(function ($query) use ($status_parameters) {
if (in_array('sent', $status_parameters)) {
$query->orWhere(function ($q) {
$q->where('status_id', Quote::STATUS_SENT)
->whereNull('due_date')
->orWhere('due_date', '>=', now()->toDateString());
});
}
$quote_filters = [];
if (in_array('draft', $status_parameters)) {
$quote_filters[] = Quote::STATUS_DRAFT;
}
if (in_array('approved', $status_parameters)) {
$quote_filters[] = Quote::STATUS_APPROVED;
}
if (count($quote_filters) > 0) {
$query->orWhereIn('status_id', $quote_filters);
}
if (in_array('expired', $status_parameters)) {
$query->orWhere(function ($q) {
$q->where('status_id', Quote::STATUS_SENT)
->whereNotNull('due_date')
->where('due_date', '<=', now()->toDateString());
});
}
if (in_array('upcoming', $status_parameters)) {
$query->orWhere(function ($q) {
$q->where('status_id', Quote::STATUS_SENT)
->where('due_date', '>=', now()->toDateString())
->orderBy('due_date', 'DESC');
});
}
if(in_array('converted', $status_parameters)) {
$query->orWhere(function ($q) {
$q->whereNotNull('invoice_id');
});
}
});
return $query;
}
/**
* Add PurchaseOrder Status Filter
*
* @param Builder $query
* @param string $status
*
* @return Builder
*/
protected function addPurchaseOrderStatusFilter(Builder $query, string $status): Builder
{
$status_parameters = explode(',', $status);
if (in_array('all', $status_parameters) || count($status_parameters) == 0) {
return $query;
}
$query->where(function ($query) use ($status_parameters) {
$po_status = [];
if (in_array('draft', $status_parameters)) {
$po_status[] = PurchaseOrder::STATUS_DRAFT;
}
if (in_array('sent', $status_parameters)) {
$query->orWhere(function ($q) {
$q->where('status_id', PurchaseOrder::STATUS_SENT)
->whereNull('due_date')
->orWhere('due_date', '>=', now()->toDateString());
});
}
if (in_array('accepted', $status_parameters)) {
$po_status[] = PurchaseOrder::STATUS_ACCEPTED;
}
if (in_array('cancelled', $status_parameters)) {
$po_status[] = PurchaseOrder::STATUS_CANCELLED;
}
if (count($po_status) >= 1) {
$query->whereIn('status_id', $po_status);
}
});
return $query;
}
/**
* Add Invoice Status Filter
*
* @param Builder $query
* @param string $status
* @return Builder
*/
protected function addInvoiceStatusFilter(Builder $query, string $status): Builder
{
$status_parameters = explode(',', $status);
if(in_array('all', $status_parameters) || count($status_parameters) == 0) {
return $query;
}
@ -942,6 +1198,10 @@ class BaseExport
$invoice_filters[] = Invoice::STATUS_PARTIAL;
}
if (in_array('cancelled', $status_parameters)) {
$invoice_filters[] = Invoice::STATUS_CANCELLED;
}
if (count($invoice_filters) > 0) {
$nested->whereIn('status_id', $invoice_filters);
}
@ -965,15 +1225,19 @@ class BaseExport
return $query;
}
protected function addDateRange($query)
/**
* Add Date Range
*
* @param Builder $query
* @return Builder
*/
protected function addDateRange(Builder $query): Builder
{
$query = $this->applyFilters($query);
$query = $this->applyProductFilters($query);
$date_range = $this->input['date_range'];
nlog($date_range);
if (array_key_exists('date_key', $this->input) && strlen($this->input['date_key']) > 1) {
$this->date_key = $this->input['date_key'];
}

View File

@ -127,7 +127,7 @@ class ClientExport extends BaseExport
$query = Client::query()->with('contacts')
->withTrashed()
->where('company_id', $this->company->id)
->where('is_deleted', 0);
->where('is_deleted', $this->input['include_deleted']);
$query = $this->addDateRange($query);

View File

@ -103,10 +103,14 @@ class CreditExport extends BaseExport
->withTrashed()
->with('client')
->where('company_id', $this->company->id)
->where('is_deleted', 0);
->where('is_deleted', $this->input['include_deleted']);
$query = $this->addDateRange($query);
if($this->input['status'] ?? false) {
$query = $this->addCreditStatusFilter($query, $this->input['status']);
}
if($this->input['document_email_attachment'] ?? false) {
$this->queueDocuments($query);
}
@ -162,6 +166,40 @@ class CreditExport extends BaseExport
return $this->decorateAdvancedFields($credit, $entity);
}
public function addCreditStatusFilter($query, $status): Builder
{
$status_parameters = explode(',', $status);
if (in_array('all', $status_parameters)) {
return $query;
}
$credit_filters = [];
if (in_array('draft', $status_parameters)) {
$credit_filters[] = Credit::STATUS_DRAFT;
}
if (in_array('sent', $status_parameters)) {
$credit_filters[] = Credit::STATUS_SENT;
}
if (in_array('partial', $status_parameters)) {
$credit_filters[] = Credit::STATUS_PARTIAL;
}
if (in_array('applied', $status_parameters)) {
$credit_filters[] = Credit::STATUS_APPLIED;
}
if (count($credit_filters) >= 1) {
$query->whereIn('status_id', $credit_filters);
}
return $query;
}
private function decorateAdvancedFields(Credit $credit, array $entity): array
{
// if (in_array('country_id', $this->input['report_keys'])) {

View File

@ -83,10 +83,14 @@ class ExpenseExport extends BaseExport
->with('client')
->withTrashed()
->where('company_id', $this->company->id)
->where('is_deleted', 0);
->where('is_deleted', $this->input['include_deleted']);
$query = $this->addDateRange($query);
if($this->input['status'] ?? false) {
$query = $this->addExpenseStatusFilter($query, $this->input['status']);
}
if(isset($this->input['clients'])) {
$query = $this->addClientFilter($query, $this->input['clients']);
}
@ -152,6 +156,55 @@ class ExpenseExport extends BaseExport
return $this->decorateAdvancedFields($expense, $entity);
}
protected function addExpenseStatusFilter($query, $status): Builder
{
$status_parameters = explode(',', $status);
if (in_array('all', $status_parameters)) {
return $query;
}
$query->where(function ($query) use ($status_parameters) {
if (in_array('logged', $status_parameters)) {
$query->orWhere(function ($query) {
$query->where('amount', '>', 0)
->whereNull('invoice_id')
->whereNull('payment_date')
->where('should_be_invoiced', false);
});
}
if (in_array('pending', $status_parameters)) {
$query->orWhere(function ($query) {
$query->where('should_be_invoiced', true)
->whereNull('invoice_id');
});
}
if (in_array('invoiced', $status_parameters)) {
$query->orWhere(function ($query) {
$query->whereNotNull('invoice_id');
});
}
if (in_array('paid', $status_parameters)) {
$query->orWhere(function ($query) {
$query->whereNotNull('payment_date');
});
}
if (in_array('unpaid', $status_parameters)) {
$query->orWhere(function ($query) {
$query->whereNull('payment_date');
});
}
});
return $query;
}
private function decorateAdvancedFields(Expense $expense, array $entity): array
{
// if (in_array('expense.currency_id', $this->input['report_keys'])) {

View File

@ -58,7 +58,7 @@ class InvoiceExport extends BaseExport
->withTrashed()
->with('client')
->where('company_id', $this->company->id)
->where('is_deleted', 0);
->where('is_deleted', $this->input['include_deleted']);
$query = $this->addDateRange($query);

View File

@ -71,11 +71,15 @@ class InvoiceItemExport extends BaseExport
->withTrashed()
->with('client')
->where('company_id', $this->company->id)
->where('is_deleted', 0);
->where('is_deleted', $this->input['include_deleted']);
$query = $this->addDateRange($query);
$query = $this->applyFilters($query);
if($this->input['status'] ?? false) {
$query = $this->addInvoiceStatusFilter($query, $this->input['status']);
}
$query = $this->applyProductFilters($query);
if($this->input['document_email_attachment'] ?? false) {
$this->queueDocuments($query);

View File

@ -61,6 +61,8 @@ class PaymentExport extends BaseExport
$query = $this->addDateRange($query);
$query = $this->addPaymentStatusFilters($query, $this->input['status'] ?? '');
if($this->input['document_email_attachment'] ?? false) {
$this->queueDocuments($query);
}

View File

@ -104,10 +104,12 @@ class PurchaseOrderExport extends BaseExport
->withTrashed()
->with('vendor')
->where('company_id', $this->company->id)
->where('is_deleted', 0);
->where('is_deleted', $this->input['include_deleted']);
$query = $this->addDateRange($query);
$query = $this->addPurchaseOrderStatusFilter($query, $this->input['status'] ?? '');
if($this->input['document_email_attachment'] ?? false) {
$this->queueDocuments($query);
}

View File

@ -63,10 +63,12 @@ class PurchaseOrderItemExport extends BaseExport
$query = PurchaseOrder::query()
->withTrashed()
->with('vendor')->where('company_id', $this->company->id)
->where('is_deleted', 0);
->where('is_deleted', $this->input['include_deleted']);
$query = $this->addDateRange($query);
$query = $this->addPurchaseOrderStatusFilter($query, $this->input['status'] ?? '');
if($this->input['document_email_attachment'] ?? false) {
$this->queueDocuments($query);
}

View File

@ -65,10 +65,12 @@ class QuoteExport extends BaseExport
->withTrashed()
->with('client')
->where('company_id', $this->company->id)
->where('is_deleted', 0);
->where('is_deleted', $this->input['include_deleted']);
$query = $this->addDateRange($query);
$query = $this->addQuoteStatusFilter($query, $this->input['status'] ?? '');
if($this->input['document_email_attachment'] ?? false) {
$this->queueDocuments($query);
}

View File

@ -66,10 +66,12 @@ class QuoteItemExport extends BaseExport
$query = Quote::query()
->withTrashed()
->with('client')->where('company_id', $this->company->id)
->where('is_deleted', 0);
->where('is_deleted', $this->input['include_deleted']);
$query = $this->addDateRange($query);
$query = $this->addQuoteStatusFilter($query, $this->input['status'] ?? '');
if($this->input['document_email_attachment'] ?? false) {
$this->queueDocuments($query);
}

View File

@ -57,10 +57,12 @@ class RecurringInvoiceExport extends BaseExport
->withTrashed()
->with('client')
->where('company_id', $this->company->id)
->where('is_deleted', 0);
->where('is_deleted', $this->input['include_deleted']);
$query = $this->addDateRange($query);
$query = $this->addRecurringInvoiceStatusFilter($query, $this->input['status'] ?? '');
return $query;
}

View File

@ -68,7 +68,7 @@ class TaskExport extends BaseExport
$query = Task::query()
->withTrashed()
->where('company_id', $this->company->id)
->where('is_deleted', 0);
->where('is_deleted', $this->input['include_deleted']);
$query = $this->addDateRange($query);
@ -202,6 +202,34 @@ class TaskExport extends BaseExport
}
}
/**
* Add Task Status Filter
*
* @param Builder $query
* @param string $status
* @return Builder
*/
protected function addTaskStatusFilter(Builder $query, string $status): Builder
{
$status_parameters = explode(',', $status);
if (in_array('all', $status_parameters) || count($status_parameters) == 0) {
return $query;
}
if (in_array('invoiced', $status_parameters)) {
$query->whereNotNull('invoice_id');
}
if (in_array('uninvoiced', $status_parameters)) {
$query->whereNull('invoice_id');
}
return $query;
}
private function decorateAdvancedFields(Task $task, array $entity): array
{

View File

@ -62,7 +62,7 @@ class VendorExport extends BaseExport
$query = Vendor::query()->with('contacts')
->withTrashed()
->where('company_id', $this->company->id)
->where('is_deleted', 0);
->where('is_deleted', $this->input['include_deleted']);
$query = $this->addDateRange($query);

View File

@ -37,7 +37,8 @@ class GenericReportRequest extends Request
'start_date' => 'bail|required_if:date_range,custom|nullable|date',
'report_keys' => 'present|array',
'send_email' => 'required|bool',
'document_email_attachment' => 'sometimes|bool'
'document_email_attachment' => 'sometimes|bool',
'include_deleted' => 'required|bool',
// 'status' => 'sometimes|string|nullable|in:all,draft,sent,viewed,paid,unpaid,overdue',
];
}
@ -63,6 +64,8 @@ class GenericReportRequest extends Request
$input['end_date'] = null;
}
$input['include_deleted'] = array_key_exists('include_deleted', $input) ? filter_var($input['include_deleted'], FILTER_VALIDATE_BOOLEAN) : false;
$input['user_id'] = auth()->user()->id;
$this->replace($input);

View File

@ -149,6 +149,7 @@ class PdfSlot extends Component
return render('components.livewire.pdf-slot', [
'invitation' => $this->invitation,
'entity' => $this->entity,
'settings' => $this->settings,
'data' => $this->invitation->company->settings,
'entity_type' => $this->entity_type,
'products' => $this->getProducts(),

745
composer.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -501,6 +501,7 @@ class ReportCsvGenerationTest extends TestCase
// 'start_date' => 'bail|required_if:date_range,custom|nullable|date',
'report_keys' => [],
'send_email' => false,
'include_deleted' => false,
// 'status' => 'sometimes|string|nullable|in:all,draft,sent,viewed,paid,unpaid,overdue',
];
@ -547,6 +548,7 @@ class ReportCsvGenerationTest extends TestCase
'date_range' => 'all',
'report_keys' => ["vendor.name", "vendor.city", "vendor.number"],
'send_email' => false,
'include_deleted' => false,
];
$response = $this->withHeaders([
@ -638,6 +640,7 @@ class ReportCsvGenerationTest extends TestCase
'task.custom_value4',
],
'send_email' => false,
'include_deleted' => false,
];
$response = $this->withHeaders([
@ -792,6 +795,7 @@ class ReportCsvGenerationTest extends TestCase
'date_range' => 'all',
'report_keys' => [],
'send_email' => false,
'include_deleted' => false,
];
$response = $this->withHeaders([
@ -873,6 +877,7 @@ class ReportCsvGenerationTest extends TestCase
"client.paid_to_date"
],
'send_email' => false,
'include_deleted' => false,
];
$response = $this->withHeaders([