|
@@ -12,8 +12,9 @@ public function execute_osync($otype, $oid)
|
|
|
|
|
|
public function execute_create($otype, $oid)
|
|
|
{
|
|
|
+ // Si produit maître avec déclinaisons, on synchronise toutes les déclinaisons
|
|
|
if ($otype=='product' && $this->check_combi($oid)) {
|
|
|
- return false;
|
|
|
+ $this->pd_osync_decl($oid);
|
|
|
}
|
|
|
|
|
|
return $this->pd_create($otype, $oid);
|
|
@@ -41,9 +42,6 @@ public function pd_insert_more(&$o, &$d_data_map, &$p_data)
|
|
|
// Un seul cas de $otype/$d_tref dans Doli
|
|
|
$id = $o['d_oid'];
|
|
|
|
|
|
- // product_extrafields
|
|
|
- static::d_insert_row('product_extrafields', ['fk_object' => $id], $d_data_map);
|
|
|
-
|
|
|
//var_dump($p_data);
|
|
|
|
|
|
// stock_available
|
|
@@ -65,7 +63,32 @@ public function pd_update_more(&$o, &$d_data_map, &$d_data_map_create, &$d_data,
|
|
|
$id = $o['d_oid'];
|
|
|
$p_id = $p_data['product']['id_product'];
|
|
|
$pa_id = (!empty($p_data['product_attribute']) ?$p_data['product_attribute']['id_product_attribute'] :0);
|
|
|
+
|
|
|
+ // Set Prestashop reference from Dolibarr
|
|
|
+ if ($pa_id && (empty($p_data['product_attribute']['reference']) || $p_data['product_attribute']['reference']=='NEW')) {
|
|
|
+ // Si réf déjà exisatante on récup
|
|
|
+ if (!empty($d_data['product']['ref']))
|
|
|
+ $ref = $d_data['product']['ref'];
|
|
|
+ else
|
|
|
+ $ref = $d_data_map_create['product']['ref'];
|
|
|
+ $sql = 'UPDATE ps_product_attribute
|
|
|
+ SET reference="'.$ref.'"
|
|
|
+ WHERE id_product_attribute='.$pa_id;
|
|
|
+ DB::p_update($sql);
|
|
|
+ }
|
|
|
+ elseif (empty($pa_id) && (empty($p_data['product']['reference']) || $p_data['product']['reference']=='NEW')) {
|
|
|
+ // Si réf déjà exisatante on récup
|
|
|
+ if (!empty($d_data['product']['ref']))
|
|
|
+ $ref = $d_data['product']['ref'];
|
|
|
+ else
|
|
|
+ $ref = $d_data_map_create['product']['ref'];
|
|
|
+ $sql = 'UPDATE ps_product
|
|
|
+ SET reference="'.$ref.'"
|
|
|
+ WHERE id_product='.$p_id;
|
|
|
+ DB::p_update($sql);
|
|
|
+ }
|
|
|
|
|
|
+ // Adds row in product_price if updated
|
|
|
if(isset($d_data['product']) && (
|
|
|
($d_data['product']['price'] != $d_data_map['product']['price'])
|
|
|
|| ($d_data['product']['tva_tx'] != $d_data_map['product']['tva_tx'])
|
|
@@ -155,13 +178,45 @@ public function pd_update_more(&$o, &$d_data_map, &$d_data_map_create, &$d_data,
|
|
|
// product_supplier
|
|
|
if (!empty($p_data['product_supplier'])) foreach($p_data['product_supplier'] as $row)
|
|
|
static::_action('supplier_price', 'pd', 'osync', 'product_supplier', $row['id_product_supplier']);
|
|
|
+
|
|
|
+ // Déclinaisons
|
|
|
+ if (empty($pa_id))
|
|
|
+ $this->pd_osync_decl($p_id);
|
|
|
}
|
|
|
|
|
|
-public function pd_image_sync($o, $p_data)
|
|
|
+public function pd_osync_decl($oid)
|
|
|
+{
|
|
|
+ $ok = false;
|
|
|
+ $sql = 'SELECT p.id_product_attribute
|
|
|
+ FROM ps_product_attribute p
|
|
|
+ WHERE p.id_product='.$oid;
|
|
|
+ $q = DB::p_select($sql);
|
|
|
+ $ok = false;
|
|
|
+ while(list($pa_id)=$q->fetch_row()) {
|
|
|
+ if (static::_action('product', 'pd', 'osync', 'product_attribute', $pa_id))
|
|
|
+ $ok = true;
|
|
|
+ }
|
|
|
+ if ($ok)
|
|
|
+ static::_notif_ts('product', 'product', $oid);
|
|
|
+ return;
|
|
|
+}
|
|
|
+
|
|
|
+public function pd_image_sync(&$o, &$p_data)
|
|
|
{
|
|
|
$p_id = $p_data['product']['id_product'];
|
|
|
$pa_id = (!empty($p_data['product_attribute']) ?$p_data['product_attribute']['id_product_attribute'] :0);
|
|
|
|
|
|
+ if ($pa_id) {
|
|
|
+ $sql = 'SELECT id_image
|
|
|
+ FROM ps_product_attribute_image
|
|
|
+ WHERE id_product_attribute='.$pa_id;
|
|
|
+ $q = DB::p_select($sql);
|
|
|
+ while($row=$q->fetch_assoc()) {
|
|
|
+ if ($this->image_sync($p_data, $row['id_image']))
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
// Image
|
|
|
$sql = 'SELECT id_image
|
|
|
FROM ps_image
|
|
@@ -170,31 +225,50 @@ public function pd_image_sync($o, $p_data)
|
|
|
LIMIT 1';
|
|
|
$q = DB::p_select($sql);
|
|
|
|
|
|
- foreach($q as $row) {
|
|
|
- $i_id = $row['id_image'];
|
|
|
- $p_filename = $i_id.'.jpg';
|
|
|
- $p_folder = P_FOLDER_ROOT.'/img/p';
|
|
|
- $n = strlen($i_id);
|
|
|
- for($i=0; $i<$n; $i++)
|
|
|
- $p_folder .= '/'.substr($i_id, $i, 1);
|
|
|
- var_dump($p_f=$p_folder.'/'.$p_filename);
|
|
|
- if (file_exists($p_f=$p_folder.'/'.$p_filename)) {
|
|
|
- //var_dump($p_f);
|
|
|
- if (!empty($p_data['product_attribute']))
|
|
|
- $ref = $p_data['product_attribute']['reference'];
|
|
|
- else
|
|
|
- $ref = $p_data['product']['reference'];
|
|
|
- if (empty($ref))
|
|
|
- break;
|
|
|
- $d_filename = $ref.'-'.$i_id.'.jpg';
|
|
|
- $d_folder = D_FOLDER_ROOT.'/documents/produit/'.$ref;
|
|
|
- if (!file_exists($d_folder))
|
|
|
- mkdir($d_folder);
|
|
|
- $d_f = $d_folder.'/'.$d_filename;
|
|
|
- var_dump($d_f);
|
|
|
- exec("rsync -a \"$p_f\" \"$d_f\"");
|
|
|
+ while($row=$q->fetch_assoc()) {
|
|
|
+ $this->image_sync($p_data, $row['id_image']);
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+public function image_sync(&$p_data, $i_id)
|
|
|
+{
|
|
|
+ $p_filename = $i_id.'.jpg';
|
|
|
+ $p_folder = P_FOLDER_ROOT.'/img/p';
|
|
|
+
|
|
|
+ $n = strlen($i_id);
|
|
|
+ for($i=0; $i<$n; $i++)
|
|
|
+ $p_folder .= '/'.substr($i_id, $i, 1);
|
|
|
+
|
|
|
+ var_dump($p_f=$p_folder.'/'.$p_filename);
|
|
|
+
|
|
|
+ if (file_exists($p_f=$p_folder.'/'.$p_filename)) {
|
|
|
+ //var_dump($p_f);
|
|
|
+ if (!empty($p_data['product_attribute']))
|
|
|
+ $ref = $p_data['product_attribute']['reference'];
|
|
|
+ else
|
|
|
+ $ref = $p_data['product']['reference'];
|
|
|
+ if (empty($ref))
|
|
|
+ return;
|
|
|
+
|
|
|
+ $d_filename = $ref.'-'.$i_id.'.jpg';
|
|
|
+ $d_folder = D_FOLDER_ROOT.'/documents/produit/'.$ref;
|
|
|
+ if (!file_exists($d_folder))
|
|
|
+ mkdir($d_folder);
|
|
|
+ $d_f = $d_folder.'/'.$d_filename;
|
|
|
+ var_dump($d_f);
|
|
|
+ exec("rsync -a \"$p_f\" \"$d_f\"");
|
|
|
+
|
|
|
+ // Suppression ancienne image
|
|
|
+ $fp = opendir($d_folder);
|
|
|
+ while($filename=readdir($fp)) {
|
|
|
+ if (!in_array($filename, ['.', '..', $d_filename])) {
|
|
|
+ var_dump('DELETE : '.$filename);
|
|
|
+ unlink($d_folder.'/'.$filename);
|
|
|
+ }
|
|
|
}
|
|
|
+ return true;
|
|
|
}
|
|
|
+
|
|
|
}
|
|
|
|
|
|
// -- MAPPING --
|
|
@@ -214,9 +288,16 @@ public function pd_map_update_simple(&$o, &$p_data, &$d_data=[])
|
|
|
$tva_tx = static::_pd_map_tva_taux($p_data);
|
|
|
$tms = max($p_data['product']['date_upd'], (isset($d_data['product']['tms']) ?$d_data['product']['tms'] :0));
|
|
|
|
|
|
+ // product_supplier
|
|
|
+ if (!empty($p_data['product_supplier'])) foreach($p_data['product_supplier'] as $row) {
|
|
|
+ if ($p_data['product']['id_supplier']==$row['id_supplier'])
|
|
|
+ $supplier_ref = $row['product_supplier_reference'];
|
|
|
+ }
|
|
|
+
|
|
|
$d_data_map = [
|
|
|
'product' => [
|
|
|
- 'ref' => $p_data['product']['reference'],
|
|
|
+ 'label' => $p_data['product_lang']['name'],
|
|
|
+ 'description' => str_replace(['<p>', '</p>', '<br />'], ['', "\r\n", "\r\n"], strip_tags($p_data['product_lang']['description_short'], '<p><br>').(isset($p_data['garantie']) ?"GARANTIE : ".strip_tags($p_data['garantie']['content'], '<p><br>') :'')),
|
|
|
'tms' => $tms,
|
|
|
'cost_price' => $p_data['product']['wholesale_price'],
|
|
|
'price' => $p_data['product']['price'],
|
|
@@ -231,10 +312,24 @@ public function pd_map_update_simple(&$o, &$p_data, &$d_data=[])
|
|
|
'p_active' => $p_data['product']['active'],
|
|
|
'p_available_for_order' => $p_data['product']['available_for_order'],
|
|
|
'p_online_only' => $p_data['product']['online_only'],
|
|
|
- 'garantie' => (isset($p_data['garantie']) ?$p_data['garantie']['content'] :''),
|
|
|
+ 'garantie' => (isset($p_data['garantie']) ?substr(strip_tags($p_data['garantie']['content']), 0, 255) :''),
|
|
|
+ 'fk_soc_fournisseur' => static::_pd_map_fk('supplier', 'supplier', $p_data['product']['id_supplier']),
|
|
|
+ 'supplier_ref' => (!empty($supplier_ref) ?$supplier_ref :NULL),
|
|
|
],
|
|
|
];
|
|
|
|
|
|
+ // Référence non nulle !
|
|
|
+ if (!empty($p_data['product']['reference']) && $p_data['product']['reference']!='NEW')
|
|
|
+ $d_data_map['product']['ref'] = $p_data['product']['reference'];
|
|
|
+
|
|
|
+ if (PUBLIC_PRICE) {
|
|
|
+ $d_data_map['product_extrafields']['public_price'] = (!empty($p_data['product']['recommended_public_price']) ?round($p_data['product']['recommended_public_price'], 2) :NULL);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (ECOTAX) {
|
|
|
+ $d_data_map['product_extrafields']['ecotaxdeee'] = (!empty($p_data['product']['ecotax']) ?$p_data['product']['ecotax'] :NULL);
|
|
|
+ }
|
|
|
+
|
|
|
// Pack
|
|
|
$d_data_map['product_association'] = [];
|
|
|
if (isset($p_data['pack'])) {
|
|
@@ -257,7 +352,7 @@ public function pd_map_update_simple(&$o, &$p_data, &$d_data=[])
|
|
|
return $d_data_map;
|
|
|
}
|
|
|
public function pd_map_update_combi(&$o, &$p_data, &$d_data=[])
|
|
|
-{
|
|
|
+{
|
|
|
$tva_tx = static::_pd_map_tva_taux($p_data);
|
|
|
$tms = max($p_data['product_attribute']['date_upd'], $p_data['product']['date_upd'], (isset($d_data['product']['tms']) ?$d_data['product']['tms'] :0));
|
|
|
|
|
@@ -267,10 +362,16 @@ public function pd_map_update_combi(&$o, &$p_data, &$d_data=[])
|
|
|
$attributes[] = static::$_p_attribute[$row['id_attribute']];
|
|
|
$attribute_names = implode(', ', $attributes);
|
|
|
|
|
|
+ // product_supplier
|
|
|
+ if (!empty($p_data['product_supplier'])) foreach($p_data['product_supplier'] as $row) {
|
|
|
+ if ($p_data['product']['id_supplier']==$row['id_supplier'])
|
|
|
+ $supplier_ref = $row['product_supplier_reference'];
|
|
|
+ }
|
|
|
+
|
|
|
$d_data_map = [
|
|
|
'product' => [
|
|
|
- 'ref' => $p_data['product_attribute']['reference'],
|
|
|
'label' => $p_data['product_lang']['name'].' - '.$attribute_names,
|
|
|
+ 'description' => str_replace(['<p>', '</p>', '<br />'], ['', "\r\n", "\r\n"], strip_tags($p_data['product_lang']['description_short'], '<p><br>').(isset($p_data['garantie']) ?"GARANTIE : ".strip_tags($p_data['garantie']['content'], '<p><br>') :'')),
|
|
|
'tms' => $tms,
|
|
|
'cost_price' => $p_data['product_attribute']['wholesale_price'],
|
|
|
'price' => ($p_data['product']['price']+$p_data['product_attribute']['price']),
|
|
@@ -285,9 +386,23 @@ public function pd_map_update_combi(&$o, &$p_data, &$d_data=[])
|
|
|
'p_active' => $p_data['product']['active'],
|
|
|
'p_available_for_order' => $p_data['product']['available_for_order'],
|
|
|
'p_online_only' => $p_data['product']['online_only'],
|
|
|
- 'garantie' => (isset($p_data['garantie']) ?$p_data['garantie']['content'] :''),
|
|
|
+ 'garantie' => (isset($p_data['garantie']) ?substr(strip_tags($p_data['garantie']['content']), 0, 255) :''),
|
|
|
+ 'fk_soc_fournisseur' => static::_pd_map_fk('supplier', 'supplier', $p_data['product']['id_supplier']),
|
|
|
+ 'supplier_ref' => (!empty($supplier_ref) ?$supplier_ref :NULL),
|
|
|
],
|
|
|
];
|
|
|
+
|
|
|
+ // Référence non nulle !
|
|
|
+ if (!empty($p_data['product_attribute']['reference']) && $p_data['product_attribute']['reference']!='NEW')
|
|
|
+ $d_data_map['product']['ref'] = $p_data['product_attribute']['reference'];
|
|
|
+
|
|
|
+ if (PUBLIC_PRICE) {
|
|
|
+ $d_data_map['product_extrafields']['public_price'] = (!empty($p_data['product_attribute']['recommended_public_price']) ?round($p_data['product_attribute']['recommended_public_price'], 2) :NULL);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (ECOTAX) {
|
|
|
+ $d_data_map['product_extrafields']['ecotaxdeee'] = (!empty($p_data['product_attribute']['ecotax']) ?$p_data['product_attribute']['ecotax'] :NULL);
|
|
|
+ }
|
|
|
|
|
|
return $d_data_map;
|
|
|
}
|
|
@@ -306,13 +421,21 @@ public function pd_map_create_common(&$o, &$p_data)
|
|
|
}
|
|
|
public function pd_map_create_simple(&$o, &$p_data)
|
|
|
{
|
|
|
+ // Le produit doit être enregistré proprement dans Prestashop
|
|
|
+ if (empty($p_data['product']['state']))
|
|
|
+ return;
|
|
|
+
|
|
|
+ // On doit spécifier qu'on veut créer la réf
|
|
|
if (empty($p_data['product']['reference']))
|
|
|
return;
|
|
|
|
|
|
+ $ref = ($p_data['product']['reference']!='NEW' ?$p_data['product']['reference'] :static::d_ref());
|
|
|
+ //var_dump($ref); die();
|
|
|
+
|
|
|
$d_data_map = [
|
|
|
'product' => [
|
|
|
//'rowid' => '',
|
|
|
- 'ref' => $p_data['product']['reference'],
|
|
|
+ 'ref' => $ref,
|
|
|
'entity' => 1,
|
|
|
'ref_ext' => NULL,
|
|
|
'datec' => $p_data['product']['date_add'],
|
|
@@ -404,15 +527,20 @@ public function pd_map_create_simple(&$o, &$p_data)
|
|
|
|
|
|
public function pd_map_create_combi(&$o, &$p_data)
|
|
|
{
|
|
|
- if (empty($p_data['product_attribute']['reference'])) {
|
|
|
- sync_error("ERROR ".__METHOD__."($o[p_tref], $o[p_oid]) : missing reference");
|
|
|
+ // Le produit doit être enregistré proprement dans Prestashop
|
|
|
+ if (empty($p_data['product']['state']))
|
|
|
return;
|
|
|
- }
|
|
|
-
|
|
|
+
|
|
|
+ // On doit spécifier qu'on veut créer la réf
|
|
|
+ if (empty($p_data['product_attribute']['reference']))
|
|
|
+ return;
|
|
|
+
|
|
|
+ $ref = ($p_data['product_attribute']['reference']!='NEW' ?$p_data['product_attribute']['reference'] :static::d_ref());
|
|
|
+
|
|
|
$d_data_map = [
|
|
|
'product' => [
|
|
|
//'rowid' => '',
|
|
|
- 'ref' => $p_data['product_attribute']['reference'],
|
|
|
+ 'ref' => $ref,
|
|
|
'entity' => 1,
|
|
|
'ref_ext' => NULL,
|
|
|
'datec' => max($p_data['product_attribute']['date_upd'], $p_data['product']['date_add']),
|