refill_company.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. <?php
  2. class refill_companyControl extends SystemControl
  3. {
  4. public function __construct()
  5. {
  6. parent::__construct();
  7. }
  8. public function indexOp()
  9. {
  10. $cond = [];
  11. $company_mod = Model('refill_company');
  12. if(!empty($_GET['co_name'])) {
  13. $cond['co_name'] = ['like', '%'.$_GET['co_name'].'%'];
  14. }
  15. $_GET['co_type'] = $_GET['co_type'] ?? 'merchant';
  16. if(!empty($_GET['co_type'])) {
  17. $cond['co_type'] = $_GET['co_type'];
  18. }
  19. if(!empty($_GET['opened'])) {
  20. $cond['opened'] = $_GET['opened'];
  21. }
  22. $list = $company_mod->getCompanyList($cond, 50);
  23. $company_debts = rcache("company-debts", 'refill-');
  24. foreach ($list as $key => $value)
  25. {
  26. $co_id = $value['co_id'];
  27. if(array_key_exists($co_id, $company_debts)) {
  28. $list[$key]['debt'] = number_format($company_debts[$co_id],2,'.','');
  29. }else{
  30. $list[$key]['debt'] = '/';
  31. }
  32. }
  33. Tpl::output('list', $list);
  34. Tpl::output('show_page', $company_mod->showpage());
  35. Tpl::showpage('refill.company');
  36. }
  37. public function changeStateOp()
  38. {
  39. $co_id = intval($_GET['co_id']);
  40. $opened = intval($_GET['opened']);
  41. $company_mod = Model('refill_company');
  42. $company_info = $company_mod->getCompanyInfo(['co_id' => $co_id]);
  43. if (empty($company_info) || !in_array($opened, [1, 2])) {
  44. showMessage('操作成功');
  45. }
  46. $resp = $company_mod->editCompany(['opened' => $opened], ['co_id' => $co_id]);
  47. if (!$resp) {
  48. showMessage('操作失败', 'index.php?act=refill_company&op=index', 'html', 'error');
  49. }
  50. showMessage('操作成功');
  51. }
  52. public function company_delOp()
  53. {
  54. $co_id = intval($_GET['co_id']);
  55. $company_mod = Model('refill_company');
  56. $company_info = $company_mod->getCompanyInfo(['co_id' => $co_id]);
  57. $resp = $company_mod->delCompany($co_id);
  58. if (!$resp) {
  59. showMessage('删除失败');
  60. }
  61. $co_type = $company_info['co_type'];
  62. $res = true;
  63. if ($co_type == 'merchant') {
  64. $res = Model('')->table('merchant')->where(['co_id' => $co_id])->update(['co_id' => 0]);
  65. } elseif ($co_type == 'provider') {
  66. $res = Model('')->table('refill_provider')->where(['co_id' => $co_id])->update(['co_id' => 0]);
  67. }
  68. if (!$res) {
  69. showMessage('操作失败');
  70. }
  71. showMessage('操作成功');
  72. }
  73. public function addOp()
  74. {
  75. $company_mod = Model('refill_company');
  76. if (chksubmit())
  77. {
  78. $obj_validate = new Validator();
  79. $obj_validate->validateparam = [
  80. ["input" => $_POST["co_type"], "require" => "true", "message" => '必须选择公司类型'],
  81. ["input" => $_POST["co_name"], "require" => "true", "message" => '公司名称不能为空'],
  82. ["input" => $_POST["bank_name"], "require" => "true", "message" => '收款银行名称不能为空'],
  83. ["input" => $_POST["bank_username"], "require" => "true", "message" => '开户人名称不能为空'],
  84. ["input" => $_POST["bank_card_no"], "require" => "true", "message" => '收款卡号不能为空']
  85. ];
  86. $error = $obj_validate->validate();
  87. if ($error != '') {
  88. showMessage($error);
  89. } else {
  90. $insert_array['co_name'] = $_POST['co_name'];
  91. $insert_array['co_type'] = $_POST['co_type'];
  92. $insert_array['bank_name'] = $_POST['bank_name'];
  93. $insert_array['bank_username'] = $_POST['bank_username'];
  94. $insert_array['bank_card_no'] = $_POST['bank_card_no'];
  95. $insert_array['max_debt'] = $_POST['max_debt'] ?? 0;
  96. $insert_array['province'] = $_POST['province'] ?? '';
  97. $insert_array['city'] = $_POST['city'] ?? '';
  98. $insert_array['bank_code'] = $_POST['bank_code'] ?? '';
  99. $insert_array['remark'] = $_POST['remark'] ?? '';
  100. $insert_array['oper_time'] = time();
  101. $co_id = $company_mod->addCompany($insert_array);
  102. if ($co_id) {
  103. $cid = $_POST['cid'] ?? '';
  104. $this->co_id_bind($cid, $co_id, $insert_array['co_type']);
  105. showMessage('添加成功', 'index.php?act=refill_company&op=index');
  106. } else {
  107. showMessage('添加失败');
  108. }
  109. }
  110. }
  111. else
  112. {
  113. Tpl::showpage('refill.company.add');
  114. }
  115. }
  116. public function editOp()
  117. {
  118. $co_id = $_GET['co_id'] ?? $_POST['co_id'];
  119. $company_mod = Model('refill_company');
  120. $company_info = $company_mod->getCompanyInfo(['co_id' => $co_id]);
  121. if (empty($company_info)) {
  122. showMessage('公司信息不存在');
  123. }
  124. if (chksubmit()) {
  125. $update['co_type'] = $_POST['co_type'];
  126. $update['co_name'] = $_POST['co_name'];
  127. $update['bank_name'] = trim($_POST['bank_name']);
  128. $update['bank_username'] = trim($_POST['bank_username']);
  129. $update['bank_card_no'] = trim($_POST['bank_card_no']);
  130. $update['max_debt'] = trim($_POST['max_debt']);
  131. $update['province'] = trim($_POST['province']);
  132. $update['city'] = trim($_POST['city']);
  133. $update['bank_code'] = trim($_POST['bank_code']);
  134. $update['remark'] = trim($_POST['remark']);
  135. $update['oper_time'] = time();
  136. $result = $company_mod->editCompany($update, ['co_id' => $co_id]);
  137. if (!$result) {
  138. showMessage('编辑失败');
  139. }
  140. $cid = $_POST['cid'] ?? '';
  141. $this->co_id_bind($cid, $co_id, $company_info['co_type']);
  142. $this->log("修改公司信息,公司ID:{$co_id},公司名称:{$company_info['co_name']}");
  143. showMessage('编辑成功','index.php?act=refill_company&op=index');
  144. }
  145. else
  146. {
  147. $cid = [];
  148. if($company_info['co_type'] == 'merchant') {
  149. $cid = Model('')->table('merchant')->where(['co_id' => $co_id])->field('mchid')->select();
  150. $cid = array_column($cid, 'mchid');
  151. }elseif ($company_info['co_type'] == 'provider') {
  152. $cid = Model('')->table('refill_provider')->where(['co_id' => $co_id])->field('store_id')->select();
  153. $cid = array_column($cid, 'store_id');
  154. }
  155. Tpl::output('cid', implode(',', $cid));
  156. Tpl::output('company_info', $company_info);
  157. Tpl::showpage('refill.company.edit');
  158. }
  159. }
  160. private function co_id_bind($cid, $co_id, $co_type)
  161. {
  162. if(!empty($cid)) {
  163. $cid = explode(',', $cid);
  164. if($co_type == 'merchant'){
  165. $cond['mchid'] = ['in', $cid];
  166. Model('')->table('merchant')->where(['co_id' => $co_id])->update(['co_id' => 0]);
  167. Model('')->table('merchant')->where($cond)->update(['co_id' => $co_id]);
  168. }elseif ($co_type == 'provider'){
  169. $cond['store_id'] = ['in', $cid];
  170. Model('')->table('refill_provider')->where(['co_id' => $co_id])->update(['co_id' => 0]);
  171. Model('')->table('refill_provider')->where($cond)->update(['co_id' => $co_id]);
  172. }
  173. }
  174. }
  175. public function remit_cfgOp()
  176. {
  177. if(chksubmit())
  178. {
  179. unset($_POST['form_submit']);
  180. $cache = [];
  181. foreach ($_POST as $key => $value) {
  182. $cache[$key] = $value;
  183. }
  184. wcache('remit', ['cfg' => serialize($cache)], 'refill-');
  185. showMessage('编辑成功');
  186. }
  187. else
  188. {
  189. $config = rcache('remit', 'refill-');
  190. if (empty($config)) {
  191. $config = [];
  192. } else {
  193. $config = unserialize($config['cfg']);
  194. }
  195. Tpl::output('config', $config);
  196. Tpl::showpage('remit.config');
  197. }
  198. }
  199. public function remit_recordOp()
  200. {
  201. $mod_remit = Model('refill_company_remit');
  202. $cond = [];
  203. $start_unixtime = intval(strtotime($_GET['query_start_time']));
  204. $end_unixtime = intval(strtotime($_GET['query_end_time']));
  205. if ($start_unixtime > 0 && $end_unixtime > $start_unixtime) {
  206. $cond['add_time'] = [['egt', $start_unixtime], ['lt', $end_unixtime], 'and'];
  207. } elseif ($start_unixtime > 0) {
  208. $cond['add_time'] = ['egt', $start_unixtime];
  209. } elseif ($end_unixtime > 0) {
  210. $cond['add_time'] = ['lt', $end_unixtime];
  211. }
  212. $list = $mod_remit->getRemitList($cond, 50);
  213. foreach ($list as $key => $value)
  214. {
  215. $list[$key]['amount'] = number_format($value['amount'],4,'.',',');
  216. }
  217. $refill_company = $this->refill_companys([]);
  218. Tpl::output('list', $list);
  219. Tpl::output('refill_company', $refill_company);
  220. Tpl::output('show_page', $mod_remit->showpage());
  221. Tpl::showpage('refill.company.remit');
  222. }
  223. public function remit_delOp()
  224. {
  225. $mod_remit = Model('refill_company_remit');
  226. $remit_id = $_GET['remit_id'];
  227. $this->check_remit($remit_id);
  228. $result = $mod_remit->DelRemit($remit_id);
  229. if ($result) {
  230. showMessage('删除成功');
  231. } else {
  232. showMessage('删除失败');
  233. }
  234. }
  235. public function remit_editOp()
  236. {
  237. $remit_id = $_GET['remit_id'] ?? $_POST['remit_id'];
  238. $remit = $this->check_remit($remit_id);
  239. $params = json_decode($remit['params'], true);
  240. $provider_list = $this->providers();
  241. foreach ($provider_list as $provider) {
  242. $co_id = $provider['co_id'];
  243. if(empty($co_id)) continue;
  244. $pid = $provider['provider_id'];
  245. $account_id = intval($provider['account_id']);
  246. if($account_id > 0) {
  247. $mid_pids[$account_id] = $pid;
  248. $mids[] = $account_id;
  249. }
  250. $provider['available_predeposit'] = 0;
  251. $providers[$pid] = $provider;
  252. }
  253. $refill_company = $this->refill_companys(['co_type' => refill_companyModel::co_type_provider]);
  254. if(chksubmit())
  255. {
  256. $remits = $_POST['remits'];
  257. $strs = $_POST['strs'];
  258. $operation = $_POST['operation'];
  259. $remit_total = 0;
  260. foreach ($strs as $key => $str)
  261. {
  262. $remit = $remits[$key];
  263. if(empty($remit)) continue;
  264. $item = explode('-', $str);
  265. $co_id = $item[0];
  266. $pid = $item[1];
  267. $data[$co_id][$pid] = $remit;
  268. $remit_total += $remit;
  269. }
  270. if(empty($data)) {
  271. showMessage('未提交数据');
  272. }
  273. $remit_record = [
  274. 'amount' => $remit_total,
  275. 'params' => json_encode($data),
  276. 'operation' => $operation,
  277. 'oper_time' => time()
  278. ];
  279. Model('refill_company_remit')->editRemit($remit_record, ['remit_id' => $remit_id]);
  280. showMessage('操作成功', 'index.php?act=refill_company&op=remit_record');
  281. }
  282. else
  283. {
  284. $member_data = Model('member')->field('member_id,available_predeposit')->where(['member_id' => ['in',$mids]])->select();
  285. foreach ($member_data as $member)
  286. {
  287. $mid = intval($member['member_id']);
  288. if(array_key_exists($mid,$mid_pids)) {
  289. $pid = $mid_pids[$mid];
  290. $providers[$pid]['available_predeposit'] = $member['available_predeposit'];
  291. }
  292. }
  293. foreach ($params as $co_id => $item)
  294. {
  295. $data = [];
  296. $data['co_name'] = $refill_company[$co_id]['co_name'];
  297. foreach ($item as $pid => $remit_money)
  298. {
  299. $providers[$pid]['remit'] = $remit_money;
  300. $data['providers'][] = $providers[$pid];
  301. }
  302. $result[] = $data;
  303. }
  304. Tpl::output('remit_data', $result);
  305. Tpl::output('remit', $remit);
  306. Tpl::showpage('provider.remit.edit');
  307. }
  308. }
  309. public function remit_commitOp()
  310. {
  311. $mod = Model('refill_company_remit');
  312. $remit_id = $_GET['remit_id'];
  313. $remit = $this->check_remit($remit_id);
  314. $params = json_decode($remit['params'], true);
  315. $provider_list = $this->providers();
  316. foreach ($provider_list as $provider) {
  317. $providers[$provider['provider_id']] = $provider;
  318. }
  319. $bz = '批量打款';
  320. foreach ($params as $item)
  321. {
  322. try {
  323. $trans = new trans_wapper($mod, __METHOD__);
  324. $remit_total = 0;
  325. foreach ($item as $pid => $money)
  326. {
  327. $amount_data = [
  328. 'pointsnum' => $money,
  329. 'operation' => $remit['operation'],
  330. 'bz' => $bz
  331. ];
  332. $this->credit_save_money($money, 'add', $providers[$pid]['account_id'], $bz);
  333. $this->ct_provider_amount($amount_data, $providers[$pid]);
  334. $remit_total += $money;
  335. }
  336. $trans->commit();
  337. } catch (Exception $e) {
  338. $trans->rollback();
  339. Log::record("remit_commit err: {$e->getMessage()}", Log::ERR);
  340. showMessage('提交失败');
  341. }
  342. }
  343. $mod->editRemit(['remit_state' => refill_company_remitModel::remit_have], ['remit_id' => $remit_id]);
  344. showMessage('操作成功!');
  345. }
  346. private function ct_provider_amount($params, $provider_info)
  347. {
  348. $input['provider_id'] = $provider_info['provider_id'];
  349. $input['memeber_id'] = $provider_info['account_id'];
  350. $input['amount'] = $params['pointsnum'];
  351. $input['operation'] = $params['operation'];
  352. $input['add_time'] = time();
  353. $input['bz'] = $params['bz'] ?? '';
  354. return Model('provider_amount')->addAmount($input);
  355. }
  356. private function check_remit($remit_id)
  357. {
  358. $remit = Model('refill_company_remit')->getRemit($remit_id);
  359. if(empty($remit)) {
  360. showMessage('记录不存在');
  361. }
  362. return $remit;
  363. }
  364. public function remit_exportOp()
  365. {
  366. $remit_id = $_GET['remit_id'];
  367. $remit = $this->check_remit($remit_id);
  368. $params = json_decode($remit['params']);
  369. $remit_cfg = $this->remit_cfg();
  370. $remit_max = floatval($remit_cfg['remit_max']);
  371. $refill_company = $this->refill_companys(['co_type' => refill_companyModel::co_type_provider]);
  372. foreach ($params as $co_id => $item)
  373. {
  374. $remit_total = 0;
  375. foreach ($item as $money)
  376. {
  377. $remit_total += $money;
  378. }
  379. if( $remit_total < $remit_max) {
  380. $refill_company[$co_id]['money'] = $remit_total;
  381. $remit_data[] = $refill_company[$co_id];
  382. }else{
  383. for ( $i = $remit_total / $remit_max; $i>=0; --$i )
  384. {
  385. if($i < 1) {
  386. $refill_company[$co_id]['money'] = fmod($remit_total, $remit_max);
  387. } else {
  388. $refill_company[$co_id]['money'] = $remit_max;
  389. }
  390. $remit_data[] = $refill_company[$co_id];
  391. }
  392. }
  393. }
  394. $this->Export_file($remit['title'], $remit_data);
  395. }
  396. private function Export_file($file_name, $remit_data)
  397. {
  398. Language::read('export');
  399. import('libraries.excel');
  400. $excel_obj = new Excel();
  401. $excel_data = [];
  402. //设置样式
  403. $excel_obj->setStyle(['id' => 's_title', 'Font' => ['FontName' => '宋体', 'Size' => '12', 'Bold' => '1']]);
  404. //header
  405. $excel_data[0][] = ['styleid' => 's_title', 'data' => '序号'];
  406. $excel_data[0][] = ['styleid' => 's_title', 'data' => '收款人姓名'];
  407. $excel_data[0][] = ['styleid' => 's_title', 'data' => '银行卡号'];
  408. $excel_data[0][] = ['styleid' => 's_title', 'data' => '银行编码'];
  409. $excel_data[0][] = ['styleid' => 's_title', 'data' => '省/直辖市'];
  410. $excel_data[0][] = ['styleid' => 's_title', 'data' => '城市'];
  411. $excel_data[0][] = ['styleid' => 's_title', 'data' => '开户行名称'];
  412. $excel_data[0][] = ['styleid' => 's_title', 'data' => '付款金额'];
  413. $excel_data[0][] = ['styleid' => 's_title', 'data' => '0(对私付款)/1(对公付款)'];
  414. //data
  415. foreach ($remit_data as $key => $data) {
  416. if($data['money'] <= 0) continue;
  417. $tmp = [];
  418. $tmp[] = ['data' => $key+1];
  419. $tmp[] = ['data' => $data['bank_username']];
  420. $tmp[] = ['data' => $data['bank_card_no']];
  421. $tmp[] = ['data' => $data['bank_code']];
  422. $tmp[] = ['data' => $data['province']];
  423. $tmp[] = ['data' => $data['city']];
  424. $tmp[] = ['data' => $data['bank_name']];
  425. $tmp[] = ['data' => $data['money']];
  426. $tmp[] = ['data' => 1];
  427. $excel_data[] = $tmp;
  428. }
  429. $excel_data = $excel_obj->charset($excel_data, CHARSET);
  430. $excel_obj->addArray($excel_data);
  431. $excel_obj->addWorksheet($excel_obj->charset(L('exp_od_order'), CHARSET));
  432. $excel_obj->generateXML($file_name);
  433. }
  434. }