<?php


namespace App\Repositories;


use App\Http\Controllers\PreferenceController;
use App\Invoice;
use Barryvdh\DomPDF\Facade as PDF;
use Carbon\Carbon;
use Illuminate\Support\Facades\DB;
use LaravelDaily\Invoices\Invoice as InvoicesExport;
use LaravelDaily\Invoices\Classes\Buyer;
use LaravelDaily\Invoices\Classes\InvoiceItem;
use Illuminate\Filesystem\Filesystem;

class InvoiceRepository {

    public static function all() {
        return Invoice::all();
    }

    public static function find($id){
        return Invoice::findOrFail($id);
    }

    public static function create(array $data) {
        return Invoice::create($data);
    }

    public static function update(array $data, $id) {
        Invoice::findOrFail($id)->update($data);
        return Invoice::findOrFail($id);
    }

    public static function delete($id){
        return Invoice::destroy($id);
    }

    /**
     * ALTRES FUNCIONS
     */

    //MÈTODE MOLT CRÍTIC! VIGILAR MOLT AL RETOCAR-LO
    public static function getInvoiceNextNumber($type, $serie, $prefix = null, $company_id = null) {
        if($type==="expense") {
            $row = DB::table("invoices")->select("invoice_prefix", "invoice_serie", "invoice_number")
                ->whereRaw(
                $prefix ?
                    "invoice_prefix = '$prefix' and invoice_serie = '$serie'" :
                    "invoice_prefix is null and invoice_serie = '$serie'"

                )
                ->orderByDesc("invoice_number")
                ->first();
        }
        else {
            $row = DB::table("invoices")->select("invoice_prefix", "invoice_serie", "invoice_number")
                ->whereRaw($company_id ?
                    ($prefix ?
                        "invoice_prefix = '$prefix' and invoice_serie = '$serie' and company_id = $company_id" :
                        "invoice_prefix is null and invoice_serie = '$serie' and company_id = $company_id"
                    ) :
                    ($prefix ?
                        "invoice_prefix = '$prefix' and invoice_serie = '$serie' and company_id is null" :
                        "invoice_prefix is null and invoice_serie = '$serie' and company_id is null"
                    )
                )
                ->orderByDesc("invoice_number")
                ->first();
        }

        $last_number = $row ? $row->invoice_number : 0;

        return $last_number + 1;
    }

    public static function exportHTML($id) {
        $invoice = InvoiceRepository::find($id);
        $company = $invoice->box_order->box->company;
        $user = $invoice->box_order->user;
        $boxOrder = $invoice->box_order;
        $lines = $invoice->box_order->lines;

        $userSubscription = null;
        $pricing = null;
        $lineUserSubscription = null;
        $lin = [];
        $total = 0;
        $subtotal = 0;

        foreach ($lines as $l) {
            if ($l->user_subscription_id!=null) {
                $userSubscription = UserSubscriptionRepository::find($l->user_subscription_id);
                $pricing = $userSubscription->pricing;
                $lineUserSubscription = [
                    "id"            => $userSubscription->id,
                    "name"          => $pricing->name,
                    "vat"           => $l->vat,
                    "description"   => $pricing->description,
                    "subtotal"      => $l->subtotal,
                    "total"         => $l->total
                ];
            } else array_push($lin,$l);
            $subtotal += $l->subtotal;
            $total += $l->total;
        }

        $preferences = PreferenceRepository::getInfo();

        $viewData = [
            'invoice'               => $invoice,
            'company'               => $company,
            'user'                  => $user,
            'boxOrder'              => $boxOrder,
            'userSubscription'      => $userSubscription,
            'pricing'               => $pricing,
            'lines'                 => $lines,
            'lineUserSubscription'  => $lineUserSubscription,
            'subtotal'              => $subtotal,
            'total'                 => $total,
            'preferences'           => $preferences
        ];

        /*$pdf = PDF::loadView('invoices/simpleInvoice',
                                [
                                    'invoice'               => $invoice,
                                    'company'               => $company,
                                    'user'                  => $user,
                                    'boxOrder'              => $boxOrder,
                                    'userSubscription'      => $userSubscription,
                                    'pricing'               => $pricing,
                                    'lines'                 => $lines,
                                    'lineUserSubscription'  => $lineUserSubscription,
                                    'subtotal'              => $subtotal,
                                    'total'                 => $total,
                                    'preferences'           => $preferences
                                ]);
        $data = Carbon::now()->format('Y-m-d');
        $pdf->save('pdf/'.$invoice->official_number.'_'.$data.'.pdf');*/

        /*$fs = new Filesystem();
        $data = Carbon::now()->format('Y-m-d');
        $nameFile = $invoice->official_number.'_'.$data.'.html';
        $fs->put('invoices/html/'.$nameFile, \View::make('invoices/simpleInvoice', $viewData));*/

        return view('invoices/simpleInvoice', $viewData);

    }
}
