2019-04-04 01:17:15 +02:00
|
|
|
<?php
|
2019-05-11 05:32:07 +02:00
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com)
|
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
|
|
|
* @copyright Copyright (c) 2019. Invoice Ninja LLC (https://invoiceninja.com)
|
|
|
|
*
|
|
|
|
* @license https://opensource.org/licenses/AAL
|
|
|
|
*/
|
2019-04-04 01:17:15 +02:00
|
|
|
|
|
|
|
namespace App\Repositories;
|
|
|
|
|
2019-05-15 07:03:18 +02:00
|
|
|
use App\Events\Invoice\InvoiceWasCreated;
|
|
|
|
use App\Events\Invoice\InvoiceWasUpdated;
|
2019-05-29 06:33:53 +02:00
|
|
|
use App\Factory\InvoiceInvitationFactory;
|
2019-10-16 11:28:52 +02:00
|
|
|
use App\Helpers\Invoice\InvoiceSum;
|
2019-05-15 06:47:07 +02:00
|
|
|
use App\Jobs\Company\UpdateCompanyLedgerWithInvoice;
|
2019-11-08 01:38:22 +01:00
|
|
|
use App\Jobs\Invoice\ApplyInvoiceNumber;
|
2019-11-16 04:12:29 +01:00
|
|
|
use App\Jobs\Invoice\CreateInvoiceInvitations;
|
2019-12-26 23:33:07 +01:00
|
|
|
use App\Jobs\Product\UpdateOrCreateProduct;
|
2019-09-29 10:46:53 +02:00
|
|
|
use App\Listeners\Invoice\CreateInvoiceInvitation;
|
2019-09-23 05:22:24 +02:00
|
|
|
use App\Models\ClientContact;
|
2019-04-04 01:17:15 +02:00
|
|
|
use App\Models\Invoice;
|
2019-05-29 06:33:53 +02:00
|
|
|
use App\Models\InvoiceInvitation;
|
2019-09-23 05:22:24 +02:00
|
|
|
use App\Utils\Traits\MakesHash;
|
2019-04-04 01:17:15 +02:00
|
|
|
use Illuminate\Http\Request;
|
2019-05-29 06:33:53 +02:00
|
|
|
use Illuminate\Support\Carbon;
|
2019-04-04 01:17:15 +02:00
|
|
|
|
|
|
|
/**
|
2019-04-16 05:28:05 +02:00
|
|
|
* InvoiceRepository
|
2019-04-04 01:17:15 +02:00
|
|
|
*/
|
|
|
|
class InvoiceRepository extends BaseRepository
|
|
|
|
{
|
2019-09-23 05:22:24 +02:00
|
|
|
use MakesHash;
|
2019-05-09 00:47:38 +02:00
|
|
|
/**
|
|
|
|
* Gets the class name.
|
|
|
|
*
|
2019-05-10 08:08:33 +02:00
|
|
|
* @return string The class name.
|
2019-05-09 00:47:38 +02:00
|
|
|
*/
|
2019-04-04 01:17:15 +02:00
|
|
|
public function getClassName()
|
|
|
|
{
|
2019-06-04 07:53:34 +02:00
|
|
|
|
2019-04-04 01:17:15 +02:00
|
|
|
return Invoice::class;
|
2019-06-04 07:53:34 +02:00
|
|
|
|
2019-04-04 01:17:15 +02:00
|
|
|
}
|
|
|
|
|
2019-05-09 00:47:38 +02:00
|
|
|
/**
|
|
|
|
* Saves the invoices
|
|
|
|
*
|
|
|
|
* @param array. $data The invoice data
|
2019-10-16 11:28:52 +02:00
|
|
|
* @param InvoiceSum|\App\Models\Invoice $invoice The invoice
|
2019-05-09 00:47:38 +02:00
|
|
|
*
|
2019-10-16 11:28:52 +02:00
|
|
|
* @return Invoice|InvoiceSum|\App\Models\Invoice|null Returns the invoice object
|
2019-05-09 00:47:38 +02:00
|
|
|
*/
|
|
|
|
public function save($data, Invoice $invoice) : ?Invoice
|
2019-04-04 01:17:15 +02:00
|
|
|
{
|
2019-05-15 07:03:18 +02:00
|
|
|
|
2019-05-15 06:47:07 +02:00
|
|
|
/* Always carry forward the initial invoice amount this is important for tracking client balance changes later......*/
|
|
|
|
$starting_amount = $invoice->amount;
|
2019-05-09 07:29:31 +02:00
|
|
|
|
2019-05-06 07:34:59 +02:00
|
|
|
$invoice->fill($data);
|
2019-11-16 04:12:29 +01:00
|
|
|
|
2019-04-04 01:17:15 +02:00
|
|
|
$invoice->save();
|
|
|
|
|
2019-09-23 05:22:24 +02:00
|
|
|
if(isset($data['client_contacts']))
|
|
|
|
{
|
|
|
|
foreach($data['client_contacts'] as $contact)
|
|
|
|
{
|
|
|
|
if($contact['send_invoice'] == 1)
|
|
|
|
{
|
|
|
|
$client_contact = ClientContact::find($this->decodePrimaryKey($contact['id']));
|
|
|
|
$client_contact->send_invoice = true;
|
|
|
|
$client_contact->save();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-06 23:57:09 +01:00
|
|
|
|
|
|
|
if(isset($data['invitations']))
|
|
|
|
{
|
|
|
|
|
|
|
|
$invitations = collect($data['invitations']);
|
|
|
|
|
|
|
|
/* Get array of Keyss which have been removed from the invitations array and soft delete each invitation */
|
|
|
|
collect($invoice->invitations->pluck('key'))->diff($invitations->pluck('key'))->each(function($invitation){
|
|
|
|
|
|
|
|
InvoiceInvitation::destroy($invitation);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
foreach($data['invitations'] as $invitation)
|
|
|
|
{
|
2019-12-17 23:40:15 +01:00
|
|
|
$inv = false;
|
|
|
|
|
|
|
|
if(array_key_exists ('key', $invitation))
|
|
|
|
$inv = InvoiceInvitation::whereKey($invitation['key'])->first();
|
2019-11-06 23:57:09 +01:00
|
|
|
|
|
|
|
if(!$inv)
|
|
|
|
{
|
|
|
|
$invitation['client_contact_id'] = $this->decodePrimaryKey($invitation['client_contact_id']);
|
|
|
|
|
|
|
|
$new_invitation = InvoiceInvitationFactory::create($invoice->company_id, $invoice->user_id);
|
|
|
|
$new_invitation->fill($invitation);
|
|
|
|
$new_invitation->invoice_id = $invoice->id;
|
|
|
|
$new_invitation->save();
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-11-16 04:12:29 +01:00
|
|
|
/* If no invitations have been created, this is our fail safe to maintain state*/
|
|
|
|
if($invoice->invitations->count() == 0)
|
2019-12-27 01:28:36 +01:00
|
|
|
CreateInvoiceInvitations::dispatchNow($invoice, $invoice->company);
|
2019-05-30 08:01:17 +02:00
|
|
|
|
2019-10-22 04:07:00 +02:00
|
|
|
$invoice = $invoice->calc()->getInvoice();
|
|
|
|
|
2019-05-09 07:29:31 +02:00
|
|
|
$invoice->save();
|
|
|
|
|
2019-05-15 06:47:07 +02:00
|
|
|
$finished_amount = $invoice->amount;
|
|
|
|
|
2019-10-30 03:41:18 +01:00
|
|
|
/**/
|
2019-05-15 06:47:07 +02:00
|
|
|
if($finished_amount != $starting_amount)
|
2019-05-16 00:26:21 +02:00
|
|
|
UpdateCompanyLedgerWithInvoice::dispatchNow($invoice, ($finished_amount - $starting_amount));
|
2019-05-15 07:03:18 +02:00
|
|
|
|
2019-12-27 01:28:36 +01:00
|
|
|
$invoice = ApplyInvoiceNumber::dispatchNow($invoice, $invoice->client->getMergedSettings(), $invoice->company);
|
2019-11-08 01:38:22 +01:00
|
|
|
|
2019-12-26 23:33:07 +01:00
|
|
|
if($invoice->company->update_products !== false)
|
|
|
|
UpdateOrCreateProduct::dispatch($invoice->line_items, $invoice);
|
|
|
|
|
2019-11-06 23:57:09 +01:00
|
|
|
return $invoice->fresh();
|
2019-05-09 23:50:21 +02:00
|
|
|
|
2019-04-04 01:17:15 +02:00
|
|
|
}
|
|
|
|
|
2019-05-09 00:47:38 +02:00
|
|
|
/**
|
|
|
|
* Mark the invoice as sent.
|
|
|
|
*
|
|
|
|
* @param \App\Models\Invoice $invoice The invoice
|
|
|
|
*
|
|
|
|
* @return Invoice|\App\Models\Invoice|null Return the invoice object
|
|
|
|
*/
|
|
|
|
public function markSent(Invoice $invoice) : ?Invoice
|
|
|
|
{
|
2019-11-19 22:06:48 +01:00
|
|
|
$invoice->markSent();
|
2019-10-01 08:03:57 +02:00
|
|
|
|
2019-10-30 03:41:18 +01:00
|
|
|
/*
|
|
|
|
* Why? because up until this point the invoice was a draft.
|
|
|
|
* When marked as sent it becomes a ledgerable item.
|
|
|
|
*
|
|
|
|
*/
|
2019-12-27 01:28:36 +01:00
|
|
|
$invoice = ApplyInvoiceNumber::dispatchNow($invoice, $invoice->client->getMergedSettings(), $invoice->company);
|
2019-11-08 01:38:22 +01:00
|
|
|
|
2019-11-19 11:23:56 +01:00
|
|
|
UpdateCompanyLedgerWithInvoice::dispatchNow($invoice, $invoice->balance);
|
2019-05-09 00:47:38 +02:00
|
|
|
|
|
|
|
return $invoice;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-05-30 08:01:17 +02:00
|
|
|
|
2019-05-29 13:15:42 +02:00
|
|
|
|
2019-05-29 06:33:53 +02:00
|
|
|
|
2019-04-04 01:17:15 +02:00
|
|
|
}
|