Merge pull request #19926 from aspangaro/16a9

NEW API Proposals - Add POST lines
This commit is contained in:
Laurent Destailleur 2022-01-26 12:06:33 +01:00 committed by GitHub
commit eb336c81a1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,7 +1,9 @@
<?php
/* Copyright (C) 2015 Jean-François Ferry <jfefe@aternatik.fr>
* Copyright (C) 2016 Laurent Destailleur <eldy@users.sourceforge.net>
* Copyright (C) 2020 Thibault FOUCART <support@ptibogxiv.net>
/* Copyright (C) 2015 Jean-François Ferry <jfefe@aternatik.fr>
* Copyright (C) 2016 Laurent Destailleur <eldy@users.sourceforge.net>
* Copyright (C) 2020 Thibault FOUCART <support@ptibogxiv.net>
* Copyright (C) 2022 ATM Consulting <contact@atm-consulting.fr>
* Copyright (C) 2022 OpenDSI <support@open-dsi.fr>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -320,7 +322,7 @@ class Proposals extends DolibarrApi
* @param int $id Id of commercial proposal to update
* @param array $request_data Commercial proposal line data
*
* @url POST {id}/lines
* @url POST {id}/line
*
* @return int
*/
@ -380,6 +382,84 @@ class Proposals extends DolibarrApi
}
}
/**
* Add lines to given commercial proposal
*
* @param int $id Id of commercial proposal to update
* @param array $request_data Commercial proposal line data
*
* @url POST {id}/lines
*
* @return int
*/
public function postLines($id, $request_data = null)
{
if (!DolibarrApiAccess::$user->rights->propal->creer) {
throw new RestException(401);
}
$result = $this->propal->fetch($id);
if (!$result) {
throw new RestException(404, 'Commercial Proposal not found');
}
if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
}
$errors = [];
$this->db->begin();
foreach ($request_data as $TData) {
if (empty($TData[0])) $TData = array($TData);
foreach ($TData as $lineData) {
$line = (object) $lineData;
$updateRes = $this->propal->addline(
$line->desc,
$line->subprice,
$line->qty,
$line->tva_tx,
$line->localtax1_tx,
$line->localtax2_tx,
$line->fk_product,
$line->remise_percent,
'HT',
0,
$line->info_bits,
$line->product_type,
$line->rang,
$line->special_code,
$line->fk_parent_line,
$line->fk_fournprice,
$line->pa_ht,
$line->label,
$line->date_start,
$line->date_end,
$line->array_options,
$line->fk_unit,
$line->origin,
$line->origin_id,
$line->multicurrency_subprice,
$line->fk_remise_except
);
if ($updateRes < 0) {
$errors['lineLabel'] = $line->label;
$errors['msg'] = $this->propal->errors;
}
}
}
if (empty($errors)) {
$this->db->commit();
return count($request_data);
} else {
$this->db->rollback();
throw new RestException(400, implode(", ", $errors));
}
}
/**
* Update a line of given commercial proposal
*