235 lines
11 KiB
PHP
235 lines
11 KiB
PHP
<?php
|
|
/**
|
|
* 2007-2016 Decanet
|
|
*
|
|
* NOTICE OF LICENSE
|
|
*
|
|
* This source file is subject to the Academic Free License (AFL 3.0)
|
|
* that is bundled with this package in the file LICENSE.txt.
|
|
* It is also available through the world-wide-web at this URL:
|
|
* http://opensource.org/licenses/afl-3.0.php
|
|
* If you did not receive a copy of the license and are unable to
|
|
* obtain it through the world-wide-web, please send an email
|
|
* to license@shoppinity.com so we can send you a copy immediately.
|
|
*
|
|
* DISCLAIMER
|
|
*
|
|
* Do not edit or add to this file if you wish to upgrade Shoppinity to newer
|
|
* versions in the future. If you wish to customize Shoppinity for your
|
|
* needs please refer to http://www.shoppinity.com for more information.
|
|
*
|
|
* @author DECANET SARL <decanet.fr>
|
|
* @copyright 2007-2016 DECANET SARL
|
|
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
|
|
* International Registered Trademark & Property of Shoppinity
|
|
*/
|
|
|
|
require_once dirname(__FILE__) . '/../../config/config.inc.php';
|
|
require_once(dirname(__FILE__).'/classes/PrevSepaOrder.php');
|
|
require_once(dirname(__FILE__).'/classes/PrevSepaOrderTraited.php');
|
|
require_once(dirname(__FILE__).'/classes/PrevSepa.php');
|
|
require_once(dirname(__FILE__).'/classes/SepaDD.php');
|
|
require_once(dirname(__FILE__).'/prelevementsepa.php');
|
|
|
|
|
|
$sepatodo = PrevSepaOrder::getAllTodo();
|
|
|
|
$mod_obj = new PrelevementSEPA();
|
|
$errors = array();
|
|
$order_state = false;
|
|
$token = Tools::getValue('token');
|
|
|
|
if ($token != $mod_obj->conf_keys['token']) {
|
|
Tools::displayError('Invalid token given!');
|
|
die();
|
|
}
|
|
|
|
if ((int)$mod_obj->conf_keys['state_sepa']) {
|
|
$id_order_state = (int)$mod_obj->conf_keys['state_sepa'];
|
|
$order_state = new OrderState($id_order_state);
|
|
if (!Validate::isLoadedObject($order_state)) {
|
|
$order_state = false;
|
|
}
|
|
}
|
|
|
|
if (count($sepatodo)>0) {
|
|
$sepatraited_obj = new PrevSepaOrderTraited();
|
|
$sepatraited_obj->order_state = (int)Tools::getValue('id_order_state');
|
|
$sepatraited_obj->add();
|
|
foreach ($sepatodo as $s) {
|
|
$s['order'] = new Order((int)$s['id_order']);
|
|
if ($s['order']->valid == 1) {
|
|
$sepatraited_obj->addOrder((int)$s['id_order']);
|
|
|
|
$sepadd = new SepaDD(array(
|
|
'name' => $mod_obj->conf_keys['creancier'],
|
|
'IBAN' => $mod_obj->conf_keys['IBAN'],
|
|
'BIC' => $mod_obj->conf_keys['BIC'],
|
|
'batch' => true,
|
|
'creditor_id' => $mod_obj->conf_keys['ICS'],
|
|
'delay_working' => $mod_obj->conf_keys['delay_working'],
|
|
'currency' => 'EUR'
|
|
));
|
|
$id_sepa = $s['id_sepa_detail'];
|
|
if (count($errors) == 0) {
|
|
$sepa_obj = new PrevSepaOrder((int)$id_sepa);
|
|
$order = new Order((int)$sepa_obj->id_order);
|
|
$id_order = $order->id;
|
|
if (!Validate::isLoadedObject($order)) {
|
|
$errors[] = sprintf(
|
|
Tools::displayError('Order #%d cannot be loaded'),
|
|
$id_order
|
|
);
|
|
} else {
|
|
try {
|
|
$customer = new Customer((int)$order->id_customer);
|
|
$sepa_customer_obj = new PrevSepa($sepa_obj->id_sepa);
|
|
$sepa_obj->traite = 1;
|
|
$amount = (int)($order->total_paid * 100);
|
|
$sepa_obj->save();
|
|
if ((Tools::strlen($customer->company)>0)) {
|
|
$name = $customer->company;
|
|
} elseif (Tools::strlen($customer->firstname.$customer->lastname) > 3) {
|
|
$name = $customer->firstname.' '.$customer->lastname;
|
|
} else {
|
|
$name = 'No name';
|
|
}
|
|
|
|
$payment = array(
|
|
'name' => $name,
|
|
'IBAN' => $sepa_customer_obj->iban,
|
|
'BIC' => $sepa_customer_obj->bic,
|
|
'amount' => $amount,
|
|
'type' => 'RCUR',
|
|
'collection_date' => date(
|
|
'Y-m-d',
|
|
$mod_obj->getWorkingDays(
|
|
time(),
|
|
($mod_obj->conf_keys['delay_working']?$mod_obj->conf_keys['delay_working']:2)
|
|
)
|
|
),
|
|
'mandate_id' => $order->reference,
|
|
'mandate_date' => date('Y-m-d'),
|
|
'description' => str_replace(
|
|
'[reference]',
|
|
$order->reference,
|
|
$mod_obj->conf_keys['label']
|
|
)
|
|
);
|
|
// var_dump($payment);
|
|
$sepadd->addPayment($payment);
|
|
if ($order_state && Validate::isLoadedObject($order_state)) {
|
|
$current_order_state = $order->getCurrentOrderState();
|
|
if ($current_order_state->id == $order_state->id) {
|
|
$errors[] = $mod_obj->displayWarning(
|
|
sprintf('Order #%d has already been assigned this status.', $id_order)
|
|
);
|
|
} else {
|
|
$history = new OrderHistory();
|
|
$history->id_order = $order->id;
|
|
$history->id_employee = (int)ContextCore::getContext()->employee->id;
|
|
|
|
$use_existings_payment = !$order->hasInvoice();
|
|
$history->changeIdOrderState((int)$order_state->id, $order, $use_existings_payment);
|
|
|
|
$carrier = new Carrier($order->id_carrier, $order->id_lang);
|
|
$templateVars = array();
|
|
if ($history->id_order_state == Configuration::get('PS_OS_SHIPPING')
|
|
&& $order->shipping_number
|
|
) {
|
|
$templateVars = array(
|
|
'{followup}' => str_replace('@', $order->shipping_number, $carrier->url)
|
|
);
|
|
}
|
|
|
|
if ($history->addWithemail(true, $templateVars)) {
|
|
if (Configuration::get('PS_ADVANCED_STOCK_MANAGEMENT')) {
|
|
foreach ($order->getProducts() as $product) {
|
|
if (StockAvailable::dependsOnStock($product['product_id'])) {
|
|
StockAvailable::synchronize(
|
|
$product['product_id'],
|
|
(int)$product['id_shop']
|
|
);
|
|
}
|
|
}
|
|
}
|
|
} else {
|
|
$errors[] = sprintf(
|
|
Tools::displayError('Cannot change status for order #%d.'),
|
|
$id_order
|
|
);
|
|
}
|
|
}
|
|
}
|
|
} catch (Exception $e) {
|
|
$errors[] = 'Order '.$order->reference.' : '.$e->getMessage();
|
|
}
|
|
}
|
|
}
|
|
|
|
if (count($errors) == 0) {
|
|
$filename = 'sepa_file_'.date('Y-m-d-His').'.xml';
|
|
$result_sepa = $sepadd->save();
|
|
if (!file_exists(_PS_IMG_DIR_.'sepa/')) {
|
|
mkdir(_PS_IMG_DIR_.'sepa', 0777, true);
|
|
}
|
|
|
|
file_put_contents(_PS_IMG_DIR_.'sepa/'.$filename, $result_sepa);
|
|
$attach = array('content' => $result_sepa, 'mime' => 'application/xml', 'name' => $filename);
|
|
|
|
// envoie par mail et FTP si définie.
|
|
if ((int)$mod_obj->conf_keys['sendsepaemail'] && !empty($mod_obj->conf_keys['sepaemail'])) {
|
|
MailCore::Send(
|
|
(int)ContextCore::getContext()->language->id,
|
|
'send_sepa_file',
|
|
MailCore::l('New sepa file'),
|
|
array(),
|
|
$mod_obj->conf_keys['sepaemail'],
|
|
null,
|
|
null,
|
|
$attach,
|
|
null,
|
|
null,
|
|
dirname(__FILE__).'/mails/'
|
|
);
|
|
}
|
|
|
|
// envoie du fichier SEPA sur le FTP
|
|
if ((int)$mod_obj->conf_keys['sendsepaftp']) {
|
|
$server = $mod_obj->conf_keys['ftp_host'];
|
|
$user = $mod_obj->conf_keys['ftp_login'];
|
|
$pwd = $mod_obj->conf_keys['ftp_password'];
|
|
$port = $mod_obj->conf_keys['ftp_port'];
|
|
$sslcon = $mod_obj->conf_keys['ftp_sslcon'];
|
|
$path = $mod_obj->conf_keys['ftp_dirpath'];
|
|
if (!function_exists('ftp_ssl_connect')) {
|
|
$sslcon = 0;
|
|
}
|
|
if ($sslcon) {
|
|
if (!$conn_id = @ftp_ssl_connect($server, $port)) {
|
|
die($mod_obj->l('Failed to connect to FTP '));
|
|
}
|
|
} elseif (!$conn_id = ftp_connect($server, $port)) {
|
|
die($mod_obj->l('Failed to connect to FTP '));
|
|
}
|
|
|
|
// Identification avec un nom d'utilisateur et un mot de passe
|
|
if (!ftp_login($conn_id, $user, $pwd)) {
|
|
die($mod_obj->l('Failed to log in to FTP '));
|
|
}
|
|
|
|
ftp_pasv($conn_id, true);
|
|
|
|
if (!ftp_put($conn_id, $path.$filename, _PS_IMG_DIR_.'sepa/'.$filename, FTP_ASCII)) {
|
|
die($mod_obj->l("An error occured while loading file $filename\n"));
|
|
}
|
|
|
|
// Fermeture de la connexion
|
|
ftp_close($conn_id);
|
|
// die(var_dump($result_sepa));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|