Headquarters.php 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165
  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. use think\facade\Cache;
  10. //【公司汇总】
  11. class Headquarters extends BaseController
  12. {
  13. //列表
  14. public function getList()
  15. {
  16. $post = $this->request->only(['code' => '', 'name' => '', 'status' => '', 'page' => 1, 'size' => 10, 'level' => '', 'account_id' => '', 'type' => ''], 'post');
  17. $condition = [['is_del', '=', 0]];
  18. if ($post['code'] != '') $condition[] = is_array($post['code']) ? ['code', 'in', $post['code']] : ['code', 'like', "%{$post['code']}%"];
  19. if ($post['name'] != '') $condition[] = ['name', 'like', "%{$post['name']}%"];
  20. if ($post['status'] != '') $condition[] = ['status', '=', $post['status']];
  21. if ($post['level'] != 1) {
  22. $companyCode = Db::name('account_company')
  23. ->where(['account_id' => $post['account_id'], 'is_del' => 0, 'status' => 1])
  24. ->column('companyCode');
  25. $condition[] = ['code', 'in', $companyCode];
  26. }
  27. // if ($post['type'] !== '') $condition[] = ['', 'exp', Db::raw("FIND_IN_SET({$post['type']},type)")];
  28. if ($post['type'] !== '') $condition[] = ['type', '=', $post['type']];
  29. //兼容原有各个接口的筛选
  30. $count = Db::name('headquarters')
  31. ->where($condition)
  32. ->count('id');
  33. $list = Db::name('headquarters')
  34. ->field('id,code,name,type,status,addtime,relation_code')
  35. ->where($condition)
  36. ->page($post['page'], $post['size'])
  37. ->order(['addtime' => 'desc', 'id' => 'desc'])
  38. ->select()
  39. ->toArray();
  40. return json_show(0, '获取成功', ['list' => $list, 'count' => $count]);
  41. }
  42. //添加
  43. public function add()
  44. {
  45. $post = $this->request->filter('trim')->post();
  46. Db::startTrans();
  47. try {
  48. switch ($post['type']) {
  49. case 1:
  50. Db::name('business')->insert($post['data']);
  51. break;
  52. case 2:
  53. Db::name('customer_info')->insert($post['data']);
  54. break;
  55. case 3:
  56. Db::name('supplier')->insert($post['data']);
  57. break;
  58. }
  59. Db::name('headquarters')
  60. ->insert([
  61. 'code' => $post['code'],
  62. 'name' => $post['name'],
  63. 'type' => $post['type'],
  64. 'invoice_title' => $post['invoice_title'],
  65. 'invoice_people' => $post['invoice_people'],
  66. 'invoice_addr' => $post['invoice_addr'],
  67. 'invoice_code' => $post['invoice_code'],
  68. 'invoice_bank' => $post['invoice_bank'],
  69. 'invoice_bankNo' => $post['invoice_bankNo'],
  70. 'invoice_img' => $post['invoice_img'],
  71. 'remark' => $post['remark'],
  72. 'status' => $post['status'],
  73. 'is_del' => 0,
  74. 'creater' => $post['creater'],
  75. 'createrid' => $post['createrid'],
  76. 'addtime' => date('Y-m-d H:i:s'),
  77. 'updater' => $post['updater'],
  78. 'updaterid' => $post['updaterid'],
  79. 'updatetime' => date('Y-m-d H:i:s'),
  80. ]);
  81. Db::commit();
  82. Cache::store("redis")->handler()->lpush("companycopy",$post);
  83. return json_show(0, '添加成功');
  84. } catch (Exception $e) {
  85. Db::rollback();
  86. return json_show(1002, '添加失败,' . $e->getMessage());
  87. }
  88. }
  89. //编辑
  90. public function update()
  91. {
  92. $post = $this->request->filter('trim')->post();
  93. Db::startTrans();
  94. try {
  95. switch ($post['type']) {
  96. case 1:
  97. Db::name('business')->where(['companyNo' => $post['data']['companyNo'], 'is_del' => 0])->update($post['data']);
  98. break;
  99. case 2:
  100. Db::name('customer_info')->where(['companyNo' => $post['data']['companyNo'], 'is_del' => 0])->update($post['data']);
  101. break;
  102. case 3:
  103. Db::name('supplier')->where(['code' => $post['data']['code'], 'is_del' => 0])->update($post['data']);
  104. break;
  105. }
  106. Db::name('headquarters')
  107. ->where(['code' => $post['code'], 'is_del' => 0])
  108. ->update([
  109. 'code' => $post['code'],
  110. 'name' => $post['name'],
  111. 'type' => $post['type'],
  112. 'invoice_title' => $post['invoice_title'],
  113. 'invoice_people' => $post['invoice_people'],
  114. 'invoice_addr' => $post['invoice_addr'],
  115. 'invoice_code' => $post['invoice_code'],
  116. 'invoice_bank' => $post['invoice_bank'],
  117. 'invoice_bankNo' => $post['invoice_bankNo'],
  118. 'invoice_img' => $post['invoice_img'],
  119. 'remark' => $post['remark'],
  120. 'updater' => $post['updater'],
  121. 'updaterid' => $post['updaterid'],
  122. 'updatetime' => date('Y-m-d H:i:s'),
  123. ]);
  124. Db::commit();
  125. Cache::store("redis")->handler()->lpush("companycopy",json_encode($post,JSON_UNESCAPED_UNICODE));
  126. return json_show(0, '修改成功');
  127. } catch (Exception $e) {
  128. Db::rollback();
  129. return json_show(1002, '修改失败,' . $e->getMessage());
  130. }
  131. }
  132. //详情
  133. public function info()
  134. {
  135. $code = $this->request->post('code', '', 'trim');
  136. if ($code == '') return json_show(1003, "参数 code 不能为空");
  137. $res = Db::name('headquarters')
  138. ->field(true)
  139. ->where(['code' => $code, 'is_del' => 0])
  140. ->findOrEmpty();
  141. if (empty($res)) return json_show(1004, '未找到相关汇总信息');
  142. switch ($res['type']) {
  143. case 1:
  144. $child = Db::name('business')
  145. ->field(true)
  146. ->where(['companyNo' => $code, 'is_del' => 0])
  147. ->findOrEmpty();
  148. break;
  149. case 2:
  150. $child = Db::name('customer_info')
  151. ->where(['companyNo' => $code, 'is_del' => 0])
  152. ->findOrEmpty();
  153. break;
  154. case 3:
  155. $child = Db::name('supplier')
  156. ->where(['code' => $code, 'is_del' => 0])
  157. ->findOrEmpty();
  158. break;
  159. }
  160. $res['child'] = $child;
  161. $res['relation_name'] =$res['relation_code']==""?'': Db::name('headquarters')->where('code',$res['relation_code'])
  162. ->value('name','');
  163. return json_show(0, '获取成功', $res);
  164. }
  165. //删除
  166. public function delete()
  167. {
  168. $param = $this->request->only(['ids', 'type', 'updater', 'updaterid'], 'post', 'trim');
  169. $val = Validate::rule([
  170. 'ids' => 'require|array|max:100',
  171. 'type|类别' => 'require|number|in:1,2,3',
  172. 'updater|操作人' => 'require|max:255',
  173. 'updaterid|操作人id' => 'require|number|gt:0',
  174. ]);
  175. if (!$val->check($param)) return json_show(1004, $val->getError());
  176. Db::startTrans();
  177. try {
  178. $date = date('Y-m-d H:i:s');
  179. $codes = [];
  180. switch ($param['type']) {
  181. case 1:
  182. $where = [['is_del', '=', 0], ['id', 'in', $param['ids']]];
  183. $codes = Db::name('business')->where($where)->column('companyNo');
  184. Db::name('business')->where($where)->update(['is_del' => 1, 'updatetime' => $date]);
  185. break;
  186. case 2:
  187. $where = [['is_del', '=', 0], ['id', 'in', $param['ids']]];
  188. $temp = Db::name('customer_info')->field('id,companyNo')->where($where)->where('status', 1)->findOrEmpty();
  189. if (!empty($temp)) throw new Exception($temp['companyNo'] . '启用状态,不允许删除');
  190. $codes = Db::name('customer_info')->where($where)->column('companyNo');
  191. Db::name('customer_info')
  192. ->where($where)
  193. ->update(['is_del' => 1, 'updatetime' => $date]);
  194. break;
  195. case 3:
  196. $where = [['is_del', '=', 0], ['id', 'in', $param['ids']]];
  197. $codes = Db::name('supplier')->where($where)->column('code');
  198. Db::name('supplier')->where($where)->update(['is_del' => 1, 'updatetime' => $date]);
  199. break;
  200. }
  201. Db::name('headquarters')
  202. ->where('is_del', 0)
  203. ->whereIn('code', $codes)
  204. ->update([
  205. 'is_del' => 1,
  206. 'updater' => $param['updater'],
  207. 'updaterid' => $param['updaterid'],
  208. 'updatetime' => date('Y-m-d H:i:s')
  209. ]);
  210. Db::commit();
  211. return json_show(0, '删除成功');
  212. } catch (Exception $exception) {
  213. Db::rollback();
  214. return json_show(1005, '删除失败,' . $exception->getMessage());
  215. }
  216. }
  217. //启禁用
  218. public function status()
  219. {
  220. $param = $this->request->only(['id', 'type', 'status', 'updater', 'updaterid'], 'post', 'trim');
  221. $val = Validate::rule([
  222. 'id' => 'require|number|gt:0',
  223. 'type|类别' => 'require|number|in:1,2,3',
  224. 'status|状态' => 'require|number|in:0,1',
  225. 'updater|操作人' => 'require|max:255',
  226. 'updaterid|操作人id' => 'require|number|gt:0',
  227. ]);
  228. if (!$val->check($param)) return json_show(1004, $val->getError());
  229. Db::startTrans();
  230. try {
  231. $date = date('Y-m-d H:i:s');
  232. $code = '';
  233. switch ($param['type']) {
  234. case 1:
  235. $tmp = Db::name('business')
  236. ->field('id,companyNo code,status')
  237. ->where(['id' => $param['id'], 'is_del' => 0])
  238. ->findOrEmpty();
  239. if (empty($tmp)) throw new Exception('未找到对应数据');
  240. if ($tmp['status'] == $param['status']) throw new Exception('操作重复');
  241. Db::name('business')
  242. ->where(['id' => $param['id'], 'is_del' => 0])
  243. ->where('status', '<>', $param['status'])
  244. ->update([
  245. 'status' => $param['status'],
  246. 'updatetime' => $date
  247. ]);
  248. break;
  249. case 2:
  250. $tmp = Db::name('customer_info')
  251. ->field('id,status,companyNo code')
  252. ->where(['is_del' => 0, 'id' => $param['id']])
  253. ->findOrEmpty();
  254. if (empty($tmp)) throw new Exception('未找到对应数据');
  255. if ($tmp['status'] == $param['status']) throw new Exception('操作重复');
  256. Db::name('customer_info')
  257. ->where(['id' => $param['id'], 'is_del' => 0])
  258. ->where('status', '<>', $param['status'])
  259. ->update([
  260. 'status' => $param['status'],
  261. 'updatetime' => $date
  262. ]);
  263. break;
  264. case 3:
  265. $tmp = Db::name('supplier')
  266. ->where(['id' => $param['id'], 'is_del' => 0])
  267. ->field('id,code,status')
  268. ->findOrEmpty();
  269. if (empty($tmp)) throw new Exception('该供应商不存在');
  270. if ($tmp['status'] == $param['status']) throw new Exception('操作重复');
  271. Db::name('supplier')
  272. ->where(['id' => $param['id'], 'is_del' => 0])
  273. ->where('status', '<>', $param['status'])
  274. ->update([
  275. 'status' => $param['status'],
  276. 'updatetime' => $date
  277. ]);
  278. break;
  279. }
  280. Db::name('headquarters')
  281. ->where(['code' => $tmp['code'], 'is_del' => 0])
  282. ->where('status', '<>', $param['status'])
  283. ->update([
  284. 'status' => $param['status'],
  285. 'updater' => $param['updater'],
  286. 'updaterid' => $param['updaterid'],
  287. 'updatetime' => $date
  288. ]);
  289. Db::commit();
  290. return json_show(0, '操作成功');
  291. } catch (Exception $exception) {
  292. Db::rollback();
  293. return json_show(1005, '操作失败,' . $exception->getMessage());
  294. }
  295. }
  296. //获取指定编码和名称
  297. public function getCodeAndName()
  298. {
  299. $companyNo = $this->request->filter('trim')->post('code');
  300. $res_1 = Db::name('headquarters')
  301. ->where('is_del', 0)
  302. ->whereIn('code', $companyNo)
  303. ->column('name', 'code');
  304. // $res_2 = Db::name('headquarters')
  305. // ->where('is_del', 0)
  306. // ->whereIn('relation_code', $companyNo)
  307. // ->column('name', 'relation_code');
  308. return json_show(0, '请求成功', $res_1);
  309. }
  310. //业务公司
  311. public function bGetList()
  312. {
  313. $param = $this->request->only([
  314. 'page' => 1,
  315. 'size' => 10,
  316. 'company' => '',
  317. 'status' => '',
  318. 'creater' => '',
  319. 'start' => '',
  320. 'end' => '',
  321. 'company_name' => '',
  322. ], 'post', 'trim');
  323. $where = [['a.is_del', "=", 0]];
  324. if ($param['company'] !== "") $where[] = ["a.company", "like", '%' . $param['company'] . '%'];
  325. if ($param['status'] !== "") $where[] = ["a.status", '=', $param['status']];
  326. if ($param['creater'] !== "") $where[] = ["a.creater", "like", '%' . $param['creater'] . '%'];
  327. if ($param['start'] !== "") $where[] = ["a.addtime", '>=', $param['start']];
  328. if ($param['end'] !== "") $where[] = ["a.addtime", '<=', $param['end'] . ' 23:59:59'];
  329. $count = Db::name('business')
  330. ->alias('a')
  331. ->where($where)
  332. ->count('a.id');
  333. $list = Db::name('business')
  334. ->alias('a')
  335. ->field(true)
  336. ->where($where)
  337. ->page($param['page'], $param['size'])
  338. ->order(['addtime' => 'desc', 'id' => 'desc'])
  339. ->select()
  340. ->toArray();
  341. foreach ($list as &$value) {
  342. $value['company_name'] = '';
  343. }
  344. return json_show("0", "获取成功", ['list' => $list, 'count' => $count]);
  345. }
  346. public function bCreate()
  347. {
  348. $param = $this->request->filter('trim')->post('');
  349. Db::startTrans();
  350. try {
  351. $tmp = Db::name('business')
  352. ->field('id')
  353. ->where(['company' => $param['company'], 'is_del' => 0])
  354. ->findOrEmpty();
  355. if (!empty($tmp)) throw new Exception('该公司名称已存在');
  356. Db::name('business')->insert($param);
  357. Db::name('headquarters')->insert([
  358. 'code' => $param['companyNo'],
  359. 'name' => $param['company'],
  360. 'type' => 1,
  361. 'invoice_title' => '',
  362. 'invoice_people' => '',
  363. 'invoice_addr' => $param['inv_addr'],
  364. 'invoice_mobile' => $param['mobile'],
  365. 'invoice_code' => $param['inv_code'],
  366. 'invoice_bank' => $param['inv_bank'],
  367. 'invoice_bankNo' => $param['inv_bankNo'],
  368. 'invoice_img' => $param['license_img'],
  369. 'remark' => '',
  370. 'status' => $param['status'],
  371. 'is_del' => $param['is_del'],
  372. 'creater' => $param['creater'],
  373. 'createrid' => $param['createrid'],
  374. 'addtime' => $param['addtime'],
  375. 'updater' => $param['creater'],
  376. 'updaterid' => $param['createrid'],
  377. 'updatetime' => $param['updatetime'],
  378. ]);
  379. Db::commit();
  380. $param['type']=1;
  381. Cache::store("redis")->handler()->lpush("companycopy",json_encode($param,JSON_UNESCAPED_UNICODE));
  382. return json_show(0, '添加成功');
  383. } catch (Exception $exception) {
  384. Db::rollback();
  385. return json_show(1003, $exception->getMessage());
  386. }
  387. }
  388. public function bInfo()
  389. {
  390. $companyNo = $this->request->post('companyNo', '', 'trim');
  391. if ($companyNo == "") return json_show(1004, "参数companyNo不能为空");
  392. $info = Db::name('business')
  393. ->alias('a')
  394. ->field('a.*,b.company_type')
  395. ->leftJoin('company_type b', 'b.id=a.type AND b.is_del=0')
  396. ->where(['a.companyNo' => $companyNo, 'a.is_del' => 0])
  397. ->findOrEmpty();
  398. if (empty($info)) return json_show(1002, "未找到数据");
  399. return json_show(0, '获取成功', $info);
  400. }
  401. public function bEdit()
  402. {
  403. $param = $this->request->filter('trim')->post('');
  404. Db::startTrans();
  405. try {
  406. $info = Db::name('business')
  407. ->field('id,companyNo')
  408. ->where(['id' => $param['id'], 'is_del' => 0])
  409. ->findOrEmpty();
  410. if (empty($info)) throw new Exception('未找到数据');
  411. Db::name('business')
  412. ->where(['id' => $param['id'], 'is_del' => 0])
  413. ->update(array_merge($param, ['updatetime' => date("Y-m-d H:i:s")]));
  414. Db::name('headquarters')
  415. ->where(['code' => $info['companyNo'], 'is_del' => 0])
  416. ->update([
  417. 'invoice_addr' => $param['inv_addr'],
  418. 'invoice_mobile' => $param['invoice_mobile'],
  419. 'invoice_bank' => $param['inv_bank'],
  420. 'invoice_bankNo' => $param['inv_bankNo'],
  421. 'invoice_title' => $param['invoice_title'],
  422. 'updatetime' => date("Y-m-d H:i:s"),
  423. ]);
  424. Db::commit();
  425. $param['type']=1;
  426. Cache::store("redis")->handler()->lpush("companycopy",json_encode($param,JSON_UNESCAPED_UNICODE));
  427. return json_show(0, '修改成功');
  428. } catch (Exception $exception) {
  429. Db::rollback();
  430. return json_show(1003, $exception->getMessage());
  431. }
  432. }
  433. public function bTitle()
  434. {
  435. $param = $this->request->only(['company_type' => '', 'status' => ''], 'post', 'trim');
  436. $rs = Db::name('company_type')->where('is_del', 0);
  437. if ($param['company_type'] != '') $rs->whereLike('company_type', '%' . $param['company_type'] . '%');
  438. if ($param['status'] != '') $rs->where('status', $param['status']);
  439. $list = $rs->select()->toArray();
  440. return json_show(0, '获取成功', $list);
  441. }
  442. //供应商
  443. public function sGetList()
  444. {
  445. $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');
  446. $where = [['s.is_del', "=", 0]];
  447. if ($param['name'] !== "") $where[] = ["s.name", "like", '%' . $param['name'] . '%'];
  448. if ($param['code'] !== "") $where[] = ["s.code", "like", '%' . $param['code'] . '%'];
  449. if ($param['more_code'] !== "") $where[] = ["s.code", "in", $param['more_code']];
  450. if ($param['creater'] !== "") $where[] = ["s.creater", "like", '%' . $param['creater'] . '%'];
  451. if ($param['person'] !== "") $where[] = ["s.person", "like", '%' . $param['person'] . '%'];
  452. if ($param['status'] !== "") $where[] = ["s.status", '=', $param['status']];
  453. if ($param['ocr_status'] !== "") $where[] = ["s.ocr_status", '=', $param['ocr_status']];
  454. if ($param['start'] !== "") $where[] = ["s.addtime", '>=', $param['start']];
  455. if ($param['end'] !== "") $where[] = ["s.addtime", '<=', $param['end'] . ' 23:59:59'];
  456. if ($param['is_platform'] !== "") $where[] = ["s.is_platform", '=', $param['is_platform']];
  457. // if ($param['company_name'] !== "") $where[] = ["s.createrid", 'in', $param['company_name']];
  458. $count = Db::name('supplier')
  459. ->alias('s')
  460. // ->leftJoin("depart_user u", "u.uid=s.createrid AND u.is_del=0")
  461. ->where($where)
  462. ->count('s.id');
  463. $list = Db::name('supplier')
  464. ->alias('s')
  465. ->field(true)
  466. // ->field('s.*,u.itemid')
  467. // ->leftJoin("depart_user u", "u.uid=s.createrid AND u.is_del=0")
  468. ->where($where)
  469. ->page($param['page'], $param['size'])
  470. ->order("addtime desc")
  471. ->select()
  472. ->toArray();
  473. // $all_codes = Db::name('supplier_contact')
  474. // ->whereIn('code', array_column($list, 'code'))
  475. // ->column("id,contactor,mobile", 'code');
  476. //获取开通账号的供应商
  477. // $account = checkHasAccountBySupplierNos(array_column($list,'code'));
  478. foreach ($list as &$value) {
  479. $value['company_name'] = '';
  480. }
  481. return json_show("0", "获取成功", ['list' => $list, 'count' => $count]);
  482. }
  483. public function sCreate()
  484. {
  485. $param = $this->request->only(['data', 'contact'], 'post', 'trim');
  486. $val = Validate::rule([
  487. 'data|供应商数据' => 'require|array',
  488. 'contact|联系人' => 'require|array',
  489. ]);
  490. if ($val->check($param) == false) return json_show(1004, $val->getError());
  491. Db::startTrans();
  492. try {
  493. $tmp = Db::name('supplier')
  494. ->field('id')
  495. ->where(['name' => $param['data']['name'], 'is_del' => 0])
  496. ->findOrEmpty();
  497. if (!empty($tmp)) throw new Exception('该供应商名称已存在');
  498. $id = Db::name('supplier')->insertGetId($param['data']);
  499. Db::name('supplier_contact')->insert($param['contact']);
  500. Db::name('headquarters')->insert([
  501. 'code' => $param['data']['code'],
  502. 'name' => $param['data']['name'],
  503. 'type' => 3,
  504. 'invoice_title' => '',
  505. 'invoice_people' => '',
  506. 'invoice_addr' => '',
  507. 'invoice_mobile' => $param['contact']['mobile'],
  508. 'invoice_code' => '',
  509. 'invoice_bank' => '',
  510. 'invoice_bankNo' => '',
  511. 'invoice_img' => $param['data']['license_img'],
  512. 'remark' => $param['data']['remark'],
  513. 'status' => $param['data']['status'],
  514. 'is_del' => $param['data']['is_del'],
  515. 'creater' => $param['data']['creater'],
  516. 'createrid' => $param['data']['createrid'],
  517. 'addtime' => $param['data']['addtime'],
  518. 'updater' => $param['data']['creater'],
  519. 'updaterid' => $param['data']['createrid'],
  520. 'updatetime' => $param['data']['updatetime'],
  521. ]);
  522. Db::commit();
  523. $param['data']['type']=3;
  524. Cache::store("redis")->handler()->lpush("companycopy",json_encode($param['data'],JSON_UNESCAPED_UNICODE));
  525. return json_show(0, '添加成功', ['id' => $id]);
  526. } catch (Exception $exception) {
  527. Db::rollback();
  528. return json_show(1003, $exception->getMessage());
  529. }
  530. }
  531. public function sInfo()
  532. {
  533. $code = $this->request->post('code', '', 'trim');
  534. if ($code == "") return json_show(1004, "参数code不能为空");
  535. $info = Db::name('supplier')
  536. ->alias('a')
  537. ->field('a.*,b.contactor,b.mobile,b.position,b.email,b.telephone')
  538. ->leftJoin('supplier_contact b', 'b.code=a.code AND b.is_del=0')
  539. ->where(['a.code' => $code, 'a.is_del' => 0])
  540. ->findOrEmpty();
  541. if (empty($info)) return json_show(1002, "未找到供应商数据");
  542. return json_show(0, '获取成功', $info);
  543. }
  544. public function sEdit()
  545. {
  546. $param = $this->request->only(['data', 'contact'], 'post', 'trim');
  547. $val = Validate::rule([
  548. 'data|供应商数据' => 'require|array',
  549. 'contact|联系人' => 'require|array',
  550. ]);
  551. if ($val->check($param) == false) return json_show(1004, $val->getError());
  552. Db::startTrans();
  553. try {
  554. $tmp = Db::name('supplier')
  555. ->field('id')
  556. ->where(['name' => $param['data']['name'], 'is_del' => 0])
  557. ->where('id', '<>', $param['data']['id'])
  558. ->findOrEmpty();
  559. if (!empty($tmp)) throw new Exception('该供应商名称已存在');
  560. $id = Db::name('supplier')
  561. ->where(['id' => $param['data']['id'], 'is_del' => 0])
  562. ->strict(false)
  563. ->update($param['data']);
  564. $tmp = Db::name('supplier_contact')
  565. ->field('id')
  566. ->where(['code' => $param['contact']['code'], 'is_del' => 0])
  567. ->findOrEmpty();
  568. if (empty($tmp)) Db::name('supplier_contact')->insert(array_merge($param['contact'], ['addtime' => $param['contact']['updatetime']]));
  569. else Db::name('supplier_contact')->where(['id' => $tmp['id'], 'is_del' => 0])->update($param['contact']);
  570. Db::name('headquarters')
  571. ->where(['code' => $param['contact']['code'], 'is_del' => 0])
  572. ->update([
  573. 'name' => $param['data']['name'],
  574. 'type' => 3,
  575. 'invoice_title' => '',
  576. 'invoice_people' => '',
  577. 'invoice_addr' => '',
  578. 'invoice_mobile' => $param['contact']['mobile'],
  579. 'invoice_code' => '',
  580. 'invoice_bank' => '',
  581. 'invoice_bankNo' => '',
  582. 'invoice_img' => $param['data']['license_img'],
  583. 'remark' => $param['data']['remark'],
  584. 'is_del' => $param['data']['is_del'],
  585. 'updater' => $param['data']['updater'],
  586. 'updaterid' => $param['data']['updaterid'],
  587. 'updatetime' => date("Y-m-d H:i:s"),
  588. ]);
  589. $this->checkSupplier($param['data']);
  590. Db::commit();
  591. $param['data']['type']=3;
  592. Cache::store("redis")->handler()->lpush("companycopy",json_encode($param['data'],JSON_UNESCAPED_UNICODE));
  593. return json_show(0, '修改成功', ['id' => $id]);
  594. } catch (Exception $exception) {
  595. Db::rollback();
  596. return json_show(1003, $exception->getMessage());
  597. }
  598. }
  599. //客户
  600. public function cInfo()
  601. {
  602. $companyNo = $this->request->post('companyNo', '', 'trim');
  603. $info = Db::name('customer_info')
  604. ->where(['is_del' => 0, 'companyNo' => $companyNo])
  605. ->field(true)
  606. ->findOrEmpty();
  607. $info['member'] = Db::name("customer_member")
  608. ->where(['companyNo' => $companyNo, "is_del" => 0])
  609. ->order('id')
  610. ->select()
  611. ->toArray();
  612. return json_show(0, "获取成功", $info);
  613. }
  614. public function cTitle()
  615. {
  616. $param = $this->request->only([
  617. 'page' => 1,
  618. 'size' => 10,
  619. 'companyNo' => '',
  620. 'companyName' => '',
  621. 'itemid' => '',
  622. ], 'post', 'trim');
  623. $where [] = ['is_del', "=", 0];
  624. if ($param['companyNo'] != '') $where[] = ['companyNo', 'like', '%' . $param['companyNo'] . '%'];
  625. if ($param['companyName'] != '') $where[] = ['companyName', 'like', '%' . $param['companyName'] . '%'];
  626. if ($param['itemid'] != '') $where[] = ['itemid', '=', $param['itemid']];
  627. $count = Db::name('customer_info')
  628. ->where($where)
  629. ->count('id');
  630. $item = Db::name('customer_info')
  631. ->where($where)
  632. ->field("id,companyNo,companyName,area,LENGTH(companyName) as weight,status")
  633. ->order(['weight' => 'asc', 'id' => 'desc'])
  634. ->page($param['page'], $param['size'])
  635. ->select()
  636. ->toArray();
  637. return json_show(0, "获取成功", ['item' => $item, 'count' => $count]);
  638. }
  639. public function cCreate()
  640. {
  641. $param = $this->request->only([
  642. 'companyName',
  643. 'parent' => 0,
  644. 'customer_member',
  645. 'uid',
  646. 'uname',
  647. 'branch',
  648. 'middle',
  649. 'area',
  650. ], 'post', 'trim');
  651. $val = Validate::rule([
  652. 'companyName|客户名称' => 'require',
  653. 'customer_member|联系方式' => 'require|array|max:100',
  654. 'uid' => 'require|number|gt:0',
  655. 'uname|创建人' => 'require',
  656. 'branch|省级' => 'require',
  657. 'middle|市级' => 'require',
  658. 'area|区域' => 'require',
  659. ]);
  660. if ($val->check($param) == false) return json_show(1004, $val->getError());
  661. $companyNo = makeNo("KH");
  662. $rename = Db::name('customer_org1')
  663. ->field('id')
  664. ->where(['is_del' => 0, 'id' => $param['parent']])
  665. ->findOrEmpty();
  666. $item = Db::name('customer_info')
  667. ->field('id')
  668. ->where(['itemid' => $rename['id'] ?? 0, 'companyName' => $param['companyName'], 'is_del' => 0])
  669. ->findOrEmpty();
  670. if (!empty($item)) return json_show(1002, "公司名称已存在");
  671. $createrid = $param['uid'];//isset($user["data"]['id']) ? $user["data"]['id'] : "";
  672. $creater = $param['uname'];//isset($user["data"]['nickname']) ? $user["data"]['nickname'] : "";
  673. Db::startTrans();
  674. try {
  675. $date = date('Y-m-d H:i:s');
  676. $data = [
  677. "companyNo" => $companyNo,
  678. "companyName" => $param['companyName'],
  679. "parent" => $param['parent'],
  680. "itemid" => $param['parent'],
  681. "area" =>$param['area'],
  682. "status" => 0,
  683. "branch" =>$param['branch'],
  684. "middle" =>$param['middle'],
  685. "is_del" => 0,
  686. "createrid" => $createrid,
  687. "creater" => $creater,
  688. "addtime" => $date,
  689. "updatetime" => $date,
  690. ];
  691. $datainfo = Db::name('customer_info')->insert($data);
  692. if ($datainfo) {
  693. $var = [];
  694. foreach ($param['customer_member'] as $value) {
  695. $var[] = [
  696. 'commobile' => $value['commobile'] ?? '',
  697. 'comtel' => '',
  698. 'contactor' => $value['contactor'] ?? '',
  699. 'position' => $value['position'] ?? '',
  700. 'wxaccount' => $value['wxaccount'] ?? '',
  701. 'qqaccount' => $value['qqaccount'] ?? '',
  702. 'email' => $value['email'] ?? '',
  703. 'comdepart' => $value['comdepart'] ?? '',
  704. 'status' => $value['status'] ?? '',
  705. 'createrid' => $createrid,
  706. 'creater' => $creater,
  707. 'companyNo' => $companyNo,
  708. 'is_del' => 0,
  709. 'addtime' => $date,
  710. 'updatetime' => $date,
  711. ];
  712. }
  713. $vp = Db::name('customer_member')->insertAll($var);
  714. if ($vp == "") throw new Exception('新建联系人失败');
  715. //汇总表
  716. Db::name('headquarters')
  717. ->insert([
  718. 'code' => $companyNo,
  719. 'name' => $param['companyName'],
  720. 'type' => 2,
  721. 'creater' => $param['uname'],
  722. 'createrid' => $param['uid'],
  723. "addtime" => $date,
  724. 'updater' => $param['uname'],
  725. 'updaterid' => $param['uid'],
  726. "updatetime" => $date,
  727. ]);
  728. Db::commit();
  729. $data['type']=2;
  730. Cache::store("redis")->handler()->lpush("companycopy",json_encode($data,JSON_UNESCAPED_UNICODE));
  731. return json_show(0, "新建成功");
  732. } else throw new Exception('新建失败');
  733. } catch (Exception $e) {
  734. Db::rollback();
  735. return json_show(1005, $e->getMessage());
  736. }
  737. }
  738. public function cList()
  739. {
  740. $param = $this->request->only(['page' => 1, 'size' => 10, 'companyName' => '', 'status' => '', 'creater' => '', 'start' => '', 'end' => ''], 'post', 'trim');
  741. $where = [["a.is_del", "=", 0]];
  742. if ($param['companyName'] != "") $where[] = ['b.companyName', "like", '%' . $param['companyName'] . '%'];
  743. if ($param['status'] !== "") $where[] = ['status', "=", $param['status']];
  744. if ($param['creater'] != "") $where[] = ['a.creater', "like", '%' . $param['creater'] . '%'];
  745. if ($param['start'] !== "") $where[] = ['a.addtime', ">=", date('Y-m-d H:i:s', strtotime($param['start']))];
  746. if ($param['end'] !== "") $where[] = ['a.addtime', "<", date('Y-m-d H:i:s', strtotime($param['end']) + 24 * 3600)];
  747. $count = Db::name('customer_member')
  748. ->alias('a')
  749. ->leftJoin('customer_info b', "b.companyNo=a.companyNo")
  750. ->where($where)
  751. ->count('a.id');
  752. $total = ceil($count / $param['size']);
  753. $page = $param['page'] >= $total ? $total : $param['page'];
  754. $list = Db::name('customer_member')
  755. ->alias('a')
  756. ->leftJoin('customer_info b', "b.companyNo=a.companyNo")
  757. ->where($where)
  758. ->page($page, $param['size'])
  759. ->order("addtime desc")
  760. ->field("a.*,b.companyName,b.parent,b.area")
  761. ->select()
  762. ->toArray();
  763. return json_show(0, "获取成功", ['list' => $list, 'count' => $count]);
  764. }
  765. public function cEdit()
  766. {
  767. $param = $this->request->only([
  768. 'id',
  769. 'companyName',
  770. 'parent',
  771. 'customer_member',
  772. 'uid',
  773. 'uname',
  774. 'branch',
  775. 'middle',
  776. 'area',
  777. ], 'post', 'trim');
  778. $val = Validate::rule([
  779. 'id' => 'require|number|gt:0',
  780. 'companyName|客户名称' => 'require',
  781. 'parent' => 'require',
  782. 'customer_member|联系方式' => 'require|array|max:100',
  783. 'uid' => 'require|number|gt:0',
  784. 'uname|修改人' => 'require',
  785. 'branch|省级'=> 'require',
  786. 'middle|市级'=> 'require',
  787. 'area|区域'=> 'require',
  788. ]);
  789. if ($val->check($param) == false) return json_show(1004, $val->getError());
  790. $idinfo = Db::name('customer_info')
  791. ->field('id,status,companyNo')
  792. ->where(['id' => $param['id'], 'is_del' => 0])
  793. ->findOrEmpty();
  794. if (empty($idinfo)) return json_show(1004, "未找到数据");
  795. if ($idinfo['status'] == 1) return json_show(1002, "状态是启用状态,无法编辑");
  796. $rename = Db::name('customer_org1')
  797. ->where(['is_del' => 0, 'id' => $param['parent']])
  798. ->field('id')
  799. ->findOrEmpty();
  800. $item = Db::name('customer_info')
  801. ->field('id')
  802. ->where(['itemid' => $rename['id'] ?? 0, 'companyName' => $param['companyName'], 'is_del' => 0])
  803. ->where('id', '<>', $param['id'])
  804. ->findOrEmpty();
  805. if (!empty($item)) return json_show(1002, "公司名称已存在");
  806. $createrid = $param['uid'];//isset($user["data"]['id']) ? $user["data"]['id'] : "";
  807. $creater = $param['uname'];//isset($user["data"]['nickname']) ? $user["data"]['nickname'] : "";
  808. Db::startTrans();
  809. try {
  810. $date = date('Y-m-d H:i:s');
  811. $data = [
  812. "id" => $param['id'],
  813. "companyName" => $param['companyName'],
  814. "area" =>$param['area'],
  815. "branch" =>$param['branch'],
  816. "middle" =>$param['middle'],
  817. "parent" => $param['parent'],
  818. "updatetime" => $date,
  819. ];
  820. $datainfo = Db::name('customer_info')->save($data);
  821. if ($datainfo) {
  822. foreach ($param['customer_member'] as $value) {
  823. $item = [];
  824. $item['commobile'] = isset($value['commobile']) ? $value['commobile'] : "";
  825. $item['comtel'] = "";
  826. isset($value['id']) && $value['id'] !== "" ? $item['id'] = $value['id'] : '';
  827. $item['contactor'] = isset($value['contactor']) ? $value['contactor'] : "";
  828. $item['position'] = isset($value['position']) ? $value['position'] : "";
  829. $item['wxaccount'] = isset($value['wxaccount']) ? $value['wxaccount'] : "";
  830. $item['qqaccount'] = isset($value['qqaccount']) ? $value['qqaccount'] : "";
  831. $item['email'] = isset($value['email']) ? $value['email'] : "";
  832. $item['comdepart'] = isset($value['comdepart']) ? $value['comdepart'] : "";
  833. $item['status'] = $value['status'];
  834. $item['createrid'] = $createrid;
  835. $item['creater'] = $creater;
  836. $item['companyNo'] = isset($idinfo['companyNo']) ? $idinfo['companyNo'] : "";
  837. $item['is_del'] = 0;
  838. $item['addtime'] = $date;
  839. $item['updatetime'] = $date;
  840. $vp = Db::name('customer_member')->save($item);
  841. if ($vp == false) throw new Exception('更新失败');
  842. }
  843. //更新汇总表
  844. Db::name('headquarters')
  845. ->where(['is_del' => 0, 'type' => 2, 'code' => $idinfo['companyNo']])
  846. ->update([
  847. 'name' => $param['companyName'],
  848. 'updater' => $param['uname'],
  849. 'updaterid' => $param['uid'],
  850. "updatetime" => $date,
  851. ]);
  852. Db::commit();
  853. return json_show(0, "更新成功");
  854. } else throw new Exception("更新失败");
  855. } catch (Exception $e) {
  856. Db::rollback();
  857. return json_show(1005, $e->getMessage());
  858. }
  859. }
  860. //供应商升级成业务公司
  861. public function supplerUpgrade()
  862. {
  863. $post = $this->request->only(['code', 'inv_bank', 'inv_bankNo', 'inv_addr', 'invoice_mobile', 'uid', 'uname', 'invoice_title'], 'post', 'trim');
  864. $val = Validate::rule([
  865. 'code|供应商编码' => 'require',
  866. 'inv_bank|银行名称' => 'require|max:255',
  867. 'inv_bankNo|银行卡号' => 'require|number',
  868. 'inv_addr|联系地址' => 'require|max:255',
  869. 'invoice_mobile' => 'require',
  870. 'invoice_title|公司抬头' => 'require',
  871. ]);
  872. if ($val->check($post) == false) return json_show(1004, $val->getError());
  873. $res = Db::name('supplier')
  874. ->alias('a')
  875. ->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')
  876. ->leftJoin('supplier_contact b', 'b.code=a.code')
  877. ->where(['a.code' => $post['code'], 'a.is_del' => 0])
  878. ->findOrEmpty();
  879. if (empty($res)) return json_show(1004, '该供应商不存在');
  880. if ($res['status'] != 1) return json_show(1004, '该供应商已禁用');
  881. if ($res['is_upgrade'] == 1) return json_show(1004, '不能重复升级');
  882. $temp = Db::name('company_type')
  883. ->field('id')
  884. ->where(['company_type' => $res['nature'], 'is_del' => 0])
  885. ->findOrEmpty();
  886. // if (empty($temp)) return json_show(1004, '对应公司类型不存在');
  887. Db::startTrans();
  888. try {
  889. $date = date('Y-m-d H:i:s');
  890. $business_code = makeNo('GS');
  891. //业务公司列表
  892. Db::name('business')
  893. ->insert([
  894. 'company' => $res['name'],
  895. 'companyNo' => $business_code,
  896. 'inv_code' => $res['registercode'],
  897. 'company_type' => $res['nature'],
  898. 'type' => $temp['id'] ?? 0,
  899. 'creater' => $post['uname'],
  900. 'createrid' => $post['uid'],
  901. 'inv_legaler' => $res['legaler'],
  902. 'inv_time' => $res['registertime'],
  903. 'inv_addr' => $post['inv_addr'],
  904. 'inv_bank' => $post['inv_bank'],
  905. 'inv_bankNo' => $post['inv_bankNo'],
  906. 'contactor' => $res['contactor'],
  907. 'mobile' => $res['mobile'],
  908. 'addr' => $res['addr'],
  909. 'inv_scope' => $res['scope'],
  910. 'license_img' => $res['license_img'],
  911. 'invoice_title' => $post['invoice_title'],
  912. 'invoice_mobile' => $post['invoice_mobile'],
  913. 'status' => 1,
  914. 'is_del' => 0,
  915. 'addtime' => $date,
  916. 'updatetime' => $date,
  917. ]);
  918. $temp_supplier = Db::name('headquarters')
  919. ->where(['code' => $post['code'], 'type' => 3])
  920. ->findOrEmpty();
  921. //汇总表
  922. Db::name('headquarters')
  923. ->where(['code' => $post['code'], 'type' => 3])
  924. ->update([
  925. 'relation_code' => $business_code,
  926. 'updater' => $post['uname'],
  927. 'updaterid' => $post['uid'],
  928. 'updatetime' => $date,
  929. ]);
  930. Db::name('headquarters')
  931. ->insert(array_merge($temp_supplier, [
  932. 'id' => null,
  933. 'code' => $business_code,
  934. 'type' => 1,//业务公司
  935. 'relation_code' => $post['code']
  936. ]));
  937. //供应商
  938. Db::name('supplier')
  939. ->where(['code' => $post['code'], 'is_upgrade' => 0, 'is_del' => 0])
  940. ->update(['is_upgrade' => 1, 'updatetime' => $date]);
  941. //账号与公司的关联关系
  942. Db::name('account_company')
  943. ->insert([
  944. 'account_id' => $post['uid'],
  945. 'companyCode' => $business_code,
  946. 'companyName' => $res['name'],
  947. 'company_type' => 2,
  948. 'status' => 1,
  949. 'is_del' => 0,
  950. 'addtime' => $date,
  951. 'updatetime' => $date,
  952. ]);
  953. Db::commit();
  954. return json_show(0, '升级成功');
  955. } catch (Exception $exception) {
  956. Db::rollback();
  957. return json_show(1004, '升级失败,' . $exception->getMessage());
  958. }
  959. }
  960. //获取业务公司列表(为了采销临时脚本同步数据到结算平台使用,正式上线要删除)
  961. public function getBusinessListTmp()
  962. {
  963. $where = $this->request->filter('trim')->post('where', []);
  964. $list = Db::name('business')
  965. ->alias('a')
  966. ->field('a.*,b.code as supplierNo')
  967. ->leftJoin('headquarters b', 'b.relation_code=a.companyNo')
  968. ->where($where)
  969. ->select()
  970. ->toArray();
  971. return json_show(0, '获取成功', $list);
  972. }
  973. private function checkSupplier($supplier){
  974. $hquest = Db::name("headquarters")->where(["code"=>$supplier["code"],"is_del"=>0])->findOrEmpty();
  975. if(empty($hquest)) throw new Exception("未找到供应商关联数据");
  976. if($hquest['relation_code']!='' && $hquest['type']==3){
  977. $comp = Db::name('headquarters')->where(['code'=>$hquest['relation_code'],'is_del'=>0])->findOrEmpty();
  978. if(empty($comp)) throw new Exception('未找到供应商关联业务公司数据');
  979. $business = Db::name("business")->where(['companyNo'=>$hquest['relation_code'],'is_del'=>0])->findOrEmpty();
  980. if(empty($business)) throw new Exception('未找到供应商关联业务公司数据');
  981. $comp['name'] = $supplier['name'];
  982. $comp['invoice_title'] = $supplier['name'];
  983. $comp['invoice_people'] = $supplier['legaler'];
  984. $comp['invoice_addr'] = $supplier['addr'];
  985. $comp['invoice_code'] = $supplier['registercode'];
  986. $comp['invoice_img'] = $supplier['license_img'];
  987. $comp['updatetime'] = date("Y-m-d H:i:s");
  988. $ip = Db::name('headquarters')->save($comp);
  989. if($ip==false) throw new Exception('供应商关联业务公司数据更新失败');
  990. $business['company'] =$supplier['name'];
  991. $business['inv_code'] =$supplier['registercode'];
  992. // $business['invoice_title'] =$supplier['name'];
  993. $business['inv_legaler'] =$supplier['legaler'];
  994. $business['inv_time'] =$supplier['registertime'];
  995. $business['addr'] =$supplier['addr'];
  996. $business['inv_scope'] =$supplier['scope'];
  997. $business['license_img'] =$supplier['license_img'];
  998. $business['creater'] =$supplier['updater'];
  999. $business['createrid'] =$supplier['updaterid'];
  1000. $business['updatetime'] = date('Y-m-d H:i:s');
  1001. $bp = Db::name('business')->save($business);
  1002. if($bp==false) throw new Exception('业务公司数据更新失败');
  1003. $business['type']=1;
  1004. Cache::store('redis')->handler()->lpush('companycopy',json_encode($business,JSON_UNESCAPED_UNICODE));
  1005. }
  1006. }
  1007. }