-- --------------------------------------------------------
-- Arrebol Consuntancy
-- version 1.0.1
-- http://www.arrebolconsultancy.com
-- --------------------------------------------------------

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET AUTOCOMMIT = 0;
START TRANSACTION;
-- SET time_zone = "+00:00";

/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;
DROP DATABASE IF EXISTS `car_driver_tepic`;

CREATE DATABASE IF NOT EXISTS `car_driver_tepic` DEFAULT CHARACTER SET latin1 COLLATE latin1_spanish_ci;
USE `car_driver_tepic`;
-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `PERSON`
--
CREATE TABLE `PERSON` (
  `id` char(36) NOT NULL,
  `first_name` varchar(25) NOT NULL,
  `second_name` varchar(25) DEFAULT NULL,
  `last_name` varchar(25) DEFAULT NULL,
  `middle_name` varchar(25) DEFAULT NULL,
  `mail` varchar(200) DEFAULT NULL,
  `birthdate` date DEFAULT NULL,
  `curp` varchar(20) DEFAULT NULL,
  `rfc` varchar(13) DEFAULT NULL,
  `avatar` varchar(150) DEFAULT NULL,
  `person_type` enum('CUSTOMER','PROVIDER','EMPLOYEE','DRIVER') NOT NULL,
  `payment` float DEFAULT NULL,
  `payment_type` enum('WEEKLY','FOURTEENTH','BIWEEKLY','MONTHLY') DEFAULT NULL,
  `license` varchar(20) DEFAULT NULL,
  `expiration_license` date DEFAULT NULL,
  `ife` varchar(20) DEFAULT NULL,
  `retainer` float DEFAULT NULL,
  `admission_date` date DEFAULT NULL,
  `active` tinyint NOT NULL,
  `black_list` enum('ENABLED','DISABLED') DEFAULT 'DISABLED',
  `black_list_description` varchar(500) DEFAULT NULL,
  `root` tinyint NOT NULL DEFAULT '0',
  `created_by` char(36) NOT NULL,
  `created_on` datetime DEFAULT CURRENT_TIMESTAMP,
  `last_updated_by` char(36) DEFAULT NULL,
  `last_updated_on` datetime DEFAULT CURRENT_TIMESTAMP,
  `adeudo` float default 0,
  `name_endorsement` varchar(500) default '',
  `parentesco_endorsement` varchar(500) default '',
  `phone_endorsement` varchar(500) default '',
    imss double DEFAULT 0,
salary double DEFAULT 0,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
-- --------------------------------------------------------
--
-- Estructura de tabla para la tabla `CAR_DRIVER_USER`
--
CREATE TABLE `CAR_DRIVER_USER` (
  `id` char(36) NOT NULL,
  `person_id` char(36) NOT NULL,
  `username` varchar(100) NOT NULL,
  `password` varchar(100) NOT NULL,
  `active` tinyint NOT NULL,
  `root` tinyint DEFAULT '0',
  `user_status_type` enum('ENABLED','DISABLED','DELETED') NOT NULL DEFAULT 'ENABLED',
  `created_by` varchar(36) NOT NULL,
  `created_on` datetime DEFAULT CURRENT_TIMESTAMP,
  `last_updated_by` varchar(36) DEFAULT NULL,
  `last_updated_on` datetime DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`,`person_id`),
  UNIQUE KEY `user_username_uk` (`username`),
  KEY `user_person_id_fk` (`person_id`),
  CONSTRAINT `user_person_id_fk` FOREIGN KEY (`person_id`) REFERENCES `PERSON` (`id`) ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
-- --------------------------------------------------------
--
-- Alter table para la tabla `PERSON`
--
ALTER TABLE `PERSON`
ADD CONSTRAINT `person_user_created_by_id_fk` 
       FOREIGN KEY (`created_by`) REFERENCES `CAR_DRIVER_USER`(`id`)
            ON UPDATE CASCADE
            ON DELETE RESTRICT;

ALTER TABLE `PERSON`
ADD CONSTRAINT `person_user_last_updated_by_id_fk` 
       FOREIGN KEY (`last_updated_by`) REFERENCES `CAR_DRIVER_USER`(`id`)
            ON UPDATE CASCADE
            ON DELETE RESTRICT;
-- --------------------------------------------------------
--
-- Estructura de tabla para la tabla `ADDRESS`
--
-- --------------------------------------------------------
CREATE TABLE `ADDRESS` (
  `id` char(36) NOT NULL,
  `street` varchar(50) NOT NULL,
  `street_2` varchar(50) DEFAULT NULL,
  `extended_address` varchar(50) DEFAULT NULL,
  `locality` varchar(50) DEFAULT NULL,
  `region` varchar(50) DEFAULT NULL,
  `zip_code` varchar(10) DEFAULT NULL,
  `address_type` enum('HOME','COMPANY','BILLING') NOT NULL,
  `preferenced` tinyint DEFAULT '0',
  `address_google_maps` varchar(250) DEFAULT NULL,
  `latitud` varchar(50) DEFAULT NULL,
  `longitud` varchar(50) DEFAULT NULL,
  `person_id` char(36) NOT NULL,
  `created_by` char(36) NOT NULL,
  `created_on` datetime DEFAULT CURRENT_TIMESTAMP,
  `last_updated_by` char(36) DEFAULT NULL,
  `last_updated_on` datetime DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  KEY `address_person_id_fk` (`person_id`),
  KEY `address_user_created_by_id_fk` (`created_by`),
  KEY `address_user_last_updated_by_id_fk` (`last_updated_by`),
  CONSTRAINT `address_person_id_fk` FOREIGN KEY (`person_id`) REFERENCES `PERSON` (`id`) ON UPDATE CASCADE,
  CONSTRAINT `address_user_created_by_id_fk` FOREIGN KEY (`created_by`) REFERENCES `CAR_DRIVER_USER` (`id`) ON UPDATE CASCADE,
  CONSTRAINT `address_user_last_updated_by_id_fk` FOREIGN KEY (`last_updated_by`) REFERENCES `CAR_DRIVER_USER` (`id`) ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Estructura de tabla para la tabla `CONTACT_NUMBER`
--
CREATE TABLE `CONTACT_NUMBER` (
  `id` char(36) NOT NULL,
  `contact_number` varchar(20) NOT NULL,
  `lada` varchar(4) DEFAULT NULL,
  `number_type` enum('HOME','COMPANY','MOBILE') NOT NULL,
  `preferenced` tinyint DEFAULT '0',
  `person_id` char(36) NOT NULL,
  `created_by` char(36) NOT NULL,
  `created_on` datetime DEFAULT CURRENT_TIMESTAMP,
  `last_updated_by` char(36) DEFAULT NULL,
  `last_updated_on` datetime DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  KEY `contact_number_person_id_fk` (`person_id`),
  KEY `contact_number_user_created_by_id_fk` (`created_by`),
  KEY `contact_number_user_last_updated_by_id_fk` (`last_updated_by`),
  CONSTRAINT `contact_number_person_id_fk` FOREIGN KEY (`person_id`) REFERENCES `PERSON` (`id`) ON UPDATE CASCADE,
  CONSTRAINT `contact_number_user_created_by_id_fk` FOREIGN KEY (`created_by`) REFERENCES `CAR_DRIVER_USER` (`id`) ON UPDATE CASCADE,
  CONSTRAINT `contact_number_user_last_updated_by_id_fk` FOREIGN KEY (`last_updated_by`) REFERENCES `CAR_DRIVER_USER` (`id`) ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `PERSONAL_REFERENCE`
--
CREATE TABLE `PERSONAL_REFERENCE` (
  `id` char(36) NOT NULL,
  `complete_name` varchar(50) NOT NULL,
  `complete_address` varchar(250) DEFAULT NULL,
  `phone` varchar(20) NOT NULL,
  `reference_type` enum('WIFE','HUSBAND','FATHER','MOTHER','BROTHER','AUNT','GRANPARENT','COUSIN','NEPHEW','FRIEND','COWORKER','OTHER') NOT NULL,
  `person_id` char(36) NOT NULL,
  `created_by` char(36) NOT NULL,
  `created_on` datetime DEFAULT CURRENT_TIMESTAMP,
  `last_updated_by` char(36) DEFAULT NULL,
  `last_updated_on` datetime DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  KEY `reference_person_id_fk` (`person_id`),
  KEY `reference_user_created_by_id_fk` (`created_by`),
  KEY `reference_user_last_updated_by_id_fk` (`last_updated_by`),
  CONSTRAINT `reference_person_id_fk` FOREIGN KEY (`person_id`) REFERENCES `PERSON` (`id`) ON UPDATE CASCADE,
  CONSTRAINT `reference_user_created_by_id_fk` FOREIGN KEY (`created_by`) REFERENCES `CAR_DRIVER_USER` (`id`) ON UPDATE CASCADE,
  CONSTRAINT `reference_user_last_updated_by_id_fk` FOREIGN KEY (`last_updated_by`) REFERENCES `CAR_DRIVER_USER` (`id`) ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `DOCUMENT`
--
CREATE TABLE `DOCUMENT` (
  `id` char(36) NOT NULL,
  `path` varchar(250) NOT NULL,
  `document_type` enum('IFE','LICENSE','CAR_IMAGE','SELFIE') NOT NULL,
  `file_name` varchar(50) NOT NULL,
  `document` longblob NOT NULL,
  `person_id` char(36) NOT NULL,
  `created_by` char(36) NOT NULL,
  `created_on` datetime DEFAULT CURRENT_TIMESTAMP,
  `last_updated_by` char(36) DEFAULT NULL,
  `last_updated_on` datetime DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  KEY `document_person_id_fk` (`person_id`),
  KEY `document_user_created_by_id_fk` (`created_by`),
  KEY `document_user_last_updated_by_id_fk` (`last_updated_by`),
  CONSTRAINT `document_person_id_fk` FOREIGN KEY (`person_id`) REFERENCES `PERSON` (`id`) ON UPDATE CASCADE,
  CONSTRAINT `document_user_created_by_id_fk` FOREIGN KEY (`created_by`) REFERENCES `CAR_DRIVER_USER` (`id`) ON UPDATE CASCADE,
  CONSTRAINT `document_user_last_updated_by_id_fk` FOREIGN KEY (`last_updated_by`) REFERENCES `CAR_DRIVER_USER` (`id`) ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
-- --------------------------------------------------------
--
-- Estructura de tabla para la tabla `PERMISSION`
--
CREATE TABLE `PERMISSION` (
  `id` char(36) NOT NULL,
  `name` varchar(200) NOT NULL,
  `description` varchar(200) NOT NULL,
  `menu_path` varchar(200) NOT NULL,
  `left_to_right_order` tinyint NOT NULL,
  `top_to_bottom_order` smallint(6) NOT NULL,
  `general_access` tinyint NOT NULL DEFAULT '0',
  `application_menu` tinyint NOT NULL DEFAULT '1',
  `active` tinyint NOT NULL DEFAULT '1',
  `parent_name` varchar(200) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `USER_HAS_PERMISSION`
--

-- --------------------------------------------------------
CREATE TABLE `USER_HAS_PERMISSION` (
  `id_user` char(36) NOT NULL,
  `id_permission` char(36) NOT NULL,
  PRIMARY KEY (`id_user`,`id_permission`),
  KEY `user_has_permission_permission_id_fk` (`id_permission`),
  CONSTRAINT `user_has_permission_permission_id_fk` FOREIGN KEY (`id_permission`) REFERENCES `PERMISSION` (`id`) ON UPDATE CASCADE,
  CONSTRAINT `user_has_permission_user_id_fk` FOREIGN KEY (`id_user`) REFERENCES `CAR_DRIVER_USER` (`id`) ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
-- --------------------------------------------------------
--
-- Estructura de tabla para la tabla `BRAND`
--
CREATE TABLE `BRAND` (
  `id` char(36) NOT NULL,
  `name` varchar(200) NOT NULL,
  `brand_type` enum('VEHICLE','OTHER') NOT NULL,
  `created_by` char(36) NOT NULL,
  `created_on` datetime DEFAULT CURRENT_TIMESTAMP,
  `last_updated_by` char(36) DEFAULT NULL,
  `last_updated_on` datetime DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE KEY `brand_name_uk` (`name`),
  KEY `brand_user_created_by_id_fk` (`created_by`),
  KEY `brand_user_last_updated_by_id_fk` (`last_updated_by`),
  CONSTRAINT `brand_user_created_by_id_fk` FOREIGN KEY (`created_by`) REFERENCES `CAR_DRIVER_USER` (`id`) ON UPDATE CASCADE,
  CONSTRAINT `brand_user_last_updated_by_id_fk` FOREIGN KEY (`last_updated_by`) REFERENCES `CAR_DRIVER_USER` (`id`) ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
-- --------------------------------------------------------
--
-- Estructura de tabla para la tabla `PRODUCT`
--
CREATE TABLE `PRODUCT` (
  `id` char(36) NOT NULL,
  `name` varchar(200) NOT NULL,
  `code` varchar(50) NOT NULL,
  `amount` double DEFAULT NULL,
  `utility` numeric(10,2) DEFAULT NULL,
  `quantity` int(11) DEFAULT NULL,
  `quantity_2` int DEFAULT 0,
  `life` int(11) DEFAULT NULL,
  `brand_id` char(36) NOT NULL,
  `tipo_uso` varchar(250) default '',
  `compatibilidad` varchar(250) default '',
  `amount2` double default 0,
  `utility2` decimal(10,2) default 0,
  `created_by` char(36) NOT NULL,
  `created_on` datetime DEFAULT CURRENT_TIMESTAMP,
  `last_updated_by` char(36) DEFAULT NULL,
  `last_updated_on` datetime DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE KEY `product_name_uk` (`name`),
  KEY `product_brand_id_fk` (`brand_id`),
  KEY `product_user_created_by_id_fk` (`created_by`),
  KEY `product_user_last_updated_by_id_fk` (`last_updated_by`),
  CONSTRAINT `product_brand_id_fk` FOREIGN KEY (`brand_id`) REFERENCES `BRAND` (`id`) ON UPDATE CASCADE,
  CONSTRAINT `product_user_created_by_id_fk` FOREIGN KEY (`created_by`) REFERENCES `CAR_DRIVER_USER` (`id`) ON UPDATE CASCADE,
  CONSTRAINT `product_user_last_updated_by_id_fk` FOREIGN KEY (`last_updated_by`) REFERENCES `CAR_DRIVER_USER` (`id`) ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
-- --------------------------------------------------------
--
-- Estructura de tabla para la tabla `VEHICLE`
--
CREATE TABLE `VEHICLE` (
  `id` char(36) NOT NULL,
  `name` varchar(200) NOT NULL,
  `brand_id` char(36) NOT NULL,
  `created_by` char(36) NOT NULL,
  `created_on` datetime DEFAULT CURRENT_TIMESTAMP,
  `last_updated_by` char(36) DEFAULT NULL,
  `last_updated_on` datetime DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE KEY `vehicle_name_uk` (`name`),
  KEY `vehicle_brand_id_fk` (`brand_id`),
  KEY `vehicle_user_created_by_id_fk` (`created_by`),
  KEY `vehicle_user_last_updated_by_id_fk` (`last_updated_by`),
  CONSTRAINT `vehicle_brand_id_fk` FOREIGN KEY (`brand_id`) REFERENCES `BRAND` (`id`) ON UPDATE CASCADE,
  CONSTRAINT `vehicle_user_created_by_id_fk` FOREIGN KEY (`created_by`) REFERENCES `CAR_DRIVER_USER` (`id`) ON UPDATE CASCADE,
  CONSTRAINT `vehicle_user_last_updated_by_id_fk` FOREIGN KEY (`last_updated_by`) REFERENCES `CAR_DRIVER_USER` (`id`) ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
-- --------------------------------------------------------
--
-- Estructura de tabla para la tabla `COLOR`
--
CREATE TABLE `COLOR` (
  `id` char(36) NOT NULL,
  `name` varchar(200) NOT NULL,
  `created_by` char(36) NOT NULL,
  `created_on` datetime DEFAULT CURRENT_TIMESTAMP,
  `last_updated_by` char(36) DEFAULT NULL,
  `last_updated_on` datetime DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE KEY `color_name_uk` (`name`),
  KEY `color_user_created_by_id_fk` (`created_by`),
  KEY `color_user_last_updated_by_id_fk` (`last_updated_by`),
  CONSTRAINT `color_user_created_by_id_fk` FOREIGN KEY (`created_by`) REFERENCES `CAR_DRIVER_USER` (`id`) ON UPDATE CASCADE,
  CONSTRAINT `color_user_last_updated_by_id_fk` FOREIGN KEY (`last_updated_by`) REFERENCES `CAR_DRIVER_USER` (`id`) ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
-- --------------------------------------------------------
--
-- Estructura de tabla para la tabla `TAXI`
--
CREATE TABLE `TAXI` (
  `id` char(36) NOT NULL,
  `placa` varchar(12) NOT NULL,
  `economic_number` varchar(12) DEFAULT NULL,
  `serial_number` varchar(60) DEFAULT NULL,
  `motor_mumber` varchar(60) DEFAULT NULL,
  `comments` varchar(200) DEFAULT NULL,
  `year` varchar(4) DEFAULT NULL,
  `insurance_number` varchar(50) DEFAULT NULL,
  `insurance_end_date` date DEFAULT NULL,
  `km` float DEFAULT NULL,
  `km_maintenance` float default 0,
  `pay_day` double DEFAULT 200.00,
  `pay_saving` double DEFAULT 20.00,
  `pay_accident` double DEFAULT 10.00,  
  `payment_car` numeric(10,2),
  `active` tinyint DEFAULT '1',
  `vehicle_id` char(36) DEFAULT NULL,
  `adeudo_customer` float default 0,
  `app_platform` enum('TAXI','UBER','IN_DRIVER','CABIFY','EASY_TAXI','SIGO','CITY_DRIVE','OTHER') DEFAULT 'TAXI',
  `day_monday` enum('ENEBLED','DISABLED') DEFAULT 'DISABLED',
  `day_tuesday` enum('ENEBLED','DISABLED') DEFAULT 'DISABLED',
  `day_wednesday` enum('ENEBLED','DISABLED') DEFAULT 'DISABLED',
  `day_thursday` enum('ENEBLED','DISABLED') DEFAULT 'DISABLED',
  `day_friday` enum('ENEBLED','DISABLED') DEFAULT 'DISABLED',
  `day_saturday` enum('ENEBLED','DISABLED') DEFAULT 'DISABLED',
  `day_sunday` enum('ENEBLED','DISABLED') DEFAULT 'DISABLED',
  `insurance_name` varchar(100),
  `insurance_cobertura` varchar(100),
  `color_id` char(36) DEFAULT NULL,
  `customer_id` char(36) NOT NULL,
  `driver_id` char(36) DEFAULT NULL,
  `gps` enum('ENEBLED','DISABLED') default 'DISABLED',
  `gps_number` varchar(20) default '',
  `created_by` char(36) NOT NULL,
  `created_on` datetime DEFAULT CURRENT_TIMESTAMP,
  `last_updated_by` char(36) DEFAULT NULL,
  `last_updated_on` datetime DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  KEY `taxi_vehicle_vehicle_id_id_fk` (`vehicle_id`),
  KEY `taxi_color_color_id_id_fk` (`color_id`),
  KEY `taxi_user_created_by_id_fk` (`created_by`),
  KEY `taxi_user_last_updated_by_id_fk` (`last_updated_by`),
  KEY `taxi_person_customer_id_id_fk` (`customer_id`),
  KEY `taxi_person_driver_id_id_fk` (`driver_id`),
  CONSTRAINT `taxi_color_color_id_id_fk` FOREIGN KEY (`color_id`) REFERENCES `COLOR` (`id`) ON UPDATE CASCADE,
  CONSTRAINT `taxi_person_customer_id_id_fk` FOREIGN KEY (`customer_id`) REFERENCES `PERSON` (`id`) ON UPDATE CASCADE,
  CONSTRAINT `taxi_person_driver_id_id_fk` FOREIGN KEY (`driver_id`) REFERENCES `PERSON` (`id`) ON UPDATE CASCADE,
  CONSTRAINT `taxi_user_created_by_id_fk` FOREIGN KEY (`created_by`) REFERENCES `CAR_DRIVER_USER` (`id`) ON UPDATE CASCADE,
  CONSTRAINT `taxi_user_last_updated_by_id_fk` FOREIGN KEY (`last_updated_by`) REFERENCES `CAR_DRIVER_USER` (`id`) ON UPDATE CASCADE,
  CONSTRAINT `taxi_vehicle_vehicle_id_id_fk` FOREIGN KEY (`vehicle_id`) REFERENCES `VEHICLE` (`id`) ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
-- --------------------------------------------------------
--
-- Estructura de tabla para la tabla `SERVICE`
--
CREATE TABLE `SERVICE` (
  `id` char(36) NOT NULL,
  `name` varchar(50) NOT NULL,
  `charge` float NOT NULL,
  `active` tinyint DEFAULT '1',
  `discount_fix` int default 0,
  `created_by` char(36) NOT NULL,
  `created_on` datetime DEFAULT CURRENT_TIMESTAMP,
  `last_updated_by` char(36) DEFAULT NULL,
  `last_updated_on` datetime DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  KEY `service_user_created_by_id_fk` (`created_by`),
  KEY `service_user_last_updated_by_id_fk` (`last_updated_by`),
  CONSTRAINT `service_user_created_by_id_fk` FOREIGN KEY (`created_by`) REFERENCES `CAR_DRIVER_USER` (`id`) ON UPDATE CASCADE,
  CONSTRAINT `service_user_last_updated_by_id_fk` FOREIGN KEY (`last_updated_by`) REFERENCES `CAR_DRIVER_USER` (`id`) ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
-- --------------------------------------------------------
--
-- Estructura de tabla para la tabla `PERSON_HAS_SERVICE`
--
CREATE TABLE `PERSON_HAS_SERVICE` (
  `person_id` char(36) NOT NULL,
  `service_id` char(36) NOT NULL,
  PRIMARY KEY (`person_id`,`service_id`),
  KEY `person_has_service_service_id_fk` (`service_id`),
  CONSTRAINT `person_has_service_person_id_fk` FOREIGN KEY (`person_id`) REFERENCES `PERSON` (`id`) ON UPDATE CASCADE,
  CONSTRAINT `person_has_service_service_id_fk` FOREIGN KEY (`service_id`) REFERENCES `SERVICE` (`id`) ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
-- --------------------------------------------------------
--
-- Estructura de tabla para la tabla `CONTRACT`
--
CREATE TABLE `CONTRACT` (
  `id` char(36) NOT NULL,
  `name` varchar(50) DEFAULT NULL,
  `price` float NOT NULL,
  `start_date` date NOT NULL,
  `end_date` date NOT NULL,
  `active` tinyint NOT NULL,
  `taxi_id` char(36) NOT NULL,
  `created_by` char(36) NOT NULL,
  `created_on` datetime DEFAULT CURRENT_TIMESTAMP,
  `last_updated_by` char(36) DEFAULT NULL,
  `last_updated_on` datetime DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  KEY `contract_taxi_id_fk` (`taxi_id`),
  KEY `contract_user_created_by_id_fk` (`created_by`),
  KEY `contract_user_last_updated_by_id_fk` (`last_updated_by`),
  CONSTRAINT `contract_taxi_id_fk` FOREIGN KEY (`taxi_id`) REFERENCES `TAXI` (`id`) ON UPDATE CASCADE,
  CONSTRAINT `contract_user_created_by_id_fk` FOREIGN KEY (`created_by`) REFERENCES `CAR_DRIVER_USER` (`id`) ON UPDATE CASCADE,
  CONSTRAINT `contract_user_last_updated_by_id_fk` FOREIGN KEY (`last_updated_by`) REFERENCES `CAR_DRIVER_USER` (`id`) ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
-- --------------------------------------------------------
--
-- Estructura de tabla para la tabla `CONTRACT_HAS_SERVICE`
--
CREATE TABLE `CONTRACT_HAS_SERVICE` (
  `contract_id` char(36) NOT NULL,
  `service_id` char(36) NOT NULL,
  PRIMARY KEY (`contract_id`,`service_id`),
  KEY `contract_has_service_service_id_fk` (`service_id`),
  CONSTRAINT `contract_has_service_contract_id_fk` FOREIGN KEY (`contract_id`) REFERENCES `CONTRACT` (`id`) ON UPDATE CASCADE,
  CONSTRAINT `contract_has_service_service_id_fk` FOREIGN KEY (`service_id`) REFERENCES `SERVICE` (`id`) ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
-- --------------------------------------------------------
--
-- Estructura de tabla para la tabla `EXPENSE`
--
CREATE TABLE `EXPENSE` (
  `id` char(36) NOT NULL,
  `folio` varchar(60) NOT NULL,
  `payment` double NOT NULL,
  `payment_date` datetime DEFAULT CURRENT_TIMESTAMP,
  `description` varchar(200) DEFAULT NULL,
  `expense_type` enum('BILL','NOTE','CORTE','ELECTRICO','TORNO','SOLDADURA','ESTETICO','GRUA','INICIO','PAGO_CLIENTE','OTRAS_SALIDAS','OTRAS_ENTRADAS','MANDADOS','APOYO_VIAL','RETORNO_EFECTIVO'),
  `expense_status_type` enum('PENDING','AUTHORIZED') NOT NULL,
  `amount_menudeo` double default 0,
  `taxi_id` char(36) DEFAULT NULL,
  `driver_id` char(36) DEFAULT NULL,
  `created_by` char(36) NOT NULL,
  `created_on` datetime DEFAULT CURRENT_TIMESTAMP,
  `last_updated_by` char(36) DEFAULT NULL,
  `last_updated_on` datetime DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  KEY `expense_taxi_id_fk` (`taxi_id`),
  KEY `expense_person_id_fk` (`driver_id`),
  KEY `expense_user_created_by_id_fk` (`created_by`),
  KEY `expense_user_last_updated_by_id_fk` (`last_updated_by`),
  CONSTRAINT `expense_person_id_fk` FOREIGN KEY (`driver_id`) REFERENCES `PERSON` (`id`) ON UPDATE CASCADE,
  CONSTRAINT `expense_taxi_id_fk` FOREIGN KEY (`taxi_id`) REFERENCES `TAXI` (`id`) ON UPDATE CASCADE,
  CONSTRAINT `expense_user_created_by_id_fk` FOREIGN KEY (`created_by`) REFERENCES `CAR_DRIVER_USER` (`id`) ON UPDATE CASCADE,
  CONSTRAINT `expense_user_last_updated_by_id_fk` FOREIGN KEY (`last_updated_by`) REFERENCES `CAR_DRIVER_USER` (`id`) ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
-- --------------------------------------------------------
--
-- Estructura de tabla para la tabla `HOLIDAY`
--
CREATE TABLE `HOLIDAY` (
  `id` char(36) NOT NULL,
  `holyday_date` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `description` varchar(200) DEFAULT NULL,
  `created_by` char(36) NOT NULL,
  `created_on` datetime DEFAULT CURRENT_TIMESTAMP,
  `last_updated_by` char(36) DEFAULT NULL,
  `last_updated_on` datetime DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  KEY `holiday_user_created_by_id_fk` (`created_by`),
  KEY `holiday_user_last_updated_by_id_fk` (`last_updated_by`),
  CONSTRAINT `holiday_user_created_by_id_fk` FOREIGN KEY (`created_by`) REFERENCES `CAR_DRIVER_USER` (`id`) ON UPDATE CASCADE,
  CONSTRAINT `holiday_user_last_updated_by_id_fk` FOREIGN KEY (`last_updated_by`) REFERENCES `CAR_DRIVER_USER` (`id`) ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
-- --------------------------------------------------------
--
-- Estructura de tabla para la tabla `INVOICE`
--
CREATE TABLE `INVOICE` (
  `id` char(36) NOT NULL,   
  `folio` varchar(50) NOT NULL,
  `buy_date` date NOT NULL,
  `comment` varchar(250) DEFAULT NULL,
  `subtotal` float NOT NULL,
  `iva` float NOT NULL,
  `total` float NOT NULL,
  `status_type` enum('PENDING','APPROVED') NOT NULL,
  `active` tinyint NOT NULL,
  `provider_id` char(36) NOT NULL,
  `created_by` char(36) NOT NULL,
  `created_on` datetime DEFAULT CURRENT_TIMESTAMP,
  `last_updated_by` char(36) DEFAULT NULL,
  `last_updated_on` datetime DEFAULT CURRENT_TIMESTAMP,
  `taxi_id` char(36) DEFAULT NULL,
  `id_location` varchar(50),
  PRIMARY KEY (`id`),
  KEY `invoice_person_id_fk` (`provider_id`),
  KEY `invoice_user_created_by_id_fk` (`created_by`),
  KEY `invoice_user_last_updated_by_id_fk` (`last_updated_by`),
  CONSTRAINT `invoice_person_id_fk` FOREIGN KEY (`provider_id`) REFERENCES `PERSON` (`id`) ON UPDATE CASCADE,
  CONSTRAINT `invoice_taxi_id_fk` FOREIGN KEY (`taxi_id`) REFERENCES `TAXI` (`id`) ON UPDATE CASCADE,
  CONSTRAINT `invoice_user_created_by_id_fk` FOREIGN KEY (`created_by`) REFERENCES `CAR_DRIVER_USER` (`id`) ON UPDATE CASCADE,
  CONSTRAINT `invoice_user_last_updated_by_id_fk` FOREIGN KEY (`last_updated_by`) REFERENCES `CAR_DRIVER_USER` (`id`) ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
-- --------------------------------------------------------
--
-- Estructura de tabla para la tabla `INVOICE_HAS_PRODUCT`
--
CREATE TABLE `INVOICE_HAS_PRODUCT` (
  `invoice_id` char(36) NOT NULL,
  `product_id` char(36) NOT NULL,
  `price` float NOT NULL,
  `quantity` int(11) NOT NULL,
  `price_menudeo` float default 0
  `created_by` char(36) NOT NULL,
  `created_on` datetime DEFAULT CURRENT_TIMESTAMP,
  `last_updated_by` char(36) DEFAULT NULL,
  `last_updated_on` datetime DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`invoice_id`,`product_id`),
  KEY `invoice_has_product_product_if_fk` (`product_id`),
  KEY `invoice_has_product_user_created_by_id_fk` (`created_by`),
  KEY `invoice_has_product_user_last_updated_by_id_fk` (`last_updated_by`),
  CONSTRAINT `invoice_has_product_invoice_id_fk` FOREIGN KEY (`invoice_id`) REFERENCES `INVOICE` (`id`) ON UPDATE CASCADE,
  CONSTRAINT `invoice_has_product_product_if_fk` FOREIGN KEY (`product_id`) REFERENCES `PRODUCT` (`id`) ON UPDATE CASCADE,
  CONSTRAINT `invoice_has_product_user_created_by_id_fk` FOREIGN KEY (`created_by`) REFERENCES `CAR_DRIVER_USER` (`id`) ON UPDATE CASCADE,
  CONSTRAINT `invoice_has_product_user_last_updated_by_id_fk` FOREIGN KEY (`last_updated_by`) REFERENCES `CAR_DRIVER_USER` (`id`) ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
-- --------------------------------------------------------
--
-- Estructura de tabla para la tabla `PRODUCT_INVOICE_BK`
--
CREATE TABLE `PRODUCT_INVOICE_BK` (
  `id` char(36) NOT NULL,
  `invoice_id` char(36) NOT NULL,
  `product_id` char(36) NOT NULL,
  `product_amount_bk` double DEFAULT NULL,
  `product_quantity_bk` int(11) DEFAULT NULL,
  `created_by` char(36) NOT NULL,
  `created_on` datetime DEFAULT CURRENT_TIMESTAMP,
  `last_updated_by` char(36) DEFAULT NULL,
  `last_updated_on` datetime DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  KEY `product_invoice_bk_invoice_id_fk` (`invoice_id`),
  KEY `product_invoice_bk_product_id_fk` (`product_id`),
  KEY `product_invoice_bk_user_created_by_id_fk` (`created_by`),
  KEY `product_invoice_bk_user_last_updated_by_id_fk` (`last_updated_by`),
  CONSTRAINT `product_invoice_bk_invoice_id_fk` FOREIGN KEY (`invoice_id`) REFERENCES `INVOICE` (`id`) ON UPDATE CASCADE,
  CONSTRAINT `product_invoice_bk_product_id_fk` FOREIGN KEY (`product_id`) REFERENCES `PRODUCT` (`id`) ON UPDATE CASCADE,
  CONSTRAINT `product_invoice_bk_user_created_by_id_fk` FOREIGN KEY (`created_by`) REFERENCES `CAR_DRIVER_USER` (`id`) ON UPDATE CASCADE,
  CONSTRAINT `product_invoice_bk_user_last_updated_by_id_fk` FOREIGN KEY (`last_updated_by`) REFERENCES `CAR_DRIVER_USER` (`id`) ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
-- --------------------------------------------------------
--
-- Estructura de tabla para la tabla `SALE`
--
CREATE TABLE `SALE` (
  `id` char(36) NOT NULL,
  `pay_day` double NOT NULL,
  `pay_saving` double NOT NULL,
  `pay_date` datetime DEFAULT CURRENT_TIMESTAMP,
  `description` varchar(200) DEFAULT NULL,
  `accident` double DEFAULT NULL,
  `pay_day_as_spected` double DEFAULT 0.0,
  `pay_saving_as_spected` double DEFAULT 0.0,
  `accident_as_spected` double DEFAULT 0.0,
  `total_to_pay` double DEFAULT 0.0,
  `active` tinyint NOT NULL,
  `person_id` char(36) NOT NULL,
  `taxi_id` char(36) DEFAULT NULL,
  `transfer` enum('ENEBLED','DISABLED') default 'DISABLED';
  `created_by` char(36) NOT NULL,
  `created_on` datetime DEFAULT CURRENT_TIMESTAMP,
  `last_updated_by` char(36) DEFAULT NULL,
  `last_updated_on` datetime DEFAULT CURRENT_TIMESTAMP,
  `garage` tinyint default 0,
  PRIMARY KEY (`id`),
  KEY `sale_user_created_by_id_fk` (`created_by`),
  KEY `sale_user_last_updated_by_id_fk` (`last_updated_by`),
  KEY `sale_person_id_fk` (`person_id`),
  KEY `sale_taxi_id_fk` (`taxi_id`),
  CONSTRAINT `sale_person_id_fk` FOREIGN KEY (`person_id`) REFERENCES `PERSON` (`id`) ON UPDATE CASCADE,
  CONSTRAINT `sale_taxi_id_fk` FOREIGN KEY (`taxi_id`) REFERENCES `TAXI` (`id`) ON UPDATE CASCADE,
  CONSTRAINT `sale_user_created_by_id_fk` FOREIGN KEY (`created_by`) REFERENCES `CAR_DRIVER_USER` (`id`) ON UPDATE CASCADE,
  CONSTRAINT `sale_user_last_updated_by_id_fk` FOREIGN KEY (`last_updated_by`) REFERENCES `CAR_DRIVER_USER` (`id`) ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
-- --------------------------------------------------------
--
-- Estructura de tabla para la tabla `SANCTION`
--
CREATE TABLE `SANCTION` (
  `id` char(36) NOT NULL,
  `payment` double NOT NULL,
  `payment_date` datetime DEFAULT CURRENT_TIMESTAMP,
  `description` varchar(200) DEFAULT NULL,
  `sanction_type` enum('SANCTION','DISPOSITION','ACCIDENT','ADD','FEE','ESTUDIO','RETORNO_EFECTIVO','DISPOSICION_INGRESO','GASTO_SIN_AFECTAR','INGRESO_SINIESTRO','ABONO_DEUDA','DISPOSICION_FINIQUITO', 'AGREGAR_DEUDA') NOT NULL,
  `driver_id` char(36) NOT NULL,
  `active` tinyint default true;
  `created_by` char(36) NOT NULL,
  `created_on` datetime DEFAULT CURRENT_TIMESTAMP,
  `last_updated_by` char(36) DEFAULT NULL,
  `last_updated_on` datetime DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  KEY `sanction_person_id_fk` (`driver_id`),
  KEY `sanction_user_created_by_id_fk` (`created_by`),
  KEY `sanction_user_last_updated_by_id_fk` (`last_updated_by`),
  CONSTRAINT `sanction_person_id_fk` FOREIGN KEY (`driver_id`) REFERENCES `PERSON` (`id`) ON UPDATE CASCADE,
  CONSTRAINT `sanction_user_created_by_id_fk` FOREIGN KEY (`created_by`) REFERENCES `CAR_DRIVER_USER` (`id`) ON UPDATE CASCADE,
  CONSTRAINT `sanction_user_last_updated_by_id_fk` FOREIGN KEY (`last_updated_by`) REFERENCES `CAR_DRIVER_USER` (`id`) ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

CREATE TABLE `LOCATION` (
  `id` char(36) NOT NULL,
  `name` varchar(200) NOT NULL,
  `active` tinyint default true,
  `created_by` char(36) NOT NULL,
  `created_on` datetime DEFAULT CURRENT_TIMESTAMP,
  `last_updated_by` char(36) DEFAULT NULL,
  `last_updated_on` datetime DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE KEY `location_name_uk` (`name`),
  KEY `location_user_created_by_id_fk` (`created_by`),
  KEY `location_user_last_updated_by_id_fk` (`last_updated_by`),
  CONSTRAINT `location_user_created_by_id_fk` FOREIGN KEY (`created_by`) REFERENCES `CAR_DRIVER_USER` (`id`) ON UPDATE CASCADE,
  CONSTRAINT `location_user_last_updated_by_id_fk` FOREIGN KEY (`last_updated_by`) REFERENCES `CAR_DRIVER_USER` (`id`) ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
-- --------------------------------------------------------
--
-- Estructura de tabla para la tabla `SERVICE_ORDER`
--
CREATE TABLE `SERVICE_ORDER` (
  `id` char(36) NOT NULL,
  `name` varchar(14) NOT NULL,
  `name_number` int(11) NOT NULL,
  `km` float DEFAULT NULL,
  `subtotal` float DEFAULT NULL,
  `iva` float DEFAULT NULL,
  `total` float DEFAULT NULL,
  `service_order_type` enum('PENDING','APPROVED','PROGRESS','FINISHED','EMPTY_STOCK','PASE_TALLER'),
  `payment_method_type` enum('CREDIT_CARD','DEBIT_CARD','CASH','EMPTY') NOT NULL,
  `active` tinyint NOT NULL,
  `pending_date` datetime DEFAULT NULL,
  `approved_date` datetime DEFAULT NULL,
  `progress_date` datetime DEFAULT NULL,
  `finish_date` datetime DEFAULT NULL,
  `description` varchar(200) DEFAULT NULL,
  `taxi_id` char(36) NOT NULL,
  `km_maintenance` float default 0,
  `pase_taller_date` datetime default null,
  `taller_externo` enum('ENEBLED','DISABLED') default 'DISABLED',
  `created_by` char(36) NOT NULL,
  `created_on` datetime DEFAULT CURRENT_TIMESTAMP,
  `last_updated_by` char(36) DEFAULT NULL,
  `last_updated_on` datetime DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE KEY `service_order_uk` (`name`),
  KEY `service_order_taxi_id_fk` (`taxi_id`),
  KEY `service_order_user_created_by_id_fk` (`created_by`),
  KEY `service_order_user_last_updated_by_id_fk` (`last_updated_by`),
  CONSTRAINT `service_order_taxi_id_fk` FOREIGN KEY (`taxi_id`) REFERENCES `TAXI` (`id`) ON UPDATE CASCADE,
  CONSTRAINT `service_order_user_created_by_id_fk` FOREIGN KEY (`created_by`) REFERENCES `CAR_DRIVER_USER` (`id`) ON UPDATE CASCADE,
  CONSTRAINT `service_order_user_last_updated_by_id_fk` FOREIGN KEY (`last_updated_by`) REFERENCES `CAR_DRIVER_USER` (`id`) ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
-- --------------------------------------------------------
--
-- Estructura de tabla para la tabla `SERVICE_ORDER_HAS_PRODUCT`
--
CREATE TABLE `SERVICE_ORDER_HAS_PRODUCT` (
  `service_order_id` char(36) NOT NULL,
  `product_id` char(36) NOT NULL,
  `quantity` int(11) NOT NULL,
  `price` float NOT NULL,
  `total` float NOT NULL,
  `mayoreo` float default 0, 
  `cobrado` ENUM('ENEBLED', 'DISABLED') default 'DISABLED',
  `created_by` char(36) NOT NULL,
  `created_on` datetime DEFAULT CURRENT_TIMESTAMP,
  `last_updated_by` char(36) DEFAULT NULL,
  `last_updated_on` datetime DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`service_order_id`,`product_id`),
  KEY `service_order_has_product_product_id_fk` (`product_id`),
  KEY `service_order_has_product_user_created_by_id_fk` (`created_by`),
  KEY `service_order_has_product_user_last_updated_by_id_fk` (`last_updated_by`),
  CONSTRAINT `service_order_has_product_product_id_fk` FOREIGN KEY (`product_id`) REFERENCES `PRODUCT` (`id`) ON UPDATE CASCADE,
  CONSTRAINT `service_order_has_product_service_order_id_fk` FOREIGN KEY (`service_order_id`) REFERENCES `SERVICE_ORDER` (`id`) ON UPDATE CASCADE,
  CONSTRAINT `service_order_has_product_user_created_by_id_fk` FOREIGN KEY (`created_by`) REFERENCES `CAR_DRIVER_USER` (`id`) ON UPDATE CASCADE,
  CONSTRAINT `service_order_has_product_user_last_updated_by_id_fk` FOREIGN KEY (`last_updated_by`) REFERENCES `CAR_DRIVER_USER` (`id`) ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
-- --------------------------------------------------------------
--
-- Estructura de tabla para la tabla `SERVICE_ORDER_NOTIFICATION`
--
CREATE TABLE `SERVICE_ORDER_NOTIFICATION` (
  `id` char(36) NOT NULL,
  `title` varchar(50) NOT NULL,
  `message` varchar(250) NOT NULL,
  `created_on` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `service_order_action_type` enum('CREATED','APPROVED','PROGRESS','FINISHED','REJECTED') NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
-- -------------------------------------------------------------------------
--
-- Estructura de tabla para la tabla `SERVICE_ORDER_NOTIFICATION_DELIVERY_TO`
--
CREATE TABLE `SERVICE_ORDER_NOTIFICATION_DELIVERY_TO` (
  `id` char(36) NOT NULL,
  `id_notification` char(36) NOT NULL,
  `id_user` char(36) NOT NULL,
  `delivery_status_type` enum('ENABLED','DISABLED','DELETED') NOT NULL,
  PRIMARY KEY (`id`),
  KEY `srvc_ordr_ntfc_dlvr_to_notification_fk` (`id_notification`),
  KEY `srvc_ordr_ntfc_dlvr_to_user_fk` (`id_user`),
  CONSTRAINT `srvc_ordr_ntfc_dlvr_to_notification_fk` FOREIGN KEY (`id_notification`) REFERENCES `SERVICE_ORDER_NOTIFICATION` (`id`) ON UPDATE CASCADE,
  CONSTRAINT `srvc_ordr_ntfc_dlvr_to_user_fk` FOREIGN KEY (`id_user`) REFERENCES `CAR_DRIVER_USER` (`id`) ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
-- -------------------------------------------------------------------------
--
-- Estructura de tabla para la tabla `STOP_NOTIFICATION`
--
CREATE TABLE `STOP_NOTIFICATION` (
  `id` char(36) NOT NULL,
  `id_user` char(36) NOT NULL,
  `stop_notification_type` enum('SERVICE_ORDER') NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `stop_notication_uk` (`id_user`,`stop_notification_type`),
  CONSTRAINT `stop_notification_user_fk` FOREIGN KEY (`id_user`) REFERENCES `CAR_DRIVER_USER` (`id`) ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
-- -------------------------------------------------------------------------
--
-- Estructura de tabla para la tabla `TEST`
--
CREATE TABLE `TEST` (
  `id` char(36) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
-- -------------------------------------------------------------------------
--
-- Estructura de tabla para la tabla `TO_COLLECT`
--
CREATE TABLE `TO_COLLECT` (
  `id` char(36) NOT NULL,
  `to_collect` date DEFAULT NULL,
  `complete` tinyint DEFAULT '0',
  `total` int(11) DEFAULT '0',
  `driver_id` char(36) NOT NULL,
  `created_by` char(36) NOT NULL,
  `created_on` datetime DEFAULT CURRENT_TIMESTAMP,
  `last_updated_by` char(36) DEFAULT NULL,
  `last_updated_on` datetime DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  KEY `to_collect_driver_id_fk` (`driver_id`),
  KEY `to_collect_user_created_by_id_fk` (`created_by`),
  KEY `to_collect_user_last_updated_by_id_fk` (`last_updated_by`),
  CONSTRAINT `to_collect_driver_id_fk` FOREIGN KEY (`driver_id`) REFERENCES `PERSON` (`id`) ON UPDATE CASCADE,
  CONSTRAINT `to_collect_user_created_by_id_fk` FOREIGN KEY (`created_by`) REFERENCES `CAR_DRIVER_USER` (`id`) ON UPDATE CASCADE,
  CONSTRAINT `to_collect_user_last_updated_by_id_fk` FOREIGN KEY (`last_updated_by`) REFERENCES `CAR_DRIVER_USER` (`id`) ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
-- --------------------------------------------------------------------
-- 
-- Estructura para la tabla `EXPENSE_COMPANY`
--
-- Cobros/Gastos de la compañia.
-- 
CREATE TABLE `EXPENSE_COMPANY`(
    `id` char(36) NOT NULL,
    `amount` numeric(8,2) NOT NULL,
    `description` varchar(200),
    `expense_company_type` ENUM('PAYMENT_IN', 'PAYMENT_OUT') NOT NULL,
    `active_status` ENUM('ENEBLED', 'DISABLED') NOT NULL DEFAULT 'ENEBLED',
    `created_by` char(36) NOT NULL,
    `created_on` datetime DEFAULT CURRENT_TIMESTAMP,
    `last_updated_by` char(36) DEFAULT NULL,
    `last_updated_on` datetime DEFAULT CURRENT_TIMESTAMP,
    CONSTRAINT `expense_company_pk` PRIMARY KEY (`id`),
    CONSTRAINT `expense_company_user_created_by_id_fk` FOREIGN KEY (`created_by`) REFERENCES `CAR_DRIVER_USER` (`id`) ON UPDATE CASCADE,
    CONSTRAINT `expense_company_user_last_updated_by_id_fk` FOREIGN KEY (`last_updated_by`) REFERENCES `CAR_DRIVER_USER` (`id`) ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
-- --------------------------------------------------------------------
-- 
-- Estructura para la tabla `STABLE_GENERAL_BOX`
--
-- Totales de la caja de CAR DRIVER.
-- 
CREATE TABLE `STABLE_GENERAL_BOX`(
    `id` char(36) NOT NULL,
    `total_general_box` numeric(8,2) NOT NULL,
    `total_envelope` numeric(8,2) NOT NULL,
    `total_bank_note` numeric(8,2) NOT NULL,
    `total_coin` numeric(8,2) NOT NULL,
    `total_stable` numeric(8,2) NOT NULL,
    `active_status` ENUM('ENEBLED', 'DISABLED') NOT NULL DEFAULT 'ENEBLED',
    `description` varchar(200),
    `created_by` char(36) NOT NULL,
    `created_on` datetime DEFAULT CURRENT_TIMESTAMP,
    `last_updated_by` char(36) DEFAULT NULL,
    `last_updated_on` datetime DEFAULT CURRENT_TIMESTAMP,
    CONSTRAINT `stable_general_box_pk` PRIMARY KEY (`id`),
    CONSTRAINT `stable_general_box_user_created_by_id_fk` FOREIGN KEY (`created_by`) REFERENCES `CAR_DRIVER_USER` (`id`) ON UPDATE CASCADE,
    CONSTRAINT `stable_general_box_user_last_updated_by_id_fk` FOREIGN KEY (`last_updated_by`) REFERENCES `CAR_DRIVER_USER` (`id`) ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
-- --------------------------------------------------------------------
-- 
-- Estructura para la tabla `STABLE_SMALL_BOX`
--
-- Totales de la caja chica de CAR DRIVER.
-- 
CREATE TABLE `STABLE_SMALL_BOX`(
    `id` char(36) NOT NULL,
    `total_small_box` numeric(8,2) NOT NULL,
    `total_envelope` numeric(8,2) NOT NULL,
    `total_bank_note` numeric(8,2) NOT NULL,
    `total_coin` numeric(8,2) NOT NULL,
    `total_stable` numeric(8,2) NOT NULL,
    `active_status` ENUM('ENEBLED', 'DISABLED') NOT NULL DEFAULT 'ENEBLED',
    `description` varchar(200),
    `created_by` char(36) NOT NULL,
    `created_on` datetime DEFAULT CURRENT_TIMESTAMP,
    `last_updated_by` char(36) DEFAULT NULL,
    `last_updated_on` datetime DEFAULT CURRENT_TIMESTAMP,
    CONSTRAINT `stable_small_box_pk` PRIMARY KEY (`id`),
    CONSTRAINT `stable_small_box_user_created_by_id_fk` FOREIGN KEY (`created_by`) REFERENCES `CAR_DRIVER_USER` (`id`) ON UPDATE CASCADE,
    CONSTRAINT `stable_small_box_user_last_updated_by_id_fk` FOREIGN KEY (`last_updated_by`) REFERENCES `CAR_DRIVER_USER` (`id`) ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
-- --------------------------------------------------------------------
-- 
-- Estructura para la tabla `CLOSING_DAY`
--
-- Corte diario en CAR DRIVER.
-- 
CREATE TABLE `CLOSING_DAY`(
    `id` char(36) NOT NULL,
    `user_id` char(36) NOT NULL,
    `amount_paid` numeric(8,2) NOT NULL,
    `amount_expected` numeric(8,2) NOT NULL,
    `active_status` ENUM('ENEBLED', 'DISABLED') NOT NULL DEFAULT 'ENEBLED',
    `comments` varchar(200),
    `created_by` char(36) NOT NULL,
    `created_on` datetime DEFAULT CURRENT_TIMESTAMP,
    `last_updated_by` char(36) DEFAULT NULL,
    `last_updated_on` datetime DEFAULT CURRENT_TIMESTAMP,
    CONSTRAINT `closing_day_pk` PRIMARY KEY (`id`),
    CONSTRAINT `closing_day_user_id_fk` FOREIGN KEY (`user_id`) REFERENCES `CAR_DRIVER_USER` (`id`) ON UPDATE CASCADE,
    CONSTRAINT `closing_day_user_created_by_id_fk` FOREIGN KEY (`created_by`) REFERENCES `CAR_DRIVER_USER` (`id`) ON UPDATE CASCADE,
    CONSTRAINT `closing_day_user_last_updated_by_id_fk` FOREIGN KEY (`last_updated_by`) REFERENCES `CAR_DRIVER_USER` (`id`) ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
-- --------------------------------------------------------------------
-- 
-- Estructura para la tabla `CLOSING_DAY_DETAILS`
--
-- Detalles del corte diario en CAR DRIVER.
-- 
CREATE TABLE `CLOSING_DAY_DETAILS`(
    `id` char(36) NOT NULL,
    `closing_day_id` char(36) NOT NULL,
    `type` varchar(200) DEFAULT NULL,
    `amount` numeric(8,2) NOT NULL,
    `amount_saving` numeric(8,2) DEFAULT NULL,
    `amount_accident` numeric(8,2) DEFAULT NULL,
    `person_id` char(36),
    `comments` varchar(500) DEFAULT NULL,
    `created_by` char(36) NOT NULL,
    `created_on` datetime DEFAULT CURRENT_TIMESTAMP,
    `last_updated_by` char(36) DEFAULT NULL,
    `last_updated_on` datetime DEFAULT CURRENT_TIMESTAMP,
    CONSTRAINT `closing_day_details_pk` PRIMARY KEY (`id`),
    CONSTRAINT `closing_day_details_closing_id_fk` FOREIGN KEY (`closing_day_id`) REFERENCES `CLOSING_DAY` (`id`) ON UPDATE CASCADE,
    CONSTRAINT `closing_day_details_person_id_fk` FOREIGN KEY (`person_id`) REFERENCES `PERSON` (`id`) ON UPDATE CASCADE,
    CONSTRAINT `closing_day_details_user_created_by_id_fk` FOREIGN KEY (`created_by`) REFERENCES `CAR_DRIVER_USER` (`id`) ON UPDATE CASCADE,
    CONSTRAINT `closing_day_details_user_last_updated_by_id_fk` FOREIGN KEY (`last_updated_by`) REFERENCES `CAR_DRIVER_USER` (`id`) ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
-- --------------------------------------------------------
--
-- Estructura para la tabla `SYSTEM_PROPERTY`
--
CREATE TABLE `SYSTEM_PROPERTY` (
  `id` char(36) NOT NULL,
  `property_name` enum('FEE', 'LIMIT_KM', 'COMISION') NOT NULL,
  `property_value` varchar(250) NOT NULL,
  `created_by` char(36) NOT NULL,
  `created_on` datetime DEFAULT CURRENT_TIMESTAMP,
  `last_updated_by` char(36) DEFAULT NULL,
  `last_updated_on` datetime DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
-- --------------------------------------------------------
--
-- Estructura para la tabla `FEE_BY_DRIVER`
--
CREATE TABLE `FEE_BY_DRIVER` (
  `id` char(36) NOT NULL,
  `driver_id` char(36) NOT NULL,
  `fee_date` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `amount` double DEFAULT 0.0,
  `comments` varchar(250),
  `fee_status` enum('PAID','NO_PAID','DELETED') NOT NULL DEFAULT 'NO_PAID',
  `created_by` char(36) NOT NULL,
  `created_on` datetime DEFAULT CURRENT_TIMESTAMP,
  `last_updated_by` char(36) DEFAULT NULL,
  `last_updated_on` datetime DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  CONSTRAINT `fee_by_driver_person_id_fk` FOREIGN KEY (`driver_id`) REFERENCES `PERSON` (`id`) ON UPDATE CASCADE,
  CONSTRAINT `fee_by_driver_created_by_id_fk` FOREIGN KEY (`created_by`) REFERENCES `CAR_DRIVER_USER` (`id`) ON UPDATE CASCADE,
  CONSTRAINT `fee_by_driver_last_updated_by_id_fk` FOREIGN KEY (`last_updated_by`) REFERENCES `CAR_DRIVER_USER` (`id`) ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
-- --------------------------------------------------------
--
-- Estructura para la tabla `CAR_DRIVER_BITACORA`
--
CREATE TABLE `CAR_DRIVER_BITACORA`(
    `id` char(36) NOT NULL,
    `comments_user` varchar(300) NOT NULL,
    `action` varchar(50) NOT NULL,
    `description` varchar(300) NOT NULL,
    `name_user` varchar(200) NOT NULL,
    `created_by` char(36) NOT NULL,
    `created_on` datetime DEFAULT CURRENT_TIMESTAMP,
    `last_updated_by` char(36) DEFAULT NULL,
    `last_updated_on` datetime DEFAULT CURRENT_TIMESTAMP,
    CONSTRAINT `car_driver_bitacora_pk` PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
-- --------------------------------------------------------
--
-- Estructura para la tabla `TRANSFER_CAR_DRIVER`
--
CREATE TABLE `TRANSFER_CAR_DRIVER`(
    `id` char(36) NOT NULL,
    `id_user_transmitter` char(36) NOT NULL,
    `id_user_receiver` char(36) NOT NULL,
    `active_status` ENUM('ENEBLED', 'DISABLED') NOT NULL DEFAULT 'ENEBLED',
    `action_status` ENUM('PENDING', 'APPROVED','REJECTED') NOT NULL DEFAULT 'PENDING',
    `amount_to_transfer` double,
    `created_by` char(36) NOT NULL,
    `created_on` datetime DEFAULT CURRENT_TIMESTAMP,
    `last_updated_by` char(36) DEFAULT NULL,
    `last_updated_on` datetime DEFAULT CURRENT_TIMESTAMP,
    CONSTRAINT `apc_transfer_pk` PRIMARY KEY (`id`),
    CONSTRAINT `transfer_user_transmitter_to_user_fk` 
        FOREIGN KEY (`id_user_transmitter`) REFERENCES `CAR_DRIVER_USER`(`id`)
            ON UPDATE CASCADE
            ON DELETE RESTRICT,
    CONSTRAINT `transfer_user_receiver_to_user_fk` 
        FOREIGN KEY (`id_user_receiver`) REFERENCES `CAR_DRIVER_USER`(`id`)
            ON UPDATE CASCADE
            ON DELETE RESTRICT
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
-- --------------------------------------------------------
--
-- Estructura para la tabla `PAYMENT_CLIENT`
--
CREATE TABLE `PAYMENT_CLIENT`(
    `id` char(36) NOT NULL,
    `id_taxi` char(36) NOT NULL,
    `active_status` ENUM('ENEBLED', 'DISABLED') NOT NULL DEFAULT 'ENEBLED',
    `action_status` ENUM('PENDING', 'APPROVED','REJECTED') NOT NULL DEFAULT 'PENDING',
    `amount_to_pay` double,
    `discount` double,
    `comments_discount` varchar(500),
    `increase` double,
    `comments_increase` varchar(500),
    `total_payment` double,
    `init_date` datetime,
    `end_date` datetime,
    `next_payment_date` datetime,
    `comments` varchar(500),
    `dias_seguro_ventas` int default 0,
    `pago_seguro_ventas` float default 0,
    `total_contrato` float default 0,
    `incremento_contrato` float default 0,
    `descuento_contrato` float default 0,
    `abono_adeudo` double default 0,
    `created_by` char(36) NOT NULL,
    `created_on` datetime DEFAULT CURRENT_TIMESTAMP,
    `last_updated_by` char(36) DEFAULT NULL,
    `last_updated_on` datetime DEFAULT CURRENT_TIMESTAMP,
    CONSTRAINT `apc_payment_client_pk` PRIMARY KEY (`id`),
    CONSTRAINT `payment_client_taxi_to_taxi_fk` 
        FOREIGN KEY (`id_taxi`) REFERENCES `TAXI`(`id`)
            ON UPDATE CASCADE
            ON DELETE RESTRICT
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
-- --------------------------------------------------------
--
-- Estructura para la tabla `RELEASE_RESOURCE`
--
CREATE TABLE `RELEASE_RESOURCE` (
  `id` char(36) NOT NULL,
  `driver_id` char(36) DEFAULT NULL,
  `taxi_id` char(36) DEFAULT NULL,
  `start_date` datetime NOT NULL,
  `end_date` datetime DEFAULT NULL,
  `active` enum('ENABLED','DISABLED') NOT NULL DEFAULT 'ENABLED',
  `created_by` char(36) NOT NULL,
  `created_on` datetime DEFAULT CURRENT_TIMESTAMP,
  `last_updated_by` char(36) DEFAULT NULL,
  `last_updated_on` datetime DEFAULT CURRENT_TIMESTAMP,
   CONSTRAINT `resource_person_id_pk` PRIMARY KEY (`id`),
   CONSTRAINT `release_resource_person_id_fk` 
        FOREIGN KEY (`driver_id`) REFERENCES `PERSON`(`id`)
            ON UPDATE CASCADE
            ON DELETE RESTRICT,
    CONSTRAINT `release_resource_taxi_id_fk` 
        FOREIGN KEY (`taxi_id`) REFERENCES `TAXI`(`id`)
            ON UPDATE CASCADE
            ON DELETE RESTRICT,
    CONSTRAINT `release_resource_created_by_id_fk` 
        FOREIGN KEY (`created_by`) REFERENCES `CAR_DRIVER_USER` (`id`) 
            ON UPDATE CASCADE
            ON DELETE RESTRICT,
    CONSTRAINT `release_resource_last_updated_by_id_fk` 
        FOREIGN KEY (`last_updated_by`) REFERENCES `CAR_DRIVER_USER` (`id`) 
            ON UPDATE CASCADE
            ON DELETE RESTRICT
)ENGINE=InnoDB DEFAULT CHARSET=latin1;

CREATE TABLE `HISTORY_UPDATE_KM`(
    `id` char(36) NOT NULL,
    `id_taxi` char(36) NOT NULL,
    `active_status` ENUM('ENEBLED', 'DISABLED') NOT NULL DEFAULT 'ENEBLED',
    `last_km` float,
    `new_km` float,
    `module` varchar(500),
    `comments` varchar(500),
    `created_by` char(36) NOT NULL,
    `created_on` datetime DEFAULT CURRENT_TIMESTAMP,
    `last_updated_by` char(36) DEFAULT NULL,
    `last_updated_on` datetime DEFAULT CURRENT_TIMESTAMP,
    CONSTRAINT `history_update_km_pk` PRIMARY KEY (`id`),
    CONSTRAINT `history_update_km_taxi_to_taxi_fk` 
        FOREIGN KEY (`id_taxi`) REFERENCES `TAXI`(`id`)
            ON UPDATE CASCADE
            ON DELETE RESTRICT
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
-- --------------------------------------------------------
--
-- Estructura para la función `buildCompleteName`
--
DELIMITER //
CREATE FUNCTION buildCompleteName(
    first_name varchar(25),
    second_name varchar(25) ,
    last_name varchar(25) ,
    middle_name varchar(25))
    RETURNS VARCHAR (103) DETERMINISTIC
    RETURN 
        CONCAT(
            CASE
                WHEN first_name IS NOT NULL AND first_name != ''
                THEN CONCAT(SUBSTR(UPPER(first_name), 1, 1),SUBSTR(LOWER(first_name), 2), ' ')
                ELSE ''
            END,
            CASE
                WHEN second_name IS NOT NULL AND second_name != ''
                THEN CONCAT(SUBSTR(UPPER(second_name), 1, 1),SUBSTR(LOWER(second_name), 2), ' ')
                ELSE ''
            END, 
            CASE
                WHEN last_name IS NOT NULL AND last_name != ''
                THEN CONCAT(SUBSTR(UPPER(last_name), 1, 1),SUBSTR(LOWER(last_name), 2), ' ')
                ELSE ''
            END,
            CASE
                WHEN middle_name IS NOT NULL AND middle_name != ''
                THEN CONCAT(SUBSTR(UPPER(middle_name), 1, 1),SUBSTR(LOWER(middle_name), 2))
                ELSE ''
            END
        )
//
DELIMITER ;

ALTER TABLE SANCTION
MODIFY COLUMN sanction_type enum('SANCTION','DISPOSITION','ACCIDENT','ADD','FEE','ESTUDIO','RETORNO_EFECTIVO','DISPOSICION_INGRESO','GASTO_SIN_AFECTAR','INGRESO_SINIESTRO','ABONO_DEUDA','DISPOSICION_FINIQUITO')

ALTER TABLE SYSTEM_PROPERTY
MODIFY COLUMN property_name enum('FEE', 'LIMIT_KM');

INSERT INTO SYSTEM_PROPERTY VALUES ('4a2111b6-331d-33ec-246c-91ccdff0fe72', 'LIMIT_KM' , 10000, '09ea84b8-b9d1-4a71-adfe-db653b2a9982', 
CURDATE(), NULL, NULL);

CREATE TABLE `AVISOS_CHOFER`(
    `id` char(36) NOT NULL,
    `id_person` char(36) NOT NULL,
    `active_status` ENUM('ENEBLED', 'DISABLED') NOT NULL DEFAULT 'ENEBLED',
    `comments` varchar(500),
    `created_by` char(36) NOT NULL,
    `created_on` datetime DEFAULT CURRENT_TIMESTAMP,
    `last_updated_by` char(36) DEFAULT NULL,
    `last_updated_on` datetime DEFAULT CURRENT_TIMESTAMP,
    CONSTRAINT `avisos_chofer_pk` PRIMARY KEY (`id`),
    CONSTRAINT `avisos_chofer_person_to_person_fk` 
        FOREIGN KEY (`id_person`) REFERENCES `PERSON`(`id`)
            ON UPDATE CASCADE
            ON DELETE RESTRICT
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

CREATE TABLE `AVISOS_CHOFER`(
    `id` char(36) NOT NULL,
    `id_person` char(36) NOT NULL,
    `active_status` ENUM('ENEBLED', 'DISABLED') NOT NULL DEFAULT 'ENEBLED',
    `comments` varchar(500),
    `created_by` char(36) NOT NULL,
    `created_on` datetime DEFAULT CURRENT_TIMESTAMP,
    `last_updated_by` char(36) DEFAULT NULL,
    `last_updated_on` datetime DEFAULT CURRENT_TIMESTAMP,
    CONSTRAINT `avisos_chofer_pk` PRIMARY KEY (`id`),
    CONSTRAINT `avisos_chofer_person_to_person_fk` 
        FOREIGN KEY (`id_person`) REFERENCES `PERSON`(`id`)
            ON UPDATE CASCADE
            ON DELETE RESTRICT
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

CREATE TABLE `STOCKTAKING_MOVEMENTS`(
    `id` char(36) NOT NULL,
    `id_product` char(36) NOT NULL,
    `active_status` ENUM('ENEBLED', 'DISABLED') NOT NULL DEFAULT 'ENEBLED',
    `type_movement` ENUM('IN', 'OUT') NOT NULL,
    `quantity` int,
    `comments` varchar(500),
    `id_location` varchar(50),
    `created_by` char(36) NOT NULL,
    `created_on` datetime DEFAULT CURRENT_TIMESTAMP,
    `last_updated_by` char(36) DEFAULT NULL,
    `last_updated_on` datetime DEFAULT CURRENT_TIMESTAMP,
    CONSTRAINT `stocktaking_movements_pk` PRIMARY KEY (`id`),
    CONSTRAINT `stocktaking_movements_id_product_to_product_fk` 
        FOREIGN KEY (`id_product`) REFERENCES `PRODUCT`(`id`)
            ON UPDATE CASCADE
            ON DELETE RESTRICT
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

CREATE TABLE `FINANCING_DRIVER` (
  `id` char(36) NOT NULL,
  `driver_id` varchar(20) NOT NULL,
  `last_debit` double DEFAULT 0,
  `month_debit` double DEFAULT 0,
  `increase` double DEFAULT 0,
  `discount` double DEFAULT 0,
  `comments_discount` varchar(500),
  `comments_increase` varchar(500),
  `payment` double DEFAULT 0,
  `actually_debit` double DEFAULT 0,
  `comments` varchar(500),
  `active_status` enum('ENEBLED','DISABLED'),
  `created_by` char(36) NOT NULL,
  `created_on` datetime DEFAULT CURRENT_TIMESTAMP,
  `last_updated_by` char(36) DEFAULT NULL,
  `last_updated_on` datetime DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  CONSTRAINT `driver_id_person_id_fk` FOREIGN KEY (`driver_id`) REFERENCES `PERSON` (`id`) ON UPDATE CASCADE
  ) ENGINE=InnoDB DEFAULT CHARSET=latin1;

CREATE TABLE `EMPLOYEE_SANCTION` (
  `id` char(36) NOT NULL,
  `payment` double NOT NULL,
  `description` varchar(200) DEFAULT NULL,
  `employee_sanction_type` enum('ABONO_DEUDA','AGREGAR_DEUDA') NOT NULL,
  `employee_id` char(36) NOT NULL,
  `active` tinyint default true,
  `created_by` char(36) NOT NULL,
  `created_on` datetime DEFAULT CURRENT_TIMESTAMP,
  `last_updated_by` char(36) DEFAULT NULL,
  `last_updated_on` datetime DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  KEY `employee_sanction_person_id_fk` (`employee_id`),
  KEY `employee_sanction_user_created_by_id_fk` (`created_by`),
  KEY `employee_sanction_user_last_updated_by_id_fk` (`last_updated_by`),
  CONSTRAINT `employee_sanction_person_id_fk` FOREIGN KEY (`employee_id`) REFERENCES `PERSON` (`id`) ON UPDATE CASCADE,
  CONSTRAINT `employee_sanction_user_created_by_id_fk` FOREIGN KEY (`created_by`) REFERENCES `CAR_DRIVER_USER` (`id`) ON UPDATE CASCADE,
  CONSTRAINT `employee_sanction_user_last_updated_by_id_fk` FOREIGN KEY (`last_updated_by`) REFERENCES `CAR_DRIVER_USER` (`id`) ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

CREATE TABLE `RETURN_INVOICE` (
  `id` char(36) NOT NULL,
  `return_payment` double NOT NULL,
  `description` varchar(200) DEFAULT NULL,
  `invoice_id` char(36) NOT NULL,
  `active` tinyint default true,
  `id_location` char(36) NOT NULL,
  `created_by` char(36) NOT NULL,
  `created_on` datetime DEFAULT CURRENT_TIMESTAMP,
  `last_updated_by` char(36) DEFAULT NULL,
  `last_updated_on` datetime DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  KEY `return_invoice_invoice_id_fk` (`invoice_id`),
  KEY `return_invoice_location_id_fk` (`id_location`),
  KEY `return_invoice_user_created_by_id_fk` (`created_by`),
  KEY `return_invoice_user_last_updated_by_id_fk` (`last_updated_by`),
  CONSTRAINT `return_invoice_invoice_id_fk` FOREIGN KEY (`invoice_id`) REFERENCES `INVOICE` (`id`) ON UPDATE CASCADE,
  CONSTRAINT `return_invoice_user_created_by_id_fk` FOREIGN KEY (`created_by`) REFERENCES `CAR_DRIVER_USER` (`id`) ON UPDATE CASCADE,
  CONSTRAINT `return_invoice_user_last_updated_by_id_fk` FOREIGN KEY (`last_updated_by`) REFERENCES `CAR_DRIVER_USER` (`id`) ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

CREATE TABLE `TRANSFER_STOCKTAKING` (
  `id` char(36) NOT NULL,
  `comment` varchar(200) DEFAULT NULL,
  `transfer_location_id` char(36) NOT NULL,
  `status_type` enum('PENDING','APPROVED'),
  `active` tinyint default true,
  `id_location` char(36) NOT NULL,
  `created_by` char(36) NOT NULL,
  `created_on` datetime DEFAULT CURRENT_TIMESTAMP,
  `last_updated_by` char(36) DEFAULT NULL,
  `last_updated_on` datetime DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  KEY `transfer_stocktaking_transfer_location_id_fk` (`transfer_location_id`),
  KEY `transfer_stocktaking_location_id_fk` (`id_location`),
  KEY `transfer_stocktaking_user_created_by_id_fk` (`created_by`),
  KEY `transfer_stocktaking_user_last_updated_by_id_fk` (`last_updated_by`),
  CONSTRAINT `transfer_stocktaking_transfer_location_id_fk` FOREIGN KEY (`transfer_location_id`) REFERENCES `LOCATION` (`id`) ON UPDATE CASCADE,
  CONSTRAINT `transfer_stocktaking_user_created_by_id_fk` FOREIGN KEY (`created_by`) REFERENCES `CAR_DRIVER_USER` (`id`) ON UPDATE CASCADE,
  CONSTRAINT `transfer_stocktaking_user_last_updated_by_id_fk` FOREIGN KEY (`last_updated_by`) REFERENCES `CAR_DRIVER_USER` (`id`) ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

CREATE TABLE `TRANSFER_STOCKTAKING_HAS_PRODUCT` (
`transfer_stocktaking_id` char(36) NOT NULL,
`product_id` char(36) NOT NULL,
`quantity` int default 0,
`price` float default 0,
`created_by` char(36) NOT NULL,
`created_on` datetime DEFAULT CURRENT_TIMESTAMP,
`last_updated_by` char(36) DEFAULT NULL,
`last_updated_on` datetime DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`transfer_stocktaking_id`,`product_id`),
KEY `transfer_stocktaking_has_product_product_if_fk` (`product_id`),
KEY `transfer_stocktaking_has_product_user_created_by_id_fk` (`created_by`),
KEY `transfer_stocktaking_has_product_user_last_updated_by_id_fk` (`last_updated_by`),
CONSTRAINT `transfer_stocktaking_has_product_transfer_stocktaking_id_fk` FOREIGN KEY (`transfer_stocktaking_id`) REFERENCES `TRANSFER_STOCKTAKING` (`id`) ON UPDATE CASCADE,
CONSTRAINT `transfer_stocktaking_has_product_product_if_fk` FOREIGN KEY (`product_id`) REFERENCES `PRODUCT` (`id`) ON UPDATE CASCADE,
CONSTRAINT `transfer_stocktaking_has_product_user_created_by_id_fk` FOREIGN KEY (`created_by`) REFERENCES `CAR_DRIVER_USER` (`id`) ON UPDATE CASCADE,
CONSTRAINT `transfer_stocktaking_has_product_user_last_updated_by_id_fk` FOREIGN KEY (`last_updated_by`) REFERENCES `CAR_DRIVER_USER` (`id`) ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;        

CREATE TABLE `TRANSFER_STOCKTAKING_HAS_PRODUCT_BK` (
`id` char(36) NOT NULL,
`transfer_stocktaking_id` char(36) NOT NULL,
`product_id` char(36) NOT NULL,
`quantity_bk` int default 0,
`price_bk` float default 0,
`created_by` char(36) NOT NULL,
`created_on` datetime DEFAULT CURRENT_TIMESTAMP,
`last_updated_by` char(36) DEFAULT NULL,
`last_updated_on` datetime DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
KEY `transfer_stocktaking_has_product_bk_product_if_fk` (`product_id`),
KEY `transfer_stocktaking_has_product_bk_user_created_by_id_fk` (`created_by`),
KEY `transfer_stocktaking_has_product_bk_user_last_updated_by_id_fk` (`last_updated_by`),
CONSTRAINT `transfer_stocktaking_has_product_bk_transfer_stocktaking_id_fk` FOREIGN KEY (`transfer_stocktaking_id`) REFERENCES `TRANSFER_STOCKTAKING` (`id`) ON UPDATE CASCADE,
CONSTRAINT `transfer_stocktaking_has_product_bk_product_if_fk` FOREIGN KEY (`product_id`) REFERENCES `PRODUCT` (`id`) ON UPDATE CASCADE,
CONSTRAINT `transfer_stocktaking_has_product_bk_user_created_by_id_fk` FOREIGN KEY (`created_by`) REFERENCES `CAR_DRIVER_USER` (`id`) ON UPDATE CASCADE,
CONSTRAINT `transfer_stocktaking_has_product_bk_user_last_updated_by_id_fk` FOREIGN KEY (`last_updated_by`) REFERENCES `CAR_DRIVER_USER` (`id`) ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;  

CREATE TABLE `CHANGE_AMOUNT_PRODUCT` (
  `id` char(36) NOT NULL,
  `comment` varchar(200) DEFAULT NULL,
  `product_id` char(36) NOT NULL,
  `status_type` enum('PENDING','APPROVED'),
  `active` tinyint default true,
  `id_location` char(36) NOT NULL,
  `created_by` char(36) NOT NULL,
  `created_on` datetime DEFAULT CURRENT_TIMESTAMP,
  `last_updated_by` char(36) DEFAULT NULL,
  `last_updated_on` datetime DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  KEY `change_amount_product_product_id_fk` (`product_id`),
  KEY `change_amount_product_location_id_fk` (`id_location`),
  KEY `change_amount_product_user_created_by_id_fk` (`created_by`),
  KEY `change_amount_product_user_last_updated_by_id_fk` (`last_updated_by`),
  CONSTRAINT `change_amount_product_product_id_fk` FOREIGN KEY (`product_id`) REFERENCES `PRODUCT` (`id`) ON UPDATE CASCADE,
  CONSTRAINT `change_amount_product_user_created_by_id_fk` FOREIGN KEY (`created_by`) REFERENCES `CAR_DRIVER_USER` (`id`) ON UPDATE CASCADE,
  CONSTRAINT `change_amount_product_user_last_updated_by_id_fk` FOREIGN KEY (`last_updated_by`) REFERENCES `CAR_DRIVER_USER` (`id`) ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;                          
  
CREATE TABLE `PAYROLL` (
  `id` char(36) NOT NULL,
  `user_id` char(36) NOT NULL,
  `salary` double NOT NULL,
  `imss` double NOT NULL,
  `bono1` double NOT NULL,
  `bono2` double NOT NULL,
  `bono3` double NOT NULL,
  `increase` double NOT NULL,
  `description_increase` varchar(200) DEFAULT NULL,
  `discount` double NOT NULL,
  `description_discount` varchar(200) DEFAULT NULL,
  `cajeado` double NOT NULL,
  `adelanto` double NOT NULL,
  `total` double NOT NULL,
  `abono` double NOT NULL,
  `active` tinyint DEFAULT '1',
  `id_location` varchar(50) DEFAULT NULL,
  `created_by` char(36) NOT NULL,
  `created_on` datetime DEFAULT CURRENT_TIMESTAMP,
  `last_updated_by` char(36) DEFAULT NULL,
  `last_updated_on` datetime DEFAULT CURRENT_TIMESTAMP,
  `init_date` date DEFAULT NULL,
  `end_date` date DEFAULT NULL,
  `saving` double DEFAULT '0',
  PRIMARY KEY (`id`),
  KEY `payroll_user_id_fk` (`user_id`),
  KEY `payroll_user_created_by_id_fk` (`created_by`),
  KEY `payroll_user_last_updated_by_id_fk` (`last_updated_by`),
  CONSTRAINT `payroll_user_created_by_id_fk` FOREIGN KEY (`created_by`) REFERENCES `CAR_DRIVER_USER` (`id`) ON UPDATE CASCADE,
  CONSTRAINT `payroll_user_id_fk` FOREIGN KEY (`user_id`) REFERENCES `CAR_DRIVER_USER` (`id`) ON UPDATE CASCADE,
  CONSTRAINT `payroll_user_last_updated_by_id_fk` FOREIGN KEY (`last_updated_by`) REFERENCES `CAR_DRIVER_USER` (`id`) ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

CREATE TABLE `ADELANTOS` (
  `id` char(36) NOT NULL,
  `comment` varchar(200) DEFAULT NULL,
  `amount` double DEFAULT NULL,
  `active` tinyint DEFAULT '1',
  `employee_id` char(36) NOT NULL,
  `id_location` char(36) NOT NULL,
  `created_by` char(36) NOT NULL,
  `created_on` datetime DEFAULT CURRENT_TIMESTAMP,
  `last_updated_by` char(36) DEFAULT NULL,
  `last_updated_on` datetime DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  KEY `adelantos_employee_id_fk` (`employee_id`),
  KEY `adelantos_location_id_fk` (`id_location`),
  KEY `adelantos_user_created_by_id_fk` (`created_by`),
  KEY `adelantos_user_last_updated_by_id_fk` (`last_updated_by`),
  CONSTRAINT `adelantos_employee_id_fk` FOREIGN KEY (`employee_id`) REFERENCES `PERSON` (`id`) ON UPDATE CASCADE,
  CONSTRAINT `adelantos_user_created_by_id_fk` FOREIGN KEY (`created_by`) REFERENCES `CAR_DRIVER_USER` (`id`) ON UPDATE CASCADE,
  CONSTRAINT `adelantos_user_last_updated_by_id_fk` FOREIGN KEY (`last_updated_by`) REFERENCES `CAR_DRIVER_USER` (`id`) ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

CREATE TABLE `AVISOS_CHOFER` (
  `id` char(36) NOT NULL,
  `id_person` char(36) NOT NULL,
  `active_status` enum('ENEBLED','DISABLED') NOT NULL DEFAULT 'ENEBLED',
  `comments` varchar(500) DEFAULT NULL,
  `created_by` char(36) NOT NULL,
  `created_on` datetime DEFAULT CURRENT_TIMESTAMP,
  `last_updated_by` char(36) DEFAULT NULL,
  `last_updated_on` datetime DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  KEY `avisos_chofer_person_to_person_fk` (`id_person`),
  CONSTRAINT `avisos_chofer_person_to_person_fk` FOREIGN KEY (`id_person`) REFERENCES `PERSON` (`id`) ON DELETE RESTRICT ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

CREATE TABLE `RETURN_INVOICE` (
  `id` char(36) NOT NULL,
  `return_payment` double NOT NULL,
  `description` varchar(200) DEFAULT NULL,
  `invoice_id` char(36) NOT NULL,
  `active` tinyint DEFAULT '1',
  `id_location` char(36) NOT NULL,
  `created_by` char(36) NOT NULL,
  `created_on` datetime DEFAULT CURRENT_TIMESTAMP,
  `last_updated_by` char(36) DEFAULT NULL,
  `last_updated_on` datetime DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  KEY `return_invoice_invoice_id_fk` (`invoice_id`),
  KEY `return_invoice_location_id_fk` (`id_location`),
  KEY `return_invoice_user_created_by_id_fk` (`created_by`),
  KEY `return_invoice_user_last_updated_by_id_fk` (`last_updated_by`),
  CONSTRAINT `return_invoice_invoice_id_fk` FOREIGN KEY (`invoice_id`) REFERENCES `INVOICE` (`id`) ON UPDATE CASCADE,
  CONSTRAINT `return_invoice_user_created_by_id_fk` FOREIGN KEY (`created_by`) REFERENCES `CAR_DRIVER_USER` (`id`) ON UPDATE CASCADE,
  CONSTRAINT `return_invoice_user_last_updated_by_id_fk` FOREIGN KEY (`last_updated_by`) REFERENCES `CAR_DRIVER_USER` (`id`) ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1; 

CREATE TABLE `TRANSFER_MONEY_LOCATION` (
  `id` char(36) NOT NULL,
  `amount` double NOT NULL,
  `description` varchar(200) DEFAULT NULL,
  `autorizacion` enum('ENEBLED','DISABLED') NOT NULL DEFAULT 'DISABLED',
  `location_send` char(36) NOT NULL,
  `location_receiver` char(36) NOT NULL,
  `active` tinyint DEFAULT '1',
  `id_location` char(36) NOT NULL,
  `created_by` char(36) NOT NULL,
  `created_on` datetime DEFAULT CURRENT_TIMESTAMP,
  `last_updated_by` char(36) DEFAULT NULL,
  `last_updated_on` datetime DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  KEY `transfer_money_location_location_send_id_fk` (`location_send`),
  KEY `transfer_money_location_location_receiver_id_fk` (`location_receiver`),
  KEY `transfer_money_location_location_id_fk` (`id_location`),
  KEY `transfer_money_location_user_created_by_id_fk` (`created_by`),
  KEY `transfer_money_location_user_last_updated_by_id_fk` (`last_updated_by`),
  CONSTRAINT `transfer_money_location_location_send_id_fk` FOREIGN KEY (`location_send`) REFERENCES `LOCATION` (`id`) ON UPDATE CASCADE,
  CONSTRAINT `transfer_money_location_location_receiver_id_fk` FOREIGN KEY (`location_receiver`) REFERENCES `LOCATION` (`id`) ON UPDATE CASCADE,
  CONSTRAINT `transfer_money_location_user_created_by_id_fk` FOREIGN KEY (`created_by`) REFERENCES `CAR_DRIVER_USER` (`id`) ON UPDATE CASCADE,
  CONSTRAINT `transfer_money_location_user_last_updated_by_id_fk` FOREIGN KEY (`last_updated_by`) REFERENCES `CAR_DRIVER_USER` (`id`) ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;  

CREATE TABLE `CHECK_STOCKTAKING` (
  `id` char(36) NOT NULL,
  `description` varchar(500) DEFAULT NULL,
  `check_status` enum('PENDING','FINISH','CANCELED') NOT NULL DEFAULT 'PENDING',
  `active` tinyint DEFAULT '1',
  `id_location` char(36) NOT NULL,
  `created_by` char(36) NOT NULL,
  `created_on` datetime DEFAULT CURRENT_TIMESTAMP,
  `last_updated_by` char(36) DEFAULT NULL,
  `last_updated_on` datetime DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  KEY `check_stocktaking_location_id_fk` (`id_location`),
  KEY `check_stocktaking_user_created_by_id_fk` (`created_by`),
  KEY `check_stocktaking_user_last_updated_by_id_fk` (`last_updated_by`),
  CONSTRAINT `check_stocktaking_user_created_by_id_fk` FOREIGN KEY (`created_by`) REFERENCES `CAR_DRIVER_USER` (`id`) ON UPDATE CASCADE,
  CONSTRAINT `check_stocktaking_user_last_updated_by_id_fk` FOREIGN KEY (`last_updated_by`) REFERENCES `CAR_DRIVER_USER` (`id`) ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;  

CREATE TABLE `CHECK_STOCKTAKING_DETAIL` (
  `id` char(36) NOT NULL,
  `id_check_stocktaking` char(36) NOT NULL,
  `id_product` char(36) NOT NULL,
  `last_stocktaking` int,
  `new_stocktaking` int,
  `description` varchar(500) DEFAULT NULL,
  `active` tinyint DEFAULT '1',
  `created_by` char(36) NOT NULL,
  `created_on` datetime DEFAULT CURRENT_TIMESTAMP,
  `last_updated_by` char(36) DEFAULT NULL,
  `last_updated_on` datetime DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  KEY `check_stocktaking_detail_check_stocktaking_id_fk` (`id_check_stocktaking`),
  KEY `check_stocktaking_detail_product_id_fk` (`id_product`),
  KEY `check_stocktaking_detail_user_created_by_id_fk` (`created_by`),
  KEY `check_stocktaking_detail_user_last_updated_by_id_fk` (`last_updated_by`),
  CONSTRAINT `check_stocktaking_detail_user_created_by_id_fk` FOREIGN KEY (`created_by`) REFERENCES `CAR_DRIVER_USER` (`id`) ON UPDATE CASCADE,
  CONSTRAINT `check_stocktaking_detail_user_last_updated_by_id_fk` FOREIGN KEY (`last_updated_by`) REFERENCES `CAR_DRIVER_USER` (`id`) ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;            

CREATE TABLE `GENERAL_PUBLIC` (
  `id` char(36) NOT NULL,
  `first_name` varchar(25) NOT NULL,
  `second_name` varchar(25) DEFAULT NULL,
  `last_name` varchar(25) NOT NULL,
  `middle_name` varchar(25) DEFAULT NULL,
  `phone` varchar(25) DEFAULT NULL,
  `placas` varchar(50) DEFAULT NULL,
  `vehicle_id` char(36) DEFAULT NULL,
  `color_id` char(36) DEFAULT NULL,
  `km` float DEFAULT '0',
  `km_maintenance` float DEFAULT '0',
  `limit_credit` double DEFAULT '0',
  `adeudo` double DEFAULT '0',
  `id_location` varchar(50) DEFAULT NULL,
  `active_status` enum('ENEBLED','DISABLED') DEFAULT NULL,
  `created_by` char(36) NOT NULL,
  `created_on` datetime DEFAULT CURRENT_TIMESTAMP,
  `last_updated_by` char(36) DEFAULT NULL,
  `last_updated_on` datetime DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  KEY `vehicle_id_vehicle_id_fk` (`vehicle_id`),
  KEY `color_id_color_id_fk` (`color_id`),
  CONSTRAINT `color_id_color_id_fk` FOREIGN KEY (`color_id`) REFERENCES `COLOR` (`id`) ON UPDATE CASCADE,
  CONSTRAINT `vehicle_id_vehicle_id_fk` FOREIGN KEY (`vehicle_id`) REFERENCES `VEHICLE` (`id`) ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;   

CREATE TABLE `TIEMPO_AIRE`(
    `id` char(36) NOT NULL,
    `amount_mayoreo` numeric(8,2) NOT NULL,
    `amount_menudeo` numeric(8,2) NOT NULL,
    `description` varchar(200),
    `id_taxi` varchar(50) DEFAULT NULL,
    `active_status` ENUM('ENEBLED', 'DISABLED') NOT NULL DEFAULT 'ENEBLED',
    `vigencia` datetime DEFAULT CURRENT_TIMESTAMP,
    `created_by` char(36) NOT NULL,
    `created_on` datetime DEFAULT CURRENT_TIMESTAMP,
    `last_updated_by` char(36) DEFAULT NULL,
    `last_updated_on` datetime DEFAULT CURRENT_TIMESTAMP,
    `id_location` varchar(50) DEFAULT NULL,
    CONSTRAINT `tiempo_aire_pk` PRIMARY KEY (`id`),
    CONSTRAINT `tiempo_aire_user_created_by_id_fk` FOREIGN KEY (`created_by`) REFERENCES `CAR_DRIVER_USER` (`id`) ON UPDATE CASCADE,
    CONSTRAINT `tiempo_aire_user_last_updated_by_id_fk` FOREIGN KEY (`last_updated_by`) REFERENCES `CAR_DRIVER_USER` (`id`) ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

CREATE TABLE `BANK_CARD`(
    `id` char(36) NOT NULL,
    `bank` varchar(200),
    `titular` varchar(200),
    `numero` varchar(200),
    `active_status` ENUM('ENEBLED', 'DISABLED') NOT NULL DEFAULT 'ENEBLED',
    `created_by` char(36) NOT NULL,
    `created_on` datetime DEFAULT CURRENT_TIMESTAMP,
    `last_updated_by` char(36) DEFAULT NULL,
    `last_updated_on` datetime DEFAULT CURRENT_TIMESTAMP,
    `id_location` varchar(50) DEFAULT NULL,
    CONSTRAINT `bank_card_pk` PRIMARY KEY (`id`),
    CONSTRAINT `bank_card_user_created_by_id_fk` FOREIGN KEY (`created_by`) REFERENCES `CAR_DRIVER_USER` (`id`) ON UPDATE CASCADE,
    CONSTRAINT `bank_card_user_last_updated_by_id_fk` FOREIGN KEY (`last_updated_by`) REFERENCES `CAR_DRIVER_USER` (`id`) ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

ALTER TABLE SERVICE_ORDER
ADD COLUMN `general_public_id` char(36) DEFAULT NULL; 

CREATE TABLE `SALE_GENERAL_PUBLIC` (
  `id` char(36) NOT NULL,
  `general_public_id` char(36) NOT NULL,
  `km` float DEFAULT '0',
  `km_maintenance` float DEFAULT '0',
  `type_sale` enum('CASH','CREDIT') DEFAULT NULL,
  `adeudo` double DEFAULT '0',
  `amount` double DEFAULT '0',
  `active_status` enum('ENEBLED','DISABLED') DEFAULT NULL,
  `created_by` char(36) NOT NULL,
  `created_on` datetime DEFAULT CURRENT_TIMESTAMP,
  `last_updated_by` char(36) DEFAULT NULL,
  `last_updated_on` datetime DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  KEY `general_public_id_general_public_id_fk` (`general_public_id`),
  CONSTRAINT `general_public_id_general_public_id_fk` FOREIGN KEY (`general_public_id`) REFERENCES `GENERAL_PUBLIC` (`id`) ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

CREATE TABLE `MUTUAL_ADD` (
  `id` char(36) NOT NULL,
  `payment` double NOT NULL,
  `description` varchar(200) DEFAULT NULL,
  `mutual_add_type` enum('SINIESTRO', 'CUOTA_MUTUAL', 'PAGO_LAMINADO', 'PAGO_DEDUCIBLE', 'NUEVO_INGRESO', 'OTROS_INGRESOS') NOT NULL,
  `driver_id` char(36),
  `active` tinyint default true,
  `id_location` char(36) NOT NULL,
  `created_by` char(36) NOT NULL,
  `created_on` datetime DEFAULT CURRENT_TIMESTAMP,
  `last_updated_by` char(36) DEFAULT NULL,
  `last_updated_on` datetime DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  CONSTRAINT `mutual_add_user_created_by_id_fk` FOREIGN KEY (`created_by`) REFERENCES `CAR_DRIVER_USER` (`id`) ON UPDATE CASCADE,
  CONSTRAINT `mutual_add_user_last_updated_by_id_fk` FOREIGN KEY (`last_updated_by`) REFERENCES `CAR_DRIVER_USER` (`id`) ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

CREATE TABLE `MUTUAL_OUT` (
  `id` char(36) NOT NULL,
  `payment` double NOT NULL,
  `description` varchar(200) DEFAULT NULL,
  `mutual_out_type` enum('PAGO_LAMINADO', 'PAGO_DEDUCIBLE', 'COMPRAS', 'GASOLINA', 'NOMINA', 'DISPOSICION', 'OTROS_EGRESOS') NOT NULL,
  `driver_id` char(36),
  `active` tinyint default true,
  `id_location` char(36) NOT NULL,
  `created_by` char(36) NOT NULL,
  `created_on` datetime DEFAULT CURRENT_TIMESTAMP,
  `last_updated_by` char(36) DEFAULT NULL,
  `last_updated_on` datetime DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  CONSTRAINT `mutual_out_user_created_by_id_fk` FOREIGN KEY (`created_by`) REFERENCES `CAR_DRIVER_USER` (`id`) ON UPDATE CASCADE,
  CONSTRAINT `mutual_out_user_last_updated_by_id_fk` FOREIGN KEY (`last_updated_by`) REFERENCES `CAR_DRIVER_USER` (`id`) ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

CREATE TABLE `MUTUAL_CLOSING_DAY`(
    `id` char(36) NOT NULL,
    `user_id` char(36) NOT NULL,
    `amount_paid` numeric(8,2) NOT NULL,
    `amount_expected` numeric(8,2) NOT NULL,
    `active_status` ENUM('ENEBLED', 'DISABLED') NOT NULL DEFAULT 'ENEBLED',
    `comments` varchar(200),
    `id_location` char(36) NOT NULL,
    `created_by` char(36) NOT NULL,
    `created_on` datetime DEFAULT CURRENT_TIMESTAMP,
    `last_updated_by` char(36) DEFAULT NULL,
    `last_updated_on` datetime DEFAULT CURRENT_TIMESTAMP,
    CONSTRAINT `mutual_closing_day_pk` PRIMARY KEY (`id`),
    CONSTRAINT `mutual_closing_day_user_id_fk` FOREIGN KEY (`user_id`) REFERENCES `CAR_DRIVER_USER` (`id`) ON UPDATE CASCADE,
    CONSTRAINT `mutual_closing_day_user_created_by_id_fk` FOREIGN KEY (`created_by`) REFERENCES `CAR_DRIVER_USER` (`id`) ON UPDATE CASCADE,
    CONSTRAINT `mutual_closing_day_user_last_updated_by_id_fk` FOREIGN KEY (`last_updated_by`) REFERENCES `CAR_DRIVER_USER` (`id`) ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
-- --------------------------------------------------------------------
-- 
-- Estructura para la tabla `CLOSING_DAY_DETAILS`
--
-- Detalles del corte diario en CAR DRIVER.
-- 
CREATE TABLE `MUTUAL_CLOSING_DAY_DETAILS`(
    `id` char(36) NOT NULL,
    `mutual_closing_day_id` char(36) NOT NULL,
    `type` varchar(200) DEFAULT NULL,
    `amount` numeric(8,2) NOT NULL,
    `person_id` char(36),
    `comments` varchar(500) DEFAULT NULL,
    `created_by` char(36) NOT NULL,
    `created_on` datetime DEFAULT CURRENT_TIMESTAMP,
    `last_updated_by` char(36) DEFAULT NULL,
    `last_updated_on` datetime DEFAULT CURRENT_TIMESTAMP,
    CONSTRAINT `mutual_closing_day_details_pk` PRIMARY KEY (`id`),
    CONSTRAINT `mutual_closing_day_details_closing_id_fk` FOREIGN KEY (`mutual_closing_day_id`) REFERENCES `MUTUAL_CLOSING_DAY` (`id`) ON UPDATE CASCADE,
    CONSTRAINT `mutual_closing_day_details_person_id_fk` FOREIGN KEY (`person_id`) REFERENCES `PERSON` (`id`) ON UPDATE CASCADE,
    CONSTRAINT `mutual_closing_day_details_user_created_by_id_fk` FOREIGN KEY (`created_by`) REFERENCES `CAR_DRIVER_USER` (`id`) ON UPDATE CASCADE,
    CONSTRAINT `mutual_closing_day_details_user_last_updated_by_id_fk` FOREIGN KEY (`last_updated_by`) REFERENCES `CAR_DRIVER_USER` (`id`) ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

CREATE TABLE `TRANSFER_MUTUAL`(
    `id` char(36) NOT NULL,
    `id_user_transmitter` char(36) NOT NULL,
    `id_user_receiver` char(36) NOT NULL,
    `active_status` ENUM('ENEBLED', 'DISABLED') NOT NULL DEFAULT 'ENEBLED',
    `action_status` ENUM('PENDING', 'APPROVED','REJECTED') NOT NULL DEFAULT 'PENDING',
    `amount_to_transfer` double,
    `created_by` char(36) NOT NULL,
    `created_on` datetime DEFAULT CURRENT_TIMESTAMP,
    `last_updated_by` char(36) DEFAULT NULL,
    `last_updated_on` datetime DEFAULT CURRENT_TIMESTAMP,
    CONSTRAINT `mutual_transfer_pk` PRIMARY KEY (`id`),
    CONSTRAINT `mutual_transfer_user_transmitter_to_user_fk` 
        FOREIGN KEY (`id_user_transmitter`) REFERENCES `CAR_DRIVER_USER`(`id`)
            ON UPDATE CASCADE
            ON DELETE RESTRICT,
    CONSTRAINT `mutual_transfer_user_receiver_to_user_fk` 
        FOREIGN KEY (`id_user_receiver`) REFERENCES `CAR_DRIVER_USER`(`id`)
            ON UPDATE CASCADE
            ON DELETE RESTRICT
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

ALTER TABLE CLOSING_DAY 
ADD COLUMN amount_transfer decimal(8,2) default 0;

ALTER TABLE STABLE_SMALL_BOX 
ADD COLUMN total_transfer decimal(8,2) default 0;

ALTER TABLE SERVICE_ORDER 
ADD COLUMN `pase_taller_user` char(36) DEFAULT NULL;

ALTER TABLE SERVICE_ORDER 
ADD COLUMN `pendiente_user` char(36) DEFAULT NULL;

ALTER TABLE SERVICE_ORDER 
ADD COLUMN `aprobado_user` char(36) DEFAULT NULL;

ALTER TABLE SERVICE_ORDER 
ADD COLUMN `progreso_user` char(36) DEFAULT NULL;

ALTER TABLE SERVICE_ORDER 
ADD COLUMN `terminado_user` char(36) DEFAULT NULL;

CREATE TABLE `MUTUAL_STABLE_SMALL_BOX`(
    `id` char(36) NOT NULL,
    `total_small_box` numeric(8,2) NOT NULL,
    `total_envelope` numeric(8,2) NOT NULL,
    `total_bank_note` numeric(8,2) NOT NULL,
    `total_coin` numeric(8,2) NOT NULL,
    `total_stable` numeric(8,2) NOT NULL,
    `total_transfer` decimal(8,2) default 0,
    `active_status` ENUM('ENEBLED', 'DISABLED') NOT NULL DEFAULT 'ENEBLED',
    `description` varchar(200),
    `created_by` char(36) NOT NULL,
    `created_on` datetime DEFAULT CURRENT_TIMESTAMP,
    `last_updated_by` char(36) DEFAULT NULL,
    `last_updated_on` datetime DEFAULT CURRENT_TIMESTAMP,
    CONSTRAINT `mutual_stable_small_box_pk` PRIMARY KEY (`id`),
    CONSTRAINT `mutual_stable_small_box_user_created_by_id_fk` FOREIGN KEY (`created_by`) REFERENCES `CAR_DRIVER_USER` (`id`) ON UPDATE CASCADE,
    CONSTRAINT `mutual_stable_small_box_user_last_updated_by_id_fk` FOREIGN KEY (`last_updated_by`) REFERENCES `CAR_DRIVER_USER` (`id`) ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

CREATE TABLE `MUTUAL_EXPENSE_COMPANY` (
  `id` char(36) NOT NULL,
  `amount` decimal(8,2) NOT NULL,
  `description` varchar(200) DEFAULT NULL,
  `expense_company_type` enum('PAYMENT_IN','PAYMENT_OUT') NOT NULL,
  `active_status` enum('ENEBLED','DISABLED') NOT NULL DEFAULT 'ENEBLED',
  `created_by` char(36) NOT NULL,
  `created_on` datetime DEFAULT CURRENT_TIMESTAMP,
  `last_updated_by` char(36) DEFAULT NULL,
  `last_updated_on` datetime DEFAULT CURRENT_TIMESTAMP,
  `id_location` varchar(50) DEFAULT '4d0859be-e31a-4acb-a690-2a5c2cb027a7',
  PRIMARY KEY (`id`),
  KEY `mutual_expense_company_user_created_by_id_fk` (`created_by`),
  KEY `mutual_expense_company_user_last_updated_by_id_fk` (`last_updated_by`),
  CONSTRAINT `mutual_expense_company_user_created_by_id_fk` FOREIGN KEY (`created_by`) REFERENCES `CAR_DRIVER_USER` (`id`) ON UPDATE CASCADE,
  CONSTRAINT `mutual_expense_company_user_last_updated_by_id_fk` FOREIGN KEY (`last_updated_by`) REFERENCES `CAR_DRIVER_USER` (`id`) ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

CREATE TABLE `MUTUAL_INICIOS` (
  `id` char(36) NOT NULL,
  `amount` decimal(8,2) NOT NULL,
  `description` varchar(200) DEFAULT NULL,
  `user_id` char(36) NOT NULL,
  `active_status` enum('ENEBLED','DISABLED') NOT NULL DEFAULT 'ENEBLED',
  `created_by` char(36) NOT NULL,
  `created_on` datetime DEFAULT CURRENT_TIMESTAMP,
  `last_updated_by` char(36) DEFAULT NULL,
  `last_updated_on` datetime DEFAULT CURRENT_TIMESTAMP,
  `id_location` varchar(50) DEFAULT '4d0859be-e31a-4acb-a690-2a5c2cb027a7',
  PRIMARY KEY (`id`),
  KEY `mutual_inicios_user_created_by_id_fk` (`created_by`),
  KEY `mutual_inicios_user_last_updated_by_id_fk` (`last_updated_by`),
  CONSTRAINT `mutual_inicios_user_created_by_id_fk` FOREIGN KEY (`created_by`) REFERENCES `CAR_DRIVER_USER` (`id`) ON UPDATE CASCADE,
  CONSTRAINT `mutual_inicios_user_last_updated_by_id_fk` FOREIGN KEY (`last_updated_by`) REFERENCES `CAR_DRIVER_USER` (`id`) ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

CREATE TABLE `MUTUAL_STABLE_GENERAL_BOX` (
  `id` char(36) NOT NULL,
  `total_general_box` decimal(8,2) NOT NULL,
  `total_envelope` decimal(8,2) NOT NULL,
  `total_bank_note` decimal(8,2) NOT NULL,
  `total_coin` decimal(8,2) NOT NULL,
  `total_stable` decimal(8,2) NOT NULL,
  `active_status` enum('ENEBLED','DISABLED') NOT NULL DEFAULT 'ENEBLED',
  `description` varchar(200) DEFAULT NULL,
  `created_by` char(36) NOT NULL,
  `created_on` datetime DEFAULT CURRENT_TIMESTAMP,
  `last_updated_by` char(36) DEFAULT NULL,
  `last_updated_on` datetime DEFAULT CURRENT_TIMESTAMP,
  `id_location` varchar(50) DEFAULT '4d0859be-e31a-4acb-a690-2a5c2cb027a7',
  `total_bank` decimal(8,2) DEFAULT '0.00',
  PRIMARY KEY (`id`),
  KEY `mutual_stable_general_box_user_created_by_id_fk` (`created_by`),
  KEY `mutual_stable_general_box_user_last_updated_by_id_fk` (`last_updated_by`),
  CONSTRAINT `mutual_stable_general_box_user_created_by_id_fk` FOREIGN KEY (`created_by`) REFERENCES `CAR_DRIVER_USER` (`id`) ON UPDATE CASCADE,
  CONSTRAINT `mutual_stable_general_box_user_last_updated_by_id_fk` FOREIGN KEY (`last_updated_by`) REFERENCES `CAR_DRIVER_USER` (`id`) ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;


