provider.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736
  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. if(!empty($_GET['export'])) {
  27. $condition['opened'] = 1;
  28. }
  29. $refill_company = $this->refill_companys(['co_type' => refill_companyModel::co_type_provider]);
  30. $provider_items = $provider_model->table('refill_provider,store')
  31. ->field('refill_provider.*,store.store_name,store.member_id')
  32. ->join('inner')
  33. ->on('store.store_id=refill_provider.store_id')
  34. ->where($condition)
  35. ->order('opened asc, name asc')
  36. ->page(200)
  37. ->select();
  38. $providers = [];
  39. $mid_pids = [];
  40. $mids = [];
  41. foreach ($provider_items as $provider)
  42. {
  43. $pid = $provider['provider_id'];
  44. $providers[$pid] = $provider;
  45. $providers[$pid]['available_predeposit'] = 0;
  46. $quality_text = $provider['type'] == mtopcard\OilCardPaper ? $config['oil_quality_text'] : $config['phone_quality_text'];
  47. $providers[$pid]['quality_text'] = $quality_text[$provider['qualitys']];
  48. $providers[$pid]['co_name'] = $refill_company[$provider['co_id']]['co_name'] ?? '/';
  49. $account_id = intval($provider['account_id']);
  50. if($account_id > 0) {
  51. $mid_pids[$account_id] = $pid;
  52. $mids[] = $account_id;
  53. }
  54. }
  55. $debt_total = 0;
  56. $available_total = 0;
  57. if(!empty($mids))
  58. {
  59. $member_data = Model('member')->field('member_id,available_predeposit')->where(['member_id' => ['in',$mids]])->select();
  60. foreach ($member_data as $member)
  61. {
  62. $mid = intval($member['member_id']);
  63. if(array_key_exists($mid,$mid_pids)) {
  64. $pid = $mid_pids[$mid];
  65. $providers[$pid]['available_predeposit'] = $member['available_predeposit'];
  66. if($providers[$pid]['opened'] != 1) continue;
  67. if ($member['available_predeposit'] >= 0) {
  68. $available_total += $member['available_predeposit'];
  69. } else {
  70. $debt_total += $member['available_predeposit'];
  71. }
  72. }
  73. }
  74. }
  75. if(!empty($_GET['export']))
  76. {
  77. $this->provider_export($providers);
  78. return;
  79. }
  80. $stats = ['available_total' => $available_total, 'debt_total' => $debt_total];
  81. $opened_text = ['使用中', '已禁用'];
  82. $type_text = ['油卡', '手机充值卡', '增值业务'];
  83. Tpl::output('opened_text', $opened_text);
  84. Tpl::output('type_text', $type_text);
  85. Tpl::output('provider_list', $providers);
  86. Tpl::output('stats', $stats);
  87. Tpl::output('prices', $this->all_price());
  88. Tpl::output('show_page', $provider_model->showpage());
  89. Tpl::showpage('provider.index');
  90. }
  91. private function all_price()
  92. {
  93. $result = [];
  94. $mod_price = Model('provider_price');
  95. $prices = $mod_price->getPirces(['price_id' => ['gt', 0]]);
  96. foreach ($prices as $price)
  97. {
  98. $key = "{$price['provider_name']}-{$price['product_type']}";
  99. if(!empty($price['effect_time']))
  100. {
  101. $time = '<span style="color: #0bb20c">' . date("Y-m-d H:i:s", $price['effect_time']) . '</span>';
  102. }else{
  103. $time = date("Y-m-d H:i:s", $price['update_time']);
  104. }
  105. $result[$key] = $time;
  106. }
  107. return $result;
  108. }
  109. public function recharge_manualOp()
  110. {
  111. $provider_id = $_GET['provider_id'] ?? $_POST['provider_id'];
  112. $provider_model = Model('refill_provider');
  113. $provider_info = $provider_model->getProviderInfo(['provider_id' => $provider_id]);
  114. if (empty($provider_info)) {
  115. showMessage('通道信息有误');
  116. }
  117. if (chksubmit())
  118. {
  119. $obj_validate = new Validator();
  120. $obj_validate->validateparam = [
  121. ["input" => $_POST["operation"], "require" => "true", "message" => '操作人姓名不能为空'],
  122. ["input" => $_POST["pointsnum"], "require" => "true", "message" => '预存金额不能为空']
  123. ];
  124. $operatetype = $_POST['operatetype'];
  125. $pointsnum = $_POST['pointsnum'];
  126. $money = abs($pointsnum);
  127. if ($money == 0) {
  128. showMessage('金额错误');
  129. }
  130. try {
  131. $model_merchant = Model('merchant');
  132. $trans = new trans_wapper($model_merchant, __METHOD__);
  133. $member_id = $provider_info['account_id'];
  134. if ($operatetype == 'add') {
  135. $bz = "管理员调款操作,手动增加通道余额";
  136. $this->credit_save_money($money, 'add', $member_id, $bz);
  137. $_POST['pointsnum'] = $money;
  138. } elseif ($operatetype == 'del') {
  139. $bz = "管理员调款操作,手动减少通道余额";
  140. $this->credit_save_money($money, 'del', $member_id, $bz);
  141. $_POST['pointsnum'] = -($money);
  142. } else {
  143. showMessage('预存类型错误');
  144. }
  145. $result = $this->ct_provider_amount($_POST, $provider_info);
  146. if (!$result) {
  147. $trans->rollback();
  148. showMessage('操作失败');
  149. }
  150. $trans->commit();
  151. showMessage('操作成功', 'index.php?act=provider&op=index');
  152. } catch (Exception $e) {
  153. $trans->rollback();
  154. showMessage('操作失败');
  155. }
  156. }
  157. else
  158. {
  159. Tpl::output('provider', $provider_info);
  160. Tpl::showpage('provider.recharge');
  161. }
  162. }
  163. private function ct_provider_amount($params, $provider_info)
  164. {
  165. $input['provider_id'] = $provider_info['provider_id'];
  166. $input['memeber_id'] = $provider_info['account_id'];
  167. $input['amount'] = $params['pointsnum'];
  168. $input['operation'] = $params['operation'];
  169. $input['add_time'] = time();
  170. $input['bz'] = $params['bz'] ?? '';
  171. return Model('provider_amount')->addAmount($input);
  172. }
  173. public function provider_amountOp()
  174. {
  175. $condition = [];
  176. if(!empty($_GET['provider_id'])) {
  177. $condition['provider_id'] = $_GET['provider_id'];
  178. }
  179. $start_unixtime = intval(strtotime($_GET['query_start_time']));
  180. $end_unixtime = intval(strtotime($_GET['query_end_time']));
  181. if ($start_unixtime > 0 && $end_unixtime > $start_unixtime) {
  182. $condition['add_time'] = [['egt', $start_unixtime], ['lt', $end_unixtime], 'and'];
  183. } elseif ($start_unixtime > 0) {
  184. $condition['add_time'] = ['egt', $start_unixtime];
  185. } elseif ($end_unixtime > 0) {
  186. $condition['add_time'] = ['lt', $end_unixtime];
  187. }
  188. if (!empty($_GET['export'])) {
  189. $this->ProviderAmountExport($condition);
  190. return;
  191. }
  192. $provider_list = $this->providers();
  193. foreach ($provider_list as $provider) {
  194. $providers[$provider['provider_id']] = $provider;
  195. }
  196. $mod = Model('provider_amount');
  197. $list = $mod->getAmountList($condition,50,'provider_amount.*,refill_provider.name', 'provider_amount.add_time desc, refill_provider.name asc', '', true);
  198. foreach ($list as $key => $value) {
  199. $list[$key]['store_name'] = $providers[$value['provider_id']]['store_name'];
  200. }
  201. $stat = Model()->table('provider_amount')
  202. ->where($condition)
  203. ->field('sum(amount) as amounts')
  204. ->find();
  205. Tpl::output('stat', $stat);
  206. Tpl::output('provider_list', $providers);
  207. Tpl::output('show_page', $mod->showpage());
  208. Tpl::output('info_list', $list);
  209. Tpl::showpage('provider.amount');
  210. }
  211. public function sync_cfgs()
  212. {
  213. $name_val = function ($items) {
  214. $result = [];
  215. foreach ($items as $item) {
  216. $name = $item['name'];
  217. $result[$name] = $item;
  218. }
  219. return $result;
  220. };
  221. $match = function ($all, $cur) {
  222. $inserts = $updates = [];
  223. foreach ($all as $key => $value) {
  224. if (!array_key_exists($key, $cur)) {
  225. $insert['name'] = $key;
  226. $insert['store_id'] = $value['cfg']['store_id'];
  227. $insert['qualitys'] = $value['cfg']['qualitys'];
  228. $inserts[] = $insert;
  229. } elseif ($value['cfg']['qualitys'] != $cur[$key]['qualitys']) {
  230. $update['provider_id'] = $cur[$key]['provider_id'];
  231. $update['qualitys'] = $value['cfg']['qualitys'];
  232. $updates[] = $update;
  233. }
  234. }
  235. return [$inserts, $updates];
  236. };
  237. $updater = function ($mod, $updates) {
  238. if (empty($updates)) return;
  239. foreach ($updates as $update) {
  240. $provider_id = $update['provider_id'];
  241. $data = ['qualitys' => $update['qualitys']];
  242. $mod->editProvider($data, ['provider_id' => $provider_id]);
  243. }
  244. };
  245. $inserter = function ($mod, $type, $names)
  246. {
  247. $model_member = Model('member');
  248. foreach ($names as $name) {
  249. $insert_member = [];
  250. $insert_member['member_name'] = "{$name['name']}-refill";
  251. $insert_member['member_passwd'] = md5($name['name']);
  252. $insert_id = $model_member->addMember($insert_member);
  253. $helper = new refill\divert_account();
  254. $helper->init_member($insert_id);
  255. $data = ['name' => $name['name'], 'type' => $type, 'store_id' => $name['store_id'], 'qualitys' => $name['qualitys'], 'opened' => 2, 'account_id' => $insert_id];
  256. $mod->insert($data);
  257. }
  258. };
  259. global $config;
  260. $oil_configs = $config['oil_providers'];
  261. $pho_configs = $config['phone_providers'];
  262. $third_configs = $config['third_providers'];
  263. $oils = $name_val($oil_configs);
  264. $phones = $name_val($pho_configs);
  265. $third = $name_val($third_configs);
  266. $mod_prov = Model('refill_provider');
  267. $oil_items = $mod_prov->getProviderList(['type' => 1]);
  268. $oil_items = $name_val($oil_items);
  269. [$oil_inserts, $oil_updates] = $match($oils, $oil_items);
  270. $phone_items = $mod_prov->getProviderList(['type' => 2]);
  271. $phone_items = $name_val($phone_items);
  272. [$phone_inserts, $phone_updates] = $match($phones, $phone_items);
  273. $third_items = $mod_prov->getProviderList(['type' => 3]);
  274. $third_items = $name_val($third_items);
  275. [$third_inserts, $third_updates] = $match($third, $third_items);
  276. $inserter($mod_prov, 1, $oil_inserts);
  277. $inserter($mod_prov, 2, $phone_inserts);
  278. $inserter($mod_prov, 3, $third_inserts);
  279. $updater($mod_prov, $oil_updates);
  280. $updater($mod_prov, $phone_updates);
  281. $updater($mod_prov, $third_updates);
  282. $chprice_helper = new refill\chprice_helper();
  283. $chprice_helper->update_from_file();
  284. }
  285. private function ProviderAmountExport($cond)
  286. {
  287. $result = Model('provider_amount')->getAllDatas($cond);
  288. $this->createExcel($result);
  289. }
  290. private function createExcel($data = [])
  291. {
  292. if(empty($data)) {
  293. showMessage('数据为空');
  294. }
  295. Language::read('export');
  296. import('libraries.excel');
  297. $provider_list = $this->providers();
  298. $providers = [];
  299. foreach ($provider_list as $provider) {
  300. $providers[$provider['provider_id']] = $provider;
  301. }
  302. $excel_obj = new Excel();
  303. $excel_data = [];
  304. //设置样式
  305. $excel_obj->setStyle(['id' => 's_title', 'Font' => ['FontName' => '宋体', 'Size' => '12', 'Bold' => '1']]);
  306. //header
  307. $excel_data[0][] = ['styleid' => 's_title', 'data' => '序号'];
  308. $excel_data[0][] = ['styleid' => 's_title', 'data' => '通道名称'];
  309. $excel_data[0][] = ['styleid' => 's_title', 'data' => '调款金额'];
  310. $excel_data[0][] = ['styleid' => 's_title', 'data' => '操作日期'];
  311. $excel_data[0][] = ['styleid' => 's_title', 'data' => '操作人'];
  312. //data
  313. foreach ((array)$data as $k => $v) {
  314. $tmp = [];
  315. $provider_id = intval($v['provider_id']);
  316. $provider = $providers[$provider_id];
  317. $store_name = "{$provider['name']}({$provider['store_name']})";
  318. $tmp[] = ['data' => $k+1];
  319. $tmp[] = ['data' => $store_name];
  320. $tmp[] = ['data' => $v['amount']];
  321. if (empty($v['add_time'])) {
  322. $tmp[] = ['data' => ''];
  323. } else {
  324. $tmp[] = ['data' => date('Y-m-d H:i:s', $v['add_time'])];
  325. }
  326. $tmp[] = ['data' => $v['operation']];
  327. $excel_data[] = $tmp;
  328. }
  329. $excel_data = $excel_obj->charset($excel_data, CHARSET);
  330. $excel_obj->addArray($excel_data);
  331. $excel_obj->addWorksheet($excel_obj->charset(L('exp_od_order'), CHARSET));
  332. $excel_obj->generateXML('通道调款记录导出-' . date('Y-m-d-H', time()));
  333. }
  334. public function file_syncOp()
  335. {
  336. $this->sync_cfgs();
  337. showMessage('操作完成');
  338. }
  339. public function refresh_priceOp()
  340. {
  341. $publisher = new message\publisher();
  342. $publisher->modify_refill_channel();
  343. showMessage('操作完成');
  344. }
  345. public function provider_priceOp()
  346. {
  347. $provider_id = $_GET['provider_id'];
  348. $mod_provider = Model('refill_provider');
  349. $mod_price = Model('provider_price');
  350. $provider = $mod_provider->getProviderInfo(['provider_id' => $provider_id]);
  351. $provider_name = $provider['name'];
  352. $type = $provider['type'];
  353. $price = $mod_price->getPirce(['provider_name' => $provider_name,'product_type' => $type]);
  354. $cfg = $this->price_cfg($price);
  355. if(empty($provider)) {
  356. showMessage('通道不存在');
  357. }
  358. if(chksubmit()){
  359. $admininfo = $this->getAdminInfo();
  360. $new_cfg = $this->cfg_format($_POST, $cfg);
  361. $effect_time = $_POST['effect_time'];
  362. if(!empty($effect_time))
  363. {
  364. $effect_time = strtotime($effect_time);
  365. if($effect_time < time()) {
  366. $effect_time = 0;
  367. }
  368. }
  369. else {
  370. $effect_time = 0;
  371. }
  372. $chprice_helper = new refill\chprice_helper();
  373. $ret = $chprice_helper->update($price['price_id'], $new_cfg, $effect_time, $admininfo['name']);
  374. if($effect_time > 0) {
  375. QueueClient::async_push('AysncChangePrice', ['price_id' => $price['price_id']], $effect_time - time());
  376. }
  377. if($ret) {
  378. showMessage('编辑成功');
  379. }else{
  380. showMessage('编辑失败');
  381. }
  382. }
  383. else
  384. {
  385. global $config;
  386. $all_specs = $this->all_specs($cfg['qualitys'], $type);
  387. $amounts = $this->map_cfg($cfg,$all_specs);
  388. $quality_text = $price['product_type'] == mtopcard\OilCardPaper ? $config['oil_quality_text'] : $config['phone_quality_text'];
  389. $quality_text = $quality_text[$cfg['qualitys']];
  390. Tpl::output('provider_name', $cfg['name']);
  391. Tpl::output('product_type', $price['product_type']);
  392. Tpl::output('quality_text', $quality_text);
  393. Tpl::output('quality', $cfg['qualitys']);
  394. Tpl::output('effect_time', $price['effect_time']);
  395. Tpl::output('amounts', $amounts);
  396. Tpl::output('all_specs', $all_specs);
  397. Tpl::showpage('provider.price.set');
  398. }
  399. }
  400. private function price_cfg($price)
  401. {
  402. $cfg = json_decode($price['cfg'], true);
  403. $new_cfg = $price['new_cfg'];
  404. if(!empty($new_cfg))
  405. {
  406. $cfg = json_decode($new_cfg, true);
  407. }
  408. return $cfg;
  409. }
  410. private function card_types($stypes)
  411. {
  412. $result = [];
  413. $types = explode(',',$stypes);
  414. foreach ($types as $stype) {
  415. $type = mtopcard\topcard_type($stype);
  416. $result[] = $type;
  417. }
  418. return $result;
  419. }
  420. private function map_cfg($cfg,$all_specs)
  421. {
  422. $amounts = $cfg['amount'] ?? [];
  423. $amount_list = [];
  424. foreach ($amounts as $spec => $goods)
  425. {
  426. foreach ($goods as $gitem)
  427. {
  428. $types = $this->card_types($gitem['card_type']);
  429. foreach ($types as $type) {
  430. $amount_list[$type][$spec] = $gitem['price'] ?? 0;
  431. }
  432. }
  433. }
  434. $result = [];
  435. foreach ($all_specs as $card_type => $val)
  436. {
  437. $specs = $val['specs'];
  438. foreach ($specs as $spec)
  439. {
  440. $result[$card_type][$spec] = ['price' => 0,'opened' => 0];
  441. }
  442. }
  443. foreach ($amount_list as $card_type => $prices)
  444. {
  445. foreach ($prices as $spec => $price)
  446. {
  447. if (array_key_exists($card_type, $result) && array_key_exists($spec, $result[$card_type])) {
  448. $result[$card_type][$spec]['price'] = $price;
  449. $result[$card_type][$spec]['opened'] = 1;
  450. }
  451. }
  452. }
  453. return $result;
  454. }
  455. private function all_specs($qualitys, $type): array
  456. {
  457. global $config;
  458. if ($type == mtopcard\OilCardPaper) {
  459. $refill_specs = [
  460. 'petrochina' => $config['refill_oil_specs'],
  461. 'sinopec' => $config['refill_oil_specs']
  462. ];
  463. } elseif ($type == mtopcard\PhoneCardPaper) {
  464. $refill_specs = [
  465. 'chinamobile' => $config['refill_phone_specs'],
  466. 'chinaunicom' => $config['refill_phone_specs'],
  467. 'chinatelecom' => $config['refill_phone_specs']
  468. ];
  469. } else {
  470. return [];
  471. }
  472. $all_type_specs = [];
  473. foreach ($refill_specs as $scard_type => $specs) {
  474. $data = [];
  475. $card_type = mtopcard\topcard_type($scard_type);
  476. if (in_array($card_type, [mtopcard\ChinaMobileCard, mtopcard\ChinaUnicomCard, mtopcard\ChinaTelecomCard])) {
  477. if (in_array(refill\Quality::Quick, explode(',', $qualitys))) {
  478. $specs = array_merge($config['refill_phone_small_specs'], $specs);
  479. }
  480. }
  481. $data['goods_name'] = $this->scard_type($card_type);
  482. $data['specs'] = $specs;
  483. $all_type_specs[$card_type] = $data;
  484. }
  485. return $all_type_specs;
  486. }
  487. private function cfg_format($input, $cfg)
  488. {
  489. foreach ($cfg['amount'] as $amount => $amt_items)
  490. {
  491. $new_amount = [];
  492. foreach ($amt_items as $item){
  493. $goods_id = $item['goods_id'];
  494. $quality = $item['quality'];
  495. $card_types = $this->card_types($item['card_type']);
  496. foreach ($card_types as $card_type)
  497. {
  498. $key = "price-{$card_type}-{$amount}";
  499. $arr = [
  500. 'goods_id' => $goods_id,
  501. 'price' => floatval(ncPriceFormat($input[$key])),
  502. 'quality' => $quality,
  503. 'card_type' => mtopcard\scard_type($card_type)
  504. ];
  505. $new_amount[] = $arr;
  506. }
  507. }
  508. $cfg['amount'][$amount] = $new_amount;
  509. }
  510. return $cfg;
  511. }
  512. public function provider_amount_editOp()
  513. {
  514. $amount_id = $_GET['amount_id'];
  515. $mod = Model('provider_amount');
  516. $provider_amount = $mod->where(['id' => $amount_id])->find();
  517. if(empty($provider_amount)) {
  518. showMessage('记录不存在');
  519. }
  520. if(chksubmit())
  521. {
  522. $bz = $_POST['bz'];
  523. $resp = $mod->where(['id' => $amount_id])->update(['bz' => $bz]);
  524. if($resp) {
  525. showMessage('编辑成功');
  526. }else{
  527. showMessage('编辑失败');
  528. }
  529. }
  530. else
  531. {
  532. $provider = Model('')->table('refill_provider,store')
  533. ->field('refill_provider.*,store.store_name')
  534. ->join('inner')
  535. ->on('store.store_id=refill_provider.store_id')
  536. ->where(['provider_id' => $provider_amount['provider_id']])
  537. ->find();
  538. Tpl::output('provider', $provider);
  539. Tpl::output('provider_amount', $provider_amount);
  540. Tpl::showpage('provider.amount.edit');
  541. }
  542. }
  543. public function provider_export($providers)
  544. {
  545. Language::read('export');
  546. import('libraries.excel');
  547. $excel_obj = new Excel();
  548. $excel_data = [];
  549. //设置样式
  550. $excel_obj->setStyle(['id' => 's_title', 'Font' => ['FontName' => '宋体', 'Size' => '12', 'Bold' => '1']]);
  551. //header
  552. $excel_data[0][] = ['styleid' => 's_title', 'data' => '通道ID'];
  553. $excel_data[0][] = ['styleid' => 's_title', 'data' => '通道名称'];
  554. $excel_data[0][] = ['styleid' => 's_title', 'data' => '订单成功后余额'];
  555. $excel_data[0][] = ['styleid' => 's_title', 'data' => '接口查询余额'];
  556. //data
  557. foreach ($providers as $provider) {
  558. $tmp = [];
  559. $store_name = "{$provider['name']}({$provider['store_name']})";
  560. $tmp[] = ['data' => $provider['provider_id']];
  561. $tmp[] = ['data' => $store_name];
  562. $tmp[] = ['data' => $provider['available_predeposit']];
  563. $tmp[] = ['data' => $provider['balance']];
  564. $excel_data[] = $tmp;
  565. }
  566. $excel_data = $excel_obj->charset($excel_data, CHARSET);
  567. $excel_obj->addArray($excel_data);
  568. $excel_obj->addWorksheet($excel_obj->charset(L('exp_od_order'), CHARSET));
  569. $excel_obj->generateXML('通道余额明细导出-' . date('Y-m-d-H', time()));
  570. }
  571. public function provider_remitOp()
  572. {
  573. $refill_company = $this->refill_companys(['co_type' => refill_companyModel::co_type_provider]);
  574. if (chksubmit())
  575. {
  576. $remits = $_POST['remits'];
  577. $strs = $_POST['strs'];
  578. $operation = $_POST['operation'];
  579. $title = '';
  580. $remit_total = 0;
  581. foreach ($strs as $key => $str)
  582. {
  583. $remit = $remits[$key];
  584. if(empty($remit)) continue;
  585. $item = explode('-', $str);
  586. $co_id = $item[0];
  587. $pid = $item[1];
  588. $data[$co_id][$pid] = $remit;
  589. $remit_total += $remit;
  590. }
  591. if(empty($data)) {
  592. showMessage('未提交数据');
  593. }
  594. if(ADMIN_NAME === 'YEZI') {
  595. $platform = '椰子';
  596. }else{
  597. $platform = '椰林';
  598. }
  599. foreach ($data as $co_id => $valuue)
  600. {
  601. $title .= "{$refill_company[$co_id]['co_name']}-";
  602. }
  603. $cur_time = time();
  604. $date = date("YmdHi", $cur_time);
  605. $title = "{$platform}-{$date}";
  606. $remit_record = [
  607. 'title' => $title,
  608. 'amount' => $remit_total,
  609. 'params' => json_encode($data),
  610. 'operation' => $operation,
  611. 'add_time' => $cur_time,
  612. 'oper_time' => $cur_time
  613. ];
  614. Model('refill_company_remit')->addRemit($remit_record);
  615. showMessage('操作成功', 'index.php?act=refill_company&op=remit_record');
  616. }
  617. else
  618. {
  619. $provider_items = $this->providers();
  620. $result = $this->remit_data($provider_items, $refill_company);
  621. Tpl::output('remit_data',$result);
  622. Tpl::showpage('provider.remit');
  623. }
  624. }
  625. private function remit_data($items, $refill_company): array
  626. {
  627. $providers = $mid_pids = $mids = $datas = $result =[];
  628. foreach ($items as $item)
  629. {
  630. $co_id = $item['co_id'];
  631. if(empty($co_id)) continue;
  632. $pid = $item['provider_id'];
  633. $account_id = intval($item['account_id']);
  634. if($account_id > 0) {
  635. $mid_pids[$account_id] = $pid;
  636. $mids[] = $account_id;
  637. }
  638. $item['available_predeposit'] = 0;
  639. $providers[$pid] = $item;
  640. }
  641. if(!empty($mids))
  642. {
  643. $member_data = Model('member')->field('member_id,available_predeposit')->where(['member_id' => ['in',$mids]])->select();
  644. foreach ($member_data as $member)
  645. {
  646. $mid = intval($member['member_id']);
  647. if(array_key_exists($mid,$mid_pids)) {
  648. $pid = $mid_pids[$mid];
  649. $providers[$pid]['available_predeposit'] = $member['available_predeposit'];
  650. }
  651. }
  652. }
  653. foreach ($providers as $provider)
  654. {
  655. $datas[$provider['co_id']][] = $provider;
  656. }
  657. $remit_cfg = $this->remit_cfg();
  658. foreach ($datas as $co_id => $data)
  659. {
  660. $available_total = 0;
  661. foreach ($data as $key => $value)
  662. {
  663. $remit = abs($value['available_predeposit'] - $remit_cfg['remit_money']);
  664. if ($remit > 1000 && $value['available_predeposit'] < $remit_cfg['remit_money']) {
  665. $data[$key]['remit'] = intval($remit - ($remit % 1000));
  666. } else {
  667. $data[$key]['remit'] = 0;
  668. }
  669. $available_total += $value['available_predeposit'];
  670. }
  671. if($available_total > $remit_cfg['remit_money'] || !array_key_exists($co_id, $refill_company)) continue;
  672. $result[] = [
  673. 'co_name' => $refill_company[$co_id]['co_name'],
  674. 'providers' => $data
  675. ];
  676. }
  677. return $result;
  678. }
  679. }