INSERT INTO `zz_modules` (`name`, `name2`, `module_dir`, `options`, `options2`, `icon`, `version`, `compatibility`, `order`, `parent`, `default`, `default_menu`, `enabled`, `type`, `new`) VALUES
('Stati documento', '', 'stati_documenti', 'SELECT |fields| FROM `co_statidocumento` HAVING 1=1 order by `id`', '', 'fa fa-commenting-o', '', '', 2, 90, 1, 0, 1, 'Menu', 0);

UPDATE `zz_modules` SET `parent`=( SELECT `id` FROM (SELECT `id` FROM `zz_modules` `m2` WHERE `name`='Tabelle') AS `m2`) WHERE `name` IN('Stati documento');

INSERT INTO `zz_viste` (`id_module`, `name`, `query`, `order`, `search`, `slow`, `search_inside`, `order_by`, `enabled`, `summable`, `default`) VALUES
((SELECT id FROM zz_modules WHERE name = 'Stati documento'), 'id', 'id', 1, 0, 0, '', '', 0, 0, 0),
((SELECT id FROM zz_modules WHERE name = 'Stati documento'), 'Descrizione', 'descrizione', 2, 0, 0, '', '', 1, 0, 0);


INSERT INTO `zz_gruppi_viste` (`id_gruppo`, `id_vista`) VALUES
((SELECT `id` FROM `zz_gruppi` WHERE `nome`='Amministratori'), (SELECT `id` FROM `zz_viste` WHERE id_module =  (SELECT zz_modules.id FROM zz_modules WHERE zz_modules.name = 'Stati documento') AND `name`='id')),
((SELECT `id` FROM `zz_gruppi` WHERE `nome`='Tecnici'), (SELECT `id` FROM `zz_viste` WHERE id_module =  (SELECT zz_modules.id FROM zz_modules WHERE zz_modules.name = 'Stati documento') AND `name`='id')),
((SELECT `id` FROM `zz_gruppi` WHERE `nome`='Agenti'), (SELECT `id` FROM `zz_viste` WHERE id_module =  (SELECT zz_modules.id FROM zz_modules WHERE zz_modules.name = 'Stati documento') AND `name`='id')),
((SELECT `id` FROM `zz_gruppi` WHERE `nome`='Clienti'), (SELECT `id` FROM `zz_viste` WHERE id_module =  (SELECT zz_modules.id FROM zz_modules WHERE zz_modules.name = 'Stati documento') AND `name`='id')),
((SELECT `id` FROM `zz_gruppi` WHERE `nome`='Amministratori'), (SELECT `id` FROM `zz_viste` WHERE id_module =  (SELECT zz_modules.id FROM zz_modules WHERE zz_modules.name = 'Stati documento') AND `name`='Descrizione')),
((SELECT `id` FROM `zz_gruppi` WHERE `nome`='Tecnici'), (SELECT `id` FROM `zz_viste` WHERE id_module =  (SELECT zz_modules.id FROM zz_modules WHERE zz_modules.name = 'Stati documento') AND `name`='Descrizione')),
((SELECT `id` FROM `zz_gruppi` WHERE `nome`='Agenti'), (SELECT `id` FROM `zz_viste` WHERE id_module =  (SELECT zz_modules.id FROM zz_modules WHERE zz_modules.name = 'Stati documento') AND `name`='Descrizione')),
((SELECT `id` FROM `zz_gruppi` WHERE `nome`='Clienti'), (SELECT `id` FROM `zz_viste` WHERE id_module =  (SELECT zz_modules.id FROM zz_modules WHERE zz_modules.name = 'Stati documento') AND `name`='Descrizione'));


alter Table co_statidocumento add idmenu int(0) NOT NULL ;
alter Table co_righe_documenti add grassetto int(0) NOT NULL ;
alter Table co_righe_documenti add modificabile int(0) NOT NULL ;


CREATE TABLE `doc_documentale` (
  `idscheda` int(11) NOT NULL AUTO_INCREMENT,
  `nome` varchar(255) NOT NULL,
  `descrizione` varchar(5000) NOT NULL,
  `idanagrafica` int(11) NOT NULL,
  `data_archiviazione` date NOT NULL,
  `idstato` tinyint(4) NOT NULL,
  `note1` varchar(255) NOT NULL,
  `data_scadenza` date NOT NULL,
  `idcommessa` int(11) NOT NULL,
   PRIMARY KEY (`idscheda`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

CREATE TABLE `doc_documentale_gruppo` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `descrizione` varchar(255) NOT NULL,
  `colore` varchar(7) NOT NULL DEFAULT '#FFFFFF',
   PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

INSERT INTO `doc_documentale_gruppo` (`id`, `descrizione`, `colore`) VALUES
(1, 'Da Saldare', '#e87575'),
(2, 'Saldata', '#3fcc1d'),
(3, 'Sospesa', '#ffffff'),
(4, 'Generico Documento', '#FFFFFF');


INSERT INTO `zz_modules` (`name`, `name2`, `module_dir`, `options`, `options2`, `icon`, `version`, `compatibility`, `order`, `parent`, `default`, `default_menu`, `enabled`, `type`, `new`) VALUES
('Documentale', '', 'ms_documentale', 'SELECT |fields| FROM `doc_documentale` HAVING 1=1 order by `idscheda`', '', 'fa fa-file-text', '2.0.1', '2.0.1', 7, 0, 1, 0, 1, 'menu', 0),
('Documentale Gruppi', '', 'ms_documentale_gruppo', 'SELECT |fields| FROM `doc_documentale_gruppo` HAVING 1=1 order by `id`', '', 'fa fa-external-link', '2.0.1', '2.0.1', 1, 88, 1, 0, 1, 'menu', 0);

UPDATE `zz_modules` SET `parent`=( SELECT `id` FROM (SELECT `id` FROM `zz_modules` `m2` WHERE `name`='Documentale') AS `m2`) WHERE `name` IN('Documentale Gruppi');



INSERT INTO `zz_viste` (`id_module`, `name`, `query`, `order`, `search`, `slow`, `search_inside`, `order_by`, `enabled`, `summable`, `default`) VALUES
((SELECT id FROM zz_modules WHERE name = 'Documentale'), 'id', 'idscheda', 1, 0, 0, '', '', 0, 0, 0),
((SELECT id FROM zz_modules WHERE name = 'Documentale'), 'Descrizione', 'nome', 2, 0, 0, '', '', 1, 0, 0),
((SELECT id FROM zz_modules WHERE name = 'Documentale'), 'Archiviazione', 'DATE_FORMAT(data_archiviazione, \'%d/%m/%Y\')', 3, 0, 0, '', '', 1, 0, 0),
((SELECT id FROM zz_modules WHERE name = 'Documentale'), 'Scadenza', 'DATE_FORMAT(data_scadenza, \'%d/%m/%Y\')', 4, 0, 0, '', '', 1, 0, 0),
((SELECT id FROM zz_modules WHERE name = 'Documentale'), 'Stato', '(SELECT descrizione FROM doc_documentale_gruppo WHERE id=doc_documentale.idstato)', 5, 0, 0, '', '', 1, 0, 0),
((SELECT id FROM zz_modules WHERE name = 'Documentale Gruppi'), 'id', 'id', 1, 0, 0, '', '', 0, 0, 0),
((SELECT id FROM zz_modules WHERE name = 'Documentale Gruppi') ,'Descrizione', 'descrizione', 2, 0, 0, '', '', 1, 0, 0);


INSERT INTO `zz_gruppi_viste` (`id_gruppo`, `id_vista`) VALUES
((SELECT `id` FROM `zz_gruppi` WHERE `nome`='Amministratori'), (SELECT `id` FROM `zz_viste` WHERE id_module =  (SELECT zz_modules.id FROM zz_modules WHERE zz_modules.name = 'Documentale') AND `name`='id')),
((SELECT `id` FROM `zz_gruppi` WHERE `nome`='Tecnici'), (SELECT `id` FROM `zz_viste` WHERE id_module =  (SELECT zz_modules.id FROM zz_modules WHERE zz_modules.name = 'Documentale') AND `name`='id')),
((SELECT `id` FROM `zz_gruppi` WHERE `nome`='Agenti'), (SELECT `id` FROM `zz_viste` WHERE id_module =  (SELECT zz_modules.id FROM zz_modules WHERE zz_modules.name = 'Documentale') AND `name`='id')),
((SELECT `id` FROM `zz_gruppi` WHERE `nome`='Clienti'), (SELECT `id` FROM `zz_viste` WHERE id_module =  (SELECT zz_modules.id FROM zz_modules WHERE zz_modules.name = 'Documentale') AND `name`='id')),
((SELECT `id` FROM `zz_gruppi` WHERE `nome`='Amministratori'), (SELECT `id` FROM `zz_viste` WHERE id_module =  (SELECT zz_modules.id FROM zz_modules WHERE zz_modules.name = 'Documentale') AND `name`='Descrizione')),
((SELECT `id` FROM `zz_gruppi` WHERE `nome`='Tecnici'), (SELECT `id` FROM `zz_viste` WHERE id_module =  (SELECT zz_modules.id FROM zz_modules WHERE zz_modules.name = 'Documentale') AND `name`='Descrizione')),
((SELECT `id` FROM `zz_gruppi` WHERE `nome`='Agenti'), (SELECT `id` FROM `zz_viste` WHERE id_module =  (SELECT zz_modules.id FROM zz_modules WHERE zz_modules.name = 'Documentale') AND `name`='Descrizione')),
((SELECT `id` FROM `zz_gruppi` WHERE `nome`='Clienti'), (SELECT `id` FROM `zz_viste` WHERE id_module =  (SELECT zz_modules.id FROM zz_modules WHERE zz_modules.name = 'Documentale') AND `name`='Descrizione')),
((SELECT `id` FROM `zz_gruppi` WHERE `nome`='Amministratori'), (SELECT `id` FROM `zz_viste` WHERE id_module =  (SELECT zz_modules.id FROM zz_modules WHERE zz_modules.name = 'Documentale') AND `name`='Archiviazione')),
((SELECT `id` FROM `zz_gruppi` WHERE `nome`='Tecnici'), (SELECT `id` FROM `zz_viste` WHERE id_module =  (SELECT zz_modules.id FROM zz_modules WHERE zz_modules.name = 'Documentale') AND `name`='Archiviazione')),
((SELECT `id` FROM `zz_gruppi` WHERE `nome`='Agenti'), (SELECT `id` FROM `zz_viste` WHERE id_module =  (SELECT zz_modules.id FROM zz_modules WHERE zz_modules.name = 'Documentale') AND `name`='Archiviazione')),
((SELECT `id` FROM `zz_gruppi` WHERE `nome`='Clienti'), (SELECT `id` FROM `zz_viste` WHERE id_module =  (SELECT zz_modules.id FROM zz_modules WHERE zz_modules.name = 'Documentale') AND `name`='Archiviazione')),
((SELECT `id` FROM `zz_gruppi` WHERE `nome`='Amministratori'), (SELECT `id` FROM `zz_viste` WHERE id_module =  (SELECT zz_modules.id FROM zz_modules WHERE zz_modules.name = 'Documentale') AND `name`='Scadenza')),
((SELECT `id` FROM `zz_gruppi` WHERE `nome`='Tecnici'), (SELECT `id` FROM `zz_viste` WHERE id_module =  (SELECT zz_modules.id FROM zz_modules WHERE zz_modules.name = 'Documentale') AND `name`='Scadenza')),
((SELECT `id` FROM `zz_gruppi` WHERE `nome`='Agenti'), (SELECT `id` FROM `zz_viste` WHERE id_module =  (SELECT zz_modules.id FROM zz_modules WHERE zz_modules.name = 'Documentale') AND `name`='Scadenza')),
((SELECT `id` FROM `zz_gruppi` WHERE `nome`='Clienti'), (SELECT `id` FROM `zz_viste` WHERE id_module =  (SELECT zz_modules.id FROM zz_modules WHERE zz_modules.name = 'Documentale') AND `name`='Scadenza')),
((SELECT `id` FROM `zz_gruppi` WHERE `nome`='Amministratori'), (SELECT `id` FROM `zz_viste` WHERE id_module =  (SELECT zz_modules.id FROM zz_modules WHERE zz_modules.name = 'Documentale') AND `name`='Stato')),
((SELECT `id` FROM `zz_gruppi` WHERE `nome`='Tecnici'), (SELECT `id` FROM `zz_viste` WHERE id_module =  (SELECT zz_modules.id FROM zz_modules WHERE zz_modules.name = 'Documentale') AND `name`='Stato')),
((SELECT `id` FROM `zz_gruppi` WHERE `nome`='Agenti'), (SELECT `id` FROM `zz_viste` WHERE id_module =  (SELECT zz_modules.id FROM zz_modules WHERE zz_modules.name = 'Documentale') AND `name`='Stato')),
((SELECT `id` FROM `zz_gruppi` WHERE `nome`='Clienti'), (SELECT `id` FROM `zz_viste` WHERE id_module =  (SELECT zz_modules.id FROM zz_modules WHERE zz_modules.name = 'Documentale') AND `name`='Stato')),
((SELECT `id` FROM `zz_gruppi` WHERE `nome`='Amministratori'), (SELECT `id` FROM `zz_viste` WHERE id_module =  (SELECT zz_modules.id FROM zz_modules WHERE zz_modules.name = 'Documentale Gruppi') AND `name`='id')),
((SELECT `id` FROM `zz_gruppi` WHERE `nome`='Tecnici'), (SELECT `id` FROM `zz_viste` WHERE id_module =  (SELECT zz_modules.id FROM zz_modules WHERE zz_modules.name = 'Documentale Gruppi') AND `name`='id')),
((SELECT `id` FROM `zz_gruppi` WHERE `nome`='Agenti'), (SELECT `id` FROM `zz_viste` WHERE id_module =  (SELECT zz_modules.id FROM zz_modules WHERE zz_modules.name = 'Documentale Gruppi') AND `name`='id')),
((SELECT `id` FROM `zz_gruppi` WHERE `nome`='Clienti'), (SELECT `id` FROM `zz_viste` WHERE id_module =  (SELECT zz_modules.id FROM zz_modules WHERE zz_modules.name = 'Documentale Gruppi') AND `name`='id')),
((SELECT `id` FROM `zz_gruppi` WHERE `nome`='Amministratori'), (SELECT `id` FROM `zz_viste` WHERE id_module =  (SELECT zz_modules.id FROM zz_modules WHERE zz_modules.name = 'Documentale Gruppi') AND `name`='Descrizione')),
((SELECT `id` FROM `zz_gruppi` WHERE `nome`='Tecnici'), (SELECT `id` FROM `zz_viste` WHERE id_module =  (SELECT zz_modules.id FROM zz_modules WHERE zz_modules.name = 'Documentale Gruppi') AND `name`='Descrizione')),
((SELECT `id` FROM `zz_gruppi` WHERE `nome`='Agenti'), (SELECT `id` FROM `zz_viste` WHERE id_module =  (SELECT zz_modules.id FROM zz_modules WHERE zz_modules.name = 'Documentale Gruppi') AND `name`='Descrizione')),
((SELECT `id` FROM `zz_gruppi` WHERE `nome`='Clienti'), (SELECT `id` FROM `zz_viste` WHERE id_module =  (SELECT zz_modules.id FROM zz_modules WHERE zz_modules.name = 'Documentale Gruppi') AND `name`='Descrizione'));




INSERT INTO `zz_modules` (`name`, `name2`, `module_dir`, `options`, `options2`, `icon`, `version`, `compatibility`, `order`, `parent`, `default`, `default_menu`, `enabled`, `type`, `new`) VALUES
('Movimenti', '', 'movimenti', 'SELECT |fields| FROM `mg_movimenti` HAVING 1=1 order by `id`', '', 'fa fa-external-link', '2.0.1', '2.0.1', 7, 0, 1, 0, 1, 'menu', 0);

UPDATE `zz_modules` SET `parent`=( SELECT `id` FROM (SELECT `id` FROM `zz_modules` `m2` WHERE `name`='Magazzino') AS `m2`) WHERE `name` IN('Movimenti');


INSERT INTO `zz_viste` (`id_module`, `name`, `query`, `order`, `search`, `slow`, `search_inside`, `order_by`, `enabled`, `summable`, `default`) VALUES
((SELECT id FROM zz_modules WHERE name = 'Movimenti'), 'id', 'id', 1, 0, 0, '', '', 0, 0, 0),
((SELECT id FROM zz_modules WHERE name = 'Movimenti'), 'Data', 'DATE_FORMAT(data, \'%d/%m/%Y\')', 2, 0, 0, '', '', 1, 0, 0),
((SELECT id FROM zz_modules WHERE name = 'Movimenti'), 'Articolo', 'descrizione_articolo', 3, 0, 0, '', '', 1, 0, 0),
((SELECT id FROM zz_modules WHERE name = 'Movimenti'), 'Descrizione', 'movimento', 4, 0, 0, '', '', 1, 0, 0),
((SELECT id FROM zz_modules WHERE name = 'Movimenti'), 'Qta', 'qta',5, 0, 0, '', '', 1, 0, 0);


INSERT INTO `zz_gruppi_viste` (`id_gruppo`, `id_vista`) VALUES
((SELECT `id` FROM `zz_gruppi` WHERE `nome`='Amministratori'), (SELECT `id` FROM `zz_viste` WHERE id_module =  (SELECT zz_modules.id FROM zz_modules WHERE zz_modules.name = 'Movimenti') AND `name`='id')),
((SELECT `id` FROM `zz_gruppi` WHERE `nome`='Tecnici'), (SELECT `id` FROM `zz_viste` WHERE id_module =  (SELECT zz_modules.id FROM zz_modules WHERE zz_modules.name = 'Movimenti') AND `name`='id')),
((SELECT `id` FROM `zz_gruppi` WHERE `nome`='Agenti'), (SELECT `id` FROM `zz_viste` WHERE id_module =  (SELECT zz_modules.id FROM zz_modules WHERE zz_modules.name = 'Movimenti') AND `name`='id')),
((SELECT `id` FROM `zz_gruppi` WHERE `nome`='Clienti'), (SELECT `id` FROM `zz_viste` WHERE id_module =  (SELECT zz_modules.id FROM zz_modules WHERE zz_modules.name = 'Movimenti') AND `name`='id')),
((SELECT `id` FROM `zz_gruppi` WHERE `nome`='Amministratori'), (SELECT `id` FROM `zz_viste` WHERE id_module =  (SELECT zz_modules.id FROM zz_modules WHERE zz_modules.name = 'Movimenti') AND `name`='Data')),
((SELECT `id` FROM `zz_gruppi` WHERE `nome`='Tecnici'), (SELECT `id` FROM `zz_viste` WHERE id_module =  (SELECT zz_modules.id FROM zz_modules WHERE zz_modules.name = 'Movimenti') AND `name`='Data')),
((SELECT `id` FROM `zz_gruppi` WHERE `nome`='Agenti'), (SELECT `id` FROM `zz_viste` WHERE id_module =  (SELECT zz_modules.id FROM zz_modules WHERE zz_modules.name = 'Movimenti') AND `name`='Data')),
((SELECT `id` FROM `zz_gruppi` WHERE `nome`='Clienti'), (SELECT `id` FROM `zz_viste` WHERE id_module =  (SELECT zz_modules.id FROM zz_modules WHERE zz_modules.name = 'Movimenti') AND `name`='Data')),
((SELECT `id` FROM `zz_gruppi` WHERE `nome`='Amministratori'), (SELECT `id` FROM `zz_viste` WHERE id_module =  (SELECT zz_modules.id FROM zz_modules WHERE zz_modules.name = 'Movimenti') AND `name`='Articolo')),
((SELECT `id` FROM `zz_gruppi` WHERE `nome`='Tecnici'), (SELECT `id` FROM `zz_viste` WHERE id_module =  (SELECT zz_modules.id FROM zz_modules WHERE zz_modules.name = 'Movimenti') AND `name`='Articolo')),
((SELECT `id` FROM `zz_gruppi` WHERE `nome`='Agenti'), (SELECT `id` FROM `zz_viste` WHERE id_module =  (SELECT zz_modules.id FROM zz_modules WHERE zz_modules.name = 'Movimenti') AND `name`='Articolo')),
((SELECT `id` FROM `zz_gruppi` WHERE `nome`='Clienti'), (SELECT `id` FROM `zz_viste` WHERE id_module =  (SELECT zz_modules.id FROM zz_modules WHERE zz_modules.name = 'Movimenti') AND `name`='Articolo')),
((SELECT `id` FROM `zz_gruppi` WHERE `nome`='Amministratori'), (SELECT `id` FROM `zz_viste` WHERE id_module =  (SELECT zz_modules.id FROM zz_modules WHERE zz_modules.name = 'Movimenti') AND `name`='Descrizione')),
((SELECT `id` FROM `zz_gruppi` WHERE `nome`='Tecnici'), (SELECT `id` FROM `zz_viste` WHERE id_module =  (SELECT zz_modules.id FROM zz_modules WHERE zz_modules.name = 'Movimenti') AND `name`='Descrizione')),
((SELECT `id` FROM `zz_gruppi` WHERE `nome`='Agenti'), (SELECT `id` FROM `zz_viste` WHERE id_module =  (SELECT zz_modules.id FROM zz_modules WHERE zz_modules.name = 'Movimenti') AND `name`='Descrizione')),
((SELECT `id` FROM `zz_gruppi` WHERE `nome`='Clienti'), (SELECT `id` FROM `zz_viste` WHERE id_module =  (SELECT zz_modules.id FROM zz_modules WHERE zz_modules.name = 'Movimenti') AND `name`='Descrizione')),
((SELECT `id` FROM `zz_gruppi` WHERE `nome`='Amministratori'), (SELECT `id` FROM `zz_viste` WHERE id_module =  (SELECT zz_modules.id FROM zz_modules WHERE zz_modules.name = 'Movimenti') AND `name`='Qta')),
((SELECT `id` FROM `zz_gruppi` WHERE `nome`='Tecnici'), (SELECT `id` FROM `zz_viste` WHERE id_module =  (SELECT zz_modules.id FROM zz_modules WHERE zz_modules.name = 'Movimenti') AND `name`='Qta')),
((SELECT `id` FROM `zz_gruppi` WHERE `nome`='Agenti'), (SELECT `id` FROM `zz_viste` WHERE id_module =  (SELECT zz_modules.id FROM zz_modules WHERE zz_modules.name = 'Movimenti') AND `name`='Qta')),
((SELECT `id` FROM `zz_gruppi` WHERE `nome`='Clienti'), (SELECT `id` FROM `zz_viste` WHERE id_module =  (SELECT zz_modules.id FROM zz_modules WHERE zz_modules.name = 'Movimenti') AND `name`='Qta'));