2021-08-24 13:18:32 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Requests\Statements;
|
|
|
|
|
2021-08-25 03:41:07 +02:00
|
|
|
use App\Http\Requests\Request;
|
|
|
|
use App\Models\Client;
|
2021-08-24 15:31:40 +02:00
|
|
|
use App\Models\Invoice;
|
|
|
|
use App\Models\Payment;
|
2021-08-25 03:41:07 +02:00
|
|
|
use App\Utils\Number;
|
|
|
|
use App\Utils\Traits\MakesHash;
|
|
|
|
use Carbon\Carbon;
|
2021-08-24 13:18:32 +02:00
|
|
|
|
2021-08-25 03:41:07 +02:00
|
|
|
class CreateStatementRequest extends Request
|
2021-08-24 13:18:32 +02:00
|
|
|
{
|
2021-08-25 03:41:07 +02:00
|
|
|
use MakesHash;
|
2021-08-24 13:18:32 +02:00
|
|
|
/**
|
|
|
|
* Determine if the user is authorized to make this request.
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function authorize(): bool
|
|
|
|
{
|
|
|
|
return auth()->user()->isAdmin();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the validation rules that apply to the request.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function rules()
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'start_date' => ['required'],
|
2021-08-25 03:41:07 +02:00
|
|
|
'end_date' => ['required'],
|
|
|
|
'client_id' => 'bail|required|exists:clients,id,company_id,'.auth()->user()->company()->id,
|
|
|
|
'show_payments_table' => 'boolean',
|
|
|
|
'show_aging_table' => 'boolean',
|
2021-08-24 13:18:32 +02:00
|
|
|
];
|
|
|
|
}
|
2021-08-25 03:41:07 +02:00
|
|
|
protected function prepareForValidation()
|
|
|
|
{
|
|
|
|
$input = $this->all();
|
2021-08-24 15:31:40 +02:00
|
|
|
|
2021-08-25 03:41:07 +02:00
|
|
|
$input = $this->decodePrimaryKeys($input);
|
|
|
|
|
|
|
|
$this->replace($input);
|
|
|
|
}
|
2021-08-24 15:31:40 +02:00
|
|
|
/**
|
|
|
|
* The collection of invoices for the statement.
|
|
|
|
*
|
|
|
|
* @return Invoice[]|\Illuminate\Database\Eloquent\Collection
|
|
|
|
*/
|
|
|
|
public function getInvoices()
|
|
|
|
{
|
2021-08-25 03:41:07 +02:00
|
|
|
$input = $this->all();
|
|
|
|
|
|
|
|
// $input['start_date & $input['end_date are available.
|
|
|
|
$client = Client::where('id', $input['client_id'])->first();
|
2021-08-24 15:31:40 +02:00
|
|
|
|
2021-08-25 03:41:07 +02:00
|
|
|
$from = Carbon::parse($input['start_date']);
|
|
|
|
$to = Carbon::parse($input['end_date']);
|
|
|
|
|
|
|
|
return Invoice::where('company_id', auth()->user()->company()->id)
|
|
|
|
->where('client_id', $client->id)
|
|
|
|
->whereIn('status_id', [Invoice::STATUS_SENT, Invoice::STATUS_PARTIAL, Invoice::STATUS_PAID])
|
|
|
|
->whereBetween('date',[$from, $to])
|
|
|
|
->get();
|
2021-08-24 15:31:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The collection of payments for the statement.
|
|
|
|
*
|
|
|
|
* @return Payment[]|\Illuminate\Database\Eloquent\Collection
|
|
|
|
*/
|
|
|
|
public function getPayments()
|
|
|
|
{
|
2021-08-25 03:41:07 +02:00
|
|
|
// $input['start_date & $input['end_date are available.
|
|
|
|
$input = $this->all();
|
|
|
|
|
|
|
|
$client = Client::where('id', $input['client_id'])->first();
|
|
|
|
|
|
|
|
$from = Carbon::parse($input['start_date']);
|
|
|
|
$to = Carbon::parse($input['end_date']);
|
|
|
|
|
|
|
|
return Payment::where('company_id', auth()->user()->company()->id)
|
|
|
|
->where('client_id', $client->id)
|
|
|
|
->whereIn('status_id', [Payment::STATUS_COMPLETED, Payment::STATUS_PARTIALLY_REFUNDED, Payment::STATUS_REFUNDED])
|
|
|
|
->whereBetween('date',[$from, $to])
|
|
|
|
->get();
|
2021-08-24 15:31:40 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-08-25 03:41:07 +02:00
|
|
|
|
2021-08-24 15:31:40 +02:00
|
|
|
/**
|
|
|
|
* The array of aging data.
|
|
|
|
*/
|
|
|
|
public function getAging(): array
|
|
|
|
{
|
|
|
|
return [
|
2021-08-25 03:41:07 +02:00
|
|
|
'0-30' => $this->getAgingAmount('30'),
|
|
|
|
'30-60' => $this->getAgingAmount('60'),
|
|
|
|
'60-90' => $this->getAgingAmount('90'),
|
|
|
|
'90-120' => $this->getAgingAmount('120'),
|
|
|
|
'120+' => $this->getAgingAmount('120+'),
|
2021-08-24 15:31:40 +02:00
|
|
|
];
|
|
|
|
}
|
2021-08-25 03:41:07 +02:00
|
|
|
|
|
|
|
private function getAgingAmount($range)
|
|
|
|
{
|
|
|
|
$input = $this->all();
|
|
|
|
|
|
|
|
$ranges = $this->calculateDateRanges($range);
|
|
|
|
|
|
|
|
$from = $ranges[0];
|
|
|
|
$to = $ranges[1];
|
|
|
|
|
|
|
|
$client = Client::where('id', $input['client_id'])->first();
|
|
|
|
|
|
|
|
$amount = Invoice::where('company_id', auth()->user()->company()->id)
|
|
|
|
->where('client_id', $client->id)
|
|
|
|
->whereIn('status_id', [Invoice::STATUS_SENT, Invoice::STATUS_PARTIAL])
|
|
|
|
->where('balance', '>', 0)
|
|
|
|
->whereBetween('date',[$from, $to])
|
|
|
|
->sum('balance');
|
|
|
|
|
|
|
|
return Number::formatMoney($amount, $client);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function calculateDateRanges($range)
|
|
|
|
{
|
|
|
|
|
|
|
|
$ranges = [];
|
|
|
|
|
|
|
|
switch ($range) {
|
|
|
|
case '30':
|
|
|
|
$ranges[0] = now();
|
|
|
|
$ranges[1] = now()->subDays(30);
|
|
|
|
return $ranges;
|
|
|
|
break;
|
|
|
|
case '60':
|
|
|
|
$ranges[0] = now()->subDays(30);
|
|
|
|
$ranges[1] = now()->subDays(60);
|
|
|
|
return $ranges;
|
|
|
|
break;
|
|
|
|
case '90':
|
|
|
|
$ranges[0] = now()->subDays(60);
|
|
|
|
$ranges[1] = now()->subDays(90);
|
|
|
|
return $ranges;
|
|
|
|
break;
|
|
|
|
case '120':
|
|
|
|
$ranges[0] = now()->subDays(90);
|
|
|
|
$ranges[1] = now()->subDays(120);
|
|
|
|
return $ranges;
|
|
|
|
break;
|
|
|
|
case '120+':
|
|
|
|
$ranges[0] = now()->subDays(120);
|
|
|
|
$ranges[1] = now()->subYears(40);
|
|
|
|
return $ranges;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
$ranges[0] = now()->subDays(0);
|
|
|
|
$ranges[1] = now()->subDays(30);
|
|
|
|
return $ranges;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2021-08-24 13:18:32 +02:00
|
|
|
}
|