_checkProjects(); if (!$projects->count()) { return $this->error($this->noProjects); } $project_id = request()->project_id ?? $projects->first()->id; $project = Project::find($project_id); $month = request()->month ?? date("Y-m"); $months = (new StatisticsController())->_getMonths(); $start_timestamp = strtotime($month); $end_timestamp = strtotime("+1 month", strtotime($month)); $this->model = $this->model->whereRaw("UNIX_TIMESTAMP(`created_at`) >= " . $start_timestamp . " and UNIX_TIMESTAMP(`created_at`) < " . $end_timestamp); $this->model = $this->model->where("project_id", $project_id); $data = $this->model ->with([ "orderItems", "firstItem", "lastItem", "project", "product", "customer", "manager", "bed" => function ($query) { $query->with(["room", "building"]); } ]) ->orderBy("id", "desc") ->paginate(10); foreach ($data as $order) { $order = $order->refreshTotal(); } return view($this->bladePath . ".index", compact("data", "project_id", "month", "project")); } public function changeItem(Request $request) { $order_item = OrderItems::with("order")->find($request->item_id); if ($order_item->paid_at && $request->total != $order_item->total) { return $this->error("子订单已扣款,不支持后台更改价格,请通过其他方法进行更改"); } if ($order_item->total == 0 && $request->total != $order_item->total) { return $this->error("未服务子订单不支持后台更改价格,请通过其他方法进行更改"); } DB::beginTransaction(); try { $order_item->update([ "total" => $request->total ]); if ($request->factor_item) { $factors = json_decode($order_item->factors, true); foreach ($factors as &$factor) { if (!$factor["used_for_fee"]) continue; foreach ($request->factor_item as $k => $v) { if ($k != $factor["factor_item_id"]) continue; $factor["fee_percent"] = $v["fee_percent"]; $factor["fee"] = $v["fee"]; } } $order_item->update([ "factors" => json_encode($factors) ]); } DB::commit(); return $this->success("处理成功!"); } catch (\Exception $exception) { DB::rollBack(); return $this->error($exception->getMessage()); } } public function getItem($id) { $order_item = OrderItems::with("order")->find($id); return $this->ajaxResponse($order_item); } }