You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

128 lines
4.5 KiB

5 years ago
<?php
/**
* Created by PhpStorm.
* User: weizongsong
* Date: 2019-04-12
* Time: 22:34
*/
namespace App\Http\Controllers\Admin;
4 years ago
use App\Models\OrderItems;
5 years ago
use App\Models\Orders;
4 years ago
use App\Models\Project;
5 years ago
use Illuminate\Http\Request;
4 years ago
use Illuminate\Support\Facades\DB;
5 years ago
class OrdersController extends CommonController
{
public $bladePath = "admin.orders";
public $urlPrefix = "admin/orders";
public $modelName = "订单";
public $modelClass = Orders::class;
public function index(Request $request)
{
4 years ago
$projects = (new StatisticsController())->_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);
4 years ago
if ($request->keyword) {
4 years ago
$this->model = $this->model->where(function ($query) use ($request) {
4 years ago
$query->where("serial", "like", "%" . $request->keyword . "%")
->orWhere("contact", "like", "%" . $request->keyword . "%")
->orWhere("mobile", "like", "%" . $request->keyword . "%");
});
4 years ago
}
4 years ago
if ($request->date) {
$this->model = $this->model->whereHas("orderItems", function ($query) use ($month, $request) {
$query->where("service_date", $month . "-" . $request->date);
});
}
5 years ago
$data = $this->model
->with([
"orderItems",
"firstItem",
"lastItem",
"project",
"product",
"customer",
"manager",
4 years ago
"bed" => function ($query) {
$query->with(["room", "building"]);
5 years ago
}
])
->orderBy("id", "desc")
->paginate(10);
foreach ($data as $order) {
$order = $order->refreshTotal();
}
4 years ago
return view($this->bladePath . ".index", compact("data", "project_id", "month", "project"));
5 years ago
}
4 years ago
public function changeItem(Request $request)
{
4 years ago
$order_item = OrderItems::with(["order", "siblings"])->find($request->item_id);
if (request()->is_batch) {
$order_items = $order_item->siblings;
} else {
$order_items = collect($order_item);
4 years ago
}
DB::beginTransaction();
try {
4 years ago
foreach($order_items as $order_item) {
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("未服务子订单不支持后台更改价格,请通过其他方法进行更改");
4 years ago
}
$order_item->update([
4 years ago
"total" => $request->total
4 years ago
]);
4 years ago
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)
]);
}
4 years ago
}
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);
}
5 years ago
}