mirror of
https://git.imnavajas.es/jjimenez/safekat.git
synced 2025-07-25 22:52:08 +00:00
credito
This commit is contained in:
@ -309,9 +309,26 @@ class ClienteModel extends \App\Models\BaseModel
|
||||
/*
|
||||
TO-DO: Implementar la lógica de negocio para el crédito disponible
|
||||
*/
|
||||
public function creditoDisponible($cliente_id)
|
||||
public function creditoDisponible($cliente_id, $total_pedido = 0)
|
||||
{
|
||||
return true;
|
||||
$builder = $this->db
|
||||
->table($this->table . " t1")
|
||||
->select(
|
||||
"t1.limite_credito AS limite_credito"
|
||||
)
|
||||
->where("t1.is_deleted", 0)
|
||||
->where("t1.id", $cliente_id);
|
||||
$limite = $builder->get()->getResultObject();
|
||||
|
||||
if($limite){
|
||||
|
||||
$pendiente = $this->getPendienteCobro($cliente_id);
|
||||
$credito_disponible = floatval($limite[0]->limite_credito) - $pendiente[0] - $pendiente[1] - floatval($total_pedido);
|
||||
if($credito_disponible < 0)
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public function getClienteDataFacturas($cliente_id)
|
||||
@ -402,4 +419,83 @@ class ClienteModel extends \App\Models\BaseModel
|
||||
->groupEnd()->get()->getResultObject();
|
||||
|
||||
}
|
||||
|
||||
public function getPendienteCobro($cliente_id = -1)
|
||||
{
|
||||
|
||||
$pendiente_old = $this->getTotalPendienteOldERP($cliente_id);
|
||||
|
||||
// Subconsulta para verificar la existencia en facturas_lineas
|
||||
$subquery_facturas = "(SELECT 1
|
||||
FROM facturas_lineas fl
|
||||
JOIN facturas f2 ON fl.factura_id = f2.id
|
||||
WHERE (fl.pedido_linea_impresion_id = p.id OR fl.pedido_maquetacion_id = p.id)
|
||||
AND f2.estado = 'validada')";
|
||||
|
||||
// Subconsulta para calcular el total de pedidos pendientes
|
||||
$subquery_total_pedidos = $this->db->table('pedidos p')
|
||||
->select('SUM(p.total_precio)', false)
|
||||
->join('pedidos_linea pl', 'p.id = pl.pedido_id', 'left')
|
||||
->join('presupuestos pr', 'pl.presupuesto_id = pr.id', 'left')
|
||||
->where('pr.cliente_id', $cliente_id)
|
||||
->whereIn('p.estado', ['produccion', 'finalizado', 'enviado'])
|
||||
->where("NOT EXISTS $subquery_facturas", null, false) // Implementación manual de NOT EXISTS
|
||||
->getCompiledSelect();
|
||||
|
||||
// Construcción de la consulta principal
|
||||
$builder = $this->db->table('(SELECT DISTINCT cliente_id FROM presupuestos UNION SELECT DISTINCT cliente_id FROM facturas) c');
|
||||
$builder->select('c.cliente_id');
|
||||
$builder->select('COALESCE(SUM(f.pendiente), 0) AS total_facturas');
|
||||
$builder->select("COALESCE(($subquery_total_pedidos), 0) AS total_pedidos", false);
|
||||
$builder->select("COALESCE(SUM(f.pendiente), 0) + COALESCE(($subquery_total_pedidos), 0) AS total_pendiente", false);
|
||||
$builder->join('facturas f', 'c.cliente_id = f.cliente_id AND f.deleted_at IS NULL AND f.estado = "validada"', 'left');
|
||||
$builder->where('c.cliente_id', $cliente_id);
|
||||
$builder->groupBy('c.cliente_id');
|
||||
|
||||
$query = $builder->get();
|
||||
$valor = $query->getRow();
|
||||
|
||||
if ($valor) {
|
||||
return [round(floatval($valor->total_pendiente), 2), round(floatval($pendiente_old), 2)];
|
||||
} else {
|
||||
return [0, round(floatval($pendiente_old), 2)];
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private function getTotalPendienteOldERP($customer_id = -1)
|
||||
{
|
||||
|
||||
$db = \Config\Database::connect('old_erp'); // Conectar a olderp
|
||||
$builder = $db->table('safekat.facturas f');
|
||||
$builder->select('f.customer_id');
|
||||
$builder->select('SUM(f.pendiente) + COALESCE((
|
||||
SELECT SUM(pl.total_raw)
|
||||
FROM safekat.pedido_libro pl
|
||||
WHERE pl.factura_id IS NULL
|
||||
AND pl.estado IN ("aceptado", "finalizado", "validado")
|
||||
AND pl.total_raw IS NOT NULL
|
||||
AND pl.total_raw != 0
|
||||
AND pl.inc_rei IS NULL
|
||||
AND pl.customer_id = f.customer_id
|
||||
), 0) AS total_pendiente');
|
||||
$builder->where('f.deleted_at IS NULL');
|
||||
$builder->where('f.estado', 'open');
|
||||
$builder->where('f.customer_id', $customer_id);
|
||||
$builder->groupBy('f.customer_id');
|
||||
|
||||
$query = $builder->get();
|
||||
$valor = $query->getRow();
|
||||
/*
|
||||
$builder = $db->table('vista_importe_pendiente_cliente t1')
|
||||
->select('*')
|
||||
->where('t1.customer_id', $customer_id);
|
||||
$query = $builder->get();
|
||||
$valor = $query->getRow();*/
|
||||
if ($valor) {
|
||||
return $valor->total_pendiente;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -9,8 +9,8 @@ use CodeIgniter\Model;
|
||||
class OrdenTrabajoDate extends Model
|
||||
{
|
||||
protected $table = 'orden_trabajo_dates';
|
||||
protected $primaryKey = 'orden_trabajo_id';
|
||||
protected $useAutoIncrement = false;
|
||||
protected $primaryKey = 'id';
|
||||
protected $useAutoIncrement = true;
|
||||
protected $returnType = OrdenTrabajoDateEntity::class;
|
||||
protected $useSoftDeletes = true;
|
||||
protected $protectFields = true;
|
||||
@ -62,8 +62,8 @@ class OrdenTrabajoDate extends Model
|
||||
protected $allowCallbacks = true;
|
||||
protected $beforeInsert = [];
|
||||
protected $afterInsert = ["updateOrdenTrabajoUser"];
|
||||
protected $beforeUpdate = ["updateOrdenTrabajoUser"];
|
||||
protected $afterUpdate = [];
|
||||
protected $beforeUpdate = [];
|
||||
protected $afterUpdate = ["updateOrdenTrabajoUser"];
|
||||
protected $beforeFind = [];
|
||||
protected $afterFind = [];
|
||||
protected $beforeDelete = [];
|
||||
@ -106,7 +106,7 @@ class OrdenTrabajoDate extends Model
|
||||
$mapping = [
|
||||
"fecha_encuadernado_at" => "encuadernacion_user_id",
|
||||
// "fecha_externo_at" => "null",
|
||||
"fecha_impresion_at" => "null",
|
||||
"fecha_impresion_at" => null,
|
||||
"pendiente_ferro_at" => "pendiente_ferro_user_id",
|
||||
"ferro_en_cliente_at" => "ferro_en_cliente_user_id",
|
||||
"ferro_ok_at" => "ferro_ok_user_id",
|
||||
@ -124,9 +124,12 @@ class OrdenTrabajoDate extends Model
|
||||
foreach ($data as $key => $value) {
|
||||
if(isset($mapping[$key])){
|
||||
if($value){
|
||||
$otUser->update($data["orden_trabajo_id"],[$mapping[$key] => $auth_user_id]);
|
||||
$otUser->where('orden_trabajo_id',$data['orden_trabajo_id'])
|
||||
->set([$mapping[$key] => $auth_user_id])
|
||||
->update();
|
||||
}
|
||||
}
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
|
||||
@ -9,8 +9,8 @@ use CodeIgniter\Model;
|
||||
class OrdenTrabajoUser extends Model
|
||||
{
|
||||
protected $table = 'orden_trabajo_users';
|
||||
protected $primaryKey = 'orden_trabajo_id';
|
||||
protected $useAutoIncrement = false;
|
||||
protected $primaryKey = 'id';
|
||||
protected $useAutoIncrement = true;
|
||||
protected $returnType = OrdenTrabajoUserEntity::class;
|
||||
protected $useSoftDeletes = true;
|
||||
protected $protectFields = true;
|
||||
@ -46,31 +46,7 @@ class OrdenTrabajoUser extends Model
|
||||
protected bool $updateOnlyChanged = true;
|
||||
|
||||
protected array $casts = [
|
||||
"orden_trabajo_id" => "integer",
|
||||
"user_created_id" => "?integer",
|
||||
"user_update_id" => "?integer",
|
||||
"inaplazable_revised_change_user_id" => "?integer",
|
||||
"ferro_disponible_hecho_user_id" => "?integer",
|
||||
"ferro_disponible_ok_user_id" => "?integer",
|
||||
"ferro_entregado_user_id" => "?integer",
|
||||
"pendiente_ferro_user_id" => "?integer",
|
||||
"ferro_en_cliente_user_id" => "?integer",
|
||||
"ferro_ok_user_id" => "?integer",
|
||||
"interior_bn_user_id" => "?integer",
|
||||
"interior_color_user_id" => "?integer",
|
||||
"preparacion_interior_user_id" => "?integer",
|
||||
"cubierta_user_id" => "?integer",
|
||||
"plastificado_user_id" => "?integer",
|
||||
"encuadernacion_user_id" => "?integer",
|
||||
"corte_user_id" => "?integer",
|
||||
"embalaje_user_id" => "?integer",
|
||||
"entrada_manipulado_user_id" => "?integer",
|
||||
"pre_formato_user_id" => "?integer",
|
||||
"pre_lomo_user_id" => "?integer",
|
||||
"pre_solapa_user_id" => "?integer",
|
||||
"pre_codbarras_user_id" => "?integer",
|
||||
"pre_imposicion_user_id" => "?integer",
|
||||
"pre_imprimir_user_id" => "?integer"
|
||||
|
||||
];
|
||||
protected array $castHandlers = [];
|
||||
|
||||
|
||||
Reference in New Issue
Block a user