Created
March 22, 2019 18:36
-
-
Save dura0ok/747bfc0d7c9b22cb11d5adca8f48b45c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| namespace App; | |
| use Carbon\Carbon; | |
| use Illuminate\Database\Eloquent\Model; | |
| use Illuminate\Database\Eloquent\Builder; | |
| use Webpatser\Uuid\Uuid; | |
| /** | |
| * Class Card | |
| * @package App | |
| * @method static findOrFail() Card | |
| */ | |
| class Card extends Model | |
| { | |
| public $incrementing = true; | |
| private $_subscription = null; | |
| protected $guarded = []; | |
| protected $dates = ['sold_at']; | |
| public function user() | |
| { | |
| return $this->belongsTo('App\User'); | |
| } | |
| public function city() | |
| { | |
| return $this->belongsTo(City::class); | |
| } | |
| public function activeSubscription(){ | |
| if($this->_subscription) | |
| return $this->_subscription; | |
| $subscription = $this->subscriptions()->where('is_active', true)->orderByDesc('card_subscription.id')->first(); | |
| if(!$subscription) | |
| return null; | |
| if($subscription->pivot->created_at->diffInDays(Carbon::now()) > $subscription->length) { | |
| $subscription->pivot->update(['is_active'=> false]); | |
| return null; | |
| } | |
| $this->_subscription = $subscription; | |
| return $subscription; | |
| } | |
| public function hasActiveSubscription(){ | |
| return $this->activeSubscription() ? true : false; | |
| } | |
| public function activeSubscriptionLeft(){ | |
| if(!$this->activeSubscription()) | |
| return false; | |
| $date = $this->activeSubscription()->pivot->created_at; | |
| $dateTill = $date->addDay($this->activeSubscription()->length); | |
| return $dateTill->diffInDays(Carbon::now()); | |
| } | |
| public function subscriptions(){ | |
| return $this->belongsToMany('App\Subscription','card_subscription')->withTimestamps()->withPivot(['is_active', 'id']); | |
| } | |
| public function discounts(){ | |
| return $this->hasManyThrough('App\Discount', 'App\Subscription'); | |
| } | |
| public static function getByParcedId($card_id, $no_fail = false){ | |
| $card_id = preg_replace('/\s+/', '', $card_id); | |
| if(substr($card_id, 0, 3) === '888'){ | |
| $card_code = substr($card_id, 3); | |
| }else{ | |
| $card_code = $card_id; | |
| } | |
| return $no_fail ? self::find($card_code) : self::findOrFail($card_code); | |
| } | |
| public function log(){ | |
| return $this->hasMany('App\DiscountLog'); | |
| } | |
| public function discountsByShops(array $shop_id){ | |
| return $this->activeSubscription()->discounts()->whereIn('shop_id', $shop_id)->get(); | |
| } | |
| public function discountsByShopsCount(array $shop_id){ | |
| return $this->activeSubscription()->discounts()->whereIn('shop_id', $shop_id)->count(); | |
| } | |
| public function assignSubscription($subscription){ | |
| $sub = Subscription::findOrFail($subscription); | |
| if(!$this->hasActiveSubscription()) | |
| $this->subscriptions()->attach($sub->id); | |
| // \DB::table('card_subscription')->insert([ | |
| // 'subscription_id' => $sub->id, | |
| // 'card_id' => $this->id, | |
| // 'is_active' => true | |
| // ]); | |
| } | |
| public function discountsAmount() | |
| { | |
| return $this->log()->where('is_validated', 1)->count(); | |
| } | |
| public function amountSaved() | |
| { | |
| return $this->log()->where('is_validated', 1)->sum('saved_sum'); | |
| } | |
| public function uniqueShopsVisited() | |
| { | |
| return $this->log()->where('is_validated', 1)->distinct()->get(array('shop_id'))->count(); | |
| } | |
| public function uniqueShops() | |
| { | |
| return $this->activeSubscription()->discounts()->distinct()->get(array('shop_id'))->count(); | |
| } | |
| public function scopeCostFrom(Builder $query, $param = null) | |
| { | |
| if (!$param) return $query; | |
| return $query->where('cost', '>', $param); | |
| } | |
| public function scopeCostTo(Builder $query, $param = null) | |
| { | |
| if (!$param) return $query; | |
| return $query->where('cost', '<', $param); | |
| } | |
| public function scopeActivatedFrom(Builder $query, $param = null) | |
| { | |
| if (!$param) return $query; | |
| return $query->where('activated_at', '>=', Carbon::parse($param)); | |
| } | |
| public function scopeActivatedTo(Builder $query, $param = null) | |
| { | |
| if (!$param) return $query; | |
| return $query->where('activated_at', '<=', Carbon::parse($param)); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| namespace App\Http\Controllers\v2; | |
| use App\Card; | |
| use App\Category; | |
| use App\DiscountUse; | |
| use Carbon\Carbon; | |
| use Illuminate\Http\Request; | |
| use App\Http\Controllers\Controller; | |
| class CardController extends Controller | |
| { | |
| public function index () | |
| { | |
| return view('v2.cards.index'); | |
| } | |
| public function show($id) | |
| { | |
| $card = Card::getByParcedId($id); | |
| $validations = collect(); | |
| $categories = Category::all(); | |
| $discounts = collect(); | |
| if ($card->hasActiveSubscription()) { | |
| $cs_id = $card->activeSubscription()->pivot->id; | |
| if (auth()->check() && auth()->user()->hasAnyRole(['shopuser', 'shopadmin'])) { | |
| $validations = DiscountUse::where('is_validated', false)->where('validation_sent', '>', Carbon::parse('-5 minutes'))->where('card_subscription_id', $cs_id)->get(); | |
| } | |
| $discounts = collect(); | |
| if (!request()->input('category') || request()->input('category') == 'all') { | |
| $discounts = $card->activeSubscription()->discounts()->byCity()->paginate(15)->appends('category'); | |
| } else { | |
| $cat = request()->input('category'); | |
| $discounts = $card->activeSubscription()->discounts() | |
| ->whereHas('shop', function ($q) use ($cat) { | |
| $q->where('category_id', $cat); | |
| })->byCity() | |
| ->paginate(20); | |
| } | |
| } | |
| return view('v2.cards.show') | |
| ->with('card', $card) | |
| ->with('validation', $validations) | |
| ->with('discounts', $discounts) | |
| ->with('categories', $categories); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment