feat: Implement order and invoice management, enhance addon features, and remove the 'Type' module.
This commit is contained in:
@@ -16,18 +16,31 @@ class AddonCachedRepository extends BaseCachedRepository implements AddonReposit
|
||||
{
|
||||
return $this->addonRepository->getModel();
|
||||
}
|
||||
public function filter(array $data = [], array $relationships = [], array $withCount = [], array $select = ['*'])
|
||||
{
|
||||
public function filter(
|
||||
array $data = [],
|
||||
array $relationships = [],
|
||||
array $withCount = [],
|
||||
array $select = ['*'],
|
||||
bool $returnQuery = false
|
||||
) {
|
||||
if (
|
||||
$returnQuery ||
|
||||
!empty($data['paginate'])
|
||||
) {
|
||||
return $this->addonRepository
|
||||
->filter($data, $relationships, $withCount, $select, $returnQuery);
|
||||
}
|
||||
|
||||
$cacheKey = $this->getCacheKey(md5(json_encode([
|
||||
'filterItem' => [
|
||||
'data' => $data,
|
||||
'relationships' => $relationships,
|
||||
'select' => $select,
|
||||
'withCount' => $withCount
|
||||
]
|
||||
'filter' => compact('data', 'relationships', 'withCount', 'select')
|
||||
])));
|
||||
return Cache::remember($cacheKey, $this->cacheTime, function () use ($data, $relationships, $select, $withCount) {
|
||||
return $this->addonRepository->filter($data, $relationships, $withCount, $select);
|
||||
});
|
||||
|
||||
return Cache::remember(
|
||||
$cacheKey,
|
||||
$this->cacheTime,
|
||||
fn() =>
|
||||
$this->addonRepository
|
||||
->filter($data, $relationships, $withCount, $select, $returnQuery)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,16 +11,35 @@ class AddonRepository extends BaseRepository implements AddonRepositoryInterface
|
||||
{
|
||||
return Addon::class;
|
||||
}
|
||||
public function filter(array $data = [], array $relationships = [], array $withCount = [], array $select = ['*'])
|
||||
{
|
||||
$model = $this->model->with($relationships)->select($select);
|
||||
|
||||
if (count($withCount) > 0) {
|
||||
$model = $model->withCount($withCount);
|
||||
public function filter(
|
||||
array $data = [],
|
||||
array $relationships = [],
|
||||
array $withCount = [],
|
||||
array $select = ['*'],
|
||||
bool $returnQuery = false
|
||||
) {
|
||||
$query = $this->model
|
||||
->select($select)
|
||||
->with($relationships);
|
||||
|
||||
if (!empty($data['vcenter_id'])) {
|
||||
$query->where('vcenter_id', $data['vcenter_id']);
|
||||
}
|
||||
if(isset($data['page'])) {
|
||||
return $model->paginate(15);
|
||||
|
||||
if (!empty($withCount)) {
|
||||
$query->withCount($withCount);
|
||||
}
|
||||
return $model->get();
|
||||
|
||||
if ($returnQuery) {
|
||||
return $query;
|
||||
}
|
||||
|
||||
if (!empty($data['paginate'])) {
|
||||
return $query->paginate(
|
||||
$data['per_page'] ?? 15
|
||||
);
|
||||
}
|
||||
|
||||
return $query->get();
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,13 @@ namespace App\Repositories\Addon;
|
||||
|
||||
use App\Repositories\BaseRepositoryInterface;
|
||||
|
||||
interface AddonRepositoryInterface extends BaseRepositoryInterface{
|
||||
public function filter(array $data = [],array $relationships = [],array $withCount = [],array $select = ['*']);
|
||||
interface AddonRepositoryInterface extends BaseRepositoryInterface
|
||||
{
|
||||
public function filter(
|
||||
array $data = [],
|
||||
array $relationships = [],
|
||||
array $withCount = [],
|
||||
array $select = ['*'],
|
||||
bool $returnQuery = false
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
<?php
|
||||
|
||||
namespace App\Repositories\Api\Invoice;
|
||||
|
||||
use App\Models\Invoice;
|
||||
use App\Repositories\Api\BaseApiRepository;
|
||||
|
||||
class InvoiceRepository extends BaseApiRepository implements InvoiceRepositoryInterface
|
||||
{
|
||||
public function getModel()
|
||||
{
|
||||
return Invoice::class;
|
||||
}
|
||||
|
||||
/**
|
||||
* Lấy danh sách sản phẩm đang hoạt động
|
||||
*/
|
||||
public function getInvoice(int $limit = 10, $customer = null)
|
||||
{
|
||||
return $this->model->where('customer_id', $customer)->paginate($limit);
|
||||
}
|
||||
|
||||
public function createFromOrder($order)
|
||||
{
|
||||
$invoice = $this->model->create([
|
||||
'customer_id' => $order->customer_id,
|
||||
'date' => now(),
|
||||
'due_date' => now()->addDays(3),
|
||||
'amount' => $order->amount,
|
||||
'status' => 1, // Unpaid
|
||||
]);
|
||||
|
||||
$order->update(['invoice_id' => $invoice->id]);
|
||||
|
||||
$orderItems = $order->orderItems;
|
||||
if ($orderItems->isEmpty()) {
|
||||
$orderItems = $order->orderItems()->get();
|
||||
}
|
||||
|
||||
foreach ($orderItems as $item) {
|
||||
\App\Models\InvoiceItem::create([
|
||||
'invoice_id' => $invoice->id,
|
||||
'customer_id' => $invoice->customer_id,
|
||||
'product_id' => $item->product_id,
|
||||
'description' => optional($item->product)->name ?? 'Order Item',
|
||||
'amount' => $item->price ?? $order->amount,
|
||||
'type_id' => 1,
|
||||
'billing' => $item->billing_cycle ?? 'monthly',
|
||||
]);
|
||||
}
|
||||
|
||||
return $invoice;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
<?php
|
||||
|
||||
namespace App\Repositories\Api\Invoice;
|
||||
|
||||
use App\Repositories\BaseRepositoryInterface;
|
||||
|
||||
interface InvoiceRepositoryInterface extends BaseRepositoryInterface
|
||||
{
|
||||
public function getInvoice(int $limit = 10, $customer = null);
|
||||
|
||||
public function createFromOrder($order);
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
namespace App\Repositories\Api\Order;
|
||||
|
||||
use App\Models\Order;
|
||||
use App\Repositories\Api\BaseApiRepository;
|
||||
|
||||
class OrderRepository extends BaseApiRepository implements OrderRepositoryInterface
|
||||
{
|
||||
public function getModel()
|
||||
{
|
||||
return Order::class;
|
||||
}
|
||||
|
||||
public function createItem($orderId, array $data)
|
||||
{
|
||||
return \App\Models\OrderItem::create(array_merge(
|
||||
['order_id' => $orderId],
|
||||
$data
|
||||
));
|
||||
}
|
||||
|
||||
public function processPayment($order, $method)
|
||||
{
|
||||
return $order->update([
|
||||
'status' => 'paid',
|
||||
'payment_method' => $method,
|
||||
'paid_at' => now()
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
<?php
|
||||
|
||||
namespace App\Repositories\Api\Order;
|
||||
|
||||
use App\Repositories\BaseRepositoryInterface;
|
||||
|
||||
interface OrderRepositoryInterface extends BaseRepositoryInterface
|
||||
{
|
||||
public function createItem($orderId, array $data);
|
||||
|
||||
public function processPayment($order, $method);
|
||||
}
|
||||
@@ -32,4 +32,15 @@ class ServiceRepository extends BaseApiRepository implements ServiceRepositoryIn
|
||||
->where('is_active', 1)
|
||||
->first();
|
||||
}
|
||||
|
||||
public function getPricing(int $productId, string $billingCycle)
|
||||
{
|
||||
$pricing = $this->model->find($productId)
|
||||
->pricings()
|
||||
->where('billing_cycle', $billingCycle)
|
||||
->where('is_active', 1)
|
||||
->first();
|
||||
|
||||
return $pricing ? $pricing->price : null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,4 +12,6 @@ interface ServiceRepositoryInterface extends BaseRepositoryInterface
|
||||
public function getActiveServices(int $limit = 10);
|
||||
|
||||
public function findActiveBySlug(string $slug);
|
||||
|
||||
public function getPricing(int $productId, string $billingCycle);
|
||||
}
|
||||
|
||||
@@ -1,18 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Repositories\Type;
|
||||
|
||||
use App\Repositories\BaseCachedRepository;
|
||||
|
||||
class TypeCachedRepository extends BaseCachedRepository implements TypeRepositoryInterface
|
||||
{
|
||||
public function __construct(
|
||||
public TypeRepository $typeRepository
|
||||
) {
|
||||
parent::__construct();
|
||||
}
|
||||
public function getModel()
|
||||
{
|
||||
return $this->typeRepository->getModel();
|
||||
}
|
||||
}
|
||||
@@ -1,14 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Repositories\Type;
|
||||
|
||||
use App\Models\Type;
|
||||
use App\Repositories\BaseRepository;
|
||||
|
||||
class TypeRepository extends BaseRepository implements TypeRepositoryInterface
|
||||
{
|
||||
public function getModel()
|
||||
{
|
||||
return Type::class;
|
||||
}
|
||||
}
|
||||
@@ -1,6 +0,0 @@
|
||||
<?php
|
||||
namespace App\Repositories\Type;
|
||||
|
||||
use App\Repositories\BaseRepositoryInterface;
|
||||
|
||||
interface TypeRepositoryInterface extends BaseRepositoryInterface {}
|
||||
Reference in New Issue
Block a user