<?php
namespace App\Services\Stats;


use Carbon\Carbon;
use App\Services\Util;
use App\Models\ShoppingOrder;

class Sales
{
    private $month;
    private $year;
    private $products;
    private $objects;
  

    public function __construct()
    {
        $this->month = 0;
        $this->year = 0;
        $this->products = [];
        $this->objects = [];
    }

    public function setFilterVars($month = null, $year = null, $products = null){
        $this->month = $month ? $month : intval(date('m'));
        $this->year = $year ? $year : intval(date('Y'));
        $this->products = $products;
    }

    public function setFilterProducts(){

        $ShoppingOrders = $this->getShoppingOrdersBy($this->month, $this->year);
        $products = [];
        foreach($ShoppingOrders as $ShoppingOrder){
            foreach($ShoppingOrder->shopping_order_items as $shopping_order_item){
                if($shopping_order_item->product && !$shopping_order_item->product->exclude_stats_sales && !isset($products[$shopping_order_item->product->id])){
                    $products[$shopping_order_item->product->id] = $shopping_order_item->product->name.' # '.  
                    ($shopping_order_item->product->single_commission ? $shopping_order_item->product->value_commission.' / '.$shopping_order_item->product->partner_commission : 'Staffelrabatt');
                }
            }
        }
        return $products;
    }

    private function getShoppingOrdersBy($month, $year){
        if($month == '13'){ //all the year
            $date_start = Carbon::parse('01.01.'.$year)->format('Y-m-d H:i:s');
            $date_end = Carbon::parse('31.12.'.$year)->endOfMonth()->format('Y-m-d H:i:s');
        }else{
            $date_start = Carbon::parse('01.'.$month.'.'.$year)->format('Y-m-d H:i:s');
            $date_end = Carbon::parse('01.'.$month.'.'.$year)->endOfMonth()->format('Y-m-d H:i:s');
        }        
        return  ShoppingOrder::where('paid', 1)->where('mode', 'live')->whereBetween('created_at', [$date_start, $date_end])->get();
    }

    public function getCollection(){

        $this->getObjects();
        $collection = collect();

        foreach($this->objects as $key => $obj){
            $collection->push([
                'id'                    =>  $key,
                'name'                  =>  $obj['name'],
                'number'                =>  $obj['number'],
                'qty'                   =>  $obj['qty'],
                'total'                 =>  $obj['total'],
                'pre_qty'               =>  $obj['pre_qty'],
                'pre_total'             =>  $obj['pre_total'],
                'single_commission'     =>  $obj['single_commission'],
                'value_commission'      =>  $obj['value_commission'],
                'partner_commission'    =>  $obj['partner_commission'],

            ]);
        }
        return $collection;
    }

    public function getObjects(){
        $this->readObjects();
        $this->readObjectsPreview();
        return $this->objects;
    }
  
    private function readObjects()
    {
        $shoppingOrders = $this->getShoppingOrdersBy($this->month, $this->year);
        $this->objects = [];
        
        $subtotal_full = 0; // gesamtumsatz
        $subtotal = 0;  // gesamtumsatz ohne rabatte
        $discount = 0;  // gesamtrabatte
        $subtotal_hide = 0; // ausgeschlossene Produkte

        foreach($shoppingOrders as $ShoppingOrder){
            $subtotal_full += $ShoppingOrder->subtotal_full;
            $subtotal += $ShoppingOrder->subtotal;
            $discount += $ShoppingOrder->discount;

            foreach($ShoppingOrder->shopping_order_items as $shopping_order_item){
                
                if($shopping_order_item->product){
                    if(!in_array($shopping_order_item->product->id, $this->products) && !$shopping_order_item->product->exclude_stats_sales){ //ausschließen der Produkte über filter und exclude_stats_sales
                        if(isset($this->objects[$shopping_order_item->product->id])){
                            $qty = intval($this->objects[$shopping_order_item->product->id]['qty'] + $shopping_order_item->qty);
                            $total = round($this->objects[$shopping_order_item->product->id]['total'] + ($shopping_order_item->price_net * $shopping_order_item->qty), 3);
                            $this->objects[$shopping_order_item->product->id]['qty'] = $qty;
                            $this->objects[$shopping_order_item->product->id]['total'] = $total;
                        }else{
                            $this->objects[$shopping_order_item->product->id] = [
                                'name' => $shopping_order_item->product->name, 
                                'number' => $shopping_order_item->product->number, 
                                'qty' => $shopping_order_item->qty,
                                'total' => round($shopping_order_item->price_net * $shopping_order_item->qty, 3),
                                'pre_qty' => 0,
                                'pre_total' => 0,
                                'single_commission' => $shopping_order_item->product->single_commission ? 'Ja' : 'Nein',
                                'value_commission' => $shopping_order_item->product->single_commission ? $shopping_order_item->product->value_commission : '',
                                'partner_commission' => $shopping_order_item->product->single_commission ? $shopping_order_item->product->partner_commission : '',
    
                            ];
                        }
                    }else{
                        $subtotal_hide += $shopping_order_item->price_net * $shopping_order_item->qty;
                    }
                }
            }
        }

      
       

        $this->objects[9990] = [
            'name' => 'Angezeigter Umsatz netto €', 
            'number' => '', 
            'qty' => '',
            'total' => round($subtotal_full - $subtotal_hide, 2),
            'pre_qty' => 0,
            'pre_total' => 0,
            'single_commission' => '',
            'value_commission' => '',
            'partner_commission' => '',

        ];

        $this->objects[9991] = [
            'name' => 'Ausgeblendeter Umsatz netto €', 
            'number' => '', 
            'qty' => '',
            'total' => $subtotal_hide,
            'pre_qty' => 0,
            'pre_total' => 0,
            'single_commission' => '',
            'value_commission' => '',
            'partner_commission' => '',

        ];

        $this->objects[9992] = [
            'name' => 'Gesamter Umsatz netto € (alle Verkäufe)', 
            'number' => '', 
            'qty' => '',
            'total' => $subtotal_full,
            'pre_qty' => 0,
            'pre_total' => 0,
            'single_commission' => '',
            'value_commission' => '',
            'partner_commission' => '',

        ];

        $this->objects[9998] = [
            'name' => 'Gesamte Rabatte netto € (alle Verkäufe)', 
            'number' => '', 
            'qty' => '',
            'total' => ($discount),
            'pre_qty' => 0,
            'pre_total' => 0,
            'single_commission' => '',
            'value_commission' => '',
            'partner_commission' => '',

        ];

        $this->objects[9999] = [
            'name' => 'Gesamt netto € (alle Verkäufe)', 
            'number' => '', 
            'qty' => '',
            'total' => ($subtotal),
            'pre_qty' => 0,
            'pre_total' => 0,
            'single_commission' => '',
            'value_commission' => '',
            'partner_commission' => '',

        ];

        //format total
        foreach($this->objects as $key => $obj){
            $this->objects[$key]['total'] = formatNumber($obj['total']);
        }

    }

    private function  readObjectsPreview(){


        $shoppingOrders = $this->getShoppingOrdersBy($this->month, $this->year-1);

        $subtotal_full = 0; // gesamtumsatz
        $subtotal = 0;  // gesamtumsatz ohne rabatte
        $discount = 0;  // gesamtrabatte
        $subtotal_hide = 0; // ausgeschlossene Produkte

        foreach($shoppingOrders as $ShoppingOrder){
            $subtotal_full += $ShoppingOrder->subtotal_full;
            $subtotal += $ShoppingOrder->subtotal;
            $discount += $ShoppingOrder->discount;

            foreach($ShoppingOrder->shopping_order_items as $shopping_order_item){
                
                if($shopping_order_item->product){
                    if(!in_array($shopping_order_item->product->id, $this->products) && !$shopping_order_item->product->exclude_stats_sales){ //ausschließen der Produkte über filter und exclude_stats_sales
                        if(isset($this->objects[$shopping_order_item->product->id])){   //einsetzen der Zahlen, wenn vorhanden
                            $qty = intval($this->objects[$shopping_order_item->product->id]['pre_qty'] + $shopping_order_item->qty);
                            $total = round($this->objects[$shopping_order_item->product->id]['pre_total'] + ($shopping_order_item->price_net * $shopping_order_item->qty), 3);
                            $this->objects[$shopping_order_item->product->id]['pre_qty'] = $qty;
                            $this->objects[$shopping_order_item->product->id]['pre_total'] = $total;
                        }else{ // nicht vorhanden, anlegen  
                            $this->objects[$shopping_order_item->product->id] = [
                                'name' => $shopping_order_item->product->name, 
                                'number' => $shopping_order_item->product->number, 
                                'qty' => 0,
                                'total' => 0,
                                'pre_qty' => $shopping_order_item->qty,
                                'pre_total' => round($shopping_order_item->price_net * $shopping_order_item->qty, 3),
                                'single_commission' => $shopping_order_item->product->single_commission ? 'Ja' : 'Nein',
                                'value_commission' => $shopping_order_item->product->single_commission ? $shopping_order_item->product->value_commission : '',
                                'partner_commission' => $shopping_order_item->product->single_commission ? $shopping_order_item->product->partner_commission : '',
    
                            ];
                        }
                    }else{
                        //ausgeschlossene Produkte
                        $subtotal_hide += $shopping_order_item->price_net * $shopping_order_item->qty;
                    }
                }
            }
        }

      
       

        $this->objects[9990]['pre_total'] = round($subtotal_full - $subtotal_hide, 2);
        $this->objects[9991]['pre_total'] = $subtotal_hide;
        $this->objects[9992]['pre_total'] = $subtotal_full;
        $this->objects[9998]['pre_total'] = ($discount);
        $this->objects[9999]['pre_total'] = ($subtotal);

        //format total
        foreach($this->objects as $key => $obj){
            $this->objects[$key]['pre_total'] = formatNumber($obj['pre_total']);
        }


    }
    
}