Cash.php 15 KB

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