Rebate.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. namespace App\Models;
  3. use Carbon\Carbon;
  4. use Illuminate\Support\Facades\Log;
  5. /**
  6. * @property $id
  7. * @property $effective_betting_amount
  8. * @property $date
  9. * @property $rebate_ratio
  10. * @property $member_id
  11. * @property $status
  12. * @property $amount
  13. * @property $audited_by
  14. * @property $grant_time
  15. */
  16. class Rebate extends BaseModel
  17. {
  18. protected $table = 'rebates';
  19. protected $fillable = ['date', 'member_id', 'betting_amount', 'rebate_ratio',
  20. 'amount', 'status', 'first_name', 'username', 'audited_by',
  21. 'profit', 'huishui_percentage', 'huishui_restriction', 'huishui_amount',
  22. 'effective_betting_amount', 'grant_time'
  23. ];
  24. const STATUS_WEI_FAN_YONG = 0;//未返佣
  25. const STATUS_FAN_YONG = 1; //已返佣
  26. public function member()
  27. {
  28. return $this->belongsTo(User::class, 'member_id', 'member_id')
  29. ->select(['id', 'member_id', 'username', 'first_name', 'admin_note', 'status', 'phone', 'visitor_id', 'register_ip']);
  30. }
  31. static function updateProfit($data)
  32. {
  33. // Settlement belongs to the original betting day, including overnight draws.
  34. $data['date'] = $data['date'] ?? Carbon::now('America/New_York')->format('Y-m-d');
  35. $rebate = static::where('date', $data['date'])
  36. ->where('member_id', $data['member_id'])->first();
  37. if ($rebate) {
  38. $profit = bcadd($rebate->profit, $data['profit'], 2);
  39. $rebate->profit = $profit;
  40. $rebate->save();
  41. }
  42. return $rebate;
  43. }
  44. static function addOrUpdate($data)
  45. {
  46. $data['date'] = $data['date'] ?? Carbon::now('America/New_York')->format('Y-m-d');
  47. $data['rebate_ratio'] = Config::where('field', 'rebate')->first()->val;
  48. $data['huishui_restriction'] = Config::where('field', 'huishui_restriction')->first()->val;
  49. $data['huishui_percentage'] = Config::where('field', 'huishui_percentage')->first()->val;
  50. $rebate = static::where('date', $data['date'])
  51. ->where('member_id', $data['member_id'])->first();
  52. if ($rebate) {
  53. $rebate->rebate_ratio = $data['rebate_ratio'];
  54. $rebate->first_name = $data['first_name'];
  55. $rebate->username = $data['username'];
  56. $rebate->save();
  57. $rebate->increment("betting_amount", $data['betting_amount']);
  58. } else {
  59. $rebate = static::create($data);
  60. }
  61. return $rebate;
  62. }
  63. protected function getGrantTimeAttribute($value): string
  64. {
  65. if ($value > 0) return date('Y-m-d H:i', $value);
  66. return "";
  67. }
  68. protected function getHuishuiRestrictionAttribute($value)
  69. {
  70. if ($this->status == static::STATUS_WEI_FAN_YONG) {
  71. $huishui_restriction = Config::where('field', 'huishui_restriction')->first()->val;
  72. return $huishui_restriction;
  73. }
  74. return $value;
  75. }
  76. protected function getHuishuiPercentageAttribute($value)
  77. {
  78. if ($this->status == static::STATUS_WEI_FAN_YONG) {
  79. $huishui_percentage = Config::where('field', 'huishui_percentage')->first()->val;
  80. return $huishui_percentage;
  81. }
  82. return $value;
  83. }
  84. protected function getHuishuiAmountAttribute($value)
  85. {
  86. if ($this->status == static::STATUS_WEI_FAN_YONG) {
  87. $value = 0;
  88. $huishui_restriction = Config::where('field', 'huishui_restriction')->first()->val;
  89. $huishui_percentage = Config::where('field', 'huishui_percentage')->first()->val;
  90. $lose = $this->profit * -1;
  91. if ($lose >= $huishui_restriction) {
  92. $value = bcmul($lose, $huishui_percentage, 2); // 返利金额
  93. }
  94. }
  95. return $value;
  96. }
  97. }