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;
|
|
|
|
use 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;
|
2022-09-19 02:18:39 -03:00
|
|
|
protected $fillable = [ "nombre","region","telefono","correo","referente_finanzas","cantidad_de_nucleos","fila"];
|
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
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-10-01 15:29:31 -03:00
|
|
|
//Asume que los productos están gruadados en orden de fila
|
2023-03-20 20:59:59 -03:00
|
|
|
public function obtenerTemplateDeFilasVacias(){
|
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++) {
|
|
|
|
$template[$i] = ["", "0"];
|
|
|
|
}
|
|
|
|
$template[$fila] = [$productosIDNombre[$id], "0"];
|
|
|
|
$num_fila = $fila+1;
|
|
|
|
}
|
2022-10-14 19:14:24 -03:00
|
|
|
$template[$this->obtenerFilaDeBonoTransporte()] = ["Bonos de transporte", 0];
|
2022-10-01 15:29:31 -03:00
|
|
|
return $template;
|
|
|
|
}
|
|
|
|
|
2022-10-14 19:14:24 -03:00
|
|
|
private function obtenerFilaDeBonoTransporte() {
|
|
|
|
$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');
|
|
|
|
}
|
|
|
|
|
|
|
|
private function totalPedidoSinBDT() {
|
|
|
|
$total = 0;
|
|
|
|
foreach ($this->subpedidos as $subpedido) {
|
|
|
|
$total += $subpedido->totalSinBDT();
|
|
|
|
}
|
|
|
|
return $total;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function calcularCantidadBDT() {
|
|
|
|
return ceil($this->totalPedidoSinBDT() / 500);
|
|
|
|
}
|
|
|
|
|
2022-09-19 02:18:39 -03:00
|
|
|
public function exportarPedidoEnCSV(){
|
|
|
|
$productos_en_pedido = DB::table('pedidos_aprobados')->where('grupo_de_compra_id',$this->id)->get()->keyBy('producto_id');
|
2022-10-14 19:14:24 -03:00
|
|
|
|
2022-10-01 15:29:31 -03:00
|
|
|
//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();
|
|
|
|
$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;
|
2022-09-19 02:18:39 -03:00
|
|
|
}
|
|
|
|
|
2022-10-14 19:14:24 -03:00
|
|
|
$records[$this->obtenerFilaDeBonoTransporte()][1] = $this->calcularCantidadBDT();
|
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function exportarTodosLosPedidosEnCSV(){
|
|
|
|
$gdcs=GrupoDeCompra::all();
|
|
|
|
foreach ($gdcs as $gdc){ $gdc->exportarPedidoEnCSV(); }
|
|
|
|
}
|
|
|
|
|
2021-12-30 13:12:14 -03:00
|
|
|
}
|