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

90 lines
2.3 KiB
PHP
Raw Normal View History

<?php
2020-07-01 03:06:40 +02:00
/**
* Invoice Ninja (https://invoiceninja.com).
2020-07-01 03:06:40 +02:00
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2021. 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
*/
namespace App\Services\Quote;
use App\Factory\CloneQuoteToInvoiceFactory;
use App\Factory\InvoiceInvitationFactory;
use App\Models\Invoice;
2020-06-02 12:19:29 +02:00
use App\Models\Quote;
use App\Repositories\InvoiceRepository;
use App\Utils\Traits\MakesHash;
class ConvertQuote
{
use MakesHash;
private $client;
private $invoice_repo;
public function __construct($client)
{
$this->client = $client;
$this->invoice_repo = new InvoiceRepository();
}
/**
* @param $quote
* @return mixed
*/
public function run($quote)
{
2020-10-28 11:10:49 +01:00
$invoice = CloneQuoteToInvoiceFactory::create($quote, $quote->user_id);
$invoice->design_id = $this->decodePrimaryKey($this->client->getSetting('invoice_design_id'));
//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;
$invoice = $this->invoice_repo->save($invoice_array, $invoice);
2021-04-18 13:16:32 +02:00
$invoice->fresh();
$invoice->service()
->fillDefaults()
->save();
$quote->invoice_id = $invoice->id;
2020-06-02 12:19:29 +02:00
$quote->status_id = Quote::STATUS_CONVERTED;
$quote->save();
// maybe should return invoice here
return $invoice;
}
/**
* Only create the invitations that are defined on the quote.
*
* @return Invoice $invoice
*/
private function createConversionInvitations($invoice, $quote)
{
$invites = [];
foreach($quote->invitations as $quote_invitation){
$ii = InvoiceInvitationFactory::create($invoice->company_id, $invoice->user_id);
2021-11-06 01:46:12 +01:00
$ii->key = $this->createDbHash($invoice->company->db);
$ii->client_contact_id = $quote_invitation->client_contact_id;
$invites[] = $ii;
}
return $invites;
}
}