| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\App\Http\Controllers; |
| 4 |
|
| 5 |
use FluentCart\App\Models\TaxClass; |
| 6 |
use FluentCart\Framework\Support\Arr; |
| 7 |
use FluentCart\Framework\Http\Request\Request; |
| 8 |
use FluentCart\App\Http\Requests\TaxClassRequest; |
| 9 |
|
| 10 |
class TaxClassController extends Controller |
| 11 |
{ |
| 12 |
|
| 13 |
public function index(Request $request) |
| 14 |
{ |
| 15 |
$taxClasses = TaxClass::query()->get(); |
| 16 |
|
| 17 |
$taxClasses = $taxClasses->sort(function ($a, $b) { |
| 18 |
$aPriority = (int) Arr::get($a->meta, 'priority', 0); |
| 19 |
$bPriority = (int) Arr::get($b->meta, 'priority', 0); |
| 20 |
if ($aPriority === $bPriority) { |
| 21 |
return $b->id <=> $a->id; // newer first when priority equal |
| 22 |
} |
| 23 |
return $bPriority <=> $aPriority; |
| 24 |
})->values(); |
| 25 |
|
| 26 |
return $this->sendSuccess([ |
| 27 |
'tax_classes' => $taxClasses |
| 28 |
]); |
| 29 |
} |
| 30 |
|
| 31 |
public function store(TaxClassRequest $request) |
| 32 |
{ |
| 33 |
|
| 34 |
$data = $request->getSafe($request->sanitize()); |
| 35 |
|
| 36 |
$taxClassData = [ |
| 37 |
'title' => Arr::get($data, 'title'), |
| 38 |
'meta' => [ |
| 39 |
'priority' => Arr::get($data, 'priority', 0), |
| 40 |
] |
| 41 |
]; |
| 42 |
|
| 43 |
$taxClass = TaxClass::create($taxClassData); |
| 44 |
|
| 45 |
if (is_wp_error($taxClass)) { |
| 46 |
return $this->sendError([ |
| 47 |
'message' => $taxClass->get_error_message() |
| 48 |
]); |
| 49 |
} |
| 50 |
|
| 51 |
return $this->sendSuccess([ |
| 52 |
'message' => __('Tax profile has been created successfully', 'fluent-cart') |
| 53 |
]); |
| 54 |
|
| 55 |
} |
| 56 |
|
| 57 |
public function checkAndCreateInitialTaxClasses() |
| 58 |
{ |
| 59 |
if (get_option('fluent_cart_has_tax_configure', false)) { |
| 60 |
return; |
| 61 |
} |
| 62 |
|
| 63 |
TaxClass::query()->firstOrCreate( |
| 64 |
['slug' => 'standard'], |
| 65 |
[ |
| 66 |
'title' => __('Standard', 'fluent-cart'), |
| 67 |
] |
| 68 |
); |
| 69 |
|
| 70 |
update_option('fluent_cart_has_tax_configure', true); |
| 71 |
} |
| 72 |
|
| 73 |
public function update(TaxClassRequest $request, $id) |
| 74 |
{ |
| 75 |
$data = $request->getSafe($request->sanitize()); |
| 76 |
$taxClass = TaxClass::query()->findOrFail($id); |
| 77 |
|
| 78 |
$taxClassData = [ |
| 79 |
'title' => Arr::get($data, 'title'), |
| 80 |
'meta' => [ |
| 81 |
'priority' => Arr::get($data, 'priority', 0), |
| 82 |
] |
| 83 |
]; |
| 84 |
|
| 85 |
$isUpdated = $taxClass->update($taxClassData); |
| 86 |
|
| 87 |
if (!$isUpdated) { |
| 88 |
return $this->sendError([ |
| 89 |
'message' => __('Failed to update tax profile', 'fluent-cart') |
| 90 |
]); |
| 91 |
} |
| 92 |
|
| 93 |
return $this->sendSuccess([ |
| 94 |
'message' => __('Tax profile has been updated successfully', 'fluent-cart') |
| 95 |
]); |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
public function delete(Request $request, $id) |
| 101 |
{ |
| 102 |
$taxClass = TaxClass::query()->findOrFail($id); |
| 103 |
|
| 104 |
if ($taxClass->slug === 'standard') { |
| 105 |
return $this->sendError([ |
| 106 |
'message' => __('Cannot delete the Standard tax class', 'fluent-cart') |
| 107 |
], 423); |
| 108 |
} |
| 109 |
|
| 110 |
$isDeleted = $taxClass->delete(); |
| 111 |
|
| 112 |
if (!$isDeleted) { |
| 113 |
return $this->sendError([ |
| 114 |
'message' => __('Failed to delete tax profile', 'fluent-cart') |
| 115 |
]); |
| 116 |
} |
| 117 |
|
| 118 |
return $this->sendSuccess([ |
| 119 |
'message' => __('Tax profile has been deleted successfully', 'fluent-cart') |
| 120 |
]); |
| 121 |
} |
| 122 |
|
| 123 |
} |
| 124 |
|