2017-01-30 20:40:43 +01:00
|
|
|
<?php
|
2015-03-16 22:45:25 +01:00
|
|
|
|
2017-01-30 20:40:43 +01:00
|
|
|
namespace App\Ninja\Repositories;
|
|
|
|
|
|
|
|
use App\Models\Client;
|
|
|
|
use App\Models\Credit;
|
2015-12-07 14:34:55 +01:00
|
|
|
use DB;
|
2015-10-28 20:22:07 +01:00
|
|
|
use Utils;
|
2015-03-16 22:45:25 +01:00
|
|
|
|
2015-10-28 20:22:07 +01:00
|
|
|
class CreditRepository extends BaseRepository
|
2015-03-16 22:45:25 +01:00
|
|
|
{
|
2015-10-28 20:22:07 +01:00
|
|
|
public function getClassName()
|
|
|
|
{
|
|
|
|
return 'App\Models\Credit';
|
|
|
|
}
|
|
|
|
|
2015-03-16 22:45:25 +01:00
|
|
|
public function find($clientPublicId = null, $filter = null)
|
|
|
|
{
|
2015-12-07 14:34:55 +01:00
|
|
|
$query = DB::table('credits')
|
|
|
|
->join('accounts', 'accounts.id', '=', 'credits.account_id')
|
2015-03-16 22:45:25 +01:00
|
|
|
->join('clients', 'clients.id', '=', 'credits.client_id')
|
|
|
|
->join('contacts', 'contacts.client_id', '=', 'clients.id')
|
|
|
|
->where('clients.account_id', '=', \Auth::user()->account_id)
|
2015-11-29 17:00:50 +01:00
|
|
|
->where('contacts.deleted_at', '=', null)
|
2015-03-16 22:45:25 +01:00
|
|
|
->where('contacts.is_primary', '=', true)
|
2015-12-07 14:34:55 +01:00
|
|
|
->select(
|
|
|
|
DB::raw('COALESCE(clients.currency_id, accounts.currency_id) currency_id'),
|
|
|
|
DB::raw('COALESCE(clients.country_id, accounts.country_id) country_id'),
|
|
|
|
'credits.public_id',
|
2016-04-27 10:49:54 +02:00
|
|
|
DB::raw("COALESCE(NULLIF(clients.name,''), NULLIF(CONCAT(contacts.first_name, ' ', contacts.last_name),''), NULLIF(contacts.email,'')) client_name"),
|
2015-12-07 14:34:55 +01:00
|
|
|
'clients.public_id as client_public_id',
|
2016-03-16 03:07:11 +01:00
|
|
|
'clients.user_id as client_user_id',
|
2015-12-07 14:34:55 +01:00
|
|
|
'credits.amount',
|
|
|
|
'credits.balance',
|
2017-03-28 11:40:53 +02:00
|
|
|
'credits.credit_date as credit_date_sql',
|
|
|
|
DB::raw("CONCAT(credits.credit_date, credits.created_at) as credit_date"),
|
2015-12-07 14:34:55 +01:00
|
|
|
'contacts.first_name',
|
|
|
|
'contacts.last_name',
|
|
|
|
'contacts.email',
|
|
|
|
'credits.private_notes',
|
2017-03-30 10:46:52 +02:00
|
|
|
'credits.public_notes',
|
2015-12-07 14:34:55 +01:00
|
|
|
'credits.deleted_at',
|
2016-03-16 00:08:00 +01:00
|
|
|
'credits.is_deleted',
|
|
|
|
'credits.user_id'
|
2015-12-07 14:34:55 +01:00
|
|
|
);
|
2015-03-16 22:45:25 +01:00
|
|
|
|
|
|
|
if ($clientPublicId) {
|
|
|
|
$query->where('clients.public_id', '=', $clientPublicId);
|
2016-11-27 13:20:58 +01:00
|
|
|
} else {
|
|
|
|
$query->whereNull('clients.deleted_at');
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|
|
|
|
|
2016-11-18 14:31:43 +01:00
|
|
|
$this->applyFilters($query, ENTITY_CREDIT);
|
2015-03-16 22:45:25 +01:00
|
|
|
|
|
|
|
if ($filter) {
|
|
|
|
$query->where(function ($query) use ($filter) {
|
|
|
|
$query->where('clients.name', 'like', '%'.$filter.'%');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return $query;
|
|
|
|
}
|
|
|
|
|
2016-09-23 11:02:48 +02:00
|
|
|
public function getClientDatatable($clientId)
|
|
|
|
{
|
|
|
|
$query = DB::table('credits')
|
|
|
|
->join('accounts', 'accounts.id', '=', 'credits.account_id')
|
|
|
|
->join('clients', 'clients.id', '=', 'credits.client_id')
|
|
|
|
->where('credits.client_id', '=', $clientId)
|
|
|
|
->where('clients.deleted_at', '=', null)
|
|
|
|
->where('credits.deleted_at', '=', null)
|
|
|
|
->select(
|
|
|
|
DB::raw('COALESCE(clients.currency_id, accounts.currency_id) currency_id'),
|
|
|
|
DB::raw('COALESCE(clients.country_id, accounts.country_id) country_id'),
|
|
|
|
'credits.amount',
|
|
|
|
'credits.balance',
|
2017-03-30 10:46:52 +02:00
|
|
|
'credits.credit_date',
|
|
|
|
'credits.public_notes'
|
2016-09-23 11:02:48 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
$table = \Datatable::query($query)
|
2017-01-30 17:05:31 +01:00
|
|
|
->addColumn('credit_date', function ($model) {
|
|
|
|
return Utils::fromSqlDate($model->credit_date);
|
|
|
|
})
|
|
|
|
->addColumn('amount', function ($model) {
|
|
|
|
return Utils::formatMoney($model->amount, $model->currency_id, $model->country_id);
|
|
|
|
})
|
|
|
|
->addColumn('balance', function ($model) {
|
|
|
|
return Utils::formatMoney($model->balance, $model->currency_id, $model->country_id);
|
|
|
|
})
|
2017-03-30 10:46:52 +02:00
|
|
|
->addColumn('public_notes', function ($model) {
|
|
|
|
return $model->public_notes;
|
|
|
|
})
|
2016-09-23 11:02:48 +02:00
|
|
|
->make();
|
|
|
|
|
|
|
|
return $table;
|
|
|
|
}
|
|
|
|
|
2016-07-21 14:35:23 +02:00
|
|
|
public function save($input, $credit = null)
|
2015-03-16 22:45:25 +01:00
|
|
|
{
|
2015-10-28 20:22:07 +01:00
|
|
|
$publicId = isset($data['public_id']) ? $data['public_id'] : false;
|
2016-09-23 11:02:48 +02:00
|
|
|
|
2016-05-02 19:42:13 +02:00
|
|
|
if ($credit) {
|
2017-05-30 12:56:51 +02:00
|
|
|
// do nothing
|
2016-05-02 19:42:13 +02:00
|
|
|
} elseif ($publicId) {
|
2015-03-16 22:45:25 +01:00
|
|
|
$credit = Credit::scope($publicId)->firstOrFail();
|
2016-05-02 19:42:13 +02:00
|
|
|
\Log::warning('Entity not set in credit repo save');
|
2015-03-16 22:45:25 +01:00
|
|
|
} else {
|
|
|
|
$credit = Credit::createNew();
|
2017-02-26 19:29:45 +01:00
|
|
|
$credit->balance = Utils::parseFloat($input['amount']);
|
2017-05-30 12:56:51 +02:00
|
|
|
$credit->client_id = Client::getPrivateId($input['client_id']);
|
|
|
|
$credit->credit_date = date('Y-m-d');
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|
|
|
|
|
2017-03-30 19:48:22 +02:00
|
|
|
$credit->fill($input);
|
2017-05-30 12:56:51 +02:00
|
|
|
|
|
|
|
if (isset($input['credit_date'])) {
|
|
|
|
$credit->credit_date = Utils::toSqlDate($input['credit_date']);
|
|
|
|
}
|
|
|
|
if (isset($input['amount'])) {
|
|
|
|
$credit->amount = Utils::parseFloat($input['amount']);
|
|
|
|
}
|
|
|
|
if (isset($input['balance'])) {
|
|
|
|
$credit->balance = Utils::parseFloat($input['balance']);
|
|
|
|
}
|
|
|
|
|
2015-03-16 22:45:25 +01:00
|
|
|
$credit->save();
|
|
|
|
|
|
|
|
return $credit;
|
|
|
|
}
|
|
|
|
}
|