roykin/modules/be2bill/models/MerchandConfiguration.php
Serveur preprod be83006fb6 push dev
2016-06-01 11:53:40 +02:00

653 lines
28 KiB
PHP

<?php
/**
* 1997-2016 Quadra Informatique
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0) that is available
* through the world-wide-web at this URL: http://www.opensource.org/licenses/OSL-3.0
* If you are unable to obtain it through the world-wide-web, please send an email
* to modules@quadra-informatique.fr so we can send you a copy immediately.
*
* @author Quadra Informatique <modules@quadra-informatique.fr>
* @copyright 1997-2016 Quadra Informatique
* @license http://www.opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
*/
class MerchandConfiguration extends ObjectModel
{
const TYPE_PAYMENT = 'payment';
const TYPE_AUTHORIZATION = 'authorization';
const TYPE_CAPTURE = 'capture';
const TYPE_REFUND = 'refund';
const MODE_FORM = 'form';
const MODE_FORMIFRAME = 'form-iframe';
const MODE_DIRECT = 'directlink';
const MODE_SUBMIT = 'direct-submit';
const MODE_REDIRECT = 'redirect';
public $id_b2b_merchand_configuration_account;
public $b2b_xml_account_type_code;
public $b2b_xml_mode_code;
public $currency_iso;
public $login_account;
public $password;
public $logo_url;
public $name;
public $active;
public static $definition = array(
'table' => 'b2b_merchand_configuration_account',
'primary' => 'id_b2b_merchand_configuration_account',
'multilang' => true,
'multilang_shop' => true,
'fields' => array(
'b2b_xml_account_type_code' => array(
'type' => ObjectModel :: TYPE_STRING,
'lang' => false,
'validate' => 'isGenericName',
'required' => true,
),
'b2b_xml_mode_code' => array(
'type' => ObjectModel :: TYPE_STRING,
'lang' => false,
'validate' => 'isGenericName',
'required' => true,
),
'currency_iso' => array(
'type' => ObjectModel :: TYPE_STRING,
'lang' => false,
'validate' => 'isGenericName',
'required' => true,
),
'login_account' => array(
'type' => ObjectModel :: TYPE_STRING,
'lang' => false,
'validate' => 'isGenericName',
'required' => true,
'size' => 256
),
'logo_url' => array(
'type' => ObjectModel :: TYPE_STRING,
'lang' => false,
'validate' => 'isGenericName',
'required' => false,
'size' => 256
),
'password' => array(
'type' => ObjectModel :: TYPE_HTML,
'lang' => false,
//'validate' => 'isGenericName',
'required' => true,
'size' => 256
),
'active' => array(
'type' => self::TYPE_BOOL,
'validate' => 'isBool',
),
/* Lang fields */
'name' => array(
'type' => self::TYPE_STRING,
'lang' => true,
'validate' => 'isGenericName',
'required' => true,
'size' => 128
),
)
);
/**
* return available payment in front for customer
* @return array|boolean
*/
public static function getAvailablePayments($iso_currency, $iso_country, $amount, $id_customer)
{
if (!$iso_country || !$iso_currency) {
return false;
}
$id_shop = Context::getContext()->shop->id;
$id_lang = Context::getContext()->language->id;
$sql = 'SELECT ma.id_b2b_merchand_configuration_account,ma.login_account,
ma.password, ma.b2b_xml_account_type_code, ma.logo_url, mal.name
FROM `'._DB_PREFIX_.'b2b_merchand_configuration_account` ma
LEFT JOIN `'._DB_PREFIX_.'b2b_merchand_configuration_account_shop` mas
ON mas.id_b2b_merchand_configuration_account = ma.id_b2b_merchand_configuration_account
LEFT JOIN `'._DB_PREFIX_.'b2b_merchand_configuration_account_countries` mac
ON mac.id_b2b_merchand_configuration_account = ma.id_b2b_merchand_configuration_account
LEFT JOIN `'._DB_PREFIX_.'b2b_merchand_configuration_account_lang` mal
ON mal.id_b2b_merchand_configuration_account = ma.id_b2b_merchand_configuration_account
AND mal.id_shop = mas.id_shop AND mal.id_lang = '.(int)$id_lang.'
WHERE ma.active = 1 AND mas.id_shop = '.(int)$id_shop.' AND mac.country_iso = "'.pSQL($iso_country).'"
AND ma.currency_iso = "'.pSQL($iso_currency).'"
ORDER BY mal.name';
$result = Db::getInstance()->executeS($sql);
if ($result) {
$i = 0;
foreach ($result as $account) {
/* // if logo has been updated by xml and not by config
$logo = Be2billApi::getLogoUrl($account['b2b_xml_account_type_code']);
if ($logo && isset($logo['b2b_xml_account_type_logo'])) {
$result[$i]['logo_url'] = $logo['b2b_xml_account_type_logo'];
}
if (Tools::file_exists_no_cache(
_PS_MODULE_DIR_.'be2bill/views/img/'.$account['id_b2b_merchand_configuration_account'].'.jpg'
)
) {
$result[$i]['logo_url'] = '';
}
*/
$result[$i]['logo_url'] = '';
$extensions = array('jpg','svg','png');
foreach ($extensions as $extension){
if (Tools::file_exists_no_cache(
_PS_MODULE_DIR_.'be2bill/views/img/'.$account['id_b2b_merchand_configuration_account'].'.'.$extension)
){
$result[$i]['logo_url'] = $account['id_b2b_merchand_configuration_account'].'.'.$extension;
break;
}
}
$result[$i]['has_oneclick'] = 0;
$result[$i]['has_defered'] = 0;
$result[$i]['has_delivery'] = 0;
$result[$i]['has_standard'] = 0;
$result[$i]['has_oneclickcvv'] = MerchandConfigurationOptions::getOneClickCvv(
$account['id_b2b_merchand_configuration_account']
);
$result[$i]['has_alias'] = Be2billAlias::getByIdCustomer(
(int)$id_customer,
$account['id_b2b_merchand_configuration_account']
);
$result[$i]['mode'] = self::getMainMode($account['b2b_xml_account_type_code']);
if (!$account['login_account'] || !$account['password']) {
unset($result[$i]);
} else {
$options = MerchandConfigurationOptions::getFrontOptions(
$account['id_b2b_merchand_configuration_account']
);
if ($options) {
$j = 0;
foreach ($options as $option) {
$available = true;
if ((float)$option['min_amount'] && $amount < (float)$option['min_amount']) {
$available = false;
}
if ((float)$option['max_amount'] && $amount > (float)$option['max_amount']) {
$available = false;
}
if ($available) {
$result[$i]['options'][] = $option;
$operation = self::TYPE_PAYMENT;
if ($option['b2b_xml_option'] == MerchandConfigurationOptions::TYPE_ONECLICK
&& $option['active']) {
$result[$i]['has_oneclick'] = 1;
}
if ($option['b2b_xml_option'] == MerchandConfigurationOptions::TYPE_DEFERED
&& $option['active']) {
$result[$i]['has_defered'] = 1;
$operation = self::TYPE_AUTHORIZATION;
}
if ($option['b2b_xml_option'] == MerchandConfigurationOptions::TYPE_DELIVERY
&& $option['active']) {
$result[$i]['has_delivery'] = 1;
$operation = self::TYPE_AUTHORIZATION;
}
if ($option['b2b_xml_option'] == MerchandConfigurationOptions::TYPE_STANDARD
&& $option['active']) {
$result[$i]['has_standard'] = 1;
}
$result[$i]['options'][$j]['has_ident_doc_id'] = MerchandConfigurationOptions::hasIdentDocId(
$account['b2b_xml_account_type_code'],
$operation,
$iso_country
);
$j++;
}
}
} else {
unset($result[$i]);
}
}
$i++;
}
if (!empty($result)) {
return $result;
}
}
return false;
}
/**
* return default parameters with values for an account type configuration
* @return array|boolean
*/
public static function getDefaultParameters($reference, $id_account, $option, $id_order = 0 , &$params_no_hash = null)
{
// Get first order by reference (ORDERID)
$cart = Context::getContext()->cart;
if (!$cart && $id_order) {
$cart = Cart::getCartByOrderId($id_order);
}
// Get invoice address
$address = new Address($cart->id_address_invoice);
$string_address = '';
if ($address->id) {
$string_address = implode(
' ',
array(
trim($address->address1),
trim($address->postcode),
trim($address->city),
trim($address->country)
)
);
}
$address->address1 = str_replace('"', '', $address->address1);
$country_invoice = new Country($address->id_country);
$formated_invoice_phone = self::getFormatedNumber($address->phone, $country_invoice->call_prefix);
$formated_invoice_mobile = self::getFormatedNumber($address->phone_mobile, $country_invoice->call_prefix);
// Get shipping address
$address_delivery = new Address($cart->id_address_delivery);
$address_delivery->address1 = str_replace('"', '', $address_delivery->address1);
$country_delivery = new Country($address_delivery->id_country);
$formated_delivery_phone = self::getFormatedNumber($address_delivery->phone, $country_delivery->call_prefix);
$formated_delivery_mobile = self::getFormatedNumber($address_delivery->phone_mobile, $country_delivery->call_prefix);
// Get customer
$customer = new Customer($cart->id_customer);
$dob = '';
if ($customer->birthday && $customer->birthday != '0000-00-00') {
$dob = $customer->birthday;
}
$operation = '';
switch ($option) {
case MerchandConfigurationOptions::TYPE_STANDARD:
$operation = self::TYPE_PAYMENT;
break;
case MerchandConfigurationOptions::TYPE_DEFERED:
$operation = self::TYPE_AUTHORIZATION;
break;
case MerchandConfigurationOptions::TYPE_DELIVERY:
$operation = self::TYPE_AUTHORIZATION;
break;
case MerchandConfiguration::TYPE_CAPTURE:
$operation = self::TYPE_CAPTURE;
break;
case MerchandConfiguration::TYPE_REFUND:
$operation = self::TYPE_REFUND;
break;
default:
$operation = self::TYPE_PAYMENT;
break;
}
$shop_iso = Language::getIsoById(Context::getContext()->language->id);
$merchand_account = new MerchandConfiguration((int)$id_account);
$parameters = $merchand_account->getAccountTypeParameters(
$merchand_account->b2b_xml_account_type_code,
$country_invoice->iso_code,
$operation
);
if (!$parameters) {
return false;
}
$default_params = array();
foreach ($parameters as $param) {
$version = $param['b2b_xml_account_type_parameter_set_version'];
$type = Tools::strtoupper($param['b2b_xml_parameter_type']);
$value = Tools::strtoupper($param['b2b_xml_parameter_value']);
$merchant_hash = Tools::strtoupper($param['b2b_xml_parameter_merchant_hash']);
//Update array no hash
if($merchant_hash == 'NO'){
$params_no_hash[] = $param['b2b_xml_parameter_name'];
}
if ($type == 'REQUIRED' && $value == 'YES') {
// todo all set all value possible
switch ($param['b2b_xml_parameter_name']) {
case 'AMOUNT':
$default_params['AMOUNT'] = Be2billApi::getConvertedPrice(
$cart->getOrderTotal(),
$cart->id_currency
) * 100;
break;
/* todo check real gestion */
case 'ASKIBAN':
$default_params['ASKIBAN'] = 'NO';
break;
case 'BILLINGADDRESS':
$default_params['BILLINGADDRESS'] = Tools::substr($address->address1, 0, 44);
break;
case 'BILLINGCITY':
$default_params['BILLINGCITY'] = Tools::substr($address->city, 0, 249);
break;
case 'BILLINGCOUNTRY':
$default_params['BILLINGCOUNTRY'] = $country_invoice->iso_code;
break;
case 'BILLINGFIRSTNAME':
$default_params['BILLINGFIRSTNAME'] = Tools::substr($address->firstname, 0, 9);
break;
case 'BILLINGLASTNAME':
$default_params['BILLINGLASTNAME'] = Tools::substr($address->lastname, 0, 24);
break;
case 'BILLINGMOBILEPHONE':
if ($formated_invoice_mobile) {
$default_params['BILLINGMOBILEPHONE'] = Tools::substr($formated_invoice_mobile, 0, 31);
} else {
$default_params['BILLINGMOBILEPHONE'] = Tools::substr($formated_invoice_phone, 0, 31);
}
break;
case 'BILLINGPHONE':
if ($formated_invoice_phone) {
$default_params['BILLINGPHONE'] = Tools::substr($formated_invoice_phone, 0, 31);
} else {
$default_params['BILLINGPHONE'] = Tools::substr($formated_invoice_mobile, 0, 31);
}
break;
case 'BILLINGPOSTALCODE':
$default_params['BILLINGPOSTALCODE'] = Tools::substr($address->postcode, 0, 8);
break;
case 'CARDFULLNAME':
$default_params['CARDFULLNAME'] = $address->firstname.' '.$address->lastname;
break;
case 'CART[N][DISCOUNT]':
$products = $cart->getProducts();
$i = 0;
foreach ($products as $product) {
// todo meth calcul %
$default_params['CART'][$i]['DISCOUNT'] = '0';
$default_params['CART'][$i]['MERCHANTITEMID'] = $product['id_product'];
$default_params['CART'][$i]['NAME'] = $product['name'];
$default_params['CART'][$i]['PRICE'] = Be2billApi::getConvertedPrice(
number_format($product['price'], 2, '.', ''),
$cart->id_currency
) * 100;
$default_params['CART'][$i]['QUANTITY'] = $product['cart_quantity'];
$default_params['CART'][$i]['TAX'] = $product['rate'];
$i++;
}
break;
case 'CLIENTADDRESS':
$default_params['CLIENTADDRESS'] = str_replace('"', '', $string_address);
$default_params['CLIENTADDRESS'] = Tools::substr($default_params['CLIENTADDRESS'], 0, 509);
break;
case 'CLIENTDOB':
if ($dob) {
$default_params['CLIENTDOB'] = $dob;
} else {
$default_params['CLIENTDOB'] = '1970-01-01';
}
break;
case 'CLIENTEMAIL':
$default_params['CLIENTEMAIL'] = Tools::substr($customer->email, 0, 254);
break;
/* non decrit */
case 'CLIENTGENDER':
$default_params['CLIENTGENDER'] = 'M';
if ($customer->id_gender == 2) {
$default_params['CLIENTGENDER'] = 'F';
}
break;
case 'CLIENTIDENT':
$default_params['CLIENTIDENT'] = (int)$customer->id.'_'.$customer->email;
$default_params['CLIENTIDENT'] = Tools::substr($default_params['CLIENTIDENT'], 0, 254);
break;
case 'CLIENTIP':
$default_params['CLIENTIP'] = Tools::substr($_SERVER['REMOTE_ADDR'], 0, 14);
break;
case 'CLIENTUSERAGENT':
$default_params['CLIENTUSERAGENT'] = Tools::substr($_SERVER['HTTP_USER_AGENT'], 0, 254);
break;
case 'CREATEALIAS':
$default_params['CREATEALIAS'] = 'YES';
break;
case 'DESCRIPTION':
$default_params['DESCRIPTION'] = Tools::ucfirst($operation);
break;
/* setting later */
case 'EXTRADATA':
$default_params['EXTRADATA'] = '';
break;
case 'HIDECARDFULLNAME':
$default_params['HIDECARDFULLNAME'] = 'NO';
break;
case 'HIDECLIENTEMAIL':
$default_params['HIDECLIENTEMAIL'] = 'YES';
break;
case 'IDENTIFICATIONDOCID':
$default_params['IDENTIFICATIONDOCID'] = '';
break;
case 'IDENTIFICATIONDOCTYPE':
$default_params['IDENTIFICATIONDOCTYPE'] = 'TAXSTATEMENT';
break;
case 'IDENTIFIER':
$default_params['IDENTIFIER'] = $merchand_account->login_account;
break;
case 'LANGUAGE':
$default_params['LANGUAGE'] = $shop_iso;
break;
case 'METADATA':
//$default_params['METADATA'] = '';
break;
case 'OPERATIONTYPE':
$default_params['OPERATIONTYPE'] = $operation;
break;
case 'ORDERID':
$default_params['ORDERID'] = $reference;
break;
case 'SHIPTOADDRESS':
$default_params['SHIPTOADDRESS'] = Tools::substr($address_delivery->address1, 0, 44);
break;
case 'SHIPTOCITY':
$default_params['SHIPTOCITY'] = Tools::substr($address_delivery->city, 0, 249);
break;
case 'SHIPTOCOUNTRY':
$default_params['SHIPTOCOUNTRY'] = $country_delivery->iso_code;
break;
case 'SHIPTOFIRSTNAME':
$default_params['SHIPTOFIRSTNAME'] = Tools::substr($address_delivery->firstname, 0, 9);
break;
case 'SHIPTOLASTNAME':
$default_params['SHIPTOLASTNAME'] = Tools::substr($address_delivery->lastname, 0, 24);
break;
case 'SHIPTOPHONE':
if ($formated_delivery_phone) {
$default_params['SHIPTOPHONE'] = Tools::substr($formated_delivery_phone, 0, 31);
} else {
$default_params['SHIPTOPHONE'] = Tools::substr($formated_delivery_mobile, 0, 31);
}
break;
case 'SHIPTOPOSTALCODE':
$default_params['SHIPTOPOSTALCODE'] = Tools::substr($address_delivery->postcode, 0, 8);
break;
case 'TRANSACTIONID':
$default_params['TRANSACTIONID'] = '';
break;
case 'VERSION':
$default_params['VERSION'] = $version;
break;
}
}
}
return $default_params;
}
/**
* Get account type parameters
* @param string $account_type_code, string operation
* @return array|boolean
*/
public static function getAccountTypeParameters($account_type_code, $iso_country, $operation = self::TYPE_PAYMENT)
{
if ($account_type_code) {
$no_restriction = Be2billApi::hasNotCountriesRestriction($account_type_code);
if ($no_restriction) {
$sql = 'SELECT psp.*,ps.b2b_xml_account_type_parameter_set_version
FROM `'._DB_PREFIX_.'b2b_xml_account_type_parameter_set` ps
LEFT JOIN `'._DB_PREFIX_.'b2b_xml_account_type_parameter_set_operation` pso
ON ps.id_b2b_xml_account_type_parameter_set = pso.id_b2b_xml_account_type_parameter_set
LEFT JOIN `'._DB_PREFIX_.'b2b_xml_account_type_parameter_set_parameters` psp
ON psp.id_b2b_xml_account_type_parameter_set = ps.id_b2b_xml_account_type_parameter_set
WHERE ps.b2b_xml_account_type_code = "'.pSQL($account_type_code).'"
AND pso.b2b_xml_operation_code = "'.pSQL($operation).'"';
} else {
$sql = 'SELECT psp.*,ps.b2b_xml_account_type_parameter_set_version
FROM `'._DB_PREFIX_.'b2b_xml_account_type_parameter_set` ps
LEFT JOIN `'._DB_PREFIX_.'b2b_xml_account_type_parameter_set_operation` pso
ON ps.id_b2b_xml_account_type_parameter_set = pso.id_b2b_xml_account_type_parameter_set
LEFT JOIN `'._DB_PREFIX_.'b2b_xml_account_type_parameter_set_parameters` psp
ON psp.id_b2b_xml_account_type_parameter_set = ps.id_b2b_xml_account_type_parameter_set
LEFT JOIN `'._DB_PREFIX_.'b2b_xml_account_type_parameter_set_countries` psc
ON ps.id_b2b_xml_account_type_parameter_set = psc.id_b2b_xml_account_type_parameter_set
WHERE ps.b2b_xml_account_type_code = "'.pSQL($account_type_code).'"
AND pso.b2b_xml_operation_code = "'.pSQL($operation).'"
AND psc.country_iso = "'.pSQL($iso_country).'"';
}
return Db::getInstance()->executeS($sql);
}
return false;
}
/**
* Get account type display mode
* @param int $id_account
* @return array|boolean
*/
public static function getDisplayMode($id_account)
{
if ((int)$id_account) {
$sql = 'SELECT b2b_xml_mode_code
FROM `'._DB_PREFIX_.'b2b_merchand_configuration_account`
WHERE id_b2b_merchand_configuration_account = '.(int)$id_account;
return Db::getInstance()->getValue($sql);
}
return false;
}
/**
* Get account type password
* @param int $id_account
* @return array|boolean
*/
public static function getPassword($id_account)
{
if ((int)$id_account) {
$sql = 'SELECT password
FROM `'._DB_PREFIX_.'b2b_merchand_configuration_account`
WHERE id_b2b_merchand_configuration_account = '.(int)$id_account;
return Db::getInstance()->getValue($sql);
}
return false;
}
/**
* Get account type defered days values
* @param int $id_account
* @return array|boolean
*/
public static function getDeferedDays($id_account)
{
if ((int)$id_account) {
$sql = 'SELECT b2b_xml_option_extra
FROM `'._DB_PREFIX_.'b2b_merchand_configuration_account_options`
WHERE id_b2b_merchand_configuration_account = '.(int)$id_account.' AND b2b_xml_option = "defered"';
return Db::getInstance()->getValue($sql);
}
return false;
}
/**
* Get account type display main mode
* @param string $account_type_code
* @return array|boolean
*/
public static function getMainMode($account_type_code)
{
if ($account_type_code) {
$modes = Be2billApi::getAvailableModesByAccountType($account_type_code);
if (is_array($modes)) {
$is_form = false;
$main_mode = '';
foreach ($modes as $mode) {
switch ($mode['id']) {
case self::MODE_DIRECT:
$main_mode = self::MODE_DIRECT;
break;
case self::MODE_FORM:
$is_form = true;
$main_mode = self::MODE_FORM;
break;
case self::MODE_FORMIFRAME:
$is_form = true;
$main_mode = self::MODE_FORMIFRAME;
break;
case self::MODE_REDIRECT:
$main_mode = self::MODE_REDIRECT;
break;
case self::MODE_SUBMIT:
$main_mode = self::MODE_SUBMIT;
break;
}
}
if ($is_form) {
$main_mode = self::MODE_FORM;
}
return $main_mode;
}
}
return false;
}
/**
* return formatted number phone
* @param string $phone_number string $prefix
* @return string
*/
public static function getFormatedNumber($phone_number, $prefix)
{
if ($phone_number) {
if ($prefix) {
$phone_number = Tools::substr($phone_number, 1);
$phone_number = '00'.$prefix.$phone_number;
return $phone_number;
}
return $phone_number;
}
return false;
}
/**
* Get account type id by account
* @param string $login_account
* @return array|boolean
*/
public static function getIdByReference($reference)
{
if ($reference) {
$sql = 'SELECT ma.id_b2b_merchand_configuration_account
FROM `'._DB_PREFIX_.'orders` od
LEFT JOIN `'._DB_PREFIX_.'b2b_transaction` bt ON bt.id_order = od.id_order
LEFT JOIN `'._DB_PREFIX_.'b2b_merchand_configuration_account` ma ON ma.id_b2b_merchand_configuration_account = bt.id_merchand_account
WHERE od.reference = "'.pSQL($reference).'"
LIMIT 1';
$result = Db::getInstance()->executeS($sql);
$id_account = 0;
foreach ($result as $row) {
$id_account = $row['id_b2b_merchand_configuration_account'];
}
return $id_account;
}
return false;
}
}