1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 21:22:58 +01:00

Merge pull request #5273 from turbo124/v5-develop

Subscriptions
This commit is contained in:
David Bomba 2021-03-29 19:43:39 +11:00 committed by GitHub
commit 0a655c6b6b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 118 additions and 44 deletions

View File

@ -12,6 +12,7 @@
namespace App\Events\Credit;
use App\Models\Company;
use App\Models\CreditInvitation;
use Illuminate\Queue\SerializesModels;
/**

View File

@ -94,7 +94,9 @@ class Handler extends ExceptionHandler
}
}
parent::report($exception);
if(config('ninja.expanded_logging'))
parent::report($exception);
}
private function validException($exception)
@ -105,7 +107,7 @@ class Handler extends ExceptionHandler
if (strpos($exception->getMessage(), 'Permission denied') !== false)
return false;
if (strpos($exception->getMessage(), 'flock()') !== false)
if (strpos($exception->getMessage(), 'flock') !== false)
return false;
if (strpos($exception->getMessage(), 'expects parameter 1 to be resource') !== false)
@ -114,6 +116,8 @@ class Handler extends ExceptionHandler
if (strpos($exception->getMessage(), 'fwrite()') !== false)
return false;
if(strpos($exception->getMessage(), 'LockableFile') !== false)
return false;
return true;
}

View File

@ -92,6 +92,7 @@ class BaseController extends Controller
'company.quotes.invitations.company',
'company.quotes.documents',
'company.tasks.documents',
'company.subcsriptions',
'company.tax_rates',
'company.tokens_hashed',
'company.vendors.contacts.company',
@ -340,6 +341,13 @@ class BaseController extends Controller
if(!$user->isAdmin())
$query->where('activities.user_id', $user->id);
},
'company.subscriptions'=> function ($query) use($updated_at, $user) {
$query->where('updated_at', '>=', $updated_at);
if(!$user->isAdmin())
$query->where('subscriptions.user_id', $user->id);
}
]
);

View File

@ -13,6 +13,7 @@ namespace App\Http\Requests\Subscription;
use App\Http\Requests\Request;
use App\Models\Subscription;
use Illuminate\Validation\Rule;
class StoreSubscriptionRequest extends Request
{
@ -34,10 +35,8 @@ class StoreSubscriptionRequest extends Request
public function rules()
{
return [
'user_id' => ['sometimes'],
'product_id' => ['sometimes'],
'assigned_user_id' => ['sometimes'],
'company_id' => ['sometimes'],
'is_recurring' => ['sometimes'],
'frequency_id' => ['sometimes'],
'auto_bill' => ['sometimes'],
@ -55,6 +54,7 @@ class StoreSubscriptionRequest extends Request
'plan_map' => ['sometimes'],
'refund_period' => ['sometimes'],
'webhook_configuration' => ['sometimes'],
'name' => Rule::unique('subscriptions')->where('company_id', auth()->user()->company()->id)
];
}
}

View File

@ -21,10 +21,8 @@ class Subscription extends BaseModel
use HasFactory, SoftDeletes;
protected $fillable = [
'user_id',
'product_ids',
'recurring_product_ids',
'company_id',
'frequency_id',
'auto_bill',
'promo_code',
@ -44,6 +42,7 @@ class Subscription extends BaseModel
'currency_id',
'group_id',
'price',
'name',
];
protected $casts = [

View File

@ -242,7 +242,7 @@ class BaseDriver extends AbstractPaymentDriver
event(new PaymentWasCreated($payment, $payment->company, Ninja::eventVars()));
if (property_exists($this->payment_hash->data, 'billing_context')) {
$billing_subscription = \App\Models\Subscription::find($this->payment_hash->data->billing_context->billing_subscription_id);
$billing_subscription = \App\Models\Subscription::find($this->payment_hash->data->billing_context->subscription_id);
(new SubscriptionService($billing_subscription))->completePurchase($this->payment_hash);
}

View File

@ -182,7 +182,6 @@ class PaymentRepository extends BaseRepository {
$company_currency = $client->company->settings->currency_id;
if ($company_currency != $client_currency) {
$currency = $client->currency();
$exchange_rate = new CurrencyApi();

View File

@ -24,18 +24,6 @@ class RecurringInvoiceRepository extends BaseRepository
{
$invoice = $this->alternativeSave($data, $invoice);
// $invoice->fill($data);
// $invoice->save();
// $invoice_calc = new InvoiceSum($invoice);
// $invoice->service()
// ->applyNumber()
// ->createInvitations()
// ->save();
// $invoice = $invoice_calc->build()->getRecurringInvoice();
return $invoice;
}

View File

@ -93,17 +93,20 @@ class SubscriptionRepository extends BaseRepository
return $data;
}
public function generateLineItems($subscription)
public function generateLineItems($subscription, $is_recurring = false)
{
$line_items = [];
foreach($subscription->service()->products() as $product)
if(!$is_recurring)
{
$line_items[] = (array)$this->makeLineItem($product);
foreach($subscription->service()->products() as $product)
{
$line_items[] = (array)$this->makeLineItem($product);
}
}
foreach($subscription->service()->recurring_products() as $product)
foreach($subscription->service()->recurringProducts() as $product)
{
$line_items[] = (array)$this->makeLineItem($product);
}

View File

@ -63,11 +63,22 @@ class InvoiceService
return $this;
}
/**
* Sets the exchange rate on the invoice if the client currency
* is different to the company currency.
*/
public function setExchangeRate()
{
$exchange_rate = new CurrencyApi();
// $payment->exchange_rate = $exchange_rate->exchangeRate($client_currency, $company_currency, Carbon::parse($payment->date));
$client_currency = $this->invoice->client->getSetting('currency_id');
$company_currency = $this->invoice->company->settings->currency_id;
if ($company_currency != $client_currency) {
$exchange_rate = new CurrencyApi();
$this->invoice->exchange_rate = $exchange_rate->exchangeRate($client_currency, $company_currency, now());
}
return $this;
}
@ -140,6 +151,8 @@ class InvoiceService
{
$this->invoice = (new MarkSent($this->invoice->client, $this->invoice))->run();
$this->setExchangeRate();
return $this;
}

View File

@ -14,6 +14,7 @@ namespace App\Services\Subscription;
use App\DataMapper\InvoiceItem;
use App\Factory\InvoiceFactory;
use App\Factory\InvoiceToRecurringInvoiceFactory;
use App\Factory\RecurringInvoiceFactory;
use App\Jobs\Util\SystemLogger;
use App\Models\ClientContact;
use App\Models\ClientSubscription;
@ -23,6 +24,7 @@ use App\Models\Product;
use App\Models\Subscription;
use App\Models\SystemLog;
use App\Repositories\InvoiceRepository;
use App\Repositories\RecurringInvoiceRepository;
use App\Repositories\SubscriptionRepository;
use App\Utils\Traits\CleanLineItems;
use App\Utils\Traits\MakesHash;
@ -69,18 +71,30 @@ class SubscriptionService
if(!$this->subscription->trial_enabled)
return new \Exception("Trials are disabled for this product");
// $contact = ClientContact::with('client')->find($data['contact_id']);
//create recurring invoice with start date = trial_duration + 1 day
$recurring_invoice_repo = new RecurringInvoiceRepository();
$subscription_repo = new SubscriptionRepository();
// $cs = new ClientSubscription();
// $cs->subscription_id = $this->subscription->id;
// $cs->company_id = $this->subscription->company_id;
// $cs->trial_started = time();
// $cs->trial_ends = time() + $this->subscription->trial_duration;
// $cs->quantity = $data['quantity'];
// $cs->client_id = $contact->client->id;
// $cs->save();
$recurring_invoice = RecurringInvoiceFactory::create($this->subscription->company_id, $this->subscription->user_id);
$recurring_invoice->line_items = $subscription_repo->generateLineItems($this->subscription, true);
$recurring_invoice->subscription_id = $this->subscription->id;
$recurring_invoice->frequency_id = $this->subscription->frequency_id;
$recurring_invoice->date = now();
$recurring_invoice->next_send_date = now()->addSeconds($this->subscription->trial_duration)->addDays(1);
$recurring_invoice->remaining_cycles = -1;
// $this->client_subscription = $cs;
if(strlen($data['coupon']) >=1 && ($data['coupon'] == $this->subscription->promo_code) && $this->subscription->promo_discount > 0)
{
$recurring_invoice->discount = $this->subscription->promo_discount;
$recurring_invoice->is_amount_discount = $this->subscription->is_amount_discount;
}
$recurring_invoice = $recurring_invoice_repo->save($data, $invoice);
/* Start the recurring service */
$recurring_invoice->service()
->start()
->save();
//execute any webhooks
$this->triggerWebhook();
@ -99,11 +113,12 @@ class SubscriptionService
$invoice = InvoiceFactory::create($this->subscription->company_id, $this->subscription->user_id);
$invoice->line_items = $subscription_repo->generateLineItems($this->subscription);
$invoice->subscription_id = $this->subscription->id;
if(strlen($data['coupon']) >=1 && ($data['coupon'] == $this->subscription->promo_code) && $this->subscription->promo_discount > 0)
{
$invoice->discount = $subscription->promo_discount;
$invoice->is_amount_discount = $subscription->is_amount_discount;
$invoice->discount = $this->subscription->promo_discount;
$invoice->is_amount_discount = $this->subscription->is_amount_discount;
}
return $invoice_repo->save($data, $invoice);
@ -197,7 +212,7 @@ class SubscriptionService
return Product::whereIn('id', $this->transformKeys(explode(",", $this->subscription->product_ids)))->get();
}
public function recurring_products()
public function recurringProducts()
{
return Product::whereIn('id', $this->transformKeys(explode(",", $this->subscription->recurring_product_ids)))->get();
}

View File

@ -92,7 +92,7 @@ class CompanyTransformer extends EntityTransformer
'system_logs',
'expense_categories',
'task_statuses',
'billing_subscriptions',
'subscriptions',
];
/**
@ -366,6 +366,6 @@ class CompanyTransformer extends EntityTransformer
{
$transformer = new SubscriptionTransformer($this->serializer);
return $this->includeCollection($company->billing_subscriptions, $transformer, Subscription::class);
return $this->includeCollection($company->subscriptions, $transformer, Subscription::class);
}
}

View File

@ -39,8 +39,8 @@ class SubscriptionTransformer extends EntityTransformer
'id' => $this->encodePrimaryKey($subscription->id),
'user_id' => $this->encodePrimaryKey($subscription->user_id),
'group_id' => $this->encodePrimaryKey($subscription->group_id),
'product_ids' => $subscription->product_ids,
'recurring_product_ids' => $subscription->recurring_product_ids,
'product_ids' => (string)$subscription->product_ids,
'recurring_product_ids' => (string)$subscription->recurring_product_ids,
'assigned_user_id' => $this->encodePrimaryKey($subscription->assigned_user_id),
'company_id' => $this->encodePrimaryKey($subscription->company_id),
'price' => (float) $subscription->price,

View File

@ -0,0 +1,44 @@
<?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 Tests\Unit;
use App\Models\Invoice;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Support\Carbon;
use Tests\MockAccountData;
use Tests\TestCase;
/**
* @test
*/
class RecurringDateTest extends TestCase
{
use DatabaseTransactions;
use MockAccountData;
public function setUp() :void
{
parent::setUp();
//$this->makeTestData();
}
public function testNextDay()
{
$trial = 60*60*24;
$now = Carbon::parse('2021-12-01');
$trial_ends = $now->addSeconds($trial)->addDays(1);
$this->assertequals($trial_ends->format('Y-m-d'), '2021-12-03');
}
}