Gestion des numéros de lot dans les shipments via API-REST

Bonjour,

Je rencontre un pb lors de la création de shipments via API-REST.
Tout fonctionne correctement, sauf quand je commence à vouloir intégrer les notions de numéro de lots.

Par le GUI, aucun pb, mais via l’API, des que je renseigne la valeur « detail_batch » (même vide), les lignes du shipment ne se crées plus.

J’ai même essayer d’en créer un identique à partir des données de celui fait via le GUI et même résultat.

Est ce que quelqu’un aurait une idée ? Idéalement, j’aurais souhaité passer comme variables le num de lot + la qté à expédier de ce lot là.

Merci

Infos tech complémentaires :
Dolibarr V17 et API en JSON

Bonjour,
Il faut voir quelle méthode est appeler par le GUI (avec un petit var_dump sur les paramètres) pour comprendre comment detail_batch doit être renseigné.

Bonjour,

Etant intéressé par le sujet, je me permet de mettre mes recherches ici pour faire avancer le sujet.

Voici le code de l’API :

/**
 * Create the shipment of an order
 *
 * @param int   $id       Id of the order
 * @param int	$warehouse_id Id of a warehouse
 *
 * @url     POST {id}/shipment/{warehouse_id}
 *
 * @return int
 *
 * @throws RestException 401
 * @throws RestException 404
 * @throws RestException 500 System error
 */
public function createOrderShipment($id, $warehouse_id)
{
	require_once DOL_DOCUMENT_ROOT.'/expedition/class/expedition.class.php';
	if (!DolibarrApiAccess::$user->rights->expedition->creer) {
		throw new RestException(401);
	}
	if ($warehouse_id <= 0) {
		throw new RestException(404, 'Warehouse not found');
	}
	$result = $this->commande->fetch($id);
	if (!$result) {
		throw new RestException(404, 'Order not found');
	}
	$shipment = new Expedition($this->db);
	$shipment->socid = $this->commande->socid;
	/*THOT:AMAS:24/07/2023:V1.0:Correction du bug pour lier lexpédition a la commande lors de l'appel API*/
	$shipment->origin_id = $this->commande->id;
	$shipment->origin = "commande";
	$result = $shipment->create(DolibarrApiAccess::$user);
	if ($result <= 0) {
		throw new RestException(500, 'Error on creating expedition :'.$this->db->lasterror());
	}


	foreach ($this->commande->lines as $line) {
		$result = $shipment->create_line($warehouse_id, $line->id, $line->qty);
		if ($result <= 0) {
			throw new RestException(500, 'Error on creating expedition lines:'.$this->db->lasterror());
		}
	}	
	return $shipment->id;
}

Elle appelle la fonction create_line

// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
/**
 * Create a expedition line
 *
 * @param 	int		$entrepot_id		Id of warehouse
 * @param 	int		$origin_line_id		Id of source line
 * @param 	int		$qty				Quantity
 * @param 	int		$rang				Rang
 * @param	array	$array_options		extrafields array
 * @return	int							<0 if KO, line_id if OK
 */
public function create_line($entrepot_id, $origin_line_id, $qty, $rang = 0, $array_options = null)
{
	//phpcs:enable
	global $user;

	$expeditionline = new ExpeditionLigne($this->db);
	$expeditionline->fk_expedition = $this->id;
	$expeditionline->entrepot_id = $entrepot_id;
	$expeditionline->fk_origin_line = $origin_line_id;
	$expeditionline->qty = $qty;
	$expeditionline->rang = $rang;
	$expeditionline->array_options = $array_options;

	if (($lineId = $expeditionline->insert($user)) < 0) {
		$this->errors[] = $expeditionline->error;
	}
	return $lineId;
}

Je pense que l’on devrait appeler la fonction create_line_batch avec l’API

// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
/**
* Create the detail of the expedition line. Create 1 record into expeditiondet for each warehouse and n record for each lot in this warehouse into expeditiondet_batch.
*
* @param object $line_ext Objet with full information of line. $line_ext->detail_batch must be an array of ExpeditionLineBatch
* @param array $array_options extrafields array
* @return int <0 if KO, >0 if OK
*/
public function create_line_batch($line_ext, $array_options = 0)
{
// phpcs:enable
$error = 0;
$stockLocationQty = array(); // associated array with batch qty in stock location

	$tab = $line_ext->detail_batch;
	// create stockLocation Qty array
	foreach ($tab as $detbatch) {
		if ($detbatch->entrepot_id) {
			$stockLocationQty[$detbatch->entrepot_id] += $detbatch->qty;
		}
	}
	// create shipment lines
	foreach ($stockLocationQty as $stockLocation => $qty) {
		$line_id = $this->create_line($stockLocation, $line_ext->origin_line_id, $qty, $line_ext->rang, $array_options);
		if ($line_id < 0) {
			$error++;
		} else {
			// create shipment batch lines for stockLocation
			foreach ($tab as $detbatch) {
				if ($detbatch->entrepot_id == $stockLocation) {
					if (!($detbatch->create($line_id) > 0)) {		// Create an ExpeditionLineBatch
						$error++;
					}
				}
			}
		}
	}

	if (!$error) {
		return 1;
	} else {
		return -1;
	}
}

J’ai fait un petit var_dump de la variable $shipment pour chercher des infos mais il semble ne rien avoir sur le lot… (fichier joint)
var_dump.php (21,6 Ko)

Bref je suis bloqué la !

Est-ce que qqn pourrait nous aider à débloquer le sujet ?

Mille merci par avance !

Alexandre