mirror of
https://git.imnavajas.es/jjimenez/safekat.git
synced 2025-07-25 22:52:08 +00:00
113 lines
3.9 KiB
PHP
113 lines
3.9 KiB
PHP
<?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){
|
|
|
|
// 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' => $envio->cantidad,
|
|
'cajas' => 1,
|
|
'ejemplares_por_caja' => $envio->cantidad,
|
|
'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;
|
|
}
|
|
} |