2016-01-06 20:52:09 +01:00
|
|
|
<?php namespace App\Services;
|
|
|
|
|
2016-01-08 19:01:00 +01:00
|
|
|
use DB;
|
2016-01-06 20:52:09 +01:00
|
|
|
use Utils;
|
|
|
|
use URL;
|
|
|
|
use App\Services\BaseService;
|
|
|
|
use App\Ninja\Repositories\ExpenseRepository;
|
2016-01-27 19:31:08 +01:00
|
|
|
use App\Models\Client;
|
|
|
|
use App\Models\Vendor;
|
2016-01-07 12:04:01 +01:00
|
|
|
|
2016-01-06 20:52:09 +01:00
|
|
|
class ExpenseService extends BaseService
|
|
|
|
{
|
2016-01-07 16:14:11 +01:00
|
|
|
// Expenses
|
2016-01-06 20:52:09 +01:00
|
|
|
protected $expenseRepo;
|
|
|
|
protected $datatableService;
|
|
|
|
|
|
|
|
public function __construct(ExpenseRepository $expenseRepo, DatatableService $datatableService)
|
|
|
|
{
|
|
|
|
$this->expenseRepo = $expenseRepo;
|
|
|
|
$this->datatableService = $datatableService;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getRepo()
|
|
|
|
{
|
|
|
|
return $this->expenseRepo;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function save($data)
|
|
|
|
{
|
2016-01-27 19:31:08 +01:00
|
|
|
if (isset($data['client_id']) && $data['client_id']) {
|
|
|
|
$data['client_id'] = Client::getPrivateId($data['client_id']);
|
|
|
|
}
|
2016-03-02 14:36:42 +01:00
|
|
|
|
2016-01-27 19:31:08 +01:00
|
|
|
if (isset($data['vendor_id']) && $data['vendor_id']) {
|
|
|
|
$data['vendor_id'] = Vendor::getPrivateId($data['vendor_id']);
|
|
|
|
}
|
2016-03-02 14:36:42 +01:00
|
|
|
|
2016-01-06 20:52:09 +01:00
|
|
|
return $this->expenseRepo->save($data);
|
|
|
|
}
|
|
|
|
|
2016-01-07 12:04:01 +01:00
|
|
|
public function getDatatable($search)
|
2016-01-06 20:52:09 +01:00
|
|
|
{
|
2016-01-07 12:04:01 +01:00
|
|
|
$query = $this->expenseRepo->find($search);
|
2016-01-06 20:52:09 +01:00
|
|
|
|
2016-01-07 12:04:01 +01:00
|
|
|
return $this->createDatatable(ENTITY_EXPENSE, $query);
|
2016-01-06 20:52:09 +01:00
|
|
|
}
|
|
|
|
|
2016-01-19 20:35:15 +01:00
|
|
|
public function getDatatableVendor($vendorPublicId)
|
|
|
|
{
|
|
|
|
$query = $this->expenseRepo->findVendor($vendorPublicId);
|
|
|
|
return $this->datatableService->createDatatable(ENTITY_EXPENSE,
|
|
|
|
$query,
|
|
|
|
$this->getDatatableColumnsVendor(ENTITY_EXPENSE,false),
|
|
|
|
$this->getDatatableActionsVendor(ENTITY_EXPENSE),
|
|
|
|
false);
|
|
|
|
}
|
|
|
|
|
2016-01-06 20:52:09 +01:00
|
|
|
protected function getDatatableColumns($entityType, $hideClient)
|
|
|
|
{
|
|
|
|
return [
|
2016-01-08 19:01:00 +01:00
|
|
|
[
|
2016-01-09 06:24:43 +01:00
|
|
|
'vendor_name',
|
2016-01-08 19:01:00 +01:00
|
|
|
function ($model)
|
|
|
|
{
|
2016-01-23 22:36:31 +01:00
|
|
|
if ($model->vendor_public_id) {
|
2016-03-02 14:36:42 +01:00
|
|
|
return link_to("vendors/{$model->vendor_public_id}", $model->vendor_name)->toHtml();
|
2016-01-08 19:01:00 +01:00
|
|
|
} else {
|
2016-01-23 22:36:31 +01:00
|
|
|
return '';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'client_name',
|
|
|
|
function ($model)
|
|
|
|
{
|
|
|
|
if ($model->client_public_id) {
|
2016-03-02 14:36:42 +01:00
|
|
|
return link_to("clients/{$model->client_public_id}", Utils::getClientDisplayName($model))->toHtml();
|
2016-01-23 22:36:31 +01:00
|
|
|
} else {
|
|
|
|
return '';
|
2016-01-08 19:01:00 +01:00
|
|
|
}
|
2016-01-09 06:24:43 +01:00
|
|
|
}
|
2016-01-08 19:01:00 +01:00
|
|
|
],
|
2016-01-06 20:52:09 +01:00
|
|
|
[
|
2016-01-08 19:01:00 +01:00
|
|
|
'expense_date',
|
2016-01-06 20:52:09 +01:00
|
|
|
function ($model) {
|
2016-03-02 14:36:42 +01:00
|
|
|
return link_to("expenses/{$model->public_id}/edit", Utils::fromSqlDate($model->expense_date))->toHtml();
|
2016-01-06 20:52:09 +01:00
|
|
|
}
|
|
|
|
],
|
|
|
|
[
|
2016-01-20 23:09:10 +01:00
|
|
|
'amount',
|
2016-01-06 20:52:09 +01:00
|
|
|
function ($model) {
|
2016-01-23 19:36:11 +01:00
|
|
|
// show both the amount and the converted amount
|
2016-01-21 23:29:10 +01:00
|
|
|
if ($model->exchange_rate != 1) {
|
2016-01-31 14:10:33 +01:00
|
|
|
$converted = round($model->amount * $model->exchange_rate, 2);
|
2016-03-02 14:36:42 +01:00
|
|
|
return Utils::formatMoney($model->amount, $model->expense_currency_id) . ' | ' .
|
2016-02-01 23:07:09 +01:00
|
|
|
Utils::formatMoney($converted, $model->invoice_currency_id);
|
2016-01-31 14:10:33 +01:00
|
|
|
} else {
|
2016-02-01 23:07:09 +01:00
|
|
|
return Utils::formatMoney($model->amount, $model->expense_currency_id);
|
2016-01-21 23:29:10 +01:00
|
|
|
}
|
2016-01-06 20:52:09 +01:00
|
|
|
}
|
|
|
|
],
|
|
|
|
[
|
2016-01-20 23:09:10 +01:00
|
|
|
'public_notes',
|
2016-01-06 20:52:09 +01:00
|
|
|
function ($model) {
|
2016-01-20 23:09:10 +01:00
|
|
|
return $model->public_notes != null ? substr($model->public_notes, 0, 100) : '';
|
2016-01-06 20:52:09 +01:00
|
|
|
}
|
2016-01-08 19:01:00 +01:00
|
|
|
],
|
|
|
|
[
|
2016-02-17 14:55:31 +01:00
|
|
|
'expense_status_id',
|
2016-01-08 19:01:00 +01:00
|
|
|
function ($model) {
|
2016-01-20 23:09:10 +01:00
|
|
|
return self::getStatusLabel($model->invoice_id, $model->should_be_invoiced);
|
2016-01-08 19:01:00 +01:00
|
|
|
}
|
|
|
|
],
|
2016-01-19 20:35:15 +01:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getDatatableColumnsVendor($entityType, $hideClient)
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
[
|
|
|
|
'expense_date',
|
|
|
|
function ($model) {
|
2016-01-28 15:07:03 +01:00
|
|
|
return Utils::dateToString($model->expense_date);
|
2016-01-19 20:35:15 +01:00
|
|
|
}
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'amount',
|
|
|
|
function ($model) {
|
|
|
|
return Utils::formatMoney($model->amount, false, false);
|
|
|
|
}
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'public_notes',
|
|
|
|
function ($model) {
|
|
|
|
return $model->public_notes != null ? $model->public_notes : '';
|
|
|
|
}
|
|
|
|
],
|
|
|
|
[
|
2016-01-20 23:09:10 +01:00
|
|
|
'invoice_id',
|
2016-01-19 20:35:15 +01:00
|
|
|
function ($model) {
|
2016-01-20 23:09:10 +01:00
|
|
|
return '';
|
2016-01-19 20:35:15 +01:00
|
|
|
}
|
|
|
|
],
|
2016-01-06 20:52:09 +01:00
|
|
|
];
|
|
|
|
}
|
2016-01-09 06:24:43 +01:00
|
|
|
|
2016-01-06 20:52:09 +01:00
|
|
|
protected function getDatatableActions($entityType)
|
|
|
|
{
|
2016-01-20 23:09:10 +01:00
|
|
|
return [
|
2016-01-09 06:24:43 +01:00
|
|
|
[
|
2016-01-20 23:09:10 +01:00
|
|
|
trans('texts.edit_expense'),
|
2016-01-09 06:24:43 +01:00
|
|
|
function ($model) {
|
2016-01-20 23:09:10 +01:00
|
|
|
return URL::to("expenses/{$model->public_id}/edit") ;
|
2016-01-09 06:24:43 +01:00
|
|
|
}
|
|
|
|
],
|
2016-01-21 23:29:10 +01:00
|
|
|
[
|
|
|
|
trans('texts.view_invoice'),
|
|
|
|
function ($model) {
|
|
|
|
return URL::to("/invoices/{$model->invoice_public_id}/edit");
|
|
|
|
},
|
|
|
|
function ($model) {
|
|
|
|
return $model->invoice_public_id;
|
|
|
|
}
|
|
|
|
],
|
2016-01-09 09:08:24 +01:00
|
|
|
[
|
2016-01-20 23:09:10 +01:00
|
|
|
trans('texts.invoice_expense'),
|
2016-01-09 09:08:24 +01:00
|
|
|
function ($model) {
|
2016-01-21 23:29:10 +01:00
|
|
|
return "javascript:invoiceEntity({$model->public_id})";
|
|
|
|
},
|
|
|
|
function ($model) {
|
|
|
|
return ! $model->invoice_id && (!$model->deleted_at || $model->deleted_at == '0000-00-00');
|
2016-01-09 09:08:24 +01:00
|
|
|
}
|
|
|
|
],
|
2016-01-06 20:52:09 +01:00
|
|
|
];
|
|
|
|
}
|
2016-01-20 23:09:10 +01:00
|
|
|
|
2016-01-19 20:35:15 +01:00
|
|
|
protected function getDatatableActionsVendor($entityType)
|
|
|
|
{
|
2016-01-20 23:09:10 +01:00
|
|
|
return [];
|
|
|
|
}
|
2016-03-02 14:36:42 +01:00
|
|
|
|
2016-01-20 23:09:10 +01:00
|
|
|
private function getStatusLabel($invoiceId, $shouldBeInvoiced)
|
|
|
|
{
|
|
|
|
if ($invoiceId) {
|
|
|
|
$label = trans('texts.invoiced');
|
|
|
|
$class = 'success';
|
|
|
|
} elseif ($shouldBeInvoiced) {
|
|
|
|
$label = trans('texts.pending');
|
|
|
|
$class = 'warning';
|
|
|
|
} else {
|
|
|
|
$label = trans('texts.logged');
|
|
|
|
$class = 'primary';
|
|
|
|
}
|
2016-01-19 20:35:15 +01:00
|
|
|
|
2016-01-20 23:09:10 +01:00
|
|
|
return "<h4><div class=\"label label-{$class}\">$label</div></h4>";
|
2016-01-19 20:35:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|