<?php

namespace App\Http\Controllers\Administrator;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\FundsRegisterModel;
use DataTables;
use App\ProjectModel;
use DB;
class PayableNoteController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    function __construct()
    {
        $this->middleware('permission:Payable-Note-List');
        // $this->middleware('permission:TrialBalance-Report', ['only' => ['trial_balance']]);
        // $this->middleware('permission:BalanceSheet-Report', ['only' => ['balance_sheet']]);
        // $this->middleware('permission:BalanceSheet-Print', ['only' => ['print_balance_sheet']]);
        // $this->middleware('permission:IncomeStatement-Report', ['only' => ['income_statement']]);
    }

    public function index()
    {
        return view('administrator.accounting.payable-note.index');
    }
    public function datatable_get_payable_note() {
        $payable_note = FundsRegisterModel::all();
        return  Datatables::of($payable_note)
        ->addIndexColumn()
        ->editColumn('balance', function($payable_note) {
            return "$ ".$payable_note->balance;
        })
        ->addColumn('status', function($payable_note) {
            return ($payable_note->status == 1)? "Active":"";
        })
    ->addIndexColumn()
        ->make(true);
    }
    
    public function payable_note_print()
    {
        $payable_note = FundsRegisterModel::all();
        $total = 0;
        foreach ($payable_note as $key => $value) {
            $total += $value->balance;
        }
        $branch = DB::table('nso007_branches')->where('id',\Branch::branch_id())->first();
        return view('administrator.accounting.payable-note.report-print',compact('payable_note','branch','total'));
    }

    public function payable_note_po()
    {
        $projects = ProjectModel::select(DB::raw('CONCAT(id," ",project_name) AS project_name'),'id')
            ->pluck('project_name','id')
            ->prepend('Please Select', '');
        return view('administrator.accounting.payable-note.index_note_po',compact('projects'));
    }
    public function payable_note_po_data(Request $request)
    {
        $deposit_po = DB::table('nso007_sale_stock')
            ->join('nso007_sale_detail_stock','nso007_sale_detail_stock.saledetail_saleid','nso007_sale_stock.id')
            ->join('nso007_item_stock','nso007_item_stock.id','nso007_sale_detail_stock.saledetail_itemid')
            ->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')
            ->join('nso007_client','nso007_sale_stock.sale_clientid','nso007_client.id')
            ->select(
                'nso007_sale_stock.id',
                'nso007_sale_stock.sale_receive',
                'nso007_sale_stock.sale_date',
                'nso007_item_stock.item_street',
                'nso007_item_stock.item_house',
                'nso007_item_stock.item_zone',
                'nso007_item_stock.item_size_house',
                'nso007_item_stock.item_size_land',
                'nso007_item_stock.item_name',
                'nso007_item_stock.item_price',
                'nso007_category_stock.category_name',
                'nso007_client.client_fullname',
                'nso007_client.client_phone1',
                'nso007_client.client_address',
                'nso007_project_stock.project_name'
            )
            ->whereNull('nso007_sale_stock.collect_deposit_status')
            ->orderBy('nso007_sale_stock.id','ASC')->get();
        if($request->isMethod('GET')) {
            return Datatables::of($deposit_po)
                ->addIndexColumn()
                ->addColumn('date', function ($deposit_po) {
                    return date('Y-m-d', strtotime($deposit_po->sale_date));
                })
                ->addColumn('item_price', function ($deposit_po) {
                    return "$ " . $deposit_po->item_price;
                })
                ->addColumn('item_size_land', function ($deposit_po) {
                    return $deposit_po->item_size_land . ' m<sup>2</sup>';
                })
                ->addColumn('item_size_house', function ($deposit_po) {
                    return empty($deposit_po->item_size_house) ? '' : $deposit_po->item_size_house . ' m<sup>2</sup>';
                })
                ->addColumn('sale_receive', function ($deposit_po) {
                    return "$ " . $deposit_po->sale_receive;
                })
                ->rawColumns(['item_size_house' => 'item_size_house', 'item_size_land' => 'item_size_land'])
                ->make(true);
        }else {
            $deposit_po = DB::table('nso007_sale_stock')
                ->join('nso007_sale_detail_stock','nso007_sale_detail_stock.saledetail_saleid','nso007_sale_stock.id')
                ->join('nso007_item_stock','nso007_item_stock.id','nso007_sale_detail_stock.saledetail_itemid')
                ->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')
                ->join('nso007_client','nso007_sale_stock.sale_clientid','nso007_client.id')
                ->select(
                    'nso007_sale_stock.id',
                    'nso007_sale_stock.sale_receive',
                    'nso007_sale_stock.sale_date',
                    'nso007_item_stock.item_street',
                    'nso007_item_stock.item_house',
                    'nso007_item_stock.item_zone',
                    'nso007_item_stock.item_size_house',
                    'nso007_item_stock.item_size_land',
                    'nso007_item_stock.item_name',
                    'nso007_item_stock.item_price',
                    'nso007_category_stock.category_name',
                    'nso007_client.client_fullname',
                    'nso007_client.client_phone1',
                    'nso007_client.client_address',
                    'nso007_project_stock.project_name'
                )
                ->whereNull('nso007_sale_stock.collect_deposit_status')
                ->where('nso007_project_stock.id','LIKE',isset($request->projects)? $request->projects:NULL)
                ->orderBy('nso007_sale_stock.id','ASC')->get();
            echo json_encode($deposit_po);

        }

    }
    public function payable_note_po_print(Request $request)
    {
        $deposit_po = DB::table('nso007_sale_stock')
            ->join('nso007_sale_detail_stock','nso007_sale_detail_stock.saledetail_saleid','nso007_sale_stock.id')
            ->join('nso007_item_stock','nso007_item_stock.id','nso007_sale_detail_stock.saledetail_itemid')
            ->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')
            ->join('nso007_client','nso007_sale_stock.sale_clientid','nso007_client.id')
            ->select(
                'nso007_sale_stock.id',
                'nso007_sale_stock.sale_receive',
                'nso007_sale_stock.sale_date',
                'nso007_item_stock.item_street',
                'nso007_item_stock.item_house',
                'nso007_item_stock.item_zone',
                'nso007_item_stock.item_size_house',
                'nso007_item_stock.item_size_land',
                'nso007_item_stock.item_name',
                'nso007_item_stock.item_price',
                'nso007_category_stock.category_name',
                'nso007_client.client_fullname',
                'nso007_client.client_phone1',
                'nso007_client.client_address',
                'nso007_project_stock.project_name'
            )
            ->whereNull('nso007_sale_stock.collect_deposit_status')
            ->where('nso007_project_stock.id','LIKE',isset($request->projects)? $request->projects:NULL)
            ->orderBy('nso007_sale_stock.id','ASC')->get();
        $total = 0;
        $total_price = 0;
        foreach ($deposit_po as $key => $value) {
            $total += $value->sale_receive;
            $total_price += $value->item_price;
        }
        $branch = DB::table('nso007_branches')->where('id',\Branch::branch_id())->first();
        return view('administrator.accounting.payable-note.report-print-po',compact('deposit_po','branch','total','total_price'));

    }
}
