/
Supplier.php
235 lines (206 loc) · 8.38 KB
/
Supplier.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
<?php
/**
* Supplier class, Supplier.php
* Suppliers management
* @category classes
*
* @author PrestaShop <support@prestashop.com>
* @copyright PrestaShop
* @license http://www.opensource.org/licenses/osl-3.0.php Open-source licence 3.0
* @version 1.2
*
*/
class Supplier extends ObjectModel
{
public $id;
/** @var integer supplier ID */
public $id_supplier;
/** @var string Name */
public $name;
/** @var string A short description for the discount */
public $description;
/** @var string Object creation date */
public $date_add;
/** @var string Object last modification date */
public $date_upd;
/** @var string Friendly URL */
public $link_rewrite;
/** @var string Meta title */
public $meta_title;
/** @var string Meta keywords */
public $meta_keywords;
/** @var string Meta description */
public $meta_description;
protected $fieldsRequired = array('name');
protected $fieldsSize = array('name' => 64);
protected $fieldsValidate = array('name' => 'isCatalogName');
protected $fieldsSizeLang = array('description' => 128, 'meta_title' => 128, 'meta_description' => 128, 'meta_description' => 128);
protected $fieldsValidateLang = array('description' => 'isGenericName', 'meta_title' => 'isGenericName', 'meta_description' => 'isGenericName', 'meta_keywords' => 'isGenericName');
protected $table = 'supplier';
protected $identifier = 'id_supplier';
public function __construct($id = NULL, $id_lang = NULL)
{
parent::__construct($id, $id_lang);
$this->link_rewrite = $this->getLink();
}
public function getLink()
{
return Tools::link_rewrite($this->name, false);
}
public function getFields()
{
parent::validateFields();
if (isset($this->id))
$fields['id_supplier'] = intval($this->id);
$fields['name'] = pSQL($this->name);
$fields['date_add'] = pSQL($this->date_add);
$fields['date_upd'] = pSQL($this->date_upd);
return $fields;
}
public function getTranslationsFieldsChild()
{
parent::validateFieldsLang();
return parent::getTranslationsFields(array('description', 'meta_title', 'meta_keywords', 'meta_description'));
}
/**
* Return suppliers
*
* @return array Suppliers
*/
static public function getSuppliers($getNbProducts = false, $id_lang = 0, $active = false, $p = false, $n = false)
{
global $cookie;
if (!$id_lang)
$id_lang = Configuration::get('PS_LANG_DEFAULT');
$query = 'SELECT s.*, sl.`description`';
$query .= ' FROM `'._DB_PREFIX_.'supplier` as s
LEFT JOIN `'._DB_PREFIX_.'supplier_lang` sl ON (s.`id_supplier` = sl.`id_supplier` AND sl.`id_lang` = '.intval($id_lang).')';
$query .= ' ORDER BY s.`name` ASC'.($p ? ' LIMIT '.((intval($p) - 1) * intval($n)).','.intval($n) : '');
$suppliers = Db::getInstance()->ExecuteS($query);
if ($suppliers === false)
return false;
if ($getNbProducts)
foreach ($suppliers as $key => $supplier)
{
$sql = '
SELECT p.`id_product`
FROM `'._DB_PREFIX_.'product` p
LEFT JOIN `'._DB_PREFIX_.'supplier` as m ON (m.`id_supplier`= p.`id_supplier`)
WHERE m.`id_supplier` = '.intval($supplier['id_supplier']).'
AND p.`id_product` IN (
SELECT cp.`id_product`
FROM `'._DB_PREFIX_.'category_group` cg
LEFT JOIN `'._DB_PREFIX_.'category_product` cp ON (cp.`id_category` = cg.`id_category`)
WHERE cg.`id_group` '.(!$cookie->id_customer ? '= 1' : 'IN (SELECT id_group FROM '._DB_PREFIX_.'customer_group WHERE id_customer = '.intval($cookie->id_customer).')').'
)';
$result = Db::getInstance()->ExecuteS($sql);
$suppliers[$key]['nb_products'] = sizeof($result);
}
for ($i = 0; $i < sizeof($suppliers); $i++)
if (intval(Configuration::get('PS_REWRITING_SETTINGS')))
$suppliers[$i]['link_rewrite'] = Tools::link_rewrite($suppliers[$i]['name'], false);
else
$suppliers[$i]['link_rewrite'] = 0;
return $suppliers;
}
/**
* Return name from id
*
* @param integer $id_supplier Supplier ID
* @return string name
*/
static public function getNameById($id_supplier)
{
$result = Db::getInstance()->getRow('
SELECT `name`
FROM `'._DB_PREFIX_.'supplier`
WHERE `id_supplier` = '.intval($id_supplier));
if (isset($result['name']))
return $result['name'];
return false;
}
static public function getIdByName($name)
{
$result = Db::getInstance()->getRow('
SELECT `id_supplier`
FROM `'._DB_PREFIX_.'supplier`
WHERE `name` = \''.pSQL($name).'\'');
if (isset($result['id_supplier']))
return intval($result['id_supplier']);
return false;
}
static public function getProducts($id_supplier, $id_lang, $p, $n, $orderBy = NULL, $orderWay = NULL, $getTotal = false, $active = true)
{
global $cookie;
if ($p < 1) $p = 1;
if (empty($orderBy) OR $orderBy == 'position') $orderBy = 'name';
if (empty($orderWay)) $orderWay = 'ASC';
if (!Validate::isOrderBy($orderBy) OR !Validate::isOrderWay($orderWay))
die (Tools::displayError());
/* Return only the number of products */
if ($getTotal)
{
$sql = '
SELECT p.`id_product`
FROM `'._DB_PREFIX_.'product` p
WHERE p.id_supplier = '.intval($id_supplier)
.($active ? ' AND p.`active` = 1' : '').'
AND p.`id_product` IN (
SELECT cp.`id_product`
FROM `'._DB_PREFIX_.'category_group` cg
LEFT JOIN `'._DB_PREFIX_.'category_product` cp ON (cp.`id_category` = cg.`id_category`)
WHERE cg.`id_group` '.(!$cookie->id_customer ? '= 1' : 'IN (SELECT id_group FROM '._DB_PREFIX_.'customer_group WHERE id_customer = '.intval($cookie->id_customer).')').'
)';
$result = Db::getInstance()->ExecuteS($sql);
return intval(sizeof($result));
}
$sql = '
SELECT p.*, pl.`description`, pl.`description_short`, pl.`link_rewrite`, pl.`meta_description`, pl.`meta_keywords`, pl.`meta_title`, pl.`name`, i.`id_image`, il.`legend`, s.`name` AS supplier_name, tl.`name` AS tax_name, t.`rate`, DATEDIFF(p.`date_add`, DATE_SUB(NOW(), INTERVAL '.(Validate::isUnsignedInt(Configuration::get('PS_NB_DAYS_NEW_PRODUCT')) ? Configuration::get('PS_NB_DAYS_NEW_PRODUCT') : 20).' DAY)) > 0 AS new,
(p.`price` * ((100 + (t.`rate`))/100) - IF((DATEDIFF(`reduction_from`, CURDATE()) <= 0 AND DATEDIFF(`reduction_to`, CURDATE()) >=0) OR `reduction_from` = `reduction_to`, IF(`reduction_price` > 0, `reduction_price`, (p.`price` * ((100 + (t.`rate`))/100) * `reduction_percent` / 100)),0)) AS orderprice
FROM `'._DB_PREFIX_.'product` p
LEFT JOIN `'._DB_PREFIX_.'product_lang` pl ON (p.`id_product` = pl.`id_product` AND pl.`id_lang` = '.intval($id_lang).')
LEFT JOIN `'._DB_PREFIX_.'image` i ON (i.`id_product` = p.`id_product` AND i.`cover` = 1)
LEFT JOIN `'._DB_PREFIX_.'image_lang` il ON (i.`id_image` = il.`id_image` AND il.`id_lang` = '.intval($id_lang).')
LEFT JOIN `'._DB_PREFIX_.'tax` t ON t.`id_tax` = p.`id_tax`
LEFT JOIN `'._DB_PREFIX_.'tax_lang` tl ON (t.`id_tax` = tl.`id_tax` AND tl.`id_lang` = '.intval($id_lang).')
LEFT JOIN `'._DB_PREFIX_.'supplier` s ON s.`id_supplier` = p.`id_supplier`
WHERE p.`id_supplier` = '.intval($id_supplier).($active ? ' AND p.`active` = 1' : '').'
AND p.`id_product` IN (
SELECT cp.`id_product`
FROM `'._DB_PREFIX_.'category_group` cg
LEFT JOIN `'._DB_PREFIX_.'category_product` cp ON (cp.`id_category` = cg.`id_category`)
WHERE cg.`id_group` '.(!$cookie->id_customer ? '= 1' : 'IN (SELECT id_group FROM '._DB_PREFIX_.'customer_group WHERE id_customer = '.intval($cookie->id_customer).')').'
)
ORDER BY '.(($orderBy == 'id_product') ? 'p.' : '').'`'.pSQL($orderBy).'` '.pSQL($orderWay).'
LIMIT '.((intval($p) - 1) * intval($n)).','.intval($n);
$result = Db::getInstance()->ExecuteS($sql);
if (!$result)
return false;
if ($orderBy == 'price')
Tools::orderbyPrice($result, $orderWay);
return Product::getProductsProperties($id_lang, $result);
}
public function getProductsLite($id_lang)
{
return Db::getInstance()->ExecuteS('
SELECT p.`id_product`, pl.`name`
FROM `'._DB_PREFIX_.'product` p
LEFT JOIN `'._DB_PREFIX_.'product_lang` pl ON (p.`id_product` = pl.`id_product` AND pl.`id_lang` = '.intval($id_lang).')
WHERE p.`id_supplier` = '.intval($this->id));
}
/*
* Specify if a supplier already in base
*
* @param $id_supplier Supplier id
* @return boolean
*/
static public function supplierExists($id_supplier)
{
$row = Db::getInstance()->getRow('
SELECT `id_supplier`
FROM '._DB_PREFIX_.'supplier s
WHERE s.`id_supplier` = '.intval($id_supplier));
return isset($row['id_supplier']);
}
}
?>