select m.date ,`m`.`commercial_code`,
    g.code as gamme_code, g.libelle as gamme,
    f.code as famille_code, f.libelle as famille,
    sf.code as sousfamille_code, sf.libelle as sousfamille,
    mq.code as marque_code, mq.libelle as marque,
    p.code as produit_code, p.libelle as produit,
    `p`.`ordre`, `p`.`colisage`, `p`.`marge`, 

        MAX(DATE_FORMAT(m.date, "%Y-%m-%d")) as date_chargement, 
        -- (SELECT mpr.prix_ttc 
        --  FROM mouvements as mpr 
        --  WHERE (DATE(mpr.date) = CAST(NOW() AS DATE)) AND mpr.commercial_code = 'v10' and mpr.produit_code = p.code limit 1) as prix_ttc, 
         m.prix_ttc,
         `p`.`prix_ttc` as `prix_prod`, 
         SUM(IF(p.hors_vente = "0" AND m.type='chargement', m.quantite,'0')) as chargement, 
         SUM(IF(p.hors_vente = "0" AND m.type='retour', m.quantite,'0')) as retour, 
         SUM(IF(p.hors_vente = "0" AND m.type='casse', m.quantite,'0')) as casse, 
         SUM(IF(p.hors_vente = "0" AND m.type='dlc', m.quantite,'0')) as dlc, 
         SUM(IF(p.hors_vente = "0" AND m.type='gratuiteC', m.quantite,'0')) as gratuite, 
         SUM(IF(p.hors_vente = "0" AND m.type='chargement', m.quantite,'0')) as chargement_uniq, 
         SUM(IF(p.hors_vente = "0" AND m.type='retour', m.quantite,'0')) as retour_uniq, 
         SUM(IF(p.hors_vente = "0" AND m.type='casse', m.quantite,'0')) as casse_uniq, 
         SUM(IF(p.hors_vente = "0" AND m.type='dlc', m.quantite,'0')) as dlc_uniq, 
         SUM(IF(p.hors_vente = "0" AND m.type='gratuiteC', m.quantite,'0')) as gratuite_uniq, 
         SUM(IF(p.hors_vente = "0" AND m.type='chargement', m.quantite * m.prix_ttc,'0')) as ca_chargement_uniq, 
         SUM(IF(p.hors_vente = "0" AND m.type='retour', m.quantite * m.prix_ttc,'0')) as ca_retour_uniq, 
         SUM(IF(p.hors_vente = "0" AND m.type='casse', m.quantite * m.prix_ttc,'0')) as ca_casse_uniq, 
         SUM(IF(p.hors_vente = "0" AND m.type='dlc', m.quantite * m.prix_ttc,'0')) as ca_dlc_uniq, 
         SUM(IF(p.hors_vente = "0" AND m.type='gratuiteC', m.quantite * m.prix_ttc,'0')) as ca_gratuite_uniq 
from `produits` as `p` 
left join `mouvements` as `m` on `m`.`produit_code` = `p`.`code` 
left join `gammes` as `g` on `p`.`gamme_code` = `g`.`code` 
left join `familles` as `f` on `p`.`famille_code` = `f`.`code` 
left join `sousfamilles` as `sf` on `p`.`sousfamille_code` = `sf`.`code` 
left join `marques` as `mq` on `p`.`marque_code` = `mq`.`code` 
where `m`.`etat` <> 'refused' and m.date is not null
group by `p`.`code`, DATE(m.date) , `m`.`commercial_code`
order by m.date desc, `p`.`ordre` asc