Headquarters.php 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166
  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(array_merge($info,$param),
  427. JSON_UNESCAPED_UNICODE));
  428. return json_show(0, '修改成功');
  429. } catch (Exception $exception) {
  430. Db::rollback();
  431. return json_show(1003, $exception->getMessage());
  432. }
  433. }
  434. public function bTitle()
  435. {
  436. $param = $this->request->only(['company_type' => '', 'status' => ''], 'post', 'trim');
  437. $rs = Db::name('company_type')->where('is_del', 0);
  438. if ($param['company_type'] != '') $rs->whereLike('company_type', '%' . $param['company_type'] . '%');
  439. if ($param['status'] != '') $rs->where('status', $param['status']);
  440. $list = $rs->select()->toArray();
  441. return json_show(0, '获取成功', $list);
  442. }
  443. //供应商
  444. public function sGetList()
  445. {
  446. $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');
  447. $where = [['s.is_del', "=", 0]];
  448. if ($param['name'] !== "") $where[] = ["s.name", "like", '%' . $param['name'] . '%'];
  449. if ($param['code'] !== "") $where[] = ["s.code", "like", '%' . $param['code'] . '%'];
  450. if ($param['more_code'] !== "") $where[] = ["s.code", "in", $param['more_code']];
  451. if ($param['creater'] !== "") $where[] = ["s.creater", "like", '%' . $param['creater'] . '%'];
  452. if ($param['person'] !== "") $where[] = ["s.person", "like", '%' . $param['person'] . '%'];
  453. if ($param['status'] !== "") $where[] = ["s.status", '=', $param['status']];
  454. if ($param['ocr_status'] !== "") $where[] = ["s.ocr_status", '=', $param['ocr_status']];
  455. if ($param['start'] !== "") $where[] = ["s.addtime", '>=', $param['start']];
  456. if ($param['end'] !== "") $where[] = ["s.addtime", '<=', $param['end'] . ' 23:59:59'];
  457. if ($param['is_platform'] !== "") $where[] = ["s.is_platform", '=', $param['is_platform']];
  458. // if ($param['company_name'] !== "") $where[] = ["s.createrid", 'in', $param['company_name']];
  459. $count = Db::name('supplier')
  460. ->alias('s')
  461. // ->leftJoin("depart_user u", "u.uid=s.createrid AND u.is_del=0")
  462. ->where($where)
  463. ->count('s.id');
  464. $list = Db::name('supplier')
  465. ->alias('s')
  466. ->field(true)
  467. // ->field('s.*,u.itemid')
  468. // ->leftJoin("depart_user u", "u.uid=s.createrid AND u.is_del=0")
  469. ->where($where)
  470. ->page($param['page'], $param['size'])
  471. ->order("addtime desc")
  472. ->select()
  473. ->toArray();
  474. // $all_codes = Db::name('supplier_contact')
  475. // ->whereIn('code', array_column($list, 'code'))
  476. // ->column("id,contactor,mobile", 'code');
  477. //获取开通账号的供应商
  478. // $account = checkHasAccountBySupplierNos(array_column($list,'code'));
  479. foreach ($list as &$value) {
  480. $value['company_name'] = '';
  481. }
  482. return json_show("0", "获取成功", ['list' => $list, 'count' => $count]);
  483. }
  484. public function sCreate()
  485. {
  486. $param = $this->request->only(['data', 'contact'], 'post', 'trim');
  487. $val = Validate::rule([
  488. 'data|供应商数据' => 'require|array',
  489. 'contact|联系人' => 'require|array',
  490. ]);
  491. if ($val->check($param) == false) return json_show(1004, $val->getError());
  492. Db::startTrans();
  493. try {
  494. $tmp = Db::name('supplier')
  495. ->field('id')
  496. ->where(['name' => $param['data']['name'], 'is_del' => 0])
  497. ->findOrEmpty();
  498. if (!empty($tmp)) throw new Exception('该供应商名称已存在');
  499. $id = Db::name('supplier')->insertGetId($param['data']);
  500. Db::name('supplier_contact')->insert($param['contact']);
  501. Db::name('headquarters')->insert([
  502. 'code' => $param['data']['code'],
  503. 'name' => $param['data']['name'],
  504. 'type' => 3,
  505. 'invoice_title' => '',
  506. 'invoice_people' => '',
  507. 'invoice_addr' => '',
  508. 'invoice_mobile' => $param['contact']['mobile'],
  509. 'invoice_code' => '',
  510. 'invoice_bank' => '',
  511. 'invoice_bankNo' => '',
  512. 'invoice_img' => $param['data']['license_img'],
  513. 'remark' => $param['data']['remark'],
  514. 'status' => $param['data']['status'],
  515. 'is_del' => $param['data']['is_del'],
  516. 'creater' => $param['data']['creater'],
  517. 'createrid' => $param['data']['createrid'],
  518. 'addtime' => $param['data']['addtime'],
  519. 'updater' => $param['data']['creater'],
  520. 'updaterid' => $param['data']['createrid'],
  521. 'updatetime' => $param['data']['updatetime'],
  522. ]);
  523. Db::commit();
  524. $param['data']['type']=3;
  525. Cache::store("redis")->handler()->lpush("companycopy",json_encode($param['data'],JSON_UNESCAPED_UNICODE));
  526. return json_show(0, '添加成功', ['id' => $id]);
  527. } catch (Exception $exception) {
  528. Db::rollback();
  529. return json_show(1003, $exception->getMessage());
  530. }
  531. }
  532. public function sInfo()
  533. {
  534. $code = $this->request->post('code', '', 'trim');
  535. if ($code == "") return json_show(1004, "参数code不能为空");
  536. $info = Db::name('supplier')
  537. ->alias('a')
  538. ->field('a.*,b.contactor,b.mobile,b.position,b.email,b.telephone')
  539. ->leftJoin('supplier_contact b', 'b.code=a.code AND b.is_del=0')
  540. ->where(['a.code' => $code, 'a.is_del' => 0])
  541. ->findOrEmpty();
  542. if (empty($info)) return json_show(1002, "未找到供应商数据");
  543. return json_show(0, '获取成功', $info);
  544. }
  545. public function sEdit()
  546. {
  547. $param = $this->request->only(['data', 'contact'], 'post', 'trim');
  548. $val = Validate::rule([
  549. 'data|供应商数据' => 'require|array',
  550. 'contact|联系人' => 'require|array',
  551. ]);
  552. if ($val->check($param) == false) return json_show(1004, $val->getError());
  553. Db::startTrans();
  554. try {
  555. $tmp = Db::name('supplier')
  556. ->field('id')
  557. ->where(['name' => $param['data']['name'], 'is_del' => 0])
  558. ->where('id', '<>', $param['data']['id'])
  559. ->findOrEmpty();
  560. if (!empty($tmp)) throw new Exception('该供应商名称已存在');
  561. $id = Db::name('supplier')
  562. ->where(['id' => $param['data']['id'], 'is_del' => 0])
  563. ->strict(false)
  564. ->update($param['data']);
  565. $tmp = Db::name('supplier_contact')
  566. ->field('id')
  567. ->where(['code' => $param['contact']['code'], 'is_del' => 0])
  568. ->findOrEmpty();
  569. if (empty($tmp)) Db::name('supplier_contact')->insert(array_merge($param['contact'], ['addtime' => $param['contact']['updatetime']]));
  570. else Db::name('supplier_contact')->where(['id' => $tmp['id'], 'is_del' => 0])->update($param['contact']);
  571. Db::name('headquarters')
  572. ->where(['code' => $param['contact']['code'], 'is_del' => 0])
  573. ->update([
  574. 'name' => $param['data']['name'],
  575. 'type' => 3,
  576. 'invoice_title' => '',
  577. 'invoice_people' => '',
  578. 'invoice_addr' => '',
  579. 'invoice_mobile' => $param['contact']['mobile'],
  580. 'invoice_code' => '',
  581. 'invoice_bank' => '',
  582. 'invoice_bankNo' => '',
  583. 'invoice_img' => $param['data']['license_img'],
  584. 'remark' => $param['data']['remark'],
  585. 'is_del' => $param['data']['is_del'],
  586. 'updater' => $param['data']['updater'],
  587. 'updaterid' => $param['data']['updaterid'],
  588. 'updatetime' => date("Y-m-d H:i:s"),
  589. ]);
  590. $this->checkSupplier($param['data'],$param['contact']);
  591. Db::commit();
  592. $param['data']['type']=3;
  593. Cache::store("redis")->handler()->lpush("companycopy",json_encode($param['data'],JSON_UNESCAPED_UNICODE));
  594. return json_show(0, '修改成功', ['id' => $id]);
  595. } catch (Exception $exception) {
  596. Db::rollback();
  597. return json_show(1003, $exception->getMessage());
  598. }
  599. }
  600. //客户
  601. public function cInfo()
  602. {
  603. $companyNo = $this->request->post('companyNo', '', 'trim');
  604. $info = Db::name('customer_info')
  605. ->where(['is_del' => 0, 'companyNo' => $companyNo])
  606. ->field(true)
  607. ->findOrEmpty();
  608. $info['member'] = Db::name("customer_member")
  609. ->where(['companyNo' => $companyNo, "is_del" => 0])
  610. ->order('id')
  611. ->select()
  612. ->toArray();
  613. return json_show(0, "获取成功", $info);
  614. }
  615. public function cTitle()
  616. {
  617. $param = $this->request->only([
  618. 'page' => 1,
  619. 'size' => 10,
  620. 'companyNo' => '',
  621. 'companyName' => '',
  622. 'itemid' => '',
  623. ], 'post', 'trim');
  624. $where [] = ['is_del', "=", 0];
  625. if ($param['companyNo'] != '') $where[] = ['companyNo', 'like', '%' . $param['companyNo'] . '%'];
  626. if ($param['companyName'] != '') $where[] = ['companyName', 'like', '%' . $param['companyName'] . '%'];
  627. if ($param['itemid'] != '') $where[] = ['itemid', '=', $param['itemid']];
  628. $count = Db::name('customer_info')
  629. ->where($where)
  630. ->count('id');
  631. $item = Db::name('customer_info')
  632. ->where($where)
  633. ->field("id,companyNo,companyName,area,LENGTH(companyName) as weight,status")
  634. ->order(['weight' => 'asc', 'id' => 'desc'])
  635. ->page($param['page'], $param['size'])
  636. ->select()
  637. ->toArray();
  638. return json_show(0, "获取成功", ['item' => $item, 'count' => $count]);
  639. }
  640. public function cCreate()
  641. {
  642. $param = $this->request->only([
  643. 'companyName',
  644. 'parent' => 0,
  645. 'customer_member',
  646. 'uid',
  647. 'uname',
  648. 'branch',
  649. 'middle',
  650. 'area',
  651. ], 'post', 'trim');
  652. $val = Validate::rule([
  653. 'companyName|客户名称' => 'require',
  654. 'customer_member|联系方式' => 'require|array|max:100',
  655. 'uid' => 'require|number|gt:0',
  656. 'uname|创建人' => 'require',
  657. 'branch|省级' => 'require',
  658. 'middle|市级' => 'require',
  659. 'area|区域' => 'require',
  660. ]);
  661. if ($val->check($param) == false) return json_show(1004, $val->getError());
  662. $companyNo = makeNo("KH");
  663. $rename = Db::name('customer_org1')
  664. ->field('id')
  665. ->where(['is_del' => 0, 'id' => $param['parent']])
  666. ->findOrEmpty();
  667. $item = Db::name('customer_info')
  668. ->field('id')
  669. ->where(['itemid' => $rename['id'] ?? 0, 'companyName' => $param['companyName'], 'is_del' => 0])
  670. ->findOrEmpty();
  671. if (!empty($item)) return json_show(1002, "公司名称已存在");
  672. $createrid = $param['uid'];//isset($user["data"]['id']) ? $user["data"]['id'] : "";
  673. $creater = $param['uname'];//isset($user["data"]['nickname']) ? $user["data"]['nickname'] : "";
  674. Db::startTrans();
  675. try {
  676. $date = date('Y-m-d H:i:s');
  677. $data = [
  678. "companyNo" => $companyNo,
  679. "companyName" => $param['companyName'],
  680. "parent" => $param['parent'],
  681. "itemid" => $param['parent'],
  682. "area" =>$param['area'],
  683. "status" => 0,
  684. "branch" =>$param['branch'],
  685. "middle" =>$param['middle'],
  686. "is_del" => 0,
  687. "createrid" => $createrid,
  688. "creater" => $creater,
  689. "addtime" => $date,
  690. "updatetime" => $date,
  691. ];
  692. $datainfo = Db::name('customer_info')->insert($data);
  693. if ($datainfo) {
  694. $var = [];
  695. foreach ($param['customer_member'] as $value) {
  696. $var[] = [
  697. 'commobile' => $value['commobile'] ?? '',
  698. 'comtel' => '',
  699. 'contactor' => $value['contactor'] ?? '',
  700. 'position' => $value['position'] ?? '',
  701. 'wxaccount' => $value['wxaccount'] ?? '',
  702. 'qqaccount' => $value['qqaccount'] ?? '',
  703. 'email' => $value['email'] ?? '',
  704. 'comdepart' => $value['comdepart'] ?? '',
  705. 'status' => $value['status'] ?? '',
  706. 'createrid' => $createrid,
  707. 'creater' => $creater,
  708. 'companyNo' => $companyNo,
  709. 'is_del' => 0,
  710. 'addtime' => $date,
  711. 'updatetime' => $date,
  712. ];
  713. }
  714. $vp = Db::name('customer_member')->insertAll($var);
  715. if ($vp == "") throw new Exception('新建联系人失败');
  716. //汇总表
  717. Db::name('headquarters')
  718. ->insert([
  719. 'code' => $companyNo,
  720. 'name' => $param['companyName'],
  721. 'type' => 2,
  722. 'creater' => $param['uname'],
  723. 'createrid' => $param['uid'],
  724. "addtime" => $date,
  725. 'updater' => $param['uname'],
  726. 'updaterid' => $param['uid'],
  727. "updatetime" => $date,
  728. ]);
  729. Db::commit();
  730. $data['type']=2;
  731. Cache::store("redis")->handler()->lpush("companycopy",json_encode($data,JSON_UNESCAPED_UNICODE));
  732. return json_show(0, "新建成功");
  733. } else throw new Exception('新建失败');
  734. } catch (Exception $e) {
  735. Db::rollback();
  736. return json_show(1005, $e->getMessage());
  737. }
  738. }
  739. public function cList()
  740. {
  741. $param = $this->request->only(['page' => 1, 'size' => 10, 'companyName' => '', 'status' => '', 'creater' => '', 'start' => '', 'end' => ''], 'post', 'trim');
  742. $where = [["a.is_del", "=", 0]];
  743. if ($param['companyName'] != "") $where[] = ['b.companyName', "like", '%' . $param['companyName'] . '%'];
  744. if ($param['status'] !== "") $where[] = ['status', "=", $param['status']];
  745. if ($param['creater'] != "") $where[] = ['a.creater', "like", '%' . $param['creater'] . '%'];
  746. if ($param['start'] !== "") $where[] = ['a.addtime', ">=", date('Y-m-d H:i:s', strtotime($param['start']))];
  747. if ($param['end'] !== "") $where[] = ['a.addtime', "<", date('Y-m-d H:i:s', strtotime($param['end']) + 24 * 3600)];
  748. $count = Db::name('customer_member')
  749. ->alias('a')
  750. ->leftJoin('customer_info b', "b.companyNo=a.companyNo")
  751. ->where($where)
  752. ->count('a.id');
  753. $total = ceil($count / $param['size']);
  754. $page = $param['page'] >= $total ? $total : $param['page'];
  755. $list = Db::name('customer_member')
  756. ->alias('a')
  757. ->leftJoin('customer_info b', "b.companyNo=a.companyNo")
  758. ->where($where)
  759. ->page($page, $param['size'])
  760. ->order("addtime desc")
  761. ->field("a.*,b.companyName,b.parent,b.area")
  762. ->select()
  763. ->toArray();
  764. return json_show(0, "获取成功", ['list' => $list, 'count' => $count]);
  765. }
  766. public function cEdit()
  767. {
  768. $param = $this->request->only([
  769. 'id',
  770. 'companyName',
  771. 'parent',
  772. 'customer_member',
  773. 'uid',
  774. 'uname',
  775. 'branch',
  776. 'middle',
  777. 'area',
  778. ], 'post', 'trim');
  779. $val = Validate::rule([
  780. 'id' => 'require|number|gt:0',
  781. 'companyName|客户名称' => 'require',
  782. 'parent' => 'require',
  783. 'customer_member|联系方式' => 'require|array|max:100',
  784. 'uid' => 'require|number|gt:0',
  785. 'uname|修改人' => 'require',
  786. 'branch|省级'=> 'require',
  787. 'middle|市级'=> 'require',
  788. 'area|区域'=> 'require',
  789. ]);
  790. if ($val->check($param) == false) return json_show(1004, $val->getError());
  791. $idinfo = Db::name('customer_info')
  792. ->field('id,status,companyNo')
  793. ->where(['id' => $param['id'], 'is_del' => 0])
  794. ->findOrEmpty();
  795. if (empty($idinfo)) return json_show(1004, "未找到数据");
  796. if ($idinfo['status'] == 1) return json_show(1002, "状态是启用状态,无法编辑");
  797. $rename = Db::name('customer_org1')
  798. ->where(['is_del' => 0, 'id' => $param['parent']])
  799. ->field('id')
  800. ->findOrEmpty();
  801. $item = Db::name('customer_info')
  802. ->field('id')
  803. ->where(['itemid' => $rename['id'] ?? 0, 'companyName' => $param['companyName'], 'is_del' => 0])
  804. ->where('id', '<>', $param['id'])
  805. ->findOrEmpty();
  806. if (!empty($item)) return json_show(1002, "公司名称已存在");
  807. $createrid = $param['uid'];//isset($user["data"]['id']) ? $user["data"]['id'] : "";
  808. $creater = $param['uname'];//isset($user["data"]['nickname']) ? $user["data"]['nickname'] : "";
  809. Db::startTrans();
  810. try {
  811. $date = date('Y-m-d H:i:s');
  812. $data = [
  813. "id" => $param['id'],
  814. "companyName" => $param['companyName'],
  815. "area" =>$param['area'],
  816. "branch" =>$param['branch'],
  817. "middle" =>$param['middle'],
  818. "parent" => $param['parent'],
  819. "updatetime" => $date,
  820. ];
  821. $datainfo = Db::name('customer_info')->save($data);
  822. if ($datainfo) {
  823. foreach ($param['customer_member'] as $value) {
  824. $item = [];
  825. $item['commobile'] = isset($value['commobile']) ? $value['commobile'] : "";
  826. $item['comtel'] = "";
  827. isset($value['id']) && $value['id'] !== "" ? $item['id'] = $value['id'] : '';
  828. $item['contactor'] = isset($value['contactor']) ? $value['contactor'] : "";
  829. $item['position'] = isset($value['position']) ? $value['position'] : "";
  830. $item['wxaccount'] = isset($value['wxaccount']) ? $value['wxaccount'] : "";
  831. $item['qqaccount'] = isset($value['qqaccount']) ? $value['qqaccount'] : "";
  832. $item['email'] = isset($value['email']) ? $value['email'] : "";
  833. $item['comdepart'] = isset($value['comdepart']) ? $value['comdepart'] : "";
  834. $item['status'] = $value['status'];
  835. $item['createrid'] = $createrid;
  836. $item['creater'] = $creater;
  837. $item['companyNo'] = isset($idinfo['companyNo']) ? $idinfo['companyNo'] : "";
  838. $item['is_del'] = 0;
  839. $item['addtime'] = $date;
  840. $item['updatetime'] = $date;
  841. $vp = Db::name('customer_member')->save($item);
  842. if ($vp == false) throw new Exception('更新失败');
  843. }
  844. //更新汇总表
  845. Db::name('headquarters')
  846. ->where(['is_del' => 0, 'type' => 2, 'code' => $idinfo['companyNo']])
  847. ->update([
  848. 'name' => $param['companyName'],
  849. 'updater' => $param['uname'],
  850. 'updaterid' => $param['uid'],
  851. "updatetime" => $date,
  852. ]);
  853. Db::commit();
  854. return json_show(0, "更新成功");
  855. } else throw new Exception("更新失败");
  856. } catch (Exception $e) {
  857. Db::rollback();
  858. return json_show(1005, $e->getMessage());
  859. }
  860. }
  861. //供应商升级成业务公司
  862. public function supplerUpgrade()
  863. {
  864. $post = $this->request->only(['code', 'inv_bank', 'inv_bankNo', 'inv_addr', 'invoice_mobile', 'uid', 'uname', 'invoice_title'], 'post', 'trim');
  865. $val = Validate::rule([
  866. 'code|供应商编码' => 'require',
  867. 'inv_bank|银行名称' => 'require|max:255',
  868. 'inv_bankNo|银行卡号' => 'require|number',
  869. 'inv_addr|联系地址' => 'require|max:255',
  870. 'invoice_mobile' => 'require',
  871. 'invoice_title|公司抬头' => 'require',
  872. ]);
  873. if ($val->check($post) == false) return json_show(1004, $val->getError());
  874. $res = Db::name('supplier')
  875. ->alias('a')
  876. ->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')
  877. ->leftJoin('supplier_contact b', 'b.code=a.code')
  878. ->where(['a.code' => $post['code'], 'a.is_del' => 0])
  879. ->findOrEmpty();
  880. if (empty($res)) return json_show(1004, '该供应商不存在');
  881. if ($res['status'] != 1) return json_show(1004, '该供应商已禁用');
  882. if ($res['is_upgrade'] == 1) return json_show(1004, '不能重复升级');
  883. $temp = Db::name('company_type')
  884. ->field('id')
  885. ->where(['company_type' => $res['nature'], 'is_del' => 0])
  886. ->findOrEmpty();
  887. // if (empty($temp)) return json_show(1004, '对应公司类型不存在');
  888. Db::startTrans();
  889. try {
  890. $date = date('Y-m-d H:i:s');
  891. $business_code = makeNo('GS');
  892. //业务公司列表
  893. Db::name('business')
  894. ->insert([
  895. 'company' => $res['name'],
  896. 'companyNo' => $business_code,
  897. 'inv_code' => $res['registercode'],
  898. 'company_type' => $res['nature'],
  899. 'type' => $temp['id'] ?? 0,
  900. 'creater' => $post['uname'],
  901. 'createrid' => $post['uid'],
  902. 'inv_legaler' => $res['legaler'],
  903. 'inv_time' => $res['registertime'],
  904. 'inv_addr' => $post['inv_addr'],
  905. 'inv_bank' => $post['inv_bank'],
  906. 'inv_bankNo' => $post['inv_bankNo'],
  907. 'contactor' => $res['contactor'],
  908. 'mobile' => $res['mobile'],
  909. 'addr' => $res['addr'],
  910. 'inv_scope' => $res['scope'],
  911. 'license_img' => $res['license_img'],
  912. 'invoice_title' => $post['invoice_title'],
  913. 'invoice_mobile' => $post['invoice_mobile'],
  914. 'status' => 1,
  915. 'is_del' => 0,
  916. 'addtime' => $date,
  917. 'updatetime' => $date,
  918. ]);
  919. $temp_supplier = Db::name('headquarters')
  920. ->where(['code' => $post['code'], 'type' => 3])
  921. ->findOrEmpty();
  922. //汇总表
  923. Db::name('headquarters')
  924. ->where(['code' => $post['code'], 'type' => 3])
  925. ->update([
  926. 'relation_code' => $business_code,
  927. 'updater' => $post['uname'],
  928. 'updaterid' => $post['uid'],
  929. 'updatetime' => $date,
  930. ]);
  931. Db::name('headquarters')
  932. ->insert(array_merge($temp_supplier, [
  933. 'id' => null,
  934. 'code' => $business_code,
  935. 'type' => 1,//业务公司
  936. 'relation_code' => $post['code']
  937. ]));
  938. //供应商
  939. Db::name('supplier')
  940. ->where(['code' => $post['code'], 'is_upgrade' => 0, 'is_del' => 0])
  941. ->update(['is_upgrade' => 1, 'updatetime' => $date]);
  942. //账号与公司的关联关系
  943. Db::name('account_company')
  944. ->insert([
  945. 'account_id' => $post['uid'],
  946. 'companyCode' => $business_code,
  947. 'companyName' => $res['name'],
  948. 'company_type' => 2,
  949. 'status' => 1,
  950. 'is_del' => 0,
  951. 'addtime' => $date,
  952. 'updatetime' => $date,
  953. ]);
  954. Db::commit();
  955. return json_show(0, '升级成功');
  956. } catch (Exception $exception) {
  957. Db::rollback();
  958. return json_show(1004, '升级失败,' . $exception->getMessage());
  959. }
  960. }
  961. //获取业务公司列表(为了采销临时脚本同步数据到结算平台使用,正式上线要删除)
  962. public function getBusinessListTmp()
  963. {
  964. $where = $this->request->filter('trim')->post('where', []);
  965. $list = Db::name('business')
  966. ->alias('a')
  967. ->field('a.*,b.code as supplierNo')
  968. ->leftJoin('headquarters b', 'b.relation_code=a.companyNo')
  969. ->where($where)
  970. ->select()
  971. ->toArray();
  972. return json_show(0, '获取成功', $list);
  973. }
  974. private function checkSupplier($supplier,$canact){
  975. $hquest = Db::name("headquarters")->where(["code"=>$supplier["code"],"is_del"=>0])->findOrEmpty();
  976. if(empty($hquest)) throw new Exception("未找到供应商关联数据");
  977. if($hquest['relation_code']!='' && $hquest['type']==3){
  978. $comp = Db::name('headquarters')->where(['code'=>$hquest['relation_code'],'is_del'=>0])->findOrEmpty();
  979. if(empty($comp)) throw new Exception('未找到供应商关联业务公司数据');
  980. $business = Db::name("business")->where(['companyNo'=>$hquest['relation_code'],'is_del'=>0])->findOrEmpty();
  981. if(empty($business)) throw new Exception('未找到供应商关联业务公司数据');
  982. $comp['name'] = $supplier['name'];
  983. !isset($supplier['legaler'])?:$comp['invoice_people'] = $supplier['legaler'];
  984. !isset($supplier['registercode'])?:$comp['invoice_code'] = $supplier['registercode'];
  985. !isset($supplier['license_img'])?:$comp['invoice_img'] = $supplier['license_img'];
  986. $comp['updatetime'] = date("Y-m-d H:i:s");
  987. $ip = Db::name('headquarters')->save($comp);
  988. if($ip==false) throw new Exception('供应商关联业务公司数据更新失败');
  989. $business['company'] =$supplier['name'];
  990. !isset($supplier['registercode'])?:$business['inv_code'] =$supplier['registercode'];
  991. !isset($supplier['legaler'])?:$business['inv_legaler'] =$supplier['legaler'];
  992. !isset($supplier['registertime'])?:$business['inv_time'] =$supplier['registertime'];
  993. !isset($supplier['addr'])?:$business['addr'] =$supplier['addr'];
  994. !isset($canact['contactor'])?:$business['contactor'] =$canact['contactor'];
  995. !isset($canact['mobile'])?:$business['mobile'] =$canact['mobile'];
  996. !isset($supplier['scope'])?:$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. }