mirror of
https://git.imnavajas.es/jjimenez/safekat.git
synced 2025-07-25 22:52:08 +00:00
203 lines
7.5 KiB
PHP
203 lines
7.5 KiB
PHP
<?php
|
|
|
|
namespace App\Models\Albaranes;
|
|
|
|
class AlbaranModel extends \App\Models\BaseModel
|
|
{
|
|
protected $table = "albaranes";
|
|
|
|
/**
|
|
* Whether primary key uses auto increment.
|
|
*
|
|
* @var bool
|
|
*/
|
|
protected $useAutoIncrement = true;
|
|
|
|
protected $primaryKey = 'id';
|
|
protected $returnType = 'App\Entities\Albaranes\AlbaranEntity';
|
|
protected $allowedFields = [
|
|
'envio_id',
|
|
'cliente_id',
|
|
'serie_id',
|
|
'numero_albaran',
|
|
'mostrar_precios',
|
|
'direccion_albaran',
|
|
'att_albaran',
|
|
'user_created_id',
|
|
'user_updated_id',
|
|
'created_at',
|
|
'updated_at',
|
|
'deleted_at',
|
|
'cajas',
|
|
];
|
|
|
|
protected $useSoftDeletes = true;
|
|
protected $useTimestamps = true;
|
|
protected $createdField = 'created_at';
|
|
protected $updatedField = 'updated_at';
|
|
protected $deletedField = 'deleted_at';
|
|
|
|
public function generarAlbaranes($envio_id, $envio_lineas, $cajas)
|
|
{
|
|
|
|
$user_id = auth()->user()->id;
|
|
|
|
if (!$envio_id || !$envio_lineas) {
|
|
return [
|
|
'status' => false
|
|
];
|
|
}
|
|
|
|
// el albaran es para el mismo cliente, por lo que se obtiene el cliente_id de la primera_linea
|
|
$cliente_id = $this->db->table('envios_lineas')
|
|
->select('presupuestos.cliente_id')
|
|
->join('presupuestos', 'presupuestos.id = envios_lineas.presupuesto_id')
|
|
->where('envios_lineas.id', $envio_lineas[0])
|
|
->get()
|
|
->getRow()->cliente_id;
|
|
|
|
// se genera el numero de albaran
|
|
$model_series = model('App\Models\Configuracion\SeriesFacturasModel');
|
|
$serie = $model_series->find(11);
|
|
$numero_albaran = str_replace('{number}', $serie->next, $serie->formato);
|
|
$numero_albaran = str_replace('{year}', date("Y"), $numero_albaran);
|
|
$serie->next = $serie->next + 1;
|
|
$model_series->save($serie);
|
|
|
|
// Se genera el albaran con los datos del envio
|
|
$model_envio = model('App\Models\Logistica\EnvioModel');
|
|
$envio = $model_envio->find($envio_id);
|
|
$data = [
|
|
'envio_id' => $envio->id,
|
|
'cliente_id' => $cliente_id,
|
|
'serie_id' => 11, // Serie de albaranes
|
|
'numero_albaran' => $numero_albaran,
|
|
'mostrar_precios' => 0,
|
|
'direccion_albaran' => $envio->direccion,
|
|
'att_albaran' => $envio->att,
|
|
'user_created_id' => $user_id,
|
|
'cajas' => $cajas,
|
|
];
|
|
$id_albaran = $this->insert($data);
|
|
if(!$id_albaran) {
|
|
return [
|
|
'status' => false
|
|
];
|
|
}
|
|
|
|
// Se generan las lineas del albaran
|
|
$model_albaran_linea = model('App\Models\Albaranes\AlbaranLineaModel');
|
|
$albaran_linea = [];
|
|
foreach ($envio_lineas as $linea) {
|
|
$modelLineaEnvio = model('App\Models\Logistica\EnvioLineaModel');
|
|
$datosLinea = $this->db->table('envios_lineas')
|
|
->select('presupuestos.titulo as titulo, presupuestos.isbn as isbn, presupuestos.referencia_cliente as ref_cliente,
|
|
envios_lineas.unidades_envio as cantidad, presupuestos.total_precio_unidad as precio_unidad, presupuestos.iva_reducido as iva_reducido,
|
|
ROUND(envios_lineas.unidades_envio * presupuestos.total_precio_unidad, 2) as total, pedidos_linea.id as pedido_linea_id')
|
|
->join('presupuestos', 'presupuestos.id = envios_lineas.presupuesto_id')
|
|
->join('pedidos', 'pedidos.id = envios_lineas.pedido_id')
|
|
->join('pedidos_linea', 'pedidos_linea.pedido_id = pedidos.id')
|
|
->where('envios_lineas.id', $linea)
|
|
->get()
|
|
->getRow();
|
|
$linea = $modelLineaEnvio->find($linea);
|
|
if (!$linea) {
|
|
continue;
|
|
}
|
|
$albaran_linea = [
|
|
'albaran_id' => $id_albaran,
|
|
'titulo' => $datosLinea->titulo,
|
|
'isbn' => $datosLinea->isbn,
|
|
'ref_cliente' => $datosLinea->ref_cliente,
|
|
'cantidad' => $datosLinea->cantidad,
|
|
'precio_unidad' => $datosLinea->precio_unidad,
|
|
'iva_reducido' => $datosLinea->iva_reducido,
|
|
'total' => $datosLinea->total,
|
|
'user_created_id' => $user_id,
|
|
'user_updated_id' => $user_id,
|
|
'pedido_linea_id' => $datosLinea->pedido_linea_id,
|
|
|
|
];
|
|
$model_albaran_linea->insert($albaran_linea);
|
|
}
|
|
|
|
$albaran_data = $this->db->table('albaranes t1')
|
|
->select("
|
|
t1.id,
|
|
t1.att_albaran AS att,
|
|
t1.direccion_albaran AS direccion,
|
|
t1.envio_id,
|
|
t1.numero_albaran AS numero_albaran,
|
|
DATE_FORMAT(t1.created_at, '%d/%m/%Y') AS fecha_creacion,
|
|
DATE_FORMAT(t1.fecha_albaran, '%d/%m/%Y') AS fecha_albaran,
|
|
t1.mostrar_precios AS mostrar_precios,
|
|
t1.cajas AS cajas,
|
|
")
|
|
->where('t1.id', $id_albaran)
|
|
->get()
|
|
->getResultObject();
|
|
$modelCliente = model('App\Models\Clientes\ClienteModel');
|
|
$cliente = $modelCliente->find($cliente_id);
|
|
$albaran_data[0]->cliente = $cliente->nombre;
|
|
|
|
return [
|
|
'status' => true,
|
|
'albaran' => $albaran_data[0],
|
|
];
|
|
}
|
|
|
|
public function getAlbaranesEnvio($envio_id=null){
|
|
if (!$envio_id) {
|
|
return [];
|
|
}
|
|
|
|
$albaran_data = $this->db->table('albaranes t1')
|
|
->select("
|
|
t1.id,
|
|
t1.att_albaran AS att,
|
|
t1.direccion_albaran AS direccion,
|
|
t1.envio_id,
|
|
t1.numero_albaran AS numero_albaran,
|
|
DATE_FORMAT(t1.created_at, '%d/%m/%Y') AS fecha_creacion,
|
|
DATE_FORMAT(t1.fecha_albaran, '%d/%m/%Y') AS fecha_albaran,
|
|
t1.mostrar_precios AS mostrar_precios,
|
|
t2.nombre AS cliente,
|
|
t1.cajas AS cajas
|
|
")
|
|
->join('clientes t2', 't1.cliente_id = t2.id', 'left')
|
|
->where('t1.envio_id', $envio_id)
|
|
->where('t1.deleted_at IS NULL')
|
|
->get()
|
|
->getResultObject();
|
|
return $albaran_data;
|
|
}
|
|
|
|
/**
|
|
* Get resource data for creating PDFs.
|
|
*
|
|
* @param string $search
|
|
*
|
|
* @return \CodeIgniter\Database\BaseBuilder
|
|
*/
|
|
public function getResourceForPdf($albaran_id = -1)
|
|
{
|
|
$builder = $this->db
|
|
|
|
->table($this->table . " t1")
|
|
->select(
|
|
"t1.id AS id, t1.pedido_id AS pedido_id, t1.presupuesto_id AS presupuesto_id,
|
|
t1.presupuesto_direccion_id AS presupuesto_direccion_id, t1.cliente_id AS cliente_id,
|
|
t1.serie_id AS serie_id, t1.numero_albaran AS numero_albaran, t1.mostrar_precios AS mostrar_precios,
|
|
t1.total AS total, t1.direccion_albaran AS direccion_albaran, t1.att_albaran AS att_albaran,
|
|
t1.user_created_id AS user_created_id, t1.user_updated_id AS user_updated_id,
|
|
t1.created_at AS created_at, t1.updated_at AS updated_at,
|
|
t2.nombre AS cliente"
|
|
);
|
|
$builder->join("clientes t2", "t1.cliente_id = t2.id", "left");
|
|
|
|
$builder->where("t1.deleted_at IS NULL");
|
|
$builder->where("t1.id", $albaran_id);
|
|
|
|
return $builder;
|
|
}
|
|
} |