mirror of
https://git.imnavajas.es/jjimenez/safekat.git
synced 2025-07-25 22:52:08 +00:00
añadido generar factura desde pedido
This commit is contained in:
@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Controllers\Pedidos;
|
||||
use App\Controllers\BaseController;
|
||||
use App\Controllers\Facturacion\Facturas;
|
||||
use App\Entities\Pedidos\PedidoEntity;
|
||||
use App\Models\Collection;
|
||||
use App\Models\Pedidos\PedidoModel;
|
||||
@ -366,6 +367,74 @@ class Pedido extends \App\Controllers\BaseResourceController
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public function addFactura(){
|
||||
|
||||
if($this->request->isAJAX()){
|
||||
|
||||
|
||||
$modelFactura = model('App\Models\Facturas\FacturaModel');
|
||||
$modelFacturaLinea = model('App\Models\Facturas\FacturaLineaModel');
|
||||
|
||||
$pedido_id = $this->request->getPost('pedido_id');
|
||||
$serie_id = $this->request->getPost('serie_id');
|
||||
|
||||
$datosFactura = $this->model->obtenerDatosForFactura($pedido_id);
|
||||
|
||||
if(count($datosFactura) == 0){
|
||||
return $this->respond(['status' => 'error', 'message' => 'Error obteniendo datos de factura']);
|
||||
}
|
||||
|
||||
$datosFactura = $datosFactura[0];
|
||||
|
||||
$modelFactura->insert([
|
||||
'cliente_id' => $datosFactura->cliente_id,
|
||||
'serie_id' => $serie_id,
|
||||
'estado' => 'borrador',
|
||||
'estado_pago' => 'pendiente',
|
||||
'fecha_factura_at' => date('Y-m-d'),
|
||||
'cliente_nombre' => $datosFactura->cliente_nombre,
|
||||
'cliente_cif' => $datosFactura->cliente_cif,
|
||||
'cliente_pais' => $datosFactura->cliente_pais,
|
||||
'cliente_address' => $datosFactura->cliente_direccion,
|
||||
'cliente_cp' => $datosFactura->cliente_cp,
|
||||
'cliente_cuidad' => $datosFactura->cliente_ciudad,
|
||||
'cliente_provincia' => $datosFactura->cliente_provincia,
|
||||
'user_created_id' => auth()->user()->id,
|
||||
'user_updated_id' => auth()->user()->id
|
||||
]);
|
||||
|
||||
$factura_id = $modelFactura->getInsertID();
|
||||
|
||||
if($factura_id){
|
||||
|
||||
$model_pedido_linea = model('\App\Models\Pedidos\PedidoLineaModel');
|
||||
$lineas = $model_pedido_linea->where('pedido_id', $pedido_id)->first();
|
||||
$facturas = new Facturas();
|
||||
$result = $facturas->addLineaPedidoImpresion($factura_id, $lineas->id);
|
||||
if($result['error'] == 0){
|
||||
// Se actualiza el precio total de la factura obtenido de la linea añadida
|
||||
$linea_added = $modelFacturaLinea->where('factura_id', $factura_id)->first();
|
||||
$modelFactura->set([
|
||||
'base' => $linea_added->base,
|
||||
'total' => $linea_added->total,
|
||||
'pendiente' => $linea_added->total,
|
||||
])->where('id', $factura_id)->update();
|
||||
return $this->respond(['status' => 'success', 'id' => $factura_id, 'message' => lang('Basic.global.success')]);
|
||||
}else{
|
||||
return $this->respond(['status' => 'error', 'message' => 'Error insertando lineas de factura']);
|
||||
}
|
||||
}
|
||||
|
||||
return $this->respond(['status' => 'error', 'message' => 'Error insertando factura']);
|
||||
|
||||
}else{
|
||||
return $this->failUnauthorized('Invalid request', 403);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
private function obtenerDatosFormulario(&$pedidoEntity){
|
||||
|
||||
$datos = $this->model->obtenerDatosForm($pedidoEntity->id);
|
||||
|
||||
Reference in New Issue
Block a user