1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-16 08:02:40 +01:00
invoiceninja/app/ninja/repositories/CreditRepository.php

78 lines
2.4 KiB
PHP
Raw Normal View History

2014-01-06 19:03:00 +01:00
<?php namespace ninja\repositories;
use Credit;
use Client;
use Utils;
class CreditRepository
{
2015-01-11 13:30:08 +01:00
public function find($clientPublicId = null, $filter = null)
{
$query = \DB::table('credits')
->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.is_primary', '=', true)
->select('credits.public_id', 'clients.name as client_name', 'clients.public_id as client_public_id', 'credits.amount', 'credits.balance', 'credits.credit_date', 'clients.currency_id', 'contacts.first_name', 'contacts.last_name', 'contacts.email', 'credits.private_notes', 'credits.deleted_at', 'credits.is_deleted');
2014-01-06 19:03:00 +01:00
2015-01-11 13:30:08 +01:00
if ($clientPublicId) {
$query->where('clients.public_id', '=', $clientPublicId);
}
2014-01-06 19:03:00 +01:00
2015-01-11 13:30:08 +01:00
if (!\Session::get('show_trash:credit')) {
2014-02-18 22:56:18 +01:00
$query->where('credits.deleted_at', '=', null);
}
2015-01-11 13:30:08 +01:00
if ($filter) {
$query->where(function ($query) use ($filter) {
$query->where('clients.name', 'like', '%'.$filter.'%');
});
}
2014-01-06 19:03:00 +01:00
2015-01-11 13:30:08 +01:00
return $query;
}
2014-01-06 19:03:00 +01:00
2015-01-11 13:30:08 +01:00
public function save($publicId = null, $input)
{
if ($publicId) {
2014-01-06 19:03:00 +01:00
$credit = Credit::scope($publicId)->firstOrFail();
2015-01-11 13:30:08 +01:00
} else {
2014-01-06 19:03:00 +01:00
$credit = Credit::createNew();
}
2015-01-11 13:30:08 +01:00
2014-01-06 19:03:00 +01:00
$credit->client_id = Client::getPrivateId($input['client']);
$credit->credit_date = Utils::toSqlDate($input['credit_date']);
2014-01-14 12:52:56 +01:00
$credit->amount = Utils::parseFloat($input['amount']);
2014-01-16 22:12:46 +01:00
$credit->balance = Utils::parseFloat($input['amount']);
2014-01-15 15:01:24 +01:00
$credit->private_notes = trim($input['private_notes']);
2014-01-06 19:03:00 +01:00
$credit->save();
return $credit;
2015-01-11 13:30:08 +01:00
}
2014-01-06 19:03:00 +01:00
2015-01-11 13:30:08 +01:00
public function bulk($ids, $action)
{
if (!$ids) {
2014-01-12 19:55:33 +01:00
return 0;
}
2015-01-11 13:30:08 +01:00
$credits = Credit::withTrashed()->scope($ids)->get();
2014-01-06 19:03:00 +01:00
2015-01-11 13:30:08 +01:00
foreach ($credits as $credit) {
if ($action == 'restore') {
2014-11-23 22:47:10 +01:00
$credit->restore();
2015-01-11 13:30:08 +01:00
} else {
if ($action == 'delete') {
2014-11-23 22:47:10 +01:00
$credit->is_deleted = true;
$credit->save();
2015-01-11 13:30:08 +01:00
}
2014-11-23 22:47:10 +01:00
$credit->delete();
}
2014-01-06 19:03:00 +01:00
}
return count($credits);
2015-01-11 13:30:08 +01:00
}
}