Cash.php 15 KB

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