ALTER TABLE `obchodny_partner`
  ADD COLUMN `splatnost_faktury` INT(11) NULL DEFAULT NULL AFTER `ina_korespondencna_adresa`;

INSERT INTO `form_element` (`id`, `form_group_id`, `form_element_type_id`, `db_table`, `db_column`, `db_multi_table`, `db_multivalue_table`, `column_title`, `position`, `attribute`, `width`, `height`, `help`, `example`, `direct_to_db`, `required`, `number`, `float_number`, `email`, `css_style`, `css_class`, `javascript`, `direct_multi_label`, `direct_multi_data`, `default_val`, `vdaemon`, `add_empty_option`, `html`, `lang_code`, `read_only`) VALUES (117, 26000, 1, 'obchodny_partner', 'splatnost_faktury', NULL, NULL, 'Štandardná splatnosť faktúry', 170, NULL, NULL, NULL, NULL, NULL, 1, NULL, 1, NULL, NULL, NULL, NULL, NULL, NULL, NULL, '14', NULL, NULL, NULL, NULL, NULL);

UPDATE obchodny_partner SET splatnost_faktury = 14 WHERE visibility_type = 1;
UPDATE `menu` SET `poradie`='120' WHERE  `id`=6000;

ALTER TABLE `faktura`
  ADD COLUMN `export` TINYINT(1) NOT NULL DEFAULT '0' AFTER `cislo_objednavky`;
ALTER TABLE `faktura`
  ALTER `export` DROP DEFAULT;
ALTER TABLE `faktura`
  CHANGE COLUMN `export` `export` DATETIME NULL AFTER `cislo_objednavky`;

/*1*/ INSERT INTO `form_element` (`id`, `form_group_id`, `form_element_type_id`, `db_table`, `db_column`, `column_title`, `position`, `attribute`, `direct_to_db`, `javascript`, `vdaemon`, `html`, `read_only`) VALUES ('6107', '6001', '11', 'faktura', 'datum_exportu', 'Dátum exportu', '40', NULL, '0', NULL, NULL, NULL, '1');
/*2*/ UPDATE `form_element` SET `attribute`='{"datetime_format":"d.m.Y H:i:s","step":1}' WHERE  `id`=6107;
/*3*/ ALTER TABLE `faktura`
  CHANGE COLUMN `export` `datum_exportu` DATETIME NULL DEFAULT NULL AFTER `cislo_objednavky`;
/*4*/ INSERT INTO `ucet` (`id`, `banka_id`, `nazov`, `visibility_type`) VALUES ('1', '1', 'Gajos', '1');
/*5*/ UPDATE `form_element` SET `form_element_type_id`='13' WHERE  `id`=6010;
/*6*/ DELETE FROM `form_element` WHERE  `id`=6010;
/*7*/ ALTER TABLE `faktura`
  ADD COLUMN `poznamka` TEXT NULL DEFAULT NULL AFTER `datum_exportu`;
/*9*/ INSERT INTO `form_element` (`id`, `form_group_id`, `form_element_type_id`, `db_table`, `db_column`, `column_title`, `position`, `attribute`, `direct_to_db`, `required`, `javascript`, `vdaemon`, `html`) VALUES ('6108', '6003', '2', 'faktura', 'horny_text', 'Text nad položkami', '50', NULL, '1', '0', NULL, NULL, NULL);
/*10*/ INSERT INTO `form_element` (`id`, `form_group_id`, `form_element_type_id`, `db_table`, `db_column`, `column_title`, `position`, `attribute`, `direct_to_db`, `required`, `javascript`, `vdaemon`, `html`) VALUES ('6109', '6003', '2', 'faktura', 'dolny_text', 'Text pod položkami', '60', NULL, '1', '0', NULL, NULL, NULL);
/*11*/ INSERT INTO `form_element` (`id`, `form_group_id`, `form_element_type_id`, `db_table`, `db_column`, `column_title`, `position`, `attribute`, `direct_to_db`, `required`, `javascript`, `vdaemon`, `html`) VALUES ('6110', '6003', '2', 'faktura', 'poznamka', 'Poznámka', '70', NULL, '1', '0', NULL, NULL, NULL);
/*12*/ UPDATE `form_element` SET `height`='40' WHERE  `id`=6108;
/*13*/ UPDATE `form_element` SET `height`='40' WHERE  `id`=6109;
/*14*/ UPDATE `form_element` SET `height`='40' WHERE  `id`=6110;
/*15*/ UPDATE `form_element` SET `db_column`='zmluva_id', `db_multi_table`='zmluva', `column_title`='Zmluva' WHERE  `id`=6011;
/*16*/ ALTER TABLE `faktura`
  ADD COLUMN `zmluva_id` INT(11) UNSIGNED NULL AFTER `poznamka`;
/*19*/ ALTER TABLE `faktura`
  ADD CONSTRAINT `FK_faktura_zmluva_id` FOREIGN KEY (`zmluva_id`) REFERENCES `zmluva` (`id`);

CREATE TABLE `stav_fakturacie_zmluvy` (
                                        `id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
                                        `nazov` VARCHAR(255) NULL DEFAULT NULL,
                                        PRIMARY KEY (`id`)
)
  COLLATE='utf8_general_ci'
  ENGINE=InnoDB
  AUTO_INCREMENT=3
;

INSERT INTO `stav_fakturacie_zmluvy` (`id`, `nazov`) VALUES (1, 'Nezadarená na fakturáciu');
INSERT INTO `stav_fakturacie_zmluvy` (`id`, `nazov`) VALUES (2, 'Fakturovať');

CREATE TABLE `fakturovat_ku` (
                               `id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
                               `nazov` VARCHAR(255) NULL DEFAULT NULL,
                               PRIMARY KEY (`id`)
)
  COLLATE='utf8_general_ci'
  ENGINE=InnoDB
  AUTO_INCREMENT=3
;

INSERT INTO `fakturovat_ku` (`id`, `nazov`) VALUES (1, 'Posledný deň v perióde');
INSERT INTO `fakturovat_ku` (`id`, `nazov`) VALUES (2, 'Zadaný deň v perióde');

CREATE TABLE `zasielanie_faktury` (
                                    `id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
                                    `nazov` VARCHAR(255) NULL DEFAULT NULL,
                                    PRIMARY KEY (`id`)
)
  COLLATE='utf8_general_ci'
  ENGINE=InnoDB
  AUTO_INCREMENT=4
;

INSERT INTO `zasielanie_faktury` (`id`, `nazov`) VALUES (1, 'E-mail');
INSERT INTO `zasielanie_faktury` (`id`, `nazov`) VALUES (2, 'Zelená pošta');
INSERT INTO `zasielanie_faktury` (`id`, `nazov`) VALUES (3, 'Klasická pošta');

CREATE TABLE `zmluva_fakturacia` (
                                   `id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
                                   `created_at` DATETIME NULL DEFAULT NULL,
                                   `updated_at` DATETIME NULL DEFAULT NULL,
                                   `stav_fakturacie_zmluvy_id` INT(11) UNSIGNED NULL DEFAULT NULL,
                                   `zasielanie_faktury_id` INT(11) UNSIGNED NULL DEFAULT NULL,
                                   `fakturovat_ku_id` INT(11) UNSIGNED NULL DEFAULT NULL,
                                   `zadany_den_v_periode` INT(11) UNSIGNED NULL DEFAULT NULL,
                                   `splatnost_faktury` INT(11) UNSIGNED NULL DEFAULT NULL,
                                   `prevadzka_nazov` VARCHAR(255) NULL DEFAULT NULL,
                                   `prevadzka_ulica` VARCHAR(255) NULL DEFAULT NULL,
                                   `prevadzka_psc` VARCHAR(255) NULL DEFAULT NULL,
                                   `prevadzka_mesto` VARCHAR(255) NULL DEFAULT NULL,
                                   `nazov` VARCHAR(255) NULL DEFAULT NULL,
                                   `ulica` VARCHAR(255) NULL DEFAULT NULL,
                                   `psc` VARCHAR(255) NULL DEFAULT NULL,
                                   `mesto` VARCHAR(255) NULL DEFAULT NULL,
                                   `horny_text` TEXT NULL,
                                   `dolny_text` TEXT NULL,
                                   PRIMARY KEY (`id`),
                                   INDEX `FK_zmluva_fakturacia_stav_fakturacie_zmluvy_id` (`stav_fakturacie_zmluvy_id`),
                                   INDEX `FK_zmluva_fakturacia_zasielanie_faktury_id` (`zasielanie_faktury_id`),
                                   INDEX `FK_zmluva_fakturacia_fakturovat_ku_id` (`fakturovat_ku_id`),
                                   CONSTRAINT `FK_zmluva_fakturacia_fakturovat_ku_id` FOREIGN KEY (`fakturovat_ku_id`) REFERENCES `fakturovat_ku` (`id`),
                                   CONSTRAINT `FK_zmluva_fakturacia_stav_fakturacie_zmluvy_id` FOREIGN KEY (`stav_fakturacie_zmluvy_id`) REFERENCES `stav_fakturacie_zmluvy` (`id`),
                                   CONSTRAINT `FK_zmluva_fakturacia_zasielanie_faktury_id` FOREIGN KEY (`zasielanie_faktury_id`) REFERENCES `zasielanie_faktury` (`id`)
)
  COLLATE='utf8_general_ci'
  ENGINE=InnoDB
;

CREATE TABLE `zmluva_fakturacia_zmluva` (
                                          `zmluva_fakturacia_id` INT(11) NOT NULL,
                                          `zmluva_id` INT(11) NOT NULL,
                                          PRIMARY KEY (`zmluva_fakturacia_id`, `zmluva_id`)
)
  COLLATE='utf8_general_ci'
  ENGINE=InnoDB
;

CREATE TABLE `zmluva_fakturacia_polozka` (
                                           `id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
                                           `nazov` VARCHAR(255) NULL DEFAULT NULL,
                                           `zmluva_fakturacia_id` INT(11) UNSIGNED NULL DEFAULT NULL,
                                           PRIMARY KEY (`id`),
                                           INDEX `FK_zmluva_fakturacia_polozka_zmluva_fakturacia_id` (`zmluva_fakturacia_id`),
                                           CONSTRAINT `FK_zmluva_fakturacia_polozka_zmluva_fakturacia_id` FOREIGN KEY (`zmluva_fakturacia_id`) REFERENCES `zmluva_fakturacia` (`id`)
)
  COLLATE='utf8_general_ci'
  ENGINE=InnoDB
;

/* 23:10:08 */ INSERT INTO `menu` (`id`, `menu_id`, `nazov`, `page`, `poradie`, `level`, `visibility_type`) VALUES ('30', '2', 'Fakturácia', 'zmluva_fakturacia', '60', '3', '1');
/* 23:21:08 */ INSERT INTO `form` (`id`, `nazov`, `code`) VALUES ('11', 'Fakturácia zmluvy', 'zmluva_fakturacia');
/* 23:22:05 */ INSERT INTO `form_group` (`id`, `form_id`, `nazov`, `code`, `position`, `poznamka`, `group_type`) VALUES ('23', '11', 'Základné informácie', 'zmluva_fakturacia', '10', NULL, '1');
/* 23:24:36 */ INSERT INTO `form_element` (`id`, `form_group_id`, `form_element_type_id`, `db_table`, `db_column`, `db_multi_table`, `db_multivalue_table`, `column_title`, `position`, `attribute`, `width`, `height`, `help`, `example`, `direct_to_db`, `required`, `number`, `float_number`, `email`, `css_style`, `css_class`, `javascript`, `direct_multi_label`, `direct_multi_data`, `default_val`, `vdaemon`, `add_empty_option`, `html`, `lang_code`, `read_only`) VALUES ('118', '23', '5', 'zmluva_fakturacia', 'stav_fakturacie_zmluvy_id', 'stav_fakturacie_zmluvy', NULL, 'Stav fakturácie', '10', NULL, NULL, NULL, NULL, NULL, '1', '1', NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
