Units of measure moved from config.php into item_units table
authorJanusz Dobrowolski <janusz@frontaccounting.eu>
Tue, 11 Mar 2008 21:09:11 +0000 (21:09 +0000)
committerJanusz Dobrowolski <janusz@frontaccounting.eu>
Tue, 11 Mar 2008 21:09:11 +0000 (21:09 +0000)
applications/inventory.php
inventory/includes/db/items_units_db.inc [new file with mode: 0644]
inventory/includes/inventory_db.inc
inventory/manage/item_units.php [new file with mode: 0644]
inventory/manage/items.php
sql/alter.sql

index 620e1bc4d79acd010dd72bce5df2e42b9b82be82..5fa7d5e1b6b9c4d9fbca08dcff22cf07870b01d6 100644 (file)
@@ -23,7 +23,7 @@
                        $this->add_lapp_function(2, _("Inventory Locations"),"inventory/manage/locations.php?");
                        $this->add_rapp_function(2, _("Inventory Movement Types"),"inventory/manage/movement_types.php?");
                        $this->add_rapp_function(2, _("Item Tax Types"),"taxes/item_tax_types.php?");
-                       $this->add_rapp_function(2, "","");
+                       $this->add_rapp_function(2, _("Units of Measure"),"inventory/manage/item_units.php?");
                        $this->add_rapp_function(2, _("Reorder Levels"),"inventory/reorder_level.php?");
 
                        $this->add_module(_("Pricing and Costs"));
diff --git a/inventory/includes/db/items_units_db.inc b/inventory/includes/db/items_units_db.inc
new file mode 100644 (file)
index 0000000..f755fed
--- /dev/null
@@ -0,0 +1,56 @@
+<?php
+
+function write_item_unit($selected, $abbr, $description, $decimals)
+{
+    if($selected!='')
+               $sql = "UPDATE ".TB_PREF."item_units SET
+               abbr = '$abbr',
+               name = '$description',
+               decimals = $decimals
+               WHERE    abbr = '$selected'";
+    else
+               $sql = "INSERT INTO ".TB_PREF."item_units
+                       (abbr, name, decimals) VALUES( '$abbr',
+                       '$description', $decimals)";
+         
+       db_query($sql,"an item unit could not be updated");     
+}
+
+function delete_item_unit($unit)
+{
+       $sql="DELETE FROM ".TB_PREF."item_units WHERE abbr='$unit'";    
+       
+       db_query($sql,"an unit of measure could not be deleted");       
+}
+
+function get_item_unit($unit)
+{
+       $sql="SELECT * FROM ".TB_PREF."item_units WHERE abbr='$unit'";  
+       
+       $result = db_query($sql,"an unit of measure could not be retrieved");
+       
+       return db_fetch($result);       
+}
+
+function get_unit_descr($unit)
+{
+       $sql = "SELECT description FROM ".TB_PREF."item_units WHERE abbr='$id'";
+       
+       $result = db_query($sql, "could not unit description");
+       
+       $row = db_fetch_row($result);
+       return $row[0];
+}
+
+function item_unit_used($unit) {
+       $sql= "SELECT COUNT(*) FROM ".TB_PREF."stock_master WHERE units='$unit'";
+       $result = db_query($sql, "could not query stock master");
+       $myrow = db_fetch_row($result);
+       return ($myrow[0] > 0);
+}
+
+function get_all_item_units() {
+    $sql = "SELECT * FROM ".TB_PREF."item_units ORDER BY name";
+    return  db_query($sql, "could not get stock categories");
+}
+?>
\ No newline at end of file
index b48fc54aa5b5fddab0f8e8e50ec7277f7505dcea..1faf253f96165858080ba571c34a5b4aff0e33e2 100644 (file)
@@ -12,5 +12,6 @@ include_once($path_to_root . "/inventory/includes/db/items_locations_db.inc");
 include_once($path_to_root . "/inventory/includes/db/movement_types_db.inc");
 include_once($path_to_root . "/inventory/includes/db/items_adjust_db.inc");
 include_once($path_to_root . "/inventory/includes/db/items_transfer_db.inc");
+include_once($path_to_root . "/inventory/includes/db/items_units_db.inc");
 
 ?>
\ No newline at end of file
diff --git a/inventory/manage/item_units.php b/inventory/manage/item_units.php
new file mode 100644 (file)
index 0000000..0df22f8
--- /dev/null
@@ -0,0 +1,134 @@
+<?php
+
+$page_security = 11;
+$path_to_root="../..";
+include($path_to_root . "/includes/session.inc");
+
+page(_("Units of Measure"));
+
+include_once($path_to_root . "/includes/ui.inc");
+
+include_once($path_to_root . "/inventory/includes/db/items_units_db.inc");
+
+if (isset($_GET['selected_id']))
+{
+       $selected_id = $_GET['selected_id'];
+} 
+else if (isset($_POST['selected_id']))
+{
+       $selected_id = $_POST['selected_id'];
+}
+
+//----------------------------------------------------------------------------------
+
+if (isset($_POST['ADD_ITEM']) || isset($_POST['UPDATE_ITEM'])) 
+{
+
+       //initialise no input errors assumed initially before we test
+       $input_error = 0;
+
+       if (strlen($_POST['abbr']) == 0) 
+       {
+               $input_error = 1;
+               display_error(_("The unit of measure code cannot be empty."));
+       }
+       if (strlen($_POST['description']) == 0) 
+       {
+               $input_error = 1;
+               display_error(_("The unit of measure description cannot be empty."));
+       }
+       if (!is_numeric($_POST['decimals'])) 
+       {
+               $input_error = 1;
+               display_error(_("The number of decimal places must be integer."));
+       }
+
+
+       if ($input_error !=1) {
+       write_item_unit(isset($selected_id) ? $selected_id : '', $_POST['abbr'], $_POST['description'], $_POST['decimals'] );
+               meta_forward($_SERVER['PHP_SELF']); 
+       }
+}
+
+//---------------------------------------------------------------------------------- 
+
+if (isset($_GET['delete'])) 
+{
+
+       // PREVENT DELETES IF DEPENDENT RECORDS IN 'stock_master'
+    
+       if (item_unit_used($selected_id))
+       {
+               display_error(_("Cannot delete this unit of measure because items have been created using this units."));
+
+       } 
+       else 
+       {
+               delete_item_unit($selected_id);
+               meta_forward($_SERVER['PHP_SELF']);             
+       }
+}
+
+//----------------------------------------------------------------------------------
+
+$result = get_all_item_units();
+start_table("$table_style width=50%");
+$th = array(_('Unit'), _('Description'), _('Decimals'), "", "");
+
+table_header($th);
+$k = 0; //row colour counter
+
+while ($myrow = db_fetch($result)) 
+{
+       
+       alt_table_row_color($k);
+
+       label_cell($myrow["abbr"]);
+       label_cell($myrow["name"]);
+       label_cell($myrow["decimals"]);
+
+       edit_link_cell(SID."selected_id=$myrow[0]");
+       delete_link_cell(SID."selected_id=$myrow[0]&delete=yes");
+       end_row();
+}
+
+end_table();
+
+//----------------------------------------------------------------------------------
+
+hyperlink_no_params($_SERVER['PHP_SELF'], _("New Unit of Measure"));
+
+start_form();
+
+start_table("class='tablestyle_noborder'");
+
+if (isset($selected_id)) 
+{
+       //editing an existing item category
+
+       $myrow = get_item_unit($selected_id);
+
+       $_POST['abbr'] = $myrow["abbr"];
+       $_POST['description']  = $myrow["name"];
+       $_POST['decimals']  = $myrow["decimals"];
+
+       hidden('selected_id', $selected_id);
+}
+
+if (isset($selected_id) && item_unit_used($selected_id)) {
+    label_row(_("Unit Abbreviation:"), $_POST['abbr']);
+    hidden('abbr', $_POST['abbr']);
+} else
+    text_row(_("Unit Abbreviation:"), 'abbr', null, 20, 20);
+text_row(_("Descriptive Name:"), 'description', null, 40, 40);  
+text_row(_("Decimal Places:"), 'decimals', null, 40, 40);  
+
+end_table(1);
+
+submit_add_or_update_center(!isset($selected_id));
+
+end_form();
+
+end_page();
+
+?>
index af08d0275b2f7023212b3fe0309ad4dbf04adc57..486a3d16fd54306922f2a3b0fd695b8808f55544 100644 (file)
@@ -319,9 +319,6 @@ item_tax_types_list_row(_("Item Tax Type:"), 'tax_type_id', null);
 stock_item_types_list_row(_("Item Type:"), 'mb_flag', null,
        (!isset($_POST['NewStockID']) || isset($_POST['New'])));
 
-/* The array stock_units is set up in config.php for user modification
-possible units of measure can added or modifying the array definition by editing that file */
-
 stock_units_list_row(_('Units of Measure:'), 'units', null,
        (!isset($_POST['NewStockID']) || isset($_POST['New'])));
 end_table();
index 3a98d59f3cd23cdaf80c19bbdd817d0b6b1108d0..ae2fd8176b6e781b4fbaa7390e60a40658dbefc7 100644 (file)
 -- ALTER TABLE
 -- 
 
+CREATE TABLE `0_item_units` (
+  `abbr` VARCHAR(20) NOT NULL, 
+  `name` VARCHAR(40) NOT NULL, 
+  `decimals` TINYINT(2) NOT NULL,
+  PRIMARY KEY (`abbr`),
+  UNIQUE (`name`)
+)
+TYPE = myisam
+COMMENT = 'units of measure';
+
 DROP TABLE IF EXISTS `0_form_items`; 
 
 ALTER TABLE `0_tax_types` DROP INDEX `name`, ADD UNIQUE `name` ( `name` , `rate` );