invoiceninja/app/Ninja/Reports/ProductReport.php

107 lines
3.7 KiB
PHP
Raw Normal View History

2017-01-22 12:09:29 +02:00
<?php
namespace App\Ninja\Reports;
use App\Models\Client;
2017-01-30 21:40:43 +02:00
use Auth;
use Utils;
2017-01-22 12:09:29 +02:00
class ProductReport extends AbstractReport
{
2018-01-21 15:53:43 +02:00
public function getColumns()
{
2018-01-21 16:05:35 +02:00
$columns = [
2018-01-23 20:32:11 +02:00
'client' => [],
'invoice_number' => [],
'invoice_date' => [],
'product' => [],
'description' => [],
'qty' => [],
'cost' => [],
2018-01-21 15:53:43 +02:00
//'tax_rate1',
//'tax_rate2',
];
2018-01-21 16:05:35 +02:00
$account = auth()->user()->account;
2018-01-21 21:12:49 +02:00
if ($account->invoice_item_taxes) {
$columns['tax'] = ['columnSelector-false'];
if ($account->enable_second_tax_rate) {
$columns['tax'] = ['columnSelector-false'];
}
}
2018-04-04 16:24:59 +03:00
if ($account->customLabel('product1')) {
$columns[$account->present()->customLabel('product1')] = ['columnSelector-false', 'custom'];
2018-01-21 16:05:35 +02:00
}
2018-02-14 13:25:23 +02:00
2018-04-04 16:24:59 +03:00
if ($account->customLabel('product2')) {
$columns[$account->present()->customLabel('product2')] = ['columnSelector-false', 'custom'];
2018-01-21 16:05:35 +02:00
}
return $columns;
2018-01-21 15:53:43 +02:00
}
2017-01-22 12:09:29 +02:00
public function run()
{
$account = Auth::user()->account;
$statusIds = $this->options['status_ids'];
2018-02-28 16:57:10 +02:00
$subgroup = $this->options['subgroup'];
2017-01-22 12:09:29 +02:00
$clients = Client::scope()
2017-03-31 09:01:07 +03:00
->orderBy('name')
->withArchived()
2018-02-28 16:57:10 +02:00
->with('contacts', 'user')
->with(['invoices' => function ($query) use ($statusIds) {
$query->invoices()
->withArchived()
->statusIds($statusIds)
->where('invoice_date', '>=', $this->startDate)
2017-01-22 12:09:29 +02:00
->where('invoice_date', '<=', $this->endDate)
->with(['invoice_items']);
2017-01-22 12:09:29 +02:00
}]);
foreach ($clients->get() as $client) {
foreach ($client->invoices as $invoice) {
foreach ($invoice->invoice_items as $item) {
2018-01-21 16:05:35 +02:00
$row = [
2017-01-22 12:09:29 +02:00
$this->isExport ? $client->getDisplayName() : $client->present()->link,
$this->isExport ? $invoice->invoice_number : $invoice->present()->link,
2019-01-30 21:45:46 +11:00
$this->isExport ? $invoice->invoice_date : $invoice->present()->invoice_date,
$item->product_key,
2018-02-14 13:11:47 +02:00
$item->notes,
2018-07-29 09:58:18 +03:00
$item->qty + 0,
Utils::roundSignificant($item->cost, 2),
2017-01-22 12:09:29 +02:00
];
2018-01-21 16:05:35 +02:00
2018-01-21 21:12:49 +02:00
if ($account->invoice_item_taxes) {
2018-04-23 12:44:23 +03:00
$row[] = Utils::roundSignificant($item->getTaxAmount(), 2);
2018-01-21 21:12:49 +02:00
}
2018-04-04 16:24:59 +03:00
if ($account->customLabel('product1')) {
2018-01-21 16:05:35 +02:00
$row[] = $item->custom_value1;
}
2018-02-14 13:25:23 +02:00
2018-04-04 16:24:59 +03:00
if ($account->customLabel('product2')) {
2018-01-21 16:05:35 +02:00
$row[] = $item->custom_value2;
}
$this->data[] = $row;
2018-02-28 16:57:10 +02:00
if ($subgroup == 'product') {
$dimension = $item->product_key;
} else {
$dimension = $this->getDimension($client);
}
$this->addChartData($dimension, $invoice->invoice_date, $invoice->amount);
2017-01-22 12:09:29 +02:00
}
//$this->addToTotals($client->currency_id, 'paid', $payment ? $payment->getCompletedAmount() : 0);
//$this->addToTotals($client->currency_id, 'amount', $invoice->amount);
//$this->addToTotals($client->currency_id, 'balance', $invoice->balance);
2017-01-22 12:09:29 +02:00
}
}
}
}