diff --git a/app/Http/Controllers/BaseController.php b/app/Http/Controllers/BaseController.php index 4fa44c019a..19b5c02d6f 100644 --- a/app/Http/Controllers/BaseController.php +++ b/app/Http/Controllers/BaseController.php @@ -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($user) { + $query->where('updated_at', '>=', $updated_at); + + if(!$user->isAdmin()) + $query->where('subscriptions.user_id', $user->id); + } ] ); diff --git a/app/Repositories/PaymentRepository.php b/app/Repositories/PaymentRepository.php index 1bd177c7fd..95e3c4ba4c 100644 --- a/app/Repositories/PaymentRepository.php +++ b/app/Repositories/PaymentRepository.php @@ -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(); diff --git a/app/Repositories/RecurringInvoiceRepository.php b/app/Repositories/RecurringInvoiceRepository.php index b74560bc1b..96b37281be 100644 --- a/app/Repositories/RecurringInvoiceRepository.php +++ b/app/Repositories/RecurringInvoiceRepository.php @@ -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; } diff --git a/app/Repositories/SubscriptionRepository.php b/app/Repositories/SubscriptionRepository.php index 32ce6a931b..466857f4a2 100644 --- a/app/Repositories/SubscriptionRepository.php +++ b/app/Repositories/SubscriptionRepository.php @@ -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); } diff --git a/app/Services/Invoice/InvoiceService.php b/app/Services/Invoice/InvoiceService.php index 83298578b3..72e47ceecd 100644 --- a/app/Services/Invoice/InvoiceService.php +++ b/app/Services/Invoice/InvoiceService.php @@ -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; } diff --git a/app/Services/Subscription/SubscriptionService.php b/app/Services/Subscription/SubscriptionService.php index f18fa4088e..e82067ead3 100644 --- a/app/Services/Subscription/SubscriptionService.php +++ b/app/Services/Subscription/SubscriptionService.php @@ -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,23 @@ 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(); + $invoice = RecurringInvoiceFactory::create($this->subscription->company_id, $this->subscription->user_id); + $invoice->line_items = $subscription_repo->generateLineItems($this->subscription, true); + $invoice->subscription_id = $this->subscription->id; + $invoice->frequency_id = $this->subscription->frequency_id; + $invoice->date = now()->addSeconds($this->subscription->trial_duration)->addDays(1); - // $this->client_subscription = $cs; + if(strlen($data['coupon']) >=1 && ($data['coupon'] == $this->subscription->promo_code) && $this->subscription->promo_discount > 0) + { + $invoice->discount = $this->subscription->promo_discount; + $invoice->is_amount_discount = $this->subscription->is_amount_discount; + } + + $recurring_invoice = $recurring_invoice_repo->save($data, $invoice); //execute any webhooks $this->triggerWebhook(); @@ -198,7 +205,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(); } diff --git a/app/Transformers/CompanyTransformer.php b/app/Transformers/CompanyTransformer.php index 5fd5857f59..2541dfd0f9 100644 --- a/app/Transformers/CompanyTransformer.php +++ b/app/Transformers/CompanyTransformer.php @@ -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); } } diff --git a/app/Transformers/SubscriptionTransformer.php b/app/Transformers/SubscriptionTransformer.php index da8b892d32..70f7eb5cfd 100644 --- a/app/Transformers/SubscriptionTransformer.php +++ b/app/Transformers/SubscriptionTransformer.php @@ -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, diff --git a/tests/Unit/RecurringDateTest.php b/tests/Unit/RecurringDateTest.php new file mode 100644 index 0000000000..db393f928b --- /dev/null +++ b/tests/Unit/RecurringDateTest.php @@ -0,0 +1,44 @@ +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'); + } +} \ No newline at end of file