1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-08 20:22:42 +01:00
invoiceninja/app/Services/InvoiceService.php

178 lines
5.4 KiB
PHP
Raw Normal View History

2015-10-28 20:22:07 +01:00
<?php namespace App\Services;
use App\Models\Invoice;
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;
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
{
/**
* @var ClientRepository
*/
2015-10-28 20:22:07 +01:00
protected $clientRepo;
/**
* @var InvoiceRepository
*/
2015-10-28 20:22:07 +01:00
protected $invoiceRepo;
/**
* @var DatatableService
*/
2015-11-05 23:37:04 +01:00
protected $datatableService;
2015-10-28 20:22:07 +01: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
}
/**
* @return InvoiceRepository
*/
2015-10-28 20:22:07 +01:00
protected function getRepo()
{
return $this->invoiceRepo;
}
/**
* @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
}
if ($canSaveClient) {
$client = $this->clientRepo->save($data['client']);
2016-06-20 16:14:43 +02:00
}
if ($canSaveClient || $canViewClient) {
$data['client_id'] = $client->id;
}
2015-10-28 20:22:07 +01: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;
}
/**
* @param $quote
* @param Invitation|null $invitation
* @return mixed
*/
public function convertQuote($quote, Invitation $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;
}
}
}
/**
* @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
if ($account->auto_convert_quote || ! $account->hasFeature(FEATURE_QUOTES)) {
$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)
{
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
}