123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170 |
- <?php
- namespace app\controller;
- use app\BaseController;
- use app\model\Account;
- use app\model\AccountCompany;
- use think\Exception;
- use think\facade\Db;
- use think\facade\Validate;
- use think\facade\Cache;
- //【公司汇总】
- class Headquarters extends BaseController
- {
- //列表
- public function getList()
- {
- $post = $this->request->only(['code' => '', 'name' => '', 'status' => '', 'page' => 1, 'size' => 10, 'level' => '', 'account_id' => '', 'type' => ''], 'post');
- $condition = [['is_del', '=', 0]];
- if ($post['code'] != '') $condition[] = is_array($post['code']) ? ['code', 'in', $post['code']] : ['code', 'like', "%{$post['code']}%"];
- if ($post['name'] != '') $condition[] = ['name', 'like', "%{$post['name']}%"];
- if ($post['status'] != '') $condition[] = ['status', '=', $post['status']];
- if ($post['level'] != 1) {
- $companyCode = Db::name('account_company')
- ->where(['account_id' => $post['account_id'], 'is_del' => 0, 'status' => 1])
- ->column('companyCode');
- $condition[] = ['code', 'in', $companyCode];
- }
- // if ($post['type'] !== '') $condition[] = ['', 'exp', Db::raw("FIND_IN_SET({$post['type']},type)")];
- if ($post['type'] !== '') $condition[] = ['type', '=', $post['type']];
- //兼容原有各个接口的筛选
- $count = Db::name('supplier')
- ->where($condition)
- ->count('id');
- $list = Db::name('headquarters')
- ->field('id,code,name,type,status,addtime,relation_code')
- ->where($condition)
- ->page($post['page'], $post['size'])
- ->order(['addtime' => 'desc', 'id' => 'desc'])
- ->select()
- ->toArray();
- return json_show(0, '获取成功', ['list' => $list, 'count' => $count]);
- }
- //添加
- public function add()
- {
- $post = $this->request->filter('trim')->post();
- Db::startTrans();
- try {
- switch ($post['type']) {
- case 1:
- Db::name('business')->insert($post['data']);
- break;
- case 2:
- Db::name('customer_info')->insert($post['data']);
- break;
- case 3:
- Db::name('supplier')->insert($post['data']);
- break;
- }
- Db::name('headquarters')
- ->insert([
- 'code' => $post['code'],
- 'name' => $post['name'],
- 'type' => $post['type'],
- 'invoice_title' => $post['invoice_title'],
- 'invoice_people' => $post['invoice_people'],
- 'invoice_addr' => $post['invoice_addr'],
- 'invoice_code' => $post['invoice_code'],
- 'invoice_bank' => $post['invoice_bank'],
- 'invoice_bankNo' => $post['invoice_bankNo'],
- 'invoice_img' => $post['invoice_img'],
- 'remark' => $post['remark'],
- 'status' => $post['status'],
- 'is_del' => 0,
- 'creater' => $post['creater'],
- 'createrid' => $post['createrid'],
- 'addtime' => date('Y-m-d H:i:s'),
- 'updater' => $post['updater'],
- 'updaterid' => $post['updaterid'],
- 'updatetime' => date('Y-m-d H:i:s'),
- ]);
- Db::commit();
- Cache::store("redis")->handler()->lpush("companycopy",$post);
- return json_show(0, '添加成功');
- } catch (Exception $e) {
- Db::rollback();
- return json_show(1002, '添加失败,' . $e->getMessage());
- }
- }
- //编辑
- public function update()
- {
- $post = $this->request->filter('trim')->post();
- Db::startTrans();
- try {
- switch ($post['type']) {
- case 1:
- Db::name('business')->where(['companyNo' => $post['data']['companyNo'], 'is_del' => 0])->update($post['data']);
- break;
- case 2:
- Db::name('customer_info')->where(['companyNo' => $post['data']['companyNo'], 'is_del' => 0])->update($post['data']);
- break;
- case 3:
- Db::name('supplier')->where(['code' => $post['data']['code'], 'is_del' => 0])->update($post['data']);
- break;
- }
- Db::name('headquarters')
- ->where(['code' => $post['code'], 'is_del' => 0])
- ->update([
- 'code' => $post['code'],
- 'name' => $post['name'],
- 'type' => $post['type'],
- 'invoice_title' => $post['invoice_title'],
- 'invoice_people' => $post['invoice_people'],
- 'invoice_addr' => $post['invoice_addr'],
- 'invoice_code' => $post['invoice_code'],
- 'invoice_bank' => $post['invoice_bank'],
- 'invoice_bankNo' => $post['invoice_bankNo'],
- 'invoice_img' => $post['invoice_img'],
- 'remark' => $post['remark'],
- 'updater' => $post['updater'],
- 'updaterid' => $post['updaterid'],
- 'updatetime' => date('Y-m-d H:i:s'),
- ]);
- Db::commit();
- Cache::store("redis")->handler()->lpush("companycopy",json_encode($post,JSON_UNESCAPED_UNICODE));
- return json_show(0, '修改成功');
- } catch (Exception $e) {
- Db::rollback();
- return json_show(1002, '修改失败,' . $e->getMessage());
- }
- }
- //详情
- public function info()
- {
- $code = $this->request->post('code', '', 'trim');
- if ($code == '') return json_show(1003, "参数 code 不能为空");
- $res = Db::name('headquarters')
- ->field(true)
- ->where(['code' => $code, 'is_del' => 0])
- ->findOrEmpty();
- if (empty($res)) return json_show(1004, '未找到相关汇总信息');
- switch ($res['type']) {
- case 1:
- $child = Db::name('business')
- ->field(true)
- ->where(['companyNo' => $code, 'is_del' => 0])
- ->findOrEmpty();
- break;
- case 2:
- $child = Db::name('customer_info')
- ->where(['companyNo' => $code, 'is_del' => 0])
- ->findOrEmpty();
- break;
- case 3:
- $child = Db::name('supplier')
- ->where(['code' => $code, 'is_del' => 0])
- ->findOrEmpty();
- break;
- }
- $res['child'] = $child;
- $res['relation_name'] =$res['relation_code']==""?'': Db::name('headquarters')->where('code',$res['relation_code'])
- ->value('name','');
- return json_show(0, '获取成功', $res);
- }
- //删除
- public function delete()
- {
- $param = $this->request->only(['ids', 'type', 'updater', 'updaterid'], 'post', 'trim');
- $val = Validate::rule([
- 'ids' => 'require|array|max:100',
- 'type|类别' => 'require|number|in:1,2,3',
- 'updater|操作人' => 'require|max:255',
- 'updaterid|操作人id' => 'require|number|gt:0',
- ]);
- if (!$val->check($param)) return json_show(1004, $val->getError());
- Db::startTrans();
- try {
- $date = date('Y-m-d H:i:s');
- $codes = [];
- switch ($param['type']) {
- case 1:
- $where = [['is_del', '=', 0], ['id', 'in', $param['ids']]];
- $codes = Db::name('business')->where($where)->column('companyNo');
- Db::name('business')->where($where)->update(['is_del' => 1, 'updatetime' => $date]);
- break;
- case 2:
- $where = [['is_del', '=', 0], ['id', 'in', $param['ids']]];
- $temp = Db::name('customer_info')->field('id,companyNo')->where($where)->where('status', 1)->findOrEmpty();
- if (!empty($temp)) throw new Exception($temp['companyNo'] . '启用状态,不允许删除');
- $codes = Db::name('customer_info')->where($where)->column('companyNo');
- Db::name('customer_info')
- ->where($where)
- ->update(['is_del' => 1, 'updatetime' => $date]);
- break;
- case 3:
- $where = [['is_del', '=', 0], ['id', 'in', $param['ids']]];
- $codes = Db::name('supplier')->where($where)->column('code');
- Db::name('supplier')->where($where)->update(['is_del' => 1, 'updatetime' => $date]);
- break;
- }
- Db::name('headquarters')
- ->where('is_del', 0)
- ->whereIn('code', $codes)
- ->update([
- 'is_del' => 1,
- 'updater' => $param['updater'],
- 'updaterid' => $param['updaterid'],
- 'updatetime' => date('Y-m-d H:i:s')
- ]);
- Db::commit();
- return json_show(0, '删除成功');
- } catch (Exception $exception) {
- Db::rollback();
- return json_show(1005, '删除失败,' . $exception->getMessage());
- }
- }
- //启禁用
- public function status()
- {
- $param = $this->request->only(['id', 'type', 'status', 'updater', 'updaterid'], 'post', 'trim');
- $val = Validate::rule([
- 'id' => 'require|number|gt:0',
- 'type|类别' => 'require|number|in:1,2,3',
- 'status|状态' => 'require|number|in:0,1',
- 'updater|操作人' => 'require|max:255',
- 'updaterid|操作人id' => 'require|number|gt:0',
- ]);
- if (!$val->check($param)) return json_show(1004, $val->getError());
- Db::startTrans();
- try {
- $date = date('Y-m-d H:i:s');
- $code = '';
- switch ($param['type']) {
- case 1:
- $tmp = Db::name('business')
- ->field('id,companyNo code,status')
- ->where(['id' => $param['id'], 'is_del' => 0])
- ->findOrEmpty();
- if (empty($tmp)) throw new Exception('未找到对应数据');
- if ($tmp['status'] == $param['status']) throw new Exception('操作重复');
- Db::name('business')
- ->where(['id' => $param['id'], 'is_del' => 0])
- ->where('status', '<>', $param['status'])
- ->update([
- 'status' => $param['status'],
- 'updatetime' => $date
- ]);
- break;
- case 2:
- $tmp = Db::name('customer_info')
- ->field('id,status,companyNo code')
- ->where(['is_del' => 0, 'id' => $param['id']])
- ->findOrEmpty();
- if (empty($tmp)) throw new Exception('未找到对应数据');
- if ($tmp['status'] == $param['status']) throw new Exception('操作重复');
- Db::name('customer_info')
- ->where(['id' => $param['id'], 'is_del' => 0])
- ->where('status', '<>', $param['status'])
- ->update([
- 'status' => $param['status'],
- 'updatetime' => $date
- ]);
- break;
- case 3:
- $tmp = Db::name('supplier')
- ->where(['id' => $param['id'], 'is_del' => 0])
- ->field('id,code,status')
- ->findOrEmpty();
- if (empty($tmp)) throw new Exception('该供应商不存在');
- if ($tmp['status'] == $param['status']) throw new Exception('操作重复');
- Db::name('supplier')
- ->where(['id' => $param['id'], 'is_del' => 0])
- ->where('status', '<>', $param['status'])
- ->update([
- 'status' => $param['status'],
- 'updatetime' => $date
- ]);
- break;
- }
- Db::name('headquarters')
- ->where(['code' => $tmp['code'], 'is_del' => 0])
- ->where('status', '<>', $param['status'])
- ->update([
- 'status' => $param['status'],
- 'updater' => $param['updater'],
- 'updaterid' => $param['updaterid'],
- 'updatetime' => $date
- ]);
- Db::commit();
- return json_show(0, '操作成功');
- } catch (Exception $exception) {
- Db::rollback();
- return json_show(1005, '操作失败,' . $exception->getMessage());
- }
- }
- //获取指定编码和名称
- public function getCodeAndName()
- {
- $companyNo = $this->request->filter('trim')->post('code');
- $res_1 = Db::name('supplier')
- ->where('is_del', 0)
- ->whereIn('code', $companyNo)
- ->column('name', 'code');
- // $res_2 = Db::name('headquarters')
- // ->where('is_del', 0)
- // ->whereIn('relation_code', $companyNo)
- // ->column('name', 'relation_code');
- return json_show(0, '请求成功', $res_1);
- }
- //业务公司
- public function bGetList()
- {
- $param = $this->request->only([
- 'page' => 1,
- 'size' => 10,
- 'company' => '',
- 'status' => '',
- 'creater' => '',
- 'start' => '',
- 'end' => '',
- 'company_name' => '',
- ], 'post', 'trim');
- $where = [['a.is_del', "=", 0]];
- if ($param['company'] !== "") $where[] = ["a.company", "like", '%' . $param['company'] . '%'];
- if ($param['status'] !== "") $where[] = ["a.status", '=', $param['status']];
- if ($param['creater'] !== "") $where[] = ["a.creater", "like", '%' . $param['creater'] . '%'];
- if ($param['start'] !== "") $where[] = ["a.addtime", '>=', $param['start']];
- if ($param['end'] !== "") $where[] = ["a.addtime", '<=', $param['end'] . ' 23:59:59'];
- $count = Db::name('business')
- ->alias('a')
- ->where($where)
- ->count('a.id');
- $list = Db::name('business')
- ->alias('a')
- ->field(true)
- ->where($where)
- ->page($param['page'], $param['size'])
- ->order(['addtime' => 'desc', 'id' => 'desc'])
- ->select()
- ->toArray();
- foreach ($list as &$value) {
- $value['company_name'] = '';
- }
- return json_show("0", "获取成功", ['list' => $list, 'count' => $count]);
- }
- public function bCreate()
- {
- $param = $this->request->filter('trim')->post('');
- Db::startTrans();
- try {
- $tmp = Db::name('business')
- ->field('id')
- ->where(['company' => $param['company'], 'is_del' => 0])
- ->findOrEmpty();
- if (!empty($tmp)) throw new Exception('该公司名称已存在');
- Db::name('business')->insert($param);
- Db::name('headquarters')->insert([
- 'code' => $param['companyNo'],
- 'name' => $param['company'],
- 'type' => 1,
- 'invoice_title' => '',
- 'invoice_people' => '',
- 'invoice_addr' => $param['inv_addr'],
- 'invoice_mobile' => $param['mobile'],
- 'invoice_code' => $param['inv_code'],
- 'invoice_bank' => $param['inv_bank'],
- 'invoice_bankNo' => $param['inv_bankNo'],
- 'invoice_img' => $param['license_img'],
- 'remark' => '',
- 'status' => $param['status'],
- 'is_del' => $param['is_del'],
- 'creater' => $param['creater'],
- 'createrid' => $param['createrid'],
- 'addtime' => $param['addtime'],
- 'updater' => $param['creater'],
- 'updaterid' => $param['createrid'],
- 'updatetime' => $param['updatetime'],
- ]);
- Db::commit();
- $param['type']=1;
- Cache::store("redis")->handler()->lpush("companycopy",json_encode($param,JSON_UNESCAPED_UNICODE));
- return json_show(0, '添加成功');
- } catch (Exception $exception) {
- Db::rollback();
- return json_show(1003, $exception->getMessage());
- }
- }
- public function bInfo()
- {
- $companyNo = $this->request->post('companyNo', '', 'trim');
- if ($companyNo == "") return json_show(1004, "参数companyNo不能为空");
- $info = Db::name('business')
- ->alias('a')
- ->field('a.*,b.company_type')
- ->leftJoin('company_type b', 'b.id=a.type AND b.is_del=0')
- ->where(['a.companyNo' => $companyNo, 'a.is_del' => 0])
- ->findOrEmpty();
- if (empty($info)) return json_show(1002, "未找到数据");
- return json_show(0, '获取成功', $info);
- }
- public function bEdit()
- {
- $param = $this->request->filter('trim')->post('');
- Db::startTrans();
- try {
- $info = Db::name('business')
- ->field('id,companyNo,company,contactor,inv_code,license_img')
- ->where(['id' => $param['id'], 'is_del' => 0])
- ->findOrEmpty();
- if (empty($info)) throw new Exception('未找到数据');
- Db::name('business')
- ->where(['id' => $param['id'], 'is_del' => 0])
- ->update(array_merge($param, ['updatetime' => date("Y-m-d H:i:s")]));
- Db::name('headquarters')
- ->where(['code' => $info['companyNo'], 'is_del' => 0])
- ->update([
- 'invoice_addr' => $param['inv_addr'],
- 'invoice_mobile' => $param['invoice_mobile'],
- 'invoice_bank' => $param['inv_bank'],
- 'invoice_bankNo' => $param['inv_bankNo'],
- 'invoice_title' => $param['invoice_title'],
- 'updatetime' => date("Y-m-d H:i:s"),
- ]);
- Db::commit();
- $param['type']=1;
-
- Cache::store("redis")->handler()->lpush("companycopy",json_encode(array_merge($info,$param),
- JSON_UNESCAPED_UNICODE));
- return json_show(0, '修改成功');
- } catch (Exception $exception) {
- Db::rollback();
- return json_show(1003, $exception->getMessage());
- }
- }
- public function bTitle()
- {
- $param = $this->request->only(['company_type' => '', 'status' => ''], 'post', 'trim');
- $rs = Db::name('company_type')->where('is_del', 0);
- if ($param['company_type'] != '') $rs->whereLike('company_type', '%' . $param['company_type'] . '%');
- if ($param['status'] != '') $rs->where('status', $param['status']);
- $list = $rs->select()->toArray();
- return json_show(0, '获取成功', $list);
- }
- //供应商
- public function sGetList()
- {
- $param = $this->request->only(['page' => 1, 'size' => 10, 'name' => '', 'code' => '', 'creater' => '', 'person' => '', 'status' => '', 'ocr_status' => '', 'start' => '', 'end' => '', 'company_name' => '', 'is_platform' => '', 'more_code' => ''], 'post', 'trim');
- $where = [['s.is_del', "=", 0]];
- if ($param['name'] !== "") $where[] = ["s.name", "like", '%' . $param['name'] . '%'];
- if ($param['code'] !== "") $where[] = ["s.code", "like", '%' . $param['code'] . '%'];
- if ($param['more_code'] !== "") $where[] = ["s.code", "in", $param['more_code']];
- if ($param['creater'] !== "") $where[] = ["s.creater", "like", '%' . $param['creater'] . '%'];
- if ($param['person'] !== "") $where[] = ["s.person", "like", '%' . $param['person'] . '%'];
- if ($param['status'] !== "") $where[] = ["s.status", '=', $param['status']];
- if ($param['ocr_status'] !== "") $where[] = ["s.ocr_status", '=', $param['ocr_status']];
- if ($param['start'] !== "") $where[] = ["s.addtime", '>=', $param['start']];
- if ($param['end'] !== "") $where[] = ["s.addtime", '<=', $param['end'] . ' 23:59:59'];
- if ($param['is_platform'] !== "") $where[] = ["s.is_platform", '=', $param['is_platform']];
- // if ($param['company_name'] !== "") $where[] = ["s.createrid", 'in', $param['company_name']];
- $count = Db::name('supplier')
- ->alias('s')
- // ->leftJoin("depart_user u", "u.uid=s.createrid AND u.is_del=0")
- ->where($where)
- ->count('s.id');
- $list = Db::name('supplier')
- ->alias('s')
- ->field(true)
- // ->field('s.*,u.itemid')
- // ->leftJoin("depart_user u", "u.uid=s.createrid AND u.is_del=0")
- ->where($where)
- ->page($param['page'], $param['size'])
- ->order("addtime desc")
- ->select()
- ->toArray();
- // $all_codes = Db::name('supplier_contact')
- // ->whereIn('code', array_column($list, 'code'))
- // ->column("id,contactor,mobile", 'code');
- //获取开通账号的供应商
- // $account = checkHasAccountBySupplierNos(array_column($list,'code'));
- foreach ($list as &$value) {
- $value['company_name'] = '';
- $value['has_account'] = (AccountCompany::alias("a")
- ->leftJoin('account b', 'b.id=a.account_id')
- ->where([ ['a.is_del', '=', 0], ['a.status', '=', 1], ['b.status', '=', 1],['b.level', '=', 3], ['a.companyCode', '=', $value['code']]])
- ->findOrEmpty()->isEmpty())==False;
- }
- return json_show("0", "获取成功", ['list' => $list, 'count' => $count]);
- }
- public function sCreate()
- {
- $param = $this->request->only(['data', 'contact'], 'post', 'trim');
- $val = Validate::rule([
- 'data|供应商数据' => 'require|array',
- 'contact|联系人' => 'require|array',
- ]);
- if ($val->check($param) == false) return json_show(1004, $val->getError());
- Db::startTrans();
- try {
- $tmp = Db::name('supplier')
- ->field('id')
- ->where(['name' => $param['data']['name'], 'is_del' => 0])
- ->findOrEmpty();
- if (!empty($tmp)) throw new Exception('该供应商名称已存在');
- $id = Db::name('supplier')->insertGetId($param['data']);
- Db::name('supplier_contact')->insert($param['contact']);
- Db::name('headquarters')->insert([
- 'code' => $param['data']['code'],
- 'name' => $param['data']['name'],
- 'type' => 3,
- 'invoice_title' => '',
- 'invoice_people' => '',
- 'invoice_addr' => '',
- 'invoice_mobile' => $param['contact']['mobile'],
- 'invoice_code' => '',
- 'invoice_bank' => '',
- 'invoice_bankNo' => '',
- 'invoice_img' => $param['data']['license_img'],
- 'remark' => $param['data']['remark'],
- 'status' => $param['data']['status'],
- 'is_del' => $param['data']['is_del'],
- 'creater' => $param['data']['creater'],
- 'createrid' => $param['data']['createrid'],
- 'addtime' => $param['data']['addtime'],
- 'updater' => $param['data']['creater'],
- 'updaterid' => $param['data']['createrid'],
- 'updatetime' => $param['data']['updatetime'],
- ]);
- Db::commit();
- $param['data']['type']=3;
- Cache::store("redis")->handler()->lpush("companycopy",json_encode($param['data'],JSON_UNESCAPED_UNICODE));
- return json_show(0, '添加成功', ['id' => $id]);
- } catch (Exception $exception) {
- Db::rollback();
- return json_show(1003, $exception->getMessage());
- }
- }
- public function sInfo()
- {
- $code = $this->request->post('code', '', 'trim');
- if ($code == "") return json_show(1004, "参数code不能为空");
- $info = Db::name('supplier')
- ->alias('a')
- ->field('a.*,b.contactor,b.mobile,b.position,b.email,b.telephone')
- ->leftJoin('supplier_contact b', 'b.code=a.code AND b.is_del=0')
- ->where(['a.code' => $code, 'a.is_del' => 0])
- ->findOrEmpty();
- if (empty($info)) return json_show(1002, "未找到供应商数据");
- return json_show(0, '获取成功', $info);
- }
- public function sEdit()
- {
- $param = $this->request->only(['data', 'contact'], 'post', 'trim');
- $val = Validate::rule([
- 'data|供应商数据' => 'require|array',
- 'contact|联系人' => 'require|array',
- ]);
- if ($val->check($param) == false) return json_show(1004, $val->getError());
- Db::startTrans();
- try {
- $tmp = Db::name('supplier')
- ->field('id')
- ->where(['name' => $param['data']['name'], 'is_del' => 0])
- ->where('id', '<>', $param['data']['id'])
- ->findOrEmpty();
- if (!empty($tmp)) throw new Exception('该供应商名称已存在');
- $id = Db::name('supplier')
- ->where(['id' => $param['data']['id'], 'is_del' => 0])
- ->strict(false)
- ->update($param['data']);
- $tmp = Db::name('supplier_contact')
- ->field('id')
- ->where(['code' => $param['contact']['code'], 'is_del' => 0])
- ->findOrEmpty();
- if (empty($tmp)) Db::name('supplier_contact')->insert(array_merge($param['contact'], ['addtime' => $param['contact']['updatetime']]));
- else Db::name('supplier_contact')->where(['id' => $tmp['id'], 'is_del' => 0])->update($param['contact']);
-
- Db::name('headquarters')
- ->where(['code' => $param['contact']['code'], 'is_del' => 0])
- ->update([
- 'name' => $param['data']['name'],
- 'type' => 3,
- 'invoice_title' => '',
- 'invoice_people' => '',
- 'invoice_addr' => '',
- 'invoice_mobile' => $param['contact']['mobile'],
- 'invoice_code' => '',
- 'invoice_bank' => '',
- 'invoice_bankNo' => '',
- 'invoice_img' => $param['data']['license_img'],
- 'remark' => $param['data']['remark'],
- 'is_del' => $param['data']['is_del'],
- 'updater' => $param['data']['updater'],
- 'updaterid' => $param['data']['updaterid'],
- 'updatetime' => date("Y-m-d H:i:s"),
- ]);
- $this->checkSupplier($param['data'],$param['contact']);
- Db::commit();
- $param['data']['type']=3;
- Cache::store("redis")->handler()->lpush("companycopy",json_encode($param['data'],JSON_UNESCAPED_UNICODE));
- return json_show(0, '修改成功', ['id' => $id]);
- } catch (Exception $exception) {
- Db::rollback();
- return json_show(1003, $exception->getMessage());
- }
- }
- //客户
- public function cInfo()
- {
- $companyNo = $this->request->post('companyNo', '', 'trim');
- $info = Db::name('customer_info')
- ->where(['is_del' => 0, 'companyNo' => $companyNo])
- ->field(true)
- ->findOrEmpty();
- $info['member'] = Db::name("customer_member")
- ->where(['companyNo' => $companyNo, "is_del" => 0])
- ->order('id')
- ->select()
- ->toArray();
- return json_show(0, "获取成功", $info);
- }
- public function cTitle()
- {
- $param = $this->request->only([
- 'page' => 1,
- 'size' => 10,
- 'companyNo' => '',
- 'companyName' => '',
- 'itemid' => '',
- ], 'post', 'trim');
- $where [] = ['is_del', "=", 0];
- if ($param['companyNo'] != '') $where[] = ['companyNo', 'like', '%' . $param['companyNo'] . '%'];
- if ($param['companyName'] != '') $where[] = ['companyName', 'like', '%' . $param['companyName'] . '%'];
- if ($param['itemid'] != '') $where[] = ['itemid', '=', $param['itemid']];
- $count = Db::name('customer_info')
- ->where($where)
- ->count('id');
- $item = Db::name('customer_info')
- ->where($where)
- ->field("id,companyNo,companyName,area,LENGTH(companyName) as weight,status")
- ->order(['weight' => 'asc', 'id' => 'desc'])
- ->page($param['page'], $param['size'])
- ->select()
- ->toArray();
- return json_show(0, "获取成功", ['item' => $item, 'count' => $count]);
- }
- public function cCreate()
- {
- $param = $this->request->only([
- 'companyName',
- 'parent' => 0,
- 'customer_member',
- 'uid',
- 'uname',
- 'branch',
- 'middle',
- 'area',
- ], 'post', 'trim');
- $val = Validate::rule([
- 'companyName|客户名称' => 'require',
- 'customer_member|联系方式' => 'require|array|max:100',
- 'uid' => 'require|number|gt:0',
- 'uname|创建人' => 'require',
- 'branch|省级' => 'require',
- 'middle|市级' => 'require',
- 'area|区域' => 'require',
- ]);
- if ($val->check($param) == false) return json_show(1004, $val->getError());
- $companyNo = makeNo("KH");
- $rename = Db::name('customer_org1')
- ->field('id')
- ->where(['is_del' => 0, 'id' => $param['parent']])
- ->findOrEmpty();
- $item = Db::name('customer_info')
- ->field('id')
- ->where(['itemid' => $rename['id'] ?? 0, 'companyName' => $param['companyName'], 'is_del' => 0])
- ->findOrEmpty();
- if (!empty($item)) return json_show(1002, "公司名称已存在");
- $createrid = $param['uid'];//isset($user["data"]['id']) ? $user["data"]['id'] : "";
- $creater = $param['uname'];//isset($user["data"]['nickname']) ? $user["data"]['nickname'] : "";
- Db::startTrans();
- try {
- $date = date('Y-m-d H:i:s');
- $data = [
- "companyNo" => $companyNo,
- "companyName" => $param['companyName'],
- "parent" => $param['parent'],
- "itemid" => $param['parent'],
- "area" =>$param['area'],
- "status" => 0,
- "branch" =>$param['branch'],
- "middle" =>$param['middle'],
- "is_del" => 0,
- "createrid" => $createrid,
- "creater" => $creater,
- "addtime" => $date,
- "updatetime" => $date,
- ];
- $datainfo = Db::name('customer_info')->insert($data);
- if ($datainfo) {
- $var = [];
- foreach ($param['customer_member'] as $value) {
- $var[] = [
- 'commobile' => $value['commobile'] ?? '',
- 'comtel' => '',
- 'contactor' => $value['contactor'] ?? '',
- 'position' => $value['position'] ?? '',
- 'wxaccount' => $value['wxaccount'] ?? '',
- 'qqaccount' => $value['qqaccount'] ?? '',
- 'email' => $value['email'] ?? '',
- 'comdepart' => $value['comdepart'] ?? '',
- 'status' => $value['status'] ?? '',
- 'createrid' => $createrid,
- 'creater' => $creater,
- 'companyNo' => $companyNo,
- 'is_del' => 0,
- 'addtime' => $date,
- 'updatetime' => $date,
- ];
- }
- $vp = Db::name('customer_member')->insertAll($var);
- if ($vp == "") throw new Exception('新建联系人失败');
- //汇总表
- Db::name('headquarters')
- ->insert([
- 'code' => $companyNo,
- 'name' => $param['companyName'],
- 'type' => 2,
- 'creater' => $param['uname'],
- 'createrid' => $param['uid'],
- "addtime" => $date,
- 'updater' => $param['uname'],
- 'updaterid' => $param['uid'],
- "updatetime" => $date,
- ]);
- Db::commit();
- $data['type']=2;
- Cache::store("redis")->handler()->lpush("companycopy",json_encode($data,JSON_UNESCAPED_UNICODE));
- return json_show(0, "新建成功");
- } else throw new Exception('新建失败');
- } catch (Exception $e) {
- Db::rollback();
- return json_show(1005, $e->getMessage());
- }
- }
- public function cList()
- {
- $param = $this->request->only(['page' => 1, 'size' => 10, 'companyName' => '', 'status' => '', 'creater' => '', 'start' => '', 'end' => ''], 'post', 'trim');
- $where = [["a.is_del", "=", 0]];
- if ($param['companyName'] != "") $where[] = ['b.companyName', "like", '%' . $param['companyName'] . '%'];
- if ($param['status'] !== "") $where[] = ['status', "=", $param['status']];
- if ($param['creater'] != "") $where[] = ['a.creater', "like", '%' . $param['creater'] . '%'];
- if ($param['start'] !== "") $where[] = ['a.addtime', ">=", date('Y-m-d H:i:s', strtotime($param['start']))];
- if ($param['end'] !== "") $where[] = ['a.addtime', "<", date('Y-m-d H:i:s', strtotime($param['end']) + 24 * 3600)];
- $count = Db::name('customer_member')
- ->alias('a')
- ->leftJoin('customer_info b', "b.companyNo=a.companyNo")
- ->where($where)
- ->count('a.id');
- $total = ceil($count / $param['size']);
- $page = $param['page'] >= $total ? $total : $param['page'];
- $list = Db::name('customer_member')
- ->alias('a')
- ->leftJoin('customer_info b', "b.companyNo=a.companyNo")
- ->where($where)
- ->page($page, $param['size'])
- ->order("addtime desc")
- ->field("a.*,b.companyName,b.parent,b.area")
- ->select()
- ->toArray();
- return json_show(0, "获取成功", ['list' => $list, 'count' => $count]);
- }
- public function cEdit()
- {
- $param = $this->request->only([
- 'id',
- 'companyName',
- 'parent',
- 'customer_member',
- 'uid',
- 'uname',
- 'branch',
- 'middle',
- 'area',
- ], 'post', 'trim');
- $val = Validate::rule([
- 'id' => 'require|number|gt:0',
- 'companyName|客户名称' => 'require',
- 'parent' => 'require',
- 'customer_member|联系方式' => 'require|array|max:100',
- 'uid' => 'require|number|gt:0',
- 'uname|修改人' => 'require',
- 'branch|省级'=> 'require',
- 'middle|市级'=> 'require',
- 'area|区域'=> 'require',
- ]);
- if ($val->check($param) == false) return json_show(1004, $val->getError());
- $idinfo = Db::name('customer_info')
- ->field('id,status,companyNo')
- ->where(['id' => $param['id'], 'is_del' => 0])
- ->findOrEmpty();
- if (empty($idinfo)) return json_show(1004, "未找到数据");
- if ($idinfo['status'] == 1) return json_show(1002, "状态是启用状态,无法编辑");
- $rename = Db::name('customer_org1')
- ->where(['is_del' => 0, 'id' => $param['parent']])
- ->field('id')
- ->findOrEmpty();
- $item = Db::name('customer_info')
- ->field('id')
- ->where(['itemid' => $rename['id'] ?? 0, 'companyName' => $param['companyName'], 'is_del' => 0])
- ->where('id', '<>', $param['id'])
- ->findOrEmpty();
- if (!empty($item)) return json_show(1002, "公司名称已存在");
- $createrid = $param['uid'];//isset($user["data"]['id']) ? $user["data"]['id'] : "";
- $creater = $param['uname'];//isset($user["data"]['nickname']) ? $user["data"]['nickname'] : "";
- Db::startTrans();
- try {
- $date = date('Y-m-d H:i:s');
- $data = [
- "id" => $param['id'],
- "companyName" => $param['companyName'],
- "area" =>$param['area'],
- "branch" =>$param['branch'],
- "middle" =>$param['middle'],
- "parent" => $param['parent'],
- "updatetime" => $date,
- ];
- $datainfo = Db::name('customer_info')->save($data);
- if ($datainfo) {
- foreach ($param['customer_member'] as $value) {
- $item = [];
- $item['commobile'] = isset($value['commobile']) ? $value['commobile'] : "";
- $item['comtel'] = "";
- isset($value['id']) && $value['id'] !== "" ? $item['id'] = $value['id'] : '';
- $item['contactor'] = isset($value['contactor']) ? $value['contactor'] : "";
- $item['position'] = isset($value['position']) ? $value['position'] : "";
- $item['wxaccount'] = isset($value['wxaccount']) ? $value['wxaccount'] : "";
- $item['qqaccount'] = isset($value['qqaccount']) ? $value['qqaccount'] : "";
- $item['email'] = isset($value['email']) ? $value['email'] : "";
- $item['comdepart'] = isset($value['comdepart']) ? $value['comdepart'] : "";
- $item['status'] = $value['status'];
- $item['createrid'] = $createrid;
- $item['creater'] = $creater;
- $item['companyNo'] = isset($idinfo['companyNo']) ? $idinfo['companyNo'] : "";
- $item['is_del'] = 0;
- $item['addtime'] = $date;
- $item['updatetime'] = $date;
- $vp = Db::name('customer_member')->save($item);
- if ($vp == false) throw new Exception('更新失败');
- }
- //更新汇总表
- Db::name('headquarters')
- ->where(['is_del' => 0, 'type' => 2, 'code' => $idinfo['companyNo']])
- ->update([
- 'name' => $param['companyName'],
- 'updater' => $param['uname'],
- 'updaterid' => $param['uid'],
- "updatetime" => $date,
- ]);
- Db::commit();
- return json_show(0, "更新成功");
- } else throw new Exception("更新失败");
- } catch (Exception $e) {
- Db::rollback();
- return json_show(1005, $e->getMessage());
- }
- }
- //供应商升级成业务公司
- public function supplerUpgrade()
- {
- $post = $this->request->only(['code', 'inv_bank', 'inv_bankNo', 'inv_addr', 'invoice_mobile', 'uid', 'uname', 'invoice_title'], 'post', 'trim');
- $val = Validate::rule([
- 'code|供应商编码' => 'require',
- 'inv_bank|银行名称' => 'require|max:255',
- 'inv_bankNo|银行卡号' => 'require|number',
- 'inv_addr|联系地址' => 'require|max:255',
- 'invoice_mobile' => 'require',
- 'invoice_title|公司抬头' => 'require',
- ]);
- if ($val->check($post) == false) return json_show(1004, $val->getError());
- $res = Db::name('supplier')
- ->alias('a')
- ->field('a.id,a.status,a.nature,b.contactor,b.mobile,a.name,a.code,a.registercode,a.legaler,a.registertime,a.addr,a.scope,a.license_img,a.is_upgrade')
- ->leftJoin('supplier_contact b', 'b.code=a.code')
- ->where(['a.code' => $post['code'], 'a.is_del' => 0])
- ->findOrEmpty();
- if (empty($res)) return json_show(1004, '该供应商不存在');
- if ($res['status'] != 1) return json_show(1004, '该供应商已禁用');
- if ($res['is_upgrade'] == 1) return json_show(1004, '不能重复升级');
- $temp = Db::name('company_type')
- ->field('id')
- ->where(['company_type' => $res['nature'], 'is_del' => 0])
- ->findOrEmpty();
- // if (empty($temp)) return json_show(1004, '对应公司类型不存在');
- Db::startTrans();
- try {
- $date = date('Y-m-d H:i:s');
- $business_code = makeNo('GS');
- //业务公司列表
- Db::name('business')
- ->insert([
- 'company' => $res['name'],
- 'companyNo' => $business_code,
- 'inv_code' => $res['registercode'],
- 'company_type' => $res['nature'],
- 'type' => $temp['id'] ?? 0,
- 'creater' => $post['uname'],
- 'createrid' => $post['uid'],
- 'inv_legaler' => $res['legaler'],
- 'inv_time' => $res['registertime'],
- 'inv_addr' => $post['inv_addr'],
- 'inv_bank' => $post['inv_bank'],
- 'inv_bankNo' => $post['inv_bankNo'],
- 'contactor' => $res['contactor'],
- 'mobile' => $res['mobile'],
- 'addr' => $res['addr'],
- 'inv_scope' => $res['scope'],
- 'license_img' => $res['license_img'],
- 'invoice_title' => $post['invoice_title'],
- 'invoice_mobile' => $post['invoice_mobile'],
- 'status' => 1,
- 'is_del' => 0,
- 'addtime' => $date,
- 'updatetime' => $date,
- ]);
- $temp_supplier = Db::name('headquarters')
- ->where(['code' => $post['code'], 'type' => 3])
- ->findOrEmpty();
- //汇总表
- Db::name('headquarters')
- ->where(['code' => $post['code'], 'type' => 3])
- ->update([
- 'relation_code' => $business_code,
- 'updater' => $post['uname'],
- 'updaterid' => $post['uid'],
- 'updatetime' => $date,
- ]);
- Db::name('headquarters')
- ->insert(array_merge($temp_supplier, [
- 'id' => null,
- 'code' => $business_code,
- 'type' => 1,//业务公司
- 'relation_code' => $post['code']
- ]));
- //供应商
- Db::name('supplier')
- ->where(['code' => $post['code'], 'is_upgrade' => 0, 'is_del' => 0])
- ->update(['is_upgrade' => 1, 'updatetime' => $date]);
- //账号与公司的关联关系
- Db::name('account_company')
- ->insert([
- 'account_id' => $post['uid'],
- 'companyCode' => $business_code,
- 'companyName' => $res['name'],
- 'company_type' => 2,
- 'status' => 1,
- 'is_del' => 0,
- 'addtime' => $date,
- 'updatetime' => $date,
- ]);
- Db::commit();
- return json_show(0, '升级成功');
- } catch (Exception $exception) {
- Db::rollback();
- return json_show(1004, '升级失败,' . $exception->getMessage());
- }
- }
- //获取业务公司列表(为了采销临时脚本同步数据到结算平台使用,正式上线要删除)
- public function getBusinessListTmp()
- {
- $where = $this->request->filter('trim')->post('where', []);
- $list = Db::name('business')
- ->alias('a')
- ->field('a.*,b.code as supplierNo')
- ->leftJoin('headquarters b', 'b.relation_code=a.companyNo')
- ->where($where)
- ->select()
- ->toArray();
- return json_show(0, '获取成功', $list);
- }
- private function checkSupplier($supplier,$canact){
- $hquest = Db::name("headquarters")->where(["code"=>$supplier["code"],"is_del"=>0])->findOrEmpty();
- if(empty($hquest)) throw new Exception("未找到供应商关联数据");
- if($hquest['relation_code']!='' && $hquest['type']==3){
- $comp = Db::name('headquarters')->where(['code'=>$hquest['relation_code'],'is_del'=>0])->findOrEmpty();
- if(empty($comp)) throw new Exception('未找到供应商关联业务公司数据');
- $business = Db::name("business")->where(['companyNo'=>$hquest['relation_code'],'is_del'=>0])->findOrEmpty();
- if(empty($business)) throw new Exception('未找到供应商关联业务公司数据');
- $comp['name'] = $supplier['name'];
- !isset($supplier['legaler'])?:$comp['invoice_people'] = $supplier['legaler'];
- !isset($supplier['registercode'])?:$comp['invoice_code'] = $supplier['registercode'];
- !isset($supplier['license_img'])?:$comp['invoice_img'] = $supplier['license_img'];
- $comp['updatetime'] = date("Y-m-d H:i:s");
- $ip = Db::name('headquarters')->save($comp);
- if($ip==false) throw new Exception('供应商关联业务公司数据更新失败');
- $business['company'] =$supplier['name'];
- !isset($supplier['registercode'])?:$business['inv_code'] =$supplier['registercode'];
- !isset($supplier['legaler'])?:$business['inv_legaler'] =$supplier['legaler'];
- !isset($supplier['registertime'])?:$business['inv_time'] =$supplier['registertime'];
- !isset($supplier['addr'])?:$business['addr'] =$supplier['addr'];
- !isset($canact['contactor'])?:$business['contactor'] =$canact['contactor'];
- !isset($canact['mobile'])?:$business['mobile'] =$canact['mobile'];
- !isset($supplier['scope'])?:$business['inv_scope'] =$supplier['scope'];
- $business['license_img'] =$supplier['license_img'];
- $business['creater'] =$supplier['updater'];
- $business['createrid'] =$supplier['updaterid'];
- $business['updatetime'] = date('Y-m-d H:i:s');
- $bp = Db::name('business')->save($business);
- if($bp==false) throw new Exception('业务公司数据更新失败');
- $business['type']=1;
- Cache::store('redis')->handler()->lpush('companycopy',json_encode($business,JSON_UNESCAPED_UNICODE));
- }
- }
- }
|