<?php
namespace App\Services;


use App\Models\ShoppingOrderMargin;
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])
            ->wherePaid(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])
            ->wherePaid(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 $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])
            ->wherePaid(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();
        $startDay = $now->startOfMonth()->toDateString();
        $endDay = $now->endOfMonth()->toDateString();

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

        return $sum_net_amount;
    }
    //monthy amount, sum

}