bookshop_commands   A
last analyzed

Complexity

Total Complexity 1

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 26
rs 10
c 0
b 0
f 0
wmc 1
lcom 0
cbo 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 23 1
1
<?php
2
//  ------------------------------------------------------------------------ //
3
//                      BOOKSHOP - MODULE FOR XOOPS 2                        //
4
//                  Copyright (c) 2007, 2008 Instant Zero                    //
5
//                     <http://www.instant-zero.com/>                        //
6
// ------------------------------------------------------------------------- //
7
//  This program is free software; you can redistribute it and/or modify     //
8
//  it under the terms of the GNU General Public License as published by     //
9
//  the Free Software Foundation; either version 2 of the License, or        //
10
//  (at your option) any later version.                                      //
11
//                                                                           //
12
//  You may not change or alter any portion of this comment or credits       //
13
//  of supporting developers from this source code or any supporting         //
14
//  source code which is considered copyrighted (c) material of the          //
15
//  original comment or credit authors.                                      //
16
//                                                                           //
17
//  This program is distributed in the hope that it will be useful,          //
18
//  but WITHOUT ANY WARRANTY; without even the implied warranty of           //
19
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            //
20
//  GNU General Public License for more details.                             //
21
//                                                                           //
22
//  You should have received a copy of the GNU General Public License        //
23
//  along with this program; if not, write to the Free Software              //
24
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA //
25
//  ------------------------------------------------------------------------ //
26
27
defined('XOOPS_ROOT_PATH') || exit('XOOPS root path not defined');
28
29
define('COMMAND_STATE_NOINFORMATION', 0);    // Pas encore d'informations sur la commande
30
define("COMMAND_STATE_VALIDATED", 1);		// Commande validée par Paypal
31
define('COMMAND_STATE_PENDING', 2);            // En attente
32
define('COMMAND_STATE_FAILED', 3);            // Echec
33
define("COMMAND_STATE_CANCELED", 4);		// Annulée
34
define('COMMAND_STATE_FRAUD', 5);            // Fraude
35
36
include_once XOOPS_ROOT_PATH . '/kernel/object.php';
37
if (!class_exists('Bookshop_XoopsPersistableObjectHandler')) {
38
    include_once XOOPS_ROOT_PATH . '/modules/bookshop/class/PersistableObjectHandler.php';
39
}
40
41
/**
42
 * Class bookshop_commands
43
 */
44
class bookshop_commands extends Bookshop_Object
45
{
46
    public function __construct()
47
    {
48
        $this->initVar('cmd_id', XOBJ_DTYPE_INT, null, false);
49
        $this->initVar('cmd_uid', XOBJ_DTYPE_INT, null, false);
50
        $this->initVar('cmd_date', XOBJ_DTYPE_TXTBOX, null, false);
51
        $this->initVar('cmd_state', XOBJ_DTYPE_INT, null, false);
52
        $this->initVar('cmd_ip', XOBJ_DTYPE_TXTBOX, null, false);
53
        $this->initVar('cmd_lastname', XOBJ_DTYPE_TXTBOX, null, false);
54
        $this->initVar('cmd_firstname', XOBJ_DTYPE_TXTBOX, null, false);
55
        $this->initVar('cmd_adress', XOBJ_DTYPE_TXTAREA, null, false);
56
        $this->initVar('cmd_zip', XOBJ_DTYPE_TXTBOX, null, false);
57
        $this->initVar('cmd_town', XOBJ_DTYPE_TXTBOX, null, false);
58
        $this->initVar('cmd_country', XOBJ_DTYPE_TXTBOX, null, false);
59
        $this->initVar('cmd_telephone', XOBJ_DTYPE_TXTBOX, null, false);
60
        $this->initVar('cmd_email', XOBJ_DTYPE_TXTBOX, null, false);
61
        $this->initVar('cmd_articles_count', XOBJ_DTYPE_INT, null, false);
62
        $this->initVar('cmd_total', XOBJ_DTYPE_TXTBOX, null, false);
63
        $this->initVar('cmd_shipping', XOBJ_DTYPE_TXTBOX, null, false);
64
        $this->initVar('cmd_bill', XOBJ_DTYPE_INT, null, false);
65
        $this->initVar('cmd_password', XOBJ_DTYPE_TXTBOX, null, false);
66
        $this->initVar('cmd_text', XOBJ_DTYPE_TXTAREA, null, false);
67
        $this->initVar('cmd_cancel', XOBJ_DTYPE_TXTBOX, null, false);
68
    }
69
}
70
71
/**
72
 * Class BookshopBookshop_commandsHandler
73
 */
74
class BookshopBookshop_commandsHandler extends Bookshop_XoopsPersistableObjectHandler
75
{
76
    /**
77
     * @param $db
78
     */
79
    public function __construct($db)
80
    {    //                                             Table                   Classe           Id
81
        parent::__construct($db, 'bookshop_commands', 'bookshop_commands', 'cmd_id');
82
    }
83
84
    /**
85
     * Indique si c'est la premi�re commande d'un client
86
     *
87
     * @param  integer $uid Identifiant de l'utilisateur
88
     * @return boolean Indique si c'est le cas ou pas
89
     */
90
    public function isFirstCommand($uid)
91
    {
92
        $critere = new Criteria('cmd_uid', (int)$uid, '=');
93
        if ($this->getCount($critere) > 0) {
94
            return true;
95
        } else {
96
            return false;
97
        }
98
    }
99
100
    /**
101
	 * Indique si un livre a déajà été acheté par un utilisateur
102
     *
103
     * @param  integer $uid    Identifiant de l'utilisateur
104
     * @param  integer $bookId Identifiant du livre
105
     * @return boolean Indique si c'est le cas ou pas
0 ignored issues
show
Documentation introduced by
Should the return type not be integer|boolean?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
106
     */
107
    public function BookAlreadyBought($uid, $bookId)
108
    {
109
        $sql    = 'SELECT Count(*) as cpt FROM ' . $this->db->prefix('bookshop_caddy') . ' c, ' . $this->db->prefix('bookshop_commands') . ' f WHERE c.caddy_book_id = ' . (int)$bookId . ' AND c.caddy_cmd_id = f.cmd_id AND f.cmd_uid = ' . (int)$uid;
110
        $result = $this->db->query($sql);
111
        if (!$result) {
112
            return 0;
113
        }
114
        list($count) = $this->db->fetchRow($result);
115
        if ($count > 0) {
116
            return true;
117
        } else {
118
            return false;
119
        }
120
    }
121
122
    /**
123
	 * Mise à jour des stocks pour chaque livre composant la commande
124
     * @param  integer $cmd_id Identifiant de la commande
125
     * @return void
0 ignored issues
show
Documentation introduced by
Should the return type not be boolean?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
126
     */
127
    public function updateStocks($cmd_id)
128
    {
129
        global $h_bookshop_caddy, $h_bookshop_books;
130
        // Recherche de tous les livres du caddy
131
        $caddy  = $h_bookshop_caddy->getCaddyFromCommand($cmd_id);
132
        $tblTmp = $tblBooks = array();
0 ignored issues
show
Unused Code introduced by
$tblBooks is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
133
        foreach ($caddy as $item) {
134
            $tblTmp[] = $item->getVar('caddy_book_id');
135
        }
136
        // Chargement de tous les livres
137
        $critere  = new Criteria('book_id', '(' . implode(',', $tblTmp) . ')', 'IN');
138
        $tblBooks = $h_bookshop_books->getObjects($critere, true);
139
		// Boucle sur le caddy pour mettre à jour les quantités
140
        foreach ($caddy as $item) {
141
            if (isset($tblBooks[$item->getVar('caddy_book_id')])) {
142
                $book = $tblBooks[$item->getVar('caddy_book_id')];
143
                $h_bookshop_books->decreaseStock($book, $item->getVar('caddy_qte'));
144
				$h_bookshop_books->verifyLowStock($book);	// Vérification du stock d'alerte
145
            }
146
        }
147
148
        return true;
149
    }
150
151
    /**
152
	 * Validation d'une commande et mise à jour des stocks
153
     *
154
     * @param  integer $cmd_id Identifiant de la commande
155
     * @return boolean Indique si la validation de la commande s'est bien faite ou pas
156
     */
157
    public function validateCommand($cmd_id)
158
    {
159
        $retval   = false;
160
        $commande =& $this->get($cmd_id);
161
        if (is_object($commande)) {
162
            $commande->setVar('cmd_state', COMMAND_STATE_VALIDATED);
163
            $retval = $this->insert($commande, true);
164
            if ($retval) {
165
                $this->updateStocks($cmd_id);
166
            }
167
        }
168
169
        return $retval;
170
    }
171
}
172