Supplier.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  1. <?php
  2. namespace app\user\controller;
  3. use app\user\model\CompanyType;use app\user\model\Headquarters;
  4. use app\user\model\SupplierBank;use app\user\model\SupplierContact;
  5. use think\App;
  6. use think\facade\Validate;
  7. class Supplier extends Base{
  8. public function __construct(App $app) {
  9. parent::__construct($app);
  10. $this->model = new \app\user\model\Supplier();
  11. }
  12. /** * @param 参数列表如下:
  13. * - name: 供应商名称
  14. * - source: 来源
  15. * - coop_state: 合作状态
  16. * - category: 类别
  17. * - delivery_way: 配送方式
  18. * - level: 级别
  19. * - supplier_type: 供应商类型
  20. * - pay_type: 支付方式
  21. * - registercode: 注册码
  22. * - registertime: 注册时间
  23. * - scope: 业务范围
  24. * - type: 类型
  25. * - nature: 性质
  26. * - legaler: 法人
  27. * - addr: 地址
  28. * - prove_img: 证明图片
  29. * - license_img: 供应商图片
  30. * - contactor: 联系人
  31. * - mobile: 手机号
  32. * - telephone: 电话
  33. * - position: 职位
  34. * - email: 电子邮件
  35. * - personid: 负责人id
  36. */
  37. public function create(){
  38. $param = $this->request->only(["name"=>"","source"=>"","coop_state"=>"","category"=>"","delivery_way"=>"",
  39. "level"=>"","supplier_type"=>"","pay_type"=>"","registercode"=>"","registertime"=>"","scope"=>"","type"=>"",
  40. "nature"=>"","legaler"=>"","addr"=>"","prove_img"=>"","license_img"=>"","contactor"=>"","mobile"=>"",
  41. "telephone"=>"","position"=>"","email"=>"",'personid'=>'','lead_prove'=>'','legaler_type'=>'','invoice_code'=>'','is_del'=>'0'],"post","trim");
  42. $valid=Validate::rule([
  43. 'name|供应商名称' => 'requireIf:license_img,""|max:255',
  44. 'source|供应商来源' => 'require|number|in:0,1,2',
  45. 'coop_state|合作状态' => 'require|number|in:0,1',
  46. 'category|所属类别' => 'require|number|in:0,1',
  47. 'delivery_way|提供物流方式' => 'require|number|in:0,1',
  48. 'level|供应商级别' => 'require|number|in:0,1,2',
  49. 'supplier_type|供应商性质' => 'require|number|in:0,1,2,3,4',
  50. 'pay_type|付款方式' => 'require|number|in:0,1,2',
  51. 'registercode|纳税识别编号' => 'requireIf:license_img,""|max:255',
  52. 'registertime|成立日期' => 'requireIf:license_img,""|max:255',
  53. 'scope|经营范围' => 'requireIf:license_img,""|max:3000',
  54. 'type|申请类型' => 'require|number|in:0,1',
  55. 'nature|公司类型' => 'requireIf:license_img,""|number',
  56. 'legaler|法人' => 'requireIf:license_img,""|max:255',
  57. 'addr|公司详细地址' => 'requireIf:license_img,""|max:255',
  58. 'license_img|营业执照' => 'requireIf:name,""|url',
  59. 'contactor|联系人' => 'require|max:255',
  60. 'mobile|手机号' => 'require|mobile',
  61. 'telephone|座机号' => 'max:255',
  62. 'position|职位' => 'require',
  63. 'email|邮箱' => 'require|email',
  64. 'personid|负责人' => 'require|number|gt:0',
  65. 'legaler_type|纳税人资质' => 'require|number|in:0,1,2,3,4',
  66. 'invoice_code|纳税人编号' => 'require|max:255|unique:\app\user\model\Supplier,invoice_code^is_del',
  67. 'lead_prove|资质证明' => 'url',
  68. ]);
  69. if(!$valid->check($param))return error($valid->getError());
  70. if ($param['license_img']!=''){
  71. $Busines = $this->GetBusinessInfoByParam($param['license_img']);
  72. /**
  73. * Array of attributes to getter functions (for serialization of requests)
  74. * registrationNumber - 老版本营业执照对应注册号。 - 新三证合一版本营业执照对应社会保障号。
  75. * name 企业名称。
  76. * type 公司/企业类型/主体类型/类型。
  77. * address 住所/营业场所/企业住所/主要经营场所/经营场所。
  78. * legalRepresentative 法定代表人/负责人/执行事务合伙人/投资人/经营者。
  79. * registeredCapital 注册资本/出资额。
  80. * organizationForm 组成形式。
  81. * foundDate 成立日期/注册日期。
  82. * businessTerm 营业期限。
  83. * businessScope 经营范围。
  84. * issueDate 发照日期。
  85. * confidence 相关字段的置信度信息,置信度越大,表示本次识别的对应字段的可靠性越高,在统计意义上,置信度越大,准确率越高。 置信度由算法给出,不直接等价于对应字段的准确率。
  86. * @var string[]
  87. */
  88. if($Busines['isResult']){
  89. $result = $Busines['data'];
  90. $param['ocr_status'] = 1;
  91. if ($param['name'] != $result['name']) {
  92. $param['remark'] = '企业名称与营业执照不匹配';
  93. }
  94. if ($param['registercode'] !=$result['registration_number']&& $param['registercode']!= '') {
  95. $param['remark'] .= '企业纳税识别号与营业执照不匹配';
  96. }
  97. $param['name'] = $result['name'];
  98. $param['addr']=$result['address']??"";
  99. $param['legaler']=$result['legal_representative']??"";
  100. $param['registercode']=$result['registration_number']??"";
  101. $param['registertime']=$result['found_date']??"";
  102. $param['scope']=$result['business_scope']??"";
  103. $param['nature']= CompanyType::checkType($result['type']);
  104. $param['capital'] = $result['registered_capital']??"";
  105. $param['status'] = $param['registercode']==$param['invoice_code']?1:0;
  106. }else{
  107. $param['remark'] = '营业执照识别失败';
  108. $param['ocr_status'] = 2;
  109. }
  110. }
  111. $this->model->startTrans();
  112. try{
  113. $data=[
  114. 'code'=>makeNo('QS'),
  115. 'person'=>\app\user\model\User::where('account_id','=',$param['personid'])->value('nickname',''),
  116. 'creater'=>$this->uname,
  117. 'createrid'=>$this->uid
  118. ];
  119. $create = \app\user\model\Supplier::create(array_merge($data,$param));
  120. if($create->isEmpty())throw new \Exception('供应商创建失败');
  121. $contact=[
  122. "code"=>$data['code'],
  123. "contactor"=>$param['contactor'],
  124. "mobile"=>$param['mobile'],
  125. "position"=>$param['position'],
  126. "telephone"=>$param["telephone"],
  127. "email"=>$param['email'],
  128. ];
  129. $contactC= SupplierContact::create($contact);
  130. if ($contactC->isEmpty())throw new \Exception("供应商联系人信息创建失败");
  131. $create->updater=$this->uname;
  132. $create->updaterid=$this->uid;
  133. $hqcreate= Headquarters::createInfo($create,3);
  134. if (!$hqcreate)throw new \Exception("供应商信息创建失败");
  135. }catch (\Exception $exception){
  136. $this->model->rollback();
  137. return error($exception->getMessage());
  138. }
  139. $this->model->commit();
  140. return success("创建成功");
  141. }
  142. /**
  143. * 编辑供应商
  144. * 参数:
  145. * id 供应商id
  146. * - name: 供应商名称
  147. * - source: 来源
  148. * - coop_state: 合作状态
  149. * - category: 类别
  150. * - delivery_way: 配送方式
  151. * - level: 级别
  152. * - supplier_type: 供应商类型
  153. * - pay_type: 支付方式
  154. * - registercode: 注册码
  155. * - registertime: 注册时间
  156. * - scope: 业务范围
  157. * - type: 类型
  158. * - nature: 性质
  159. * - legaler: 法人
  160. * - addr: 地址
  161. * - prove_img: 证明图片
  162. * - supplier_img: 供应商图片
  163. * - contactor: 联系人
  164. * - mobile: 手机号
  165. * - telephone: 电话
  166. * - position: 职位
  167. * - email: 电子邮件
  168. * - personid: 负责人id
  169. * @return
  170. */
  171. public function save(){
  172. $param = $this->request->param([
  173. 'id' => '',
  174. 'coop_state' => '0',
  175. 'source' => '0',
  176. 'type' => '0',
  177. 'category' => '0',
  178. 'delivery_way' => '0',
  179. 'supplier_type' => '0',
  180. 'level' => '0',
  181. 'pay_type' => '0',
  182. 'license_img' => '',
  183. 'prove_img' => '',
  184. 'contactor' => '',
  185. 'mobile' => '', // 确保这是经过验证的格式
  186. 'telephone' => '',
  187. 'position' => '',
  188. 'registercode' => '',
  189. 'name' => '',
  190. 'email' => '', // 更正了邮箱格式
  191. 'nature' => '',
  192. 'addr' => '',
  193. "capital"=>"",
  194. 'legaler' => '',
  195. 'registertime' => '',
  196. 'scope' => '',
  197. "is_del"=>0,
  198. 'personid' => '','lead_prove'=>'','legaler_type'=>'','invoice_code'=>''],"post",'trim');
  199. $valid=Validate::rule([
  200. "id|主键ID"=>"require|number|gt:0",
  201. 'name|供应商名称' => 'requireIf:license_img,""|max:255',
  202. 'source|供应商来源' => 'require|number|in:0,1,2',
  203. 'coop_state|合作状态' => 'require|number|in:0,1',
  204. 'category|所属类别' => 'require|number|in:0,1',
  205. 'delivery_way|提供物流方式' => 'require|number|in:0,1',
  206. 'level|供应商级别' => 'require|number|in:0,1,2',
  207. 'supplier_type|供应商性质' => 'require|number|in:0,1,2,3,4',
  208. 'pay_type|付款方式' => 'require|number|in:0,1,2',
  209. 'registercode|纳税识别编号' => 'requireIf:license_img,""|max:255',
  210. 'registertime|成立日期' => 'requireIf:license_img,""|max:255',
  211. 'scope|经营范围' => 'requireIf:license_img,""|max:3000',
  212. 'type|申请类型' => 'require|number|in:0,1',
  213. 'nature|公司类型' => 'requireIf:license_img,""',
  214. 'legaler|法人' => 'requireIf:license_img,""|max:255',
  215. 'addr|公司详细地址' => 'requireIf:license_img,""|max:255',
  216. 'license_img|营业执照' => 'requireIf:name,""|url',
  217. 'contactor|联系人' => 'require|max:255',
  218. 'mobile|手机号' => 'require|mobile',
  219. 'telephone|座机号' => 'max:255',
  220. 'position|职位' => 'require',
  221. 'email|邮箱' => 'require|email',
  222. 'personid|负责人' => 'require|number|gt:0',
  223. 'legaler_type|纳税人资质' => 'require|number|in:0,1,2,3,4',
  224. 'invoice_code|纳税人编号' => 'require|max:255|unique:\app\user\model\Supplier,invoice_code^is_del',
  225. 'lead_prove|资质证明' => 'url',
  226. ]);
  227. if(!$valid->check($param)) return error($valid->getError());
  228. $info = \app\user\model\Supplier::with(["contactInfo"])->where('id','=',$param['id'])->find();
  229. if(!$info)return error('供应商不存在');
  230. $goodUpdateBool=false;
  231. if($info->personid!=$param['personid'])$goodUpdateBool=true;
  232. $info->name=$param['name'];
  233. $info->personid=$param['personid'];
  234. $info->person=\app\user\model\User::where('account_id','=',$param['personid'])->value('nickname','');
  235. $info->source=$param['source'];
  236. $info->legaler_type=$param['legaler_type'];
  237. $info->invoice_code=$param['invoice_code'];
  238. $info->lead_prove=$param['lead_prove'];
  239. $info->coop_state=$param['coop_state'];
  240. $info->category=$param['category'];
  241. $info->delivery_way=$param['delivery_way'];
  242. $info->level=$param['level'];
  243. $info->supplier_type=$param['supplier_type'];
  244. $info->pay_type=$param['pay_type'];
  245. $info->type=$param['type'];
  246. $info->prove_img=$param['prove_img'];
  247. $info->updater=$this->uname;
  248. $info->updaterid=$this->uid;
  249. if($param['license_img']!='' && $info->license_img!=$param['license_img']){
  250. $business = $this->GetBusinessInfoByParam($param['license_img']);
  251. if($business['isResult']){
  252. $info->ocr_status = 1;
  253. $result = $business['data'];
  254. if ($param['name'] != $result['name']) {
  255. $info->remark = "企业名称与营业执照不匹配";
  256. }
  257. if ($param['registercode'] !=$result['registration_number']&& $param['registercode']!= '') {
  258. $info->remark .= '企业纳税识别号与营业执照不匹配';
  259. }
  260. $param['name'] = $result['name'];
  261. $param['addr']=$result['address']??'';
  262. $param['legaler']=$result['legal_representative']??'';
  263. $param['registercode']=$result['registration_number']??'';
  264. $param['registertime']=$result['found_date']??'';
  265. $param['scope']=$result['business_scope']??'';
  266. $param['nature']=CompanyType::checkType($result['type']);
  267. $param['capital'] = $result['registered_capital']??'';
  268. }else{
  269. $info->remark = '营业执照识别失败';
  270. $info->ocr_status = 2;
  271. }
  272. }
  273. $info->license_img=$param['license_img'];
  274. $info->registercode=$param['registercode'];
  275. $info->nature=$param['nature'];
  276. $info->legaler=$param['legaler'];
  277. $info->registertime=$param['registertime'];
  278. $info->capital=$param['capital'];
  279. $info->addr=$param['addr'];
  280. $info->scope=$param['scope'];
  281. $info->status=$param['registercode']==$param['invoice_code']?1:0;
  282. $info->remark .= $param['registercode']==$param['invoice_code']?"":"企业纳税识别号与营业执照不匹配";
  283. $info->contactor=$param['contactor'];
  284. $info->mobile=$param['mobile'];
  285. $this->model->startTrans();
  286. try{
  287. if($info->save()){
  288. if(is_null($info->contactInfo)){
  289. $model = new \app\user\model\SupplierContact();
  290. }else{
  291. $model = $info->contactInfo;
  292. }
  293. $contact=[
  294. "code"=>$info->code,
  295. 'contactor'=>$param['contactor'],
  296. 'mobile'=>$param['mobile'],
  297. 'position'=>$param['position'],
  298. 'telephone'=>$param['telephone'],
  299. 'email'=>$param['email'],
  300. ];
  301. $contactC=$model->save($contact);
  302. if (!$contactC)throw new \Exception('供应商联系人信息保存失败');
  303. $hqcreate= Headquarters::createInfo($info,3);
  304. if(!$hqcreate)throw new \Exception('供应商信息保存失败');
  305. }else throw new \Exception('供应商信息保存失败');
  306. $this->model->commit();
  307. }catch (\Exception $e){
  308. $this->model->rollback();
  309. return error($e->getMessage());
  310. }
  311. if($goodUpdateBool)event("updateSupplierGood",["code"=>$info->code,"personid"=>$info->personid,"person"=>$info->person]);
  312. return success("保存成功!");
  313. }
  314. /**
  315. * 获取供应商列表
  316. */
  317. public function list()
  318. {
  319. $param=$this->request->param(["page"=>1,"size"=>15,"createrid"=>"","name"=>"","start"=>"","end"=>"",'status'=>'',
  320. 'ocr_status'=>'',"invoice_code"=>""],"post","trim");
  321. $where=[["is_del","=",0]];
  322. if($param['createrid']!='') $where[]=['createrid','=',$param['createrid']];
  323. if($param['name']!='') $where[]=['name','like',"%".$param['name']."%"];
  324. if($param['status']!='') $where[]=['status','=',$param['status']];
  325. if($param['ocr_status']!='') $where[]=['ocr_status','=',$param['ocr_status']];
  326. if($param['start']!='') $where[]=['addtime','>=',$param['start']];
  327. if($param['end']!='') $where[]=['addtime','<=',$param['end']];
  328. if($param['invoice_code']!='') $where[]=['invoice_code','like','%'.$param['invoice_code']."%"];
  329. $list = $this->model->with(["contactInfo"])->where($where)->order('id desc')
  330. ->paginate(['page'=>$param['page'],'list_rows'=>$param['size']]);
  331. return success("获取成功",["list"=>$list->items(),"count"=>$list->total()]);
  332. }
  333. /**
  334. * 获取供应商详情
  335. */
  336. public function info()
  337. {
  338. $code=$this->request->param("code");
  339. if($code=="")return error("参数错误");
  340. $info = $this->model->with(["contactInfo"])->where('code','=',$code)->findOrEmpty();
  341. if($info->isEmpty())return error("供应商不存在");
  342. $info->legaler_tax = \app\user\model\Supplier::$legaler_tax[$info->legaler_type]??[];
  343. return success("获取成功",$info);
  344. }
  345. /**删除供应商
  346. * @return
  347. */
  348. public function delete()
  349. {
  350. $param=$this->request->param(["id"=>""],"post","trim");
  351. if($param['id']=="")return error("参数错误");
  352. $info = $this->model->where('id','=',$param['id'])->findOrEmpty();
  353. if($info->isEmpty())return error("供应商不存在");
  354. $this->model->startTrans();
  355. try{
  356. $info->is_del=1;
  357. $save=$info->save();
  358. if(!$save)throw new \Exception('删除失败');
  359. $header=Headquarters::where('code',$info->code)->findOrEmpty();
  360. if(!$header->isEmpty()){
  361. $header->is_del=1;
  362. $header->updater=$this->uname;
  363. $header->updaterid=$this->uid;
  364. $header->save();
  365. }
  366. $this->model->commit();
  367. }catch (\Exception $e){
  368. $this->model->rollback();
  369. return error($e->getMessage());
  370. }
  371. return success("删除成功");
  372. }
  373. /**
  374. * @return
  375. */
  376. public function status()
  377. {
  378. $param=$this->request->param(["id"=>"","status"=>""],"post","trim");
  379. $valid=Validate::check($param,[
  380. 'id|供应商id'=>'require',
  381. 'status|状态'=>'require|number|in:0,1',
  382. ]);
  383. if($valid!==true)return error(Validate::getError());
  384. $info = $this->model->where('id','=',$param['id'])->findOrEmpty();
  385. if($info->isEmpty())return error("供应商不存在");
  386. if($info->registercode!=""&& $info->invoice_code!=$info->registercode && $param['status']==1) return error("供应商纳税编号不一致");
  387. if($param['status']==1){
  388. if($info->registercode!=''&& $info->invoice_code!=$info->registercode) return error('供应商纳税编号不一致');
  389. if($info->legaler_type==='' || is_null($info->legaler_type))return error("供应商资质不能为空");
  390. }
  391. $this->model->startTrans();
  392. try{
  393. $info->status=$param['status'];
  394. $save=$info->save();
  395. if(!$save)throw new \Exception("修改失败");
  396. $header=Headquarters::where('code',$info->code)->findOrEmpty();
  397. if(!$header->isEmpty()){
  398. $header->status=$param['status'];
  399. $header->updater=$this->uname;
  400. $header->updaterid=$this->uid;
  401. $header->save();
  402. }
  403. $this->model->commit();
  404. }catch (\Exception $e){
  405. $this->model->rollback();
  406. return error($e->getMessage());
  407. }
  408. return success("修改成功");
  409. }
  410. /**
  411. * @param $post
  412. */
  413. public function upgrade()
  414. {
  415. $post = $this->request->only(['code', 'inv_bank', 'inv_bankNo', 'inv_addr', 'invoice_mobile', 'uid', 'uname', 'invoice_title'], 'post', 'trim');
  416. $val = Validate::rule([
  417. 'code|供应商编码' => 'require',
  418. 'inv_bank|银行名称' => 'require|max:255',
  419. 'inv_bankNo|银行卡号' => 'require|number',
  420. 'inv_addr|联系地址' => 'require|max:255',
  421. 'invoice_mobile' => 'require',
  422. 'invoice_title|公司抬头' => 'require',
  423. ]);
  424. if ($val->check($post) == false) return error($val->getError());
  425. $supplier = $this->model->with(["contactInfo"])->where([['code',"=", $post['code']],["is_del","=",0]])->findOrEmpty();
  426. if ($supplier->isEmpty()) return error("供应商不存在");
  427. if ($supplier->status == 0) return error("供应商未启用");
  428. if ($supplier->is_upgrade == 1) return error("供应商已升级为业务公司");
  429. $this->model->startTrans();
  430. try {
  431. $post['uid']=$this->uid;
  432. $post['uname']=$this->uname;
  433. $up= $this->model->upgrade($post);
  434. if(!$up)throw new \Exception("升级失败");
  435. $this->model->commit();
  436. }catch (\Exception $exception){
  437. $this->model->rollback();
  438. return error($exception->getMessage());
  439. }
  440. return success("升级成功");
  441. }
  442. public function selectQuery()
  443. {
  444. $data=[
  445. "source"=>\app\user\model\Supplier::$source, //供应商来源
  446. "suptype"=>\app\user\model\Supplier::$suptype, //申请类型
  447. "coop_state"=>\app\user\model\Supplier::$coop_state,//合作状态
  448. "pay_type"=>\app\user\model\Supplier::$pay_type,//付款方式
  449. "delivery_way"=>\app\user\model\Supplier::$delivery_way, //提供物流方式
  450. "level"=>\app\user\model\Supplier::$level, //供应商级别
  451. "supplier_type"=>\app\user\model\Supplier::$supplier_type,//供应商性质
  452. "legaler_type"=>\app\user\model\Supplier::$legaler_type, //纳税人资质 类型
  453. "nature"=>\app\user\model\CompanyType::where(["is_del"=>0])->order('id', 'desc')->field(["id","company_type","status"])->select()
  454. ];
  455. return success("获取成功",$data);
  456. }
  457. /**
  458. * 银行信息添加
  459. * @param code|供应商编码
  460. * @param bank_name|银行名称
  461. * @param contact_name|联系人
  462. * @param contact_mobile|联系电话
  463. * @param bank_no|银行卡号
  464. * @return \think\Response|\think\response\Json|void
  465. * @throws \think\db\exception\DbException
  466. */
  467. public function bankCreate()
  468. {
  469. $post = $this->request->only(['code', 'bank_name', 'bank_no', 'contact_name', 'contact_mobile'], 'post', 'trim');
  470. $val = Validate::rule([
  471. 'code|供应商编码' => 'require',
  472. 'bank_name|银行名称' => 'require|max:255',
  473. 'bank_no|银行卡号' => 'require|number|unique:\app\user\model\SupplierBank,bank_no',
  474. 'contact_name|联系人' => 'max:255',
  475. 'contact_mobile|联系电话' => 'max:255',
  476. ]);
  477. if ($val->check($post) == false) return error($val->getError());
  478. $supplier = $this->model->where([['code',"=", $post['code']],["is_del","=",0]])->findOrEmpty();
  479. if ($supplier->isEmpty()) return error("供应商不存在");
  480. $data=[
  481. 'code'=>$post['code'],
  482. 'bank_name'=>$post['bank_name'],
  483. 'bank_no'=>$post['bank_no'],
  484. 'contact_name'=>$post['contact_name'],
  485. 'contact_mobile'=>$post['contact_mobile'],
  486. 'apply_name'=>$this->uname,
  487. 'apply_id'=>$this->uid,
  488. "status"=>0,
  489. "is_default"=>SupplierBank::where(["code"=>$post['code']])->count()==0?1:0,
  490. ];
  491. SupplierBank::create($data);
  492. return success("添加成功");
  493. }
  494. /**
  495. * 银行信息删除
  496. * @param id|供应商银行id
  497. * @return \think\Response|\think\response\Json|void
  498. */
  499. public function bankDelete()
  500. {
  501. $post = $this->request->only(['id'], 'post', 'trim');
  502. $val = Validate::rule([
  503. 'id|供应商银行id' => 'require',
  504. ]);
  505. if ($val->check($post) == false) return error($val->getError());
  506. $bank = SupplierBank::where([['id',"=", $post['id']]])->findOrEmpty();
  507. if ($bank->isEmpty()) return error("供应商银行不存在");
  508. if($bank->is_default==1)return error("默认银行不能删除");
  509. $bank->delete();
  510. return success("删除成功");
  511. }
  512. /**
  513. * @param id|供应商银行id
  514. *@param bank_name|银行名称
  515. *@param contact_name|联系人
  516. *@param contact_mobile|联系电话
  517. *@return \think\Response|\think\response\Json|void
  518. **/
  519. public function bankUpdate()
  520. {
  521. $post = $this->request->only(['id', 'bank_name', 'bank_no', 'contact_name', 'contact_mobile'], 'post', 'trim');
  522. $val = Validate::rule([
  523. 'id|供应商银行id' => 'require',
  524. 'bank_name|银行名称' => 'require|max:255',
  525. 'bank_no|银行卡号' => 'require|number|unique:\app\user\model\SupplierBank,bank_no',
  526. 'contact_name|联系人' => 'max:255',
  527. 'contact_mobile|联系电话' => 'max:255',
  528. ]);
  529. if ($val->check($post) == false) return error($val->getError());
  530. $bank = SupplierBank::where([['id',"=", $post['id']]])->findOrEmpty();
  531. if ($bank->isEmpty()) return error("供应商银行不存在");
  532. $bank->bank_name=$post['bank_name'];
  533. $bank->bank_no=$post['bank_no'];
  534. $bank->contact_name=$post['contact_name'];
  535. $bank->contact_mobile=$post['contact_mobile'];
  536. $bank->save();
  537. return success("修改成功");
  538. }
  539. /**
  540. * @param id|供应商银行id
  541. * @return \think\Response|\think\response\Json|void
  542. * @throws \think\db\exception\DbException
  543. */
  544. public function bankDefault()
  545. {
  546. $post = $this->request->only(['id'], 'post', 'trim');
  547. $val = Validate::rule([
  548. 'id|供应商银行id' => 'require',
  549. ]);
  550. if ($val->check($post) == false) return error($val->getError());
  551. $bank = SupplierBank::where([['id',"=", $post['id']]])->findOrEmpty();
  552. if ($bank->isEmpty()) return error("供应商银行不存在");
  553. SupplierBank::where(["code"=>$bank->code,"is_default"=>1])->update(["is_default"=>0]);
  554. $bank->is_default=1;
  555. $bank->save();
  556. return success("设置成功");
  557. }
  558. /**
  559. * 银行信息列表
  560. * @param code|供应商编码
  561. * @param status|状态
  562. * @param contact_name|联系人
  563. * @param contact_mobile|联系电话
  564. * @param page|页码
  565. * @param size|每页数量
  566. * @return \think\Response|\think\response\Json|void
  567. * @throws \think\db\exception\DbException
  568. */
  569. public function bankList()
  570. {
  571. $post = $this->request->only(['code'=>"","status"=>"","contact_name"=>"","contact_mobile"=>"","page"=>1,"size"=>15], 'post', 'trim');
  572. $where=[];
  573. if($post['code']!="")$where[]=["code","=",$post['code']];
  574. if($post['status']!=="")$where[]=["status","=",$post['status']];
  575. if($post['contact_name']!="")$where[]=["contact_name","like","%".$post['contact_name']."%"];
  576. if($post['contact_mobile']!="")$where[]=["contact_mobile","like","%".$post['contact_mobile']."%"];
  577. $data=SupplierBank::with(['supplier'])->where($where)->order('id', 'desc')
  578. ->paginate(['list_rows'=>$post['size'],'page'=>$post['page']]);
  579. return success("获取成功",["list"=>$data->items(),"total"=>$data->total()]);
  580. }
  581. /**
  582. * @param id|供应商银行id
  583. * @param status|状态
  584. * @return \think\Response|\think\response\Json|void
  585. */
  586. public function bankStatus()
  587. {
  588. $post = $this->request->only(['id','status'], 'post', 'trim');
  589. $val = Validate::rule([
  590. 'id|供应商银行id' => 'require',
  591. 'status|状态' => 'require|in:0,1',
  592. ]);
  593. if ($val->check($post) == false) return error($val->getError());
  594. $bank = SupplierBank::where([['id',"=", $post['id']]])->findOrEmpty();
  595. if ($bank->isEmpty()) return error("供应商银行不存在");
  596. $bank->status=$post['status'];
  597. $bank->save();
  598. return success("修改成功");
  599. }
  600. }