merchant.php 57 KB

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