|
@@ -0,0 +1,242 @@
|
|
|
|
+<?php
|
|
|
|
+namespace Account\Lib;
|
|
|
|
+use Dever;
|
|
|
|
+class Profit
|
|
|
|
+{
|
|
|
|
+ # 获取详情
|
|
|
|
+ public function get($uid, $profit_id, $config_id, $project_id)
|
|
|
|
+ {
|
|
|
|
+ $data['uid'] = $uid;
|
|
|
|
+ $data['config_id'] = $config_id;
|
|
|
|
+ $data['project_id'] = $project_id;
|
|
|
|
+ $data['config_profit_id'] = $profit_id;
|
|
|
|
+ $data['clear'] = true;
|
|
|
|
+ $info = Dever::db('account/profit')->find($data);
|
|
|
|
+ if (!$info) {
|
|
|
|
+ $info['id'] = Dever::db('account/profit')->insert($data);
|
|
|
|
+ if (!$info['id']) {
|
|
|
|
+ return Dever::alert('账户信息不存在');
|
|
|
|
+ }
|
|
|
|
+ $info['cash'] = '0.00';
|
|
|
|
+ $info['ycash'] = '0.00';
|
|
|
|
+ }
|
|
|
|
+ return $info;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ # 获取日志列表
|
|
|
|
+ public function getList($uid, $profit_id)
|
|
|
|
+ {
|
|
|
|
+ $where['profit_id'] = $profit_id;
|
|
|
|
+ $where['uid'] = $uid;
|
|
|
|
+ $data = Dever::db('account/profit_log')->getData($where, function(&$info) {
|
|
|
|
+ return $this->info($info);
|
|
|
|
+ });
|
|
|
|
+
|
|
|
|
+ return $data;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ # 获取日志详情
|
|
|
|
+ public function getInfo($id)
|
|
|
|
+ {
|
|
|
|
+ $data = Dever::db('account/profit_log')->find($id);
|
|
|
|
+ $data = $this->info($data);
|
|
|
|
+ return $data;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ # 获取详情
|
|
|
|
+ private function info($info)
|
|
|
|
+ {
|
|
|
|
+ $info['username'] = '无';
|
|
|
|
+ $profit = Dever::db('account/profit')->find($info['profit_id']);
|
|
|
|
+ $status = Dever::db('account/profit_log')->config['status'];
|
|
|
|
+ $config = Dever::db('account/config')->find($profit['config_id']);
|
|
|
|
+ $project = Dever::db('account/config_project')->find($config['project_id']);
|
|
|
|
+ $source = Dever::db($project['source'])->find($info['uid']);
|
|
|
|
+ if ($source) {
|
|
|
|
+ $info['username'] = $source[$project['source_name']];
|
|
|
|
+ }
|
|
|
|
+ $info['config_name'] = $config['name'];
|
|
|
|
+ $info['status_name'] = Dever::status($status, $info['status']);
|
|
|
|
+ $info['cdate_string'] = date('Y-m-d H:i', $info['cdate']);
|
|
|
|
+ if ($info['fdate']) {
|
|
|
|
+ $info['fdate_string'] = date('Y-m-d H:i', $info['fdate']);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return $info;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ # 入账
|
|
|
|
+ public function up_commit($uid, $cash, $profit, $status, $source, $source_id, $name = '', $desc = '')
|
|
|
|
+ {
|
|
|
|
+ if ($cash == 0) {
|
|
|
|
+ return Dever::alert('金额不能为空0');
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (!is_array($profit)) {
|
|
|
|
+ $profit = Dever::db('account/config_profit')->find(array('key' => $profit));
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (!$profit) {
|
|
|
|
+ return Dever::alert('收益类型不存在');
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ $config = Dever::db('account/config')->find($profit['config_id']);
|
|
|
|
+ if (!$config) {
|
|
|
|
+ return Dever::alert('账户信息不存在');
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ $project = Dever::db('account/config_project')->find($config['project_id']);
|
|
|
|
+ $user = Dever::db($project['source'])->find($uid);
|
|
|
|
+ if (!$user) {
|
|
|
|
+ return Dever::alert('账户信息不存在');
|
|
|
|
+ }
|
|
|
|
+ $info = $this->get($uid, $profit['id'], $config['id'], $config['project_id']);
|
|
|
|
+
|
|
|
|
+ # 开始处理入账
|
|
|
|
+ $this->handle($profit, $uid, $cash, $info['id'], $project['source'], $source, $source_id, $name, $status, $desc);
|
|
|
|
+
|
|
|
|
+ return true;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ # 入账
|
|
|
|
+ private function add($uid, $cash, $profit_id, $status, $source, $source_id, $name = '', $desc = '')
|
|
|
|
+ {
|
|
|
|
+ $data['clear'] = true;
|
|
|
|
+ $data['profit_id'] = $profit_id;
|
|
|
|
+ $data['source'] = $source;
|
|
|
|
+ $data['source_id'] = $source_id;
|
|
|
|
+
|
|
|
|
+ $log = Dever::db('account/profit_log')->one($data);
|
|
|
|
+ $data['uid'] = $uid;
|
|
|
|
+
|
|
|
|
+ if ($name) {
|
|
|
|
+ $data['name'] = $name;
|
|
|
|
+ }
|
|
|
|
+ if ($desc) {
|
|
|
|
+ $data['desc'] = $desc;
|
|
|
|
+ }
|
|
|
|
+ $data['status'] = $status;
|
|
|
|
+ if ($log) {
|
|
|
|
+ # 结算
|
|
|
|
+ if ($cash > 0 && $log['cash'] != $cash) {
|
|
|
|
+ $status = 3;
|
|
|
|
+ if ($cash > $log['cash']) {
|
|
|
|
+ $cash = $log['cash'];
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ if ($status == 2) {
|
|
|
|
+ $data['ycash'] = $cash;
|
|
|
|
+ } elseif ($status == 3) {
|
|
|
|
+ $data['ycash'] = $log['ycash'] + $cash;
|
|
|
|
+ } elseif ($status == 4) {
|
|
|
|
+ $data['ycash'] = 0;
|
|
|
|
+ }
|
|
|
|
+ $data['where_id'] = $log['id'];
|
|
|
|
+ Dever::db('account/profit_log')->update($data);
|
|
|
|
+
|
|
|
|
+ $update['where_id'] = $profit_id;
|
|
|
|
+ $update['set_ycash'] = $data['ycash'];
|
|
|
|
+ Dever::db('account/profit')->incYcash($update);
|
|
|
|
+ } else {
|
|
|
|
+ # 入账
|
|
|
|
+ $data['cash'] = $cash;
|
|
|
|
+ if ($status == 2) {
|
|
|
|
+ $data['ycash'] = $cash;
|
|
|
|
+ }
|
|
|
|
+ $data['order_num'] = $this->createOrderNum();
|
|
|
|
+ $id = Dever::db('account/profit_log')->insert($data);
|
|
|
|
+
|
|
|
|
+ $update['where_id'] = $profit_id;
|
|
|
|
+ $update['set_cash'] = $data['cash'];
|
|
|
|
+ Dever::db('account/profit')->incCash($update);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ # 生成订单号
|
|
|
|
+ private function createOrderNum()
|
|
|
|
+ {
|
|
|
|
+ $where['order_num'] = Dever::order('S');
|
|
|
|
+ $state = Dever::db('account/profit_log')->one($where);
|
|
|
|
+ if (!$state) {
|
|
|
|
+ return $where['order_num'];
|
|
|
|
+ } else {
|
|
|
|
+ return $this->createOrderNum();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ # 计算金额
|
|
|
|
+ private function handle($profit, $uid, $cash, $profit_id, $project_source, $source, $source_id, $name, $status, $desc)
|
|
|
|
+ {
|
|
|
|
+ $rule = Dever::db('account/config_profit_rule')->select(array('profit_id' => $profit['id']));
|
|
|
|
+ if ($rule) {
|
|
|
|
+ $child = array();
|
|
|
|
+ foreach ($rule as $k => $v) {
|
|
|
|
+ $child[$v['level']] = $this->rule($v, $child, $uid, $cash, $profit_id, $project_source, $source, $source_id, $name, $status, $desc);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return $cash;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private function rule($rule, $child, $uid, $cash, $profit_id, $project_source, $source, $source_id, $name, $status, $desc)
|
|
|
|
+ {
|
|
|
|
+ if ($rule['level'] > 0) {
|
|
|
|
+ $parent = Dever::load('invite/api')->getParent($uid, $rule['level']);
|
|
|
|
+ if ($parent) {
|
|
|
|
+ $uid = $parent;
|
|
|
|
+ } else {
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ $replace = array();
|
|
|
|
+ $replace['{a}'] = $cash;
|
|
|
|
+ $replace['{b}'] = $uid;
|
|
|
|
+ $user = Dever::db($project_source)->find($uid);
|
|
|
|
+ if (isset($user['role']) && $user['role']) {
|
|
|
|
+ $temp = explode(',', $user['role']);
|
|
|
|
+ $replace['{c}'] = $temp[0];
|
|
|
|
+ if (isset($temp[1]) && $temp[1]) {
|
|
|
|
+ $level = $temp[1];
|
|
|
|
+ $level = Dever::db('user/role_level')->find($level);
|
|
|
|
+ if ($level) {
|
|
|
|
+ $replace['{d}'] = $level['level'];
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ # 验证条件
|
|
|
|
+ $condition = $rule['condition'];
|
|
|
|
+ if ($condition) {
|
|
|
|
+ $check = $this->run($condition, $replace);
|
|
|
|
+ if (!$check) {
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ $level = $rule['level'] - 1;
|
|
|
|
+ if ($child && isset($child[$level]) && $child[$level]) {
|
|
|
|
+ $replace['{e}'] = $child[$level];
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ # 计算公式
|
|
|
|
+ $exp = $rule['exp'];
|
|
|
|
+ if ($exp) {
|
|
|
|
+ $cash = $this->run($exp, $replace);
|
|
|
|
+ }
|
|
|
|
+ if ($cash > 0) {
|
|
|
|
+ $this->add($uid, $cash, $profit_id, $status, $source, $source_id, $name, $desc);
|
|
|
|
+ return $cash;
|
|
|
|
+ }
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private function run($string, $replace)
|
|
|
|
+ {
|
|
|
|
+ $keys = array_keys($replace);
|
|
|
|
+ $values = array_values($replace);
|
|
|
|
+ $string = str_replace($keys, $values, $string);
|
|
|
|
+ $eval = '$value = ' . $string . ';';
|
|
|
|
+ eval($eval);
|
|
|
|
+ return $value;
|
|
|
|
+ }
|
|
|
|
+}
|