Cash.php 16 KB

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