mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-07-09 03:14:30 -04:00
Subscriptions
This commit is contained in:
parent
f067290462
commit
2c83abe432
@ -43,6 +43,7 @@ class Subscription extends BaseModel
|
||||
'webhook_configuration',
|
||||
'currency_id',
|
||||
'group_id',
|
||||
'price',
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
|
@ -13,16 +13,86 @@
|
||||
namespace App\Repositories;
|
||||
|
||||
|
||||
use App\Factory\InvoiceFactory;
|
||||
use App\Models\Client;
|
||||
use App\Models\ClientContact;
|
||||
use App\Models\Invoice;
|
||||
use App\Models\InvoiceInvitation;
|
||||
use App\Models\Subscription;
|
||||
use App\Utils\Traits\CleanLineItems;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class SubscriptionRepository extends BaseRepository
|
||||
{
|
||||
use CleanLineItems;
|
||||
|
||||
public function save($data, Subscription $subscription): ?Subscription
|
||||
{
|
||||
$subscription
|
||||
->fill($data)
|
||||
->save();
|
||||
$subscription->fill($data);
|
||||
|
||||
$subscription->price = $this->calculatePrice($subscription);
|
||||
|
||||
$subscription->save();
|
||||
|
||||
return $subscription;
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -38,10 +38,12 @@ class SubscriptionTransformer extends EntityTransformer
|
||||
return [
|
||||
'id' => $this->encodePrimaryKey($subscription->id),
|
||||
'user_id' => $this->encodePrimaryKey($subscription->user_id),
|
||||
'product_id' => $this->encodePrimaryKey($subscription->product_id),
|
||||
'group_id' => $this->encodePrimaryKey($subscription->group_id),
|
||||
'product_ids' => $subscription->product_ids,
|
||||
'recurring_product_ids' => $subscription->recurring_product_ids,
|
||||
'assigned_user_id' => $this->encodePrimaryKey($subscription->assigned_user_id),
|
||||
'company_id' => $this->encodePrimaryKey($subscription->company_id),
|
||||
'is_recurring' => (bool)$subscription->is_recurring,
|
||||
'price' => (float) $subscription->price,
|
||||
'frequency_id' => (string)$subscription->frequency_id,
|
||||
'auto_bill' => (string)$subscription->auto_bill,
|
||||
'promo_code' => (string)$subscription->promo_code,
|
||||
|
@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
class AddPriceColumnToSubscriptionsTable extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
Schema::table('subscriptions', function (Blueprint $table) {
|
||||
$table->decimal('price', 20, 6)->default(0);
|
||||
$table->decimal('promo_price', 20, 6)->default(0);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
Schema::table('subscriptions', function (Blueprint $table) {
|
||||
//
|
||||
});
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user