123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847 |
- <?php
- namespace app\admin\controller\order;
- use app\admin\controller\notify\RechargeController;
- use app\admin\service\goods\GoodsService;
- use app\admin\service\goods\GoodsSkuService;
- use app\admin\service\member\MemberService;
- use app\controller\Curd;
- use app\model\Goods;
- use app\model\GoodsSku;
- use app\model\Member;
- use app\model\MemberAccount;
- use app\model\Order;
- use app\model\OrderSheet;
- use app\model\PayDetail;
- use app\model\SysDept;
- use support\Db;
- use support\exception\BusinessException;
- use support\Log;
- use support\Request;
- use support\Response;
- use Tinywan\Jwt\JwtToken;
- use Webman\Event\Event;
- use function PHPSTORM_META\type;
- class PayDetailController extends Curd
- {
- public function __construct()
- {
- $this->model = new PayDetail();
- }
- public function select(Request $request): Response
- {
- [$where, $format, $limit, $field, $order] = $this->selectInput($request);
- $order = $request->get('order', 'desc');
- $field = $field ?? 'pay_addtimes';
- // $where['pay_status'] = 'SUCCESS';
- if ($where['pay_category'] == 'OTHER') {
- $where['pay_status'] = 'SUCCESS';
- $where['pay_category'] = ['not in', ['RECHARGE', 'REFUND', 'ROLE_REFUND', 'CROWN_CONTINUE', 'CLEAR', 'GIVE']];
- } elseif ($where['pay_category'] == 'RECHARGE') {
- $where['pay_category'] = ['in', ['RECHARGE', 'VIP']];
- }
- if (!empty($where['pay_addtimes'])) {
- $where['pay_addtimes'][0] = !empty($where['pay_addtimes'][0]) ? strtotime($where['pay_addtimes'][0]) : '';
- $where['pay_addtimes'][1] = !empty($where['pay_addtimes'][1]) ? strtotime($where['pay_addtimes'][1]) : '';
- }
- if (!empty($request->get('premises_id'))) {
- $orderIds = Order::whereJsonContains('order_extend_json->dept_id', intval($request->get('premises_id')))
- ->whereIn('order_classify', ['RECHARGE', 'COMBINE', 'PARTNER', 'RECHARGE'])
- ->where('order_status_payment', 'SUCCESS')
- ->orWhereJsonContains('order_extend_json->referee_dept_id', intval($request->get('premises_id')))
- ->pluck('order_id')
- ->toArray();
- $orderIdsStr = "'" . implode("','", $orderIds) . "'";
- $payIds = PayDetail::whereRaw("JSON_UNQUOTE(JSON_EXTRACT(join_pay_object_json,'$.order_id')) in ({$orderIdsStr})")
- ->orWhereRaw("JSON_UNQUOTE(JSON_EXTRACT(join_pay_object_json,'$.recharge_order_id')) in ({$orderIdsStr})")
- ->pluck('pay_id')
- ->toArray();
- $where['pay_id'] = '';
- if (!empty($payIds)) {
- $where['pay_id'] = ['in', $payIds];
- }
- }
- $query = $this->doSelect($where, $field, $order, $request->get());
- return $this->doFormat($query, $format, $limit);
- }
- protected function doSelect(array $where, string $field = null, string $order = 'desc', $params = [])
- {
- $model = $this->model->with(['member', 'cert', 'memberAccount']);
- foreach ($where as $column => $value) {
- if (is_array($value)) {
- if ($value[0] === 'like' || $value[0] === 'not like') {
- $model = $model->where($column, $value[0], "%$value[1]%");
- } elseif (in_array($value[0], ['>', '=', '<', '<>'])) {
- $model = $model->where($column, $value[0], $value[1]);
- } elseif ($value[0] == 'in' && !empty($value[1])) {
- $valArr = $value[1];
- if (is_string($value[1])) {
- $valArr = explode(",", trim($value[1]));
- }
- $model = $model->whereIn($column, $valArr);
- } elseif ($value[0] == 'not in' && !empty($value[1])) {
- $valArr = $value[1];
- if (is_string($value[1])) {
- $valArr = explode(",", trim($value[1]));
- }
- $model = $model->whereNotIn($column, $valArr);
- } elseif ($value[0] == 'null') {
- $model = $model->whereNull($column);
- } elseif ($value[0] == 'not null') {
- $model = $model->whereNotNull($column);
- } elseif ($value[0] !== '' || $value[1] !== '') {
- $model = $model->whereBetween($column, $value);
- }
- } else {
- $model = $model->where($column, $value);
- }
- }
- if (empty($params['type'])) {
- $model = $model->where(function ($query) {
- $query->whereIn('pay_prepayid', ['ALIPAY', 'WXPAY', 'OFFLINE', 'OFFLINE_WXPAY', 'OFFLINE_ALIPAY', 'MONEY'])
- ->orWhere('pay_prepayid', 'like', '%CASH%')
- ->orWhere('pay_prepayid', 'like', '%WELFARE%');
- });
- } else {
- if ($params['type'] == 'NORMAL') {
- $model = $model->where(function ($query) {
- $query->whereIn('pay_prepayid', ['ALIPAY', 'WXPAY', 'OFFLINE_WXPAY', 'OFFLINE_ALIPAY', 'MONEY'])
- ->orWhere('pay_prepayid', 'like', '%CASH%');
- });
- } else if ($params['type'] == 'VIP') {
- $model = $model->where(function ($query) {
- $query->orWhere('pay_category', 'VIP');
- });
- } else {
- $model = $model->where(function ($query) {
- $query->orWhere('pay_prepayid', 'like', '%WELFARE%');
- });
- }
- }
- if (!empty($params['order_type'])) {
- if ($params['order_type'] == 'COMBINE') {
- $model = $model->where('join_pay_order_id', 'COMBINE');
- } elseif ($params['order_type'] == 'PARTNER') {
- $model = $model->where('join_pay_order_id', 'PARTNER');
- } elseif ($params['order_type'] == 'VIP') {
- $model = $model->where('pay_category', 'VIP');
- } elseif ($params['order_type'] == 'RECHARGE') {
- $model = $model->where('join_pay_order_id', '<>', 'PARTNER')
- ->where('join_pay_order_id', '<>', 'COMBINE')
- ->where('pay_category', '<>', 'VIP');
- }
- }
- if ($field) {
- $model = $model->orderBy($field, $order);
- }
- return $model;
- }
- public function selectWelfare(Request $request): Response
- {
- [$where, $format, $limit, $field, $order] = $this->selectInput($request);
- $order = $request->get('order', 'desc');
- $field = $field ?? 'pay_addtimes';
- // $where['pay_status'] = 'SUCCESS';
- if ($where['pay_category'] == 'OTHER') {
- $where['pay_status'] = 'SUCCESS';
- $where['pay_category'] = ['<>', 'RECHARGE'];
- }
- // else if ($where['pay_category'] == 'RECHARGE') {
- // $where['pay_status'] = 'SUCCESS';
- // $where['pay_category'] = 'RECHARGE';
- // $where['pay_prepayid'] = ['in','WXPAY,ALIPAY'];
- // }
- $query = $this->doSelectWelfare($where, $field, $order);
- return $this->doFormat($query, $format, $limit);
- }
- protected function doSelectWelfare(array $where, string $field = null, string $order = 'desc')
- {
- $model = $this->model->with(['member', 'cert', 'memberAccount']);
- foreach ($where as $column => $value) {
- if (is_array($value)) {
- if ($value[0] === 'like' || $value[0] === 'not like') {
- $model = $model->where($column, $value[0], "%$value[1]%");
- } elseif (in_array($value[0], ['>', '=', '<', '<>'])) {
- $model = $model->where($column, $value[0], $value[1]);
- } elseif ($value[0] == 'in' && !empty($value[1])) {
- $valArr = $value[1];
- if (is_string($value[1])) {
- $valArr = explode(",", trim($value[1]));
- }
- $model = $model->whereIn($column, $valArr);
- } elseif ($value[0] == 'not in' && !empty($value[1])) {
- $valArr = $value[1];
- if (is_string($value[1])) {
- $valArr = explode(",", trim($value[1]));
- }
- $model = $model->whereNotIn($column, $valArr);
- } elseif ($value[0] == 'null') {
- $model = $model->whereNull($column);
- } elseif ($value[0] == 'not null') {
- $model = $model->whereNotNull($column);
- } elseif ($value[0] !== '' || $value[1] !== '') {
- $model = $model->whereBetween($column, $value);
- }
- } else {
- $model = $model->where($column, $value);
- }
- }
- $model = $model->where(function ($query) {
- $query->Where('pay_prepayid', 'like', '%WELFARE%');
- });
- if ($field) {
- $model = $model->orderBy($field, $order);
- }
- return $model;
- }
- public function afterQuery($items)
- {
- foreach ($items as &$item) {
- $prepayId = explode('-', $item->pay_prepayid);
- $item->order_classify = $item->pay_category;
- if (count($prepayId) > 1) {
- $item->pay_prepayid = $prepayId[1];
- }
- $item->type = 'NORMAL';
- if (isset($prepayId[1]) && $prepayId[1] == 'WELFARE') {
- $item->type = 'WELFARE';
- } else if ($item->pay_category == 'VIP') {
- $item->type = 'VIP';
- }
- $memberAccount = [];
- if (!empty($item->memberAccount)) {
- foreach ($item->memberAccount as $account) {
- $memberAccount[strtolower($account->member_account_classify)] = $account;
- }
- $item->memberAccount = $memberAccount;
- }
- $addedAmount = 0;
- if (!empty($item->pay_extend_json)) {
- $payExtendJson = json_decode($item->pay_extend_json, true);
- if (isset($payExtendJson['remark'])) {
- $item->service = $payExtendJson['remark']['service'] ?? '';
- $item->business = $payExtendJson['remark']['business'] ?? '';
- $item->premises_id = $payExtendJson['remark']['premises_id'] ?? '';
- $item->premises_name = $payExtendJson['remark']['premises_name'] ?? '';
- }
- if (!empty($payExtendJson['added_amount'])) {
- $addedAmount = sprintf('%.2f', $payExtendJson['added_amount']);
- }
- }
- $payObjectJson = [];
- if (!empty($item->join_pay_object_json)) {
- $payObjectJson = json_decode($item->join_pay_object_json, true);
- }
- $originPayAmount = $item->pay_amount;
- if (in_array($item['join_pay_order_id'], ['COMBINE', 'PARTNER']) && !empty($item->join_pay_object_json)) {
- $item->order_classify = $item['join_pay_order_id'];
- if (isset($payObjectJson['recharge_order_id'])) {
- $payDetails = PayDetail::whereJsonContains('join_pay_object_json->order_id', $payObjectJson['recharge_order_id'])
- ->where('pay_status', 'SUCCESS')
- ->get()
- ->toArray();
- if (count($payDetails) > 1) {
- $item->pay_prepayid = 'BY_STAGES';
- } else if (count($payDetails) == 1) {
- $item->pay_prepayid = $payDetails[0]['pay_prepayid'];
- }
- $item->pay_amount = PayDetail::whereJsonContains('join_pay_object_json->order_id', $payObjectJson['recharge_order_id'])
- ->where('pay_status', 'SUCCESS')
- ->sum('pay_amount');
- $orderStatus = OrderSheet::where('join_sheet_order_id', $payObjectJson['recharge_order_id'])->value('order_sheet_status');
- if ($orderStatus == 'BEING') {
- $item->pay_status = 'BEING';
- }
- }
- }
- if (!empty($payObjectJson['recharge_config']) && $payObjectJson['recharge_config']['nbr'] > 0) {
- if ($item->join_pay_order_id == 'PARTNER') {
- $addedAmount = sprintf('%.2f', $payObjectJson['recharge_config']['nbr']);
- } else {
- $addedAmount = sprintf('%.2f', round($item->pay_amount * $payObjectJson['recharge_config']['nbr'], 2));
- }
- }
- $item->added_amount = $addedAmount;
- $item->to_account_amount = sprintf('%.2f', $originPayAmount + $addedAmount);
- if ($item->pay_category == 'VIP') {
- $payObjectJson = json_decode($item->join_pay_object_json, true);
- if (isset($payObjectJson['order_id'])) {
- $orderStatus = OrderSheet::where('join_sheet_order_id', $payObjectJson['order_id'])->value('order_sheet_status');
- if ($orderStatus == 'BEING') {
- $item->pay_status = 'BEING';
- }
- }
- }
- }
- return $items;
- }
- public function statistics(Request $request)
- {
- $type = $request->get('type');
- $orderType = $request->get('order_type');
- $premisesId = $request->get('premises_id');
- $memberId = $request->get('join_pay_member_id');
- $payAddtimes = $request->get('pay_addtimes');
- if (!empty($payAddtimes)) {
- $payAddtimes[0] = strtotime($payAddtimes[0]);
- $payAddtimes[1] = strtotime($payAddtimes[1]);
- }
- $payDetailModel = PayDetail::where('pay_status', 'SUCCESS')
- ->where('pay_category', 'RECHARGE')
- ->select(Db::raw("SUM(CASE WHEN join_pay_order_id='COMBINE' THEN 1000 ELSE pay_amount END) as amount, COUNT(pay_id) as total"));
- // ->when(!empty($type), function ($query) use ($type) {
- // if ($type == 'NORMAL') {
- // $query->whereIn('pay_prepayid', ['ALIPAY', 'WXPAY', 'OFFLINE_WXPAY', 'OFFLINE_ALIPAY', 'MONEY'])
- // ->orWhere('pay_prepayid', 'like', '%CASH%');
- // } else if ($type == 'VIP') {
- // $query->where('pay_category', 'VIP');
- // } else {
- // $query->where('pay_prepayid', 'like', '%WELFARE%');
- // }
- // })->when(!empty($orderType), function ($query) use ($orderType) {
- // if (in_array($orderType, ['COMBINE', 'PARTNER'])) {
- // $query->where('join_pay_order_id', $orderType);
- // } elseif ($orderType == 'VIP') {
- // $query->where('pay_category', 'VIP');
- // } elseif ($orderType == 'RECHARGE') {
- // $query->where('join_pay_order_id', '<>', 'PARTNER')
- // ->where('join_pay_order_id', '<>', 'COMBINE')
- // ->where('pay_category', '<>', 'VIP');
- // }
- // })->when(!empty($premisesId), function ($query) use ($premisesId) {
- // $orderIds = Order::whereJsonContains('order_extend_json->dept_id', intval($premisesId))
- // ->whereIn('order_classify', ['RECHARGE', 'COMBINE', 'PARTNER', 'RECHARGE'])
- // ->where('order_status_payment', 'SUCCESS')
- // ->orWhereJsonContains('order_extend_json->referee_dept_id', intval($premisesId))
- // ->pluck('order_id')
- // ->toArray();
- // $orderIdsStr = "'" . implode("','", $orderIds) . "'";
- // $payIds = PayDetail::whereRaw("JSON_UNQUOTE(JSON_EXTRACT(join_pay_object_json,'$.order_id')) in ({$orderIdsStr})")
- // ->orWhereRaw("JSON_UNQUOTE(JSON_EXTRACT(join_pay_object_json,'$.recharge_order_id')) in ({$orderIdsStr})")
- // ->pluck('pay_id')
- // ->toArray();
- // $query->whereIn('pay_id', $payIds);
- // })->when(!empty($memberId), function ($query) use ($memberId) {
- // $query->where('join_pay_member_id', $memberId);
- // })->when(!empty($payAddtimes),function ($query) use ($payAddtimes){
- // $query->whereBetween('pay_addtimes',$payAddtimes);
- // });
- $totalModel = clone $payDetailModel;
- $total = $totalModel->first()->toArray();
- $todayUnixStart = strtotime(date('Y-m-d'));
- $todayUnixEnd = strtotime(date('Y-m-d 23:59:59'));
- $todayTotalModel = clone $payDetailModel;
- $todayTotal = $todayTotalModel->whereBetween('pay_addtimes', [$todayUnixStart, $todayUnixEnd])
- ->first()
- ->toArray();
- $yesterdayStart = strtotime(date('Y-m-d', strtotime("-1 days")));
- $yesterdayEnd = strtotime(date('Y-m-d 23:59:59', strtotime("-1 days")));
- $yesterdayTotalModel = clone $payDetailModel;
- $yesterdayTotal = $yesterdayTotalModel->whereBetween('pay_addtimes', [$yesterdayStart, $yesterdayEnd])
- ->first()
- ->toArray();
- $monthStart = strtotime(date('Y-m-01'));
- $monthEnd = strtotime(date('Y-m-t 23:59:59'));
- $monthTotalModel = clone $payDetailModel;
- $monthTotal = $monthTotalModel->whereBetween('pay_addtimes', [$monthStart, $monthEnd])
- ->first()
- ->toArray();
- return json_success('success', [
- 'total' => $total,
- 'todayTotal' => $todayTotal,
- 'yesterdayTotal' => $yesterdayTotal,
- 'monthTotal' => $monthTotal
- ]);
- }
- /**
- * @Desc 产品订单
- * @Author Gorden
- * @Date 2024/6/5 10:10
- *
- * @param Request $request
- * @return Response
- */
- public function goodsOrder(Request $request)
- {
- $orderId = $request->get('order_id', '');
- $memberId = $request->get('member_id', '');
- if (!$orderId || !$memberId) {
- return json_fail('参数异常');
- }
- $orderSheet = OrderSheet::with([
- 'order' => function ($query) {
- $query->select('order_id', 'order_amount_total', 'order_amount_pay', 'order_is_complete', 'order_category', 'order_status_system', 'order_status_payment', 'order_status_storage', 'order_extend_json', 'order_addtimes');
- },
- 'goods' => function ($query) {
- $query->select('goods_id', 'goods_name', 'goods_status', 'goods_cover', 'goods_market_price', 'goods_sales_price', 'goods_addtimes');
- },
- 'sku' => function ($query) {
- $query->select('goods_sku_id', 'goods_sku_specs_json', 'goods_sku_market_price', 'goods_sku_sales_price');
- }
- ])->where('join_sheet_order_id', $orderId)
- ->get()
- ->toArray();
- $member = Member::with([
- 'cert' => function ($query) {
- $query->select('join_cert_member_id', 'member_cert_name');
- },
- 'info' => function ($query) {
- $query->select('join_info_member_id', 'member_info_nickname', 'member_info_headimg');
- }
- ])->where('member_id', $memberId)
- ->select('member_id', 'member_mobile', 'join_member_role_id', 'member_is_owner', 'member_is_vip', 'member_is_partner', 'member_is_referrer')
- ->first();
- if (!$member) {
- return json_fail('数据异常');
- }
- $member = $member->toArray();
- $certName = $member['cert']['member_cert_name'] ?? '';
- $nickname = $member['info']['member_info_nickname'] ?? '';
- $mobile = $member['member_mobile'] ?? '';
- $member['member_name'] = MemberService::getMemberCertName($mobile, $certName, $nickname);
- $member['info']['member_info_headimg'] = MemberService::getAvatarUrl($member['info']['member_info_headimg'] ?? '');
- $member['level'] = MemberService::getRoleName($member['join_member_role_id']);
- $order = [];
- $payDetails = PayDetail::whereJsonContains('join_pay_object_json->order_id', $orderId)
- ->where('pay_status', 'SUCCESS')
- ->select('pay_status', 'pay_amount', 'pay_paytimes', 'pay_prepayid', 'join_pay_object_json', 'pay_extend_json')
- ->orderBy('pay_paytimes', 'DESC')
- ->get()
- ->toArray();
- foreach ($orderSheet as &$item) {
- if (!empty($item['goods']) && !empty($item['goods']['goods_cover'])) {
- $item['goods']['goods_cover'] = getenv("STORAGE_DOMAIN") . $item['goods']['goods_cover'];
- }
- if (!empty($item['sku']) && !empty($item['sku']['goods_sku_specs_json'])) {
- $item['sku']['goods_sku_title'] = GoodsSkuService::getSkuTitle($item['sku']['goods_sku_specs_json']);
- }
- if (!empty($item['order'])) {
- if (!empty($item['order']['order_extend_json'])) {
- $orderExtendJson = json_decode($item['order']['order_extend_json'], true);
- if (!empty($orderExtendJson['referee'])) {
- $item['order']['referee'] = $orderExtendJson['referee'];
- }
- if (!empty($orderExtendJson['referee_premises'])) {
- $item['order']['referee_premises'] = $orderExtendJson['referee_premises'];
- }
- }
- }
- $order = $item['order'];
- }
- $order['added_amount'] = 0;
- foreach ($payDetails as $detail) {
- if (!empty($detail['join_pay_object_json'])) {
- $payObjectJson = json_decode($detail['join_pay_object_json'], true);
- if (!empty($payObjectJson['recharge_config']) && $payObjectJson['recharge_config']['nbr'] > 0) {
- if ($detail['join_pay_order_id'] == 'PARTNER') {
- $order['added_amount'] = sprintf('%.2f', $payObjectJson['recharge_config']['nbr']);
- } else {
- $order['added_amount'] = sprintf('%.2f', round($detail['pay_amount'] * $payObjectJson['recharge_config']['nbr'], 2));
- }
- // $order['added_amount'] = sprintf('%.2f',round($detail['pay_amount'] * $payObjectJson['recharge_config']['nbr'], 2));
- }
- }
- if ($order['added_amount'] == 0 && !empty($detail['pay_extend_json'])) {
- $payExtendJson = json_decode($detail['pay_extend_json'], true);
- if (!empty($payExtendJson['added_amount'])) {
- $order['added_amount'] = sprintf('%.2f', $payExtendJson['added_amount']);
- }
- }
- }
- $data = [
- 'payDetails' => $payDetails,
- 'goodsOrder' => $orderSheet,
- 'member' => $member,
- 'order' => $order
- ];
- return json_success('', $data);
- }
- /**
- * 余额账户充值
- */
- public function insertRecharge(Request $request): Response
- {
- if ($this->validate && !$this->validateClass->scene('add')->check($request->post())) {
- return json_fail($this->validateClass->getError());
- }
- $password = $request->post('recharge_password', '');
- if (!$password || $password != '123456') {
- return json_fail('充值密码错误');
- }
- Db::beginTransaction();
- try {
- $data = $this->insertRechargeInput($request);
- $data['pay_prepayid'] = $request->post('pay_prepayid', '');
- unset($data['orderId']);
- $payDetailId = $this->doInsert($data);
- (new RechargeController)->disposePaySuccess($payDetailId);
- Db::commit();
- } catch (BusinessException $customException) {
- Db::rollBack();
- _syslog('充值', '后台充值失败', false, $request->all());
- Log::error("后台充值失败", ['msg' => $customException->getMessage()]);
- return json_fail($customException->getMessage());
- } catch (\Exception $e) {
- Db::rollBack();
- _syslog('充值', '后台充值失败', false, $request->all());
- Log::error("后台充值失败", ['msg' => $e->getMessage()]);
- return json_fail('数据写入失败');
- }
- _syslog('充值', '后台充值成功', false, $request->all());
- return json_success('success');
- }
- /**
- * 余额账户赠送
- */
- public function insertGive(Request $request): Response
- {
- if ($this->validate && !$this->validateClass->scene('add')->check($request->post())) {
- return json_fail($this->validateClass->getError());
- }
- $password = $request->post('recharge_password', '');
- if (!$password || $password != '123456') {
- return json_fail('赠送密码错误');
- }
- Db::beginTransaction();
- try {
- $data = $this->insertGiveInput($request);
- $data['pay_prepayid'] = $data['join_pay_member_id'] . '-CASH';
- // 金额累加到福利账户
- $memberAccount = MemberAccount::where('join_account_member_id', $data['join_pay_member_id'])->where('member_account_classify', 'CASH')->first();
- $memberAccount->member_account_income = $memberAccount->member_account_income + $data['pay_amount'];
- $memberAccount->member_account_surplus = $memberAccount->member_account_surplus + $data['pay_amount'];
- $memberAccount->member_account_update_user_id = JwtToken::getCurrentId();
- $memberAccount->member_account_updatetimes = time();
- $memberAccount->save();
- $data['pay_amount'] = 0;
- $this->doInsert($data);
- Db::commit();
- } catch (BusinessException $customException) {
- Db::rollBack();
- return json_fail($customException->getMessage());
- } catch (\Exception $e) {
- Db::rollBack();
- dump($e->getMessage());
- return json_fail('数据写入失败11');
- }
- return json_success('success');
- }
- /**
- * 福利账户充值
- */
- public function insertRechargeWelfare(Request $request): Response
- {
- if ($this->validate && !$this->validateClass->scene('add')->check($request->post())) {
- return json_fail($this->validateClass->getError());
- }
- $password = $request->post('recharge_password', '');
- if (!$password || $password != '123456') {
- return json_fail('充值密码错误');
- }
- Db::beginTransaction();
- try {
- $data = $this->insertRechargeInput($request, 'WELFARE');
- $orderId = $data['orderId'];
- unset($data['orderId']);
- $data['pay_prepayid'] = $data['join_pay_member_id'] . '-WELFARE';
- $memberAccount = MemberAccount::where('join_account_member_id', $data['join_pay_member_id'])
- ->where('member_account_classify', 'WELFARE')
- ->first();
- if ($memberAccount->member_account_status != 'ACTIVED') {
- throw new BusinessException('福利账户未开启');
- }
- $this->doInsert($data);
- // 金额累加到福利账户
- $memberAccount->member_account_income = $memberAccount->member_account_income + $data['pay_amount'];
- $memberAccount->member_account_surplus = $memberAccount->member_account_surplus + $data['pay_amount'];
- $memberAccount->member_account_update_user_id = JwtToken::getCurrentId();
- $memberAccount->member_account_updatetimes = time();
- $memberAccount->save();
- Db::commit();
- // 入收支明细表
- $params['orderId'] = $orderId;
- $params['inout_category'] = '会员充值';
- Event::dispatch('statistics.inout.in', $params);
- } catch (BusinessException $customException) {
- Db::rollBack();
- return json_fail($customException->getMessage());
- } catch (\Exception $e) {
- Db::rollBack();
- dump($e->getMessage());
- return json_fail('数据写入失败11');
- }
- return json_success('success');
- }
- /**
- * @Desc
- * @Author Gorden
- * @Date 2024/6/5 13:39
- *
- * @param Request $request
- * @return array
- * @throws BusinessException
- */
- protected function insertRechargeInput(Request $request, $type = "CASH"): array
- {
- $params = $request->post();
- $data = $this->inputFilter($params);
- $goods = Goods::find($params['goods_id']);
- $extendJson = [];
- $orderExtendJson = [];
- // if (!$params['is_custom']) {
- // Db::beginTransaction();
- try {
- if (!$params['is_custom']) {
- $data['pay_amount'] = $goods->goods_sales_price;
- }
- $orderExtendJson['referee_dept_id'] = $params['dept_id'] ?? '';
- $orderExtendJson['referee'] = $params['referee'] ?? '';
- if (!empty($params['dept_id'])) {
- $orderExtendJson['referee_premises'] = SysDept::where('dept_id', $params['dept_id'])->value('dept_name');
- }
- $orderId = "OD" . date('ymdHi') . random_string(4, 'up');
- $orderGroupId = "OD" . date('ymdHi') . random_string(4, 'up');
- $orderData = [
- 'order_id' => $orderId,
- 'order_groupby' => $orderGroupId,
- 'join_order_member_id' => $data['join_pay_member_id'],
- 'order_name' => date('Y-m-d H:i:s') . '-订单',
- 'order_amount_total' => $data['pay_amount'],
- 'order_amount_pay' => $data['pay_amount'],
- 'order_amount_json' => '{"tax": 0, "freight": 0}',
- 'order_status_payment' => 'SUCCESS',
- 'order_classify' => 'RECHARGE',
- 'order_category' => 'RECHARGE',
- 'order_status_system' => 'PAYING',
- 'order_status_storage' => 'PENDING',
- 'order_config_json' => json_encode(['order_category' => 'RECHARGE']),
- 'order_extend_json' => json_encode($orderExtendJson),
- 'order_platform' => 'SYSTEM',
- 'order_addtimes' => time()
- ];
- if ($type == 'WELFARE') {
- $orderData['order_is_complete'] = 'Y';
- $orderData['order_status_system'] = 'DONE';
- $orderData['order_status_storage'] = 'DONE';
- unset($orderData['order_config_json']);
- }
- $sku = GoodsSku::where('join_sku_goods_id', $params['goods_id'])->first();
- Order::insert($orderData);
- $orderSheetData = [
- 'join_sheet_member_id' => $data['join_pay_member_id'],
- 'join_sheet_order_id' => $orderId,
- 'join_sheet_goods_id' => $params['goods_id'],
- 'join_sheet_goods_sku_id' => $sku ? $sku->goods_sku_id : 0,
- 'order_sheet_status' => 'DONE',
- 'order_sheet_category' => 'RECHARGE',
- 'order_sheet_num' => 1,
- 'order_sheet_price' => $data['pay_amount'],
- 'order_sheet_amount' => $data['pay_amount'],
- 'order_sheet_pay' => $data['pay_amount'],
- 'order_sheet_addtimes' => time()
- ];
- OrderSheet::insert($orderSheetData);
- // Db::commit();
- } catch (\Exception $e) {
- // Db::rollBack();
- throw new BusinessException("数据错误");
- }
- $data['orderId'] = $orderId;
- $data['join_pay_order_id'] = $orderGroupId;
- $data['join_pay_object_json'] = json_encode(['order_id' => $orderId]);
- $rate = 0;
- if (!empty($goods->goods_attribute_json) && $type == "CASH") {
- $attributeJson = json_decode($goods->goods_attribute_json, true);
- if (isset($attributeJson['added'])) {
- $rate = $attributeJson['added'] ? $attributeJson['added']['nbr'] : 0;
- }
- }
- $extendJson['added_amount'] = number_format($data['pay_amount'] * $rate, 2, '.', '');
- $extendJson['added_rate'] = $rate;
- // } else {
- // // $extendJson['notify'] = 'success';
- // $extendJson['is_custom'] = $params['is_custom'];
- // }
- $data['pay_extend_json'] = json_encode($extendJson);
- $data['pay_paytimes'] = date('Y-m-d H:i:s', strtotime($data['pay_paytimes']));
- $data['pay_prepayid'] = 0;
- return $data;
- }
- /**
- * @Desc
- * @Author Gorden
- * @Date 2024/6/5 13:39
- *
- * @param Request $request
- * @return array
- * @throws BusinessException
- */
- protected function insertGiveInput(Request $request, $type = "CASH"): array
- {
- $params = $request->post();
- $data = $this->inputFilter($params);
- $goods = Goods::find($params['goods_id']);
- $extendJson = [];
- // if (!$params['is_custom']) {
- Db::beginTransaction();
- try {
- if (!$params['is_custom']) {
- $data['pay_amount'] = $goods->goods_sales_price;
- }
- // $orderId = "OD" . date('ymdHi') . random_string(4, 'up');
- // $orderGroupId = "OD" . date('ymdHi') . random_string(4, 'up');
- // $orderData = [
- // 'order_id' => $orderId,
- // 'order_groupby' => $orderGroupId,
- // 'join_order_member_id' => $data['join_pay_member_id'],
- // 'order_name' => date('Y-m-d H:i:s') . '-订单',
- // 'order_amount_total' => $data['pay_amount'],
- // 'order_amount_pay' => $data['pay_amount'],
- // 'order_amount_json' => '{"tax": 0, "freight": 0}',
- // 'order_status_payment' => 'SUCCESS',
- // 'order_classify' => 'RECHARGE',
- // 'order_category' => 'RECHARGE',
- // 'order_status_system' => 'PAYING',
- // 'order_status_storage' => 'PENDING',
- // 'order_addtimes' => time()
- // ];
- // if ($type == 'WELFARE') {
- // $orderData['order_is_complete'] = 'Y';
- // $orderData['order_status_system'] = 'DONE';
- // $orderData['order_status_storage'] = 'DONE';
- // }
- // $sku = GoodsSku::where('join_sku_goods_id', $params['goods_id'])->first();
- //
- // Order::insert($orderData);
- // $orderSheetData = [
- // 'join_sheet_member_id' => $data['join_pay_member_id'],
- // 'join_sheet_order_id' => $orderId,
- // 'join_sheet_goods_id' => $params['goods_id'],
- // 'join_sheet_goods_sku_id' => $sku ? $sku->goods_sku_id : 0,
- // 'order_sheet_status' => 'DONE',
- // 'order_sheet_category' => 'RECHARGE',
- // 'order_sheet_num' => 1,
- // 'order_sheet_price' => $data['pay_amount'],
- // 'order_sheet_amount' => $data['pay_amount'],
- // 'order_sheet_pay' => $data['pay_amount'],
- // 'order_sheet_addtimes' => time()
- // ];
- //
- // OrderSheet::insert($orderSheetData);
- Db::commit();
- } catch (\Exception $e) {
- dump($e->getMessage());
- Db::rollBack();
- throw new BusinessException("数据错误");
- }
- $data['join_pay_order_id'] = "SYSTEM";
- $data['join_pay_object_json'] = json_encode(['order_id' => 'SYSTEM']);
- $rate = 0;
- $extendJson['added_amount'] = $data['pay_amount'];
- $extendJson['added_rate'] = $rate;
- $data['pay_extend_json'] = json_encode($extendJson);
- $data['pay_paytimes'] = date('Y-m-d H:i:s', strtotime($data['pay_paytimes']));
- $data['pay_prepayid'] = 0;
- return $data;
- }
- /**
- * @Desc 加备注-门店、服务人员、业务人员
- * @Author Gorden
- * @Date 2024/8/29 16:14
- *
- * @param Request $request
- * @return Response
- */
- public function remark(Request $request)
- {
- $payId = $request->post('pay_id', '');
- if (!$payId) {
- return json_fail('参数异常');
- }
- $payDetail = PayDetail::where('pay_id', $payId)->first();
- if (!$payDetail) {
- return json_fail("数据异常");
- }
- try {
- $data = [
- 'premises_id' => $request->post('premises_id', ''),
- 'premises_name' => $request->post('premises_name', ''),
- 'business' => $request->post('business', ''),
- 'service' => $request->post('service', '')
- ];
- $payExtendJson = [];
- if (!empty($payDetail->pay_extend_json)) {
- $payExtendJson = json_decode($payDetail->pay_extend_json, true);
- }
- $payExtendJson['remark'] = $data;
- $payDetail->pay_extend_json = json_encode($payExtendJson, JSON_UNESCAPED_UNICODE);
- $payDetail->save();
- return json_success('success');
- } catch (\Exception $e) {
- return json_fail('备注保存失败');
- }
- }
- }
|