337 lines
9.9 KiB
PHP
337 lines
9.9 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 Be2billSchedule extends ObjectModel
|
|
{
|
|
|
|
/**
|
|
* Be2bill Schedule ID
|
|
* @var integer
|
|
*/
|
|
public $id_b2b_schedule;
|
|
|
|
/**
|
|
* Order ID
|
|
* @var integer
|
|
*/
|
|
public $id_order;
|
|
|
|
/**
|
|
* TRANSACTIONID parameter send by the payment platform
|
|
* @var string
|
|
*/
|
|
public $transaction_id;
|
|
|
|
/**
|
|
* SCHEDULEID parameter send by the payment platform
|
|
* @var string
|
|
*/
|
|
public $schedule_id;
|
|
|
|
/**
|
|
* Be2bill Alias ID
|
|
* @var integer
|
|
*/
|
|
public $id_b2b_alias;
|
|
|
|
/**
|
|
* Amount to capture
|
|
* @var float
|
|
*/
|
|
public $amount;
|
|
|
|
/**
|
|
* Object creation date
|
|
* @var date
|
|
*/
|
|
public $date_add;
|
|
|
|
/**
|
|
* Date when the capture can be done
|
|
* @var date
|
|
*/
|
|
public $date_scheduled;
|
|
|
|
/**
|
|
* Date when the capture is done
|
|
* @var date
|
|
*/
|
|
public $date_done;
|
|
|
|
/**
|
|
* Is the capture done?
|
|
* @var boolean
|
|
*/
|
|
public $is_done;
|
|
|
|
/**
|
|
* Is the capture done?
|
|
* @var boolean
|
|
*/
|
|
public $option = '';
|
|
|
|
/**
|
|
* @see ObjectModel::$definition
|
|
*/
|
|
public static $definition = array(
|
|
'table' => 'b2b_schedule',
|
|
'primary' => 'id_b2b_schedule',
|
|
'fields' => array(
|
|
'id_order' => array(
|
|
'type' => self::TYPE_INT,
|
|
'validate' => 'isInt',
|
|
'required' => true
|
|
),
|
|
'transaction_id' => array(
|
|
'type' => self::TYPE_STRING,
|
|
'validate' => 'isString'
|
|
),
|
|
'schedule_id' => array(
|
|
'type' => self::TYPE_STRING,
|
|
'validate' => 'isString'
|
|
),
|
|
'id_b2b_alias' => array(
|
|
'type' => self::TYPE_INT,
|
|
'validate' => 'isInt'
|
|
),
|
|
'amount' => array(
|
|
'type' => self::TYPE_FLOAT,
|
|
'validate' => 'isFLoat',
|
|
'required' => true
|
|
),
|
|
'date_add' => array(
|
|
'type' => self::TYPE_DATE,
|
|
'validate' => 'isDate'
|
|
),
|
|
'date_scheduled' => array(
|
|
'type' => self::TYPE_DATE,
|
|
'validate' => 'isDateFormat'
|
|
),
|
|
'date_done' => array(
|
|
'type' => self::TYPE_DATE,
|
|
'validate' => 'isDateFormat'
|
|
),
|
|
'is_done' => array(
|
|
'type' => self::TYPE_BOOL,
|
|
'validate' => 'isBool'
|
|
),
|
|
'option' => array(
|
|
'type' => self::TYPE_STRING,
|
|
'validate' => 'isString'
|
|
)
|
|
)
|
|
);
|
|
|
|
/**
|
|
* Create Schedule table
|
|
* @return boolean
|
|
*/
|
|
public static function createBe2billScheduleTable()
|
|
{
|
|
$sql = 'CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'b2b_schedule` (
|
|
`id_b2b_schedule` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
|
|
`id_order` INT(10) UNSIGNED NOT NULL,
|
|
`transaction_id` VARCHAR(25) NULL DEFAULT NULL,
|
|
`schedule_id` VARCHAR(25) NULL DEFAULT NULL,
|
|
`id_b2b_alias` INT(10) UNSIGNED NULL DEFAULT NULL,
|
|
`amount` DECIMAL(17,2) NOT NULL,
|
|
`date_add` DATETIME NOT NULL,
|
|
`date_scheduled` DATETIME NULL DEFAULT NULL,
|
|
`date_done` DATETIME NULL DEFAULT NULL,
|
|
`is_done` INT(1) NOT NULL DEFAULT \'0\',
|
|
`option` VARCHAR(50) NULL DEFAULT NULL,
|
|
PRIMARY KEY (`id_b2b_schedule`)
|
|
) ENGINE='._MYSQL_ENGINE_.' DEFAULT CHARSET=utf8';
|
|
|
|
return Db::getInstance()->execute($sql);
|
|
}
|
|
/**
|
|
* return schedule for order, argument id_order
|
|
* @return array|boolean
|
|
*/
|
|
public static function getByIdOrder($id_order)
|
|
{
|
|
if (!Validate::isUnsignedId($id_order)) {
|
|
return false;
|
|
}
|
|
|
|
return Db::getInstance()->executeS('
|
|
SELECT *
|
|
FROM `'._DB_PREFIX_.'b2b_schedule`
|
|
WHERE `id_order` = \''.(int)pSQL($id_order).'\'
|
|
ORDER BY `date_scheduled` ASC
|
|
');
|
|
}
|
|
/**
|
|
* return schedule for order, argument reference
|
|
* @return array
|
|
*/
|
|
public static function getByOrderReference($reference)
|
|
{
|
|
return Db::getInstance()->executeS('
|
|
SELECT *
|
|
FROM `'._DB_PREFIX_.'b2b_schedule`
|
|
WHERE `id_order` IN (
|
|
SELECT `id_order`
|
|
FROM `'._DB_PREFIX_.'orders`
|
|
WHERE `reference` = \''.pSQL($reference).'\'
|
|
)
|
|
ORDER BY `date_scheduled` ASC
|
|
');
|
|
}
|
|
/**
|
|
* return schedule to capture for order, argument reference
|
|
* @return array|boolean
|
|
*/
|
|
public static function getOrderScheduleToCapture($reference)
|
|
{
|
|
return Db::getInstance()->executeS('
|
|
SELECT *
|
|
FROM `'._DB_PREFIX_.'b2b_schedule`
|
|
WHERE `id_order` IN (
|
|
SELECT `id_order`
|
|
FROM `'._DB_PREFIX_.'orders`
|
|
WHERE `reference` = \''.pSQL($reference).'\'
|
|
)
|
|
AND `is_done` = 0 AND `option` = "'.MerchandConfigurationOptions::TYPE_DELIVERY.'"
|
|
');
|
|
}
|
|
/**
|
|
* return next schedule for order, argument reference
|
|
* @return array|boolean
|
|
*/
|
|
public static function getNextOrderIdScheduleToCapture($reference)
|
|
{
|
|
return Db::getInstance()->getValue('
|
|
SELECT id_b2b_schedule
|
|
FROM `'._DB_PREFIX_.'b2b_schedule`
|
|
WHERE `id_order` IN (
|
|
SELECT `id_order`
|
|
FROM `'._DB_PREFIX_.'orders`
|
|
WHERE `reference` = \''.pSQL($reference).'\'
|
|
)
|
|
AND `is_done` = 0
|
|
ORDER BY `date_scheduled` ASC
|
|
');
|
|
}
|
|
/**
|
|
* return schedule to capture for order, argument type capture(defered, ntimes, delivery)
|
|
* @return array|boolean
|
|
*/
|
|
public static function getSchedulesToCapture($module_name, $option = '')
|
|
{
|
|
$sql = '
|
|
SELECT b.`id_b2b_schedule`, o.`id_order`, o.`reference`, b.`transaction_id`, b.`amount`
|
|
FROM `'._DB_PREFIX_.'b2b_schedule` b
|
|
LEFT JOIN `'._DB_PREFIX_.'orders` o ON (o.`id_order` = b.`id_order`)
|
|
WHERE DATEDIFF(NOW(), `date_scheduled`) >= 0
|
|
AND o.`module` = \''.pSQL($module_name).'\'
|
|
AND (o.`valid` = 1 OR current_state = '.pSQL(Configuration::get('BE2BILLDEF_OS_PAYMENT')).')
|
|
AND b.`is_done` = 0
|
|
';
|
|
if ($option) {
|
|
$sql .=' AND b.option = "'.pSQL($option).'"';
|
|
}
|
|
return Db::getInstance()->ExecuteS($sql);
|
|
}
|
|
/**
|
|
* return payment schedule for order
|
|
* @return array
|
|
*/
|
|
public static function getPaymentSchedule($amount, $id_account = 0)
|
|
{
|
|
$nb_times = (int)MerchandConfigurationOptions::getAccountOptionsParameterValue(
|
|
$id_account,
|
|
MerchandConfigurationOptions::TYPE_NTIMES
|
|
);
|
|
$schedule = array();
|
|
|
|
$splited_amount = $amount/$nb_times;
|
|
|
|
// if non integer amount
|
|
if (!is_int($splited_amount)) {
|
|
// first time
|
|
$first_amount = (int)$splited_amount + $nb_times * ($splited_amount-(int)$splited_amount);
|
|
// other times
|
|
$next_amounts = (int)$splited_amount;
|
|
} else {
|
|
$first_amount = $next_amounts = $splited_amount;
|
|
}
|
|
|
|
/*
|
|
* compute dates
|
|
*/
|
|
for ($i = 0; $i < $nb_times; $i++) {
|
|
switch($i) {
|
|
case 0:
|
|
// date du jour
|
|
$o_today = new DateTime();
|
|
$schedule[$o_today->format('Y-m-d')] = $first_amount;
|
|
break;
|
|
case 1:
|
|
case 2:
|
|
// date of the day
|
|
$o_today = new DateTime();
|
|
$s_year = $o_today->format('Y');
|
|
$s_month = $o_today->format('n');
|
|
$s_day = $o_today->format('d');
|
|
|
|
// calcul
|
|
$s_year += floor($i/12);
|
|
$i = $i%12;
|
|
$s_month += $i;
|
|
if ($s_month > 12) {
|
|
$s_year ++;
|
|
$s_month = $s_month % 12;
|
|
if ($s_month === 0) {
|
|
$s_month = 12;
|
|
}
|
|
}
|
|
|
|
if (!checkdate($s_month, $s_day, $s_year)) {
|
|
$o_new_date = DateTime::createFromFormat('Y-n-j', $s_year.'-'.$s_month.'-1');
|
|
$o_new_date->modify('last day of');
|
|
} else {
|
|
$o_new_date = DateTime::createFromFormat('Y-n-d', $s_year.'-'.$s_month.'-'.$s_day);
|
|
}
|
|
$o_new_date->setTime($o_today->format('H'), $o_today->format('i'), $o_today->format('s'));
|
|
|
|
$schedule[$o_new_date->format('Y-m-d')] = $next_amounts;
|
|
break;
|
|
case 3:
|
|
// date of the day
|
|
$o_today = new DateTime();
|
|
$schedule[$o_today->modify('+89 day')->format('Y-m-d')] = $next_amounts;
|
|
break;
|
|
}
|
|
}
|
|
return $schedule;
|
|
}
|
|
|
|
/**
|
|
* Returns schedule array from old tables or false
|
|
* @param none
|
|
* @return boolean|string
|
|
*/
|
|
public static function getOldList()
|
|
{
|
|
return Db::getInstance()->executeS('
|
|
SELECT *
|
|
FROM `'._DB_PREFIX_.'be2bill_schedule`
|
|
');
|
|
}
|
|
}
|