<?php


namespace App\Http\Controllers;
use Carbon;
use Request;
use App\User;
use App\Services\Invoice;
use App\Services\Payment;
use App\Models\ShoppingOrder;


class PaymentInvoiceController extends Controller
{

    private $startYear;
    private $endYear;
    private $rangeYears;
    private $activeYear;

    public function __construct()
    {
        $this->middleware('auth');
        $this->startYear = 2021;
        $this->endYear = date('Y');
        $this->rangeYears = range($this->startYear, $this->endYear);
        $this->activeYear = $this->endYear;

    }

    public function index()
    {
        $this->setActiveYears();
        $date1 = Carbon::parse('01.01.'.$this->activeYear." 00:00:00")->format('Y-m-d H:i:s');
        $date2 = Carbon::parse('31.12.'.$this->activeYear." 23:59:59")->toDateString();
    
        $data = [
            'years' => $this->rangeYears,
            'active_year' => $this->activeYear,
        ];
        return view('admin.payment.invoice.index', $data);
    }

    private function setActiveYears(){
        if(Request::get('filter_sales_year')){
            $this->activeYear = Request::get('filter_sales_year');
        }
    }

    public function datatable(){

        $this->setActiveYears();
        $date1 = Carbon::parse('01.01.'.$this->activeYear." 00:00:00")->format('Y-m-d H:i:s');
        $date2 = Carbon::parse('31.12.'.$this->activeYear." 23:59:59")->toDateString();
    
        $query = ShoppingOrder::with('shopping_user')->select('shopping_orders.*')
        ->where('shopping_orders.auth_user_id', '!=', NULL)  //::with('shopping_user', )->select('shopping_orders.*')
        ->where('mode', '=', 'live')
        //->where('paid', '=', 1)
        ->whereBetween('created_at', [$date1, $date2]);
        //->orderBy('created_at', 'DESC');

        return \DataTables::eloquent($query)
            ->addColumn('id', function (ShoppingOrder $ShoppingOrder) {
                return '<a href="' . route('admin_sales_detail', [$ShoppingOrder->id]) . '" class="btn icon-btn btn-sm btn-primary"><span class="fa fa-edit"></span></a>';
            })
            ->addColumn('total_shipping', function (ShoppingOrder $ShoppingOrder) {
                return $ShoppingOrder->getFormattedTotalShipping()." €";
            })
            ->addColumn('created_at', function (ShoppingOrder $ShoppingOrder) {
                return $ShoppingOrder->created_at->format("d.m.Y");
            })
            ->addColumn('shipping_order', function (ShoppingOrder $ShoppingOrder) {
                $ret = "";
                foreach($ShoppingOrder->shopping_order_items as $shopping_order_item){
                    $ret .= $shopping_order_item->product->name."<br>"; 
                }
                return $ret;
            })
            ->addColumn('txaction', function (ShoppingOrder $ShoppingOrder) {
                return Payment::getShoppingOrderBadge($ShoppingOrder);
            })
            ->addColumn('payment_for', function (ShoppingOrder $ShoppingOrder) {
                return Payment::getPaymentForTypeBadge($ShoppingOrder);
            })
            ->addColumn('invoice', function (ShoppingOrder $ShoppingOrder) {
                $ret = "";
                if(Invoice::isInvoice($ShoppingOrder)){
                    $ret .= '<a href="'.route('storage_file', [$ShoppingOrder->id, 'invoice', 'download']).'" class="btn btn-primary btn-xs"><i class="fa fa-download"></i></a> ';
                    $ret .= '<a href="'.route('storage_file', [$ShoppingOrder->id, 'invoice', 'stream']).'" target="_blank" class="btn btn-warning btn-xs"><i class="fa fa-eye"></i></a>';
                }else{
                    $ret = "-";
                }
                return $ret;
            })

            ->orderColumn('id', 'id $1')
            ->orderColumn('invoice_number', 'invoice_number $1')
            ->orderColumn('txaction', 'txaction $1')
            ->orderColumn('shipped', 'shipped $1')
            ->orderColumn('payment_for', 'payment_for $1')
            ->orderColumn('total_shipping', 'total_shipping $1')
            ->rawColumns(['id', 'shipping_order', 'txaction', 'payment_for', 'invoice'])
            ->make(true);
    }
}