Merge pull request #14732 from ptibogxiv/patch-359

NEW add State/Province origin for products
This commit is contained in:
Laurent Destailleur 2020-09-16 01:02:00 +02:00 committed by GitHub
commit d954de2d35
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 125 additions and 42 deletions

View File

@ -44,6 +44,7 @@ require_once DOL_DOCUMENT_ROOT.'/core/class/html.formfile.class.php';
require_once DOL_DOCUMENT_ROOT.'/core/class/canvas.class.php';
require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
require_once DOL_DOCUMENT_ROOT.'/product/class/html.formproduct.class.php';
require_once DOL_DOCUMENT_ROOT.'/core/class/html.formcompany.class.php';
require_once DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php';
require_once DOL_DOCUMENT_ROOT.'/core/class/genericobject.class.php';
require_once DOL_DOCUMENT_ROOT.'/core/lib/product.lib.php';
@ -290,7 +291,8 @@ if (empty($reshook))
$object->note_private = dol_htmlcleanlastbr(GETPOST('note_private', 'none'));
$object->note = $object->note_private; // deprecated
$object->customcode = GETPOST('customcode', 'alphanohtml');
$object->country_id = GETPOST('country_id', 'int');
$object->country_id = GETPOST('country_id', 'int');
$object->state_id = GETPOST('state_id', 'int');
$object->duration_value = $duration_value;
$object->duration_unit = $duration_unit;
$object->fk_default_warehouse = GETPOST('fk_default_warehouse');
@ -395,7 +397,8 @@ if (empty($reshook))
$object->note = $object->note_private;
}
$object->customcode = GETPOST('customcode', 'alpha');
$object->country_id = GETPOST('country_id', 'int');
$object->country_id = GETPOST('country_id', 'int');
$object->state_id = GETPOST('state_id', 'int');
$object->status = GETPOST('statut', 'int');
$object->status_buy = GETPOST('statut_buy', 'int');
$object->status_batch = GETPOST('status_batch', 'aZ09');
@ -866,6 +869,7 @@ llxHeader('', $title, $helpurl);
$form = new Form($db);
$formfile = new FormFile($db);
$formproduct = new FormProduct($db);
$formcompany = new FormCompany($db);
if (!empty($conf->accounting->enabled)) $formaccounting = new FormAccounting($db);
// Load object modBarCodeProduct
@ -908,6 +912,15 @@ if (is_object($objcanvas) && $objcanvas->displayCanvasExists($action))
//WYSIWYG Editor
require_once DOL_DOCUMENT_ROOT.'/core/class/doleditor.class.php';
print '<script type="text/javascript">';
print '$(document).ready(function () {
$("#selectcountry_id").change(function() {
document.formprod.action.value="create";
document.formprod.submit();
});
});';
print '</script>'."\n";
// Load object modCodeProduct
$module = (!empty($conf->global->PRODUCT_CODEPRODUCT_ADDON) ? $conf->global->PRODUCT_CODEPRODUCT_ADDON : 'mod_codeproduct_leopard');
if (substr($module, 0, 16) == 'mod_codeproduct_' && substr($module, -3) == 'php')
@ -922,7 +935,7 @@ if (is_object($objcanvas) && $objcanvas->displayCanvasExists($action))
dol_set_focus('input[name="ref"]');
print '<form action="'.$_SERVER["PHP_SELF"].'" method="POST">';
print '<form action="'.$_SERVER["PHP_SELF"].'" method="POST" name="formprod">';
print '<input type="hidden" name="token" value="'.newToken().'">';
print '<input type="hidden" name="action" value="add">';
print '<input type="hidden" name="type" value="'.$type.'">'."\n";
@ -942,6 +955,15 @@ if (is_object($objcanvas) && $objcanvas->displayCanvasExists($action))
$linkback = "";
print load_fiche_titre($title, $linkback, $picto);
// We set country_id, country_code and country for the selected country
$object->country_id = GETPOST('country_id', 'int) ? GETPOST('country_id', 'int') : null;
if ($object->country_id > 0)
{
$tmparray = getCountry($object->country_id, 'all');
$object->country_code = $tmparray['code'];
$object->country = $tmparray['label'];
}
dol_fiche_head('');
print '<table class="border centpercent">';
@ -1114,14 +1136,31 @@ if (is_object($objcanvas) && $objcanvas->displayCanvasExists($action))
// Custom code
if (empty($conf->global->PRODUCT_DISABLE_CUSTOM_INFO) && empty($type))
{
print '<tr><td>'.$langs->trans("CustomCode").'</td><td><input name="customcode" class="maxwidth100onsmartphone" value="'.GETPOST('customcode').'"></td>';
if ($conf->browser->layout == 'phone') print '</tr><tr>';
print '<tr><td>'.$langs->trans("CustomCode").'</td><td><input name="customcode" class="maxwidth100onsmartphone" value="'.GETPOST('customcode').'"></td></tr>';
// Origin country
print '<td>'.$langs->trans("CountryOrigin").'</td>';
print '<td>';
print $form->select_country(GETPOST('country_id', 'int'), 'country_id');
if ($user->admin) print info_admin($langs->trans("YouCanChangeValuesForThisListFromDictionarySetup"), 1);
print '</td></tr>';
print img_picto('', 'globe-americas', 'class="paddingrightonly"');
print $form->select_country((GETPOSTISSET('country_id') ? GETPOST('country_id') : $object->country_id), 'country_id', '', 0, 'minwidth300 widthcentpercentminusx');
if ($user->admin) print info_admin($langs->trans("YouCanChangeValuesForThisListFromDictionarySetup"), 1);
print '</td>';
// State
if (empty($conf->global->PRODUCT_DISABLE_STATE))
{
if ($conf->browser->layout == 'phone') print '</tr><tr>';
if (!empty($conf->global->MAIN_SHOW_REGION_IN_STATE_SELECT) && ($conf->global->MAIN_SHOW_REGION_IN_STATE_SELECT == 1 || $conf->global->MAIN_SHOW_REGION_IN_STATE_SELECT == 2))
{
print '<td>'.$form->editfieldkey('Region-StateOrigine', 'state_id', '', $object, 0).'</td><td colspan="3">';
} else {
print '<td>'.$form->editfieldkey('StateOrigin', 'state_id', '', $object, 0).'</td><td colspan="3">';
}
print $formcompany->select_state($object->state_id, $object->country_code);
print '</tr>';
}
print '</tr>';
}
// Other attributes
@ -1333,12 +1372,31 @@ if (is_object($objcanvas) && $objcanvas->displayCanvasExists($action))
//WYSIWYG Editor
require_once DOL_DOCUMENT_ROOT.'/core/class/doleditor.class.php';
print '<script type="text/javascript">';
print '$(document).ready(function () {
$("#selectcountry_id").change(function () {
document.formprod.action.value="edit";
document.formprod.submit();
});
});';
print '</script>'."\n";
// We set country_id, country_code and country for the selected country
$object->country_id = GETPOST('country_id') ? GETPOST('country_id') : $object->country_id;
if ($object->country_id)
{
$tmparray = getCountry($object->country_id, 'all');
$object->country_code = $tmparray['code'];
$object->country = $tmparray['label'];
}
$type = $langs->trans('Product');
if ($object->isService()) $type = $langs->trans('Service');
//print load_fiche_titre($langs->trans('Modify').' '.$type.' : '.(is_object($object->oldcopy)?$object->oldcopy->ref:$object->ref), "");
// Main official, simple, and not duplicated code
print '<form action="'.$_SERVER['PHP_SELF'].'?id='.$object->id.'" method="POST">'."\n";
print '<form action="'.$_SERVER['PHP_SELF'].'?id='.$object->id.'" method="POST" name="formprod">'."\n";
print '<input type="hidden" name="token" value="'.newToken().'">';
print '<input type="hidden" name="action" value="update">';
print '<input type="hidden" name="id" value="'.$object->id.'">';
@ -1349,6 +1407,7 @@ if (is_object($objcanvas) && $objcanvas->displayCanvasExists($action))
$picto = ($object->type == Product::TYPE_SERVICE ? 'service' : 'product');
dol_fiche_head($head, 'card', $titre, 0, $picto);
print '<table class="border allwidth">';
// Ref
@ -1529,12 +1588,29 @@ if (is_object($objcanvas) && $objcanvas->displayCanvasExists($action))
// Custom code
if (!$object->isService() && empty($conf->global->PRODUCT_DISABLE_CUSTOM_INFO))
{
print '<tr><td>'.$langs->trans("CustomCode").'</td><td><input name="customcode" class="maxwidth100onsmartphone" value="'.$object->customcode.'"></td>';
print '<tr><td>'.$langs->trans("CustomCode").'</td><td><input name="customcode" class="maxwidth100onsmartphone" value="'.$object->customcode.'"></td></tr>';
// Origin country
print '<td>'.$langs->trans("CountryOrigin").'</td><td>';
print '<td>'.$langs->trans("CountryOrigin").'</td>';
print '<td>';
print img_picto('', 'globe-americas', 'class="paddingrightonly"');
print $form->select_country($object->country_id, 'country_id', '', 0, 'minwidth100 maxwidthonsmartphone');
if ($user->admin) print info_admin($langs->trans("YouCanChangeValuesForThisListFromDictionarySetup"), 1);
print '</td></tr>';
print '</td>';
// State
if (empty($conf->global->PRODUCT_DISABLE_STATE))
{
if ($conf->browser->layout == 'phone') print '</tr><tr>';
if (!empty($conf->global->MAIN_SHOW_REGION_IN_STATE_SELECT) && ($conf->global->MAIN_SHOW_REGION_IN_STATE_SELECT == 1 || $conf->global->MAIN_SHOW_REGION_IN_STATE_SELECT == 2))
{
print '<td>'.$form->editfieldkey('Region-StateOrigine', 'state_id', '', $object, 0).'</td><td colspan="3">';
} else {
print '<td>'.$form->editfieldkey('StateOrigin', 'state_id', '', $object, 0).'</td><td colspan="3">';
}
print $formcompany->select_state($object->state_id, $object->country_code);
print '</td>';
}
print '</tr>';
}
// Other attributes
@ -2031,7 +2107,9 @@ if (is_object($objcanvas) && $objcanvas->displayCanvasExists($action))
print '<tr><td>'.$langs->trans("CustomCode").'</td><td colspan="2">'.$object->customcode.'</td>';
// Origin country code
print '<tr><td>'.$langs->trans("CountryOrigin").'</td><td colspan="2">'.getCountry($object->country_id, 0, $db).'</td>';
print '<tr><td>'.$langs->trans("Origin").'</td><td colspan="2">'.getCountry($object->country_id, 0, $db);
if (!empty($object->state_id)) print ' - '.getState($object->state_id, 0, $db);
print '</td>';
}
// Other attributes

View File

@ -883,6 +883,10 @@ class Product extends CommonObject
$this->country_id = 0;
}
if (empty($this->state_id)) {
$this->state_id = 0;
}
// Barcode value
$this->barcode = trim($this->barcode);
@ -1000,6 +1004,7 @@ class Product extends CommonObject
$sql .= ", url = ".($this->url ? "'".$this->db->escape($this->url)."'" : 'null');
$sql .= ", customcode = '".$this->db->escape($this->customcode)."'";
$sql .= ", fk_country = ".($this->country_id > 0 ? (int) $this->country_id : 'null');
$sql .= ", fk_state = ".($this->state_id > 0 ? (int) $this->state_id : 'null');
$sql .= ", note = ".(isset($this->note) ? "'".$this->db->escape($this->note)."'" : 'null');
$sql .= ", duration = '".$this->db->escape($this->duration_value.$this->duration_unit)."'";
$sql .= ", accountancy_code_buy = '".$this->db->escape($this->accountancy_code_buy)."'";
@ -2025,7 +2030,7 @@ class Product extends CommonObject
return -1;
}
$sql = "SELECT rowid, ref, ref_ext, label, description, url, note_public, note as note_private, customcode, fk_country, price, price_ttc,";
$sql = "SELECT rowid, ref, ref_ext, label, description, url, note_public, note as note_private, customcode, fk_country, fk_state, price, price_ttc,";
$sql .= " price_min, price_min_ttc, price_base_type, cost_price, default_vat_code, tva_tx, recuperableonly as tva_npr, localtax1_tx, localtax2_tx, localtax1_type, localtax2_type, tosell,";
$sql .= " tobuy, fk_product_type, duration, fk_default_warehouse, seuil_stock_alerte, canvas, net_measure, net_measure_units, weight, weight_units,";
$sql .= " length, length_units, width, width_units, height, height_units,";
@ -2073,6 +2078,7 @@ class Product extends CommonObject
$this->customcode = $obj->customcode;
$this->country_id = $obj->fk_country;
$this->country_code = getCountry($this->country_id, 2, $this->db);
$this->state_id = $obj->fk_state;
$this->price = $obj->price;
$this->price_ttc = $obj->price_ttc;
$this->price_min = $obj->price_min;
@ -5204,17 +5210,15 @@ class Product extends CommonObject
$this->barcode = -1; // Create barcode automatically
}
/**
* Returns the label, shot_label or code found in units dictionary from ->fk_unit.
* A langs->trans() must be called on result to get translated value.
*
* @param string $type Label type (long, short or code)
* @return string|int <0 if KO, label if OK (Example: 'long', 'short', 'unitCODE')
* @see getLabelOfUnit() in CommonObjectLine
*/
public function getLabelOfUnit($type = 'long')
{
global $langs;
/**
* Returns the text label from units dictionary
*
* @param string $type Label type (long or short)
* @return string|int <0 if ko, label if ok
*/
public function getLabelOfUnit($type = 'long')
{
global $langs;
if (!$this->fk_unit) {
return '';
@ -5222,24 +5226,25 @@ class Product extends CommonObject
$langs->load('products');
$label_type = 'label';
if ($type == 'short') $label_type = 'short_label';
elseif ($type == 'code') $label_type = 'code';
$label_type = 'label';
$sql = 'select '.$label_type.', code from '.MAIN_DB_PREFIX.'c_units where rowid='.$this->fk_unit;
$resql = $this->db->query($sql);
if ($resql && $this->db->num_rows($resql) > 0) {
$res = $this->db->fetch_array($resql);
if ($label_type == 'code') $label = 'unit'.$res['code'];
else $label = $res[$label_type];
$this->db->free($resql);
return $label;
} else {
$this->error = $this->db->error().' sql='.$sql;
dol_syslog(get_class($this)."::getLabelOfUnit Error ".$this->error, LOG_ERR);
return -1;
}
}
if ($type == 'short') {
$label_type = 'short_label';
}
$sql = 'select '.$label_type.', code from '.MAIN_DB_PREFIX.'c_units where rowid='.$this->fk_unit;
$resql = $this->db->query($sql);
if ($resql && $this->db->num_rows($resql) > 0) {
$res = $this->db->fetch_array($resql);
$label = ($label_type == 'short_label' ? $res[$label_type] : 'unit'.$res['code']);
$this->db->free($resql);
return $label;
} else {
$this->error = $this->db->error().' sql='.$sql;
dol_syslog(get_class($this)."::getLabelOfUnit Error ".$this->error, LOG_ERR);
return -1;
}
}
/**
* Return if object has a sell-by date or eat-by date