text: -- These queries are equivalent to those generated by sqlalchemy. 
  -- I've put them here for debugging purposes.
  -- ATTENTION: THEY MAY BE OUTDATED!
  
  -- To obtain them, do the following
  -- $ camelot_manage console
  -- >>> from m2000.view import *
  -- >>> v = indicadores()
  -- >>> print v
  
  -- CREATE VIEW indicadores AS
  SELECT 
    pre.beneficiaria_id, 
    pre.comentarios, 
    pre.barrio, 
    pre.beneficiaria, 
    pre.nro_credito, 
    pre.fecha_entrega, 
    pre.fecha_inicio, 
    pre.fecha_cancelacion, 
    pre.saldo_anterior, 
    pre.capital, 
    pre.tasa_interes, 
    pre.cartera, 
    pre.monto_aporte, 
    pre.deuda_total, 
    pre.cuotas, 
    pre.cuota_calculada, 
    pre.cuotas_pagadas, 
    pre.cuotas_pagadas_porcent, 
    pre.cuotas_teorico, pre.cuotas_teorico / pre.cuotas AS cuotas_teorico_porcent, 
    pre.cuotas_teorico - pre.cuotas_pagadas AS diferencia_cuotas, 
    pre.saldo, 
    pre.monto_pagado, 
    (pre.deuda_total * pre.cuotas_teorico) / pre.cuotas AS monto_teorico, 
    (pre.deuda_total * pre.cuotas_teorico) / pre.cuotas - pre.monto_pagado AS diferencia_monto, 
    estado_credito.descripcion AS estado 
  FROM ( 
    SELECT 
      beneficiaria.id AS beneficiaria_id, 
      beneficiaria.comentarios AS comentarios, 
      barrio.nombre AS barrio, 
      concat(beneficiaria.nombre, ' ', beneficiaria.apellido) AS beneficiaria, 
      credito.nro_credito AS nro_credito, 
      credito.fecha_entrega AS fecha_entrega, 
      adddate(credito.fecha_entrega, 14) AS fecha_inicio, 
      adddate(credito.fecha_entrega, credito.cuotas * 7) AS fecha_cancelacion, 
      credito.saldo_anterior AS saldo_anterior, 
      credito.deuda_total / (1 + credito.tasa_interes) AS capital, 
      credito.tasa_interes AS tasa_interes, 
      cartera.nombre AS cartera, 
      (credito.deuda_total * credito.tasa_interes) / (1 + credito.tasa_interes) AS monto_aporte, 
      credito.deuda_total AS deuda_total, credito.cuotas AS cuotas, 
      credito.deuda_total / credito.cuotas AS cuota_calculada, 
      (total_pagos_x_credito_activo_a_fecha.total_pagos * credito.cuotas) / credito.deuda_total AS cuotas_pagadas, 
      total_pagos_x_credito_activo_a_fecha.total_pagos / credito.deuda_total AS cuotas_pagadas_porcent, 
      if(datediff(parametro.fecha, adddate(credito.fecha_entrega, 14)) / 7 > credito.cuotas, 
                  credito.cuotas, 
                  datediff(parametro.fecha, adddate(credito.fecha_entrega, 14)) / 7) AS cuotas_teorico, 
      credito.deuda_total - total_pagos_x_credito_activo_a_fecha.total_pagos AS saldo, 
      total_pagos_x_credito_activo_a_fecha.total_pagos AS monto_pagado 
    FROM 
      total_pagos_x_credito_activo_a_fecha, 
      parametro, 
      credito 
      INNER JOIN beneficiaria ON beneficiaria.id = credito.beneficiaria_id 
      INNER JOIN barrio ON barrio.id = beneficiaria.barrio_id 
      INNER JOIN cartera ON cartera.id = credito.cartera_id 
    WHERE 
      total_pagos_x_credito_activo_a_fecha.credito_id = credito.id 
      AND beneficiaria.activa = true 
      AND credito.deuda_total != 0
      AND credito.fecha_entrega <= parametro.fecha 
      AND (credito.fecha_finalizacion > parametro.fecha 
           OR credito.fecha_finalizacion IS NULL)
    ) AS pre, 
    estado_credito 
  WHERE 
    pre.cuotas_teorico - pre.cuotas_pagadas > estado_credito.cuotas_adeudadas_min 
    AND pre.cuotas_teorico - pre.cuotas_pagadas <= estado_credito.cuotas_adeudadas_max;
  
  -- CREATE VIEW max_fecha AS
  SELECT max(parametro.fecha) AS max_1 FROM parametro;
  
  -- CREATE VIEW min_fecha AS
  SELECT min(parametro.fecha) AS min_1 FROM parametro;
  
  -- CREATE VIEW cheques_entregados AS
  SELECT 
    credito.id AS credito_id, 
    concat(beneficiaria.nombre, ' ', beneficiaria.apellido) AS beneficiaria, 
    barrio.nombre AS barrio, 
    cartera.nombre AS cartera, 
    credito.nro_credito, 
    credito.fecha_entrega, 
    credito.prestamo, 
    credito.monto_cheque 
  FROM 
    credito 
    INNER JOIN cartera ON cartera.id = credito.cartera_id 
    INNER JOIN beneficiaria ON beneficiaria.id = credito.beneficiaria_id 
    INNER JOIN barrio ON barrio.id = beneficiaria.barrio_id;
  
  -- CREATE VIEW credito_pagos AS
  SELECT 
    credito.id AS credito_id, 
    ifnull(pago.fecha, credito.fecha_entrega) AS fecha_pago_o_entrega, 
    ifnull(pago.monto, 0) AS monto, credito.fecha_finalizacion AS fecha_finalizacion 
  FROM 
    credito 
    LEFT OUTER JOIN pago ON credito.id = pago.credito_id;
  
  -- CREATE VIEW total_pagos_x_credito AS
  SELECT 
    credito_pagos.credito_id, 
    sum(credito_pagos.monto) AS monto 
  FROM 
    credito_pagos
  GROUP BY 
    credito_pagos.credito_id;
  
  -- CREATE VIEW creditos_activos AS
  SELECT 
    credito.id AS credito_id, 
    concat(beneficiaria.nombre, ' ', beneficiaria.apellido) AS beneficiaria, 
    beneficiaria.comentarios, 
    barrio.nombre AS barrio, 
    credito.nro_credito, 
    credito.prestamo, 
    credito.fecha_entrega, 
    credito.deuda_total - total_pagos_x_credito.monto AS saldo 
  FROM
    total_pagos_x_credito, 
    credito 
    INNER JOIN beneficiaria ON beneficiaria.id = credito.beneficiaria_id 
    INNER JOIN barrio ON barrio.id = beneficiaria.barrio_id 
  WHERE 
    credito.fecha_finalizacion IS NULL 
    AND total_pagos_x_credito.credito_id = credito.id;
  
  -- CREATE VIEW total_pagos_x_credito_activo_a_fecha AS
  SELECT 
    parametro.fecha, 
    credito_pagos.credito_id, 
    sum(credito_pagos.monto) AS total_pagos 
  FROM 
    parametro, 
    credito_pagos
  WHERE 
    parametro.fecha >= credito_pagos.fecha_pago_o_entrega 
    AND (credito_pagos.fecha_finalizacion > parametro.fecha 
         OR credito_pagos.fecha_finalizacion IS NULL) 
  GROUP BY 
    credito_pagos.credito_id, 
    parametro.fecha;
  
  -- CREATE VIEW creditos_finalizados_sin_saldar AS
  SELECT 
    credito.id AS credito_id, 
    concat(beneficiaria.nombre, ' ', beneficiaria.apellido) AS beneficiaria, 
    beneficiaria.comentarios, 
    barrio.nombre AS barrio, 
    credito.nro_credito, 
    credito.fecha_finalizacion, 
    credito.fecha_entrega, 
    credito.prestamo, 
    credito.deuda_total, 
    credito.deuda_total - total_pagos_x_credito.monto AS saldo 
  FROM 
    total_pagos_x_credito, 
    credito 
    INNER JOIN beneficiaria ON beneficiaria.id = credito.beneficiaria_id 
    INNER JOIN barrio ON barrio.id = beneficiaria.barrio_id 
  WHERE 
    credito.fecha_finalizacion IS NOT NULL 
    AND credito.deuda_total - total_pagos_x_credito.monto >= 1
    AND total_pagos_x_credito.credito_id = credito.id;
  
  -- CREATE VIEW perdida_x_incobrable AS
  SELECT 
    credito.id AS credito_id, 
    beneficiaria.comentarios, 
    concat(beneficiaria.nombre, ' ', beneficiaria.apellido) AS beneficiaria, 
    beneficiaria.fecha_baja, 
    barrio.nombre AS barrio, 
    credito.nro_credito, 
    credito.fecha_finalizacion, 
    credito.comentarios AS comentarios_baja, 
    credito.fecha_entrega, 
    credito.prestamo, 
    credito.deuda_total, 
    credito.deuda_total - total_pagos_x_credito.monto AS saldo 
  FROM 
    total_pagos_x_credito, 
    credito 
    INNER JOIN beneficiaria ON beneficiaria.id = credito.beneficiaria_id 
    INNER JOIN barrio ON barrio.id = beneficiaria.barrio_id 
  WHERE 
    credito.fecha_finalizacion IS NOT NULL 
    AND credito.comentarios IS NOT NULL 
    AND beneficiaria.activa = false 
    AND credito.deuda_total - total_pagos_x_credito.monto > 0
    AND total_pagos_x_credito.credito_id = credito.id;
  
  -- CREATE VIEW recaudacion_x_cartera AS
  SELECT 
    cartera.nombre AS cartera, 
    credito.tasa_interes, 
    sum(pago.monto) AS recaudacion, 
    barrio.nombre AS barrio, 
    cartera.id AS cartera_id, 
    barrio.id AS barrio_id 
  FROM 
    credito 
    INNER JOIN pago ON credito.id = pago.credito_id 
    INNER JOIN beneficiaria ON beneficiaria.id = credito.beneficiaria_id 
    INNER JOIN barrio ON barrio.id = beneficiaria.barrio_id 
    INNER JOIN cartera ON cartera.id = credito.cartera_id 
  WHERE 
    pago.fecha >= (SELECT min(parametro.fecha) AS min_1 FROM parametro) 
    AND pago.fecha <= (SELECT max(parametro.fecha) AS max_1 FROM parametro) 
  GROUP BY 
    cartera.id, 
    credito.tasa_interes, 
    barrio.id;
  
  -- CREATE VIEW recaudacion_x_barrio AS
  SELECT 
    yearweek(pago.fecha, 0) AS semana, 
    barrio.id AS barrio_id, 
    barrio.nombre AS barrio_nombre,
    sum(pago.monto) AS recaudacion 
  FROM 
    credito 
    INNER JOIN pago ON credito.id = pago.credito_id 
    INNER JOIN beneficiaria ON beneficiaria.id = credito.beneficiaria_id 
    INNER JOIN barrio ON barrio.id = beneficiaria.barrio_id, fecha 
  WHERE 
    pago.fecha >= (SELECT min(parametro.fecha) AS min_1 FROM parametro) 
    AND pago.fecha <= (SELECT max(parametro.fecha) AS max_1 FROM parametro) 
  GROUP BY 
    yearweek(pago.fecha, 0), 
    barrio.id;
  
  -- CREATE VIEW recaudacion AS
  SELECT 
    yearweek(pago.fecha, 0) AS semana, 
    cartera.id AS cartera_id, 
    credito.tasa_interes, 
    sum(pago.monto) AS recaudacion
  FROM 
    credito 
    INNER JOIN pago ON credito.id = pago.credito_id 
    INNER JOIN cartera ON cartera.id = credito.cartera_id 
  WHERE 
    pago.fecha >= (SELECT min(parametro.fecha) AS min_1 FROM parametro) 
    AND pago.fecha <= (SELECT max(parametro.fecha) AS max_1 FROM parametro) 
  GROUP BY 
    yearweek(pago.fecha, 0), 
    cartera.id, 
    credito.tasa_interes;
  
  -- CREATE VIEW recaudacion_real_total AS
  SELECT 
    concat(mid(recaudacion_x_barrio.semana, 1, 4), ',', mid(recaudacion_x_barrio.semana, 5, 2)) AS semana, 
    cartera.nombre AS cartera, 
    recaudacion.tasa_interes, 
    recaudacion.recaudacion, 
    cartera.id 
  FROM 
    (SELECT * FROM recaudacion) AS recaudacion, 
    cartera 
  WHERE 
    cartera.id = recaudacion.cartera_id;
  
  -- CREATE VIEW recaudacion_real_x_barrio AS
  SELECT 
    concat(mid(recaudacion_x_barrio.semana, 1, 4), ',', mid(recaudacion_x_barrio.semana, 5, 2)) AS semana, 
    recaudacion_x_barrio.barrio_nombre AS barrio, 
    recaudacion_x_barrio.recaudacion, 
    recaudacion_x_barrio.barrio_id 
  FROM 
    recaudacion_x_barrio;
  
  -- CREATE VIEW recaudacion_potencial_total AS
  SELECT 
    concat(mid(rec_pot.semana, 1, 4), '.', mid(rec_pot.semana, 5, 2)) AS semana, 
    rec_pot.recaudacion, 
    rec_pot.recaudacion_potencial, 
    rec_pot.recaudacion / rec_pot.recaudacion_potencial AS porcentaje 
  FROM (
    SELECT 
      rec_total_real.semana AS semana, 
      rec_total_real.recaudacion AS recaudacion, 
      sum(credito.deuda_total / credito.cuotas) AS recaudacion_potencial 
    FROM (
      SELECT 
        recaudacion.semana AS semana, 
        sum(recaudacion.recaudacion) AS recaudacion 
      FROM 
        recaudacion 
      GROUP BY 
        recaudacion.semana
      ) AS rec_total_real, 
      credito 
    WHERE 
      yearweek(adddate(credito.fecha_entrega, 14), 0) <= rec_total_real.semana 
      AND (yearweek(credito.fecha_finalizacion, 0) >= rec_total_real.semana 
           OR credito.fecha_finalizacion IS NULL) 
    GROUP BY 
      rec_total_real.semana
    ) AS rec_pot;
  
  -- CREATE VIEW potencial_a_cobrar_x_barrio AS
  SELECT 
    tparametro.semana, 
    beneficiaria.barrio_id, 
    barrio.nombre AS barrio_nombre, 
    sum(credito.deuda_total / credito.cuotas) AS recaudacion_potencial 
  FROM (
    SELECT DISTINCT 
      yearweek(parametro.fecha, 0) AS semana 
    FROM 
      parametro
    ) AS tparametro, 
    credito 
    INNER JOIN beneficiaria ON beneficiaria.id = credito.beneficiaria_id 
    INNER JOIN barrio ON barrio.id = beneficiaria.barrio_id 
  WHERE 
    yearweek(adddate(credito.fecha_entrega, 14), 0) <= tparametro.semana 
    AND (yearweek(credito.fecha_finalizacion, 0) >= tparametro.semana 
         OR credito.fecha_finalizacion IS NULL) 
  GROUP BY 
    tparametro.semana, 
    beneficiaria.barrio_id;
  
  -- CREATE VIEW recaudacion_potencial_total_x_barrio AS
  SELECT 
    concat(mid(potencial_a_cobrar_x_barrio.semana, 1, 4), '.', mid(potencial_a_cobrar_x_barrio.semana, 5, 2)) AS semana, 
    potencial_a_cobrar_x_barrio.barrio_nombre AS barrio, 
    ifnull(rec_real.recaudacion, %s) AS recaudacion, 
    potencial_a_cobrar_x_barrio.recaudacion_potencial, 
    ifnull(rec_real.recaudacion, %s) / potencial_a_cobrar_x_barrio.recaudacion_potencial AS porcentaje 
  FROM 
    potencial_a_cobrar_x_barrio 
    LEFT OUTER JOIN (
      SELECT 
        yearweek(pago.fecha) AS semana, 
        beneficiaria.barrio_id AS barrio_id, 
        barrio.nombre AS barrio_nombre, 
        sum(pago.monto) AS recaudacion 
      FROM 
        pago 
        INNER JOIN credito ON credito.id = pago.credito_id 
        INNER JOIN beneficiaria ON beneficiaria.id = credito.beneficiaria_id 
        INNER JOIN barrio ON barrio.id = beneficiaria.barrio_id 
      WHERE 
        pago.fecha >= (SELECT min(parametro.fecha) AS min_1 FROM parametro) 
        AND pago.fecha <= (SELECT max(parametro.fecha) AS max_1 FROM parametro) 
      GROUP BY 
        yearweek(pago.fecha), 
        beneficiaria.barrio_id
    ) AS rec_real ON potencial_a_cobrar_x_barrio.semana = rec_real.semana AND potencial_a_cobrar_x_barrio.barrio_id = rec_real.barrio_id;
  
  