provider.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  1. <?php
  2. include(BASE_CONFIG_PATH . CONFIG_PREFIX . '/refill.ini.php');
  3. require_once(BASE_HELPER_PATH . '/model/member_info.php');
  4. require_once(BASE_HELPER_PATH . '/refill/chprice_helper.php');
  5. class providerControl extends SystemControl
  6. {
  7. public function __construct()
  8. {
  9. parent::__construct();
  10. }
  11. public function indexOp()
  12. {
  13. global $config;
  14. $this->sync_cfgs();
  15. $provider_model = Model('refill_provider');
  16. $condition = [];
  17. if (trim($_GET['name']) != '') {
  18. $condition['store_name'] = ['like', '%' . $_GET['name'] . '%'];
  19. Tpl::output('name', $_GET['name']);
  20. }
  21. if (in_array($_GET['type'], [mtopcard\OilCardPaper, mtopcard\PhoneCardPaper, mtopcard\ThirdCardPaper])) {
  22. $condition['type'] = $_GET['type'];
  23. }
  24. if(!empty($_GET['quality'])) {
  25. $condition['qualitys'] = ['like', '%' . $_GET['quality'] . '%'];
  26. }
  27. $provider_items = $provider_model->table('refill_provider,store')
  28. ->field('refill_provider.*,store.store_name,store.member_id')
  29. ->join('inner')
  30. ->on('store.store_id=refill_provider.store_id')
  31. ->where($condition)
  32. ->order('opened asc, name asc')
  33. ->page(200)
  34. ->select();
  35. $providers = [];
  36. $mid_pids = [];
  37. $mids = [];
  38. foreach ($provider_items as $provider)
  39. {
  40. $pid = $provider['provider_id'];
  41. $providers[$pid] = $provider;
  42. $providers[$pid]['available_predeposit'] = 0;
  43. $quality_text = $provider['type'] == mtopcard\OilCardPaper ? $config['oil_quality_text'] : $config['phone_quality_text'];
  44. $providers[$pid]['quality_text'] = $quality_text[$provider['qualitys']];
  45. $account_id = intval($provider['account_id']);
  46. if($account_id > 0) {
  47. $mid_pids[$account_id] = $pid;
  48. $mids[] = $account_id;
  49. }
  50. }
  51. $debt_total = 0;
  52. $available_total = 0;
  53. if(!empty($mids))
  54. {
  55. $member_data = Model('member')->field('member_id,available_predeposit')->where(['member_id' => ['in',$mids]])->select();
  56. foreach ($member_data as $member)
  57. {
  58. $mid = intval($member['member_id']);
  59. if(array_key_exists($mid,$mid_pids)) {
  60. $pid = $mid_pids[$mid];
  61. $providers[$pid]['available_predeposit'] = $member['available_predeposit'];
  62. if($providers[$pid]['opened'] != 1) continue;
  63. if($member['available_predeposit'] >= 0) {
  64. $available_total += $member['available_predeposit'];
  65. }else{
  66. $debt_total += $member['available_predeposit'];
  67. }
  68. }
  69. }
  70. }
  71. $stats = ['available_total' => $available_total, 'debt_total' => $debt_total];
  72. $opened_text = ['使用中', '已禁用'];
  73. $type_text = ['油卡', '手机充值卡', '增值业务'];
  74. Tpl::output('opened_text', $opened_text);
  75. Tpl::output('type_text', $type_text);
  76. Tpl::output('provider_list', $providers);
  77. Tpl::output('stats', $stats);
  78. Tpl::output('prices', $this->all_price());
  79. Tpl::output('show_page', $provider_model->showpage());
  80. Tpl::showpage('provider.index');
  81. }
  82. private function all_price()
  83. {
  84. $result = [];
  85. $mod_price = Model('provider_price');
  86. $prices = $mod_price->getPirces(['price_id' => ['gt', 0]]);
  87. foreach ($prices as $price)
  88. {
  89. $key = "{$price['provider_name']}-{$price['product_type']}";
  90. if(!empty($price['effect_time']))
  91. {
  92. $time = '<span style="color: #0bb20c">' . date("Y-m-d H:i:s", $price['effect_time']) . '</span>';
  93. }else{
  94. $time = date("Y-m-d H:i:s", $price['update_time']);
  95. }
  96. $result[$key] = $time;
  97. }
  98. return $result;
  99. }
  100. public function recharge_manualOp()
  101. {
  102. $provider_id = $_GET['provider_id'] ?? $_POST['provider_id'];
  103. $provider_model = Model('refill_provider');
  104. $provider_info = $provider_model->getProviderInfo(['provider_id' => $provider_id]);
  105. if (empty($provider_info)) {
  106. showMessage('通道信息有误');
  107. }
  108. if (chksubmit())
  109. {
  110. $obj_validate = new Validator();
  111. $obj_validate->validateparam = [
  112. ["input" => $_POST["operation"], "require" => "true", "message" => '操作人姓名不能为空'],
  113. ["input" => $_POST["pointsnum"], "require" => "true", "message" => '预存金额不能为空']
  114. ];
  115. $operatetype = $_POST['operatetype'];
  116. $pointsnum = $_POST['pointsnum'];
  117. $money = abs($pointsnum);
  118. if ($money == 0) {
  119. showMessage('金额错误');
  120. }
  121. try {
  122. $model_merchant = Model('merchant');
  123. $trans = new trans_wapper($model_merchant, __METHOD__);
  124. $member_id = $provider_info['account_id'];
  125. if ($operatetype == 'add') {
  126. $bz = "管理员调款操作,手动增加通道余额";
  127. $this->credit_save_money($money, 'add', $member_id, $bz);
  128. $_POST['pointsnum'] = $money;
  129. } elseif ($operatetype == 'del') {
  130. $bz = "管理员调款操作,手动减少通道余额";
  131. $this->credit_save_money($money, 'del', $member_id, $bz);
  132. $_POST['pointsnum'] = -($money);
  133. } else {
  134. showMessage('预存类型错误');
  135. }
  136. $result = $this->ct_provider_amount($_POST, $provider_info);
  137. if (!$result) {
  138. $trans->rollback();
  139. showMessage('操作失败');
  140. }
  141. $trans->commit();
  142. showMessage('操作成功', 'index.php?act=provider&op=index');
  143. } catch (Exception $e) {
  144. $trans->rollback();
  145. showMessage('操作失败');
  146. }
  147. }
  148. else
  149. {
  150. Tpl::output('provider', $provider_info);
  151. Tpl::showpage('provider.recharge');
  152. }
  153. }
  154. private function ct_provider_amount($params, $provider_info)
  155. {
  156. $input['provider_id'] = $provider_info['provider_id'];
  157. $input['memeber_id'] = $provider_info['account_id'];
  158. $input['amount'] = $params['pointsnum'];
  159. $input['operation'] = $params['operation'];
  160. $input['add_time'] = time();
  161. $input['bz'] = $params['bz'] ?? '';
  162. return Model('provider_amount')->addAmount($input);
  163. }
  164. public function provider_amountOp()
  165. {
  166. $condition = [];
  167. if(!empty($_GET['provider_id'])) {
  168. $condition['provider_id'] = $_GET['provider_id'];
  169. }
  170. $start_unixtime = intval(strtotime($_GET['query_start_time']));
  171. $end_unixtime = intval(strtotime($_GET['query_end_time']));
  172. if ($start_unixtime > 0 && $end_unixtime > $start_unixtime) {
  173. $condition['add_time'] = [['egt', $start_unixtime], ['lt', $end_unixtime], 'and'];
  174. } elseif ($start_unixtime > 0) {
  175. $condition['add_time'] = ['egt', $start_unixtime];
  176. } elseif ($end_unixtime > 0) {
  177. $condition['add_time'] = ['lt', $end_unixtime];
  178. }
  179. if (!empty($_GET['export'])) {
  180. $this->ProviderAmountExport($condition);
  181. return;
  182. }
  183. $provider_list = $this->providers();
  184. foreach ($provider_list as $provider) {
  185. $providers[$provider['provider_id']] = $provider;
  186. }
  187. $mod = Model('provider_amount');
  188. $list = $mod->getAmountList($condition,50,'*', 'add_time desc', '', true);
  189. foreach ($list as $key => $value) {
  190. $list[$key]['provider_name'] = $providers[$value['provider_id']]['name'];
  191. $list[$key]['store_name'] = $providers[$value['provider_id']]['store_name'];
  192. }
  193. $stat = Model()->table('provider_amount')
  194. ->where($condition)
  195. ->field('sum(amount) as amounts')
  196. ->find();
  197. Tpl::output('stat', $stat);
  198. Tpl::output('provider_list', $providers);
  199. Tpl::output('show_page', $mod->showpage());
  200. Tpl::output('info_list', $list);
  201. Tpl::showpage('provider.amount');
  202. }
  203. public function sync_cfgs()
  204. {
  205. $name_val = function ($items) {
  206. $result = [];
  207. foreach ($items as $item) {
  208. $name = $item['name'];
  209. $result[$name] = $item;
  210. }
  211. return $result;
  212. };
  213. $match = function ($all, $cur) {
  214. $inserts = $updates = [];
  215. foreach ($all as $key => $value) {
  216. if (!array_key_exists($key, $cur)) {
  217. $insert['name'] = $key;
  218. $insert['store_id'] = $value['cfg']['store_id'];
  219. $insert['qualitys'] = $value['cfg']['qualitys'];
  220. $inserts[] = $insert;
  221. } elseif ($value['cfg']['qualitys'] != $cur[$key]['qualitys']) {
  222. $update['provider_id'] = $cur[$key]['provider_id'];
  223. $update['qualitys'] = $value['cfg']['qualitys'];
  224. $updates[] = $update;
  225. }
  226. }
  227. return [$inserts, $updates];
  228. };
  229. $updater = function ($mod, $updates) {
  230. if (empty($updates)) return;
  231. foreach ($updates as $update) {
  232. $provider_id = $update['provider_id'];
  233. $data = ['qualitys' => $update['qualitys']];
  234. $mod->editProvider($data, ['provider_id' => $provider_id]);
  235. }
  236. };
  237. $inserter = function ($mod, $type, $names)
  238. {
  239. $model_member = Model('member');
  240. foreach ($names as $name) {
  241. $insert_member = [];
  242. $insert_member['member_name'] = "{$name['name']}-refill";
  243. $insert_member['member_passwd'] = md5($name['name']);
  244. $insert_id = $model_member->addMember($insert_member);
  245. $helper = new refill\divert_account();
  246. $helper->init_member($insert_id);
  247. $data = ['name' => $name['name'], 'type' => $type, 'store_id' => $name['store_id'], 'qualitys' => $name['qualitys'], 'opened' => 2, 'account_id' => $insert_id];
  248. $mod->insert($data);
  249. }
  250. };
  251. global $config;
  252. $oil_configs = $config['oil_providers'];
  253. $pho_configs = $config['phone_providers'];
  254. $third_configs = $config['third_providers'];
  255. $oils = $name_val($oil_configs);
  256. $phones = $name_val($pho_configs);
  257. $third = $name_val($third_configs);
  258. $mod_prov = Model('refill_provider');
  259. $oil_items = $mod_prov->getProviderList(['type' => 1]);
  260. $oil_items = $name_val($oil_items);
  261. [$oil_inserts, $oil_updates] = $match($oils, $oil_items);
  262. $phone_items = $mod_prov->getProviderList(['type' => 2]);
  263. $phone_items = $name_val($phone_items);
  264. [$phone_inserts, $phone_updates] = $match($phones, $phone_items);
  265. $third_items = $mod_prov->getProviderList(['type' => 3]);
  266. $third_items = $name_val($third_items);
  267. [$third_inserts, $third_updates] = $match($third, $third_items);
  268. $inserter($mod_prov, 1, $oil_inserts);
  269. $inserter($mod_prov, 2, $phone_inserts);
  270. $inserter($mod_prov, 3, $third_inserts);
  271. $updater($mod_prov, $oil_updates);
  272. $updater($mod_prov, $phone_updates);
  273. $updater($mod_prov, $third_updates);
  274. $chprice_helper = new refill\chprice_helper();
  275. $chprice_helper->update_from_file();
  276. }
  277. private function ProviderAmountExport($cond)
  278. {
  279. $result = Model('provider_amount')->getAllDatas($cond);
  280. $this->createExcel($result);
  281. }
  282. private function createExcel($data = [])
  283. {
  284. if(empty($data)) {
  285. showMessage('数据为空');
  286. }
  287. Language::read('export');
  288. import('libraries.excel');
  289. $provider_list = $this->providers();
  290. $providers = [];
  291. foreach ($provider_list as $provider) {
  292. $providers[$provider['provider_id']] = $provider;
  293. }
  294. $excel_obj = new Excel();
  295. $excel_data = [];
  296. //设置样式
  297. $excel_obj->setStyle(['id' => 's_title', 'Font' => ['FontName' => '宋体', 'Size' => '12', 'Bold' => '1']]);
  298. //header
  299. $excel_data[0][] = ['styleid' => 's_title', 'data' => '序号'];
  300. $excel_data[0][] = ['styleid' => 's_title', 'data' => '通道名称'];
  301. $excel_data[0][] = ['styleid' => 's_title', 'data' => '调款金额'];
  302. $excel_data[0][] = ['styleid' => 's_title', 'data' => '操作日期'];
  303. $excel_data[0][] = ['styleid' => 's_title', 'data' => '操作人'];
  304. //data
  305. foreach ((array)$data as $k => $v) {
  306. $tmp = [];
  307. $provider_id = intval($v['provider_id']);
  308. $provider = $providers[$provider_id];
  309. $store_name = "{$provider['name']}({$provider['store_name']})";
  310. $tmp[] = ['data' => $k+1];
  311. $tmp[] = ['data' => $store_name];
  312. $tmp[] = ['data' => $v['amount']];
  313. if (empty($v['add_time'])) {
  314. $tmp[] = ['data' => ''];
  315. } else {
  316. $tmp[] = ['data' => date('Y-m-d H:i:s', $v['add_time'])];
  317. }
  318. $tmp[] = ['data' => $v['operation']];
  319. $excel_data[] = $tmp;
  320. }
  321. $excel_data = $excel_obj->charset($excel_data, CHARSET);
  322. $excel_obj->addArray($excel_data);
  323. $excel_obj->addWorksheet($excel_obj->charset(L('exp_od_order'), CHARSET));
  324. $excel_obj->generateXML('通道调款记录导出-' . date('Y-m-d-H', time()));
  325. }
  326. public function file_syncOp()
  327. {
  328. $chprice_helper = new refill\chprice_helper();
  329. $chprice_helper->update_from_file();
  330. showMessage('操作完成');
  331. }
  332. public function refresh_priceOp()
  333. {
  334. $publisher = new message\publisher();
  335. $publisher->modify_refill_channel();
  336. showMessage('操作完成');
  337. }
  338. public function provider_priceOp()
  339. {
  340. $provider_id = $_GET['provider_id'];
  341. $mod_provider = Model('refill_provider');
  342. $mod_price = Model('provider_price');
  343. $provider = $mod_provider->getProviderInfo(['provider_id' => $provider_id]);
  344. $provider_name = $provider['name'];
  345. $type = $provider['type'];
  346. $price = $mod_price->getPirce(['provider_name' => $provider_name,'product_type' => $type]);
  347. $cfg = $this->price_cfg($price);
  348. if(empty($provider)) {
  349. showMessage('通道不存在');
  350. }
  351. if(chksubmit()){
  352. $admininfo = $this->getAdminInfo();
  353. $new_cfg = $this->cfg_format($_POST, $cfg);
  354. $effect_time = $_POST['effect_time'];
  355. if(!empty($effect_time))
  356. {
  357. $effect_time = strtotime($effect_time);
  358. if($effect_time < time()) {
  359. $effect_time = 0;
  360. }
  361. }
  362. else {
  363. $effect_time = 0;
  364. }
  365. $chprice_helper = new refill\chprice_helper();
  366. $ret = $chprice_helper->update($price['price_id'], $new_cfg, $effect_time, $admininfo['name']);
  367. if($effect_time > 0) {
  368. QueueClient::async_push('AysncChangePrice', ['price_id' => $price['price_id']], $effect_time - time());
  369. }
  370. if($ret) {
  371. showMessage('编辑成功');
  372. }else{
  373. showMessage('编辑失败');
  374. }
  375. }
  376. else
  377. {
  378. global $config;
  379. $all_specs = $this->all_specs($cfg['qualitys'], $type);
  380. $amounts = $this->map_cfg($cfg,$all_specs);
  381. $quality_text = $price['product_type'] == mtopcard\OilCardPaper ? $config['oil_quality_text'] : $config['phone_quality_text'];
  382. $quality_text = $quality_text[$cfg['qualitys']];
  383. Tpl::output('provider_name', $cfg['name']);
  384. Tpl::output('product_type', $price['product_type']);
  385. Tpl::output('quality_text', $quality_text);
  386. Tpl::output('quality', $cfg['qualitys']);
  387. Tpl::output('effect_time', $price['effect_time']);
  388. Tpl::output('amounts', $amounts);
  389. Tpl::output('all_specs', $all_specs);
  390. Tpl::showpage('provider.price.set');
  391. }
  392. }
  393. private function price_cfg($price)
  394. {
  395. $cfg = json_decode($price['cfg'], true);
  396. $new_cfg = $price['new_cfg'];
  397. if(!empty($new_cfg))
  398. {
  399. $cfg = json_decode($new_cfg, true);
  400. }
  401. return $cfg;
  402. }
  403. private function card_types($stypes)
  404. {
  405. $result = [];
  406. $types = explode(',',$stypes);
  407. foreach ($types as $stype) {
  408. $type = mtopcard\topcard_type($stype);
  409. $result[] = $type;
  410. }
  411. return $result;
  412. }
  413. private function map_cfg($cfg,$all_specs)
  414. {
  415. $amounts = $cfg['amount'] ?? [];
  416. $amount_list = [];
  417. foreach ($amounts as $spec => $goods)
  418. {
  419. foreach ($goods as $gitem)
  420. {
  421. $types = $this->card_types($gitem['card_type']);
  422. foreach ($types as $type) {
  423. $amount_list[$type][$spec] = $gitem['price'] ?? 0;
  424. }
  425. }
  426. }
  427. $result = [];
  428. foreach ($all_specs as $card_type => $val)
  429. {
  430. $specs = $val['specs'];
  431. foreach ($specs as $spec)
  432. {
  433. $result[$card_type][$spec] = ['price' => 0,'opened' => 0];
  434. }
  435. }
  436. foreach ($amount_list as $card_type => $prices)
  437. {
  438. foreach ($prices as $spec => $price)
  439. {
  440. if (array_key_exists($card_type, $result) && array_key_exists($spec, $result[$card_type])) {
  441. $result[$card_type][$spec]['price'] = $price;
  442. $result[$card_type][$spec]['opened'] = 1;
  443. }
  444. }
  445. }
  446. return $result;
  447. }
  448. private function all_specs($qualitys, $type): array
  449. {
  450. global $config;
  451. if ($type == mtopcard\OilCardPaper) {
  452. $refill_specs = [
  453. 'petrochina' => $config['refill_oil_specs'],
  454. 'sinopec' => $config['refill_oil_specs']
  455. ];
  456. } elseif ($type == mtopcard\PhoneCardPaper) {
  457. $refill_specs = [
  458. 'chinamobile' => $config['refill_phone_specs'],
  459. 'chinaunicom' => $config['refill_phone_specs'],
  460. 'chinatelecom' => $config['refill_phone_specs']
  461. ];
  462. } else {
  463. return [];
  464. }
  465. $all_type_specs = [];
  466. foreach ($refill_specs as $scard_type => $specs) {
  467. $data = [];
  468. $card_type = mtopcard\topcard_type($scard_type);
  469. if (in_array($card_type, [mtopcard\ChinaMobileCard, mtopcard\ChinaUnicomCard, mtopcard\ChinaTelecomCard])) {
  470. if (in_array(refill\Quality::Quick, explode(',', $qualitys))) {
  471. $specs = array_merge($config['refill_phone_small_specs'], $specs);
  472. }
  473. }
  474. $data['goods_name'] = $this->scard_type($card_type);
  475. $data['specs'] = $specs;
  476. $all_type_specs[$card_type] = $data;
  477. }
  478. return $all_type_specs;
  479. }
  480. private function cfg_format($input, $cfg)
  481. {
  482. foreach ($cfg['amount'] as $amount => $amt_items)
  483. {
  484. $new_amount = [];
  485. foreach ($amt_items as $item){
  486. $goods_id = $item['goods_id'];
  487. $quality = $item['quality'];
  488. $card_types = $this->card_types($item['card_type']);
  489. foreach ($card_types as $card_type)
  490. {
  491. $key = "price-{$card_type}-{$amount}";
  492. $arr = [
  493. 'goods_id' => $goods_id,
  494. 'price' => floatval(ncPriceFormat($input[$key])),
  495. 'quality' => $quality,
  496. 'card_type' => mtopcard\scard_type($card_type)
  497. ];
  498. $new_amount[] = $arr;
  499. }
  500. }
  501. $cfg['amount'][$amount] = $new_amount;
  502. }
  503. return $cfg;
  504. }
  505. }