-- ------------------------------------------------------------------------------------------------------------------------------------------------------
update tb_a_mproducts
  set nombre = REPLACE(nombre, '\t', '')
	where nombre like '%\t%';

-- ------------------------------------------------------------------------------------------------------------------------------------------------------
ALTER TABLE `tb_f_mcustomers` 
    ADD COLUMN `moneda` VARCHAR(5) CHARACTER SET 'utf8mb4' COLLATE 'utf8mb4_spanish_ci' NULL DEFAULT '' AFTER `factory_monto`;

-- ------------------------------------------------------------------------------------------------------------------------------------------------------
ALTER TABLE `tb_f_oinvoice` 
    ADD COLUMN `vendedor_nombre` VARCHAR(120) CHARACTER SET 'utf8mb4' COLLATE 'utf8mb4_spanish_ci' NULL DEFAULT '' AFTER `vendedor`,
    CHANGE COLUMN `vendedor` `vendedor` VARCHAR(10) CHARACTER SET 'utf8mb4' COLLATE 'utf8mb4_spanish_ci' NULL DEFAULT '' ;

-- ------------------------------------------------------------------------------------------------------------------------------------------------------
DROP PROCEDURE if exists `sp_lista_f_mcliente`;
DROP PROCEDURE if exists `sp_lista_f_mcliente_factory`;
DROP PROCEDURE if exists `sp_lista_f_mcliente_invoice`;
DROP PROCEDURE if exists `sp_cbusqueda_f_sales_orders`;

-- ------------------------------------------------------------------------------------------------------------------------------------------------------
CREATE TABLE `tb_f_oquote` (
  `codemp` VARCHAR(2) CHARACTER SET 'utf8mb4' COLLATE 'utf8mb4_spanish_ci' NOT NULL DEFAULT '',
  `sucursal` VARCHAR(3) CHARACTER SET 'utf8mb4' COLLATE 'utf8mb4_spanish_ci' NOT NULL DEFAULT '',
  `numero` VARCHAR(15) CHARACTER SET 'utf8mb4' COLLATE 'utf8mb4_spanish_ci' NOT NULL DEFAULT '',
  `fecha` DATE NULL DEFAULT '1900-01-01',
  `fecha_u` DATE NULL DEFAULT '1900-01-01',
  `cliente` VARCHAR(10) CHARACTER SET 'utf8mb4' COLLATE 'utf8mb4_spanish_ci' NULL DEFAULT '',
  `ship_to` INT NULL DEFAULT '0',
  `envio_prioridad` VARCHAR(10) CHARACTER SET 'utf8mb4' COLLATE 'utf8mb4_spanish_ci' NULL DEFAULT '',
  `nota` VARCHAR(500) CHARACTER SET 'utf8mb4' COLLATE 'utf8mb4_spanish_ci' NULL DEFAULT '',
  `numero_item` smallint NULL DEFAULT '0',
  `total_precio` DECIMAL(10,2) NULL DEFAULT '0.00',
  `moneda` VARCHAR(5) CHARACTER SET 'utf8mb4' COLLATE 'utf8mb4_spanish_ci' NULL DEFAULT '',
  `vendedor` VARCHAR(10) CHARACTER SET 'utf8mb4' COLLATE 'utf8mb4_spanish_ci' NULL DEFAULT '',
  `usuario` VARCHAR(10) CHARACTER SET 'utf8mb4' COLLATE 'utf8mb4_spanish_ci' NULL DEFAULT '',
  `estado` SMALLINT NULL DEFAULT '0',
  PRIMARY KEY (`codemp`, `sucursal`, `numero`))
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8mb4
COLLATE = utf8mb4_spanish_ci;

CREATE TABLE `tb_f_oquote_dproducts` (
  `codemp` VARCHAR(2) CHARACTER SET 'utf8mb4' COLLATE 'utf8mb4_spanish_ci' NULL DEFAULT '',
  `sucursal` VARCHAR(3) CHARACTER SET 'utf8mb4' COLLATE 'utf8mb4_spanish_ci' NULL DEFAULT '',
  `numero` VARCHAR(15) CHARACTER SET 'utf8mb4' COLLATE 'utf8mb4_spanish_ci' NULL DEFAULT '',
  `producto` VARCHAR(100) CHARACTER SET 'utf8mb4' COLLATE 'utf8mb4_spanish_ci' NULL DEFAULT '',
  `nombre` VARCHAR(300) CHARACTER SET 'utf8mb4' COLLATE 'utf8mb4_spanish_ci' NULL DEFAULT '',
  `precio_venta` DECIMAL(10,2) NULL DEFAULT '0.00',
  `cantidad_ord` DECIMAL(10,2) NULL DEFAULT '0.00',
  `total_ord` DECIMAL(10,2) NULL DEFAULT '0.00',
  `nota` VARCHAR(1000) CHARACTER SET 'utf8mb4' COLLATE 'utf8mb4_spanish_ci' NULL DEFAULT '',
  `unidad_medida` VARCHAR(15) CHARACTER SET 'utf8mb4' COLLATE 'utf8mb4_spanish_ci' NULL DEFAULT '',
  `award` SMALLINT NULL DEFAULT '0',
  `manual` SMALLINT NULL DEFAULT '0',
  `item` INT NOT NULL AUTO_INCREMENT,
  PRIMARY KEY (`item`))
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8mb4
COLLATE = utf8mb4_spanish_ci;

-- ------------------------------------------------------------------------------------------------------------------------------------------------------
ALTER TABLE `tb_f_osales_dproducts` 
    ADD COLUMN `award` SMALLINT NULL DEFAULT '0' AFTER `tipo_sync`,
    CHANGE COLUMN `unidad_medida` `unidad_medida` VARCHAR(15) CHARACTER SET 'utf8mb4' COLLATE 'utf8mb4_spanish_ci' NULL DEFAULT '' AFTER `nombre`,
    CHANGE COLUMN `costo_unitario` `costo_unitario` DECIMAL(10,2) NULL DEFAULT '0.00' AFTER `descuento_p`,
    CHANGE COLUMN `precio_venta` `precio_venta` DECIMAL(10,2) NULL DEFAULT '0.00' AFTER `precio_lista`,
    CHANGE COLUMN `impuesto_monto` `impuesto_monto` DECIMAL(10,2) NULL DEFAULT '0.00' AFTER `cantidad_env`,
    CHANGE COLUMN `fecha_req` `fecha_req` DATE NULL DEFAULT '1900-01-01' AFTER `impuesto_monto`,
    CHANGE COLUMN `fecha_env` `fecha_env` DATE NULL DEFAULT '1900-01-01' AFTER `fecha_req`;

-- ------------------------------------------------------------------------------------------------------------------------------------------------------
ALTER TABLE `tb_f_oinvoice_dproducts` 
    CHANGE COLUMN `unidad_medida` `unidad_medida` VARCHAR(15) CHARACTER SET 'utf8mb4' COLLATE 'utf8mb4_spanish_ci' NULL DEFAULT '' AFTER `nombre`,
    CHANGE COLUMN `costo_unitario` `costo_unitario` DECIMAL(10,2) NULL DEFAULT '0.00' AFTER `unidad_medida`,
    CHANGE COLUMN `precio_lista` `precio_lista` DECIMAL(10,2) NULL DEFAULT '0.00' AFTER `costo_unitario`,
    CHANGE COLUMN `precio_venta` `precio_venta` DECIMAL(10,2) NULL DEFAULT '0.00' AFTER `precio_lista`,
    CHANGE COLUMN `cantidad_env` `cantidad_env` DECIMAL(10,2) NULL DEFAULT '0.00' AFTER `cantidad_ord`,
    CHANGE COLUMN `cantidad_fac` `cantidad_fac` DECIMAL(10,2) NULL DEFAULT '0.00' AFTER `cantidad_res`,
    CHANGE COLUMN `impuesto_monto` `impuesto_monto` DECIMAL(10,2) NULL DEFAULT '0.00' AFTER `cantidad_fac`,
    CHANGE COLUMN `fecha_req` `fecha_req` DATE NULL DEFAULT '1900-01-01' AFTER `impuesto_monto`,
    CHANGE COLUMN `fecha_env` `fecha_env` DATE NULL DEFAULT '1900-01-01' AFTER `fecha_ent`;

-- ------------------------------------------------------------------------------------------------------------------------------------------------------
ALTER TABLE `tb_f_osales_orders` 
    ADD COLUMN `presupuesto` VARCHAR(15) NULL DEFAULT '' AFTER `referencia`;

ALTER TABLE `tb_f_osales_orders` 
    ADD COLUMN `usuario` VARCHAR(10) CHARACTER SET 'utf8mb4' COLLATE 'utf8mb4_spanish_ci' NULL DEFAULT '' AFTER `usuario_sistema`;

-- ------------------------------------------------------------------------------------------------------------------------------------------------------
ALTER TABLE `tb_f_oquote` 
    ADD COLUMN `bill_name` VARCHAR(100) CHARACTER SET 'utf8mb4' COLLATE 'utf8mb4_spanish_ci' NULL DEFAULT '' AFTER `ship_to`,
    ADD COLUMN `envio_nombre` VARCHAR(100) CHARACTER SET 'utf8mb4' COLLATE 'utf8mb4_spanish_ci' NULL DEFAULT '' AFTER `bill_name`,
    ADD COLUMN `envio_pais` VARCHAR(70) CHARACTER SET 'utf8mb4' COLLATE 'utf8mb4_spanish_ci' NULL DEFAULT '' AFTER `envio_nombre`,
    ADD COLUMN `envio_estado_ciudad` VARCHAR(70) CHARACTER SET 'utf8mb4' COLLATE 'utf8mb4_spanish_ci' NULL DEFAULT '' AFTER `envio_pais`,
    ADD COLUMN `envio_ciudad` VARCHAR(70) CHARACTER SET 'utf8mb4' COLLATE 'utf8mb4_spanish_ci' NULL DEFAULT '' AFTER `envio_estado_ciudad`,
    ADD COLUMN `envio_zcode` VARCHAR(10) CHARACTER SET 'utf8mb4' COLLATE 'utf8mb4_spanish_ci' NULL DEFAULT '' AFTER `envio_ciudad`,
    ADD COLUMN `envio_direccion` VARCHAR(250) CHARACTER SET 'utf8mb4' COLLATE 'utf8mb4_spanish_ci' NULL DEFAULT '' AFTER `envio_zcode`;

ALTER TABLE `tb_f_oquote_dproducts` 
    ADD COLUMN `nuevo_usado` VARCHAR(15) CHARACTER SET 'utf8mb4' COLLATE 'utf8mb4_spanish_ci' NULL DEFAULT '' AFTER `nota`;

-- ------------------------------------------------------------------------------------------------------------------------------------------------------
DROP PROCEDURE if exists `sp_cbusqueda_f_obutget`;
DROP PROCEDURE if exists `sp_form_f_obudget_u`;
DROP PROCEDURE if exists `sp_form_f_obudget_c`;
DROP PROCEDURE if exists `sp_form_f_obudget_dproducts_c`;
DROP PROCEDURE if exists `sp_form_f_obudget_dproducts_u`;
DROP PROCEDURE if exists `sp_form_f_osales_customer_moneda`;
DROP PROCEDURE if exists `sp_form_a_mproducts_u`;

-- ------------------------------------------------------------------------------------------------------------------------------------------------------
ALTER TABLE `tb_a_mproducts_dinventory` 
    DROP COLUMN `stock_existencia`,
    ADD COLUMN `cantidad_act` DECIMAL(10,2) NULL DEFAULT '0.00' AFTER `almacen`,
    ADD COLUMN `cantidad_ord` DECIMAL(10,2) NULL DEFAULT '0.00' AFTER `cantidad_act`,
    ADD COLUMN `cantidad_env` DECIMAL(10,2) NULL DEFAULT '0.00' AFTER `cantidad_ord`;

ALTER TABLE `tb_a_mproducts_dinventory` 
    CHANGE COLUMN `articulo` `producto` VARCHAR(100) CHARACTER SET 'utf8mb4' COLLATE 'utf8mb4_spanish_ci' NULL DEFAULT '' ;

ALTER TABLE `tb_a_mproducts_dinventory` 
    ADD COLUMN `stock_min` DECIMAL(10,2) NULL DEFAULT '0.00' AFTER `almacen`,
    ADD COLUMN `stock_max` DECIMAL(10,2) NULL DEFAULT '0.00' AFTER `stock_min`,
    ADD COLUMN `stock_reo` DECIMAL(10,2) NULL DEFAULT '0.00' AFTER `stock_max`, 
    RENAME TO  `tb_a_ostock` ;

ALTER TABLE `tb_a_ostock`ALTER TABLE `tb_a_ostock` 
    CHANGE COLUMN `almacen` `almacen` VARCHAR(10) CHARACTER SET 'utf8mb4' COLLATE 'utf8mb4_spanish_ci' NULL DEFAULT '' AFTER `sucursal`;
    CHANGE COLUMN `cantidad_act` `stock_disponible` DECIMAL(10,2) NULL DEFAULT '0.00' ,
    CHANGE COLUMN `cantidad_ord` `stock_reservado` DECIMAL(10,2) NULL DEFAULT '0.00' ,
    CHANGE COLUMN `cantidad_env` `stock_enmano` DECIMAL(10,2) NULL DEFAULT '0.00' ,
    ADD COLUMN `stock_existencia` DECIMAL(10,2) NULL DEFAULT '0.00' AFTER `stock_enmano`;

ALTER TABLE `tb_a_mwarehouse` 
    ADD COLUMN `localizacion` VARCHAR(120) NULL DEFAULT '' AFTER `nombre`;

-- ------------------------------------------------------------------------------------------------------------------------------------------------------
DELETE FROM `db_usupplyit`.`tb_a_mwarehouse` WHERE (`codemp` = '01') and (`sucursal` = '001') and (`codigo` = '0100100001');
INSERT INTO `db_usupplyit`.`tb_a_mwarehouse` (`codemp`, `sucursal`, `codigo`, `nombre`, `localizacion`, `nota`, `referencia`, `usuario`, `estado`) 
    VALUES ('01', '001', '0100100000', 'WHAREHOUSE', '', '', '', '', '1');

set @almacen = '0100100000' ;
set @stock_cero = '0.00'; 
insert into tb_a_ostock (codemp, sucursal, almacen, producto, stock_min, stock_max, stock_reo, stock_disponible, stock_reservado, stock_enmano, stock_existencia, fecha_r, fecha_u)
	select 	codemp, sucursal, @almacen, trim(codigo), @stock_cero, @stock_cero, @stock_cero, stock_disponible, stock_reserv, stock_enmano, stock_existencia, fecha_rec, fecha_sync
		from tb_a_mproducts
        order by trim(codigo) ;

ALTER TABLE `tb_a_mproducts` 
    DROP COLUMN `stock_existencia`,
    DROP COLUMN `stock_enmano`,
    DROP COLUMN `stock_reserv`,
    DROP COLUMN `stock_disponible`;

ALTER TABLE `tb_f_ocustomers_pl_dproducts` 
	CHANGE COLUMN `precio_lista` `precio_venta` DECIMAL(10,2) NULL DEFAULT '0.00' ;

-- ------------------------------------------------------------------------------------------------------------------------------------------------------
ALTER TABLE `tb_f_mseller` 
    ADD COLUMN `comision_porc` DECIMAL(6,2) NULL DEFAULT '0.00' AFTER `nombre`,
    ADD COLUMN `comision_neto` SMALLINT NULL DEFAULT '0' AFTER `comision_porc`,
    CHANGE COLUMN `nombre` `nombre` VARCHAR(120) CHARACTER SET 'utf8mb4' COLLATE 'utf8mb4_spanish_ci' NULL DEFAULT '' ,
    CHANGE COLUMN `estado` `estado` SMALLINT NULL DEFAULT '1' ;


ALTER TABLE `tb_f_mseller` 
DROP COLUMN `comision_porc`,
CHANGE COLUMN `comision_tipo` `comision_tipo` VARCHAR(10) NULL DEFAULT '' ;

-- ------------------------------------------------------------------------------------------------------------------------------------------------------
CREATE TABLE `tb_f_mcommission` (
  `codemp` VARCHAR(2) CHARACTER SET 'utf8mb4' COLLATE 'utf8mb4_spanish_ci' NOT NULL,
  `sucursal` VARCHAR(3) CHARACTER SET 'utf8mb4' COLLATE 'utf8mb4_spanish_ci' NOT NULL,
  `codigo` VARCHAR(10) CHARACTER SET 'utf8mb4' COLLATE 'utf8mb4_spanish_ci' NOT NULL,
  `nombre` VARCHAR(120) CHARACTER SET 'utf8mb4' COLLATE 'utf8mb4_spanish_ci' NULL DEFAULT '',
  `comision_tipo` smallint NULL DEFAULT '0',
  `estado` SMALLINT NULL DEFAULT '1',
  PRIMARY KEY (`codemp`, `sucursal`, `codigo`))
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8mb4
COLLATE = utf8mb4_spanish_ci;


CREATE TABLE `tb_f_mcommission_drange` (
  `codemp` VARCHAR(2) CHARACTER SET 'utf8mb4' COLLATE 'utf8mb4_spanish_ci' NULL DEFAULT '',
  `sucursal` VARCHAR(3) CHARACTER SET 'utf8mb4' COLLATE 'utf8mb4_spanish_ci' NULL DEFAULT '',
  `codigo` VARCHAR(10) CHARACTER SET 'utf8mb4' COLLATE 'utf8mb4_spanish_ci' NULL DEFAULT '',
  `desde` DECIMAL(15,2) NULL DEFAULT '0.00',
  `hasta` DECIMAL(15,2) NULL DEFAULT '0.00',
  `porcentaje` DECIMAL(6,2) NULL DEFAULT '0.00',
  `item` INT NOT NULL AUTO_INCREMENT,
  PRIMARY KEY (`item`))
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8mb4
COLLATE = utf8mb4_spanish_ci;

-- ------------------------------------------------------------------------------------------------------------------------------------------------------
-- ------------------------------------------------------------------------------------------------------------------------------------------------------
-- ------------------------------------------------------------------------------------------------------------------------------------------------------
-- Consulta para Comision Vendedores.
-- ------------------------------------------------------------------------------------------------------------------------------------------------------
-- ------------------------------------------------------------------------------------------------------------------------------------------------------
-- ------------------------------------------------------------------------------------------------------------------------------------------------------
drop temporary table if exists f_osales_orders ;
create temporary table f_osales_orders
	select	tb_f_osales_orders.codemp, 
			tb_f_osales_orders.sucursal, 
			tb_f_osales_orders.numero, 
            tb_f_osales_orders.fecha, 
            tb_f_osales_orders.cliente,
            tb_f_osales_orders.referencia,
            tb_f_osales_orders.vendedor
		from tb_f_osales_orders
		where tb_f_osales_orders.fecha between '2025-11-01' and '2025-12-31'
				and tb_f_osales_orders.estado <> 2
		order by tb_f_osales_orders.numero ;


drop temporary table if exists c_opurchase_dprod ;
create temporary table c_opurchase_dprod
	select	tb_c_opurchase_orders.codemp, 
			tb_c_opurchase_orders.sucursal, 
			tb_c_opurchase_orders.numero,
            f_osales_orders.numero as factura,
            tb_c_opurchase_orders.proveedor, 
            tb_c_opurchase_orders.referencia,
            tb_c_opurchase_dproducts.producto,
            tb_c_opurchase_dproducts.cantidad_order as cantidad_compra,
            tb_c_opurchase_dproducts.precio_venta as precio_compra,
            round(tb_c_opurchase_dproducts.precio_venta * tb_c_opurchase_dproducts.cantidad_order, 2) as total_compra
		from tb_c_opurchase_orders
			inner join f_osales_orders
				on tb_c_opurchase_orders.codemp = f_osales_orders.codemp
					and tb_c_opurchase_orders.sucursal = f_osales_orders.sucursal
                    and tb_c_opurchase_orders.referencia = f_osales_orders.referencia
                    and f_osales_orders.referencia <> '' 
                    and tb_c_opurchase_orders.estado <> 2 
			inner join tb_c_opurchase_dproducts
				on tb_c_opurchase_orders.codemp = tb_c_opurchase_dproducts.codemp
					and tb_c_opurchase_orders.sucursal = tb_c_opurchase_dproducts.sucursal
                    and tb_c_opurchase_orders.numero = tb_c_opurchase_dproducts.numero;

drop temporary table if exists f_osales_orders_dprod ;
create temporary table f_osales_orders_dprod
	select	f_osales_orders.codemp, 
            f_osales_orders.sucursal, 
            f_osales_orders.numero, 
            coalesce(c_opurchase_dprod.numero, '') as compra,
            tb_f_osales_dproducts.producto, 
            coalesce(c_opurchase_dprod.cantidad_compra, 0) as cantidad_compra,
            tb_f_osales_dproducts.cantidad_ord as cantidad_venta,
            round(tb_f_osales_dproducts.cantidad_ord * coalesce(c_opurchase_dprod.precio_compra, 2)) as total_compra,
            round(tb_f_osales_dproducts.cantidad_ord * tb_f_osales_dproducts.precio_venta, 2) as total_venta
		from f_osales_orders
			inner join tb_f_osales_dproducts
				on f_osales_orders.codemp = tb_f_osales_dproducts.codemp
					and f_osales_orders.sucursal = tb_f_osales_dproducts.sucursal
                    and f_osales_orders.numero = tb_f_osales_dproducts.numero
			left join c_opurchase_dprod
				on tb_f_osales_dproducts.codemp = c_opurchase_dprod.codemp
					and tb_f_osales_dproducts.sucursal = c_opurchase_dprod.sucursal
                    and tb_f_osales_dproducts.numero = c_opurchase_dprod.factura
                    and tb_f_osales_dproducts.producto = c_opurchase_dprod.producto
		order by f_osales_orders.numero, tb_f_osales_dproducts.producto ;
            
drop temporary table if exists f_osales_orders_total ;
create temporary table f_osales_orders_total
	select	codemp, sucursal, numero,
			sum(cantidad_compra) as cantidad_compra,
            sum(cantidad_venta) as cantidad_venta,
			sum(total_compra) as total_compra,
            sum(total_venta) as total_venta,
            sum(total_venta - total_compra) total_neto
		from f_osales_orders_dprod
        group by codemp, sucursal, numero ;
            
select	f_osales_orders.codemp, 
		f_osales_orders.sucursal, 
		f_osales_orders.numero, 
		f_osales_orders.fecha, 
		f_osales_orders.cliente, 
		-- f_osales_orders.numero_so, 
		f_osales_orders.referencia, 
        f_osales_orders_total.cantidad_compra,
        f_osales_orders_total.cantidad_venta,
        f_osales_orders_total.total_compra,
		f_osales_orders_total.total_venta,
        f_osales_orders_total.total_neto, 
        case when coalesce(tb_f_mseller.comision_tipo, 0) = 1
				then round((f_osales_orders_total.total_venta * coalesce(tb_f_mseller.comision_porc, 0)) / 100, 2) 
                else round((f_osales_orders_total.total_neto * coalesce(tb_f_mseller.comision_porc, 0)) / 100, 2) 
			end as total_vendedor
	from f_osales_orders 
		left join f_osales_orders_total
			on f_osales_orders.codemp = f_osales_orders_total.codemp
				and f_osales_orders.sucursal = f_osales_orders_total.sucursal
				and f_osales_orders.numero = f_osales_orders_total.numero 
		left join tb_f_mseller
			on f_osales_orders.codemp = tb_f_mseller.codemp
				and f_osales_orders.sucursal = tb_f_mseller.sucursal
				and f_osales_orders.vendedor = tb_f_mseller.codigo 
	order by f_osales_orders.numero ;

-- ------------------------------------------------------------------------------------------------------------------------------------------------------
ALTER TABLE `tb_f_mseller` 
CHANGE COLUMN `comision_neto` `comision_tipo` SMALLINT NULL DEFAULT '0' ;

-- ------------------------------------------------------------------------------------------------------------------------------------------------------
update tb_f_mcustomers set moneda = 'USD' where moneda <> ' USD' ;
update tb_f_obudget set moneda = 'USD' where moneda <> ' USD' ;
update tb_f_oinvoice set moneda = 'USD' where moneda <> ' USD' ;
update tb_f_oquote set moneda = 'USD' where moneda <> ' USD' ;
update tb_f_osales_orders set moneda = 'USD' where moneda <> ' USD' ;

-- ------------------------------------------------------------------------------------------------------------------------------------------------------

-- ------------------------------------------------------------------------------------------------------------------------------------------------------

-- ------------------------------------------------------------------------------------------------------------------------------------------------------

-- ------------------------------------------------------------------------------------------------------------------------------------------------------
