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