OrdersTransport.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php namespace App\Http\Controllers\Admin;
  2. use App\Models\AdminUser;
  3. use App\Models\Orders;
  4. use App\Models\Product;
  5. use App\Models\OrdersProduct;
  6. use App\Models\OrdersTransport as Model;
  7. use App\Models\CustomScore;
  8. use Illuminate\Support\Carbon;
  9. use Illuminate\Support\Facades\DB;
  10. /**
  11. * 订单物流
  12. *
  13. * @author 刘相欣
  14. *
  15. */
  16. class OrdersTransport extends Auth{
  17. protected function _initialize(){
  18. parent::_initialize();
  19. $this->assign('breadcrumb1','订单物流');
  20. $this->assign('breadcrumb2','物流发货');
  21. }
  22. /**
  23. * 首页列表
  24. *
  25. * */
  26. public function index(Model $Model,AdminUser $AdminUser,Orders $Orders){
  27. // 接受参数
  28. $code = request('out_code','');
  29. $adminName = request('admin_name','');
  30. $startTime = request('start_time','');
  31. $endTime = request('end_time','');
  32. // 编码转ID
  33. $id = $code ? $Model->codeToId($code) : 0;
  34. $adminId = $AdminUser->getByName($adminName,'uid');
  35. // 查询条件
  36. $map = [];
  37. // 编码ID
  38. if( $id ) $map[] = ['orders_transport.id','=',$id];
  39. if( $adminId ) $map[] = ['admin_uid','=',$adminId];
  40. if( $startTime ) $map[] = ['stock.insert_time','>=',Carbon::createFromFormat('Y-m-d',$startTime)->startOfDay()->getTimestamp()];
  41. if( $endTime ) $map[] = ['stock.insert_time','<=',Carbon::createFromFormat('Y-m-d',$endTime)->endOfDay()->getTimestamp()];
  42. // 查询数据
  43. $list = $Model->query()->where($map)->paginate(request('limit',config('page_num',10)))->appends(request()->all());
  44. // 循环处理数据
  45. foreach ($list as $key => $value) {
  46. // id转编号
  47. $value['out_code'] = $Model->idToCode($value['id']);
  48. // id转编号
  49. $value['order_code'] = $Orders->idToCode($value['order_id']);
  50. // 创建人员
  51. $value['admin_name'] = $AdminUser->getOne($value['admin_uid'],'username');
  52. // 重组
  53. $list[$key] = $value;
  54. }
  55. // 分配数据
  56. $this->assign('empty', '<tr><td colspan="20">~~暂无数据</td></tr>');
  57. $this->assign('list', $list);
  58. // 加载模板
  59. return $this->fetch();
  60. }
  61. /**
  62. * 添加
  63. *
  64. * */
  65. public function add(Model $Model,Orders $Orders,OrdersProduct $OrdersProduct,Product $Product,CustomScore $CustomScore){
  66. // 获取编码转ID
  67. $orderId = $Orders->codeToId(request('order_code',''));
  68. // 客户ID
  69. $order = $Orders->query()->find($orderId,['id','custom_uid','status']);
  70. // post添加
  71. if( request()->isMethod('post') ){
  72. // 提示新增失败
  73. if( !$orderId ) return json_send(['code'=>'error','msg'=>'请核对订单编码']);
  74. // 客户ID
  75. $order = $Orders->query()->find($orderId,['id','custom_uid','status']);
  76. // 提示新增失败
  77. if( !$order ) return json_send(['code'=>'error','msg'=>'订单不存在']);
  78. // 转数组
  79. $order = $order->toArray();
  80. // 物流编号
  81. $data['track_number'] = request('track_number','');
  82. $data['order_id'] = $orderId;
  83. $data['admin_uid'] = admin('uid');
  84. // 组合数据,写入订单表,子表
  85. DB::beginTransaction();
  86. try {
  87. // 写入
  88. $id = $Model->add($data);
  89. // 提示新增失败
  90. if( !$id ) {
  91. // 回滚
  92. DB::rollBack();
  93. // 添加失败
  94. return json_send(['code'=>'error','msg'=>'新增出库失败']);
  95. }
  96. // 修改订单状态为已完成
  97. $result = $Orders->edit($orderId,['status'=>8]);
  98. // 提示新增失败
  99. if( !$result ) {
  100. // 回滚
  101. DB::rollBack();
  102. // 添加失败
  103. return json_send(['code'=>'error','msg'=>'主订单状态修改失败']);
  104. }
  105. // 修改为已发货
  106. $result = $OrdersProduct->query()->where([['order_id','=',$orderId]])->update(['status'=>8,'update_time'=>time()]);
  107. // 提示新增失败
  108. if( !$result ) {
  109. // 回滚
  110. DB::rollBack();
  111. // 添加失败
  112. return json_send(['code'=>'error','msg'=>'订单状态修改失败']);
  113. }
  114. // 如果订单的状态是待完成,给客户发放下单积分
  115. // if( $order['status'] == 1 ) $CustomScore->trade($order['custom_uid'],$orderId,config('give_orders_score',0),5,1);
  116. // 提交事务
  117. DB::commit();
  118. } catch (\Throwable $th) {
  119. // 回滚
  120. DB::rollBack();
  121. // 添加失败
  122. return json_send(['code'=>'error','msg'=>'发货失败','data'=>['error'=>$th->getMessage()]]);
  123. }
  124. // 记录行为
  125. $this->addAdminHistory(admin('uid'),$Model->getTable(),$id,1,[],$data);
  126. // 告知结果
  127. return json_send(['code'=>'success','msg'=>'发货成功','action'=>'add']);
  128. }
  129. // 查询数据
  130. $orderList = $OrdersProduct->query()->where([['order_id','=',$orderId]])->get(['order_id','product_id','buy_num','pay_total','sku_attr_names as product_spec'])->toArray();
  131. // 如果有数据的话
  132. foreach ($orderList as $key => $value) {
  133. // 订单编码
  134. $value['product_code'] = $Product->idToCode($value['product_id']);
  135. // 订单编码
  136. $value['order_code'] = $Orders->idToCode($value['order_id']);
  137. // 订单编码
  138. $value['product_name'] = $Product->getOne($value['product_id'],'name');
  139. // 返回结果
  140. $orderList[$key] = $value;
  141. }
  142. // 分配数据
  143. $this->assign('crumbs','发货');
  144. $this->assign('orderList',$orderList);
  145. // 加载模板
  146. return $this->fetch();
  147. }
  148. }