2020-02-11 21:57:25 +01:00
|
|
|
<?php
|
2020-07-01 03:06:40 +02:00
|
|
|
|
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
2020-07-01 03:06:40 +02:00
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2022-04-27 05:20:41 +02:00
|
|
|
* @copyright Copyright (c) 2022. Invoice Ninja LLC (https://invoiceninja.com)
|
2020-07-01 03:06:40 +02:00
|
|
|
*
|
2021-06-16 08:58:16 +02:00
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
2020-07-01 03:06:40 +02:00
|
|
|
*/
|
|
|
|
|
2020-02-11 21:57:25 +01:00
|
|
|
namespace App\Services\Quote;
|
|
|
|
|
|
|
|
use App\Factory\CloneQuoteToInvoiceFactory;
|
2021-10-18 04:31:21 +02:00
|
|
|
use App\Factory\InvoiceInvitationFactory;
|
|
|
|
use App\Models\Invoice;
|
2020-06-02 12:19:29 +02:00
|
|
|
use App\Models\Quote;
|
2020-02-11 21:57:25 +01:00
|
|
|
use App\Repositories\InvoiceRepository;
|
2022-03-25 07:12:49 +01:00
|
|
|
use App\Utils\Traits\GeneratesConvertedQuoteCounter;
|
2021-08-11 12:06:28 +02:00
|
|
|
use App\Utils\Traits\MakesHash;
|
2020-02-11 21:57:25 +01:00
|
|
|
|
|
|
|
class ConvertQuote
|
|
|
|
{
|
2021-08-11 12:06:28 +02:00
|
|
|
use MakesHash;
|
2022-03-25 07:12:49 +01:00
|
|
|
use GeneratesConvertedQuoteCounter;
|
2021-08-11 12:06:28 +02:00
|
|
|
|
2020-02-11 21:57:25 +01:00
|
|
|
private $client;
|
2021-08-11 12:06:28 +02:00
|
|
|
|
2020-02-11 21:57:25 +01:00
|
|
|
private $invoice_repo;
|
|
|
|
|
2020-05-27 06:46:19 +02:00
|
|
|
public function __construct($client)
|
2020-02-11 21:57:25 +01:00
|
|
|
{
|
|
|
|
$this->client = $client;
|
2020-05-27 06:46:19 +02:00
|
|
|
$this->invoice_repo = new InvoiceRepository();
|
2020-02-11 21:57:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $quote
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2020-02-14 04:32:22 +01:00
|
|
|
public function run($quote)
|
2020-02-11 21:57:25 +01:00
|
|
|
{
|
2020-10-28 11:10:49 +01:00
|
|
|
$invoice = CloneQuoteToInvoiceFactory::create($quote, $quote->user_id);
|
2021-08-11 12:06:28 +02:00
|
|
|
$invoice->design_id = $this->decodePrimaryKey($this->client->getSetting('invoice_design_id'));
|
2021-10-18 04:31:21 +02:00
|
|
|
|
|
|
|
//create invitations here before the repo save()
|
|
|
|
//we need to do this here otherwise the repo_save will create
|
|
|
|
//invitations for ALL contacts
|
|
|
|
$invites = $this->createConversionInvitations($invoice, $quote);
|
|
|
|
$invoice_array = $invoice->toArray();
|
|
|
|
$invoice_array['invitations'] = $invites;
|
|
|
|
|
2022-03-25 07:12:49 +01:00
|
|
|
//try and convert the invoice number to a quote number here.
|
2022-06-21 11:57:17 +02:00
|
|
|
if ($this->client->getSetting('shared_invoice_quote_counter')) {
|
2022-03-25 07:12:49 +01:00
|
|
|
$converted_number = $this->harvestQuoteCounter($quote, $invoice, $this->client);
|
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if ($converted_number) {
|
2022-03-25 07:12:49 +01:00
|
|
|
$invoice_array['number'] = $converted_number;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-18 04:31:21 +02:00
|
|
|
$invoice = $this->invoice_repo->save($invoice_array, $invoice);
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2020-05-27 06:46:19 +02:00
|
|
|
$invoice->fresh();
|
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
$invoice->service()
|
2021-07-25 10:05:53 +02:00
|
|
|
->fillDefaults()
|
2022-06-08 12:40:26 +02:00
|
|
|
->adjustInventory()
|
2020-05-27 06:46:19 +02:00
|
|
|
->save();
|
|
|
|
|
|
|
|
$quote->invoice_id = $invoice->id;
|
2020-06-02 12:19:29 +02:00
|
|
|
$quote->status_id = Quote::STATUS_CONVERTED;
|
2020-05-27 06:46:19 +02:00
|
|
|
$quote->save();
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2020-02-11 21:57:25 +01:00
|
|
|
// maybe should return invoice here
|
2020-05-27 06:46:19 +02:00
|
|
|
return $invoice;
|
2020-02-11 21:57:25 +01:00
|
|
|
}
|
2021-10-18 04:31:21 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Only create the invitations that are defined on the quote.
|
2022-06-21 11:57:17 +02:00
|
|
|
*
|
2021-10-18 04:31:21 +02:00
|
|
|
* @return Invoice $invoice
|
|
|
|
*/
|
|
|
|
private function createConversionInvitations($invoice, $quote)
|
|
|
|
{
|
|
|
|
$invites = [];
|
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
foreach ($quote->invitations as $quote_invitation) {
|
2021-10-18 04:31:21 +02:00
|
|
|
$ii = InvoiceInvitationFactory::create($invoice->company_id, $invoice->user_id);
|
2021-11-06 01:46:12 +01:00
|
|
|
$ii->key = $this->createDbHash($invoice->company->db);
|
2021-10-18 04:31:21 +02:00
|
|
|
$ii->client_contact_id = $quote_invitation->client_contact_id;
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2021-10-18 04:31:21 +02:00
|
|
|
$invites[] = $ii;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $invites;
|
|
|
|
}
|
2020-02-11 21:57:25 +01:00
|
|
|
}
|