merchant.php 59 KB

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