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

1071 lines
38 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Models\Appointment;
use App\Models\Hospital;
use App\Models\HospitalExtra;
use App\Models\NoProfitsharing;
use App\Models\ProfitsharingActionLog;
use App\Models\ProfitsharingLog;
use App\Models\UserAccount;
use App\Models\UserOrder;
use App\Models\UserPerson;
use App\Models\WeChatPay;
use App\Models\WeChatRefund;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Yo;
use Login;
use Lu;
use Illuminate\Support\Str;
class UserOrderController extends Controller
{
public function check_order(Request $request)
{
$password = $request->post('password');
if ($password != env('INTERNAL_PASSWORD')) Yo::error_echo(200078);
$code = $request->post('appid');
$hospital_info = Hospital::where('code', $code)->where('del', 2)->first();
if (!$hospital_info) Yo::error_echo(100000, ['医院']);
$appointment_number = $request->post('serve_id');
$user_order = UserOrder::where('appointment_number', $appointment_number)->first();
if (!$user_order) Yo::error_echo(100000, ['订单']);
$exam_number = $request->post('exam_id', '');
if (!!$exam_number) {
// 到检
if ($user_order->status != 2) Yo::error_echo(200080);
if ($user_order->check_status == 2) Yo::error_echo(200079);
$user_order->exam_number = $exam_number;
$user_order->check_status = 2;
$user_order->check_time = date('Y-m-d H:i:s');
$user_order->status = 4;
$user_order->save();
if ($user_order->price == $user_order->true_price) {
$hospital_sharing_content = self::sharing_config($user_order->hospital, false);
if ($hospital_sharing_content['open'] == 1
|| $hospital_sharing_content['open'] == 3) {
$slc = ProfitsharingLog::where('order', $user_order->id)->first();
if (!$slc) {
$p = new ProfitsharingController();
$sharing = $p->sharing($user_order->id, $hospital_sharing_content['hospital']);
$receivers = [];
$sharing_map = [];
$np = NoProfitsharing::where('hospital', $user_order->hospital)->orderBy('id', 'desc')->get();
$mp_items = [];
foreach ($np as $item) {
$mp_items[] = $item->item_id;
}
$price = 0;
$hold = 0;
$buy_info = json_decode($user_order->buy_info, true);
if ($buy_info['combo']['id'] != 0) {
foreach ($buy_info['combo']['items'] as $item) {
if ($item['discount_type'] == '打折') {
$item_price = $item['price'] * $item['discount_value'];
} else {
$item_price = $item['price'] - $item['discount_value'];
}
$item_price = floor($item_price * 100) / 100;
if (!in_array($item['id'], $mp_items)) {
$price += $item_price;
} else {
$hold += $item_price;
}
}
}
foreach ($buy_info['items'] as $item) {
$item_price = $item['price'];
if (!in_array($item['id'], $mp_items)) {
$price += $item_price;
} else {
$hold += $item_price;
}
}
foreach ($sharing as $item) {
$formula = $item->formula;
$amount = $p->eval_action($formula, [
'price' => $price, // 参与分账的金额
'hold' => $hold, // 不参与分账的金额
]);
$receivers[] = [
'type' => $item->type == 1 ? 'MERCHANT_ID' : 'PERSONAL_OPENID',
'account' => $item->account,
'amount' => intval(bcmul($amount, 100, 0)),
'description' => $item->desc
];
$sharing_map[] = [
'amount' => $amount,
'formula' => $formula,
];
$pal = new ProfitsharingActionLog();
$pal->hospital = $item->hospital;
$pal->order = $user_order->id;
$pal->pro_id = $item->id;
$pal->formula = $formula;
$pal->price = $price;
$pal->hold = $hold;
$pal->money = $amount;
$pal->save();
}
if (count($receivers) != 0) {
if ($hospital_sharing_content['open'] == 1) {
$sharing_data = [
'transaction_id' => $user_order->transaction,
'receivers' => $receivers
];
$hospital_extra_content = self::pay_config($user_order->hospital);
$wcp = new WeChatPayController();
$builder_config = [
'appid' => env('WX_APP_ID'),
'pem_path' => base_path() . $hospital_extra_content['wxp']['key'],
'cer_path' => base_path() . $hospital_extra_content['wxp']['crt'],
'cer_num' => $hospital_extra_content['wxp']['number'],
'mchid' => $hospital_extra_content['wxp']['id'],
'v3' => $hospital_extra_content['wxp']['v3'],
];
$wcp->builder($builder_config);
$res = $wcp->profitsharing($sharing_data);
} else {
$builder_config = [];
$res = [
'hold' => $hold,
'price' => $price,
];
}
$sl = new ProfitsharingLog();
$sl->hospital = $user_order->hospital;
$sl->type = $hospital_sharing_content['open'];
$sl->order = $user_order->id;
$sl->builder = json_encode($builder_config, JSON_UNESCAPED_UNICODE);
$sl->receivers = json_encode($receivers, JSON_UNESCAPED_UNICODE);
$sl->sharing = json_encode($sharing_map, JSON_UNESCAPED_UNICODE);
$sl->response = json_encode($res, JSON_UNESCAPED_UNICODE);
$sl->recover = '{}';
$sl->status = 1;
$sl->save();
}
}
}
}
} else {
// 删除
$user_order->status = 3;
$user_order->save();
$appointment = Appointment::where('id', $user_order->appointment)->first();
if (!!$appointment) {
$appointment->used_count -= 1;
$appointment->save();
}
}
return Yo::update_echo($appointment_number);
}
public function done_test(Request $request)
{
$id = $request->post('id');
$user_order = UserOrder::where('id', $id)->first();
if (!$user_order) Yo::error_echo(100000, ['订单']);
$hospital_sharing_content = self::sharing_config($user_order->hospital, false);
$receivers = [];
$sharing_map = [];
$price = 0;
$hold = 0;
if ($hospital_sharing_content['open'] == 1
|| $hospital_sharing_content['open'] == 3) {
$p = new ProfitsharingController();
$sharing = $p->sharing($user_order->id, $hospital_sharing_content['hospital']);
$np = NoProfitsharing::where('hospital', $user_order->hospital)->orderBy('id', 'desc')->get();
$mp_items = [];
foreach ($np as $item) {
$mp_items[] = $item->item_id;
}
$price = 0;
$hold = 0;
$buy_info = json_decode($user_order->buy_info, true);
if ($buy_info['combo']['id'] != 0) {
foreach ($buy_info['combo']['items'] as $item) {
if ($item['discount_type'] == '打折') {
$item_price = $item['price'] * $item['discount_value'];
} else {
$item_price = $item['price'] - $item['discount_value'];
}
$item_price = floor($item_price * 100) / 100;;
if (!in_array($item['id'], $mp_items)) {
$price += $item_price;
} else {
$hold += $item_price;
}
}
}
foreach ($buy_info['items'] as $item) {
$item_price = $item['price'];
if (!in_array($item['id'], $mp_items)) {
$price += $item_price;
} else {
$hold += $item_price;
}
}
foreach ($sharing as $item) {
$formula = $item->formula;
$amount = $p->eval_action($formula, [
'price' => $price, // 参与分账的金额
'hold' => $hold, // 不参与分账的金额
]);
$receivers[] = [
'type' => $item->type == 1 ? 'MERCHANT_ID' : 'PERSONAL_OPENID',
'account' => $item->account,
'amount' => intval(bcmul($amount, 100, 0)),
'description' => $item->desc
];
$sharing_map[] = [
'amount' => $amount,
'formula' => $formula,
];
}
}
return Yo::update_echo([
'$hold' => $hold,
'$price' => $price,
'a' => $receivers,
'b' => $sharing_map,
]);
}
public function create_appointment($hospital, $data)
{
$peis = new PEISApiController();
$url_code = $data['type'] == 1 ? '个检预约' : '团检预约';
unset($data['type']);
$info = $peis::Post($url_code, $hospital, $data);
return $info;
}
public function exam_info($hospital, $data)
{
$peis = new PEISApiController();
$info = $peis::Post('体检报告查询', $hospital, $data);
return $info;
}
public function cancel_appointment($hospital, $data)
{
$peis = new PEISApiController();
$url_code = $data['type'] == 1 ? '个检预约取消' : '团检预约取消';
unset($data['type']);
$info = $peis::Post($url_code, $hospital, $data);
return $info;
}
public function appointment_info($hospital, $data)
{
$peis = new PEISApiController();
$url_code = $data['type'] == 1 ? '个检预约查询' : '团检预约查询';
unset($data['type']);
$info = $peis::Post($url_code, $hospital, $data);
return $info;
}
public function item_check($hospital, $data)
{
$peis = new PEISApiController();
$check = $peis::Post('套餐项目检查', $hospital, $data);
return $check;
}
public function time_check($hospital, $data)
{
$peis = new PEISApiController();
$check = $peis::Post('预约时间检查', $hospital, $data);
return $check;
}
public function create(Request $request)
{
Login::user();
$cic = new ComboItemController();
$hospital = $request->post('hospital');
$combo = $request->post('combo');
$person = $request->post('person');
if (count($person) == 0) Yo::error_echo(200044);
$person_info = UserPerson::where('id', $person[0]['id'])
->where('user', Login::$info->id)
->where('del', 2)->first();
if (!$person_info) Yo::error_echo(100000, ['人员信息']);
$price = 0;
$buy_info = [
'combo' => [
'id' => 0,
'name' => '自选项目',
'price' => 0,
'items' => []
],
'items' => [],
'group' => [
'id' => '',
],
];
if (!!$combo) {
$combo_info = $cic->combo_info($hospital, [
'套餐Id' => $combo,
]);
$price += $combo_info['price'];
$buy_info['combo'] = [
'id' => $combo_info['id'],
'name' => $combo_info['name'],
'price' => $combo_info['price'],
'items' => $combo_info['items'],
];
}
$items = $request->post('items');
$items_list = [];
if (count($items) != 0) {
$items_list = $cic->items($hospital, [
'价格下限' => "0",
'价格上限' => "999999",
"性别" => null,
"妇检" => null,
"套餐Id" => null,
"项目Id列表" => $items
]);
}
foreach ($items_list as $item) {
$price += $item['price'];
$buy_info['items'][] = [
'id' => $item['id'],
'name' => $item['name'],
'price' => $item['price']
];
}
$group = $request->post('group');
$group_info = false;
if (!!$group) {
$cic = new ComboItemController();
$group_info = $cic->group_info($hospital, [
"电话号码" => null,
"证件号码" => null,
"预约Id" => $group
]);
$buy_info['group'] = [
'id' => $group,
'combo_name' => $group_info['combo_name'],
'combo_id' => $group_info['combo_id'],
'group_name' => $group_info['group_name'],
'items' => $group_info['items'],
'appointment_number' => $group_info['appointment_number'],
];
}
$time = $request->post('time');
$time_info = Appointment::where('id', $time['id'])->where('status', 1)->where('del', 2)->first();
if (!$time_info) Yo::error_echo(200046);
if ($time_info->max_count <= $time_info->used_count) {
$time_info = false;
}
$check_items = [];
foreach ($items as $item) {
$check_items[] = [
'Id' => $item
];
}
if (!!($combo == 0 ? null : $combo) || count($check_items) != 0) {
$item_check = self::item_check($hospital, [
'人员信息列表' => [[
"序号" => 0,
"性别" => $person_info->sex == 1 ? '男' : '女',
"年龄" => floor((time() - strtotime($person_info->birthday)) / 86400 / 360),
"婚姻状态" => $person_info->marriage == 1 ? '已婚' : '未婚',
]],
'套餐Id' => $combo == 0 ? null : $combo,
'可选项目信息' => $check_items,
]);
if (count($item_check['data']) != 1) {
Yo::error_exit(Lu::exit([
'code' => 200045,
'message' => $item_check['message'],
'data' => [],
]));
}
}
// $time_check = self::time_check($hospital, [
// '索引信息列表' => [[
// "序号" => 0,
// "电话号码" => $person_info->phone,
// "证件号码" => $person_info->id_number,
// ]],
// '预约时间' => $time_info->date . ' ' . $time_info->start_time,
// ]);
// if (count($time_check['data']) != 1) Yo::error_echo(200047);
if (!!$group_info) {
if (strtotime($group_info['start_time'] . ' 00:00:00') > strtotime($time_info->date . ' ' . $time_info->start_time)) Yo::error_echo(200047);
if (strtotime($group_info['end_time'] . ' 23:59:59') < strtotime($time_info->date . ' ' . $time_info->end_time)) Yo::error_echo(200047);
}
$true_price = $price;
if (Login::$info->pay > 0 && $true_price > 0) {
$true_price = Login::$info->pay;
}
$referral = $request->post('referral');
$order = new UserOrder();
$order->user = Login::$info->id;
$order->hospital = $hospital;
$order->name = $person_info->name;
$order->id_number = $person_info->id_number;
$order->phone = $person_info->phone;
$order->birthday = $person_info->birthday;
$order->sex = $person_info->sex;
$order->marriage = $person_info->marriage;
$order->buy_info = json_encode($buy_info, JSON_UNESCAPED_UNICODE);
$order->type = ($combo == 0 && !!$group) ? 2 : 1;
$order->combo = $combo;
$order->price = $price;
$order->true_price = $true_price;
$order->group_price = 0;
$order->status = 1;
$order->referral = $referral;
$order->appointment = $time_info->id;
$order->appointment_time = $time_info->date . ' ' . $time_info->start_time;
$order->appointment_number = !!$group ? $group : '';
$order->show = '';
$order->save();
$show = date('ymd') . str_pad(($order->id % 100000000), 8, "0", STR_PAD_LEFT);
$order->show = $show;
$order->save();
$time_info->used_count += 1;
$time_info->save();
return Yo::create_echo($order->id);
}
public function mp_list(Request $request)
{
Login::user();
$status = $request->post('status');
$list = UserOrder::select('*')
->selectRaw("IFNULL((select name from hospitals where hospitals.id = user_orders.hospital),'') as hospital_name")
->selectRaw("IFNULL((select address from hospitals where hospitals.id = user_orders.hospital),'') as address")
->selectRaw("IFNULL((select longitude from hospitals where hospitals.id = user_orders.hospital),'') as longitude")
->selectRaw("IFNULL((select latitude from hospitals where hospitals.id = user_orders.hospital),'') as latitude")
->where('user', Login::$info->id)
->where(function ($query) use ($status) {
if ($status != 0) $query->where('status', $status);
})
->orderBy('id', 'desc')->get();
return Yo::echo(['list' => $list]);
}
public function mp_info(Request $request)
{
Login::user();
$id = $request->post('id');
$order_info = UserOrder::where('id', $id)
->where('user', Login::$info->id)
->orderBy('id', 'desc')->first();
if (!$order_info) Yo::error_echo(100000, ['订单']);
if ($order_info->status == 1 && $order_info->pay_type == 1) {
$hospital_extra_content = self::pay_config($order_info->hospital);
$wcp = new WeChatPayController();
$wcp->builder([
'appid' => env('WX_APP_ID'),
'pem_path' => base_path() . $hospital_extra_content['wxp']['key'],
'cer_path' => base_path() . $hospital_extra_content['wxp']['crt'],
'cer_num' => $hospital_extra_content['wxp']['number'],
'mchid' => $hospital_extra_content['wxp']['id'],
'v3' => $hospital_extra_content['wxp']['v3'],
]);
$check = $wcp->check($order_info->show);
if ($check['trade_state'] == 'SUCCESS') {
$we_chat_pay = WeChatPay::where('out_trade_no', $order_info->show)->first();
if (!!$we_chat_pay) {
$we_chat_pay->callback = json_encode([
'type' => 'auto',
'input' => '',
'headers' => '',
'res' => $check,
], JSON_UNESCAPED_UNICODE);
$we_chat_pay->save();
}
$ret = self::done($order_info->id, $check['transaction_id']);
if ($ret == 'status') {
$order_info = UserOrder::where('id', $id)
->where('user', Login::$info->id)
->orderBy('id', 'desc')->first();
}
}
}
return Yo::echo(['info' => $order_info]);
}
public function user_cancel(Request $request)
{
Login::user();
$id = $request->post('id');
$order_info = UserOrder::where('id', $id)
->where('user', Login::$info->id)
->orderBy('id', 'desc')->first();
if (!$order_info) Yo::error_echo(100000, ['订单']);
if ($order_info->status != 1) Yo::error_echo(200077);
$appointment = Appointment::where('id', $order_info->appointment)->first();
if (!!$appointment) {
$appointment->used_count -= 1;
$appointment->save();
}
$order_info->status = 3;
$order_info->save();
return Yo::update_echo($order_info->id);
}
public function refund(Request $request)
{
Login::user();
$id = $request->post('id');
$order_info = UserOrder::where('id', $id)
->where('user', Login::$info->id)
->orderBy('id', 'desc')->first();
if (!$order_info) Yo::error_echo(100000, ['订单']);
if ($order_info->status != 2) Yo::error_echo(200051);
if ($order_info->check_status == 2) Yo::error_echo(200052);
$res = self::appointment_info($order_info->hospital, [
'type' => $order_info->type,
"证件号码" => null,
"电话号码" => null,
"预约Id" => $order_info->appointment_number,
]);
if (count($res['data']) < 1) Yo::error_echo(200052);
if ($res['data'][0]['当前状态'] !== '未登记') Yo::error_echo(200052);
switch ($order_info->pay_type) {
case 1:
$hospital_extra_content = self::pay_config($order_info->hospital);
$wcp = new WeChatPayController();
$wcp->builder([
'appid' => env('WX_APP_ID'),
'pem_path' => base_path() . $hospital_extra_content['wxp']['key'],
'cer_path' => base_path() . $hospital_extra_content['wxp']['crt'],
'cer_num' => $hospital_extra_content['wxp']['number'],
'mchid' => $hospital_extra_content['wxp']['id'],
'v3' => $hospital_extra_content['wxp']['v3'],
]);
$out_trade_no = $order_info->show;
$wcr = new WeChatRefund();
$post_data = [
'transaction_id' => $order_info->transaction,
'out_refund_no' => $out_trade_no,
'total' => $order_info->true_price * 100,
];
$wcr->out_trade_no = $out_trade_no;
$wcr->post_data = json_encode($post_data, JSON_UNESCAPED_UNICODE);
$pay = $wcp->refund($post_data);
$wcr->params = json_encode($pay, JSON_UNESCAPED_UNICODE);
$wcr->save();
if (isset($pay['code'])) Yo::error_echo(200076);
if ($pay['status'] != 'PROCESSING' && $pay['status'] != 'SUCCESS') Yo::error_echo(200053);
$order_info->status = 5;
$order_info->refund = $wcr->id;
$order_info->refund_time = date('Y-m-d H:i:s');
$order_info->save();
break;
case 2:
break;
case 3:
$order_info->status = 5;
$order_info->refund_time = date('Y-m-d H:i:s');
$order_info->save();
break;
}
self::cancel_appointment($order_info->hospital, [
'type' => $order_info->type,
'预约Id' => $order_info->appointment_number
]);
$appointment = Appointment::where('id', $order_info->appointment)->first();
if (!!$appointment) {
$appointment->used_count -= 1;
$appointment->save();
}
$order_info->appointment_number = '';
$order_info->save();
return Yo::echo(['info' => $order_info]);
}
public function exam(Request $request)
{
$number = $request->post('number');
$hospital = $request->post('hospital');
$info = self::exam_info($hospital, [
"电话号码" => "",
"证件号码" => "",
"体检号" => $number
]);
return Yo::echo(['info' => $info]);
}
public function cancel(Request $request)
{
$number = $request->post('number');
$hospital = $request->post('hospital');
$info = self::cancel_appointment($hospital, [
'type' => 2,
'预约Id' => $number
]);
return Yo::echo(['info' => $info]);
}
public function done($id, $transaction = '')
{
$order_info = UserOrder::where('id', $id)->first();
if (!$order_info) Yo::error_echo(100000, ['订单']);
if ($order_info->status != 1) return 'status';
$buy_info = json_decode($order_info->buy_info, true);
$check_items = [];
foreach ($buy_info['items'] as $item) {
$check_items[] = [
'Id' => $item['id'],
'价格' => $item['price']
];
}
$combo = $order_info->combo;
if (!!($combo == 0 ? null : $combo) || count($check_items) != 0) {
$item_check = self::item_check($order_info->hospital, [
'人员信息列表' => [[
"序号" => 0,
"性别" => $order_info->sex == 1 ? '男' : '女',
"年龄" => floor((time() - strtotime($order_info->birthday)) / 86400 / 360),
"婚姻状态" => $order_info->marriage == 1 ? '已婚' : '未婚',
]],
'套餐Id' => $order_info->combo == 0 ? null : $order_info->combo,
'可选项目信息' => $check_items,
]);
if (count($item_check['data']) != 1) {
Yo::error_exit([
'code' => 200045,
'message' => $item_check['message'],
'data' => [],
]);
}
}
// $time_check = self::time_check($order_info->hospital, [
// '索引信息列表' => [[
// "序号" => 0,
// "电话号码" => $order_info->phone,
// "证件号码" => $order_info->id_number,
// ]],
// '预约时间' => $order_info->appointment_time,
// ]);
// if (count($time_check['data']) != 1) Yo::error_echo(200047);
if ($order_info->type == 1) {
$pay_status = true;
if ($order_info->pay_type == 3 && $order_info->price > 0) $pay_status = false;
$cad = [
'type' => 1,
"姓名" => $order_info->name,
"性别" => $order_info->sex == 1 ? '男' : '女',
"年龄" => floor((time() - strtotime($order_info->birthday)) / 86400 / 360),
"婚姻状态" => $order_info->marriage == 1 ? '已婚' : '未婚',
"证件号码" => $order_info->id_number,
"电话号码" => $order_info->phone,
"预约时间" => $order_info->appointment_time,
"介绍人" => $order_info->referral,
'套餐Id' => $order_info->combo == 0 ? null : $order_info->combo,
"可选项目信息" => $check_items,
"已收费" => $pay_status,
"总计金额" => $order_info->price,
];
} else {
$cad = [
'type' => 2,
"预约Id" => $order_info->appointment_number,
"预约时间" => $order_info->appointment_time,
'套餐Id' => null,
"可选项目信息" => $check_items
];
}
$create_appointment = self::create_appointment($order_info->hospital, $cad);
$order_info->transaction = $transaction;
$order_info->status = 2;
$order_info->pay_time = date('Y-m-d H:i:s');
if ($create_appointment['code'] != 0) Yo::error_echo(200050, [$create_appointment['message']]);
$order_info->appointment_number = $create_appointment['data'][0];
$appointment_info = self::appointment_info($order_info->hospital, [
'type' => $order_info->type,
"证件号码" => null,
"电话号码" => null,
"预约Id" => $order_info->appointment_number,
]);
$order_info->appointment_info = json_encode($appointment_info['data'][0], JSON_UNESCAPED_UNICODE);
$order_info->save();
return $order_info;
}
public function pay_config($hospital, $error = true)
{
$mark = 'pay';
$hospital_extra = HospitalExtra::where('hospital', $hospital)->where('mark', $mark)->first();
$hospital_extra_public = HospitalExtra::where('hospital', 0)->where('mark', $mark)->first();
$hospital_extra_public_content = json_decode($hospital_extra_public->content, true);
if (!!$hospital_extra) {
$hospital_extra_content = json_decode($hospital_extra->content, true);
$hospital_extra_content['wxp'] = $hospital_extra_content['wxp']['open'] == 0 ? $hospital_extra_public_content['wxp'] : $hospital_extra_content['wxp'];
} else {
$hospital_extra_content = $hospital_extra_public_content;
}
if ($hospital_extra_content['wxp']['open'] == 0) $hospital_extra_content['wxp']['open'] = 2;
if ($error && $hospital_extra_content['wxp']['open'] == 2) Yo::error_echo(200047);
return $hospital_extra_content;
}
public function sharing_config($hospital, $error = true)
{
$mark = 'profitsharing';
$hospital_extra = HospitalExtra::where('hospital', $hospital)->where('mark', $mark)->first();
$hospital_extra_public = HospitalExtra::where('hospital', 0)->where('mark', $mark)->first();
$hospital_extra_public_content = json_decode($hospital_extra_public->content, true);
if (!!$hospital_extra) {
$hospital_extra_content = json_decode($hospital_extra->content, true);
if ($hospital_extra_content['open'] == 0) {
$hospital_extra_content = [
'open' => $hospital_extra_public_content['open'],
'hospital' => 0
];
} else {
$hospital_extra_content = [
'open' => $hospital_extra_content['open'],
'hospital' => $hospital
];
}
} else {
$hospital_extra_content = [
'open' => $hospital_extra_public_content['open'],
'hospital' => 0
];;
}
if ($hospital_extra_content['open'] == 0) $hospital_extra_content['open'] = 2;
return $hospital_extra_content;
}
public function admin_list(Request $request)
{
Login::admin([], [26, 30]);
$hospital = $request->post('hospital');
if (Login::$info->hospital != 0) {
if ($hospital != Login::$info->hospital) {
Yo::error_echo(100000, ['机构/医院']);
}
}
$start_time = $request->post('start_time');
$end_time = $request->post('end_time');
$time = $request->post('time');
$search = $request->post('search');
$status = $request->post('status');
// $list = UserOrder::select('*')
// ->where('hospital', $hospital);
$list = UserOrder::select('user_orders.*', 'profitsharing_logs.receivers','profitsharing_logs.status as sharing_status','profitsharing_logs.response as sharing_response')
->where('user_orders.hospital', $hospital)
->leftjoin('profitsharing_logs', 'user_orders.id', '=', 'profitsharing_logs.order');
if ($status == 2) {
$list = $list->where('status', 2)->where('refund_time', null);
} elseif ($status == 7) {
$list = $list->where('status', 2)->whereNotNull('refund_time');
} elseif ($status == 6) {
$list = $list->where('status', 2)->where('refund_time', null)->where('check_status', 2);
} else {
$list = $list->where(function ($query) use ($status) {
if ($status != 0) $query->where('status', $status);
});
}
$list = $list->where(function ($query) use ($search) {
if ($search != '') $query->where('name', $search)
->orWhere('id_number', $search)
->orWhere('phone', $search)
->orWhere('show', $search);
});
if (!!$start_time) {
switch ($time) {
case 1:
$list->where('created_at', '>=', $start_time);
break;
case 2:
$list->where('pay_time', '>=', $start_time);
break;
case 3:
$list->where('appointment_time', '>=', $start_time);
break;
}
}
if (!!$end_time) {
switch ($time) {
case 1:
$list->where('created_at', '<=', $end_time);
break;
case 2:
$list->where('pay_time', '<=', $end_time);
break;
case 3:
$list->where('appointment_time', '<=', $end_time);
break;
}
}
$list=$list->orderBy('id', 'desc')->paginate(10);
foreach ($list as $key=>$value){
$items=[];
$buy_info=json_decode($value->buy_info,true);
foreach ($buy_info['items'] as $k=>$v){
$items[]=$v['id'];
}
foreach ($buy_info['combo']['items'] as $k=>$v){
$items[]=$v['id'];
}
$hold_item=DB::table('no_profitsharings')->where('hospital',$hospital)->whereIn('item_id',$items)->get();
$list[$key]['hold_items']=$hold_item;
}
return Yo::echo($list);
}
public function callback($id)
{
$input = file_get_contents('php://input');
$headers = request()->header();
$hospital_extra_content = self::pay_config($id);
$wcp = new WeChatPayController();
$res = $wcp->callback($input, $headers, $hospital_extra_content['wxp']['v3'], 'file://' . base_path() . $hospital_extra_content['wxp']['crt']);
if (!!$res) {
if ($res['trade_state'] == 'SUCCESS') {
$order_info = UserOrder::where('show', $res['out_trade_no'])->where('status', 1)->first();
if (!!$order_info) {
$we_chat_pay = WeChatPay::where('out_trade_no', $res['out_trade_no'])->first();
if (!!$we_chat_pay) {
$we_chat_pay->callback = json_encode([
'type' => 'callback',
'input' => $input,
'headers' => $headers,
'res' => $res,
], JSON_UNESCAPED_UNICODE);
$we_chat_pay->save();
}
self::done($order_info->id, $res['transaction_id']);
}
}
}
return Lu::exit([
'code' => 'SUCCESS',
'message' => '成功',
]);
}
public function pay(Request $request)
{
Login::user();
$order = $request->post('order');
$order_info = UserOrder::where('id', $order)->where('user', Login::$info->id)->where('status', 1)->first();
if (!$order_info) Yo::error_echo(100000, ['订单']);
if (strtotime($order_info->created_at) + 60 * 60 * 2 <= time()) Yo::error_echo(200049);
$buy_info = json_decode($order_info->buy_info, true);
$check_items = [];
foreach ($buy_info['items'] as $item) {
$check_items[] = ['Id' => $item['id']];
}
$combo = $order_info->combo;
if (!!($combo == 0 ? null : $combo) || count($check_items) != 0) {
$item_check = self::item_check($order_info->hospital, [
'人员信息列表' => [[
"序号" => 0,
"性别" => $order_info->sex == 1 ? '男' : '女',
"年龄" => floor((time() - strtotime($order_info->birthday)) / 86400 / 360),
"婚姻状态" => $order_info->marriage == 1 ? '已婚' : '未婚',
]],
'套餐Id' => $order_info->combo == 0 ? null : $order_info->combo,
'可选项目信息' => $check_items,
]);
if (count($item_check['data']) != 1) {
Yo::error_exit([
'code' => 200045,
'message' => $item_check['message'],
'data' => [],
]);
}
}
// $time_check = self::time_check($order_info->hospital, [
// '索引信息列表' => [[
// "序号" => 0,
// "电话号码" => $order_info->phone,
// "证件号码" => $order_info->id_number,
// ]],
// '预约时间' => $order_info->appointment_time,
// ]);
// if (count($time_check['data']) != 1) Yo::error_echo(200047);
$pay_type = $request->post('pay_type');
if ($order_info->true_price == 0) $pay_type = 3;
if ($order_info->true_price > 0 && $pay_type == 3) $pay_type = 1;
$hospital_extra_content = self::pay_config($order_info->hospital, false);
$hospital_sharing_content = self::sharing_config($order_info->hospital, false);
if ($pay_type == 1 && $hospital_extra_content['wxp']['open'] == 2) $pay_type = 3;
switch ($pay_type) {
case 1:
$we_chat_pay = WeChatPay::where('out_trade_no', $order_info->show)->first();
if (!!$we_chat_pay) {
$params = json_decode($we_chat_pay->params, true);
$pay = [
'appid' => $params['appId'],
'timestamp' => $params['timeStamp'],
'nonce_str' => $params['nonceStr'],
'package' => $params['package'],
'pay_sign' => $params['paySign'],
'sign_type' => $params['signType'],
];
} else {
$wcp = new WeChatPayController();
$wcp->builder([
'appid' => env('WX_APP_ID'),
'pem_path' => base_path() . $hospital_extra_content['wxp']['key'],
'cer_path' => base_path() . $hospital_extra_content['wxp']['crt'],
'cer_num' => $hospital_extra_content['wxp']['number'],
'mchid' => $hospital_extra_content['wxp']['id'],
'v3' => $hospital_extra_content['wxp']['v3'],
]);
$out_trade_no = $order_info->show;
$user_account_info = UserAccount::where('user', Login::$info->id)->first();
$pay = $wcp->create([
'description' => '体检预约',
'out_trade_no' => $out_trade_no,
'notify_url' => env('APP_URL') . '/api/Pay/Wxp/callback/' . $order_info->hospital,
'total' => $order_info->true_price * 100,
'openid' => $user_account_info->account,
'profit_sharing' => $hospital_sharing_content['open'] == 1,
]);
$order_info->pay_type = 1;
$order_info->save();
}
return Yo::echo([
'action' => 'wxp',
'pay' => $pay
]);
case 2:
Yo::error_echo(200047);
case 3:
$order_info->pay_type = 3;
$order_info->save();
self::done($order_info->id);
return Yo::echo([
'action' => 'done',
'pay' => false,
]);
}
}
public function admin_cancel(Request $request)
{
Login::admin([], [26, 30]);
$id = $request->post('id');
$type = $request->post('type');
$order_info = UserOrder::where('id', $id)->first();
if (!$order_info) Yo::error_echo(100000, ['订单']);
$hospital = $order_info->hospital;
if (Login::$info->hospital != 0) {
if ($hospital != Login::$info->hospital) {
Yo::error_echo(100000, ['机构/医院']);
}
}
if (!$order_info->appointment_number) Yo::error_echo(200077);
if ($order_info->check_status == 2) Yo::error_echo(200052);
$order_info->status = 3;
switch ($type) {
case 1:
// 取消预约
if ($order_info->status == 1) Yo::error_echo(200077);
self::cancel_appointment($order_info->hospital, [
'type' => $order_info->type,
'预约Id' => $order_info->appointment_number
]);
$order_info->appointment_number = '';
break;
case 2:
// 取消订单
break;
}
$appointment = Appointment::where('id', $order_info->appointment)->first();
if (!!$appointment) {
$appointment->used_count -= 1;
$appointment->save();
}
$order_info->save();
return Yo::update_echo();
}
public function admin_refund(Request $request)
{
Login::admin([], [26, 30]);
$id = $request->post('id');
$type = $request->post('type');
$order_info = UserOrder::where('id', $id)->first();
if (!$order_info) Yo::error_echo(100000, ['订单']);
$hospital = $order_info->hospital;
if (Login::$info->hospital != 0) {
if ($hospital != Login::$info->hospital) {
Yo::error_echo(100000, ['机构/医院']);
}
}
if ($order_info->status != 2) Yo::error_echo(200075);
if ($order_info->check_status == 2) Yo::error_echo(200052);
if ($order_info->pay_type != 1) Yo::error_echo(200075);
$cancel = false;
switch ($type) {
case 1:
// 仅退款
break;
case 2:
// 退款&取消预约
$cancel = true;
break;
}
$hospital_extra_content = self::pay_config($order_info->hospital);
$wcp = new WeChatPayController();
$wcp->builder([
'appid' => env('WX_APP_ID'),
'pem_path' => base_path() . $hospital_extra_content['wxp']['key'],
'cer_path' => base_path() . $hospital_extra_content['wxp']['crt'],
'cer_num' => $hospital_extra_content['wxp']['number'],
'mchid' => $hospital_extra_content['wxp']['id'],
'v3' => $hospital_extra_content['wxp']['v3'],
]);
$out_trade_no = $order_info->show;
$wcr = new WeChatRefund();
$post_data = [
'transaction_id' => $order_info->transaction,
'out_refund_no' => $out_trade_no,
'total' => $order_info->true_price * 100,
];
$wcr->admin = Login::$info->id;
$wcr->out_trade_no = $out_trade_no;
$wcr->post_data = json_encode($post_data, JSON_UNESCAPED_UNICODE);
$pay = $wcp->refund($post_data);
$wcr->params = json_encode($pay, JSON_UNESCAPED_UNICODE);
$wcr->save();
if (isset($pay['code'])) Yo::error_echo(200076);
if ($pay['status'] != 'PROCESSING' && $pay['status'] != 'SUCCESS') Yo::error_echo(200053);
$order_info->refund = $wcr->id;
$order_info->refund_time = date('Y-m-d H:i:s');
if ($cancel) {
self::cancel_appointment($order_info->hospital, [
'type' => $order_info->type,
'预约Id' => $order_info->appointment_number
]);
$appointment = Appointment::where('id', $order_info->appointment)->first();
if (!!$appointment) {
$appointment->used_count -= 1;
$appointment->save();
}
$order_info->appointment_number = '';
$order_info->status = 5;
}
$order_info->save();
return Yo::update_echo();
}
}