From 68f8e1eb51b4af253aca2cb15b7583fba6ac4ecd Mon Sep 17 00:00:00 2001 From: Rodney Figaro Date: Wed, 8 Feb 2017 17:31:41 +0100 Subject: [PATCH] add ERP mock objects for testing orders without real ERP --- .gitignore | 3 +- log/index.php | 35 ++++ modules/erporder/Mocks/ErpTools.php | 8 + modules/erporder/Mocks/MockRipcord.php | 14 ++ modules/erporder/Mocks/newOrder.php | 255 +++++++++++++++++++++++++ modules/erporder/erporder.php | 36 ++-- 6 files changed, 339 insertions(+), 12 deletions(-) create mode 100644 log/index.php create mode 100644 modules/erporder/Mocks/ErpTools.php create mode 100644 modules/erporder/Mocks/MockRipcord.php create mode 100644 modules/erporder/Mocks/newOrder.php diff --git a/.gitignore b/.gitignore index 6f7f264..df03bdb 100755 --- a/.gitignore +++ b/.gitignore @@ -6,6 +6,7 @@ *.tar.gz download/* log/* +!log/index.php modules/gamification/ upload/* *sitemap.xml @@ -86,4 +87,4 @@ modules/*/mails/* config.codekit *.sublime-project *.sublime-workspace -.zfs/ \ No newline at end of file +.zfs/ diff --git a/log/index.php b/log/index.php new file mode 100644 index 0000000..2d4b752 --- /dev/null +++ b/log/index.php @@ -0,0 +1,35 @@ + +* @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 +*/ + +header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); +header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT"); + +header("Cache-Control: no-store, no-cache, must-revalidate"); +header("Cache-Control: post-check=0, pre-check=0", false); +header("Pragma: no-cache"); + +header("Location: ../"); +exit; diff --git a/modules/erporder/Mocks/ErpTools.php b/modules/erporder/Mocks/ErpTools.php new file mode 100644 index 0000000..6555535 --- /dev/null +++ b/modules/erporder/Mocks/ErpTools.php @@ -0,0 +1,8 @@ +$param, 'text'=>$text), true), FILE_APPEND); + } +} \ No newline at end of file diff --git a/modules/erporder/Mocks/MockRipcord.php b/modules/erporder/Mocks/MockRipcord.php new file mode 100644 index 0000000..875d565 --- /dev/null +++ b/modules/erporder/Mocks/MockRipcord.php @@ -0,0 +1,14 @@ + \ No newline at end of file diff --git a/modules/erporder/Mocks/newOrder.php b/modules/erporder/Mocks/newOrder.php new file mode 100644 index 0000000..5d131e2 --- /dev/null +++ b/modules/erporder/Mocks/newOrder.php @@ -0,0 +1,255 @@ + 'manual', + 'picking_policy' => 'direct', + 'pricelist_id' => 1, // Euro + 'warehouse_id' => 7, // Roykin + 'origin' => 'Roykin Prestashop', + ); + + private $line = array( + 'delay' => 0, + 'product_uom' => 1, + 'state' => 'confirmed' + ); + + public $lines = array(); + + private $id_fake = 0; + private $data_collected = array(); + + public function __construct($model, $uid) + { + $this->model = $model; + $this->uid = $uid; + } + + public function create($table, $params) + { + $this->data_collected[] = array( + 'method' => 'create', + 'table' => $table, + 'params' => $params + ); + + $d = new DateTime(); + $id_fake = $d->format('His'); + return $id_fake; + } + + public function exec_workflow($order_id) + { + file_put_contents(_PS_ROOT_DIR_.'/log/erp_fake_order_id_'.$order_id , print_r( + array( + 'data_collected' => $this->data_collected, + 'order' => $this->order, + 'lines' => $this->lines + ), + true)); + return true; + } + + public function search($table, $cond) + { + $this->data_collected[] = array( + 'method' => 'search', + 'table' => $table, + 'params' => $cond + ); + return ''; + } + + public function read($table, $params, $key = false) + { + $this->data_collected[] = array( + 'method' => 'read', + 'table' => $table, + 'params' => $params, + 'key' => $key + ); + return array(rand(0, 100)); + } + + public function assignWaitValidation($bool) + { + $this->order['wait_validation'] = $bool; + } + + public function assignPositionFiscal($id) + { + $this->order['fiscal_position'] = $id; + } + + public function assignCurrency($id) + { + $this->order['currency_id'] = $id; + } + + public function assignDate($date) + { + $this->order['date_order'] = $date; + } + + public function assignReference($ref) + { + $this->order['name'] = $ref; + } + + public function assignCustomer(Customer $customer) + { + $this->order['partner_id'] = (int)$customer->id_erp; + } + + public function assignState($state) + { + $this->order['state'] = $state; + } + + public function assignPayment($payment) + { + $this->order['payment_term'] = $payment; + } + + public function assignInvoiceCustomer(Address $address) + { + $id = $this->getAddressInErp($address, NewOrder::TYPE_INVOICE); + $this->order['partner_invoice_id'] = (int)$id; + } + + public function assignShippingCustomer(Address $address) + { + $id = $this->getAddressInErp($address, NewOrder::TYPE_DELIVERY); + $this->order['partner_shipping_id'] = (int)$id; + } + + public function assignCarrier($id) + { + $this->order['carrier_id'] = $id; + } + + public function assignNote($note) + { + $this->order['note'] = $note; + } + + public function checkShippingId($id) + { + return ($this->order->id_carrier == $id); + } + + private function newLine($key) + { + $this->lines[$key] = $this->line; + } + + public function assignLineProduct($key, $value) + { + if (empty($this->lines[$key])) { + $this->newLine($key); + } + $this->lines[$key]['product_id'] = $value; + } + + public function assignLineName($key, $value) + { + if (empty($this->lines[$key])) { + $this->newLine($key); + } + $this->lines[$key]['name'] = $value; + } + + public function assignLineOrder($key, $value) + { + if (empty($this->lines[$key])) { + $this->newLine($key); + } + $this->lines[$key]['order_id'] = $value; + } + + public function assignLineUnitPrice($key, $value) + { + if (empty($this->lines[$key])) { + $this->newLine($key); + } + $this->lines[$key]['price_unit'] = $value; + } + + public function assignLineQuantity($key, $value) + { + if (empty($this->lines[$key])) { + $this->newLine($key); + } + $this->lines[$key]['product_uom_qty'] = $value; + } + + public function assignLineTaxes($key, $value) + { + if (empty($this->lines[$key])) { + $this->newLine($key); + } + $this->lines[$key]['tax_id'] = $value; + } + + public function assignLineProductTemplate($key, $value) + { + if (empty($this->lines[$key])) { + $this->newLine($key); + } + $this->lines[$key]['product_tmpl_id'] = $value; + } + + private function getAddressInErp(Address $address, $type) + { + return NewOrder::TYPE_INVOICE?1:2; + /* + // $handle = fopen(dirname(__FILE__).'/log.txt', 'a+'); + // fwrite($handle, 'heure : '.date('Y-m-d H:i:s').PHP_EOL); + // fwrite($handle, 'is_erp : '.$address->is_erp.PHP_EOL); + // fwrite($handle, 'id_erp : '.$address->id_erp.PHP_EOL); + // if ($address->is_erp == 0) { + // return $this->createAddressInErp($address, $type); + // } + + + $country = $this->search('res.country', array( + array('name', '=', $address->country) + )); + $country = $this->read('res.country', $country)[0]; + + $addr = array( + array('id', '=', $address->id_erp), + // array('street', '=', $address->address1), + // array('zip', '=', $address->postcode), + // array('country_id', '=', $country['id']), + // array('type', '=', $type) + ); + + $addrs = $this->search('res.partner', $addr); + // foreach ($addrs as $addr) { + // fwrite($handle, 'resultat recherche : '.$addr.PHP_EOL); + // } + if (count($addrs) == 0) { + return $this->createAddressInErp($address, $type); + } + + return $addrs[0]; + */ + } + + public static function logError($record, $type = 'unknown') + { + file_put_contents(_PS_ROOT_DIR_.'/log/error_erp_fake_order_id_'.$order_id , print_r(array('record'=>$record, 'type'=>$type), true)); + return true; + } +} diff --git a/modules/erporder/erporder.php b/modules/erporder/erporder.php index 35fbf04..1972d4d 100644 --- a/modules/erporder/erporder.php +++ b/modules/erporder/erporder.php @@ -27,9 +27,16 @@ if (!defined('_PS_VERSION_')) exit; -require 'newOrder.php'; -require_once _PS_ROOT_DIR_.'/erp/ripcord/ripcord.php'; -require_once _PS_ROOT_DIR_.'/erp/ErpTools.php'; +if (defined('TEST_DISABLE_ERP') && TEST_DISABLE_ERP) { + require_once __DIR__.'/Mocks/newOrder.php'; + require_once __DIR__.'/Mocks/MockRipcord.php'; + require_once __DIR__.'/Mocks/ErpTools.php'; +} +else { + require 'newOrder.php'; + require_once _PS_ROOT_DIR_.'/erp/ripcord/ripcord.php'; + require_once _PS_ROOT_DIR_.'/erp/ErpTools.php'; +} class Erporder extends Module { @@ -45,7 +52,11 @@ class Erporder extends Module $this->displayName = $this->l('Erp order'); $this->description = $this->l('Sync order in Odoo'); - if (defined('TEST_DISABLE_ERP') && !TEST_DISABLE_ERP) { + if (defined('TEST_DISABLE_ERP') && TEST_DISABLE_ERP) { + $this->uid = '0'; + $this->model = MockRipcord::client(''); + } + else { $this->common = ripcord::client(ERP_URL.'/xmlrpc/2/common'); $this->common->version(); $this->uid = $this->common->authenticate(ERP_DB, ERP_USERNAME, ERP_PASSWORD, array()); @@ -65,10 +76,6 @@ class Erporder extends Module public function hookActionOrderStatusPostUpdate($params) { - if (defined('TEST_DISABLE_ERP') && TEST_DISABLE_ERP) { - return; - } - $order = new Order($params['id_order']); // if ($_SERVER['REMOTE_ADDR'] != '88.163.22.99') { @@ -245,6 +252,13 @@ class Erporder extends Module } } +if (defined('TEST_DISABLE_ERP') && TEST_DISABLE_ERP) { + if ($order_id) { + $res = $newOrder->exec_workflow((int)$order_id); + } + return; +} + if ($isCB && $order_id) { $res = $newOrder->exec_workflow((int)$order_id); @@ -273,9 +287,9 @@ class Erporder extends Module public function hookActionPDFInvoiceRender($params) { - if (defined('TEST_DISABLE_ERP') && TEST_DISABLE_ERP) { - return; - } +if (defined('TEST_DISABLE_ERP') && TEST_DISABLE_ERP) { + return; +} if (!Tools::getValue('id_order')) { die(Tools::displayError('Order was not found.'));