provider.php 29 KB

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