1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-06 11:12:43 +01:00
invoiceninja/app/Repositories/SubscriptionRepository.php

140 lines
4.2 KiB
PHP
Raw Normal View History

<?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;
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;
use App\Models\Subscription;
2021-03-26 21:28:56 +01:00
use App\Utils\Traits\CleanLineItems;
use Illuminate\Support\Facades\DB;
class SubscriptionRepository extends BaseRepository
{
2021-03-26 21:28:56 +01:00
use CleanLineItems;
public function save($data, Subscription $subscription): ?Subscription
{
2021-03-26 21:28:56 +01:00
$subscription->fill($data);
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();
return $subscription;
}
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
{
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);
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([
2021-03-27 04:22:19 +01:00
'user_id' => $subscription->user_id,
'company_id' => $subscription->company_id,
2021-03-26 21:28:56 +01:00
'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);
2021-03-27 04:22:19 +01:00
2021-03-26 21:28:56 +01:00
$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;
}
2021-03-29 04:14:55 +02:00
public function generateLineItems($subscription, $is_recurring = false)
2021-03-26 21:28:56 +01:00
{
$line_items = [];
2021-03-29 04:14:55 +02:00
if(!$is_recurring)
{
2021-03-29 04:14:55 +02:00
foreach($subscription->service()->products() as $product)
{
$line_items[] = (array)$this->makeLineItem($product);
}
}
2021-03-29 04:14:55 +02:00
2021-03-31 02:20:41 +02:00
foreach($subscription->service()->recurring_products() as $product)
{
2021-03-27 04:22:19 +01:00
$line_items[] = (array)$this->makeLineItem($product);
}
2021-03-26 21:28:56 +01:00
$line_items = $this->cleanItems($line_items);
return $line_items;
2021-03-26 21:28:56 +01:00
}
private function makeLineItem($product)
{
$item = new InvoiceItem;
$item->quantity = $product->quantity;
$item->product_key = $product->product_key;
$item->notes = $product->notes;
$item->cost = $product->price;
$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;
}
}