update
This commit is contained in:
@@ -2,8 +2,11 @@
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use App\Enums\Invoice\InvoiceStatus;
|
||||
use App\Enums\Order\OrderStatus;
|
||||
use App\Enums\Product\ProductBillingCycle;
|
||||
use App\Enums\Product\ProductTypeEnums;
|
||||
use App\Enums\TransactionStatus;
|
||||
use App\Http\Helpers\Helper;
|
||||
use App\Jobs\Hosting\CreateHostingJobs;
|
||||
use App\Jobs\Mail\Customer\OrderCreatedJob;
|
||||
@@ -12,6 +15,7 @@ use App\Models\Customer;
|
||||
use App\Models\Order;
|
||||
use App\Models\OrderItem;
|
||||
use App\Models\Payment;
|
||||
use App\Models\Transaction;
|
||||
use App\Repositories\Invoice\InvoiceRepositoryInterface;
|
||||
use App\Repositories\InvoiceItem\InvoiceItemRepositoryInterface;
|
||||
use App\Repositories\Module\Hosting\HostingRepositoryInterface as ModuleHostingRepositoryInterface;
|
||||
@@ -20,6 +24,7 @@ use App\Repositories\Order\OrderRepositoryInterface;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class OrderService
|
||||
{
|
||||
@@ -41,8 +46,13 @@ class OrderService
|
||||
$paymentRecord = Payment::find((int) $data['payment_method']);
|
||||
$isBalancePayment = $paymentRecord && (int) $paymentRecord->type === 0;
|
||||
|
||||
// Balance payment → auto-confirm order & invoice
|
||||
$effectiveStatus = $isBalancePayment ? 1 : (int) $data['status'];
|
||||
// Balance payment → auto-confirm order, otherwise use requested order status.
|
||||
$effectiveOrderStatus = $isBalancePayment ? OrderStatus::ACTIVE->value : (int) $data['status'];
|
||||
$effectiveInvoiceStatus = match ($effectiveOrderStatus) {
|
||||
OrderStatus::ACTIVE->value => InvoiceStatus::PAID->value,
|
||||
OrderStatus::CANCELLED->value => InvoiceStatus::CANCELLED->value,
|
||||
default => InvoiceStatus::UNPAID->value,
|
||||
};
|
||||
|
||||
[$productItems, $relIdData, $total] = $this->buildProductItems($data);
|
||||
|
||||
@@ -50,14 +60,14 @@ class OrderService
|
||||
|
||||
$invoiceData = [
|
||||
'invoice_number' => Helper::generateUniqueCode(),
|
||||
'due_date' => Carbon::today()->toDateString(),
|
||||
'paid_at' => $effectiveStatus === 1 ? Carbon::now() : null,
|
||||
'due_date' => Carbon::today()->addDays(7)->toDateString(),
|
||||
'paid_at' => $effectiveInvoiceStatus === InvoiceStatus::PAID->value ? Carbon::now() : null,
|
||||
'sub_total' => $total,
|
||||
'tax' => $total * (10 / 100),
|
||||
'total' => $orderTotal,
|
||||
'payment_method' => $paymentRecord?->id,
|
||||
'customer_id' => $data['customer_id'],
|
||||
'status' => $effectiveStatus,
|
||||
'status' => $effectiveInvoiceStatus,
|
||||
];
|
||||
|
||||
$orderData = [
|
||||
@@ -69,11 +79,11 @@ class OrderService
|
||||
'payment_method' => $paymentRecord?->code ?? null,
|
||||
'payment_proof' => $data['payment_proof'] ?? null,
|
||||
'user_id' => Auth::guard('admin')->id(),
|
||||
'status' => $effectiveStatus,
|
||||
'status' => $effectiveOrderStatus,
|
||||
'client_ip' => request()->getClientIp(),
|
||||
];
|
||||
|
||||
$order = DB::transaction(function () use ($invoiceData, $orderData, $productItems, $relIdData, $data, $orderTotal, $isBalancePayment) {
|
||||
$order = DB::transaction(function () use ($invoiceData, $orderData, $productItems, $relIdData, $data, $orderTotal, $isBalancePayment, $effectiveInvoiceStatus) {
|
||||
$invoice = $this->invoiceRepository->create($invoiceData);
|
||||
|
||||
$productItems = array_map(fn ($item) => array_merge($item, ['invoice_id' => $invoice->id]), $productItems);
|
||||
@@ -136,6 +146,24 @@ class OrderService
|
||||
$customer->decrement('total', $orderTotal);
|
||||
}
|
||||
|
||||
if ($effectiveInvoiceStatus === InvoiceStatus::PAID->value) {
|
||||
Transaction::firstOrCreate(
|
||||
[
|
||||
'customer_id' => $data['customer_id'],
|
||||
'trans' => $invoice->invoice_number,
|
||||
'type' => 'invoice',
|
||||
],
|
||||
[
|
||||
'uuid' => (string) Str::uuid(),
|
||||
'public_code' => 'INV-PAY-' . now()->format('Ymd') . '-' . strtoupper(Str::random(6)),
|
||||
'amount' => $orderTotal,
|
||||
'user_id' => Auth::guard('admin')->id(),
|
||||
'status' => TransactionStatus::COMPLETED->value,
|
||||
'note' => 'Admin tạo đơn hàng và xác nhận thanh toán hóa đơn #' . $invoice->invoice_number,
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
return $order;
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user