merchant.php 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275
  1. <?php
  2. /**
  3. * 机构管理界面
  4. *
  5. **by 好商城V3 www.33hao.com 运营版*/
  6. defined('InShopNC') or exit('Access Invalid!');
  7. include(BASE_CONFIG_PATH . CONFIG_PREFIX . '/refill.ini.php');
  8. require_once(BASE_HELPER_PATH . '/stat_helper.php');
  9. require_once(BASE_ROOT_PATH . '/helper/refill/functional.php');
  10. require_once(BASE_HELPER_PATH . '/task/task_helper.php');
  11. use refill\Quality;
  12. use refill\quality_ploy;
  13. class merchantControl extends SystemControl
  14. {
  15. const EXPORT_SIZE = 1000;
  16. public function __construct()
  17. {
  18. parent::__construct();
  19. Language::read('merchant');
  20. }
  21. /**
  22. * 机构列表
  23. */
  24. public function merchantOp()
  25. {
  26. global $config;
  27. $model_merchant = Model('merchant');
  28. $condition = [];
  29. if (trim($_GET['merchant_name']) != '') {
  30. $condition['company_name'] = ['like', '%' . $_GET['merchant_name'] . '%'];
  31. }
  32. $debt_total = 0;
  33. $available_total = 0;
  34. $merchant_list = $model_merchant->getMerchantList($condition, 200, 'available_predeposit desc,merchant_state asc,mchid desc', true);
  35. foreach ($merchant_list as $key => $merchant) {
  36. $mchid = $merchant['mchid'];
  37. $available_predeposit = $merchant['available_predeposit'] - $merchant['credit_bonus'];
  38. $merchant_list[$key]['available_predeposit'] = number_format($available_predeposit,4,'.',',');;
  39. if(!in_array($mchid, $config['test_mchid']) && $merchant['merchant_state'] == 1) {
  40. if($available_predeposit >= 0) {
  41. $available_total += $available_predeposit;
  42. }else{
  43. $debt_total += $available_predeposit;
  44. }
  45. }
  46. }
  47. $stats = ['available_total' => $available_total, 'debt_total' => $debt_total];
  48. Tpl::output('merchant_state_text', self::STATE_TEXT);
  49. Tpl::output('merchant_list', $merchant_list);
  50. Tpl::output('stats', $stats);
  51. Tpl::output('page', $model_merchant->showpage('2'));
  52. Tpl::showpage('merchant.index');
  53. }
  54. public function changeStateOp()
  55. {
  56. $mchid = intval($_GET['mchid']);
  57. $state = intval($_GET['state']);
  58. $model_merchant = Model('merchant');
  59. $merchant_info = $model_merchant->getMerchantInfo(['mchid' => $mchid]);
  60. if (empty($merchant_info) || !in_array($state, [1, 2])) {
  61. showMessage('操作成功', 'index.php?act=merchant&op=merchant');
  62. }
  63. $resp = $model_merchant->editMerchant(['merchant_state' => $state], ['mchid' => $mchid]);
  64. if (!$resp) {
  65. showMessage('操作失败', 'index.php?act=merchant&op=merchant', 'html', 'error');
  66. }
  67. showMessage('操作成功', 'index.php?act=merchant&op=merchant');
  68. }
  69. public function AllCloseMerchantOp()
  70. {
  71. $model_merchant = Model('merchant');
  72. $resp = $model_merchant->editMerchant(['merchant_state' => 2], ['mchid' => ['gt',0]]);
  73. showMessage('操作成功');
  74. }
  75. public function merchant_addOp()
  76. {
  77. if (chksubmit())
  78. {
  79. $obj_validate = new Validator();
  80. $obj_validate->validateparam = [
  81. ["input" => $_POST["name"], "require" => "true", "message" => '机构账号不能为空'],
  82. ["input" => $_POST["company_name"], "require" => "true", "message" => '机构公司名称不能为空'],
  83. ["input" => $_POST["password"], "require" => "true", "message" => '密码不能为空']
  84. ];
  85. $error = $obj_validate->validate();
  86. if ($error != '') {
  87. showMessage($error);
  88. }
  89. else
  90. {
  91. $model_merchant = Model('merchant');
  92. $model_member = Model('member');
  93. $trans = new trans_wapper($model_merchant, __METHOD__);
  94. try
  95. {
  96. $name = trim($_POST['name']);
  97. $company_name = trim($_POST['company_name']);
  98. $pwd = trim($_POST['password']);
  99. $alarm_amount = $_POST['alarm_amount'] ?? 0;
  100. $insert_member['member_name'] = md5($name . time());
  101. $insert_member['member_passwd'] = $pwd;
  102. $insert_id = $model_member->addMember($insert_member);
  103. if ($insert_id == false) {
  104. $trans->rollback();
  105. showMessage('操作失败', 'index.php?act=merchant&op=merchant', 'html', 'error');
  106. }
  107. $insert_array['name'] = $name;
  108. $insert_array['company_name'] = $company_name;
  109. $insert_array['org_pwd'] = $pwd;
  110. $insert_array['password'] = md5($pwd);
  111. $insert_array['admin_id'] = trim($insert_id);
  112. $insert_array['alarm_amount'] = $alarm_amount;
  113. $result = $model_merchant->addMerchant($insert_array);
  114. if ($result > 0) {
  115. $helper = new refill\divert_account();
  116. $helper->init_member($insert_id);
  117. $ret = true;
  118. } else {
  119. $ret = false;
  120. }
  121. if($ret)
  122. {
  123. $trans->commit();
  124. $url = [[
  125. 'url' => 'index.php?act=merchant&op=merchant',
  126. 'msg' => '返回机构列表',
  127. ], [
  128. 'url' => 'index.php?act=merchant&op=merchant_add',
  129. 'msg' => '继续新增机构',
  130. ]];
  131. $this->log('添加机构:' . '[ ' . $_POST['name'] . ']', 1);
  132. showMessage('机构添加成功', $url);
  133. } else {
  134. $trans->rollback();
  135. showMessage('机构添加失败');
  136. }
  137. }
  138. catch (Exception $e) {
  139. $trans->rollback();
  140. showMessage('操作失败', 'index.php?act=merchant&op=merchant', 'html', 'error');
  141. }
  142. }
  143. }
  144. Tpl::showpage('merchant.add');
  145. }
  146. public function merchant_editOp()
  147. {
  148. $mchid = $_GET['mchid'] ?? $_POST['mchid'];
  149. $model_merchant = Model('merchant');
  150. $merchant = $model_merchant->getMerchantInfo(['mchid' => $mchid], '*', true);
  151. if (empty($merchant)) {
  152. showMessage('机构信息有误');
  153. }
  154. if (chksubmit())
  155. {
  156. $update_credit_bonus = ncPriceFormat($_POST['credit_bonus']);
  157. $operatetype = $_POST['operatetype'];
  158. if ($update_credit_bonus > 0) {
  159. if ($merchant['credit_bonus'] + $update_credit_bonus < 0) {
  160. showMessage('授信金额调整后不能小于0');
  161. }
  162. if ($operatetype == 'add') {
  163. $update['credit_bonus'] = ['exp', 'credit_bonus+' . $update_credit_bonus];
  164. } elseif ($operatetype == 'del') {
  165. $update['credit_bonus'] = ['exp', 'credit_bonus-' . $update_credit_bonus];
  166. }
  167. }
  168. $update['company_name'] = trim($_POST['company_name']);
  169. $update['org_pwd'] = trim($_POST['password']);
  170. $update['password'] = md5($update['org_pwd']);
  171. $update['alarm_amount'] = $_POST['alarm_amount'] ?? 0;
  172. $update['manual_recharge'] = $_POST['manual_recharge'];
  173. $day_timeout = intval($_POST['day_timeout']);
  174. $night_timeout = intval($_POST['night_timeout']);
  175. if($day_timeout <= 0) {
  176. $day_timeout = 180;
  177. }
  178. if($night_timeout <= 0) {
  179. $day_timeout = 180;
  180. }
  181. $update['day_timeout'] = $day_timeout;
  182. $update['night_timeout'] = $night_timeout;
  183. if(refill\functional::isDay()) {
  184. $update['time_out'] = intval($_POST['day_timeout']);
  185. } else {
  186. $update['time_out'] = intval($_POST['night_timeout']);
  187. }
  188. $member_id = $merchant['admin_id'];
  189. $model_merchant = Model('merchant');
  190. $trans = new trans_wapper($model_merchant, __METHOD__);
  191. try {
  192. if ($update_credit_bonus > 0) {
  193. $this->credit_save_money($update_credit_bonus, $operatetype, $member_id);
  194. }
  195. $result = $model_merchant->editMerchant($update, ['mchid' => $mchid]);
  196. if (!$result) {
  197. $trans->rollback();
  198. showMessage('机构编辑失败', 'index.php?act=merchant&op=merchant');
  199. }
  200. $trans->commit();
  201. $this->log('编辑机构:' . '[ ' . $merchant['name'] . ']', 1);
  202. showMessage('机构编辑成功', 'index.php?act=merchant&op=merchant');
  203. } catch (Exception $e) {
  204. $trans->rollback();
  205. showMessage('机构编辑失败', 'index.php?act=merchant&op=merchant');
  206. }
  207. }
  208. Tpl::output('merchant', $merchant);
  209. Tpl::showpage('merchant.edit');
  210. }
  211. public function priceOp()
  212. {
  213. $quality = $_GET['quality'] ?? 1;
  214. if (chksubmit()) {
  215. $mchid = $_POST['mchid'];
  216. //合并表单数据
  217. $card_types = $_POST['cardtype'];
  218. $specs = $_POST['spec'];
  219. $prices = $_POST['price'];
  220. $extra_prices = $_POST['extra_price'];
  221. $max_inprices = $_POST['max_inprice'];
  222. foreach ($card_types as $key => $card_type) {
  223. $data['card_type'] = $card_type;
  224. $data['spec'] = intval($specs[$key]);
  225. $data['price'] = ncPriceFormat($prices[$key]);
  226. $data['extra_price'] = ncPriceFormat($extra_prices[$key]);
  227. $data['max_inprice'] = ncPriceFormat($max_inprices[$key]);
  228. $params[] = $data;
  229. }
  230. foreach ($params as $param) {
  231. if ($param['price'] > 0) {
  232. $insert['mchid'] = $mchid;
  233. $insert['spec'] = $param['spec'];
  234. $insert['price'] = $param['price'];
  235. $insert['extra_price'] = $param['extra_price'];
  236. $insert['max_inprice'] = $param['max_inprice'];
  237. $insert['card_types'] = $param['card_type'];
  238. $insert['quality'] = $quality;
  239. $inserts[] = $insert;
  240. }
  241. }
  242. $model_merchant = Model('merchant');
  243. $trans = new trans_wapper($model_merchant, __METHOD__);
  244. try {
  245. //删除旧费率
  246. $res = $model_merchant->delPrices(['mchid' => $mchid, 'quality' => $quality]);
  247. if(!$res) {
  248. $trans->rollback();
  249. showMessage('删除旧费率失败');
  250. }
  251. $ret = true;
  252. //更新新费率
  253. if (!empty($inserts)) {
  254. $ret = $model_merchant->insertPrices($inserts);
  255. }
  256. if($ret) {
  257. $trans->commit();
  258. showMessage('操作成功');
  259. }else{
  260. $trans->rollback();
  261. showMessage('更新费率失败');
  262. }
  263. } catch (Exception $e) {
  264. $trans->rollback();
  265. Log::record("merchant_price update err: {$e->getMessage()}", Log::ERR);
  266. showMessage('操作失败');
  267. }
  268. }
  269. $mchid = $_GET['mchid'] ?? 0;
  270. $goods = $this->GoodsFormat($mchid, $quality);
  271. Tpl::output('goods', $goods);
  272. Tpl::showpage('merchant.price');
  273. }
  274. public function third_merchant_priceOp()
  275. {
  276. $mchid = $_GET['mchid'] ?? $_POST['mchid'];
  277. $mod = Model('thrid_refill');
  278. if (chksubmit()) {
  279. //合并表单数据
  280. $card_types = $_POST['cardtype'];
  281. $specs = $_POST['spec'];
  282. $prices = $_POST['price'];
  283. $pcodes = $_POST['pcode'];
  284. $quality = 1;
  285. $params = [];
  286. foreach ($card_types as $key => $card_type) {
  287. $data['card_type'] = $card_type;
  288. $data['spec'] = intval($specs[$key]);
  289. $data['price'] = ncPriceFormat($prices[$key]);
  290. $data['pcode'] = $pcodes[$key];
  291. $params[] = $data;
  292. }
  293. $model_merchant = Model('merchant');
  294. $trans = new trans_wapper($model_merchant, __METHOD__);
  295. try {
  296. foreach ($params as $param) {
  297. $insert = [];
  298. if ($param['price'] > 0) {
  299. $insert['mchid'] = $mchid;
  300. $insert['spec'] = $param['spec'];
  301. $insert['price'] = $param['price'];
  302. $insert['card_types'] = $param['card_type'];
  303. $insert['quality'] = $quality;
  304. $insert['pcode'] = $param['pcode'];
  305. }
  306. //删除旧费率
  307. $res = $model_merchant->delPrices(['mchid' => $mchid, 'quality' => $quality, 'pcode' => $param['pcode']]);
  308. if(!$res) {
  309. $trans->rollback();
  310. showMessage('删除旧费率失败');
  311. }
  312. $ret = true;
  313. //更新新费率
  314. if (!empty($insert)) {
  315. $ret = $model_merchant->table('merchant_price')->insert($insert);
  316. }
  317. if(!$ret) {
  318. $trans->rollback();
  319. showMessage('更新费率失败');
  320. }
  321. }
  322. $trans->commit();
  323. showMessage('操作成功', 'index.php?act=merchant&op=merchant');
  324. } catch (Exception $e) {
  325. $trans->rollback();
  326. Log::record("merchant_price pcode update err: {$e->getMessage()}", Log::ERR);
  327. showMessage('操作失败');
  328. }
  329. } else {
  330. $condition = [];
  331. if (!empty($_GET['system_code'])) {
  332. $condition['system_code'] = $_GET['system_code'];
  333. }
  334. if (!empty($_GET['product_type'])) {
  335. $condition['product_type'] = $_GET['product_type'];
  336. }
  337. if (trim($_GET['product_name']) != '') {
  338. $condition['product_name'] = ['like', '%' . $_GET['product_name'] . '%'];
  339. }
  340. $third_product = $mod->getProductList($condition, 50);
  341. $model_merchant = Model('merchant');
  342. $items = $model_merchant->table('merchant_price')->where(['mchid' => $mchid, 'quality' => 1, 'card_types' => mtopcard\ThirdRefillCard])->select();
  343. $goods = [];
  344. foreach ($items as $item) {
  345. $goods[$item['pcode']] = $item['price'];
  346. }
  347. Tpl::output('third_product_type', self::THIRD_PRODUCT_TYPE);
  348. Tpl::output('third_product', $third_product);
  349. Tpl::output('goods', $goods);
  350. Tpl::output('page', $mod->showpage());
  351. Tpl::showpage('third.merchant.price');
  352. }
  353. }
  354. private function GoodsFormat($mchid, $quality)
  355. {
  356. $all_spector = function ($quality) {
  357. global $config;
  358. $refill_specs = $config['refill_specs'];
  359. $all_type_specs = [];
  360. foreach ($refill_specs as $scard_type => $specs) {
  361. $card_type = mtopcard\topcard_type($scard_type);
  362. if (in_array($card_type, [mtopcard\ChinaMobileCard, mtopcard\ChinaUnicomCard, mtopcard\ChinaTelecomCard])) {
  363. if ($quality == refill\Quality::Quick) {
  364. $specs = array_merge($config['refill_phone_small_specs'], $specs);
  365. }
  366. }
  367. $all_type_specs[$card_type] = $specs;
  368. }
  369. $result = [];
  370. foreach ($all_type_specs as $card_type => $specs) {
  371. foreach ($specs as $spec) {
  372. $result["{$card_type}-{$spec}"] = ['card_type' => $card_type, 'spec' => $spec];
  373. }
  374. }
  375. return $result;
  376. };
  377. $merch_spector = function ($mchid, $quality) {
  378. $model_merchant = Model('merchant');
  379. $items = $model_merchant->table('merchant_price')->where(['mchid' => $mchid, 'quality' => $quality])->select();
  380. $result = [];
  381. foreach ($items as $item) {
  382. $card_types = explode(',', $item['card_types']);
  383. foreach ($card_types as $card_type) {
  384. $value['spec'] = intval($item['spec']);
  385. $result["{$card_type}-{$value['spec']}"] = ['card_type' => $card_type,
  386. 'spec' => intval($item['spec']),
  387. 'price' => $item['price'],
  388. 'extra_price' => $item['extra_price'],
  389. 'max_inprice' => $item['max_inprice']
  390. ];
  391. }
  392. }
  393. return $result;
  394. };
  395. $all_cardtype_specs = $all_spector($quality);
  396. $merch_cardtype_specs = $merch_spector($mchid, $quality);
  397. $merger = function ($all_specs, $mech_specs) {
  398. $result = [];
  399. foreach ($all_specs as $cardtype_spec => $value) {
  400. $card_name = $this->scard_type($value['card_type']);
  401. $data = [];
  402. $data['goods_name'] = $card_name;
  403. $data['card_type'] = $value['card_type'];
  404. $data['spec'] = $value['spec'];
  405. if (array_key_exists($cardtype_spec, $mech_specs)) {
  406. $data['price'] = $mech_specs[$cardtype_spec]['price'];
  407. $data['extra_price'] = $mech_specs[$cardtype_spec]['extra_price'];
  408. $data['max_inprice'] = $mech_specs[$cardtype_spec]['max_inprice'];
  409. } else {
  410. $data['price'] = 0;
  411. $data['extra_price'] = 0;
  412. $data['max_inprice'] = 0;
  413. }
  414. $result[$value['card_type']][] = $data;
  415. }
  416. return $result;
  417. };
  418. return $merger($all_cardtype_specs, $merch_cardtype_specs);
  419. }
  420. public function check_merchantOp()
  421. {
  422. $mchid = trim($_GET['mchid']);
  423. if (!$mchid) {
  424. echo '';
  425. die;
  426. }
  427. $model_merchant = Model('merchant');
  428. $merchant_info = $model_merchant->getMerchantInfo(['mchid' => $mchid], '*');
  429. if (empty($merchant_info)) {
  430. echo '';
  431. die;
  432. }
  433. echo 'true';
  434. }
  435. /**
  436. * ajax操作
  437. */
  438. public function ajaxOp()
  439. {
  440. $model_merchant = Model('merchant');
  441. switch ($_GET['branch']) {
  442. /**
  443. * 验证机构名称是否重复
  444. */
  445. case 'check_mch_name':
  446. $condition['name'] = $_GET['name'];
  447. $list = $model_merchant->getMerchantInfo($condition);
  448. if (empty($list)) {
  449. echo 'true';
  450. } else {
  451. echo 'false';
  452. }
  453. break;
  454. /**
  455. * 验证机构是否存在
  456. */
  457. case 'check_merchant':
  458. $condition['mchid'] = intval($_GET['mchid']);
  459. $list = $model_merchant->getMerchantInfo($condition);
  460. if (!empty($list)) {
  461. echo 'true';
  462. } else {
  463. echo 'false';
  464. }
  465. }
  466. }
  467. public function check_evidenceOp()
  468. {
  469. $status = $_GET['status'];
  470. $apply_id = intval($_GET['apply_id']);
  471. if ($apply_id > 0) {
  472. $model_merchant = Model('merchant');
  473. $evidence_info = $model_merchant->getRefillEvidenceInfo(['apply_id' => $apply_id], '*', true);
  474. if (empty($evidence_info)) {
  475. showMessage('充值申请不存在');
  476. }
  477. if ($status == 'pass') {
  478. $model_merchant->editRefillEvidence(['apply_id' => $apply_id], ['status' => 2, 'check_time' => time()]);
  479. } elseif ($status == 'unpass') {
  480. $model_merchant->editRefillEvidence(['apply_id' => $apply_id], ['status' => 3, 'check_time' => time()]);
  481. } else {
  482. showMessage('审核类型错误');
  483. }
  484. }
  485. showMessage(L('nc_common_save_succ'), urlAdmin('refill_evidence', 'index'));
  486. }
  487. //处理充值申请
  488. public function rechargeOp()
  489. {
  490. if (chksubmit()) {
  491. $mchid = $this->add_money();
  492. $model_merchant = Model('merchant');
  493. $merchant_info = $model_merchant->getMerchantInfo(['mchid' => $mchid]);
  494. if ($merchant_info['alarm_amount'] < $merchant_info['available_predeposit']) {
  495. //更新预警短信通知限制
  496. $mch_cache = rcache("merchant-notify", 'refill-');
  497. $caches = empty($mch_cache['data']) ? [] : unserialize($mch_cache['data']);
  498. $caches[$mchid] = ['last_time' => 0, 'send_count' => 0];
  499. wcache("merchant-notify", ['data' => serialize($caches)], 'refill-');
  500. }
  501. showMessage('操作成功', 'index.php?act=refill_evidence&op=index');
  502. } else {
  503. $apply_id = $_GET['apply_id'];
  504. if (!empty($apply_id)) {
  505. $model_merchant = Model('merchant');
  506. $evidence_info = $model_merchant->getRefillEvidenceInfo(['apply_id' => $apply_id], '*,member.available_predeposit', true);
  507. Tpl::output('apply_id', $apply_id);
  508. Tpl::output('amount', $evidence_info['amount']);
  509. Tpl::output('available_predeposit', $evidence_info['available_predeposit']);
  510. }
  511. global $config;
  512. Tpl::output('receive_bank', $config['receive_bank'][COMPANY_NAME]);
  513. Tpl::showpage('recharge.add');
  514. }
  515. }
  516. public function recharge_manualOp()
  517. {
  518. $type = $_GET['type'] ?? $_POST['type'];
  519. if (chksubmit()) {
  520. $add_type = '';
  521. $obj_validate = new Validator();
  522. if ($type == 'add') {
  523. $add_type = '加款';
  524. $obj_validate->validateparam = [
  525. ["input" => $_POST["mch_id"], "require" => "true", "message" => '机构号不能为空'],
  526. ["input" => $_POST["bank_username"], "require" => "true", "message" => '开户人姓名不能为空'],
  527. ["input" => $_POST["bank_name"], "require" => "true", "message" => '开户银行不能为空'],
  528. ["input" => $_POST["pointsnum"], "require" => "true", "message" => '预存金额不能为空'],
  529. ["input" => $_POST["receive_bank"], "require" => "true", "message" => '请选择收款银行']
  530. ];
  531. } elseif ($type == 'edit') {
  532. $add_type = '调款';
  533. $obj_validate->validateparam = [
  534. ["input" => $_POST["mch_id"], "require" => "true", "message" => '机构号不能为空'],
  535. ["input" => $_POST["bank_username"], "require" => "true", "message" => '操作人姓名不能为空'],
  536. ["input" => $_POST["pointsnum"], "require" => "true", "message" => '预存金额不能为空']
  537. ];
  538. }
  539. $operatetype = $_POST['operatetype'];
  540. $error = $obj_validate->validate();
  541. if ($error != '') {
  542. showMessage($error);
  543. }
  544. $mchid = $_POST["mch_id"];
  545. $model_merchant = Model('merchant');
  546. $merchant_info = $model_merchant->getMerchantInfo(['mchid' => $mchid]);
  547. if (!$merchant_info) {
  548. showMessage('对应机构不存在');
  549. }
  550. $pointsnum = $_POST['pointsnum'];
  551. $money = abs($pointsnum);
  552. if ($money == 0) {
  553. showMessage('预存金额错误');
  554. }
  555. if (!empty($_FILES['voucher']['name'])) {
  556. $upload = new UploadFile();
  557. $upload->set('default_dir', ATTACH_UPFILE);
  558. $result = $upload->upfile('voucher');
  559. if ($result) {
  560. $_POST['voucher_name'] = $upload->file_name;
  561. } else {
  562. showMessage($upload->error);
  563. }
  564. }
  565. $trans = new trans_wapper($model_merchant, __METHOD__);
  566. try {
  567. $member_id = $merchant_info['admin_id'];
  568. if ($operatetype == 'add') {
  569. $bz = "管理员{$add_type}操作,手动增加预存金额";
  570. $this->credit_save_money($money, 'add', $member_id, $bz);
  571. $_POST['pointsnum'] = $money;
  572. } elseif ($operatetype == 'del') {
  573. $bz = "管理员{$add_type}操作,手动减少预存金额";
  574. $this->credit_save_money($money, 'del', $member_id, $bz);
  575. $_POST['pointsnum'] = -($money);
  576. } else {
  577. showMessage('预存类型错误');
  578. }
  579. $result = $this->ct_refill_evidence($_POST, $merchant_info);
  580. if (!$result) {
  581. $trans->rollback();
  582. showMessage('操作失败', 'index.php?act=merchant&op=merchant');
  583. }
  584. $trans->commit();
  585. if ($merchant_info['alarm_amount'] < $merchant_info['available_predeposit']) {
  586. //更新预警短信通知限制
  587. $mch_cache = rcache("merchant-notify", 'refill-');
  588. $caches = empty($mch_cache['data']) ? [] : unserialize($mch_cache['data']);
  589. $caches[$mchid] = ['last_time' => 0, 'send_count' => 0];
  590. wcache("merchant-notify", ['data' => serialize($caches)], 'refill-');
  591. }
  592. showMessage('操作成功', 'index.php?act=refill_evidence&op=index');
  593. } catch (Exception $e) {
  594. $trans->rollback();
  595. showMessage('操作失败', 'index.php?act=refill_evidence&op=index');
  596. }
  597. } else {
  598. $mchid = $_GET['mchid'] ?? $_POST['mchid'];
  599. $model_merchant = Model('merchant');
  600. $merchant = $model_merchant->getMerchantInfo(['mchid' => $mchid]);
  601. if (empty($merchant)) {
  602. showMessage('机构信息有误');
  603. }
  604. Tpl::output('merchant', $merchant);
  605. global $config;
  606. Tpl::output('receive_bank', $config['receive_bank'][COMPANY_NAME]);
  607. $page = "recharge.manual.{$type}";
  608. Tpl::showpage($page);
  609. }
  610. }
  611. private function ct_refill_evidence($params, $merchant_info)
  612. {
  613. $admininfo = $this->getAdminInfo();
  614. $mem_info = Model('member')->getMemberInfo(['member_id' => $merchant_info['admin_id']]);
  615. $input['mchid'] = $merchant_info['mchid'];
  616. $input['mch_name'] = $merchant_info['name'];
  617. $input['member_id'] = $merchant_info['admin_id'];
  618. $input['amount'] = $params['pointsnum'];
  619. $input['bank_username'] = $params['bank_username'];
  620. $input['bank_name'] = $params['bank_name'] ?? '';
  621. $input['bz'] = $params['pointsdesc'];
  622. $input['voucher_name'] = $params['voucher_name'] ?? '/';
  623. $input['status'] = 2;
  624. $input['is_operation'] = 2;
  625. $input['add_time'] = $input['check_time'] = time();
  626. $input['after_available'] = ncPriceFormat($mem_info['available_predeposit'] + $params['pointsnum']);
  627. $input['admin_name'] = $admininfo['name'];
  628. $input['admin_id'] = $admininfo['id'];
  629. $input['add_type'] = $params['add_type'];
  630. $input['receive_bank'] = $params['receive_bank'] ?? 0;
  631. if ($params['type'] == 'add') {
  632. $input['is_bank'] = 1;
  633. }
  634. $model_merchant = Model('merchant');
  635. return $model_merchant->addRefillEvidence($input);
  636. }
  637. public function add_money()
  638. {
  639. $obj_validate = new Validator();
  640. $obj_validate->validateparam = [
  641. ["input" => $_POST["apply_id"], "require" => "true", "message" => Language::get('admin_points_member_error_again')],
  642. ["input" => $_POST["pointsnum"], "require" => "true", 'validator' => 'Compare', 'operator' => ' >= ', 'to' => 1, "message" => Language::get('admin_points_points_min_error')],
  643. ["input" => $_POST["receive_bank"], "require" => "true", "message" => '请选择收款银行']
  644. ];
  645. $error = $obj_validate->validate();
  646. if ($error != '') {
  647. showMessage($error, '', '', 'error');
  648. }
  649. $money = abs(floatval($_POST['pointsnum']));
  650. $memo = trim($_POST['pointsdesc']);
  651. if ($money <= 0) {
  652. showMessage('输入的金额必需大于0', '', 'html', 'error');
  653. }
  654. $apply_id = intval($_POST['apply_id']);
  655. $model_merchant = Model('merchant');
  656. $receive_bank = $_POST['receive_bank'] ?? 0;
  657. $evidence_info = $model_merchant->getRefillEvidenceInfo(['apply_id' => $apply_id], '*', true);
  658. if (!is_array($evidence_info) || count($evidence_info) <= 0) {
  659. showMessage("无效的充值申请信息", "index.php?act=merchant&op=recharge&apply_id={$apply_id}", '', 'error');
  660. }
  661. if ($evidence_info['status'] != 2) {
  662. showMessage("该充值申请未被审核通过", "index.php?act=merchant&op=recharge&apply_id={$apply_id}", '', 'error');
  663. }
  664. if ($evidence_info['is_operation'] == 2) {
  665. showMessage("该充值申请未已被预存过", "index.php?act=merchant&op=recharge&apply_id={$apply_id}", '', 'error');
  666. }
  667. //查询会员信息
  668. $obj_member = Model('member');
  669. $member_id = intval($evidence_info['member_id']);
  670. $member_info = $obj_member->getMemberInfo(['member_id' => $member_id], '*', true);
  671. $available_predeposit = floatval($member_info['available_predeposit']);
  672. $freeze_predeposit = floatval($member_info['freeze_predeposit']);
  673. if ($_POST['operatetype'] == 3 && $money > $available_predeposit) {
  674. showMessage(('可冻结预存款不足,会员当前预存款') . $available_predeposit, 'index.php?act=predeposit&op=predeposit_add', '', 'error');
  675. }
  676. if ($_POST['operatetype'] == 4 && $money > $freeze_predeposit) {
  677. showMessage(('可恢复冻结预存款不足,会员当前冻结预存款') . $freeze_predeposit, 'index.php?act=predeposit&op=predeposit_add', '', 'error');
  678. }
  679. $model_pd = Model('predeposit');
  680. $order_sn = $apply_id;
  681. $admininfo = $this->getAdminInfo();
  682. $log_msg = "管理员【" . $admininfo['name'] . "】操作会员【" . $member_info['member_name'] . "】预存款,金额为" . $money . ",编号为" . $order_sn;
  683. $admin_act = 'sys_add_money';
  684. switch ($_POST['operatetype']) {
  685. case 1:
  686. $admin_act = "sys_add_money";
  687. $log_msg = "管理员【" . $admininfo['name'] . "】操作会员【" . $member_info['member_name'] . "】预存款【增加】,金额为" . $money . ",编号为" . $order_sn;
  688. break;
  689. case 2:
  690. $admin_act = "sys_del_money";
  691. $log_msg = "管理员【" . $admininfo['name'] . "】操作会员【" . $member_info['member_name'] . "】预存款【减少】,金额为" . $money . ",编号为" . $order_sn;
  692. break;
  693. default:
  694. showMessage('操作失败', 'index.php?act=refill_evidence&op=index');
  695. break;
  696. }
  697. $trans = new trans_wapper($model_pd, __METHOD__);
  698. try {
  699. $evidence_info = $model_merchant->getRefillEvidenceInfo(['apply_id' => $apply_id], '*', true,true);
  700. if ($evidence_info['is_operation'] == 2) {
  701. $trans->commit();
  702. showMessage('已经处理', 'index.php?act=refill_evidence&op=index', 'html', 'error');
  703. }
  704. //扣除冻结的预存款
  705. $data = [];
  706. $data['member_id'] = $member_info['member_id'];
  707. $data['member_name'] = $member_info['member_name'];
  708. $data['amount'] = $money;
  709. $data['order_sn'] = $order_sn;
  710. $data['admin_name'] = $admininfo['name'];
  711. $data['pdr_sn'] = $order_sn;
  712. $data['lg_desc'] = $memo;
  713. $isRefill = $model_pd->isRefill($data['member_id']);
  714. $model_pd->changePd($admin_act, $data,$isRefill);
  715. $after_available = ncPriceFormat($available_predeposit + $evidence_info['amount']);
  716. $model_merchant->editRefillEvidence(
  717. ['apply_id' => $apply_id],
  718. [
  719. 'is_operation' => 2, 'after_available' => $after_available,
  720. 'admin_id' => $admininfo['id'], 'admin_name' => $admininfo['name'],
  721. 'receive_bank' => $receive_bank
  722. ]
  723. );
  724. $trans->commit();
  725. $this->log($log_msg, 1);
  726. return $evidence_info['mchid'];
  727. } catch (Exception $e) {
  728. $trans->rollback();
  729. $this->log($log_msg, 0);
  730. showMessage($e->getMessage(), 'index.php?act=refill_evidence&op=index', 'html', 'error');
  731. }
  732. }
  733. public function checkevidenceOp()
  734. {
  735. $apply_id = trim($_GET['apply_id']);
  736. if (!$apply_id) {
  737. echo '';
  738. die;
  739. }
  740. $model_merchant = Model('merchant');
  741. $evidence_info = $model_merchant->getRefillEvidenceInfo(['apply_id' => $apply_id], '*');
  742. if (empty($evidence_info)) {
  743. echo '';
  744. die;
  745. }
  746. echo 'true';
  747. }
  748. public function notify_merchantOp()
  749. {
  750. $order_id = $_GET['order_id'];
  751. $mod_order = Model('vr_order');
  752. $order_info = $mod_order->getOrderInfo(['order_id' => $order_id]);
  753. $refill_info = Model('refill_order')->getOrderInfo(['order_id' => $order_id, 'inner_status' => 0, 'is_retrying' => 0]);
  754. if (empty($refill_info)) {
  755. showMessage('订单不存在,或不符合条件', '');
  756. }
  757. if ($order_info['order_state'] == ORDER_STATE_SEND) {
  758. QueueClient::push("QueryRefillState", ['order_id' => $order_id]);
  759. } else {
  760. QueueClient::push("NotifyMerchantComplete", ['order_id' => $order_id, 'manual' => true]);
  761. }
  762. showMessage('操作成功', '');
  763. }
  764. public function notify_manual_merchantOp()
  765. {
  766. $order_id = $_GET['order_id'];
  767. $type = $_GET['type'];
  768. $mod_order = Model('vr_order');
  769. $order_info = $mod_order->getOrderInfo(['order_id' => $order_id, 'order_state' => ORDER_STATE_SEND]);
  770. $refill_info = Model('refill_order')->getOrderInfo(['order_id' => $order_id, 'inner_status' => 0, 'is_retrying' => 0]);
  771. if (empty($refill_info) || empty($order_info)) {
  772. showMessage('订单不存在,或不符合条件');
  773. }
  774. $fetch_order_ids = $this->check_fetch_order($order_id);
  775. if (in_array($order_id, $fetch_order_ids)) {
  776. showMessage('此订单不可手动操作,请联系抢单人员操作!');
  777. }
  778. if ($type == 'success') {
  779. refill\util::manual_success($order_id);
  780. } elseif ($type == 'cancel') {
  781. refill\util::manual_cancel($order_id);
  782. } else {
  783. showMessage('手动操作类型错误', 'index.php?act=refill_order&op=index');
  784. }
  785. $this->manual_log_record($refill_info, $type);
  786. $this->log("手动处理,订单号:{$order_info['order_sn']}", 1);
  787. showMessage('操作成功');
  788. }
  789. private function manual_log_record($order, $oper_type)
  790. {
  791. $admininfo = $this->getAdminInfo();
  792. $cur_time = time();
  793. $bz = "手动处理订单,操作者:{$admininfo['name']}";
  794. $ins = [
  795. 'order_id' => $order['order_id'], 'order_sn' => $order['order_sn'],'order_time' => $order['order_time'], 'notify_time' => $order['notify_time'],
  796. 'manual_type' => $oper_type, 'admin_id' => $admininfo['id'], 'admin_name' => $admininfo['name'], 'act_time' => $cur_time, 'bz' => $bz
  797. ];
  798. Model('refill_buyback')->insert($ins);
  799. }
  800. public function provider_provinceOp()
  801. {
  802. $provider_model = Model('refill_provider');
  803. $provider_id = $_GET['id'] ?? $_POST['id'];
  804. $provider = $provider_model->getProviderInfo(['provider_id' => $provider_id]);
  805. if (empty($provider)) {
  806. showMessage('通道信息有误');
  807. }
  808. $operator = [mtopcard\ChinaMobileCard, mtopcard\ChinaUnicomCard, mtopcard\ChinaTelecomCard];
  809. if (chksubmit()) {
  810. foreach ($operator as $opr) {
  811. $key = $opr . '-province';
  812. $province = $_POST[$key];
  813. if ($province == -1) {
  814. $updata[$opr] = -1;
  815. } else {
  816. $updata[$opr] = implode(',', $province);
  817. }
  818. }
  819. $resp = $provider_model->editProvider(['provinces' => serialize($updata)], ['provider_id' => $provider_id]);
  820. if ($resp) {
  821. showMessage('编辑成功', 'index.php?act=provider&op=index');
  822. } else {
  823. showMessage('编辑失败', "index.php?act=merchant&op=provider_province&id={$provider_id}");
  824. }
  825. } else {
  826. $data = unserialize($provider['provinces']);
  827. $provinces = [];
  828. foreach ($operator as $opr) {
  829. if (empty($data)) {
  830. $provinces[$opr] = [-1];
  831. } else {
  832. $provinces[$opr] = explode(',', $data[$opr]);
  833. }
  834. }
  835. $province_list = mtopcard\ProvinceList;
  836. Tpl::output('province_list', $province_list);
  837. Tpl::output('provider', $provider);
  838. Tpl::output('provinces', $provinces);
  839. Tpl::showpage('provider.province');
  840. }
  841. }
  842. public function changeProviderStateOp()
  843. {
  844. $provider_id = intval($_GET['id']);
  845. $state = intval($_GET['state']);
  846. $provider_model = Model('refill_provider');
  847. $provider_info = $provider_model->getProviderInfo(['provider_id' => $provider_id]);
  848. if (empty($provider_info) || !in_array($state, [1, 2])) {
  849. showMessage('操作成功', 'index.php?act=provider&op=index');
  850. }
  851. $resp = $provider_model->editProvider(['opened' => $state], ['provider_id' => $provider_id]);
  852. if (!$resp) {
  853. showMessage('操作失败', 'index.php?act=provider&op=index', 'html', 'error');
  854. }
  855. showMessage('操作成功', 'index.php?act=provider&op=index');
  856. }
  857. /**
  858. * 新增通道
  859. */
  860. public function provider_addOp()
  861. {
  862. $provider_model = Model('refill_provider');
  863. if (chksubmit()) {
  864. $params = $_POST;
  865. unset($params['form_submit']);
  866. $result = $provider_model->addProvider($params);
  867. if ($result) {
  868. $url = [
  869. [
  870. 'url' => 'index.php?act=provider&op=index',
  871. 'msg' => '返回通道列表',
  872. ],
  873. [
  874. 'url' => 'index.php?act=merchant&op=provider_add',
  875. 'msg' => '继续新增通道',
  876. ],
  877. ];
  878. $this->log('添加通道:' . '[ ' . $_POST['name'] . ']', 1);
  879. showMessage('通道添加成功', $url);
  880. } else {
  881. showMessage('通道添加失败');
  882. }
  883. }
  884. Tpl::showpage('provider.add');
  885. }
  886. public function provider_editOp()
  887. {
  888. $provider_model = Model('refill_provider');
  889. if (chksubmit()) {
  890. $provider_id = intval($_POST['provider_id']) ?? '';
  891. $provider = $provider_model->getProviderInfo(['provider_id' => $provider_id]);
  892. if (empty($provider)) {
  893. showMessage('通道信息有误');
  894. }
  895. $params = $_POST;
  896. unset($params['form_submit']);
  897. if (empty($params)) {
  898. showMessage('通道编辑成功', 'index.php?act=provider&op=index');
  899. }
  900. $result = $provider_model->editProvider($params, ['provider_id' => $provider_id]);
  901. if ($result) {
  902. $this->log('编辑通道:' . '[ ' . $provider['name'] . ']', 1);
  903. showMessage('通道编辑成功', 'index.php?act=provider&op=index');
  904. } else {
  905. showMessage('通道编辑失败', "index.php?act=merchant&op=provider_edit&id={$provider_id}");
  906. }
  907. }
  908. $provider_id = intval($_GET['id']) ?? '';
  909. $provider = $provider_model->getProviderInfo(['provider_id' => $provider_id]);
  910. if (empty($provider)) {
  911. showMessage('通道信息有误');
  912. }
  913. $type_text = ['油卡', '手机充值卡'];
  914. Tpl::output('type_text', $type_text);
  915. Tpl::output('provider', $provider);
  916. Tpl::showpage('provider.edit');
  917. }
  918. public function provider_delOp()
  919. {
  920. $provider_model = Model('refill_provider');
  921. $provider_id = intval($_GET['id']) ?? '';
  922. $provider = $provider_model->getProviderInfo(['provider_id' => $provider_id]);
  923. if (empty($provider)) {
  924. showMessage('通道信息有误');
  925. }
  926. $result = $provider_model->delProvider(['provider_id' => $provider_id]);
  927. if ($result) {
  928. $this->log('删除通道:' . '[ ' . $provider['name'] . ']', 1);
  929. showMessage('通道删除成功', 'index.php?act=provider&op=index');
  930. } else {
  931. showMessage('通道删除失败', "index.php?act=provider&op=index");
  932. }
  933. }
  934. public function OrderStatsCheckOp()
  935. {
  936. $stat_id = $_GET['stat_id'] ?? $_POST['stat_id'];
  937. $mod_stat = Model('refill_stats');
  938. $stats_data = $mod_stat->getStatsInfo(['stat_id' => $stat_id]);
  939. if (empty($stats_data)) {
  940. showMessage('对应数据不存在', '');
  941. }
  942. $type = $stats_data['type'];
  943. if (chksubmit()) {
  944. $corder_success_count = $_POST['corder_success_count'] ?? 0;
  945. $corder_success_amounts = $_POST['corder_success_amounts'] ?? 0;
  946. $corder_success_refill_amounts = $_POST['corder_success_refill_amounts'] ?? 0;
  947. $refund = $_POST['refund'] ?? 0;
  948. $remark = $_POST['remark'];
  949. $gap_order_count = $gap_success_amounts = $gap_success_refill_amounts = 0;
  950. if ($type == 'provider') {
  951. if (!empty($corder_success_amounts)) {
  952. $gap_success_amounts = $corder_success_amounts - $stats_data['success_channel_amounts'];
  953. }
  954. if (!empty($corder_success_count)) {
  955. $gap_order_count = $corder_success_count - $stats_data['success_count'];
  956. }
  957. } elseif ($type == 'merchant') {
  958. if (!empty($corder_success_count)) {
  959. $gap_order_count = $stats_data['success_count'] - $corder_success_count;
  960. }
  961. if (!empty($corder_success_amounts)) {
  962. $gap_success_amounts = $stats_data['success_channel_amounts'] - $corder_success_amounts;
  963. }
  964. } else {
  965. showMessage('对账数据类型错误', 'index.php?act=OrderStats&op=index');
  966. }
  967. if (!empty($corder_success_refill_amounts)) {
  968. $gap_success_refill_amounts = $corder_success_refill_amounts - $stats_data['success_refill_amounts'];
  969. }
  970. $updata['corder_success_count'] = $corder_success_count;
  971. $updata['corder_success_amounts'] = $corder_success_amounts;
  972. $updata['corder_success_refill_amounts'] = $corder_success_refill_amounts;
  973. $updata['gap_order_count'] = $gap_order_count;
  974. $updata['gap_success_amounts'] = $gap_success_amounts;
  975. $updata['gap_success_refill_amounts'] = $gap_success_refill_amounts;
  976. $updata['remark'] = $remark;
  977. $updata['refund'] = $refund;
  978. if (!empty($corder_success_count) && !empty($corder_success_amounts) && !empty($corder_success_refill_amounts)) {
  979. if ($gap_order_count == 0 && $gap_success_amounts == 0 && $gap_success_refill_amounts == 0) {
  980. $updata['check_status'] = 1;
  981. } else {
  982. $updata['check_status'] = 2;
  983. }
  984. }
  985. $res = $mod_stat->edit($stat_id, $updata);
  986. if ($res) {
  987. showMessage('操作成功', "index.php?act=OrderStats&op=index&type={$type}&cid={$stats_data['cid']}&order_time_type={$stats_data['order_time_type']}");
  988. } else {
  989. showMessage('操作失败');
  990. }
  991. } else {
  992. $type_text = ['provider' => '上游', 'merchant' => '商户'];
  993. Tpl::output('stats_type', $type_text[$type]);
  994. Tpl::output('stats_data', $stats_data);
  995. Tpl::showpage('order.stats.check');
  996. }
  997. }
  998. public function OrderStatsReloadOp()
  999. {
  1000. $stat_id = $_GET['stat_id'];
  1001. $mod_stat = Model('refill_stats');
  1002. $stats_data = $mod_stat->getStatsInfo(['stat_id' => $stat_id]);
  1003. if (empty($stats_data)) {
  1004. showMessage('对应数据不存在', '');
  1005. }
  1006. $cond = [
  1007. 'type' => $stats_data['type'],
  1008. 'time_stamp' => $stats_data['time_stamp'],
  1009. 'cid' => $stats_data['cid'],
  1010. 'order_time_type' => $stats_data['order_time_type']
  1011. ];
  1012. $manager = new task\manager();
  1013. $task = $manager->add_task('order_stat_reload',$cond,0,3600);
  1014. if ($task->completed() && $task->success()) {
  1015. showMessage('操作成功');
  1016. } else {
  1017. showMessage("操作成功,后台任务已开始重新统计,请稍后查看新数据");
  1018. }
  1019. }
  1020. public function merchant_ctlOp()
  1021. {
  1022. $mchid = $_GET['mchid'] ?? $_POST['mchid'];
  1023. $model_merchant = Model('merchant');
  1024. $merchant = $model_merchant->getMerchantInfo(['mchid' => $mchid], '*', true);
  1025. if (empty($merchant)) {
  1026. showMessage('机构信息有误');
  1027. }
  1028. $qualitys = [
  1029. refill\Quality::Normal, refill\Quality::Quick, refill\Quality::CardKey, refill\Quality::ThirdShop,
  1030. refill\Quality::SlowTwentyFour, refill\Quality::SlowSix, refill\Quality::SlowTwo, refill\Quality::SlowFortyEight,
  1031. refill\Quality::SlowSeventyTwo, refill\Quality::Fastest
  1032. ];
  1033. if(chksubmit())
  1034. {
  1035. if(intval($_POST['transfer_opened']) === 1) {
  1036. if($_POST['transfer_mchid'] == $mchid) {
  1037. showMessage('预回调设置:转发通道不可为自身!');
  1038. }
  1039. $transfer_merchant = $model_merchant->getMerchantInfo(['mchid' => $_POST['transfer_mchid']], '*', true);
  1040. if(empty($transfer_merchant)) {
  1041. showMessage('预回调设置:转发通道不存在!');
  1042. }
  1043. }
  1044. $transfer_data = function (){
  1045. $transfer_cfg['transfer_opened'] = intval($_POST['transfer_opened']);
  1046. $transfer_cfg['transfer_mchid'] = intval($_POST['transfer_mchid']);
  1047. $transfer_cfg['transfer_time'] = intval($_POST['transfer_time']);
  1048. return serialize($transfer_cfg);
  1049. };
  1050. $intercept_data = function (){
  1051. $intercept_cfg['is_transfer'] = intval($_POST['is_transfer']) === 1;
  1052. if (!empty($_POST['card_states'])) {
  1053. foreach ($_POST['card_states'] as $card_state)
  1054. {
  1055. $card_states[] = intval($card_state);
  1056. }
  1057. $intercept_cfg['card_states'] = $card_states;
  1058. }
  1059. if (!empty($_POST['card_types'])) {
  1060. foreach ($_POST['card_types'] as $card_type)
  1061. {
  1062. $card_types[] = intval($card_type);
  1063. }
  1064. $intercept_cfg['card_types'] = $card_types;
  1065. }
  1066. return serialize($intercept_cfg);
  1067. };
  1068. $retry_times_data = function ($qualitys){
  1069. $qualities = [];
  1070. foreach ($qualitys as $quality) {
  1071. $quality = intval($quality);
  1072. $day_secs_key = "{$quality}-day_secs";
  1073. $night_secs_key = "{$quality}-night_secs";
  1074. $times_key = "{$quality}-times";
  1075. if(!empty($_POST[$day_secs_key]) && !empty($_POST[$night_secs_key]) && !empty($_POST[$times_key])) {
  1076. $qualities[$quality] = ['day_secs' => intval($_POST[$day_secs_key]), 'night_secs' => intval($_POST[$night_secs_key]), 'times' => intval($_POST[$times_key])];
  1077. }
  1078. }
  1079. $retry_times_cfg['qualities'] = $qualities;
  1080. $ratio = floatval($_POST['ratio']) ?? 0;
  1081. $period = intval($_POST['period']) ?? 0;
  1082. $profit_ratio = floatval($_POST['profit_ratio']) ?? 0;
  1083. $retry_times_cfg['lower_ratio'] = ['ratio' => $ratio, 'period' => $period];
  1084. $retry_times_cfg['profit_ratio'] = $profit_ratio;
  1085. $profit_formula = $_POST['profit_formula'] ?? '';
  1086. $retry_times_cfg['profit_formula'] = $profit_formula;
  1087. return serialize($retry_times_cfg);
  1088. };
  1089. $retry_times_cfg = $retry_times_data($qualitys);
  1090. $update['quality'] = intval($_POST['quality']);
  1091. $update['oil_quality'] = intval($_POST['oil_quality']);
  1092. $update['intercept_cfg'] = $intercept_data();
  1093. $update['retry_times_cfg'] = $retry_times_cfg;
  1094. $update['transfer_cfg'] = $transfer_data();
  1095. $resp = $model_merchant->editMerchant($update, ['mchid' => $mchid]);
  1096. if ($resp) {
  1097. $this->update_time_out($mchid, $qualitys);
  1098. showMessage('操作成功', 'index.php?act=merchant&op=merchant');
  1099. } else {
  1100. showMessage('操作失败');
  1101. }
  1102. }
  1103. else
  1104. {
  1105. foreach ($qualitys as $value) {
  1106. $quality[$value] = $this->quality_format($value,mtopcard\ChinaMobileCard);
  1107. }
  1108. Tpl::output('intercept', $this->merchant_intercept($merchant));
  1109. Tpl::output('retry_times', $this->merchant_retry_times($merchant));
  1110. Tpl::output('transfer_cfg', $this->transfer_cfg($merchant));
  1111. Tpl::output('merchant', $merchant);
  1112. Tpl::output('quality', $quality);
  1113. Tpl::output('card_state', mtopcard\CardState);
  1114. Tpl::showpage('merchant.ctl');
  1115. }
  1116. }
  1117. private function update_time_out($mchid, $qualitys)
  1118. {
  1119. $model_merchant = Model('merchant');
  1120. $merchant = $model_merchant->getMerchantInfo(['mchid' => $mchid], '*', true);
  1121. $org_quality = $merchant['quality'];
  1122. $retry_times_cfg = $this->merchant_retry_times($merchant);
  1123. $day_timeout = 0;
  1124. $night_timeout = 0;
  1125. $quality_ploy = new quality_ploy();
  1126. $qualities = $quality_ploy->qualities($org_quality);
  1127. foreach ($qualities as $quality) {
  1128. $day_timeout += $retry_times_cfg['qualities'][$quality]['day_secs'];
  1129. $night_timeout += $retry_times_cfg['qualities'][$quality]['night_secs'];
  1130. }
  1131. if ($this->is_day()) {
  1132. $time_out = $day_timeout;
  1133. } else {
  1134. $time_out = $night_timeout;
  1135. }
  1136. $update['day_timeout'] = $day_timeout;
  1137. $update['night_timeout'] = $night_timeout;
  1138. $update['time_out'] = $time_out;
  1139. $model_merchant->editMerchant($update, ['mchid' => $mchid]);
  1140. }
  1141. private function is_day(): bool
  1142. {
  1143. $hour = date("H");
  1144. if ($hour >= 23 || $hour <= 7) {
  1145. return false;
  1146. } else {
  1147. return true;
  1148. }
  1149. }
  1150. private function merchant_intercept($merchant)
  1151. {
  1152. $intercept_cfg = $merchant['intercept_cfg'];
  1153. if (empty($intercept_cfg)) {
  1154. $intercept_cfg = ['card_states' => [], 'is_transfer' => 0];
  1155. } else {
  1156. $intercept_cfg = unserialize($intercept_cfg);
  1157. }
  1158. return $intercept_cfg;
  1159. }
  1160. private function merchant_retry_times($merchant)
  1161. {
  1162. $retry_times_cfg = $merchant['retry_times_cfg'];
  1163. if (empty($retry_times_cfg)) {
  1164. $retry_times_cfg = [];
  1165. } else {
  1166. $retry_times_cfg = unserialize($retry_times_cfg);
  1167. }
  1168. return $retry_times_cfg;
  1169. }
  1170. private function transfer_cfg($merchant)
  1171. {
  1172. $transfer_cfg = $merchant['transfer_cfg'];
  1173. if (empty($transfer_cfg)) {
  1174. $transfer_cfg = ['transfer_opened' => 0, 'transfer_mchid' => 0, 'transfer_time' => 0];
  1175. } else {
  1176. $transfer_cfg = unserialize($transfer_cfg);
  1177. }
  1178. return $transfer_cfg;
  1179. }
  1180. }