2021-03-08 15:18:14 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
|
|
|
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
|
|
|
|
*
|
|
|
|
* @license https://opensource.org/licenses/AAL
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Repositories;
|
|
|
|
|
|
|
|
|
2021-03-26 21:28:56 +01:00
|
|
|
use App\Factory\InvoiceFactory;
|
|
|
|
use App\Models\Client;
|
|
|
|
use App\Models\ClientContact;
|
|
|
|
use App\Models\Invoice;
|
|
|
|
use App\Models\InvoiceInvitation;
|
2021-03-25 11:55:59 +01:00
|
|
|
use App\Models\Subscription;
|
2021-03-26 21:28:56 +01:00
|
|
|
use App\Utils\Traits\CleanLineItems;
|
|
|
|
use Illuminate\Support\Facades\DB;
|
2021-03-08 15:18:14 +01:00
|
|
|
|
2021-03-25 11:55:59 +01:00
|
|
|
class SubscriptionRepository extends BaseRepository
|
2021-03-08 15:18:14 +01:00
|
|
|
{
|
2021-03-26 21:28:56 +01:00
|
|
|
use CleanLineItems;
|
|
|
|
|
2021-03-25 11:55:59 +01:00
|
|
|
public function save($data, Subscription $subscription): ?Subscription
|
2021-03-08 15:18:14 +01:00
|
|
|
{
|
2021-03-26 21:28:56 +01:00
|
|
|
$subscription->fill($data);
|
|
|
|
|
|
|
|
$subscription->price = $this->calculatePrice($subscription);
|
|
|
|
|
|
|
|
$subscription->save();
|
2021-03-08 15:18:14 +01:00
|
|
|
|
2021-03-25 11:55:59 +01:00
|
|
|
return $subscription;
|
2021-03-08 15:18:14 +01:00
|
|
|
}
|
2021-03-26 21:28:56 +01:00
|
|
|
|
|
|
|
private function calculatePrices($subscription) :array
|
|
|
|
{
|
|
|
|
|
|
|
|
DB::beginTransaction();
|
|
|
|
|
|
|
|
$data = [];
|
|
|
|
|
|
|
|
$client = Client::factory()->create([
|
|
|
|
'user_id' => $subscription->user_id,
|
|
|
|
'company_id' => $subscription->company_id,
|
|
|
|
'group_settings_id' => $subscription->group_id,
|
|
|
|
'country_id' => $subscription->company->settings->country_id,
|
|
|
|
]);
|
|
|
|
|
|
|
|
$contact = ClientContact::factory()->create([
|
|
|
|
'user_id' => $subscription->user_id,
|
|
|
|
'company_id' => $subscription->company_id,
|
|
|
|
'client_id' => $client->id,
|
|
|
|
'is_primary' => 1,
|
|
|
|
'send_email' => true,
|
|
|
|
]);
|
|
|
|
|
|
|
|
$invoice = InvoiceFactory::create($subscription->company_id, $subscription->user_id);
|
|
|
|
|
|
|
|
$invitation = InvoiceInvitation::factory()->create([
|
|
|
|
'user_id' => $subscription->user_id,
|
|
|
|
'company_id' => $subscription->company_id,
|
|
|
|
'invoice_id' => $invoice->id,
|
|
|
|
'client_contact_id' => $contact->id,
|
|
|
|
]);
|
|
|
|
|
|
|
|
$invoice->setRelation('invitations', $invitation);
|
|
|
|
$invoice->setRelation('client', $client);
|
|
|
|
$invoice->setRelation('company', $subscription->company);
|
|
|
|
$invoice->load('client');
|
|
|
|
|
|
|
|
$invoice->line_items = $this->generateLineItems($subscription);
|
|
|
|
$data['price'] = $invoice->calc()->getTotal();
|
|
|
|
|
|
|
|
$invoice->discount = $subscription->promo_discount;
|
|
|
|
$invoice->is_amount_discount = $subscription->is_amount_discount;
|
|
|
|
|
|
|
|
$data['promo_price'] = $invoice->calc()->getTotal();
|
|
|
|
|
|
|
|
DB::rollBack();
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function generateLineItems($subscription)
|
|
|
|
{
|
|
|
|
|
|
|
|
$line_items = [];
|
|
|
|
|
|
|
|
$line_items = $this->cleanItems($line_items);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-03-25 11:55:59 +01:00
|
|
|
}
|