viene fatto un arrotondamento per eccesso.
A runtime cioè faccio il calcolo con uno script php però non mi coincide con il totale calcolato tramite il trigger.
Questa sono le tabelle coinvolte:
codice:
CREATE TABLE IF NOT EXISTS `documento_vendita`(
`tipo_tupla` enum('0','1','2','3') NOT NULL,
`data_creazione` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
`id` int(11) NOT NULL AUTO_INCREMENT,
`n` int(11) NOT NULL,
`anno` int(4) NOT NULL,
`data` date NOT NULL,
`totale` decimal(10,2) NOT NULL,
`cliente` int(11) NOT NULL,
`differita` tinyint(1) NOT NULL DEFAULT '0',
`nota` tinyint(1) NOT NULL DEFAULT '0',
`rif_fattura` INT(11),
`dipendente` varchar(16) NOT NULL,
FOREIGN KEY ( `rif_fattura`) REFERENCES documento_vendita(`id`) ,
PRIMARY KEY (`id`),
KEY `documento_vendita_ibfk_1` (`cliente`),
KEY `documento_vendita_ibfk_2` (`dipendente`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS `articolo_appartiene_a_documento_vendita` (
`codice` int(11) NOT NULL,
`id` int(11) NOT NULL,
`sconto` int(11) DEFAULT '0',
`prezzo_vendita` decimal(10,2) NOT NULL,
`quantita` decimal(10,2) NOT NULL DEFAULT '1',
`iva` decimal(10,2) NOT NULL,
`in_ddt` tinyint(1) NOT NULL DEFAULT '0',
FOREIGN KEY (codice) references articolo(codice),
FOREIGN KEY (id) references documento_vendita(id),
PRIMARY KEY (`codice`,`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
e questo è il trigger
codice:
CREATE TRIGGER`totale_documento_vendita_insert` BEFORE INSERT ON `articolo_appartiene_a_documento_vendita`
FOR EACH ROW BEGIN
DECLARE tot_imp_articolo decimal;
DECLARE iva_articolo int;
DECLARE tot_iva_articolo decimal;
DECLARE sconto decimal;
DECLARE tipo_doc char;
DECLARE giacenza_attuale decimal;
SELECT IVA from articolo where codice = new.codice INTO iva_articolo;
SET sconto = new.prezzo_vendita*new.sconto/100;
SET tot_imp_articolo = new.prezzo_vendita - sconto;
SET tot_iva_articolo = tot_imp_articolo * iva_articolo / 100;
update documento_vendita
SET totale = totale + ((tot_imp_articolo + tot_iva_articolo))*new.quantita
where id = new.id;
SELECT tipo_tupla FROM documento_vendita WHERE id = new.id INTO tipo_doc;
SELECT giacenza FROM articolo WHERE codice = new.codice INTO giacenza_attuale;
IF tipo_doc = '2' THEN
UPDATE articolo SET giacenza = giacenza_attuale - new.quantita WHERE codice = new.codice;
ELSE
UPDATE articolo SET giacenza = 0 WHERE codice = new.codice;
END IF;
END;
mi sono accorta dell'errore perchè ad un certo punto avevo: 99 € di articoli (3 articoli con prezzo unitario 33) quindi 19,80 € di iva ma il totale nel database era 120 anzichè 118,80