Can't execute query

SELECT orp.ora ora1,
				CASE WHEN c2.stationare>0 THEN ADDTIME(ord.ora, SEC_TO_TIME(-c2.stationare*60)) ELSE CAST(ord.ora AS TIME) END ora2,, legaturi.pret,, legaturi.durata, traseu_id, trase.nume, trase.note, trase.dtModificat, trase.sursa, trase.trust_level, trase.tel_rezervari,
				trase.rezervariP, trase.tipRezervari, trase.plataRezervari,
				CASE WHEN masini.tip IS NULL THEN curse.tip_masina ELSE masini.tip END tipAuto, masini.dotari, masini.categorie, coid, co.nume conume,
				CASE WHEN brands.nume IS NULL THEN co.brand ELSE brands.nume END cobrand, co.rating,
				CASE WHEN brands.logo IS NULL THEN co.logo ELSE brands.logo END logo,,, co.emailRez, co.subdomain,
				orp.statie_id s1id, ord.statie_id s2id, co.platitor, co.appconst,
				orp.alt_statie_id alt_statie_p, ord.alt_statie_id alt_statie_d, orp.company_id alt_company, DATEDIFF(NOW(), trase.dtModificat) vechime,
				CASE WHEN co.data_expir > NOW() THEN 1 ELSE 0 END isPartner, CASE WHEN co.mobil='' THEN ELSE co.mobil END telRez, (co.rezervari & trase.rezervari) curezervari, orp_id, ord_id,, co.comision,
				curse.d1, curse.d2, curse.d3, curse.d4, curse.d5, curse.d6, curse.d7, legaturi.versiuni, curse.cursa cuno, cursa_id
				, trase.reguliFlexPrice,CASE WHEN THEN curse.ref ELSE cursa2.ref END refCursa, trase.ref refTraseu
				, idRuta, legaturi.weekdays
				,bo.dtStart boStart,
						CASE WHEN bo.dtEnd IS NULL AND legaturi.dtStart>'2020-08-12' THEN legaturi.dtStart ELSE bo.dtEnd END boEnd, bo.note boNote
				, CASE WHEN trase.tipRezervari & 4 >0 AND (tro.permisii IS NULL OR tro.permisii & 2 = 2) THEN 1 ELSE 0 END areVanzari
				, tro.permisii, trase.saptamanal
			FROM legaturi
				JOIN orar orp ON = legaturi.ora_plecare_id
				JOIN orar ord ON = legaturi.ora_sosire_id
				JOIN circuit c2 ON
				JOIN trase ON legaturi.traseu_id = AND trase.antena=0
				LEFT JOIN tronsoane tro ON tro.traseu_id=legaturi.traseu_id AND tro.st_plecare_id=legaturi.statie_plecare_id AND tro.st_sosire_id=legaturi.statie_sosire_id
				LEFT JOIN brands ON
				JOIN companii co ON trase.company_id =
				JOIN curse ON
				JOIN curse cursa2 ON
				LEFT JOIN masini ON = CASE orp.zi WHEN 0 THEN curse.d3=
															WHEN 1 THEN curse.d2
															WHEN 2 THEN curse.d1
				LEFT OUTER JOIN blackouts bo ON bo.dtStart<='2020-08-12' AND bo.dtEnd>'2020-08-12' AND
											 (bo.traseu_id IS NULL AND bo.operator_id=trase.company_id
												OR bo.cursa_id IS NULL AND
			WHERE legaturi.loc_plecare_id =18992
				AND legaturi.loc_sosire_id =188
				 AND (legaturi.dtEnd IS NULL OR legaturi.dtEnd > '2020-08-12') AND (legaturi.dtStart IS NULL OR legaturi.dtStart <= '2020-08-12') 
				AND trase.d2=1
			ORDER BY areVanzari desc, orp.ora24

MySQL Error: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'WHEN 1 THEN curse.d2 WHEN 2 THEN curse.d1 END ' at line 32

dblib.php   338   db_query ()
Curse.class.php   485   getLFQ ()
curse.php   59   getCurseDirecte ()
index.php   761   include ()

This script cannot continue, terminating.