NEW New REST API explorer. Can create invoice and orders with lines.

This commit is contained in:
Laurent Destailleur 2016-07-29 02:28:51 +02:00
parent 9ca7f2cb66
commit 4e8e7f8e05
6 changed files with 100 additions and 122 deletions

View File

@ -86,8 +86,11 @@ class DolibarrApi
function _cleanObjectDatas($object) { function _cleanObjectDatas($object) {
// Remove $db object property for object // Remove $db object property for object
unset($object->db); unset($object->db);
// Remove linkedObjects. We should already have linkedObjectIds that avoid huge responses
unset($object->linkedObjects);
// Remove the $oldcopy property because it is not supported by the JSON // Remove the $oldcopy property because it is not supported by the JSON
// encoder. The following error is generated when trying to serialize // encoder. The following error is generated when trying to serialize
// it: "Error encoding/decoding JSON: Type is not supported" // it: "Error encoding/decoding JSON: Type is not supported"
@ -109,13 +112,15 @@ class DolibarrApi
} }
// If object has linked objects, remove $db property // If object has linked objects, remove $db property
/*
if(isset($object->linkedObjects) && count($object->linkedObjects) > 0) { if(isset($object->linkedObjects) && count($object->linkedObjects) > 0) {
foreach($object->linkedObjects as $type_object => $linked_object) { foreach($object->linkedObjects as $type_object => $linked_object) {
foreach($linked_object as $object2clean) { foreach($linked_object as $object2clean) {
$this->_cleanObjectDatas($object2clean); $this->_cleanObjectDatas($object2clean);
} }
} }
} }*/
return $object; return $object;
} }

View File

@ -156,7 +156,7 @@ class Orders extends DolibarrApi
throw new RestException(503, 'Error when retrieve commande list'); throw new RestException(503, 'Error when retrieve commande list');
} }
if( ! count($obj_ret)) { if( ! count($obj_ret)) {
throw new RestException(404, 'No commande found'); throw new RestException(404, 'No order found');
} }
return $obj_ret; return $obj_ret;
} }
@ -178,15 +178,16 @@ class Orders extends DolibarrApi
foreach($request_data as $field => $value) { foreach($request_data as $field => $value) {
$this->commande->$field = $value; $this->commande->$field = $value;
} }
if (isset($request_data["lines"])) { /*if (isset($request_data["lines"])) {
$lines = array(); $lines = array();
foreach ($request_data["lines"] as $line) { foreach ($request_data["lines"] as $line) {
array_push($lines, (object) $line); array_push($lines, (object) $line);
} }
$this->commande->lines = $lines; $this->commande->lines = $lines;
} }*/
if(! $this->commande->create(DolibarrApiAccess::$user) ) { if ($this->commande->create(DolibarrApiAccess::$user) <= 0) {
throw new RestException(500, "Error while creating order"); $errormsg = $this->commande->error;
throw new RestException(500, $errormsg ? $errormsg : "Error while creating order");
} }
return $this->commande->id; return $this->commande->id;
@ -208,7 +209,7 @@ class Orders extends DolibarrApi
$result = $this->commande->fetch($id); $result = $this->commande->fetch($id);
if( ! $result ) { if( ! $result ) {
throw new RestException(404, 'Commande not found'); throw new RestException(404, 'Order not found');
} }
if( ! DolibarrApi::_checkAccessToResource('commande',$this->commande->id)) { if( ! DolibarrApi::_checkAccessToResource('commande',$this->commande->id)) {
@ -239,7 +240,7 @@ class Orders extends DolibarrApi
$result = $this->commande->fetch($id); $result = $this->commande->fetch($id);
if( ! $result ) { if( ! $result ) {
throw new RestException(404, 'Commande not found'); throw new RestException(404, 'Order not found');
} }
if( ! DolibarrApi::_checkAccessToResource('commande',$this->commande->id)) { if( ! DolibarrApi::_checkAccessToResource('commande',$this->commande->id)) {

View File

@ -693,8 +693,8 @@ class Commande extends CommonOrder
* Note that this->ref can be set or empty. If empty, we will use "(PROV)" * Note that this->ref can be set or empty. If empty, we will use "(PROV)"
* *
* @param User $user Objet user that make creation * @param User $user Objet user that make creation
* @param int $notrigger Disable all triggers * @param int $notrigger Disable all triggers
* @return int <0 if KO, >0 if OK * @return int <0 if KO, >0 if OK
*/ */
function create($user, $notrigger=0) function create($user, $notrigger=0)
{ {
@ -808,37 +808,43 @@ class Commande extends CommonOrder
*/ */
for ($i=0;$i<$num;$i++) for ($i=0;$i<$num;$i++)
{ {
$line = $this->lines[$i];
// Test and convert into object this->lines[$i]. When coming from REST API, we may still have an array
//if (! is_object($line)) $line=json_decode(json_encode($line), FALSE); // convert recursively array into object.
if (! is_object($line)) $line = (object) $line;
// Reset fk_parent_line for no child products and special product // Reset fk_parent_line for no child products and special product
if (($this->lines[$i]->product_type != 9 && empty($this->lines[$i]->fk_parent_line)) || $this->lines[$i]->product_type == 9) { if (($line->product_type != 9 && empty($line->fk_parent_line)) || $line->product_type == 9) {
$fk_parent_line = 0; $fk_parent_line = 0;
} }
$result = $this->addline( $result = $this->addline(
$this->lines[$i]->desc, $line->desc,
$this->lines[$i]->subprice, $line->subprice,
$this->lines[$i]->qty, $line->qty,
$this->lines[$i]->tva_tx, $line->tva_tx,
$this->lines[$i]->localtax1_tx, $line->localtax1_tx,
$this->lines[$i]->localtax2_tx, $line->localtax2_tx,
$this->lines[$i]->fk_product, $line->fk_product,
$this->lines[$i]->remise_percent, $line->remise_percent,
$this->lines[$i]->info_bits, $line->info_bits,
$this->lines[$i]->fk_remise_except, $line->fk_remise_except,
'HT', 'HT',
0, 0,
$this->lines[$i]->date_start, $line->date_start,
$this->lines[$i]->date_end, $line->date_end,
$this->lines[$i]->product_type, $line->product_type,
$this->lines[$i]->rang, $line->rang,
$this->lines[$i]->special_code, $line->special_code,
$fk_parent_line, $fk_parent_line,
$this->lines[$i]->fk_fournprice, $line->fk_fournprice,
$this->lines[$i]->pa_ht, $line->pa_ht,
$this->lines[$i]->label, $line->label,
$this->lines[$i]->array_options, $line->array_options,
$this->lines[$i]->fk_unit, $line->fk_unit,
$this->element, $this->element,
$this->lines[$i]->id $line->id
); );
if ($result < 0) if ($result < 0)
{ {
@ -851,7 +857,7 @@ class Commande extends CommonOrder
return -1; return -1;
} }
// Defined the new fk_parent_line // Defined the new fk_parent_line
if ($result > 0 && $this->lines[$i]->product_type == 9) { if ($result > 0 && $line->product_type == 9) {
$fk_parent_line = $result; $fk_parent_line = $result;
} }
} }

View File

@ -2520,58 +2520,6 @@ if ($action == 'create')
print '<tr><td>' . $langs->trans('MulticurrencyTotalTTC') . '</td><td colspan="2">' . price($objectsrc->multicurrency_total_ttc) . "</td></tr>"; print '<tr><td>' . $langs->trans('MulticurrencyTotalTTC') . '</td><td colspan="2">' . price($objectsrc->multicurrency_total_ttc) . "</td></tr>";
} }
} }
else
{
// Show deprecated optional form to add product line here
if (! empty($conf->global->PRODUCT_SHOW_WHEN_CREATE)) {
print '<tr><td colspan="3">';
// Zone de choix des produits predefinis a la creation
print '<table class="noborder" width="100%">';
print '<tr>';
print '<td>' . $langs->trans('ProductsAndServices') . '</td>';
print '<td>' . $langs->trans('Qty') . '</td>';
print '<td>' . $langs->trans('ReductionShort') . '</td>';
print '<td> &nbsp; &nbsp; </td>';
if (! empty($conf->service->enabled)) {
print '<td>' . $langs->trans('ServiceLimitedDuration') . '</td>';
}
print '</tr>';
for($i = 1; $i <= $NBLINES; $i ++) {
print '<tr>';
print '<td>';
// multiprix
if (! empty($conf->global->PRODUIT_MULTIPRICES))
$form->select_produits('', 'idprod' . $i, '', $conf->product->limit_size, $soc->price_level);
else
$form->select_produits('', 'idprod' . $i, '', $conf->product->limit_size);
print '</td>';
print '<td><input type="text" size="2" name="qty' . $i . '" value="1"></td>';
print '<td class="nowrap"><input type="text" size="1" name="remise_percent' . $i . '" value="' . $soc->remise_percent . '">%</td>';
print '<td>&nbsp;</td>';
// Si le module service est actif, on propose des dates de debut et fin a la ligne
if (! empty($conf->service->enabled)) {
print '<td class="nowrap">';
print '<table class="nobordernopadding"><tr class="nocellnopadd">';
print '<td class="nobordernopadding nowrap">';
print $langs->trans('From') . ' ';
print '</td><td class="nobordernopadding nowrap">';
print $form->select_date('', 'date_start' . $i, $usehm, $usehm, 1, "add", 1, 0, 1);
print '</td></tr>';
print '<td class="nobordernopadding nowrap">';
print $langs->trans('to') . ' ';
print '</td><td class="nobordernopadding nowrap">';
print $form->select_date('', 'date_end' . $i, $usehm, $usehm, 1, "add", 1, 0, 1);
print '</td></tr></table>';
print '</td>';
}
print "</tr>\n";
}
print '</table>';
print '</td></tr>';
}
}
print "</table>\n"; print "</table>\n";

View File

@ -68,7 +68,7 @@ class Invoices extends DolibarrApi
$result = $this->invoice->fetch($id); $result = $this->invoice->fetch($id);
if( ! $result ) { if( ! $result ) {
throw new RestException(404, 'Facture not found'); throw new RestException(404, 'Invoice not found');
} }
if( ! DolibarrApi::_checkAccessToResource('facture',$this->invoice->id)) { if( ! DolibarrApi::_checkAccessToResource('facture',$this->invoice->id)) {
@ -128,6 +128,7 @@ class Invoices extends DolibarrApi
$sql .= " AND sc.fk_user = ".$search_sale; $sql .= " AND sc.fk_user = ".$search_sale;
} }
// TODO remove this, useless for WS
$nbtotalofrecords = 0; $nbtotalofrecords = 0;
if (empty($conf->global->MAIN_DISABLE_FULL_SCANLIST)) if (empty($conf->global->MAIN_DISABLE_FULL_SCANLIST))
{ {
@ -178,7 +179,7 @@ class Invoices extends DolibarrApi
function post($request_data = NULL) function post($request_data = NULL)
{ {
if(! DolibarrApiAccess::$user->rights->facture->creer) { if(! DolibarrApiAccess::$user->rights->facture->creer) {
throw new RestException(401); throw new RestException(401, "Insuffisant rights");
} }
// Check mandatory fields // Check mandatory fields
$result = $this->_validate($request_data); $result = $this->_validate($request_data);
@ -189,8 +190,18 @@ class Invoices extends DolibarrApi
if(! array_keys($request_data,'date')) { if(! array_keys($request_data,'date')) {
$this->invoice->date = dol_now(); $this->invoice->date = dol_now();
} }
if( ! $this->invoice->create(DolibarrApiAccess::$user)) { /* We keep lines as an array
throw new RestException(500); if (isset($request_data["lines"])) {
$lines = array();
foreach ($request_data["lines"] as $line) {
array_push($lines, (object) $line);
}
$this->invoice->lines = $lines;
}*/
if ($this->invoice->create(DolibarrApiAccess::$user) <= 0) {
$errormsg = $this->invoice->error;
throw new RestException(500, $errormsg ? $errormsg : "Error while creating order");
} }
return $this->invoice->id; return $this->invoice->id;
} }
@ -210,7 +221,7 @@ class Invoices extends DolibarrApi
$result = $this->invoice->fetch($id); $result = $this->invoice->fetch($id);
if( ! $result ) { if( ! $result ) {
throw new RestException(404, 'Facture not found'); throw new RestException(404, 'Invoice not found');
} }
if( ! DolibarrApi::_checkAccessToResource('facture',$this->invoice->id)) { if( ! DolibarrApi::_checkAccessToResource('facture',$this->invoice->id)) {
@ -240,7 +251,7 @@ class Invoices extends DolibarrApi
} }
$result = $this->invoice->fetch($id); $result = $this->invoice->fetch($id);
if( ! $result ) { if( ! $result ) {
throw new RestException(404, 'Facture not found'); throw new RestException(404, 'Invoice not found');
} }
if( ! DolibarrApi::_checkAccessToResource('facture',$this->facture->id)) { if( ! DolibarrApi::_checkAccessToResource('facture',$this->facture->id)) {

View File

@ -449,7 +449,7 @@ class Facture extends CommonInvoice
/* /*
* Insert lines of invoices into database * Insert lines of invoices into database
*/ */
if (count($this->lines) && is_object($this->lines[0])) // If this->lines is array on InvoiceLines (preferred mode) if (count($this->lines) && is_object($this->lines[0])) // If this->lines is array of InvoiceLines (preferred mode)
{ {
$fk_parent_line = 0; $fk_parent_line = 0;
@ -483,49 +483,56 @@ class Facture extends CommonInvoice
} }
} }
} }
else // If this->lines is not object of invoice lines else // If this->lines is an array of invoice line arrays
{ {
$fk_parent_line = 0; $fk_parent_line = 0;
dol_syslog("There is ".count($this->lines)." lines that are array lines"); dol_syslog("There is ".count($this->lines)." lines that are array lines");
foreach ($this->lines as $i => $val) foreach ($this->lines as $i => $val)
{ {
if (($this->lines[$i]->info_bits & 0x01) == 0) // We keep only lines with first bit = 0 $line = $this->lines[$i];
// Test and convert into object this->lines[$i]. When coming from REST API, we may still have an array
//if (! is_object($line)) $line=json_decode(json_encode($line), FALSE); // convert recursively array into object.
if (! is_object($line)) $line = (object) $line;
if (($line->info_bits & 0x01) == 0) // We keep only lines with first bit = 0
{ {
// Reset fk_parent_line for no child products and special product // Reset fk_parent_line for no child products and special product
if (($this->lines[$i]->product_type != 9 && empty($this->lines[$i]->fk_parent_line)) || $this->lines[$i]->product_type == 9) { if (($line->product_type != 9 && empty($line->fk_parent_line)) || $line->product_type == 9) {
$fk_parent_line = 0; $fk_parent_line = 0;
} }
$result = $this->addline( $result = $this->addline(
$this->lines[$i]->desc, $line->desc,
$this->lines[$i]->subprice, $line->subprice,
$this->lines[$i]->qty, $line->qty,
$this->lines[$i]->tva_tx, $line->tva_tx,
$this->lines[$i]->localtax1_tx, $line->localtax1_tx,
$this->lines[$i]->localtax2_tx, $line->localtax2_tx,
$this->lines[$i]->fk_product, $line->fk_product,
$this->lines[$i]->remise_percent, $line->remise_percent,
$this->lines[$i]->date_start, $line->date_start,
$this->lines[$i]->date_end, $line->date_end,
$this->lines[$i]->fk_code_ventilation, $line->fk_code_ventilation,
$this->lines[$i]->info_bits, $line->info_bits,
$this->lines[$i]->fk_remise_except, $line->fk_remise_except,
'HT', 'HT',
0, 0,
$this->lines[$i]->product_type, $line->product_type,
$this->lines[$i]->rang, $line->rang,
$this->lines[$i]->special_code, $line->special_code,
$this->element, $this->element,
$this->lines[$i]->id, $line->id,
$fk_parent_line, $fk_parent_line,
$this->lines[$i]->fk_fournprice, $line->fk_fournprice,
$this->lines[$i]->pa_ht, $line->pa_ht,
$this->lines[$i]->label, $line->label,
$this->lines[$i]->array_options, $line->array_options,
$this->lines[$i]->situation_percent, $line->situation_percent,
$this->lines[$i]->fk_prev_id, $line->fk_prev_id,
$this->lines[$i]->fk_unit $line->fk_unit
); );
if ($result < 0) if ($result < 0)
{ {
@ -536,7 +543,7 @@ class Facture extends CommonInvoice
} }
// Defined the new fk_parent_line // Defined the new fk_parent_line
if ($result > 0 && $this->lines[$i]->product_type == 9) { if ($result > 0 && $line->product_type == 9) {
$fk_parent_line = $result; $fk_parent_line = $result;
} }
} }