2018-04-29 08:00:37 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Jobs\Client;
|
|
|
|
|
2018-05-01 16:57:35 +02:00
|
|
|
use Utils;
|
2018-04-29 21:27:52 +02:00
|
|
|
use App\Models\InvoiceItem;
|
2018-04-29 08:00:37 +02:00
|
|
|
use App\Models\Invoice;
|
2018-04-29 16:27:55 +02:00
|
|
|
use App\Models\Payment;
|
2018-04-29 21:27:52 +02:00
|
|
|
use App\Models\Eloquent;
|
2018-04-29 08:00:37 +02:00
|
|
|
|
|
|
|
class GenerateStatementData
|
|
|
|
{
|
2018-04-30 22:00:22 +02:00
|
|
|
public function __construct($client, $options, $contact = false)
|
2018-04-29 08:00:37 +02:00
|
|
|
{
|
|
|
|
$this->client = $client;
|
|
|
|
$this->options = $options;
|
2018-04-30 22:00:22 +02:00
|
|
|
$this->contact = $contact;
|
2018-04-29 08:00:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute the job.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function handle()
|
|
|
|
{
|
|
|
|
$client = $this->client;
|
2018-04-30 22:00:22 +02:00
|
|
|
$client->load('contacts');
|
2018-05-01 16:57:35 +02:00
|
|
|
|
2018-04-29 08:00:37 +02:00
|
|
|
$account = $client->account;
|
2018-05-01 16:57:35 +02:00
|
|
|
$account->load(['date_format', 'datetime_format']);
|
2018-05-01 14:47:19 +02:00
|
|
|
|
2018-04-30 22:00:22 +02:00
|
|
|
$invoice = new Invoice();
|
2018-05-01 16:57:35 +02:00
|
|
|
$invoice->invoice_date = Utils::today();
|
2018-04-30 22:00:22 +02:00
|
|
|
$invoice->account = $account;
|
2018-04-29 08:00:37 +02:00
|
|
|
$invoice->client = $client;
|
|
|
|
|
2018-04-29 16:27:55 +02:00
|
|
|
$invoice->invoice_items = $this->getInvoices();
|
|
|
|
|
|
|
|
if ($this->options['show_payments']) {
|
2018-05-01 14:47:19 +02:00
|
|
|
$payments = $this->getPayments($invoice->invoice_items);
|
|
|
|
$invoice->invoice_items = $invoice->invoice_items->merge($payments);
|
2018-04-29 16:27:55 +02:00
|
|
|
}
|
|
|
|
|
2018-05-01 16:57:35 +02:00
|
|
|
$invoice->hidePrivateFields();
|
|
|
|
|
2018-04-29 16:27:55 +02:00
|
|
|
return json_encode($invoice);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getInvoices()
|
|
|
|
{
|
|
|
|
$statusId = intval($this->options['status_id']);
|
|
|
|
|
2018-04-30 22:00:22 +02:00
|
|
|
$invoices = Invoice::with(['client'])
|
2018-04-29 08:00:37 +02:00
|
|
|
->invoices()
|
2018-04-29 16:27:55 +02:00
|
|
|
->whereClientId($this->client->id)
|
2018-04-29 08:00:37 +02:00
|
|
|
->whereIsPublic(true)
|
2018-04-29 16:27:55 +02:00
|
|
|
->withArchived()
|
2018-04-29 08:00:37 +02:00
|
|
|
->orderBy('invoice_date', 'asc');
|
|
|
|
|
|
|
|
if ($statusId == INVOICE_STATUS_PAID) {
|
|
|
|
$invoices->where('invoice_status_id', '=', INVOICE_STATUS_PAID);
|
|
|
|
} elseif ($statusId == INVOICE_STATUS_UNPAID) {
|
|
|
|
$invoices->where('invoice_status_id', '!=', INVOICE_STATUS_PAID);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($statusId == INVOICE_STATUS_PAID || ! $statusId) {
|
2018-04-29 16:27:55 +02:00
|
|
|
$invoices->where('invoice_date', '>=', $this->options['start_date'])
|
|
|
|
->where('invoice_date', '<=', $this->options['end_date']);
|
2018-04-29 08:00:37 +02:00
|
|
|
}
|
|
|
|
|
2018-04-30 22:00:22 +02:00
|
|
|
if ($this->contact) {
|
|
|
|
$invoices->whereHas('invitations', function ($query) {
|
|
|
|
$query->where('contact_id', $this->contact->id);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-04-29 21:27:52 +02:00
|
|
|
$invoices = $invoices->get();
|
|
|
|
$data = collect();
|
|
|
|
|
|
|
|
for ($i=0; $i<$invoices->count(); $i++) {
|
|
|
|
$invoice = $invoices[$i];
|
|
|
|
$item = new InvoiceItem();
|
2018-05-01 14:47:19 +02:00
|
|
|
$item->id = $invoice->id;
|
2018-04-29 21:27:52 +02:00
|
|
|
$item->product_key = $invoice->invoice_number;
|
|
|
|
$item->custom_value1 = $invoice->invoice_date;
|
|
|
|
$item->custom_value2 = $invoice->due_date;
|
2018-04-30 11:34:51 +02:00
|
|
|
$item->notes = $invoice->amount;
|
|
|
|
$item->cost = $invoice->balance;
|
|
|
|
$item->qty = 1;
|
2018-04-29 21:27:52 +02:00
|
|
|
$item->invoice_item_type_id = 1;
|
|
|
|
$data->push($item);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->options['show_aging']) {
|
|
|
|
$aging = $this->getAging($invoices);
|
|
|
|
$data = $data->merge($aging);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $data;
|
2018-04-29 16:27:55 +02:00
|
|
|
}
|
|
|
|
|
2018-05-01 14:47:19 +02:00
|
|
|
private function getPayments($invoices)
|
2018-04-29 16:27:55 +02:00
|
|
|
{
|
2018-04-30 22:00:22 +02:00
|
|
|
$payments = Payment::with('invoice', 'payment_type')
|
2018-04-29 16:27:55 +02:00
|
|
|
->withArchived()
|
|
|
|
->whereClientId($this->client->id)
|
2018-05-01 16:57:35 +02:00
|
|
|
//->excludeFailed()
|
2018-04-29 16:27:55 +02:00
|
|
|
->where('payment_date', '>=', $this->options['start_date'])
|
|
|
|
->where('payment_date', '<=', $this->options['end_date']);
|
|
|
|
|
2018-05-01 14:47:19 +02:00
|
|
|
if ($this->contact) {
|
|
|
|
$payments->whereIn('invoice_id', $invoices->pluck('id'));
|
|
|
|
}
|
|
|
|
|
2018-04-29 21:27:52 +02:00
|
|
|
$payments = $payments->get();
|
|
|
|
$data = collect();
|
|
|
|
|
|
|
|
for ($i=0; $i<$payments->count(); $i++) {
|
|
|
|
$payment = $payments[$i];
|
|
|
|
$item = new InvoiceItem();
|
|
|
|
$item->product_key = $payment->invoice->invoice_number;
|
|
|
|
$item->custom_value1 = $payment->payment_date;
|
2018-05-01 14:47:19 +02:00
|
|
|
$item->custom_value2 = $payment->present()->payment_type;
|
2018-04-29 21:27:52 +02:00
|
|
|
$item->cost = $payment->getCompletedAmount();
|
|
|
|
$item->invoice_item_type_id = 3;
|
|
|
|
$data->push($item);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $data;
|
2018-04-29 16:27:55 +02:00
|
|
|
}
|
|
|
|
|
2018-04-29 21:27:52 +02:00
|
|
|
private function getAging($invoices)
|
2018-04-29 16:27:55 +02:00
|
|
|
{
|
2018-04-29 21:27:52 +02:00
|
|
|
$data = collect();
|
|
|
|
$ageGroups = [
|
|
|
|
'age_group_0' => 0,
|
|
|
|
'age_group_30' => 0,
|
|
|
|
'age_group_60' => 0,
|
|
|
|
'age_group_90' => 0,
|
|
|
|
'age_group_120' => 0,
|
|
|
|
];
|
|
|
|
|
|
|
|
foreach ($invoices as $invoice) {
|
|
|
|
$age = $invoice->present()->ageGroup;
|
|
|
|
$ageGroups[$age] += $invoice->getRequestedAmount();
|
|
|
|
}
|
|
|
|
|
|
|
|
$item = new InvoiceItem();
|
|
|
|
$item->product_key = $ageGroups['age_group_0'];
|
|
|
|
$item->notes = $ageGroups['age_group_30'];
|
|
|
|
$item->custom_value1 = $ageGroups['age_group_60'];
|
|
|
|
$item->custom_value1 = $ageGroups['age_group_90'];
|
|
|
|
$item->cost = $ageGroups['age_group_120'];
|
|
|
|
$item->invoice_item_type_id = 4;
|
|
|
|
$data->push($item);
|
2018-04-29 08:00:37 +02:00
|
|
|
|
2018-04-29 21:27:52 +02:00
|
|
|
return $data;
|
2018-04-29 08:00:37 +02:00
|
|
|
}
|
|
|
|
}
|