<?php


namespace App\Repositories;


use App\Box;
use App\BoxOrder;
use App\BoxOrderLine;
use App\Discount;

class BoxOrderRepository implements RepositoryInterface
{
    public static function all() {
        return BoxOrder::all();
    }

    public static function create(array $data){

        // Calculem el preu de cada linea amb el discount ( si en te ) i anem sumant el total al preu total
        $lines = [];
        $currentTotal = 0;
        $currentSubtotal = 0;
        $vat = Box::find($data['box_id'])->tax_prcnt_box_order;
        $data['vat'] = $vat;


        foreach ($data['lines'] as $l) {
            if (isset($l['discount_id']) && $l['discount_id']!=null) {
                $discount = Discount::find($l['discount_id']);
                if ($discount->price) $price = $l['price'] - $discount->price;
                else if ($discount->percentatge) $price = $l['price'] * (100 - $discount->percentatge / 100);
            } else {
                $price = $l['price'];
            }
            $l['total'] = number_format((float)($price * $l['quantity']), 2, '.', '');
            $currentTotal += $l['total'];
            $l['subtotal'] = number_format((float)(((($price*(100))/(100+$vat)) * $l['quantity'])), 2, '.', '');
            $currentSubtotal += $l['subtotal'];
            $l['vat'] = $vat;
            array_push($lines, $l);
        }


        // Mirem si hi ha discount aplicable a la totalitat de la comanda
        if ($data['discount_id'] && $l['discount_id']!=null) {
            $discount = Discount::find($data['discount_id']);
            if ($discount->price) {
                $currentTotal = $currentTotal - $discount->price;
                $currentSubtotal = $currentSubtotal - $discount->price;
            }
            else if ($discount->percentatge) {
                $currentTotal = $currentTotal - $discount->percentatge;
                $currentSubtotal = $currentSubtotal - $discount->percentatge;
            }
        }

        $data['subtotal'] = number_format((float)($currentSubtotal), 2, '.', '');
        $data['total'] = number_format((float)($currentTotal), 2, '.', '');

        $order = BoxOrder::create($data);

        foreach ($lines as $l) {
            $order->lines()->create($l);
        }

        return BoxOrder::find($order->id);
    }

    public static function update(array $data, $id){

        // Calculem el preu de cada linea amb el discount ( si en te ) i anem sumant el total al preu total
        $lines = [];
        $currentTotal = 0;
        $currentSubtotal = 0;
        $vat = Box::find($data['box_id'])->tax_prcnt_box_order;
        $data['vat'] = $vat;
        foreach ($data['lines'] as $l) {

            if ($l['discount_id']) {
                $discount = Discount::find($l['discount_id']);
                if ($discount->price) $price = $l['price'] - $discount->price;
                else if ($discount->percentatge) $price = $l['price'] * (100 - $discount->percentatge/100);
            } else {
                $price = $l['price'];
            }
            $l['total'] = number_format((float)($price * $l['quantity']), 2, '.', '');
            $currentTotal += $l['total'];
            $l['subtotal'] = number_format((float)(((($price*(100))/(100+$vat)) * $l['quantity'])), 2, '.', '');
            $currentSubtotal += $l['subtotal'];
            $l['vat'] = $vat;
            array_push($lines, $l);
        }

        // Mirem si hi ha discount aplicable a la totalitat de la comanda
        if ($data['discount_id']) {
            $discount = Discount::find($data['discount_id']);
            if ($discount->price) {
                $currentTotal = $currentTotal - $discount->price;
                $currentSubtotal = $currentSubtotal - $discount->price;
            }
            else if ($discount->percentatge) {
                $currentTotal = $currentTotal - $discount->percentatge;
                $currentSubtotal = $currentSubtotal - $discount->percentatge;
            }
        }

        $data['subtotal'] = number_format((float)($currentSubtotal), 2, '.', '');
        $data['total'] = number_format((float)($currentTotal), 2, '.', '');

        $order = BoxOrder::find($id);
        $order->update($data);


        $order->lines()->delete();
        foreach ($lines as $l) {
            $order->lines()->create($l);
        }
        return BoxOrder::findOrFail($id);
    }

    public static function delete($id){
        BoxOrder::find($id)->lines()->delete();
        return BoxOrder::destroy($id);
    }

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

    public static function boxOrderLines($id){
        $order = BoxOrder::findOrFail($id);
        return $order->lines;
    }

    public static function payments($id){
        return BoxOrder::findOrFall($id)->payments;
    }

    public static function associateLines($id,$lines_id){
        $order = BoxOrder::findOrFail($id);
        foreach ($lines_id as $l){
            $order->lines()->attach($lines_id);
        }
        return true;
    }

    public static function getOrdersByUserId($user_id, $box_id = null) {
        $condition = [["user_id", $user_id]];
        if($box_id) {
            $condition = array_push($condition, ["box_id", $box_id]);
        }

        return BoxOrder::where($condition)->get();
    }

    public static function  getDiscount($id) {
        return BoxOrder::findOrFail($id)->discount;
    }

    public static function box($id) {
        return BoxOrder::findOrFail($id)->box;
    }

    public static function user($id) {
        return BoxOrder::findOrFail($id)->user;
    }
}
