Exemple #1
0
function paiement($type_paiement)
{
    if (!$_SESSION['navig']->client->id || $_SESSION['navig']->panier->nbart < 1) {
        redirige(urlfond());
    }
    $total = 0;
    $nbart = 0;
    $poids = 0;
    $unitetr = 0;
    ActionsModules::instance()->appel_module("avantcommande");
    $modules = new Modules();
    $modules->charger_id($type_paiement);
    if (!$modules->actif) {
        return 0;
    }
    try {
        $modpaiement = ActionsModules::instance()->instancier($modules->nom);
        $commande = new Commande();
        $commande->transport = $_SESSION['navig']->commande->transport;
        $commande->client = $_SESSION['navig']->client->id;
        $commande->remise = 0;
        $devise = ActionsDevises::instance()->get_devise_courante();
        $commande->devise = $devise->id;
        $commande->taux = $devise->taux;
        $client = new Client();
        $client->charger_id($_SESSION['navig']->client->id);
        $adr = new Venteadr();
        $adr->raison = $client->raison;
        $adr->entreprise = $client->entreprise;
        $adr->nom = $client->nom;
        $adr->prenom = $client->prenom;
        $adr->adresse1 = $client->adresse1;
        $adr->adresse2 = $client->adresse2;
        $adr->adresse3 = $client->adresse3;
        $adr->cpostal = $client->cpostal;
        $adr->ville = $client->ville;
        $adr->tel = $client->telfixe . "  " . $client->telport;
        $adr->pays = $client->pays;
        $adrcli = $adr->add();
        $commande->adrfact = $adrcli;
        $adr = new Venteadr();
        $livraison = new Adresse();
        if ($livraison->charger($_SESSION['navig']->adresse)) {
            $adr->raison = $livraison->raison;
            $adr->entreprise = $livraison->entreprise;
            $adr->nom = $livraison->nom;
            $adr->prenom = $livraison->prenom;
            $adr->adresse1 = $livraison->adresse1;
            $adr->adresse2 = $livraison->adresse2;
            $adr->adresse3 = $livraison->adresse3;
            $adr->cpostal = $livraison->cpostal;
            $adr->ville = $livraison->ville;
            $adr->tel = $livraison->tel;
            $adr->pays = $livraison->pays;
        } else {
            $adr->raison = $client->raison;
            $adr->entreprise = $client->entreprise;
            $adr->nom = $client->nom;
            $adr->prenom = $client->prenom;
            $adr->adresse1 = $client->adresse1;
            $adr->adresse2 = $client->adresse2;
            $adr->adresse3 = $client->adresse3;
            $adr->cpostal = $client->cpostal;
            $adr->ville = $client->ville;
            $adr->tel = $client->telfixe . "  " . $client->telport;
            $adr->pays = $client->pays;
        }
        $adrlivr = $adr->add();
        $commande->adrlivr = $adrlivr;
        $commande->facture = 0;
        $commande->statut = Commande::NONPAYE;
        $commande->paiement = $type_paiement;
        $commande->lang = ActionsLang::instance()->get_id_langue_courante();
        $commande->id = $commande->add();
        $pays = new Pays();
        $pays->charger($adr->pays);
        $correspondanceParent = array(null);
        foreach ($_SESSION['navig']->panier->tabarticle as $pos => &$article) {
            $venteprod = new Venteprod();
            $dectexte = "\n";
            $produit = new Produit();
            $stock = new Stock();
            foreach ($article->perso as $perso) {
                $declinaison = new Declinaison();
                $declinaisondesc = new Declinaisondesc();
                if (is_numeric($perso->valeur) && $modpaiement->defalqcmd) {
                    // diminution des stocks de déclinaison si on est sur un module de paiement qui défalque de suite
                    $stock->charger($perso->valeur, $article->produit->id);
                    $stock->valeur -= $article->quantite;
                    $stock->maj();
                }
                $declinaison->charger($perso->declinaison);
                $declinaisondesc->charger($declinaison->id);
                // recup valeur declidisp ou string
                if ($declinaison->isDeclidisp($perso->declinaison)) {
                    $declidisp = new Declidisp();
                    $declidispdesc = new Declidispdesc();
                    $declidisp->charger($perso->valeur);
                    $declidispdesc->charger_declidisp($declidisp->id);
                    $dectexte .= "- " . $declinaisondesc->titre . " : " . $declidispdesc->titre . "\n";
                } else {
                    $dectexte .= "- " . $declinaisondesc->titre . " : " . $perso->valeur . "\n";
                }
            }
            // diminution des stocks classiques si on est sur un module de paiement qui défalque de suite
            $produit = new Produit($article->produit->ref);
            if ($modpaiement->defalqcmd) {
                $produit->stock -= $article->quantite;
                $produit->maj();
            }
            /* Gestion TVA */
            $prix = $article->produit->prix;
            $prix2 = $article->produit->prix2;
            $tva = $article->produit->tva;
            if ($pays->tva != "" && (!$pays->tva || $pays->tva && $_SESSION['navig']->client->intracom != "" && !$pays->boutique)) {
                $prix = round($prix / (1 + $tva / 100), 2);
                $prix2 = round($prix2 / (1 + $tva / 100), 2);
                $tva = 0;
            }
            $venteprod->quantite = $article->quantite;
            if (!$article->produit->promo) {
                $venteprod->prixu = $prix;
            } else {
                $venteprod->prixu = $prix2;
            }
            $venteprod->ref = $article->produit->ref;
            $venteprod->titre = $article->produitdesc->titre . " " . $dectexte;
            $venteprod->chapo = $article->produitdesc->chapo;
            $venteprod->description = $article->produitdesc->description;
            $venteprod->tva = $tva;
            $venteprod->commande = $commande->id;
            $venteprod->id = $venteprod->add();
            $correspondanceParent[] = $venteprod->id;
            // ajout dans ventedeclisp des declidisp associées au venteprod
            foreach ($article->perso as $perso) {
                $declinaison = new Declinaison();
                $declinaison->charger($perso->declinaison);
                // si declidisp (pas un champs libre)
                if ($declinaison->isDeclidisp($perso->declinaison)) {
                    $vdec = new Ventedeclidisp();
                    $vdec->venteprod = $venteprod->id;
                    $vdec->declidisp = $perso->valeur;
                    $vdec->add();
                }
            }
            ActionsModules::instance()->appel_module("apresVenteprod", $venteprod, $pos);
            $total += $venteprod->prixu * $venteprod->quantite;
            $nbart++;
            $poids += $article->produit->poids;
        }
        foreach ($correspondanceParent as $id_panier => $id_venteprod) {
            if ($_SESSION['navig']->panier->tabarticle[$id_panier]->parent >= 0) {
                $venteprod->charger($id_venteprod);
                $venteprod->parent = $correspondanceParent[$_SESSION['navig']->panier->tabarticle[$id_panier]->parent];
                $venteprod->maj();
            }
        }
        $pays = new Pays($_SESSION['navig']->client->pays);
        if ($_SESSION['navig']->client->pourcentage > 0) {
            $commande->remise = $total * $_SESSION['navig']->client->pourcentage / 100;
        }
        $total -= $commande->remise;
        if ($_SESSION['navig']->promo->id != "") {
            $commande->remise += calc_remise($total);
            $_SESSION['navig']->promo->utilise = 1;
            if (!empty($commande->remise)) {
                $commande->remise = round($commande->remise, 2);
            }
            $commande->maj();
            $temppromo = new Promo();
            $temppromo->charger_id($_SESSION['navig']->promo->id);
            $temppromo->utilise++;
            $temppromo->maj();
            $promoutil = new Promoutil();
            $promoutil->commande = $commande->id;
            $promoutil->promo = $temppromo->id;
            $promoutil->code = $temppromo->code;
            $promoutil->type = $temppromo->type;
            $promoutil->valeur = $temppromo->valeur;
            $promoutil->add();
        }
        if ($commande->remise > $total) {
            $commande->remise = $total;
        }
        $commande->port = port();
        if (intval($commande->port) <= 0) {
            $commande->port = 0;
        }
        $_SESSION['navig']->promo = new Promo();
        $_SESSION['navig']->commande = $commande;
        $commande->transaction = genid($commande->id, 6);
        $commande->maj();
        $total = $_SESSION['navig']->panier->total(1, $_SESSION['navig']->commande->remise) + $_SESSION['navig']->commande->port;
        if ($total < $_SESSION['navig']->commande->port) {
            $total = $_SESSION['navig']->commande->port;
        }
        $_SESSION['navig']->commande->total = $total;
        ActionsModules::instance()->appel_module("aprescommande", $commande);
        // Appeler la méthode mail du plugin de paiement...
        $modpaiement->mail($commande);
        // ... et la méthode paiement
        $modpaiement->paiement($commande);
    } catch (Exception $e) {
        // FIXME: Echec de commande -> cas à traiter ?
    }
}
function substitpanier($texte)
{
    $total = 0;
    $totalht = 0;
    $totaleco = 0;
    $totaltaxe = 0;
    $allItemCount = $nb_article = 0;
    if ($_SESSION['navig']->adresse) {
        $adr = new Adresse();
        $adr->charger($_SESSION['navig']->adresse);
        $idpays = $adr->pays;
    } else {
        $idpays = $_SESSION['navig']->client->pays;
    }
    $pays = new Pays();
    $pays->charger($idpays);
    $total = $_SESSION['navig']->panier->total();
    $totalht = $_SESSION['navig']->panier->total(0);
    $totaleco = $_SESSION['navig']->panier->totalecotaxe();
    $tva = $total - $totalht;
    $nb_article = $_SESSION['navig']->panier->nbart;
    foreach ($_SESSION['navig']->panier->tabarticle as $anItem) {
        $allItemCount += $anItem->quantite;
    }
    unset($anItem);
    $port = port();
    if ($port < 0) {
        $port = 0;
    }
    $totcmdport = $total + $port;
    $remise = $remise_client = $remise_promo = 0;
    if ($_SESSION['navig']->client->pourcentage > 0) {
        $remise_client = $total * $_SESSION['navig']->client->pourcentage / 100;
    }
    $remise_promo += calc_remise($total);
    $remise = $remise_promo + $remise_client;
    $totcmdport -= $remise;
    $totremise = $total - $remise;
    if ($totcmdport < $port) {
        $totcmdport = $port;
    }
    $totcmdportht = $totalht + $port;
    $totalht = formatter_somme($totalht);
    $total = formatter_somme($total);
    $totaleco = formatter_somme($totaleco);
    $totaltaxe = formatter_somme($totaltaxe);
    $port = formatter_somme($port);
    $totcmdport = formatter_somme($totcmdport);
    $remise = formatter_somme($remise);
    $totremise = formatter_somme($totremise);
    $totcmdportht = formatter_somme($totcmdportht);
    $tva = formatter_somme($tva, 2, ".", "");
    $remise_client = formatter_somme($remise_client);
    $remise_promo = formatter_somme($remise_promo);
    $totpoids = $_SESSION['navig']->panier->poids();
    $texte = str_replace("#PANIER_TOTALHT", "{$totalht}", $texte);
    $texte = str_replace("#PANIER_TOTALECO", "{$totaleco}", $texte);
    $texte = str_replace("#PANIER_TOTALTVA", "{$tva}", $texte);
    // total TVA du panier = #PANIER_TVA
    $texte = str_replace("#PANIER_TOTAL", "{$total}", $texte);
    $texte = str_replace("#PANIER_PORT", "{$port}", $texte);
    $texte = str_replace("#PANIER_TOTPORTHT", "{$totcmdportht}", $texte);
    $texte = str_replace("#PANIER_TOTPORT", "{$totcmdport}", $texte);
    $texte = str_replace("#PANIER_TOTREMISE", "{$totremise}", $texte);
    $texte = str_replace("#PANIER_REMISE_CLIENT", "{$remise_client}", $texte);
    $texte = str_replace("#PANIER_REMISE_PROMO", "{$remise_promo}", $texte);
    $texte = str_replace("#PANIER_REMISE", "{$remise}", $texte);
    $texte = str_replace("#PANIER_NBART_TOTAL", $allItemCount, $texte);
    $texte = str_replace("#PANIER_NBART", "" . $nb_article . "", $texte);
    $texte = str_replace("#PANIER_POIDS", "{$totpoids}", $texte);
    $texte = str_replace("#PANIER_TVA", "{$tva}", $texte);
    // total TVA du panier
    return $texte;
}