FIX Can't set cost price when product is not on purchase

This commit is contained in:
Laurent Destailleur 2021-08-09 20:01:06 +02:00
parent 3f58e7c5db
commit f3dbe81df4
3 changed files with 70 additions and 11 deletions

View File

@ -103,6 +103,9 @@ if ($id > 0 || $ref) {
$object->fetch($id, $ref);
}
$usercanread = (($object->type == Product::TYPE_PRODUCT && $user->rights->produit->lire) || ($object->type == Product::TYPE_SERVICE && $user->rights->service->lire));
$usercancreate = (($object->type == Product::TYPE_PRODUCT && $user->rights->produit->creer) || ($object->type == Product::TYPE_SERVICE && $user->rights->service->creer));
if ($object->id > 0) {
if ($object->type == $object::TYPE_PRODUCT) {
restrictedArea($user, 'produit', $object->id, 'product&product', '', '');
@ -123,9 +126,6 @@ if ($cancel) {
$action = '';
}
$usercanread = (($object->type == Product::TYPE_PRODUCT && $user->rights->produit->lire) || ($object->type == Product::TYPE_SERVICE && $user->rights->service->lire));
$usercancreate = (($object->type == Product::TYPE_PRODUCT && $user->rights->produit->creer) || ($object->type == Product::TYPE_SERVICE && $user->rights->service->creer));
$parameters = array('socid'=>$socid, 'id_prod'=>$id);
$reshook = $hookmanager->executeHooks('doActions', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
if ($reshook < 0) {
@ -404,13 +404,23 @@ if ($id > 0 || $ref) {
print '<div class="underbanner clearboth"></div>';
print '<table class="border tableforfield centpercent">';
// Type
if (!empty($conf->product->enabled) && !empty($conf->service->enabled)) {
$typeformat = 'select;0:'.$langs->trans("Product").',1:'.$langs->trans("Service");
print '<tr><td class="">';
print (empty($conf->global->PRODUCT_DENY_CHANGE_PRODUCT_TYPE)) ? $form->editfieldkey("Type", 'fk_product_type', $object->type, $object, 0, $typeformat) : $langs->trans('Type');
print '</td><td>';
print $form->editfieldval("Type", 'fk_product_type', $object->type, $object, 0, $typeformat);
print '</td></tr>';
}
// Cost price. Can be used for margin module for option "calculate margin on explicit cost price
print '<tr><td>';
$textdesc = $langs->trans("CostPriceDescription");
$textdesc .= "<br>".$langs->trans("CostPriceUsage");
$text = $form->textwithpicto($langs->trans("CostPrice"), $textdesc, 1, 'help', '');
print $form->editfieldkey($text, 'cost_price', $object->cost_price, $object, $usercancreate, 'amount:6');
print '</td><td colspan="2">';
print '</td><td>';
print $form->editfieldval($text, 'cost_price', $object->cost_price, $object, $usercancreate, 'amount:6');
print '</td></tr>';
@ -425,7 +435,7 @@ if ($id > 0 || $ref) {
// Best buying Price
print '<tr><td class="titlefieldcreate">'.$langs->trans("BuyingPriceMin").'</td>';
print '<td colspan="2">';
print '<td>';
$product_fourn = new ProductFournisseur($db);
if ($product_fourn->find_min_price_product_fournisseur($object->id) > 0) {
if ($product_fourn->product_fourn_price_id > 0) {

View File

@ -717,8 +717,18 @@ if (!empty($conf->global->PRODUIT_MULTIPRICES) || !empty($conf->global->PRODUIT_
$soc->id = $socid;
$soc->fetch($socid);
// Type
if (!empty($conf->product->enabled) && !empty($conf->service->enabled)) {
$typeformat = 'select;0:'.$langs->trans("Product").',1:'.$langs->trans("Service");
print '<tr><td class="">';
print (empty($conf->global->PRODUCT_DENY_CHANGE_PRODUCT_TYPE)) ? $form->editfieldkey("Type", 'fk_product_type', $object->type, $object, 0, $typeformat) : $langs->trans('Type');
print '</td><td>';
print $form->editfieldval("Type", 'fk_product_type', $object->type, $object, 0, $typeformat);
print '</td></tr>';
}
// Selling price
print '<tr><td class="titlefield">';
print '<tr><td class="titlefieldcreate">';
print $langs->trans("SellingPrice");
print '</td>';
print '<td colspan="2">';
@ -791,13 +801,33 @@ if (!empty($conf->global->PRODUIT_MULTIPRICES) || !empty($conf->global->PRODUIT_
}
} else {
if (!empty($conf->global->PRODUIT_MULTIPRICES_USE_VAT_PER_LEVEL)) { // using this option is a bug. kept for backward compatibility
// Type
if (!empty($conf->product->enabled) && !empty($conf->service->enabled)) {
$typeformat = 'select;0:'.$langs->trans("Product").',1:'.$langs->trans("Service");
print '<tr><td class="">';
print (empty($conf->global->PRODUCT_DENY_CHANGE_PRODUCT_TYPE)) ? $form->editfieldkey("Type", 'fk_product_type', $object->type, $object, 0, $typeformat) : $langs->trans('Type');
print '</td><td>';
print $form->editfieldval("Type", 'fk_product_type', $object->type, $object, 0, $typeformat);
print '</td></tr>';
}
// We show only vat for level 1
print '<tr><td class="titlefield">'.$langs->trans("DefaultTaxRate").'</td>';
print '<tr><td class="titlefieldcreate">'.$langs->trans("DefaultTaxRate").'</td>';
print '<td colspan="2">'.vatrate($object->multiprices_tva_tx[1], true).'</td>';
print '</tr>';
} else {
// Type
if (!empty($conf->product->enabled) && !empty($conf->service->enabled)) {
$typeformat = 'select;0:'.$langs->trans("Product").',1:'.$langs->trans("Service");
print '<tr><td class="">';
print (empty($conf->global->PRODUCT_DENY_CHANGE_PRODUCT_TYPE)) ? $form->editfieldkey("Type", 'fk_product_type', $object->type, $object, 0, $typeformat) : $langs->trans('Type');
print '</td><td>';
print $form->editfieldval("Type", 'fk_product_type', $object->type, $object, 0, $typeformat);
print '</td></tr>';
}
// TVA
print '<tr><td class="titlefield">'.$langs->trans("DefaultTaxRate").'</td><td>';
print '<tr><td class="titlefieldcreate">'.$langs->trans("DefaultTaxRate").'</td><td>';
$positiverates = '';
if (price2num($object->tva_tx)) {

View File

@ -77,6 +77,7 @@ $batchnumber = GETPOST('batch_number', 'san_alpha');
if (!empty($batchnumber)) {
$batchnumber = trim($batchnumber);
}
$cost_price = GETPOST('cost_price', 'alpha');
// Security check
if ($user->socid) {
@ -113,6 +114,9 @@ $hookmanager->initHooks(array('stockproductcard', 'globalcard'));
$error = 0;
$usercanread = (($object->type == Product::TYPE_PRODUCT && $user->rights->produit->lire) || ($object->type == Product::TYPE_SERVICE && $user->rights->service->lire));
$usercancreate = (($object->type == Product::TYPE_PRODUCT && $user->rights->produit->creer) || ($object->type == Product::TYPE_SERVICE && $user->rights->service->creer));
if ($object->id > 0) {
if ($object->type == $object::TYPE_PRODUCT) {
restrictedArea($user, 'produit', $object->id, 'product&product', '', '');
@ -139,6 +143,21 @@ if ($reshook < 0) {
setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
}
if ($action == 'setcost_price') {
if ($id) {
$result = $object->fetch($id);
$object->cost_price = price2num($cost_price);
$result = $object->update($object->id, $user);
if ($result > 0) {
setEventMessages($langs->trans("RecordSaved"), null, 'mesgs');
$action = '';
} else {
$error++;
setEventMessages($object->error, $object->errors, 'errors');
}
}
}
if ($action == 'addlimitstockwarehouse' && !empty($user->rights->produit->creer)) {
$seuil_stock_alerte = GETPOST('seuil_stock_alerte');
$desiredstock = GETPOST('desiredstock');
@ -600,9 +619,9 @@ if ($id > 0 || $ref) {
if (!empty($conf->product->enabled) && !empty($conf->service->enabled)) {
$typeformat = 'select;0:'.$langs->trans("Product").',1:'.$langs->trans("Service");
print '<tr><td class="">';
print (empty($conf->global->PRODUCT_DENY_CHANGE_PRODUCT_TYPE)) ? $form->editfieldkey("Type", 'fk_product_type', $object->type, $object, $usercancreate, $typeformat) : $langs->trans('Type');
print (empty($conf->global->PRODUCT_DENY_CHANGE_PRODUCT_TYPE)) ? $form->editfieldkey("Type", 'fk_product_type', $object->type, $object, 0, $typeformat) : $langs->trans('Type');
print '</td><td>';
print $form->editfieldval("Type", 'fk_product_type', $object->type, $object, $usercancreate, $typeformat);
print $form->editfieldval("Type", 'fk_product_type', $object->type, $object, 0, $typeformat);
print '</td></tr>';
}
@ -618,7 +637,7 @@ if ($id > 0 || $ref) {
$textdesc .= "<br>".$langs->trans("CostPriceUsage");
$text = $form->textwithpicto($langs->trans("CostPrice"), $textdesc, 1, 'help', '');
print $form->editfieldkey($text, 'cost_price', $object->cost_price, $object, $usercancreate, 'amount:6');
print '</td><td colspan="2">';
print '</td><td>';
print $form->editfieldval($text, 'cost_price', $object->cost_price, $object, $usercancreate, 'amount:6');
print '</td></tr>';