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