1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-08 20:22:42 +01:00
invoiceninja/app/Ninja/Reports/ProfitAndLossReport.php
2018-01-23 20:32:11 +02:00

80 lines
3.1 KiB
PHP

<?php
namespace App\Ninja\Reports;
use App\Models\Expense;
use App\Models\Payment;
use Auth;
class ProfitAndLossReport extends AbstractReport
{
public function getColumns()
{
return [
'type' => [],
'client' => [],
'amount' => [],
'date' => [],
'notes' => [],
];
}
public function run()
{
$account = Auth::user()->account;
$payments = Payment::scope()
->orderBy('payment_date', 'desc')
->with('client.contacts', 'invoice')
->withArchived()
->excludeFailed()
->where('payment_date', '>=', $this->startDate)
->where('payment_date', '<=', $this->endDate);
foreach ($payments->get() as $payment) {
$client = $payment->client;
$invoice = $payment->invoice;
if ($client->is_deleted || $invoice->is_deleted) {
continue;
}
$this->data[] = [
trans('texts.payment'),
$client ? ($this->isExport ? $client->getDisplayName() : $client->present()->link) : '',
$account->formatMoney($payment->getCompletedAmount(), $client),
$payment->present()->payment_date,
$payment->present()->method,
];
$this->addToTotals($client->currency_id, 'revenue', $payment->getCompletedAmount(), $payment->present()->month);
$this->addToTotals($client->currency_id, 'expenses', 0, $payment->present()->month);
$this->addToTotals($client->currency_id, 'profit', $payment->getCompletedAmount(), $payment->present()->month);
}
$expenses = Expense::scope()
->orderBy('expense_date', 'desc')
->with('client.contacts')
->withArchived()
->where('expense_date', '>=', $this->startDate)
->where('expense_date', '<=', $this->endDate);
foreach ($expenses->get() as $expense) {
$client = $expense->client;
$this->data[] = [
trans('texts.expense'),
$client ? ($this->isExport ? $client->getDisplayName() : $client->present()->link) : '',
'-' . $expense->present()->amount,
$expense->present()->expense_date,
$expense->present()->category,
];
$this->addToTotals($expense->expense_currency_id, 'revenue', 0, $expense->present()->month);
$this->addToTotals($expense->expense_currency_id, 'expenses', $expense->amountWithTax(), $expense->present()->month);
$this->addToTotals($expense->expense_currency_id, 'profit', $expense->amountWithTax() * -1, $expense->present()->month);
}
//$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);
}
}