1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-14 07:02:34 +01:00
invoiceninja/app/Services/InvoiceService.php

310 lines
11 KiB
PHP
Raw Normal View History

2015-10-28 20:22:07 +01:00
<?php namespace App\Services;
use Auth;
2015-11-05 23:37:04 +01:00
use Utils;
use URL;
2015-10-28 20:22:07 +01:00
use App\Services\BaseService;
use App\Ninja\Repositories\InvoiceRepository;
use App\Ninja\Repositories\ClientRepository;
use App\Events\QuoteInvitationWasApproved;
use App\Models\Invitation;
2016-03-16 00:08:00 +01:00
use App\Models\Invoice;
use App\Models\Client;
2016-03-16 00:08:00 +01:00
use App\Models\Payment;
2015-10-28 20:22:07 +01:00
class InvoiceService extends BaseService
{
protected $clientRepo;
protected $invoiceRepo;
2015-11-05 23:37:04 +01:00
protected $datatableService;
2015-10-28 20:22:07 +01:00
2015-11-05 23:37:04 +01:00
public function __construct(ClientRepository $clientRepo, InvoiceRepository $invoiceRepo, DatatableService $datatableService)
2015-10-28 20:22:07 +01:00
{
$this->clientRepo = $clientRepo;
$this->invoiceRepo = $invoiceRepo;
2015-11-05 23:37:04 +01:00
$this->datatableService = $datatableService;
2015-10-28 20:22:07 +01:00
}
protected function getRepo()
{
return $this->invoiceRepo;
}
public function save($data, $checkSubPermissions = false)
2015-10-28 20:22:07 +01:00
{
if (isset($data['client'])) {
$can_save_client = !$checkSubPermissions;
if(!$can_save_client){
if(empty($data['client']['public_id']) || $data['client']['public_id']=='-1'){
$can_save_client = Client::canCreate();
}
else{
$can_save_client = Client::wherePublicId($data['client']['public_id'])->first()->canEdit();
}
}
if($can_save_client){
$client = $this->clientRepo->save($data['client']);
$data['client_id'] = $client->id;
}
2015-10-28 20:22:07 +01:00
}
$invoice = $this->invoiceRepo->save($data, $checkSubPermissions);
2016-03-02 14:36:42 +01:00
2015-10-28 20:22:07 +01:00
$client = $invoice->client;
$client->load('contacts');
$sendInvoiceIds = [];
foreach ($client->contacts as $contact) {
if ($contact->send_invoice || count($client->contacts) == 1) {
$sendInvoiceIds[] = $contact->id;
}
}
2016-03-02 14:36:42 +01:00
2015-10-28 20:22:07 +01:00
foreach ($client->contacts as $contact) {
$invitation = Invitation::scope()->whereContactId($contact->id)->whereInvoiceId($invoice->id)->first();
if (in_array($contact->id, $sendInvoiceIds) && !$invitation) {
$invitation = Invitation::createNew();
$invitation->invoice_id = $invoice->id;
$invitation->contact_id = $contact->id;
$invitation->invitation_key = str_random(RANDOM_KEY_LENGTH);
$invitation->save();
} elseif (!in_array($contact->id, $sendInvoiceIds) && $invitation) {
$invitation->delete();
}
}
return $invoice;
}
public function convertQuote($quote, $invitation = null)
{
$invoice = $this->invoiceRepo->cloneInvoice($quote, $quote->id);
if (!$invitation) {
return $invoice;
}
2016-03-02 14:36:42 +01:00
foreach ($invoice->invitations as $invoiceInvitation) {
if ($invitation->contact_id == $invoiceInvitation->contact_id) {
return $invoiceInvitation->invitation_key;
}
}
}
2015-10-28 20:22:07 +01:00
public function approveQuote($quote, $invitation = null)
{
2016-03-10 23:29:07 +01:00
$account = $quote->account;
2015-10-28 20:22:07 +01:00
if (!$quote->is_quote || $quote->quote_invoice_id) {
return null;
}
2016-03-02 14:36:42 +01:00
if ($account->auto_convert_quote || ! $account->isPro()) {
$invoice = $this->convertQuote($quote, $invitation);
2015-10-28 20:22:07 +01:00
event(new QuoteInvitationWasApproved($quote, $invoice, $invitation));
2015-10-28 20:22:07 +01:00
return $invoice;
} else {
$quote->markApproved();
2015-10-28 20:22:07 +01:00
event(new QuoteInvitationWasApproved($quote, null, $invitation));
2016-03-02 14:36:42 +01:00
foreach ($quote->invitations as $invoiceInvitation) {
if ($invitation->contact_id == $invoiceInvitation->contact_id) {
return $invoiceInvitation->invitation_key;
}
2015-10-28 20:22:07 +01:00
}
}
2015-11-05 23:37:04 +01:00
}
public function getDatatable($accountId, $clientPublicId = null, $entityType, $search)
{
$query = $this->invoiceRepo->getInvoices($accountId, $clientPublicId, $entityType, $search)
->where('invoices.is_quote', '=', $entityType == ENTITY_QUOTE ? true : false);
2016-03-16 00:08:00 +01:00
if(!Utils::hasPermission('view_all')){
$query->where('invoices.user_id', '=', Auth::user()->id);
}
2015-11-05 23:37:04 +01:00
return $this->createDatatable($entityType, $query, !$clientPublicId);
}
protected function getDatatableColumns($entityType, $hideClient)
{
return [
[
'invoice_number',
function ($model) use ($entityType) {
if(!Invoice::canEditItem($model)){
return $model->invoice_number;
}
2016-03-02 14:36:42 +01:00
return link_to("{$entityType}s/{$model->public_id}/edit", $model->invoice_number, ['class' => Utils::getEntityRowClass($model)])->toHtml();
2015-11-05 23:37:04 +01:00
}
],
[
'client_name',
function ($model) {
if(!Client::canViewItemByOwner($model->client_user_id)){
return Utils::getClientDisplayName($model);
}
2016-03-02 14:36:42 +01:00
return link_to("clients/{$model->client_public_id}", Utils::getClientDisplayName($model))->toHtml();
2015-11-05 23:37:04 +01:00
},
! $hideClient
],
[
'invoice_date',
function ($model) {
return Utils::fromSqlDate($model->invoice_date);
}
],
[
'amount',
function ($model) {
return Utils::formatMoney($model->amount, $model->currency_id, $model->country_id);
2015-11-05 23:37:04 +01:00
}
],
[
'balance',
function ($model) {
return $model->partial > 0 ?
trans('texts.partial_remaining', [
'partial' => Utils::formatMoney($model->partial, $model->currency_id, $model->country_id),
'balance' => Utils::formatMoney($model->balance, $model->currency_id, $model->country_id)]
2015-11-05 23:37:04 +01:00
) :
Utils::formatMoney($model->balance, $model->currency_id, $model->country_id);
2015-11-05 23:37:04 +01:00
},
$entityType == ENTITY_INVOICE
],
[
'due_date',
function ($model) {
return Utils::fromSqlDate($model->due_date);
},
],
[
'invoice_status_name',
function ($model) use ($entityType) {
return $model->quote_invoice_id ? link_to("invoices/{$model->quote_invoice_id}/edit", trans('texts.converted'))->toHtml() : self::getStatusLabel($entityType, $model);
2015-11-05 23:37:04 +01:00
}
]
];
}
protected function getDatatableActions($entityType)
{
return [
[
trans("texts.edit_{$entityType}"),
function ($model) use ($entityType) {
return URL::to("{$entityType}s/{$model->public_id}/edit");
2016-03-16 00:08:00 +01:00
},
function ($model) {
return Invoice::canEditItem($model);
2015-11-05 23:37:04 +01:00
}
],
[
trans("texts.clone_{$entityType}"),
function ($model) use ($entityType) {
return URL::to("{$entityType}s/{$model->public_id}/clone");
2016-03-16 00:08:00 +01:00
},
function ($model) {
return Invoice::canCreate();
2015-11-05 23:37:04 +01:00
}
],
[
trans("texts.view_history"),
function ($model) use ($entityType) {
return URL::to("{$entityType}s/{$entityType}_history/{$model->public_id}");
}
],
2016-03-16 00:08:00 +01:00
[
'--divider--', function(){return false;},
function ($model) {
return Invoice::canEditItem($model) || Payment::canCreate();
}
],
2015-11-05 23:37:04 +01:00
[
trans("texts.mark_sent"),
function ($model) {
return "javascript:markEntity({$model->public_id})";
},
function ($model) {
2016-03-16 00:08:00 +01:00
return $model->invoice_status_id < INVOICE_STATUS_SENT && Invoice::canEditItem($model);
2015-11-05 23:37:04 +01:00
}
],
[
trans('texts.enter_payment'),
function ($model) {
return URL::to("payments/create/{$model->client_public_id}/{$model->public_id}");
},
function ($model) use ($entityType) {
2016-03-16 00:08:00 +01:00
return $entityType == ENTITY_INVOICE && $model->balance > 0 && Payment::canCreate();
2015-11-05 23:37:04 +01:00
}
],
[
trans("texts.view_quote"),
function ($model) {
return URL::to("quotes/{$model->quote_id}/edit");
},
function ($model) use ($entityType) {
2016-03-16 00:08:00 +01:00
return $entityType == ENTITY_INVOICE && $model->quote_id && Invoice::canEditItem($model);
2015-11-05 23:37:04 +01:00
}
],
[
trans("texts.view_invoice"),
function ($model) {
return URL::to("invoices/{$model->quote_invoice_id}/edit");
},
function ($model) use ($entityType) {
2016-03-16 00:08:00 +01:00
return $entityType == ENTITY_QUOTE && $model->quote_invoice_id && Invoice::canEditItem($model);
2015-11-05 23:37:04 +01:00
}
],
[
trans("texts.convert_to_invoice"),
function ($model) {
return "javascript:convertEntity({$model->public_id})";
},
function ($model) use ($entityType) {
2016-03-16 00:08:00 +01:00
return $entityType == ENTITY_QUOTE && ! $model->quote_invoice_id && Invoice::canEditItem($model);
2015-11-05 23:37:04 +01:00
}
]
];
}
private function getStatusLabel($entityType, $model)
2015-11-05 23:37:04 +01:00
{
2016-02-11 20:12:50 +01:00
// check if invoice is overdue
if (Utils::parseFloat($model->balance) && $model->due_date && $model->due_date != '0000-00-00') {
if (\DateTime::createFromFormat('Y-m-d', $model->due_date) < new \DateTime("now")) {
$label = $entityType == ENTITY_INVOICE ? trans('texts.overdue') : trans('texts.expired');
return "<h4><div class=\"label label-danger\">" . $label . "</div></h4>";
2016-02-11 20:12:50 +01:00
}
}
$label = trans("texts.status_" . strtolower($model->invoice_status_name));
2015-11-05 23:37:04 +01:00
$class = 'default';
2016-02-11 20:12:50 +01:00
switch ($model->invoice_status_id) {
2015-11-05 23:37:04 +01:00
case INVOICE_STATUS_SENT:
$class = 'info';
break;
case INVOICE_STATUS_VIEWED:
$class = 'warning';
break;
case INVOICE_STATUS_APPROVED:
$class = 'success';
break;
2015-11-05 23:37:04 +01:00
case INVOICE_STATUS_PARTIAL:
$class = 'primary';
break;
case INVOICE_STATUS_PAID:
$class = 'success';
break;
}
return "<h4><div class=\"label label-{$class}\">$label</div></h4>";
}
2016-03-02 14:36:42 +01:00
}