/* definisce il database corrente*/
use BI_089;

/* Fatture */

INSERT INTO _cdm_ VALUES (NULL, 'Elimina fatture non stampate', NOW()+0);

DELETE AFATT1_01, AVALRF_01, AFATTZ_01
FROM AFATT1_01
LEFT JOIN AFATTZ_01 ON AFATTZ_FK1_AFATT1 = AFATT1_ID
LEFT JOIN AVALRF_01 ON AVALRF_FK1_AFATTZ = AFATTZ_ID
WHERE AFATT1_AA_IVA = 0;

INSERT INTO _cdm_ VALUES (NULL, 'Elimina fatture proforma', NOW()+0);

DELETE AFATT1_01, AVALRF_01, AFATTZ_01
FROM AFATT1_01
LEFT JOIN AFATTZ_01 ON AFATTZ_FK1_AFATT1 = AFATT1_ID
LEFT JOIN AVALRF_01 ON AVALRF_FK1_AFATTZ = AFATTZ_ID
LEFT JOIN ATBACF_01 ON ATBACF_ID = AFATT1_FK1_ATBACF
WHERE ATBACF_TIPFAT = '5';

/* Elimina righe vuoti da fatture e bolle */
INSERT INTO _cdm_ VALUES (NULL, 'Elimina righe vuoti', NOW()+0);

DELETE FROM AFATTZ_01 WHERE AFATTZ_TIPRIG = 'V';
DELETE FROM ABOLLZ_01 WHERE ABOLLZ_TIPRIG = 'V';

INSERT INTO _cdm_ VALUES (NULL, 'Elaborazione terminata', NOW()+0);

