<?php

namespace App\Http\Controllers\Administrator;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Auth;
use DataTables;
use DB;
use App\ItemModel;
use App\ProjectModel;
use App\CategoryModel;
use App\BranchModel;
use Illuminate\Support\Facades\Crypt;
class ReportCommissionController extends Controller
{
    function __construct()
    {
        $this->middleware('permission:Commission-Project-Owner-List', ['only' => ['index']]);

    }
    public function index()
    {
        $projects = ProjectModel::select(DB::raw('CONCAT(id," ",project_name) AS project_name'),'id')
            ->pluck('project_name','id')
            ->prepend('Please Select', '');
        $items = ItemModel::select(DB::raw('CONCAT(id," ",item_name) AS item_name'),'id')
            ->where('item_salestatus',2)
            ->pluck('item_name','id')
            ->prepend('Please Select', '');
        $category = CategoryModel::select(DB::raw('CONCAT(id," ",category_name) AS category_name'),'id')
            ->pluck('category_name','id')
            ->prepend('Please Select', '');
        return view('administrator.report-commission.index',compact('projects','items','category'));
    }
    public function datatable_get_report_commission(Request $request){
        $log_id = Auth::user()->id;
        $commission = DB::table('nso007_agency_commission_list')
                        
                        ->join('nso007_agency_commission', 'nso007_agency_commission.id', '=', 'nso007_agency_commission_list.commission_id')
                        ->join('users', 'nso007_agency_commission_list.agency_id', '=', 'users.id')
                        ->join('nso007_item_stock', 'nso007_item_stock.id', '=', 'nso007_agency_commission.item_id')
                        ->join('nso007_project_stock', 'nso007_project_stock.id', '=', 'nso007_item_stock.item_projectid')
                        ->join('nso007_category_stock', 'nso007_category_stock.id', '=', 'nso007_item_stock.item_categoryid')
                        ->where('nso007_agency_commission_list.status', 1)
                        ->select(
                            'nso007_category_stock.category_name',
                            'nso007_project_stock.project_name',
                            'nso007_item_stock.item_name',
                            'nso007_agency_commission_list.commission',
                            'nso007_agency_commission_list.amount',
                            'nso007_agency_commission_list.receive',
                            'nso007_agency_commission_list.id',
                            'users.user_code',
                            DB::raw('CONCAT(users.user_firstname," ",users.user_lastname) as full_name')
                        )
                        ->get();
        if($request->isMethod('GET')) {
            return Datatables::of($commission)
                ->addColumn('balance', function ($commission) {
                    $balance = $commission->amount - $commission->receive;
                    if ($balance <= 0) {
                        return '$' . number_format(0, 2);
                    } else {
                        return '$' . number_format($balance, 2);
                    }
                })
                ->addColumn('balance_per', function ($commission) {
                    $balance = $commission->amount - $commission->receive;
                    $balance_per = $balance * 100 / $commission->amount;
                    if ($balance <= 0) {
                        return number_format(0, 2) . ' %';
                    } else {
                        return number_format($balance_per, 2) . ' %';
                    }
                })
                ->editColumn('full_name', function ($commission) {
                    return $commission->full_name . " (" . $commission->user_code . ")";
                })
                ->addColumn('amount', function ($commission) {
                    return '$' . number_format($commission->amount, 2);
                })
                ->addColumn('receive', function ($commission) {
                    return '$' . number_format($commission->receive, 2);
                })
                ->addColumn('commission', function ($commission) {
                    return $commission->commission . '%';
                })
                ->addIndexColumn()
                ->make(true);
        }else{
            $commission = DB::table('nso007_agency_commission_list')

                ->join('nso007_agency_commission', 'nso007_agency_commission.id', '=', 'nso007_agency_commission_list.commission_id')
                ->join('users', 'nso007_agency_commission_list.agency_id', '=', 'users.id')
                ->join('nso007_item_stock', 'nso007_item_stock.id', '=', 'nso007_agency_commission.item_id')
                ->join('nso007_project_stock', 'nso007_project_stock.id', '=', 'nso007_item_stock.item_projectid')
                ->join('nso007_category_stock', 'nso007_category_stock.id', '=', 'nso007_item_stock.item_categoryid')
                ->where('nso007_agency_commission_list.status', 1)
                ->select(
                    'nso007_category_stock.category_name',
                    'nso007_project_stock.project_name',
                    'nso007_item_stock.item_name',
                    'nso007_agency_commission_list.commission',
                    'nso007_agency_commission_list.amount',
                    'nso007_agency_commission_list.receive',
                    'nso007_agency_commission_list.id',
                    'users.user_code',
                    DB::raw('CONCAT(users.user_firstname," ",users.user_lastname) as full_name')
                )
                ->where('nso007_project_stock.id','LIKE',isset($request->project_name)? $request->project_name:NULL)
                ->where('nso007_category_stock.id','LIKE',isset($request->category_name)? $request->category_name:NULL)
                ->where('nso007_item_stock.id','LIKE',isset($request->item_name)? $request->item_name:NULL)
                ->get();
            echo json_encode($commission);
        }
    }
    public function report_commission_print(Request $request)
    {
        $commission = DB::table('nso007_agency_commission_list')

            ->join('nso007_agency_commission', 'nso007_agency_commission.id', '=', 'nso007_agency_commission_list.commission_id')
            ->join('users', 'nso007_agency_commission_list.agency_id', '=', 'users.id')
            ->join('nso007_item_stock', 'nso007_item_stock.id', '=', 'nso007_agency_commission.item_id')
            ->join('nso007_project_stock', 'nso007_project_stock.id', '=', 'nso007_item_stock.item_projectid')
            ->join('nso007_category_stock', 'nso007_category_stock.id', '=', 'nso007_item_stock.item_categoryid')
            ->where('nso007_agency_commission_list.status', 1)
            ->select(
                'nso007_category_stock.category_name',
                'nso007_project_stock.project_name',
                'nso007_item_stock.item_name',
                'nso007_agency_commission_list.commission',
                'nso007_agency_commission_list.amount',
                'nso007_agency_commission_list.receive',
                'nso007_agency_commission_list.id',
                'users.user_code',
                DB::raw('CONCAT(users.user_firstname," ",users.user_lastname) as full_name')
            )
            ->where('nso007_project_stock.id','LIKE',isset($request->project_name)? $request->project_name:NULL)
            ->where('nso007_category_stock.id','LIKE',isset($request->category_name)? $request->category_name:NULL)
            ->where('nso007_item_stock.id','LIKE',isset($request->item_name)? $request->item_name:NULL)
            ->orderBy('id','asc')->get();

        $sale_amount = 0;
        $sale_receive = 0;
        $sale_balance = 0;
        foreach ($commission as $key => $value) {
            $sale_amount += $value->amount;
            $sale_receive += $value->receive;
            $sale_balance += $value->amount-$value->receive;
        }
        $branch = DB::table('nso007_branches')->where('id',\Branch::branch_id())->first();
        return view('administrator.report-commission.report-view-print',compact('commission','sale_amount','sale_receive','sale_balance','branch'));

    }
}
