refill_amount_stats.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. <?php
  2. class refill_amount_statsControl extends SystemControl
  3. {
  4. private $setting_key;
  5. public function __construct()
  6. {
  7. parent::__construct();
  8. $this->setting_key = 'amount_stats_settings';
  9. }
  10. public function indexOp()
  11. {
  12. Tpl::showpage('refill_amount_stats.index');
  13. }
  14. public function stats_settingsOp()
  15. {
  16. $data = $this->get_stats_settings();
  17. exit(json_encode($data));
  18. }
  19. public function stats_settings_saveOp()
  20. {
  21. $writer = function ($key,$val) {
  22. $cache = rcache($this->setting_key, 'refill-');
  23. $data = unserialize($cache['data']) ?? [];
  24. $data[$key] = $val;
  25. wcache($this->setting_key, ['data' => serialize($data)], 'refill-');
  26. };
  27. $writer($_POST['key'], $_POST['value']);
  28. $data = $this->get_stats_settings();
  29. exit(json_encode($data));
  30. }
  31. private function get_stats_settings(): array
  32. {
  33. $settings = rcache($this->setting_key, 'refill-');
  34. $settings = unserialize($settings['data']);
  35. return [
  36. 'state' => true,
  37. 'channel_names' => $settings['channel_names'] ?? [],
  38. 'merchant_names' => $settings['merchant_names'] ?? [],
  39. 'channel_fast' => $settings['channel_fast'] ?? [],
  40. 'channel_normal' => $settings['channel_normal'] ?? [],
  41. 'merchant' => $settings['merchant'] ?? [],
  42. 'daily_channel' => $settings['daily_channel'] ?? [],
  43. 'daily_merchant' => $settings['daily_merchant'] ?? []
  44. ];
  45. }
  46. public function sys_channelsOp()
  47. {
  48. global $config;
  49. $provider_model = Model('refill_provider');
  50. $condition['opened'] = 1;
  51. $provider_items = $provider_model->table('refill_provider,store')
  52. ->field('refill_provider.name,refill_provider.qualitys,refill_provider.type,store.store_name,store.store_id')
  53. ->join('inner')
  54. ->on('store.store_id=refill_provider.store_id')
  55. ->where($condition)
  56. ->order('opened asc, name asc')
  57. ->select();
  58. $provider_items = array_map(function ($item) use($config) {
  59. $quality_text = $item['type'] == mtopcard\OilCardPaper ? $config['oil_quality_text'] : $config['phone_quality_text'];
  60. return [
  61. 'store_id' => $item['store_id'],
  62. 'name' => $item['name'],
  63. 'quality_text' => $quality_text[$item['qualitys']],
  64. 'store_name' => $item['store_name'],
  65. 'tag' => ADMIN_NAME
  66. ];
  67. }, $provider_items);
  68. $data = [
  69. 'state' => true,
  70. 'list' => $provider_items
  71. ];
  72. exit(json_encode($data));
  73. }
  74. public function channel_dataOp()
  75. {
  76. $store_ids = $_GET['store_ids'];
  77. if (empty($store_ids)) {
  78. $data = [
  79. 'state' => true,
  80. 'list' => []
  81. ];
  82. exit(json_encode($data));
  83. }
  84. $condition['refill_provider.store_id'] = ['in', $store_ids];
  85. $provider_model = Model('refill_provider');
  86. $provider_items = $provider_model->table('refill_provider,store')
  87. ->field('refill_provider.*,store.store_name,store.member_id')
  88. ->join('inner')
  89. ->on('store.store_id=refill_provider.store_id')
  90. ->where($condition)
  91. ->order('opened asc, name asc')
  92. ->select();
  93. $providers = [];
  94. foreach ($provider_items as $provider)
  95. {
  96. $pid = $provider['provider_id'];
  97. $providers[$pid] = $provider;
  98. $providers[$pid]['available_predeposit'] = 0;
  99. $account_id = intval($provider['account_id']);
  100. if($account_id > 0) {
  101. $mid_pids[$account_id] = $pid;
  102. $mids[] = $account_id;
  103. }
  104. }
  105. if(!empty($mids))
  106. {
  107. $member_data = Model('member')->field('member_id,available_predeposit')->where(['member_id' => ['in',$mids]])->select();
  108. foreach ($member_data as $member)
  109. {
  110. $mid = intval($member['member_id']);
  111. if(array_key_exists($mid,$mid_pids)) {
  112. $pid = $mid_pids[$mid];
  113. $providers[$pid]['available_predeposit'] = $member['available_predeposit'];
  114. }
  115. }
  116. }
  117. $list = [];
  118. foreach ($providers as $val){
  119. $item = [
  120. 'tag' => ADMIN_NAME,
  121. 'store_name' => $val['store_name'],
  122. 'name' => $val['name'],
  123. 'balance' => $val['balance'] ?? 0,
  124. 'available_predeposit' => $val['available_predeposit'] ?? 0,
  125. ];
  126. $list[] = $item;
  127. }
  128. $data = [
  129. 'state' => true,
  130. 'list' => $list
  131. ];
  132. exit(json_encode($data));
  133. }
  134. public function sys_mchsOp()
  135. {
  136. $model_merchant = Model('merchant');
  137. $condition['merchant_state'] = 1;
  138. $merchant_list = $model_merchant->getMerchantList($condition, 200, 'available_predeposit desc,merchant_state asc,mchid desc', true);
  139. $merchant_list = array_map(function($item) {
  140. return [
  141. 'mch_id' => $item['mchid'],
  142. 'name' => $item['name'],
  143. 'mch_name' => $item['company_name'],
  144. 'tag' => ADMIN_NAME
  145. ];
  146. }, $merchant_list);
  147. $data = [
  148. 'state' => true,
  149. 'list' => $merchant_list
  150. ];
  151. exit(json_encode($data));
  152. }
  153. public function mch_dataOp()
  154. {
  155. $mch_ids = $_GET['mch_ids'];
  156. if (empty($mch_ids)) {
  157. $data = [
  158. 'state' => true,
  159. 'list' => []
  160. ];
  161. exit(json_encode($data));
  162. }
  163. $condition['merchant.mchid'] = ['in', $mch_ids];
  164. $model_merchant = Model('merchant');
  165. $merchant_list = $model_merchant->getMerchantList($condition, 200, 'available_predeposit desc,merchant_state asc,mchid desc', true);
  166. $merchant_debts = rcache("merchant-debts-detail", 'refill-');
  167. foreach ($merchant_list as $key => $merchant) {
  168. $mchid = $merchant['mchid'];
  169. $available_predeposit = $merchant['available_predeposit'] - $merchant['credit_bonus'];
  170. $merchant_list[$key]['available_predeposit'] = $available_predeposit;
  171. if (array_key_exists($mchid, $merchant_debts)) {
  172. $debt_detail = unserialize($merchant_debts[$mchid]);
  173. $merchant_list[$key]['debt'] = $debt_detail;
  174. } else {
  175. $merchant_list[$key]['debt'] = ['debt' => 0, 'send_amounts' => 0, 'lack_amounts' => 0];
  176. }
  177. }
  178. $list = [];
  179. foreach ($merchant_list as $val){
  180. $item = [
  181. 'tag' => ADMIN_NAME,
  182. 'mch_id' => $val['mchid'],
  183. 'mch_name' => $val['company_name'],
  184. 'name' => $val['name'],
  185. 'debt' => $val['debt']['debt'] ?? 0,
  186. 'available_predeposit' => $val['available_predeposit'] ?? 0,
  187. ];
  188. $list[] = $item;
  189. }
  190. $data = [
  191. 'state' => true,
  192. 'list' => $list
  193. ];
  194. exit(json_encode($data));
  195. }
  196. public function daily_statementOp()
  197. {
  198. Tpl::showpage('refill_amount_stats.daily_statement');
  199. }
  200. public function daily_provider_dataOp()
  201. {
  202. if (!$_GET['cid'] || !$_GET['query_day']) {
  203. $data = [
  204. 'state' => true,
  205. 'list' => []
  206. ];
  207. exit(json_encode($data));
  208. }
  209. $provider_model = Model('');
  210. $provider_condition['opened'] = 1;
  211. $provider_condition['store.store_id'] = ['in', $_GET['cid']];
  212. $provider_items = $provider_model->table('refill_provider,store')
  213. ->field('store.store_name,store.store_id,refill_provider.provider_id,refill_provider.name')
  214. ->join('inner')
  215. ->on('store.store_id=refill_provider.store_id')
  216. ->where($provider_condition)
  217. ->order('opened asc, name asc')
  218. ->select();
  219. $list = [];
  220. foreach ($provider_items as $item) {
  221. $store_id = $item['store_id'];
  222. $list[$store_id] = [
  223. 'day' => $_GET['query_day'],
  224. 'store_name' => $item['store_name'],
  225. 'name' => $item['name'],
  226. 'initial_balance' => 0,
  227. 'repayment_amount' => 0,
  228. 'tag' => ADMIN_NAME,
  229. 'store_id' => $store_id,
  230. 'notify_time' => [
  231. 'quantity' => 0,
  232. 'card_value' => 0,
  233. 'discounted_amount' => 0,
  234. ],
  235. 'order_time' => [
  236. 'quantity' => 0,
  237. 'card_value' => 0,
  238. 'discounted_amount' => 0,
  239. ]
  240. ];
  241. }
  242. $order_condition['type'] = 'provider';
  243. $order_condition['time_text'] = $_GET['query_day'];
  244. $order_condition['cid'] = ['in', $_GET['cid']];
  245. $model_refill_order = Model('refill_order');
  246. $stats_list = $model_refill_order->getOrderStatsListALl($order_condition);
  247. foreach ($stats_list as $item)
  248. {
  249. $store_id = $item['cid'];
  250. if (!array_key_exists($store_id, $list)) {
  251. continue;
  252. }
  253. $time_type = $item['order_time_type'];
  254. if (!in_array($time_type, ['notify_time', 'order_time'])) {
  255. continue;
  256. }
  257. $list[$store_id][$time_type]['quantity'] = $item['success_count'];
  258. $list[$store_id][$time_type]['card_value'] = $item['success_refill_amounts'];
  259. $list[$store_id][$time_type]['discounted_amount'] = $item['success_channel_amounts'];
  260. }
  261. $amount_condition['refill_provider.store_id'] = ['in', $_GET['cid']];
  262. $start_unixtime = intval(strtotime($_GET['query_day'] . '00:00:00'));
  263. $end_unixtime = $start_unixtime + 86400;
  264. $amount_condition['add_time'] = [['egt', $start_unixtime], ['lt', $end_unixtime], 'and'];
  265. $amount_mod = Model('provider_amount');
  266. $amount_list = $amount_mod->getAmountList($amount_condition,200,'provider_amount.amount,refill_provider.store_id', 'provider_amount.add_time desc, refill_provider.name asc', '', true);
  267. foreach ($amount_list as $value) {
  268. $store_id = $value['store_id'];
  269. $list[$store_id]['repayment_amount'] += $value['amount'];
  270. }
  271. $data = [
  272. 'state' => true,
  273. 'list' => array_values($list)
  274. ];
  275. exit(json_encode($data));
  276. }
  277. public function daily_merchant_dataOp()
  278. {
  279. if (!$_GET['cid'] || !$_GET['query_day']) {
  280. $data = [
  281. 'state' => true,
  282. 'list' => []
  283. ];
  284. exit(json_encode($data));
  285. }
  286. $model_merchant = Model('merchant');
  287. $mch_ids = $_GET['cid'];
  288. $condition['merchant.mchid'] = ['in', $mch_ids];
  289. $merchant_list = $model_merchant->getMerchantList($condition, 200, 'available_predeposit desc,merchant_state asc,mchid desc', true);
  290. $list = [];
  291. foreach ($merchant_list as $item) {
  292. $mchid = $item['mchid'];
  293. $list[$mchid] = [
  294. 'day' => $_GET['query_day'],
  295. 'mch_name' => $item['company_name'],
  296. 'name' => $item['name'],
  297. 'initial_balance' => 0,
  298. 'repayment_amount' => 0,
  299. 'tag' => ADMIN_NAME,
  300. 'mch_id' => $mchid,
  301. 'notify_time' => [
  302. 'quantity' => 0,
  303. 'card_value' => 0,
  304. 'discounted_amount' => 0,
  305. ],
  306. 'order_time' => [
  307. 'quantity' => 0,
  308. 'card_value' => 0,
  309. 'discounted_amount' => 0,
  310. ]
  311. ];
  312. }
  313. $order_condition['type'] = 'merchant';
  314. $order_condition['time_text'] = $_GET['query_day'];
  315. $order_condition['cid'] = ['in', $_GET['cid']];
  316. $model_refill_order = Model('refill_order');
  317. $stats_list = $model_refill_order->getOrderStatsList($order_condition, 200, '*', 'time_stamp desc, cname asc');
  318. foreach ($stats_list as $item)
  319. {
  320. $store_id = $item['cid'];
  321. if (!array_key_exists($store_id, $list)) {
  322. continue;
  323. }
  324. $time_type = $item['order_time_type'];
  325. if (!in_array($time_type, ['notify_time', 'order_time'])) {
  326. continue;
  327. }
  328. $list[$store_id][$time_type]['quantity'] = $item['success_count'];
  329. $list[$store_id][$time_type]['card_value'] = $item['success_refill_amounts'];
  330. $list[$store_id][$time_type]['discounted_amount'] = $item['success_mch_amounts'];
  331. }
  332. $start_unixtime = intval(strtotime($_GET['query_day'] . '00:00:00'));
  333. $end_unixtime = $start_unixtime + 86400;
  334. $amount_condition['add_time'] = [['egt', $start_unixtime], ['lt', $end_unixtime], 'and'];
  335. $amount_condition['mchid'] = ['in', $_GET['cid']];
  336. $amount_condition['check_time'] = ['gt', 0];
  337. $amount_condition['status'] = 2;
  338. $merchant_model = Model('merchant');
  339. $evidence_list = $merchant_model->getRefillEvidenceAll($amount_condition, 'refill_evidence.*,member.available_predeposit', 'refill_evidence.add_time desc');
  340. foreach ($evidence_list as $value)
  341. {
  342. $mchid = $value['mchid'];
  343. $list[$mchid]['repayment_amount'] += $value['amount'];
  344. }
  345. $data = [
  346. 'state' => true,
  347. 'list' => array_values($list)
  348. ];
  349. exit(json_encode($data));
  350. }
  351. }