123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 |
- <?php namespace App\Http\Controllers\Admin;
- use App\Models\AdminUser;
- use App\Models\Orders;
- use App\Models\Product;
- use App\Models\OrdersProduct;
- use App\Models\OrdersTransport as Model;
- use App\Models\CustomScore;
- use Illuminate\Support\Carbon;
- use Illuminate\Support\Facades\DB;
- /**
- * 订单物流
- *
- * @author 刘相欣
- *
- */
- class OrdersTransport extends Auth{
-
- protected function _initialize(){
- parent::_initialize();
- $this->assign('breadcrumb1','订单物流');
- $this->assign('breadcrumb2','物流发货');
- }
- /**
- * 首页列表
- *
- * */
- public function index(Model $Model,AdminUser $AdminUser,Orders $Orders){
- // 接受参数
- $code = request('out_code','');
- $adminName = request('admin_name','');
- $startTime = request('start_time','');
- $endTime = request('end_time','');
- // 编码转ID
- $id = $code ? $Model->codeToId($code) : 0;
- $adminId = $AdminUser->getByName($adminName,'uid');
- // 查询条件
- $map = [];
- // 编码ID
- if( $id ) $map[] = ['orders_transport.id','=',$id];
- if( $adminId ) $map[] = ['admin_uid','=',$adminId];
- if( $startTime ) $map[] = ['stock.insert_time','>=',Carbon::createFromFormat('Y-m-d',$startTime)->startOfDay()->getTimestamp()];
- if( $endTime ) $map[] = ['stock.insert_time','<=',Carbon::createFromFormat('Y-m-d',$endTime)->endOfDay()->getTimestamp()];
- // 查询数据
- $list = $Model->query()->where($map)->paginate(request('limit',config('page_num',10)))->appends(request()->all());
- // 循环处理数据
- foreach ($list as $key => $value) {
- // id转编号
- $value['out_code'] = $Model->idToCode($value['id']);
- // id转编号
- $value['order_code'] = $Orders->idToCode($value['order_id']);
- // 创建人员
- $value['admin_name'] = $AdminUser->getOne($value['admin_uid'],'username');
- // 重组
- $list[$key] = $value;
- }
- // 分配数据
- $this->assign('empty', '<tr><td colspan="20">~~暂无数据</td></tr>');
- $this->assign('list', $list);
- // 加载模板
- return $this->fetch();
- }
- /**
- * 添加
- *
- * */
- public function add(Model $Model,Orders $Orders,OrdersProduct $OrdersProduct,Product $Product,CustomScore $CustomScore){
- // 获取编码转ID
- $orderId = $Orders->codeToId(request('order_code',''));
- // 客户ID
- $order = $Orders->query()->find($orderId,['id','custom_uid','status']);
- // post添加
- if( request()->isMethod('post') ){
- // 提示新增失败
- if( !$orderId ) return json_send(['code'=>'error','msg'=>'请核对订单编码']);
- // 客户ID
- $order = $Orders->query()->find($orderId,['id','custom_uid','status']);
- // 提示新增失败
- if( !$order ) return json_send(['code'=>'error','msg'=>'订单不存在']);
- // 转数组
- $order = $order->toArray();
- // 物流编号
- $data['track_number'] = request('track_number','');
- $data['order_id'] = $orderId;
- $data['admin_uid'] = admin('uid');
- // 组合数据,写入订单表,子表
- DB::beginTransaction();
- try {
- // 写入
- $id = $Model->add($data);
- // 提示新增失败
- if( !$id ) {
- // 回滚
- DB::rollBack();
- // 添加失败
- return json_send(['code'=>'error','msg'=>'新增出库失败']);
- }
- // 修改订单状态为已完成
- $result = $Orders->edit($orderId,['status'=>8]);
- // 提示新增失败
- if( !$result ) {
- // 回滚
- DB::rollBack();
- // 添加失败
- return json_send(['code'=>'error','msg'=>'主订单状态修改失败']);
- }
- // 修改为已发货
- $result = $OrdersProduct->query()->where([['order_id','=',$orderId]])->update(['status'=>8,'update_time'=>time()]);
- // 提示新增失败
- if( !$result ) {
- // 回滚
- DB::rollBack();
- // 添加失败
- return json_send(['code'=>'error','msg'=>'订单状态修改失败']);
- }
- // 如果订单的状态是待完成,给客户发放下单积分
- // if( $order['status'] == 1 ) $CustomScore->trade($order['custom_uid'],$orderId,config('give_orders_score',0),5,1);
- // 提交事务
- DB::commit();
- } catch (\Throwable $th) {
- // 回滚
- DB::rollBack();
- // 添加失败
- return json_send(['code'=>'error','msg'=>'发货失败','data'=>['error'=>$th->getMessage()]]);
- }
- // 记录行为
- $this->addAdminHistory(admin('uid'),$Model->getTable(),$id,1,[],$data);
- // 告知结果
- return json_send(['code'=>'success','msg'=>'发货成功','action'=>'add']);
- }
- // 查询数据
- $orderList = $OrdersProduct->query()->where([['order_id','=',$orderId]])->get(['order_id','product_id','buy_num','pay_total','sku_attr_names as product_spec'])->toArray();
- // 如果有数据的话
- foreach ($orderList as $key => $value) {
- // 订单编码
- $value['product_code'] = $Product->idToCode($value['product_id']);
- // 订单编码
- $value['order_code'] = $Orders->idToCode($value['order_id']);
- // 订单编码
- $value['product_name'] = $Product->getOne($value['product_id'],'name');
- // 返回结果
- $orderList[$key] = $value;
- }
- // 分配数据
- $this->assign('crumbs','发货');
- $this->assign('orderList',$orderList);
- // 加载模板
- return $this->fetch();
- }
-
- }
|