VueOperationController.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2020/12/3
  6. * Time: 14:42
  7. */
  8. namespace app\backend\modules\order\controllers;
  9. use app\common\components\BaseController;
  10. use app\common\models\Order;
  11. use app\common\models\order\ManualRefundLog;
  12. use app\common\models\PayType;
  13. use app\frontend\modules\order\services\OrderService;
  14. use app\common\models\order\Remark;
  15. use app\common\exceptions\AppException;
  16. use Illuminate\Support\Facades\DB;
  17. use app\framework\Http\Request;
  18. class VueOperationController extends BaseController
  19. {
  20. protected $param;
  21. /**
  22. * @var Order
  23. */
  24. protected $order;
  25. public $transactionActions = ['pay','send','addOrderExpress','separateSend','cancelSend','receive','close'];
  26. /**
  27. * @return mixed|void
  28. * @throws AppException
  29. */
  30. public function preAction()
  31. {
  32. parent::preAction();
  33. $this->param = request()->input();
  34. if (!isset($this->param['order_id'])) {
  35. throw new AppException('order_id不能为空!');
  36. }
  37. $this->order = Order::find($this->param['order_id']);
  38. if (!isset($this->order)) {
  39. throw new AppException('未找到该订单!');
  40. }
  41. }
  42. /**
  43. * 支付
  44. * @return \Illuminate\Http\JsonResponse
  45. * @throws \app\common\exceptions\AppException
  46. */
  47. public function pay()
  48. {
  49. $this->order->backendPay();
  50. return $this->successJson('操作成功');
  51. }
  52. /**
  53. * 确认发货
  54. * @return mixed
  55. * @throws \app\common\exceptions\AppException
  56. */
  57. public function send()
  58. {
  59. OrderService::orderSend($this->param);
  60. return $this->successJson('操作成功');
  61. }
  62. /**
  63. * 多包裹继续返回
  64. * @return mixed
  65. * @throws \app\common\exceptions\AppException
  66. */
  67. public function addOrderExpress()
  68. {
  69. OrderService::addOrderExpress($this->param);
  70. return $this->successJson('操作成功');
  71. }
  72. /**
  73. * 多包裹发货
  74. * @return mixed
  75. * @throws \app\common\exceptions\AppException
  76. */
  77. public function separateSend()
  78. {
  79. if ($this->order->status == Order::WAIT_SEND) {
  80. OrderService::orderSend($this->param);
  81. } else {
  82. OrderService::addOrderExpress($this->param);
  83. }
  84. return $this->successJson('操作成功');
  85. }
  86. /**
  87. * 取消发货
  88. * @return mixed
  89. * @throws \app\common\exceptions\AppException
  90. */
  91. public function cancelSend()
  92. {
  93. OrderService::orderCancelSend($this->param);
  94. return $this->successJson('操作成功');
  95. }
  96. /**
  97. * 确认收货
  98. * @return mixed
  99. * @throws \app\common\exceptions\AppException
  100. */
  101. public function receive()
  102. {
  103. OrderService::orderReceive($this->param);
  104. return $this->successJson('操作成功');
  105. }
  106. /**
  107. * 关闭订单
  108. * @return mixed
  109. * @throws \app\common\exceptions\AppException
  110. */
  111. public function close()
  112. {
  113. OrderService::orderClose($this->param);
  114. return $this->successJson('操作成功');
  115. }
  116. /**
  117. * 退款并关闭订单
  118. * @return mixed
  119. * @throws \app\common\exceptions\AppException
  120. */
  121. public function manualRefund()
  122. {
  123. if ($this->order->isPending()) {
  124. throw new AppException("订单已锁定,无法继续操作");
  125. }
  126. if ($this->order->hasOneRefundApply && $this->order->hasOneRefundApply->isRefunding()) {
  127. throw new AppException('订单有售后记录待处理,无法继续操作');
  128. }
  129. \app\backend\modules\refund\services\RefundOperationService::orderCloseAndRefund($this->order);
  130. //$result = $this->order->refund();
  131. ManualRefundLog::saveLog($this->order->id);
  132. return $this->successJson('操作成功');
  133. }
  134. public function partRefund(Request $request)
  135. {
  136. $order = Order::find($request->input('order_id'));
  137. if (!isset($order)) {
  138. throw new AppException('订单不存在');
  139. }
  140. if ($order->status < Order::WAIT_SEND) {
  141. throw new AppException('订单未付款,无法退款');
  142. }
  143. if ($order->hasOneRefundApply && $order->hasOneRefundApply->isRefunding()) {
  144. throw new AppException('申请已提交,处理中');
  145. }
  146. $refundApply = new \app\backend\modules\refund\services\operation\RefundApply(['uid'=>$order->uid]);
  147. $refundApply->setRelation('order',$order);
  148. DB::transaction(function()use($refundApply){
  149. $refundApply->execute();
  150. });
  151. // $order = Order::find($request->input('order_id'));
  152. return $this->successJson('操作成功',$refundApply->id);
  153. }
  154. public function remarks()
  155. {
  156. $order = Order::find(request()->input('order_id'));
  157. if(!$order){
  158. throw new AppException("未找到该订单".request()->input('order_id'));
  159. }
  160. if(request()->has('remark')){
  161. $remark = $order->hasOneOrderRemark;
  162. if (!$remark) {
  163. $remark = new Remark([
  164. 'order_id' => request()->input('order_id'),
  165. 'remark' => request()->input('remark')
  166. ]);
  167. if(!$remark->save()){
  168. return $this->errorJson('订单备注保存失败');
  169. }
  170. } else {
  171. $reUp = Remark::where('order_id', request()->input('order_id') )
  172. ->where('remark', $remark->remark)
  173. ->update(['remark'=> request()->input('remark')]);
  174. if (!$reUp) {
  175. return $this->errorJson('订单备注保存失败');
  176. }
  177. }
  178. }
  179. return $this->successJson('订单备注保存成功');
  180. }
  181. public function invoice()
  182. {
  183. $order = Order::with(['orderInvoice'])->find(request()->input('order_id'));
  184. if(!$order){
  185. throw new AppException("未找到该订单".request()->input('order_id'));
  186. }
  187. if (!request()->has('invoice')) {
  188. throw new AppException('未上传图片');
  189. }
  190. $orderInvoice = $order->orderInvoice;
  191. if ($orderInvoice) {
  192. $orderInvoice->invoice = request()->input('invoice');
  193. $orderInvoice->save();
  194. }
  195. $order->invoice = request()->input('invoice');
  196. $order->save();
  197. //发邮件
  198. // $flag = \Illuminate\Support\Facades\Mail::to('email')->subjuet('订单发票')->send(new \app\Mail\OrderInvoice(yz_tomedia(request()->input('invoice'))));
  199. return $this->successJson('订单发票保存成功');
  200. }
  201. public function setOrder($order)
  202. {
  203. $this->order = $order;
  204. }
  205. public function setParam($param)
  206. {
  207. $this->param = $param;
  208. }
  209. }