Rebate.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. namespace App\Models;
  3. use Carbon\Carbon;
  4. use Illuminate\Database\Eloquent\Builder;
  5. use Illuminate\Foundation\Auth\User as Authenticatable;
  6. use Illuminate\Notifications\Notifiable;
  7. use Laravel\Sanctum\HasApiTokens;
  8. /**
  9. * @mixin Builder
  10. * @method static Builder|static where($column, $operator = null, $value = null, $boolean = 'and')
  11. */
  12. class Rebate extends Authenticatable
  13. {
  14. use HasApiTokens, Notifiable;
  15. protected $table = 'rebates';
  16. // protected $hidden = ['created_at', 'updated_at'];
  17. protected $fillable = ['date', 'member_id', 'betting_amount', 'rebate_ratio',
  18. 'amount', 'status', 'first_name', 'username', 'audited_by',
  19. 'profit', 'huishui_percentage', 'huishui_restriction', 'huishui_amount',
  20. ];
  21. static function updateProfit($data)
  22. {
  23. $data['date'] = Carbon::now('America/New_York')->format('Y-m-d');
  24. $rebate = static::where('date', $data['date'])
  25. ->where('member_id', $data['member_id'])->first();
  26. $rebate->increment('profit', $data['profit']);
  27. return $rebate;
  28. }
  29. static function addOrUpdate($data)
  30. {
  31. $rebate = static::where('date', $data['date'])
  32. ->where('member_id', $data['member_id'])->first();
  33. if ($rebate) {
  34. $rebate->rebate_ratio = $data['rebate_ratio'];
  35. $rebate->first_name = $data['first_name'];
  36. $rebate->username = $data['username'];
  37. $rebate->save();
  38. $rebate->increment("betting_amount", $data['betting_amount']);
  39. } else {
  40. $rebate = static::create($data);
  41. }
  42. return $rebate;
  43. }
  44. protected function getHuishuiRestrictionAttribute($value)
  45. {
  46. if ($this->status == 0) {
  47. $huishui_restriction = Config::where('field', 'huishui_restriction')->first()->val;
  48. return $huishui_restriction;
  49. }
  50. return $value;
  51. }
  52. protected function getHuishuiPercentageAttribute($value)
  53. {
  54. if ($this->status == 0) {
  55. $huishui_percentage = Config::where('field', 'huishui_percentage')->first()->val;
  56. return $huishui_percentage;
  57. }
  58. return $value;
  59. }
  60. protected function getHuishuiAmountAttribute($value)
  61. {
  62. if ($this->status == 0) {
  63. $value = 0;
  64. $huishui_restriction = Config::where('field', 'huishui_restriction')->first()->val;
  65. $huishui_percentage = Config::where('field', 'huishui_percentage')->first()->val;
  66. $lose = $this->profit * -1;
  67. if ($lose >= $huishui_restriction) {
  68. $value = bcmul($lose, $huishui_percentage, 2); // 返利金额
  69. }
  70. }
  71. return $value;
  72. }
  73. protected function getCreatedAtAttribute($value)
  74. {
  75. return \Carbon\Carbon::parse($value)->setTimezone('Asia/Shanghai')->format('Y-m-d H:i:s');
  76. }
  77. protected function getUpdatedAtAttribute($value)
  78. {
  79. return \Carbon\Carbon::parse($value)->setTimezone('Asia/Shanghai')->format('Y-m-d H:i:s');
  80. }
  81. }