provider.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729
  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,'*', 'add_time desc', '', true);
  198. foreach ($list as $key => $value) {
  199. $list[$key]['provider_name'] = $providers[$value['provider_id']]['name'];
  200. $list[$key]['store_name'] = $providers[$value['provider_id']]['store_name'];
  201. }
  202. $stat = Model()->table('provider_amount')
  203. ->where($condition)
  204. ->field('sum(amount) as amounts')
  205. ->find();
  206. Tpl::output('stat', $stat);
  207. Tpl::output('provider_list', $providers);
  208. Tpl::output('show_page', $mod->showpage());
  209. Tpl::output('info_list', $list);
  210. Tpl::showpage('provider.amount');
  211. }
  212. public function sync_cfgs()
  213. {
  214. $name_val = function ($items) {
  215. $result = [];
  216. foreach ($items as $item) {
  217. $name = $item['name'];
  218. $result[$name] = $item;
  219. }
  220. return $result;
  221. };
  222. $match = function ($all, $cur) {
  223. $inserts = $updates = [];
  224. foreach ($all as $key => $value) {
  225. if (!array_key_exists($key, $cur)) {
  226. $insert['name'] = $key;
  227. $insert['store_id'] = $value['cfg']['store_id'];
  228. $insert['qualitys'] = $value['cfg']['qualitys'];
  229. $inserts[] = $insert;
  230. } elseif ($value['cfg']['qualitys'] != $cur[$key]['qualitys']) {
  231. $update['provider_id'] = $cur[$key]['provider_id'];
  232. $update['qualitys'] = $value['cfg']['qualitys'];
  233. $updates[] = $update;
  234. }
  235. }
  236. return [$inserts, $updates];
  237. };
  238. $updater = function ($mod, $updates) {
  239. if (empty($updates)) return;
  240. foreach ($updates as $update) {
  241. $provider_id = $update['provider_id'];
  242. $data = ['qualitys' => $update['qualitys']];
  243. $mod->editProvider($data, ['provider_id' => $provider_id]);
  244. }
  245. };
  246. $inserter = function ($mod, $type, $names)
  247. {
  248. $model_member = Model('member');
  249. foreach ($names as $name) {
  250. $insert_member = [];
  251. $insert_member['member_name'] = "{$name['name']}-refill";
  252. $insert_member['member_passwd'] = md5($name['name']);
  253. $insert_id = $model_member->addMember($insert_member);
  254. $helper = new refill\divert_account();
  255. $helper->init_member($insert_id);
  256. $data = ['name' => $name['name'], 'type' => $type, 'store_id' => $name['store_id'], 'qualitys' => $name['qualitys'], 'opened' => 2, 'account_id' => $insert_id];
  257. $mod->insert($data);
  258. }
  259. };
  260. global $config;
  261. $oil_configs = $config['oil_providers'];
  262. $pho_configs = $config['phone_providers'];
  263. $third_configs = $config['third_providers'];
  264. $oils = $name_val($oil_configs);
  265. $phones = $name_val($pho_configs);
  266. $third = $name_val($third_configs);
  267. $mod_prov = Model('refill_provider');
  268. $oil_items = $mod_prov->getProviderList(['type' => 1]);
  269. $oil_items = $name_val($oil_items);
  270. [$oil_inserts, $oil_updates] = $match($oils, $oil_items);
  271. $phone_items = $mod_prov->getProviderList(['type' => 2]);
  272. $phone_items = $name_val($phone_items);
  273. [$phone_inserts, $phone_updates] = $match($phones, $phone_items);
  274. $third_items = $mod_prov->getProviderList(['type' => 3]);
  275. $third_items = $name_val($third_items);
  276. [$third_inserts, $third_updates] = $match($third, $third_items);
  277. $inserter($mod_prov, 1, $oil_inserts);
  278. $inserter($mod_prov, 2, $phone_inserts);
  279. $inserter($mod_prov, 3, $third_inserts);
  280. $updater($mod_prov, $oil_updates);
  281. $updater($mod_prov, $phone_updates);
  282. $updater($mod_prov, $third_updates);
  283. $chprice_helper = new refill\chprice_helper();
  284. $chprice_helper->update_from_file();
  285. }
  286. private function ProviderAmountExport($cond)
  287. {
  288. $result = Model('provider_amount')->getAllDatas($cond);
  289. $this->createExcel($result);
  290. }
  291. private function createExcel($data = [])
  292. {
  293. if(empty($data)) {
  294. showMessage('数据为空');
  295. }
  296. Language::read('export');
  297. import('libraries.excel');
  298. $provider_list = $this->providers();
  299. $providers = [];
  300. foreach ($provider_list as $provider) {
  301. $providers[$provider['provider_id']] = $provider;
  302. }
  303. $excel_obj = new Excel();
  304. $excel_data = [];
  305. //设置样式
  306. $excel_obj->setStyle(['id' => 's_title', 'Font' => ['FontName' => '宋体', 'Size' => '12', 'Bold' => '1']]);
  307. //header
  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. $excel_data[0][] = ['styleid' => 's_title', 'data' => '操作人'];
  313. //data
  314. foreach ((array)$data as $k => $v) {
  315. $tmp = [];
  316. $provider_id = intval($v['provider_id']);
  317. $provider = $providers[$provider_id];
  318. $store_name = "{$provider['name']}({$provider['store_name']})";
  319. $tmp[] = ['data' => $k+1];
  320. $tmp[] = ['data' => $store_name];
  321. $tmp[] = ['data' => $v['amount']];
  322. if (empty($v['add_time'])) {
  323. $tmp[] = ['data' => ''];
  324. } else {
  325. $tmp[] = ['data' => date('Y-m-d H:i:s', $v['add_time'])];
  326. }
  327. $tmp[] = ['data' => $v['operation']];
  328. $excel_data[] = $tmp;
  329. }
  330. $excel_data = $excel_obj->charset($excel_data, CHARSET);
  331. $excel_obj->addArray($excel_data);
  332. $excel_obj->addWorksheet($excel_obj->charset(L('exp_od_order'), CHARSET));
  333. $excel_obj->generateXML('通道调款记录导出-' . date('Y-m-d-H', time()));
  334. }
  335. public function file_syncOp()
  336. {
  337. $this->sync_cfgs();
  338. showMessage('操作完成');
  339. }
  340. public function refresh_priceOp()
  341. {
  342. $publisher = new message\publisher();
  343. $publisher->modify_refill_channel();
  344. showMessage('操作完成');
  345. }
  346. public function provider_priceOp()
  347. {
  348. $provider_id = $_GET['provider_id'];
  349. $mod_provider = Model('refill_provider');
  350. $mod_price = Model('provider_price');
  351. $provider = $mod_provider->getProviderInfo(['provider_id' => $provider_id]);
  352. $provider_name = $provider['name'];
  353. $type = $provider['type'];
  354. $price = $mod_price->getPirce(['provider_name' => $provider_name,'product_type' => $type]);
  355. $cfg = $this->price_cfg($price);
  356. if(empty($provider)) {
  357. showMessage('通道不存在');
  358. }
  359. if(chksubmit()){
  360. $admininfo = $this->getAdminInfo();
  361. $new_cfg = $this->cfg_format($_POST, $cfg);
  362. $effect_time = $_POST['effect_time'];
  363. if(!empty($effect_time))
  364. {
  365. $effect_time = strtotime($effect_time);
  366. if($effect_time < time()) {
  367. $effect_time = 0;
  368. }
  369. }
  370. else {
  371. $effect_time = 0;
  372. }
  373. $chprice_helper = new refill\chprice_helper();
  374. $ret = $chprice_helper->update($price['price_id'], $new_cfg, $effect_time, $admininfo['name']);
  375. if($effect_time > 0) {
  376. QueueClient::async_push('AysncChangePrice', ['price_id' => $price['price_id']], $effect_time - time());
  377. }
  378. if($ret) {
  379. showMessage('编辑成功');
  380. }else{
  381. showMessage('编辑失败');
  382. }
  383. }
  384. else
  385. {
  386. global $config;
  387. $all_specs = $this->all_specs($cfg['qualitys'], $type);
  388. $amounts = $this->map_cfg($cfg,$all_specs);
  389. $quality_text = $price['product_type'] == mtopcard\OilCardPaper ? $config['oil_quality_text'] : $config['phone_quality_text'];
  390. $quality_text = $quality_text[$cfg['qualitys']];
  391. Tpl::output('provider_name', $cfg['name']);
  392. Tpl::output('product_type', $price['product_type']);
  393. Tpl::output('quality_text', $quality_text);
  394. Tpl::output('quality', $cfg['qualitys']);
  395. Tpl::output('effect_time', $price['effect_time']);
  396. Tpl::output('amounts', $amounts);
  397. Tpl::output('all_specs', $all_specs);
  398. Tpl::showpage('provider.price.set');
  399. }
  400. }
  401. private function price_cfg($price)
  402. {
  403. $cfg = json_decode($price['cfg'], true);
  404. $new_cfg = $price['new_cfg'];
  405. if(!empty($new_cfg))
  406. {
  407. $cfg = json_decode($new_cfg, true);
  408. }
  409. return $cfg;
  410. }
  411. private function card_types($stypes)
  412. {
  413. $result = [];
  414. $types = explode(',',$stypes);
  415. foreach ($types as $stype) {
  416. $type = mtopcard\topcard_type($stype);
  417. $result[] = $type;
  418. }
  419. return $result;
  420. }
  421. private function map_cfg($cfg,$all_specs)
  422. {
  423. $amounts = $cfg['amount'] ?? [];
  424. $amount_list = [];
  425. foreach ($amounts as $spec => $goods)
  426. {
  427. foreach ($goods as $gitem)
  428. {
  429. $types = $this->card_types($gitem['card_type']);
  430. foreach ($types as $type) {
  431. $amount_list[$type][$spec] = $gitem['price'] ?? 0;
  432. }
  433. }
  434. }
  435. $result = [];
  436. foreach ($all_specs as $card_type => $val)
  437. {
  438. $specs = $val['specs'];
  439. foreach ($specs as $spec)
  440. {
  441. $result[$card_type][$spec] = ['price' => 0,'opened' => 0];
  442. }
  443. }
  444. foreach ($amount_list as $card_type => $prices)
  445. {
  446. foreach ($prices as $spec => $price)
  447. {
  448. if (array_key_exists($card_type, $result) && array_key_exists($spec, $result[$card_type])) {
  449. $result[$card_type][$spec]['price'] = $price;
  450. $result[$card_type][$spec]['opened'] = 1;
  451. }
  452. }
  453. }
  454. return $result;
  455. }
  456. private function all_specs($qualitys, $type): array
  457. {
  458. global $config;
  459. if ($type == mtopcard\OilCardPaper) {
  460. $refill_specs = [
  461. 'petrochina' => $config['refill_oil_specs'],
  462. 'sinopec' => $config['refill_oil_specs']
  463. ];
  464. } elseif ($type == mtopcard\PhoneCardPaper) {
  465. $refill_specs = [
  466. 'chinamobile' => $config['refill_phone_specs'],
  467. 'chinaunicom' => $config['refill_phone_specs'],
  468. 'chinatelecom' => $config['refill_phone_specs']
  469. ];
  470. } else {
  471. return [];
  472. }
  473. $all_type_specs = [];
  474. foreach ($refill_specs as $scard_type => $specs) {
  475. $data = [];
  476. $card_type = mtopcard\topcard_type($scard_type);
  477. if (in_array($card_type, [mtopcard\ChinaMobileCard, mtopcard\ChinaUnicomCard, mtopcard\ChinaTelecomCard])) {
  478. if (in_array(refill\Quality::Quick, explode(',', $qualitys))) {
  479. $specs = array_merge($config['refill_phone_small_specs'], $specs);
  480. }
  481. }
  482. $data['goods_name'] = $this->scard_type($card_type);
  483. $data['specs'] = $specs;
  484. $all_type_specs[$card_type] = $data;
  485. }
  486. return $all_type_specs;
  487. }
  488. private function cfg_format($input, $cfg)
  489. {
  490. foreach ($cfg['amount'] as $amount => $amt_items)
  491. {
  492. $new_amount = [];
  493. foreach ($amt_items as $item){
  494. $goods_id = $item['goods_id'];
  495. $quality = $item['quality'];
  496. $card_types = $this->card_types($item['card_type']);
  497. foreach ($card_types as $card_type)
  498. {
  499. $key = "price-{$card_type}-{$amount}";
  500. $arr = [
  501. 'goods_id' => $goods_id,
  502. 'price' => floatval(ncPriceFormat($input[$key])),
  503. 'quality' => $quality,
  504. 'card_type' => mtopcard\scard_type($card_type)
  505. ];
  506. $new_amount[] = $arr;
  507. }
  508. }
  509. $cfg['amount'][$amount] = $new_amount;
  510. }
  511. return $cfg;
  512. }
  513. public function provider_amount_editOp()
  514. {
  515. $amount_id = $_GET['amount_id'];
  516. $mod = Model('provider_amount');
  517. $provider_amount = $mod->where(['id' => $amount_id])->find();
  518. if(empty($provider_amount)) {
  519. showMessage('记录不存在');
  520. }
  521. if(chksubmit())
  522. {
  523. $bz = $_POST['bz'];
  524. $resp = $mod->where(['id' => $amount_id])->update(['bz' => $bz]);
  525. if($resp) {
  526. showMessage('编辑成功');
  527. }else{
  528. showMessage('编辑失败');
  529. }
  530. }
  531. else
  532. {
  533. $provider = Model('')->table('refill_provider,store')
  534. ->field('refill_provider.*,store.store_name')
  535. ->join('inner')
  536. ->on('store.store_id=refill_provider.store_id')
  537. ->where(['provider_id' => $provider_amount['provider_id']])
  538. ->find();
  539. Tpl::output('provider', $provider);
  540. Tpl::output('provider_amount', $provider_amount);
  541. Tpl::showpage('provider.amount.edit');
  542. }
  543. }
  544. public function provider_export($providers)
  545. {
  546. Language::read('export');
  547. import('libraries.excel');
  548. $excel_obj = new Excel();
  549. $excel_data = [];
  550. //设置样式
  551. $excel_obj->setStyle(['id' => 's_title', 'Font' => ['FontName' => '宋体', 'Size' => '12', 'Bold' => '1']]);
  552. //header
  553. $excel_data[0][] = ['styleid' => 's_title', 'data' => '通道ID'];
  554. $excel_data[0][] = ['styleid' => 's_title', 'data' => '通道名称'];
  555. $excel_data[0][] = ['styleid' => 's_title', 'data' => '订单成功后余额'];
  556. $excel_data[0][] = ['styleid' => 's_title', 'data' => '接口查询余额'];
  557. //data
  558. foreach ($providers as $provider) {
  559. $tmp = [];
  560. $store_name = "{$provider['name']}({$provider['store_name']})";
  561. $tmp[] = ['data' => $provider['provider_id']];
  562. $tmp[] = ['data' => $store_name];
  563. $tmp[] = ['data' => $provider['available_predeposit']];
  564. $tmp[] = ['data' => $provider['balance']];
  565. $excel_data[] = $tmp;
  566. }
  567. $excel_data = $excel_obj->charset($excel_data, CHARSET);
  568. $excel_obj->addArray($excel_data);
  569. $excel_obj->addWorksheet($excel_obj->charset(L('exp_od_order'), CHARSET));
  570. $excel_obj->generateXML('通道余额明细导出-' . date('Y-m-d-H', time()));
  571. }
  572. public function provider_remitOp()
  573. {
  574. $refill_company = $this->refill_companys(['co_type' => refill_companyModel::co_type_provider]);
  575. if (chksubmit())
  576. {
  577. $remits = $_POST['remits'];
  578. $strs = $_POST['strs'];
  579. $operation = $_POST['operation'];
  580. $title = '';
  581. $remit_total = 0;
  582. foreach ($strs as $key => $str)
  583. {
  584. $remit = $remits[$key];
  585. if(empty($remit)) continue;
  586. $item = explode('-', $str);
  587. $co_id = $item[0];
  588. $pid = $item[1];
  589. $data[$co_id][$pid] = $remit;
  590. $remit_total += $remit;
  591. }
  592. if(empty($data)) {
  593. showMessage('未提交数据');
  594. }
  595. foreach ($data as $co_id => $valuue)
  596. {
  597. $title .= "{$refill_company[$co_id]['co_name']}-";
  598. }
  599. $title .= '打款';
  600. $remit_record = [
  601. 'title' => $title,
  602. 'amount' => $remit_total,
  603. 'params' => json_encode($data),
  604. 'operation' => $operation,
  605. 'add_time' => time(),
  606. 'oper_time' => time()
  607. ];
  608. Model('refill_company_remit')->addRemit($remit_record);
  609. showMessage('操作成功', 'index.php?act=refill_company&op=remit_record');
  610. }
  611. else
  612. {
  613. $provider_items = $this->providers();
  614. $result = $this->remit_data($provider_items, $refill_company);
  615. Tpl::output('remit_data',$result);
  616. Tpl::showpage('provider.remit');
  617. }
  618. }
  619. private function remit_data($items, $refill_company): array
  620. {
  621. $providers = $mid_pids = $mids = $datas = $result =[];
  622. foreach ($items as $item)
  623. {
  624. $co_id = $item['co_id'];
  625. if(empty($co_id)) continue;
  626. $pid = $item['provider_id'];
  627. $account_id = intval($item['account_id']);
  628. if($account_id > 0) {
  629. $mid_pids[$account_id] = $pid;
  630. $mids[] = $account_id;
  631. }
  632. $item['available_predeposit'] = 0;
  633. $providers[$pid] = $item;
  634. }
  635. if(!empty($mids))
  636. {
  637. $member_data = Model('member')->field('member_id,available_predeposit')->where(['member_id' => ['in',$mids]])->select();
  638. foreach ($member_data as $member)
  639. {
  640. $mid = intval($member['member_id']);
  641. if(array_key_exists($mid,$mid_pids)) {
  642. $pid = $mid_pids[$mid];
  643. $providers[$pid]['available_predeposit'] = $member['available_predeposit'];
  644. }
  645. }
  646. }
  647. foreach ($providers as $provider)
  648. {
  649. $datas[$provider['co_id']][] = $provider;
  650. }
  651. $remit_cfg = $this->remit_cfg();
  652. foreach ($datas as $co_id => $data)
  653. {
  654. $available_total = 0;
  655. foreach ($data as $key => $value)
  656. {
  657. if($value['available_predeposit'] > $remit_cfg['remit_money']) {
  658. $data[$key]['remit'] = 0;
  659. }else{
  660. $data[$key]['remit'] = abs($value['available_predeposit'] - $remit_cfg['remit_money']);
  661. }
  662. $available_total += $value['available_predeposit'];
  663. }
  664. if($available_total > $remit_cfg['remit_money']) continue;
  665. $result[] = [
  666. 'co_name' => $refill_company[$co_id]['co_name'],
  667. 'providers' => $data
  668. ];
  669. }
  670. return $result;
  671. }
  672. }