2017-01-30 20:40:43 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Services;
|
2015-10-28 20:22:07 +01:00
|
|
|
|
|
|
|
use App\Events\QuoteInvitationWasApproved;
|
2016-03-16 03:07:11 +01:00
|
|
|
use App\Models\Client;
|
2017-01-30 20:40:43 +01:00
|
|
|
use App\Models\Invitation;
|
|
|
|
use App\Models\Invoice;
|
2016-05-23 18:52:20 +02:00
|
|
|
use App\Ninja\Datatables\InvoiceDatatable;
|
2017-01-30 20:40:43 +01:00
|
|
|
use App\Ninja\Repositories\ClientRepository;
|
|
|
|
use App\Ninja\Repositories\InvoiceRepository;
|
2017-07-19 11:59:56 +02:00
|
|
|
use App\Jobs\DownloadInvoices;
|
2017-01-30 20:40:43 +01:00
|
|
|
use Auth;
|
|
|
|
use Utils;
|
2015-10-28 20:22:07 +01:00
|
|
|
|
|
|
|
class InvoiceService extends BaseService
|
|
|
|
{
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @var ClientRepository
|
|
|
|
*/
|
2015-10-28 20:22:07 +01:00
|
|
|
protected $clientRepo;
|
2016-07-03 18:11:58 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var InvoiceRepository
|
|
|
|
*/
|
2015-10-28 20:22:07 +01:00
|
|
|
protected $invoiceRepo;
|
2016-07-03 18:11:58 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var DatatableService
|
|
|
|
*/
|
2015-11-05 23:37:04 +01:00
|
|
|
protected $datatableService;
|
2015-10-28 20:22:07 +01:00
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* InvoiceService constructor.
|
|
|
|
*
|
2017-01-30 20:40:43 +01:00
|
|
|
* @param ClientRepository $clientRepo
|
2016-07-03 18:11:58 +02:00
|
|
|
* @param InvoiceRepository $invoiceRepo
|
2017-01-30 20:40:43 +01:00
|
|
|
* @param DatatableService $datatableService
|
2016-07-03 18:11:58 +02:00
|
|
|
*/
|
|
|
|
public function __construct(
|
|
|
|
ClientRepository $clientRepo,
|
|
|
|
InvoiceRepository $invoiceRepo,
|
|
|
|
DatatableService $datatableService
|
2017-01-30 17:05:31 +01:00
|
|
|
) {
|
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
|
|
|
}
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @return InvoiceRepository
|
|
|
|
*/
|
2015-10-28 20:22:07 +01:00
|
|
|
protected function getRepo()
|
|
|
|
{
|
|
|
|
return $this->invoiceRepo;
|
|
|
|
}
|
|
|
|
|
2017-07-19 11:59:56 +02:00
|
|
|
/**
|
|
|
|
* @param $ids
|
|
|
|
* @param $action
|
|
|
|
*
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function bulk($ids, $action)
|
|
|
|
{
|
|
|
|
if ($action == 'download') {
|
|
|
|
$invoices = $this->getRepo()->findByPublicIdsWithTrashed($ids);
|
|
|
|
dispatch(new DownloadInvoices(Auth::user(), $invoices));
|
2017-07-19 16:34:09 +02:00
|
|
|
return count($invoices);
|
2017-07-19 11:59:56 +02:00
|
|
|
} else {
|
|
|
|
return parent::bulk($ids, $action);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
2017-01-30 20:40:43 +01:00
|
|
|
* @param array $data
|
2016-07-03 18:11:58 +02:00
|
|
|
* @param Invoice|null $invoice
|
2017-01-30 20:40:43 +01:00
|
|
|
*
|
2016-07-03 18:11:58 +02:00
|
|
|
* @return \App\Models\Invoice|Invoice|mixed
|
|
|
|
*/
|
|
|
|
public function save(array $data, Invoice $invoice = null)
|
2015-10-28 20:22:07 +01:00
|
|
|
{
|
|
|
|
if (isset($data['client'])) {
|
2016-04-27 22:56:14 +02:00
|
|
|
$canSaveClient = false;
|
2016-06-20 16:14:43 +02:00
|
|
|
$canViewClient = false;
|
2016-05-23 18:52:20 +02:00
|
|
|
$clientPublicId = array_get($data, 'client.public_id') ?: array_get($data, 'client.id');
|
2016-04-27 22:56:14 +02:00
|
|
|
if (empty($clientPublicId) || $clientPublicId == '-1') {
|
|
|
|
$canSaveClient = Auth::user()->can('create', ENTITY_CLIENT);
|
|
|
|
} else {
|
2016-06-20 16:14:43 +02:00
|
|
|
$client = Client::scope($clientPublicId)->first();
|
|
|
|
$canSaveClient = Auth::user()->can('edit', $client);
|
|
|
|
$canViewClient = Auth::user()->can('view', $client);
|
2016-05-23 18:52:20 +02:00
|
|
|
}
|
2016-04-21 12:01:36 +02:00
|
|
|
if ($canSaveClient) {
|
2016-03-16 03:07:11 +01:00
|
|
|
$client = $this->clientRepo->save($data['client']);
|
2016-06-20 16:14:43 +02:00
|
|
|
}
|
|
|
|
if ($canSaveClient || $canViewClient) {
|
2016-03-16 03:07:11 +01:00
|
|
|
$data['client_id'] = $client->id;
|
|
|
|
}
|
2015-10-28 20:22:07 +01:00
|
|
|
}
|
|
|
|
|
2017-03-28 10:55:02 +02:00
|
|
|
return $this->invoiceRepo->save($data, $invoice);
|
2015-10-28 20:22:07 +01:00
|
|
|
}
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @param $quote
|
|
|
|
* @param Invitation|null $invitation
|
2017-01-30 20:40:43 +01:00
|
|
|
*
|
2016-07-03 18:11:58 +02:00
|
|
|
* @return mixed
|
|
|
|
*/
|
2016-07-04 12:21:40 +02:00
|
|
|
public function convertQuote($quote)
|
2016-01-20 09:41:57 +01:00
|
|
|
{
|
2018-03-13 22:04:24 +01:00
|
|
|
$account = $quote->account;
|
|
|
|
$invoice = $this->invoiceRepo->cloneInvoice($quote, $quote->id);
|
|
|
|
|
|
|
|
if ($account->auto_archive_quote) {
|
|
|
|
$this->invoiceRepo->archive($quote);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $invoice;
|
2016-01-20 09:41:57 +01:00
|
|
|
}
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @param $quote
|
|
|
|
* @param Invitation|null $invitation
|
2017-01-30 20:40:43 +01:00
|
|
|
*
|
2016-07-03 18:11:58 +02:00
|
|
|
* @return mixed|null
|
|
|
|
*/
|
|
|
|
public function approveQuote($quote, Invitation $invitation = null)
|
2015-10-28 20:22:07 +01:00
|
|
|
{
|
2016-03-10 23:29:07 +01:00
|
|
|
$account = $quote->account;
|
2016-05-23 18:52:20 +02:00
|
|
|
|
2017-01-30 17:05:31 +01:00
|
|
|
if (! $account->hasFeature(FEATURE_QUOTES) || ! $quote->isType(INVOICE_TYPE_QUOTE) || $quote->quote_invoice_id) {
|
2015-10-28 20:22:07 +01:00
|
|
|
return null;
|
|
|
|
}
|
2016-03-02 14:36:42 +01:00
|
|
|
|
2017-03-12 21:01:08 +01:00
|
|
|
event(new QuoteInvitationWasApproved($quote, $invitation));
|
|
|
|
|
2016-07-20 09:03:46 +02:00
|
|
|
if ($account->auto_convert_quote) {
|
2016-07-04 12:21:40 +02:00
|
|
|
$invoice = $this->convertQuote($quote);
|
|
|
|
|
|
|
|
foreach ($invoice->invitations as $invoiceInvitation) {
|
|
|
|
if ($invitation->contact_id == $invoiceInvitation->contact_id) {
|
|
|
|
$invitation = $invoiceInvitation;
|
|
|
|
}
|
|
|
|
}
|
2016-01-20 09:41:57 +01:00
|
|
|
} else {
|
|
|
|
$quote->markApproved();
|
2016-07-20 09:03:46 +02:00
|
|
|
}
|
2015-10-28 20:22:07 +01:00
|
|
|
|
2018-03-13 22:04:24 +01:00
|
|
|
if ($account->auto_archive_quote) {
|
|
|
|
$this->invoiceRepo->archive($quote);
|
|
|
|
}
|
|
|
|
|
2016-07-20 09:03:46 +02:00
|
|
|
return $invitation->invitation_key;
|
2015-11-05 23:37:04 +01:00
|
|
|
}
|
|
|
|
|
2017-01-30 20:40:43 +01:00
|
|
|
public function getDatatable($accountId, $clientPublicId, $entityType, $search)
|
2015-11-05 23:37:04 +01:00
|
|
|
{
|
2016-11-24 10:22:37 +01:00
|
|
|
$datatable = new InvoiceDatatable(true, $clientPublicId);
|
2016-05-29 14:45:37 +02:00
|
|
|
$datatable->entityType = $entityType;
|
|
|
|
|
2015-11-05 23:37:04 +01:00
|
|
|
$query = $this->invoiceRepo->getInvoices($accountId, $clientPublicId, $entityType, $search)
|
2016-05-26 16:56:54 +02:00
|
|
|
->where('invoices.invoice_type_id', '=', $entityType == ENTITY_QUOTE ? INVOICE_TYPE_QUOTE : INVOICE_TYPE_STANDARD);
|
2015-11-05 23:37:04 +01:00
|
|
|
|
2018-06-07 12:08:34 +02:00
|
|
|
if (! Utils::hasPermission('view_invoice')) {
|
2016-03-16 00:08:00 +01:00
|
|
|
$query->where('invoices.user_id', '=', Auth::user()->id);
|
|
|
|
}
|
2015-11-05 23:37:04 +01:00
|
|
|
|
2016-05-23 18:52:20 +02:00
|
|
|
return $this->datatableService->createDatatable($datatable, $query);
|
2015-11-05 23:37:04 +01:00
|
|
|
}
|
2016-01-20 09:41:57 +01:00
|
|
|
}
|