Supplier.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. <?php
  2. namespace app\user\controller;
  3. use app\user\model\Headquarters;use app\user\model\SupplierContact;use think\App;use think\facade\Validate;use think\Response;use think\response\Json;
  4. class Supplier extends Base{
  5. public $noLogin=["*"];
  6. public function __construct(App $app) {
  7. parent::__construct($app);
  8. $this->model = new \app\user\model\Supplier();
  9. }
  10. /** * @param 参数列表如下:
  11. * - name: 供应商名称
  12. * - source: 来源
  13. * - coop_state: 合作状态
  14. * - category: 类别
  15. * - delivery_way: 配送方式
  16. * - level: 级别
  17. * - supplier_type: 供应商类型
  18. * - pay_type: 支付方式
  19. * - registercode: 注册码
  20. * - registertime: 注册时间
  21. * - scope: 业务范围
  22. * - type: 类型
  23. * - nature: 性质
  24. * - legaler: 法人
  25. * - addr: 地址
  26. * - prove_img: 证明图片
  27. * - supplier_img: 供应商图片
  28. * - contactor: 联系人
  29. * - mobile: 手机号
  30. * - telephone: 电话
  31. * - position: 职位
  32. * - email: 电子邮件
  33. * - personid: 负责人id
  34. */
  35. public function create(){
  36. $param = $this->request->param(["name"=>"","source"=>"","coop_state"=>"","category"=>"","delivery_way"=>"",
  37. "level"=>"","supplier_type"=>"","pay_type"=>"","registercode"=>"","registertime"=>"","scope"=>"","type"=>"",
  38. "nature"=>"","legaler"=>"","addr"=>"","prove_img"=>"","supplier_img"=>"","contactor"=>"","mobile"=>"",
  39. "telephone"=>"","position"=>"","email"=>"",'personid'=>''],"post","trim");
  40. $valid=Validate::rule([
  41. 'name|供应商名称' => 'requireIf:supplier_img,""|max:255',
  42. 'source|供应商来源' => 'require|number|in:0,1,2',
  43. 'coop_state|合作状态' => 'require|number|in:0,1',
  44. 'category|所属类别' => 'require|number|in:0,1',
  45. 'delivery_way|提供物流方式' => 'require|number|in:0,1',
  46. 'level|供应商级别' => 'require|number|in:0,1,2',
  47. 'supplier_type|供应商性质' => 'require|number|in:0,1,2,3,4',
  48. 'pay_type|付款方式' => 'require|number|in:0,1,2',
  49. 'registercode|纳税识别编号' => 'requireIf:supplier_img,""|max:255',
  50. 'registertime|成立日期' => 'requireIf:supplier_img,""|max:255',
  51. 'scope|经营范围' => 'requireIf:supplier_img,""|max:3000',
  52. 'type|申请类型' => 'require|number|in:0,1',
  53. 'nature|公司类型' => 'require|number|in:1,2,3,4,5,6,7,8,9',
  54. 'legaler|法人' => 'requireIf:supplier_img,""|max:255',
  55. 'addr|公司详细地址' => 'requireIf:supplier_img,""|max:255',
  56. 'prove_img|资质证明' => 'url',
  57. 'supplier_img|营业执照' => 'requireIf:name,""|url',
  58. 'contactor|联系人' => 'require|max:255',
  59. 'mobile|手机号' => 'require|mobile',
  60. 'telephone|座机号' => 'max:255',
  61. 'position|职位' => 'require',
  62. 'email|邮箱' => 'require|email',
  63. 'personid|负责人' => 'require|number|gt:0',
  64. ]);
  65. if(!$valid->check($param))return error($valid->getError());
  66. if ($param['supplier_img']!=''){
  67. $param['license_img'] = $param['supplier_img'];
  68. $result = $this->GetBusinessInfoByParam($param['supplier_img']);
  69. /**
  70. * Array of attributes to getter functions (for serialization of requests)
  71. * registrationNumber - 老版本营业执照对应注册号。 - 新三证合一版本营业执照对应社会保障号。
  72. * name 企业名称。
  73. * type 公司/企业类型/主体类型/类型。
  74. * address 住所/营业场所/企业住所/主要经营场所/经营场所。
  75. * legalRepresentative 法定代表人/负责人/执行事务合伙人/投资人/经营者。
  76. * registeredCapital 注册资本/出资额。
  77. * organizationForm 组成形式。
  78. * foundDate 成立日期/注册日期。
  79. * businessTerm 营业期限。
  80. * businessScope 经营范围。
  81. * issueDate 发照日期。
  82. * confidence 相关字段的置信度信息,置信度越大,表示本次识别的对应字段的可靠性越高,在统计意义上,置信度越大,准确率越高。 置信度由算法给出,不直接等价于对应字段的准确率。
  83. * @var string[]
  84. */
  85. if($result['code']==200){
  86. $param['ocr_status'] = 1;
  87. if ($param['name'] != $result['name']) {
  88. $param['remark'] = '企业名称与营业执照不匹配';
  89. }
  90. if ($param['registercode'] !=$result['registrationNumber']&& $param['registercode']!= '') {
  91. $param['remark'] .= '企业纳税识别号与营业执照不匹配';
  92. }
  93. $param['addr']=$result['address']??"";
  94. $param['legaler']=$result['legalRepresentative']??"";
  95. $param['registercode']=$result['registrationNumber']??"";
  96. $param['registertime']=$result['foundDate']??"";
  97. $param['scope']=$result['businessScope']??"";
  98. $param['nature']=$result['type']??"";
  99. $param['capital'] = $result['registeredCapital']??"";
  100. }else{
  101. $param['remark'] = '营业执照识别失败';
  102. $param['ocr_status'] = 2;
  103. }
  104. }
  105. $this->model->startTrans();
  106. try{
  107. $data=[
  108. 'code'=>makeNo('QS'),
  109. 'person'=>\app\user\model\User::where('account_id','=',$param['personid'])->value('nickname',''),
  110. 'creater'=>$this->uname,
  111. 'createrid'=>$this->uid
  112. ];
  113. $create = \app\user\model\Supplier::create(array_merge($data,$param));
  114. if(!$create->isEmpty())throw new Exception('供应商创建失败');
  115. $contact=[
  116. "code"=>$data['code'],
  117. "contactor"=>$param['contactor'],
  118. "mobile"=>$param['mobile'],
  119. "position"=>$param['position'],
  120. "telephone"=>$param["telephone"],
  121. "email"=>$param['email'],
  122. ];
  123. $contactC= SupplierContact::create($contact);
  124. if (!$contactC->isEmpty())throw new Exception("供应商联系人信息创建失败");
  125. $create->updater=$this->uname;
  126. $create->updaterid=$this->uid;
  127. $hqcreate= Headquarters::createInfo($create,3);
  128. if (!$hqcreate)throw new Exception("供应商信息创建失败");
  129. }catch (Exception $exception){
  130. $this->model->rollback();
  131. return error($exception->getMessage());
  132. }
  133. $this->model->commit();
  134. return success("创建成功");
  135. }
  136. /**
  137. * 编辑供应商
  138. * 参数:
  139. * id 供应商id
  140. * - name: 供应商名称
  141. * - source: 来源
  142. * - coop_state: 合作状态
  143. * - category: 类别
  144. * - delivery_way: 配送方式
  145. * - level: 级别
  146. * - supplier_type: 供应商类型
  147. * - pay_type: 支付方式
  148. * - registercode: 注册码
  149. * - registertime: 注册时间
  150. * - scope: 业务范围
  151. * - type: 类型
  152. * - nature: 性质
  153. * - legaler: 法人
  154. * - addr: 地址
  155. * - prove_img: 证明图片
  156. * - supplier_img: 供应商图片
  157. * - contactor: 联系人
  158. * - mobile: 手机号
  159. * - telephone: 电话
  160. * - position: 职位
  161. * - email: 电子邮件
  162. * - personid: 负责人id
  163. * @return Response|Json|void
  164. */
  165. public function save(){
  166. $param = $this->request->param([
  167. 'id' => '',
  168. 'coop_state' => '0',
  169. 'source' => '0',
  170. 'type' => '0',
  171. 'category' => '1',
  172. 'delivery_way' => '0',
  173. 'supplier_type' => '0',
  174. 'level' => '0',
  175. 'pay_type' => '0',
  176. 'supplier_img' => '',
  177. 'prove_img' => '',
  178. 'contactor' => '',
  179. 'mobile' => '', // 确保这是经过验证的格式
  180. 'telephone' => '',
  181. 'position' => '',
  182. 'registercode' => '',
  183. 'name' => '',
  184. 'email' => '', // 更正了邮箱格式
  185. 'nature' => '',
  186. 'addr' => '',
  187. 'legaler' => '',
  188. 'registertime' => '',
  189. 'scope' => '',
  190. 'personid' => ''],"post",'json');
  191. $valid=Validate::rule([
  192. "id|主键ID"=>"require|number|gt:0",
  193. 'name|供应商名称' => 'requireIf:supplier_img,""|max:255',
  194. 'source|供应商来源' => 'require|number|in:0,1,2',
  195. 'coop_state|合作状态' => 'require|number|in:0,1',
  196. 'category|所属类别' => 'require|number|in:0,1',
  197. 'delivery_way|提供物流方式' => 'require|number|in:0,1',
  198. 'level|供应商级别' => 'require|number|in:0,1,2',
  199. 'supplier_type|供应商性质' => 'require|number|in:0,1,2,3,4',
  200. 'pay_type|付款方式' => 'require|number|in:0,1,2',
  201. 'registercode|纳税识别编号' => 'requireIf:supplier_img,""|max:255',
  202. 'registertime|成立日期' => 'requireIf:supplier_img,""|max:255',
  203. 'scope|经营范围' => 'requireIf:supplier_img,""|max:3000',
  204. 'type|申请类型' => 'require|number|in:0,1',
  205. 'nature|公司类型' => 'require|number|in:1,2,3,4,5,6,7,8,9',
  206. 'legaler|法人' => 'requireIf:supplier_img,""|max:255',
  207. 'addr|公司详细地址' => 'requireIf:supplier_img,""|max:255',
  208. 'prove_img|资质证明' => 'url',
  209. 'supplier_img|营业执照' => 'requireIf:name,""|url',
  210. 'contactor|联系人' => 'require|max:255',
  211. 'mobile|手机号' => 'require|mobile',
  212. 'telephone|座机号' => 'max:255',
  213. 'position|职位' => 'require',
  214. 'email|邮箱' => 'require|email',
  215. 'personid|负责人' => 'require|number|gt:0',
  216. ]);
  217. if(!$valid->check($param))return error($valid->getError());
  218. $info = \app\user\model\Supplier::with(["contactInfo"])->where('id','=',$param['id'])->find();
  219. if(!$info)return error('供应商不存在');
  220. $goodUpdateBool=false;
  221. if($info->personid!=$param['personid'])$goodUpdateBool=true;
  222. $info->name=$param['name'];
  223. $info->personid=$param['personid'];
  224. $info->person=\app\user\model\User::where('account_id','=',$param['personid'])->value('nickname','');
  225. $info->source=$param['source'];
  226. $info->coop_state=$param['coop_state'];
  227. $info->category=$param['category'];
  228. $info->delivery_way=$param['delivery_way'];
  229. $info->level=$param['level'];
  230. $info->supplier_type=$param['supplier_type'];
  231. $info->pay_type=$param['pay_type'];
  232. $info->type=$param['type'];
  233. $info->prove_img=$param['prove_img'];
  234. $info->updater=$this->uname;
  235. $info->updaterid=$this->uid;
  236. if($param['supplier_img']!='' && $info->license_img!=$param['supplier_img']){
  237. $info->license_img=$param['supplier_img'];
  238. $result = $this->GetBusinessInfoByParam($param['supplier_img']);
  239. if($result['code']=='200'){
  240. $info->ocr_status = 1;
  241. if ($param['name'] != $result['name']) {
  242. $info->remark = "企业名称与营业执照不匹配";
  243. }
  244. if ($param['registercode'] !=$result['registrationNumber']&& $param['registercode']!= '') {
  245. $info->remark .= '企业纳税识别号与营业执照不匹配';
  246. }
  247. $param['addr']=$result['address']??'';
  248. $param['legaler']=$result['legalRepresentative']??'';
  249. $param['registercode']=$result['registrationNumber']??'';
  250. $param['registertime']=$result['foundDate']??'';
  251. $param['scope']=$result['businessScope']??'';
  252. $param['nature']=$result['type']??'';
  253. $param['capital'] = $result['registeredCapital']??'';
  254. }else{
  255. $info->remark = '营业执照识别失败';
  256. $info->ocr_status = 2;
  257. }
  258. }
  259. $info->registercode=$param['registercode'];
  260. $info->nature=$param['nature'];
  261. $info->legaler=$param['legaler'];
  262. $info->registertime=$param['registertime'];
  263. $info->capital=$param['capital'];
  264. $info->addr=$param['addr'];
  265. $info->scope=$param['scope'];
  266. $this->model->startTrans();
  267. try{
  268. if($info->save()){
  269. $contact=[
  270. 'contactor'=>$param['contactor'],
  271. 'mobile'=>$param['mobile'],
  272. 'position'=>$param['position'],
  273. 'telephone'=>$param['telephone'],
  274. 'email'=>$param['email'],
  275. ];
  276. $contactC=$info->contactInfo->save($contact);
  277. if (!$contactC)throw new Exception('供应商联系人信息保存失败');
  278. $hqcreate= Headquarters::createInfo($info,3);
  279. if(!$hqcreate)throw new Exception('供应商信息保存失败');
  280. }else throw new Exception('供应商信息保存失败');
  281. $this->model->commit();
  282. }catch (Exception $e){
  283. $this->model->rollback();
  284. return error($e->getMessage());
  285. }
  286. if($goodUpdateBool)event("updateSupplierGood",["code"=>$info->code,"personid"=>$info->personid,"person"=>$info->person]);
  287. return success("保存成功!");
  288. }
  289. /**
  290. * 获取供应商列表
  291. */
  292. public function list()
  293. {
  294. $param=$this->request->param(["page"=>1,"size"=>15,"createrid"=>"","name"=>"","start"=>"","end"=>"",'status'=>'','ocr_status'=>''],"post","trim");
  295. $where=[["is_del","=",0]];
  296. if($param['createrid']!='') $where[]=['createrid','=',$param['createrid']];
  297. if($param['name']!='') $where[]=['name','like',"%".$param['name']."%"];
  298. if($param['status']!='') $where[]=['status','=',$param['status']];
  299. if($param['ocr_status']!='') $where[]=['ocr_status','=',$param['ocr_status']];
  300. if($param['start']!='') $where[]=['addtime','>=',$param['start']];
  301. if($param['end']!='') $where[]=['addtime','<=',$param['end']];
  302. $list = $this->model->with(["contactInfo"])->where($where)->order('id desc')
  303. ->paginate(['page'=>$param['page'],'list_rows'=>$param['size']]);
  304. return success("获取成功",["list"=>$list->items(),"count"=>$list->total()]);
  305. }
  306. /**
  307. * 获取供应商详情
  308. */
  309. public function info()
  310. {
  311. $code=$this->request->param("code");
  312. if(!$code=="")return error("参数错误");
  313. $info = $this->model->with(["contactInfo"])->where('code','=',$code)->findOrEmpty();
  314. if($info->isEmpty())return error("供应商不存在");
  315. return success("获取成功",$info);
  316. }
  317. /**删除供应商
  318. * @return Response|Json|void
  319. */
  320. public function delete()
  321. {
  322. $param=$this->request->param(["id"=>""],"post","trim");
  323. if($param['id']=="")return error("参数错误");
  324. $info = $this->model->where('id','=',$param['id'])->findOrEmpty();
  325. if($info->isEmpty())return error("供应商不存在");
  326. $info->is_del=1;
  327. if($info->save()){
  328. $headquarters = new Headquarters();
  329. $headquarters->update([
  330. 'is_del'=>1,
  331. 'updater'=>$this->uname,
  332. 'updaterid'=>$this->uid,
  333. ],['code'=>$info->code]);
  334. }
  335. return success("删除成功");
  336. }
  337. /**
  338. * @return Json|Response|Response|Json|void
  339. */
  340. public function status()
  341. {
  342. $param=$this->request->param(["id"=>"","status"=>""],"post","trim");
  343. $valid=Validate::check($param,[
  344. 'id|供应商id'=>'require',
  345. 'status|状态'=>'require|number|in:0,1',
  346. ]);
  347. if($valid!==true)return error(Validate::getError());
  348. $info = $this->model->where('id','=',$param['id'])->findOrEmpty();
  349. if($info->isEmpty())return error("供应商不存在");
  350. $info->status=$param['status'];
  351. if($info->save()){
  352. $headquarters = new Headquarters();
  353. $headquarters->update([
  354. 'status'=>$param['status'],
  355. 'updater'=>$this->uname,
  356. 'updaterid'=>$this->uid,
  357. ],['code'=>$info->code]);
  358. }else return error("修改失败");
  359. return success("修改成功");
  360. }
  361. /**
  362. * @param $post
  363. * @return Response|Json|void
  364. * @throws PDOException
  365. */
  366. public function upgrade()
  367. {
  368. $post = $this->request->only(['code', 'inv_bank', 'inv_bankNo', 'inv_addr', 'invoice_mobile', 'uid', 'uname', 'invoice_title'], 'post', 'trim');
  369. $val = Validate::rule([
  370. 'code|供应商编码' => 'require',
  371. 'inv_bank|银行名称' => 'require|max:255',
  372. 'inv_bankNo|银行卡号' => 'require|number',
  373. 'inv_addr|联系地址' => 'require|max:255',
  374. 'invoice_mobile' => 'require',
  375. 'invoice_title|公司抬头' => 'require',
  376. ]);
  377. if ($val->check($post) == false) return error($val->getError());
  378. $supplier = $this->model->with(["contactInfo"])->where([['code',"=", $post['code']],["is_del","=",0]])->findOrEmpty();
  379. if ($supplier->isEmpty()) return error("供应商不存在");
  380. if ($supplier->status == 0) return error("供应商未启用");
  381. if ($supplier->is_upgrade == 1) return error("供应商已升级为业务公司");
  382. $this->model->startTrans();
  383. try {
  384. $post['uid']=$this->uid;
  385. $post['uname']=$this->uname;
  386. $up= $this->model->upgrade($post);
  387. if(!$up)throw new Exception("升级失败");
  388. $this->model->commit();
  389. }catch (Exception $exception){
  390. $this->model->rollback();
  391. return error($exception->getMessage());
  392. }
  393. return success("升级成功");
  394. }
  395. }