provider.php 28 KB

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