I'm currently modifying open source PHP POS, I would like to amend the code instead of showing warning and display the 0 stock items to don't display the result if the quantity is 0.
I manage to change the message from normal message to a javascript popup however I still prefer to not show the item. Because my staff who use this system always ignores the popup and when the month end it showed quite a number of negative items and creates headache for me to check and alter. Therefore I'm seeking professional helps here to advise and assist me. I look high and low and manage to get a bunch of code which I believe it might be the one. Please correct me if I'm wrong.
The code:
function add_item($item_id,$quantity=1,$discount=0,$price=null,$description=null,$serialnumber=null)
{
//make sure item exists
if(!$this->CI->Item->exists($item_id))
{
//try to get item id given an item_number
$item_id = $this->CI->Item->get_item_id($item_id);
if(!$item_id)
return false;
}
//Alain Serialization and Description
//Get all items in the cart so far...
$items = $this->get_cart();
//We need to loop through all items in the cart.
//If the item is already there, get it's key($updatekey).
//We also need to get the next key that we are going to use in case we need to add the
//item to the cart. Since items can be deleted, we can't use a count. we use the highest key + 1.
$maxkey=0; //Highest key so far
$itemalreadyinsale=FALSE; //We did not find the item yet.
$insertkey=0; //Key to use for new entry.
$updatekey=0; //Key to use to update(quantity)
foreach ($items as $item)
{
//We primed the loop so maxkey is 0 the first time.
//Also, we have stored the key in the element itself so we can compare.
if($maxkey <= $item['line'])
{
$maxkey = $item['line'];
}
if($item['item_id']==$item_id)
{
$itemalreadyinsale=TRUE;
$updatekey=$item['line'];
}
}
$insertkey=$maxkey+1;
//array/cart records are identified by $insertkey and item_id is just another field.
$item = array(($insertkey)=>
array(
'item_id'=>$item_id,
'line'=>$insertkey,
'name'=>$this->CI->Item->get_info($item_id)->name,
'item_number'=>$this->CI->Item->get_info($item_id)->item_number,
'description'=>$description!=null ? $description: $this->CI->Item->get_info($item_id)->description,
'serialnumber'=>$serialnumber!=null ? $serialnumber: '',
'allow_alt_description'=>$this->CI->Item->get_info($item_id)->allow_alt_description,
'is_serialized'=>$this->CI->Item->get_info($item_id)->is_serialized,
'quantity'=>$quantity,
'discount'=>$discount,
'price'=>$price!=null ? $price: $this->CI->Item->get_info($item_id)->unit_price
)
);
//Item already exists and is not serialized, add to quantity
if($itemalreadyinsale && ($this->CI->Item->get_info($item_id)->is_serialized ==0) )
{
$items[$updatekey]['quantity']+=$quantity;
}
else
{
//add to existing array
$items+=$item;
}
$this->set_cart($items);
return true;
}
function out_of_stock($item_id)
{
//make sure item exists
if(!$this->CI->Item->exists($item_id))
{
//try to get item id given an item_number
$item_id = $this->CI->Item->get_item_id($item_id);
if(!$item_id)
return false;
}
$item = $this->CI->Item->get_info($item_id);
$quanity_added = $this->get_quantity_already_added($item_id);
if ($item->quantity - $quanity_added < 0)
{
return true;
}
return false;
}
Thank you in advance!
Jeff
You only show the code of the frontend (the "template"), and unfortunately this code is obviously missing some parts at the end - at least the table row end should be there.
However I assumed that the missing </tr> element would come just after the code snippet.
What I did here is simply placing the <tr> containing the product in a condition so that it will only be shown if the in-stock quantity is > 0.
<div id="register_wrapper">
<?php echo form_open("sales/change_mode",array('id'=>'mode_form')); ?>
<span><?php echo $this->lang->line('sales_mode') ?></span>
<?php echo form_dropdown('mode',$modes,$mode,'onchange="$(\'#mode_form\').submit();"'); ?>
<div id="show_suspended_sales_button">
<?php echo anchor("sales/suspended/width:425",
"<div class='small_button'><span style='font-size:73%;'>".$this->lang->line('sales_suspended_sales')."</span></div>",
array('class'=>'thickbox none','title'=>$this->lang->line('sales_suspended_sales')));
?>
</div>
</form>
<?php echo form_open("sales/add",array('id'=>'add_item_form')); ?>
<label id="item_label" for="item">
<?php
if($mode=='sale')
{
echo $this->lang->line('sales_find_or_scan_item');
}
else
{
echo $this->lang->line('sales_find_or_scan_item_or_receipt');
}
?>
</label>
<?php echo form_input(array('name'=>'item','id'=>'item','size'=>'40'));?>
<div id="new_item_button_register" >
<?php echo anchor("items/view/-1/width:360",
"<div class='small_button'><span>".$this->lang->line('sales_new_item') </span></div>",
array('class'=>'thickbox none','title'=>$this->lang- >line('sales_new_item')));
?>
</div>
</form>
<table id="register">
<thead>
<tr>
<th style="width:11%;"><?php echo $this->lang->line('common_delete'); ?></th>
<th style="width:30%;"><?php echo $this->lang->line('sales_item_number'); ?></th>
<th style="width:30%;"><?php echo $this->lang->line('sales_item_name'); ?></th>
<th style="width:11%;"><?php echo $this->lang->line('sales_price'); ?></th>
<th style="width:11%;"><?php echo $this->lang->line('sales_quantity'); ?></th>
<th style="width:11%;"><?php echo $this->lang->line('sales_discount'); ?></th>
<th style="width:15%;"><?php echo $this->lang->line('sales_total'); ?></th>
<th style="width:11%;"><?php echo $this->lang->line('sales_edit'); ?></th>
</tr>
</thead>
<tbody id="cart_contents">
<?php
if(count($cart)==0)
{
?>
<tr><td colspan='8'>
<div class='warning_message' style='padding:7px;'><?php echo $this->lang- >line('sales_no_items_in_cart'); ?></div>
</tr></tr>
<?php
}
else
{
foreach(array_reverse($cart, true) as $line=>$item)
{
$cur_item_info = $this->Item->get_info($item['item_id']);
// THIS IS THE CONDITION WHERE I CHECK THE AVAILABILITY STATE
if ($cur_item_info->quantity > 0) {
echo form_open("sales/edit_item/$line");
?>
<tr>
<td><?php echo anchor("sales/delete_item/$line",'['.$this->lang->line('common_delete').']');?></td>
<td><?php echo $item['item_number']; ?></td>
<td style="align:center;"><?php echo $item['name']; ?><br /> [<?php echo $cur_item_info->quantity; ?> in stock]</td>
<?php if ($items_module_allowed)
{
?>
<td><?php echo form_input(array('name'=>'price','value'=>$item['price'],'size'=>'6'));?></td>
<?php
}
else
{
?>
<td><?php echo $item['price']; ?></td>
<?php echo form_hidden('price',$item['price']); ?>
<?php
}
?>
<td>
<?php
if($item['is_serialized']==1)
{
echo $item['quantity'];
echo form_hidden('quantity',$item['quantity']);
}
else
{
echo form_input(array('name'=>'quantity','value'=>$item['quantity'],'size'=>'2'));
}
?>
</td>
</tr>
<?php
// I assumed that there must be a table row closer (above)
} // end if quantity > 0
?>
HTH
Regarding the new code - you have a function named "out_of_stock", I'd try to put a call to this function just at the beginning of add_item. Please note that this is just a guess as I don't know the intrinsics of your shop system:
function add_item($item_id,$quantity=1,$discount=0,$price=null,$description=null,$serialnumber=null)
{
//make sure item exists
if(!$this->CI->Item->exists($item_id))
{
//try to get item id given an item_number
$item_id = $this->CI->Item->get_item_id($item_id);
if(!$item_id)
return false;
}
// add the out-of-stock check here
if (out_ot_stock($item_id)) {
return false;
}
// continue with the rest of code
Related
I unable to delete specific record from session array. I want to delete specific row in table when I click on delete link.
<?php
session_start();
if(isset($_GET["product"]) && isset($_GET["category"])){
$nomProduct = trim($_GET["product"]);
$category = trim($_GET["category"]);
$_SESSION['product'][] = array(
"nomProduct" => $nomProduct ,
"category" => $category
);
//session_destroy();
}
?>
html table
<table class="table">
<?php foreach($_SESSION["product"] as $items) { ?>
<tr>
<th width="250px"><?php echo $items['nomProduct']; ?></th>
<td><?php echo $items['category']; ?></td>
<td style="text-align: right">Delete<td>
</tr>
<?php }?>
</table>
`
$key=array_search($_GET['product'],$_SESSION['product']);
if($key!==false)
unset($_SESSION['product'][$key]);
$_SESSION["product"] = array_values($_SESSION["product"]);
`
Maybe this might help!
You need to find the key as this is an array.
EDIT:
Made an example for you, here when you click the link, it deletes the first name from the session array.
<?php
session_start();
$_SESSION["user"] = ["fname"=>"William","lname"=>"Henry" ];
if(isset($_GET["delete"]))
{
if($_GET["key"])
{
$key=$_GET["key"];
unset($_SESSION['user'][$key]);
}
}
?>
HTML on the same page
<h1>
<?php
if(isset($_SESSION["user"]["fname"]))echo $_SESSION["user"]["fname"]." ";
if(isset($_SESSION["user"]["lname"]))echo $_SESSION["user"]["lname"];
?>
</h1>
Delete First Name
If you want to delete the lastname (lname), change the key=lname in the href of the link, hope this example helps in your case
Modify your HTML
<table class="table">
<?php foreach($_SESSION["product"] as $key => $items) { ?>
<tr>
<th width="250px"><?php echo $items['nomProduct']; ?></th>
<td><?php echo $items['category']; ?></td>
<td style="text-align: right">Delete<td>
</tr>
<?php }?>
</table>
Catch the array key and remove it from session array.
$key = filter_input(INPUT_GET, 'key');
unset($_SESSION['product'][$key]);
I am working on a point of sale for a hardware shop developed using codeigniter framework.
I want to come up with a detailed statement of account like in the picture below
I have two tables which are sales and payment, I want to get data from this two tables so that I can generate a statement of account of a specific customer.
This will help since the customer can be able to see all the items he/she bought on cash basis or credit basis and also show how much they has paid grouped by date.
It will also be possible to calculate the amount due.
I can be able to list separately (sales and payment) by using the below code and thereby calculate the amount due.
Sales
<?php
$salestotal = 0;
if (is_array($sales) || is_object($sales))
{
foreach ($sales as $key=>$sale): {?>
<tr>
<td>
<?php echo $sale->date; ?>
</td>
<td>
<?php echo $sale->id; ?>
</td>
<td>
<?php echo $sale->grand_total; ?>
</td>
<td>
<?php echo $sale->paid; ?>
</td>
<td></td>
</tr>
<?php
if(isset($sale->grand_total))
$salestotal += $sale->grand_total;
} endforeach ; }?>
Payments
<?php
$paymentstotal = 0;
if (is_array($payments) || is_object($payments))
{
foreach ($payments as $key=>$payment): {?>
<tr>
<td>
<?php echo $payment->date; ?>
</td>
<td class="text-center">
<?php echo $payment->sale_id; ?>
</td>
<td class="text-center">
<?php echo $payment->paid_by; ?>
</td>
<td class="text-center">
<?php echo $payment->cheque_no; ?>
</td>
<td class="text-center">
<?php echo $payment->amount; ?>
</td>
<td class="text-center">
<?php echo $this->customers_model->getUserna($payment->created_by); ?>
</td>
<td class="text-center">
<?php echo $payment->pos_paid; ?>
</td>
<td class="text-center">
<?php echo $payment->pos_balance; ?>
</td>
<td class="text-right">
<?php echo $this->customers_model->getStorename($payment->store_id); ?>
</td>
</tr>
<?php
if(isset($payment->amount))
$paymentstotal += $payment->amount;
} endforeach ;}?>
My controller
function statement($id = NULL)
{
if (!$this->Admin) {
$this->session->set_flashdata('error', $this->lang->line('access_denied'));
redirect('pos');
}
if($this->input->get('id')) { $id = $this->input->get('id', TRUE); }
$this->data['sales'] = $this->customers_model->getSales($id);
$this->data['payments'] = $this->customers_model->getPayments($id);
$this->data['customer'] = $this->customers_model->getCustomername($id);
$this->data['customer_id'] = $id;
$this->page_cons('customers/statement', $this->data);
}
My Model
public function getSales($id)
{
$q = $this->db->get_where('sales', array('customer_id' => $id));
if( $q->num_rows() > 0 ) {
return $q->result();
}
return FALSE;
}
public function getPayments($id)
{
$q = $this->db->get_where('payments', array('customer_id' => $id));
if( $q->num_rows() > 0 ) {
return $q->result();
}
return FALSE;
}
How do I combine this two tables?
I hope I am clear enough on this question, I have been trying to Google but I have no luck.
I will appreciate any help. Thanks
Edit
MYSQL tables
Sales
Payments
You can reuse the given methods getSales() and getPayments() in a new method getSalesWithPayments() to combine the two results:
public function getSalesWithPayments($customerId) {
$sales = [];
foreach ($this->getSales($customerId) as $sale) {
$sale->payment = null;
$sales[$sale->id] = $sale;
}
foreach ($this->getPayments($customerId) as $payment) {
$sales[$payment->sale_id]->payment = $payment;
}
return $sales;
}
In the first loop you initialize $sale->payment (for the case that a sale has no payment yet) and populate the $sales array, which is indexed by the id column. This way you can easily access any sale by its id without an expensive search.
In the second loop you assign the payments to the corresponding sales.
Note that you might need to add logic to handle empty results (which wouldn't be necessary if you would just return an empty array instead of FALSE).
You can now use it like..
Controller:
$this->data['sales'] = $this->customers_model->getSalesWithPayments($id);
View:
<?php foreach ($sales as $sale): ?>
<tr>
<td><?php echo $sale->date; ?></td>
<td><?php echo $sale->id; ?></td>
...
<?php if($sale->payment !== null): ?>
<td><?php echo $sale->payment->date; ?></td>
<td><?php echo $sale->payment->amount; ?></td>
...
<?php else: // empty cells if no payment ?>
<td></td>
<td></td>
...
<?php endif; ?>
</tr>
<?php endforeach; ?>
If you post the sales and payments table the answer will be better and exact. You can use MySQL Join to get combined data from 2 tables.
Try this:
$this->db->select('*')
->from('sales')
->join('payments', 'payments.sales_id = sales.id', 'right')
->where(array('sales.customer_id' => $id));
$results = $this->db->get();
I am practicing with PHP, and as a result, I ended up creating a dummy online store. I managed to implement most of the online functionality, but I am struggling with the shopping cart.
Once the user logs in and enters the product area of the site, I want the user to be able to add items to a cart. I have been following a phpAcademy YouTube tutorial. I've managed to display all the products with an add button/hyperlink to link each product to a processing page called cart.php. Each button's link matches their associated product ID.
When I test this and click "add", the ID of the product does not appear on the cart.php page.
user_man_boxing_gloves.php:
<?php
session_start();
include('connect_mysql.php');
$product_name = 'product_name';
$product_qua = 'product_qua';
$product_price = 'product_price';
$product_image = 'product_image';
$product_des = 'product_des';
$get = mysql_query("SELECT product_id, product_image, product_name, product_des, product_price, product_type FROM products WHERE product_type='ManGloves' AND product_qua > 0 ORDER BY product_id DESC");
if(mysql_num_rows($get) == 0)
{
echo "There are no Products to display";
}
else
{
?>
<?php
while($get_row = mysql_fetch_assoc($get))
{
?>
<table id='display'>
<tr><td><?php echo "<img src=$get_row[$product_image] class='grow'>" ?></td></tr>
<tr>
<th></th>
<th><strong>Avalible</strong></th>
<th><strong>Price</strong></th>
<th><strong>Description</strong></th>
</tr>
<tr>
<td width='290px'><?php echo "$get_row[$product_name]" ?></td>
<td width='290px'><?php echo "$get_row[$product_qua]" ?></td>
<td width='290px'><?php echo "$get_row[$product_price]" ?></td>
<td width='290px'><?php echo "$get_row[$product_des]" ?></td>
</tr>
<tr>
<td><?php echo 'Add'; ?></td>
</tr>
</table>
<?php
}
}
?>
cart.php:
<?php
if(isset($_GET['add'])){
$_SESSION['cart_'.$_GET['add']]+='1';
}
echo $_SESSION['cart_'];
?>
I want to display the product ID to see if my code works, and I want to do further processing after verifying that it works.
Looking at the screenshot, it appears that the add button correctly shows the product ID.
It looks like the issue in cart.php deals with the following snippet:
if(isset($_GET['add'])){
$_SESSION['cart_'.$_GET['add']]+='1';
}
Working this out, this would mean that with an ID of 1, you could see the following in your session array:
$_SESSION['cart_1'] = 1;
$_SESSION['cart_2'] = 4;
What you probably want, for the display, is to store an array into cart. That is,
if(isset($_SESSION['cart']))
{
$arr = unserialize($_SESSION['cart']);
}
else
{
$arr = array();
}
if(isset($_GET['add'])){
$arr[$_GET['add']] += 1;
}
$_SESSION['cart'] = serialize($arr);
var_dump(unserialize($_SESSION['cart']));
I need to generate a table with php, that will display the images - names stored on database. It has to display 3 images in a row. The images are added to the database all the time, so I need that to be automatically generated, instead of hard coding the tables. I am not sure how do I do that? Please help!
You need to cycle the result recordset and print out the new row every 3rd element.
For example:
<table>
<tr>
<?php $i=0; foreach ($images as $image): ?>
<td><?php echo $image['name'] ?> <img src="<?php echo $image['path'] ?>" /></td>
<?php if(++$i%3==0): ?>
</tr><tr>
<?php endif ?>
<?php endforeach ?>
</tr>
</table>
suppose u get the all images name from database in an array
$img_array = array(
1=>'f.jpg',
2=>'s.jpg',
3=>'t.jpg',
4=>'f.jpg',
5=>'e.jpg'
);
// now create dynamic table via php
<table border="1" cellpadding="2" cellspacing="2" width="100%">
<tr>
<?php
$i=0;
foreach($img_array as $k){
if($i%3==0) { ?> </tr><tr> <?php } ?>
<td><img src="<?php echo $k?>" border="0"></td>
<?php $i++; } ?>
</tr>
</table>
Note: please write full path of image in src before <?php echo $k?>
Iterate the image records, using modulo 3 to change to the next table row.
Something like:
echo '<table><tr>';
foreach ($images) {
echo '<td>$image</td>';
if ($i % 3 == 0) {
echo '</tr><tr>';
}
}
echo '</tr></table>';
A simple table like that would be like
<table>
<tr><td>1</td><td>2</td><td>3</td></tr>
<tr><td>1</td><td>2</td><td>3</td></tr>
</table>
To generate this automatically you need to store where you are in the table, first col, 2nd col or 3th col.
<?php
$pos = 1;
print "<table>"
for ($i=0; $i<=10;$i++)
{
if ($pos==1)
{
print "<tr><td>1</td>";
$pos=2;
}
else if ($pos==2)
{
print "<td>2</td>";
$pos=3;
}
else if ($pos==3)
{
print "<td>3</td></tr>";
$pos=1;
}
}
if ($pos==2 || $pos==3)
print "</tr>";
print "</table>"
Keep in mind that if you use the options with $i%3 from the other comments, that your table will start end/or finish with an empty row. This would need additional checks. The $i%3 will give the remainder of the division of $i and 3. So when $i/3 == 0, means it is true on every third row.
I have been on this for a day now and still cant solve it though it should be quite simple. I have a php code.
foreach($cart as $line=>$item)
{
echo form_open("sales/edit_item/$line");
?>
<td style="align:center;"><?php echo $item['name']; ?></td>
<?php if ($items_module_allowed)
{
?>
<td><?php echo form_input(array('name'=>'price','value'=>$item['price'],'size'=>'6'));?></td>
<?php
}
else
{
?>
<td><?php echo $item['price']; ?></td>
<?php echo form_hidden('price',$item['price']); ?>
<?php
}
?>
<td>
<?php
if($item['is_serialized']==1)
{
echo $item['quantity'];
echo form_hidden('quantity',$item['quantity']);
}
else
{
echo form_input(array('name'=>'quantity','value'=>$item['quantity'],'size'=>'2'));
}
?>
</td>
</div></div>
<td><?php echo to_currency($item['price']*$item['quantity']-$item['price']*$item['quantity']*$item['discount']/100); ?></td>
<?php
if($item['allow_alt_description']==1)
{
}
else
{
if ($item['description']!='')
{
}
else
{
}
}
?>
</td>
<td> </td>
<td style="color:#2F4F4F";>
<?php
if($item['is_serialized']==1)
{
}
?>
</td>
<td colspan=3 style="text-align:left;">
<?php
if($item['is_serialized']==1)
{
}
?>
</td>
</tr>
<tr style="height:3px">
<td colspan=8 style="background-color:white"> </td>
</tr> </form>
<?php
}
}
?>
This creates fields with relevant data and works ok. I just need to get the last field and echo it, I have tried everything and it still loops through the array, or does not work. This might be simple to someone else but it has confused me for a day now.
When you use a loop, such as for or foreach or while, it will iterate over every single child element of the array until it reaches the end. You don't need to loop, you simply need to access the last member of the array, like so:
$lastLine = end( array_keys($cart) );
echo form_open("sales/edit_item/{$lastLine}");
Edit: Now that I understand a bit better:
$lastItem = array_slice($cart, -1, null, true);
$line = key($lastItem);
$item = reset($lastItem);
echo form_open("sales/edit_item/{$line}");
?>
<!-- do all your html-ish stuff here. -->