DROP VIEW IF EXISTS doctos;
CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `doctos`  AS
SELECT `cheque`.`tienda_id` AS `tienda_id`,
       `cheque`.`moneda_id` AS `moneda_id`,
       `cheque`.`numero` AS `numero`,
       `cheque`.`cliente_id` AS `cliente_id`,
       `cliente`.`nombre` AS `cliente`,
       if((`cheque`.`new_date` is null),`cheque`.`original_date`,
          `cheque`.`new_date`) AS `fecha`,
       `cheque`.`alta_db` AS `alta_db`,
       `cheque`.`alta_por` AS `alta_por`,
       `cheque`.`quantity` AS `quantity`,
       `cheque`.`total_payments` AS `total_payments`,
       if(((`cheque`.`quantity` - `cheque`.`total_payments`) < 0),0,(`cheque`.`quantity` - `cheque`.`total_payments`)) AS `debe`,
       `cliente`.`saldo_cliente` AS `saldo`,
       `cheque`.`cuentaT_deliveredto_id` AS `cuentaT_deliveredto_id`,
       `cheque`.`origen_cuentat_id` AS `origen_cuentat_id`,
       `cheque`.`numero_sistema` AS `numero_sistema`,
       `cheque`.`remarks` AS `remarks`,
       `cheque`.`autorizado` AS `autorizado`,
       `cheque`.`autorizado_por` AS `autorizado_por`,
       `cheque`.`autorizado_el` AS `autorizado_el`,
       `cheque`.`borrado` AS `borrado`,
       `cheque`.`borrado_por` AS `borrado_por`,
       `cheque`.`borrado_el` AS `borrado_el`,
       `cheque`.`cheque_id` AS `doc_id`,
       `cheque`.`cheque_id` AS `doc_origen_id`,
       if(((`cheque`.`origen_cuentat_id` is null) or (`cheque`.`origen_cuentat_id` = '') or (`cheque`.`delivered_directo` = '1')),`cheque`.`tipo`,'CHEQUE') AS `origen`,
       `cheque`.`paid` AS `paid`,
       `cheque`.`delivered_directo` AS `delivered_directo`,
       `cheque`.`ultimo_movimiento` AS `ultimo_movimiento`,
       `cheque`.`desautorizado_el` AS `desautorizado_el`,
       `cheque`.`numero_contenedor` AS `numero_contenedor`,
       `cheque`.`lugar_de_entrada` AS `lugar_de_entrada`,
       `cheque`.`tipo_de_tela` AS `tipo_de_tela`, NULL AS `banco_cuenta_id`, NULL AS `deposit_fecha`, 0 AS `deposit_total`,
       `cheque`.`moneda_id` AS `doc_moneda`,
       `cheque`.`remarks_wot` AS `remarks_wot`,
       `cheque`.`cuentat_mov_id` AS `cuentat_mov_id`, 0 AS `acta_quantity`,
       `cheque`.`rechazado` AS `rechazado`,
       `cheque`.`categoria_id` AS `categoria_id`,
       `categoria`.`categoria` AS `categoria`,
       `cheque`.`tipo` AS `tipo`,
       `cheque`.`paid_when` AS `paid_when`,
       `cheque`.`ultimo_pago` AS `ultimo_pago`,
       0.00 AS `tipo_cambio`,
       `cliente`.`saldo_cliente_moneda_id` AS `saldo_cliente_moneda_id`,
       `cliente`.`saldo_cliente_usd` AS `saldo_cliente_usd`,
       `cheque`.`puede_borrar` AS `puede_borrar`,
       `cheque`.`bloqueado_el` AS `bloqueado_el`,
       `cheque`.`set_origen_cuentat` AS `set_origen_cuentat`,
       `cheque`.`puede_origen_cuentat` AS `puede_origen_cuentat`,
       `cheque`.`ultimo_cambio`,
       `cheque`.`puede_cambiar_tipo_cambio`,
       `cheque`.`conversion_ultimo_cambio`
FROM ((`cheque` left join `cliente` on((`cheque`.`cliente_id` = `cliente`.`cliente_id`))) left join `categoria` on((`cheque`.`categoria_id` = `categoria`.`categoria_id`)))

union all
select `cheque_log`.`tienda_id` AS `tienda_id`,
       `cheque_log`.`moneda_id` AS `moneda_id`,
       `cheque_log`.`numero` AS `numero`,
       `cheque_log`.`cliente_id` AS `cliente_id`,
       `cliente`.`nombre` AS `cliente`,
       if((`cheque_log`.`new_date` is null),`cheque_log`.`original_date`,
          `cheque_log`.`new_date`) AS `fecha`,
       `cheque_log`.`alta_db` AS `alta_db`,
       `cheque_log`.`alta_por` AS `alta_por`,
       `cheque_log`.`quantity` AS `quantity`,
       `cheque_log`.`total_payments` AS `total_payments`,
       if(((`cheque_log`.`quantity` - `cheque_log`.`total_payments`) < 0),0,(`cheque_log`.`quantity` - `cheque_log`.`total_payments`)) AS `debe`,
       `cliente`.`saldo_cliente` AS `saldo`,
       `cheque_log`.`cuentaT_deliveredto_id` AS `cuentaT_deliveredto_id`,
       `cheque_log`.`origen_cuentat_id` AS `origen_cuentat_id`,
       `cheque_log`.`numero_sistema` AS `numero_sistema`,
       `cheque_log`.`remarks` AS `remarks`,
       `cheque_log`.`autorizado` AS `autorizado`,
       `cheque_log`.`autorizado_por` AS `autorizado_por`,
       `cheque_log`.`autorizado_el` AS `autorizado_el`,
       `cheque_log`.`borrado` AS `borrado`,
       `cheque_log`.`borrado_por` AS `borrado_por`,
       `cheque_log`.`borrado_el` AS `borrado_el`,
       `cheque_log`.`cheque_id` AS `doc_id`,
       `cheque_log`.`cheque_id` AS `doc_origen_id`,
       if(((`cheque_log`.`origen_cuentat_id` is null) or (`cheque_log`.`origen_cuentat_id` = '') or (`cheque_log`.`delivered_directo` = '1')),`cheque_log`.`tipo`,'CHEQUE') AS `origen`,
       `cheque_log`.`paid` AS `paid`,
       `cheque_log`.`delivered_directo` AS `delivered_directo`,
       `cheque_log`.`ultimo_movimiento` AS `ultimo_movimiento`,
       `cheque_log`.`desautorizado_el` AS `desautorizado_el`,
       `cheque_log`.`numero_contenedor` AS `numero_contenedor`,
       `cheque_log`.`lugar_de_entrada` AS `lugar_de_entrada`,
       `cheque_log`.`tipo_de_tela` AS `tipo_de_tela`,
       NULL AS `banco_cuenta_id`,
       NULL AS `deposit_fecha`,0 AS `deposit_total`,
       `cheque_log`.`moneda_id` AS `doc_moneda`,
       `cheque_log`.`remarks_wot` AS `remarks_wot`,
       `cheque_log`.`cuentat_mov_id` AS `cuentat_mov_id`,0 AS `acta_quantity`,
       `cheque_log`.`rechazado` AS `rechazado`,0 AS `categoria_id`,'' AS `categoria`,
       `cheque_log`.`tipo` AS `tipo`,
       `cheque_log`.`paid_when` AS `paid_when`,
       NULL AS `ultimo_pago`,0.00 AS `tipo_cambio`,
       `cliente`.`saldo_cliente_moneda_id` AS `saldo_cliente_moneda_id`,
       `cliente`.`saldo_cliente_usd` AS `saldo_cliente_usd`,
       'No' AS `puede_borrar`,
       `cheque_log`.`alta_db` AS `bloqueado_el`,
       '' AS `set_origen_cuentat`,
       'No' AS `puede_origen_cuentat`,
       `cheque_log`.`ultimo_cambio`,
       'No' AS puede_cambiar_tipo_cambio,
       '' AS conversion_ultimo_cambio
FROM (`cheque_log` left join `cliente` on((`cheque_log`.`cliente_id` = `cliente`.`cliente_id`)))

union all
select `pagare`.`tienda_id` AS `tienda_id`,
       `pagare`.`moneda_id` AS `moneda_id`,
       `pagare`.`numero` AS `numero`,
       `pagare`.`cliente_id` AS `cliente_id`,
       `cliente`.`nombre` AS `cliente`,
       if((`pagare`.`new_date` is null),`pagare`.`original_date`,
          `pagare`.`new_date`) AS `fecha`,
       `pagare`.`alta_db` AS `alta_db`,
       `pagare`.`alta_por` AS `alta_por`,
       `pagare`.`quantity` AS `quantity`,
       `pagare`.`total_payments` AS `total_payments`,
       if(((`pagare`.`quantity` - `pagare`.`total_payments`) < 0),0,(`pagare`.`quantity` - `pagare`.`total_payments`)) AS `debe`,
       `cliente`.`saldo_cliente` AS `saldo`,
       `pagare`.`cuentaT_deliveredto_id` AS `cuentaT_deliveredto_id`,
       `pagare`.`origen_cuentat_id` AS `origen_cuentat_id`,
       `pagare`.`numero_sistema` AS `numero_sistema`,
       `pagare`.`remarks` AS `remarks`,
       `pagare`.`autorizado` AS `autorizado`,
       `pagare`.`autorizado_por` AS `autorizado_por`,
       `pagare`.`autorizado_el` AS `autorizado_el`,
       `pagare`.`borrado` AS `borrado`,
       `pagare`.`borrado_por` AS `borrado_por`,
       `pagare`.`borrado_el` AS `borrado_el`,
       `pagare`.`pagare_id` AS `doc_id`,
       `pagare`.`pagare_id` AS `doc_origen_id`,'PAGARE' AS `origen`,
       `pagare`.`paid` AS `paid`,
       `pagare`.`delivered_directo` AS `delivered_directo`,
       `pagare`.`ultimo_movimiento` AS `ultimo_movimiento`,
       `pagare`.`desautorizado_el` AS `desautorizado_el`,
       `pagare`.`numero_contenedor` AS `numero_contenedor`,
       `pagare`.`lugar_de_entrada` AS `lugar_de_entrada`,
       `pagare`.`tipo_de_tela` AS `tipo_de_tela`,
       NULL AS `banco_cuenta_id`,
       NULL AS `deposit_fecha`,0 AS `deposit_total`,
       `pagare`.`moneda_id` AS `doc_moneda`,
       `pagare`.`remarks_wot` AS `remarks_wot`,
       `pagare`.`cuentat_mov_id` AS `cuentat_mov_id`,0 AS `acta_quantity`,
       `pagare`.`rechazado` AS `rechazado`,
       `pagare`.`categoria_id` AS `categoria_id`,
       `categoria`.`categoria` AS `categoria`,
       `pagare`.`tipo` AS `tipo`,
       `pagare`.`paid_when` AS `paid_when`,
       `pagare`.`ultimo_pago` AS `ultimo_pago`,
       pagare.conversion_tipo_cambio AS `tipo_cambio`,
       `cliente`.`saldo_cliente_moneda_id` AS `saldo_cliente_moneda_id`,
       `cliente`.`saldo_cliente_usd` AS `saldo_cliente_usd`,
       `pagare`.`puede_borrar` AS `puede_borrar`,
       `pagare`.`bloqueado_el` AS `bloqueado_el`,
       `pagare`.`set_origen_cuentat` AS `set_origen_cuentat`,
       `pagare`.`puede_origen_cuentat` AS `puede_origen_cuentat`,
       `pagare`.`ultimo_cambio`,
       `pagare`.`puede_cambiar_tipo_cambio`,
       `pagare`.`conversion_ultimo_cambio`
FROM ((`pagare` left join `cliente` on((`pagare`.`cliente_id` = `cliente`.`cliente_id`))) left join `categoria` on((`pagare`.`categoria_id` = `categoria`.`categoria_id`)))

union all
select `pagare_log`.`tienda_id` AS `tienda_id`,
       `pagare_log`.`moneda_id` AS `moneda_id`,
       `pagare_log`.`numero` AS `numero`,
       `pagare_log`.`cliente_id` AS `cliente_id`,
       `cliente`.`nombre` AS `cliente`,
       if((`pagare_log`.`new_date` is null),`pagare_log`.`original_date`,
          `pagare_log`.`new_date`) AS `fecha`,
       `pagare_log`.`alta_db` AS `alta_db`,
       `pagare_log`.`alta_por` AS `alta_por`,
       `pagare_log`.`quantity` AS `quantity`,
       `pagare_log`.`total_payments` AS `total_payments`,
       if(((`pagare_log`.`quantity` - `pagare_log`.`total_payments`) < 0),0,(`pagare_log`.`quantity` - `pagare_log`.`total_payments`)) AS `debe`,
       `cliente`.`saldo_cliente` AS `saldo`,
       `pagare_log`.`cuentaT_deliveredto_id` AS `cuentaT_deliveredto_id`,
       `pagare_log`.`origen_cuentat_id` AS `origen_cuentat_id`,
       `pagare_log`.`numero_sistema` AS `numero_sistema`,
       `pagare_log`.`remarks` AS `remarks`,
       `pagare_log`.`autorizado` AS `autorizado`,
       `pagare_log`.`autorizado_por` AS `autorizado_por`,
       `pagare_log`.`autorizado_el` AS `autorizado_el`,
       `pagare_log`.`borrado` AS `borrado`,
       `pagare_log`.`borrado_por` AS `borrado_por`,
       `pagare_log`.`borrado_el` AS `borrado_el`,
       `pagare_log`.`pagare_id` AS `doc_id`,
       `pagare_log`.`pagare_id` AS `doc_origen_id`,'PAGARE' AS `origen`,
       `pagare_log`.`paid` AS `paid`,
       `pagare_log`.`delivered_directo` AS `delivered_directo`,
       `pagare_log`.`ultimo_movimiento` AS `ultimo_movimiento`,
       `pagare_log`.`desautorizado_el` AS `desautorizado_el`,
       `pagare_log`.`numero_contenedor` AS `numero_contenedor`,
       `pagare_log`.`lugar_de_entrada` AS `lugar_de_entrada`,
       `pagare_log`.`tipo_de_tela` AS `tipo_de_tela`,
       NULL AS `banco_cuenta_id`,
       NULL AS `deposit_fecha`,0 AS `deposit_total`,
       `pagare_log`.`moneda_id` AS `doc_moneda`,
       `pagare_log`.`remarks_wot` AS `remarks_wot`,
       `pagare_log`.`cuentat_mov_id` AS `cuentat_mov_id`,0 AS `acta_quantity`,
       `pagare_log`.`rechazado` AS `rechazado`,0 AS `categoria_id`,'' AS `categoria`,
       `pagare_log`.`tipo` AS `tipo`,
       `pagare_log`.`paid_when` AS `paid_when`,
       NULL AS `ultimo_pago`,0.00 AS `tipo_cambio`,
       `cliente`.`saldo_cliente_moneda_id` AS `saldo_cliente_moneda_id`,
       `cliente`.`saldo_cliente_usd` AS `saldo_cliente_usd`,
       'No' AS `puede_borrar`,
       `pagare_log`.`alta_db` AS `bloqueado_el`,
       '' AS `set_origen_cuentat`,
       'No' AS `puede_origen_cuentat`,
       `pagare_log`.`ultimo_cambio`,
       'No' AS puede_cambiar_tipo_cambio,
       '' AS conversion_ultimo_cambio
FROM (`pagare_log` left join `cliente` on((`pagare_log`.`cliente_id` = `cliente`.`cliente_id`)))

union all
select `vale`.`tienda_id` AS `tienda_id`,
       `vale`.`moneda_id` AS `moneda_id`,
       `vale`.`vale_number` AS `numero`,
       `vale`.`cliente_id` AS `cliente_id`,
       if((`vale`.`cliente_id` is null),convert(`vale`.`cliente` using utf8mb4),`cliente`.`nombre`) AS `cliente`,
       `vale`.`fecha` AS `fecha`,
       `vale`.`alta_db` AS `alta_db`,
       `vale`.`alta_por` AS `alta_por`,
       `vale`.`quantity` AS `quantity`,
       `vale`.`total_payments` AS `total_payments`,
       if(((`vale`.`quantity` - `vale`.`total_payments`) < 0),0,(`vale`.`quantity` - `vale`.`total_payments`)) AS `debe`,
       `cliente`.`saldo_cliente` AS `saldo`,
       `vale`.`dinero_en_cuentaT_id` AS `cuentaT_deliveredto_id`,
       `vale`.`dinero_en_cuentaT_id` AS `origen_cuentat_id`,
       `vale`.`numero_sistema` AS `numero_sistema`,
       `vale`.`remarks` AS `remarks`,
       `vale`.`autorizado` AS `autorizado`,
       `vale`.`autorizado_por` AS `autorizado_por`,
       `vale`.`autorizado_el` AS `autorizado_el`,
       `vale`.`borrado` AS `borrado`,
       `vale`.`borrado_por` AS `borrado_por`,
       `vale`.`borrado_el` AS `borrado_el`,
       `vale`.`vale_id` AS `doc_id`,
       `vale`.`vale_id` AS `doc_origen_id`,'VALE' AS `origen`,
       `vale`.`paid` AS `paid`,0 AS `delivered_directo`,
       `vale`.`ultimo_movimiento` AS `ultimo_movimiento`,
       `vale`.`desautorizado_el` AS `desautorizado_el`,
       `vale`.`numero_contenedor` AS `numero_contenedor`,
       `vale`.`lugar_de_entrada` AS `lugar_de_entrada`,
       `vale`.`tipo_de_tela` AS `tipo_de_tela`,
       `vale`.`banco_cuenta_id` AS `banco_cuenta_id`,
       `vale`.`deposit_fecha` AS `deposit_fecha`,
       `vale`.`deposit_total` AS `deposit_total`,
       `vale`.`moneda_id` AS `doc_moneda`,
       `vale`.`remarks_wot` AS `remarks_wot`,
       `vale`.`cuenta_t_mov_id` AS `cuentat_mov_id`,0 AS `acta_quantity`,
       `vale`.`rechazado` AS `rechazado`,
       `vale`.`categoria_id` AS `categoria_id`,
       `categoria`.`categoria` AS `categoria`,
       `vale`.`tipo` AS `tipo`,
       `vale`.`paid_when` AS `paid_when`,
       `vale`.`ultimo_pago` AS `ultimo_pago`,
       `vale`.`conversion_tipo_cambio` AS `tipo_cambio`,
       `cliente`.`saldo_cliente_moneda_id` AS `saldo_cliente_moneda_id`,
       `cliente`.`saldo_cliente_usd` AS `saldo_cliente_usd`,
       `vale`.`puede_borrar` AS `puede_borrar`,
       `vale`.`bloqueado_el` AS `bloqueado_el`,
       '' AS `set_origen_cuentat`,
       'No' AS `puede_origen_cuentat`,
       `vale`.`ultimo_cambio`,
       `vale`.`puede_cambiar_tipo_cambio`,
       `vale`.`conversion_ultimo_cambio`
FROM ((`vale` left join `cliente` on((`vale`.`cliente_id` = `cliente`.`cliente_id`))) left join `categoria` on((`vale`.`categoria_id` = `categoria`.`categoria_id`)))

union all
select `vale_log`.`tienda_id` AS `tienda_id`,
       `vale_log`.`moneda_id` AS `moneda_id`,
       `vale_log`.`vale_number` AS `numero`,
       `vale_log`.`cliente_id` AS `cliente_id`,
       if((`vale_log`.`cliente_id` is null),convert(`vale_log`.`cliente` using utf8mb4),`cliente`.`nombre`) AS `cliente`,
       `vale_log`.`fecha` AS `fecha`,
       `vale_log`.`alta_db` AS `alta_db`,
       `vale_log`.`alta_por` AS `alta_por`,
       `vale_log`.`quantity` AS `quantity`,
       `vale_log`.`total_payments` AS `total_payments`,
       if(((`vale_log`.`quantity` - `vale_log`.`total_payments`) < 0),0,(`vale_log`.`quantity` - `vale_log`.`total_payments`)) AS `debe`,
       `cliente`.`saldo_cliente` AS `saldo`,
       `vale_log`.`dinero_en_cuentaT_id` AS `cuentaT_deliveredto_id`,
       `vale_log`.`dinero_en_cuentaT_id` AS `origen_cuentat_id`,
       `vale_log`.`numero_sistema` AS `numero_sistema`,
       `vale_log`.`remarks` AS `remarks`,
       `vale_log`.`autorizado` AS `autorizado`,
       `vale_log`.`autorizado_por` AS `autorizado_por`,
       `vale_log`.`autorizado_el` AS `autorizado_el`,
       `vale_log`.`borrado` AS `borrado`,
       `vale_log`.`borrado_por` AS `borrado_por`,
       `vale_log`.`borrado_el` AS `borrado_el`,
       `vale_log`.`vale_id` AS `doc_id`,
       `vale_log`.`vale_id` AS `doc_origen_id`,'VALE' AS `origen`,
       `vale_log`.`paid` AS `paid`,0 AS `delivered_directo`,
       `vale_log`.`ultimo_movimiento` AS `ultimo_movimiento`,
       `vale_log`.`desautorizado_el` AS `desautorizado_el`,
       `vale_log`.`numero_contenedor` AS `numero_contenedor`,
       `vale_log`.`lugar_de_entrada` AS `lugar_de_entrada`,
       `vale_log`.`tipo_de_tela` AS `tipo_de_tela`,
       `vale_log`.`banco_cuenta_id` AS `banco_cuenta_id`,
       `vale_log`.`deposit_fecha` AS `deposit_fecha`,
       `vale_log`.`deposit_total` AS `deposit_total`,
       `vale_log`.`moneda_id` AS `doc_moneda`,
       `vale_log`.`remarks_wot` AS `remarks_wot`,
       `vale_log`.`cuenta_t_mov_id` AS `cuentat_mov_id`,0 AS `acta_quantity`,
       `vale_log`.`rechazado` AS `rechazado`,0 AS `categoria_id`,'' AS `categoria`,
       `vale_log`.`tipo` AS `tipo`,
       `vale_log`.`paid_when` AS `paid_when`,
       NULL AS `ultimo_pago`,
       `vale_log`.`conversion_tipo_cambio` AS `tipo_cambio`,
       `cliente`.`saldo_cliente_moneda_id` AS `saldo_cliente_moneda_id`,
       `cliente`.`saldo_cliente_usd` AS `saldo_cliente_usd`,
       'No' AS `puede_borrar`,
       `vale_log`.`alta_db` AS `bloqueado_el`,
       '' AS `set_origen_cuentat`,
       'No' AS `puede_origen_cuentat`,
       `vale_log`.`ultimo_cambio`,
       'No' AS puede_cambiar_tipo_cambio,
       '' AS conversion_ultimo_cambio
FROM (`vale_log` left join `cliente` on((`vale_log`.`cliente_id` = `cliente`.`cliente_id`)))

union all
select `compra`.`tienda_id` AS `tienda_id`,
       `compra`.`moneda_id` AS `moneda_id`,
       `compra`.`numero` AS `numero`,
       `compra`.`cliente_id` AS `cliente_id`,
       `cliente`.`nombre` AS `cliente`,
       if((`compra`.`new_date` is null),`compra`.`original_date`,
          `compra`.`new_date`) AS `fecha`,
       `compra`.`alta_db` AS `alta_db`,
       `compra`.`alta_por` AS `alta_por`,
       `compra`.`quantity` AS `quantity`,
       `compra`.`total_payments` AS `total_payments`,
       if(((`compra`.`quantity` - `compra`.`total_payments`) < 0),0,(`compra`.`quantity` - `compra`.`total_payments`)) AS `debe`,
       `cliente`.`saldo_cliente` AS `saldo`,
       `compra`.`cuentaT_deliveredto_id` AS `cuentaT_deliveredto_id`,
       `compra`.`origen_cuentat_id` AS `origen_cuentat_id`,
       `compra`.`numero_sistema` AS `numero_sistema`,
       `compra`.`remarks` AS `remarks`,
       `compra`.`autorizado` AS `autorizado`,
       `compra`.`autorizado_por` AS `autorizado_por`,
       `compra`.`autorizado_el` AS `autorizado_el`,
       `compra`.`borrado` AS `borrado`,
       `compra`.`borrado_por` AS `borrado_por`,
       `compra`.`borrado_el` AS `borrado_el`,
       `compra`.`compra_id` AS `doc_id`,
       `compra`.`compra_id` AS `doc_origen_id`,'COMPRA' AS `origen`,
       `compra`.`paid` AS `paid`,
       `compra`.`delivered_directo` AS `delivered_directo`,
       `compra`.`ultimo_movimiento` AS `ultimo_movimiento`,
       `compra`.`desautorizado_el` AS `desautorizado_el`,
       `compra`.`numero_contenedor` AS `numero_contenedor`,
       `compra`.`lugar_de_entrada` AS `lugar_de_entrada`,
       `compra`.`tipo_de_tela` AS `tipo_de_tela`,
       NULL AS `banco_cuenta_id`,
       NULL AS `deposit_fecha`,0 AS `deposit_total`,
       `compra`.`moneda_id` AS `doc_moneda`,
       `compra`.`remarks_wot` AS `remarks_wot`,
       `compra`.`cuentat_mov_id` AS `cuentat_mov_id`,0 AS `acta_quantity`,
       `compra`.`rechazado` AS `rechazado`,
       `compra`.`categoria_id` AS `categoria_id`,
       `categoria`.`categoria` AS `categoria`,
       `compra`.`tipo` AS `tipo`,
       `compra`.`paid_when` AS `paid_when`,
       `compra`.`ultimo_pago` AS `ultimo_pago`,0.00 AS `tipo_cambio`,
       `cliente`.`saldo_cliente_moneda_id` AS `saldo_cliente_moneda_id`,
       `cliente`.`saldo_cliente_usd` AS `saldo_cliente_usd`,
       `compra`.`puede_borrar` AS `puede_borrar`,
       `compra`.`bloqueado_el` AS `bloqueado_el`,
       `compra`.`set_origen_cuentat` AS `set_origen_cuentat`,
       `compra`.`puede_origen_cuentat` AS `puede_origen_cuentat`,
       `compra`.`ultimo_cambio`,
       'No' AS puede_cambiar_tipo_cambio,
       '' AS conversion_ultimo_cambio
FROM ((`compra` left join `cliente` on((`compra`.`cliente_id` = `cliente`.`cliente_id`))) left join `categoria` on((`compra`.`categoria_id` = `categoria`.`categoria_id`)))

union all
select `compra_log`.`tienda_id` AS `tienda_id`,
       `compra_log`.`moneda_id` AS `moneda_id`,
       `compra_log`.`numero` AS `numero`,
       `compra_log`.`cliente_id` AS `cliente_id`,
       `cliente`.`nombre` AS `cliente`,
       if((`compra_log`.`new_date` is null),`compra_log`.`original_date`,
          `compra_log`.`new_date`) AS `fecha`,
       `compra_log`.`alta_db` AS `alta_db`,
       `compra_log`.`alta_por` AS `alta_por`,
       `compra_log`.`quantity` AS `quantity`,
       `compra_log`.`total_payments` AS `total_payments`,
       if(((`compra_log`.`quantity` - `compra_log`.`total_payments`) < 0),0,(`compra_log`.`quantity` - `compra_log`.`total_payments`)) AS `debe`,
       `cliente`.`saldo_cliente` AS `saldo`,
       `compra_log`.`cuentaT_deliveredto_id` AS `cuentaT_deliveredto_id`,
       `compra_log`.`origen_cuentat_id` AS `origen_cuentat_id`,
       `compra_log`.`numero_sistema` AS `numero_sistema`,
       `compra_log`.`remarks` AS `remarks`,
       `compra_log`.`autorizado` AS `autorizado`,
       `compra_log`.`autorizado_por` AS `autorizado_por`,
       `compra_log`.`autorizado_el` AS `autorizado_el`,
       `compra_log`.`borrado` AS `borrado`,
       `compra_log`.`borrado_por` AS `borrado_por`,
       `compra_log`.`borrado_el` AS `borrado_el`,
       `compra_log`.`compra_id` AS `doc_id`,
       `compra_log`.`compra_id` AS `doc_origen_id`,'COMPRA' AS `origen`,
       `compra_log`.`paid` AS `paid`,
       `compra_log`.`delivered_directo` AS `delivered_directo`,
       `compra_log`.`ultimo_movimiento` AS `ultimo_movimiento`,
       `compra_log`.`desautorizado_el` AS `desautorizado_el`,
       `compra_log`.`numero_contenedor` AS `numero_contenedor`,
       `compra_log`.`lugar_de_entrada` AS `lugar_de_entrada`,
       `compra_log`.`tipo_de_tela` AS `tipo_de_tela`,
       NULL AS `banco_cuenta_id`,
       NULL AS `deposit_fecha`,0 AS `deposit_total`,
       `compra_log`.`moneda_id` AS `doc_moneda`,
       `compra_log`.`remarks_wot` AS `remarks_wot`,
       `compra_log`.`cuentat_mov_id` AS `cuentat_mov_id`,0 AS `acta_quantity`,
       `compra_log`.`rechazado` AS `rechazado`,0 AS `categoria_id`,'' AS `categoria`,
       `compra_log`.`tipo` AS `tipo`,
       `compra_log`.`paid_when` AS `paid_when`,
       NULL AS `ultimo_pago`,0.00 AS `tipo_cambio`,
       `cliente`.`saldo_cliente_moneda_id` AS `saldo_cliente_moneda_id`,
       `cliente`.`saldo_cliente_usd` AS `saldo_cliente_usd`,
       'No' AS `puede_borrar`,
       `compra_log`.`alta_db` AS `bloqueado_el`,
       '' AS `set_origen_cuentat`,
       'No' AS `puede_origen_cuentat`,
       `compra_log`.`ultimo_cambio`,
       'No' AS puede_cambiar_tipo_cambio,
       '' AS conversion_ultimo_cambio
FROM (`compra_log` left join `cliente` on((`compra_log`.`cliente_id` = `cliente`.`cliente_id`)))

union all
select `ch`.`tienda_id` AS `tienda_id`,
       `cp`.`moneda_id` AS `moneda_id`,
       `ch`.`numero` AS `doc_numero`,
       `ch`.`cliente_id` AS `cliente_id`,
       `cli`.`nombre` AS `cliente`,
       `cp`.`fecha` AS `fecha`,
       `cp`.`alta_db` AS `alta_db`,
       `cp`.`alta_por` AS `alta_por`,
       `ch`.`quantity` AS `quantity`,
       `ch`.`total_payments` AS `total_payments`,
       if(((`ch`.`quantity` - `ch`.`total_payments`) < 0),0,(`ch`.`quantity` - `ch`.`total_payments`)) AS `debe`,
       `cli`.`saldo_cliente` AS `saldo`,
       NULL AS `cuentaT_deliveredto_id`,
       `cp`.`cuentaT_id` AS `origen_cuentat_id`,
       `cp`.`numero_referencia` AS `numero_sistema`,
       `cp`.`comentario` AS `remarks`,
       `cp`.`autorizado` AS `autorizado`,
       `cp`.`autorizado_por` AS `autorizado_por`,
       `cp`.`autorizado_el` AS `autorizado_el`,
       'No' AS `borrado`,
       `cp`.`alta_por` AS `borrado_por`,
       '' AS `borrado_el`,
       `cp`.`cheque_pago_id` AS `doc_id`,
       `ch`.`cheque_id` AS `doc_origen_id`,
       if((`ch`.`tipo` <> ''),replace(if((`cp`.`banco_cuenta_id` > 0),'CHEQUE LINK',
                                         if((`cp`.`metodo_pago_id` = 14),'CHEQUE VALE','CHEQUE CASH')),'CHEQUE',`ch`.`tipo`),
          if((`cp`.`banco_cuenta_id` > 0),'CHEQUE LINK',
             if((`cp`.`metodo_pago_id` = 14),'CHEQUE VALE','CHEQUE CASH'))) AS `origen`,
       `ch`.`paid` AS `paid`,
       `ch`.`delivered_directo` AS `delivered_directo`,
       if((`cp`.`banco_cuenta_id` > 0),'A CUENTA T.C PACTADO','A CUENTA T.C PACTADO') AS `ultimo_movimiento`,
       NULL AS `desautorizado_el`,'' AS `numero_contenedor`,'' AS `lugar_de_entrada`,'' AS `tipo_de_tela`,
       `cp`.`banco_cuenta_id` AS `banco_cuenta_id`,
       `cp`.`deposit_fecha` AS `deposit_fecha`,
       `cp`.`deposit_total` AS `deposit_total`,
       `ch`.`moneda_id` AS `doc_moneda`,
       `ch`.`remarks_wot` AS `remarks_wot`,
       `cp`.`cuenta_t_mov_id` AS `cuentat_mov_id`,
       `cp`.`quantity_total` AS `acta_quantity`,
       `cp`.`rechazado` AS `rechazado`,0 AS `categoria_id`,'' AS `categoria`,
       `ch`.`tipo` AS `tipo`,
       NULL AS `paid_when`,
       NULL AS `ultimo_pago`,0.00 AS `tipo_cambio`,
       `cli`.`saldo_cliente_moneda_id` AS `saldo_cliente_moneda_id`,
       `cli`.`saldo_cliente_usd` AS `saldo_cliente_usd`,
       'No' AS `puede_borrar`,
       `cp`.`alta_db` AS `bloqueado_el`,
       '' AS `set_origen_cuentat`,
       'No' AS `puede_origen_cuentat`,
       `cp`.`ultimo_cambio`,
       'No' AS puede_cambiar_tipo_cambio,
       '' AS conversion_ultimo_cambio
FROM ((`cheque_pago` `cp` left join `cheque` `ch` on((`ch`.`cheque_id` = `cp`.`cheque_id`))) left join `cliente` `cli` on((`ch`.`cliente_id` = `cli`.`cliente_id`)))
where (ch.conversion_pactada = 1 AND cp.alta_db >= ch.conversion_ultimo_cambio)

union all
select `ch`.`tienda_id` AS `tienda_id`,
       `cp`.`moneda_id` AS `moneda_id`,
       `ch`.`numero` AS `doc_numero`,
       `ch`.`cliente_id` AS `cliente_id`,
       `cli`.`nombre` AS `cliente`,
       `cp`.`fecha` AS `fecha`,
       `cp`.`alta_db` AS `alta_db`,
       `cp`.`alta_por` AS `alta_por`,
       `ch`.`quantity` AS `quantity`,
       `ch`.`total_payments` AS `total_payments`,
       if(((`ch`.`quantity` - `ch`.`total_payments`) < 0),0,(`ch`.`quantity` - `ch`.`total_payments`)) AS `debe`,
       `cli`.`saldo_cliente` AS `saldo`,
       NULL AS `cuentaT_deliveredto_id`,
       `cp`.`cuentaT_id` AS `origen_cuentat_id`,
       `cp`.`numero_referencia` AS `numero_sistema`,
       `cp`.`comentario` AS `remarks`,
       `cp`.`autorizado` AS `autorizado`,
       `cp`.`autorizado_por` AS `autorizado_por`,
       `cp`.`autorizado_el` AS `autorizado_el`,'Si' AS `borrado`,
       `cp`.`alta_por` AS `borrado_por`,
       `cp`.`fecha_borrado` AS `borrado_el`,
       `cp`.`cheque_pago_log_id` AS `doc_id`,
       `ch`.`cheque_id` AS `doc_origen_id`,
       if((`ch`.`tipo` <> ''),replace(if((`cp`.`banco_cuenta_id` > 0),'CHEQUE LINK',
                                         if((`cp`.`metodo_pago_id` = 14),'CHEQUE VALE','CHEQUE CASH')),'CHEQUE',`ch`.`tipo`),
          if((`cp`.`banco_cuenta_id` > 0),'CHEQUE LINK',
             if((`cp`.`metodo_pago_id` = 14),'CHEQUE VALE','CHEQUE CASH'))) AS `origen`,
       `ch`.`paid` AS `paid`,
       `ch`.`delivered_directo` AS `delivered_directo`,
       if((`cp`.`banco_cuenta_id` > 0),'LINK BORRADO','A CUENTA BORRADO') AS `ultimo_movimiento`,
       `cp`.`desautorizado_el` AS `desautorizado_el`,'' AS `numero_contenedor`,'' AS `lugar_de_entrada`,'' AS `tipo_de_tela`,
       `cp`.`banco_cuenta_id` AS `banco_cuenta_id`,
       `cp`.`deposit_fecha` AS `deposit_fecha`,
       `cp`.`deposit_total` AS `deposit_total`,
       `ch`.`moneda_id` AS `doc_moneda`,
       `ch`.`remarks_wot` AS `remarks_wot`,
       `cp`.`cuenta_t_mov_id` AS `cuentat_mov_id`,
       `cp`.`quantity_total` AS `acta_quantity`,
       `cp`.`rechazado` AS `rechazado`,0 AS `categoria_id`,'' AS `categoria`,
       `ch`.`tipo` AS `tipo`,
       NULL AS `paid_when`,
       NULL AS `ultimo_pago`,0.00 AS `tipo_cambio`,
       `cli`.`saldo_cliente_moneda_id` AS `saldo_cliente_moneda_id`,
       `cli`.`saldo_cliente_usd` AS `saldo_cliente_usd`,
       'No' AS `puede_borrar`,
       `cp`.`alta_db` AS `bloqueado_el`,
       '' AS `set_origen_cuentat`,
       'No' AS `puede_origen_cuentat`,
       `cp`.`ultimo_cambio`,
       'No' AS puede_cambiar_tipo_cambio,
       '' AS conversion_ultimo_cambio
FROM ((`cheque_pago_log` `cp` left join `cheque` `ch` on((`ch`.`cheque_id` = `cp`.`cheque_id`))) left join `cliente` `cli` on((`ch`.`cliente_id` = `cli`.`cliente_id`)))

union all
select `ch`.`tienda_id` AS `tienda_id`,
       `cp`.`moneda_id` AS `moneda_id`,
       `ch`.`numero` AS `doc_numero`,
       `ch`.`cliente_id` AS `cliente_id`,
       `cli`.`nombre` AS `cliente`,
       `cp`.`fecha` AS `fecha`,
       `cp`.`alta_db` AS `alta_db`,
       `cp`.`alta_por` AS `alta_por`,
       `ch`.`quantity` AS `quantity`,
       `ch`.`total_payments` AS `total_payments`,
       if(((`ch`.`quantity` - `ch`.`total_payments`) < 0),0,(`ch`.`quantity` - `ch`.`total_payments`)) AS `debe`,
       `cli`.`saldo_cliente` AS `saldo`,
       NULL AS `cuentaT_deliveredto_id`,
       `cp`.`cuentaT_id` AS `origen_cuentat_id`,
       `cp`.`numero_referencia` AS `numero_sistema`,
       `cp`.`comentario` AS `remarks`,
       `cp`.`autorizado` AS `autorizado`,
       `cp`.`autorizado_por` AS `autorizado_por`,
       `cp`.`autorizado_el` AS `autorizado_el`,
       'No' AS `borrado`,
       `cp`.`alta_por` AS `borrado_por`,
       '' AS `borrado_el`,
       `cp`.`pagare_pago_id` AS `doc_id`,
       `ch`.`pagare_id` AS `doc_origen_id`,
       if((`cp`.`banco_cuenta_id` > 0),'PAGARE LINK',
          if((`cp`.`metodo_pago_id` = 14),'PAGARE VALE','PAGARE CASH')) AS `origen`,
       `ch`.`paid` AS `paid`,
       `ch`.`delivered_directo` AS `delivered_directo`,
       if((`cp`.`banco_cuenta_id` > 0),'A CUENTA T.C PACTADO','A CUENTA T.C PACTADO') AS `ultimo_movimiento`,
       NULL AS `desautorizado_el`,'' AS `numero_contenedor`,'' AS `lugar_de_entrada`,'' AS `tipo_de_tela`,
       `cp`.`banco_cuenta_id` AS `banco_cuenta_id`,
       `cp`.`deposit_fecha` AS `deposit_fecha`,
       `cp`.`deposit_total` AS `deposit_total`,
       `ch`.`moneda_id` AS `doc_moneda`,
       `ch`.`remarks_wot` AS `remarks_wot`,
       `cp`.`cuenta_t_mov_id` AS `cuentat_mov_id`,
       `cp`.`quantity_total` AS `acta_quantity`,
       `cp`.`rechazado` AS `rechazado`,0 AS `categoria_id`,'' AS `categoria`,
       `ch`.`tipo` AS `tipo`,
       NULL AS `paid_when`,
       NULL AS `ultimo_pago`,0.00 AS `tipo_cambio`,
       `cli`.`saldo_cliente_moneda_id` AS `saldo_cliente_moneda_id`,
       `cli`.`saldo_cliente_usd` AS `saldo_cliente_usd`,
       'No' AS `puede_borrar`,
       `cp`.`alta_db` AS `bloqueado_el`,
       '' AS `set_origen_cuentat`,
       'No' AS `puede_origen_cuentat`,
       `cp`.`ultimo_cambio`,
       'No' AS puede_cambiar_tipo_cambio,
       '' AS conversion_ultimo_cambio
FROM ((`pagare_pago` `cp` left join `pagare` `ch` on((`ch`.`pagare_id` = `cp`.`pagare_id`))) left join `cliente` `cli` on((`ch`.`cliente_id` = `cli`.`cliente_id`)))
where (ch.conversion_pactada = 1 AND cp.alta_db >= ch.conversion_ultimo_cambio)

union all
select `ch`.`tienda_id` AS `tienda_id`,
       `cp`.`moneda_id` AS `moneda_id`,
       `ch`.`numero` AS `doc_numero`,
       `ch`.`cliente_id` AS `cliente_id`,
       `cli`.`nombre` AS `cliente`,
       `cp`.`fecha` AS `fecha`,
       `cp`.`alta_db` AS `alta_db`,
       `cp`.`alta_por` AS `alta_por`,
       `ch`.`quantity` AS `quantity`,
       `ch`.`total_payments` AS `total_payments`,
       if(((`ch`.`quantity` - `ch`.`total_payments`) < 0),0,(`ch`.`quantity` - `ch`.`total_payments`)) AS `debe`,
       `cli`.`saldo_cliente` AS `saldo`,
       NULL AS `cuentaT_deliveredto_id`,
       `cp`.`cuentaT_id` AS `origen_cuentat_id`,
       `cp`.`numero_referencia` AS `numero_sistema`,
       `cp`.`comentario` AS `remarks`,
       `cp`.`autorizado` AS `autorizado`,
       `cp`.`autorizado_por` AS `autorizado_por`,
       `cp`.`autorizado_el` AS `autorizado_el`,'Si' AS `borrado`,
       `cp`.`alta_por` AS `borrado_por`,
       `cp`.`fecha_borrado` AS `borrado_el`,
       `cp`.`pagare_pago_log_id` AS `doc_id`,
       `ch`.`pagare_id` AS `doc_origen_id`,
       if((`cp`.`banco_cuenta_id` > 0),'PAGARE LINK',
          if((`cp`.`metodo_pago_id` = 14),'PAGARE VALE','PAGARE CASH')) AS `origen`,
       `ch`.`paid` AS `paid`,
       `ch`.`delivered_directo` AS `delivered_directo`,
       if((`cp`.`banco_cuenta_id` > 0),'LINK BORRADO','A CUENTA BORRADO') AS `ultimo_movimiento`,
       `cp`.`desautorizado_el` AS `desautorizado_el`,'' AS `numero_contenedor`,'' AS `lugar_de_entrada`,'' AS `tipo_de_tela`,
       `cp`.`banco_cuenta_id` AS `banco_cuenta_id`,
       `cp`.`deposit_fecha` AS `deposit_fecha`,
       `cp`.`deposit_total` AS `deposit_total`,
       `ch`.`moneda_id` AS `doc_moneda`,
       `ch`.`remarks_wot` AS `remarks_wot`,
       `cp`.`cuenta_t_mov_id` AS `cuentat_mov_id`,
       `cp`.`quantity_total` AS `acta_quantity`,
       `cp`.`rechazado` AS `rechazado`,0 AS `categoria_id`,'' AS `categoria`,
       `ch`.`tipo` AS `tipo`,
       NULL AS `paid_when`,
       NULL AS `ultimo_pago`,0.00 AS `tipo_cambio`,
       `cli`.`saldo_cliente_moneda_id` AS `saldo_cliente_moneda_id`,
       `cli`.`saldo_cliente_usd` AS `saldo_cliente_usd`,
       'No' AS `puede_borrar`,
       `cp`.`alta_db` AS `bloqueado_el`,
       '' AS `set_origen_cuentat`,
       'No' AS `puede_origen_cuentat`,
       `cp`.`ultimo_cambio`,
       'No' AS puede_cambiar_tipo_cambio,
       '' AS conversion_ultimo_cambio
FROM ((`pagare_pago_log` `cp` left join `pagare` `ch` on((`ch`.`pagare_id` = `cp`.`pagare_id`))) left join `cliente` `cli` on((`ch`.`cliente_id` = `cli`.`cliente_id`)))

union all
select `ch`.`tienda_id` AS `tienda_id`,
       `cp`.`moneda_id` AS `moneda_id`,
       `ch`.`vale_number` AS `doc_numero`,
       `ch`.`cliente_id` AS `cliente_id`,
       `cli`.`nombre` AS `cliente`,
       `cp`.`fecha` AS `fecha`,
       `cp`.`alta_db` AS `alta_db`,
       `cp`.`alta_por` AS `alta_por`,
       `ch`.`quantity` AS `quantity`,
       `ch`.`total_payments` AS `total_payments`,
       if(((`ch`.`quantity` - `ch`.`total_payments`) < 0),0,(`ch`.`quantity` - `ch`.`total_payments`)) AS `debe`,
       `cli`.`saldo_cliente` AS `saldo`,
       NULL AS `cuentaT_deliveredto_id`,
       `cp`.`cuentaT_id` AS `origen_cuentat_id`,
       `cp`.`numero_referencia` AS `numero_sistema`,
       `cp`.`comentario` AS `remarks`,
       `cp`.`autorizado` AS `autorizado`,
       `cp`.`autorizado_por` AS `autorizado_por`,
       `cp`.`autorizado_el` AS `autorizado_el`,
       'No' AS `borrado`,
       `cp`.`alta_por` AS `borrado_por`,
       '' AS `borrado_el`,
       `cp`.`vale_pago_id` AS `doc_id`,
       `ch`.`vale_id` AS `doc_origen_id`,
       if((`cp`.`banco_cuenta_id` > 0),'VALE PAGO LINK','VALE PAGO CASH') AS `origen`,
       `ch`.`paid` AS `paid`,0 AS `delivered_directo`,
       if((`cp`.`banco_cuenta_id` > 0),'LINK BORRADO','A CUENTA BORRADO') AS `ultimo_movimiento`,
       NULL AS `desautorizado_el`,'' AS `numero_contenedor`,'' AS `lugar_de_entrada`,'' AS `tipo_de_tela`,
       `cp`.`banco_cuenta_id` AS `banco_cuenta_id`,
       `cp`.`deposit_fecha` AS `deposit_fecha`,
       `cp`.`deposit_total` AS `deposit_total`,
       `ch`.`moneda_id` AS `doc_moneda`,
       `ch`.`remarks_wot` AS `remarks_wot`,
       `cp`.`cuenta_t_mov_id` AS `cuentat_mov_id`,
       `cp`.`quantity_total` AS `acta_quantity`,
       `cp`.`rechazado` AS `rechazado`,0 AS `categoria_id`,'' AS `categoria`,
       `ch`.`tipo` AS `tipo`,
       NULL AS `paid_when`,
       NULL AS `ultimo_pago`,0.00 AS `tipo_cambio`,
       `cli`.`saldo_cliente_moneda_id` AS `saldo_cliente_moneda_id`,
       `cli`.`saldo_cliente_usd` AS `saldo_cliente_usd`,
       'No' AS `puede_borrar`,
       `cp`.`alta_db` AS `bloqueado_el`,
       '' AS `set_origen_cuentat`,
       'No' AS `puede_origen_cuentat`,
       `cp`.`ultimo_cambio`,
       'No' AS puede_cambiar_tipo_cambio,
       '' AS conversion_ultimo_cambio
FROM ((`vale_pago` `cp` left join `vale` `ch` on((`ch`.`vale_id` = `cp`.`vale_id`))) left join `cliente` `cli` on((`ch`.`cliente_id` = `cli`.`cliente_id`)))

union all
select `ch`.`tienda_id` AS `tienda_id`,
       `cp`.`moneda_id` AS `moneda_id`,
       `ch`.`vale_number` AS `doc_numero`,
       `ch`.`cliente_id` AS `cliente_id`,
       `cli`.`nombre` AS `cliente`,
       `cp`.`fecha` AS `fecha`,
       `cp`.`alta_db` AS `alta_db`,
       `cp`.`alta_por` AS `alta_por`,
       `ch`.`quantity` AS `quantity`,
       `ch`.`total_payments` AS `total_payments`,
       if(((`ch`.`quantity` - `ch`.`total_payments`) < 0),0,(`ch`.`quantity` - `ch`.`total_payments`)) AS `debe`,
       `cli`.`saldo_cliente` AS `saldo`,
       NULL AS `cuentaT_deliveredto_id`,
       `cp`.`cuentaT_id` AS `origen_cuentat_id`,
       `cp`.`numero_referencia` AS `numero_sistema`,
       `cp`.`comentario` AS `remarks`,
       `cp`.`autorizado` AS `autorizado`,
       `cp`.`autorizado_por` AS `autorizado_por`,
       `cp`.`autorizado_el` AS `autorizado_el`,'Si' AS `borrado`,
       `cp`.`alta_por` AS `borrado_por`,
       `cp`.`fecha_borrado` AS `borrado_el`,
       `cp`.`vale_pago_log_id` AS `doc_id`,
       `ch`.`vale_id` AS `doc_origen_id`,
       if((`cp`.`banco_cuenta_id` > 0),'VALE PAGO LINK','VALE PAGO CASH') AS `origen`,
       `ch`.`paid` AS `paid`,0 AS `delivered_directo`,
       if((`cp`.`banco_cuenta_id` > 0),'LINK BORRADO','A CUENTA BORRADO') AS `ultimo_movimiento`,
       NULL AS `desautorizado_el`,'' AS `numero_contenedor`,'' AS `lugar_de_entrada`,'' AS `tipo_de_tela`,
       `cp`.`banco_cuenta_id` AS `banco_cuenta_id`,
       `cp`.`deposit_fecha` AS `deposit_fecha`,
       `cp`.`deposit_total` AS `deposit_total`,
       `ch`.`moneda_id` AS `doc_moneda`,
       `ch`.`remarks_wot` AS `remarks_wot`,
       `cp`.`cuenta_t_mov_id` AS `cuentat_mov_id`,
       `cp`.`quantity_total` AS `acta_quantity`,
       `cp`.`rechazado` AS `rechazado`,0 AS `categoria_id`,'' AS `categoria`,
       `ch`.`tipo` AS `tipo`,
       NULL AS `paid_when`,
       NULL AS `ultimo_pago`,0.00 AS `tipo_cambio`,
       `cli`.`saldo_cliente_moneda_id` AS `saldo_cliente_moneda_id`,
       `cli`.`saldo_cliente_usd` AS `saldo_cliente_usd`,
       'No' AS `puede_borrar`,
       `cp`.`alta_db` AS `bloqueado_el`,
       '' AS `set_origen_cuentat`,
       'No' AS `puede_origen_cuentat`,
       `cp`.`ultimo_cambio`,
       'No' AS puede_cambiar_tipo_cambio,
       '' AS conversion_ultimo_cambio
FROM ((`vale_pago_log` `cp` left join `vale` `ch` on((`ch`.`vale_id` = `cp`.`vale_id`))) left join `cliente` `cli` on((`ch`.`cliente_id` = `cli`.`cliente_id`)))

union all
select `ch`.`tienda_id` AS `tienda_id`,
       `cp`.`moneda_id` AS `moneda_id`,
       `ch`.`numero` AS `doc_numero`,
       `ch`.`cliente_id` AS `cliente_id`,
       `cli`.`nombre` AS `cliente`,
       `cp`.`fecha` AS `fecha`,
       `cp`.`alta_db` AS `alta_db`,
       `cp`.`alta_por` AS `alta_por`,
       `ch`.`quantity` AS `quantity`,
       `ch`.`total_payments` AS `total_payments`,
       if(((`ch`.`quantity` - `ch`.`total_payments`) < 0),0,(`ch`.`quantity` - `ch`.`total_payments`)) AS `debe`,
       `cli`.`saldo_cliente` AS `saldo`,
       NULL AS `cuentaT_deliveredto_id`,
       `cp`.`cuentaT_id` AS `origen_cuentat_id`,
       `cp`.`numero_referencia` AS `numero_sistema`,
       `cp`.`comentario` AS `remarks`,
       `cp`.`autorizado` AS `autorizado`,
       `cp`.`autorizado_por` AS `autorizado_por`,
       `cp`.`autorizado_el` AS `autorizado_el`,'Si' AS `borrado`,
       `cp`.`alta_por` AS `borrado_por`,
       `cp`.`fecha_borrado` AS `borrado_el`,
       `cp`.`compra_pago_log_id` AS `doc_id`,
       `ch`.`compra_id` AS `doc_origen_id`,'COMPRA CASH' AS `origen`,
       `ch`.`paid` AS `paid`,
       `ch`.`delivered_directo` AS `delivered_directo`,'A CUENTA BORRADO' AS `ultimo_movimiento`,
       `cp`.`desautorizado_el` AS `desautorizado_el`,'' AS `numero_contenedor`,'' AS `lugar_de_entrada`,'' AS `tipo_de_tela`,
       NULL AS `banco_cuenta_id`,
       `cp`.`deposit_fecha` AS `deposit_fecha`,
       `cp`.`deposit_total` AS `deposit_total`,
       `ch`.`moneda_id` AS `doc_moneda`,
       `ch`.`remarks_wot` AS `remarks_wot`,
       `cp`.`cuenta_t_mov_id` AS `cuentat_mov_id`,
       `cp`.`quantity_total` AS `acta_quantity`,
       `cp`.`rechazado` AS `rechazado`,0 AS `categoria_id`,'' AS `categoria`,
       `ch`.`tipo` AS `tipo`,
       NULL AS `paid_when`,
       NULL AS `ultimo_pago`,0.00 AS `tipo_cambio`,
       `cli`.`saldo_cliente_moneda_id` AS `saldo_cliente_moneda_id`,
       `cli`.`saldo_cliente_usd` AS `saldo_cliente_usd`,
       'No' AS `puede_borrar`,
       `cp`.`alta_db` AS `bloqueado_el`,
       '' AS `set_origen_cuentat`,
       'No' AS `puede_origen_cuentat`,
       `cp`.`ultimo_cambio`,
       'No' AS puede_cambiar_tipo_cambio,
       '' AS conversion_ultimo_cambio
FROM ((`compra_pago_log` `cp` left join `compra` `ch` on((`ch`.`compra_id` = `cp`.`compra_id`))) left join `cliente` `cli` on((`ch`.`cliente_id` = `cli`.`cliente_id`)))

union all
select `cuentat_a_banco`.`tienda_id` AS `tienda_id`,
       `cuentat_a_banco`.`moneda_id` AS `moneda_id`,
       `cuentat_a_banco`.`numero_referencia` AS `doc_numero`,'' AS `cliente_id`,
       `cuentat_a_banco`.`cliente` AS `cliente`,
       `cuentat_a_banco`.`fecha` AS `fecha`,
       `cuentat_a_banco`.`ultimo_cambio` AS `alta_db`,
       `cuentat_a_banco`.`ultimo_cambio_por` AS `alta_por`,
       `cuentat_a_banco`.`egreso` AS `quantity`,0 AS `total_payments`,0 AS `debe`,0 AS `saldo`,
       NULL AS `cuentaT_deliveredto_id`,
       if((`cuentat_a_banco`.`afecta_cuentat` = 'NO'),NULL,`cuentat_a_banco`.`cuentaT_id`) AS `origen_cuentat_id`,
       `cuentat_a_banco`.`numero_referencia` AS `numero_sistema`,
       `cuentat_a_banco`.`remarks` AS `remarks`,
       `cuentat_a_banco`.`autorizado` AS `autorizado`,
       `cuentat_a_banco`.`autorizado_por` AS `autorizado_por`,
       `cuentat_a_banco`.`autorizado_el` AS `autorizado_el`,'Si' AS `borrado`,
       `cuentat_a_banco`.`alta_por` AS `borrado_por`,
       `cuentat_a_banco`.`alta_db` AS `borrado_el`,
       `cuentat_a_banco`.`cuentat_a_banco_mov_id` AS `doc_id`,
       `cuentat_a_banco`.`cuentat_a_banco_mov_id` AS `doc_origen_id`,'CUENTA T' AS `origen`,0 AS `paid`,0 AS `delivered_directo`,
       if(((`cuentat_a_banco`.`deposit_total` > `cuentat_a_banco`.`egreso`) and (`cuentat_a_banco`.`ide_amount` = 0) and (`cuentat_a_banco`.`ide_percent` = 0) and (`cuentat_a_banco`.`tarjeta_amount` = 0) and (`cuentat_a_banco`.`tarjeta_percent` = 0)),'LINK PARCIAL BORRADO','LINK BORRADO') AS `ultimo_movimiento`,
       `cuentat_a_banco`.`desautorizado_el` AS `desautorizado_el`,'' AS `numero_contenedor`,'' AS `lugar_de_entrada`,'' AS `tipo_de_tela`,
       `cuentat_a_banco`.`banco_cuenta_id` AS `banco_cuenta_id`,
       `cuentat_a_banco`.`deposit_fecha` AS `deposit_fecha`,
       `cuentat_a_banco`.`deposit_total` AS `deposit_total`,
       `cuentat_a_banco`.`moneda_id` AS `doc_moneda`,
       `cuentat_a_banco`.`remarks` AS `remarks_wot`,
       `cuentat_a_banco`.`cuentaT_mov_id` AS `cuentat_mov_id`,0 AS `acta_quantity`,
       `cuentat_a_banco`.`rechazado` AS `rechazado`,0 AS `categoria_id`,'' AS `categoria`,'' AS `tipo`,
       NULL AS `paid_when`,
       NULL AS `ultimo_pago`,0.00 AS `tipo_cambio`,1 AS `saldo_cliente_moneda_id`,0 AS `saldo_cliente_usd`,
       `cuentat_a_banco`.`puede_borrar` AS `puede_borrar`,
       `cuentat_a_banco`.`ultimo_cambio` AS `bloqueado_el`,
       '' AS `set_origen_cuentat`,
       'No' AS `puede_origen_cuentat`,
       `cuentat_a_banco`.`ultimo_cambio`,
       'No' AS puede_cambiar_tipo_cambio,
       '' AS conversion_ultimo_cambio
FROM `cuentat_a_banco` where ((`cuentat_a_banco`.`activo` = 'No') and (`cuentat_a_banco`.`factura_directa` = 'NO'))  ;
