AdminLoseRentController.php 6.8 KB
<?php
// +----------------------------------------------------------------------
// | bronet [ 以客户为中心 以奋斗者为本 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.bronet.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 老猫 <bronet@126.com>
// +----------------------------------------------------------------------
namespace app\portal\controller;

use app\portal\model\AddRentModel;
use app\portal\model\ChangeRentModel;
use app\portal\model\ConditionModel;
use app\portal\model\LoseRentModel;
use app\portal\model\RentModel;
use app\portal\model\VehicleModel;
use cmf\controller\AdminBaseController;
use app\portal\model\PortalPostModel;
use app\portal\service\PostService;
use app\portal\model\PortalCategoryModel;
use think\Db;
use app\admin\model\ThemeModel;
use think\Loader;


class AdminLoseRentController extends AdminBaseController
{
    /**
     * 报失器械申请
     * @adminMenu(
     *     'name'   => '报失器械申请',
     *     'parent' => 'portal/AdminRentStatus/default',
     *     'display'=> true,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '报失器械申请',
     *     'param'  => ''
     * )
     */
    public function index()
    {
        $data = $this->request->param();
        $name_arr = array();
        $return = $this->adminIndex($data);
        $ret = $return['page_arr'];
        if(!empty($return['where_arr']['create_time'])){
            $where_change['l.create_time'] = $return['where_arr']['create_time'];
        }
        //        商品名
        if(!empty($data['keyword'])){
            $where_change['l.goods_num'] = array('like','%'.$data['keyword'].'%');
            $ret['keyword'] = $data['keyword'];
            $this->assign('keyword',$data['keyword']);
        }
        //        店铺名
        if(!empty($data['store'])){
            $ret['store'] = $data['store'];
            $where_change['l.store_id'] = $data['store'];
            $this->assign('store_id',$data['store']);
        }
        $where_change['l.status'] = array('neq',9);
        $list = Db::name('LoseRent')->alias('l')
            ->where($where_change)
            ->join('Store s','s.id = l.store_id')
            ->join('Vehicle v','v.id = l.vehicle_id')
            ->field('v.name , s.store_name,s.id as store_id ,l.*')
            ->order('l.create_time desc')
            ->paginate(10,false,['query'=>$ret]);
        $where_store['status'] = array('neq',9);
        $store= Db::name('Store')
            ->where($where_store)
            ->select()->toArray();
        $page = $list->render();
        $this->assign('page', $page);
        $this->assign('list', $list);
        $this->assign('store', $store);
        return $this->fetch();
    }



    /**
     * 报失申请审核
     * @adminMenu(
     *     'name'   => '增加申请审核',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '增加申请审核',
     *     'param'  => ''
     * )
     */
    public function agree()
    {
        Db::startTrans();
        $data   = $this->request->param();
        $model = new LoseRentModel();
        $where_status['id'] = $data['id'];
        $where_status['status'] = 2;
        $status = $model->where($where_status)->find();
        if($status){
            if($data['status'] == 3){
//                同意
//                减库存
//                商品
                $rentModel = new  RentModel();
                $where_rent['goods_id'] = $status['vehicle_id'];
                $where_rent['goods_num'] = $status['goods_num'];
                $res_rentId = $rentModel->where($where_rent)->find();
                if(!$res_rentId){
                    Db::rollback();
                    $this->apiResponse('0','操作失败');
                }
                $up_rent['status'] = 9;
                $up_rent['update_time'] = time();
                $res_rent = $rentModel->where($where_rent)->update($up_rent);
                if(!$res_rent){
                    Db::rollback();
                    $this->apiResponse('0','操作失败');
                }
                $where_ve['id'] = $status['vehicle_id'];
                $res_now = Db::name('Vehicle')->where($where_ve)->setDec('now_stock',1);
                $res_ve = Db::name('Vehicle')->where($where_ve)->setDec('stock',1);
                if(!$res_now || !$res_ve){
                    Db::rollback();
                    $this->apiResponse('0','操作失败');
                }
//                店铺
                $where_store['id'] = $status['store_id'];
                $where_store['vehicle_id'] = $status['vehicle_id'];
                $store = Db::name('StoreVehicle')->where($where_store)->find();
                $arr = array();
                if($store){
                    $exp = explode('+',$store['rent_id']);
                    foreach ($exp as $k=>$v){
                        if($v != $res_rentId['id']){
                            $arr[] = $v;
                        }
                    }
                    $up_store['rent_id'] = implode('+',$arr);
                    $up_store['stock'] = $store['stock']-1;
                    $up_store['now_stock'] = $store['now_stock']-1;
                    $res_store = Db::name('StoreVehicle')->where($where_store)->update($up_store);
                    if(!$res_store){
                        Db::rollback();
                        $this->apiResponse('0','操作失败');
                    }
                }else{
                    Db::rollback();
                    $this->apiResponse('0','操作失败');
                }
            }

            $update['status'] = $data['status'];
            $res = $model->where($where_status)->update($update);
            if($res){
                Db::commit();
                $this->apiResponse('1','操作成功');
            }else{
                Db::rollback();
                $this->apiResponse('0','操作失败');
            }
        }else{
            Db::rollback();
            $this->apiResponse('0','状态错误');
        }
    }

    /**
     * 删除报失申请
     * @adminMenu(
     *     'name'   => '删除申请',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '删除申请',
     *     'param'  => ''
     * )
     */
    public function del()
    {
        $model = new AddRentModel();
        $this->adminDel($model);
    }





}