dolibarr  16.0.5
server_productorservice.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2006-2016 Laurent Destailleur <eldy@users.sourceforge.net>
3  * Copyright (C) 2012 JF FERRY <jfefe@aternatik.fr>
4  * Copyright (C) 2020 Frédéric France <frederic.france@netlogic.fr>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <https://www.gnu.org/licenses/>.
18  *
19  * Path to WSDL is: http://localhost/dolibarr/webservices/server_productorservice.php?wsdl
20  */
21 
27 if (!defined("NOCSRFCHECK")) {
28  define("NOCSRFCHECK", '1');
29 }
30 
31 require '../master.inc.php';
32 require_once NUSOAP_PATH.'/nusoap.php'; // Include SOAP
33 require_once DOL_DOCUMENT_ROOT.'/core/lib/ws.lib.php';
34 require_once DOL_DOCUMENT_ROOT.'/core/lib/functions.lib.php';
35 require_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php';
36 
37 require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
38 require_once DOL_DOCUMENT_ROOT."/categories/class/categorie.class.php";
39 require_once DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php';
40 
41 
42 
43 dol_syslog("Call Dolibarr webservices interfaces");
44 
45 $langs->load("main");
46 
47 // Enable and test if module web services is enabled
48 if (empty($conf->global->MAIN_MODULE_WEBSERVICES)) {
49  $langs->load("admin");
50  dol_syslog("Call Dolibarr webservices interfaces with module webservices disabled");
51  print $langs->trans("WarningModuleNotActive", 'WebServices').'.<br><br>';
52  print $langs->trans("ToActivateModule");
53  exit;
54 }
55 
56 // Create the soap Object
57 $server = new nusoap_server();
58 $server->soap_defencoding = 'UTF-8';
59 $server->decode_utf8 = false;
60 $ns = 'http://www.dolibarr.org/ns/';
61 $server->configureWSDL('WebServicesDolibarrProductOrService', $ns);
62 $server->wsdl->schemaTargetNamespace = $ns;
63 
64 
65 // Define WSDL Authentication object
66 $server->wsdl->addComplexType(
67  'authentication',
68  'complexType',
69  'struct',
70  'all',
71  '',
72  array(
73  'dolibarrkey' => array('name'=>'dolibarrkey', 'type'=>'xsd:string'),
74  'sourceapplication' => array('name'=>'sourceapplication', 'type'=>'xsd:string'),
75  'login' => array('name'=>'login', 'type'=>'xsd:string'),
76  'password' => array('name'=>'password', 'type'=>'xsd:string'),
77  'entity' => array('name'=>'entity', 'type'=>'xsd:string')
78  )
79 );
80 // Define WSDL Return object
81 $server->wsdl->addComplexType(
82  'result',
83  'complexType',
84  'struct',
85  'all',
86  '',
87  array(
88  'result_code' => array('name'=>'result_code', 'type'=>'xsd:string'),
89  'result_label' => array('name'=>'result_label', 'type'=>'xsd:string')
90  )
91 );
92 
93 $productorservice_fields = array(
94  'id' => array('name'=>'id', 'type'=>'xsd:string'),
95  'ref' => array('name'=>'ref', 'type'=>'xsd:string'),
96  'ref_ext' => array('name'=>'ref_ext', 'type'=>'xsd:string'),
97  'type' => array('name'=>'type', 'type'=>'xsd:string'),
98  'label' => array('name'=>'label', 'type'=>'xsd:string'),
99  'description' => array('name'=>'description', 'type'=>'xsd:string'),
100  'date_creation' => array('name'=>'date_creation', 'type'=>'xsd:dateTime'),
101  'date_modification' => array('name'=>'date_modification', 'type'=>'xsd:dateTime'),
102  'note' => array('name'=>'note', 'type'=>'xsd:string'),
103  'status_tobuy' => array('name'=>'status_tobuy', 'type'=>'xsd:string'),
104  'status_tosell' => array('name'=>'status_tosell', 'type'=>'xsd:string'),
105  'barcode' => array('name'=>'barcode', 'type'=>'xsd:string'),
106  'barcode_type' => array('name'=>'barcode_type', 'type'=>'xsd:string'),
107  'country_id' => array('name'=>'country_id', 'type'=>'xsd:string'),
108  'country_code' => array('name'=>'country_code', 'type'=>'xsd:string'),
109  'customcode' => array('name'=>'customcode', 'type'=>'xsd:string'),
110 
111  'price_net' => array('name'=>'price_net', 'type'=>'xsd:string'),
112  'price' => array('name'=>'price', 'type'=>'xsd:string'),
113  'price_min_net' => array('name'=>'price_min_net', 'type'=>'xsd:string'),
114  'price_min' => array('name'=>'price_min', 'type'=>'xsd:string'),
115 
116  'price_base_type' => array('name'=>'price_base_type', 'type'=>'xsd:string'),
117 
118  'vat_rate' => array('name'=>'vat_rate', 'type'=>'xsd:string'),
119  'vat_npr' => array('name'=>'vat_npr', 'type'=>'xsd:string'),
120  'localtax1_tx' => array('name'=>'localtax1_tx', 'type'=>'xsd:string'),
121  'localtax2_tx' => array('name'=>'localtax2_tx', 'type'=>'xsd:string'),
122 
123  'stock_alert' => array('name'=>'stock_alert', 'type'=>'xsd:string'),
124  'stock_real' => array('name'=>'stock_real', 'type'=>'xsd:string'),
125  'stock_pmp' => array('name'=>'stock_pmp', 'type'=>'xsd:string'),
126  'warehouse_ref' => array('name'=>'warehouse_ref', 'type'=>'xsd:string'), // Used only for create or update to set which warehouse to use for stock correction if stock_real differs from database
127 
128  'canvas' => array('name'=>'canvas', 'type'=>'xsd:string'),
129  'import_key' => array('name'=>'import_key', 'type'=>'xsd:string'),
130 
131  'dir' => array('name'=>'dir', 'type'=>'xsd:string'),
132  'images' => array('name'=>'images', 'type'=>'tns:ImagesArray')
133 );
134 
135 
136 $elementtype = 'product';
137 
138 
139 //Retrieve all extrafield for product
140 // fetch optionals attributes and labels
141 $extrafields = new ExtraFields($db);
142 $extrafields->fetch_name_optionals_label($elementtype, true);
143 $extrafield_array = null;
144 if (is_array($extrafields) && count($extrafields) > 0) {
145  $extrafield_array = array();
146 }
147 if (isset($extrafields->attributes[$elementtype]['label']) && is_array($extrafields->attributes[$elementtype]['label']) && count($extrafields->attributes[$elementtype]['label'])) {
148  foreach ($extrafields->attributes[$elementtype]['label'] as $key => $label) {
149  $type = $extrafields->attributes[$elementtype]['type'][$key];
150  if ($type == 'date' || $type == 'datetime') {
151  $type = 'xsd:dateTime';
152  } else {
153  $type = 'xsd:string';
154  }
155 
156  $extrafield_array['options_'.$key] = array('name'=>'options_'.$key, 'type'=>$type);
157  }
158 }
159 
160 if (!empty($extrafield_array) && is_array($extrafield_array)) {
161  $productorservice_fields = array_merge($productorservice_fields, $extrafield_array);
162 }
163 
164 // Define other specific objects
165 $server->wsdl->addComplexType(
166  'product',
167  'complexType',
168  'struct',
169  'all',
170  '',
171  $productorservice_fields
172 );
173 
174 
175 /*
176  * Image of product
177  */
178 $server->wsdl->addComplexType(
179  'ImagesArray',
180  'complexType',
181  'array',
182  'sequence',
183  '',
184  array(
185  'image' => array(
186  'name' => 'image',
187  'type' => 'tns:image',
188  'minOccurs' => '0',
189  'maxOccurs' => 'unbounded'
190  )
191  )
192 );
193 
194 /*
195  * An image
196  */
197 $server->wsdl->addComplexType(
198  'image',
199  'complexType',
200  'struct',
201  'all',
202  '',
203  array(
204  'photo' => array('name'=>'photo', 'type'=>'xsd:string'),
205  'photo_vignette' => array('name'=>'photo_vignette', 'type'=>'xsd:string'),
206  'imgWidth' => array('name'=>'imgWidth', 'type'=>'xsd:string'),
207  'imgHeight' => array('name'=>'imgHeight', 'type'=>'xsd:string')
208  )
209 );
210 
211 
212 // Define other specific objects
213 $server->wsdl->addComplexType(
214  'filterproduct',
215  'complexType',
216  'struct',
217  'all',
218  '',
219  array(
220  //'limit' => array('name'=>'limit','type'=>'xsd:string'),
221  'type' => array('name'=>'type', 'type'=>'xsd:string'),
222  'status_tobuy' => array('name'=>'status_tobuy', 'type'=>'xsd:string'),
223  'status_tosell' => array('name'=>'status_tosell', 'type'=>'xsd:string'),
224  )
225 );
226 
227 $server->wsdl->addComplexType(
228  'ProductsArray2',
229  'complexType',
230  'array',
231  'sequence',
232  '',
233  array(
234  'product' => array(
235  'name' => 'product',
236  'type' => 'tns:product',
237  'minOccurs' => '0',
238  'maxOccurs' => 'unbounded'
239  )
240  )
241 );
242 
243 
244 // 5 styles: RPC/encoded, RPC/literal, Document/encoded (not WS-I compliant), Document/literal, Document/literal wrapped
245 // Style merely dictates how to translate a WSDL binding to a SOAP message. Nothing more. You can use either style with any programming model.
246 // http://www.ibm.com/developerworks/webservices/library/ws-whichwsdl/
247 $styledoc = 'rpc'; // rpc/document (document is an extend into SOAP 1.0 to support unstructured messages)
248 $styleuse = 'encoded'; // encoded/literal/literal wrapped
249 // Better choice is document/literal wrapped but literal wrapped not supported by nusoap.
250 
251 
252 // Register WSDL
253 $server->register(
254  'getProductOrService',
255  // Entry values
256  array('authentication'=>'tns:authentication', 'id'=>'xsd:string', 'ref'=>'xsd:string', 'ref_ext'=>'xsd:string', 'lang'=>'xsd:string'),
257  // Exit values
258  array('result'=>'tns:result', 'product'=>'tns:product'),
259  $ns,
260  $ns.'#getProductOrService',
261  $styledoc,
262  $styleuse,
263  'WS to get product or service'
264 );
265 
266 // Register WSDL
267 $server->register(
268  'createProductOrService',
269  // Entry values
270  array('authentication'=>'tns:authentication', 'product'=>'tns:product'),
271  // Exit values
272  array('result'=>'tns:result', 'id'=>'xsd:string'),
273  $ns,
274  $ns.'#createProductOrService',
275  $styledoc,
276  $styleuse,
277  'WS to create a product or service'
278 );
279 
280 // Register WSDL
281 $server->register(
282  'updateProductOrService',
283  // Entry values
284  array('authentication'=>'tns:authentication', 'product'=>'tns:product'),
285  // Exit values
286  array('result'=>'tns:result', 'id'=>'xsd:string'),
287  $ns,
288  $ns.'#updateProductOrService',
289  $styledoc,
290  $styleuse,
291  'WS to update a product or service'
292 );
293 
294 // Register WSDL
295 $server->register(
296  'deleteProductOrService',
297  // Entry values
298  array('authentication'=>'tns:authentication', 'listofid'=>'xsd:string'),
299  // Exit values
300  array('result'=>'tns:result', 'nbdeleted'=>'xsd:int'),
301  $ns,
302  $ns.'#deleteProductOrService',
303  $styledoc,
304  $styleuse,
305  'WS to delete a product or service'
306 );
307 
308 // Register WSDL
309 $server->register(
310  'getListOfProductsOrServices',
311  // Entry values
312  array('authentication'=>'tns:authentication', 'filterproduct'=>'tns:filterproduct'),
313  // Exit values
314  array('result'=>'tns:result', 'products'=>'tns:ProductsArray2'),
315  $ns,
316  $ns.'#getListOfProductsOrServices',
317  $styledoc,
318  $styleuse,
319  'WS to get list of all products or services id and ref'
320 );
321 
322 // Register WSDL
323 $server->register(
324  'getProductsForCategory',
325  // Entry values
326  array('authentication'=>'tns:authentication', 'id'=>'xsd:string', 'lang'=>'xsd:string'),
327  // Exit values
328  array('result'=>'tns:result', 'products'=>'tns:ProductsArray2'),
329  $ns,
330  $ns.'#getProductsForCategory',
331  $styledoc,
332  $styleuse,
333  'WS to get list of all products or services for a category'
334 );
335 
336 
347 function getProductOrService($authentication, $id = '', $ref = '', $ref_ext = '', $lang = '')
348 {
349  global $db, $conf, $langs;
350 
351  dol_syslog("Function: getProductOrService login=".$authentication['login']." id=".$id." ref=".$ref." ref_ext=".$ref_ext);
352 
353  $langcode = ($lang ? $lang : (empty($conf->global->MAIN_LANG_DEFAULT) ? 'auto' : $conf->global->MAIN_LANG_DEFAULT));
354  $langs->setDefaultLang($langcode);
355 
356  if ($authentication['entity']) {
357  $conf->entity = $authentication['entity'];
358  }
359 
360  // Init and check authentication
361  $objectresp = array();
362  $errorcode = '';
363  $errorlabel = '';
364  $error = 0;
365  $fuser = check_authentication($authentication, $error, $errorcode, $errorlabel);
366  // Check parameters
367  if (!$error && (($id && $ref) || ($id && $ref_ext) || ($ref && $ref_ext))) {
368  $error++;
369  $errorcode = 'BAD_PARAMETERS'; $errorlabel = "Parameter id, ref and ref_ext can't be both provided. You must choose one or other but not both.";
370  }
371 
372  if (!$error) {
373  $langcode = ($lang ? $lang : (empty($conf->global->MAIN_LANG_DEFAULT) ? 'auto' : $conf->global->MAIN_LANG_DEFAULT));
374  $langs->setDefaultLang($langcode);
375 
376  $fuser->getrights();
377 
378  $nbmax = 10;
379  if ($fuser->rights->produit->lire || $fuser->rights->service->lire) {
380  $product = new Product($db);
381  $result = $product->fetch($id, $ref, $ref_ext);
382 
383  if ($result > 0) {
384  $product->load_stock();
385 
386  $dir = (!empty($conf->product->dir_output) ? $conf->product->dir_output : $conf->service->dir_output);
387  $pdir = get_exdir($product->id, 2, 0, 0, $product, 'product').$product->ref."/";
388  $dir = $dir.'/'.$pdir;
389 
390  if (!empty($product->multilangs[$langs->defaultlang]["label"])) {
391  $product->label = $product->multilangs[$langs->defaultlang]["label"];
392  }
393  if (!empty($product->multilangs[$langs->defaultlang]["description"])) {
394  $product->description = $product->multilangs[$langs->defaultlang]["description"];
395  }
396  if (!empty($product->multilangs[$langs->defaultlang]["note"])) {
397  $product->note = $product->multilangs[$langs->defaultlang]["note"];
398  }
399 
400  $productorservice_result_fields = array(
401  'id' => $product->id,
402  'ref' => $product->ref,
403  'ref_ext' => $product->ref_ext,
404  'label' => $product->label,
405  'description' => $product->description,
406  'date_creation' => dol_print_date($product->date_creation, 'dayhourrfc'),
407  'date_modification' => dol_print_date($product->date_modification, 'dayhourrfc'),
408  'note' => $product->note,
409  'status_tosell' => $product->status,
410  'status_tobuy' => $product->status_buy,
411  'type' => $product->type,
412  'barcode' => $product->barcode,
413  'barcode_type' => $product->barcode_type,
414  'country_id' => $product->country_id > 0 ? $product->country_id : '',
415  'country_code' => $product->country_code,
416  'custom_code' => $product->customcode,
417 
418  'price_net' => $product->price,
419  'price' => $product->price_ttc,
420  'price_min_net' => $product->price_min,
421  'price_min' => $product->price_min_ttc,
422  'price_base_type' => $product->price_base_type,
423  'vat_rate' => $product->tva_tx,
425  'vat_npr' => $product->tva_npr,
427  'localtax1_tx' => $product->localtax1_tx,
428  'localtax2_tx' => $product->localtax2_tx,
429 
430  'stock_real' => $product->stock_reel,
431  'stock_virtual' => $product->stock_theorique,
432  'stock_alert' => $product->seuil_stock_alerte,
433  'pmp' => $product->pmp,
434  'import_key' => $product->import_key,
435  'dir' => $pdir,
436  'images' => $product->liste_photos($dir, $nbmax)
437  );
438 
439  $elementtype = 'product';
440 
441  //Retrieve all extrafield for thirdsparty
442  // fetch optionals attributes and labels
443  $extrafields = new ExtraFields($db);
444  $extrafields->fetch_name_optionals_label($elementtype, true);
445  //Get extrafield values
446  $product->fetch_optionals();
447 
448  if (isset($extrafields->attributes[$elementtype]['label']) && is_array($extrafields->attributes[$elementtype]['label']) && count($extrafields->attributes[$elementtype]['label'])) {
449  foreach ($extrafields->attributes[$elementtype]['label'] as $key => $label) {
450  $productorservice_result_fields = array_merge($productorservice_result_fields, array('options_'.$key => $product->array_options['options_'.$key]));
451  }
452  }
453 
454  // Create
455  $objectresp = array(
456  'result'=>array('result_code'=>'OK', 'result_label'=>''),
457  'product'=>$productorservice_result_fields
458  );
459  } else {
460  $error++;
461  $errorcode = 'NOT_FOUND'; $errorlabel = 'Object not found for id='.$id.' nor ref='.$ref.' nor ref_ext='.$ref_ext;
462  }
463  } else {
464  $error++;
465  $errorcode = 'PERMISSION_DENIED'; $errorlabel = 'User does not have permission for this request';
466  }
467  }
468 
469  if ($error) {
470  $objectresp = array('result'=>array('result_code' => $errorcode, 'result_label' => $errorlabel));
471  }
472  //var_dump($objectresp);exit;
473  return $objectresp;
474 }
475 
476 
484 function createProductOrService($authentication, $product)
485 {
486  global $db, $conf;
487 
488  $now = dol_now();
489 
490  dol_syslog("Function: createProductOrService login=".$authentication['login']);
491 
492  if ($authentication['entity']) {
493  $conf->entity = $authentication['entity'];
494  }
495 
496  // Init and check authentication
497  $objectresp = array();
498  $errorcode = '';
499  $errorlabel = '';
500  $error = 0;
501  $fuser = check_authentication($authentication, $error, $errorcode, $errorlabel);
502  // Check parameters
503  if (empty($product['price_base_type'])) {
504  if (isset($product['price_net']) && $product['price_net'] > 0) {
505  $product['price_base_type'] = 'HT';
506  }
507  if (isset($product['price']) && $product['price'] > 0) {
508  $product['price_base_type'] = 'TTC';
509  }
510  }
511 
512  if (isset($product['price_net']) && $product['price_net'] > 0 && isset($product['price']) && $product['price'] > 0) {
513  $error++; $errorcode = 'KO'; $errorlabel = "You must choose between price or price_net to provide price.";
514  }
515 
516  if (!empty($product['barcode']) && empty($product['barcode_type'])) {
517  $error++; $errorcode = 'KO'; $errorlabel = "You must set a barcode type when setting a barcode.";
518  }
519 
520  if (!$error) {
521  include_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php';
522 
523  $newobject = new Product($db);
524  $newobject->ref = $product['ref'];
525  $newobject->ref_ext = empty($product['ref_ext']) ? '' : $product['ref_ext'];
526  $newobject->type = empty($product['type']) ? 0 : $product['type'];
527  $newobject->label = empty($product['label']) ? '' : $product['label'];
528  $newobject->description = empty($product['description']) ? '' : $product['description'];
529  $newobject->note_public = empty($product['note_public']) ? '' : $product['note_public'];
530  $newobject->note_private = empty($product['note_private']) ? '' : $product['note_private'];
531  $newobject->status = empty($product['status_tosell']) ? 0 : $product['status_tosell'];
532  $newobject->status_buy = empty($product['status_tobuy']) ? 0 : $product['status_tobuy'];
533  $newobject->price = isset($product['price_net']) ? $product['price_net'] : 0;
534  $newobject->price_ttc = isset($product['price']) ? $product['price'] : 0;
535  $newobject->tva_tx = empty($product['vat_rate']) ? 0 : $product['vat_rate'];
536  $newobject->price_base_type = $product['price_base_type'];
537  $newobject->date_creation = $now;
538 
539  if (!empty($product['barcode'])) {
540  $newobject->barcode = $product['barcode'];
541  $newobject->barcode_type = $product['barcode_type'];
542  }
543 
544  $newobject->stock_reel = isset($product['stock_real']) ? $product['stock_real'] : null;
545  $newobject->pmp = isset($product['pmp']) ? $product['pmp'] : null;
546  $newobject->seuil_stock_alerte = isset($product['stock_alert']) ? $product['stock_alert'] : null;
547 
548  $newobject->country_id = isset($product['country_id']) ? $product['country_id'] : 0;
549  if (!empty($product['country_code'])) {
550  $newobject->country_id = getCountry($product['country_code'], 3);
551  }
552  $newobject->customcode = isset($product['customcode']) ? $product['customcode'] : '';
553 
554  $newobject->canvas = isset($product['canvas']) ? $product['canvas'] : '';
555  /*foreach($product['lines'] as $line)
556  {
557  $newline=new FactureLigne($db);
558  $newline->type=$line['type'];
559  $newline->desc=$line['desc'];
560  $newline->fk_product=$line['fk_product'];
561  $newline->total_ht=$line['total_net'];
562  $newline->total_vat=$line['total_vat'];
563  $newline->total_ttc=$line['total'];
564  $newline->vat=$line['vat_rate'];
565  $newline->qty=$line['qty'];
566  $newline->fk_product=$line['product_id'];
567  }*/
568  //var_dump($product['ref_ext']);
569  //var_dump($product['lines'][0]['type']);
570 
571  $elementtype = 'product';
572 
573  $extrafields = new ExtraFields($db);
574  $extrafields->fetch_name_optionals_label($elementtype, true);
575  if (isset($extrafields->attributes[$elementtype]['label']) && is_array($extrafields->attributes[$elementtype]['label']) && count($extrafields->attributes[$elementtype]['label'])) {
576  foreach ($extrafields->attributes[$elementtype]['label'] as $key => $label) {
577  $key = 'options_'.$key;
578  $newobject->array_options[$key] = $product[$key];
579  }
580  }
581 
582  $db->begin();
583 
584  $result = $newobject->create($fuser, 0);
585  if ($result <= 0) {
586  $error++;
587  }
588 
589  if (!$error) {
590  // Update stock if stock count is provided and differs from database after creation or update
591  if (isset($product['stock_real']) && $product['stock_real'] != '' && !empty($conf->global->stock->enabled)) {
592  include_once DOL_DOCUMENT_ROOT.'/product/stock/class/entrepot.class.php';
593 
594  $savstockreal = $newobject->stock_reel;
595  $newobject->load_stock('novirtual,nobatch'); // This overwrite ->stock_reel, surely 0 because we have just created product
596  $getstockreal = $newobject->stock_reel;
597 
598  if ($savstockreal != $getstockreal) {
599  $warehouse = new Entrepot($db);
600  $warehouse->fetch(0, $product['warehouse_ref']);
601  if ($warehouse->id > 0) {
602  if (($savstockreal - $getstockreal) > 0) {
603  $result = $newobject->correct_stock($fuser, $warehouse->id, ($savstockreal - $getstockreal), 0, 'Correction from external call (Web Service)', 0, 'WS'.dol_print_date($now, 'dayhourlog'));
604  }
605  if (($savstockreal - $getstockreal) > 0) {
606  $result = $newobject->correct_stock($fuser, $warehouse->id, ($savstockreal - $getstockreal), 1, 'Correction from external call (Web Service)', 0, 'WS'.dol_print_date($now, 'dayhourlog'));
607  }
608  if ($result <= 0) {
609  $error++;
610  $newobject->error = 'You set a different value for stock, but correction of stock count (before='.$getstockreal.', after='.$savstockreal.') fails with error '.$newobject->error;
611  }
612  } else {
613  $error++;
614  $newobject->error = 'You set a different value for stock but we failed to find warehouse '.$product['warehouse_ref'].' to make correction.';
615  }
616  }
617  }
618  }
619 
620  if (!$error) {
621  $db->commit();
622  $objectresp = array('result'=>array('result_code'=>'OK', 'result_label'=>''), 'id'=>$newobject->id, 'ref'=>$newobject->ref);
623  } else {
624  $db->rollback();
625  $error++;
626  $errorcode = 'KO';
627  $errorlabel = $newobject->error;
628  }
629  }
630 
631  if ($error) {
632  $objectresp = array('result'=>array('result_code' => $errorcode, 'result_label' => $errorlabel));
633  }
634 
635  return $objectresp;
636 }
637 
638 
646 function updateProductOrService($authentication, $product)
647 {
648  global $db, $conf;
649 
650  $now = dol_now();
651 
652  dol_syslog("Function: updateProductOrService login=".$authentication['login']);
653 
654  if ($authentication['entity']) {
655  $conf->entity = $authentication['entity'];
656  }
657 
658  // Init and check authentication
659  $objectresp = array();
660  $errorcode = '';
661  $errorlabel = '';
662  $error = 0;
663  $fuser = check_authentication($authentication, $error, $errorcode, $errorlabel);
664  // Check parameters
665  if ($product['price_net'] > 0) {
666  $product['price_base_type'] = 'HT';
667  }
668  if ($product['price'] > 0) {
669  $product['price_base_type'] = 'TTC';
670  }
671 
672  if ($product['price_net'] > 0 && $product['price'] > 0) {
673  $error++; $errorcode = 'KO'; $errorlabel = "You must choose between price or price_net to provide price.";
674  }
675 
676 
677  if ($product['barcode'] && !$product['barcode_type']) {
678  $error++; $errorcode = 'KO'; $errorlabel = "You must set a barcode type when setting a barcode.";
679  }
680 
681  if (!$error) {
682  include_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php';
683 
684  $newobject = new Product($db);
685  $newobject->fetch($product['id']);
686 
687  if (isset($product['ref'])) {
688  $newobject->ref = $product['ref'];
689  }
690  if (isset($product['ref_ext'])) {
691  $newobject->ref_ext = $product['ref_ext'];
692  }
693  $newobject->type = $product['type'];
694  $newobject->label = $product['label'];
695  $newobject->description = $product['description'];
696  $newobject->note = $product['note'];
697  $newobject->status = $product['status_tosell'];
698  $newobject->status_buy = $product['status_tobuy'];
699  $newobject->price = $product['price_net'];
700  $newobject->price_ttc = $product['price'];
701  $newobject->tva_tx = $product['vat_rate'];
702  $newobject->price_base_type = $product['price_base_type'];
703  $newobject->date_creation = $now;
704 
705  if ($product['barcode']) {
706  $newobject->barcode = $product['barcode'];
707  $newobject->barcode_type = $product['barcode_type'];
708  }
709 
710  $newobject->stock_reel = isset($product['stock_real']) ? $product['stock_real'] : null;
711  $newobject->pmp = isset($product['pmp']) ? $product['pmp'] : null;
712  $newobject->seuil_stock_alerte = isset($product['stock_alert']) ? $product['stock_alert'] : null;
713 
714  $newobject->country_id = isset($product['country_id']) ? $product['country_id'] : 0;
715  if (!empty($product['country_code'])) {
716  $newobject->country_id = getCountry($product['country_code'], 3);
717  }
718  $newobject->customcode = isset($product['customcode']) ? $product['customcode'] : '';
719 
720  $newobject->canvas = isset($product['canvas']) ? $product['canvas'] : '';
721 
722  $elementtype = 'product';
723 
724  $extrafields = new ExtraFields($db);
725  $extrafields->fetch_name_optionals_label($elementtype, true);
726  if (isset($extrafields->attributes[$elementtype]['label']) && is_array($extrafields->attributes[$elementtype]['label']) && count($extrafields->attributes[$elementtype]['label'])) {
727  foreach ($extrafields->attributes[$elementtype]['label'] as $key => $label) {
728  $key = 'options_'.$key;
729  $newobject->array_options[$key] = $product[$key];
730  }
731  }
732 
733  $db->begin();
734 
735  $result = $newobject->update($newobject->id, $fuser);
736  if ($result <= 0) {
737  $error++;
738  } else {
739  // Update stock if stock count is provided and differs from database after creation or update
740  if (isset($product['stock_real']) && $product['stock_real'] != '' && !empty($conf->global->stock->enabled)) {
741  include_once DOL_DOCUMENT_ROOT.'/product/stock/class/entrepot.class.php';
742 
743  $savstockreal = $newobject->stock_reel;
744  $newobject->load_stock('novirtual,nobatch'); // This overwrite ->stock_reel
745  $getstockreal = $newobject->stock_reel;
746 
747  if ($savstockreal != $getstockreal) {
748  $warehouse = new Entrepot($db);
749  $warehouse->fetch(0, $product['warehouse_ref']);
750  if ($warehouse->id > 0) {
751  if (($savstockreal - $getstockreal) > 0) {
752  $result = $newobject->correct_stock($fuser, $warehouse->id, ($savstockreal - $getstockreal), 0, 'Correction from external call (Web Service)', 0, 'WS'.dol_print_date($now, 'dayhourlog'));
753  }
754  if (($savstockreal - $getstockreal) > 0) {
755  $result = $newobject->correct_stock($fuser, $warehouse->id, ($savstockreal - $getstockreal), 1, 'Correction from external call (Web Service)', 0, 'WS'.dol_print_date($now, 'dayhourlog'));
756  }
757  if ($result <= 0) {
758  $error++;
759  $newobject->error = 'You set a different value for stock, but correction of stock count (before='.$getstockreal.', after='.$savstockreal.') fails with error '.$newobject->error;
760  }
761  } else {
762  $error++;
763  $newobject->error = 'You set a different value for stock but we failed to find warehouse '.$product['warehouse_ref'].' to make correction.';
764  }
765  }
766  }
767  }
768 
769  if (!$error) {
770  if ($newobject->price_base_type == 'HT') {
771  $result = $newobject->updatePrice($newobject->price, $newobject->price_base_type, $fuser);
772  if ($result <= 0) {
773  $error++;
774  }
775  } elseif ($newobject->price_base_type == 'TTC') {
776  $result = $newobject->updatePrice($newobject->price_ttc, $newobject->price_base_type, $fuser);
777  if ($result <= 0) {
778  $error++;
779  }
780  }
781  }
782 
783  if (!$error) {
784  $db->commit();
785  $objectresp = array('result'=>array('result_code'=>'OK', 'result_label'=>''), 'id'=>$newobject->id, 'ref'=>$newobject->ref);
786  } else {
787  $db->rollback();
788  $error++;
789  $errorcode = 'KO';
790  $errorlabel = $newobject->error;
791  }
792  }
793 
794  if ($error) {
795  $objectresp = array('result'=>array('result_code' => $errorcode, 'result_label' => $errorlabel));
796  }
797 
798  return $objectresp;
799 }
800 
801 
809 function deleteProductOrService($authentication, $listofidstring)
810 {
811  global $db, $conf;
812 
813  dol_syslog("Function: deleteProductOrService login=".$authentication['login']);
814 
815  if ($authentication['entity']) {
816  $conf->entity = $authentication['entity'];
817  }
818 
819  // Init and check authentication
820  $objectresp = array();
821  $errorcode = '';
822  $errorlabel = '';
823  $error = 0;
824  $fuser = check_authentication($authentication, $error, $errorcode, $errorlabel);
825 
826  // User must be defined to user authenticated
827  global $user;
828  $user = $fuser;
829 
830  $listofid = explode(',', trim($listofidstring));
831  $listofiddeleted = array();
832 
833  // Check parameters
834  if (count($listofid) == 0 || empty($listofid[0])) {
835  $error++; $errorcode = 'KO'; $errorlabel = "List of Id of products or services to delete are required.";
836  }
837 
838  if (!$error) {
839  $firsterror = '';
840 
841  $db->begin();
842 
843  foreach ($listofid as $id) {
844  $newobject = new Product($db);
845  $result = $newobject->fetch($id);
846 
847  if ($result == 0) {
848  $error++;
849  $firsterror = 'Product or service with id '.$id.' not found';
850  break;
851  } else {
852  $result = $newobject->delete($user);
853  if ($result <= 0) {
854  $error++;
855  $firsterror = $newobject->error;
856  break;
857  }
858 
859  $listofiddeleted[] = $id;
860  }
861  }
862 
863  if (!$error) {
864  $db->commit();
865  //$objectresp=array('result'=>array('result_code'=>'OK', 'result_label'=>''), 'listofid'=>$listofiddeleted);
866  $objectresp = array('result'=>array('result_code'=>'OK', 'result_label'=>''), 'nbdeleted'=>count($listofiddeleted));
867  } else {
868  $db->rollback();
869  $error++;
870  $errorcode = 'KO';
871  $errorlabel = $firsterror;
872  }
873  }
874 
875  if ($error) {
876  //$objectresp = array('result'=>array('result_code' => $errorcode, 'result_label' => $errorlabel), 'listofid'=>$listofiddeleted);
877  $objectresp = array('result'=>array('result_code' => $errorcode, 'result_label' => $errorlabel), 'nbdeleted'=>0);
878  } elseif (count($listofiddeleted) == 0) {
879  //$objectresp=array('result'=>array('result_code'=>'NOT_FOUND', 'result_label'=>'No product or service with id '.join(',',$listofid).' found'), 'listofid'=>$listofiddeleted);
880  $objectresp = array('result'=>array('result_code'=>'NOT_FOUND', 'result_label'=>'No product or service with id '.join(',', $listofid).' found'), 'nbdeleted'=>0);
881  }
882 
883  return $objectresp;
884 }
885 
886 
894 function getListOfProductsOrServices($authentication, $filterproduct)
895 {
896  global $db, $conf;
897 
898  dol_syslog("Function: getListOfProductsOrServices login=".$authentication['login']);
899 
900  if ($authentication['entity']) {
901  $conf->entity = $authentication['entity'];
902  }
903 
904  // Init and check authentication
905  $objectresp = array();
906  $arrayproducts = array();
907  $errorcode = '';
908  $errorlabel = '';
909  $error = 0;
910  $fuser = check_authentication($authentication, $error, $errorcode, $errorlabel);
911  // Check parameters
912 
913  if (!$error) {
914  $sql = "SELECT rowid, ref, ref_ext";
915  $sql .= " FROM ".MAIN_DB_PREFIX."product";
916  $sql .= " WHERE entity=".$conf->entity;
917  foreach ($filterproduct as $key => $val) {
918  if ($key == 'type' && $val >= 0) {
919  $sql .= " AND fk_product_type = ".((int) $val);
920  }
921  if ($key == 'status_tosell') {
922  $sql .= " AND tosell = ".((int) $val);
923  }
924  if ($key == 'status_tobuy') {
925  $sql .= " AND tobuy = ".((int) $val);
926  }
927  }
928  $resql = $db->query($sql);
929  if ($resql) {
930  $num = $db->num_rows($resql);
931 
932  $i = 0;
933  while ($i < $num) {
934  $obj = $db->fetch_object($resql);
935  $arrayproducts[] = array('id'=>$obj->rowid, 'ref'=>$obj->ref, 'ref_ext'=>$obj->ref_ext);
936  $i++;
937  }
938  } else {
939  $error++;
940  $errorcode = $db->lasterrno();
941  $errorlabel = $db->lasterror();
942  }
943  }
944 
945  if ($error) {
946  $objectresp = array(
947  'result'=>array('result_code' => $errorcode, 'result_label' => $errorlabel),
948  'products'=>$arrayproducts
949  );
950  } else {
951  $objectresp = array(
952  'result'=>array('result_code' => 'OK', 'result_label' => ''),
953  'products'=>$arrayproducts
954  );
955  }
956 
957  return $objectresp;
958 }
959 
960 
969 function getProductsForCategory($authentication, $id, $lang = '')
970 {
971  global $db, $conf, $langs;
972 
973  $langcode = ($lang ? $lang : (empty($conf->global->MAIN_LANG_DEFAULT) ? 'auto' : $conf->global->MAIN_LANG_DEFAULT));
974  $langs->setDefaultLang($langcode);
975 
976  dol_syslog("Function: getProductsForCategory login=".$authentication['login']." id=".$id);
977 
978  if ($authentication['entity']) {
979  $conf->entity = $authentication['entity'];
980  }
981 
982  $objectresp = array();
983  $errorcode = ''; $errorlabel = '';
984  $error = 0;
985 
986  $fuser = check_authentication($authentication, $error, $errorcode, $errorlabel);
987 
988 
989  if (!$error && !$id) {
990  $error++;
991  $errorcode = 'BAD_PARAMETERS'; $errorlabel = "Parameter id must be provided.";
992  }
993 
994 
995  if (!$error) {
996  $langcode = ($lang ? $lang : (empty($conf->global->MAIN_LANG_DEFAULT) ? 'auto' : $conf->global->MAIN_LANG_DEFAULT));
997  $langs->setDefaultLang($langcode);
998 
999  $fuser->getrights();
1000 
1001  $nbmax = 10;
1002  if ($fuser->rights->produit->lire) {
1003  $categorie = new Categorie($db);
1004  $result = $categorie->fetch($id);
1005  if ($result > 0) {
1006  $table = "product";
1007  $field = "product";
1008  $sql = "SELECT fk_".$field." FROM ".MAIN_DB_PREFIX."categorie_".$table;
1009  $sql .= " WHERE fk_categorie = ".((int) $id);
1010  $sql .= " ORDER BY fk_".$field." ASC";
1011 
1012 
1013  dol_syslog("getProductsForCategory get id of product into category", LOG_DEBUG);
1014  $res = $db->query($sql);
1015  if ($res) {
1016  $iProduct = 0;
1017  $tmpproduct = new Product($db);
1018  $products = array();
1019  while ($rec = $db->fetch_array($res)) {
1020  $tmpproduct->fetch($rec['fk_'.$field]);
1021  if ($tmpproduct->status > 0) {
1022  $dir = (!empty($conf->product->dir_output) ? $conf->product->dir_output : $conf->service->dir_output);
1023  $pdir = get_exdir($tmpproduct->id, 2, 0, 0, $tmpproduct, 'product').$tmpproduct->id."/photos/";
1024  $dir = $dir.'/'.$pdir;
1025 
1026  $products[] = array(
1027  'id' => $tmpproduct->id,
1028  'ref' => $tmpproduct->ref,
1029  'ref_ext' => $tmpproduct->ref_ext,
1030  'label' => !empty($tmpproduct->multilangs[$langs->defaultlang]["label"]) ? $tmpproduct->multilangs[$langs->defaultlang]["label"] : $tmpproduct->label,
1031  'description' => !empty($tmpproduct->multilangs[$langs->defaultlang]["description"]) ? $tmpproduct->multilangs[$langs->defaultlang]["description"] : $tmpproduct->description,
1032  'date_creation' => dol_print_date($tmpproduct->date_creation, 'dayhourrfc'),
1033  'date_modification' => dol_print_date($tmpproduct->date_modification, 'dayhourrfc'),
1034  'note' => !empty($tmpproduct->multilangs[$langs->defaultlang]["note"]) ? $tmpproduct->multilangs[$langs->defaultlang]["note"] : $tmpproduct->note,
1035  'status_tosell' => $tmpproduct->status,
1036  'status_tobuy' => $tmpproduct->status_buy,
1037  'type' => $tmpproduct->type,
1038  'barcode' => $tmpproduct->barcode,
1039  'barcode_type' => $tmpproduct->barcode_type,
1040  'country_id' => $tmpproduct->country_id > 0 ? $tmpproduct->country_id : '',
1041  'country_code' => $tmpproduct->country_code,
1042  'custom_code' => $tmpproduct->customcode,
1043 
1044  'price_net' => $tmpproduct->price,
1045  'price' => $tmpproduct->price_ttc,
1046  'vat_rate' => $tmpproduct->tva_tx,
1047 
1048  'price_base_type' => $tmpproduct->price_base_type,
1049 
1050  'stock_real' => $tmpproduct->stock_reel,
1051  'stock_alert' => $tmpproduct->seuil_stock_alerte,
1052  'pmp' => $tmpproduct->pmp,
1053  'import_key' => $tmpproduct->import_key,
1054  'dir' => $pdir,
1055  'images' => $tmpproduct->liste_photos($dir, $nbmax)
1056  );
1057 
1058  $elementtype = 'product';
1059 
1060  //Retrieve all extrafield for thirdsparty
1061  // fetch optionals attributes and labels
1062  $extrafields = new ExtraFields($db);
1063  $extrafields->fetch_name_optionals_label($elementtype, true);
1064  //Get extrafield values
1065  $tmpproduct->fetch_optionals();
1066 
1067  if (isset($extrafields->attributes[$elementtype]['label']) && is_array($extrafields->attributes[$elementtype]['label']) && count($extrafields->attributes[$elementtype]['label'])) {
1068  foreach ($extrafields->attributes[$elementtype]['label'] as $key => $label) {
1069  $products[$iProduct] = array_merge($products[$iProduct], array('options_'.$key => $tmpproduct->array_options['options_'.$key]));
1070  }
1071  }
1072 
1073  $iProduct++;
1074  }
1075  }
1076 
1077  // Retour
1078  $objectresp = array(
1079  'result'=>array('result_code'=>'OK', 'result_label'=>''),
1080  'products'=> $products
1081  );
1082  } else {
1083  $errorcode = 'NORECORDS_FOR_ASSOCIATION'; $errorlabel = 'No products associated'.$sql;
1084  $objectresp = array('result'=>array('result_code' => $errorcode, 'result_label' => $errorlabel));
1085  dol_syslog("getProductsForCategory:: ".$errorcode, LOG_DEBUG);
1086  }
1087  } else {
1088  $error++;
1089  $errorcode = 'NOT_FOUND'; $errorlabel = 'Object not found for id='.$id;
1090  }
1091  } else {
1092  $error++;
1093  $errorcode = 'PERMISSION_DENIED'; $errorlabel = 'User does not have permission for this request';
1094  }
1095  }
1096 
1097  if ($error) {
1098  $objectresp = array('result'=>array('result_code' => $errorcode, 'result_label' => $errorlabel));
1099  }
1100 
1101  return $objectresp;
1102 }
1103 
1104 // Return the results.
1105 $server->service(file_get_contents("php://input"));
getProductsForCategory
getProductsForCategory($authentication, $id, $lang='')
Get list of products for a category.
Definition: server_productorservice.php:969
getProductOrService
getProductOrService($authentication, $id='', $ref='', $ref_ext='', $lang='')
Get produt or service.
Definition: server_productorservice.php:347
createProductOrService
createProductOrService($authentication, $product)
Create an invoice.
Definition: server_productorservice.php:484
Categorie
Class to manage categories.
Definition: categorie.class.php:47
dol_print_date
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs='', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
Definition: functions.lib.php:2514
updateProductOrService
updateProductOrService($authentication, $product)
Update a product or service.
Definition: server_productorservice.php:646
get_exdir
get_exdir($num, $level, $alpha, $withoutslash, $object, $modulepart='')
Return a path to have a the directory according to object where files are stored.
Definition: functions.lib.php:6549
getListOfProductsOrServices
getListOfProductsOrServices($authentication, $filterproduct)
getListOfProductsOrServices
Definition: server_productorservice.php:894
dol_syslog
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
Definition: functions.lib.php:1603
ExtraFields
Class to manage standard extra fields.
Definition: extrafields.class.php:39
Product
Class to manage products or services.
Definition: product.class.php:46
check_authentication
check_authentication($authentication, &$error, &$errorcode, &$errorlabel)
Check authentication array and set error, errorcode, errorlabel.
Definition: ws.lib.php:35
Entrepot
Class to manage warehouses.
Definition: entrepot.class.php:35
dol_now
dol_now($mode='auto')
Return date for now.
Definition: functions.lib.php:2845
$resql
if(isModEnabled('facture') &&!empty($user->rights->facture->lire)) if((isModEnabled('fournisseur') &&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->rights->fournisseur->facture->lire)||(isModEnabled('supplier_invoice') && $user->rights->supplier_invoice->lire)) if(isModEnabled('don') &&!empty($user->rights->don->lire)) if(isModEnabled('tax') &&!empty($user->rights->tax->charges->lire)) if(isModEnabled('facture') &&isModEnabled('commande') && $user->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $resql
Social contributions to pay.
Definition: index.php:742
getCountry
getCountry($searchkey, $withcode='', $dbtouse=0, $outputlangs='', $entconv=1, $searchlabel='')
Return country label, code or id from an id, code or label.
Definition: company.lib.php:489
deleteProductOrService
deleteProductOrService($authentication, $listofidstring)
Delete a product or service.
Definition: server_productorservice.php:809