CodeIgniter, update stock - php

i need some help...
I have a problem with codeigniter, i have this code by myself
public function venderProduto($id)
{
$data=array('id_venda' => null,
'id_cliente' => $id_cliente,
'medicamento' => $medicamento,
'quantidade' => $quantidade,
'preco' => $preco);
$this->db->where('id_venda', $id);
$this->db->select('produto.stock,vendas.quantidade');
$this->db->insert('vendas', $data);
$this->db->update('stock-quantidade');
}
I need to upgrade the stock of my product that is on the table products(produtos) when I insert a sale on my table sales(vendas), want to take the amount that will see the stock...
Someone help me pls...

It looks like your mixing up your queries. One needs to be completed and then the second can be performed.
If I understand you correctly you want to insert a record into the sales table and then update the products table.
Function does not show how values for id_cliente, medicamento, quantidade, and preco are getting into function. They would either need to be set as function parameters or supplied as GET or POST vars.
public function venderProduto($id, $id_cliente, $medicamento, $quantidade, $preco)
{
$data = array('id_venda' => null,
'id_cliente' => $id_cliente,
'medicamento' => $medicamento,
'quantidade' => $quantidade,
'preco' => $preco);
$this->db->insert('vendas', $data);
//test to make sure that insert was successful
if ($this->db->insert_id())
{
//get existing stock quantity
$this->db->select('stock-quantidade');
$this->db->where('id_venda', $id);
$query = $this->db->get('produtos');
$row = $query->row();
$quantity_in_stock = $row->stock-quantidade;
//I am assuming that $quantidade is the quantity of the order
$new_quantity_in_stock = $quantity_in_stock - $quantidade;
//update products table
$this->db->where('id_venda', $id);
$this->db->update('produtos', array('stock-quantidade' => $new_quantity_in_stock ));
}
}
The above may or may not work exactly as shown but this IS the logic and order you should use. If you have trouble you need to post any error messages you receive.
So the order of this is:
insert sale.
test to make sure insert was successful.
get existing quantity of stock.
Subtract order quantity from stock quantity.
Update stock quantity.

Related

Creating default object from empty value Yii2

Using yii2 trying make success callback for payments. Callback work, but i need make changes for order. In my common/congig/main.php:
'successCallback' => function($invoice) {
$order = \common\models\Checkout::findOne($invoice->order_id);
$order->payment_status = 1;
$order->update();
}
$invoice->order_id; receives current order id, i need change payment status for checkout model.
Update:
can I somehow run it recursively? for example, if I have several records with one ID?
The problem in your code is that findOne() requires the name of the column to compare the value, in this case, the Checkout's ID column.
Assuming it's order_id like in invoice table.
The code will be like this:
'successCallback' => function($invoice) {
$order = \common\models\Checkout::findOne(['order_id' => $invoice->order_id]);
$order->payment_status = 1;
$order->update();
}
Replace 'order_id' with the checkout's id column if it has a different name.
Update
To update multiple records you could do something like this:
'successCallback' => function($invoice) {
\common\models\Checkout::updateAll(['payment_status'=>1],['order_id' => $invoice->order_id]);
}
Make a DB backup before testing this code.

Laravel update specific column in database 'cost' and add supplier_id to another database table

i'm working with sensitive data hope you can help find if there any wrong in code's writing
i have list of suppliers in my database i added column 'cost'
i'm trying to update and insert cost for existing suppliers from specific query
and i created model and migration to get foreign keys too by adding the puled supplier id from the query
....
$suppliers_data = $suppliers_query->fetchall(PDO::FETCH_ASSOC);
foreach ($suppliers_data as $supplier_data) {
$supplier_name = $supplier_data['supplier_name'];
$cost_rate = $supplier_data['Cost'];
if (!Supplier::where('supplier', $supplier_name)->exists()) {
Supplier::insert([
'supplier' => $supplier_name,
'cost_rate' => $cost_rate
]);
} else {
Supplier::update([
'cost_rate' => $cost_rate // does this will update cost for the current supplier ?
]);
}
$supplier_id = Supplier::where('supplier', $supplier_name)->pluck('supplier_id');
Test::insert($supplier_id);
}
$supplier_count = test::count();
Test::update(['test_data_count' => $supplier_count]);
Updating table data with supplier name is not correct here I believe. Instead of using supplier name in where condition using particular supplier id is recommended for better application. Names can be duplicate so its not a good idea to use supplier name in where.
In your current code I have 2 things to say :
You need to add where in update eloquent to work properly
Supplier::where('supplier', $supplier_name)
->update([
'cost_rate' => $cost_rate // this will update cost for the current supplier
]);
Or to minimalize the code you can use updateorCreate method instead of making insert and update in the if() else() condition
Supplier::updateOrCreate(
['supplier' => $supplier_name],
['cost_rate' => $cost_rate]
);

Make a discount on a price list form a db using PHP and CodeIgniter

I have a view with a form to input a percentage of discount/rise and apply it to a list of products stored in a table of a database, here is the code of the controller which I explain below:
$rise = 1+$this->input->post('rise')/100;
$this->db->where('client',$this->session->userdata('id_client'));
$query = $this->db->get('products');
foreach ($query->result() as $row){
$price = array(
'price' => $row->price*$rise
);
$this->db->where('id',$row->id);
$this->db->update('products',$price);
}
redirect('site/select_client');
First set the rise, then get from the products table all the ones that correspond to the client that we are working on (stored as session variable), an for each one set the array to update and update in for the product that has the corresponding id. (the table has the columns id, client, name and price). After that it should redirect to another controller.
It shows no error but no changes are made on the database.
I modified your code, please use this and check. it will work correctly.
$rise = $this->input->post('rise')/100;
$this->db->where('client',$this->session->userdata('id_client'));
$query = $this->db->get('products');
foreach ($query->result() as $row){
$price=$row->price + ($row->price * $rise);
$price = array(
'price' => $price
);
$this->db->where('id',$row->id);
$this->db->update('products',$price);
}

Adding new column to ordered products in osCommerce

I have a store using osCommerce with the Dynamo one page checkout addon. I added a new column called "drop_ship_id" to the orders_products table and products table. Under the checkout_process.php file is where I believe the products that the user has ordered, are entered into the orders_products table in the database. I'm trying to pull the drop_ship_id field from each ordered product, and enter it into the orders_products table where I can call on it later on in the checkout process. Here is my current code snip:
// initialized for the email confirmation
$products_ordered = '';
$subtotal = 0;
$total_tax = 0;
for ($i=0; $i<sizeof($order->products); $i++) {
if (!in_array($payment, $suspended_payment)) {
$checkout->reduce_stock($order->products[$i]);
}
$sql_data_array = array('orders_id' => $insert_id,
'products_id' => tep_get_prid($order->products[$i]['id']),
'products_model' => $order->products[$i]['model'],
'products_name' => $order->products[$i]['name'],
'products_price' => $order->products[$i]['price'],
'final_price' => $order->products[$i]['final_price'],
'products_tax' => $order->products[$i]['tax'],
'products_quantity' => $order->products[$i]['qty'],
'drop_ship_id' => $order->products[$i]['drop_ship_id']);
tep_db_perform($database['ORDERS_PRODUCTS'], $sql_data_array);
$order_products_id = tep_db_insert_id();
Not sure if this is where it belongs, but I'm unable to get it to transfer the drop_ship_id field with this code. Does anyone know a good way of getting this to work?
You can use the error log to try and dump some information to the logs:
error_log("product id: " . $order->products[$i]['products_id'] . ", dropship order id: " . $order->products[$i]['drop_ship_id']);
As to why the value is not being transferred, I would check a few things in includes/classes/order.php - when creating a new order, it will copy the values from the cart's list of products. So you should ensure either:
the products in the cart contain this field (i.e. this field should be one of the select values in the SQL when the product is retrieved from the database and put into the cart).
you add the field in afterwards by explicitly writing the value within order.php.
And later on, when loading a saved order from the database, you should ensure you add the drop_ship_id to your SQL query.

Codeigniter cart can't insert data

I'm trying to add some data to a codeigniter (HMVC codeigniter) chopping cart and display it, i'm using this method in the main cart controller:
function add_to_cart(){
$this->load->library('cart');
// Get data
$userID = $this->input->post('userID');
$eventID = $this->input->post('eventID');
$tickedID = $this->input->post('tickedID');
// Get ticket data
$this->load->module('ticket');
$ticket_query = $this->ticket->get_where($tickedID);
//echo $this->session->all_userdata();
foreach($ticket_query->result() as $ticket_data){
$ticketPrice = $ticket_data->price;
$ticketCategory = $ticket_data->category;
}
//echo 'tickedID: '.$tickedID.' price: '.$ticketPrice.' category: '.$ticketCategory;
// Add item to cart
$data_items = array(
'id' => $tickedID,
'qty' => 1,
'price' => $ticketPrice,
'category' => $ticketCategory,
'options' => array()
);
$this->cart->insert($data_items);
$cart = $this->cart->contents();
echo '<pre>';
echo print_r($cart);
echo '</pre>';
}
Basically i'm getting the userID, eventID and tickedID variables from the session, then I run a query to get the ticked with the specific id. I run through the results of the query and get the $thicketPrice and $ticketCategory variables from it. Then I attempt to set the variables in $data_items to insert in the cart itself. FInally I attempt to echo the contents of the care and all I get is an empty array.
The session, database and cart libraries are all autoloaded and the sessions are using the database, they have the ci_sessions table. THe sessions also have an ecrypted key, what is wrong?
Some attention for successful cart insert:
'price' > 0
'name' (or similar field) better not in unicode
You need a name index as it's mandatory.
id - Each product in your store must have a unique identifier. Typically this will be an "sku" or other such identifier.
qty - The quantity being purchased.
price - The price of the item.
name - The name of the item.
options - Any additional attributes that are needed to identify the product. These must be passed via an array.
Important: The first four array indexes above (id, qty, price, and name) are required. If you omit any of them the data will not be saved to the cart. The fifth index (options) is optional. It is intended to be used in cases where your product has options associated with it. Use an array for options, as shown above.
From http://ellislab.com/codeigniter/user-guide/libraries/cart.html
So, something like this then:
$data_items = array(
'id' => $tickedID,
'qty' => 1,
'price' => $ticketPrice,
'name' => $someName,
'options' => array('category'=>$ticketCategory)
);

Categories