<?php
namespace App\Model;


class Stock extends GenericObject
{
    static $table_name = 'stock';

    public function cargar($data){
        $this->stk_articu = $data->articulo->id;
        $this->stk_coddep = $data->deposito->id;
        $this->stk_canti = $data->cantidad;
    }

    public static function toJSON($result)
    {
        if ($result === null) return $result;
        $temp = array();
        $depositos = json_decode(Deposito::toJSON(Deposito::all()), true);
        $depositos = Stock::group_by($depositos,"id");
        foreach($result as $item){
            if (sizeof($depositos[$item->stk_coddep])>0) {
                $object = array(
                    "deposito" => $depositos[$item->stk_coddep][0],
                    "cantidad" => $item->stk_canti,
                    "articulo_id" => $item->stk_articu
                );
                $temp[] = $object;
            }
        }
        return json_encode($temp);
    }

}