DetailController.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. <?php
  2. /**
  3. * 订单详情
  4. * Created by PhpStorm.
  5. * Author: 芸众商城 www.yunzshop.com
  6. * Date: 2017/3/4
  7. * Time: 上午11:16
  8. */
  9. namespace app\backend\modules\order\controllers;
  10. use app\backend\modules\member\models\Member;
  11. use app\backend\modules\order\models\Order;
  12. use app\backend\modules\order\models\OrderGoods;
  13. use app\backend\modules\order\models\VueOrder;
  14. use app\backend\modules\refund\models\RefundApply;
  15. use app\common\components\BaseController;
  16. use app\common\exceptions\AppException;
  17. use app\common\models\Goods;
  18. use app\common\models\MemberShopInfo;
  19. use app\common\modules\order\OrderOperationsCollector;
  20. use app\common\services\DivFromService;
  21. class DetailController extends BaseController
  22. {
  23. public function getMemberButtons()
  24. {
  25. $orderStatus = array_keys(app('OrderManager')->setting('status'));
  26. $buttons = array_map(function ($orderStatus) {
  27. var_dump($orderStatus);
  28. $order = Order::where('status', $orderStatus)->orderBy('id', 'desc')->first();
  29. dump($order->buttonModels);
  30. dump($order->oldButtonModels);
  31. }, $orderStatus);
  32. }
  33. public function ajax()
  34. {
  35. $order = Order::orders()->with(['deductions', 'coupons', 'discounts','orderFees', 'orderServiceFees', 'orderPays' => function ($query) {
  36. $query->with('payType');
  37. }, 'hasOnePayType']);
  38. if (request()->has('id')) {
  39. $order = $order->find(request('id'));
  40. }
  41. if (request()->has('order_sn')) {
  42. $order = $order->where('order_sn', request('order_sn'))->first();
  43. }
  44. if (!$order) {
  45. throw new AppException('未找到订单');
  46. }
  47. if (!empty($order->express)) {
  48. $express = $order->express->getExpress($order->express->express_code, $order->express->express_sn);
  49. $dispatch['express_sn'] = $order->express->express_sn;
  50. $dispatch['company_name'] = $order->express->express_company_name;
  51. $dispatch['data'] = $express['data'];
  52. $dispatch['thumb'] = $order->hasManyOrderGoods[0]->thumb;
  53. $dispatch['tel'] = '95533';
  54. $dispatch['status_name'] = $express['status_name'];
  55. }
  56. return $order->toArray();
  57. }
  58. public function express()
  59. {
  60. // $express = RefundApply::where('order_id',request('id'))->with('returnExpress')->first();
  61. // dd($express);
  62. $order = Order::orders()->with(['deductions', 'coupons', 'discounts','orderFees', 'orderServiceFees', 'orderPays' => function ($query) {
  63. $query->with('payType');
  64. }, 'hasOnePayType']);
  65. if (request()->has('id')) {
  66. $order = $order->find(request('id'));
  67. }
  68. if (request()->has('order_sn')) {
  69. $order = $order->where('order_sn', request('order_sn'))->first();
  70. }
  71. if (!$order) {
  72. throw new AppException('未找到订单');
  73. }
  74. // dd($order->hasOneRefundApply->returnExpress);
  75. if (!empty($order->hasOneRefundApply->returnExpress)) {
  76. $express = $order->express->getExpress($order->hasOneRefundApply->returnExpress->express_code, $order->hasOneRefundApply->returnExpress->express_sn);
  77. $dispatch['express_sn'] = $order->hasOneRefundApply->returnExpress->express_sn;
  78. $dispatch['company_name'] = $order->hasOneRefundApply->returnExpress->express_company_name;
  79. $dispatch['data'] = $express['data'];
  80. $dispatch['thumb'] = $order->hasManyOrderGoods[0]->thumb;
  81. $dispatch['tel'] = '95533';
  82. $dispatch['status_name'] = $express['status_name'];
  83. }
  84. return $this->errorJson('查询成功',$dispatch);
  85. }
  86. /**
  87. * @param \Request $request
  88. * @return string
  89. * @throws AppException
  90. * @throws \Throwable
  91. */
  92. public function index(\Illuminate\Http\Request $request)
  93. {
  94. $order = Order::orders()->with(['deductions', 'coupons', 'discounts','orderFees', 'orderServiceFees', 'orderInvoice', 'orderPays' => function ($query) {
  95. $query->with('payType');
  96. }, 'hasOnePayType','hasOneExpeditingDelivery','expressmany'=>function($query){
  97. $query->with(['ordergoods'=>function($q){
  98. $q->select('id','goods_id','thumb','title','goods_option_title','goods_sn','goods_market_price','payment_amount','total','order_express_id');
  99. }]);
  100. }]);
  101. if (request()->has('id')) {
  102. $order = $order->find(request('id'));
  103. }
  104. if (request()->has('order_sn')) {
  105. $order = $order->where('order_sn', request('order_sn'))->first();
  106. }
  107. if (!$order) {
  108. throw new AppException('未找到订单');
  109. }
  110. //dd($order->toArray());
  111. $dispatch = [];
  112. if (!$order->expressmany->isEmpty() && $order->status>1) {
  113. //兼容以前的 因为批量发货并不会把快递id赋值给订单商品
  114. if($order->is_all_send_goods==0){
  115. $express = $order->express->getExpress($order->express->express_code, $order->express->express_sn);
  116. $dispatch[0]['order_express_id'] = $order->expressmany[0]->id;
  117. $dispatch[0]['express_sn'] = $order->expressmany[0]->express_sn;
  118. $dispatch[0]['company_name'] = $order->expressmany[0]->express_company_name;
  119. $dispatch[0]['data'] = $express['data'];
  120. $dispatch[0]['thumb'] = $order->hasManyOrderGoods[0]->thumb;
  121. $dispatch[0]['tel'] = '95533';
  122. $dispatch[0]['status_name'] = $express['status_name'];
  123. $dispatch[0]['count'] = count($order->hasManyOrderGoods);
  124. $dispatch[0]['goods'] = $order->hasManyOrderGoods;
  125. }else{
  126. $expressmany = $order->expressmany;
  127. foreach ($expressmany as $k=>$v){
  128. $express = $order->express->getExpress($v->express_code, $v->express_sn);
  129. $dispatch[$k]['order_express_id'] = $v->id;
  130. $dispatch[$k]['express_sn'] = $v->express_sn;
  131. $dispatch[$k]['company_name'] = $v->express_company_name;
  132. $dispatch[$k]['data'] = $express['data'];
  133. $dispatch[$k]['thumb'] = $v->ordergoods[0]->thumb;
  134. $dispatch[$k]['tel'] = '95533';
  135. $dispatch[$k]['status_name'] = $express['status_name'];
  136. $dispatch[$k]['count'] = count($v['ordergoods']);
  137. $dispatch[$k]['goods'] = $v['ordergoods'];
  138. }
  139. }
  140. }
  141. if ($order->orderInvoice) {
  142. $order->invoice_type= $order->orderInvoice->invoice_type;
  143. $order->email = $order->orderInvoice->email;
  144. $order->rise_type = $order->orderInvoice->rise_type;
  145. $order->collect_name = $order->orderInvoice->collect_name;
  146. $order->company_number = $order->orderInvoice->company_number;
  147. $order->invoice = $order->orderInvoice->invoice;
  148. }
  149. $trade = \Setting::get('shop.trade');
  150. foreach ($order['hasManyOrderGoods'] as $key => $order_goods){
  151. $order['hasManyOrderGoods'][$key]['goods_price'] = bcdiv($order_goods['goods_price'],$order_goods['total'],2);
  152. $order['hasManyOrderGoods'][$key]['goods_market_price'] = bcdiv($order_goods['goods_market_price'],$order_goods['total'],2);
  153. $order['hasManyOrderGoods'][$key]['goods_cost_price'] = bcdiv($order_goods['goods_cost_price'],$order_goods['total'],2);
  154. }
  155. $order = $order ? $order->toArray() : [];
  156. if (empty($order['belongs_to_member'])) {
  157. $yz_member = MemberShopInfo::withTrashed()->where('member_id', $order['uid'])->first();
  158. }
  159. //因增加多包裹功能所以is_zhu就是多包裹功能所使用,原因是部分插件直接调用主程序的订单详情页面所以不能在原页面上直接更改
  160. return view('order.detail', [
  161. 'order' => $order,
  162. 'is_zhu'=>1,
  163. 'yz_member' => $yz_member,
  164. 'invoice_set'=>$trade['invoice'],
  165. 'dispatch' => $dispatch,
  166. 'div_from' => $this->getDivFrom($order),
  167. 'var' => \YunShop::app()->get(),
  168. 'ops' => 'order.ops',
  169. 'edit_goods' => 'goods.goods.edit'
  170. ])->render();
  171. }
  172. protected function getDivFrom($order)
  173. {
  174. if (!$order || !$order['has_many_order_goods']) {
  175. return ['status' => false];
  176. }
  177. $goods_ids = [];
  178. foreach ($order['has_many_order_goods'] as $key => $goods) {
  179. $goods_ids[] = $goods['goods_id'];
  180. }
  181. $memberInfo = Member::select('realname', 'idcard')->where('uid', $order['uid'])->first();
  182. $result['status'] = DivFromService::isDisplay($goods_ids);
  183. $result['member_name'] = $order['has_many_member_certified']['realname'] ?: $memberInfo->realname;
  184. $result['member_card'] = $order['has_many_member_certified']['idcard'] ?: $memberInfo->idcard;
  185. return $result;
  186. }
  187. public function vueIndex()
  188. {
  189. $order_id = intval(request()->input('id'));
  190. $order_sn = request()->input('order_sn', '');
  191. if (empty($order_id) && empty($order_sn)) {
  192. throw new AppException('订单参数为空');
  193. }
  194. if (empty($order_id)) {
  195. $order_id = VueOrder::uniacid()->where('order_sn',$order_sn)->value('id');
  196. request()->offsetSet('id', $order_id);
  197. }
  198. $data['requestInputs'] = request()->input();
  199. return view('order.vue-detail', ['data'=> json_encode($data)])->render();
  200. }
  201. //新订单详情接口
  202. public function getData()
  203. {
  204. $order_id = intval(request()->input('id'));
  205. if (empty($order_id)) {
  206. throw new AppException('订单参数为空');
  207. }
  208. /**
  209. * @var VueOrder $order
  210. */
  211. $order = VueOrder::detailOrders()->find($order_id);
  212. //todo 这里不在模型的 $appends 属性加动态显示,减少不必要的查询
  213. $order->fixed_link = $order->getOrderType()->fixedLink();
  214. $refundApply = $order->getOrderType()->afterSales(); //订单售后
  215. //dd($order->hasOneRefundApply);
  216. //dd($order->toArray());
  217. $order->orderSteps = (new \app\backend\modules\order\steps\OrderStatusStepManager($order))->getStepItems();
  218. if (!$order) {
  219. throw new AppException('未找到订单');
  220. }
  221. $dispatch = [];
  222. if (!$order->expressmany->isEmpty()) {
  223. //兼容以前的 因为批量发货并不会把快递id赋值给订单商品
  224. if($order->is_all_send_goods==0){
  225. $express = $order->express->getExpress($order->express->express_code, $order->express->express_sn);
  226. $dispatch[0]['order_express_id'] = $order->expressmany[0]->id;
  227. $dispatch[0]['express_sn'] = $order->expressmany[0]->express_sn;
  228. $dispatch[0]['company_name'] = $order->expressmany[0]->express_company_name;
  229. $dispatch[0]['data'] = $express['data'];
  230. $dispatch[0]['thumb'] = $order->hasManyOrderGoods[0]['thumb'];
  231. $dispatch[0]['tel'] = '95533';
  232. $dispatch[0]['status_name'] = $express['status_name'];
  233. $dispatch[0]['count'] = count($order->hasManyOrderGoods);
  234. $dispatch[0]['goods'] = $order->hasManyOrderGoods;
  235. }else{
  236. $expressmany = $order->expressmany;
  237. foreach ($expressmany as $k=>$v){
  238. $express = $order->express->getExpress($v->express_code, $v->express_sn);
  239. $dispatch[$k]['order_express_id'] = $v->id;
  240. $dispatch[$k]['express_sn'] = $v->express_sn;
  241. $dispatch[$k]['company_name'] = $v->express_company_name;
  242. $dispatch[$k]['data'] = $express['data'];
  243. $dispatch[$k]['tel'] = '95533';
  244. $dispatch[$k]['status_name'] = $express['status_name'];
  245. $goods = $v->hasManyOrderPackage->map(function ($item){
  246. $goods = OrderGoods::where([
  247. 'id' => $item->order_goods_id,
  248. 'order_id' => $item->order_id
  249. ])->first();
  250. $goods and $goods->total = $item->total;
  251. return $goods ?: [];
  252. });
  253. $goods = $v->ordergoods->merge($goods);
  254. $dispatch[$k]['count'] = $goods->sum('total');
  255. $dispatch[$k]['goods'] = $goods;
  256. $dispatch[$k]['thumb'] = $goods[0]['thumb'];
  257. }
  258. }
  259. }
  260. if ($order->orderInvoice) {
  261. $order->invoice_type= $order->orderInvoice->invoice_type;
  262. $order->email = $order->orderInvoice->email;
  263. $order->rise_type = $order->orderInvoice->rise_type;
  264. $order->collect_name = $order->orderInvoice->collect_name;
  265. $order->company_number = $order->orderInvoice->company_number;
  266. $order->invoice = yz_tomedia($order->orderInvoice->invoice);
  267. } else {
  268. $order->invoice = yz_tomedia($order->invoice);
  269. }
  270. $order->hasManyOrderGoods->map(function ($order_goods) {
  271. $order_goods->goods_price = bcdiv($order_goods->goods_price,$order_goods->total,2);
  272. $order_goods->goods_market_price = bcdiv($order_goods->goods_market_price,$order_goods->total,2);
  273. $order_goods->goods_cost_price = bcdiv($order_goods->goods_cost_price,$order_goods->total,2);
  274. $order_goods->goods_vip_price = bcdiv($order_goods->vip_price,$order_goods->total,2);
  275. });
  276. $order = $order ? $order->toArray() : [];
  277. if (empty($order['belongs_to_member'])) {
  278. $yz_member = MemberShopInfo::withTrashed()->where('member_id', $order['uid'])->first();
  279. }
  280. $data = [
  281. 'order' => $order,
  282. 'refundApply' => $refundApply,
  283. 'dispatch' => $dispatch?:[],
  284. 'yz_member' => $yz_member?:[],
  285. 'div_from' => $this->getDivFrom($order),
  286. 'expressCompanies' => \app\common\repositories\ExpressCompany::create()->all(),
  287. ];
  288. // dd($data);
  289. return $this->successJson('detail', $data);
  290. }
  291. //退款物流信息
  292. public function refundExpress()
  293. {
  294. $order_id = intval(request()->input('order_id'));
  295. $refund_value = request()->input('refund_value');
  296. $order = VueOrder::uniacid()->with([
  297. 'hasOneRefundApply' => function($query) {
  298. return $query->with('returnExpress')->with('resendExpress');
  299. }])->where('id',$order_id)->first();
  300. if (!$order) {
  301. throw new AppException('未找到订单');
  302. }
  303. if (is_null($order->hasOneRefundApply->returnExpress) && is_null($order->hasOneRefundApply->resendExpress)) {
  304. throw new AppException('物流信息为空');
  305. }
  306. $dispatch = [];
  307. if ($refund_value == 20) {
  308. //买家寄回物流信息
  309. $express = (new \app\common\models\order\Express())->getExpress($order->hasOneRefundApply->returnExpress->express_code, $order->hasOneRefundApply->returnExpress->express_sn);
  310. $dispatch['express_sn'] = $order->hasOneRefundApply->returnExpress->express_sn;
  311. $dispatch['company_name'] = $order->hasOneRefundApply->returnExpress->express_company_name;
  312. $dispatch['data'] = $express['data'];
  313. $dispatch['thumb'] = $order->hasManyOrderGoods[0]->thumb;
  314. $dispatch['tel'] = '95533';
  315. $dispatch['status_name'] = $express['status_name'];
  316. } elseif ($refund_value == 30) {
  317. //商家发货物流信息
  318. $express = (new \app\common\models\order\Express())->getExpress($order->hasOneRefundApply->resendExpress->express_code, $order->hasOneRefundApply->resendExpress->express_sn);
  319. $dispatch['express_sn'] = $order->hasOneRefundApply->resendExpress->express_sn;
  320. $dispatch['company_name'] = $order->hasOneRefundApply->resendExpress->express_company_name;
  321. $dispatch['data'] = $express['data'];
  322. $dispatch['thumb'] = $order->hasManyOrderGoods[0]->thumb;
  323. $dispatch['tel'] = '95533';
  324. $dispatch['status_name'] = $express['status_name'];
  325. }
  326. return $this->successJson('查询成功',$dispatch);
  327. }
  328. public function orderGoodsPartRefund()
  329. {
  330. $order_id = intval(request()->input('order_id'));
  331. $order = VueOrder::uniacid()->with(['orderGoods'])->where('id',$order_id)->first();
  332. if ($order->orderGoods->isEmpty()) {
  333. return $this->errorJson('获取订单商品数据错误');
  334. }
  335. //处理订单可退款商品数量
  336. $orderGoods = $order->orderGoods->map(function ($orderGoods) {
  337. $orderGoods->refundable_total = $orderGoods->total - $orderGoods->getRefundTotal();
  338. $orderGoods->unit_price = bankerRounding($orderGoods->payment_amount / $orderGoods->total);
  339. return $orderGoods;
  340. });
  341. return $this->successJson('订单商品数据',$orderGoods);
  342. }
  343. }