123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403 |
- <?php
- namespace app\user\controller;
- use app\user\model\Headquarters;use app\user\model\SupplierContact;use think\App;use think\facade\Validate;use think\Response;use think\response\Json;
- class Supplier extends Base{
- public $noLogin=["*"];
- public function __construct(App $app) {
- parent::__construct($app);
- $this->model = new \app\user\model\Supplier();
- }
- /** * @param 参数列表如下:
- * - name: 供应商名称
- * - source: 来源
- * - coop_state: 合作状态
- * - category: 类别
- * - delivery_way: 配送方式
- * - level: 级别
- * - supplier_type: 供应商类型
- * - pay_type: 支付方式
- * - registercode: 注册码
- * - registertime: 注册时间
- * - scope: 业务范围
- * - type: 类型
- * - nature: 性质
- * - legaler: 法人
- * - addr: 地址
- * - prove_img: 证明图片
- * - supplier_img: 供应商图片
- * - contactor: 联系人
- * - mobile: 手机号
- * - telephone: 电话
- * - position: 职位
- * - email: 电子邮件
- * - personid: 负责人id
- */
- public function create(){
- $param = $this->request->param(["name"=>"","source"=>"","coop_state"=>"","category"=>"","delivery_way"=>"",
- "level"=>"","supplier_type"=>"","pay_type"=>"","registercode"=>"","registertime"=>"","scope"=>"","type"=>"",
- "nature"=>"","legaler"=>"","addr"=>"","prove_img"=>"","supplier_img"=>"","contactor"=>"","mobile"=>"",
- "telephone"=>"","position"=>"","email"=>"",'personid'=>''],"post","trim");
- $valid=Validate::rule([
- 'name|供应商名称' => 'requireIf:supplier_img,""|max:255',
- 'source|供应商来源' => 'require|number|in:0,1,2',
- 'coop_state|合作状态' => 'require|number|in:0,1',
- 'category|所属类别' => 'require|number|in:0,1',
- 'delivery_way|提供物流方式' => 'require|number|in:0,1',
- 'level|供应商级别' => 'require|number|in:0,1,2',
- 'supplier_type|供应商性质' => 'require|number|in:0,1,2,3,4',
- 'pay_type|付款方式' => 'require|number|in:0,1,2',
- 'registercode|纳税识别编号' => 'requireIf:supplier_img,""|max:255',
- 'registertime|成立日期' => 'requireIf:supplier_img,""|max:255',
- 'scope|经营范围' => 'requireIf:supplier_img,""|max:3000',
- 'type|申请类型' => 'require|number|in:0,1',
- 'nature|公司类型' => 'require|number|in:1,2,3,4,5,6,7,8,9',
- 'legaler|法人' => 'requireIf:supplier_img,""|max:255',
- 'addr|公司详细地址' => 'requireIf:supplier_img,""|max:255',
- 'prove_img|资质证明' => 'url',
- 'supplier_img|营业执照' => 'requireIf:name,""|url',
- 'contactor|联系人' => 'require|max:255',
- 'mobile|手机号' => 'require|mobile',
- 'telephone|座机号' => 'max:255',
- 'position|职位' => 'require',
- 'email|邮箱' => 'require|email',
- 'personid|负责人' => 'require|number|gt:0',
- ]);
- if(!$valid->check($param))return error($valid->getError());
- if ($param['supplier_img']!=''){
- $param['license_img'] = $param['supplier_img'];
- $result = $this->GetBusinessInfoByParam($param['supplier_img']);
- /**
- * Array of attributes to getter functions (for serialization of requests)
- * registrationNumber - 老版本营业执照对应注册号。 - 新三证合一版本营业执照对应社会保障号。
- * name 企业名称。
- * type 公司/企业类型/主体类型/类型。
- * address 住所/营业场所/企业住所/主要经营场所/经营场所。
- * legalRepresentative 法定代表人/负责人/执行事务合伙人/投资人/经营者。
- * registeredCapital 注册资本/出资额。
- * organizationForm 组成形式。
- * foundDate 成立日期/注册日期。
- * businessTerm 营业期限。
- * businessScope 经营范围。
- * issueDate 发照日期。
- * confidence 相关字段的置信度信息,置信度越大,表示本次识别的对应字段的可靠性越高,在统计意义上,置信度越大,准确率越高。 置信度由算法给出,不直接等价于对应字段的准确率。
- * @var string[]
- */
- if($result['code']==200){
- $param['ocr_status'] = 1;
- if ($param['name'] != $result['name']) {
- $param['remark'] = '企业名称与营业执照不匹配';
- }
- if ($param['registercode'] !=$result['registrationNumber']&& $param['registercode']!= '') {
- $param['remark'] .= '企业纳税识别号与营业执照不匹配';
- }
- $param['addr']=$result['address']??"";
- $param['legaler']=$result['legalRepresentative']??"";
- $param['registercode']=$result['registrationNumber']??"";
- $param['registertime']=$result['foundDate']??"";
- $param['scope']=$result['businessScope']??"";
- $param['nature']=$result['type']??"";
- $param['capital'] = $result['registeredCapital']??"";
- }else{
- $param['remark'] = '营业执照识别失败';
- $param['ocr_status'] = 2;
- }
- }
- $this->model->startTrans();
- try{
- $data=[
- 'code'=>makeNo('QS'),
- 'person'=>\app\user\model\User::where('account_id','=',$param['personid'])->value('nickname',''),
- 'creater'=>$this->uname,
- 'createrid'=>$this->uid
- ];
- $create = \app\user\model\Supplier::create(array_merge($data,$param));
- if(!$create->isEmpty())throw new Exception('供应商创建失败');
- $contact=[
- "code"=>$data['code'],
- "contactor"=>$param['contactor'],
- "mobile"=>$param['mobile'],
- "position"=>$param['position'],
- "telephone"=>$param["telephone"],
- "email"=>$param['email'],
- ];
- $contactC= SupplierContact::create($contact);
- if (!$contactC->isEmpty())throw new Exception("供应商联系人信息创建失败");
- $create->updater=$this->uname;
- $create->updaterid=$this->uid;
- $hqcreate= Headquarters::createInfo($create,3);
- if (!$hqcreate)throw new Exception("供应商信息创建失败");
- }catch (Exception $exception){
- $this->model->rollback();
- return error($exception->getMessage());
- }
- $this->model->commit();
- return success("创建成功");
- }
- /**
- * 编辑供应商
- * 参数:
- * id 供应商id
- * - name: 供应商名称
- * - source: 来源
- * - coop_state: 合作状态
- * - category: 类别
- * - delivery_way: 配送方式
- * - level: 级别
- * - supplier_type: 供应商类型
- * - pay_type: 支付方式
- * - registercode: 注册码
- * - registertime: 注册时间
- * - scope: 业务范围
- * - type: 类型
- * - nature: 性质
- * - legaler: 法人
- * - addr: 地址
- * - prove_img: 证明图片
- * - supplier_img: 供应商图片
- * - contactor: 联系人
- * - mobile: 手机号
- * - telephone: 电话
- * - position: 职位
- * - email: 电子邮件
- * - personid: 负责人id
- * @return Response|Json|void
- */
- public function save(){
- $param = $this->request->param([
- 'id' => '',
- 'coop_state' => '0',
- 'source' => '0',
- 'type' => '0',
- 'category' => '1',
- 'delivery_way' => '0',
- 'supplier_type' => '0',
- 'level' => '0',
- 'pay_type' => '0',
- 'supplier_img' => '',
- 'prove_img' => '',
- 'contactor' => '',
- 'mobile' => '', // 确保这是经过验证的格式
- 'telephone' => '',
- 'position' => '',
- 'registercode' => '',
- 'name' => '',
- 'email' => '', // 更正了邮箱格式
- 'nature' => '',
- 'addr' => '',
- 'legaler' => '',
- 'registertime' => '',
- 'scope' => '',
- 'personid' => ''],"post",'json');
- $valid=Validate::rule([
- "id|主键ID"=>"require|number|gt:0",
- 'name|供应商名称' => 'requireIf:supplier_img,""|max:255',
- 'source|供应商来源' => 'require|number|in:0,1,2',
- 'coop_state|合作状态' => 'require|number|in:0,1',
- 'category|所属类别' => 'require|number|in:0,1',
- 'delivery_way|提供物流方式' => 'require|number|in:0,1',
- 'level|供应商级别' => 'require|number|in:0,1,2',
- 'supplier_type|供应商性质' => 'require|number|in:0,1,2,3,4',
- 'pay_type|付款方式' => 'require|number|in:0,1,2',
- 'registercode|纳税识别编号' => 'requireIf:supplier_img,""|max:255',
- 'registertime|成立日期' => 'requireIf:supplier_img,""|max:255',
- 'scope|经营范围' => 'requireIf:supplier_img,""|max:3000',
- 'type|申请类型' => 'require|number|in:0,1',
- 'nature|公司类型' => 'require|number|in:1,2,3,4,5,6,7,8,9',
- 'legaler|法人' => 'requireIf:supplier_img,""|max:255',
- 'addr|公司详细地址' => 'requireIf:supplier_img,""|max:255',
- 'prove_img|资质证明' => 'url',
- 'supplier_img|营业执照' => 'requireIf:name,""|url',
- 'contactor|联系人' => 'require|max:255',
- 'mobile|手机号' => 'require|mobile',
- 'telephone|座机号' => 'max:255',
- 'position|职位' => 'require',
- 'email|邮箱' => 'require|email',
- 'personid|负责人' => 'require|number|gt:0',
- ]);
- if(!$valid->check($param))return error($valid->getError());
- $info = \app\user\model\Supplier::with(["contactInfo"])->where('id','=',$param['id'])->find();
- if(!$info)return error('供应商不存在');
- $goodUpdateBool=false;
- if($info->personid!=$param['personid'])$goodUpdateBool=true;
- $info->name=$param['name'];
- $info->personid=$param['personid'];
- $info->person=\app\user\model\User::where('account_id','=',$param['personid'])->value('nickname','');
- $info->source=$param['source'];
- $info->coop_state=$param['coop_state'];
- $info->category=$param['category'];
- $info->delivery_way=$param['delivery_way'];
- $info->level=$param['level'];
- $info->supplier_type=$param['supplier_type'];
- $info->pay_type=$param['pay_type'];
- $info->type=$param['type'];
- $info->prove_img=$param['prove_img'];
- $info->updater=$this->uname;
- $info->updaterid=$this->uid;
- if($param['supplier_img']!='' && $info->license_img!=$param['supplier_img']){
- $info->license_img=$param['supplier_img'];
- $result = $this->GetBusinessInfoByParam($param['supplier_img']);
- if($result['code']=='200'){
- $info->ocr_status = 1;
- if ($param['name'] != $result['name']) {
- $info->remark = "企业名称与营业执照不匹配";
- }
- if ($param['registercode'] !=$result['registrationNumber']&& $param['registercode']!= '') {
- $info->remark .= '企业纳税识别号与营业执照不匹配';
- }
- $param['addr']=$result['address']??'';
- $param['legaler']=$result['legalRepresentative']??'';
- $param['registercode']=$result['registrationNumber']??'';
- $param['registertime']=$result['foundDate']??'';
- $param['scope']=$result['businessScope']??'';
- $param['nature']=$result['type']??'';
- $param['capital'] = $result['registeredCapital']??'';
- }else{
- $info->remark = '营业执照识别失败';
- $info->ocr_status = 2;
- }
- }
- $info->registercode=$param['registercode'];
- $info->nature=$param['nature'];
- $info->legaler=$param['legaler'];
- $info->registertime=$param['registertime'];
- $info->capital=$param['capital'];
- $info->addr=$param['addr'];
- $info->scope=$param['scope'];
- $this->model->startTrans();
- try{
- if($info->save()){
- $contact=[
- 'contactor'=>$param['contactor'],
- 'mobile'=>$param['mobile'],
- 'position'=>$param['position'],
- 'telephone'=>$param['telephone'],
- 'email'=>$param['email'],
- ];
- $contactC=$info->contactInfo->save($contact);
- if (!$contactC)throw new Exception('供应商联系人信息保存失败');
- $hqcreate= Headquarters::createInfo($info,3);
- if(!$hqcreate)throw new Exception('供应商信息保存失败');
- }else throw new Exception('供应商信息保存失败');
- $this->model->commit();
- }catch (Exception $e){
- $this->model->rollback();
- return error($e->getMessage());
- }
- if($goodUpdateBool)event("updateSupplierGood",["code"=>$info->code,"personid"=>$info->personid,"person"=>$info->person]);
- return success("保存成功!");
- }
- /**
- * 获取供应商列表
- */
- public function list()
- {
- $param=$this->request->param(["page"=>1,"size"=>15,"createrid"=>"","name"=>"","start"=>"","end"=>"",'status'=>'','ocr_status'=>''],"post","trim");
- $where=[["is_del","=",0]];
- if($param['createrid']!='') $where[]=['createrid','=',$param['createrid']];
- if($param['name']!='') $where[]=['name','like',"%".$param['name']."%"];
- if($param['status']!='') $where[]=['status','=',$param['status']];
- if($param['ocr_status']!='') $where[]=['ocr_status','=',$param['ocr_status']];
- if($param['start']!='') $where[]=['addtime','>=',$param['start']];
- if($param['end']!='') $where[]=['addtime','<=',$param['end']];
- $list = $this->model->with(["contactInfo"])->where($where)->order('id desc')
- ->paginate(['page'=>$param['page'],'list_rows'=>$param['size']]);
- return success("获取成功",["list"=>$list->items(),"count"=>$list->total()]);
- }
- /**
- * 获取供应商详情
- */
- public function info()
- {
- $code=$this->request->param("code");
- if(!$code=="")return error("参数错误");
- $info = $this->model->with(["contactInfo"])->where('code','=',$code)->findOrEmpty();
- if($info->isEmpty())return error("供应商不存在");
- return success("获取成功",$info);
- }
- /**删除供应商
- * @return Response|Json|void
- */
- public function delete()
- {
- $param=$this->request->param(["id"=>""],"post","trim");
- if($param['id']=="")return error("参数错误");
- $info = $this->model->where('id','=',$param['id'])->findOrEmpty();
- if($info->isEmpty())return error("供应商不存在");
- $info->is_del=1;
- if($info->save()){
- $headquarters = new Headquarters();
- $headquarters->update([
- 'is_del'=>1,
- 'updater'=>$this->uname,
- 'updaterid'=>$this->uid,
- ],['code'=>$info->code]);
- }
- return success("删除成功");
- }
- /**
- * @return Json|Response|Response|Json|void
- */
- public function status()
- {
- $param=$this->request->param(["id"=>"","status"=>""],"post","trim");
- $valid=Validate::check($param,[
- 'id|供应商id'=>'require',
- 'status|状态'=>'require|number|in:0,1',
- ]);
- if($valid!==true)return error(Validate::getError());
- $info = $this->model->where('id','=',$param['id'])->findOrEmpty();
- if($info->isEmpty())return error("供应商不存在");
- $info->status=$param['status'];
- if($info->save()){
- $headquarters = new Headquarters();
- $headquarters->update([
- 'status'=>$param['status'],
- 'updater'=>$this->uname,
- 'updaterid'=>$this->uid,
- ],['code'=>$info->code]);
- }else return error("修改失败");
- return success("修改成功");
- }
- /**
- * @param $post
- * @return Response|Json|void
- * @throws PDOException
- */
- public function upgrade()
- {
- $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 error($val->getError());
- $supplier = $this->model->with(["contactInfo"])->where([['code',"=", $post['code']],["is_del","=",0]])->findOrEmpty();
- if ($supplier->isEmpty()) return error("供应商不存在");
- if ($supplier->status == 0) return error("供应商未启用");
- if ($supplier->is_upgrade == 1) return error("供应商已升级为业务公司");
- $this->model->startTrans();
- try {
- $post['uid']=$this->uid;
- $post['uname']=$this->uname;
- $up= $this->model->upgrade($post);
- if(!$up)throw new Exception("升级失败");
- $this->model->commit();
- }catch (Exception $exception){
- $this->model->rollback();
- return error($exception->getMessage());
- }
- return success("升级成功");
- }
- }
|