Files
le-matelot/client/plugins/incitation/Incitation.class.php
2020-01-27 08:56:08 +01:00

895 lines
16 KiB
PHP

<?php
include_once(realpath(dirname(__FILE__)) . "/../../../classes/PluginsClassiques.class.php");
include_once(realpath(dirname(__FILE__)) . "/../../../classes/Promo.class.php");
include_once(realpath(dirname(__FILE__)) . "/../../../classes/Commande.class.php");
include_once(realpath(dirname(__FILE__)) . "/../../../classes/Venteprod.class.php");
include_once(realpath(dirname(__FILE__)) . "/../../../classes/Produit.class.php");
class Incitation extends PluginsClassiques {
public $id;
public $type;
public $actif;
public $valref;
public $seuiltotal;
public $qtetotal;
public $prodpanier;
public $qteprod;
public $inclurepromo;
public $inclurenouveaute;
const TABLE = "incitation";
public $table = self::TABLE;
public $bddvars = array ("id", "type", "actif", "valref", "seuiltotal", "qtetotal", "prodpanier", "qteprod", "inclurepromo", "inclurenouveaute");
public function __construct() {
parent::__construct("incitation");
}
public function init() {
$query_incitation = "CREATE TABLE `incitation` (
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`type` TEXT ,
`actif` FLOAT ,
`valref` TEXT ,
`seuiltotal` FLOAT ,
`qtetotal` FLOAT ,
`prodpanier` TEXT ,
`qteprod` FLOAT ,
`inclurepromo` INT ,
`inclurenouveaute` INT
);";
$this->query($query_incitation);
$chargeref = new Incitation();
$chargeref->inclurepromo = 1;
$chargeref->inclurenouveaute = 1;
$chargeref->add();
}
public function charger($id) {
return $this->getVars("select * from $this->table where id=$id");
}
// Calcul combien de références offertes par Incitation sont enregistrées dans le panier
protected function nbre_bonusref() {
$nbre=0;
$query = "select * from $this->table where type='bonusref' and actif=1";
$resul = $this->query($query);
while($resul && $bonusref = $this->fetch_object($resul)) {
foreach($_SESSION['navig']->panier->tabarticle as &$art) {
if($art->produit->ref==$bonusref->valref) $nbre++;
}
}
return $nbre;
}
// Calcul combien de références offertes par Incitation sont enregistrées dans une commande
protected function nbre_bonusref_commande($commande, $tab) {
$nbre=0;
$query = "select * from $this->table where type='bonusref' and actif=1";
$resul = $this->query($query);
while($resul && $bonusref = $this->fetch_object($resul)) {
foreach($tab as $tab) {
if($tab ==$bonusref->valref) $nbre++;
}
return $nbre;
}
}
// Conversion des sommes/pourcentages d'une remise pour l'entrée dans la session :
protected function convertir_valeur ($total, $valeur1, $type1, $type2) {
if ($type1 == $type2 ) {
return $valeur1;
}
if ($type1 ==2 && $type2 ==1) {
$valeur = round ($valeur1*$total/100, 2);
return $valeur;
}
if ($type1 ==1 && $type2 ==2) {
$valeur = round ($valeur1*100/$total, 2);
return $valeur;
}
}
// Calcul préalable du total panier avec ou sans les nouveauté et promos
protected function total_incitation($bonusremise) {
$total = 0;
$taxe = 0;
foreach($_SESSION['navig']->panier->tabarticle as &$art) {
$prix =0;
$quantite = $art->quantite;
if( $art->produit->promo == 0 && $art->produit->nouveaute == 0)
$prix = $art->produit->prix;
if ($art->produit->promo == 1 && $art->produit->nouveaute == 1) {
if ($bonusremise->inclurepromo ==1 && $bonusremise->inclurenouveaute == 1)
$prix = $art->produit->prix2;
}
if ($art->produit->promo == 0 && $art->produit->nouveaute == 1) {
if ($bonusremise->inclurenouveaute ==1)
$prix = $art->produit->prix;
}
if ($art->produit->promo ==1 && $art->produit->nouveaute == 0) {
if ($bonusremise->inclurepromo ==1)
$prix = $art->produit->prix2;
}
if ($prix > 0) $prodtva = $art->produit->tva;
$taxe += ($prix - ($prix/(1+$prodtva/100))) * $quantite;
$total += $prix*$quantite;
}
$pays = new Pays();
if($_SESSION['navig']->adresse != "" && $_SESSION['navig']->adresse != 0) {
$adr = new Adresse($_SESSION['navig']->adresse);
$pays->charger($adr->pays);
}
else
$pays->charger($_SESSION['navig']->client->pays);
if($tva && $pays->tva != "" && (! $pays->tva || ($pays->tva && $_SESSION['navig']->client->intracom != "")))
$total -= $taxe;
return round($total, 2);
}
// Calcul préalable de la quantité d'articles du panier avec ou sans les nouveauté et promos et sans les références offertes
protected function quantite_incitation($bonusremise) {
$quantite=0;
foreach($_SESSION['navig']->panier->tabarticle as &$art) {
if( ! $art->produit->promo && ! $art->produit->nouveaute)
$quantite += $art->quantite;
elseif ($art->produit->promo && $art->produit->nouveaute) {
if ($bonusremise->inclurepromo ==1 && $bonusremise->inclurenouveaute ==1)
$quantite += $art->quantite;
}
elseif (! $art->produit->promo && $art->produit->nouveaute) {
if ($bonusremise->inclurenouveaute ==1)
$quantite += $art->quantite;
}
elseif ($art->produit->promo && ! $art->produit->nouveaute) {
if ($bonusremise->inclurepromo ==1)
$quantite += $art->quantite;
}
}
$qte = $this->nbre_bonusref();
$quantite-=$qte;
return $quantite;
}
// Calcul la valeur d'une incitation de type remise si OK
protected function calcul_remise($bonusremise) {
$tpromo = new Promo($bonusremise->valref);
$total = $this->total_incitation($bonusremise);
//1°) Si soummission d'un code promo et code validé par Thélia : recalculer le total avant d'évaluer l'attribution de la remise.
if($_REQUEST['action']== "codepromo" && $_REQUEST['code']!=='' && $_REQUEST['code']== $_SESSION['navig']->promo->code) {
$cpromo = new Promo($_SESSION['navig']->promo->code);
if($cpromo->type ==1) {
$total-=$cpromo->valeur;
if($total>=$bonusremise->seuiltotal) $_SESSION['navig']->promo->valeur+=$this->convertir_valeur($total, $tpromo->valeur, $tpromo->type, 1);
}
if($cpromo->type ==2) {
$cremise = round($cpromo->valeur*$total/100, 2);
$total-=$cremise;
if($total>=$bonusremise->seuiltotal) $_SESSION['navig']->promo->valeur+=$this->convertir_valeur($total, $tpromo->valeur, $tpromo->type, 2);
}
}
//2° Sinon : injecter la remise dans la session
else {
$_SESSION['navig']->promo->id = $tpromo->id;
$_SESSION['navig']->promo->code = $tpromo->code;
// Les remises se cumulent + la première remise dicte le type (somme ou pourcentage) enregistré dans la session pour toutes les autres remises qui seront converties au besoin:
if ($_SESSION['navig']->promo->type=='') $_SESSION['navig']->promo->type = $tpromo->type;
$_SESSION['navig']->promo->valeur += $this->convertir_valeur($total, $tpromo->valeur, $tpromo->type, $_SESSION['navig']->promo->type);
$_SESSION['navig']->promo->mini = $tpromo->mini;
$_SESSION['navig']->promo->utilise = $tpromo->utilise;
$_SESSION['navig']->promo->illimite = $tpromo->illimite;
$_SESSION['navig']->promo->datefin = $tpromo->datefin;
}
}
// ***************************************************************************************************************************************************
// ***************************************************************************************************************************************************
public function action() {
// La fonction est exécutée sur les pages du site qui contiennent #PARAM_FOND_incitation_active=1, et sur les pages panier et commande
global $res, $fond;
if ($fond != 'panier' && $fond != 'commande' && lireVarFond('incitation_active', $res) != 1) return;
// *************************Fonction remise***********************************************************************************************************
if ($_REQUEST['action']!=='codepromo') $_SESSION['navig']->promo = new Promo;
// Lister les incitations de type remise activées
$query = "select * from $this->table where type='remise' and actif=1";
$resul = $this->query($query);
while($resul && $bonusremise = $this->fetch_object($resul)) {
$varqteprod = $this->quantite_incitation($bonusremise);
$total = $this->total_incitation($bonusremise);
$total -= $total* $_SESSION['navig']->client->pourcentage / 100;
// Conditions : remise active et code promo assigné et seuil total atteint et qantité prod atteinte :
if($bonusremise->valref !=='' && $total>=$bonusremise->seuiltotal && $varqteprod>=$bonusremise->qtetotal) {
//1° Si aucune référence spécifique n'est exigée pour cette incitation :
if(!$bonusremise->prodpanier) {
$this->calcul_remise($bonusremise);
}
//2° Si une référence spécifique est exigée pour cette incitation :
else{
// La condition supp : si la référence spécifique est présente dans le panier et en quantité suffisante :
foreach($_SESSION['navig']->panier->tabarticle as &$art) {
if($art->produit->ref==$bonusremise->prodpanier && $art->quantite>=$bonusremise->qteprod) {
$this->calcul_remise($bonusremise);
}
}
}
}
}
// ********************************fonction port offert******************************************************************************************************
$query2 = "select * from $this->table where type='portoffert' and actif=1";
$resul2 = $this->query($query2);
while($resul2 && $bonusportoffert = $this->fetch_object($resul2)) {
$varqteprod = $this->quantite_incitation($bonusportoffert);
$total = $this->total_incitation($bonusportoffert);
$total -= $total* $_SESSION['navig']->client->pourcentage / 100;
$total2 = $_SESSION['navig']->panier->total();
$total2 -= $total2* $_SESSION['navig']->client->pourcentage / 100;
//Prise en compte d'un codepromo avant calcul
$promo = &$_SESSION['navig']->promo;
if ($promo->type==1) $total2 -= $promo->valeur;
if ($promo->type==2) $total2 -= round ($total*$promo->valeur/100, 2);
// conditions :
if ($total >= $bonusportoffert->seuiltotal && $varqteprod >= $bonusportoffert->qtetotal) {
//1° Si aucune référence spécifique n'est exigée pour cette incitation :
if (!$bonusportoffert->prodpanier) {
$incitation_port= 0;
$res = str_replace("#PANIER_PORT", "$incitation_port", $res);
$res = str_replace("#PORT", "$incitation_port", $res);
$res = str_replace("#PANIER_TOTPORT", "$total2", $res);
}
// 2° sinon :
else{
foreach($_SESSION['navig']->panier->tabarticle as &$art) {
if($art->produit->ref==$bonusportoffert->prodpanier && $art->quantite>=$bonusportoffert->qteprod) {
$incitation_port= 0;
$res = str_replace("#PANIER_PORT", "$incitation_port", $res);
$res = str_replace("#PORT", "$incitation_port", $res);
$res = str_replace("#PANIER_TOTPORT", "$total2", $res);
}
}
}
}
}
//*******************************fonction référence offerte*********************************************************************************************
// 1°) rajouter le produit offert dans le panier si conditions remplies
$query3 = "select * from $this->table where type='bonusref' and actif=1";
$resul3 = $this->query($query3);
while($resul3 && $bonusref = $this->fetch_object($resul3)) {
$varqteprod = $this->quantite_incitation($bonusref);
$total = $this->total_incitation($bonusref);
$total -= $total* $_SESSION['navig']->client->pourcentage / 100;
$prodok=1;
if ($_SESSION['navig']->promo->type==1) $total -= $_SESSION['navig']->promo->valeur;
if ($_SESSION['navig']->promo->type==2) $total -= round ($total*$_SESSION['navig']->promo->valeur/100, 2);
if ($bonusref->valref !=="" && $total >= $bonusref->seuiltotal && $varqteprod >= $bonusref->qtetotal) {
if (!$bonusref->prodpanier) {
$_SESSION['navig']->panier->ajouter($bonusref->valref, 1, "", "", "");
//$_SESSION['navig']->panier->ajouter("CADEAU1",1,"","","");
$_SESSION['cadeau'] = 1;
}
else {
$prodok=0;
foreach($_SESSION['navig']->panier->tabarticle as &$art) {
if($art->produit->ref==$bonusref->prodpanier && $art->quantite>=$bonusref->qteprod) {
//$_SESSION['navig']->panier->ajouter($bonusref->valref, 1, "", "", "");
//$_SESSION['navig']->panier->ajouter("CADEAU1",1,"","","");
$_SESSION['cadeau'] = 1;
$prodok=1;
}
}
}
}
//2°) Supprimer le cadeau du panier si conditions non remplies
if ($total < $bonusref->seuiltotal || $varqteprod < $bonusref->qtetotal || $prodok==0) {
$_SESSION['cadeau'] = 0;
$idx = 0;
foreach($_SESSION['navig']->panier->tabarticle as &$art) {
if($art->produit->ref==$bonusref->valref) {
$_SESSION['navig']->panier->supprimer($idx);
}
$idx++;
}
}
}
}
// ******************* aprescommande() pour les incitation de type "port offert"*********************************************************************
public function aprescommande($commande) {
$query5 = "select * from $this->table where type='portoffert' and actif=1";
$resul5 = $this->query($query5);
while($resul5 && $bonusportoffert = $this->fetch_object($resul5)) {
$qte=0;
$total=0;
$tab_venteprod = array();
$tab_venteprod_qte = array();
$venteprod = new Venteprod();
$query4 = "select * from $venteprod->table where commande='" . $commande->id . "'";
$resul4 = $venteprod->query($query4);
while($resul4 && $venteprod = $venteprod->fetch_object($resul4)) {
$qte2=0;
if ($prodtemp->promo ==0 && $prodtemp->nouveaute ==0) {
$qte +=$venteprod->quantite;
$qte2 =$venteprod->quantite;
$total += $venteprod->prixu*$qte2;
$tab_venteprod[] = $venteprod->ref;
$tab_venteprod_qte[] = $venteprod->quantite;
}
if ($prodtemp->promo ==1 && $prodtemp->nouveaute ==1) {
if ($bonusportoffert->inclurepromo==1 && $bonusportoffert->inclurenouveaute ==1) {
$qte +=$venteprod->quantite;
$qte2 =$venteprod->quantite;
$total += $venteprod->prixu*$qte2;
$tab_venteprod[] = $venteprod->ref;
$tab_venteprod_qte[] = $venteprod->quantite;
}
}
if ($prodtemp->promo ==0 && $prodtemp->nouveaute ==1) {
if ($bonusportoffert->inclurenouveaute ==1) {
$qte +=$venteprod->quantite;
$qte2 =$venteprod->quantite;
$total += $venteprod->prixu*$qte2;
$tab_venteprod[] = $venteprod->ref;
$tab_venteprod_qte[] = $venteprod->quantite;
}
}
if ($prodtemp->promo ==1 && $prodtemp->nouveaute ==0) {
if ($bonusportoffert->inclurepromo ==1) {
$qte +=$venteprod->quantite;
$qte2 =$venteprod->quantite;
$total += $venteprod->prixu*$qte2;
$tab_venteprod[] = $venteprod->ref;
$tab_venteprod_qte[] = $venteprod->quantite;
}
}
$qte2 = $this->nbre_bonusref_commande($commande, $tab_venteprod);
$varqteprod = $qte - $qte2;
// conditions :
if ($total >= $bonusportoffert->seuiltotal && $varqteprod >= $bonusportoffert->qtetotal) {
//1° Si aucune référence spécifique n'est exigée pour cette incitation :
if (!$bonusportoffert->prodpanier) {
$_SESSION['navig']->commande->total -= $_SESSION['navig']->commande->port;
$commande->port = 0;
$commande->maj();
return;
}
// 2° sinon :
if ($bonusportoffert->prodpanier) {
$i=-1;
foreach($tab_venteprod as $v) {
$i++;
if($v == $bonusportoffert->prodpanier && $tab_venteprod_qte[$i]>=$bonusportoffert->qteprod) {
$_SESSION['navig']->commande->total -= $_SESSION['navig']->commande->port;
$commande->port = 0;
$commande->maj();
return;
}
}
}
}
}
}
}
public function destroy() {
$query_incitation = "DROP TABLE `incitation`";
$resul_incitation = $this->query($query_incitation);
}
}
?>