1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-21 08:51:34 +02:00
invoiceninja/app/Repositories/CreditRepository.php
David Bomba 3d31f810c0
Set Invitations as a default include for invoices (#3362)
* Working on importing company gateways

* Fix for companyuser settings object

* Migrate client_gateway_tokens

* Working on Notificaitons

* Working on notifications

* Failsafe for user-company

* unlink files

* Set DB for jobs

* Always have a fallback for company_id

* Fixes for user model

* Formatting for MultiDB

* Working on Company Ledger Tests

* Fixes for contact request

* Set Invitations as a default include for invoices
2020-02-24 21:15:30 +11:00

98 lines
2.7 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\Models\Credit;
use App\Models\CreditInvitation;
use App\Utils\Traits\MakesHash;
use Illuminate\Http\Request;
/**
* CreditRepository
*/
class CreditRepository extends BaseRepository
{
use MakesHash;
public function __construct()
{
}
/**
* Gets the class name.
*
* @return string The class name.
*/
public function getClassName()
{
return Credit::class;
}
/**
* Saves the client and its contacts
*
* @param array $data The data
* @param \App\Models\Company $client The Company
*
* @return Credit|\App\Models\Credit|null Credit Object
*/
public function save(array $data, Credit $credit, $invoice = null) : ?Credit
{
$credit->fill($data);
$credit->save();
if(!$credit->number)
$credit->number = $credit->client->getNextCreditNumber($credit->client);
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($credit->invitations->pluck('key'))->diff($invitations->pluck('key'))->each(function ($invitation) {
CreditInvitation::destroy($invitation);
});
foreach ($data['invitations'] as $invitation) {
$cred = false;
if (array_key_exists('key', $invitation)) {
$cred = CreditInvitation::whereKey($invitation['key'])->first();
}
if (!$cred) {
//$invitation['client_contact_id'] = $this->decodePrimaryKey($invitation['client_contact_id']);
$new_invitation = CreditInvitationFactory::create($invoice->company_id, $invoice->user_id);
$new_invitation->fill($invitation);
$new_invitation->credit_id = $credit->id;
$new_invitation->client_contact_id = $this->decodePrimaryKey($invitation['client_contact_id']);
$new_invitation->save();
}
}
}
/**
* Perform calculations on the
* credit note
*/
$credit = $credit->calc()->getCredit();
$credit->save();
return $credit;
}
}