From 1e3b8e56f226e5a8b0fae167738839ea337a5160 Mon Sep 17 00:00:00 2001
From: guerinaxel <guerinaxel57@gmail.com>
Date: Thu, 22 May 2014 12:10:40 +0200
Subject: [PATCH] Add extrafields to productorservice webservice

---
 .../webservices/server_productorservice.php   | 221 ++++++++++++------
 1 file changed, 144 insertions(+), 77 deletions(-)

diff --git a/htdocs/webservices/server_productorservice.php b/htdocs/webservices/server_productorservice.php
index c2eba3f9d1b..4d7f8867e65 100644
--- a/htdocs/webservices/server_productorservice.php
+++ b/htdocs/webservices/server_productorservice.php
@@ -34,6 +34,7 @@ require_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php';
 
 require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
 require_once(DOL_DOCUMENT_ROOT."/categories/class/categorie.class.php");
+require_once DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php';
 
 
 
@@ -88,6 +89,64 @@ $server->wsdl->addComplexType(
     )
 );
 
+$productorservice_fields = array(
+    'id' => array('name'=>'id','type'=>'xsd:string'),
+    'ref' => array('name'=>'ref','type'=>'xsd:string'),
+    'ref_ext' => array('name'=>'ref_ext','type'=>'xsd:string'),
+    'type' => array('name'=>'type','type'=>'xsd:string'),
+    'label' => array('name'=>'label','type'=>'xsd:string'),
+    'description' => array('name'=>'description','type'=>'xsd:string'),
+    'date_creation' => array('name'=>'date_creation','type'=>'xsd:dateTime'),
+    'date_modification' => array('name'=>'date_modification','type'=>'xsd:dateTime'),
+    'note' => array('name'=>'note','type'=>'xsd:string'),
+    'status_tobuy' => array('name'=>'status_tobuy','type'=>'xsd:string'),
+    'status_tosell' => array('name'=>'status_tosell','type'=>'xsd:string'),
+    'barcode' => array('name'=>'barcode','type'=>'xsd:string'),
+    'barcode_type' => array('name'=>'barcode_type','type'=>'xsd:string'),
+    'country_id' => array('name'=>'country_id','type'=>'xsd:string'),
+    'country_code' => array('name'=>'country_code','type'=>'xsd:string'),
+    'customcode' => array('name'=>'customcode','type'=>'xsd:string'),
+
+    'price_net' => array('name'=>'price_net','type'=>'xsd:string'),
+    'price' => array('name'=>'price','type'=>'xsd:string'),
+    'price_min_net' => array('name'=>'price_min_net','type'=>'xsd:string'),
+    'price_min' => array('name'=>'price_min','type'=>'xsd:string'),
+
+    'price_base_type' => array('name'=>'price_base_type','type'=>'xsd:string'),
+
+    'vat_rate' => array('name'=>'vat_rate','type'=>'xsd:string'),
+    'vat_npr' => array('name'=>'vat_npr','type'=>'xsd:string'),
+    'localtax1_tx' => array('name'=>'localtax1_tx','type'=>'xsd:string'),
+    'localtax2_tx' => array('name'=>'localtax2_tx','type'=>'xsd:string'),
+
+    'stock_alert' => array('name'=>'stock_alert','type'=>'xsd:string'),
+    'stock_real' => array('name'=>'stock_real','type'=>'xsd:string'),
+    'stock_pmp' => array('name'=>'stock_pmp','type'=>'xsd:string'),
+    'canvas' => array('name'=>'canvas','type'=>'xsd:string'),
+    'import_key' => array('name'=>'import_key','type'=>'xsd:string'),
+
+    'dir' => array('name'=>'dir','type'=>'xsd:string'),
+    'images' => array('name'=>'images','type'=>'tns:ImagesArray')
+);
+
+//Retreive all extrafield for product
+// fetch optionals attributes and labels
+$extrafields=new ExtraFields($db);
+$extralabels=$extrafields->fetch_name_optionals_label('product',true);
+if (count($extrafields)>0) {
+	$extrafield_array = array();
+}
+foreach($extrafields->attribute_label as $key=>$label)
+{
+	$type =$extrafields->attribute_type[$key];
+	if ($type=='date' || $type=='datetime') {$type='xsd:dateTime';}
+	else {$type='xsd:string';}
+
+	$extrafield_array['options_'.$key]=array('name'=>'options_'.$key,'type'=>$type);
+}
+
+$productorservice_fields=array_merge($productorservice_fields,$extrafield_array);
+
 // Define other specific objects
 $server->wsdl->addComplexType(
     'product',
@@ -95,45 +154,7 @@ $server->wsdl->addComplexType(
     'struct',
     'all',
     '',
-    array(
-    	'id' => array('name'=>'id','type'=>'xsd:string'),
-		'ref' => array('name'=>'ref','type'=>'xsd:string'),
-        'ref_ext' => array('name'=>'ref_ext','type'=>'xsd:string'),
-    	'type' => array('name'=>'type','type'=>'xsd:string'),
-		'label' => array('name'=>'label','type'=>'xsd:string'),
-        'description' => array('name'=>'description','type'=>'xsd:string'),
-        'date_creation' => array('name'=>'date_creation','type'=>'xsd:dateTime'),
-        'date_modification' => array('name'=>'date_modification','type'=>'xsd:dateTime'),
-        'note' => array('name'=>'note','type'=>'xsd:string'),
-    	'status_tobuy' => array('name'=>'status_tobuy','type'=>'xsd:string'),
-    	'status_tosell' => array('name'=>'status_tosell','type'=>'xsd:string'),
-    	'barcode' => array('name'=>'barcode','type'=>'xsd:string'),
-    	'barcode_type' => array('name'=>'barcode_type','type'=>'xsd:string'),
-		'country_id' => array('name'=>'country_id','type'=>'xsd:string'),
-    	'country_code' => array('name'=>'country_code','type'=>'xsd:string'),
-    	'customcode' => array('name'=>'customcode','type'=>'xsd:string'),
-
-    	'price_net' => array('name'=>'price_net','type'=>'xsd:string'),
-    	'price' => array('name'=>'price','type'=>'xsd:string'),
-    	'price_min_net' => array('name'=>'price_min_net','type'=>'xsd:string'),
-    	'price_min' => array('name'=>'price_min','type'=>'xsd:string'),
-
-    	'price_base_type' => array('name'=>'price_base_type','type'=>'xsd:string'),
-
-    	'vat_rate' => array('name'=>'vat_rate','type'=>'xsd:string'),
-    	'vat_npr' => array('name'=>'vat_npr','type'=>'xsd:string'),
-    	'localtax1_tx' => array('name'=>'localtax1_tx','type'=>'xsd:string'),
-    	'localtax2_tx' => array('name'=>'localtax2_tx','type'=>'xsd:string'),
-
-    	'stock_alert' => array('name'=>'stock_alert','type'=>'xsd:string'),
-    	'stock_real' => array('name'=>'stock_real','type'=>'xsd:string'),
-    	'stock_pmp' => array('name'=>'stock_pmp','type'=>'xsd:string'),
-		'canvas' => array('name'=>'canvas','type'=>'xsd:string'),
-		'import_key' => array('name'=>'import_key','type'=>'xsd:string'),
-
-		'dir' => array('name'=>'dir','type'=>'xsd:string'),
-		'images' => array('name'=>'images','type'=>'tns:ImagesArray')
-    )
+    $productorservice_fields
 );
 
 
@@ -357,47 +378,62 @@ function getProductOrService($authentication,$id='',$ref='',$ref_ext='',$lang=''
             	if (! empty($product->multilangs[$langs->defaultlang]["label"]))     		$product->label =  $product->multilangs[$langs->defaultlang]["label"];
             	if (! empty($product->multilangs[$langs->defaultlang]["description"]))     	$product->description =  $product->multilangs[$langs->defaultlang]["description"];
             	if (! empty($product->multilangs[$langs->defaultlang]["note"]))     		$product->note =  $product->multilangs[$langs->defaultlang]["note"];
-
+		
+		$productorservice_result_fields = array(
+		    'id' => $product->id,
+	   	    'ref' => $product->ref,
+	   	    'ref_ext' => $product->ref_ext,
+	    	    'label' => $product->label,
+	    	    'description' => $product->description,
+	    	    'date_creation' => dol_print_date($product->date_creation,'dayhourrfc'),
+	    	    'date_modification' => dol_print_date($product->date_modification,'dayhourrfc'),
+	            'note' => $product->note,
+	            'status_tosell' => $product->status,
+	            'status_tobuy' => $product->status_buy,
+		    'type' => $product->type,
+		    'barcode' => $product->barcode,
+		    'barcode_type' => $product->barcode_type,
+		    'country_id' => $product->country_id>0?$product->country_id:'',
+		    'country_code' => $product->country_code,
+		    'custom_code' => $product->customcode,
+	
+	            'price_net' => $product->price,
+	            'price' => $product->price_ttc,
+	            'price_min_net' => $product->price_min,
+	            'price_min' => $product->price_min_ttc,
+	            'price_base_type' => $product->price_base_type,
+		    'vat_rate' => $product->tva_tx,
+		    //! French VAT NPR
+		    'vat_npr' => $product->tva_npr,
+		    //! Spanish local taxes
+		    'localtax1_tx' => $product->localtax1_tx,
+		    'localtax2_tx' => $product->localtax2_tx,
+	
+		    'stock_real' => $product->stock_reel,
+		    'stock_alert' => $product->seuil_stock_alerte,
+		    'pmp' => $product->pmp,
+		    'import_key' => $product->import_key,
+		    'dir' => $pdir,
+		    'images' => $product->liste_photos($dir,$nbmax=10)
+                );
+                
+                //Retreive all extrafield for thirdsparty
+            	// fetch optionals attributes and labels
+            	$extrafields=new ExtraFields($db);
+            	$extralabels=$extrafields->fetch_name_optionals_label('product',true);
+            	//Get extrafield values
+            	$product->fetch_optionals($product->id,$extralabels);
+            	
+            	foreach($extrafields->attribute_label as $key=>$label)
+            	{
+            		$productorservice_result_fields=array_merge($productorservice_result_fields,array('options_'.$key => $product->array_options['options_'.$key]));
+            	}
+		
                 // Create
                 $objectresp = array(
 			    	'result'=>array('result_code'=>'OK', 'result_label'=>''),
-			        'product'=>array(
-				    	'id' => $product->id,
-			   			'ref' => $product->ref,
-			   			'ref_ext' => $product->ref_ext,
-			    		'label' => $product->label,
-			    		'description' => $product->description,
-			    		'date_creation' => dol_print_date($product->date_creation,'dayhourrfc'),
-			    		'date_modification' => dol_print_date($product->date_modification,'dayhourrfc'),
-			            'note' => $product->note,
-			            'status_tosell' => $product->status,
-			            'status_tobuy' => $product->status_buy,
-                		'type' => $product->type,
-				        'barcode' => $product->barcode,
-				        'barcode_type' => $product->barcode_type,
-                		'country_id' => $product->country_id>0?$product->country_id:'',
-				        'country_code' => $product->country_code,
-				        'custom_code' => $product->customcode,
-
-			        	'price_net' => $product->price,
-			        	'price' => $product->price_ttc,
-			        	'price_min_net' => $product->price_min,
-			        	'price_min' => $product->price_min_ttc,
-			        	'price_base_type' => $product->price_base_type,
-				        'vat_rate' => $product->tva_tx,
-				        //! French VAT NPR
-				        'vat_npr' => $product->tva_npr,
-				        //! Spanish local taxes
-				        'localtax1_tx' => $product->localtax1_tx,
-				        'localtax2_tx' => $product->localtax2_tx,
-
-				        'stock_real' => $product->stock_reel,
-                		'stock_alert' => $product->seuil_stock_alerte,
-				        'pmp' => $product->pmp,
-                		'import_key' => $product->import_key,
-                		'dir' => $pdir,
-                		'images' => $product->liste_photos($dir,$nbmax=10)
-                ));
+			        'product'=>$productorservice_result_fields
+                );
             }
             else
             {
@@ -497,6 +533,14 @@ function createProductOrService($authentication,$product)
         }*/
         //var_dump($product['ref_ext']);
         //var_dump($product['lines'][0]['type']);
+        
+        $extrafields=new ExtraFields($db);
+	$extralabels=$extrafields->fetch_name_optionals_label('product',true);
+	foreach($extrafields->attribute_label as $key=>$label)
+	{
+		$key='options_'.$key;
+		$newobject->array_options[$key]=$product[$key];
+	}
 
         $db->begin();
 
@@ -608,6 +652,14 @@ function updateProductOrService($authentication,$product)
         }*/
         //var_dump($product['ref_ext']);
         //var_dump($product['lines'][0]['type']);
+        
+	$extrafields=new ExtraFields($db);
+	$extralabels=$extrafields->fetch_name_optionals_label('product',true);
+	foreach($extrafields->attribute_label as $key=>$label)
+	{
+		$key='options_'.$key;
+		$newobject->array_options[$key]=$product[$key];
+	}
 
         $db->begin();
 
@@ -875,6 +927,7 @@ function getProductsForCategory($authentication,$id,$lang='')
 					{
 						$obj = new Product($db);
 						$obj->fetch($rec['fk_'.$field]);
+						$iProduct = 0;
 						if($obj->status > 0 )
 						{
 							$dir = (!empty($conf->product->dir_output)?$conf->product->dir_output:$conf->service->dir_output);
@@ -912,6 +965,20 @@ function getProductsForCategory($authentication,$id,$lang='')
 		                		'dir' => $pdir,
 		                		'images' => $obj->liste_photos($dir,$nbmax=10)
 							);
+							
+							//Retreive all extrafield for thirdsparty
+							// fetch optionals attributes and labels
+							$extrafields=new ExtraFields($db);
+							$extralabels=$extrafields->fetch_name_optionals_label('product',true);
+							//Get extrafield values
+							$product->fetch_optionals($obj->id,$extralabels);
+							
+							foreach($extrafields->attribute_label as $key=>$label)
+							{
+								$products[$iProduct]=array_merge($products[$iProduct],array('options_'.$key => $product->array_options['options_'.$key]));
+							}
+							
+							$iProduct++;
 						}
 
 					}
-- 
GitLab