NEW: add "productpricecard" hook and uniformize code

This commit is contained in:
Regis Houssin 2015-06-10 18:42:16 +02:00
parent 7f06727c56
commit e43d4a6239

View File

@ -2,7 +2,7 @@
/* Copyright (C) 2001-2007 Rodolphe Quiedeville <rodolphe@quiedeville.org> /* Copyright (C) 2001-2007 Rodolphe Quiedeville <rodolphe@quiedeville.org>
* Copyright (C) 2004-2014 Laurent Destailleur <eldy@users.sourceforge.net> * Copyright (C) 2004-2014 Laurent Destailleur <eldy@users.sourceforge.net>
* Copyright (C) 2005 Eric Seigne <eric.seigne@ryxeo.com> * Copyright (C) 2005 Eric Seigne <eric.seigne@ryxeo.com>
* Copyright (C) 2005-2013 Regis Houssin <regis.houssin@capnetworks.com> * Copyright (C) 2005-2015 Regis Houssin <regis.houssin@capnetworks.com>
* Copyright (C) 2006 Andre Cianfarani <acianfa@free.fr> * Copyright (C) 2006 Andre Cianfarani <acianfa@free.fr>
* Copyright (C) 2014 Florian Henry <florian.henry@open-concept.pro> * Copyright (C) 2014 Florian Henry <florian.henry@open-concept.pro>
* Copyright (C) 2014 Juanjo Menent <jmenent@2byte.es> * Copyright (C) 2014 Juanjo Menent <jmenent@2byte.es>
@ -43,9 +43,12 @@ if (! empty($conf->global->PRODUIT_CUSTOMER_PRICES)) {
$langs->load("products"); $langs->load("products");
$langs->load("bills"); $langs->load("bills");
$mesg=''; $error=0; $errors=array(); $_error=0;
$id = GETPOST('id', 'int'); $id = GETPOST('id', 'int');
$ref = GETPOST('ref', 'alpha'); $ref = GETPOST('ref', 'alpha');
$action = GETPOST('action', 'alpha'); $action = GETPOST('action', 'alpha');
$cancel = GETPOST('cancel', 'alpha');
$eid = GETPOST('eid', 'int'); $eid = GETPOST('eid', 'int');
// Security check // Security check
@ -54,25 +57,33 @@ $fieldtype = (! empty($ref) ? 'ref' : 'rowid');
if ($user->societe_id) $socid = $user->societe_id; if ($user->societe_id) $socid = $user->societe_id;
$result = restrictedArea($user, 'produit|service', $fieldvalue, 'product&product', '', '', $fieldtype); $result = restrictedArea($user, 'produit|service', $fieldvalue, 'product&product', '', '', $fieldtype);
$object = new Product($db); if ($id > 0 || ! empty($ref))
{
$object = new Product($db);
$object->fetch($id, $ref);
}
// Clean param // Clean param
if (! empty($conf->global->PRODUIT_MULTIPRICES) && empty($conf->global->PRODUIT_MULTIPRICES_LIMIT)) $conf->global->PRODUIT_MULTIPRICES_LIMIT = 5; if (! empty($conf->global->PRODUIT_MULTIPRICES) && empty($conf->global->PRODUIT_MULTIPRICES_LIMIT)) $conf->global->PRODUIT_MULTIPRICES_LIMIT = 5;
$error=0; // Initialize technical object to manage hooks of thirdparties. Note that conf->hooks_modules contains array array
$hookmanager->initHooks(array('productpricecard','globalcard'));
/* /*
* Actions * Actions
*/ */
if (GETPOST("cancel")) $action=''; if ($cancel) $action='';
if ($action == 'update_price' && ! GETPOST("cancel") && ($user->rights->produit->creer || $user->rights->service->creer)) $parameters=array('id'=>$id, 'ref'=>$ref);
$reshook=$hookmanager->executeHooks('doActions',$parameters,$object,$action); // Note that $action and $object may have been modified by some hooks
if ($reshook < 0) setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
if (empty($reshook))
{ {
$result = $object->fetch($id); if ($action == 'update_price' && !$cancel && ($user->rights->produit->creer || $user->rights->service->creer))
{
$error=0;
$maxpricesupplier = $object->min_recommended_price(); $maxpricesupplier = $object->min_recommended_price();
$object->fk_price_expression = empty($eid) ? 0 : $eid; //0 discards expression $object->fk_price_expression = empty($eid) ? 0 : $eid; //0 discards expression
@ -152,35 +163,33 @@ if ($action == 'update_price' && ! GETPOST("cancel") && ($user->rights->produit-
$action = 'edit_price'; $action = 'edit_price';
setEventMessage($object->error, 'errors'); setEventMessage($object->error, 'errors');
} }
} }
if ($action == 'delete' && $user->rights->produit->supprimer) if ($action == 'delete' && $user->rights->produit->supprimer)
{ {
$result = $object->log_price_delete($user, $_GET ["lineid"]); $result = $object->log_price_delete($user, $_GET ["lineid"]);
if ($result < 0) { if ($result < 0) {
setEventMessage($object->error, 'errors'); setEventMessage($object->error, 'errors');
} }
} }
/** /**
* *************************************************** * ***************************************************
* Price by quantity * Price by quantity
* *************************************************** * ***************************************************
*/ */
$error = 0; if ($action == 'activate_price_by_qty') { // Activating product price by quantity add a new price, specified as by quantity
if ($action == 'activate_price_by_qty') { // Activating product price by quantity add a new price, specified as by quantity
$result = $object->fetch($id);
$level = GETPOST('level'); $level = GETPOST('level');
$object->updatePrice(0, $object->price_base_type, $user, $object->tva_tx, 0, $level, $object->tva_npr, 1); $object->updatePrice(0, $object->price_base_type, $user, $object->tva_tx, 0, $level, $object->tva_npr, 1);
} }
if ($action == 'edit_price_by_qty') { // Edition d'un prix par quantité if ($action == 'edit_price_by_qty') { // Edition d'un prix par quantité
$rowid = GETPOST('rowid'); $rowid = GETPOST('rowid');
} }
if ($action == 'update_price_by_qty') { // Ajout / Mise à jour d'un prix par quantité if ($action == 'update_price_by_qty') { // Ajout / Mise à jour d'un prix par quantité
$result = $object->fetch($id);
// Récupération des variables // Récupération des variables
$rowid = GETPOST('rowid'); $rowid = GETPOST('rowid');
@ -226,40 +235,37 @@ if ($action == 'update_price_by_qty') { // Ajout / Mise à jour d'un prix par qu
$result = $db->query($sql); $result = $db->query($sql);
} }
} }
} }
if ($action == 'delete_price_by_qty') { if ($action == 'delete_price_by_qty') {
$rowid = GETPOST('rowid'); $rowid = GETPOST('rowid');
$sql = "DELETE FROM " . MAIN_DB_PREFIX . "product_price_by_qty"; $sql = "DELETE FROM " . MAIN_DB_PREFIX . "product_price_by_qty";
$sql .= " WHERE rowid = " . GETPOST('rowid'); $sql .= " WHERE rowid = " . GETPOST('rowid');
$result = $db->query($sql); $result = $db->query($sql);
} }
if ($action == 'delete_all_price_by_qty') { if ($action == 'delete_all_price_by_qty') {
$priceid = GETPOST('priceid'); $priceid = GETPOST('priceid');
$sql = "DELETE FROM " . MAIN_DB_PREFIX . "product_price_by_qty"; $sql = "DELETE FROM " . MAIN_DB_PREFIX . "product_price_by_qty";
$sql .= " WHERE fk_product_price = " . $priceid; $sql .= " WHERE fk_product_price = " . $priceid;
$result = $db->query($sql); $result = $db->query($sql);
} }
/** /**
* *************************************************** * ***************************************************
* Price by customer * Price by customer
* **************************************************** * ****************************************************
*/ */
if ($action == 'add_customer_price_confirm' && ! $_POST ["cancel"] && ($user->rights->produit->creer || $user->rights->service->creer)) { if ($action == 'add_customer_price_confirm' && !$cancel && ($user->rights->produit->creer || $user->rights->service->creer)) {
$error=0;
$maxpricesupplier = $object->min_recommended_price(); $maxpricesupplier = $object->min_recommended_price();
$update_child_soc = GETPOST('updatechildprice'); $update_child_soc = GETPOST('updatechildprice');
$result = $object->fetch($id);
// add price by customer // add price by customer
$prodcustprice->fk_soc = GETPOST('socid', 'int'); $prodcustprice->fk_soc = GETPOST('socid', 'int');
$prodcustprice->fk_product = $object->id; $prodcustprice->fk_product = $object->id;
@ -288,9 +294,9 @@ if ($action == 'add_customer_price_confirm' && ! $_POST ["cancel"] && ($user->ri
$action = ''; $action = '';
} }
} }
if ($action == 'delete_customer_price' && ($user->rights->produit->supprimer || $user->rights->service->supprimer)) { if ($action == 'delete_customer_price' && ($user->rights->produit->supprimer || $user->rights->service->supprimer)) {
// Delete price by customer // Delete price by customer
$prodcustprice->id = GETPOST('lineid'); $prodcustprice->id = GETPOST('lineid');
$result = $prodcustprice->delete($user); $result = $prodcustprice->delete($user);
@ -301,13 +307,10 @@ if ($action == 'delete_customer_price' && ($user->rights->produit->supprimer ||
setEventMessage($langs->trans('Delete'), 'errors'); setEventMessage($langs->trans('Delete'), 'errors');
} }
$action = ''; $action = '';
} }
if ($action == 'update_customer_price_confirm' && ! $_POST ["cancel"] && ($user->rights->produit->creer || $user->rights->service->creer)) { if ($action == 'update_customer_price_confirm' && !$cancel && ($user->rights->produit->creer || $user->rights->service->creer)) {
$result = $object->fetch($id);
$error=0;
$maxpricesupplier = $object->min_recommended_price(); $maxpricesupplier = $object->min_recommended_price();
$update_child_soc = GETPOST('updatechildprice'); $update_child_soc = GETPOST('updatechildprice');
@ -340,17 +343,16 @@ if ($action == 'update_customer_price_confirm' && ! $_POST ["cancel"] && ($user-
$action = ''; $action = '';
} }
}
} }
/* /*
* View * View
*/ */
$form = new Form($db); $form = new Form($db);
if (! empty($id) || ! empty($ref))
$result = $object->fetch($id, $ref);
llxHeader("", "", $langs->trans("CardProduct" . $object->type)); llxHeader("", "", $langs->trans("CardProduct" . $object->type));
$head = product_prepare_head($object); $head = product_prepare_head($object);