merchant.php 54 KB

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