Skip to content

Commit

Permalink
Debug API for prices of products
Browse files Browse the repository at this point in the history
  • Loading branch information
eldy committed Jan 14, 2018
1 parent 133e87f commit 3c3402b
Show file tree
Hide file tree
Showing 5 changed files with 162 additions and 16 deletions.
3 changes: 3 additions & 0 deletions htdocs/api/index.php
Expand Up @@ -79,6 +79,9 @@
}


// This 2 lines are usefull only if we want to exclude some Urls from the explorer
//use Luracast\Restler\Explorer;
//Explorer::$excludedPaths = array('/categories');


// Analyze URLs
Expand Down
6 changes: 3 additions & 3 deletions htdocs/core/class/html.form.class.php
Expand Up @@ -1931,11 +1931,11 @@ function select_produits_list($selected='',$htmlname='productid',$filtertype='',
if (! empty($conf->global->PRODUIT_CUSTOMER_PRICES_BY_QTY))
{
$sql.= ", (SELECT pp.rowid FROM ".MAIN_DB_PREFIX."product_price as pp WHERE pp.fk_product = p.rowid";
if ($price_level >= 1 && !empty($conf->global->PRODUIT_MULTIPRICES)) $sql.= " AND price_level=".$price_level;
if ($price_level >= 1 && !empty($conf->global->PRODUIT_CUSTOMER_PRICES_BY_QTY_MULTIPRICES)) $sql.= " AND price_level=".$price_level;
$sql.= " ORDER BY date_price";
$sql.= " DESC LIMIT 1) as price_rowid";
$sql.= ", (SELECT pp.price_by_qty FROM ".MAIN_DB_PREFIX."product_price as pp WHERE pp.fk_product = p.rowid";
if ($price_level >= 1 && !empty($conf->global->PRODUIT_MULTIPRICES)) $sql.= " AND price_level=".$price_level;
$sql.= ", (SELECT pp.price_by_qty FROM ".MAIN_DB_PREFIX."product_price as pp WHERE pp.fk_product = p.rowid"; // price_by_qty is 1 if some prices by qty exists in subtable
if ($price_level >= 1 && !empty($conf->global->PRODUIT_CUSTOMER_PRICES_BY_QTY_MULTIPRICES)) $sql.= " AND price_level=".$price_level;
$sql.= " ORDER BY date_price";
$sql.= " DESC LIMIT 1) as price_by_qty";
$selectFields.= ", price_rowid, price_by_qty";
Expand Down
2 changes: 1 addition & 1 deletion htdocs/product/admin/product.php
Expand Up @@ -55,7 +55,7 @@
'PRODUIT_CUSTOMER_PRICES'=>$langs->trans('PriceByCustomer'), // Different price for each customer
);
$keyforparam='PRODUIT_CUSTOMER_PRICES_BY_QTY';
if ($conf->global->MAIN_FEATURES_LEVEL >= 2 || ! empty($conf->global->$keyforparam)) $select_pricing_rules['PRODUIT_CUSTOMER_PRICES_BY_QTY'] = $langs->trans('PriceByQuantity').' ('.$langs->trans("VersionExperimental").')'; // TODO If this is enabled, price must be hidden when price by qty is enabled, also price for quantity must be used when adding product into order/propal/invoice
if ($conf->global->MAIN_FEATURES_LEVEL >= 1 || ! empty($conf->global->$keyforparam)) $select_pricing_rules['PRODUIT_CUSTOMER_PRICES_BY_QTY'] = $langs->trans('PriceByQuantity').' ('.$langs->trans("VersionExperimental").')'; // TODO If this is enabled, price must be hidden when price by qty is enabled, also price for quantity must be used when adding product into order/propal/invoice
$keyforparam='PRODUIT_CUSTOMER_PRICES_BY_QTY_MULTIPRICES';
if ($conf->global->MAIN_FEATURES_LEVEL >= 2 || ! empty($conf->global->$keyforparam)) $select_pricing_rules['PRODUIT_CUSTOMER_PRICES_BY_QTY_MULTIPRICES'] = $langs->trans('MultiPricesAbility') . '+' . $langs->trans('PriceByQuantity').' ('.$langs->trans("VersionExperimental").')';

Expand Down
147 changes: 141 additions & 6 deletions htdocs/product/class/api_products.class.php
Expand Up @@ -54,15 +54,18 @@ function __construct()
/**
* Get properties of a product object
*
* Return an array with product informations
* Return an array with product information.
* TODO implement getting a product by ref or by $ref_ext
*
* @param int $id ID of product
* @param int $id ID of product
* @param int $includestockdata Load also information about stock (slower)
* @return array|mixed data without useless information
*
* @throws RestException
* TODO implement getting a product by ref or by $ref_ext
* @throws RestException
* @throws 401
* @throws 404
*/
function get($id)
function get($id, $includestockdata=0)
{
if(! DolibarrApiAccess::$user->rights->produit->lire) {
throw new RestException(401);
Expand All @@ -77,7 +80,10 @@ function get($id)
throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
}

$this->product->load_stock();
if ($includestockdata)
{
$this->product->load_stock();
}

return $this->_cleanObjectDatas($this->product);
}
Expand Down Expand Up @@ -196,6 +202,10 @@ function post($request_data = NULL)
* @param int $id Id of product to update
* @param array $request_data Datas
* @return int
*
* @throws RestException
* @throws 401
* @throws 404
*/
function put($id, $request_data = NULL)
{
Expand Down Expand Up @@ -250,6 +260,7 @@ function delete($id)
return $this->product->delete(DolibarrApiAccess::$user);
}


/**
* Get categories for a product
*
Expand Down Expand Up @@ -284,6 +295,121 @@ function getCategories($id, $sortfield = "s.rowid", $sortorder = 'ASC', $limit =
return $result;
}

/**
* Get prices per segment for a product
*
* @param int $id ID of product
*
* @return mixed
*
* @url GET {id}/selling_multiprices/per_segment
*/
function getCustomerPricesPerSegment($id)
{
global $conf;

if (! DolibarrApiAccess::$user->rights->produit->lire) {
throw new RestException(401);
}

if (empty($conf->global->PRODUIT_MULTIPRICES))
{
throw new RestException(400, 'API not available: this mode of pricing is not enabled by setup');
}

$result = $this->product->fetch($id);
if ( ! $result ) {
throw new RestException(404, 'Product not found');
}

if ($result < 0) {
throw new RestException(503, 'Error when retrieve prices list : '.$categories->error);
}

return array(
'multiprices'=>$this->product->multiprices,
'multiprices_inc_tax'=>$this->product->multiprices_ttc,
'multiprices_min'=>$this->product->multiprices_min,
'multiprices_min_inc_tax'=>$this->product->multiprices_min_ttc,
'multiprices_vat'=>$this->product->multiprices_tva_tx,
'multiprices_base_type'=>$this->product->multiprices_base_type,
//'multiprices_default_vat_code'=>$this->product->multiprices_default_vat_code
);
}

/**
* Get prices per customer for a product
*
* @param int $id ID of product
*
* @return mixed
*
* @url GET {id}/selling_multiprices/per_customer
*/
function getCustomerPricesPerCustomer($id)
{
global $conf;

if (! DolibarrApiAccess::$user->rights->produit->lire) {
throw new RestException(401);
}

if (empty($conf->global->PRODUIT_CUSTOMER_PRICES))
{
throw new RestException(400, 'API not available: this mode of pricing is not enabled by setup');
}

$result = $this->product->fetch($id);
if ( ! $result ) {
throw new RestException(404, 'Product not found');
}

if ($result < 0) {
throw new RestException(503, 'Error when retrieve prices list : '.$categories->error);
}

throw new RestException(501, 'Feature not yet available');
//return $result;
}

/**
* Get prices per quantity for a product
*
* @param int $id ID of product
*
* @return mixed
*
* @url GET {id}/selling_multiprices/per_quantity
*/
function getCustomerPricesPerQuantity($id)
{
global $conf;

if (! DolibarrApiAccess::$user->rights->produit->lire) {
throw new RestException(401);
}

if (empty($conf->global->PRODUIT_CUSTOMER_PRICES_BY_QTY))
{
throw new RestException(400, 'API not available: this mode of pricing is not enabled by setup');
}

$result = $this->product->fetch($id);
if ( ! $result ) {
throw new RestException(404, 'Product not found');
}

if ($result < 0) {
throw new RestException(503, 'Error when retrieve prices list : '.$categories->error);
}

return array(
'prices_by_qty'=>$this->product->prices_by_qty[0], // 1 if price by quantity was activated for the product
'prices_by_qty_list'=>$this->product->prices_by_qty_list[0]
);
}


/**
* Clean sensible object datas
*
Expand All @@ -295,6 +421,15 @@ function _cleanObjectDatas($object) {
$object = parent::_cleanObjectDatas($object);

unset($object->regeximgext);
unset($object->price_by_qty);
unset($object->prices_by_qty_id);
unset($object->libelle);
unset($object->product_id_already_linked);

unset($object->name);
unset($object->firstname);
unset($object->lastname);
unset($object->civility_id);

return $object;
}
Expand Down
20 changes: 14 additions & 6 deletions htdocs/product/class/product.class.php
Expand Up @@ -1963,7 +1963,7 @@ function fetch($id='', $ref='', $ref_ext='', $ignore_expression=0)
if (! empty($conf->global->MAIN_MULTILANGS)) $this->getMultiLangs();

// Load multiprices array
if (! empty($conf->global->PRODUIT_MULTIPRICES))
if (! empty($conf->global->PRODUIT_MULTIPRICES)) // prices per segment
{
for ($i=1; $i <= $conf->global->PRODUIT_MULTIPRICES_LIMIT; $i++)
{
Expand All @@ -1990,6 +1990,7 @@ function fetch($id='', $ref='', $ref_ext='', $ignore_expression=0)
$this->multiprices_recuperableonly[$i]=$result["recuperableonly"];

// Price by quantity
/*
$this->prices_by_qty[$i]=$result["price_by_qty"];
$this->prices_by_qty_id[$i]=$result["rowid"];
// Récuperation de la liste des prix selon qty si flag positionné
Expand Down Expand Up @@ -2022,7 +2023,7 @@ function fetch($id='', $ref='', $ref_ext='', $ignore_expression=0)
dol_print_error($this->db);
return -1;
}
}
}*/
}
else
{
Expand All @@ -2031,7 +2032,11 @@ function fetch($id='', $ref='', $ref_ext='', $ignore_expression=0)
}
}
}
else if (! empty($conf->global->PRODUIT_CUSTOMER_PRICES_BY_QTY))
elseif (! empty($conf->global->PRODUIT_CUSTOMER_PRICES)) // prices per customers
{
// Nothing loaded by default. List may be very long.
}
else if (! empty($conf->global->PRODUIT_CUSTOMER_PRICES_BY_QTY)) // prices per quantity
{
$sql = "SELECT price, price_ttc, price_min, price_min_ttc,";
$sql.= " price_base_type, tva_tx, default_vat_code, tosell, price_by_qty, rowid";
Expand Down Expand Up @@ -2066,7 +2071,7 @@ function fetch($id='', $ref='', $ref_ext='', $ignore_expression=0)
$resultat[$ii]["unitprice"]= $result["unitprice"];
$resultat[$ii]["quantity"]= $result["quantity"];
$resultat[$ii]["remise_percent"]= $result["remise_percent"];
$resultat[$ii]["remise"]= $result["remise"]; // deprecated
//$resultat[$ii]["remise"]= $result["remise"]; // deprecated
$resultat[$ii]["price_base_type"]= $result["price_base_type"];
$ii++;
}
Expand All @@ -2085,6 +2090,10 @@ function fetch($id='', $ref='', $ref_ext='', $ignore_expression=0)
return -1;
}
}
else if (! empty($conf->global->PRODUIT_CUSTOMER_PRICES_BY_QTY_MULTIPRICES)) // prices per customer and quantity
{
// Not yet implemented
}

if (!empty($conf->dynamicprices->enabled) && !empty($this->fk_price_expression) && empty($ignore_expression))
{
Expand All @@ -2101,8 +2110,7 @@ function fetch($id='', $ref='', $ref_ext='', $ignore_expression=0)
}

// We should not load stock during the fetch. If someone need stock of product, he must call load_stock after fetching product.
//$res=$this->load_stock();
// instead we just init the stock_warehouse array
// Instead we just init the stock_warehouse array
$this->stock_warehouse = array();

return 1;
Expand Down

0 comments on commit 3c3402b

Please sign in to comment.