1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-20 16:31:33 +02:00
invoiceninja/app/Ninja/Repositories/CreditRepository.php
Joshua Dwire 90e1f6695c Improve user permission support
* Hide links based on permissions
* Restrict editing/creating within other UI
2016-03-15 22:07:11 -04:00

82 lines
2.9 KiB
PHP

<?php namespace App\Ninja\Repositories;
use DB;
use Utils;
use App\Models\Credit;
use App\Models\Client;
use App\Ninja\Repositories\BaseRepository;
class CreditRepository extends BaseRepository
{
public function getClassName()
{
return 'App\Models\Credit';
}
public function find($clientPublicId = null, $filter = null)
{
$query = DB::table('credits')
->join('accounts', 'accounts.id', '=', 'credits.account_id')
->join('clients', 'clients.id', '=', 'credits.client_id')
->join('contacts', 'contacts.client_id', '=', 'clients.id')
->where('clients.account_id', '=', \Auth::user()->account_id)
->where('clients.deleted_at', '=', null)
->where('contacts.deleted_at', '=', null)
->where('contacts.is_primary', '=', true)
->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',
'clients.name as client_name',
'clients.public_id as client_public_id',
'clients.user_id as client_user_id',
'credits.amount',
'credits.balance',
'credits.credit_date',
'contacts.first_name',
'contacts.last_name',
'contacts.email',
'credits.private_notes',
'credits.deleted_at',
'credits.is_deleted',
'credits.user_id'
);
if ($clientPublicId) {
$query->where('clients.public_id', '=', $clientPublicId);
}
if (!\Session::get('show_trash:credit')) {
$query->where('credits.deleted_at', '=', null);
}
if ($filter) {
$query->where(function ($query) use ($filter) {
$query->where('clients.name', 'like', '%'.$filter.'%');
});
}
return $query;
}
public function save($input)
{
$publicId = isset($data['public_id']) ? $data['public_id'] : false;
if ($publicId) {
$credit = Credit::scope($publicId)->firstOrFail();
} else {
$credit = Credit::createNew();
}
$credit->client_id = Client::getPrivateId($input['client']);
$credit->credit_date = Utils::toSqlDate($input['credit_date']);
$credit->amount = Utils::parseFloat($input['amount']);
$credit->balance = Utils::parseFloat($input['amount']);
$credit->private_notes = trim($input['private_notes']);
$credit->save();
return $credit;
}
}