338 lines
11 KiB
PHP
338 lines
11 KiB
PHP
<?php
|
|
/*
|
|
* 2007-2015 PrestaShop
|
|
*
|
|
* 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@prestashop.com so we can send you a copy immediately.
|
|
*
|
|
* DISCLAIMER
|
|
*
|
|
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
|
|
* versions in the future. If you wish to customize PrestaShop for your
|
|
* needs please refer to http://www.prestashop.com for more information.
|
|
*
|
|
* @author PrestaShop SA <contact@prestashop.com>
|
|
* @copyright 2007-2015 PrestaShop SA
|
|
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
|
|
* International Registered Trademark & Property of PrestaShop SA
|
|
*/
|
|
|
|
if (!defined('_PS_VERSION_'))
|
|
exit;
|
|
|
|
if (defined('ERP_ENABLED') && ERP_ENABLED) {
|
|
require 'newOrder.php';
|
|
require_once _PS_ROOT_DIR_.'/erp/ripcord/ripcord.php';
|
|
require_once _PS_ROOT_DIR_.'/erp/ErpTools.php';
|
|
}
|
|
elseif (defined('ERP_MOCK_ENABLED') && ERP_MOCK_ENABLED) {
|
|
require_once __DIR__.'/Mocks/newOrder.php';
|
|
require_once __DIR__.'/Mocks/MockRipcord.php';
|
|
require_once __DIR__.'/Mocks/ErpTools.php';
|
|
}
|
|
|
|
class Erporder extends Module
|
|
{
|
|
|
|
public function __construct()
|
|
{
|
|
$this->name = 'erporder';
|
|
$this->tab = 'administration';
|
|
$this->version = '1.0.0';
|
|
$this->author = 'Antadis';
|
|
|
|
parent::__construct();
|
|
$this->displayName = $this->l('Erp order');
|
|
$this->description = $this->l('Sync order in Odoo');
|
|
|
|
if (defined('ERP_ENABLED') && ERP_ENABLED) {
|
|
$this->common = ripcord::client(ERP_URL.'/xmlrpc/2/common');
|
|
$this->common->version();
|
|
$this->uid = $this->common->authenticate(ERP_DB, ERP_USERNAME, ERP_PASSWORD, array());
|
|
|
|
$this->model = ripcord::client(ERP_URL.'/xmlrpc/2/object');
|
|
$this->report = ripcord::client(ERP_URL.'/xmlrpc/2/report');
|
|
}
|
|
else if (defined('ERP_MOCK_ENABLED') && ERP_MOCK_ENABLED) {
|
|
$this->uid = '0';
|
|
$this->model = MockRipcord::client('');
|
|
}
|
|
}
|
|
|
|
public function install()
|
|
{
|
|
return (parent::install()
|
|
&& $this->registerHook('actionOrderStatusPostUpdate')
|
|
&& $this->registerHook('actionPDFInvoiceRender')
|
|
);
|
|
}
|
|
|
|
public function hookActionOrderStatusPostUpdate($params)
|
|
{
|
|
if (!defined('ERP_ENABLED') || !defined('ERP_PRODUCT_PROMO_ID')) {
|
|
return;
|
|
}
|
|
|
|
if (!ERP_ENABLED && (!defined('ERP_MOCK_ENABLED') || !ERP_MOCK_ENABLED)) {
|
|
return;
|
|
}
|
|
|
|
$order = new Order($params['id_order']);
|
|
|
|
// if ($_SERVER['REMOTE_ADDR'] != '88.163.22.99') {
|
|
$orderStatut = $params['newOrderStatus']->id;
|
|
$statut_available = array(1,2,10);
|
|
if (!in_array($orderStatut, $statut_available)) {
|
|
return false;
|
|
}
|
|
|
|
// if ($order->current_state == 9
|
|
// || $order->current_state == 13
|
|
// || $order->current_state == 6
|
|
// || $order->current_state == 8
|
|
// || $order->current_state == 7) {
|
|
// return false;
|
|
// }
|
|
|
|
$result = Db::getInstance()->getValue('SELECT `id_erp` FROM `ps_orders_erp` WHERE `id_order` = '.(int)$order->id);
|
|
if ($result !== false ) {
|
|
return false;
|
|
}
|
|
//}
|
|
|
|
$cart = new Cart($order->id_cart);
|
|
$newOrder = new NewOrder($this->model, $this->uid);
|
|
$customer = new Customer($order->id_customer);
|
|
$carrier = new Carrier($order->id_carrier);
|
|
$isCB = ($orderStatut == 2);
|
|
|
|
$assoc_carrier = array(
|
|
1 => 1,
|
|
2 => 2,
|
|
);
|
|
$assoc_payment_term = array(
|
|
'Paiement cash par carte de paiement' => 4,
|
|
'CB/Visa/MasterCard' => 4,
|
|
'Chèque' => 5,
|
|
'Virement bancaire' => 6,
|
|
'Be2bill paiement dynamique par internet' => 4
|
|
);
|
|
|
|
$erp_product_promo_id = (int)ERP_PRODUCT_PROMO_ID;
|
|
|
|
$currency_ids = $newOrder->search('res.currency', array(array('name', '=', 'EUR')));
|
|
$currency_id = $newOrder->read('res.currency', $currency_ids, 'id');
|
|
|
|
//$carrier_ids = $newOrder->search('account.payment.term', array(array('active', '=', true))); d($newOrder->read('account.payment.term', $carrier_ids)); die;
|
|
|
|
$note = $order->getFirstMessage();
|
|
|
|
$invoice_adress = new Address($order->id_address_invoice);
|
|
|
|
if ($invoice_adress->id_country != 8) {
|
|
$zone_id = Address::getZoneById($order->id_address_invoice);
|
|
if ($zone_id == Zone::EUROPE) {
|
|
$newOrder->assignPositionFiscal(1);
|
|
} else {
|
|
$newOrder->assignPositionFiscal(2);
|
|
}
|
|
}
|
|
$newOrder->assignCurrency($currency_id);
|
|
$newOrder->assignDate($order->date_add);
|
|
$newOrder->assignReference($order->reference);
|
|
$newOrder->assignCustomer($customer);
|
|
$newOrder->assignInvoiceCustomer($invoice_adress);
|
|
$newOrder->assignShippingCustomer(new Address($order->id_address_delivery));
|
|
$newOrder->assignCarrier((int)$assoc_carrier[$carrier->id_reference]);
|
|
$newOrder->assignState(NewOrder::ORDER_DRAFT);
|
|
$newOrder->assignPayment($assoc_payment_term[$order->payment]);
|
|
$newOrder->assignWaitValidation($isCB);
|
|
if (!empty($note)) {
|
|
$newOrder->assignNote($note);
|
|
}
|
|
|
|
$order_id = $newOrder->create('sale.order', $newOrder->order);
|
|
|
|
if (!empty($order_id['faultCode'])) {
|
|
$a = array_merge($order_id, $newOrder->order);
|
|
ErpTools::logError($a, 'insert into sale.order');
|
|
}
|
|
|
|
// if ($_SERVER['REMOTE_ADDR'] != '88.163.22.99') {
|
|
// maj ID ERP de la commande
|
|
$change = Db::getInstance()->execute('
|
|
INSERT INTO `'._DB_PREFIX_.'orders_erp`
|
|
VALUES (
|
|
'.$order->id.',
|
|
'.(int) $order_id.'
|
|
)'
|
|
);
|
|
// }
|
|
|
|
foreach ($order->getProducts() as $key => $p) {
|
|
$product = new Product($p['product_id']);
|
|
$attr = $product->getAttributeCombinationsById($p['product_attribute_id'], (int)Context::getContext()->language->id)[0];
|
|
|
|
$res = $newOrder->search('product.product', array(array('id', '=', $attr['id_erp'])));
|
|
$tax_id = $newOrder->read('product.product', $res, 'taxes_id');
|
|
|
|
$newOrder->assignLineProduct($key, $attr['id_erp']);
|
|
$newOrder->assignLineName($key, $p['product_name']);
|
|
$newOrder->assignLineOrder($key, $order_id);
|
|
$newOrder->assignLineUnitPrice($key, $p['unit_price_tax_excl'] / $p['nb_per_box']);
|
|
$newOrder->assignLineQuantity($key, $p['product_quantity'] * $p['nb_per_box']);
|
|
//$newOrder->assignLineTaxes($key, array(array(4, $tax_id[0], false)));
|
|
if ($invoice_adress->id_country == 8) {
|
|
$newOrder->assignLineTaxes($key, array(array(4, $tax_id[0], false)));
|
|
} else {
|
|
$newOrder->assignLineTaxes($key, array(array(6, false, array())));
|
|
}
|
|
|
|
$line_id = $newOrder->create('sale.order.line', $newOrder->lines[$key]);
|
|
if (!empty($line_id['faultCode'])) {
|
|
$a = array_merge($line_id, $newOrder->order);
|
|
ErpTools::logError($a, 'insert into sale.order.line');
|
|
}
|
|
}
|
|
|
|
$discounts = $order->getDiscounts();
|
|
foreach($discounts as $discount) {
|
|
$res = $newOrder->search('product.product', array(array('id', '=', $erp_product_promo_id)));
|
|
$tax_id = $newOrder->read('product.product', $res, 'taxes_id');
|
|
|
|
$linesLength = count($newOrder->lines);
|
|
$newOrder->assignLineOrder($linesLength, $order_id);
|
|
$newOrder->assignLineProduct($linesLength, $erp_product_promo_id);
|
|
$newOrder->assignLineName($linesLength, $discount['name']);
|
|
$newOrder->assignLineUnitPrice($linesLength, $discount['value_tax_excl'] * -1);
|
|
$newOrder->assignLineQuantity($linesLength, 1);
|
|
|
|
if ($invoice_adress->id_country == 8) {
|
|
$newOrder->assignLineTaxes($linesLength, array(array(4, 2, false)));
|
|
} else {
|
|
$newOrder->assignLineTaxes($key, array(array(6, false, array())));
|
|
}
|
|
|
|
$line_id = $newOrder->create('sale.order.line', $newOrder->lines[$linesLength]);
|
|
if (!empty($line_id['faultCode'])) {
|
|
$a = array_merge($line_id, $newOrder->order);
|
|
ErpTools::logError($a, 'insert into sale.order.line');
|
|
}
|
|
|
|
}
|
|
|
|
$c = new Carrier($order->id_carrier);
|
|
|
|
if ($c->id_reference == 2) {
|
|
$linesLength = count($newOrder->lines);
|
|
$shipping_ids = $newOrder->search('product.product', array(array('name', '=', 'Shipping')));
|
|
$shipping_id = $newOrder->read('product.product', $shipping_ids, 'id');
|
|
|
|
$newOrder->assignLineOrder($linesLength, $order_id);
|
|
$newOrder->assignLineProduct($linesLength, $shipping_id);
|
|
$newOrder->assignLineName($linesLength, trim($carrier->name));
|
|
$newOrder->assignLineQuantity($linesLength, 1);
|
|
$newOrder->assignLineUnitPrice($linesLength, $order->total_shipping_tax_excl);
|
|
|
|
if ($invoice_adress->id_country == 8) {
|
|
$newOrder->assignLineTaxes($linesLength, array(array(4, 2, false)));
|
|
} else {
|
|
$newOrder->assignLineTaxes($linesLength, array(array(6, false, array())));
|
|
}
|
|
|
|
$line_id = $newOrder->create('sale.order.line', $newOrder->lines[$linesLength]);
|
|
if (!empty($line_id['faultCode'])) {
|
|
$a = array_merge($line_id, $newOrder->order);
|
|
ErpTools::logError($a, 'insert into sale.order.line');
|
|
}
|
|
}
|
|
|
|
if (!ERP_ENABLED && defined('ERP_MOCK_ENABLED') && ERP_MOCK_ENABLED) {
|
|
if ($order_id) {
|
|
$res = $newOrder->exec_workflow((int)$order_id);
|
|
}
|
|
return;
|
|
}
|
|
|
|
if ($isCB && $order_id) {
|
|
|
|
$res = $newOrder->exec_workflow((int)$order_id);
|
|
/* if ($res) {
|
|
$history = new OrderHistory();
|
|
$history->id_order = $params['id_order'];
|
|
$history->id_employee = 0;
|
|
$history->id_order_state = 2;
|
|
$history->add();
|
|
$history->changeIdOrderState($history->id_order_state, $order);
|
|
$history->id_order_state = 3;
|
|
$history->add();
|
|
$history->changeIdOrderState($history->id_order_state, $order);
|
|
} */
|
|
}
|
|
|
|
|
|
// $history = new OrderHistory();
|
|
// $history->id_order = $order->id;
|
|
// $history->id_employee = 0;
|
|
// $history->id_order_state = 19;
|
|
// $history->add();
|
|
// $history->changeIdOrderState($history->id_order_state, $order);
|
|
|
|
}
|
|
|
|
public function hookActionPDFInvoiceRender($params)
|
|
{
|
|
if (!defined('ERP_ENABLED') || !ERP_ENABLED) {
|
|
return;
|
|
}
|
|
|
|
if (!Tools::getValue('id_order')) {
|
|
die(Tools::displayError('Order was not found.'));
|
|
}
|
|
|
|
$order = new Order(Tools::getValue('id_order'));
|
|
$newOrder = new NewOrder($this->model, $this->uid);
|
|
|
|
$id_erp = Db::getInstance()->getValue('SELECT `id_erp` FROM `'._DB_PREFIX_.'orders_erp` WHERE `id_order` = ' . (int) $order->id);
|
|
|
|
if (!empty(Context::getContext()->customer->id)) {
|
|
if (Context::getContext()->customer->id != $order->id_customer) {
|
|
die(Tools::displayError('The invoice was not found.'));
|
|
}
|
|
}
|
|
|
|
$order_erp = $newOrder->search('sale.order', array(
|
|
array('id', '=', $id_erp)
|
|
));
|
|
$invoice_ids = $newOrder->read('sale.order', $order_erp, 'invoice_ids');
|
|
|
|
$result = $this->report->render_report(ERP_DB, $this->uid, ERP_PASSWORD, 'account.report_invoice', $invoice_ids);
|
|
if (!empty($result['result'])) {
|
|
$report_data = base64_decode($result['result']);
|
|
|
|
$filename = $order->reference.'.pdf';
|
|
$file = fopen($filename, 'w+');
|
|
fwrite($file, $report_data);
|
|
fclose($file);
|
|
|
|
header('Content-type: application/pdf');
|
|
header('Content-Disposition: inline; filename='.basename($filename));
|
|
header('Content-Transfer-Encoding: binary');
|
|
header('Content-Length: '.filesize($filename));
|
|
|
|
readfile($filename);
|
|
unlink($filename);
|
|
} else {
|
|
die(Tools::displayError('The invoice was not found.'));
|
|
}
|
|
}
|
|
|
|
}
|