Cash.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. <?php
  2. namespace Option\Lib;
  3. use Dever;
  4. class Cash
  5. {
  6. private function getInfo($type, $cash)
  7. {
  8. if ($type > 3) {
  9. $cash_type = 2;
  10. } else {
  11. $cash_type = 1;
  12. }
  13. if ($type == 1 || $type == 4) {
  14. $table = 'jiaofu';
  15. $prefix = 'J';
  16. } elseif ($type == 2 || $type == 5) {
  17. $table = 'fafang';
  18. $prefix = 'F';
  19. } elseif ($type == 3 || $type == 6) {
  20. $table = 'duifu';
  21. $prefix = 'D';
  22. $cash = -1*$cash;
  23. }
  24. return array($table, $cash, $cash_type, $prefix);
  25. }
  26. public function resetParent($uid, $parent)
  27. {
  28. # 获取所有下级
  29. $child = $this->getChild($uid, false);
  30. if ($child) {
  31. foreach ($child as $k => $v) {
  32. if ($v['level'] > 1) {
  33. $this->update(array('id' => $v['id']), array('uid' => $parent));
  34. }
  35. }
  36. }
  37. return $this->delete(array('to_uid' => $uid, 'level' => 1), array('uid' => $parent));
  38. }
  39. public function up($aid, $mid, $type, $cash, $desc, $alert = true)
  40. {
  41. if ($cash == 0) {
  42. return false;
  43. }
  44. $account = Dever::db('option/account')->find(array('id' => $aid, 'clear' => true));
  45. if ($account['status'] <= 2) {
  46. list($table, $cash, $cash_type, $prefix) = $this->getInfo($type, $cash);
  47. $where = array();
  48. $where['clear'] = true;
  49. $where['aid'] = $aid;
  50. $where['mid'] = $mid;
  51. $where['type'] = $cash_type;
  52. $info = false;
  53. if (!$info) {
  54. $data = $where;
  55. $data['order_num'] = $this->getOrderId($table, $prefix);
  56. $col = $table;
  57. $cash_info = Dever::db('option/cash')->find($where);
  58. if (!$cash_info) {
  59. Dever::db('option/cash')->insert($where);
  60. $cash_info = Dever::db('option/cash')->find($where);
  61. }
  62. if ($table == 'fafang') {
  63. $data['yue'] = $cash_info[$col] + $cash;
  64. $data['status'] = 1;
  65. if ($cash_info['jiaofu'] < $cash) {
  66. if ($alert) {
  67. Dever::alert('交付期权价值不足发放');
  68. } else {
  69. return false;
  70. $data['status'] = 2;
  71. }
  72. }
  73. } elseif ($table == 'duifu') {
  74. $data['yue'] = $cash_info[$col] + $cash;
  75. $data['status'] = 1;
  76. if ($cash_info['fafang'] < $cash) {
  77. if ($alert) {
  78. Dever::alert('发放期权价值不足兑付');
  79. } else {
  80. return false;
  81. $data['status'] = 2;
  82. }
  83. }
  84. } else {
  85. $col = 'dai' . $table;
  86. $data['status'] = 2;
  87. }
  88. $data['cash'] = $cash;
  89. $data['desc'] = $desc;
  90. $data['clear'] = true;
  91. $state = Dever::db('option/bill_' . $table)->insert($data);
  92. if ($state && $cash_info) {
  93. # 更新余额
  94. $update = array();
  95. $update['where_id'] = $cash_info['id'];
  96. $update['set_cash'] = $cash;
  97. $update['clear'] = true;
  98. $method = 'upCash_' . $col;
  99. if ($table == 'fafang') {
  100. $update['jiaofu'] = $cash;
  101. if ($data['status'] == 2) {
  102. $update['jiaofu'] = 0;
  103. }
  104. } elseif ($table == 'duifu') {
  105. $update['fafang'] = $cash;
  106. if ($data['status'] == 2) {
  107. $update['fafang'] = 0;
  108. }
  109. } else {
  110. $update['daijiaofu_date'] = time();
  111. }
  112. Dever::db('option/cash')->$method($update);
  113. }
  114. }
  115. return $state;
  116. }
  117. return false;
  118. }
  119. /**
  120. * 生成订单号
  121. *
  122. * @return mixed
  123. */
  124. public function getOrderId($table, $prefix)
  125. {
  126. $where['order_num'] = Dever::order($prefix);
  127. $state = Dever::db('option/bill_' . $table)->one($where);
  128. if (!$state) {
  129. return $where['order_num'];
  130. } else {
  131. return $this->getOrderId($table, $prefix);
  132. }
  133. }
  134. # 获取日志列表
  135. public function getList($aid, $table, $status, $name, $audit = false)
  136. {
  137. $where = array();
  138. $where['aid'] = $aid;
  139. if ($status > 0) {
  140. $where['status'] = $status;
  141. }
  142. if ($audit) {
  143. $where['audit'] = $audit;
  144. }
  145. $type = Dever::input('type');
  146. if ($type) {
  147. $where['type'] = $type;
  148. }
  149. $table = 'option/bill_' . $table;
  150. if ($status == 2) {
  151. $data = Dever::db($table)->select($where);
  152. } else {
  153. $data = Dever::db($table)->getData($where);
  154. }
  155. if ($data) {
  156. $audit = Dever::db($table)->config['config_audit'];
  157. foreach ($data as $k => $v) {
  158. $data[$k] = $this->getOne($v, $name, $audit);
  159. }
  160. }
  161. return $data;
  162. }
  163. # 获取日志详情
  164. public function getView($id, $aid, $table, $name)
  165. {
  166. if (!$id) {
  167. Dever::alert('参数错误');
  168. }
  169. $where = array();
  170. $where['id'] = $id;
  171. $where['aid'] = $aid;
  172. $table = 'option/bill_' . $table;
  173. $data = Dever::db($table)->find($where);
  174. if ($data) {
  175. $audit = Dever::db($table)->config['config_audit'];
  176. $data = $this->getOne($data, $name, $audit);
  177. }
  178. return $data;
  179. }
  180. # 获取详情
  181. private function getOne($data, $name, $audit)
  182. {
  183. if ($data['type'] == 1) {
  184. $data['type_name'] = '期权' . $name ;
  185. } else {
  186. $data['type_name'] = '原始期权' . $name;
  187. }
  188. $data['audit_name'] = $audit[$data['audit']];
  189. $data['cdate_string'] = date('Y-m-d H:i', $data['cdate']);
  190. return $data;
  191. }
  192. # 确认期权价值 废弃
  193. /*
  194. public function setValue_commit($info)
  195. {
  196. $cash = array();
  197. $state = Dever::db('option/bill_jiaofu')->update(array('status' => 1, 'where_id' => $info['id']));
  198. if ($state) {
  199. $account_cash = Dever::db('option/cash')->find(array('type' => $info['type'], 'aid' => $info['aid']));
  200. if ($account_cash) {
  201. $update['where_id'] = $account_cash['id'];
  202. $update['daijiaofu'] = $account_cash['daijiaofu'] - $info['cash'];
  203. $update['jiaofu'] = $account_cash['jiaofu'] + $info['cash'];
  204. if ($update['daijiaofu'] < 0) {
  205. $update['daijiaofu'] = 0;
  206. }
  207. $update['clear'] = true;
  208. Dever::db('option/cash')->update($update);
  209. # 获取待发放数据 对之前发放的数据进行发放
  210. $fafang = Dever::db('option/bill_fafang')->find(array('status' => 2, 'type' => $info['type'], 'aid' => $info['aid']));
  211. if ($fafang) {
  212. foreach ($fafang as $v) {
  213. if ($info['cash'] >= $v['cash']) {
  214. Dever::db('option/bill_fafang')->update(array('where_id' => $v['id'], 'status' => 1));
  215. $info['cash'] -= $v['cash'];
  216. }
  217. }
  218. }
  219. }
  220. }
  221. }
  222. */
  223. # 确认期权价值
  224. public function setValue_commit($info)
  225. {
  226. $this->setValue_act($info);
  227. }
  228. # 确认期权价值
  229. public function setValue_act($info)
  230. {
  231. $account_cash = Dever::db('option/cash')->select(array('aid' => $info['aid']));
  232. if ($account_cash) {
  233. foreach ($account_cash as $k => $v) {
  234. $cash = Dever::db('option/bill_jiaofu')->getTotal(array('where_end' => $info['jiaofu_date'], 'where_type' => $v['type'], 'where_aid' => $v['aid'], 'where_mid' => $v['mid'], 'where_status' => 2));
  235. if ($cash && $cash['total'] > 0) {
  236. $update = array();
  237. $update['where_id'] = $v['id'];
  238. $update['daijiaofu'] = $v['daijiaofu'] - $cash['total'];
  239. $update['jiaofu'] = $v['jiaofu'] + $cash['total'];
  240. if ($update['daijiaofu'] < 0) {
  241. $update['daijiaofu'] = 0;
  242. }
  243. $update['clear'] = true;
  244. Dever::db('option/cash')->update($update);
  245. # 获取待发放数据 对之前发放的数据进行发放
  246. $fafang = Dever::db('option/bill_fafang')->find(array('status' => 2, 'type' => $v['type'], 'aid' => $v['aid'], 'mid' => $v['mid']));
  247. if ($fafang) {
  248. foreach ($fafang as $v1) {
  249. if ($cash['total'] >= $v1['cash']) {
  250. Dever::db('option/bill_fafang')->update(array('where_id' => $v['id'], 'status' => 1));
  251. $cash['total'] -= $v1['cash'];
  252. }
  253. }
  254. }
  255. Dever::db('option/bill_jiaofu')->upYes(array('where_end' => $info['jiaofu_date'], 'where_aid' => $v['aid'], 'where_mid' => $v['mid'], 'where_type' => $v['type'], 'where_status' => 2, 'set_agreement_id' => $info['id'], 'set_status' => 1, 'set_qdate' => time()));
  256. }
  257. }
  258. }
  259. }
  260. # 展示详情
  261. public function show()
  262. {
  263. $id = Dever::input('id');
  264. $config = Dever::db('bill/cash')->config['set'];
  265. $info = Dever::db('bill/cash')->one($id);
  266. $status = $config['status'][$info['status']];
  267. $type = $config['type'][$info['type']];
  268. $member = Dever::db('agent/member')->find($info['mid']);
  269. $role = Dever::db('setting/role')->one($member['role']);
  270. $level = Dever::db('setting/level')->one($member['level_id']);
  271. if ($member['shop_id']) {
  272. $shop = Dever::db('shop/info')->one($member['shop_id']);
  273. } else {
  274. $shop['id'] = -1;
  275. $shop['name'] = '无';
  276. }
  277. $cdate = date('Y-m-d H:i', $info['cdate']);
  278. if ($info['operdate']) {
  279. $opertime = date('Y-m-d H:i', $info['operdate']);
  280. } else {
  281. $opertime = '';
  282. }
  283. $result = array();
  284. $result['代理商信息'] = array
  285. (
  286. 'type' => 'info',
  287. 'content' => array
  288. (
  289. array
  290. (
  291. array('代理商', $member['name'] . ' ' . $member['mobile']),
  292. array('代理角色', $role['name'] . ($level ? '('.$level['name'].')' : '')),
  293. array('所属店铺', $shop['name']),
  294. ),
  295. array
  296. (
  297. array('资金余额', '¥' . $member['cash'] . '元'),
  298. //array('直推业绩', '¥' . $member['sell'] . '元'),
  299. array('团队业绩', '¥' . $member['group_sell'] . '元'),
  300. ),
  301. ),
  302. );
  303. $result['交易信息'] = array
  304. (
  305. 'type' => 'info',
  306. 'content' => array
  307. (
  308. array
  309. (
  310. array('流水号', $info['order_num']),
  311. array('交易时间', $cdate),
  312. array('交易类型', $type),
  313. ),
  314. array
  315. (
  316. array('交易金额', '¥' . $info['cash'] . '元'),
  317. array('交易后账户余额', '¥' . $info['yue'] . '元'),
  318. array('交易说明', $info['desc']),
  319. ),
  320. )
  321. );
  322. $result['审核信息'] = array
  323. (
  324. 'type' => 'info',
  325. 'content' => array
  326. (
  327. array
  328. (
  329. array('审核时间', $opertime),
  330. array('审核状态', $status),
  331. array('备注', $info['audit_desc']),
  332. ),
  333. )
  334. );
  335. # 提现信息
  336. $button = array();
  337. if ($info['status'] == 1) {
  338. $button[] = array
  339. (
  340. 'type' => 'edit',
  341. 'link' => Dever::url('project/database/update?project=bill&table=cash&where_id='.$info['id'].'&col=audit,audit_desc&oper_save_jump=cash&oper_table=cash&oper_parent=cash', 'manage'),
  342. 'name' => '审核',
  343. );
  344. }
  345. if ($info['type'] == 11) {
  346. $tixian = Dever::db('bill/tixian')->find($info['type_id']);
  347. if ($tixian) {
  348. if ($info['status'] == 2 && $tixian['status'] == 1) {
  349. $button[] = array
  350. (
  351. 'type' => 'edit',
  352. 'link' => Dever::url('project/database/update?project=bill&table=tixian&where_id='.$tixian['id'].'&col=audit,audit_desc,pic&oper_save_jump=tixian&oper_table=tixian&oper_parent=tixian', 'manage'),
  353. 'name' => '发放',
  354. );
  355. }
  356. $bank = Dever::db('setting/bank')->find($tixian['bank']);
  357. $result['提现信息'] = array
  358. (
  359. 'type' => 'info',
  360. 'content' => array
  361. (
  362. array
  363. (
  364. array('银行名称', $bank['name']),
  365. array('开户行', $tixian['bankname']),
  366. ),
  367. array
  368. (
  369. array('姓名', $tixian['name']),
  370. array('卡号', $tixian['card']),
  371. ),
  372. )
  373. );
  374. if ($tixian['status'] == 2) {
  375. if ($tixian['operdate']) {
  376. $opertime = date('Y-m-d H:i', $tixian['operdate']);
  377. } else {
  378. $opertime = '';
  379. }
  380. $pic = '';
  381. if ($tixian['pic']) {
  382. $temp = explode(',', $tixian['pic']);
  383. foreach ($temp as $k => $v) {
  384. $pic .= '<a href="'.Dever::pic($v).'" target="_blank"><img src="'.Dever::pic($v).'" width="150" /></a>';
  385. }
  386. }
  387. $result['发放信息'] = array
  388. (
  389. 'type' => 'info',
  390. 'content' => array
  391. (
  392. array
  393. (
  394. array('发放时间', $opertime),
  395. array('备注', $tixian['audit_desc']),
  396. ),
  397. array
  398. (
  399. array('凭证', $pic),
  400. ),
  401. )
  402. );
  403. }
  404. }
  405. }
  406. $head = array
  407. (
  408. 'name' => '基本信息',
  409. 'btn' => $button,
  410. );
  411. $html = Dever::show($head, $result);
  412. return $html;
  413. }
  414. }