provider.php 21 KB

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