Rebate.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. $data['date'] = Carbon::now('America/New_York')->format('Y-m-d');
  34. $rebate = static::where('date', $data['date'])
  35. ->where('member_id', $data['member_id'])->first();
  36. if ($rebate) {
  37. $profit = bcadd($rebate->profit, $data['profit'], 2);
  38. $rebate->profit = $profit;
  39. $rebate->save();
  40. }
  41. return $rebate;
  42. }
  43. static function addOrUpdate($data)
  44. {
  45. $data['date'] = Carbon::now('America/New_York')->format('Y-m-d');
  46. $data['rebate_ratio'] = Config::where('field', 'rebate')->first()->val;
  47. $data['huishui_restriction'] = Config::where('field', 'huishui_restriction')->first()->val;
  48. $data['huishui_percentage'] = Config::where('field', 'huishui_percentage')->first()->val;
  49. $rebate = static::where('date', $data['date'])
  50. ->where('member_id', $data['member_id'])->first();
  51. if ($rebate) {
  52. $rebate->rebate_ratio = $data['rebate_ratio'];
  53. $rebate->first_name = $data['first_name'];
  54. $rebate->username = $data['username'];
  55. $rebate->save();
  56. $rebate->increment("betting_amount", $data['betting_amount']);
  57. } else {
  58. $rebate = static::create($data);
  59. }
  60. return $rebate;
  61. }
  62. protected function getGrantTimeAttribute($value): string
  63. {
  64. if ($value > 0) return date('Y-m-d H:i', $value);
  65. return "";
  66. }
  67. protected function getHuishuiRestrictionAttribute($value)
  68. {
  69. if ($this->status == static::STATUS_WEI_FAN_YONG) {
  70. $huishui_restriction = Config::where('field', 'huishui_restriction')->first()->val;
  71. return $huishui_restriction;
  72. }
  73. return $value;
  74. }
  75. protected function getHuishuiPercentageAttribute($value)
  76. {
  77. if ($this->status == static::STATUS_WEI_FAN_YONG) {
  78. $huishui_percentage = Config::where('field', 'huishui_percentage')->first()->val;
  79. return $huishui_percentage;
  80. }
  81. return $value;
  82. }
  83. protected function getHuishuiAmountAttribute($value)
  84. {
  85. if ($this->status == static::STATUS_WEI_FAN_YONG) {
  86. $value = 0;
  87. $huishui_restriction = Config::where('field', 'huishui_restriction')->first()->val;
  88. $huishui_percentage = Config::where('field', 'huishui_percentage')->first()->val;
  89. $lose = $this->profit * -1;
  90. if ($lose >= $huishui_restriction) {
  91. $value = bcmul($lose, $huishui_percentage, 2); // 返利金额
  92. }
  93. }
  94. return $value;
  95. }
  96. }