mirror of
https://git.imnavajas.es/jjimenez/safekat.git
synced 2025-07-25 22:52:08 +00:00
trabajando en pagos (datepicker)
This commit is contained in:
@ -83,96 +83,100 @@ class FacturasLineas extends \App\Controllers\BaseResourceController
|
||||
|
||||
include(APPPATH . "ThirdParty/DatatablesEditor/DataTables.php");
|
||||
|
||||
|
||||
// Build our Editor instance and process the data coming from _POST
|
||||
$response = Editor::inst( $db, 'cliente_plantilla_precios_lineas' )
|
||||
$response = Editor::inst( $db, 'facturas_lineas' )
|
||||
->fields(
|
||||
Field::inst( 'plantilla_id' ),
|
||||
Field::inst( 'tipo' ),
|
||||
Field::inst( 'tipo_maquina' ),
|
||||
Field::inst( 'tipo_impresion' ),
|
||||
Field::inst( 'id' ),
|
||||
Field::inst( 'base' ),
|
||||
Field::inst( 'total_iva' ),
|
||||
Field::inst( 'total' ),
|
||||
Field::inst( 'cantidad' )
|
||||
->validator('Validate::numeric', array(
|
||||
'message' => lang('Facturas.validation.numerico'))
|
||||
)
|
||||
->validator('Validate::notEmpty', array(
|
||||
'message' => lang('Facturas.validation.requerido'))
|
||||
),
|
||||
Field::inst( 'descripcion' )
|
||||
->validator('Validate::notEmpty', array(
|
||||
'message' => lang('Facturas.validation.requerido'))
|
||||
),
|
||||
Field::inst( 'precio_unidad' )
|
||||
->getFormatter( 'Format::toDecimalChar')->setFormatter( 'Format::fromDecimalChar')
|
||||
->validator('Validate::numeric', array(
|
||||
"decimal" => ',',
|
||||
'message' => lang('Facturas.validation.decimal'))
|
||||
)
|
||||
->validator('Validate::notEmpty', array(
|
||||
'message' => lang('Facturas.validation.requerido'))
|
||||
),
|
||||
Field::inst( 'iva' )
|
||||
->validator('Validate::numeric', array(
|
||||
'message' => lang('Facturas.validation.numerico'))
|
||||
)
|
||||
->validator('Validate::notEmpty', array(
|
||||
'message' => lang('Facturas.validation.requerido'))
|
||||
),
|
||||
Field::inst( 'pedido_linea_impresion_id' )
|
||||
->setFormatter(function($val, $data, $opts) {
|
||||
return $val === '' ? null : $val;
|
||||
}),
|
||||
Field::inst( 'factura_id' ),
|
||||
Field::inst( 'user_updated_id' ),
|
||||
Field::inst( 'updated_at' ),
|
||||
Field::inst( 'tiempo_min' )
|
||||
->getFormatter( 'Format::toDecimalChar')->setFormatter( 'Format::fromDecimalChar')
|
||||
->validator( 'Validate::notEmpty',array(
|
||||
'message' => lang('ClientePrecios.validation.required'))
|
||||
)
|
||||
->validator('Validate::numeric', array(
|
||||
"decimal" => ',',
|
||||
'message' => lang('ClientePrecios.validation.decimal'))
|
||||
),
|
||||
|
||||
Field::inst( 'tiempo_max' )
|
||||
->getFormatter( 'Format::toDecimalChar')->setFormatter( 'Format::fromDecimalChar')
|
||||
->validator( 'Validate::notEmpty',array(
|
||||
'message' => lang('ClientePrecios.validation.required'))
|
||||
)
|
||||
->validator('Validate::numeric', array(
|
||||
"decimal" => ',',
|
||||
'message' => lang('ClientePrecios.validation.decimal'))
|
||||
),
|
||||
|
||||
Field::inst( 'precio_hora' )
|
||||
->getFormatter( 'Format::toDecimalChar')->setFormatter( 'Format::fromDecimalChar')
|
||||
->validator( 'Validate::notEmpty',array(
|
||||
'message' => lang('ClientePrecios.validation.required'))
|
||||
)
|
||||
->validator('Validate::numeric', array(
|
||||
"decimal" => ',',
|
||||
'message' => lang('ClientePrecios.validation.decimal'))
|
||||
),
|
||||
|
||||
Field::inst( 'margen' )
|
||||
->getFormatter( 'Format::toDecimalChar')->setFormatter( 'Format::fromDecimalChar')
|
||||
->validator( 'Validate::notEmpty',array(
|
||||
'message' => lang('ClientePrecios.validation.required'))
|
||||
)
|
||||
->validator('Validate::numeric', array(
|
||||
"decimal" => ',',
|
||||
'message' => lang('ClientePrecios.validation.decimal'))
|
||||
),
|
||||
|
||||
)
|
||||
->validator(function ($editor, $action, $data) {
|
||||
if ($action === Editor::ACTION_CREATE || $action === Editor::ACTION_EDIT) {
|
||||
foreach ($data['data'] as $pkey => $values) {
|
||||
// Si no se quiere borrar...
|
||||
if ($data['data'][$pkey]['is_deleted'] != 1) {
|
||||
$process_data['tiempo_min'] = $data['data'][$pkey]['tiempo_min'];
|
||||
$process_data['tiempo_max'] = $data['data'][$pkey]['tiempo_max'];
|
||||
$process_data['tipo'] = $data['data'][$pkey]['tipo'];
|
||||
$process_data['tipo_maquina'] = $data['data'][$pkey]['tipo_maquina'];
|
||||
$process_data['tipo_impresion'] = $data['data'][$pkey]['tipo_impresion'];
|
||||
|
||||
$response = $this->model->checkIntervals($process_data, $pkey, $data['data'][$pkey]['plantilla_id']);
|
||||
// No se pueden duplicar valores al crear o al editar
|
||||
if (!empty($response)) {
|
||||
return $response;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
->on('preCreate', function ($editor, &$values) {
|
||||
$session = session();
|
||||
$datetime = (new \CodeIgniter\I18n\Time("now"));
|
||||
$totales = $this->generate_totales(
|
||||
$values['factura_id'],
|
||||
$values['pedido_linea_impresion_id'],
|
||||
$values['precio_unidad'],
|
||||
$values['iva'],
|
||||
$values['cantidad'],
|
||||
$values['old_cantidad']);
|
||||
$editor
|
||||
->field('user_updated_id')
|
||||
->setValue($session->id_user);
|
||||
->setValue(auth()->user()->id);
|
||||
$editor
|
||||
->field('updated_at')
|
||||
->setValue($datetime->format('Y-m-d H:i:s'));
|
||||
->field('base')
|
||||
->setValue($totales['base']);
|
||||
$editor
|
||||
->field('total_iva')
|
||||
->setValue($totales['total_iva']);
|
||||
$editor
|
||||
->field('total')
|
||||
->setValue($totales['total']);
|
||||
$editor
|
||||
->field('user_updated_id')
|
||||
->setValue(auth()->user()->id);
|
||||
})
|
||||
->on('preEdit', function ($editor, &$values) {
|
||||
$session = session();
|
||||
$datetime = (new \CodeIgniter\I18n\Time("now"));
|
||||
->on('preEdit', function ($editor, $id, &$values) {
|
||||
$totales = $this->generate_totales(
|
||||
$values['factura_id'],
|
||||
$values['pedido_linea_impresion_id'],
|
||||
$values['precio_unidad'],
|
||||
$values['iva'],
|
||||
$values['cantidad'],
|
||||
$values['old_cantidad']);
|
||||
$editor
|
||||
->field('factura_id')
|
||||
->setValue($values['factura_id']);
|
||||
$editor
|
||||
->field('pedido_linea_impresion_id')
|
||||
->setValue(null);
|
||||
$editor
|
||||
->field('pedido_maquetacion_id')
|
||||
->setValue(null);
|
||||
$editor
|
||||
->field('user_updated_id')
|
||||
->setValue($session->id_user);
|
||||
->setValue(auth()->user()->id);
|
||||
$editor
|
||||
->field('updated_at')
|
||||
->setValue($datetime->format('Y-m-d H:i:s'));
|
||||
->field('base')
|
||||
->setValue($totales['base']);
|
||||
$editor
|
||||
->field('total_iva')
|
||||
->setValue($totales['total_iva']);
|
||||
$editor
|
||||
->field('total')
|
||||
->setValue($totales['total']);
|
||||
})
|
||||
->debug(true)
|
||||
->process($_POST)
|
||||
@ -189,4 +193,36 @@ class FacturasLineas extends \App\Controllers\BaseResourceController
|
||||
return $this->failUnauthorized('Invalid request', 403);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function updateTotalesFactura($factura_id = 0){
|
||||
if($factura_id == 0){
|
||||
return;
|
||||
}
|
||||
|
||||
$model = model('\App\Models\Facturas\FacturaModel');
|
||||
$model->updateTotales($factura_id);
|
||||
}
|
||||
|
||||
private function generate_totales($factura_id, $pedido_linea_id, $precio_unidad, $iva, $cantidad, $old_cantidad)
|
||||
{
|
||||
|
||||
// si es una linea que se refiere a pedido
|
||||
if ($pedido_linea_id != null && $factura_id != null) {
|
||||
// se actualiza la cantidad de la linea de pedido en la tabla pivote facturas_pedidos_lineas
|
||||
$this->model->updateFacturaPedidoLinea($factura_id, $pedido_linea_id, $old_cantidad, $cantidad);
|
||||
}
|
||||
|
||||
// se calcula y se actualiza el subtotal, total_iva y total
|
||||
// redondeando a 4 decimales el precio_unidad y a dos el resto
|
||||
$base = round($precio_unidad * $cantidad, 2);
|
||||
$total_iva = round($base * $iva / 100, 2);
|
||||
$total = round($base + $total_iva, 2);
|
||||
$values = [];
|
||||
$values['base'] = $base;
|
||||
$values['total_iva'] = $total_iva;
|
||||
$values['total'] = $total;
|
||||
|
||||
return $values;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user