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

5 years ago
<?php
namespace App\Http\Controllers\Manager;
use AlicFeng\IdentityCard\InfoHelper;
use App\Customer;
5 years ago
use App\Events\OrderAssigned;
5 years ago
use App\Libs\AlipayF2F;
5 years ago
use App\Libs\WxMicroPay;
5 years ago
use App\Models\Area;
5 years ago
use App\Models\Balance;
5 years ago
use App\Models\Bed;
5 years ago
use App\Models\OrderItems;
5 years ago
use App\Models\Orders;
5 years ago
use App\Models\ParamedicLevel;
5 years ago
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;
5 years ago
use Illuminate\Support\Facades\Log;
5 years ago
use Intervention\Image\Facades\Image;
use SimpleSoftwareIO\QrCode\Facades\QrCode;
class OrdersController extends CommonController
{
/**
* @OA\Get(
* path="/manager/get-projects",
5 years ago
* summary="V2 获取医院列表",
* description="获取医院列表",
5 years ago
* @OA\Parameter(name="token", in="query", @OA\Schema(type="string"), required=true, description="token"),
* @OA\Response(
* response="200",
5 years ago
* description="获取医院列表"
5 years ago
* )
* )
*/
public function getProjects()
{
5 years ago
$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();
5 years ago
return response()->json($projects->toArray());
}
/**
* @OA\Get(
5 years ago
* path="/manager/get-care-product/{project_id}",
* summary="V2-获取产品详情",
5 years ago
* description="获取产品详情",
* @OA\Parameter(name="token", in="query", @OA\Schema(type="string"), required=true, description="token"),
5 years ago
* @OA\Parameter(name="project_id", in="path", @OA\Schema(type="integer"), required=true, description="project_id"),
5 years ago
* @OA\Response(
* response="200",
* description="获取产品详情"
* )
* )
*/
5 years ago
public function getCareProduct($project_id)
5 years ago
{
5 years ago
DB::enableQueryLog();
$product = (new Product())->where("project_id", $project_id)->with([
"productItems" => function ($query) {
$query->select("id", "name", "product_id", "patient_quantity", "price");
},
5 years ago
"productParamedicLevels" => function ($query) {
5 years ago
$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");
5 years ago
},
"factors" => function ($query) {
5 years ago
$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();
5 years ago
return response()->json($product->toArray());
}
/**
* @OA\Get(
5 years ago
* path="/manager/get-project-orders-count/{project_id}",
* summary="V2-获取医院订单数量角标",
* description="获取医院订单数量角标",
5 years ago
* @OA\Parameter(name="token", in="query", @OA\Schema(type="string"), required=true, description="token"),
5 years ago
* @OA\Parameter(name="project_id", in="query", @OA\Schema(type="integer"), required=true, description="医院id"),
5 years ago
* @OA\Response(
* response="200",
5 years ago
* description="获取医院订单数量角标"
5 years ago
* )
* )
*/
5 years ago
public function getProjectOrdersCount($project_id)
5 years ago
{
$orders_count = [];
5 years ago
$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();
5 years ago
5 years ago
return response()->json(compact("orders_count"));
5 years ago
}
/**
* @OA\Get(
5 years ago
* path="/manager/get-projcet-orders/{project_id}",
* summary="V2-获取订单列表",
5 years ago
* description="获取订单列表",
* @OA\Parameter(name="token", in="query", @OA\Schema(type="string"), required=true, description="token"),
5 years ago
* @OA\Parameter(name="project_id", in="path", @OA\Schema(type="integer"), required=false, description="医院ID"),
5 years ago
* @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"),
5 years ago
* @OA\Parameter(name="page_size", in="query", @OA\Schema(type="integer"), required=false, description="每页数量默认为5"),
5 years ago
* @OA\Parameter(name="status", in="query", @OA\Schema(type="string"), required=false, description="订单状态:[pending=>待处理,ongoing=>进行中,finished=>已完成]"),
5 years ago
* @OA\Response(
* response="200",
* description="获取订单列表"
* )
* )
*/
5 years ago
public function list($project_id)
5 years ago
{
5 years ago
$model = $this->_getOrderModel();
5 years ago
$model = $model->ofProject($project_id);
5 years ago
if (request()->keyword) {
$keyword = request()->keyword;
5 years ago
$model = $model->where(function ($query) use ($keyword) {
5 years ago
$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) {
5 years ago
case "pending":
$model = $model->whereIn("status", [Orders::STATUS_UNCONFIRMED, Orders::STATUS_UNASSIGNED]);
5 years ago
break;
case "ongoing":
case "finished":
5 years ago
$model = $model->where("status", constant(Orders::class . "::STATUS_" . strtoupper(request()->status)));
5 years ago
break;
default:
5 years ago
//do nothing
5 years ago
}
5 years ago
$page_size = request()->page_size ?? 5;
5 years ago
$data = $model->orderBy("id", "desc")->paginate($page_size);
5 years ago
foreach ($data as $order) {
$order = $order->refreshTotal();
5 years ago
$order->balance = $order->customer->balance;
5 years ago
}
return response()->json($data->toArray());
}
/**
* @OA\Get(
* path="/manager/get-order/{id}",
5 years ago
* summary="V2-获取订单详情",
5 years ago
* 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)
{
5 years ago
$model = $this->_getOrderModel();
5 years ago
$order = $model->with([
5 years ago
"orderItems",
5 years ago
"recharges",
"refunds"
5 years ago
])->find($id);
5 years ago
$order = $order->refreshTotal();
$order->balance = $order->customer->balance;
return response()->json($order->toArray());
}
5 years ago
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",
5 years ago
"orders.product_item_id",
"orders.product_paramedic_level_id",
5 years ago
"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([
5 years ago
"productItem" => function ($query) {
$query->select("id", "name");
},
"paramedicLevel" => function ($query) {
$query->select("paramedic_level.id", "paramedic_level.name");
},
5 years ago
"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) {
5 years ago
$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");
5 years ago
}
]);
return $model;
}
5 years ago
/**
* @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(
5 years ago
* path="/manager/get-area-beds/{area_id}",
5 years ago
* 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());
}
5 years ago
/**
* @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();
5 years ago
return response()->json($paramedics);
5 years ago
}
5 years ago
/**
* @OA\POST(
* path="/manager/create-patient",
5 years ago
* summary="V2-创建被护理人",
5 years ago
* 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,
5 years ago
"age" => request()->age,
"mobile" => request()->mobile
5 years ago
];
$vo = (new Patient())->create($data);
return response()->json($vo);
}
/**
* @OA\POST(
* path="/manager/create-order",
5 years ago
* summary="V2-创建订单",
5 years ago
* 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"),
5 years ago
* @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="联系人电话"),
5 years ago
* @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([
5 years ago
"errorcode" => "101",
5 years ago
"errormsg" => "客户名下有即将开始或正在进行中的订单,暂不可以再次下单"
]);
}
5 years ago
$product = (new Product())->find(request()->product_id);
5 years ago
$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([
5 years ago
"errorcode" => "102",
5 years ago
"errormsg" => "协商价格不能低于系统指导价"
]);
}
5 years ago
$patient = (new Patient())->firstOrCreate([
5 years ago
"customer_id" => $customer->id,
5 years ago
"name" => request()->patient_name
5 years ago
]);
$patient->update([
"age" => request()->patient_age,
"sex" => request()->patient_sex,
5 years ago
"mobile" => request()->patient_mobile,
5 years ago
]);
5 years ago
$order = (new Orders())->create([
"customer_id" => $customer->id,
5 years ago
"project_id" => $product->id,
5 years ago
"product_id" => request()->product_id,
5 years ago
"patient_id" => $patient->id,
"contact" => request()->contact ?? request()->patient_name,
"mobile" => request()->mobile ?? request()->patient_mobile,
5 years ago
"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),
5 years ago
"status" => request()->paramedic_id ? Orders::STATUS_ONGOING : Orders::STATUS_UNASSIGNED,
5 years ago
"manager_id" => $this->manager->id
]);
$order->getSerial();
5 years ago
if (request()->paramedic_id) {
event(new OrderAssigned($order));
}
5 years ago
DB::commit();
return $this->getOrder($order->id);
} catch (\Exception $exception) {
DB::rollBack();
return response()->json([
"errorcode" => $exception->getCode(),
"errormsg" => $exception->getMessage()
]);
}
}
5 years ago
/**
* @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()
]);
}
}
5 years ago
/**
* @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",
5 years ago
* 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
* }
* )
* )
* }
5 years ago
* )
* )
*/
public function updateOrderItems(Request $request)
{
5 years ago
$ids = explode(",", $request->ids);
$order_items = (new OrderItems())->whereHas("order")->whereIn("id", $ids)->with("customer")->get();
5 years ago
if (!$order_items->count()) {
return response()->json([
"errorcode" => "105",
"errormsg" => "没有获取到子订单"
]);
}
5 years ago
$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() . "天已付款的子订单进行价格更改";
5 years ago
}
5 years ago
if ($total_increased > $customer->balance) {
5 years ago
$errors[] = "价格补差{$total_increased}超过了客户余额{$customer->balance},请先充值";
5 years ago
}
//todo:价格低于指导价的提示
5 years ago
if (count($errors)) {
return response()->json([
"errorcode" => "105",
5 years ago
"errormsg" => implode("", $errors)
5 years ago
]);
}
if (count($errors) && !$request->skip_warnings) {
return response()->json([
"has_warnings" => true,
5 years ago
"errormsg" => implode("", $warnings)
5 years ago
]);
}
DB::beginTransaction();
try {
5 years ago
//根据已付款的价格异动子订单循环添加余额平衡表记录,并随时更新客户余额值,执行完毕后保存客户余额更新
5 years ago
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
]);
5 years ago
}
5 years ago
$customer->save();
5 years ago
//循环更新所有子订单
$update = [];
if ($request->has("bed_id")) {
$update["bed_id"] = $request->bed_id;
}
5 years ago
if ($request->has("paramedic_id")) {
5 years ago
$update["paramedic_id"] = $request->paramedic_id;
}
if ($request->has("price")) {
$update["total"] = $request->price;
}
foreach ($order_items as $order_item) {
$order_item->update($update);
}
5 years ago
DB::commit();
5 years ago
return response()->json([
"updated_items" => count($order_items),
"updated_paid_items" => count($price_changed_paid_items)
]);
5 years ago
} catch (\Exception $exception) {
DB::rollBack();
return response()->json([
"errorcode" => $exception->getCode(),
"errormsg" => $exception->getMessage()
]);
}
}
5 years ago
/**
5 years ago
* @OA\POST(
5 years ago
* 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,
5 years ago
"payment" => request()->type,
"manager_id" => $this->manager->id
5 years ago
]);
$recharge = $recharge->getSerial();
switch (request()->type) {
case "weixin":
5 years ago
$res = (new WxMicroPay())->pay($recharge);
5 years ago
if ($res === true) {
return response()->json(true);
} else {
5 years ago
return response()->json([
"errorcode" => 60003,
5 years ago
"errormsg" => $res->getMessage()
5 years ago
]);
}
break;
5 years ago
case "alipay":
5 years ago
$res = (new AlipayF2F())->pay($recharge);
5 years ago
if ($res["status"]) {
return response()->json(true);
} else {
5 years ago
return response()->json([
"errorcode" => $res["code"],
"errormsg" => $res["msg"]
]);
}
5 years ago
break;
default:
return response()->json([
"errorcode" => 60003,
"errormsg" => "不正确的支付方式"
]);
5 years ago
}
}
5 years ago
/**
* @OA\POST(
5 years ago
* path="/manager/assign-order/{id}",
* summary="V2-派单",
* description="派单",
5 years ago
* @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"),
5 years ago
* @OA\Parameter(name="paramedic_id", in="query", @OA\Schema(type="integer"), required=true, description="护工id"),
5 years ago
* @OA\Response(
* response="200",
5 years ago
* description="派单"
5 years ago
* )
* )
*/
5 years ago
public function assignOrder($id)
5 years ago
{
5 years ago
$order = (new Orders())->find($id);
5 years ago
if (!in_array($order->status, [Orders::STATUS_UNCONFIRMED, Orders::STATUS_UNASSIGNED])) {
5 years ago
return response()->json([
"errorcode" => 50001,
"errormsg" => "订单状态不适配"
]);
}
DB::beginTransaction();
try {
5 years ago
$order->update([
"paramedic_id" => request()->paramedic_id,
"status" => Orders::STATUS_ONGOING
]);
event(new OrderAssigned($order));
5 years ago
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}",
5 years ago
* summary="V2-取消订单",
5 years ago
* 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}",
5 years ago
* summary="V2-现金或pos刷卡充值",
5 years ago
* 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}",
5 years ago
* summary="V2-退款",
5 years ago
* 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="金额,非负数"),
5 years ago
* @OA\Parameter(name="payment", in="query", @OA\Schema(type="string"), required=true, description="退款方式枚举cashonline。注意可用的退款方式需要通过pre-chackout获取随意提交金额可能会造成问题"),
5 years ago
* @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()
]);
}
}
5 years ago
/**
* @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());
}
5 years ago
}