1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-11 13:42:49 +01:00
invoiceninja/app/Repositories/QuoteRepository.php

98 lines
3.0 KiB
PHP
Raw Normal View History

2019-05-02 13:07:38 +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-05-02 13:07:38 +02:00
namespace App\Repositories;
use App\Factory\QuoteInvitationFactory;
use App\Helpers\Invoice\InvoiceSum;
use App\Jobs\Quote\ApplyQuoteNumber;
use App\Jobs\Quote\CreateQuoteInvitations;
use App\Models\Client;
use App\Models\ClientContact;
2019-05-02 13:07:38 +02:00
use App\Models\Quote;
use App\Models\QuoteInvitation;
2019-05-02 13:07:38 +02:00
use Illuminate\Http\Request;
/**
* QuoteRepository
*/
class QuoteRepository extends BaseRepository
{
public function getClassName()
{
return Quote::class;
}
public function save($data, Quote $quote) : ?Quote
{
/* Always carry forward the initial invoice amount this is important for tracking client balance changes later......*/
$starting_amount = $quote->amount;
$quote->fill($data);
2019-05-02 13:07:38 +02:00
$quote->save();
2019-05-02 13:07:38 +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();
}
}
}
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($quote->invitations->pluck('key'))->diff($invitations->pluck('key'))->each(function ($invitation) {
QuoteInvitation::destroy($invitation);
});
foreach ($data['invitations'] as $invitation) {
$inv = false;
2019-05-02 13:07:38 +02:00
if (array_key_exists('key', $invitation)) {
$inv = QuoteInvitation::whereKey($invitation['key'])->first();
}
2019-05-02 13:07:38 +02:00
if (!$inv) {
$invitation['client_contact_id'] = $this->decodePrimaryKey($invitation['client_contact_id']);
$new_invitation = QuoteInvitationFactory::create($quote->company_id, $quote->user_id);
$new_invitation->fill($invitation);
$new_invitation->quote_id = $quote->id;
$new_invitation->save();
}
}
}
/* If no invitations have been created, this is our fail safe to maintain state*/
if ($quote->invitations->count() == 0) {
CreateQuoteInvitations::dispatchNow($quote, $quote->company);
}
$quote = $quote->calc()->getInvoice();
2019-05-02 13:07:38 +02:00
$quote->save();
$finished_amount = $quote->amount;
$quote = ApplyQuoteNumber::dispatchNow($quote, $quote->client->getMergedSettings(), $quote->company);
return $quote->fresh();
}
}