2021-03-08 15:18:14 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2022-04-27 05:20:41 +02:00
|
|
|
* @copyright Copyright (c) 2022. Invoice Ninja LLC (https://invoiceninja.com)
|
2021-03-08 15:18:14 +01:00
|
|
|
*
|
2021-06-16 08:58:16 +02:00
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
2021-03-08 15:18:14 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Repositories;
|
|
|
|
|
2021-06-24 13:26:14 +02:00
|
|
|
use App\DataMapper\ClientSettings;
|
2021-03-26 22:39:08 +01:00
|
|
|
use App\DataMapper\InvoiceItem;
|
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;
|
|
|
|
|
2022-07-06 09:12:29 +02:00
|
|
|
public int $quantity = 1;
|
|
|
|
|
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);
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2021-03-27 04:22:19 +01:00
|
|
|
$calculated_prices = $this->calculatePrice($subscription);
|
|
|
|
|
|
|
|
$subscription->price = $calculated_prices['price'];
|
|
|
|
$subscription->promo_price = $calculated_prices['promo_price'];
|
2021-03-26 21:28:56 +01:00
|
|
|
|
|
|
|
$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
|
|
|
|
2021-03-27 04:22:19 +01:00
|
|
|
private function calculatePrice($subscription) :array
|
2021-03-26 21:28:56 +01:00
|
|
|
{
|
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
// DB::beginTransaction();
|
2021-06-24 13:52:02 +02:00
|
|
|
DB::connection(config('database.default'))->beginTransaction();
|
2022-06-21 11:57:17 +02:00
|
|
|
$data = [];
|
2021-03-26 21:28:56 +01:00
|
|
|
|
|
|
|
$client = Client::factory()->create([
|
2022-06-21 11:57:17 +02:00
|
|
|
'user_id' => $subscription->user_id,
|
|
|
|
'company_id' => $subscription->company_id,
|
|
|
|
'group_settings_id' => $subscription->group_id,
|
|
|
|
'country_id' => $subscription->company->settings->country_id,
|
|
|
|
'settings' => ClientSettings::defaults(),
|
|
|
|
]);
|
2021-03-26 21:28:56 +01:00
|
|
|
|
|
|
|
$contact = ClientContact::factory()->create([
|
2022-06-21 11:57:17 +02:00
|
|
|
'user_id' => $subscription->user_id,
|
|
|
|
'company_id' => $subscription->company_id,
|
|
|
|
'client_id' => $client->id,
|
|
|
|
'is_primary' => 1,
|
|
|
|
'send_email' => true,
|
|
|
|
]);
|
2021-03-26 21:28:56 +01:00
|
|
|
|
|
|
|
$invoice = InvoiceFactory::create($subscription->company_id, $subscription->user_id);
|
2021-03-27 04:22:19 +01:00
|
|
|
$invoice->client_id = $client->id;
|
|
|
|
|
|
|
|
$invoice->save();
|
2021-03-26 21:28:56 +01:00
|
|
|
|
|
|
|
$invitation = InvoiceInvitation::factory()->create([
|
2022-06-21 11:57:17 +02:00
|
|
|
'user_id' => $subscription->user_id,
|
|
|
|
'company_id' => $subscription->company_id,
|
|
|
|
'invoice_id' => $invoice->id,
|
|
|
|
'client_contact_id' => $contact->id,
|
2021-03-26 21:28:56 +01:00
|
|
|
]);
|
|
|
|
|
|
|
|
$invoice->setRelation('invitations', $invitation);
|
|
|
|
$invoice->setRelation('client', $client);
|
|
|
|
$invoice->setRelation('company', $subscription->company);
|
|
|
|
$invoice->load('client');
|
|
|
|
$invoice->line_items = $this->generateLineItems($subscription);
|
2021-03-27 04:22:19 +01:00
|
|
|
|
2021-03-26 21:28:56 +01:00
|
|
|
$data['price'] = $invoice->calc()->getTotal();
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2021-03-26 21:28:56 +01:00
|
|
|
$invoice->discount = $subscription->promo_discount;
|
|
|
|
$invoice->is_amount_discount = $subscription->is_amount_discount;
|
|
|
|
|
|
|
|
$data['promo_price'] = $invoice->calc()->getTotal();
|
|
|
|
|
2021-06-24 13:52:02 +02:00
|
|
|
// DB::rollBack();
|
|
|
|
DB::connection(config('database.default'))->rollBack();
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2021-03-26 21:28:56 +01:00
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
2021-06-27 13:55:15 +02:00
|
|
|
public function generateLineItems($subscription, $is_recurring = false, $is_credit = false)
|
2021-03-26 21:28:56 +01:00
|
|
|
{
|
2021-06-27 13:55:15 +02:00
|
|
|
$multiplier = $is_credit ? -1 : 1;
|
2021-03-26 21:28:56 +01:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
$line_items = [];
|
2021-03-26 21:28:56 +01:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if (! $is_recurring) {
|
|
|
|
foreach ($subscription->service()->products() as $product) {
|
|
|
|
$line_items[] = (array) $this->makeLineItem($product, $multiplier);
|
2021-03-29 04:14:55 +02:00
|
|
|
}
|
2021-03-26 22:39:08 +01:00
|
|
|
}
|
2022-06-21 11:57:17 +02:00
|
|
|
|
|
|
|
foreach ($subscription->service()->recurring_products() as $product) {
|
|
|
|
$line_items[] = (array) $this->makeLineItem($product, $multiplier);
|
2021-03-26 22:39:08 +01:00
|
|
|
}
|
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
$line_items = $this->cleanItems($line_items);
|
2021-03-26 21:28:56 +01:00
|
|
|
|
2021-03-26 22:39:08 +01:00
|
|
|
return $line_items;
|
2021-03-26 21:28:56 +01:00
|
|
|
}
|
|
|
|
|
2021-06-27 13:55:15 +02:00
|
|
|
private function makeLineItem($product, $multiplier)
|
2021-03-26 22:39:08 +01:00
|
|
|
{
|
|
|
|
$item = new InvoiceItem;
|
2022-07-06 09:12:29 +02:00
|
|
|
$item->quantity = $this->quantity;
|
2021-03-26 22:39:08 +01:00
|
|
|
$item->product_key = $product->product_key;
|
|
|
|
$item->notes = $product->notes;
|
2022-06-21 11:57:17 +02:00
|
|
|
$item->cost = $product->price * $multiplier;
|
2021-03-26 22:39:08 +01:00
|
|
|
$item->tax_rate1 = $product->tax_rate1 ?: 0;
|
|
|
|
$item->tax_name1 = $product->tax_name1 ?: '';
|
|
|
|
$item->tax_rate2 = $product->tax_rate2 ?: 0;
|
|
|
|
$item->tax_name2 = $product->tax_name2 ?: '';
|
|
|
|
$item->tax_rate3 = $product->tax_rate3 ?: 0;
|
|
|
|
$item->tax_name3 = $product->tax_name3 ?: '';
|
|
|
|
$item->custom_value1 = $product->custom_value1 ?: '';
|
|
|
|
$item->custom_value2 = $product->custom_value2 ?: '';
|
|
|
|
$item->custom_value3 = $product->custom_value3 ?: '';
|
|
|
|
$item->custom_value4 = $product->custom_value4 ?: '';
|
|
|
|
|
|
|
|
return $item;
|
|
|
|
}
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|