Headquarters.php 47 KB

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