2021-12-30 13:12:14 -03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App;
|
|
|
|
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2022-09-06 21:38:01 -03:00
|
|
|
use Mpdf\Mpdf;
|
2022-09-19 02:18:39 -03:00
|
|
|
use League\Csv\CannotInsertRecord;
|
|
|
|
use League\Csv\Writer;
|
|
|
|
use App\Producto;
|
2024-09-01 13:11:53 -03:00
|
|
|
use Illuminate\Support\Facades\DB;
|
2022-10-14 19:14:24 -03:00
|
|
|
use League\Csv\Reader;
|
2021-12-30 13:12:14 -03:00
|
|
|
|
|
|
|
class GrupoDeCompra extends Model
|
|
|
|
{
|
|
|
|
public $timestamps = false;
|
2023-11-12 20:13:17 -03:00
|
|
|
protected $fillable = [ "nombre","region","telefono","correo","referente_finanzas","cantidad_de_nucleos","fila", "devoluciones_habilitadas"];
|
2021-12-30 13:12:14 -03:00
|
|
|
protected $table = 'grupos_de_compra';
|
2022-01-07 18:02:48 -03:00
|
|
|
protected $hidden = ['password'];
|
2022-02-22 10:45:16 -03:00
|
|
|
|
2024-07-11 17:03:19 -03:00
|
|
|
public function toggleDevoluciones()
|
|
|
|
{
|
|
|
|
$this->devoluciones_habilitadas = !$this->devoluciones_habilitadas;
|
|
|
|
$this->save();
|
|
|
|
return $this->devoluciones_habilitadas;
|
|
|
|
}
|
|
|
|
|
2022-02-22 10:45:16 -03:00
|
|
|
public function subpedidos() {
|
|
|
|
return $this->hasMany('App\Subpedido');
|
|
|
|
}
|
2022-09-06 21:38:01 -03:00
|
|
|
|
2022-11-12 18:44:22 -03:00
|
|
|
public function pedidosAprobados() {
|
|
|
|
return $this->subpedidos->where('aprobado',1);
|
|
|
|
}
|
|
|
|
|
2022-09-06 21:38:01 -03:00
|
|
|
public function exportarPlanillasAPdf() {
|
2022-11-12 18:44:22 -03:00
|
|
|
$subpedidos = $this->pedidosAprobados();
|
2022-09-06 21:38:01 -03:00
|
|
|
//generar pdf
|
2022-11-12 18:44:22 -03:00
|
|
|
$mpdf = new Mpdf();
|
2022-09-06 21:38:01 -03:00
|
|
|
foreach ($subpedidos as $subpedido) {
|
|
|
|
$tabla = $subpedido->generarHTML();
|
|
|
|
// agregar la tabla al pdf en una nueva página
|
|
|
|
$mpdf->WriteHTML($tabla);
|
|
|
|
$mpdf->AddPage();
|
|
|
|
}
|
2022-11-12 18:53:06 -03:00
|
|
|
$filename = $this->nombre . '.pdf';
|
2022-09-06 21:38:01 -03:00
|
|
|
// imprimir el pdf
|
2022-11-12 18:53:06 -03:00
|
|
|
$mpdf->Output($filename, "D");
|
2022-09-06 21:38:01 -03:00
|
|
|
}
|
|
|
|
|
2024-08-27 20:57:11 -03:00
|
|
|
static function filaVacia(string $product, int $columns): array
|
|
|
|
{
|
2023-07-06 00:18:52 -03:00
|
|
|
$fila = [$product];
|
|
|
|
for ($i = 1; $i <= $columns; $i++) {
|
|
|
|
$fila[$i] = "0";
|
|
|
|
}
|
|
|
|
return $fila;
|
|
|
|
}
|
|
|
|
|
2022-10-01 15:29:31 -03:00
|
|
|
//Asume que los productos están gruadados en orden de fila
|
2024-08-27 20:57:11 -03:00
|
|
|
public static function obtenerTemplateDeFilasVacias(int $columns){
|
2022-10-01 15:29:31 -03:00
|
|
|
$productosFilaID = Producto::productosFilaID();
|
|
|
|
$productosIDNombre = Producto::productosIDNombre();
|
|
|
|
$num_fila = 1;
|
|
|
|
$template = [];
|
|
|
|
foreach ($productosFilaID as $fila => $id) {
|
|
|
|
for ($i = $num_fila; $i < $fila; $i++) {
|
2024-08-27 20:57:11 -03:00
|
|
|
$template[$i] = GrupoDeCompra::filaVacia("", $columns);
|
2022-10-01 15:29:31 -03:00
|
|
|
}
|
2024-08-27 20:57:11 -03:00
|
|
|
$template[$fila] = GrupoDeCompra::filaVacia($productosIDNombre[$id], $columns);
|
2022-10-01 15:29:31 -03:00
|
|
|
$num_fila = $fila+1;
|
|
|
|
}
|
2024-08-27 20:57:11 -03:00
|
|
|
$template[GrupoDeCompra::obtenerFilaDeBonoTransporte()] = GrupoDeCompra::filaVacia("Bonos de transporte", $columns);
|
2022-10-01 15:29:31 -03:00
|
|
|
return $template;
|
|
|
|
}
|
|
|
|
|
2024-08-27 20:57:11 -03:00
|
|
|
private static function obtenerFilaDeBonoTransporte() {
|
2022-10-14 19:14:24 -03:00
|
|
|
$csv = Reader::createFromPath(resource_path('csv/productos.csv'), 'r');
|
|
|
|
$csv->setDelimiter("|");
|
|
|
|
$csv->setEnclosure("'");
|
|
|
|
$registros = $csv->getRecords();
|
|
|
|
|
|
|
|
foreach($registros as $key => $registro)
|
2023-03-20 20:59:59 -03:00
|
|
|
if ($registro[0] == 'T') return $key;
|
2022-10-14 19:14:24 -03:00
|
|
|
|
|
|
|
throw new Exception('No hay bono de transporte');
|
|
|
|
}
|
|
|
|
|
2023-05-24 15:01:31 -03:00
|
|
|
private function totalPedidosSinBonos() {
|
2022-10-14 19:14:24 -03:00
|
|
|
$total = 0;
|
2023-05-24 15:01:31 -03:00
|
|
|
foreach ($this->pedidosAprobados() as $pedido) {
|
|
|
|
$total += ceil($pedido->totalSinBonos());
|
2022-10-14 19:14:24 -03:00
|
|
|
}
|
|
|
|
return $total;
|
|
|
|
}
|
|
|
|
|
2023-05-24 15:01:31 -03:00
|
|
|
public function calcularCantidadBDT() {
|
2024-04-08 15:31:00 -03:00
|
|
|
return ceil($this->totalPedidosSinBonos() / 500);
|
2022-10-14 19:14:24 -03:00
|
|
|
}
|
|
|
|
|
2023-10-04 22:37:59 -03:00
|
|
|
public function totalBonosBarriales() {
|
|
|
|
$total = 0;
|
|
|
|
$bonoBarrial = Producto::where('nombre','LIKE','%barrial%')->first();
|
|
|
|
if ($bonoBarrial) {
|
|
|
|
$pedidos = $this->pedidosAprobados();
|
|
|
|
foreach ($pedidos as $pedido) {
|
|
|
|
$bonoPedido = $pedido->productos()->find($bonoBarrial["id"]);
|
|
|
|
if ($bonoPedido) {
|
|
|
|
$total += $bonoPedido["pivot"]["total"];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $total;
|
|
|
|
}
|
|
|
|
|
2022-09-19 02:18:39 -03:00
|
|
|
public function exportarPedidoEnCSV(){
|
2024-08-27 20:57:11 -03:00
|
|
|
$records = $this->generarColumnaCantidades();
|
2022-09-19 02:18:39 -03:00
|
|
|
try {
|
|
|
|
$writer = Writer::createFromPath(resource_path('csv/exports/'.$this->nombre.'.csv'), 'w');
|
|
|
|
$writer->insertAll($records);
|
|
|
|
} catch (CannotInsertRecord $e) {
|
|
|
|
var_export($e->getRecords());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-27 20:57:11 -03:00
|
|
|
public function generarColumnaCantidades() {
|
|
|
|
$productos_en_pedido = DB::table('pedidos_aprobados')->where('grupo_de_compra_id',$this->id)->get()->keyBy('producto_id');
|
|
|
|
|
|
|
|
//si no hay pedidos aprobados, salir
|
|
|
|
if ($productos_en_pedido->count() == 0) {
|
|
|
|
\Log::debug("El grupo de compra ". $this->nombre . " no tiene pedidos aprobados.");
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
$records = $this->obtenerTemplateDeFilasVacias(1);
|
|
|
|
$productos_id_fila = Producto::productosIdFila();
|
|
|
|
foreach ($productos_en_pedido as $id => $producto_pedido){
|
|
|
|
$fila = $productos_id_fila[$id];
|
|
|
|
$records[$fila][1] = $producto_pedido->cantidad_pedida;
|
|
|
|
}
|
|
|
|
|
|
|
|
$records[$this->obtenerFilaDeBonoTransporte()][1] = $this->calcularCantidadBDT();
|
|
|
|
|
|
|
|
return $records;
|
|
|
|
}
|
|
|
|
|
2023-07-06 00:18:52 -03:00
|
|
|
public function exportarPedidoConNucleosEnCSV() {
|
|
|
|
$productos_en_pedido = DB::table('pedidos_aprobados')->where('grupo_de_compra_id',$this->id)->get()->keyBy('producto_id');
|
|
|
|
|
|
|
|
// si no hay pedidos aprobados, salir
|
2024-08-27 20:57:11 -03:00
|
|
|
if ($productos_en_pedido->count() == 0) {
|
|
|
|
\Log::debug("El grupo de compra ". $this->nombre . " no tiene pedidos aprobados.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-07-06 00:18:52 -03:00
|
|
|
$pedidos = $this->pedidosAprobados();
|
|
|
|
// Generar tabla vacía con una columna por núcleo
|
|
|
|
$records = $this->obtenerTemplateDeFilasVacias($pedidos->count());
|
|
|
|
$productos_id_fila = Producto::productosIdFila();
|
2024-08-27 20:57:11 -03:00
|
|
|
|
2023-07-06 00:18:52 -03:00
|
|
|
foreach ($productos_en_pedido as $id => $producto_pedido) {
|
|
|
|
$fila = $productos_id_fila[$id];
|
|
|
|
$i = 1;
|
|
|
|
// Poner cantidad de cada producto para cada núcleo
|
|
|
|
foreach ($pedidos as $pedido) {
|
|
|
|
$producto = $pedido->productos()->find($id);
|
|
|
|
$records[$fila][$i] = $producto == NULL ? 0 : $producto["pivot"]["cantidad"];
|
|
|
|
$i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Insertar lista de núcleos en la primera fila
|
|
|
|
$nucleos = [""];
|
|
|
|
$i = 1;
|
|
|
|
foreach ($pedidos as $pedido) {
|
|
|
|
$nucleos[$i] = $pedido->nombre;
|
|
|
|
$i++;
|
|
|
|
}
|
|
|
|
array_splice($records, 0, 0, array($nucleos));
|
2024-08-27 20:57:11 -03:00
|
|
|
|
2023-07-06 00:18:52 -03:00
|
|
|
// Guardar en un archivo .csv
|
|
|
|
try {
|
|
|
|
$writer = Writer::createFromPath(resource_path('csv/exports/'.$this->nombre.'-completo.csv'), 'w');
|
|
|
|
$writer->insertAll($records);
|
|
|
|
} catch (CannotInsertRecord $e) {
|
|
|
|
var_export($e->getRecords());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-19 02:18:39 -03:00
|
|
|
public static function exportarTodosLosPedidosEnCSV(){
|
2024-08-27 20:57:11 -03:00
|
|
|
$gdcs = GrupoDeCompra::all();
|
|
|
|
$planilla = GrupoDeCompra::obtenerTemplateDeFilasVacias($gdcs->count());
|
|
|
|
$barrios = [""];
|
|
|
|
foreach ($gdcs as $gdc) {
|
|
|
|
$barrios[] = $gdc->nombre;
|
|
|
|
}
|
|
|
|
array_splice($planilla, 0, 0, array($barrios));
|
|
|
|
|
|
|
|
// Guardar en un archivo .csv
|
|
|
|
try {
|
|
|
|
$writer = Writer::createFromPath(resource_path('csv/exports/total-pedidos.csv'), 'w');
|
|
|
|
$writer->insertAll($planilla);
|
|
|
|
} catch (CannotInsertRecord $e) {
|
|
|
|
var_export($e->getRecords());
|
|
|
|
}
|
2022-09-19 02:18:39 -03:00
|
|
|
}
|
2021-12-30 13:12:14 -03:00
|
|
|
}
|