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

134 lines
4.4 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;
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
{
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, $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-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 {
$canSaveClient = Auth::user()->can('edit', Client::scope($clientPublicId)->first());
2016-05-23 18:52:20 +02:00
}
if ($canSaveClient) {
$client = $this->clientRepo->save($data['client']);
$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;
}
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;
2016-05-23 18:52:20 +02:00
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->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);
2015-11-05 23:37:04 +01:00
$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
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
}