diff --git a/htdocs/install/mysql/migration/12.0.0-13.0.0.sql b/htdocs/install/mysql/migration/12.0.0-13.0.0.sql index bf9432a6e81..f370863004a 100644 --- a/htdocs/install/mysql/migration/12.0.0-13.0.0.sql +++ b/htdocs/install/mysql/migration/12.0.0-13.0.0.sql @@ -408,13 +408,16 @@ DELETE FROM llx_rights_def where module = 'holiday' and perms = 'lire_tous'; CREATE TABLE llx_c_product_nature ( rowid integer AUTO_INCREMENT PRIMARY KEY, - code integer NOT NULL, + code tinyint NOT NULL, label varchar(100), active tinyint DEFAULT 1 NOT NULL ) ENGINE=innodb; ALTER TABLE llx_c_product_nature ADD UNIQUE INDEX uk_c_product_nature(code); -INSERT INTO llx_c_product_nature (code, label, active) VALUES (-1, '', 1); INSERT INTO llx_c_product_nature (code, label, active) VALUES (0, 'RowMaterial', 1); INSERT INTO llx_c_product_nature (code, label, active) VALUES (1, 'Finished', 1); + +ALTER TABLE llx_product MODIFY COLUMN finished tinyint DEFAULT NULL; +ALTER TABLE llx_product ADD CONSTRAINT fk_product_finished FOREIGN KEY (finished) REFERENCES llx_c_product_nature (code); + diff --git a/htdocs/install/mysql/tables/llx_c_product_nature.sql b/htdocs/install/mysql/tables/llx_c_product_nature.sql index b2f9892aa1e..3c467ce7cf8 100644 --- a/htdocs/install/mysql/tables/llx_c_product_nature.sql +++ b/htdocs/install/mysql/tables/llx_c_product_nature.sql @@ -18,7 +18,7 @@ CREATE TABLE llx_c_product_nature ( rowid integer AUTO_INCREMENT PRIMARY KEY, - code varchar(3) NOT NULL, + code tinyint NOT NULL, label varchar(100), active tinyint DEFAULT 1 NOT NULL ) ENGINE=innodb; diff --git a/htdocs/install/mysql/tables/llx_product.key.sql b/htdocs/install/mysql/tables/llx_product.key.sql index c3dd33c88c5..c0526a8f2cc 100644 --- a/htdocs/install/mysql/tables/llx_product.key.sql +++ b/htdocs/install/mysql/tables/llx_product.key.sql @@ -33,6 +33,7 @@ ALTER TABLE llx_product ADD INDEX idx_product_fk_barcode_type (fk_barcode_type); ALTER TABLE llx_product ADD INDEX idx_product_fk_project (fk_project); ALTER TABLE llx_product ADD UNIQUE INDEX uk_product_barcode (barcode, fk_barcode_type, entity); ALTER TABLE llx_product ADD CONSTRAINT fk_product_fk_unit FOREIGN KEY (fk_unit) REFERENCES llx_c_units (rowid); +ALTER TABLE llx_product ADD CONSTRAINT fk_product_finished FOREIGN KEY (finished) REFERENCES llx_c_product_nature (code); ALTER TABLE llx_product ADD CONSTRAINT fk_product_fk_country FOREIGN KEY (fk_country) REFERENCES llx_c_country (rowid); ALTER TABLE llx_product ADD CONSTRAINT fk_product_barcode_type FOREIGN KEY (fk_barcode_type) REFERENCES llx_c_barcode_type (rowid); diff --git a/htdocs/install/mysql/tables/llx_product.sql b/htdocs/install/mysql/tables/llx_product.sql index bc583a76ce2..2431c884c62 100644 --- a/htdocs/install/mysql/tables/llx_product.sql +++ b/htdocs/install/mysql/tables/llx_product.sql @@ -92,7 +92,7 @@ create table llx_product lifo double(24,8), -- To store valuation of stock calculated using lifo method, for this product. TODO Not used, should be replaced by stock value stored into movement table. fk_default_warehouse integer DEFAULT NULL, canvas varchar(32) DEFAULT NULL, - finished tinyint DEFAULT NULL, -- 1=manufactured product, 0=matiere premiere + finished tinyint DEFAULT NULL, -- see dictionnary c_product_nature hidden tinyint DEFAULT 0, -- Not used. Deprecated. import_key varchar(14), -- Import key model_pdf varchar(255), -- model save dodument used diff --git a/htdocs/product/card.php b/htdocs/product/card.php index e3467e871c8..62813ee2a57 100644 --- a/htdocs/product/card.php +++ b/htdocs/product/card.php @@ -321,8 +321,19 @@ if (empty($reshook)) $object->surface_units = GETPOST('surface_units'); // This is not the fk_unit but the power of unit $object->volume = GETPOST('volume'); $object->volume_units = GETPOST('volume_units'); // This is not the fk_unit but the power of unit - $object->finished = GETPOST('finished', 'alpha'); - $object->fk_unit = GETPOST('units', 'alpha'); // This is the fk_unit of sale + $finished = GETPOST('finished', 'int'); + if ($finished > 0) { + $object->finished = $finished; + } else { + $object->finished = null; + } + + $units = GETPOST('units', 'int'); + if ($units > 0) { + $object->fk_unit = $units; + } else { + $object->fk_unit = null; + } $accountancy_code_sell = GETPOST('accountancy_code_sell', 'alpha'); $accountancy_code_sell_intra = GETPOST('accountancy_code_sell_intra', 'alpha'); @@ -435,10 +446,15 @@ if (empty($reshook)) $object->surface_units = GETPOST('surface_units'); // This is not the fk_unit but the power of unit $object->volume = GETPOST('volume'); $object->volume_units = GETPOST('volume_units'); // This is not the fk_unit but the power of unit - $object->finished = GETPOST('finished', 'alpha'); + + $finished = GETPOST('finished', 'int'); + if ($finished >= 0) { + $object->finished = $finished; + } else { + $object->finished = null; + } $units = GETPOST('units', 'int'); - if ($units > 0) { $object->fk_unit = $units; } else { @@ -1547,8 +1563,7 @@ if (is_object($objcanvas) && $objcanvas->displayCanvasExists($action)) } else { // Nature print ''.$form->textwithpicto($langs->trans("NatureOfProductShort"), $langs->trans("NatureOfProductDesc")).''; - $statutarray = array('-1'=>' ', '1' => $langs->trans("Finished"), '0' => $langs->trans("RowMaterial")); - print $form->selectarray('finished', $statutarray, $object->finished); + print $formproduct->selectProductNature('finished', $object->finished); print ''; // Brut Weight diff --git a/htdocs/product/class/html.formproduct.class.php b/htdocs/product/class/html.formproduct.class.php index fa1b349ef5c..8241be3446e 100644 --- a/htdocs/product/class/html.formproduct.class.php +++ b/htdocs/product/class/html.formproduct.class.php @@ -430,14 +430,16 @@ class FormProduct * NAture of product labels are defined in llx_c_product_nature * * @param string $name Name of HTML field - * @param string $default Preselected value - * @param int $mode 1=Use label as value, 0=Use rowid + * @param string $selected Preselected value + * @param int $mode 1=Use label as value, 0=Use code * @return string */ - public function selectProductNature($name = 'finished', $default = '-1', $mode = 0) + public function selectProductNature($name = 'finished', $selected = '', $mode = 0) { global $langs, $db; + $langs->load('products'); + $return = ''; // TODO Use a cache @@ -459,18 +461,29 @@ class FormProduct return -1; } else { $return .= ''; } diff --git a/htdocs/product/class/product.class.php b/htdocs/product/class/product.class.php index 18565347072..edaa5e6857b 100644 --- a/htdocs/product/class/product.class.php +++ b/htdocs/product/class/product.class.php @@ -983,7 +983,7 @@ class Product extends CommonObject $sql .= ", tosell = ".(int) $this->status; $sql .= ", tobuy = ".(int) $this->status_buy; $sql .= ", tobatch = ".((empty($this->status_batch) || $this->status_batch < 0) ? '0' : (int) $this->status_batch); - $sql .= ", finished = ".((!isset($this->finished) || $this->finished < 0) ? "null" : (int) $this->finished); + $sql .= ", finished = ".((!isset($this->finished) || $this->finished < 0 || $this->finished == '') ? "null" : (int) $this->finished); $sql .= ", net_measure = ".($this->net_measure != '' ? "'".$this->db->escape($this->net_measure)."'" : 'null'); $sql .= ", net_measure_units = ".($this->net_measure_units != '' ? "'".$this->db->escape($this->net_measure_units)."'" : 'null'); $sql .= ", weight = ".($this->weight != '' ? "'".$this->db->escape($this->weight)."'" : 'null'); @@ -4562,10 +4562,21 @@ class Product extends CommonObject global $langs; $langs->load('products'); - if ($this->finished == '0') { return $langs->trans("RowMaterial"); - } - if ($this->finished == '1') { return $langs->trans("Finished"); + if (isset($this->finished) && $this->finished>=0) { + $sql = 'SELECT label, code FROM '.MAIN_DB_PREFIX.'c_product_nature where code='.$this->finished.' AND active=1'; + $resql = $this->db->query($sql); + if ($resql && $this->db->num_rows($resql) > 0) { + $res = $this->db->fetch_array($resql); + $label = $langs->trans($res['label']); + $this->db->free($resql); + return $label; + } else { + $this->error = $this->db->error().' sql='.$sql; + dol_syslog(get_class($this)."::".__METHOD__.' Error '.$this->error, LOG_ERR); + return -1; + } } + return ''; }