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.

1040 lines
45 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?php
namespace App\Http\Controllers\Manager;
use AlicFeng\IdentityCard\InfoHelper;
use App\Customer;
use App\Events\OrderAssigned;
use App\Libs\AlipayF2F;
use App\Libs\WxMicroPay;
use App\Models\Area;
use App\Models\Balance;
use App\Models\Bed;
use App\Models\OrderItems;
use App\Models\Orders;
use App\Models\ParamedicLevel;
use App\Models\Patient;
use App\Models\Product;
use App\Models\ProductItems;
use App\Models\ProductParamedicLevel;
use App\Models\Project;
use App\Models\Recharge;
use App\Models\Refund;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Intervention\Image\Facades\Image;
use SimpleSoftwareIO\QrCode\Facades\QrCode;
class OrdersController extends CommonController
{
/**
* @OA\Get(
* path="/manager/get-projects",
* summary="V2 获取医院列表",
* description="获取医院列表",
* @OA\Parameter(name="token", in="query", @OA\Schema(type="string"), required=true, description="token"),
* @OA\Response(
* response="200",
* description="获取医院列表"
* )
* )
*/
public function getProjects()
{
$projects = (new Project())
->join("manager_project", 'project.id', '=', 'manager_project.project_id')
->join('managers', 'managers.id', '=', 'manager_project.manager_id')
->whereRaw("managers.id = " . $this->manager->id)
->select("project.id", "project.name", "project.address", "project.latitude", "project.longitude")
->get();
return response()->json($projects->toArray());
}
/**
* @OA\Get(
* path="/manager/get-care-product/{project_id}",
* summary="V2-获取产品详情",
* description="获取产品详情",
* @OA\Parameter(name="token", in="query", @OA\Schema(type="string"), required=true, description="token"),
* @OA\Parameter(name="project_id", in="path", @OA\Schema(type="integer"), required=true, description="project_id"),
* @OA\Response(
* response="200",
* description="获取产品详情"
* )
* )
*/
public function getCareProduct($project_id)
{
DB::enableQueryLog();
$product = (new Product())->where("project_id", $project_id)->with([
"productItems" => function ($query) {
$query->select("id", "name", "product_id", "patient_quantity", "price");
},
"productParamedicLevels" => function ($query) {
$query
->select("product_paramedic_level.id", "product_paramedic_level.product_id", "product_paramedic_level.price")
->leftJoin("paramedic_level", "product_paramedic_level.paramedic_level_id", "=", "paramedic_level.id")
->addSelect("paramedic_level.name as paramedic_level_name");
},
"factors" => function ($query) {
$query->with(["factorItems" => function ($query) {
$query->select("id", "name", "factor_id", "price", "myindex")->orderBy("myindex");
}])->select("id", "name", "product_id")->orderBy("myindex");
}])
->leftJoin("project", "project.id", "=", "product.project_id")
->select("product.id", "product.name", "product.project_id", "project.name as project_name")->first();
return response()->json($product->toArray());
}
/**
* @OA\Get(
* path="/manager/get-project-orders-count/{project_id}",
* summary="V2-获取医院订单数量角标",
* description="获取医院订单数量角标",
* @OA\Parameter(name="token", in="query", @OA\Schema(type="string"), required=true, description="token"),
* @OA\Parameter(name="project_id", in="query", @OA\Schema(type="integer"), required=true, description="医院id"),
* @OA\Response(
* response="200",
* description="获取医院订单数量角标"
* )
* )
*/
public function getProjectOrdersCount($project_id)
{
$orders_count = [];
$orders_count["all"] = (new Orders())->ofProject($project_id)->count();
$orders_count["pending"] = (new Orders())->ofProject($project_id)->whereIn("status", [Orders::STATUS_UNCONFIRMED, Orders::STATUS_UNASSIGNED])->count();
$orders_count["ongoing"] = (new Orders())->ofProject($project_id)->where("status", Orders::STATUS_ONGOING)->count();
$orders_count["finished"] = (new Orders())->ofProject($project_id)->where("status", Orders::STATUS_ONGOING)->count();
return response()->json(compact("orders_count"));
}
/**
* @OA\Get(
* path="/manager/get-projcet-orders/{project_id}",
* summary="V2-获取订单列表",
* description="获取订单列表",
* @OA\Parameter(name="token", in="query", @OA\Schema(type="string"), required=true, description="token"),
* @OA\Parameter(name="project_id", in="path", @OA\Schema(type="integer"), required=false, description="医院ID"),
* @OA\Parameter(name="keyword", in="query", @OA\Schema(type="string"), required=false, description="查询关键词"),
* @OA\Parameter(name="page", in="query", @OA\Schema(type="integer"), required=false, description="当前页码默认为1"),
* @OA\Parameter(name="page_size", in="query", @OA\Schema(type="integer"), required=false, description="每页数量默认为5"),
* @OA\Parameter(name="status", in="query", @OA\Schema(type="string"), required=false, description="订单状态:[pending=>待处理,ongoing=>进行中,finished=>已完成]"),
* @OA\Response(
* response="200",
* description="获取订单列表"
* )
* )
*/
public function list($project_id)
{
$model = $this->_getOrderModel();
$model = $model->ofProject($project_id);
if (request()->keyword) {
$keyword = request()->keyword;
$model = $model->where(function ($query) use ($keyword) {
$query
->where("serial", "like", "%{$keyword}%")
->orWhereHas("patient", function ($query) use ($keyword) {
$query->where("name", "like", "%{$keyword}%");
})->orWhereHas("paramedic", function ($query) use ($keyword) {
$query->where("name", "like", "%{$keyword}%");
});
});
}
switch (request()->status) {
case "pending":
$model = $model->whereIn("status", [Orders::STATUS_UNCONFIRMED, Orders::STATUS_UNASSIGNED]);
break;
case "ongoing":
case "finished":
$model = $model->where("status", constant(Orders::class . "::STATUS_" . strtoupper(request()->status)));
break;
default:
//do nothing
}
$page_size = request()->page_size ?? 5;
$data = $model->orderBy("id", "desc")->paginate($page_size);
foreach ($data as $order) {
$order = $order->refreshTotal();
$order->balance = $order->customer->balance;
}
return response()->json($data->toArray());
}
/**
* @OA\Get(
* path="/manager/get-order/{id}",
* summary="V2-获取订单详情",
* description="获取订单详情",
* @OA\Parameter(name="token", in="query", @OA\Schema(type="string"), required=true, description="token"),
* @OA\Parameter(name="id", in="path", @OA\Schema(type="integer"), required=true, description="id"),
* @OA\Response(
* response="200",
* description="获取订单详情"
* )
* )
*/
public function getOrder($id)
{
$model = $this->_getOrderModel();
$order = $model->with([
"orderItems" => function ($query) {
},
"recharges",
"refunds"
])->find($id);
$order = $order->refreshTotal();
$order->balance = $order->customer->balance;
return response()->json($order->toArray());
}
public function _getOrderModel()
{
$model = $order = (new Orders())
->select(
"orders.id",
"orders.serial",
"orders.customer_id",
"orders.manager_id",
"orders.bed_id",
"orders.patient_id",
"orders.project_id",
"orders.product_id",
"orders.product_item_id",
"orders.product_paramedic_level_id",
"orders.from_date",
"orders.to_date",
"orders.status",
"orders.total",
"orders.paid_total",
"orders.contact",
"orders.mobile",
"orders.paramedic_id",
"orders.price",
"orders.factors",
"orders.patient_quantity",
"orders.created_at"
)
->with([
"productItem" => function ($query) {
$query->select("id", "name");
},
"paramedicLevel" => function ($query) {
$query->select("paramedic_level.id", "paramedic_level.name");
},
"project" => function ($query) {
$query->select("id", "name");
},
"bed" => function ($query) {
$query->select("bed.id", "bed.name")
->leftJoin("building", "building.id", "=", "bed.building_id")
->leftJoin("area", "area.id", "=", "bed.area_id")
->leftJoin("room", "room.id", "=", "bed.room_id")
->addSelect("room.name as room_name", "area.name as area_name", "building.name as building_name");
},
"customer" => function ($query) {
$query->select("id", "name", "balance");
},
"patient" => function ($query) {
$query->select("id", "name", "sex", "age", "mobile");
},
"paramedic" => function ($query) {
$query->select("paramedic.id", "paramedic.name", "paramedic.mobile")
->leftJoin("paramedic_level", "paramedic_level.id", "=", "paramedic.paramedic_level_id")
->addSelect("paramedic_level.name as paramedic_level_name");
}
]);
return $model;
}
/**
* @OA\Get(
* path="/manager/get-project-areas/{project_id}",
* summary="V2-获取医院病区",
* description="获取医院病区",
* @OA\Parameter(name="token", in="query", @OA\Schema(type="string"), required=true, description="token"),
* @OA\Parameter(name="project_id", in="path", @OA\Schema(type="integer"), required=true, description="project id"),
* @OA\Response(
* response="200",
* description="获取医院床位"
* )
* )
*/
public function getProjectAreas($project_id)
{
$areas = (new Area())
->where("area.project_id", $project_id)
->select("area.id", "area.name", "area.building_id")
->leftJoin("building", "building.id", "=", "area.building_id")
->addSelect("building.name as building_name")
->orderBy("building.myindex")
->orderBy("area.myindex")
->get();
return response()->json($areas->toArray());
}
/**
* @OA\Get(
* path="/manager/get-area-beds/{area_id}",
* summary="V2-根据病区获取病床",
* description="根据病区获取病床",
* @OA\Parameter(name="token", in="query", @OA\Schema(type="string"), required=true, description="token"),
* @OA\Parameter(name="area_id", in="path", @OA\Schema(type="integer"), required=true, description="area id"),
* @OA\Response(
* response="200",
* description="根据病区获取病床"
* )
* )
*/
public function getAreaBeds($area_id)
{
$beds = (new Bed())
->where("bed.area_id", $area_id)
->select("bed.id", "bed.name", "bed.area_id", "bed.room_id")
->leftJoin("room", "room.id", "=", "bed.room_id")
->addSelect("room.name as room_name")
->orderBy("bed.name")
->get();
return response()->json($beds->toArray());
}
/**
* @OA\Get(
* path="/manager/get-available-paramedics",
* summary="V2-获取可用护工列表(已基本准确,需进一步打磨)",
* description="获取可用护工列表",
* @OA\Parameter(name="token", in="query", @OA\Schema(type="string"), required=true, description="token"),
* @OA\Parameter(name="bed_id", in="query", @OA\Schema(type="integer"), required=true, description="床位ID"),
* @OA\Parameter(name="sex", in="query", @OA\Schema(type="string"), required=true, description="性别:[男/女]"),
* @OA\Parameter(name="start_date", in="query", @OA\Schema(type="string"), required=false, description="日期,默认为当天"),
* @OA\Parameter(name="factors", in="query", @OA\Schema(type="object"), required=true, description="价格因子选择,[{id:1,factor_item_id:1},{...}],如果为空数组请传[]"),
* @OA\Response(
* response="200",
* description="获取可用护工列表"
* )
* )
*/
public function getAvailableParamedics(Request $request)
{
$paramedics = (new Orders())->getAvailableParamedics();
return response()->json($paramedics);
}
/**
* @OA\POST(
* path="/manager/create-patient",
* summary="V2-创建被护理人",
* description="创建被护理人",
* @OA\Parameter(name="token", in="query", @OA\Schema(type="string"), required=true, description="token"),
* @OA\Parameter(name="name", in="query", @OA\Schema(type="string"), required=true, description="姓名"),
* @OA\Parameter(name="sex", in="query", @OA\Schema(type="string"), required=true, description="性别:[男/女]"),
* @OA\Parameter(name="age", in="query", @OA\Schema(type="integer"), required=true, description="年龄,只需填写入院时的年龄"),
* @OA\Response(
* response="200",
* description="创建被护理人"
* )
* )
*/
public function createPatient()
{
$data = [
"name" => request()->name,
"sex" => request()->sex,
"age" => request()->age,
"mobile" => request()->mobile
];
$vo = (new Patient())->create($data);
return response()->json($vo);
}
/**
* @OA\POST(
* path="/manager/create-order",
* summary="V2-创建订单",
* description="创建订单",
* @OA\Parameter(name="token", in="query", @OA\Schema(type="string"), required=true, description="token"),
* @OA\Parameter(name="product_id", in="query", @OA\Schema(type="integer"), required=true, description="产品id"),
* @OA\Parameter(name="product_item_id", in="query", @OA\Schema(type="integer"), required=true, description="产品型号id"),
* @OA\Parameter(name="product_paramedic_level_id", in="query", @OA\Schema(type="integer"), required=true, description="产品-护工等级id"),
* @OA\Parameter(name="factors", in="query", @OA\Schema(type="object"), required=true, description="价格因子选择,[{id:1,factor_item_id:1},{...}],如果为空数组请传[]"),
* @OA\Parameter(name="bed_id", in="query", @OA\Schema(type="integer"), required=true, description="床位id"),
* @OA\Parameter(name="paramedic_id", in="query", @OA\Schema(type="integer"), required=false, description="护工id"),
* @OA\Parameter(name="patient_name", in="query", @OA\Schema(type="string"), required=true, description="被护理人姓名"),
* @OA\Parameter(name="patient_mobile", in="query", @OA\Schema(type="string"), required=true, description="被护理人电话"),
* @OA\Parameter(name="patient_sex", in="query", @OA\Schema(type="string"), required=true, description="被护理人性别:男,女"),
* @OA\Parameter(name="patient_age", in="query", @OA\Schema(type="string"), required=true, description="被护理人年龄"),
* @OA\Parameter(name="contact", in="query", @OA\Schema(type="string"), required=false, description="联系人"),
* @OA\Parameter(name="mobile", in="query", @OA\Schema(type="string"), required=false, description="联系人电话"),
* @OA\Parameter(name="from_date", in="query", @OA\Schema(type="string"), required=true, description="开始日期"),
* @OA\Parameter(name="to_date", in="query", @OA\Schema(type="string"), required=true, description="结束日期"),
* @OA\Parameter(name="price", in="query", @OA\Schema(type="number"), required=true, description="协商价格"),
* @OA\Response(
* response="200",
* description="创建订单"
* )
* )
*/
public function createOrder()
{
DB::beginTransaction();
try {
$customer = (new Customer())->firstOrCreate([
"mobile" => trim(request()->mobile)
]);
$has_orders = Orders::where("customer_id", $customer->id)->whereIn("status", [Orders::STATUS_UNCONFIRMED, Orders::STATUS_UNASSIGNED, Orders::STATUS_ONGOING])->count();
if ($has_orders) {
return response()->json([
"errorcode" => "101",
"errormsg" => "客户名下有即将开始或正在进行中的订单,暂不可以再次下单"
]);
}
$product = (new Product())->find(request()->product_id);
$product_paramedic_level = (new ProductParamedicLevel())->find(request()->product_paramedic_level_id);
$product_item = (new ProductItems())->find(request()->product_item_id);
$price = $product_item->price + $product_paramedic_level->price;
$factors = (new Orders())->requestFactorsToOrderFactors();
$price += collect($factors)->sum("price");
if (request()->price < $price) {
return response()->json([
"errorcode" => "102",
"errormsg" => "协商价格不能低于系统指导价"
]);
}
$patient = (new Patient())->firstOrCreate([
"customer_id" => $customer->id,
"name" => request()->patient_name
]);
$patient->update([
"age" => request()->patient_age,
"sex" => request()->patient_sex,
"mobile" => request()->patient_mobile,
]);
$order = (new Orders())->create([
"customer_id" => $customer->id,
"project_id" => $product->id,
"product_id" => request()->product_id,
"patient_id" => $patient->id,
"contact" => request()->contact ?? request()->patient_name,
"mobile" => request()->mobile ?? request()->patient_mobile,
"from_date" => request()->from_date,
"to_date" => request()->to_date,
"product_item_id" => request()->product_item_id,
"product_paramedic_level_id" => request()->product_paramedic_level_id,
"bed_id" => request()->bed_id,
"paramedic_id" => request()->paramedic_id,
"price" => request()->price,
"factors" => json_encode($factors),
"status" => request()->paramedic_id ? Orders::STATUS_ONGOING : Orders::STATUS_UNASSIGNED,
"manager_id" => $this->manager->id
]);
$order->getSerial();
if (request()->paramedic_id) {
event(new OrderAssigned($order));
}
DB::commit();
return $this->getOrder($order->id);
} catch (\Exception $exception) {
DB::rollBack();
return response()->json([
"errorcode" => $exception->getCode(),
"errormsg" => $exception->getMessage()
]);
}
}
/**
* @OA\POST(
* path="/manager/update-order/{id}",
* summary="V2-订单修改(静态修改不影响当前所有子订单)",
* description="订单修改",
* @OA\Parameter(name="token", in="query", @OA\Schema(type="string"), required=true, description="token"),
* @OA\Parameter(name="product_id", in="query", @OA\Schema(type="integer"), required=true, description="产品id"),
* @OA\Parameter(name="product_item_id", in="query", @OA\Schema(type="integer"), required=true, description="产品型号id"),
* @OA\Parameter(name="product_paramedic_level_id", in="query", @OA\Schema(type="integer"), required=true, description="产品-护工等级id"),
* @OA\Parameter(name="factors", in="query", @OA\Schema(type="object"), required=true, description="价格因子选择,[{id:1,factor_item_id:1},{...}],如果为空数组请传[]"),
* @OA\Parameter(name="bed_id", in="query", @OA\Schema(type="integer"), required=true, description="床位id"),
* @OA\Parameter(name="paramedic_id", in="query", @OA\Schema(type="integer"), required=false, description="护工id"),
* @OA\Parameter(name="patient_name", in="query", @OA\Schema(type="string"), required=true, description="被护理人姓名"),
* @OA\Parameter(name="patient_mobile", in="query", @OA\Schema(type="string"), required=true, description="被护理人电话"),
* @OA\Parameter(name="patient_sex", in="query", @OA\Schema(type="string"), required=true, description="被护理人性别:男,女"),
* @OA\Parameter(name="patient_age", in="query", @OA\Schema(type="string"), required=true, description="被护理人年龄"),
* @OA\Parameter(name="contact", in="query", @OA\Schema(type="string"), required=false, description="联系人"),
* @OA\Parameter(name="mobile", in="query", @OA\Schema(type="string"), required=false, description="联系人电话"),
* @OA\Parameter(name="from_date", in="query", @OA\Schema(type="string"), required=true, description="开始日期"),
* @OA\Parameter(name="to_date", in="query", @OA\Schema(type="string"), required=true, description="结束日期"),
* @OA\Parameter(name="price", in="query", @OA\Schema(type="number"), required=true, description="协商价格"),
* @OA\Response(
* response="200",
* description="订单修改"
* )
* )
*/
public function updateOrder($id)
{
DB::beginTransaction();
try {
$order = Orders::with(["customer", "patient", "product", "productItem", "productParamedicLevel"])->find($id);
if (request()->has("paramedic_id")) {
return response()->json([
"errorcode" => "103",
"errormsg" => "修改订单不包含护工更改,更换护工请移步"
]);
}
$product_paramedic_level = request()->has("product_paramedic_level_id") ? (new ProductParamedicLevel())->find(request()->product_paramedic_level_id) : $order->productParamedicLevel;
$product_item = request()->has("product_item_id") ? (new ProductItems())->find(request()->product_item_id) : $order->productItem;
$price = $product_item->price + $product_paramedic_level->price;
$factors = request()->has("factors") ? (new Orders())->requestFactorsToOrderFactors() : json_decode($order->factors);
$price += collect($factors)->sum("price");
if (request()->has("price") && request()->price < $price) {
return response()->json([
"errorcode" => "102",
"errormsg" => "协商价格不能低于系统指导价"
]);
}
//更新被陪护人
if (request()->has("patient_name", "patient_mobile", "patient_sex", "patient_age")) {
$order->patient->update([
"name" => request()->patient_name,
"age" => request()->patient_age,
"sex" => request()->patient_sex,
"mobile" => request()->patient_mobile,
]);
}
//更新订单
$update = (new Orders())->filterRequestColumns(request(), ["id"]);
if (request()->has("contact", "patient_name") && !request()->contact) {
$update["contact"] = request()->patient_name;
}
if (request()->has("mobile", "patient_mobile") && !request()->mobile) {
$update["mobile"] = request()->patient_mobile;
}
if (request()->has("factors")) {
$update["factors"] = $factors;
}
$order->update($update);
DB::commit();
return response()->json($order->toArray());
} catch (\Exception $exception) {
DB::rollBack();
return response()->json([
"errorcode" => $exception->getCode(),
"errormsg" => $exception->getMessage()
]);
}
}
/**
* @OA\POST(
* path="/manager/update-order-items",
* summary="V2-子订单修改(覆盖单条修改)",
* description="子订单修改",
* @OA\Parameter(name="token", in="query", @OA\Schema(type="string"), required=true, description="token"),
* @OA\Parameter(name="ids", in="query", @OA\Schema(type="integer"), required=true, description="子订单id可传单个id或数组"),
* @OA\Parameter(name="bed_id", in="query", @OA\Schema(type="integer"), required=false, description="床位id"),
* @OA\Parameter(name="paramedic_id", in="query", @OA\Schema(type="integer"), required=false, description="护工id"),
* @OA\Parameter(name="price", in="query", @OA\Schema(type="integer"), required=false, description="价格"),
* @OA\Parameter(name="skip_warnings", in="query", @OA\Schema(type="integer"), required=false, description="跳过警示项目"),
* @OA\Response(
* response="200",
* description="子订单修改",
* content={
* @OA\MediaType(
* mediaType="application/json",
* @OA\Schema(
* @OA\Property(
* property="errorcode",
* type="integer",
* description="错误码;仅在发生错误时发送"
* ),
* @OA\Property(
* property="errormsg",
* type="string",
* description="返回消息有errorcode时为错误内容无errorcode时为提示内容"
* ),
* @OA\Property(
* property="has_warnings",
* type="boolean",
* description="是否有警告内容仅在有警告内容且无错误代码的情况下发送此时读取errormsg为警告内容"
* ),
* @OA\Property(
* property="updated_items",
* type="integer",
* description="更新的子订单总数;仅在成功的情况下发送"
* ),
* @OA\Property(
* property="updated_paid_items",
* type="integer",
* description="产生了价格波动的子订单总数,连锁效应为每条价格波动会新增一条财务记录,并影响客户的余额;仅在成功的情况下发送"
* ),
* example={
* "errorcode": "105",
* "errormsg": "价格补差100超过了客户余额90请先充值",
* "has_warnings": 1,
* "updated_items": 10,
* "updated_paid_items": 3
* }
* )
* )
* }
* )
* )
*/
public function updateOrderItems(Request $request)
{
$order_items = (new OrderItems())->whereHas("order")->whereIn("id", (array)$request->ids)->with("customer")->get();
if (!$order_items->count()) {
return response()->json([
"errorcode" => "105",
"errormsg" => "没有获取到子订单"
]);
}
$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[] = "价格补差{$total_increased}超过了客户余额{$customer->balance},请先充值";
}
//todo:价格低于指导价的提示
if (count($errors)) {
return response()->json([
"errorcode" => "105",
"errormsg" => implode("", $errors)
]);
}
if (count($errors) && !$request->skip_warnings) {
return response()->json([
"has_warnings" => true,
"errormsg" => implode("", $warnings)
]);
}
DB::beginTransaction();
try {
//根据已付款的价格异动子订单循环添加余额平衡表记录,并随时更新客户余额值,执行完毕后保存客户余额更新
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();
//循环更新所有子订单
$update = [];
if ($request->has("bed_id")) {
$update["bed_id"] = $request->bed_id;
}
if ($request->has("bed_id")) {
$update["paramedic_id"] = $request->paramedic_id;
}
if ($request->has("price")) {
$update["total"] = $request->price;
}
foreach ($order_items as $order_item) {
$order_item->update($update);
}
DB::commit();
return response()->json([
"updated_items" => count($order_items),
"updated_paid_items" => count($price_changed_paid_items)
]);
} catch (\Exception $exception) {
DB::rollBack();
return response()->json([
"errorcode" => $exception->getCode(),
"errormsg" => $exception->getMessage()
]);
}
}
/**
* @OA\POST(
* path="/manager/scan-pay/{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="money", in="query", @OA\Schema(type="number"), required=true, description="支付金额"),
* @OA\Parameter(name="auth_code", in="query", @OA\Schema(type="string"), required=true, description="扫码后获取的支付码"),
* @OA\Parameter(name="type", in="query", @OA\Schema(type="string"), required=true, description="支付方式weixin,alipay"),
* @OA\Response(
* response="200",
* description="扫用户支付码收款"
* )
* )
*/
public function scanPay($order_id)
{
$order = (new Orders())->find($order_id);
$recharge = (new Recharge())->create([
"customer_id" => $order->customer->id,
"money" => request()->money,
"order_id" => $order->id,
"payment" => request()->type
]);
$recharge = $recharge->getSerial();
switch (request()->type) {
case "weixin":
$res = (new WxMicroPay())->pay($recharge);
if ($res === true) {
return response()->json(true);
} else {
return response()->json([
"errorcode" => 60003,
"errormsg" => $res->getMessage()
]);
}
break;
case "alipay":
$res = (new AlipayF2F())->pay($recharge);
if ($res["status"]) {
return response()->json(true);
} else {
return response()->json([
"errorcode" => $res["code"],
"errormsg" => $res["msg"]
]);
}
break;
default:
return response()->json([
"errorcode" => 60003,
"errormsg" => "不正确的支付方式"
]);
}
}
/**
* @OA\POST(
* path="/manager/assign-order/{id}",
* summary="V2-派单",
* description="派单",
* @OA\Parameter(name="token", in="query", @OA\Schema(type="string"), required=true, description="token"),
* @OA\Parameter(name="id", in="path", @OA\Schema(type="integer"), required=true, description="订单id"),
* @OA\Parameter(name="paramedic_id", in="query", @OA\Schema(type="integer"), required=true, description="护工id"),
* @OA\Response(
* response="200",
* description="派单"
* )
* )
*/
public function assignOrder($id)
{
$order = (new Orders())->find($id);
if ($order->status !== Orders::STATUS_UNCONFIRMED || $order->status !== Orders::STATUS_UNASSIGNED) {
return response()->json([
"errorcode" => 50001,
"errormsg" => "订单状态不适配"
]);
}
DB::beginTransaction();
try {
$order->update([
"paramedic_id" => request()->paramedic_id,
"status" => Orders::STATUS_ONGOING
]);
event(new OrderAssigned($order));
DB::commit();
return response()->json($order);
} catch (\Exception $exception) {
DB::rollBack();
return response()->json([
"errorcode" => $exception->getCode(),
"errormsg" => $exception->getMessage()
]);
}
}
/**
* @OA\POST(
* path="/manager/cancel-order/{id}",
* summary="V2-取消订单",
* description="取消订单",
* @OA\Parameter(name="token", in="query", @OA\Schema(type="string"), required=true, description="token"),
* @OA\Parameter(name="id", in="path", @OA\Schema(type="integer"), required=true, description="订单id"),
* @OA\Response(
* response="200",
* description="取消订单"
* )
* )
*/
public function cancelOrder($id)
{
$order = (new Orders())->with("orderItems")->find($id);
if (!in_array($order->status, [Orders::STATUS_UNCONFIRMED, Orders::STATUS_UNASSIGNED])) {
return response()->json([
"errorcode" => 50001,
"errormsg" => "订单状态不适配"
]);
}
DB::beginTransaction();
try {
foreach ($order->orderItems as $orderItem) {
$orderItem->delete();
}
$order->delete();
DB::commit();
return response()->json($order);
} catch (\Exception $exception) {
DB::rollBack();
return response()->json([
"errorcode" => $exception->getCode(),
"errormsg" => $exception->getMessage()
]);
}
}
/**
* @OA\POST(
* path="/manager/get-balance/{customer_id}",
* summary="获取用户余额",
* description="获取用户余额",
* @OA\Parameter(name="token", in="query", @OA\Schema(type="string"), required=true, description="token"),
* @OA\Parameter(name="customer_id", in="path", @OA\Schema(type="integer"), required=true, description="用户id"),
* @OA\Response(
* response="200",
* description="获取用户余额"
* )
* )
*/
public function getBalance($customer_id)
{
$customer = (new Customer())->find($customer_id);
if (!$customer) {
return response()->json([
"errorcode" => 70001,
"errormsg" => "没找到用户信息"
]);
}
return response()->json([
"balance" => $customer->balance
]);
}
/**
* @OA\POST(
* path="/manager/recharge-for-order/{id}",
* summary="V2-现金或pos刷卡充值",
* description="现金或pos刷卡充值",
* @OA\Parameter(name="token", in="query", @OA\Schema(type="string"), required=true, description="token"),
* @OA\Parameter(name="id", in="path", @OA\Schema(type="integer"), required=true, description="订单id"),
* @OA\Parameter(name="payment", in="query", @OA\Schema(type="string"), required=true, description="枚举pos=>pos机刷卡支付, cash=>现金支付"),
* @OA\Parameter(name="money", in="query", @OA\Schema(type="number"), required=true, description="金额,非负数"),
* @OA\Parameter(name="remark", in="query", @OA\Schema(type="string"), required=false, description="备注信息:现金支付时可以忽略;刷卡支付时,填入回单号或卡号以便财务对账"),
* @OA\Response(
* response="200",
* description="现金或pos刷卡充值"
* )
* )
*/
public function rechargeForOrder($id)
{
DB::beginTransaction();
try {
$order = (new Orders())->find($id);
//创建充值记录
$recharge = [
"customer_id" => $order->customer->id,
"money" => request()->money,
"order_id" => $order->id,
"payment" => request()->payment,
"manager_id" => $this->manager->id,
"paid_at" => date("Y-m-d H:i:s")
];
switch (request()->payment) {
case "pos":
case "cash":
$recharge["remark"] = request()->remark;
break;
default:
return response()->json([
"errorcode" => "0",
"errormsg" => "不正确的支付方式"
]);
}
$recharge = (new Recharge())->create($recharge);
$recharge = $recharge->getSerial();
//更新用户余额,创建流水记录
$customer_balance = $recharge->customer->balance + $recharge->money;
$recharge->customer->update([
"balance" => $customer_balance
]);
$balance = (new Balance())->create([
"customer_id" => $recharge->customer->id,
"order_id" => $recharge->order_id,
"belongs_type" => get_class($recharge),
"belongs_id" => $recharge->id,
"money" => $recharge->money,
"balance" => $customer_balance
]);
DB::commit();
return response()->json([
"recharge" => $recharge,
"balance" => $balance
]);
} catch (\Exception $exception) {
DB::rollBack();
return response()->json([
"errorcode" => $exception->getCode(),
"errormsg" => $exception->getMessage()
]);
}
}
/**
* @OA\POST(
* path="/manager/refund-for-order/{id}",
* summary="V2-退款",
* description="退款",
* @OA\Parameter(name="token", in="query", @OA\Schema(type="string"), required=true, description="token"),
* @OA\Parameter(name="id", in="path", @OA\Schema(type="integer"), required=true, description="订单id"),
* @OA\Parameter(name="money", in="query", @OA\Schema(type="number"), required=true, description="金额,非负数"),
* @OA\Parameter(name="payment", in="query", @OA\Schema(type="string"), required=true, description="退款方式枚举cashonline。注意可用的退款方式需要通过pre-chackout获取随意提交金额可能会造成问题"),
* @OA\Parameter(name="recharge_id", in="query", @OA\Schema(type="integer"), required=false, description="相关在线支付的id用于原路返回。退款方式为online时必须给出"),
* @OA\Parameter(name="remark", in="query", @OA\Schema(type="string"), required=false, description="备注信息"),
* @OA\Response(
* response="200",
* description="退款"
* )
* )
*/
public function refundForOrder($id)
{
DB::beginTransaction();
try {
$order = (new Orders())->find($id);
//创建退款记录
$refund = [
"customer_id" => $order->customer->id,
"manager_id" => $this->manager->id,
"money" => request()->money,
"order_id" => $order->id,
"remark" => request()->remark
];
switch (request()->payment) {
case "cash":
$refund["payment"] = request()->payment;
$refund["paid_at"] = date("Y-m-d H:i:s");
break;
case "online":
$recharge = Recharge::find(request()->recharge_id);
$refund["payment"] = $recharge->payment;
$refund["recharge_id"] = request()->recharge_id;
break;
default:
return response()->json([
"errorcode" => "0",
"errormsg" => "不正确的退款方式"
]);
}
$refund = (new Refund())->create($refund);
$refund = $refund->getSerial();
//更新用户余额,创建流水记录
$customer_balance = $refund->customer->balance - $refund->money;
$refund->customer->update([
"balance" => $customer_balance
]);
$balance = (new Balance())->create([
"customer_id" => $refund->customer->id,
"order_id" => $refund->order_id,
"belongs_type" => get_class($refund),
"belongs_id" => $refund->id,
"money" => -$refund->money,
"balance" => $customer_balance
]);
DB::commit();
return response()->json([
"refund" => $refund,
"balance" => $balance
]);
} catch (\Exception $exception) {
DB::rollBack();
return response()->json([
"errorcode" => $exception->getCode(),
"errormsg" => $exception->getMessage()
]);
}
}
/**
* @OA\Get(
* path="/manager/get-project-paramedic-levels/{project_id}",
* summary="V2-获取医院护工等级",
* description="获取医院护工等级",
* @OA\Parameter(name="token", in="query", @OA\Schema(type="string"), required=true, description="token"),
* @OA\Parameter(name="project_id", in="query", @OA\Schema(type="string"), required=true, description="医院ID"),
* @OA\Response(
* response="200",
* description="获取医院护工等级"
* )
* )
*/
public function getProjectParamedicLevels($project_id)
{
$paramedic_levels = ParamedicLevel::where("project_id", $project_id)->orderBy("myindex")->get();
return response()->json($paramedic_levels->toArray());
}
}