1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-24 10:21:35 +02:00
invoiceninja/app/Services/ExpenseService.php

162 lines
4.5 KiB
PHP
Raw Normal View History

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-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)
{
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-09 06:24:43 +01:00
if($model->vendor_public_id) {
return link_to("vendors/{$model->vendor_public_id}", $model->vendor_name);
2016-01-08 19:01:00 +01:00
} else {
2016-01-09 06:24:43 +01:00
return 'No vendor' ;
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-01-08 19:01:00 +01:00
return Utils::fromSqlDate($model->expense_date);
2016-01-06 20:52:09 +01:00
}
],
[
'amount',
2016-01-06 20:52:09 +01:00
function ($model) {
return Utils::formatMoney($model->amount, false, false);
2016-01-06 20:52:09 +01:00
}
],
[
'public_notes',
2016-01-06 20:52:09 +01:00
function ($model) {
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
],
[
'invoice_id',
2016-01-08 19:01:00 +01:00
function ($model) {
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) {
return $model->expense_date;
}
],
[
'amount',
function ($model) {
return Utils::formatMoney($model->amount, false, false);
}
],
[
'public_notes',
function ($model) {
return $model->public_notes != null ? $model->public_notes : '';
}
],
[
'invoice_id',
2016-01-19 20:35:15 +01:00
function ($model) {
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)
{
return [
2016-01-09 06:24:43 +01:00
[
trans('texts.edit_expense'),
2016-01-09 06:24:43 +01:00
function ($model) {
return URL::to("expenses/{$model->public_id}/edit") ;
2016-01-09 06:24:43 +01:00
}
],
/*
2016-01-09 09:08:24 +01:00
[
trans('texts.invoice_expense'),
2016-01-09 09:08:24 +01:00
function ($model) {
return URL::to("expense/invoice/{$model->public_id}") . '?client=1';
2016-01-09 09:08:24 +01:00
}
],
*/
2016-01-06 20:52:09 +01:00
];
}
2016-01-19 20:35:15 +01:00
protected function getDatatableActionsVendor($entityType)
{
return [];
}
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
return "<h4><div class=\"label label-{$class}\">$label</div></h4>";
2016-01-19 20:35:15 +01:00
}
}