Headquarters.php 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131
  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' => $param['data']['updatetime'],
  588. ]);
  589. Db::commit();
  590. $param['data']['type']=3;
  591. Cache::store("redis")->handler()->lpush("companycopy",json_encode($param['data'],JSON_UNESCAPED_UNICODE));
  592. return json_show(0, '修改成功', ['id' => $id]);
  593. } catch (Exception $exception) {
  594. Db::rollback();
  595. return json_show(1003, $exception->getMessage());
  596. }
  597. }
  598. //客户
  599. public function cInfo()
  600. {
  601. $companyNo = $this->request->post('companyNo', '', 'trim');
  602. $info = Db::name('customer_info')
  603. ->where(['is_del' => 0, 'companyNo' => $companyNo])
  604. ->field(true)
  605. ->findOrEmpty();
  606. $info['member'] = Db::name("customer_member")
  607. ->where(['companyNo' => $companyNo, "is_del" => 0])
  608. ->order('id')
  609. ->select()
  610. ->toArray();
  611. return json_show(0, "获取成功", $info);
  612. }
  613. public function cTitle()
  614. {
  615. $param = $this->request->only([
  616. 'page' => 1,
  617. 'size' => 10,
  618. 'companyNo' => '',
  619. 'companyName' => '',
  620. 'itemid' => '',
  621. ], 'post', 'trim');
  622. $where [] = ['is_del', "=", 0];
  623. if ($param['companyNo'] != '') $where[] = ['companyNo', 'like', '%' . $param['companyNo'] . '%'];
  624. if ($param['companyName'] != '') $where[] = ['companyName', 'like', '%' . $param['companyName'] . '%'];
  625. if ($param['itemid'] != '') $where[] = ['itemid', '=', $param['itemid']];
  626. $count = Db::name('customer_info')
  627. ->where($where)
  628. ->count('id');
  629. $item = Db::name('customer_info')
  630. ->where($where)
  631. ->field("id,companyNo,companyName,area,LENGTH(companyName) as weight")
  632. ->order(['weight' => 'asc', 'id' => 'desc'])
  633. ->page($param['page'], $param['size'])
  634. ->select()
  635. ->toArray();
  636. return json_show(0, "获取成功", ['item' => $item, 'count' => $count]);
  637. }
  638. public function cCreate()
  639. {
  640. $param = $this->request->only([
  641. 'companyName',
  642. 'parent' => 0,
  643. 'customer_member',
  644. 'uid',
  645. 'uname',
  646. 'branch',
  647. 'middle',
  648. 'area',
  649. ], 'post', 'trim');
  650. $val = Validate::rule([
  651. 'companyName|客户名称' => 'require',
  652. 'customer_member|联系方式' => 'require|array|max:100',
  653. 'uid' => 'require|number|gt:0',
  654. 'uname|创建人' => 'require',
  655. 'branch|省级' => 'require',
  656. 'middle|市级' => 'require',
  657. 'area|区域' => 'require',
  658. ]);
  659. if ($val->check($param) == false) return json_show(1004, $val->getError());
  660. $companyNo = makeNo("KH");
  661. $rename = Db::name('customer_org1')
  662. ->field('id')
  663. ->where(['is_del' => 0, 'id' => $param['parent']])
  664. ->findOrEmpty();
  665. $item = Db::name('customer_info')
  666. ->field('id')
  667. ->where(['itemid' => $rename['id'] ?? 0, 'companyName' => $param['companyName'], 'is_del' => 0])
  668. ->findOrEmpty();
  669. if (!empty($item)) return json_show(1002, "公司名称已存在");
  670. $createrid = $param['uid'];//isset($user["data"]['id']) ? $user["data"]['id'] : "";
  671. $creater = $param['uname'];//isset($user["data"]['nickname']) ? $user["data"]['nickname'] : "";
  672. Db::startTrans();
  673. try {
  674. $date = date('Y-m-d H:i:s');
  675. $data = [
  676. "companyNo" => $companyNo,
  677. "companyName" => $param['companyName'],
  678. "parent" => $param['parent'],
  679. "itemid" => $param['parent'],
  680. "area" =>$param['area'],
  681. "status" => 0,
  682. "branch" =>$param['branch'],
  683. "middle" =>$param['middle'],
  684. "is_del" => 0,
  685. "createrid" => $createrid,
  686. "creater" => $creater,
  687. "addtime" => $date,
  688. "updatetime" => $date,
  689. ];
  690. $datainfo = Db::name('customer_info')->insert($data);
  691. if ($datainfo) {
  692. $var = [];
  693. foreach ($param['customer_member'] as $value) {
  694. $var[] = [
  695. 'commobile' => $value['commobile'] ?? '',
  696. 'comtel' => '',
  697. 'contactor' => $value['contactor'] ?? '',
  698. 'position' => $value['position'] ?? '',
  699. 'wxaccount' => $value['wxaccount'] ?? '',
  700. 'qqaccount' => $value['qqaccount'] ?? '',
  701. 'email' => $value['email'] ?? '',
  702. 'comdepart' => $value['comdepart'] ?? '',
  703. 'status' => $value['status'] ?? '',
  704. 'createrid' => $createrid,
  705. 'creater' => $creater,
  706. 'companyNo' => $companyNo,
  707. 'is_del' => 0,
  708. 'addtime' => $date,
  709. 'updatetime' => $date,
  710. ];
  711. }
  712. $vp = Db::name('customer_member')->insertAll($var);
  713. if ($vp == "") throw new Exception('新建联系人失败');
  714. //汇总表
  715. Db::name('headquarters')
  716. ->insert([
  717. 'code' => $companyNo,
  718. 'name' => $param['companyName'],
  719. 'type' => 2,
  720. 'creater' => $param['uname'],
  721. 'createrid' => $param['uid'],
  722. "addtime" => $date,
  723. 'updater' => $param['uname'],
  724. 'updaterid' => $param['uid'],
  725. "updatetime" => $date,
  726. ]);
  727. Db::commit();
  728. $data['type']=2;
  729. Cache::store("redis")->handler()->lpush("companycopy",json_encode($data,JSON_UNESCAPED_UNICODE));
  730. return json_show(0, "新建成功");
  731. } else throw new Exception('新建失败');
  732. } catch (Exception $e) {
  733. Db::rollback();
  734. return json_show(1005, $e->getMessage());
  735. }
  736. }
  737. public function cList()
  738. {
  739. $param = $this->request->only(['page' => 1, 'size' => 10, 'companyName' => '', 'status' => '', 'creater' => '', 'start' => '', 'end' => ''], 'post', 'trim');
  740. $where = [["a.is_del", "=", 0]];
  741. if ($param['companyName'] != "") $where[] = ['b.companyName', "like", '%' . $param['companyName'] . '%'];
  742. if ($param['status'] !== "") $where[] = ['status', "=", $param['status']];
  743. if ($param['creater'] != "") $where[] = ['a.creater', "like", '%' . $param['creater'] . '%'];
  744. if ($param['start'] !== "") $where[] = ['a.addtime', ">=", date('Y-m-d H:i:s', strtotime($param['start']))];
  745. if ($param['end'] !== "") $where[] = ['a.addtime', "<", date('Y-m-d H:i:s', strtotime($param['end']) + 24 * 3600)];
  746. $count = Db::name('customer_member')
  747. ->alias('a')
  748. ->leftJoin('customer_info b', "b.companyNo=a.companyNo")
  749. ->where($where)
  750. ->count('a.id');
  751. $total = ceil($count / $param['size']);
  752. $page = $param['page'] >= $total ? $total : $param['page'];
  753. $list = Db::name('customer_member')
  754. ->alias('a')
  755. ->leftJoin('customer_info b', "b.companyNo=a.companyNo")
  756. ->where($where)
  757. ->page($page, $param['size'])
  758. ->order("addtime desc")
  759. ->field("a.*,b.companyName,b.parent,b.area")
  760. ->select()
  761. ->toArray();
  762. return json_show(0, "获取成功", ['list' => $list, 'count' => $count]);
  763. }
  764. public function cEdit()
  765. {
  766. $param = $this->request->only([
  767. 'id',
  768. 'companyName',
  769. 'parent',
  770. 'customer_member',
  771. 'uid',
  772. 'uname',
  773. 'branch',
  774. 'middle',
  775. 'area',
  776. ], 'post', 'trim');
  777. $val = Validate::rule([
  778. 'id' => 'require|number|gt:0',
  779. 'companyName|客户名称' => 'require',
  780. 'parent' => 'require',
  781. 'customer_member|联系方式' => 'require|array|max:100',
  782. 'uid' => 'require|number|gt:0',
  783. 'uname|修改人' => 'require',
  784. 'branch|省级'=> 'require',
  785. 'middle|市级'=> 'require',
  786. 'area|区域'=> 'require',
  787. ]);
  788. if ($val->check($param) == false) return json_show(1004, $val->getError());
  789. $idinfo = Db::name('customer_info')
  790. ->field('id,status,companyNo')
  791. ->where(['id' => $param['id'], 'is_del' => 0])
  792. ->findOrEmpty();
  793. if (empty($idinfo)) return json_show(1004, "未找到数据");
  794. if ($idinfo['status'] == 1) return json_show(1002, "状态是启用状态,无法编辑");
  795. $rename = Db::name('customer_org1')
  796. ->where(['is_del' => 0, 'id' => $param['parent']])
  797. ->field('id')
  798. ->findOrEmpty();
  799. $item = Db::name('customer_info')
  800. ->field('id')
  801. ->where(['itemid' => $rename['id'] ?? 0, 'companyName' => $param['companyName'], 'is_del' => 0])
  802. ->where('id', '<>', $param['id'])
  803. ->findOrEmpty();
  804. if (!empty($item)) return json_show(1002, "公司名称已存在");
  805. $createrid = $param['uid'];//isset($user["data"]['id']) ? $user["data"]['id'] : "";
  806. $creater = $param['uname'];//isset($user["data"]['nickname']) ? $user["data"]['nickname'] : "";
  807. Db::startTrans();
  808. try {
  809. $date = date('Y-m-d H:i:s');
  810. $data = [
  811. "id" => $param['id'],
  812. "companyName" => $param['companyName'],
  813. "area" =>$param['area'],
  814. "branch" =>$param['branch'],
  815. "middle" =>$param['middle'],
  816. "parent" => $param['parent'],
  817. "updatetime" => $date,
  818. ];
  819. $datainfo = Db::name('customer_info')->save($data);
  820. if ($datainfo) {
  821. foreach ($param['customer_member'] as $value) {
  822. $item = [];
  823. $item['commobile'] = isset($value['commobile']) ? $value['commobile'] : "";
  824. $item['comtel'] = "";
  825. isset($value['id']) && $value['id'] !== "" ? $item['id'] = $value['id'] : '';
  826. $item['contactor'] = isset($value['contactor']) ? $value['contactor'] : "";
  827. $item['position'] = isset($value['position']) ? $value['position'] : "";
  828. $item['wxaccount'] = isset($value['wxaccount']) ? $value['wxaccount'] : "";
  829. $item['qqaccount'] = isset($value['qqaccount']) ? $value['qqaccount'] : "";
  830. $item['email'] = isset($value['email']) ? $value['email'] : "";
  831. $item['comdepart'] = isset($value['comdepart']) ? $value['comdepart'] : "";
  832. $item['status'] = $value['status'];
  833. $item['createrid'] = $createrid;
  834. $item['creater'] = $creater;
  835. $item['companyNo'] = isset($idinfo['companyNo']) ? $idinfo['companyNo'] : "";
  836. $item['is_del'] = 0;
  837. $item['addtime'] = $date;
  838. $item['updatetime'] = $date;
  839. $vp = Db::name('customer_member')->save($item);
  840. if ($vp == false) throw new Exception('更新失败');
  841. }
  842. //更新汇总表
  843. Db::name('headquarters')
  844. ->where(['is_del' => 0, 'type' => 2, 'code' => $idinfo['companyNo']])
  845. ->update([
  846. 'name' => $param['companyName'],
  847. 'updater' => $param['uname'],
  848. 'updaterid' => $param['uid'],
  849. "updatetime" => $date,
  850. ]);
  851. Db::commit();
  852. return json_show(0, "更新成功");
  853. } else throw new Exception("更新失败");
  854. } catch (Exception $e) {
  855. Db::rollback();
  856. return json_show(1005, $e->getMessage());
  857. }
  858. }
  859. //供应商升级成业务公司
  860. public function supplerUpgrade()
  861. {
  862. $post = $this->request->only(['code', 'inv_bank', 'inv_bankNo', 'inv_addr', 'invoice_mobile', 'uid', 'uname', 'invoice_title'], 'post', 'trim');
  863. $val = Validate::rule([
  864. 'code|供应商编码' => 'require',
  865. 'inv_bank|银行名称' => 'require|max:255',
  866. 'inv_bankNo|银行卡号' => 'require|number',
  867. 'inv_addr|联系地址' => 'require|max:255',
  868. 'invoice_mobile' => 'require',
  869. 'invoice_title|公司抬头' => 'require',
  870. ]);
  871. if ($val->check($post) == false) return json_show(1004, $val->getError());
  872. $res = Db::name('supplier')
  873. ->alias('a')
  874. ->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')
  875. ->leftJoin('supplier_contact b', 'b.code=a.code')
  876. ->where(['a.code' => $post['code'], 'a.is_del' => 0])
  877. ->findOrEmpty();
  878. if (empty($res)) return json_show(1004, '该供应商不存在');
  879. if ($res['status'] != 1) return json_show(1004, '该供应商已禁用');
  880. if ($res['is_upgrade'] == 1) return json_show(1004, '不能重复升级');
  881. $temp = Db::name('company_type')
  882. ->field('id')
  883. ->where(['company_type' => $res['nature'], 'is_del' => 0])
  884. ->findOrEmpty();
  885. // if (empty($temp)) return json_show(1004, '对应公司类型不存在');
  886. Db::startTrans();
  887. try {
  888. $date = date('Y-m-d H:i:s');
  889. $business_code = makeNo('GS');
  890. //业务公司列表
  891. Db::name('business')
  892. ->insert([
  893. 'company' => $res['name'],
  894. 'companyNo' => $business_code,
  895. 'inv_code' => $res['registercode'],
  896. 'company_type' => $res['nature'],
  897. 'type' => $temp['id'] ?? 0,
  898. 'creater' => $post['uname'],
  899. 'createrid' => $post['uid'],
  900. 'inv_legaler' => $res['legaler'],
  901. 'inv_time' => $res['registertime'],
  902. 'inv_addr' => $post['inv_addr'],
  903. 'inv_bank' => $post['inv_bank'],
  904. 'inv_bankNo' => $post['inv_bankNo'],
  905. 'contactor' => $res['contactor'],
  906. 'mobile' => $res['mobile'],
  907. 'addr' => $res['addr'],
  908. 'inv_scope' => $res['scope'],
  909. 'license_img' => $res['license_img'],
  910. 'invoice_title' => $post['invoice_title'],
  911. 'invoice_mobile' => $post['invoice_mobile'],
  912. 'status' => 1,
  913. 'is_del' => 0,
  914. 'addtime' => $date,
  915. 'updatetime' => $date,
  916. ]);
  917. $temp_supplier = Db::name('headquarters')
  918. ->where(['code' => $post['code'], 'type' => 3])
  919. ->findOrEmpty();
  920. //汇总表
  921. Db::name('headquarters')
  922. ->where(['code' => $post['code'], 'type' => 3])
  923. ->update([
  924. 'relation_code' => $business_code,
  925. 'updater' => $post['uname'],
  926. 'updaterid' => $post['uid'],
  927. 'updatetime' => $date,
  928. ]);
  929. Db::name('headquarters')
  930. ->insert(array_merge($temp_supplier, [
  931. 'id' => null,
  932. 'code' => $business_code,
  933. 'type' => 1,//业务公司
  934. 'relation_code' => $post['code']
  935. ]));
  936. //供应商
  937. Db::name('supplier')
  938. ->where(['code' => $post['code'], 'is_upgrade' => 0, 'is_del' => 0])
  939. ->update(['is_upgrade' => 1, 'updatetime' => $date]);
  940. //账号与公司的关联关系
  941. Db::name('account_company')
  942. ->insert([
  943. 'account_id' => $post['uid'],
  944. 'companyCode' => $business_code,
  945. 'companyName' => $res['name'],
  946. 'company_type' => 2,
  947. 'status' => 1,
  948. 'is_del' => 0,
  949. 'addtime' => $date,
  950. 'updatetime' => $date,
  951. ]);
  952. Db::commit();
  953. return json_show(0, '升级成功');
  954. } catch (Exception $exception) {
  955. Db::rollback();
  956. return json_show(1004, '升级失败,' . $exception->getMessage());
  957. }
  958. }
  959. //获取业务公司列表(为了采销临时脚本同步数据到结算平台使用,正式上线要删除)
  960. public function getBusinessListTmp()
  961. {
  962. $where = $this->request->filter('trim')->post('where', []);
  963. $list = Db::name('business')
  964. ->alias('a')
  965. ->field('a.*,b.code as supplierNo')
  966. ->leftJoin('headquarters b', 'b.relation_code=a.companyNo')
  967. ->where($where)
  968. ->select()
  969. ->toArray();
  970. return json_show(0, '获取成功', $list);
  971. }
  972. }