Update source into github
This commit is contained in:
@@ -0,0 +1,81 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Services\Invoice;
|
||||
|
||||
use App\Enums\Invoice\InvoiceStatus;
|
||||
use App\Models\Invoice;
|
||||
use App\Models\InvoiceAddons;
|
||||
use App\Models\InvoiceItem;
|
||||
use App\Models\Order;
|
||||
use Illuminate\Http\Client\Request;
|
||||
|
||||
class InvoiceService
|
||||
{
|
||||
public function createInvoice(Order $order): Invoice
|
||||
{
|
||||
$productTypes = collect($order->orderItems)->pluck('product_type');
|
||||
|
||||
$invoiceType = match (true) {
|
||||
$productTypes->contains(3) => 3,
|
||||
$productTypes->contains(4) => 4,
|
||||
default => 1,
|
||||
};
|
||||
|
||||
$maxMonths = $order->orderItems->max('months') ?? 1;
|
||||
$dueDate = now()->addMonths($maxMonths);
|
||||
|
||||
$invoice = Invoice::create([
|
||||
'date' => now(),
|
||||
'order_num' => $order->order_num,
|
||||
'due_date' => $dueDate,
|
||||
'date_paid' => now(),
|
||||
'sub_total' => $order->amount,
|
||||
'tax' => 0,
|
||||
'total' => $order->amount,
|
||||
'customer_id' => $order->customer_id,
|
||||
'client_ip' => request()->ip(),
|
||||
'order_id' => $order->id,
|
||||
'note' => 'Thanh toán đơn hàng #' . $order->order_num,
|
||||
'invoice_type' => $invoiceType,
|
||||
'status' => 1,
|
||||
]);
|
||||
|
||||
foreach ($order->orderItems as $item) {
|
||||
$invoiceItem = InvoiceItem::create([
|
||||
'invoice_id' => $invoice->id,
|
||||
'product_id' => $item->product_id,
|
||||
'months' => $item->months,
|
||||
'os_id' => $item->os_id,
|
||||
'server_name' => $item->server_name,
|
||||
'quantity' => $item->quantity,
|
||||
'price' => $item->price,
|
||||
'product_type' => $item->product_type,
|
||||
]);
|
||||
|
||||
foreach ($item->addons as $addon) {
|
||||
InvoiceAddons::create([
|
||||
'invoice_item_id' => $invoiceItem->id,
|
||||
'addon_id' => $addon->addon_id,
|
||||
'qty' => $addon->qty,
|
||||
'price' => $addon->price,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
$order->update(['invoice_id' => $invoice->id]);
|
||||
|
||||
return $invoice;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public function markAsPaid(Invoice $invoice): Invoice
|
||||
{
|
||||
$invoice->update([
|
||||
'status' => InvoiceStatus::PAID->value,
|
||||
'date_paid' => now(),
|
||||
]);
|
||||
|
||||
return $invoice;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user