-- ============================================================================
-- cleanup_comenzi_sterse_nefacturate.sql
-- 2026-04-08
--
-- Soft-delete (sters=1) comenzile din ROA care sunt:
--   1. Active (sters=0)
--   2. Nu au factura activa in VANZARI
--   3. Mai vechi de 3 zile (DATA_COMANDA < SYSDATE - 3)
--
-- Motivatie: comenzi de test importate din GoMag care au fost facturate manual
-- (direct, nu factura din comanda). Raman pe veci ca active nefacturate.
--
-- IMPORTANT: Ruleaza intai SELECT-ul de preview inainte de UPDATE!
-- ============================================================================

SET SERVEROUTPUT ON;
SET LINESIZE 200;

-- ============================================================================
-- STEP 1: PREVIEW — vezi ce se va marca sters
-- ============================================================================

PROMPT;
PROMPT === PREVIEW: Comenzi active, nefacturate, mai vechi de 3 zile ===;
PROMPT;

SELECT c.id_comanda,
       c.nr_comanda,
       c.comanda_externa,
       c.data_comanda,
       c.id_part,
       (SELECT COUNT(*) FROM comenzi_elemente e
         WHERE e.id_comanda = c.id_comanda AND e.sters = 0) AS nr_elemente
  FROM comenzi c
 WHERE c.sters = 0
   AND c.data_comanda < SYSDATE - 3
   AND NOT EXISTS (
       SELECT 1 FROM vanzari v
        WHERE v.id_comanda = c.id_comanda
          AND v.sters = 0
   )
 ORDER BY c.data_comanda;

-- ============================================================================
-- STEP 2: SOFT-DELETE — decomentati blocul dupa verificarea preview-ului
-- ============================================================================

/*
DECLARE
    v_elemente_count  NUMBER := 0;
    v_comenzi_count   NUMBER := 0;
BEGIN
    -- Mai intai soft-delete pe detalii (COMENZI_ELEMENTE)
    UPDATE comenzi_elemente SET sters = 1
     WHERE sters = 0
       AND id_comanda IN (
         SELECT c.id_comanda
           FROM comenzi c
          WHERE c.sters = 0
            AND c.data_comanda < SYSDATE - 3
            AND NOT EXISTS (
                SELECT 1 FROM vanzari v
                 WHERE v.id_comanda = c.id_comanda
                   AND v.sters = 0
            )
     );
    v_elemente_count := SQL%ROWCOUNT;

    -- Apoi soft-delete pe header (COMENZI)
    UPDATE comenzi SET sters = 1
     WHERE sters = 0
       AND data_comanda < SYSDATE - 3
       AND NOT EXISTS (
           SELECT 1 FROM vanzari v
            WHERE v.id_comanda = comenzi.id_comanda
              AND v.sters = 0
       );
    v_comenzi_count := SQL%ROWCOUNT;

    DBMS_OUTPUT.PUT_LINE('=== REZULTAT CLEANUP ===');
    DBMS_OUTPUT.PUT_LINE('Elemente marcate sters: ' || v_elemente_count);
    DBMS_OUTPUT.PUT_LINE('Comenzi marcate sters:  ' || v_comenzi_count);

    -- COMMIT explicit — decomentati doar dupa ce sunteti siguri
    -- COMMIT;

    -- Sau ROLLBACK daca ceva nu arata bine:
    -- ROLLBACK;
END;
/
*/

PROMPT;
PROMPT === Pentru a executa, decomentati blocul PL/SQL si COMMIT ===;
PROMPT;
