weizong song 5 years ago
parent 634a3f21f0
commit f2b071a0fc

@ -569,29 +569,61 @@ class OrdersController extends CommonController
*/
public function updateOrderItems(Request $request)
{
$order_items = (new OrderItems())->whereHas("order")->whereIn("id", (array)$request->ids)->get();
if ($request->has("price")) {
$warnings = [];
$errors = [];
$order_items = (new OrderItems())->whereHas("order")->whereIn("id", (array)$request->ids)->with("customer")->get();
$customer = $order_items->first()->customer;
$price_changed_paid_items = $order_items->filter(function ($item) use ($request) {
return $request->has("price") && $item->paid_at && $request->price != $item->total;
});
$total_increased = $price_changed_paid_items->count() * $request->price - $price_changed_paid_items->sum("total");
$warnings = [];
$errors = [];
if ($price_changed_paid_items->count()) {
$warnings[] = "将对" . $price_changed_paid_items->count() . "天已付款的子订单进行价格更改";
}
if ($total_increased > $customer->balance) {
$errors[] = "价格补差超过了客户余额,请先充值";
}
//todo:价格低于指导价的提示
if (count($errors)) {
return response()->json([
"errorcode" => "105",
"errormsg" => implode("", $errors)
"errormsg" => implode("", $errors)
]);
}
if (count($errors) && !$request->skip_warnings) {
return response()->json([
"has_warnings" => true,
"errormsg" => implode("", $warnings)
"errormsg" => implode("", $warnings)
]);
}
DB::beginTransaction();
try {
//循环更新所有子订单
$update = (new OrderItems())->filterRequestColumns($request);
foreach ($order_items as $order_item) {
//todo
$order_item->update($update);
}
//根据已付款的价格异动子订单循环添加余额平衡表记录,并随时更新客户余额值
foreach ($price_changed_paid_items as $price_changed_paid_item) {
$total_offset = $price_changed_paid_item->total - $request->price;
$customer->balance = $customer->balance + $total_offset;
(new Balance())->create([
"customer_id" => $customer->id,
"order_id" => $price_changed_paid_item->order_id,
"belongs_type" => get_class($price_changed_paid_item),
"belongs_id" => $price_changed_paid_item->id,
"money" => $total_offset,
"balance" => $customer->balance
]);
}
//保存客户余额更新
$customer->save();
DB::commit();
return response()->json(["updated_items" => count($order_items)]);
} catch (\Exception $exception) {

Loading…
Cancel
Save