roykin/modules/erporder/Mocks/newOrder.php

256 lines
5.3 KiB
PHP

<?php
class NewOrder
{
const TYPE_INVOICE = 'invoice';
const TYPE_DELIVERY = 'delivery';
const ORDER_DRAFT = 'draft';
const ORDER_SALE = 'progress';
private $model = false;
private $uid = false;
public $order = array(
'order_policy' => 'manual',
'picking_policy' => 'direct',
'pricelist_id' => 1, // Euro
'warehouse_id' => 7, // Roykin/Levest
'origin' => 'Levest 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;
}
}