Cash.php 7.6 KB

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