1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 21:22:58 +01:00
invoiceninja/app/Repositories/QuoteRepository.php
David Bomba 957ac9f5d8
Fix for password protected authorization (#3198)
* Remove unnecessary save() on invoice

* Update copyright

* Working on Credit Repository

* Implement credits as a paymentable entity

* Add credit_id to transformer

* fix rules for update payment

* Fix random deleted_at keys in transformers

* Fix for password_protect check
2020-01-07 11:13:47 +11:00

102 lines
3.1 KiB
PHP

<?php
/**
* Invoice Ninja (https://invoiceninja.com)
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2020. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://opensource.org/licenses/AAL
*/
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;
use App\Models\Quote;
use App\Models\QuoteInvitation;
use App\Utils\Traits\MakesHash;
use Illuminate\Http\Request;
/**
* QuoteRepository
*/
class QuoteRepository extends BaseRepository
{
use MakesHash;
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);
$quote->save();
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;
if (array_key_exists('key', $invitation)) {
$inv = QuoteInvitation::whereKey($invitation['key'])->first();
}
if (!$inv) {
$new_invitation = QuoteInvitationFactory::create($quote->company_id, $quote->user_id);
$new_invitation->fill($invitation);
$new_invitation->quote_id = $quote->id;
$new_invitation->client_contact_id = $this->decodePrimaryKey($invitation['client_contact_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();
$quote->save();
$finished_amount = $quote->amount;
$quote = ApplyQuoteNumber::dispatchNow($quote, $quote->client->getMergedSettings(), $quote->company);
return $quote->fresh();
}
}