Cash.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. <?php namespace Pay\Yspay;
  2. use Dever;
  3. class Cash
  4. {
  5. # 千分制
  6. private $num = 1000;
  7. # 备付金
  8. private $bfj = 100;
  9. # 入账:待确认
  10. public function add($merchant_id, $amount, $order_num, $source_order_num, $fenzhang = 0)
  11. {
  12. $merchant = Dever::db('pay/yspay_merchant')->one($merchant_id);
  13. if ($merchant) {
  14. $data = array();
  15. $data['account_id'] = $merchant['account_id'];
  16. $data['merchant_id'] = $merchant['id'];
  17. $data['order_num'] = $order_num;
  18. $data['source_order_num'] = $source_order_num;
  19. $info = Dever::db('pay/yspay_cash')->find($data);
  20. if ($amount && $amount > 0) {
  21. $this->getCash($amount, $merchant, $data, $fenzhang);
  22. }
  23. $data['status'] = 1;
  24. if ($info) {
  25. $data['where_id'] = $id = $info['id'];
  26. Dever::db('pay/yspay_cash')->update($data);
  27. } else {
  28. $id = Dever::db('pay/yspay_cash')->insert($data);
  29. }
  30. return $id;
  31. }
  32. return false;
  33. }
  34. # 获取账户信息
  35. public function getInfo($merchant_id)
  36. {
  37. $merchant = Dever::db('pay/yspay_merchant')->one($merchant_id);
  38. $yue = $merchant['hf_cash'] - $merchant['hf_tx_cash'];
  39. $yue = $yue/$this->num;
  40. $beifujin = 0;
  41. if ($yue >= $this->bfj) {
  42. $beifujin = $this->bfj;
  43. }
  44. $tixian = $yue - $beifujin;
  45. $info = '当前账户余额:¥' . Dever::number($yue) . '&nbsp;&nbsp;&nbsp;&nbsp;备付金:¥'.Dever::number($beifujin).'&nbsp;&nbsp;&nbsp;&nbsp;可提现余额:¥' . Dever::number($tixian);
  46. return $info;
  47. }
  48. # 修改状态
  49. public function up($id, $status = 2, $amount = false, $fenzhang = 0)
  50. {
  51. if ($amount) {
  52. $amount = $amount * 100;
  53. }
  54. if (is_numeric($id)) {
  55. $where['id'] = $id;
  56. } else {
  57. $where['source_order_num'] = $id;
  58. }
  59. $info = Dever::db('pay/yspay_cash')->find($where);
  60. if ($info) {
  61. $merchant = Dever::db('pay/yspay_merchant')->one($info['merchant_id']);
  62. if ($merchant) {
  63. $update = array('status' => $status, 'where_id' => $info['id']);
  64. if ($status == 2) {
  65. if ($amount && $amount > 0 && $info['ycash'] != $amount) {
  66. $this->getCash($amount, $merchant, $update, $fenzhang);
  67. }
  68. $update['fdate'] = time();
  69. }
  70. $state = Dever::db('pay/yspay_cash')->update($update);
  71. if ($state) {
  72. if ($status == 2) {
  73. $info = Dever::db('pay/yspay_cash')->find(array('id' => $info['id'], 'clear' => true));
  74. $total = Dever::db('pay/yspay_cash')->getTotal(array('status' => 2, 'merchant_id' => $info['merchant_id']));
  75. if ($total) {
  76. Dever::load('pay/yspay/merchant')->log($info['merchant_id'], 1, $info['hf_cash']);
  77. Dever::db('pay/yspay_merchant')->update(array('where_id' => $info['merchant_id'], 'cash' => $total['cash'], 'hf_cash' => $total['hf_cash'], 'fz_cash' => $total['fz_cash']));
  78. }
  79. $info = Dever::db('pay/yspay_cash')->find(array('id' => $info['id'], 'clear' => true));
  80. if ($info['fz_cash'] && $info['fz_cash'] > 0) {
  81. $state = $this->fenzhang_act($info);
  82. if ($state == 1) {
  83. $update = array();
  84. $update['where_id'] = $info['id'];
  85. if ($state == 1) {
  86. $update['fenzhang_status'] = 2;
  87. } else {
  88. $update['fenzhang_status'] = 3;
  89. }
  90. $update['rdate'] = time();
  91. Dever::db('pay/yspay_cash')->update($update);
  92. }
  93. }
  94. }
  95. return $state;
  96. }
  97. }
  98. }
  99. return false;
  100. }
  101. private function getCash($amount, $merchant, &$data, $fenzhang = 0)
  102. {
  103. $amount = $amount*10;
  104. $fenzhang = $fenzhang*$this->num;
  105. $yspay = Dever::db('pay/yspay')->one(array('account_id' => $merchant['account_id']));
  106. if ($yspay) {
  107. $cash_per = 0;
  108. if ($merchant['type'] == 1 && $yspay['type'] == 2) {
  109. $cash_per = $merchant['cash_per'];
  110. if (!$cash_per) {
  111. if ($yspay && $yspay['cash_per']) {
  112. $cash_per = $yspay['cash_per'];
  113. }
  114. }
  115. if (!$cash_per || $cash_per <= 0) {
  116. $cash_per = 0;
  117. } else {
  118. $cash_per = $cash_per/100;
  119. }
  120. }
  121. $per = $yspay['per']/100;
  122. $cash_jy_per = $yspay['cash_jy_per']/100;
  123. $data['ycash'] = $amount;
  124. $data['yl_cash'] = $data['ycash'] * $per;
  125. $data['pt_cash'] = $data['ycash'] * $cash_jy_per;
  126. $data['cash'] = $data['ycash'] - $data['yl_cash'] - $data['pt_cash'];
  127. if ($yspay['cash_type'] == 1) {
  128. $fz_cash = $data['ycash'];
  129. } else {
  130. $fz_cash = $data['cash'];
  131. }
  132. //$data['fz_cash'] = $fz_cash*$cash_per;
  133. $data['fz_cash'] = 0;
  134. if ($fenzhang && $fenzhang > 0) {
  135. $data['fz_cash'] += $fenzhang;
  136. }
  137. $data['hf_cash'] = $data['cash'] - $data['fz_cash'];
  138. }
  139. }
  140. private function huafu_act($info)
  141. {
  142. $merchant = Dever::db('pay/yspay_merchant')->one($info['merchant_id']);
  143. if ($merchant) {
  144. $config = Dever::db('pay/yspay')->one(array('account_id' => $merchant['account_id']));
  145. if ($config) {
  146. return Dever::load('pay/yspay/multi')->huafu($config, $merchant, $info);
  147. }
  148. }
  149. return false;
  150. }
  151. private function fenzhang_act($info)
  152. {
  153. $merchant = Dever::db('pay/yspay_merchant')->one($info['merchant_id']);
  154. if ($merchant) {
  155. $config = Dever::db('pay/yspay')->one(array('account_id' => $merchant['account_id']));
  156. if ($config) {
  157. return Dever::load('pay/yspay/multi')->fenzhang($config, $merchant, $info);
  158. }
  159. }
  160. return false;
  161. }
  162. /*
  163. private function tixian_act($info)
  164. {
  165. $merchant = Dever::db('pay/yspay_merchant')->one($info['merchant_id']);
  166. if ($merchant) {
  167. $config = Dever::db('pay/yspay')->one(array('account_id' => $merchant['account_id']));
  168. if ($config) {
  169. return Dever::load('pay/yspay/account')->act($config, $merchant, $info);
  170. }
  171. }
  172. return false;
  173. }*/
  174. # 将测试数据改成待入账
  175. public function test_edit_api()
  176. {
  177. $data = Dever::db('pay/yspay_cash')->select(array('order_num' => 'test123', 'status' => 1));
  178. if ($data) {
  179. foreach ($data as $k => $v) {
  180. $this->up($v['id'], 2);
  181. }
  182. }
  183. return 'ok';
  184. }
  185. # 资金划付
  186. public function huafu_commit_api()
  187. {
  188. $where = array('status' => 2);
  189. $account_id = Dever::input('account_id');
  190. if ($account_id) {
  191. $where['account_id'] = $account_id;
  192. }
  193. $merchant_id = Dever::input('merchant_id');
  194. if ($merchant_id) {
  195. $where['merchant_id'] = $merchant_id;
  196. }
  197. $data = Dever::db('pay/yspay_cash')->select($where);
  198. if ($data) {
  199. foreach ($data as $k => $v) {
  200. $this->up($v['id'], 3);
  201. }
  202. }
  203. return 'ok';
  204. }
  205. # 随机获取平台商户
  206. public function getMid($account_id)
  207. {
  208. $merchant = Dever::db('pay/yspay_merchant')->select(array('type' => 2, 'account_id' => $account_id, 'status' => 1));
  209. if ($merchant) {
  210. $key = array_rand($merchant);
  211. if (isset($merchant[$key])) {
  212. return $merchant[$key];
  213. }
  214. }
  215. return false;
  216. }
  217. public function tixian($id, $name, $data)
  218. {
  219. $merchant_id = Dever::param('merchant_id', $data);
  220. $cash = Dever::param('cash', $data);
  221. $merchant = Dever::db('pay/yspay_merchant')->one($merchant_id);
  222. if ($merchant) {
  223. $admin = Dever::load('manage/auth.info');
  224. $this->tixian_act($merchant, $cash, $admin['username']);
  225. }
  226. return false;
  227. }
  228. public function tixian_act($merchant, $cash, $name = '')
  229. {
  230. if ($cash <= 0) {
  231. Dever::alert('提现金额应大于0');
  232. }
  233. $cash = $cash*$this->num;
  234. $yue = $merchant['hf_cash'] - $merchant['hf_tx_cash'] - $this->num*$this->bfj;
  235. if ($yue < $cash) {
  236. Dever::alert('余额不足');
  237. }
  238. $info['merchant_id'] = $merchant['id'];
  239. $info['hf_cash'] = $cash;
  240. $info['order_num'] = Dever::order('TX');
  241. $status = $this->huafu_act($info);
  242. if ($status == 1) {
  243. Dever::db('pay/yspay_merchant')->updateTxCash(array('where_id' => $merchant['id'], 'hf_tx_cash' => $cash));
  244. Dever::load('pay/yspay/merchant')->log($merchant['id'], $merchant['account_id'], 3, $cash, $name);
  245. }
  246. }
  247. }