i want to decrease stock product when transactions is complete, I try with foreach from data but the stock is not decreased correctly
CONTROLLER :
for ($i=0; $i < $count ; $i++) {
//SAVE DETAIL PENJUALAN
$data[] = array(
'nonota' => $this->input->post('nonota',TRUE),
'id_brg' => $this->input->post('kd_brg',TRUE)[$i],
'nama_brg' => $this->input->post('nama',TRUE)[$i],
'jml_brg' => $this->input->post('jml',TRUE)[$i],
'harga_brg' => $this->input->post('harga',TRUE)[$i],
);
//DELETE CART
$cart[] = array(
'rowid' => $this->input->post('rowid',TRUE)[$i],
'qty' => 0
);
$update[] = array(
'id' => $this->input->post('kd_brg',TRUE)[$i],
'stok' => 'stok' - $this->input->post('jml',TRUE)[$i]
);
$this->M_penjualan->updatestock($update,'tbl_barang');
}
MODEL
function updatestock($update) {
$this->db->update_batch('tbl_barang',$update,'id');
}
1.Maybe products ids are not the same as the ones you get from the iteration.
2.Take a look at this line:
'stok' => 'stok' - $this->input->post('jml',TRUE)[$i]
You change value of a string, because 'stok' is a string. It would be something else if you would make $stok - $this->input... .Of course if stock represents some number.
3.Also I would first try to update with simple update. $this->db->set('stok', 'stok-'.$stok, false); - somewhere in your model function.
Related
I have a store function that saves array items into my items table and together with that I am trying to check if the product_is is already in my Warehouse1StockSummaries. if still not, I will grab the product_id and its qty, If its there already then I want to ADD the value from the 'stock_in_qty' which is inside the array to the 'qty_in' in my Warehouse1StockSummaries. I hope my explanation make sense to you :)
here's my code.
public function store(Request $request)
{
$input = $request->all();
$items = [];
for($i=0; $i<= count($input['stock_in_qty']); $i++) {
if(empty($input['stock_in_qty'][$i]) || !is_numeric($input['stock_in_qty'][$i])) continue;
$acceptItem = [
'order_id' => $input['order_id'][$i],
'product_id' => $input['product_id'][$i],
'order_item_id' => $input['order_item_id'][$i],
'delivery_date' => $input['delivery_date'][$i],
'company_id' => $input['company_id'][$i],
'stock_in_qty' => intval($input['stock_in_qty'][$i]),
'stock_out_qty' => $input['stock_out_qty'][$i],
'transfer_to' => $input['transfer_to'][$i],
'delivery_note' => $input['delivery_note'][$i],
'user_id' => $input['user_id'][$i]
];
$product_id = $input['product_id'][$i];
$qty_in = intval($input['stock_in_qty'][$i]);
// dd($qty_in);
// ADD stock_in_qty TO QTY_IN ????
$stockSummary = Warehouse1StockSummaries::updateOrCreate(
['product_id' => $product_id ],
['qty_in' => $qty_in,
'qty_out' => null
]);
// dd($stockSummary);
array_push($items, Warehouse1stocks::create($acceptItem));
}
return redirect()->route('orders.index');
}
I check and everything is ok the only missing is the part where I need to grab the value from 'stock_in_qty' and add to 'qty_in' if the product id is already found in Warehouse1StockSummaries. Thank you so much in advance!
You could use the wasRecentlyCreated property on the model to determine if the model has just been created or not. If it hasn't then it won't have used $qty_in value, this means you could then use the increment() to add to the existing value in the database:
$stockSummary = Warehouse1StockSummaries::firstOrCreate(
['product_id' => $product_id ],
['qty_in' => $qty_in, 'qty_out' => null]
);
if (!$stockSummary->wasRecentlyCreated) {
$stockSummary->increment('qty_in', $qty_in);
}
I'm working on an Online Store project using PHP and MySQLi. Most of this project is done and now I'm struggling with total price that a user must pay. This total price is showing in the cart.php page (where users can see all the product items that they have added earlier). So it must be set to the total price of all current items.
So here is the code of cart.php:
if(isset($_GET['cart_id']))
{
$cart_id = $_GET['cart_id'];
$get_add = "SELECT * FROM cart WHERE cart_id = '$cart_id'";
$run_add = mysqli_query($con,$get_add);
$cart_items = [];
while ($row_results = mysqli_fetch_array($run_add)){
$item = array(
'table_id' => $row_results['table_id'],
'cart_id' => $row_results['cart_id'],
'pro_id' => $row_results['product_id'],
'pro_title' => $row_results['product_title'],
'pro_price' => $row_results['product_price'],
'pro_img' => $row_results['product_image'],
'pro_supplier' => $row_results['product_supplier'],
'qty' => $row_results['qty'],
'cart_ip' => $row_results['cart_ip'],
);
$cart_items[] = $item;
}
foreach ($cart_items as $cart) {
echo $cart['pro_price']
}
}
The table cart structure goes like this:
see image here
So now I want to print the sum of all products as total_price, till now I tried several different ways but I could not get the result.
So if you know how to solve this question, please let me know.. I really really appreciate that...
Thanks in advance!
Assuming you need the sum of qty*product_price you could sum this way
$tot = 0;
while ($row_results = mysqli_fetch_array($run_add)){
$cart_items[] = array(
'table_id' => $row_results['table_id'],
'cart_id' => $row_results['cart_id'],
'pro_id' => $row_results['product_id'],
'pro_title' => $row_results['product_title'],
'pro_price' => $row_results['product_price'],
'pro_img' => $row_results['product_image'],
'pro_supplier' => $row_results['product_supplier'],
'qty' => $row_results['qty'],
'cart_ip' => $row_results['cart_ip'],
);
$tot += $row_results['qty']*$row_results['product_price'];
}
foreach ($cart_items as $cart) {
echo $cart['pro_price'];
}
echo $tot;
I am doing a shopping cart using code-igniter. While I can do the add cart functions, I'm somewhat confused with how to update them with regards to database. All I want is when I change the item quantity in the cart and click update ,both the cart and database must be updated for that particular item.I tried to do it but couldn't get it.Can someone please enlighten me what to do?
controller code
$this->load->model('user_m');
$result['query'] = $this->user_m->get($id); // inserts coresponing item
foreach ($result['query'] as $row)
$id = $row->pid;
$qty = $a;
$quan=$row->quantity;
$price = $row->pprice;
$name = $row->pname;
$q=$quan-$a; // for remainig stock i.e total qty-user qty
$data = array(
'id' => $id,
'qty' => $qty,
'price' => $price,
'name' => $name,
'stock' =>$q
);
$this->cart->insert($data);
$this->load->model('user_m');
$result['qry'] = $this->user_m->up_cart($id,$q);
redirect($_SERVER['HTTP_REFERER']);
}
just tell me how to update pls!
when ever you add any product in cart, it will generate an uniq row ID and that will be unique for each and every product and hence you need to update quantity by that row ID
$data = array(
'rowid' => 'b99ccdf16028f015540f341130b6d8ec',
'qty' => 3
);
$this->cart->update($data);
// Or a multi-dimensional array
$data = array(
array(
'rowid' => 'b99ccdf16028f015540f341130b6d8ec',
'qty' => 3
),
array(
'rowid' => 'xw82g9q3r495893iajdh473990rikw23',
'qty' => 4
),
array(
'rowid' => 'fh4kdkkkaoe30njgoe92rkdkkobec333',
'qty' => 2
)
);
$this->cart->update($data);
Now you want to know about how you will get row id.
$cart=$this->cart->contents();
it will return whole array of cart and when you listing it on webpage, please associate row id with quantity text box to update that particular product.
please check following url:
https://ellislab.com/codeigniter/user-guide/libraries/cart.html
I have the following function to add products to shopping cart:
function AddToCart($pid)
{
$_SESSION['products'][]['product_id'] = $pid;
}
What is the best way to store product quantity to the same array with the product id, since the array after the ['products'] generates automatically I'm confused how can I assign quantity there? Thank you for your help:)
Why not store all of the information you need at the same time? Here is an excerpt from my own cart:
// add item to cart
$_SESSION['cart'][] = array('product_id' => $product_id,
'cat_id' => $cat_id,
'title' => $title,
'default_img' => $default_img,
'price' => $valid_product['base_price'],
'weight' => $valid_product['weight'],
'length' => $valid_product['length'],
'width' => $valid_product['width'],
'height' => $valid_product['height'],
'surcharge' => $surcharge,
'quantity' => $quantity,
'prop_selection' => $prop_selection,
'po_id' => $po_id,
'stock' => $stock,
'shipper_id' => $valid_product['shipper_id']);
If I understand the question correctly, when AddToCart() is called for a $pid that is already in the array, do you just want it to increment a quantity property for that product? Seems to me you just will need to use an associative array for $_SESSION['products'] instead of a numerically indexed array like you are now:
function AddToCart($pid, $quantity=1) {
if(is_array($_SESSION['products']) && array_key_exists($pid, $_SESSION['products'])) {
$_SESSION['products'][$pid]['quantity'] += $quantity;
} else {
$_SESSION['products'][$pid] = array(
'product_id' => $pid,
'quantity' => $quantity
);
}
}
How can I get the last inserted ID of a query using the batch insert in CodeIgniter. I used the code $this->db->insert_id() but it returns the ID of my first inserted array. I can't get the last insert.
Here's what I did:
for ($x = 0; $x < sizeof($filtername); $x++) {
$orders[] = array(
'poid' => null,
'order_id' => $poid,
'item_desc' => $filtername[$x],
'item_qty' => $filterquantity[$x],
'item_price' => $filterprice[$x],
'total' => $filtertotal[$x],
'cash_on_delivery' => $val_delivery,
'is_check' => $val_check,
'bank_transfer' => $val_transfer,
'transaction_date' => $dateorder
);
}
$this->db->insert_batch('po_order', $orders);
echo $this->db->insert_id(); //will return the first insert array
I can't spot where's my error. My last option is to get it using a query. I also did mysql_insert_id() but always returns to 0.
I think the best way would be to use the batch insert instead of individual inserts in a loop for performance , but to get the last insert id, ADD the First Insert ID & the Affected Rows.
$this->db->insert_batch('po_order', $orders);
$total_affected_rows = $this->db->affected_rows();
$first_insert_id = $this->db->insert_id();
$last_id = ($first_insert_id + $total_affected_rows - 1);
You will need to do something like this,
$insertIds = array();
for ($x = 0; $x < sizeof($filtername); $x++) {
$orders = array(
'poid' => null,
'order_id' => $poid,
'item_desc' => $filtername[$x],
'item_qty' => $filterquantity[$x],
'item_price' => $filterprice[$x],
'total' => $filtertotal[$x],
'cash_on_delivery' => $val_delivery,
'is_check' => $val_check,
'bank_transfer' => $val_transfer,
'transaction_date' => $dateorder
);
$this->db->insert('po_order', $orders);
$insertIds[$x] = $this->db->insert_id(); //will return the first insert array
}
print_r($insertIds); //print all insert ids