55 lines
1.5 KiB
PHP
55 lines
1.5 KiB
PHP
<?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;
|
|
}
|
|
}
|