|
|
|
|
@ -142,12 +142,13 @@ class OrderItems extends SoftDeletesModel
|
|
|
|
|
$threshold = 100;
|
|
|
|
|
$last_id = cache("last_auto_checkout_order_item_id", 0);
|
|
|
|
|
|
|
|
|
|
$unpaid_order_items = $this
|
|
|
|
|
$unpaid_order_items = (new OrderItems())
|
|
|
|
|
->whereHas("order", function ($query) {
|
|
|
|
|
$query->where("status", Orders::STATUS_ONGOING);
|
|
|
|
|
})
|
|
|
|
|
->whereNull("paid_at")
|
|
|
|
|
->where("id", ">", $last_id);
|
|
|
|
|
->where("id", ">", $last_id)
|
|
|
|
|
->with("customer");
|
|
|
|
|
|
|
|
|
|
$unpaid_order_items = $unpaid_order_items
|
|
|
|
|
->orderBy("id")
|
|
|
|
|
@ -161,20 +162,14 @@ class OrderItems extends SoftDeletesModel
|
|
|
|
|
Log::channel("daily_auto_checkout")->info("Last id:" . $last_id);
|
|
|
|
|
foreach ($unpaid_order_items as $item) {
|
|
|
|
|
cache(['last_auto_checkout_order_item_id' => $item->id], now()->addSeconds(90));
|
|
|
|
|
$customer = $item->order->customer;
|
|
|
|
|
$customer = $item->customer;
|
|
|
|
|
if ($customer->balance < $item->total || $item->total == 0) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$item->update([
|
|
|
|
|
"paid_at" => date("Y-m-d H:i:s")
|
|
|
|
|
]);
|
|
|
|
|
$item->update(["paid_at" => date("Y-m-d H:i:s")]);
|
|
|
|
|
|
|
|
|
|
$balance = $customer->balance - $item->total;
|
|
|
|
|
$customer->update([
|
|
|
|
|
"balance" => $balance
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
(new Balance())->create([
|
|
|
|
|
"customer_id" => $customer->id,
|
|
|
|
|
"order_id" => $item->order_id,
|
|
|
|
|
@ -184,7 +179,7 @@ class OrderItems extends SoftDeletesModel
|
|
|
|
|
"balance" => $balance
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$item->order->refreshTotal();
|
|
|
|
|
$customer->update(["balance" => $balance]);
|
|
|
|
|
}
|
|
|
|
|
Log::channel("daily_auto_checkout")->info("From " . $unpaid_order_items->first()->id . " to " . $unpaid_order_items->last()->id);
|
|
|
|
|
}
|
|
|
|
|
|