
# This is a fix for InnoDB in MySQL >= 4.1.x
# It "suspends judgement" for fkey relationships until are tables are set.
SET FOREIGN_KEY_CHECKS = 0;

#-----------------------------------------------------------------------------
#-- node_home
#-----------------------------------------------------------------------------

DROP TABLE IF EXISTS `node_home`;


CREATE TABLE `node_home`
(
	`id` INTEGER  NOT NULL AUTO_INCREMENT,
	`node_id` INTEGER,
	`edito_title` VARCHAR(255),
	`edito_content` TEXT,
	PRIMARY KEY (`id`),
	INDEX `node_home_FI_1` (`node_id`),
	CONSTRAINT `node_home_FK_1`
		FOREIGN KEY (`node_id`)
		REFERENCES `node` (`id`)
		ON DELETE CASCADE
)Engine=InnoDB;

#-----------------------------------------------------------------------------
#-- node_page
#-----------------------------------------------------------------------------

DROP TABLE IF EXISTS `node_page`;


CREATE TABLE `node_page`
(
	`id` INTEGER  NOT NULL AUTO_INCREMENT,
	`node_id` INTEGER,
	`content` TEXT,
	PRIMARY KEY (`id`),
	INDEX `node_page_FI_1` (`node_id`),
	CONSTRAINT `node_page_FK_1`
		FOREIGN KEY (`node_id`)
		REFERENCES `node` (`id`)
		ON DELETE CASCADE
)Engine=InnoDB;

#-----------------------------------------------------------------------------
#-- node_news
#-----------------------------------------------------------------------------

DROP TABLE IF EXISTS `node_news`;


CREATE TABLE `node_news`
(
	`id` INTEGER  NOT NULL AUTO_INCREMENT,
	`node_id` INTEGER,
	`published_at` DATETIME,
	`introduction` TEXT,
	`content` TEXT,
	`picture` VARCHAR(255),
	PRIMARY KEY (`id`),
	INDEX `node_news_FI_1` (`node_id`),
	CONSTRAINT `node_news_FK_1`
		FOREIGN KEY (`node_id`)
		REFERENCES `node` (`id`)
		ON DELETE CASCADE
)Engine=InnoDB;

#-----------------------------------------------------------------------------
#-- node_network
#-----------------------------------------------------------------------------

DROP TABLE IF EXISTS `node_network`;


CREATE TABLE `node_network`
(
	`id` INTEGER  NOT NULL AUTO_INCREMENT,
	`node_id` INTEGER,
	`introduction` TEXT,
	PRIMARY KEY (`id`),
	INDEX `node_network_FI_1` (`node_id`),
	CONSTRAINT `node_network_FK_1`
		FOREIGN KEY (`node_id`)
		REFERENCES `node` (`id`)
		ON DELETE CASCADE
)Engine=InnoDB;

#-----------------------------------------------------------------------------
#-- katao_config
#-----------------------------------------------------------------------------

DROP TABLE IF EXISTS `katao_config`;


CREATE TABLE `katao_config`
(
	`id` INTEGER  NOT NULL AUTO_INCREMENT,
	`katao_member_id` INTEGER,
	`name` VARCHAR(255)  NOT NULL,
	`address` TEXT,
	`phone` VARCHAR(255)  NOT NULL,
	`email` VARCHAR(255)  NOT NULL,
	`siret_number` CHAR(14)  NOT NULL,
	`tva_number` CHAR(13)  NOT NULL,
	`rcs_number` VARCHAR(255)  NOT NULL,
	`capital` INTEGER  NOT NULL,
	`initial_transactions_count` INTEGER default 0,
	`initial_transactions_sum` FLOAT default 0,
	`default_product_picture` VARCHAR(255),
	`default_product_margin` INTEGER default 15,
	`default_product_sol_percent` INTEGER default 5,
	`mailing_list_email` VARCHAR(255),
	`cart_validation_days_number` INTEGER default 1 NOT NULL,
	`accounting_code_prefix_purchase` VARCHAR(12)  NOT NULL,
	`accounting_code_prefix_sell` VARCHAR(12)  NOT NULL,
	`accounting_code_bank` VARCHAR(12)  NOT NULL,
	`accounting_code_bank_sol` VARCHAR(12)  NOT NULL,
	`accounting_code_credit` VARCHAR(12)  NOT NULL,
	`accounting_code_error_adjustment_positive` VARCHAR(12)  NOT NULL,
	`accounting_code_error_adjustment_negative` VARCHAR(12)  NOT NULL,
	`accounting_code_sol_discount` VARCHAR(12)  NOT NULL,
	`accounting_code_fees_shipping` VARCHAR(12)  NOT NULL,
	`accounting_code_fees_billing` VARCHAR(12)  NOT NULL,
	`accounting_code_discount` VARCHAR(12)  NOT NULL,
	`member_fee` FLOAT default 0,
	`additional_currency_name` VARCHAR(255),
	`debug_email` VARCHAR(255),
	`bank_name` VARCHAR(255),
	`bank_account` VARCHAR(255),
	PRIMARY KEY (`id`),
	INDEX `katao_config_FI_1` (`katao_member_id`),
	CONSTRAINT `katao_config_FK_1`
		FOREIGN KEY (`katao_member_id`)
		REFERENCES `katao_member` (`id`)
		ON DELETE CASCADE
)Engine=InnoDB;

#-----------------------------------------------------------------------------
#-- katao_node
#-----------------------------------------------------------------------------

DROP TABLE IF EXISTS `katao_node`;


CREATE TABLE `katao_node`
(
	`id` INTEGER  NOT NULL AUTO_INCREMENT,
	`city` VARCHAR(255)  NOT NULL,
	`responsible` INTEGER,
	`begin_at` DATETIME  NOT NULL,
	`is_default` INTEGER(1) default 0,
	`created_at` DATETIME,
	`updated_at` DATETIME,
	PRIMARY KEY (`id`),
	INDEX `katao_node_FI_1` (`responsible`),
	CONSTRAINT `katao_node_FK_1`
		FOREIGN KEY (`responsible`)
		REFERENCES `katao_user` (`id`)
		ON DELETE SET NULL
)Engine=InnoDB;

#-----------------------------------------------------------------------------
#-- katao_period
#-----------------------------------------------------------------------------

DROP TABLE IF EXISTS `katao_period`;


CREATE TABLE `katao_period`
(
	`id` INTEGER  NOT NULL AUTO_INCREMENT,
	`name` VARCHAR(255)  NOT NULL,
	`begin_at` DATETIME  NOT NULL,
	`finish_at` DATETIME  NOT NULL,
	`order_ended_at` DATETIME  NOT NULL,
	`status` INTEGER default 1 NOT NULL,
	`exported` INTEGER(1) default 0,
	`created_at` DATETIME,
	`updated_at` DATETIME,
	PRIMARY KEY (`id`)
)Engine=InnoDB;

#-----------------------------------------------------------------------------
#-- katao_product_category
#-----------------------------------------------------------------------------

DROP TABLE IF EXISTS `katao_product_category`;


CREATE TABLE `katao_product_category`
(
	`id` INTEGER  NOT NULL AUTO_INCREMENT,
	`name` VARCHAR(255)  NOT NULL,
	`url_identifier` VARCHAR(255)  NOT NULL,
	`accounting_code_suffix` VARCHAR(12)  NOT NULL,
	`sort_order` INTEGER default 0,
	`created_at` DATETIME,
	`updated_at` DATETIME,
	PRIMARY KEY (`id`)
)Engine=InnoDB;

#-----------------------------------------------------------------------------
#-- katao_product_family
#-----------------------------------------------------------------------------

DROP TABLE IF EXISTS `katao_product_family`;


CREATE TABLE `katao_product_family`
(
	`id` INTEGER  NOT NULL AUTO_INCREMENT,
	`katao_product_category_id` INTEGER  NOT NULL,
	`name` VARCHAR(255)  NOT NULL,
	`url_identifier` VARCHAR(255)  NOT NULL,
	`created_at` DATETIME,
	`updated_at` DATETIME,
	PRIMARY KEY (`id`),
	INDEX `katao_product_family_FI_1` (`katao_product_category_id`),
	CONSTRAINT `katao_product_family_FK_1`
		FOREIGN KEY (`katao_product_category_id`)
		REFERENCES `katao_product_category` (`id`)
		ON DELETE CASCADE
)Engine=InnoDB;

#-----------------------------------------------------------------------------
#-- katao_product
#-----------------------------------------------------------------------------

DROP TABLE IF EXISTS `katao_product`;


CREATE TABLE `katao_product`
(
	`id` INTEGER  NOT NULL AUTO_INCREMENT,
	`reference` VARCHAR(255),
	`name` VARCHAR(255)  NOT NULL,
	`url_identifier` VARCHAR(255)  NOT NULL,
	`description` TEXT,
	`katao_product_family_id` INTEGER  NOT NULL,
	`katao_supplier_id` INTEGER  NOT NULL,
	`max_sol_amount` INTEGER,
	`unit_price_euro` FLOAT  NOT NULL,
	`authorize_decimal` INTEGER(1) default 0,
	`margin` FLOAT,
	`tva_rate` FLOAT default 0,
	`min_order_number` INTEGER default 1 NOT NULL,
	`picture` VARCHAR(255),
	`delivery_delay` INTEGER,
	`is_archived` INTEGER(1) default 0,
	`accounting_code_purchase` VARCHAR(12)  NOT NULL,
	`accounting_code_sell` VARCHAR(12)  NOT NULL,
	`created_at` DATETIME,
	`updated_at` DATETIME,
	PRIMARY KEY (`id`),
	INDEX `katao_product_FI_1` (`katao_product_family_id`),
	CONSTRAINT `katao_product_FK_1`
		FOREIGN KEY (`katao_product_family_id`)
		REFERENCES `katao_product_family` (`id`)
		ON DELETE CASCADE,
	INDEX `katao_product_FI_2` (`katao_supplier_id`),
	CONSTRAINT `katao_product_FK_2`
		FOREIGN KEY (`katao_supplier_id`)
		REFERENCES `katao_supplier` (`id`)
		ON DELETE CASCADE
)Engine=InnoDB;

#-----------------------------------------------------------------------------
#-- katao_user
#-----------------------------------------------------------------------------

DROP TABLE IF EXISTS `katao_user`;


CREATE TABLE `katao_user`
(
	`id` INTEGER  NOT NULL AUTO_INCREMENT,
	`katao_member_id` INTEGER,
	`katao_supplier_id` INTEGER,
	`status` INTEGER default 2 NOT NULL,
	`email` VARCHAR(255),
	`address1` VARCHAR(255)  NOT NULL,
	`address2` VARCHAR(255),
	`zip` VARCHAR(255),
	`city` VARCHAR(255)  NOT NULL,
	`phone` VARCHAR(255),
	`fax` VARCHAR(255),
	`gmap_lng` DOUBLE,
	`gmap_lat` DOUBLE,
	`login` VARCHAR(255)  NOT NULL,
	`password` VARCHAR(255)  NOT NULL,
	`is_seo` INTEGER(1) default 0,
	`is_admin` INTEGER(1) default 0,
	`created_at` DATETIME,
	`updated_at` DATETIME,
	PRIMARY KEY (`id`),
	INDEX `katao_user_FI_1` (`katao_member_id`),
	CONSTRAINT `katao_user_FK_1`
		FOREIGN KEY (`katao_member_id`)
		REFERENCES `katao_member` (`id`)
		ON DELETE CASCADE,
	INDEX `katao_user_FI_2` (`katao_supplier_id`),
	CONSTRAINT `katao_user_FK_2`
		FOREIGN KEY (`katao_supplier_id`)
		REFERENCES `katao_supplier` (`id`)
		ON DELETE CASCADE
)Engine=InnoDB;

#-----------------------------------------------------------------------------
#-- katao_member
#-----------------------------------------------------------------------------

DROP TABLE IF EXISTS `katao_member`;


CREATE TABLE `katao_member`
(
	`id` INTEGER  NOT NULL AUTO_INCREMENT,
	`katao_index` INTEGER  NOT NULL,
	`katao_node_id` INTEGER  NOT NULL,
	`next_katao_node_id` INTEGER,
	`katao_member_id` INTEGER,
	`first_name` VARCHAR(255)  NOT NULL,
	`last_name` VARCHAR(255)  NOT NULL,
	`accounting_code` CHAR(6)  NOT NULL,
	`accounting_code_sol` CHAR(6)  NOT NULL,
	`initial_amount_euro` FLOAT default 0,
	`initial_amount_sol` INTEGER default 0,
	`is_referer` INTEGER(1) default 0,
	`is_delegate` INTEGER(1) default 0,
	`is_anonymous` INTEGER(1) default 0,
	`is_member` INTEGER(1) default 1,
	`card_number_sol` CHAR(8),
	`user_situation` VARCHAR(255),
	`user_birthday` DATE,
	`parrain_name` VARCHAR(255),
	`created_at` DATETIME,
	`updated_at` DATETIME,
	PRIMARY KEY (`id`),
	INDEX `katao_member_FI_1` (`katao_node_id`),
	CONSTRAINT `katao_member_FK_1`
		FOREIGN KEY (`katao_node_id`)
		REFERENCES `katao_node` (`id`)
		ON DELETE CASCADE,
	INDEX `katao_member_FI_2` (`next_katao_node_id`),
	CONSTRAINT `katao_member_FK_2`
		FOREIGN KEY (`next_katao_node_id`)
		REFERENCES `katao_node` (`id`)
		ON DELETE SET NULL,
	INDEX `katao_member_FI_3` (`katao_member_id`),
	CONSTRAINT `katao_member_FK_3`
		FOREIGN KEY (`katao_member_id`)
		REFERENCES `katao_member` (`id`)
		ON DELETE SET NULL
)Engine=InnoDB;

#-----------------------------------------------------------------------------
#-- katao_supplier
#-----------------------------------------------------------------------------

DROP TABLE IF EXISTS `katao_supplier`;


CREATE TABLE `katao_supplier`
(
	`id` INTEGER  NOT NULL AUTO_INCREMENT,
	`katao_index` INTEGER  NOT NULL,
	`member_id` INTEGER,
	`referer_id` INTEGER,
	`name` VARCHAR(255)  NOT NULL,
	`url_identifier` VARCHAR(255)  NOT NULL,
	`presentation` TEXT,
	`email` VARCHAR(255),
	`address1` VARCHAR(255)  NOT NULL,
	`address2` VARCHAR(255),
	`zip` VARCHAR(255),
	`city` VARCHAR(255)  NOT NULL,
	`phone` VARCHAR(255),
	`fax` VARCHAR(255),
	`website` VARCHAR(255),
	`gmap_lng` DOUBLE,
	`gmap_lat` DOUBLE,
	`accounting_code` CHAR(6)  NOT NULL,
	`accounting_code_sol` CHAR(6)  NOT NULL,
	`siret_number` CHAR(14)  NOT NULL,
	`is_global` INTEGER(1) default 0,
	`order_min_amount` FLOAT default 0,
	`created_at` DATETIME,
	`updated_at` DATETIME,
	PRIMARY KEY (`id`),
	UNIQUE KEY `katao_supplier_U_1` (`siret_number`),
	INDEX `katao_supplier_FI_1` (`member_id`),
	CONSTRAINT `katao_supplier_FK_1`
		FOREIGN KEY (`member_id`)
		REFERENCES `katao_member` (`id`)
		ON DELETE CASCADE,
	INDEX `katao_supplier_FI_2` (`referer_id`),
	CONSTRAINT `katao_supplier_FK_2`
		FOREIGN KEY (`referer_id`)
		REFERENCES `katao_member` (`id`)
		ON DELETE CASCADE
)Engine=InnoDB;

#-----------------------------------------------------------------------------
#-- katao_supplier_node
#-----------------------------------------------------------------------------

DROP TABLE IF EXISTS `katao_supplier_node`;


CREATE TABLE `katao_supplier_node`
(
	`katao_supplier_id` INTEGER  NOT NULL,
	`katao_node_id` INTEGER  NOT NULL,
	PRIMARY KEY (`katao_supplier_id`,`katao_node_id`),
	CONSTRAINT `katao_supplier_node_FK_1`
		FOREIGN KEY (`katao_supplier_id`)
		REFERENCES `katao_supplier` (`id`)
		ON UPDATE CASCADE
		ON DELETE CASCADE,
	INDEX `katao_supplier_node_FI_2` (`katao_node_id`),
	CONSTRAINT `katao_supplier_node_FK_2`
		FOREIGN KEY (`katao_node_id`)
		REFERENCES `katao_node` (`id`)
		ON UPDATE CASCADE
		ON DELETE CASCADE
)Engine=InnoDB;

#-----------------------------------------------------------------------------
#-- katao_node_product
#-----------------------------------------------------------------------------

DROP TABLE IF EXISTS `katao_node_product`;


CREATE TABLE `katao_node_product`
(
	`id` INTEGER  NOT NULL AUTO_INCREMENT,
	`katao_node_id` INTEGER  NOT NULL,
	`katao_period_id` INTEGER  NOT NULL,
	`katao_product_id` INTEGER  NOT NULL,
	PRIMARY KEY (`id`),
	INDEX `katao_node_product_FI_1` (`katao_node_id`),
	CONSTRAINT `katao_node_product_FK_1`
		FOREIGN KEY (`katao_node_id`)
		REFERENCES `katao_node` (`id`)
		ON DELETE CASCADE,
	INDEX `katao_node_product_FI_2` (`katao_period_id`),
	CONSTRAINT `katao_node_product_FK_2`
		FOREIGN KEY (`katao_period_id`)
		REFERENCES `katao_period` (`id`)
		ON DELETE CASCADE,
	INDEX `katao_node_product_FI_3` (`katao_product_id`),
	CONSTRAINT `katao_node_product_FK_3`
		FOREIGN KEY (`katao_product_id`)
		REFERENCES `katao_product` (`id`)
		ON DELETE CASCADE
)Engine=InnoDB;

#-----------------------------------------------------------------------------
#-- katao_node_supplier
#-----------------------------------------------------------------------------

DROP TABLE IF EXISTS `katao_node_supplier`;


CREATE TABLE `katao_node_supplier`
(
	`id` INTEGER  NOT NULL AUTO_INCREMENT,
	`katao_node_id` INTEGER  NOT NULL,
	`katao_period_id` INTEGER  NOT NULL,
	`katao_supplier_id` INTEGER  NOT NULL,
	`include_all_products` INTEGER(1) default 0 NOT NULL,
	PRIMARY KEY (`id`),
	INDEX `katao_node_supplier_FI_1` (`katao_node_id`),
	CONSTRAINT `katao_node_supplier_FK_1`
		FOREIGN KEY (`katao_node_id`)
		REFERENCES `katao_node` (`id`)
		ON DELETE CASCADE,
	INDEX `katao_node_supplier_FI_2` (`katao_period_id`),
	CONSTRAINT `katao_node_supplier_FK_2`
		FOREIGN KEY (`katao_period_id`)
		REFERENCES `katao_period` (`id`)
		ON DELETE CASCADE,
	INDEX `katao_node_supplier_FI_3` (`katao_supplier_id`),
	CONSTRAINT `katao_node_supplier_FK_3`
		FOREIGN KEY (`katao_supplier_id`)
		REFERENCES `katao_supplier` (`id`)
		ON DELETE CASCADE
)Engine=InnoDB;

#-----------------------------------------------------------------------------
#-- katao_period_product
#-----------------------------------------------------------------------------

DROP TABLE IF EXISTS `katao_period_product`;


CREATE TABLE `katao_period_product`
(
	`id` INTEGER  NOT NULL AUTO_INCREMENT,
	`katao_period_id` INTEGER  NOT NULL,
	`katao_product_id` INTEGER  NOT NULL,
	PRIMARY KEY (`id`),
	INDEX `katao_period_product_FI_1` (`katao_period_id`),
	CONSTRAINT `katao_period_product_FK_1`
		FOREIGN KEY (`katao_period_id`)
		REFERENCES `katao_period` (`id`)
		ON DELETE CASCADE,
	INDEX `katao_period_product_FI_2` (`katao_product_id`),
	CONSTRAINT `katao_period_product_FK_2`
		FOREIGN KEY (`katao_product_id`)
		REFERENCES `katao_product` (`id`)
		ON DELETE CASCADE
)Engine=InnoDB;

#-----------------------------------------------------------------------------
#-- katao_period_supplier
#-----------------------------------------------------------------------------

DROP TABLE IF EXISTS `katao_period_supplier`;


CREATE TABLE `katao_period_supplier`
(
	`id` INTEGER  NOT NULL AUTO_INCREMENT,
	`katao_period_id` INTEGER  NOT NULL,
	`katao_supplier_id` INTEGER  NOT NULL,
	`include_all_products` INTEGER(1) default 0 NOT NULL,
	PRIMARY KEY (`id`),
	INDEX `katao_period_supplier_FI_1` (`katao_period_id`),
	CONSTRAINT `katao_period_supplier_FK_1`
		FOREIGN KEY (`katao_period_id`)
		REFERENCES `katao_period` (`id`)
		ON DELETE CASCADE,
	INDEX `katao_period_supplier_FI_2` (`katao_supplier_id`),
	CONSTRAINT `katao_period_supplier_FK_2`
		FOREIGN KEY (`katao_supplier_id`)
		REFERENCES `katao_supplier` (`id`)
		ON DELETE CASCADE
)Engine=InnoDB;

#-----------------------------------------------------------------------------
#-- katao_cart
#-----------------------------------------------------------------------------

DROP TABLE IF EXISTS `katao_cart`;


CREATE TABLE `katao_cart`
(
	`id` INTEGER  NOT NULL AUTO_INCREMENT,
	`katao_user_id` INTEGER  NOT NULL,
	`katao_period_id` INTEGER  NOT NULL,
	`katao_node_id` INTEGER  NOT NULL,
	`status` INTEGER default 1 NOT NULL,
	`created_at` DATETIME,
	`updated_at` DATETIME,
	PRIMARY KEY (`id`),
	INDEX `katao_cart_FI_1` (`katao_user_id`),
	CONSTRAINT `katao_cart_FK_1`
		FOREIGN KEY (`katao_user_id`)
		REFERENCES `katao_user` (`id`)
		ON DELETE CASCADE,
	INDEX `katao_cart_FI_2` (`katao_period_id`),
	CONSTRAINT `katao_cart_FK_2`
		FOREIGN KEY (`katao_period_id`)
		REFERENCES `katao_period` (`id`)
		ON DELETE CASCADE,
	INDEX `katao_cart_FI_3` (`katao_node_id`),
	CONSTRAINT `katao_cart_FK_3`
		FOREIGN KEY (`katao_node_id`)
		REFERENCES `katao_node` (`id`)
		ON DELETE CASCADE
)Engine=InnoDB;

#-----------------------------------------------------------------------------
#-- katao_cart_product
#-----------------------------------------------------------------------------

DROP TABLE IF EXISTS `katao_cart_product`;


CREATE TABLE `katao_cart_product`
(
	`id` INTEGER  NOT NULL AUTO_INCREMENT,
	`katao_cart_id` INTEGER  NOT NULL,
	`katao_product_id` INTEGER  NOT NULL,
	`product_reference` VARCHAR(255),
	`product_name` VARCHAR(255)  NOT NULL,
	`product_price_euro` FLOAT  NOT NULL,
	`product_margin` FLOAT,
	`product_tva_rate` FLOAT default 0,
	`quantity` FLOAT default 1 NOT NULL,
	`quantity_adjusted` FLOAT default 0,
	`has_been_adjusted` INTEGER(1) default 0,
	`quantity_delivered` FLOAT default 0,
	`katao_order_product_id` INTEGER,
	`created_at` DATETIME,
	`updated_at` DATETIME,
	PRIMARY KEY (`id`),
	INDEX `katao_cart_product_FI_1` (`katao_cart_id`),
	CONSTRAINT `katao_cart_product_FK_1`
		FOREIGN KEY (`katao_cart_id`)
		REFERENCES `katao_cart` (`id`)
		ON DELETE CASCADE,
	INDEX `katao_cart_product_FI_2` (`katao_product_id`),
	CONSTRAINT `katao_cart_product_FK_2`
		FOREIGN KEY (`katao_product_id`)
		REFERENCES `katao_product` (`id`)
		ON DELETE CASCADE,
	INDEX `katao_cart_product_FI_3` (`katao_order_product_id`),
	CONSTRAINT `katao_cart_product_FK_3`
		FOREIGN KEY (`katao_order_product_id`)
		REFERENCES `katao_order_product` (`id`)
		ON DELETE SET NULL
)Engine=InnoDB;

#-----------------------------------------------------------------------------
#-- katao_order
#-----------------------------------------------------------------------------

DROP TABLE IF EXISTS `katao_order`;


CREATE TABLE `katao_order`
(
	`id` INTEGER  NOT NULL AUTO_INCREMENT,
	`katao_supplier_id` INTEGER  NOT NULL,
	`katao_period_id` INTEGER  NOT NULL,
	`katao_node_id` INTEGER  NOT NULL,
	`status` INTEGER default 1 NOT NULL,
	`is_simulated` INTEGER(1) default 0,
	`supplier_name` VARCHAR(255)  NOT NULL,
	`supplier_email` VARCHAR(255)  NOT NULL,
	`supplier_address1` VARCHAR(255)  NOT NULL,
	`supplier_address2` VARCHAR(255),
	`supplier_zip` VARCHAR(255),
	`supplier_city` VARCHAR(255)  NOT NULL,
	`katao_name` VARCHAR(255)  NOT NULL,
	`katao_address` TEXT,
	`katao_siret_number` CHAR(14)  NOT NULL,
	`katao_rcs_number` VARCHAR(255)  NOT NULL,
	`katao_capital` INTEGER  NOT NULL,
	`delivery_address` TEXT,
	`created_at` DATETIME,
	`updated_at` DATETIME,
	PRIMARY KEY (`id`),
	INDEX `katao_order_FI_1` (`katao_supplier_id`),
	CONSTRAINT `katao_order_FK_1`
		FOREIGN KEY (`katao_supplier_id`)
		REFERENCES `katao_supplier` (`id`)
		ON DELETE CASCADE,
	INDEX `katao_order_FI_2` (`katao_period_id`),
	CONSTRAINT `katao_order_FK_2`
		FOREIGN KEY (`katao_period_id`)
		REFERENCES `katao_period` (`id`)
		ON DELETE CASCADE,
	INDEX `katao_order_FI_3` (`katao_node_id`),
	CONSTRAINT `katao_order_FK_3`
		FOREIGN KEY (`katao_node_id`)
		REFERENCES `katao_node` (`id`)
		ON DELETE CASCADE
)Engine=InnoDB;

#-----------------------------------------------------------------------------
#-- katao_order_product
#-----------------------------------------------------------------------------

DROP TABLE IF EXISTS `katao_order_product`;


CREATE TABLE `katao_order_product`
(
	`id` INTEGER  NOT NULL AUTO_INCREMENT,
	`katao_order_id` INTEGER,
	`katao_product_id` INTEGER,
	`product_reference` VARCHAR(255),
	`product_name` VARCHAR(255)  NOT NULL,
	`product_price_euro` FLOAT  NOT NULL,
	`product_margin` FLOAT,
	`product_tva_rate` FLOAT default 0,
	`quantity` FLOAT default 1 NOT NULL,
	`quantity_received` FLOAT default 0,
	`quantity_delivered` FLOAT default 0,
	`has_been_received` INTEGER(1) default 0,
	`has_been_delivered` INTEGER(1) default 0,
	`created_at` DATETIME,
	`updated_at` DATETIME,
	PRIMARY KEY (`id`),
	INDEX `katao_order_product_FI_1` (`katao_order_id`),
	CONSTRAINT `katao_order_product_FK_1`
		FOREIGN KEY (`katao_order_id`)
		REFERENCES `katao_order` (`id`)
		ON DELETE CASCADE,
	INDEX `katao_order_product_FI_2` (`katao_product_id`),
	CONSTRAINT `katao_order_product_FK_2`
		FOREIGN KEY (`katao_product_id`)
		REFERENCES `katao_product` (`id`)
		ON DELETE CASCADE
)Engine=InnoDB;

#-----------------------------------------------------------------------------
#-- katao_invoice
#-----------------------------------------------------------------------------

DROP TABLE IF EXISTS `katao_invoice`;


CREATE TABLE `katao_invoice`
(
	`id` INTEGER  NOT NULL AUTO_INCREMENT,
	`katao_member_id` INTEGER  NOT NULL,
	`katao_period_id` INTEGER  NOT NULL,
	`katao_node_id` INTEGER  NOT NULL,
	`katao_cart_id` INTEGER,
	`status` INTEGER default 1 NOT NULL,
	`member_first_name` VARCHAR(255)  NOT NULL,
	`member_last_name` VARCHAR(255)  NOT NULL,
	`member_email` VARCHAR(255)  NOT NULL,
	`member_address1` VARCHAR(255)  NOT NULL,
	`member_address2` VARCHAR(255),
	`member_zip` VARCHAR(255),
	`member_city` VARCHAR(255)  NOT NULL,
	`katao_name` VARCHAR(255)  NOT NULL,
	`katao_address` TEXT,
	`katao_siret_number` CHAR(14)  NOT NULL,
	`katao_rcs_number` VARCHAR(255)  NOT NULL,
	`katao_capital` INTEGER  NOT NULL,
	`sol_amount` INTEGER default 0,
	`created_at` DATETIME,
	`updated_at` DATETIME,
	PRIMARY KEY (`id`),
	INDEX `katao_invoice_FI_1` (`katao_member_id`),
	CONSTRAINT `katao_invoice_FK_1`
		FOREIGN KEY (`katao_member_id`)
		REFERENCES `katao_member` (`id`)
		ON DELETE CASCADE,
	INDEX `katao_invoice_FI_2` (`katao_period_id`),
	CONSTRAINT `katao_invoice_FK_2`
		FOREIGN KEY (`katao_period_id`)
		REFERENCES `katao_period` (`id`)
		ON DELETE CASCADE,
	INDEX `katao_invoice_FI_3` (`katao_node_id`),
	CONSTRAINT `katao_invoice_FK_3`
		FOREIGN KEY (`katao_node_id`)
		REFERENCES `katao_node` (`id`)
		ON DELETE CASCADE,
	INDEX `katao_invoice_FI_4` (`katao_cart_id`),
	CONSTRAINT `katao_invoice_FK_4`
		FOREIGN KEY (`katao_cart_id`)
		REFERENCES `katao_cart` (`id`)
		ON DELETE CASCADE
)Engine=InnoDB;

#-----------------------------------------------------------------------------
#-- katao_invoice_product
#-----------------------------------------------------------------------------

DROP TABLE IF EXISTS `katao_invoice_product`;


CREATE TABLE `katao_invoice_product`
(
	`id` INTEGER  NOT NULL AUTO_INCREMENT,
	`katao_invoice_id` INTEGER  NOT NULL,
	`katao_product_id` INTEGER  NOT NULL,
	`katao_cart_product_id` INTEGER  NOT NULL,
	`katao_order_product_id` INTEGER  NOT NULL,
	`product_reference` VARCHAR(255),
	`product_name` VARCHAR(255)  NOT NULL,
	`product_price_euro` FLOAT  NOT NULL,
	`product_margin` FLOAT,
	`product_tva_rate` FLOAT default 0,
	`quantity` FLOAT default 1 NOT NULL,
	`quantity_delivered` FLOAT default 0,
	`sol_amount` INTEGER default 0,
	`created_at` DATETIME,
	`updated_at` DATETIME,
	PRIMARY KEY (`id`),
	INDEX `katao_invoice_product_FI_1` (`katao_invoice_id`),
	CONSTRAINT `katao_invoice_product_FK_1`
		FOREIGN KEY (`katao_invoice_id`)
		REFERENCES `katao_invoice` (`id`)
		ON DELETE CASCADE,
	INDEX `katao_invoice_product_FI_2` (`katao_product_id`),
	CONSTRAINT `katao_invoice_product_FK_2`
		FOREIGN KEY (`katao_product_id`)
		REFERENCES `katao_product` (`id`)
		ON DELETE CASCADE,
	INDEX `katao_invoice_product_FI_3` (`katao_cart_product_id`),
	CONSTRAINT `katao_invoice_product_FK_3`
		FOREIGN KEY (`katao_cart_product_id`)
		REFERENCES `katao_cart_product` (`id`)
		ON DELETE CASCADE,
	INDEX `katao_invoice_product_FI_4` (`katao_order_product_id`),
	CONSTRAINT `katao_invoice_product_FK_4`
		FOREIGN KEY (`katao_order_product_id`)
		REFERENCES `katao_order_product` (`id`)
		ON DELETE CASCADE
)Engine=InnoDB;

#-----------------------------------------------------------------------------
#-- katao_member_deposit
#-----------------------------------------------------------------------------

DROP TABLE IF EXISTS `katao_member_deposit`;


CREATE TABLE `katao_member_deposit`
(
	`id` INTEGER  NOT NULL AUTO_INCREMENT,
	`katao_member_id` INTEGER  NOT NULL,
	`amount` FLOAT  NOT NULL,
	`comment` TEXT,
	`currency` INTEGER default 1 NOT NULL,
	`payment_mode` INTEGER default 0,
	`check_number` VARCHAR(255),
	`check_bank` VARCHAR(255),
	`check_deposit` VARCHAR(255),
	`status` INTEGER default 1 NOT NULL,
	`valued_at` DATETIME,
	`valid_until` DATETIME,
	`created_at` DATETIME,
	`updated_at` DATETIME,
	PRIMARY KEY (`id`),
	INDEX `katao_member_deposit_FI_1` (`katao_member_id`),
	CONSTRAINT `katao_member_deposit_FK_1`
		FOREIGN KEY (`katao_member_id`)
		REFERENCES `katao_member` (`id`)
		ON DELETE CASCADE
)Engine=InnoDB;

#-----------------------------------------------------------------------------
#-- katao_member_node
#-----------------------------------------------------------------------------

DROP TABLE IF EXISTS `katao_member_node`;


CREATE TABLE `katao_member_node`
(
	`id` INTEGER  NOT NULL AUTO_INCREMENT,
	`katao_member_id` INTEGER  NOT NULL,
	`katao_node_id` INTEGER  NOT NULL,
	`created_at` DATETIME,
	PRIMARY KEY (`id`),
	INDEX `katao_member_node_FI_1` (`katao_member_id`),
	CONSTRAINT `katao_member_node_FK_1`
		FOREIGN KEY (`katao_member_id`)
		REFERENCES `katao_member` (`id`)
		ON DELETE CASCADE,
	INDEX `katao_member_node_FI_2` (`katao_node_id`),
	CONSTRAINT `katao_member_node_FK_2`
		FOREIGN KEY (`katao_node_id`)
		REFERENCES `katao_node` (`id`)
		ON DELETE CASCADE
)Engine=InnoDB;

#-----------------------------------------------------------------------------
#-- katao_supplier_invoice
#-----------------------------------------------------------------------------

DROP TABLE IF EXISTS `katao_supplier_invoice`;


CREATE TABLE `katao_supplier_invoice`
(
	`id` INTEGER  NOT NULL AUTO_INCREMENT,
	`katao_supplier_id` INTEGER  NOT NULL,
	`katao_period_id` INTEGER  NOT NULL,
	`status` INTEGER default 1 NOT NULL,
	`supplier_name` VARCHAR(255)  NOT NULL,
	`supplier_email` VARCHAR(255)  NOT NULL,
	`supplier_address1` VARCHAR(255)  NOT NULL,
	`supplier_address2` VARCHAR(255),
	`supplier_zip` VARCHAR(255),
	`supplier_city` VARCHAR(255)  NOT NULL,
	`katao_name` VARCHAR(255)  NOT NULL,
	`katao_address` TEXT,
	`katao_siret_number` CHAR(14)  NOT NULL,
	`katao_rcs_number` VARCHAR(255)  NOT NULL,
	`katao_capital` INTEGER  NOT NULL,
	`reference` VARCHAR(255),
	`invoice_date` DATETIME  NOT NULL,
	`due_date` DATETIME  NOT NULL,
	`payment_date` DATETIME,
	`payment_mode` INTEGER default 1 NOT NULL,
	`payment_details` VARCHAR(255),
	`payment_amount_euro` FLOAT default 0,
	`payment_amount_sol` FLOAT default 0,
	`taux0_bases_ht` FLOAT default 0,
	`[REDACTED]` FLOAT default 0,
	`taux0_amount` FLOAT default 0,
	`taux1_bases_ht` FLOAT default 0,
	`[REDACTED]` FLOAT default 0,
	`taux1_amount` FLOAT default 0,
	`taux2_bases_ht` FLOAT default 0,
	`[REDACTED]` FLOAT default 0,
	`taux2_amount` FLOAT default 0,
	`total_ht` FLOAT default 0,
	`total_tva` FLOAT default 0,
	`comment` TEXT,
	`fees_shipping_ht` FLOAT default 0,
	`fees_shipping_rate` FLOAT default 0,
	`fees_shipping_tva` FLOAT default 0,
	`fees_billing_ht` FLOAT default 0,
	`fees_billing_rate` FLOAT default 0,
	`fees_billing_tva` FLOAT default 0,
	`created_at` DATETIME,
	`updated_at` DATETIME,
	PRIMARY KEY (`id`),
	INDEX `katao_supplier_invoice_FI_1` (`katao_supplier_id`),
	CONSTRAINT `katao_supplier_invoice_FK_1`
		FOREIGN KEY (`katao_supplier_id`)
		REFERENCES `katao_supplier` (`id`)
		ON DELETE CASCADE,
	INDEX `katao_supplier_invoice_FI_2` (`katao_period_id`),
	CONSTRAINT `katao_supplier_invoice_FK_2`
		FOREIGN KEY (`katao_period_id`)
		REFERENCES `katao_period` (`id`)
		ON DELETE CASCADE
)Engine=InnoDB;

#-----------------------------------------------------------------------------
#-- katao_supplier_invoice_product
#-----------------------------------------------------------------------------

DROP TABLE IF EXISTS `katao_supplier_invoice_product`;


CREATE TABLE `katao_supplier_invoice_product`
(
	`id` INTEGER  NOT NULL AUTO_INCREMENT,
	`katao_supplier_invoice_id` INTEGER,
	`katao_product_id` INTEGER,
	`product_reference` VARCHAR(255),
	`product_name` VARCHAR(255)  NOT NULL,
	`product_price_euro` FLOAT  NOT NULL,
	`product_margin` FLOAT,
	`product_tva_rate` FLOAT default 0,
	`quantity` FLOAT default 1 NOT NULL,
	`product_price_total` FLOAT default 0,
	`created_at` DATETIME,
	`updated_at` DATETIME,
	PRIMARY KEY (`id`),
	INDEX `katao_supplier_invoice_product_FI_1` (`katao_supplier_invoice_id`),
	CONSTRAINT `katao_supplier_invoice_product_FK_1`
		FOREIGN KEY (`katao_supplier_invoice_id`)
		REFERENCES `katao_supplier_invoice` (`id`)
		ON DELETE CASCADE,
	INDEX `katao_supplier_invoice_product_FI_2` (`katao_product_id`),
	CONSTRAINT `katao_supplier_invoice_product_FK_2`
		FOREIGN KEY (`katao_product_id`)
		REFERENCES `katao_product` (`id`)
		ON DELETE CASCADE
)Engine=InnoDB;

#-----------------------------------------------------------------------------
#-- katao_supplier_invoice_order
#-----------------------------------------------------------------------------

DROP TABLE IF EXISTS `katao_supplier_invoice_order`;


CREATE TABLE `katao_supplier_invoice_order`
(
	`katao_supplier_invoice_id` INTEGER  NOT NULL,
	`katao_order_id` INTEGER  NOT NULL,
	PRIMARY KEY (`katao_supplier_invoice_id`,`katao_order_id`),
	CONSTRAINT `katao_supplier_invoice_order_FK_1`
		FOREIGN KEY (`katao_supplier_invoice_id`)
		REFERENCES `katao_supplier_invoice` (`id`)
		ON UPDATE CASCADE
		ON DELETE CASCADE,
	INDEX `katao_supplier_invoice_order_FI_2` (`katao_order_id`),
	CONSTRAINT `katao_supplier_invoice_order_FK_2`
		FOREIGN KEY (`katao_order_id`)
		REFERENCES `katao_order` (`id`)
		ON UPDATE CASCADE
		ON DELETE CASCADE
)Engine=InnoDB;

#-----------------------------------------------------------------------------
#-- katao_supplier_product_category
#-----------------------------------------------------------------------------

DROP TABLE IF EXISTS `katao_supplier_product_category`;


CREATE TABLE `katao_supplier_product_category`
(
	`katao_supplier_id` INTEGER  NOT NULL,
	`katao_product_category_id` INTEGER  NOT NULL,
	PRIMARY KEY (`katao_supplier_id`,`katao_product_category_id`),
	CONSTRAINT `katao_supplier_product_category_FK_1`
		FOREIGN KEY (`katao_supplier_id`)
		REFERENCES `katao_supplier` (`id`)
		ON UPDATE CASCADE
		ON DELETE CASCADE,
	INDEX `katao_supplier_product_category_FI_2` (`katao_product_category_id`),
	CONSTRAINT `katao_supplier_product_category_FK_2`
		FOREIGN KEY (`katao_product_category_id`)
		REFERENCES `katao_product_category` (`id`)
		ON UPDATE CASCADE
		ON DELETE CASCADE
)Engine=InnoDB;

#-----------------------------------------------------------------------------
#-- katao_member_group
#-----------------------------------------------------------------------------

DROP TABLE IF EXISTS `katao_member_group`;


CREATE TABLE `katao_member_group`
(
	`id` INTEGER  NOT NULL AUTO_INCREMENT,
	`katao_member_group_name` VARCHAR(255),
	PRIMARY KEY (`id`)
)Engine=InnoDB;

#-----------------------------------------------------------------------------
#-- katao_assoc_member_group
#-----------------------------------------------------------------------------

DROP TABLE IF EXISTS `katao_assoc_member_group`;


CREATE TABLE `katao_assoc_member_group`
(
	`member_id` INTEGER  NOT NULL,
	`group_id` INTEGER  NOT NULL,
	PRIMARY KEY (`member_id`,`group_id`),
	CONSTRAINT `katao_assoc_member_group_FK_1`
		FOREIGN KEY (`member_id`)
		REFERENCES `katao_member` (`id`)
		ON DELETE CASCADE,
	INDEX `katao_assoc_member_group_FI_2` (`group_id`),
	CONSTRAINT `katao_assoc_member_group_FK_2`
		FOREIGN KEY (`group_id`)
		REFERENCES `katao_member_group` (`id`)
		ON DELETE CASCADE
)Engine=InnoDB;

#-----------------------------------------------------------------------------
#-- katao_member_adhesion
#-----------------------------------------------------------------------------

DROP TABLE IF EXISTS `katao_member_adhesion`;


CREATE TABLE `katao_member_adhesion`
(
	`id` INTEGER  NOT NULL AUTO_INCREMENT,
	`katao_user_id` INTEGER  NOT NULL,
	`year` DATE  NOT NULL,
	`amount` FLOAT  NOT NULL,
	`type` INTEGER(2),
	`payment_kind` INTEGER  NOT NULL,
	`check_no` VARCHAR(255),
	PRIMARY KEY (`id`),
	INDEX `katao_member_adhesion_FI_1` (`katao_user_id`),
	CONSTRAINT `katao_member_adhesion_FK_1`
		FOREIGN KEY (`katao_user_id`)
		REFERENCES `katao_user` (`id`)
)Engine=InnoDB;

# This restores the fkey checks, after having unset them earlier
SET FOREIGN_KEY_CHECKS = 1;
