PageRenderTime 52ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 1ms

/classes/Customer.php

https://bitbucket.org/yhjohn/ayanapure.com
PHP | 838 lines | 558 code | 108 blank | 172 comment | 59 complexity | 11e362d7b564ae504601a707551eaa33 MD5 | raw file
Possible License(s): LGPL-2.1, LGPL-3.0
  1. <?php
  2. /*
  3. * 2007-2012 PrestaShop
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@prestashop.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade PrestaShop to newer
  18. * versions in the future. If you wish to customize PrestaShop for your
  19. * needs please refer to http://www.prestashop.com for more information.
  20. *
  21. * @author PrestaShop SA <contact@prestashop.com>
  22. * @copyright 2007-2012 PrestaShop SA
  23. * @version Release: $Revision: 7499 $
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. * International Registered Trademark & Property of PrestaShop SA
  26. */
  27. class CustomerCore extends ObjectModel
  28. {
  29. public $id;
  30. public $id_shop;
  31. public $id_shop_group;
  32. /** @var string Secure key */
  33. public $secure_key;
  34. /** @var string protected note */
  35. public $note;
  36. /** @var integer Gender ID */
  37. public $id_gender = 0;
  38. /** @var integer Default group ID */
  39. public $id_default_group = _PS_DEFAULT_CUSTOMER_GROUP_;
  40. /** @var string Lastname */
  41. public $lastname;
  42. /** @var string Firstname */
  43. public $firstname;
  44. /** @var string Birthday (yyyy-mm-dd) */
  45. public $birthday = null;
  46. /** @var string e-mail */
  47. public $email;
  48. /** @var boolean Newsletter subscription */
  49. public $newsletter;
  50. /** @var string Newsletter ip registration */
  51. public $ip_registration_newsletter;
  52. /** @var string Newsletter ip registration */
  53. public $newsletter_date_add;
  54. /** @var boolean Opt-in subscription */
  55. public $optin;
  56. /** @var string WebSite **/
  57. public $website;
  58. /** @var string Company */
  59. public $company;
  60. /** @var string SIRET */
  61. public $siret;
  62. /** @var string APE */
  63. public $ape;
  64. /** @var float Outstanding allow amount (B2B opt) */
  65. public $outstanding_allow_amount = 0;
  66. /** @var integer Show public prices (B2B opt) */
  67. public $show_public_prices = 0;
  68. /** @var int Risk ID (B2B opt) */
  69. public $id_risk;
  70. /** @var integer Max payment day */
  71. public $max_payment_days = 0;
  72. /** @var integer Password */
  73. public $passwd;
  74. /** @var datetime Password */
  75. public $last_passwd_gen;
  76. /** @var boolean Status */
  77. public $active = true;
  78. /** @var boolean Status */
  79. public $is_guest = 0;
  80. /** @var boolean True if carrier has been deleted (staying in database as deleted) */
  81. public $deleted = 0;
  82. /** @var string Object creation date */
  83. public $date_add;
  84. /** @var string Object last modification date */
  85. public $date_upd;
  86. public $years;
  87. public $days;
  88. public $months;
  89. /** @var int customer id_country as determined by geolocation */
  90. public $geoloc_id_country;
  91. /** @var int customer id_state as determined by geolocation */
  92. public $geoloc_id_state;
  93. /** @var string customer postcode as determined by geolocation */
  94. public $geoloc_postcode;
  95. /** @var boolean is the customer logged in */
  96. public $logged = 0;
  97. /** @var int id_guest meaning the guest table, not the guest customer */
  98. public $id_guest;
  99. public $groupBox;
  100. protected $webserviceParameters = array(
  101. 'fields' => array(
  102. 'id_default_group' => array('xlink_resource' => 'groups'),
  103. 'newsletter_date_add' => array(),
  104. 'ip_registration_newsletter' => array(),
  105. 'last_passwd_gen' => array('setter' => null),
  106. 'secure_key' => array('setter' => null),
  107. 'deleted' => array(),
  108. 'passwd' => array('setter' => 'setWsPasswd'),
  109. ),
  110. 'associations' => array(
  111. 'groups' => array('resource' => 'group'),
  112. )
  113. );
  114. /**
  115. * @see ObjectModel::$definition
  116. */
  117. public static $definition = array(
  118. 'table' => 'customer',
  119. 'primary' => 'id_customer',
  120. 'fields' => array(
  121. 'secure_key' => array('type' => self::TYPE_STRING, 'validate' => 'isMd5', 'copy_post' => false),
  122. 'lastname' => array('type' => self::TYPE_STRING, 'validate' => 'isName', 'required' => true, 'size' => 32),
  123. 'firstname' => array('type' => self::TYPE_STRING, 'validate' => 'isName', 'required' => true, 'size' => 32),
  124. 'email' => array('type' => self::TYPE_STRING, 'validate' => 'isEmail', 'required' => true, 'size' => 128),
  125. 'passwd' => array('type' => self::TYPE_STRING, 'validate' => 'isPasswd', 'required' => true, 'size' => 32),
  126. 'last_passwd_gen' => array('type' => self::TYPE_STRING, 'copy_post' => false),
  127. 'id_gender' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId'),
  128. 'birthday' => array('type' => self::TYPE_DATE, 'validate' => 'isBirthDate'),
  129. 'newsletter' => array('type' => self::TYPE_BOOL, 'validate' => 'isBool'),
  130. 'newsletter_date_add' => array('type' => self::TYPE_DATE,'copy_post' => false),
  131. 'ip_registration_newsletter' => array('type' => self::TYPE_STRING, 'copy_post' => false),
  132. 'optin' => array('type' => self::TYPE_BOOL, 'validate' => 'isBool'),
  133. 'website' => array('type' => self::TYPE_STRING, 'validate' => 'isUrl'),
  134. 'company' => array('type' => self::TYPE_STRING, 'validate' => 'isGenericName'),
  135. 'siret' => array('type' => self::TYPE_STRING, 'validate' => 'isSiret'),
  136. 'ape' => array('type' => self::TYPE_STRING, 'validate' => 'isApe'),
  137. 'outstanding_allow_amount' => array('type' => self::TYPE_INT, 'validate' => 'isFloat', 'copy_post' => false),
  138. 'show_public_prices' => array('type' => self::TYPE_BOOL, 'validate' => 'isBool', 'copy_post' => false),
  139. 'id_risk' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedInt', 'copy_post' => false),
  140. 'max_payment_days' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedInt', 'copy_post' => false),
  141. 'active' => array('type' => self::TYPE_BOOL, 'validate' => 'isBool', 'copy_post' => false),
  142. 'deleted' => array('type' => self::TYPE_BOOL, 'validate' => 'isBool', 'copy_post' => false),
  143. 'note' => array('type' => self::TYPE_HTML, 'validate' => 'isCleanHtml', 'size' => 65000, 'copy_post' => false),
  144. 'is_guest' => array('type' => self::TYPE_BOOL, 'validate' => 'isBool', 'copy_post' => false),
  145. 'id_shop' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId', 'copy_post' => false),
  146. 'id_shop_group' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId', 'copy_post' => false),
  147. 'id_default_group' => array('type' => self::TYPE_INT, 'copy_post' => false),
  148. 'date_add' => array('type' => self::TYPE_DATE, 'validate' => 'isDate', 'copy_post' => false),
  149. 'date_upd' => array('type' => self::TYPE_DATE, 'validate' => 'isDate', 'copy_post' => false),
  150. ),
  151. );
  152. protected static $_defaultGroupId = array();
  153. protected static $_customerHasAddress = array();
  154. protected static $_customer_groups = array();
  155. public function add($autodate = true, $null_values = true)
  156. {
  157. $this->id_shop = ($this->id_shop) ? $this->id_shop : Context::getContext()->shop->id;
  158. $this->id_shop_group = ($this->id_shop_group) ? $this->id_shop_group : Context::getContext()->shop->id_shop_group;
  159. $this->birthday = (empty($this->years) ? $this->birthday : (int)$this->years.'-'.(int)$this->months.'-'.(int)$this->days);
  160. $this->secure_key = md5(uniqid(rand(), true));
  161. $this->last_passwd_gen = date('Y-m-d H:i:s', strtotime('-'.Configuration::get('PS_PASSWD_TIME_FRONT').'minutes'));
  162. if ($this->newsletter && !Validate::isDate($this->newsletter_date_add))
  163. $this->newsletter_date_add = date('Y-m-d H:i:s');
  164. if ($this->id_default_group == _PS_DEFAULT_CUSTOMER_GROUP_)
  165. if ($this->is_guest)
  166. $this->id_default_group = Configuration::get('PS_GUEST_GROUP');
  167. else
  168. $this->id_default_group = Configuration::get('PS_CUSTOMER_GROUP');
  169. /* Can't create a guest customer, if this feature is disabled */
  170. if ($this->is_guest && !Configuration::get('PS_GUEST_CHECKOUT_ENABLED'))
  171. return false;
  172. $success = parent::add($autodate, $null_values);
  173. $this->updateGroup($this->groupBox);
  174. return $success;
  175. }
  176. public function update($nullValues = false)
  177. {
  178. $this->birthday = (empty($this->years) ? $this->birthday : (int)$this->years.'-'.(int)$this->months.'-'.(int)$this->days);
  179. if ($this->newsletter && !Validate::isDate($this->newsletter_date_add))
  180. $this->newsletter_date_add = date('Y-m-d H:i:s');
  181. if (Context::getContext()->controller->controller_type == 'admin')
  182. $this->updateGroup($this->groupBox);
  183. if ($this->deleted)
  184. {
  185. $addresses = $this->getAddresses((int)Configuration::get('PS_LANG_DEFAULT'));
  186. foreach ($addresses as $address)
  187. {
  188. $obj = new Address((int)$address['id_address']);
  189. $obj->delete();
  190. }
  191. }
  192. return parent::update(true);
  193. }
  194. public function delete()
  195. {
  196. if (!count(Order::getCustomerOrders((int)$this->id)))
  197. {
  198. $addresses = $this->getAddresses((int)Configuration::get('PS_LANG_DEFAULT'));
  199. foreach ($addresses as $address)
  200. {
  201. $obj = new Address((int)$address['id_address']);
  202. $obj->delete();
  203. }
  204. }
  205. Db::getInstance()->execute('DELETE FROM `'._DB_PREFIX_.'customer_group` WHERE `id_customer` = '.(int)$this->id);
  206. Db::getInstance()->execute('DELETE FROM '._DB_PREFIX_.'message WHERE id_customer='.(int)$this->id);
  207. Db::getInstance()->execute('DELETE FROM '._DB_PREFIX_.'specific_price WHERE id_customer='.(int)$this->id);
  208. Db::getInstance()->execute('DELETE FROM '._DB_PREFIX_.'compare WHERE id_customer='.(int)$this->id);
  209. $carts = Db::getInstance()->executes('SELECT id_cart
  210. FROM '._DB_PREFIX_.'cart
  211. WHERE id_customer='.(int)$this->id);
  212. if ($carts)
  213. foreach ($carts as $cart)
  214. {
  215. Db::getInstance()->execute('DELETE FROM '._DB_PREFIX_.'cart WHERE id_cart='.(int)$cart['id_cart']);
  216. Db::getInstance()->execute('DELETE FROM '._DB_PREFIX_.'cart_product WHERE id_cart='.(int)$cart['id_cart']);
  217. }
  218. $cts = Db::getInstance()->executes('SELECT id_customer_thread
  219. FROM '._DB_PREFIX_.'customer_thread
  220. WHERE id_customer='.(int)$this->id);
  221. if ($cts)
  222. foreach ($cts as $ct)
  223. {
  224. Db::getInstance()->execute('DELETE FROM '._DB_PREFIX_.'customer_thread WHERE id_customer_thread='.(int)$ct['id_customer_thread']);
  225. Db::getInstance()->execute('DELETE FROM '._DB_PREFIX_.'customer_message WHERE id_customer_thread='.(int)$ct['id_customer_thread']);
  226. }
  227. CartRule::deleteByIdCustomer((int)$this->id);
  228. return parent::delete();
  229. }
  230. /**
  231. * Return customers list
  232. *
  233. * @return array Customers
  234. */
  235. public static function getCustomers()
  236. {
  237. $sql = 'SELECT `id_customer`, `email`, `firstname`, `lastname`
  238. FROM `'._DB_PREFIX_.'customer`
  239. WHERE 1 '.Shop::addSqlRestriction(Shop::SHARE_CUSTOMER).'
  240. ORDER BY `id_customer` ASC';
  241. return Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($sql);
  242. }
  243. /**
  244. * Return customer instance from its e-mail (optionnaly check password)
  245. *
  246. * @param string $email e-mail
  247. * @param string $passwd Password is also checked if specified
  248. * @return Customer instance
  249. */
  250. public function getByEmail($email, $passwd = null)
  251. {
  252. if (!Validate::isEmail($email) || ($passwd && !Validate::isPasswd($passwd)))
  253. die (Tools::displayError());
  254. $sql = 'SELECT *
  255. FROM `'._DB_PREFIX_.'customer`
  256. WHERE `email` = \''.pSQL($email).'\'
  257. '.Shop::addSqlRestriction(Shop::SHARE_CUSTOMER).'
  258. '.(isset($passwd) ? 'AND `passwd` = \''.Tools::encrypt($passwd).'\'' : '').'
  259. AND `deleted` = 0
  260. AND `is_guest` = 0';
  261. $result = Db::getInstance()->getRow($sql);
  262. if (!$result)
  263. return false;
  264. $this->id = $result['id_customer'];
  265. foreach ($result as $key => $value)
  266. if (key_exists($key, $this))
  267. $this->{$key} = $value;
  268. return $this;
  269. }
  270. /**
  271. * Retrieve customers by email address
  272. *
  273. * @static
  274. * @param $email
  275. * @return array
  276. */
  277. public static function getCustomersByEmail($email)
  278. {
  279. $sql = 'SELECT *
  280. FROM `'._DB_PREFIX_.'customer`
  281. WHERE `email` = \''.pSQL($email).'\'
  282. '.Shop::addSqlRestriction(Shop::SHARE_CUSTOMER);
  283. return Db::getInstance()->ExecuteS($sql);
  284. }
  285. /**
  286. * Check id the customer is active or not
  287. *
  288. * @return boolean customer validity
  289. */
  290. public static function isBanned($id_customer)
  291. {
  292. if (!Validate::isUnsignedId($id_customer))
  293. return true;
  294. $result = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow('
  295. SELECT `id_customer`
  296. FROM `'._DB_PREFIX_.'customer`
  297. WHERE `id_customer` = \''.(int)$id_customer.'\'
  298. AND active = 1
  299. AND `deleted` = 0');
  300. if (isset($result['id_customer']))
  301. return false;
  302. return true;
  303. }
  304. /**
  305. * Check if e-mail is already registered in database
  306. *
  307. * @param string $email e-mail
  308. * @param $return_id boolean
  309. * @param $ignore_guest boolean, to exclude guest customer
  310. * @return Customer ID if found, false otherwise
  311. */
  312. public static function customerExists($email, $return_id = false, $ignore_guest = true)
  313. {
  314. if (!Validate::isEmail($email))
  315. die (Tools::displayError());
  316. $sql = 'SELECT `id_customer`
  317. FROM `'._DB_PREFIX_.'customer`
  318. WHERE `email` = \''.pSQL($email).'\'
  319. '.Shop::addSqlRestriction(Shop::SHARE_CUSTOMER).
  320. ($ignore_guest ? ' AND `is_guest` = 0' : '');
  321. $result = Db::getInstance()->getRow($sql);
  322. if ($return_id)
  323. return $result['id_customer'];
  324. return isset($result['id_customer']);
  325. }
  326. /**
  327. * Check if an address is owned by a customer
  328. *
  329. * @param integer $id_customer Customer ID
  330. * @param integer $id_address Address ID
  331. * @return boolean result
  332. */
  333. public static function customerHasAddress($id_customer, $id_address)
  334. {
  335. if (!array_key_exists($id_customer, self::$_customerHasAddress))
  336. {
  337. self::$_customerHasAddress[$id_customer] = (bool)Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue('
  338. SELECT `id_address`
  339. FROM `'._DB_PREFIX_.'address`
  340. WHERE `id_customer` = '.(int)$id_customer.'
  341. AND `id_address` = '.(int)$id_address.'
  342. AND `deleted` = 0');
  343. }
  344. return self::$_customerHasAddress[$id_customer];
  345. }
  346. public static function resetAddressCache($id_customer)
  347. {
  348. if (array_key_exists($id_customer, self::$_customerHasAddress))
  349. unset(self::$_customerHasAddress[$id_customer]);
  350. }
  351. /**
  352. * Return customer addresses
  353. *
  354. * @param integer $id_lang Language ID
  355. * @return array Addresses
  356. */
  357. public function getAddresses($id_lang)
  358. {
  359. $sql = 'SELECT a.*, cl.`name` AS country, s.name AS state, s.iso_code AS state_iso
  360. FROM `'._DB_PREFIX_.'address` a
  361. LEFT JOIN `'._DB_PREFIX_.'country` c ON (a.`id_country` = c.`id_country`)
  362. LEFT JOIN `'._DB_PREFIX_.'country_lang` cl ON (c.`id_country` = cl.`id_country`)
  363. LEFT JOIN `'._DB_PREFIX_.'state` s ON (s.`id_state` = a.`id_state`)
  364. WHERE `id_lang` = '.(int)$id_lang.' AND `id_customer` = '.(int)$this->id.' AND a.`deleted` = 0';
  365. return Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($sql);
  366. }
  367. /**
  368. * Count the number of addresses for a customer
  369. *
  370. * @param integer $id_customer Customer ID
  371. * @return integer Number of addresses
  372. */
  373. public static function getAddressesTotalById($id_customer)
  374. {
  375. return Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue('
  376. SELECT COUNT(`id_address`)
  377. FROM `'._DB_PREFIX_.'address`
  378. WHERE `id_customer` = '.(int)$id_customer.'
  379. AND `deleted` = 0'
  380. );
  381. }
  382. /**
  383. * Check if customer password is the right one
  384. *
  385. * @param string $passwd Password
  386. * @return boolean result
  387. */
  388. public static function checkPassword($id_customer, $passwd)
  389. {
  390. if (!Validate::isUnsignedId($id_customer) || !Validate::isMd5($passwd))
  391. die (Tools::displayError());
  392. $sql = 'SELECT `id_customer`
  393. FROM `'._DB_PREFIX_.'customer`
  394. WHERE `id_customer` = '.$id_customer.'
  395. AND `passwd` = \''.$passwd.'\'';
  396. return (bool)Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue($sql);
  397. }
  398. /**
  399. * Light back office search for customers
  400. *
  401. * @param string $query Searched string
  402. * @return array Corresponding customers
  403. */
  404. public static function searchByName($query)
  405. {
  406. $sql = 'SELECT *
  407. FROM `'._DB_PREFIX_.'customer`
  408. WHERE (
  409. `email` LIKE \'%'.pSQL($query).'%\'
  410. OR `id_customer` LIKE \'%'.pSQL($query).'%\'
  411. OR `lastname` LIKE \'%'.pSQL($query).'%\'
  412. OR `firstname` LIKE \'%'.pSQL($query).'%\'
  413. )'.Shop::addSqlRestriction(Shop::SHARE_CUSTOMER);
  414. return Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($sql);
  415. }
  416. /**
  417. * Search for customers by ip address
  418. *
  419. * @param string $ip Searched string
  420. */
  421. public static function searchByIp($ip)
  422. {
  423. return Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('
  424. SELECT DISTINCT c.*
  425. FROM `'._DB_PREFIX_.'customer` c
  426. LEFT JOIN `'._DB_PREFIX_.'guest` g ON g.id_customer = c.id_customer
  427. LEFT JOIN `'._DB_PREFIX_.'connections` co ON g.id_guest = co.id_guest
  428. WHERE co.`ip_address` = \''.ip2long(trim($ip)).'\'');
  429. }
  430. /**
  431. * Return several useful statistics about customer
  432. *
  433. * @return array Stats
  434. */
  435. public function getStats()
  436. {
  437. $result = Db::getInstance()->getRow('
  438. SELECT COUNT(`id_order`) AS nb_orders, SUM(`total_paid` / o.`conversion_rate`) AS total_orders
  439. FROM `'._DB_PREFIX_.'orders` o
  440. WHERE o.`id_customer` = '.(int)$this->id.'
  441. AND o.valid = 1');
  442. $result2 = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow('
  443. SELECT MAX(c.`date_add`) AS last_visit
  444. FROM `'._DB_PREFIX_.'guest` g
  445. LEFT JOIN `'._DB_PREFIX_.'connections` c ON c.id_guest = g.id_guest
  446. WHERE g.`id_customer` = '.(int)$this->id);
  447. $result3 = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow('
  448. SELECT (YEAR(CURRENT_DATE)-YEAR(c.`birthday`)) - (RIGHT(CURRENT_DATE, 5)<RIGHT(c.`birthday`, 5)) AS age
  449. FROM `'._DB_PREFIX_.'customer` c
  450. WHERE c.`id_customer` = '.(int)$this->id);
  451. $result['last_visit'] = $result2['last_visit'];
  452. $result['age'] = ($result3['age'] != date('Y') ? $result3['age'] : '--');
  453. return $result;
  454. }
  455. public function getLastConnections()
  456. {
  457. return Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('
  458. SELECT c.date_add, COUNT(cp.id_page) AS pages, TIMEDIFF(MAX(cp.time_end), c.date_add) as time, http_referer,INET_NTOA(ip_address) as ipaddress
  459. FROM `'._DB_PREFIX_.'guest` g
  460. LEFT JOIN `'._DB_PREFIX_.'connections` c ON c.id_guest = g.id_guest
  461. LEFT JOIN `'._DB_PREFIX_.'connections_page` cp ON c.id_connections = cp.id_connections
  462. WHERE g.`id_customer` = '.(int)$this->id.'
  463. GROUP BY c.`id_connections`
  464. ORDER BY c.date_add DESC
  465. LIMIT 10');
  466. }
  467. /*
  468. * Specify if a customer already in base
  469. *
  470. * @param $id_customer Customer id
  471. * @return boolean
  472. */
  473. // DEPRECATED
  474. public function customerIdExists($id_customer)
  475. {
  476. return Customer::customerIdExistsStatic((int)$id_customer);
  477. }
  478. public static function customerIdExistsStatic($id_customer)
  479. {
  480. $row = Db::getInstance()->getRow('
  481. SELECT `id_customer`
  482. FROM '._DB_PREFIX_.'customer c
  483. WHERE c.`id_customer` = '.(int)$id_customer);
  484. return isset($row['id_customer']);
  485. }
  486. /**
  487. * Update customer groups associated to the object
  488. *
  489. * @param array $list groups
  490. */
  491. public function updateGroup($list)
  492. {
  493. $this->cleanGroups();
  494. if ($list && !empty($list))
  495. $this->addGroups($list);
  496. else
  497. $this->addGroups(array($this->id_default_group));
  498. }
  499. public function cleanGroups()
  500. {
  501. Db::getInstance()->execute('DELETE FROM `'._DB_PREFIX_.'customer_group` WHERE `id_customer` = '.(int)$this->id);
  502. }
  503. public function addGroups($groups)
  504. {
  505. foreach ($groups as $group)
  506. {
  507. $row = array('id_customer' => (int)$this->id, 'id_group' => (int)$group);
  508. Db::getInstance()->insert('customer_group', $row);
  509. }
  510. }
  511. public static function getGroupsStatic($id_customer)
  512. {
  513. if (!Group::isFeatureActive() || $id_customer == 0)
  514. return array(Configuration::get('PS_CUSTOMER_GROUP'));
  515. if (!isset(self::$_customer_groups[$id_customer]))
  516. {
  517. self::$_customer_groups[$id_customer] = array();
  518. $result = Db::getInstance()->executeS('
  519. SELECT cg.`id_group`
  520. FROM '._DB_PREFIX_.'customer_group cg
  521. WHERE cg.`id_customer` = '.(int)$id_customer);
  522. foreach ($result as $group)
  523. self::$_customer_groups[$id_customer][] = (int)$group['id_group'];
  524. }
  525. return self::$_customer_groups[$id_customer];
  526. }
  527. public function getGroups()
  528. {
  529. return Customer::getGroupsStatic((int)$this->id);
  530. }
  531. /**
  532. * @deprecated since 1.5
  533. */
  534. public function isUsed()
  535. {
  536. Tools::displayAsDeprecated();
  537. return false;
  538. }
  539. public function getBoughtProducts()
  540. {
  541. return Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('
  542. SELECT * FROM `'._DB_PREFIX_.'orders` o
  543. LEFT JOIN `'._DB_PREFIX_.'order_detail` od ON o.id_order = od.id_order
  544. WHERE o.valid = 1 AND o.`id_customer` = '.(int)$this->id);
  545. }
  546. public static function getDefaultGroupId($id_customer)
  547. {
  548. if (!Group::isFeatureActive())
  549. return Configuration::get('PS_CUSTOMER_GROUP');
  550. if (!isset(self::$_defaultGroupId[(int)$id_customer]))
  551. self::$_defaultGroupId[(int)$id_customer] = Db::getInstance()->getValue('
  552. SELECT `id_default_group`
  553. FROM `'._DB_PREFIX_.'customer`
  554. WHERE `id_customer` = '.(int)$id_customer
  555. );
  556. return self::$_defaultGroupId[(int)$id_customer];
  557. }
  558. public static function getCurrentCountry($id_customer, Cart $cart = null)
  559. {
  560. if (!$cart)
  561. $cart = Context::getContext()->cart;
  562. if (!$cart || !$cart->{Configuration::get('PS_TAX_ADDRESS_TYPE')})
  563. $id_address = (int)Db::getInstance()->getValue('
  564. SELECT `id_address`
  565. FROM `'._DB_PREFIX_.'address`
  566. WHERE `id_customer` = '.(int)$id_customer.'
  567. AND `deleted` = 0 ORDER BY `id_address`'
  568. );
  569. else
  570. $id_address = $cart->{Configuration::get('PS_TAX_ADDRESS_TYPE')};
  571. $ids = Address::getCountryAndState($id_address);
  572. return (int)$ids['id_country'] ? $ids['id_country'] : Configuration::get('PS_COUNTRY_DEFAULT');
  573. }
  574. public function toggleStatus()
  575. {
  576. parent::toggleStatus();
  577. /* Change status to active/inactive */
  578. return Db::getInstance()->execute('
  579. UPDATE `'.pSQL(_DB_PREFIX_.$this->def['table']).'`
  580. SET `date_upd` = NOW()
  581. WHERE `'.$this->def['primary'].'` = '.(int)$this->id);
  582. }
  583. public function isGuest()
  584. {
  585. return (bool)$this->is_guest;
  586. }
  587. public function transformToCustomer($id_lang, $password = null)
  588. {
  589. if (!$this->isGuest())
  590. return false;
  591. if (empty($password))
  592. $password = Tools::passwdGen();
  593. if (!Validate::isPasswd($password))
  594. return false;
  595. $this->is_guest = 0;
  596. $this->passwd = Tools::encrypt($password);
  597. $this->cleanGroups();
  598. $this->addGroups(array(Configuration::get('PS_CUSTOMER_GROUP'))); // add default customer group
  599. if ($this->update())
  600. {
  601. $vars = array(
  602. '{firstname}' => $this->firstname,
  603. '{lastname}' => $this->lastname,
  604. '{email}' => $this->email,
  605. '{passwd}' => $password
  606. );
  607. Mail::Send(
  608. (int)$id_lang,
  609. 'guest_to_customer',
  610. Mail::l('Your guest account has been transformed to customer account', (int)$id_lang),
  611. $vars,
  612. $this->email,
  613. $this->firstname.' '.$this->lastname,
  614. null,
  615. null,
  616. null,
  617. null,
  618. _PS_MAIL_DIR_,
  619. false,
  620. (int)$this->id_shop
  621. );
  622. return true;
  623. }
  624. return false;
  625. }
  626. public function setWsPasswd($passwd)
  627. {
  628. if ($this->id != 0)
  629. {
  630. if ($this->passwd != $passwd)
  631. $this->passwd = Tools::encrypt($passwd);
  632. }
  633. else
  634. $this->passwd = Tools::encrypt($passwd);
  635. return true;
  636. }
  637. /**
  638. * Check customer informations and return customer validity
  639. *
  640. * @since 1.5.0
  641. * @param boolean $with_guest
  642. * @return boolean customer validity
  643. */
  644. public function isLogged($with_guest = false)
  645. {
  646. if (!$with_guest && $this->is_guest == 1)
  647. return false;
  648. /* Customer is valid only if it can be load and if object password is the same as database one */
  649. if ($this->logged == 1 && $this->id && Validate::isUnsignedId($this->id) && Customer::checkPassword($this->id, $this->passwd))
  650. return true;
  651. return false;
  652. }
  653. /**
  654. * Logout
  655. *
  656. * @since 1.5.0
  657. */
  658. public function logout()
  659. {
  660. if (isset(Context::getContext()->cookie))
  661. Context::getContext()->cookie->logout();
  662. $this->logged = 0;
  663. }
  664. /**
  665. * Soft logout, delete everything links to the customer
  666. * but leave there affiliate's informations
  667. *
  668. * @since 1.5.0
  669. */
  670. public function mylogout()
  671. {
  672. if (isset(Context::getContext()->cookie))
  673. Context::getContext()->cookie->mylogout();
  674. $this->logged = 0;
  675. }
  676. public function getLastCart($with_order = true)
  677. {
  678. $carts = Cart::getCustomerCarts((int)$this->id, $with_order);
  679. if (!count($carts))
  680. return false;
  681. $cart = array_shift($carts);
  682. $cart = new Cart((int)$cart['id_cart']);
  683. return ($cart->nbProducts() === 0 ? (int)$cart->id : false);
  684. }
  685. public function getOutstanding()
  686. {
  687. $query = new DbQuery();
  688. $query->select('SUM(oi.total_paid_tax_incl)');
  689. $query->from('order_invoice', 'oi');
  690. $query->leftJoin('orders', 'o', 'oi.id_order = o.id_order');
  691. $query->groupBy('o.id_customer');
  692. $query->where('o.id_customer = '.(int)$this->id);
  693. $total_paid = (float)Db::getInstance()->getValue($query->build());
  694. $query = new DbQuery();
  695. $query->select('SUM(op.amount)');
  696. $query->from('order_payment', 'op');
  697. $query->leftJoin('order_invoice_payment', 'oip', 'op.id_order_payment = oip.id_order_payment');
  698. $query->leftJoin('orders', 'o', 'oip.id_order = o.id_order');
  699. $query->groupBy('o.id_customer');
  700. $query->where('o.id_customer = '.(int)$this->id);
  701. $total_rest = (float)Db::getInstance()->getValue($query->build());
  702. return $total_paid - $total_rest;
  703. }
  704. public function getWsGroups()
  705. {
  706. return Db::getInstance()->executeS('
  707. SELECT cg.`id_group` as id
  708. FROM '._DB_PREFIX_.'customer_group cg
  709. '.Shop::addSqlAssociation('group', 'cg').'
  710. WHERE cg.`id_customer` = '.(int)$this->id
  711. );
  712. }
  713. public function setWsGroups($result)
  714. {
  715. $groups = array();
  716. foreach ($result as $row)
  717. $groups[] = $row['id'];
  718. $this->cleanGroups();
  719. $this->addGroups($groups);
  720. return true;
  721. }
  722. /**
  723. * @see ObjectModel::getWebserviceObjectList()
  724. */
  725. public function getWebserviceObjectList($sql_join, $sql_filter, $sql_sort, $sql_limit)
  726. {
  727. $sql_filter .= Shop::addSqlRestriction(Shop::SHARE_CUSTOMER, 'main');
  728. return parent::getWebserviceObjectList($sql_join, $sql_filter, $sql_sort, $sql_limit);
  729. }
  730. }