2015-10-28 20:22:07 +01:00
|
|
|
<?php namespace App\Services;
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
use App\Models\Invoice;
|
2016-01-20 09:41:57 +01:00
|
|
|
use Auth;
|
2015-11-05 23:37:04 +01:00
|
|
|
use Utils;
|
2015-10-28 20:22:07 +01:00
|
|
|
use App\Ninja\Repositories\InvoiceRepository;
|
|
|
|
use App\Ninja\Repositories\ClientRepository;
|
|
|
|
use App\Events\QuoteInvitationWasApproved;
|
|
|
|
use App\Models\Invitation;
|
2016-03-16 03:07:11 +01:00
|
|
|
use App\Models\Client;
|
2016-05-23 18:52:20 +02:00
|
|
|
use App\Ninja\Datatables\InvoiceDatatable;
|
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.
|
|
|
|
*
|
|
|
|
* @param ClientRepository $clientRepo
|
|
|
|
* @param InvoiceRepository $invoiceRepo
|
|
|
|
* @param DatatableService $datatableService
|
|
|
|
*/
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @return InvoiceRepository
|
|
|
|
*/
|
2015-10-28 20:22:07 +01:00
|
|
|
protected function getRepo()
|
|
|
|
{
|
|
|
|
return $this->invoiceRepo;
|
|
|
|
}
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @param array $data
|
|
|
|
* @param Invoice|null $invoice
|
|
|
|
* @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
|
|
|
}
|
|
|
|
|
2016-05-02 19:42:13 +02:00
|
|
|
$invoice = $this->invoiceRepo->save($data, $invoice);
|
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;
|
|
|
|
}
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @param $quote
|
|
|
|
* @param Invitation|null $invitation
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function convertQuote($quote, Invitation $invitation = null)
|
2016-01-20 09:41:57 +01:00
|
|
|
{
|
|
|
|
$invoice = $this->invoiceRepo->cloneInvoice($quote, $quote->id);
|
|
|
|
if (!$invitation) {
|
|
|
|
return $invoice;
|
|
|
|
}
|
2016-03-02 14:36:42 +01:00
|
|
|
|
2016-01-20 09:41:57 +01:00
|
|
|
foreach ($invoice->invitations as $invoiceInvitation) {
|
|
|
|
if ($invitation->contact_id == $invoiceInvitation->contact_id) {
|
|
|
|
return $invoiceInvitation->invitation_key;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @param $quote
|
|
|
|
* @param Invitation|null $invitation
|
|
|
|
* @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
|
|
|
|
2016-05-26 16:56:54 +02:00
|
|
|
if (!$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
|
|
|
|
2016-04-19 04:35:18 +02:00
|
|
|
if ($account->auto_convert_quote || ! $account->hasFeature(FEATURE_QUOTES)) {
|
2016-01-20 09:41:57 +01:00
|
|
|
$invoice = $this->convertQuote($quote, $invitation);
|
2015-10-28 20:22:07 +01:00
|
|
|
|
2016-01-20 09:41:57 +01:00
|
|
|
event(new QuoteInvitationWasApproved($quote, $invoice, $invitation));
|
2015-10-28 20:22:07 +01:00
|
|
|
|
2016-01-20 09:41:57 +01:00
|
|
|
return $invoice;
|
|
|
|
} else {
|
|
|
|
$quote->markApproved();
|
2015-10-28 20:22:07 +01:00
|
|
|
|
2016-01-20 09:41:57 +01:00
|
|
|
event(new QuoteInvitationWasApproved($quote, null, $invitation));
|
2016-03-02 14:36:42 +01:00
|
|
|
|
2016-01-20 09:41:57 +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)
|
|
|
|
{
|
2016-05-23 18:52:20 +02:00
|
|
|
$datatable = new InvoiceDatatable( ! $clientPublicId, $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
|
|
|
|
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
|
|
|
|
2016-05-23 18:52:20 +02:00
|
|
|
return $this->datatableService->createDatatable($datatable, $query);
|
2015-11-05 23:37:04 +01:00
|
|
|
}
|
2016-03-02 14:36:42 +01:00
|
|
|
|
2016-01-20 09:41:57 +01:00
|
|
|
}
|