From 409fd169cf398c542aae8ec58aed0058103125df Mon Sep 17 00:00:00 2001 From: eldy Date: Sun, 2 Oct 2011 01:23:57 +0200 Subject: [PATCH] New: Add webservice to create a product --- ChangeLog | 3 +- htdocs/product/class/product.class.php | 35 +-- htdocs/webservices/server_invoice.php | 58 ++--- .../webservices/server_productorservice.php | 189 +++++++++++++-- test/soapui/Dolibarr-soapui-project.xml | 228 +++++++++++++++++- 5 files changed, 436 insertions(+), 77 deletions(-) diff --git a/ChangeLog b/ChangeLog index 208706b57d9..e377db27912 100644 --- a/ChangeLog +++ b/ChangeLog @@ -22,12 +22,13 @@ For users: - New: Can search on part of barcode into POS module. For developers: +- New: Add webservice to get or create a prodcut or service. - Qual: Removed no more used external libraries. - Qual: Cleaned a lot of dead code. - Qual: Parent templates classes were moved as abstract classes. - Qual: Add more "hooks". - Qual: Add "canvas" feature to overwrite page of thirdparty, contact, product with yours. -- More OOP (usage of "abstract", "static", ...), unifformize constructors. +- More OOP (usage of "abstract", "static", ...), uniformize constructors. ***** ChangeLog for 3.1 compared to 3.0 ***** diff --git a/htdocs/product/class/product.class.php b/htdocs/product/class/product.class.php index fbf38befc79..cb662f912e9 100644 --- a/htdocs/product/class/product.class.php +++ b/htdocs/product/class/product.class.php @@ -45,7 +45,8 @@ class Product extends CommonObject var $id ; //! Ref var $ref; - var $libelle; + var $libelle; // TODO deprecated + var $label; var $description; //! Prix de vente var $price; // Price without tax @@ -68,14 +69,14 @@ class Product extends CommonObject var $localtax2_tx; //! Type 0 for regular product, 1 for service (Advanced feature: 2 for assembly kit, 3 for stock kit) var $type; - var $typestring; //! Stock var $stock_reel; //! Average price value for product entry into stock (PMP) var $pmp; - + //! Stock alert var $seuil_stock_alerte; + //! Duree de validite du service var $duration_value; //! Unite de duree @@ -188,9 +189,9 @@ class Product extends CommonObject /** * Insert product into database * - * @param user User making insert - * @param notrigger Disable triggers - * @return int Id of product/service if OK or number of error < 0 + * @param User $user User making insert + * @param int $notrigger Disable triggers + * @return int Id of product/service if OK or number of error < 0 */ function create($user,$notrigger=0) { @@ -255,6 +256,7 @@ class Product extends CommonObject dol_syslog("Product::Create ref=".$this->ref." price=".$this->price." price_ttc=".$this->price_ttc." tva_tx=".$this->tva_tx." price_base_type=".$this->price_base_type." Category : ".$this->catid, LOG_DEBUG); + $now=dol_now(); $this->db->begin(); @@ -287,7 +289,7 @@ class Product extends CommonObject $sql.= ", canvas"; $sql.= ", finished"; $sql.= ") VALUES ("; - $sql.= $this->db->idate(mktime()); + $sql.= $this->db->idate($now); $sql.= ", ".$conf->entity; $sql.= ", '".$this->ref."'"; $sql.= ", ".price2num($price_min_ht); @@ -357,7 +359,8 @@ class Product extends CommonObject else { // Product already exists with this ref - $langs->trans("Error")." : ".$langs->trans("ErrorProductAlreadyExists",$this->ref); + $langs->load("products"); + $this->error = $langs->transnoentitiesnoconv("ErrorProductAlreadyExists",$this->ref); } } else @@ -391,9 +394,9 @@ class Product extends CommonObject /** * Update a record into database * - * @param id Id of product - * @param user Object user making update - * @return int 1 if OK, -1 if ref already exists, -2 if other error + * @param int $id Id of product + * @param User $user Object user making update + * @return int 1 if OK, -1 if ref already exists, -2 if other error */ function update($id, $user) { @@ -1047,6 +1050,11 @@ class Product extends CommonObject $this->label = $object->label; $this->description = $object->description; $this->note = $object->note; + + $this->type = $object->fk_product_type; + $this->status = $object->tosell; + $this->status_buy = $object->tobuy; + $this->customcode = $object->customcode; $this->country_id = $object->fk_country; $this->country_code = getCountry($this->country_id,2,$this->db); @@ -1062,14 +1070,10 @@ class Product extends CommonObject $this->localtax1_tx = $object->localtax1_tx; $this->localtax2_tx = $object->localtax2_tx; - $this->type = $object->fk_product_type; - $this->status = $object->tosell; - $this->status_buy = $object->tobuy; $this->finished = $object->finished; $this->duration = $object->duration; $this->duration_value = substr($object->duration,0,dol_strlen($object->duration)-1); $this->duration_unit = substr($object->duration,-1); - $this->seuil_stock_alerte = $object->seuil_stock_alerte; $this->canvas = $object->canvas; $this->weight = $object->weight; $this->weight_units = $object->weight_units; @@ -1085,6 +1089,7 @@ class Product extends CommonObject $this->accountancy_code_buy = $object->accountancy_code_buy; $this->accountancy_code_sell= $object->accountancy_code_sell; + $this->seuil_stock_alerte = $object->seuil_stock_alerte; $this->stock_reel = $object->stock; $this->pmp = $object->pmp; diff --git a/htdocs/webservices/server_invoice.php b/htdocs/webservices/server_invoice.php index 030b8c234a2..05e8b93ad7d 100755 --- a/htdocs/webservices/server_invoice.php +++ b/htdocs/webservices/server_invoice.php @@ -246,8 +246,8 @@ $server->register( /** * Get invoice from id, ref or ref_ext * - * @param object $authentication - * + * @param array $authentication Array of authentication information + * @return array Array result */ function getInvoice($authentication,$id='',$ref='',$ref_ext='') { @@ -352,6 +352,9 @@ function getInvoice($authentication,$id='',$ref='',$ref_ext='') /** * Get list of invoices for third party + * + * @param array $authentication Array of authentication information + * @return array Array result */ function getInvoicesForThirdParty($authentication,$idthirdparty) { @@ -467,7 +470,11 @@ function getInvoicesForThirdParty($authentication,$idthirdparty) /** - * Get list of invoices for third party + * Create an invoice + * + * @param array $authentication Array of authentication information + * @param Facture $invoice Invoice + * @return array Array result */ function createInvoice($authentication,$invoice) { @@ -475,7 +482,7 @@ function createInvoice($authentication,$invoice) $now=dol_now(); - dol_syslog("Function: createInvoiceForThirdParty login=".$authentication['login']." idthirdparty=".$idthirdparty); + dol_syslog("Function: createInvoiceForThirdParty login=".$authentication['login']); if ($authentication['entity']) $conf->entity=$authentication['entity']; @@ -488,17 +495,17 @@ function createInvoice($authentication,$invoice) if (! $error) { - $newinvoice=new Facture($db); - $newinvoice->socid=$invoice['thirdparty_id']; - $newinvoice->type=$invoice['type']; - $newinvoice->ref_ext=$invoice['ref_ext']; - $newinvoice->date=$invoice['date']; - $newinvoice->date_lim_reglement=$invoice['date_due']; - $newinvoice->note=$invoice['note']; - $newinvoice->note_public=$invoice['note_public']; - $newinvoice->statut=$invoice['status']; - $newinvoice->fk_project=$invoice['project_id']; - $newinvoice->date_creation=$now; + $newobject=new Facture($db); + $newobject->socid=$invoice['thirdparty_id']; + $newobject->type=$invoice['type']; + $newobject->ref_ext=$invoice['ref_ext']; + $newobject->date=$invoice['date']; + $newobject->date_lim_reglement=$invoice['date_due']; + $newobject->note=$invoice['note']; + $newobject->note_public=$invoice['note_public']; + $newobject->statut=$invoice['status']; + $newobject->fk_project=$invoice['project_id']; + $newobject->date_creation=$now; foreach($invoice['lines'] as $line) { $newline=new FactureLigne($db); @@ -517,35 +524,32 @@ function createInvoice($authentication,$invoice) $db->begin(); - $result=$newinvoice->create($user,0,0); + $result=$newobject->create($fuser,0,0); if ($result < 0) { $error++; } - if ($newinvoice->statut == 1) // We want invoice validated + if ($newobject->statut == 1) // We want invoice validated { - $newinvoice->validate($user); + $result=$newobject->validate($fuser); + if ($result < 0) + { + $error++; + } } - $result=$newinvoice->create($user,0,0); - if ($result < 0) - { - $error++; - } - - if (! $error) { $db->commit(); - $objectresp=array('result'=>array('result_code'=>'OK', 'result_label'=>''),'id'=>$newinvoice->id,'ref'=>$newinvoice->ref); + $objectresp=array('result'=>array('result_code'=>'OK', 'result_label'=>''),'id'=>$newobject->id,'ref'=>$newobject->ref); } else { $db->rollback(); $error++; $errorcode='KO'; - $errorlabel=$newinvoice->error; + $errorlabel=$newobject->error; } } diff --git a/htdocs/webservices/server_productorservice.php b/htdocs/webservices/server_productorservice.php index 59676e34395..35c69f98352 100755 --- a/htdocs/webservices/server_productorservice.php +++ b/htdocs/webservices/server_productorservice.php @@ -89,18 +89,31 @@ $server->wsdl->addComplexType( '', array( 'id' => array('name'=>'id','type'=>'xsd:string'), - 'ref' => array('name'=>'name','type'=>'xsd:string'), + 'ref' => array('name'=>'ref','type'=>'xsd:string'), 'ref_ext' => array('name'=>'ref_ext','type'=>'xsd:string'), - 'label' => array('name'=>'label','type'=>'xsd:string'), + 'type' => array('name'=>'type','type'=>'xsd:string'), + 'label' => array('name'=>'label','type'=>'xsd:string'), 'description' => array('name'=>'description','type'=>'xsd:string'), 'date_creation' => array('name'=>'date_creation','type'=>'xsd:dateTime'), 'date_modification' => array('name'=>'date_modification','type'=>'xsd:dateTime'), 'note' => array('name'=>'note','type'=>'xsd:string'), - 'tobuy' => array('name'=>'tobuy','type'=>'xsd:string'), - 'tosell' => array('name'=>'tosell','type'=>'xsd:string'), - 'type' => array('name'=>'type','type'=>'xsd:string'), + 'status_tobuy' => array('name'=>'status_tobuy','type'=>'xsd:string'), + 'status_tosell' => array('name'=>'status_tosell','type'=>'xsd:string'), 'barcode' => array('name'=>'barcode','type'=>'xsd:string'), - 'country_id' => array('name'=>'country_id','type'=>'xsd:string') + 'barcode_type' => array('name'=>'barcode_type','type'=>'xsd:string'), + 'country_id' => array('name'=>'country_id','type'=>'xsd:string'), + 'country_code' => array('name'=>'country_code','type'=>'xsd:string'), + 'customcode' => array('name'=>'customcode','type'=>'xsd:string'), + + 'price_net' => array('name'=>'price_net','type'=>'xsd:string'), + 'price' => array('name'=>'price','type'=>'xsd:string'), + 'price_base_type' => array('name'=>'price_base_type','type'=>'xsd:string'), + + 'stock_alert' => array('name'=>'stock_alert','type'=>'xsd:string'), + 'stock_real' => array('name'=>'stock_real','type'=>'xsd:string'), + 'stock_pmp' => array('name'=>'stock_pmp','type'=>'xsd:string'), + 'canvas' => array('name'=>'canvas','type'=>'xsd:string'), + 'import_key' => array('name'=>'import_key','type'=>'xsd:string') ) ); @@ -113,19 +126,6 @@ $styledoc='rpc'; // rpc/document (document is an extend into SOAP 1.0 to s $styleuse='encoded'; // encoded/literal/literal wrapped // Better choice is document/literal wrapped but literal wrapped not supported by nusoap. -// Register WSDL -$server->register( - 'createProductOrService', - // Entry values - array('authentication'=>'tns:authentication','product'=>'tns:product'), - // Exit values - array('result'=>'tns:result','id'=>'xsd:string'), - $ns, - $ns.'#createProductOrService', - $styledoc, - $styleuse, - 'WS to create a product or service' -); // Register WSDL $server->register( @@ -141,8 +141,26 @@ $server->register( 'WS to get product or service' ); +// Register WSDL +$server->register( + 'createProductOrService', + // Entry values + array('authentication'=>'tns:authentication','product'=>'tns:product'), + // Exit values + array('result'=>'tns:result','id'=>'xsd:string'), + $ns, + $ns.'#createProductOrService', + $styledoc, + $styleuse, + 'WS to create a product or service' +); -// Full methods code + +/** + * Get produt or service + * + * @param array $authentication Array of authentication information + */ function getProductOrService($authentication,$id='',$ref='',$ref_ext='') { global $db,$conf,$langs; @@ -178,18 +196,31 @@ function getProductOrService($authentication,$id='',$ref='',$ref_ext='') 'result'=>array('result_code'=>'OK', 'result_label'=>''), 'product'=>array( 'id' => $product->id, - 'ref' => $product->name, + 'ref' => $product->ref, 'ref_ext' => $product->ref_ext, 'label' => $product->label, 'description' => $product->description, 'date_creation' => $product->date_creation, 'date_modification' => $product->date_modification, 'note' => $product->note, - 'tobuy' => $product->tobuy, - 'tosell' => $product->tosell, - 'type' => $product->type, + 'status_tosell' => $product->status, + 'status_tobuy' => $product->status_buy, + 'type' => $product->type, 'barcode' => $product->barcode, - 'country_id' => $product->country_id + 'barcode_type' => $product->barcode_type, + 'country_id' => $product->country_id>0?$product->country_id:'', + 'country_code' => $product->country_code, + 'custom_code' => $product->customcode, + + 'price_net' => $product->price, + 'price' => ($product->price_ttc-$product->price), + 'vat_rate' => $product->tva_tx, + 'price_base_type' => $product->price_base_type, + + 'stock_real' => $product->stock_reel, + 'stock_alert' => $product->seuil_stock_alerte, + 'pmp' => $product->pmp, + 'import_key' => $product->import_key )); } else @@ -214,6 +245,114 @@ function getProductOrService($authentication,$id='',$ref='',$ref_ext='') } +/** + * Create an invoice + * + * @param array $authentication Array of authentication information + * @param Product $product Product + * @return array Array result + */ +function createProductOrService($authentication,$product) +{ + global $db,$conf,$langs; + + $now=dol_now(); + + dol_syslog("Function: createProductOrService login=".$authentication['login']); + + if ($authentication['entity']) $conf->entity=$authentication['entity']; + + // Init and check authentication + $objectresp=array(); + $errorcode='';$errorlabel=''; + $error=0; + $fuser=check_authentication($authentication,$error,$errorcode,$errorlabel); + // Check parameters + if ($product['price_net'] > 0) $product['price_base_type']='HT'; + if ($product['price'] > 0) $product['price_base_type']='TTC'; + + if ($product['price_net'] > 0 && $product['price'] > 0) + { + $error++; $errorcode='KO'; $errorlabel="You must choose between price or price_net to provide price."; + } + + + if (! $error) + { + include_once(DOL_DOCUMENT_ROOT.'/lib/company.lib.php'); + + $newobject=new Product($db); + $newobject->ref=$product['ref']; + $newobject->ref_ext=$product['ref_ext']; + $newobject->type=$product['type']; + $newobject->libelle=$product['label']; // TODO deprecated + $newobject->label=$product['label']; + $newobject->description=$product['description']; + $newobject->note=$product['note']; + $newobject->status=$product['status_tosell']; + $newobject->status_buy=$product['status_tobuy']; + $newobject->price=$product['price_net']; + $newobject->price_ttc=$product['price']; + $newobject->tva_tx=$product['vat_rate']; + $newobject->price_base_type=$product['price_base_type']; + $newobject->date_creation=$now; + + $newobject->stock_reel=$product['stock_real']; + $newobject->pmp=$product['pmp']; + $newobject->seuil_stock_alert=$product['stock_alert']; + + $newobject->country_id=$product['country_id']; + if ($product['country_code']) $newobject->country_id=getCountry($product['country_code'],3); + $newobject->customcode=$product['customcode']; + + $newobject->canvas=$product['canvas']; + /*foreach($product['lines'] as $line) + { + $newline=new FactureLigne($db); + $newline->type=$line['type']; + $newline->desc=$line['desc']; + $newline->fk_product=$line['fk_product']; + $newline->total_ht=$line['total_net']; + $newline->total_vat=$line['total_vat']; + $newline->total_ttc=$line['total']; + $newline->vat=$line['vat_rate']; + $newline->qty=$line['qty']; + $newline->fk_product=$line['product_id']; + }*/ + //var_dump($product['ref_ext']); + //var_dump($product['lines'][0]['type']); + + $db->begin(); + + $result=$newobject->create($fuser,0); + if ($result <= 0) + { + $error++; + } + + if (! $error) + { + $db->commit(); + $objectresp=array('result'=>array('result_code'=>'OK', 'result_label'=>''),'id'=>$newobject->id,'ref'=>$newobject->ref); + } + else + { + $db->rollback(); + $error++; + $errorcode='KO'; + $errorlabel=$newobject->error; + } + + } + + if ($error) + { + $objectresp = array('result'=>array('result_code' => $errorcode, 'result_label' => $errorlabel)); + } + + return $objectresp; +} + // Return the results. $server->service($HTTP_RAW_POST_DATA); diff --git a/test/soapui/Dolibarr-soapui-project.xml b/test/soapui/Dolibarr-soapui-project.xml index d24736ccde3..71f1e13f5bd 100755 --- a/test/soapui/Dolibarr-soapui-project.xml +++ b/test/soapui/Dolibarr-soapui-project.xml @@ -55,16 +55,16 @@ -]]>http://schemas.xmlsoap.org/wsdl/http://localhost/dolibarrnew/webservices/server_other.php<xml-fragment/>UTF-8http://localhost/dolibarrnew/webservices/server_other.php +]]>http://schemas.xmlsoap.org/wsdl/http://localhostdolibarr/dolibarrnew/webservices/server_other.php<xml-fragment/>UTF-8http://localhostdolibarr/dolibarrnew/webservices/server_other.php - ? + dolibarrkey ? - ? - ? + admin + admin @@ -341,7 +341,7 @@ -]]>http://schemas.xmlsoap.org/wsdl/http://localhost/dolibarrnew/webservices/server_invoice.php<xml-fragment/>UTF-8http://localhost/dolibarrnew/webservices/server_invoice.php +]]>http://schemas.xmlsoap.org/wsdl/http://localhostdolibarr/dolibarrnew/webservices/server_invoice.php<xml-fragment/>UTF-8http://localhostdolibarr/dolibarrnew/webservices/server_invoice.php @@ -394,13 +394,71 @@ -]]><xml-fragment/>UTF-8http://localhost/dolibarrnew/webservices/server_invoice.php +]]>UTF-8http://localhostdolibarr/dolibarrnew/webservices/server_invoice.php + + + + + + ? + ? + ? + ? + ? + + + + ? + ? + ? + ? + ? + ? + ? + ? + ? + ? + ? + ? + ? + ? + ? + ? + ? + ? + ? + ? + ? + + + + + ? + ? + ? + ? + ? + ? + ? + ? + ? + ? + ? + ? + ? + ? + + + + + +]]><xml-fragment/>UTF-8http://localhostdolibarr/dolibarrnew/webservices/server_invoice.php - ? + dolibarrkey ? admin admin @@ -411,7 +469,7 @@ -]]><xml-fragment/>UTF-8http://localhost/dolibarrnew/webservices/server_invoice.php +]]><xml-fragment/>UTF-8http://localhostdolibarr/dolibarrnew/webservices/server_invoice.php @@ -601,4 +659,156 @@ all -]]> \ No newline at end of file +]]>http://localhostdolibarr/dolibarrnew/webservices/server_productorservice.php?wsdl + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + WS to create a product or service + + + + + WS to get product or service + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +]]>http://schemas.xmlsoap.org/wsdl/http://localhostdolibarr/dolibarrnew/webservices/server_productorservice.php<xml-fragment/>UTF-8http://localhostdolibarr/dolibarrnew/webservices/server_productorservice.php + + + + + + ? + ? + ? + ? + ? + + + + PPP + + + Description of PPP + xxxxx + 1 + 0 + 1 + 123456 + FR + 10 + + + +]]><xml-fragment/>UTF-8http://localhostdolibarr/dolibarrnew/webservices/server_productorservice.php + + + + + + dolibarrkey + ? + admin + admin + + + 1 + + + + +]]> \ No newline at end of file