PaymentReceived.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <?php
  2. namespace app\models\order;
  3. use app\admin\model\system\StoreBill;
  4. use app\models\system\SystemStore;
  5. use crmeb\traits\ModelTrait;
  6. use crmeb\basic\BaseModel;
  7. use think\Exception;
  8. use app\models\user\{User, UserBill};
  9. use crmeb\services\PHPExcelService;
  10. /**
  11. * B子订单详情
  12. * Class ArticleCategory
  13. * @package app\models\article
  14. */
  15. class PaymentReceived extends BaseModel
  16. {
  17. use ModelTrait;
  18. protected function getTypeAttr($value)
  19. {
  20. switch ($value) {
  21. case 0:
  22. return '余额';
  23. break;
  24. case 1:
  25. return '消费券';
  26. break;
  27. case 2:
  28. return '积分';
  29. break;
  30. }
  31. }
  32. /**
  33. * 生成订单唯一id
  34. * @param $uid 用户uid
  35. * @return string
  36. */
  37. public static function getNewOrderId()
  38. {
  39. list($msec, $sec) = explode(' ', microtime());
  40. $msectime = number_format((floatval($msec) + floatval($sec)) * 1000, 0, '', '');
  41. $orderId = 'qr_' . $msectime . mt_rand(10000, 99999);
  42. while (self::be(['order_id' => $orderId])) $orderId = 'qr_' . $msectime . mt_rand(10000, 99999);
  43. return $orderId;
  44. }
  45. /**
  46. * 创建支付
  47. * @param $pay_type
  48. * @param $code
  49. * @param $pay_uid
  50. * @param $collect_uid
  51. * @param $amount
  52. */
  53. public static function Create_Payment($pay_type, $code, $pay_uid, $collect_uid, $amount, $type = 0)
  54. {
  55. self::beginTrans();
  56. $order_id = self::getNewOrderId();
  57. $add_time = time();
  58. $pay_user = User::find($pay_uid);
  59. $collect_user = User::find($collect_uid);
  60. try {
  61. switch ($type) {
  62. case 0:
  63. $field = "now_money";
  64. $title = "余额";
  65. break;
  66. case 1:
  67. $field = "consumer";
  68. $title = "消费券";
  69. break;
  70. case 2:
  71. $field = "integral";
  72. $title = "积分";
  73. break;
  74. }
  75. if (bcsub($pay_user[$field], $amount, 2) < 0) return self::setErrorInfo($title . '不足!', true);
  76. $paid = 1;
  77. $pay_time = time();
  78. if ($collect_user['user_store_id'] == 0) {
  79. $transfer = bcdiv(bcmul($amount, sys_config('pay_collection_commission'), 2), 100, 2);
  80. $recommend = bcdiv(bcmul($amount, sys_config('pay_collection_recommend'), 2), 100, 2);
  81. } else {
  82. $transfer = bcdiv(bcmul($amount, sys_config('store_pay_collection_commission'), 2), 100, 2);
  83. $recommend = bcdiv(bcmul($amount, sys_config('store_pay_collection_recommend'), 2), 100, 2);
  84. }
  85. $service_charge = $transfer;
  86. $res = self::create(compact('pay_type', 'pay_uid', 'code', 'collect_uid', 'amount', 'order_id', 'add_time', 'paid', 'pay_time', 'service_charge', 'type'));
  87. if ($res) {
  88. $now_money = bcsub($amount, $transfer, 2);
  89. @file_put_contents("pay.txt", 'uid:' . $pay_uid . 'collect_uid:' . $collect_uid . '--' . $amount . '--' . $transfer . '--' . $now_money);
  90. User::where('uid', $pay_uid)->dec($field, $amount)->update();
  91. $res2 = UserBill::expend("扫码付款-" . $title, $pay_uid, 'now_money', 'qr_des_' . $field, $amount, $res['id'], bcsub($pay_user[$field], $amount, 2), '支付给' . $collect_user['nickname'] . $amount . $title, 1);
  92. $str = sprintf("收到%s用户%.2f%s,到账:%.2f%s,手续费:%.2f", $pay_user['nickname'], $amount, $title, $now_money, $title, $transfer);
  93. $store = SystemStore::get($collect_user['user_store_id']);
  94. if ($collect_user['user_store_id'] > 0 && $store && $field == 'now_money') {
  95. $res1 = StoreBill::income('扫码收款-' . $title, $collect_user['user_store_id'], 'product_sale', $now_money, $res['id'], bcadd($store['money'], $now_money, 2), $str);
  96. $res2 = SystemStore::bcInc($collect_user['user_store_id'], 'money', $now_money, 'id');
  97. } else {
  98. User::where('uid', $collect_uid)->inc($field, $now_money)->update();
  99. $res1 = UserBill::income('扫码收款-' . $title, $collect_uid, 'now_money', 'qr_add_' . $field, $now_money, $res['id'], bcadd($collect_user[$field], $now_money, 2), $str);
  100. }
  101. if ($store) {
  102. $spread = User::get($store['spread_uid']);
  103. } else {
  104. $spread = User::get($collect_user['spread_uid']);
  105. }
  106. $res3 = true;
  107. if ($spread && $recommend > 0) {
  108. User::where('uid', $spread['uid'])->inc($field, $recommend)->update();
  109. $res3 = UserBill::income("扫码付款推荐奖-" . $title, $spread['uid'], 'now_money', 'qr_recommend_' . $field, $recommend, $res['id'], bcadd($spread[$field], $recommend, 2), '推荐用户收款' . $title . '获得推荐奖', 1);
  110. }
  111. }
  112. if ($res && $res1 && $res2 && $res3) {
  113. self::commitTrans();
  114. } else {
  115. self::rollbackTrans();
  116. }
  117. return $res;
  118. } catch (Exception $e) {
  119. echo $e->getMessage();
  120. self::rollbackTrans();
  121. }
  122. return false;
  123. }
  124. /**
  125. * 获取支付信息
  126. * @param $order_id
  127. */
  128. public static function getPayment($order_id)
  129. {
  130. $info = self::where('order_id', $order_id)->find();
  131. if ($info) {
  132. $info = $info->toArray();
  133. $info['pay_user'] = User::find($info['pay_uid'])->toarray();
  134. $info['collect_user'] = User::find($info['collect_uid'])->toarray();
  135. }
  136. return $info;
  137. }
  138. /**
  139. * 获取列表
  140. * @param $where
  141. */
  142. public static function lst($where)
  143. {
  144. $model = new self;
  145. if (isset($where['data']) && $where['data'] != '') $model = $model->getModelTime($where, $model);
  146. if (isset($where['pay_type']) && $where['pay_type'] > -1) $model = $model->where('pay_type', $where['pay_type']);
  147. if (isset($where['way']) && isset($where['way']) != '') {
  148. switch ($where['way']) {
  149. case 'pay':
  150. if (isset($where['uid']) && $where['uid'] > 0) $model = $model->where('pay_uid', $where['uid']);
  151. break;
  152. case 'collect':
  153. if (isset($where['uid']) && $where['uid'] > 0) $model = $model->where('collect_uid', $where['uid']);
  154. break;
  155. }
  156. } else {
  157. if (isset($where['uid']) && $where['uid'] > 0) $model = $model->where('pay_uid|collect_uid', $where['uid']);
  158. }
  159. if (isset($where['is_tip']) && $where['is_tip'] > -1) $model = $model->where('is_tip', $where['is_tip']);
  160. $count = $model->count();
  161. $data = $model->page($where['page'], $where['limit'])->select();
  162. $data = $data ? $data->toArray() : [];
  163. foreach ($data as &$v) {
  164. $v['pay_user'] = User::find($v['pay_uid'])->toarray();
  165. $v['collect_user'] = User::find($v['collect_uid'])->toarray();
  166. }
  167. if (isset($where['excel']) && $where['excel'] == 1) {
  168. $pay_type = ['收款码', '扫付款码'];
  169. foreach ($data as $index => $item) {
  170. $export[] = [
  171. $item['order_id'],
  172. $item['pay_name'] => $item['pay_user']['real_name'] . ":" . $item['pay_user']['uid'],
  173. $item['amount'],
  174. $item['collect_name'] => $item['collect_user']['real_name'] . ":" . $item['collect_user']['uid'],
  175. $item['add_time'],
  176. $item['pay_type'] => $pay_type[$item['pay_type']],
  177. ];
  178. }
  179. PHPExcelService::setExcelHeader(['订单号', '付款人', '金额', '收款人', '时间', '方式', '类型'])
  180. ->setExcelTile('收付码', '收付码列表' . time(), ' 生成时间:' . date('Y-m-d H:i:s', time()))
  181. ->setExcelContent($export)
  182. ->ExcelSave();
  183. }
  184. return compact('count', 'data');
  185. }
  186. public static function collection($uid)
  187. {
  188. $card_no = User::where('uid', $uid)->value('merchant_no');
  189. if (empty(trim($card_no))) {
  190. $card_no = User::max('merchant_no');
  191. if (intval($card_no) < 10000000000) {
  192. $card_no = "10000000001";
  193. } else {
  194. $card_no = str_pad(bcadd($card_no, 1), 11, "0", STR_PAD_LEFT);
  195. }
  196. User::edit(['merchant_no' => $card_no], $uid);
  197. }
  198. return $card_no;
  199. }
  200. }