Supplier.php 19 KB

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