/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 * Author:  Oscar
 * Created: 15/11/2021
 */
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`),
  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;

INSERT INTO LOCATION VALUES('4d0859be-e31a-4acb-a690-2a5c2cb027a7', 'Tepic', 1, '09ea84b8-b9d1-4a71-adfe-db653b2a9982', CURDATE(),null,null);

ALTER TABLE CAR_DRIVER_USER 
ADD COLUMN id_location varchar(50) default '4d0859be-e31a-4acb-a690-2a5c2cb027a7';

ALTER TABLE PERSON 
ADD COLUMN id_location varchar(50) default '4d0859be-e31a-4acb-a690-2a5c2cb027a7';

CREATE OR REPLACE VIEW `PERSON_LIST_VIEW` AS
SELECT
p.id AS id,
CONCAT(
	CASE
		WHEN p.first_name IS NOT NULL AND p.first_name != ''
		THEN CONCAT(SUBSTR(UPPER(p.first_name), 1, 1),SUBSTR(LOWER(p.first_name), 2), ' ')
		ELSE ''
	END,
	CASE
		WHEN p.second_name IS NOT NULL AND p.second_name != ''
		THEN CONCAT(SUBSTR(UPPER(p.second_name), 1, 1),SUBSTR(LOWER(p.second_name), 2), ' ')
		ELSE ''
	END, 
	CASE
		WHEN p.last_name IS NOT NULL AND p.last_name != ''
		THEN CONCAT(SUBSTR(UPPER(p.last_name), 1, 1),SUBSTR(LOWER(p.last_name), 2), ' ')
		ELSE ''
	END,
	CASE
		WHEN p.middle_name IS NOT NULL AND p.middle_name != ''
		THEN CONCAT(SUBSTR(UPPER(p.middle_name), 1, 1),SUBSTR(LOWER(p.middle_name), 2))
		ELSE ''
	END
) AS full_name,
(SELECT cn.contact_number FROM CONTACT_NUMBER cn WHERE cn.number_type = 'HOME' AND cn.person_id = p.id)AS contact_number_local, 
(SELECT cn.contact_number FROM CONTACT_NUMBER cn WHERE cn.number_type = 'MOBILE' AND cn.person_id = p.id)AS contact_number_mobile, 
(SELECT a.address_google_maps FROM ADDRESS a WHERE a.address_type = 'HOME' AND a.person_id = p.id) AS address,
(
SELECT GROUP_CONCAT(CONCAT(economic_number)) FROM TAXI WHERE customer_id =p.id
GROUP BY customer_id
) AS customer_vehicles,
p.admission_date AS driver_admission_date,
p.person_type AS person_type,
IF(ISNULL(p.adeudo),0,p.adeudo) AS customer_adeudo,
p.id_location as location
FROM PERSON p
WHERE p.active = 1
ORDER BY full_name;

ALTER TABLE TAXI 
ADD COLUMN id_location varchar(50) default '4d0859be-e31a-4acb-a690-2a5c2cb027a7';

ALTER TABLE CLOSING_DAY 
ADD COLUMN id_location varchar(50) default '4d0859be-e31a-4acb-a690-2a5c2cb027a7';

ALTER TABLE STABLE_SMALL_BOX 
ADD COLUMN id_location varchar(50) default '4d0859be-e31a-4acb-a690-2a5c2cb027a7';

ALTER TABLE STABLE_GENERAL_BOX 
ADD COLUMN id_location varchar(50) default '4d0859be-e31a-4acb-a690-2a5c2cb027a7';

ALTER TABLE EXPENSE_COMPANY 
ADD COLUMN id_location varchar(50) default '4d0859be-e31a-4acb-a690-2a5c2cb027a7';

ALTER TABLE EXPENSE 
ADD COLUMN id_location varchar(50) default '4d0859be-e31a-4acb-a690-2a5c2cb027a7';

CREATE OR REPLACE VIEW `DRIVER_INACTIVE_AND_BLACK_LIST_VIEW` AS
SELECT
p.id AS id,
CONCAT(
	CASE
		WHEN p.first_name IS NOT NULL AND p.first_name != ''
		THEN CONCAT(SUBSTR(UPPER(p.first_name), 1, 1),SUBSTR(LOWER(p.first_name), 2), ' ')
		ELSE ''
	END,
	CASE
		WHEN p.second_name IS NOT NULL AND p.second_name != ''
		THEN CONCAT(SUBSTR(UPPER(p.second_name), 1, 1),SUBSTR(LOWER(p.second_name), 2), ' ')
		ELSE ''
	END, 
	CASE
		WHEN p.last_name IS NOT NULL AND p.last_name != ''
		THEN CONCAT(SUBSTR(UPPER(p.last_name), 1, 1),SUBSTR(LOWER(p.last_name), 2), ' ')
		ELSE ''
	END,
	CASE
		WHEN p.middle_name IS NOT NULL AND p.middle_name != ''
		THEN CONCAT(SUBSTR(UPPER(p.middle_name), 1, 1),SUBSTR(LOWER(p.middle_name), 2))
		ELSE ''
	END
) AS full_name,
(SELECT cn.contact_number FROM CONTACT_NUMBER cn WHERE cn.number_type = 'HOME' AND cn.person_id = p.id)AS contact_number_local, 
(SELECT cn.contact_number FROM CONTACT_NUMBER cn WHERE cn.number_type = 'MOBILE' AND cn.person_id = p.id)AS contact_number_mobile, 
(SELECT a.address_google_maps FROM ADDRESS a WHERE a.address_type = 'HOME' AND a.person_id = p.id) AS address,
p.admission_date AS driver_admission_date,
IF(ISNULL(p.black_list),'DISABLED', p.black_list) AS black_list,
p.black_list_description AS black_list_description,
p.last_updated_on AS black_list_date,
p.adeudo AS black_list_adeudo,
p.id_location
FROM PERSON p
WHERE 
	p.active = 0 AND
    p.person_type = 'DRIVER'
ORDER BY full_name;