闫增涛
2025-04-10 298f8b963162a767a8b9c3905123e68bca1e39ea
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
<?php
/**
+-----------------------------------------------------------------------------------------------
* GouGuOPEN [ 左手研发,右手开源,未来可期!]
+-----------------------------------------------------------------------------------------------
* @Copyright (c) 2021~2024 http://www.gouguoa.com All rights reserved.
+-----------------------------------------------------------------------------------------------
* @Licensed 勾股OA,开源且可免费使用,但并不是自由软件,未经授权许可不能去除勾股OA的相关版权信息
+-----------------------------------------------------------------------------------------------
* @Author 勾股工作室 <hdm58@qq.com>
+-----------------------------------------------------------------------------------------------
*/
declare (strict_types = 1);
namespace app\contract\controller;
 
use app\api\BaseController;
use app\contract\model\Contract;
use app\contract\model\Purchase;
use app\contract\model\SupplierContact;
use think\facade\Db;
use think\facade\View;
 
class Api extends BaseController
{
    //获取销售合同协议
    public function get_contract()
    {
        $param = get_params();
        $uid = $this->uid;
        $where = array();
        $whereOr = array();
        if (!empty($param['keywords'])) {
            $where[] = ['id|name|code', 'like', '%' . $param['keywords'] . '%'];
        }
        $where[] = ['delete_time', '=', 0];
        $where[] = ['check_status', '=', 2];        
        //是否是合同管理员
        $auth = isAuth($uid,'contract_admin','conf_1');
        if($auth == 0){
            $whereOr[] =['admin_id|prepared_uid|sign_uid|keeper_uid', '=', $uid];
            $whereOr[] = ['', 'exp', Db::raw("FIND_IN_SET('{$uid}',share_ids)")];
            $whereOr[] = ['', 'exp', Db::raw("FIND_IN_SET('{$uid}',check_uids)")];
            $whereOr[] = ['', 'exp', Db::raw("FIND_IN_SET('{$uid}',check_history_uids)")];
            $dids_a = get_leader_departments($uid);
            $dids_b = get_role_departments($uid);
            $dids = array_merge($dids_a, $dids_b);
            if(!empty($dids)){
                $whereOr[] = ['did','in',$dids];
            }
        }
        $model = new Contract();
        $list = $model->datalist($param,$where,$whereOr);
        return table_assign(0, '', $list);
    }
 
    //销售合同归档操作
    public function contract_archive()
    {
        if (request()->isPost()) {
            $param = get_params();
            $tips='操作成功,合同已转入到归档合同列表';
            if($param['archive_status'] == 1){
                $param['archive_uid'] = $this->uid;
                $param['archive_time'] = time();
            }
            else{
                $param['archive_uid'] = 0;
                $param['archive_time'] = 0;
                $tips='操作成功,合同已从归档合同列表转出';
            }
            if (Db::name('Contract')->strict(false)->update($param) !== false) {
                return to_assign(0, $tips);
            } else {
                return to_assign(1, "操作失败");
            }
        } else {
            return to_assign(1, "错误的请求");
        }
    }
    
    //销售合同中止等操作
    public function contract_stop()
    {
        if (request()->isPost()) {
            $param = get_params();
            $tips='操作成功,合同已转入到中止合同列表';
            if($param['stop_status'] == 1){
                $param['stop_uid'] = $this->uid;
                $param['stop_time'] = time();
            }
            else{
                $param['stop_uid'] = 0;
                $param['stop_time'] = 0;
                $tips='操作成功,合同已从中止合同列表转出';
            }
            if (Db::name('Contract')->strict(false)->update($param) !== false) {
                return to_assign(0, $tips);
            } else {
                return to_assign(1, "操作失败");
            }
        } else {
            return to_assign(1, "错误的请求");
        }
    }
    //销售合同作废等操作
    public function contract_tovoid()
    {
        if (request()->isPost()) {
            $param = get_params();
            $tips='操作成功,合同已转入到作废合同列表';
            if($param['void_status'] == 1){
                $param['void_uid'] = $this->uid;
                $param['void_time'] = time();
            }
            else{
                $param['void_uid'] = 0;
                $param['void_time'] = 0;
                $tips='操作成功,合同已从作废合同列表转出';
            }
            if (Db::name('Contract')->strict(false)->update($param) !== false) {
                return to_assign(0, $tips);
            } else {
                return to_assign(1, "操作失败");
            }
        } else {
            return to_assign(1, "错误的请求");
        }
    }
    
    //获取产品分类数据
    public function get_productcate_tree()
    {
        $cate = get_base_data('ProductCate');
        $list = get_tree($cate, 0, 2);
        $data['trees'] = $list;
        return json($data);
    }
    
    //获取销售产品列表
    public function get_product()
    {
        $param = get_params();
        $where = array();
        if (!empty($param['keywords'])) {
            $where[] = ['id|title', 'like', '%' . $param['keywords'] . '%'];
        }
        $where[] = ['delete_time', '=', 0];
        $rows = empty($param['limit']) ? get_config('app.page_size') : $param['limit'];
        $list = Db::name('Product')->field('id,title,sale_price,purchase_price,base_price,unit,specs')->order('id asc')->where($where)->paginate(['list_rows'=> $rows]);
        table_assign(0, '', $list);
    }
    
    /*
    -------------------------------------------------分割线---------------------------------------------------------
    */
    //获取采购合同协议
    public function get_purchase()
    {
        $param = get_params();
        $uid = $this->uid;
        $where = array();
        $whereOr = array();
        if (!empty($param['keywords'])) {
            $where[] = ['id|name|code', 'like', '%' . $param['keywords'] . '%'];
        }
        $where[] = ['delete_time', '=', 0];
        $where[] = ['check_status', '=', 2];
        $auth = isAuth($uid,'contract_admin','conf_1');
        if($auth == 0){
            $whereOr[] =['admin_id|prepared_uid|sign_uid|keeper_uid', '=', $uid];
            $whereOr[] = ['', 'exp', Db::raw("FIND_IN_SET('{$uid}',share_ids)")];
            $whereOr[] = ['', 'exp', Db::raw("FIND_IN_SET('{$uid}',check_uids)")];
            $whereOr[] = ['', 'exp', Db::raw("FIND_IN_SET('{$uid}',check_history_uids)")];
            $dids_a = get_leader_departments($uid);
            $dids_b = get_role_departments($uid);
            $dids = array_merge($dids_a, $dids_b);
            if(!empty($dids)){
                $whereOr[] = ['did','in',$dids];
            }
        }
        $model = new Purchase();
        $list = $model->datalist($param,$where,$whereOr);
        return table_assign(0, '', $list);
    }
 
    //采购合同归档操作
    public function purchase_archive()
    {
        if (request()->isPost()) {
            $param = get_params();
            $tips='操作成功,合同已转入到归档合同列表';
            if($param['archive_status'] == 1){
                $param['archive_uid'] = $this->uid;
                $param['archive_time'] = time();
            }
            else{
                $param['archive_uid'] = 0;
                $param['archive_time'] = 0;
                $tips='操作成功,合同已从归档合同列表转出';
            }
            if (Db::name('Purchase')->strict(false)->update($param) !== false) {
                return to_assign(0, $tips);
            } else {
                return to_assign(1, "操作失败");
            }
        } else {
            return to_assign(1, "错误的请求");
        }
    }
    
    //采购合同中止等操作
    public function purchase_stop()
    {
        if (request()->isPost()) {
            $param = get_params();
            $tips='操作成功,合同已转入到中止合同列表';
            if($param['stop_status'] == 1){
                $param['stop_uid'] = $this->uid;
                $param['stop_time'] = time();
            }
            else{
                $param['stop_uid'] = 0;
                $param['stop_time'] = 0;
                $tips='操作成功,合同已从中止合同列表转出';
            }
            if (Db::name('Purchase')->strict(false)->update($param) !== false) {
                return to_assign(0, $tips);
            } else {
                return to_assign(1, "操作失败");
            }
        } else {
            return to_assign(1, "错误的请求");
        }
    }
    //采购合同作废等操作
    public function purchase_tovoid()
    {
        if (request()->isPost()) {
            $param = get_params();
            $tips='操作成功,合同已转入到作废合同列表';
            if($param['void_status'] == 1){
                $param['void_uid'] = $this->uid;
                $param['void_time'] = time();
            }
            else{
                $param['void_uid'] = 0;
                $param['void_time'] = 0;
                $tips='操作成功,合同已从作废合同列表转出';
            }
            if (Db::name('Purchase')->strict(false)->update($param) !== false) {
                return to_assign(0, $tips);
            } else {
                return to_assign(1, "操作失败");
            }
        } else {
            return to_assign(1, "错误的请求");
        }
    }
    
    //获取采购品分类数据
    public function get_purchasedcate_tree()
    {
        $cate = get_base_data('PurchasedCate');
        $list = get_tree($cate, 0, 2);
        $data['trees'] = $list;
        return json($data);
    }
    
    //获取供应商列表
    public function get_supplier()
    {
        $param = get_params();
        $where = array();
        if (!empty($param['keywords'])) {
            $where[] = ['id|title', 'like', '%' . $param['keywords'] . '%'];
        }
        $where[] = ['delete_time', '=', 0];
        $rows = empty($param['limit']) ? get_config('app.page_size') : $param['limit'];
        $list = Db::name('Supplier')->field('id,title,address')->order('id asc')->where($where)->paginate(['list_rows'=> $rows])->each(function($item, $key){
            $contact = Db::name('SupplierContact')->where(['sid'=>$item['id'],'is_default'=>1])->find();
            if(!empty($contact)){
                $item['contact_name'] = $contact['name'];
                $item['contact_mobile'] = $contact['mobile'];
            }
            else{
                $item['contact_name'] = '';
                $item['contact_mobile'] = '';
            }
            return $item;
        });
        table_assign(0, '', $list);
    }
    
    //获取采购物品列表
    public function get_purchased()
    {
        $param = get_params();
        $where = array();
        if (!empty($param['keywords'])) {
            $where[] = ['id|title', 'like', '%' . $param['keywords'] . '%'];
        }
        $where[] = ['delete_time', '=', 0];
        $rows = empty($param['limit']) ? get_config('app.page_size') : $param['limit'];
        $list = Db::name('Purchased')->field('id,title,purchase_price,sale_price,unit,specs')->order('id asc')->where($where)->paginate(['list_rows'=> $rows]);
        table_assign(0, '', $list);
    }    
    
    //获取供应商联系人数据
    public function get_supplier_contact()
    {
        $param = get_params();
        $where = array();
        $where[] = ['delete_time', '=', 0];
        $where[] = ['sid', '=', $param['supplier_id']];
        $rows = empty($param['limit']) ? get_config('app.page_size') : $param['limit'];
        $content = SupplierContact::where($where)
            ->order('create_time desc')
            ->paginate(['list_rows'=> $rows])
            ->each(function ($item, $key) {                    
                $item->admin_name = Db::name('Admin')->where(['id' => $item->admin_id])->value('name');
                $item->create_time = date('Y-m-d H:i:s', (int) $item->create_time);
            });
        return table_assign(0, '', $content);
    }
    
    //设置供应商联系人
    public function set_supplier_contact()
    {
        if (request()->isAjax()) {
            $param = get_params();
            $detail= SupplierContact::where(['id' => $param['id']])->find();
            SupplierContact::where(['sid' => $detail['sid']])->strict(false)->field(true)->update(['is_default'=>0]);
            $res = SupplierContact::where(['id' => $param['id']])->update(['is_default'=>1]);
            if ($res) {
                add_log('edit', $param['id'], $param,'供应商联系人');
                return to_assign();
            } else {
                return to_assign(1, '操作失败');
            }
        } else {
           return to_assign(1, '参数错误');
        }
    }
}