2020-01-03 10:34:10 +01:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com)
|
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2020-01-07 01:13:47 +01:00
|
|
|
* @copyright Copyright (c) 2020. Invoice Ninja LLC (https://invoiceninja.com)
|
2020-01-03 10:34:10 +01:00
|
|
|
*
|
|
|
|
* @license https://opensource.org/licenses/AAL
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Repositories;
|
|
|
|
|
2020-02-25 09:33:53 +01:00
|
|
|
use App\Factory\CreditInvitationFactory;
|
|
|
|
use App\Models\ClientContact;
|
2020-01-03 10:34:10 +01:00
|
|
|
use App\Models\Credit;
|
2020-01-07 01:13:47 +01:00
|
|
|
use App\Models\CreditInvitation;
|
|
|
|
use App\Utils\Traits\MakesHash;
|
2020-01-03 10:34:10 +01:00
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* CreditRepository
|
|
|
|
*/
|
|
|
|
class CreditRepository extends BaseRepository
|
|
|
|
{
|
2020-01-07 01:13:47 +01:00
|
|
|
use MakesHash;
|
|
|
|
|
2020-01-03 10:34:10 +01:00
|
|
|
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
|
|
|
|
*/
|
2020-02-25 09:33:53 +01:00
|
|
|
public function save(array $data, Credit $credit) : ?Credit
|
2020-01-03 10:34:10 +01:00
|
|
|
{
|
2020-02-26 04:26:07 +01:00
|
|
|
|
2020-01-07 01:13:47 +01:00
|
|
|
$credit->fill($data);
|
2020-02-25 09:33:53 +01:00
|
|
|
|
2020-01-07 01:13:47 +01:00
|
|
|
$credit->save();
|
|
|
|
|
2020-02-24 11:15:30 +01:00
|
|
|
if(!$credit->number)
|
|
|
|
$credit->number = $credit->client->getNextCreditNumber($credit->client);
|
2020-01-07 01:13:47 +01:00
|
|
|
|
2020-02-25 09:33:53 +01:00
|
|
|
if (isset($data['client_contacts'])) {
|
|
|
|
foreach ($data['client_contacts'] as $contact) {
|
|
|
|
if ($contact['send_email'] == 1 && is_string($contact['id'])) {
|
|
|
|
$client_contact = ClientContact::find($this->decodePrimaryKey($contact['id']));
|
|
|
|
$client_contact->send_email = true;
|
|
|
|
$client_contact->save();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-01-07 01:13:47 +01:00
|
|
|
if (isset($data['invitations'])) {
|
|
|
|
$invitations = collect($data['invitations']);
|
|
|
|
|
2020-02-25 09:33:53 +01:00
|
|
|
/* Get array of Keys which have been removed from the invitations array and soft delete each invitation */
|
|
|
|
$credit->invitations->pluck('key')->diff($invitations->pluck('key'))->each(function ($invitation) {
|
|
|
|
|
|
|
|
$invite = $this->getInvitationByKey($invitation);
|
2020-01-07 01:13:47 +01:00
|
|
|
|
2020-02-25 09:33:53 +01:00
|
|
|
if($invite)
|
|
|
|
$invite->forceDelete();
|
|
|
|
|
|
|
|
});
|
2020-01-07 01:13:47 +01:00
|
|
|
|
|
|
|
foreach ($data['invitations'] as $invitation) {
|
2020-02-25 09:33:53 +01:00
|
|
|
$inv = false;
|
2020-01-07 01:13:47 +01:00
|
|
|
|
|
|
|
if (array_key_exists('key', $invitation)) {
|
2020-02-25 09:33:53 +01:00
|
|
|
$inv = $this->getInvitationByKey($invitation['key']);
|
2020-01-07 01:13:47 +01:00
|
|
|
}
|
|
|
|
|
2020-02-25 09:33:53 +01:00
|
|
|
if (!$inv) {
|
|
|
|
|
|
|
|
if (isset($invitation['id'])) {
|
|
|
|
unset($invitation['id']);
|
|
|
|
}
|
2020-01-07 01:13:47 +01:00
|
|
|
|
2020-02-25 09:33:53 +01:00
|
|
|
$new_invitation = CreditInvitationFactory::create($credit->company_id, $credit->user_id);
|
2020-01-07 01:13:47 +01:00
|
|
|
$new_invitation->fill($invitation);
|
|
|
|
$new_invitation->credit_id = $credit->id;
|
2020-02-25 09:33:53 +01:00
|
|
|
$new_invitation->client_contact_id = $invitation['client_contact_id'];
|
2020-01-07 01:13:47 +01:00
|
|
|
$new_invitation->save();
|
2020-02-25 09:33:53 +01:00
|
|
|
|
2020-01-07 01:13:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-25 09:33:53 +01:00
|
|
|
$credit->load('invitations');
|
|
|
|
|
|
|
|
/* If no invitations have been created, this is our fail safe to maintain state*/
|
|
|
|
if ($credit->invitations->count() == 0) {
|
|
|
|
$credit->service()->createInvitations();
|
|
|
|
}
|
2020-01-07 01:13:47 +01:00
|
|
|
/**
|
|
|
|
* Perform calculations on the
|
|
|
|
* credit note
|
|
|
|
*/
|
|
|
|
|
2020-02-19 21:44:12 +01:00
|
|
|
$credit = $credit->calc()->getCredit();
|
2020-01-07 01:13:47 +01:00
|
|
|
|
|
|
|
$credit->save();
|
|
|
|
|
2020-02-25 09:33:53 +01:00
|
|
|
return $credit->fresh();
|
|
|
|
|
|
|
|
}
|
2020-01-07 01:13:47 +01:00
|
|
|
|
2020-02-25 09:33:53 +01:00
|
|
|
public function getInvitationByKey($key) :?CreditInvitation
|
|
|
|
{
|
|
|
|
return CreditInvitation::whereRaw("BINARY `key`= ?", [$key])->first();
|
2020-01-03 10:34:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|