mirror of
https://git.imnavajas.es/jjimenez/safekat.git
synced 2025-07-25 22:52:08 +00:00
161 lines
6.2 KiB
PHP
Executable File
161 lines
6.2 KiB
PHP
Executable File
<?php
|
|
|
|
namespace App\Models\Pedidos;
|
|
|
|
|
|
|
|
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\Pedidos\AlbaranEntity';
|
|
protected $allowedFields = [
|
|
'pedido_id',
|
|
'presupuesto_id',
|
|
'presupuesto_direccion_id',
|
|
'cliente_id',
|
|
'serie_id',
|
|
'numero_albaran',
|
|
'mostrar_precios',
|
|
'total',
|
|
'direccion_albaran',
|
|
'att_albaran',
|
|
'user_created_id',
|
|
'user_updated_id',
|
|
'created_at',
|
|
'updated_at',
|
|
'deleted_at',
|
|
];
|
|
|
|
protected $useSoftDeletes = true;
|
|
protected $useTimestamps = true;
|
|
protected $createdField = 'created_at';
|
|
protected $updatedField = 'updated_at';
|
|
protected $deletedField = 'deleted_at';
|
|
|
|
public function generarAlbaranes($pedido_id, $presupuestos_id, $user_id){
|
|
|
|
$model_presupuesto = model('App\Models\Presupuestos\PresupuestoModel');
|
|
$model_presupuesto_direcciones = model('App\Models\Presupuestos\PresupuestoDireccionesModel');
|
|
$model_series = model('App\Models\Configuracion\SeriesFacturasModel');
|
|
|
|
$presupuestos = $model_presupuesto->find($presupuestos_id);
|
|
|
|
$return_data = [];
|
|
|
|
foreach ($presupuestos as $presupuesto) {
|
|
|
|
$envios = $model_presupuesto_direcciones->where('presupuesto_id', $presupuesto->id)->findAll();
|
|
|
|
foreach($envios as $envio){
|
|
|
|
// se buscan los albaranes en este presupuesto con la misma direccion y con el mismo presupuesto_id en albaran
|
|
// en albaran linea para obtener la cantidad total enviada
|
|
$model_albaran = model('App\Models\Pedidos\AlbaranModel');
|
|
$model_albaran_linea = model('App\Models\Pedidos\AlbaranLineaModel');
|
|
$albaranes = $model_albaran->where('presupuesto_id', $presupuesto->id)
|
|
->where('presupuesto_direccion_id', $envio->id)->findAll();
|
|
// se suman las cantidades de los albaranes
|
|
$cantidad_enviada = 0;
|
|
foreach($albaranes as $albaran){
|
|
$lineas = $model_albaran_linea->where('albaran_id', $albaran->id)->findAll();
|
|
foreach($lineas as $linea){
|
|
$cantidad_enviada += $linea->cantidad;
|
|
}
|
|
}
|
|
|
|
if($cantidad_enviada >= intval($envio->cantidad)){
|
|
continue;
|
|
}
|
|
// calculo precio_unidad
|
|
$precio_unidad = $presupuesto->total_aceptado/$presupuesto->tirada;
|
|
|
|
$albaran_linea = [];
|
|
$albaran_linea = [
|
|
'titulo' => $presupuesto->titulo,
|
|
'isbn' => $presupuesto->isbn,
|
|
'ref_cliente' => $presupuesto->ref_cliente,
|
|
'cantidad' => intval($envio->cantidad)-$cantidad_enviada,
|
|
'cajas' => 1,
|
|
'ejemplares_por_caja' => intval($envio->cantidad)-$cantidad_enviada,
|
|
'precio_unidad' => $precio_unidad,
|
|
'total' => $precio_unidad * $envio->cantidad,
|
|
'user_created_id' => $user_id,
|
|
'user_updated_id' => $user_id,
|
|
];
|
|
|
|
|
|
$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);
|
|
|
|
$albaran = [
|
|
'pedido_id' => $pedido_id,
|
|
'presupuesto_id' => $presupuesto->id,
|
|
'presupuesto_direccion_id' => $envio->id,
|
|
'cliente_id' => $presupuesto->cliente_id,
|
|
'serie_id' => 11, // Serie de albaranes
|
|
'numero_albaran' => $numero_albaran,
|
|
'mostrar_precios' => 0,
|
|
'total' => $albaran_linea['total'],
|
|
'direccion_albaran' => $envio->direccion,
|
|
'att_albaran' => $envio->att,
|
|
'user_created_id' => $user_id,
|
|
'user_updated_id' => $user_id,
|
|
'fecha_albaran' => date('d/m/Y'),
|
|
];
|
|
|
|
$id_albaran = $this->insert($albaran);
|
|
$model_albaran_linea = model('App\Models\Pedidos\AlbaranLineaModel');
|
|
$albaran['id'] = $id_albaran;
|
|
$albaran_linea['albaran_id'] = $id_albaran;
|
|
$id_albaran_linea =$model_albaran_linea->insert($albaran_linea);
|
|
$albaran_linea['id'] = $id_albaran_linea;
|
|
|
|
array_push($return_data, ["albaran"=>$albaran, "lineas" =>[$albaran_linea]]);
|
|
}
|
|
}
|
|
|
|
return $return_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;
|
|
}
|
|
} |