Skip to content

Commit 3e48a8f

Browse files
committed
[Fix] Currency Issue
1 parent 1b5c6b7 commit 3e48a8f

22 files changed

+568
-588
lines changed

database/migrations/2022_07_24_092101_create_plans_table.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ public function up()
2424
$table->string('slug')->unique();
2525
$table->longText('description')->nullable();
2626
$table->boolean('is_active')->default(true);
27+
$table->string('default_interval')->default('month');
2728
$table->string('interval')->default('month');
2829
$table->unsignedInteger('interval_count')->default(1);
29-
$table->string('currency', 3)->nullable();
3030
$table->double('price', 12, 2)->default(0.00);
3131
$table->unsignedInteger('trial_days')->nullable()->default(0);
3232
$table->timestamps();

database/migrations/2022_07_24_092102_create_coupons_table.php

-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ public function up(): void
2121
$table->string('promotion_code')->unique();
2222
$table->string('promotion_id');
2323
$table->{$this->jsonable()}('applies_to')->nullable();
24-
$table->string('currency')->nullable();
2524
$table->string('duration');
2625
$table->unsignedInteger('duration_in_months')->nullable();
2726
$table->unsignedInteger('max_redemptions')->nullable();

database/migrations/2023_05_01_084503_create_invoices_table.php

-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ public function up()
4949
$table->unsignedBigInteger('quantity')->nullable();
5050
$table->double('price', 15, 2)->nullable()->default(0);
5151
$table->double('total', 15, 2)->nullable()->default(0);
52-
$table->string('currency')->nullable();
5352

5453
$table->foreign('plan_id')->references('id')->on('plans')->nullOnDelete();
5554
$table->foreign('invoice_id')->references('id')->on('subscription_invoices')->cascadeOnUpdate()->cascadeOnDelete();

resources/views/includes/pricing.blade.php

+4-7
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
</div>
66
<div class="ct-plan-title">
77
<h3>{{ $label }}
8-
@if ($monthly_fee > 0)
8+
@if ($price > 0)
99
<span>*</span>
1010
@endif
1111
</h3>
@@ -19,17 +19,14 @@
1919
</li>
2020
@endforeach
2121
</ul>
22-
<div class="ct-plan-amount @if ($monthly_fee <= 0) visibility-hidden @endif">
22+
<div class="ct-plan-amount @if ($price <= 0) visibility-hidden @endif">
2323
<span class="cur_symbol">{{ $cur_symbol }}</span>
24-
<span class="ct-ptablebox-price month">{{ $monthly_fee }}</span>
25-
<span style="display: none" class="ct-ptablebox-price year">{{ $yearly_fee }}</span>
26-
<span class="pac_frequency month">/Per Month</span>
27-
<span style="display: none" class="pac_frequency year">/Per Year</span>
24+
<span class="ct-ptablebox-price month">{{ $price }}</span>
25+
<span class="pac_frequency month">{{ $interval_label }}</span>
2826
</div>
2927
<div class="ct-plan-footer">
3028
<form action="{{ app_url('sign-up') }}" method="get">
3129
<input type="hidden" name="plan" value="{{ $id }}">
32-
<input class="plan-interval" type="hidden" name="interval" value="month">
3330
<button
3431
class="btn btn--md btn--square btn--fill btn--icon-right btn--primary text-center margin_top30 z-index-1">Signup</button>
3532
</form>

resources/views/pdfs/invoice-pos.blade.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,13 @@
7575
</div>
7676
</td>
7777
<td class="text-center">
78-
{{ $item->price() }}
78+
{{ format_amount($item->price, $currency) }}
7979
</td>
8080
<td class="text-center">
8181
{{ $item->quantity }}
8282
</td>
8383
<td class="text-right">
84-
{{ $item->amount() }}
84+
{{ format_amount($item->total, $currency) }}
8585
</td>
8686
</tr>
8787
@endforeach

resources/views/pdfs/invoice-receipt.blade.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,11 @@
6262
<td class="text-center border-top">
6363
{{ $item->quantity }}
6464
</td>
65-
<td class="text-center border-top">
66-
{{ $item->price() }}
65+
<td class="text-center">
66+
{{ format_amount($item->price, $currency) }}
6767
</td>
68-
<td class="text-right border-top">
69-
{{ $item->amount() }}
68+
<td class="text-right">
69+
{{ format_amount($item->total, $currency) }}
7070
</td>
7171
</tr>
7272
@endforeach

resources/views/pdfs/order-pos.blade.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,13 @@
7575
</div>
7676
</td>
7777
<td class="text-center">
78-
{{ format_amount($item->price * 100) }}
78+
{{ format_amount($item->price, $currency) }}
7979
</td>
8080
<td class="text-center">
8181
{{ $item->quantity }}
8282
</td>
8383
<td class="text-right">
84-
{{ format_amount($item->total * 100) }}
84+
{{ format_amount($item->total, $currency) }}
8585
</td>
8686
</tr>
8787
@endforeach

resources/views/pdfs/order-receipt.blade.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,13 @@
6363
</p>
6464
</td>
6565
<td class="text-center">
66-
{{ format_amount($item->price * 100) }}
66+
{{ format_amount($item->price, $currency) }}
6767
</td>
6868
<td class="text-center">
6969
{{ $item->quantity }}
7070
</td>
7171
<td class="text-right">
72-
{{ format_amount($item->total * 100) }}
72+
{{ format_amount($item->total, $currency) }}
7373
</td>
7474
</tr>
7575
@endforeach
+4-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
<div class="container {{ $class }}">
2-
<div class="row">
2+
{{-- <div class="row">
33
<div class="col-xs-12 mb-30">
44
<div id="interval-switch" class="interval-switch">
5-
<span>Monthly</span>
65
<label class="ct-switch">
76
<input type="checkbox">
87
<span class="ct-slider round"></span>
@@ -11,7 +10,7 @@
1110
<span class="badge bg-warning">20% discount</span>
1211
</div>
1312
</div>
14-
</div>
13+
</div> --}}
1514
<div class="row">
1615
@foreach ($plans as $key => $plan)
1716
<div class="col-lg-4 col-md-12 mb-30">
@@ -20,12 +19,12 @@
2019
@endforeach
2120
</div>
2221
</div>
23-
<script>
22+
{{-- <script>
2423
let year = false
2524
$("#interval-switch input").change(() => {
2625
$(".year").toggle(!year);
2726
$(".month").toggle(year);
2827
$(".plan-interval").val(!year ? 'year' : 'month');
2928
year = !year
3029
});
31-
</script>
30+
</script> --}}

src/Models/Coupon.php

-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ class Coupon extends Model
1717
protected $fillable = [
1818
'name',
1919
'promotion_code',
20-
'currency',
2120
'duration',
2221
'duration_in_months',
2322
'max_redemptions',

src/Models/Shop/Order.php

+17-24
Original file line numberDiff line numberDiff line change
@@ -493,41 +493,34 @@ public function restock()
493493
};
494494
}
495495

496-
public function posPdf()
496+
private function toPdfArray(): array
497497
{
498-
return Pdf::loadView('coderstm::pdfs.order-pos', [
498+
return [
499499
'id' => $this->formated_id,
500+
'currency' => $this->currency,
500501
'phone_number' => optional($this->contact)->phone_number,
501502
'customer_name' => optional($this->customer)->name ?? 'NA',
503+
'billing_address' => optional($this->billing_address)->label,
502504
'line_items' => $this->line_items,
503505
'location' => optional($this->location)->address_label,
504-
'sub_total' => format_amount($this->sub_total * 100),
505-
'tax_total' => format_amount($this->tax_total * 100),
506-
'discount_total' => format_amount($this->discount_total * 100),
507-
'grand_total' => format_amount($this->grand_total * 100),
508-
'paid_total' => format_amount($this->paid_total * 100),
509-
'due_amount' => format_amount($this->due_amount * 100),
506+
'sub_total' => format_amount($this->sub_total),
507+
'tax_total' => format_amount($this->tax_total),
508+
'discount_total' => format_amount($this->discount_total),
509+
'grand_total' => format_amount($this->grand_total),
510+
'paid_total' => format_amount($this->paid_total),
511+
'due_amount' => format_amount($this->due_amount),
510512
'created_at' => $this->created_at->format('d-m-Y h:i a'),
511-
])->setPaper([0, 0, 260.00, 600.80]);
513+
];
514+
}
515+
516+
public function posPdf()
517+
{
518+
return Pdf::loadView('coderstm::pdfs.order-pos', $this->toPdfArray())->setPaper([0, 0, 260.00, 600.80]);
512519
}
513520

514521
public function receiptPdf()
515522
{
516-
return Pdf::loadView('coderstm::pdfs.order-receipt', [
517-
'id' => $this->formated_id,
518-
'phone_number' => optional($this->contact)->phone_number,
519-
'customer_name' => optional($this->customer)->name ?? 'NA',
520-
'billing_address' => optional($this->billing_address)->label,
521-
'line_items' => $this->line_items,
522-
'location' => optional($this->location)->address_label,
523-
'sub_total' => format_amount($this->sub_total * 100),
524-
'tax_total' => format_amount($this->tax_total * 100),
525-
'discount_total' => format_amount($this->discount_total * 100),
526-
'grand_total' => format_amount($this->grand_total * 100),
527-
'paid_total' => format_amount($this->paid_total * 100),
528-
'due_amount' => format_amount($this->due_amount * 100),
529-
'created_at' => $this->created_at->format('d-m-Y h:i a'),
530-
]);
523+
return Pdf::loadView('coderstm::pdfs.order-receipt', $this->toPdfArray());
531524
}
532525

533526
public function total()

src/Models/Shop/Product.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -127,11 +127,11 @@ protected function price(): Attribute
127127
return Attribute::make(
128128
get: function () {
129129
if ($this->has_variant) {
130-
$min = format_amount($this->variants_min_price * 100 ?? 0);
131-
$max = format_amount($this->variants_max_price * 100 ?? 0);
130+
$min = format_amount($this->variants_min_price ?? 0);
131+
$max = format_amount($this->variants_max_price ?? 0);
132132
return $min != $max ? "{$min} - {$max}" : $min;
133133
}
134-
return format_amount($this->default_variant_min_price * 100 ?? 0);
134+
return format_amount($this->default_variant_min_price ?? 0);
135135
},
136136
);
137137
}

src/Models/Shop/Product/Variant.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ public function getTitleAttribute()
115115

116116
public function getPriceFormatedAttribute()
117117
{
118-
return format_amount($this->price * 100);
118+
return format_amount($this->price);
119119
}
120120

121121
public function scopeOnlyTrackInventory($query)

src/Models/Subscription.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -781,7 +781,7 @@ public function upcomingInvoice($start = false, $dateFrom = null): InvoiceReposi
781781
'subscription_id' => $this->id,
782782
'due_date' => $start ? $this->dateFrom() : $period->getEndDate(),
783783
'billing_address' => $this->user->address->toArray(),
784-
'currency' => $plan->currency,
784+
'currency' => config('cashier.currency'),
785785
'collect_tax' => true,
786786
'line_items' => $this->generateLineItems($plan, $period),
787787
'discount' => $this->discount(),
@@ -809,7 +809,6 @@ protected function generateLineItems(Plan $plan, $period)
809809
'price' => $plan->price,
810810
'total' => $plan->price,
811811
'quantity' => 1,
812-
'currency' => $plan->currency,
813812
'options' => ['title' => $title]
814813
]
815814
];

src/Models/Subscription/Invoice/LineItem.php

-16
Original file line numberDiff line numberDiff line change
@@ -22,26 +22,10 @@ class LineItem extends Model
2222
'quantity',
2323
'price',
2424
'total',
25-
'currency',
2625
];
2726

2827
public function invoice(): BelongsTo
2928
{
3029
return $this->belongsTo(Invoice::class);
3130
}
32-
33-
public function amount()
34-
{
35-
return $this->formatAmount($this->total);
36-
}
37-
38-
public function price()
39-
{
40-
return $this->formatAmount($this->price);
41-
}
42-
43-
protected function formatAmount($amount)
44-
{
45-
return format_amount($amount, $this->currency);
46-
}
4731
}

src/Models/Subscription/Plan.php

+22-9
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,14 @@ class Plan extends Model
2828
'description',
2929
'note',
3030
'is_active',
31+
'default_interval',
3132
'interval',
3233
'interval_count',
33-
'currency',
3434
'price',
3535
'trial_days',
3636
];
3737

38-
protected $appends = ['feature_lines', 'price_formated'];
38+
protected $appends = ['feature_lines', 'price_formated', 'interval_label'];
3939

4040
protected $casts = [
4141
'is_active' => 'boolean',
@@ -68,6 +68,13 @@ protected function priceFormated(): Attribute
6868
);
6969
}
7070

71+
protected function intervalLabel(): Attribute
72+
{
73+
return Attribute::make(
74+
get: fn () => $this->formatInterval(),
75+
);
76+
}
77+
7178
public function features(): BelongsToMany
7279
{
7380
return $this->belongsToMany(Feature::class, 'plan_features')->withPivot('value');
@@ -126,21 +133,27 @@ public function formatPrice()
126133
return $this->formatAmount($this->price);
127134
}
128135

136+
protected function formatInterval()
137+
{
138+
$interval = $this->interval->value;
139+
140+
if ($this->interval_count > 1) {
141+
return "/ {$this->interval_count} {$interval}s";
142+
} else {
143+
return "/ {$interval}";
144+
}
145+
}
146+
147+
129148
protected function formatAmount($amount)
130149
{
131-
return format_amount($amount, $this->currency);
150+
return format_amount($amount);
132151
}
133152

134153
protected static function booted()
135154
{
136155
parent::booted();
137156

138-
static::creating(function (self $model): void {
139-
if (empty($model->currency)) {
140-
$model->currency = config('cashier.currency');
141-
}
142-
});
143-
144157
static::updated(queueable(function ($model) {
145158
if ($model->wasChanged('is_active') && !$model->is_active) {
146159
foreach ($model->subscriptions()->cursor() as $subscription) {

0 commit comments

Comments
 (0)