

CREATE TABLE IF NOT EXISTS `app_checkin_log` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `emp_id` int(11) NOT NULL,
  `idcard` varchar(80) NOT NULL,
  `name` varchar(80) NOT NULL,
  `phone` varchar(80) NOT NULL,
  `image_file` varchar(255) DEFAULT NULL,
  `checkin_time` datetime NOT NULL,
  `checkout_time` datetime DEFAULT NULL,
  `latitude` decimal(10,8) DEFAULT NULL,
  `longitude` decimal(11,8) DEFAULT NULL,
  `location_accuracy` decimal(10,2) DEFAULT NULL,
  `device_info` text,
  PRIMARY KEY (`id`),
  KEY `emp_date` (`emp_id`,`checkin_time`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE IF NOT EXISTS `checkin_location` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `company_name` varchar(150) NOT NULL,
  `latitude` decimal(10,8) NOT NULL,
  `longitude` decimal(11,8) NOT NULL,
  `radius` int(11) NOT NULL DEFAULT 25,
  `address` varchar(255) DEFAULT NULL,
  `status` varchar(1) NOT NULL DEFAULT 'Y',
  `updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

INSERT INTO `checkin_location`
  (`company_name`, `latitude`, `longitude`, `radius`, `address`, `status`)
SELECT * FROM (
  SELECT 'บริษัท ไอยรา อินเตอร์ฟูดส์ จำกัด' AS company_name,
         13.93417100 AS latitude,
         100.38491000 AS longitude,
         25 AS radius,
         '14/31 ม.4 ต.บางบัวทอง อ.บางบัวทอง จ.นนทบุรี 11110' AS address,
         'Y' AS status
) AS tmp
WHERE NOT EXISTS (SELECT 1 FROM `checkin_location` LIMIT 1);

CREATE TABLE IF NOT EXISTS `app_work_station_staff` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `station_code` varchar(20) NOT NULL,
  `emp_id` int(11) NOT NULL,
  `emp_name` varchar(80) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `station_emp` (`station_code`,`emp_id`),
  KEY `emp_id` (`emp_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE IF NOT EXISTS `app_work_log` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `planing_id` int(11) NOT NULL DEFAULT 0,
  `station_code` varchar(20) NOT NULL,
  `emp_id` int(11) NOT NULL,
  `emp_name` varchar(80) NOT NULL,
  `start_time` datetime NOT NULL,
  `end_time` datetime DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `emp_station` (`emp_id`,`station_code`,`start_time`),
  KEY `planing_id` (`planing_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

