quanwei
2 days ago 73b874c72ad55eb9eef21c36160ac0de58f0189e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
<?php
 
namespace app\api\controller\takeout;
 
use app\api\controller\Controller;
use app\common\library\easywechat\AppMp;
use app\api\model\store\Store as StoreModel;
use app\common\model\store\Order as StoreOrderModel;
use app\api\model\store\Clerk as ClerkModel;
use app\supplier\model\store\Clerk as supplierClerkModel;
use app\api\model\order\Order as OrderModel;
use app\api\model\supplier\Supplier as SupplierModel;
use app\api\service\pay\PayService;
use app\common\model\store\Clerkcash as CashModel;
use app\common\exception\BaseException;
use app\api\model\settings\Message as MessageModel; // by lyzflash
use app\api\model\user\UserAuth;
 
use app\common\model\takeout\Commander as CommanderModel;
use app\api\model\takeout\Deliveryman as DeliverymanModel;
 
/**
 * 门店列表
 */
class Deliveryman extends Controller
{
    private $user;
    private $deliveryman; // 配送员
 
    /**
     * 构造方法
     */
    public function initialize()
    {
        parent::initialize();
        $this->user = $this->getUser();   // 用户信息
        $deliveryman = DeliverymanModel::detail($this->user['user_id']);
        if (empty($deliveryman)) {
            throw new BaseException(['msg' => '您还不是配送员']);
        }
        $this->deliveryman = $deliveryman;
    }
 
    /**
     * 门店详情
     */
    public function detail()
    {
        $user = $this->getUser();
        $detail =ClerkModel::detail(['user_id'=>$user['user_id']]);
        if (empty($detail)) {
            return $this->renderError('您还不是配送员');
        }
        $store_id=$detail['store_id'];
        $store = StoreModel::detail($store_id);
        $settlement = $this->getSet();
        $payType=$settlement['pay_type'];
        return $this->renderSuccess('', compact('detail','payType','store'));
    }
    //成交数据
    public function tradeData($url = '', $platform= '')
    {
        $data = $this->postData();
        $user = $this->getUser();
       
        $detail =ClerkModel::detail(['user_id'=>$user['user_id']]);
        if (empty($detail)) {
            return $this->renderError('您还不是配送员');
        }
        $store_id=$detail['store_id'];
        $store =StoreModel::detail($store_id);
         
        if (!$store['shop_supplier_id'] > 0) {
            return $this->renderError('未找到店铺');
        }
        //累积成交笔数
        $totalCount = OrderModel::getTotalPayOrderByStore($store['store_id'],$detail['clerk_id']);
        //今日成交笔数
        $todayCount = OrderModel::getTodayPayOrderByStore($store['store_id'],$detail['clerk_id']);
        //累积领取
        $supplier = SupplierModel::detail($store['shop_supplier_id']);
        // 订单统计 by lyzflash
        $model = new OrderModel;
        $orderCount = [
            'delivery' => $model->getCount(false, 'delivery', ['clerk_id' => $this->clerk['clerk_id']]),
            'received' => $model->getCount(false, 'received', ['clerk_id' => $this->clerk['clerk_id']]),
            // 'refund' => $model->getCount(false, 'refund', ['store_id' => $store_id]),
        ];
        //如果来源是小程序, 则获取小程序订阅消息id.获取订单通知.
        $temlIds = MessageModel::getMessageByNameArr($platform, ['delivery_new_order']);
        return $this->renderSuccess('', compact('totalCount','store' ,'todayCount', 'supplier','detail','temlIds', 'orderCount'));
    }
    /**
     * 我的订单列表
     */
    public function order($dataType)
    {
        $data = $this->postData();
        $model = new OrderModel;
        $user = $this->getUser();
        
        $list = $model->getList($user['user_id'], $dataType, $data);
        $show_alipay = PayService::isAlipayOpen($data['pay_source'], $user['app_id']);
        return $this->renderSuccess('', compact('list', 'show_alipay'));
    }
    /**
     * 提交申请
     */
    public function cashsubmit($data)
    {
        $user = $this->getUser();
        $detail =ClerkModel::detail(['user_id'=>$user['user_id']]);
        if (empty($detail)) {
            return $this->renderError('您还不是配送员');
        }
        
        $formData = json_decode(htmlspecialchars_decode($data), true);
         
        // 数据验证
        $this->validation($detail, $formData);
        
        // 新增申请记录
        $CashModel=new CashModel;
        $CashModel->save(array_merge($formData, [
            'user_id' => $user['user_id'],
            'apply_status' => 10,
            'app_id' => $user['app_id'],
        ]));
        // 冻结用户资金
        if ( $detail->freezeMoney($formData['money'])) {
            return $this->renderSuccess('成功');
        }
        return $this->renderError('提交失败');
    }
    private function getSet()
    {
        //提现方式 10微信 20支付宝 30银行卡
        $payType=['10','20','30'];
        $settlement=['pay_type'=>$payType,'min_money'=>0.01];
        return $settlement;
    }
    /**
     * 数据验证
     */
    private function validation($detail, $data)
    {
        // 结算设置
        $settlement = $this->getSet();
        // 最低提现佣金
        if ($data['money'] <= 0) {
            throw new BaseException(['msg' => '提现金额不正确']);
        }
        if ($detail['money'] <= 0) {
            throw new BaseException(['msg' => '当前用户没有可提现佣金']);
        }
        if ($data['money'] > $detail['money']) {
            throw new BaseException(['msg' => '提现金额不能大于可提现佣金']);
        }
        if ($data['money'] < $settlement['min_money']) {
            throw new BaseException(['msg' => '最低提现金额为' . $settlement['min_money']]);
        }
        if (!in_array($data['pay_type'], $settlement['pay_type'])) {
            throw new BaseException(['msg' => '提现方式不正确']);
        }
        if ($data['pay_type'] == '20') {
            if (empty($data['alipay_name']) || empty($data['alipay_account'])) {
                throw new BaseException(['msg' => '请补全提现信息']);
            }
        } elseif ($data['pay_type'] == '30') {
            if (empty($data['bank_name']) || empty($data['bank_account']) || empty($data['bank_card'])) {
                throw new BaseException(['msg' => '请补全提现信息']);
            }
        }elseif ($data['pay_type'] == '10') {
            //微信支付需要实名认证
            $auth = UserAuth::detail($detail['user_id']);
            if(empty($auth)){
                throw new BaseException(['msg' => '请先到个人中心->设置->实名认证']);
            }elseif(!empty($auth) && $auth["auth_status"] != 1){
                throw new BaseException(['msg' => '您的实名认证还未审核通过']);
            }
        }
    }
    /**
     * 提现明细
     */
    public function cashlists($apply_status = -1)
    {
        $user = $this->getUser();
        $user_id=$user['user_id'];
        
        $model = new CashModel;
        $apply_status > -1 && $model = $model->where('apply_status', '=', $apply_status);
        $list=$model->where('user_id', '=', $user_id)->order(['create_time' => 'desc'])
            ->paginate(15);
            
        return $this->renderSuccess('', [
            // 提现明细列表
            'list' => $list
        ]);
    }
    /**
     * 结算订单列表
     */
    public function orderlists()
    {
        $user = $this->getUser();
        $user_id=$user['user_id'];
        $Clerk =ClerkModel::detail(['user_id'=>$user['user_id']]);
        if (empty($Clerk)) {
            return $this->renderError('您还不是配送员');
        }
        $store_id=$Clerk['store_id'];
        $clerk_id = $Clerk['clerk_id'];
        $model = new StoreOrderModel;
        return $this->renderSuccess('', [
            // 提现明细列表
            'list' => $model->getSettled($store_id, (int)$clerk_id),
        ]);
    }
    /**
     * 我的配送员列表
     */
    public function teamlists()
    {
        $user = $this->getUser();
        $ClerkGly =ClerkModel::detail(['user_id'=>$user['user_id'],'type'=>20]);
        if (empty($ClerkGly)) {
            return $this->renderError('您还不是门店管理员');
        }
        $store_id=$ClerkGly['store_id'];
        $detail = StoreModel::detail($store_id);
        
        $data = $this->postData();
        $data['shop_supplier_id'] = $detail['shop_supplier_id'];
        $search=$data['search'];
        $model = new supplierClerkModel;
        return $this->renderSuccess('', [
            // 队长用户信息
            'store' =>$detail,
            // 我的团队列表
            'list' => $model->getList(0,$store_id,$search,$data),
        ]);
    }
 
    /**
     * 确认配送 by lyzflash
     */
    public function orderDelivery($order_id)
    {
        $model = new OrderModel;
        $model = $model->detail(['order_id' => $order_id, 'extract_clerk_id' => $this->clerk['clerk_id']]);
        if (empty($model)) {
            return $this->renderError('没有找到订单');
        }
        // 将订单改为已发货状态
        if ($model->save(['delivery_status' => 20, 'delivery_time' => time()]) ) {
            return $this->renderSuccess('成功');
        }
        return $this->renderError($model->getError() ?: '操作失败');
    }
 
}