pafait's avatar

JQuery

Hello every one i have this problem which i have been trying to solve since two days. i have this jQuery keyup event function which imidiately calculate the total price of a product when the product quantity and rate is entered

 $("#rate").keyup(function(){
    rate = $(this).val();
    quantity = $("#quantity").val();
    total = rate * quantity;
    $("#total").val(total);
    
}); 

Now what i want is that the total should be calculated imiadiately the product quantity is entered . I dont want to be the one inputing the rate of the product .For that i have made a function in my controller which gets the stock_rate of the product and now i dont now how to used that function in my jQuery please can some one help me or guide me on how to do that. here is my controller function

 private function stock_rate(){
    $stock_rate =DB:: table('tbl_stock')
          ->where('stock_rate')
          ->first();
    return $stock_rate;
    }

and the save function is

  $sell->sell_total_price = $qty * $stocke=$this ->stock_rate();
0 likes
19 replies
Borisu's avatar

Just pass your stock rate to the view and then assign it in the javascript portion. Something like this:

// in the controller
return view('your_view', compact('stock_rate');

// in the v view
<script>
    // ...other stuff before
    var stock_rate = {{ $stock_rate }}
    // ...other stuff after
</script>
pafait's avatar

Hi Borisu thanks for you quick responds this is what i have done but is not working

$("#quantity").keyup(function(){
    quantity= $(this).val();
    var stock_rate = {{$stock_rate}}
    total = quantity * stock_rate;
    $("#total").val(total);
    
});
Borisu's avatar

Ok can you check in the console if there are any errors? That should help.

pafait's avatar

@Borisu this is what i get as error

Object of class Illuminate\Support\Collection could not be converted to int

phpMick's avatar

You are passing the whole collection, instead of just one field.

You need to do :

return $stock_rate->fieldname;

pafait's avatar

please i which to know if this is correct i am trying to get the price of a product on my stock table

 private function stock_rate($product_id){
    $stock_rate =DB:: table('tbl_stock')
          ->where('stock_rate',$product_id)
          ->first();
    return $stock_rate->stock_rate;
}
1 like
Borisu's avatar

@pafait It seems to me your whole stock_rate function is wrong. What are the field names on the tbl_stock table?

pafait's avatar

here is it @Borisu

stock_id int(11) NOT NULL, category_id int(11) NOT NULL DEFAULT '0', brand_id int(11) NOT NULL DEFAULT '0', product_id int(11) NOT NULL DEFAULT '0', stock_quantity int(11) NOT NULL DEFAULT '0', stock_rate int(11) NOT NULL DEFAULT '0', total_price int(11) NOT NULL DEFAULT '0', stock_paid_amount int(11) NOT NULL DEFAULT '0', stock_due_amount int(11) NOT NULL DEFAULT '0', pay_due int(11) NOT NULL DEFAULT '0', due_total int(11) NOT NULL DEFAULT '0', stock_date date NOT NULL, supplier_id int(11) NOT NULL DEFAULT '0', added_by int(11) NOT NULL DEFAULT '0', entry_time datetime NOT NULL DEFAULT CURRENT_TIMESTAMP, quantity_remains int(11) NOT NULL DEFAULT '0'

SerhiiNuzhnyi's avatar

Somehow

$ajax({
type: "POST",
url: "stock-rate-route",
success: function(data){
 $("#rate").keyup(function(){
    quantity = $("#quantity").val();
    total = data.stock_rate * quantity;
    $("#total").val(total);
    
});
}

stock-rate-route should be registered in routes php file

Borisu's avatar

Yeah as i thought, you need the following:

 private function stock_rate($product_id) {
    $stock_rate =DB:: table('tbl_stock')
          ->where('product_id', $product_id)
          ->first();
    return $stock_rate->stock_rate;
}

@SerhiiNuzhnyi that's just a dummy text for the example I think, not the actual link.

pafait's avatar

But @Borisu should the function name be same with the return value?

Borisu's avatar

You can name this function anything you want, you can also just do:

private function stock_rate($product_id) {
    return DB::table('tbl_stock')
        ->where('product_id', $product_id)
        ->first()
        ->stock_rate;
}
pafait's avatar

Hi can you pls help me on how to set the value of that function into my script this is what i have done but it seems i am getting this wrong

 $("#quantity").keyup(function(){
    quantity = $(this).val();
    var price = $(this).val(stock_rate);
    total = quantity * price ;
    $("#total").val(total);
Borisu's avatar

Ok,

$(document).ready(function() {
    $("#quantity").keyup(function(){
    quantity = parseFloat($(this).val());
    var price = parseFloat($(this).val(stock_rate));
    total = quantity * price;
    $("#total").text(total); // if it's an input field then val(total) is correct
    });
});
pafait's avatar

Hey @Borisu thanks for your help i really appreciate. I tried it but the total is not calculated ie when inputing the quantity with the keyup function the total has to be calculated. with the script i gave up when i entered a value into the quantity text box i had zero as total. i even var_dump the function in the controle and it was fine

Borisu's avatar
Borisu
Best Answer
Level 37

Oops sorry there is a mistake in the code:

// in the controller
$stock_rate = stock_rate($productId);
return view('whatever', compact('stock_rate'));

// in the view
<input id="quantity">
<input type="hidden" id="price" value="{{ $stock_rate }}">
<div id="total"></div>

// in the page script, make sure to load this after jQuery at the bottom of your view...
$(document).ready(function() {
    $("#quantity").keyup(function() {
    quantity = parseFloat($(this).val());
    price = parseFloat($('#price').val());
    total = quantity * price;
    $("#total").text(total);
    });
});
1 like
pafait's avatar

Hi pls i want to know if it posible for me getting from my stock table both the stock_rate and the stock_id of each stock. this is what i have done but nothing

 private function check_rate($product_id, $stock_id) {
$stock_rate =DB:: table('tbl_stock')
      ->where('product_id','stock_id', $product_id, $stock_id)
      ->first();
return $stock_rate->stock_rate;
 }

i get this error

Missing argument 2 for App\Http\Controllers\SellController::check_rate(), called in C:\inventorybackup\app\Http\Controllers\SellController.php on line 154 and defined

what i am trying to do is i want the total of each sales to be calculated taking into consideration the differences at the level of the stock prices. For example i have 10pens in my stock table where i baught the first five pens at a rate of 100 each and the last five at a rate of 110 each. I want that if i carry out a sale of 6pens using my FIFO method the first 5pens should be calculated at a rate of 100 each and the last pen should be at the rate of 110 since it is from the last stock i baught so that i can have a total of 610.

With this methode

private function check_rate($product_id) {
$stock_rate =DB:: table('tbl_stock')
      ->where('product_id', $product_id)
      ->first();
return $stock_rate->stock_rate;

} it uses the stock_rate of the first stock to calculate since the have same ID.

please how can i query my table in other to have the above done? thanks for your helps in advance

Borisu's avatar

Sorry, but I think you're not learning anything by getting the solution. You'll benefit more from reading the documentation and trying it yourself. Best of luck!

Please or to participate in this conversation.