SELECT action.actiondate, cus.firstname, cus.surname, stock.stockstno, finprop.finco, acclink.accsum, stock.registrationnumber, stock.stockmake, stock.stockmod, action.username, stock.stockchasno, cus.postcode, financeco.financeco, action.actionid, site.site, saletype.saletype, dept.depttype, dept.deptbut, event.eventid, event.deptlinkid, date(TOT.date), IF((deal.del=1), 'Yes', 'No') AS dealdel, date(TOT2.date), event.userid, eventstocklink.statusid, fitoptlink.fitoptsum, eventstocklink.selling, eventstocklink.margin, eventstocklink.newtypeid, eventstocklink.packdisc, finprop.vb, finprop.warrcomm, finprop.fincomm, finprop.gap, deal.adminfee, finprop.sup, finprop.motins, finprop.other, stockstate.statedesc, discountlink.discountsum, eventstocklink.purch, eventstocklink.fda, eventstocklink.rqm, finprop.rate, finprop.agreetype, eventstocklink.qqm, eventstocklink.rom, eventstocklink.gm, eventstocklink.gmdeldisc, eventstocklink.gmoptdisc, eventstocklink.retailer, eventstocklink.retdeldisc, eventstocklink.retoptdisc, eventstocklink.associate, eventstocklink.assocdeldisc, eventstocklink.assocoptdisc, eventstocklink.pref, eventstocklink.regallow, eventstocklink.freeinsurance, eventstocklink.cussaving, eventstocklink.loyalty, eventstocklink.0finance, eventstocklink.listprice, eventstocklink.otrprice, eventstocklink.partnersid, eventstocklink.stocktypeid, bonuslink.bonussum, ST.saletype
FROM event
LEFT JOIN action ON action.eventid = event.eventid
LEFT JOIN saletypelink AS STL ON event.saletypeid = STL.saletypelinkid
LEFT JOIN saletype AS ST ON STL.saletypeid = ST.saletypeid
LEFT JOIN response AS TOT ON TOT.actionid = action.actionid
LEFT JOIN (SELECT R.date, R.actionid, TS.statusid, TA.eventid FROM action AS TA
LEFT JOIN eventstocklink AS TS ON TS.eventstocklinkid = TA.eventstocklinkid LEFT JOIN stockstate AS TST ON TST.stateid = TS.statusid
LEFT JOIN response AS R ON R.actionid = TA.actionid
LEFT JOIN deal AS D ON D.actionid = TA.actionid
WHERE D.del = 1 AND R.reasonid = 'Handover')
AS TOT2 ON TOT2.eventid = event.eventid
LEFT JOIN actiontype ON actiontype.actionid = action.typeid LEFT JOIN saletype ON saletype.saletypeid = STL.saletypeid LEFT JOIN eventstocklink ON eventstocklink.eventstocklinkid = action.eventstocklinkid
LEFT JOIN stock ON stock.stockid = eventstocklink.stockid
LEFT JOIN deptlink ON deptlink.deptlinkid = event.deptlinkid
LEFT JOIN site ON site.siteid = deptlink.siteid
LEFT JOIN dept ON dept.deptid = deptlink.deptid
LEFT JOIN eventtype ON eventtype.eventid = event.typeid
LEFT JOIN cus ON cus.cusid = event.cusid
LEFT JOIN deal ON deal.actionid = action.actionid
LEFT JOIN finprop ON finprop.eventstocklinkid = eventstocklink.eventstocklinkid
LEFT JOIN financeco ON finprop.fincoid = financeco.finid LEFT JOIN stockstate ON stockstate.stateid = eventstocklink.statusid
LEFT JOIN (SELECT accessorieslink.acclinkid, accessorieslink.actionid, sum(accessorieslink.accprice) AS accsum FROM accessorieslink GROUP BY accessorieslink.actionid)
AS acclink ON acclink.actionid = action.actionid
LEFT JOIN (SELECT fittedoptionslink.fitoptlinkid, fittedoptionslink.actionid, sum(fittedoptionslink.fitoptprice) AS fitoptsum FROM fittedoptionslink GROUP BY fittedoptionslink.actionid)
AS fitoptlink ON fitoptlink.actionid = eventstocklink.eventstocklinkid
LEFT JOIN (SELECT discountlink.discountlinkid, discountlink.actionid, sum(discountlink.discount) AS discountsum FROM discountlink GROUP BY discountlink.actionid)
AS discountlink ON discountlink.actionid = action.actionid
LEFT JOIN (SELECT bonuslink.bonuslinkid, bonuslink.actionid, sum(bonuslink.bonus) AS bonussum FROM bonuslink GROUP BY bonuslink.actionid)
AS bonuslink ON bonuslink.actionid = eventstocklink.eventstocklinkid
WHERE (action.typeid = '2' AND deal.lost = 0 AND event.saletypeid = 5 AND (eventstocklink.statusid = 2) AND year(TOT.date) = year(current_date) and month(TOT.date) = month(current_date) ) GROUP BY action.actionid