12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190 |
- <?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;
- //【公司汇总】
- 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('headquarters')
- ->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'],
- 'updater' => date('Y-m-d H:i:s'),
- ]);
- Db::commit();
- 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'],
- 'updater' => date('Y-m-d H:i:s'),
- ]);
- Db::commit();
- 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('headquarters')
- ->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'];
- // if ($param['company_name'] !== "") $where[] = ["a.createrid", 'in', $param['company_name']];
- $count = Db::name('business')
- ->alias('a')
- // ->leftJoin("depart_user u", "u.uid=a.createrid AND u.is_del=0")
- ->where($where)
- ->count('a.id');
- $list = Db::name('business')
- ->alias('a')
- ->field(true)
- // ->field('a.*,u.itemid')
- // ->leftJoin("depart_user u", "u.uid=a.createrid AND u.is_del=0")
- ->where($where)
- ->page($param['page'], $param['size'])
- ->order(['addtime' => 'desc', 'id' => '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'] = '';
- }
- 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();
- 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')
- ->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();
- 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'] = '';
- }
- 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();
- 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' => $param['data']['updatetime'],
- ]);
- Db::commit();
- 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,LENGTH(companyName) as weight")
- ->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'], 'post', 'trim');
- $val = Validate::rule([
- 'companyName' => 'require',
- 'customer_member' => 'require|array|max:100',
- 'uid' => 'require|number|gt:0',
- 'uname' => 'require',
- ]);
- if ($val->check($param) == false) return json_show(1004, $val->getError());
- // $token = isset($this->post['token']) && $this->post['token'] != '' ? trim($this->post['token']) : "";
- // if ($token == '') {
- // return json_show(1005, "参数token不能为空");
- // }
- $companyNo = makeNo("KH");
- // $companyName = isset($this->post['companyName']) && $this->post['companyName'] !== "" ? $this->post['companyName'] : "";
- // if ($companyName == "") {
- // return json_show(1002, "参数companyName不能为空");
- // }
- // $parent = isset($this->post['parent']) && $this->post['parent'] !== "" ? intval($this->post['parent'] ) : "";
- $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, "公司名称已存在");
- // if ($parent === "") {
- // return json_show(1002, "参数parent不能为空");
- // }
- //$itemid = isset($this->post['itemid']) && $this->post['itemid'] !== "" ? intval($this->post['itemid'] ) : "";
- // $customer_member = isset($this->post['customer_member']) && $this->post['customer_member'] !== "" ? $this->post['customer_member'] : "";
- // if ($customer_member == "") {
- // return json_show(1002, "参数customer_member不能为空");
- // }
- // $status = isset($this->post['status']) && $this->post['status'] !=="" ? intval($this->post['status']):"";
- // if($status==""){
- // return json_show(1002,"参数status不能为空");
- // }
- // $user = GetUserInfo($token);
- // if (empty($user) || $user['code'] != 0) {
- // return json_show(102, "创建人数据不存在");
- // }
- $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" => "",
- "comdepart" => "",
- "commobile" => "",
- "comtel" => "",
- "contactor" => "",
- "depart" => "",
- "mobile" => "",
- "invoice_title" => "",
- "invoice_people" => "",
- "invoice_addr" => "",
- "invoice_mobile" => "",
- "invoice_code" => "",
- "invoice_bank" => "",
- "invoice_bankNo" => "",
- "companyCode" => "",
- "status" => 0,
- "sales_name" => "",
- "sales_depart" => "",
- "branch" => "",
- "middle" => "",
- "country" => "",
- "is_del" => 0,
- "createrid" => $createrid,
- "creater" => $creater,
- "modifier" => "",
- "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();
- 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');
- // $page = isset($this->post['page']) && $this->post['page'] !== "" ? intval($this->post['page']) : "1";
- // $size = isset($this->post['size']) && $this->post['size'] !== "" ? intval($this->post['size']) : "10";
- $where = [["a.is_del", "=", 0]];
- // $companyName = isset($this->post['companyName']) && $this->post['companyName'] !== "" ? trim($this->post['companyName']) : "";
- if ($param['companyName'] != "") $where[] = ['b.companyName', "like", '%' . $param['companyName'] . '%'];
- // $status = isset($this->post['status']) && $this->post['status'] !=="" ? intval($this->post['status']) :"";
- if ($param['status'] !== "") $where[] = ['status', "=", $param['status']];
- // $creater = isset($this->post['creater']) && $this->post['creater'] !== "" ? intval($this->post['creater']) : "";
- if ($param['creater'] != "") $where[] = ['a.creater', "like", '%' . $param['creater'] . '%'];
- // $start = isset($this->post['start']) && $this->post['start'] !== "" ? $this->post['start'] : "";
- if ($param['start'] !== "") $where[] = ['a.addtime', ">=", date('Y-m-d H:i:s', strtotime($param['start']))];
- // $end = isset($this->post['end']) && $this->post['end'] !== "" ? $this->post['end'] : "";
- 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")
- ->select()
- ->toArray();
- return json_show(0, "获取成功", ['list' => $list, 'count' => $count]);
- }
- public function cEdit()
- {
- $param = $this->request->only(['id', 'companyName', 'parent', 'customer_member', 'uid', 'uname'], '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',]);
- 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'],
- //"companyNo" => $companyNo,
- "parent" => $param['parent'],
- "updatetime" => $date,
- ];
- $datainfo = Db::name('customer_info')->save($data);
- if ($datainfo) {
- foreach ($param['customer_member'] as $value) {
- // $to = GetInfoById($token, ['id' => $value['ownerid']]);
- //
- // if (empty($to) || $to['code'] != 0) {
- // return json_show(1002, "数据拥有者信息不存在");
- // }
- $item = [];
- // $info = $to['data'];
- // $item['owner'] = $info['nickname'];
- // $item['ownerid'] = $info['id'];
- $item['commobile'] = isset($value['commobile']) ? $value['commobile'] : "";
- $item['comtel'] = "";
- // $item['id'] =isset($value['id']) ? $value['id'] : "";
- 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['is_private'] = isset($value['is_private']) ? $value['is_private'] : "";
- $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;
- // $var[]=$item;
- $vp = Db::name('customer_member')->save($item);
- // var_dump(Db::name('customer_member')->getLastSql());
- // $var = array_diff($customer_member,$item);
- // $jsn = json_encode($var);
- 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);
- }
- }
|