<?php
namespace App\Controller;

use App\Definition\Controller;
use Incoviba\old\Venta\Entrega;
use Incoviba\old\Proyecto\Proyecto;
use Incoviba\old\Venta\Unidad;
use Stringy\Stringy;

class Other
{
	use Controller;
	
	protected static function setDefault()
	{
		self::$default = view('other.list');
	}
	public static function entregar_multiple()
	{
		if ($_SERVER['REQUEST_METHOD'] == 'POST') {
			$file = $_FILES['entregas'];
			$data = explode(PHP_EOL, trim(file_get_contents($file['tmp_name'])));
			foreach ($data as $line) {
				$info = explode(';', $line);
				if ($info[0] == 'Departamento' or $info[1] == 'Fecha') {
					continue;
				}
				$entrega = \Model::factory(Entrega::class)->create();
				$unidad = \Model::factory(Unidad::class)->where('descripcion', $info[0])->where('proyecto', post('proyecto'))->find_one();
				if (!$unidad->venta()->find_one()) {
					echo 'x';
					continue;
				}
				$venta = $unidad->venta()->find_one();
				$entrega->fecha = \Carbon\Carbon::parse($info[1])->format('Y-m-d');
				if ($venta->entrega == '0') {
					$entrega->save();
					$venta->entrega = $entrega->id;
					$venta->save();
					echo '.';
				} else {
					echo 'x';
				}
			}
		} else {
			$proyectos = \Model::factory(Proyecto::class)->order_by_asc('descripcion')->find_many();
			return view('other.entregar_multiple', compact('proyectos'));
		}
	}
	public static function capacidades()
	{
		$capacidades = [];
		$controllers = glob(config('locations.app') . '/Controller/*.php');
		foreach ($controllers as $controller) {
			if (basename($controller) == 'Admin.php' or basename($controller) == 'Other.php') {
				continue;
			}
			$class = Stringy::create($controller)->replace(config('locations.app'), '/App')->replace('.php', '')->replace('/', '\\')->__toString();
			$ref = new \ReflectionClass($class);
			$static = $ref->getMethods(\ReflectionMethod::IS_STATIC && \ReflectionMethod::IS_PUBLIC);
			foreach ($static as $method) {
				if ($method->name == 'setDefault' or $method->name == 'index') {
					continue;
				}
				$capacidades []= $method;
			}
		}
		return view('other.capacidades', compact('capacidades'));
	}
}
?>