|
|
|
|
@ -729,6 +729,107 @@ class OrdersController extends CommonController
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @OA\POST(
|
|
|
|
|
* path="/manager/checkout-order-items/{order_id}",
|
|
|
|
|
* summary="V2-中途结算(即只依次结算子订单,并不结束订单,只需余额刚好)",
|
|
|
|
|
* description="中途结算",
|
|
|
|
|
* @OA\Parameter(name="token", in="query", @OA\Schema(type="string"), required=true, description="token"),
|
|
|
|
|
* @OA\Parameter(name="order_id", in="path", @OA\Schema(type="integer"), required=true, description="订单id"),
|
|
|
|
|
* @OA\Parameter(name="to_date", in="query", @OA\Schema(type="date"), required=true, description="结算到的日子")
|
|
|
|
|
* @OA\Parameter(name="just_check", in="query", @OA\Schema(type="boolean"), required=false, description="是否只是check一下要多少钱")
|
|
|
|
|
* @OA\Response(
|
|
|
|
|
* response="200",
|
|
|
|
|
* description="中途结算"
|
|
|
|
|
* )
|
|
|
|
|
* )
|
|
|
|
|
*/
|
|
|
|
|
public function checkoutOrderItems($order_id, Request $request)
|
|
|
|
|
{
|
|
|
|
|
$order = Orders::with("customer")->find($order_id);
|
|
|
|
|
$order_items = OrderItems::where("order_id", $order_id)->whereRaw("DATEDIFF('{$request->to_date}',`service_date`) >= 0")->orderBy("service_date", "asc")->get();
|
|
|
|
|
$unpaid_order_items = $order_items->filter(function ($item) {
|
|
|
|
|
return $item->total > 0 && !$item->paid_at;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if ($order_items->count()) {
|
|
|
|
|
$to_generate_days = Carbon::parse($request->to_date)->diffInDays($order_items->last()->service_date, false);
|
|
|
|
|
$to_generate_start_date = Carbon::parse($order_items->last()->service_date)->addDay()->toDateString();
|
|
|
|
|
} else {
|
|
|
|
|
$to_generate_days = Carbon::parse($request->to_date)->diffInDays($order->from_date, false) + 1;
|
|
|
|
|
$to_generate_start_date = $order->from_date;
|
|
|
|
|
}
|
|
|
|
|
$to_generate_days = max(0, $to_generate_days);
|
|
|
|
|
$prepay_total = $unpaid_order_items->sum("total") + $to_generate_days * $order->price;
|
|
|
|
|
$to_recharge_total = $prepay_total - $order->customer->balance;
|
|
|
|
|
|
|
|
|
|
//如果只是检查一下需要充值金额,直接返回
|
|
|
|
|
if ($request->just_check) {
|
|
|
|
|
return response()->json([
|
|
|
|
|
"to_recharge_total" => $to_recharge_total
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//没有需要操作的子订单,无意义
|
|
|
|
|
if (!$unpaid_order_items->count() && !$to_generate_days) {
|
|
|
|
|
return response()->json([
|
|
|
|
|
"errorcode" => 30001,
|
|
|
|
|
"errormsg" => "截止到中途结算日,没有需要结算的子订单,如需预充值请发起收款操作"
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//需要预先充值
|
|
|
|
|
if ($to_recharge_total > 0) {
|
|
|
|
|
return response()->json([
|
|
|
|
|
"errorcode" => 30002,
|
|
|
|
|
"errormsg" => "余额不足,请先充值",
|
|
|
|
|
"to_recharge_total" => $to_recharge_total,
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DB::beginTransaction();
|
|
|
|
|
try {
|
|
|
|
|
for ($i = 0; $i < $to_generate_days; $i++) {
|
|
|
|
|
if ($i > 0) {
|
|
|
|
|
$service_date = Carbon::parse($to_generate_start_date)->addDays($i)->toDateString();
|
|
|
|
|
} else {
|
|
|
|
|
$service_date = $to_generate_start_date;
|
|
|
|
|
}
|
|
|
|
|
$new_item = (new OrderItems())->createItem($order->id, $service_date);
|
|
|
|
|
$unpaid_order_items->concat($new_item);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach ($unpaid_order_items as $order_item) {
|
|
|
|
|
//更新子订单支付状态
|
|
|
|
|
$order_item->update(["paid_at" => date("Y-m-d H:i:s")]);
|
|
|
|
|
|
|
|
|
|
//更新客户余额(暂不保存,最后统一保存)
|
|
|
|
|
$order->customer->balance = $order->customer->balance - $order_item->total;
|
|
|
|
|
|
|
|
|
|
//创建收款记录
|
|
|
|
|
(new Balance())->create([
|
|
|
|
|
"customer_id" => $order->customer->id,
|
|
|
|
|
"order_id" => $order->id,
|
|
|
|
|
"belongs_type" => get_class($order_item),
|
|
|
|
|
"belongs_id" => $order_item->id,
|
|
|
|
|
"money" => -$order_item->total,
|
|
|
|
|
"balance" => $order->customer->balance
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DB::commit();
|
|
|
|
|
return response()->json([
|
|
|
|
|
"checkout_items" => count($unpaid_order_items)
|
|
|
|
|
]);
|
|
|
|
|
} catch (\Exception $exception) {
|
|
|
|
|
DB::rollBack();
|
|
|
|
|
return response()->json([
|
|
|
|
|
"errorcode" => $exception->getCode(),
|
|
|
|
|
"errormsg" => $exception->getMessage()
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @OA\POST(
|
|
|
|
|
@ -748,6 +849,13 @@ class OrdersController extends CommonController
|
|
|
|
|
*/
|
|
|
|
|
public function scanPay($order_id)
|
|
|
|
|
{
|
|
|
|
|
if (request()->money <= 0) {
|
|
|
|
|
return response()->json([
|
|
|
|
|
"errorcode" => 60003,
|
|
|
|
|
"errormsg" => "付款金额不正确"
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$order = (new Orders())->find($order_id);
|
|
|
|
|
$recharge = (new Recharge())->create([
|
|
|
|
|
"customer_id" => $order->customer->id,
|
|
|
|
|
|