Headquarters.php 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882
  1. <?php
  2. namespace app\controller;
  3. use app\BaseController;
  4. use app\model\Account;
  5. use app\model\AccountCompany;
  6. use think\Exception;
  7. use think\facade\Db;
  8. use think\facade\Validate;
  9. //【公司汇总】
  10. class Headquarters extends BaseController
  11. {
  12. //列表
  13. public function getList()
  14. {
  15. $post = $this->request->only(['code' => '', 'name' => '', 'status' => '', 'page' => 1, 'size' => 10, 'level' => '', 'account_id' => '','type'=>''], 'post');
  16. $condition = [['is_del', '=', 0]];
  17. if ($post['code'] != '') $condition[] = is_array($post['code']) ? ['code', 'in', $post['code']] : ['code', 'like', "%{$post['code']}%"];
  18. if ($post['name'] != '') $condition[] = ['name', 'like', "%{$post['name']}%"];
  19. if ($post['status'] != '') $condition[] = ['status', '=', $post['status']];
  20. if ($post['level'] != 1) {
  21. $companyCode = Db::name('account_company')
  22. ->where(['account_id' => $post['account_id'], 'is_del' => 0, 'status' => 1])
  23. ->column('companyCode');
  24. $condition[] = ['code', 'in', $companyCode];
  25. }
  26. // if ($post['type'] !== '') $condition[] = ['', 'exp', Db::raw("FIND_IN_SET({$post['type']},type)")];
  27. if ($post['type'] !== '') $condition[] = ['type', '=', $post['type']];
  28. //兼容原有各个接口的筛选
  29. $count = Db::name('headquarters')
  30. ->where($condition)
  31. ->count('id');
  32. $list = Db::name('headquarters')
  33. ->field('id,code,name,type,status,addtime,relation_code')
  34. ->where($condition)
  35. ->page($post['page'], $post['size'])
  36. ->order(['addtime' => 'desc', 'id' => 'desc'])
  37. ->select()
  38. ->toArray();
  39. return json_show(0, '获取成功', ['list' => $list, 'count' => $count]);
  40. }
  41. //添加
  42. public function add()
  43. {
  44. $post = $this->request->filter('trim')->post();
  45. Db::startTrans();
  46. try {
  47. switch ($post['type']) {
  48. case 1:
  49. Db::name('business')->insert($post['data']);
  50. break;
  51. case 2:
  52. Db::name('company_info')->insert($post['data']);
  53. break;
  54. case 3:
  55. Db::name('supplier')->insert($post['data']);
  56. break;
  57. }
  58. Db::name('headquarters')
  59. ->insert([
  60. 'code' => $post['code'],
  61. 'name' => $post['name'],
  62. 'type' => $post['type'],
  63. 'invoice_title' => $post['invoice_title'],
  64. 'invoice_people' => $post['invoice_people'],
  65. 'invoice_addr' => $post['invoice_addr'],
  66. 'invoice_code' => $post['invoice_code'],
  67. 'invoice_bank' => $post['invoice_bank'],
  68. 'invoice_bankNo' => $post['invoice_bankNo'],
  69. 'invoice_img' => $post['invoice_img'],
  70. 'remark' => $post['remark'],
  71. 'status' => $post['status'],
  72. 'is_del' => 0,
  73. 'creater' => $post['creater'],
  74. 'createrid' => $post['createrid'],
  75. 'addtime' => date('Y-m-d H:i:s'),
  76. 'updater' => $post['updater'],
  77. 'updaterid' => $post['updaterid'],
  78. 'updater' => date('Y-m-d H:i:s'),
  79. ]);
  80. Db::commit();
  81. return json_show(0, '添加成功');
  82. } catch (Exception $e) {
  83. Db::rollback();
  84. return json_show(1002, '添加失败,' . $e->getMessage());
  85. }
  86. }
  87. //编辑
  88. public function update()
  89. {
  90. $post = $this->request->filter('trim')->post();
  91. Db::startTrans();
  92. try {
  93. switch ($post['type']) {
  94. case 1:
  95. Db::name('business')->where(['companyNo' => $post['data']['companyNo'], 'is_del' => 0])->update($post['data']);
  96. break;
  97. case 2:
  98. Db::name('company_info')->where(['companyNo' => $post['data']['companyNo'], 'is_del' => 0])->update($post['data']);
  99. break;
  100. case 3:
  101. Db::name('supplier')->where(['code' => $post['data']['code'], 'is_del' => 0])->update($post['data']);
  102. break;
  103. }
  104. Db::name('headquarters')
  105. ->where(['code' => $post['code'], 'is_del' => 0])
  106. ->update([
  107. 'code' => $post['code'],
  108. 'name' => $post['name'],
  109. 'type' => $post['type'],
  110. 'invoice_title' => $post['invoice_title'],
  111. 'invoice_people' => $post['invoice_people'],
  112. 'invoice_addr' => $post['invoice_addr'],
  113. 'invoice_code' => $post['invoice_code'],
  114. 'invoice_bank' => $post['invoice_bank'],
  115. 'invoice_bankNo' => $post['invoice_bankNo'],
  116. 'invoice_img' => $post['invoice_img'],
  117. 'remark' => $post['remark'],
  118. 'updater' => $post['updater'],
  119. 'updaterid' => $post['updaterid'],
  120. 'updater' => date('Y-m-d H:i:s'),
  121. ]);
  122. Db::commit();
  123. return json_show(0, '修改成功');
  124. } catch (Exception $e) {
  125. Db::rollback();
  126. return json_show(1002, '修改失败,' . $e->getMessage());
  127. }
  128. }
  129. //详情
  130. public function info()
  131. {
  132. $code = $this->request->post('code', '', 'trim');
  133. if ($code == '') return json_show(1003, "参数 code 不能为空");
  134. $res = Db::name('headquarters')
  135. ->field(true)
  136. ->where(['code' => $code, 'is_del' => 0])
  137. ->findOrEmpty();
  138. if (empty($res)) return json_show(1004, '未找到相关汇总信息');
  139. switch ($res['type']) {
  140. case 1:
  141. $child = Db::name('business')
  142. ->field(true)
  143. ->where(['companyNo' => $code, 'is_del' => 0])
  144. ->findOrEmpty();
  145. break;
  146. case 2:
  147. $child = Db::name('company_info')
  148. ->where(['companyNo' => $code, 'is_del' => 0])
  149. ->findOrEmpty();
  150. break;
  151. case 3:
  152. $child = Db::name('supplier')
  153. ->where(['code' => $code, 'is_del' => 0])
  154. ->findOrEmpty();
  155. break;
  156. }
  157. $res['child'] = $child;
  158. return json_show(0, '获取成功', $res);
  159. }
  160. //删除
  161. public function delete()
  162. {
  163. $param = $this->request->only(['ids', 'type', 'updater', 'updaterid'], 'post', 'trim');
  164. $val = Validate::rule([
  165. 'ids' => 'require|array|max:100',
  166. 'type|类别' => 'require|number|in:1,2,3',
  167. 'updater|操作人' => 'require|max:255',
  168. 'updaterid|操作人id' => 'require|number|gt:0',
  169. ]);
  170. if (!$val->check($param)) return json_show(1004, $val->getError());
  171. Db::startTrans();
  172. try {
  173. $date = date('Y-m-d H:i:s');
  174. $codes = [];
  175. switch ($param['type']) {
  176. case 1:
  177. $where = [['is_del', '=', 0], ['id', 'in', $param['ids']]];
  178. $codes = Db::name('business')->where($where)->column('companyNo');
  179. Db::name('business')->where($where)->update(['is_del' => 1, 'updatetime' => $date]);
  180. break;
  181. case 2:
  182. Db::name('company_info')
  183. ->where('is_del', 0)
  184. ->whereIn('companyNo', $param['codes'])
  185. ->update(['is_del' => 1, 'updatetime' => $date]);
  186. break;
  187. case 3:
  188. $where = [['is_del', '=', 0], ['id', 'in', $param['ids']]];
  189. $codes = Db::name('supplier')->where($where)->column('code');
  190. Db::name('supplier')->where($where)->update(['is_del' => 1, 'updatetime' => $date]);
  191. break;
  192. }
  193. Db::name('headquarters')
  194. ->where('is_del', 0)
  195. ->whereIn('code', $codes)
  196. ->update([
  197. 'is_del' => 1,
  198. 'updater' => $param['updater'],
  199. 'updaterid' => $param['updaterid'],
  200. 'updatetime' => date('Y-m-d H:i:s')
  201. ]);
  202. Db::commit();
  203. return json_show(0, '删除成功');
  204. } catch (Exception $exception) {
  205. Db::rollback();
  206. return json_show(1005, '删除失败,' . $exception->getMessage());
  207. }
  208. }
  209. //启禁用
  210. public function status()
  211. {
  212. $param = $this->request->only(['id', 'type', 'status', 'updater', 'updaterid'], 'post', 'trim');
  213. $val = Validate::rule([
  214. 'id' => 'require|number|gt:0',
  215. 'type|类别' => 'require|number|in:1,2,3',
  216. 'status|状态' => 'require|number|in:0,1',
  217. 'updater|操作人' => 'require|max:255',
  218. 'updaterid|操作人id' => 'require|number|gt:0',
  219. ]);
  220. if (!$val->check($param)) return json_show(1004, $val->getError());
  221. Db::startTrans();
  222. try {
  223. $date = date('Y-m-d H:i:s');
  224. $code = '';
  225. switch ($param['type']) {
  226. case 1:
  227. $tmp = Db::name('business')
  228. ->field('id,companyNo code,status')
  229. ->where(['id' => $param['id'], 'is_del' => 0])
  230. ->findOrEmpty();
  231. if (empty($tmp)) throw new Exception('未找到对应数据');
  232. if ($tmp['status'] == $param['status']) throw new Exception('操作重复');
  233. Db::name('business')
  234. ->where(['id' => $param['id'], 'is_del' => 0])
  235. ->where('status', '<>', $param['status'])
  236. ->update([
  237. 'status' => $param['status'],
  238. 'updatetime' => $date
  239. ]);
  240. break;
  241. case 2:
  242. Db::name('company_info')
  243. ->where(['companyNo' => $param['data']['companyNo'], 'is_del' => 0])
  244. ->where('status', '<>', $param['status'])
  245. ->update([
  246. 'status' => $param['status'],
  247. 'updatetime' => $date
  248. ]);
  249. break;
  250. case 3:
  251. $tmp = Db::name('supplier')
  252. ->where(['id' => $param['id'], 'is_del' => 0])
  253. ->field('id,code,status')
  254. ->findOrEmpty();
  255. if (empty($tmp)) throw new Exception('该供应商不存在');
  256. if ($tmp['status'] == $param['status']) throw new Exception('操作重复');
  257. Db::name('supplier')
  258. ->where(['id' => $param['id'], 'is_del' => 0])
  259. ->where('status', '<>', $param['status'])
  260. ->update([
  261. 'status' => $param['status'],
  262. 'updatetime' => $date
  263. ]);
  264. break;
  265. }
  266. Db::name('headquarters')
  267. ->where(['code' => $tmp['code'], 'is_del' => 0])
  268. ->where('status', '<>', $param['status'])
  269. ->update([
  270. 'status' => $param['status'],
  271. 'updater' => $param['updater'],
  272. 'updaterid' => $param['updaterid'],
  273. 'updatetime' => $date
  274. ]);
  275. Db::commit();
  276. return json_show(0, '操作成功');
  277. } catch (Exception $exception) {
  278. Db::rollback();
  279. return json_show(1005, '操作失败,' . $exception->getMessage());
  280. }
  281. }
  282. //获取指定编码和名称
  283. public function getCodeAndName()
  284. {
  285. $companyNo = $this->request->filter('trim')->post('code');
  286. $res_1 = Db::name('headquarters')
  287. ->where('is_del', 0)
  288. ->whereIn('code', $companyNo)
  289. ->column('name', 'code');
  290. // $res_2 = Db::name('headquarters')
  291. // ->where('is_del', 0)
  292. // ->whereIn('relation_code', $companyNo)
  293. // ->column('name', 'relation_code');
  294. return json_show(0, '请求成功', $res_1);
  295. }
  296. //业务公司
  297. public function bGetList()
  298. {
  299. $param = $this->request->only([
  300. 'page' => 1,
  301. 'size' => 10,
  302. 'company' => '',
  303. 'status' => '',
  304. 'creater' => '',
  305. 'start' => '',
  306. 'end' => '',
  307. 'company_name' => '',
  308. ], 'post', 'trim');
  309. $where = [['a.is_del', "=", 0]];
  310. if ($param['company'] !== "") $where[] = ["a.company", "like", '%' . $param['company'] . '%'];
  311. if ($param['status'] !== "") $where[] = ["a.status", '=', $param['status']];
  312. if ($param['creater'] !== "") $where[] = ["a.creater", "like", '%' . $param['creater'] . '%'];
  313. if ($param['start'] !== "") $where[] = ["a.addtime", '>=', $param['start']];
  314. if ($param['end'] !== "") $where[] = ["a.addtime", '<=', $param['end'] . ' 23:59:59'];
  315. // if ($param['company_name'] !== "") $where[] = ["a.createrid", 'in', $param['company_name']];
  316. $count = Db::name('business')
  317. ->alias('a')
  318. // ->leftJoin("depart_user u", "u.uid=a.createrid AND u.is_del=0")
  319. ->where($where)
  320. ->count('a.id');
  321. $list = Db::name('business')
  322. ->alias('a')
  323. ->field(true)
  324. // ->field('a.*,u.itemid')
  325. // ->leftJoin("depart_user u", "u.uid=a.createrid AND u.is_del=0")
  326. ->where($where)
  327. ->page($param['page'], $param['size'])
  328. ->order(['addtime' => 'desc', 'id' => 'desc'])
  329. ->select()
  330. ->toArray();
  331. // $all_codes = Db::name('supplier_contact')
  332. // ->whereIn('code', array_column($list, 'code'))
  333. // ->column("id,contactor,mobile", 'code');
  334. //获取开通账号的供应商
  335. // $account = checkHasAccountBySupplierNos(array_column($list,'code'));
  336. foreach ($list as &$value) {
  337. $value['company_name'] = '';
  338. }
  339. return json_show("0", "获取成功", ['list' => $list, 'count' => $count]);
  340. }
  341. public function bCreate()
  342. {
  343. $param = $this->request->filter('trim')->post('');
  344. Db::startTrans();
  345. try {
  346. $tmp = Db::name('business')
  347. ->field('id')
  348. ->where(['company' => $param['company'], 'is_del' => 0])
  349. ->findOrEmpty();
  350. if (!empty($tmp)) throw new Exception('该公司名称已存在');
  351. Db::name('business')->insert($param);
  352. Db::name('headquarters')->insert([
  353. 'code' => $param['companyNo'],
  354. 'name' => $param['company'],
  355. 'type' => 1,
  356. 'invoice_title' => '',
  357. 'invoice_people' => '',
  358. 'invoice_addr' => $param['inv_addr'],
  359. 'invoice_mobile' => $param['mobile'],
  360. 'invoice_code' => $param['inv_code'],
  361. 'invoice_bank' => $param['inv_bank'],
  362. 'invoice_bankNo' => $param['inv_bankNo'],
  363. 'invoice_img' => $param['license_img'],
  364. 'remark' => '',
  365. 'status' => $param['status'],
  366. 'is_del' => $param['is_del'],
  367. 'creater' => $param['creater'],
  368. 'createrid' => $param['createrid'],
  369. 'addtime' => $param['addtime'],
  370. 'updater' => $param['creater'],
  371. 'updaterid' => $param['createrid'],
  372. 'updatetime' => $param['updatetime'],
  373. ]);
  374. Db::commit();
  375. return json_show(0, '添加成功');
  376. } catch (Exception $exception) {
  377. Db::rollback();
  378. return json_show(1003, $exception->getMessage());
  379. }
  380. }
  381. public function bInfo()
  382. {
  383. $companyNo = $this->request->post('companyNo', '', 'trim');
  384. if ($companyNo == "") return json_show(1004, "参数companyNo不能为空");
  385. $info = Db::name('business')
  386. ->alias('a')
  387. ->field('a.*,b.company_type')
  388. ->leftJoin('company_type b', 'b.id=a.type AND b.is_del=0')
  389. ->where(['a.companyNo' => $companyNo, 'a.is_del' => 0])
  390. ->findOrEmpty();
  391. if (empty($info)) return json_show(1002, "未找到数据");
  392. return json_show(0, '获取成功', $info);
  393. }
  394. public function bEdit()
  395. {
  396. $param = $this->request->filter('trim')->post('');
  397. Db::startTrans();
  398. try {
  399. $info = Db::name('business')
  400. ->field('id,companyNo')
  401. ->where(['id' => $param['id'], 'is_del' => 0])
  402. ->findOrEmpty();
  403. if (empty($info)) throw new Exception('未找到数据');
  404. Db::name('business')
  405. ->where(['id' => $param['id'], 'is_del' => 0])
  406. ->update($param);
  407. Db::name('headquarters')
  408. ->where(['code' => $info['companyNo'], 'is_del' => 0])
  409. ->update([
  410. 'invoice_addr' => $param['inv_addr'],
  411. 'invoice_mobile' => $param['mobile'],
  412. 'invoice_bank' => $param['inv_bank'],
  413. 'invoice_bankNo' => $param['inv_bankNo'],
  414. 'invoice_title' => $param['invoice_title'],
  415. 'updatetime' => date("Y-m-d H:i:s"),
  416. ]);
  417. Db::commit();
  418. return json_show(0, '修改成功');
  419. } catch (Exception $exception) {
  420. Db::rollback();
  421. return json_show(1003, $exception->getMessage());
  422. }
  423. }
  424. public function bTitle()
  425. {
  426. $param = $this->request->only(['company_type' => '', 'status' => ''], 'post', 'trim');
  427. $rs = Db::name('company_type')->where('is_del', 0);
  428. if ($param['company_type'] != '') $rs->whereLike('company_type', '%' . $param['company_type'] . '%');
  429. if ($param['status'] != '') $rs->where('status', $param['status']);
  430. $list = $rs->select()->toArray();
  431. return json_show(0, '获取成功', $list);
  432. }
  433. //供应商
  434. public function sGetList()
  435. {
  436. $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');
  437. $where = [['s.is_del', "=", 0]];
  438. if ($param['name'] !== "") $where[] = ["s.name", "like", '%' . $param['name'] . '%'];
  439. if ($param['code'] !== "") $where[] = ["s.code", "like", '%' . $param['code'] . '%'];
  440. if ($param['more_code'] !== "") $where[] = ["s.code", "in", $param['more_code']];
  441. if ($param['creater'] !== "") $where[] = ["s.creater", "like", '%' . $param['creater'] . '%'];
  442. if ($param['person'] !== "") $where[] = ["s.person", "like", '%' . $param['person'] . '%'];
  443. if ($param['status'] !== "") $where[] = ["s.status", '=', $param['status']];
  444. if ($param['ocr_status'] !== "") $where[] = ["s.ocr_status", '=', $param['ocr_status']];
  445. if ($param['start'] !== "") $where[] = ["s.addtime", '>=', $param['start']];
  446. if ($param['end'] !== "") $where[] = ["s.addtime", '<=', $param['end'] . ' 23:59:59'];
  447. if ($param['is_platform'] !== "") $where[] = ["s.is_platform", '=', $param['is_platform']];
  448. // if ($param['company_name'] !== "") $where[] = ["s.createrid", 'in', $param['company_name']];
  449. $count = Db::name('supplier')
  450. ->alias('s')
  451. // ->leftJoin("depart_user u", "u.uid=s.createrid AND u.is_del=0")
  452. ->where($where)
  453. ->count('s.id');
  454. $list = Db::name('supplier')
  455. ->alias('s')
  456. ->field(true)
  457. // ->field('s.*,u.itemid')
  458. // ->leftJoin("depart_user u", "u.uid=s.createrid AND u.is_del=0")
  459. ->where($where)
  460. ->page($param['page'], $param['size'])
  461. ->order("addtime desc")
  462. ->select()
  463. ->toArray();
  464. // $all_codes = Db::name('supplier_contact')
  465. // ->whereIn('code', array_column($list, 'code'))
  466. // ->column("id,contactor,mobile", 'code');
  467. //获取开通账号的供应商
  468. // $account = checkHasAccountBySupplierNos(array_column($list,'code'));
  469. foreach ($list as &$value) {
  470. $value['company_name'] = '';
  471. }
  472. return json_show("0", "获取成功", ['list' => $list, 'count' => $count]);
  473. }
  474. public function sCreate()
  475. {
  476. $param = $this->request->only(['data', 'contact'], 'post', 'trim');
  477. $val = Validate::rule([
  478. 'data|供应商数据' => 'require|array',
  479. 'contact|联系人' => 'require|array',
  480. ]);
  481. if ($val->check($param) == false) return json_show(1004, $val->getError());
  482. Db::startTrans();
  483. try {
  484. $tmp = Db::name('supplier')
  485. ->field('id')
  486. ->where(['name' => $param['data']['name'], 'is_del' => 0])
  487. ->findOrEmpty();
  488. if (!empty($tmp)) throw new Exception('该供应商名称已存在');
  489. $id = Db::name('supplier')->insertGetId($param['data']);
  490. Db::name('supplier_contact')->insert($param['contact']);
  491. Db::name('headquarters')->insert([
  492. 'code' => $param['data']['code'],
  493. 'name' => $param['data']['name'],
  494. 'type' => 3,
  495. 'invoice_title' => '',
  496. 'invoice_people' => '',
  497. 'invoice_addr' => '',
  498. 'invoice_mobile' => $param['contact']['mobile'],
  499. 'invoice_code' => '',
  500. 'invoice_bank' => '',
  501. 'invoice_bankNo' => '',
  502. 'invoice_img' => $param['data']['license_img'],
  503. 'remark' => $param['data']['remark'],
  504. 'status' => $param['data']['status'],
  505. 'is_del' => $param['data']['is_del'],
  506. 'creater' => $param['data']['creater'],
  507. 'createrid' => $param['data']['createrid'],
  508. 'addtime' => $param['data']['addtime'],
  509. 'updater' => $param['data']['creater'],
  510. 'updaterid' => $param['data']['createrid'],
  511. 'updatetime' => $param['data']['updatetime'],
  512. ]);
  513. Db::commit();
  514. return json_show(0, '添加成功', ['id' => $id]);
  515. } catch (Exception $exception) {
  516. Db::rollback();
  517. return json_show(1003, $exception->getMessage());
  518. }
  519. }
  520. public function sInfo()
  521. {
  522. $code = $this->request->post('code', '', 'trim');
  523. if ($code == "") return json_show(1004, "参数code不能为空");
  524. $info = Db::name('supplier')
  525. ->alias('a')
  526. ->field('a.*,b.contactor,b.mobile,b.position,b.email,b.telephone')
  527. ->leftJoin('supplier_contact b', 'b.code=a.code AND b.is_del=0')
  528. ->where(['a.code' => $code, 'a.is_del' => 0])
  529. ->findOrEmpty();
  530. if (empty($info)) return json_show(1002, "未找到供应商数据");
  531. return json_show(0, '获取成功', $info);
  532. }
  533. public function sEdit()
  534. {
  535. $param = $this->request->only(['data', 'contact'], 'post', 'trim');
  536. $val = Validate::rule([
  537. 'data|供应商数据' => 'require|array',
  538. 'contact|联系人' => 'require|array',
  539. ]);
  540. if ($val->check($param) == false) return json_show(1004, $val->getError());
  541. Db::startTrans();
  542. try {
  543. $tmp = Db::name('supplier')
  544. ->field('id')
  545. ->where(['name' => $param['data']['name'], 'is_del' => 0])
  546. ->where('id', '<>', $param['data']['id'])
  547. ->findOrEmpty();
  548. if (!empty($tmp)) throw new Exception('该供应商名称已存在');
  549. $id = Db::name('supplier')
  550. ->where(['id' => $param['data']['id'], 'is_del' => 0])
  551. ->update($param['data']);
  552. $tmp = Db::name('supplier_contact')
  553. ->field('id')
  554. ->where(['code' => $param['contact']['code'], 'is_del' => 0])
  555. ->findOrEmpty();
  556. if (empty($tmp)) Db::name('supplier_contact')->insert(array_merge($param['contact'], ['addtime' => $param['contact']['updatetime']]));
  557. else Db::name('supplier_contact')->where(['id' => $tmp['id'], 'is_del' => 0])->update($param['contact']);
  558. Db::name('headquarters')
  559. ->where(['code' => $param['data']['code'], 'is_del' => 0])
  560. ->update([
  561. 'name' => $param['data']['name'],
  562. 'type' => 3,
  563. 'invoice_title' => '',
  564. 'invoice_people' => '',
  565. 'invoice_addr' => '',
  566. 'invoice_mobile' => $param['contact']['mobile'],
  567. 'invoice_code' => '',
  568. 'invoice_bank' => '',
  569. 'invoice_bankNo' => '',
  570. 'invoice_img' => $param['data']['license_img'],
  571. 'remark' => $param['data']['remark'],
  572. 'is_del' => $param['data']['is_del'],
  573. 'updater' => $param['data']['creater'],
  574. 'updaterid' => $param['data']['createrid'],
  575. 'updatetime' => $param['data']['updatetime'],
  576. ]);
  577. Db::commit();
  578. return json_show(0, '修改成功', ['id' => $id]);
  579. } catch (Exception $exception) {
  580. Db::rollback();
  581. return json_show(1003, $exception->getMessage());
  582. }
  583. }
  584. //客户
  585. public function cInfo()
  586. {
  587. $companyNo = $this->request->post('companyNo','','trim');
  588. $info = Db::name('customer_info')
  589. ->where(['is_del'=>0,'companyNo'=>$companyNo])
  590. ->field(true)
  591. ->findOrEmpty();
  592. return json_show(0, "获取成功", $info);
  593. }
  594. public function cTitle()
  595. {
  596. $param = $this->request->only([
  597. 'page' => 1,
  598. 'size' => 10,
  599. 'companyNo' => '',
  600. 'companyName' => '',
  601. 'itemid' => '',
  602. ], 'post', 'trim');
  603. $where [] = ['is_del', "=", 0];
  604. if ($param['companyNo'] != '') $where[] = ['companyNo', 'like', '%' . $param['companyNo'] . '%'];
  605. if ($param['companyName'] != '') $where[] = ['companyName', 'like', '%' . $param['companyName'] . '%'];
  606. if ($param['itemid'] != '') $where[] = ['itemid', '=', $param['itemid']];
  607. $count = Db::name('customer_info')
  608. ->where($where)
  609. ->count('id');
  610. $item = Db::name('customer_info')
  611. ->where($where)
  612. ->field("id,companyNo,companyName,LENGTH(companyName) as weight")
  613. ->order(['weight' => 'asc', 'id' => 'desc'])
  614. ->page($param['page'], $param['size'])
  615. ->select()
  616. ->toArray();
  617. return json_show(0, "获取成功", ['item' => $item, 'count' => $count]);
  618. }
  619. //供应商升级成业务公司
  620. public function supplerUpgrade()
  621. {
  622. $post = $this->request->only(['code', 'inv_bank', 'inv_bankNo', 'inv_addr', 'mobile', 'uid', 'uname', 'invoice_title'], 'post', 'trim');
  623. $val = Validate::rule([
  624. 'code|供应商编码' => 'require',
  625. 'inv_bank|银行名称' => 'require|max:255',
  626. 'inv_bankNo|银行卡号' => 'require|number',
  627. 'inv_addr|联系地址' => 'require|max:255',
  628. 'mobile|联系电话' => 'require',
  629. 'invoice_title|公司抬头' => 'require',
  630. ]);
  631. if ($val->check($post) == false) return json_show(1004, $val->getError());
  632. $res = Db::name('supplier')
  633. ->alias('a')
  634. ->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')
  635. ->leftJoin('supplier_contact b', 'b.code=a.code')
  636. ->where(['a.code' => $post['code'], 'a.is_del' => 0])
  637. ->findOrEmpty();
  638. if (empty($res)) return json_show(1004, '该供应商不存在');
  639. if ($res['status'] != 1) return json_show(1004, '该供应商已禁用');
  640. if ($res['is_upgrade'] == 1) return json_show(1004, '不能重复升级');
  641. $temp = Db::name('company_type')
  642. ->field('id')
  643. ->where(['company_type' => $res['nature'], 'is_del' => 0])
  644. ->findOrEmpty();
  645. // if (empty($temp)) return json_show(1004, '对应公司类型不存在');
  646. Db::startTrans();
  647. try {
  648. $date = date('Y-m-d H:i:s');
  649. $business_code = makeNo('GS');
  650. //业务公司列表
  651. Db::name('business')
  652. ->insert([
  653. 'company' => $res['name'],
  654. 'companyNo' => $business_code,
  655. 'inv_code' => $res['registercode'],
  656. 'company_type' => $res['nature'],
  657. 'type' => $temp['id'] ?? 0,
  658. 'creater' => $post['uname'],
  659. 'createrid' => $post['uid'],
  660. 'inv_legaler' => $res['legaler'],
  661. 'inv_time' => $res['registertime'],
  662. 'inv_addr' => $post['inv_addr'],
  663. 'inv_bank' => $post['inv_bank'],
  664. 'inv_bankNo' => $post['inv_bankNo'],
  665. 'contactor' => $res['contactor'],
  666. 'mobile' => $res['mobile'],
  667. 'addr' => $res['addr'],
  668. 'inv_scope' => $res['scope'],
  669. 'license_img' => $res['license_img'],
  670. 'invoice_title' => $post['invoice_title'],
  671. 'status' => 1,
  672. 'is_del' => 0,
  673. 'addtime' => $date,
  674. 'updatetime' => $date,
  675. ]);
  676. $temp_supplier = Db::name('headquarters')
  677. ->where(['code' => $post['code'], 'type' => 3])
  678. ->findOrEmpty();
  679. //汇总表
  680. Db::name('headquarters')
  681. ->where(['code' => $post['code'], 'type' => 3])
  682. ->update([
  683. 'relation_code' => $business_code,
  684. 'updater' => $post['uname'],
  685. 'updaterid' => $post['uid'],
  686. 'updatetime' => $date,
  687. ]);
  688. Db::name('headquarters')
  689. ->insert(array_merge($temp_supplier, [
  690. 'id'=>null,
  691. 'code' => $business_code,
  692. 'type' => 1,//业务公司
  693. 'relation_code'=> $post['code']
  694. ]));
  695. //供应商
  696. Db::name('supplier')
  697. ->where(['code' => $post['code'], 'is_upgrade' => 0, 'is_del' => 0])
  698. ->update(['is_upgrade' => 1, 'updatetime' => $date]);
  699. //账号与公司的关联关系
  700. Db::name('account_company')
  701. ->insert([
  702. 'account_id' => $post['uid'],
  703. 'companyCode' => $business_code,
  704. 'companyName' => $res['name'],
  705. 'company_type' => 2,
  706. 'status' => 1,
  707. 'is_del' => 0,
  708. 'addtime' => $date,
  709. 'updatetime' => $date,
  710. ]);
  711. Db::commit();
  712. return json_show(0, '升级成功');
  713. } catch (Exception $exception) {
  714. Db::rollback();
  715. return json_show(1004, '升级失败,' . $exception->getMessage());
  716. }
  717. }
  718. //获取业务公司列表(为了采销临时脚本同步数据到结算平台使用,正式上线要删除)
  719. public function getBusinessListTmp()
  720. {
  721. $where = $this->request->filter('trim')->post('where', []);
  722. $list = Db::name('business')
  723. ->alias('a')
  724. ->field('a.*,b.code as supplierNo')
  725. ->leftJoin('headquarters b','b.relation_code=a.companyNo')
  726. ->where($where)
  727. ->select()
  728. ->toArray();
  729. return json_show(0, '获取成功', $list);
  730. }
  731. }