Skip to content

Instantly share code, notes, and snippets.

@msonowal
Created November 14, 2016 15:07
Show Gist options
  • Select an option

  • Save msonowal/2d104475500db1f60aabce58bbc811a9 to your computer and use it in GitHub Desktop.

Select an option

Save msonowal/2d104475500db1f60aabce58bbc811a9 to your computer and use it in GitHub Desktop.
<?php
namespace convertifier\Models;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
use convertifier\Models\PaymentOrder;
use convertifier\Models\User;
use convertifier\Helpers\Helper;
class Earning extends Model
{
protected $fillable = [
'title', 'amount', 'payment_order_id', 'user_id', 'direction', 'type', 'note', 'status'
];
public static function scopeUncleared($query, $date = null)
{
$query->where('status', 0)->credit();
if ( $date !==null ){
$query->whereDate('created_at', '<=', $date);
}
return $query;
}
public static function creditReferral($payment_order_id)
{
$payment_order = PaymentOrder::find($payment_order_id);
if (!is_null($payment_order)){
$user = User::find($payment_order->user_id);
if ( !is_null($user) ){
$referrer_user = $user->referrerUser();
if ( !is_null($referrer_user) ){
$amount = ($payment_order->plan_price / 100) * 10;
static::create([
'payment_order_id' => $payment_order_id,
'user_id' => $user->referrer_id,
'amount' => $amount,
'direction' => 'credit',
'type' => 'paid_plan',
'status' => 0,
'note' => 'Credit referral on behalf of paid plan chosen by his referral user ',
]);
Helper::logInfo('RE#47', 'Uncleared Earning has been credited to the referrer #'.$user->referrer_id. ' by user #'.$user->id);
}
Helper::logError('RE#49', 'Referrer user not available or not found for credit for user #'.$user->id);
}
Helper::logError('RE#33', ' User not found for the Payment order');
}else{
Helper::logError('RE#53', 'Unable to find payment order to credit referral #'.$payment_order_id);
}
}
public static function scopeCredit($query)
{
return $query->where('direction', 'credit');
}
public static function scopeofUser($query, $user_id)
{
return $query->where('user_id', $user_id);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment