<?php
namespace App\Services;


use App\Models\ShoppingOrderMargin;
use App\Models\UserCreditMargin;
use App\User;
use Carbon;

class UserMarign
{

    public static function getMontlyPrice(User $user, $date = null, $format = false){

        $now = $date ? Carbon::parse($date) : Carbon::now();
        $startDay = $now->startOfMonth()->toDateString();
        $endDay = $now->endOfMonth()->toDateString();

        $sum_net_amount = ShoppingOrderMargin::whereUserId($user->id)
            ->whereBetween('from', [$startDay, $endDay])
            ->whereOrderPaid(true)
            ->whereCancellation(false)
            ->sum('net_price');
        if($format){
            $sum_net_amount = Util::formatNumber($sum_net_amount);
        }
        return $sum_net_amount;
    }

    public static function getMontlyPriceOpen(User $user, $date = null, $format = false){

        $now = $date ? Carbon::parse($date) : Carbon::now();
        $startDay = $now->startOfMonth()->toDateString();
        $endDay = $now->endOfMonth()->toDateString();

        $sum_net_amount = ShoppingOrderMargin::whereUserId($user->id)
            ->whereBetween('from', [$startDay, $endDay])
            ->whereOrderPaid(false)
            ->whereCancellation(false)
            ->sum('net_price');
        if($format){
            $sum_net_amount = Util::formatNumber($sum_net_amount);
        }
        return $sum_net_amount;
    }

    public static function getMontlyAmount($user, $date = null, $format = false){

        if(!$user instanceof User){
            return 0;
        }

        $now = $date ? Carbon::parse($date) : Carbon::now();
        $startDay = $now->startOfMonth()->toDateString();
        $endDay = $now->endOfMonth()->toDateString();
    
        $sum_net_amount = ShoppingOrderMargin::whereUserId($user->id)
            ->whereBetween('from', [$startDay, $endDay])
            ->whereOrderPaid(true)
            ->whereCancellation(false)
            ->sum('net_amount');
        if($format){
            $sum_net_amount = Util::formatNumber($sum_net_amount);
        }

        return $sum_net_amount;
    }


    public static function getMontlyPartnerCommission(User $user, $date = null, $format = false){

        //$now = $date ? Carbon::parse($date) : Carbon::now();
        $start = Carbon::parse('01.01.2021');
    	$end = Carbon::now();
        $startDay = $start->startOfMonth()->toDateString();
        $endDay = $end->endOfMonth()->toDateString();

        $sum_net_amount = ShoppingOrderMargin::whereMSponsorId($user->id)
            ->whereBetween('from', [$startDay, $endDay])
            ->whereOrderPaid(true)
            ->whereCancellation(false)
            ->sum('net_partner_commission');
        if($format){
            $sum_net_amount = Util::formatNumber($sum_net_amount);
        }

        return $sum_net_amount;
    }


    public static function getMontlyPartnerCommissionOpen(User $user, $date = null, $format = false){

        if(!$date){
            $start = Carbon::parse('01.01.2021');
            $end = Carbon::now();
            $startDay = $start->startOfMonth()->toDateString();
            $endDay = $end->endOfMonth()->toDateString();
        }else{
            $now = $date ? Carbon::parse($date) : Carbon::now();
            $startDay = $now->startOfMonth()->toDateString();
            $endDay = $now->endOfMonth()->toDateString();
        }

        $sum_net_amount = ShoppingOrderMargin::whereMSponsorId($user->id)
            ->whereBetween('from', [$startDay, $endDay])
            ->whereOrderPaid(true)
            ->whereCancellation(false)
            ->wherePartnerCommissionPaid(false)
            ->where('partner_commission_pending_to', '<', Carbon::now())
            ->sum('net_partner_commission');
        if($format){
            $sum_net_amount = Util::formatNumber($sum_net_amount);
        }
        return $sum_net_amount;
    }
    public static function getMontlyPartnerCommissionPending(User $user, $date = null, $format = false){

        if(!$date){
            $start = Carbon::parse('01.01.2021');
            $end = Carbon::now();
            $startDay = $start->startOfMonth()->toDateString();
            $endDay = $end->endOfMonth()->toDateString();
        }else{
            $now = $date ? Carbon::parse($date) : Carbon::now();
            $startDay = $now->startOfMonth()->toDateString();
            $endDay = $now->endOfMonth()->toDateString();
        }
       
        $sum_net_amount = ShoppingOrderMargin::whereMSponsorId($user->id)
            ->whereBetween('from', [$startDay, $endDay])
            ->whereOrderPaid(true)
            ->whereCancellation(false)
            ->wherePartnerCommissionPaid(false)
            ->where('partner_commission_pending_to', '>=', Carbon::now())
            ->sum('net_partner_commission');
        if($format){
            $sum_net_amount = Util::formatNumber($sum_net_amount);
        }

        return $sum_net_amount;
    }
    /*
    public static function getMontlyPartnerCommissionOpenByID($user_id, $date = null, $format = false, $addUserCreditMargin = false){

        $sum_net_amount = ShoppingOrderMargin::whereMSponsorId($user_id)
            ->whereOrderPaid(true)
            ->whereCancellation(false)
            ->wherePartnerCommissionPaid(false)
            ->where('partner_commission_pending_to', '<', Carbon::now())
            ->sum('net_partner_commission');
        if($addUserCreditMargin){
            $sum_net_credit = UserCreditMargin::whereUserId($user_id)
            ->wherePaid(false)
            ->sum('credit');
            $sum_net_amount += $sum_net_credit;
        }

        if($format){
            $sum_net_amount = Util::formatNumber($sum_net_amount);
        }

        return $sum_net_amount;
    }
    */
   

   /*
    public static function getMontlyPartnerCommissionPendingByID($user_id, $date = null, $format = false){

        $sum_net_amount = ShoppingOrderMargin::whereMSponsorId($user_id)
            ->whereOrderPaid(true)
            ->whereCancellation(false)
            ->wherePartnerCommissionPaid(false)
            ->where('partner_commission_pending_to', '>=', Carbon::now())
            ->sum('net_partner_commission');
        if($format){
            $sum_net_amount = Util::formatNumber($sum_net_amount);
        }

        return $sum_net_amount;
    }
    */

    public static function getPartnerCommissionItems($user_id, $isPending = false){

        $query = ShoppingOrderMargin::whereMSponsorId($user_id)
            ->whereOrderPaid(true)
            ->whereOutPaid(false)
            ->whereCancellation(false)
            ->wherePartnerCommissionPaid(false)
            ->whereNotNull('partner_commission_pending_to');
    
            if ($isPending) {
                $query->where('partner_commission_pending_to', '>=', Carbon::now());
            } else {
                $query->where('partner_commission_pending_to', '<', Carbon::now());
            }
               
        return $query->get();
    }

    public static function getShopCommissionItems($user_id, $isPending = false){

        $query = ShoppingOrderMargin::whereUserId($user_id)
            ->whereOrderPaid(true)
            ->whereOutPaid(false)
            ->whereCancellation(false)
            ->whereMarginPaid(false)
            ->whereNotNull('margin_pending_to')
            ->whereIn('status', [7,8]);
        
        if ($isPending) {
            $query->where('margin_pending_to', '>=', Carbon::now());
        } else {
            $query->where('margin_pending_to', '<', Carbon::now());
        }
        return $query->get();
    }

    public static function getUserCreditMarginByUserID($user_id){
        $query = UserCreditMargin::whereUserId($user_id)
            ->wherePaid(false);
        return $query->get();
    }

    /*public static function getOrderFromPartnerCommissionPendingByID($user_id){

        $ShoppingOrderMargins = ShoppingOrderMargin::whereMSponsorId($user_id)
            ->whereOrderPaid(true)
            ->whereCancellation(false)
            ->wherePartnerCommissionPaid(false)
            ->where('partner_commission_pending_to', '>=', Carbon::now())
            ->get();
       
        return $ShoppingOrderMargins;
    }
    */
    /*
    public static function getOrderFromShopPendingByID($user_id){

        $ShoppingOrderMargins = ShoppingOrderMargin::whereUserId($user_id)
            ->whereOrderPaid(true)
            ->whereCancellation(false)
            ->wherePartnerCommissionPaid(false)
            ->whereIn('status', [7, 8])
            ->where('partner_commission_pending_to', '>=', Carbon::now())
            ->get();
       
        return $ShoppingOrderMargins;
    }
        */
}