provider.php 19 KB

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