I'm making some modifications in PHP Point of Sale 11.3. Presently called as Open Source Point of Sale which is build using Codeigniter. While adding a new item into inventory, there is a text field which I wanted to be filled automatically incremented by one than the value of the previous item.
The view code:
<?php
echo form_open('items/save/'.$item_info->item_id,array('id'=>'item_form'));
?>
<fieldset id="item_basic_info">
<legend><?php echo $this->lang->line("items_basic_information"); ?></legend>
<div class="field_row clearfix">
<?php echo form_label($this->lang->line('items_item_number').':', 'name',array('class'=>'wide')); ?>
<div class='form_field'>
<?php echo form_input(array(
'name'=>'item_number',
'id'=>'item_number',
'value'=>$item_info->item_number)
);
?>
</div>
</div>
<div class="field_row clearfix">
<?php echo form_label($this->lang->line('items_name').':', 'name',array('class'=>'required wide')); ?>
<div class='form_field'>
<?php echo form_input(array(
'name'=>'name',
'id'=>'name',
'value'=>$item_info->name)
);?>
</div>
</div>
<div class="field_row clearfix">
<?php echo form_label($this->lang->line('items_category').':', 'category',array('class'=>'required wide')); ?>
<div class='form_field'>
<?php echo form_input(array(
'name'=>'category',
'id'=>'category',
'value'=>$item_info->category)
);?>
</div>
</div>
<div class="field_row clearfix">
<?php echo form_label($this->lang->line('items_supplier').':', 'supplier',array('class'=>'required wide')); ?>
<div class='form_field'>
<?php echo form_dropdown('supplier_id', $suppliers, $selected_supplier);?>
</div>
</div>
<div class="field_row clearfix">
<?php echo form_label($this->lang->line('items_cost_price').':', 'cost_price',array('class'=>'required wide')); ?>
<div class='form_field'>
<?php echo form_input(array(
'name'=>'cost_price',
'size'=>'8',
'id'=>'cost_price',
'value'=>$item_info->cost_price)
);?>
</div>
</div>
<div class="field_row clearfix">
<?php echo form_label($this->lang->line('items_unit_price').':', 'unit_price',array('class'=>'required wide')); ?>
<div class='form_field'>
<?php echo form_input(array(
'name'=>'unit_price',
'size'=>'8',
'id'=>'unit_price',
'value'=>$item_info->unit_price)
);?>
</div>
</div>
<div class="field_row clearfix">
<?php echo form_label($this->lang->line('items_tax_1').':', 'tax_percent_1',array('class'=>'wide')); ?>
<div class='form_field'>
<?php echo form_input(array(
'name'=>'tax_names[]',
'id'=>'tax_name_1',
'size'=>'8',
// 'value'=> isset($item_tax_info[0]['name']) ? $item_tax_info[0]['name'] : $this->config->item('default_tax_1_name'))
'value'=> isset($item_tax_info[0]['name']) ? $item_tax_info[0]['name'] : $this->config->item('default_tax_1_name'))
);
?>
</div>
<div class='form_field'>
<?php echo form_input(array(
'name'=>'tax_percents[]',
'id'=>'tax_percent_name_1',
'size'=>'3',
'value'=> isset($item_tax_info[0]['percent']) ? $item_tax_info[0]['percent'] : $default_tax_1_rate)
);?>
%
</div>
</div>
<div class="field_row clearfix">
<?php echo form_label($this->lang->line('items_tax_2').':', 'tax_percent_2',array('class'=>'wide')); ?>
<div class='form_field'>
<?php echo form_input(array(
'name'=>'tax_names[]',
'id'=>'tax_name_2',
'size'=>'8',
'value'=> isset($item_tax_info[1]['name']) ? $item_tax_info[1]['name'] : $this->config->item('default_tax_2_name'))
);?>
</div>
<div class='form_field'>
<?php echo form_input(array(
'name'=>'tax_percents[]',
'id'=>'tax_percent_name_2',
'size'=>'3',
'value'=> isset($item_tax_info[1]['percent']) ? $item_tax_info[1]['percent'] : $default_tax_2_rate)
);?>
%
</div>
</div>
<div class="field_row clearfix">
<?php echo form_label($this->lang->line('items_quantity').':', 'quantity',array('class'=>'required wide')); ?>
<div class='form_field'>
<?php echo form_input(array(
'name'=>'quantity',
'id'=>'quantity',
'value'=>$item_info->quantity)
);?>
</div>
</div>
<div class="field_row clearfix">
<?php echo form_label($this->lang->line('items_reorder_level').':', 'reorder_level',array('class'=>'required wide')); ?>
<div class='form_field'>
<?php echo form_input(array(
'name'=>'reorder_level',
'id'=>'reorder_level',
'value'=>$item_info->reorder_level)
);?>
</div>
</div>
<div class="field_row clearfix">
<?php echo form_label($this->lang->line('items_location').':', 'location',array('class'=>'wide')); ?>
<div class='form_field'>
<?php echo form_input(array(
'name'=>'location',
'id'=>'location',
'value'=>$item_info->location)
);?>
</div>
</div>
<div class="field_row clearfix">
<?php echo form_label($this->lang->line('items_description').':', 'description',array('class'=>'wide')); ?>
<div class='form_field'>
<?php echo form_textarea(array(
'name'=>'description',
'id'=>'description',
'value'=>$item_info->description,
'rows'=>'5',
'cols'=>'17')
);?>
</div>
</div>
<div class="field_row clearfix">
<?php echo form_label($this->lang->line('items_allow_alt_desciption').':', 'allow_alt_description',array('class'=>'wide')); ?>
<div class='form_field'>
<?php echo form_checkbox(array(
'name'=>'allow_alt_description',
'id'=>'allow_alt_description',
'value'=>1,
'checked'=>($item_info->allow_alt_description)? 1 :0)
);?>
</div>
</div>
<div class="field_row clearfix">
<?php echo form_label($this->lang->line('items_is_serialized').':', 'is_serialized',array('class'=>'wide')); ?>
<div class='form_field'>
<?php echo form_checkbox(array(
'name'=>'is_serialized',
'id'=>'is_serialized',
'value'=>1,
'checked'=>($item_info->is_serialized)? 1 : 0)
);?>
</div>
</div>
<?php
echo form_submit(array(
'name'=>'submit',
'id'=>'submit',
'value'=>$this->lang->line('common_submit'),
'class'=>'submit_button float_right')
);
?>
</fieldset>
<?php
echo form_close();
?>
<script type='text/javascript'>
//validation and submit handling
$(document).ready(function()
{
$("#category").autocomplete("<?php echo site_url('items/suggest_category');?>",{max:100,minChars:0,delay:10});
$("#category").result(function(event, data, formatted){});
$("#category").search();
$('#item_form').validate({
submitHandler:function(form)
{
/*
make sure the hidden field #item_number gets set
to the visible scan_item_number value
*/
$('#item_number').val($('#scan_item_number').val());
$(form).ajaxSubmit({
success:function(response)
{
tb_remove();
post_item_form_submit(response);
},
dataType:'json'
});
},
errorLabelContainer: "#error_message_box",
wrapper: "li",
rules:
{
name:"required",
category:"required",
cost_price:
{
required:true,
number:true
},
unit_price:
{
required:true,
number:true
},
tax_percent:
{
required:true,
number:true
},
quantity:
{
required:true,
number:true
},
reorder_level:
{
required:true,
number:true
}
},
messages:
{
name:"<?php echo $this->lang->line('items_name_required'); ?>",
category:"<?php echo $this->lang->line('items_category_required'); ?>",
cost_price:
{
required:"<?php echo $this->lang->line('items_cost_price_required'); ?>",
number:"<?php echo $this->lang->line('items_cost_price_number'); ?>"
},
unit_price:
{
required:"<?php echo $this->lang->line('items_unit_price_required'); ?>",
number:"<?php echo $this->lang->line('items_unit_price_number'); ?>"
},
tax_percent:
{
required:"<?php echo $this->lang->line('items_tax_percent_required'); ?>",
number:"<?php echo $this->lang->line('items_tax_percent_number'); ?>"
},
quantity:
{
required:"<?php echo $this->lang->line('items_quantity_required'); ?>",
number:"<?php echo $this->lang->line('items_quantity_number'); ?>"
},
reorder_level:
{
required:"<?php echo $this->lang->line('items_reorder_level_required'); ?>",
number:"<?php echo $this->lang->line('items_reorder_level_number'); ?>"
}
}
});
});
</script>
Controller:
function view($item_id=-1)
{
$data['item_info']=$this->Item->get_info($item_id);
$data['item_tax_info']=$this->Item_taxes->get_info($item_id);
$suppliers = array('' => $this->lang->line('items_none'));
foreach($this->Supplier->get_all()->result_array() as $row)
{
$suppliers[$row['person_id']] = $row['company_name'] .' ('.$row['first_name'] .' '. $row['last_name'].')';
}
$data['suppliers']=$suppliers;
$data['selected_supplier'] = $this->Item->get_info($item_id)->supplier_id;
$data['default_tax_1_rate']=($item_id==-1) ? $this->Appconfig->get('default_tax_1_rate') : '';
$data['default_tax_2_rate']=($item_id==-1) ? $this->Appconfig->get('default_tax_2_rate') : '';
$this->load->view("items/form",$data);
}
Model:
<?php
class Item extends Model
{
/*
Determines if a given item_id is an item
*/
function exists($item_id)
{
$this->db->from('items');
$this->db->where('item_id',$item_id);
$query = $this->db->get();
return ($query->num_rows()==1);
}
/*
Returns all the items
*/
function get_all($limit=10000, $offset=0)
{
$this->db->from('items');
$this->db->where('deleted',0);
$this->db->order_by("name", "asc");
$this->db->limit($limit);
$this->db->offset($offset);
return $this->db->get();
}
function count_all()
{
$this->db->from('items');
$this->db->where('deleted',0);
return $this->db->count_all_results();
}
function get_all_filtered($low_inventory=0,$is_serialized=0,$no_description)
{
$this->db->from('items');
if ($low_inventory !=0 )
{
$this->db->where('quantity <=','reorder_level');
}
if ($is_serialized !=0 )
{
$this->db->where('is_serialized',1);
}
if ($no_description!=0 )
{
$this->db->where('description','');
}
$this->db->where('deleted',0);
$this->db->order_by("name", "asc");
return $this->db->get();
}
/*
Gets information about a particular item
*/
function get_info($item_id)
{
$this->db->from('items');
$this->db->where('item_id',$item_id);
$query = $this->db->get();
if($query->num_rows()==1)
{
return $query->row();
}
else
{
//Get empty base parent object, as $item_id is NOT an item
$item_obj=new stdClass();
//Get all the fields from items table
$fields = $this->db->list_fields('items');
foreach ($fields as $field)
{
$item_obj->$field='';
}
return $item_obj;
}
}
/*
Get an item id given an item number
*/
function get_item_id($item_number)
{
$this->db->from('items');
$this->db->where('item_number',$item_number);
$query = $this->db->get();
if($query->num_rows()==1)
{
return $query->row()->item_id;
}
return false;
}
/*
Gets information about multiple items
*/
function get_multiple_info($item_ids)
{
$this->db->from('items');
$this->db->where_in('item_id',$item_ids);
$this->db->order_by("item", "asc");
return $this->db->get();
}
/*
Inserts or updates a item
*/
function save(&$item_data,$item_id=false)
{
if (!$item_id or !$this->exists($item_id))
{
if($this->db->insert('items',$item_data))
{
$item_data['item_id']=$this->db->insert_id();
return true;
}
return false;
}
$this->db->where('item_id', $item_id);
return $this->db->update('items',$item_data);
}
/*
Updates multiple items at once
*/
function update_multiple($item_data,$item_ids)
{
$this->db->where_in('item_id',$item_ids);
return $this->db->update('items',$item_data);
}
/*
Deletes one item
*/
function delete($item_id)
{
$this->db->where('item_id', $item_id);
return $this->db->update('items', array('deleted' => 1));
}
/*
Deletes a list of items
*/
function delete_list($item_ids)
{
$this->db->where_in('item_id',$item_ids);
return $this->db->update('items', array('deleted' => 1));
}
/*
Get search suggestions to find items
*/
function get_search_suggestions($search,$limit=25)
{
$suggestions = array();
$this->db->from('items');
$this->db->like('name', $search);
$this->db->where('deleted',0);
$this->db->order_by("name", "asc");
$by_name = $this->db->get();
foreach($by_name->result() as $row)
{
$suggestions[]=$row->name;
}
$this->db->select('category');
$this->db->from('items');
$this->db->where('deleted',0);
$this->db->distinct();
$this->db->like('category', $search);
$this->db->order_by("category", "asc");
$by_category = $this->db->get();
foreach($by_category->result() as $row)
{
$suggestions[]=$row->category;
}
$this->db->from('items');
$this->db->like('item_number', $search);
$this->db->where('deleted',0);
$this->db->order_by("item_number", "asc");
$by_item_number = $this->db->get();
foreach($by_item_number->result() as $row)
{
$suggestions[]=$row->item_number;
}
//only return $limit suggestions
if(count($suggestions > $limit))
{
$suggestions = array_slice($suggestions, 0,$limit);
}
return $suggestions;
}
function get_item_search_suggestions($search,$limit=25)
{
$suggestions = array();
$this->db->from('items');
$this->db->where('deleted',0);
$this->db->like('name', $search);
$this->db->order_by("name", "asc");
$by_name = $this->db->get();
foreach($by_name->result() as $row)
{
$suggestions[]=$row->item_id.'|'.$row->name;
}
$this->db->from('items');
$this->db->where('deleted',0);
$this->db->like('item_number', $search);
$this->db->order_by("item_number", "asc");
$by_item_number = $this->db->get();
foreach($by_item_number->result() as $row)
{
$suggestions[]=$row->item_id.'|'.$row->item_number;
}
//only return $limit suggestions
if(count($suggestions > $limit))
{
$suggestions = array_slice($suggestions, 0,$limit);
}
return $suggestions;
}
function get_category_suggestions($search)
{
$suggestions = array();
$this->db->distinct();
$this->db->select('category');
$this->db->from('items');
$this->db->like('category', $search);
$this->db->where('deleted', 0);
$this->db->order_by("category", "asc");
$by_category = $this->db->get();
foreach($by_category->result() as $row)
{
$suggestions[]=$row->category;
}
return $suggestions;
}
/*
Preform a search on items
*/
function search($search)
{
$this->db->from('items');
$this->db->where("(name LIKE '%".$this->db->escape_like_str($search)."%' or
item_number LIKE '%".$this->db->escape_like_str($search)."%' or
category LIKE '%".$this->db->escape_like_str($search)."%') and deleted=0");
$this->db->order_by("name", "asc");
return $this->db->get();
}
function get_categories()
{
$this->db->select('category');
$this->db->from('items');
$this->db->where('deleted',0);
$this->db->distinct();
$this->db->order_by("category", "asc");
return $this->db->get();
}
}
?>
The item_number field in the view needs to be automatically filled incremented by one. Say the numbering series starts from 10001. The field should be prefilled with 10002 when New Item is added.
Ideas?