12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229 |
- <?php
- namespace addons\unishop\controller;
- use addons\unishop\extend\Hashids;
- use addons\unishop\model\Area;
- use addons\unishop\model\Config;
- use addons\unishop\model\Evaluate;
- use addons\unishop\model\Product;
- use app\admin\model\unishop\Coupon as CouponModel;
- use addons\unishop\model\DeliveryRule as DeliveryRuleModel;
- use addons\unishop\model\OrderRefund;
- use app\admin\model\unishop\OrderRefundProduct;
- use think\Db;
- use think\Exception;
- use addons\unishop\model\Address as AddressModel;
- use think\Hook;
- use think\Loader;
- use addons\epay\library\Service;
- class Order extends Base
- {
-
- protected $frequently = ['getorders','getOrders','detail'];
- protected $noNeedLogin = ['count'];
-
- public function create()
- {
- $productId = input('id', 0);
- try {
- $user_id = $this->auth->id;
-
- if ($productId) {
- $number = input('number',1,'intval');
- $number = intval($number);
- $productId = \addons\unishop\extend\Hashids::decodeHex($productId);
- $product = (new Product)->where(['id' => $productId, 'switch' => Product::SWITCH_ON, 'deletetime' => null])->find();
-
- $spec = input('spec', '');
- $productData[0] = $product->getDataOnCreateOrder($spec,$number);
- } else {
-
- $cart = input('cart');
- $carts = (new \addons\unishop\model\Cart)
- ->whereIn('id', $cart)
- ->with(['product'])
- ->order(['id' => 'desc'])
- ->select();
- foreach ($carts as $cart) {
- if ($cart->product instanceof Product) {
- $productData[] = $cart->product->getDataOnCreateOrder($cart->spec ? $cart->spec : '', $cart->number);
- }
- }
- }
- if (empty($productData) || !$productData) {
- $this->error(__('Product not exist'));
- }
-
-
-
-
-
-
- $order_price = '0';
- $delivery_price = '0';
- foreach ($productData as &$product) {
- $product['image'] = Config::getImagesFullUrl($product['image']);
- $product['sales_price'] = round($product['sales_price'], 2);
- $product['market_price'] = round($product['market_price'], 2);
- $order_price = bcadd($order_price,$product['total_price'],2);
- }
- $this->success('', [
- 'product' => $productData,
- 'order_price' => $order_price,
- 'delivery_price' => $delivery_price,
- 'total_price' => bcadd($order_price,$delivery_price,2),
- 'wallet' => Db::name('user_wallet')->where('user_id',$this->auth->id)->find(),
- ]);
- } catch (Exception $e) {
- $this->error($e->getMessage(), false);
- }
- }
-
- public function submit()
- {
- $data = input();
- $pay_type = input('pay_type','wechat');
- if(!in_array($pay_type,['wechat','alipay','wallet'])){
- $this->error();
- }
- try {
- $validate = Loader::validate('\\addons\\unishop\\validate\\Order');
- if (!$validate->check($data, [], 'submit')) {
- throw new Exception($validate->getError());
- }
- Db::startTrans();
-
- if (empty(Hook::get('create_order_before'))) {
- Hook::add('create_order_before', 'addons\\unishop\\behavior\\Order');
- }
-
- if (empty(Hook::get('create_order_after'))) {
- Hook::add('create_order_after', 'addons\\unishop\\behavior\\Order');
- }
- $orderModel = new \addons\unishop\model\Order();
- $result = $orderModel->createOrder($this->auth->id, $data);
- Db::commit();
- } catch (Exception $e) {
- Db::rollback();
- $this->error($e->getMessage(), false);
- }
- $this->success('', $result);
- }
-
- public function getDelivery()
- {
- $cityId = input('city_id', 0);
- $delivery = (new DeliveryRuleModel())->getDelivetyByArea($cityId);
- $this->success('', $delivery['list']);
- }
-
- public function getOrders()
- {
-
- $type = input('type', 0);
- $page = input('page', 1);
- $pagesize = input('pagesize', 10);
- try {
- $orderModel = new \addons\unishop\model\Order();
- $result = $orderModel->getOrdersByType($this->auth->id, $type, $page, $pagesize);
- } catch (Exception $e) {
- $this->error($e->getMessage());
- }
- $this->success('', $result);
- }
-
- public function getOrders_num(){
-
- $status = [
- [
- 'status' => 1,
- 'status_text' => '待付款',
- 'number' => 0,
- ],
- [
- 'status' => 3,
- 'status_text' => '待核销',
- 'number' => 0,
- ],
- [
- 'status' => 4,
- 'status_text' => '已完成',
- 'number' => 0,
- ],
- [
- 'status' => 40,
- 'status_text' => '待评价',
- 'number' => 0,
- ],
- ];
- $orderModel = new \addons\unishop\model\Order();
- foreach($status as $key => $val){
- $val['number'] = $orderModel->getOrdersByType_num($this->auth->id,$val['status']);
- $status[$key] = $val;
- }
- $this->success(1, $status);
- }
-
- public function cancel()
- {
- $order_id = input('order_id', 0);
- $order_id = \addons\unishop\extend\Hashids::decodeHex($order_id);
- $orderModel = new \addons\unishop\model\Order();
- $order = $orderModel->where(['id' => $order_id, 'user_id' => $this->auth->id])->find();
- if (!$order) {
- $this->error(__('Order not exist'));
- }
- switch ($order['status']) {
- case \addons\unishop\model\Order::STATUS_REFUND:
- $this->error('此订单已退款,无法取消');
- break;
- case \addons\unishop\model\Order::STATUS_CANCEL:
- $this->error('此订单已取消, 无需再取消');
- break;
- }
- if ($order['have_paid'] != \addons\unishop\model\Order::PAID_NO) {
- $this->error('此订单已支付,无法取消');
- }
- if ($order['have_received'] != \addons\unishop\model\Order::RECEIVED_NO) {
- $this->error('此订单已核销,无法取消');
- }
- if ($order['status'] == \addons\unishop\model\Order::STATUS_NORMAL && $order['have_paid'] == \addons\unishop\model\Order::PAID_NO) {
- $order->status = \addons\unishop\model\Order::STATUS_CANCEL;
- $order->canceltime = time();
- $order->save();
-
-
- $this->success('取消成功', true);
- }
- }
-
- public function payed_cancel()
- {
- $order_id = input('order_id', 0);
- $order_id = \addons\unishop\extend\Hashids::decodeHex($order_id);
- $orderModel = new \addons\unishop\model\Order();
- $order = $orderModel->where(['id' => $order_id, 'user_id' => $this->auth->id])->find();
- if (!$order) {
- $this->error(__('Order not exist'));
- }
- switch ($order['status']) {
- case \addons\unishop\model\Order::STATUS_REFUND:
- $this->error('此订单已退款,无法取消');
- break;
- case \addons\unishop\model\Order::STATUS_CANCEL:
- $this->error('此订单已取消, 无需再取消');
- break;
- }
- if ($order['have_paid'] == \addons\unishop\model\Order::PAID_NO) {
- $this->error('未支付订单,直接取消即可');
- }
- if ($order['have_received'] != \addons\unishop\model\Order::RECEIVED_NO) {
- $this->error('此订单已核销,无法退订');
- }
- Db::startTrans();
- if ($order['status'] == \addons\unishop\model\Order::STATUS_NORMAL && $order['have_received'] == \addons\unishop\model\Order::RECEIVED_NO) {
- $refund_amount = $order['total_price'];
- $refund_status = 3;
-
- $params = [
- 'refund_status' => $refund_status,
- 'updatetime' => time(),
- 'refund_amount' => $refund_amount,
- 'status' => \addons\unishop\model\Order::STATUS_CANCEL,
- 'canceltime' => time(),
- ];
- if($refund_status == 3) {
- $params['had_refund'] = time();
- }
- $result = Db::name('unishop_order')->where(['id' => $order_id])->update($params);
- if (!$result) {
- Db::rollback();
- $this->error('取消失败');
- }
-
- $coupon_user_id = Db::name('unishop_order_extend')->where('order_id',$order_id)->value('coupon_user_id');
- if(!empty($coupon_user_id)){
- Db::name('unishop_coupon_user')->where('id',$coupon_user_id)->where('user_id',$this->auth->id)->update(['status'=>0]);
- }
-
- if($refund_status == 3 && $refund_amount > 0){
- $order = Db::name('unishop_order')->where('id',$order_id)->find();
- if($order['pay_type'] == 2){
- $wallet = new \app\common\model\Wallet();
- $wallet_rs = $wallet->lockChangeAccountRemain($order['user_id'],'money',$refund_amount,32,'商城取消订单退款'.$order['out_trade_no'],'unishop_order',$order_id);
- if($wallet_rs['status'] === false){
- Db::rollback();
- $this->error($wallet_rs['msg']);
- }
- }elseif($order['pay_type'] == 3){
- $refund_result = $this->old_refund($order,$refund_amount);
- if($refund_result !== true){
- Db::rollback();
- $this->error($refund_result);
- }
- }
- }
- }
- Db::commit();
- $this->success('退订成功', true);
- }
-
- private function old_refund($order, $refund_price)
- {
- $table = 'unishop_order';
- $remark = '订单退款';
- if($order['pay_type'] == 3){
- $order['pay_type'] = 'wechat';
- }
- if($order['pay_type'] == 4){
- $order['pay_type'] = 'alipay';
- }
-
- $refund_data = [
- 'order_id' => $order['id'],
- 'out_refund_no'=> createUniqueNo('R'),
- 'pay_fee' => $order['total_price'],
- 'refund_price' => $refund_price,
- 'pay_type' => $order['pay_type'],
- 'status' => 0,
- 'createtime' => time(),
- 'updatetime' => time(),
- 'table' => $table,
- 'table_id' => $order['id'],
- ];
- $refund_log_id = Db::name('pay_order_refund_log')->insertGetId($refund_data);
- if(!$refund_log_id){
- return '退款失败';
- }
- if ($order['pay_type'] == 'wechat' || $order['pay_type'] == 'alipay') {
-
-
- $order_data = [
- 'out_trade_no' => $order['pay_out_trade_no']
- ];
- if ($order['pay_type'] == 'wechat') {
- $total_fee = $order['total_price'] * 100;
- $refund_fee = $refund_price * 100;
- $order_data = array_merge($order_data, [
- 'out_refund_no' => $refund_data['out_refund_no'],
- 'total_fee' => $total_fee,
- 'refund_fee' => $refund_fee,
- 'refund_desc' => $remark,
- ]);
- } else {
- $order_data = array_merge($order_data, [
- 'out_request_no' => $refund_data['out_refund_no'],
- 'refund_amount' => $refund_price,
- ]);
- }
-
- if ($order['pay_type'] == 'wechat') {
- $wxpay = new \app\common\library\Wxpay;
- $result = $wxpay->WxPayRefund($order_data);
-
- if ($result['return_code'] == 'SUCCESS' && $result['result_code'] == 'SUCCESS') {
- Db::name('pay_order_refund_log')->where('id',$refund_log_id)->update(['status'=>1]);
- return true;
- } else {
- return $result['return_msg'];
- }
- } else {
- $result = Service::submitRefund($order['total_price'],$refund_price,$order['pay_out_trade_no'],$refund_data['out_refund_no'],$order['pay_type'],$remark,'');
- if($result['code'] == '10000'){
- Db::name('pay_order_refund_log')->where('id',$refund_log_id)->update(['status'=>1]);
- return true;
- }else{
- return $result['msg'];
- }
-
-
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- }
- return true;
- }
-
- public function delete()
- {
- $order_id = input('order_id', 0);
- $order_id = \addons\unishop\extend\Hashids::decodeHex($order_id);
- $orderModel = new \addons\unishop\model\Order();
- $order = $orderModel->where(['id' => $order_id, 'user_id' => $this->auth->id])->find();
- if (!$order) {
- $this->error(__('Order not exist'));
- }
- if ($order['status'] == \addons\unishop\model\Order::STATUS_NORMAL) {
- $this->error('只能删除已取消或已退货的订单');
- }
- if ($order['status'] == \addons\unishop\model\Order::STATUS_REFUND && $order['refund_status'] == \addons\unishop\model\Order::REFUND_STATUS_APPLY) {
- $this->error('订单退款中,不可删除订单');
- }
- $order->delete();
- $this->success('删除成功', true);
- }
-
- public function received()
- {
- $order_id = input('order_id', 0);
- $order_id = \addons\unishop\extend\Hashids::decodeHex($order_id);
- $orderModel = new \addons\unishop\model\Order();
- $order = $orderModel->where(['id' => $order_id, 'user_id' => $this->auth->id])->find();
- if (!$order) {
- $this->error(__('Order not exist'));
- }
- if ($order->have_delivered == 0) {
- $this->error('未发货,不能确认收货');
- }
- $order->have_received = time();
- $order->save();
- $this->success('已确认收货', true);
- }
-
- public function comment()
- {
- $rate = input('rate', 5);
- $anonymous = input('anonymous', 0);
- $images = input('images','','trim');
- $comment = input('comment');
- $order_id = input('order_id', 0);
- $order_id = \addons\unishop\extend\Hashids::decodeHex($order_id);
- $product_id = input('product_id');
- $product_id = \addons\unishop\extend\Hashids::decodeHex($product_id);
- $orderProductModel = new \addons\unishop\model\OrderProduct();
- $orderProduct = $orderProductModel->field('id,id as order_product_id,spec')->where(['product_id' => $product_id, 'order_id' => $order_id, 'user_id' => $this->auth->id])->find();
- $orderModel = new \addons\unishop\model\Order();
- $order = $orderModel->where(['id' => $order_id, 'user_id' => $this->auth->id])->find();
- if (!$orderProduct || !$order) {
- $this->error(__('Order not exist'));
- }
- if ($order->have_received == $orderModel::RECEIVED_NO) {
- $this->error(__('未核销,不可评价'));
- }
- $images_arr = explode(',',$images);
- if(count($images_arr) > 5){
- $this->error('最多上传5张图片');
- }
- $result = false;
- try {
- $evaluate = new Evaluate();
- $evaluate->user_id = $this->auth->id;
- $evaluate->order_id = $order_id;
- $evaluate->order_product_id = $orderProduct->order_product_id;
- $evaluate->product_id = $product_id;
- $evaluate->rate = $rate;
- $evaluate->anonymous = $anonymous;
- $evaluate->images = $images;
- $evaluate->comment = $comment;
- $evaluate->spec = $orderProduct->spec;
- $result = $evaluate->save();
- $orderProduct = Db::name('unishop_order_product')->where(['order_id' => $order_id])->count();
- $orderevaluate = Db::name('unishop_evaluate')->where(['order_id' => $order_id])->count();
- if ($result && ($orderProduct == $orderevaluate)) {
- $order->have_commented = time();
- $order->save();
- }
- } catch (Exception $e) {
- $this->error($e->getMessage());
- }
- if ($result !== false) {
- $this->success(__('Thanks for the evaluation'));
- } else {
- $this->error(__('Evaluation failure'));
- }
- }
-
- public function count()
- {
- if (!$this->auth->isLogin()) {
- $this->error('');
- }
- $order = new \addons\unishop\model\Order();
- $list = $order
- ->where([
- 'user_id' => $this->auth->id,
- ])
- ->where('status', '<>', \addons\unishop\model\Order::STATUS_CANCEL)
- ->where(function ($query) {
- $query
- ->whereOr([
- 'have_paid' => \addons\unishop\model\Order::PAID_NO,
- 'have_delivered' => \addons\unishop\model\Order::DELIVERED_NO,
- 'have_received' => \addons\unishop\model\Order::RECEIVED_NO,
- 'have_commented' => \addons\unishop\model\Order::COMMENTED_NO
- ])
- ->whereOr('refund_status', '>', \addons\unishop\model\Order::REFUND_STATUS_NONE);
- })
- ->field('have_paid,have_delivered,have_received,have_commented,refund_status,had_refund')
- ->select();
- $data = [
- 'unpaid' => 0,
- 'undelivered' => 0,
- 'unreceived' => 0,
- 'uncomment' => 0,
- 'refund' => 0
- ];
- foreach ($list as $item) {
- switch (true) {
- case $item['have_paid'] > 0 && $item['have_delivered'] > 0 && $item['have_received'] > 0 && $item['have_commented'] == 0 && $item['refund_status'] == 0:
- $data['uncomment']++;
- break;
- case $item['have_paid'] > 0 && $item['have_delivered'] > 0 && $item['have_received'] == 0 && $item['have_commented'] == 0 && $item['refund_status'] == 0:
- $data['unreceived']++;
- break;
- case $item['have_paid'] > 0 && $item['have_delivered'] == 0 && $item['have_received'] == 0 && $item['have_commented'] == 0 && $item['refund_status'] == 0:
- $data['undelivered']++;
- break;
- case $item['have_paid'] == 0 && $item['have_delivered'] == 0 && $item['have_received'] == 0 && $item['have_commented'] == 0 && $item['refund_status'] == 0:
- $data['unpaid']++;
- break;
- case $item['refund_status'] > 0 && $item['had_refund'] == 0 && $item['refund_status'] != 3:
- $data['refund']++;
- break;
- }
- }
- $this->success('', $data);
- }
-
- public function detail()
- {
- $order_id = input('order_id', 0);
- $order_id = \addons\unishop\extend\Hashids::decodeHex($order_id);
- try {
- $orderModel = new \addons\unishop\model\Order();
- $order = $orderModel
- ->with([
- 'products' => function ($query) {
- $query->field('id,id as order_product_id,title,image,number,price,spec,order_id,product_id');
- },
- 'evaluate' => function ($query) {
- $query->field('id,order_id,order_product_id,product_id');
- }
- ])
- ->where(['id' => $order_id, 'user_id' => $this->auth->id])->find();
- if ($order) {
- $order = $order->append(['state', 'paidtime', 'deliveredtime', 'receivedtime', 'commentedtime', 'pay_type_text', 'refund_status_text'])->toArray();
- $order['state_text'] = $orderModel->getStatetextAttr($order['state'],$order['refund_status']);
-
- $evaluate = array_column($order['evaluate'], 'order_product_id');
- foreach ($order['products'] as &$product) {
- $product['image'] = Config::getImagesFullUrl($product['image']);
- if (in_array($product['order_product_id'], $evaluate)) {
- $product['evaluate'] = true;
- } else {
- $product['evaluate'] = false;
- }
- }
- unset($order['evaluate']);
- unset($order['extend']);
- unset($order['pay_out_trade_no']);
-
- $qrcode_string = 'order_hexiao_no|' . $order['out_trade_no'];
- $order['order_hexiao_qrcode'] = httpurllocal($this->inviteimage($qrcode_string));
- }
- } catch (Exception $e) {
- $this->error($e->getMessage());
- }
- $this->success('', $order);
- }
-
- private function inviteimage($introcode) {
- $params['text'] = $introcode;
- $qrcode_service = \addons\qrcode\library\Service::qrcode($params);
- $qrcodePath = ROOT_PATH . 'public/uploads/qrcode/';
- if (!is_dir($qrcodePath)) {
- @mkdir($qrcodePath);
- }
- if (is_really_writable($qrcodePath)) {
- $filename = md5(implode('', $params)) . '.png';
- $filePath = $qrcodePath . $filename;
- $qrcode_service->writeFile($filePath);
- }
- return '/uploads/qrcode/' . $filename;
- }
-
- public function refundInfo()
- {
- $order_id = input('order_id');
- $order_id = \addons\unishop\extend\Hashids::decodeHex($order_id);
- $orderModel = new \addons\unishop\model\Order();
- $order = $orderModel
- ->with([
- 'products' => function ($query) {
- $query->field('id,order_id,image,number,price,spec,title,product_id,(1) as choose');
- },
- 'refund',
- 'refundProducts'
- ])
- ->field('id,status,total_price,delivery_price,have_commented,have_delivered,have_paid,have_received,refund_status')
- ->where(['id' => $order_id, 'user_id' => $this->auth->id])->find();
- if (!$order) {
- $this->error(__('Order not exist'));
- }
- $order = $order->append(['refund_status_text'])->toArray();
- foreach ($order['products'] as &$product) {
- $product['image'] = Config::getImagesFullUrl($product['image']);
- $product['choose'] = 0;
-
- if ($order['status'] == \addons\unishop\model\Order::STATUS_REFUND) {
- foreach ($order['refund_products'] as $refundProduct) {
- if ($product['order_product_id'] == $refundProduct['order_product_id']) {
- $product['choose'] = 1;
- }
- }
- }
- }
- unset($order['refund_products']);
- $this->success('', $order);
- }
-
- public function refund()
- {
- $order_id = input('order_id');
- $order_id = Hashids::decodeHex($order_id);
- $orderModel = new \addons\unishop\model\Order();
- $order = $orderModel->where(['id' => $order_id, 'user_id' => $this->auth->id])->find();
- if (!$order) {
- $this->error(__('Order not exist'));
- }
-
- if ($order['have_delivered'] == 0) {
- $this->error(__('订单未发货,可直接取消,无需申请售后'));
- }
- if($order->status == 0){
- $this->error('当前订单不能申请退款');
- }
- if($order->status == -1){
- $this->error('当前订单已经申请过退款');
- }
-
- $amount = $order->total_price;
- $serviceType = input('service_type',1);
- $receivingStatus = input('receiving_status',1);
- $reasonType = input('reason_type','');
- $refundExplain = input('refund_explain','');
- $images = input('images','');
- $orderProductId = Db::name('unishop_order_product')->where('order_id',$order_id)->column('product_id');
- $orderProductId = implode(',', $orderProductId);
- if (!$orderProductId) {
- $this->error(__('Please select goods'));
- }
- if (!in_array($receivingStatus, [OrderRefund::UNRECEIVED, OrderRefund::RECEIVED])) {
- $this->error(__('Please select goods status'));
- }
- if (!in_array($serviceType, [OrderRefund::TYPE_REFUND_NORETURN, OrderRefund::TYPE_REFUND_RETURN, OrderRefund::TYPE_EXCHANGE])) {
- $this->error(__('Please select service type'));
- }
- if (in_array($serviceType, [OrderRefund::TYPE_REFUND_NORETURN, OrderRefund::TYPE_REFUND_RETURN]) && $order['total_price'] > 0) {
- if (!$amount) {
- $this->error(__('Please fill in the refund amount'));
- }
- }
- try {
- Db::startTrans();
- $orderRefund = new OrderRefund();
- $orderRefund->user_id = $this->auth->id;
- $orderRefund->order_id = $order_id;
- $orderRefund->receiving_status = $receivingStatus;
- $orderRefund->service_type = $serviceType;
- $orderRefund->reason_type = $reasonType;
- $orderRefund->amount = $amount;
- $orderRefund->refund_explain = $refundExplain;
- $orderRefund->images = $images;
- $orderRefund->save();
- $productIdArr = explode(',', $orderProductId);
- $refundProduct = [];
- foreach ($productIdArr as $orderProductId) {
- $tmp['order_product_id'] = $orderProductId;
- $tmp['order_id'] = $order_id;
- $tmp['user_id'] = $this->auth->id;
- $tmp['refund_id'] = $orderRefund['id'];
- $tmp['createtime'] = time();
- $refundProduct[] = $tmp;
- }
- (new OrderRefundProduct)->insertAll($refundProduct);
- $order->status = \addons\unishop\model\Order::STATUS_REFUND;
- $order->refund_status = \addons\unishop\model\Order::REFUND_STATUS_APPLY;
- $order->save();
- Db::commit();
- } catch (Exception $e) {
- Db::rollback();
- $this->error($e->getMessage());
- }
- $this->success('已申请', 1);
- }
-
- public function refundDelivery()
- {
- $orderId = input('order_id');
- $expressNumber = input('express_number');
- if (!$expressNumber) {
- $this->error(__('Please fill in the express number'));
- }
- $orderId = Hashids::decodeHex($orderId);
- $orderModel = new \addons\unishop\model\Order();
- $order = $orderModel
- ->where(['id' => $orderId, 'user_id' => $this->auth->id])
- ->with(['refund'])->find();
- if (!$order || !$order->refund) {
- $this->error(__('Order not exist'));
- }
- try {
- Db::startTrans();
- $order->refund->express_number = $expressNumber;
- $order->refund_status = \addons\unishop\model\Order::REFUND_STATUS_APPLY;
- if ($order->refund->save() && $order->save()) {
- Db::commit();
- } else {
- throw new Exception(__('Operation failed'));
- }
- } catch (Exception $e) {
- Db::rollback();
- $this->success($e->getMessage());
- }
- $this->success('', 1);
- }
-
- public function express() {
- $params = input();
- if (!class_exists(\addons\expressquery\library\Expressquery::class)) {
- $this->error('请先安装插件《物流信息接口》', []);
- }
- $info = get_addon_info('expressquery');
- if ($info['state'] == 0) {
- $this->error('请开启插件《物流信息接口》', []);
- }
- $expModle = new \addons\expressquery\library\Expressquery();
- $list = $expModle->getExpressList($params['express'], $params['expresssn']);
- if ($list['code'] == 0) {
- $this->error($list['msg']);
- }
- $expressInfo = Db::name('expressquery')->where(['express' => $params['express']])->find();
- $this->success('', [
- 'message' => $list['data'] ?? [],
- 'company' => $expressInfo['name'] ?? '快递单号'
- ]);
- }
- }
|