The sales notifications is not working because this line amountArray[$idproduct] += $amount; is returning me offset. I don't know how to fix it.
My full function is this:
function saveAllSaleDetails($idsale, $sale) {
$this->conexion->startTransaction();
$amountArray = [];
try {
foreach ($sale as $detail):
$idproduct = $detail['id'];
$amount = $detail['amount'];
$price = $detail['price'];
$subtotal = $detail['subtotal'];
$iduser = 1;
$this->saveSaleDetail($idsale, $idproduct, $amount, $price, $subtotal, $iduser);
$amountArray[$idproduct] += $amount;
$stock = $this->product->getProductStock($idproduct);
$stock = $stock[0][0] - $amountArray[$idproduct];
if ($stock <= 20) {
$product = $this->product->getProductById($idproduct);
$message = $product[0][1]." stock is bellow 20.";
notification::add($message, $idproduct, 'warning', 'product.php');
}
endforeach;
$this->conexion->commit();
$this->conexion->cerrar();
return true;
} catch (Exception $e) {
$this->conexion->rollback();
$this->conexion->cerrar();
var_dump($e->getMessage());
return false;
}
}
The problem is because of this line,
$amountArray[$idproduct] += $amount;
The above statement can be expanded as
$amountArray[$idproduct] = $amountArray[$idproduct] + $amount;
Initially during the first iteration of foreach loop, $amountArray[1] and $amountArray[2] are not set, and hence you're getting these undefined offset error.
So instead of
$amountArray[$idproduct] += $amount;
Do this:
$amountArray[$idproduct] = isset($amountArray[$idproduct]) ? $amountArray[$idproduct] + $amount : $amount;
Related
i have bill and product_bill tables.
i need to use bill_id in product_bill table and i should create bill first ,
so id did this
public function store(Request $request)
{
if( Bill::first() == null )
{
$bill_id = 1;
}
else {
$bill_id = Bill::orderBy('id','desc')->first()->id +1 ;
}
// dd($bill_id);
$brand_id = auth('brand')->id();
$bill = new Bill();
$bill->brand_id = $brand_id;
$bill->date = Carbon::today();
$data = $request->all();
$products = [];
$total = 0;
for($i = 0 ; $i<count($data['id']) ; $i++)
{
$prod = new ProductsBill();
$prod->product_id = $data['id'][$i] ;
$prod->quantity = $data['quantity'][$i];
$prod->bill_id = $bill_id;
$products[] = $prod->attributesToArray();
$price = Product::find($prod->product_id)->price;
$total += $price * $prod->quantity;
}
$bill->total = $total;
$bill->save();
ProductsBill::insert($products);
return redirect()->back();
}
when i delete any bill the new bill_id in product_bill table get the next number for last one,
i need to know the id will be create next
thanks.
If you use save() function on your $bill variable, you will get the id by accessing $bill->id. If you want to ensure that you transaction is completed, you could use DB transactions.
use DB;
class ... {
...
public function store(Request $request)
{
$brand_id = auth('brand')->id();
$bill = new Bill();
$bill->brand_id = $brand_id;
$bill->date = Carbon::today();
DB::beginTransaction();
try{
$bill->save();
$data = $request->all();
$products = [];
$total = 0;
for($i = 0 ; $i<count($data['id']) ; $i++)
{
$prod = new ProductsBill();
$prod->product_id = $data['id'][$i] ;
$prod->quantity = $data['quantity'][$i];
$prod->bill_id = $bill->id;
$products[] = $prod->attributesToArray();
$price = Product::find($prod->product_id)->price;
$total += $price * $prod->quantity;
}
$bill->total = $total;
$bill->save();
ProductsBill::insert($products);
DB::commit();
return redirect()->back();
} catch(\Illuminate\Database\QueryException $ex){
DB::rollBack();
// Exception redirect here
}
}
...
}
If I understand your issue correctly, you could probably use the mysql functions to get the last inserted ID and add one to that.
https://dev.mysql.com/doc/refman/8.0/en/information-functions.html#function_last-insert-id
I need to loop through all pending orders "DONE"
then loop through each order items and save them to an array DONE
then call the api which will return a response like this DONE
{"response":
[{"Lookup":"SKU","Quantity":3,"StoreName":"Shop1"},
{"Lookup":"SKU","Quantity":30,"StoreName":"Shop2"},
{"Lookup":"SKU","Quantity":15,"StoreName":"Shop3"},
{"Lookup":"SKU","Quantity":50,"StoreName":"Shop4"}]}
after that i need to check the quantity from Shop3 first, if it is bigger than the ordered amount, then take it from there and save the new quantity, if there is still remaining quantity, then loop through the rest of the shops, and take from the shop that have the most quantity, and this where I'm Stuck. if I looped through all stores and there is still quantity to get ordered, i should report it too.
What I have done so far is this.
$requiredItems--> is an array of (sku => Ordered Quantity) which is accurate
$report = array();
$allItemsStoreQty = array();
$unavailableItems = array();
foreach ($requiredItems as $sku => $qty) {
$remainingQty = $qty;
$itemStoreQty = array();
//Get Stock Breakdown
$stock = $this->getStock($sku);
if ($stock['Shop3'] >= $qty) {
$storeQty = array();
$storeQty['Shop3'] = $qty;
array_push($itemStoreQty, $storeQty);
} else {
if ($stock['Shop3'] < $qty) {
$storeQty = array();
$storeQty['Shop3'] = $qty;
$remainingQty = $qty - $stock['Shop3'];
$stock['Shop3'] = 0;
array_push($itemStoreQty, $storeQty);
}
arsort($stock);
foreach ($stock as $storeStockName => $storeStockQty) {
if ($remainingQty > 0 && $storeStockQty > 0) {
$currentQty = $remainingQty - $storeStockQty;
if ($currentQty <= 0) {
$storeQty = array();
$storeQty[$storeStockName] = $remainingQty;
array_push($itemStoreQty, $storeQty);
$remainingQty = 0;
} else {
$storeQty = array();
$storeQty[$storeStockName] = $currentQty;
array_push($itemStoreQty, $storeQty);
$remainingQty = $currentQty;
}
}
}
$allItemsStoreQty[$sku] = $itemStoreQty;
}
if ($remainingQty > 0) {
$unavailableItems[$sku] = $remainingQty;
}
}
$report['available'] = $allItemsStoreQty;
$report['unavailable'] = $unavailableItems;
return $report;
but the report that is coming in wrong numbers, I don't know where is my mistake, I know it's somewhere in the logic :( But I can't find it.
as far as i can see, the problem is:
if ($stock['Shop3'] >= $qty) {
$storeQty = array();
$storeQty['Shop3'] = $stock['shop3']; // not $qty
array_push($itemStoreQty, $storeQty);
}
and
} else {
$storeQty = array();
$storeQty[$storeStockName] = $storeStockQty; // not $currentQty;
array_push($itemStoreQty, $storeQty);
$remainingQty = $currentQty;
}
I wanted to make a simple calculations to summarized what I purchased.
Using $_GET every time the value is updated it should save in an array then when 'start' is executed, it gives the sum. Sorry the codes here are just googled and I'm not really a programmer. I don't know how to combine the two sets of code (array + sum).
$number = $_GET ['input'];
$arr = array ($number);
$data = array($arr);
foreach ($tareas as $tarea) {
$data[] = $tarea;
}
var_dump($data);
$sum = 0;
foreach($group as $key=>$arr) {
$sum+= $arr;
}
echo $sum;
So I got this code from withinweb.com and modified to make it simpler.
<?php session_start();
$products = array($_GET["prod"]);
$amounts = array($_GET ["cost"]);
if ( !isset($_SESSION["total"]) ) {
$_SESSION["total"] = 0;
for ($i=0; $i< count($products); $i++) {
// $_SESSION["qty"][$i] = 0;
$_SESSION["amounts"][$i] = 0;
}
}
//---------------------------
//Reset
if ( isset($_GET['reset']) )
{
if ($_GET["reset"] == 'true')
{
unset($_SESSION["qty"]); //The quantity for each product
unset($_SESSION["amounts"]); //The amount from each product
unset($_SESSION["total"]); //The total cost
unset($_SESSION["cart"]); //Which item has been chosen
}
}
//---------------------------
//Add
if ( isset($_GET["add"]) )
{
$i = $_GET["add"];
$qty = $_SESSION["qty"][$i] + 1;
$_SESSION["amounts"][$i] = $amounts[$i] * $qty;
$_SESSION["cart"][$i] = $i;
$_SESSION["qty"][$i] = $qty;
}
//---------------------------
//Delete
if ( isset($_GET["delete"]) )
{
$i = $_GET["delete"];
$qty = $_SESSION["qty"][$i];
$qty--;
$_SESSION["qty"][$i] = $qty;
//remove item if quantity is zero
if ($qty == 0) {
$_SESSION["amounts"][$i] = 0;
unset($_SESSION["cart"][$i]);
}
else
{
$_SESSION["amounts"][$i] = $amounts[$i] * $qty;
}
}
//cart
if ( isset($_SESSION["cart"]) ) {
$total = 0;
foreach ( $_SESSION["cart"] as $i ) {
echo '' . $products[$_SESSION["cart"][$i]] . ' - ' . $_SESSION["amounts"][$i] . '<br>';
$total = $total + $_SESSION["amounts"][$i];
}
$_SESSION["total"] = $total;
echo'
<br>
Total : ' . $total . '
';
}
?>
When I input ?add=0&prod=apple&cost=100, it gives me:
apple - 100
Total : 100
But when I add another session, ?add=1&prod=orange&cost=200 it doesn't give the right answer.
orange - 100
- 0
Total : 100
It should return me this value, I'm puzzled where could be the error.
apple - 100
orange - 200
Total : 300
Yes, I'm not a coder, but trying to solve a big problem.. :) Thanks for those who help.
I Need to restrict the tax amount. so i went Mage/Tax/Model/Calculation.php
Then Finds calcTaxAmount() Tax Applying Function. I need to restrict tax who all are enter the tax vatid in checkout onepage tax should be zero
so
public function calcTaxAmount($price, $taxRate, $priceIncludeTax = false, $round = true)
{
$billing = Mage::getModel('checkout/session')->getQuote()->getCustomerTaxvat();
if($billing != "" )
{
return 0;
}
$taxRate = $taxRate / 100;
if ($priceIncludeTax) {
$amount = $price * (1 - 1 / (1 + $taxRate));
} else {
$amount = $price * $taxRate;
}
if ($round) {
return $this->round($amount);
}
return $amount;
}
I added the new condition. its working some stores of multistores. Only one store cannot working properly. it causes user cannot register , and addtocart not working for that particular store. i found getQuote the issue. i remove the new condtion like below working fine.
Old Function:-
public function calcTaxAmount($price, $taxRate, $priceIncludeTax = false, $round = true)
{
$taxRate = $taxRate / 100;
if ($priceIncludeTax) {
$amount = $price * (1 - 1 / (1 + $taxRate));
} else {
$amount = $price * $taxRate;
}
if ($round) {
return $this->round($amount);
}
return $amount;
}
Try below code hope this helps.
public function calcTaxAmount($price, $taxRate, $priceIncludeTax = false, $round = true)
{
$currenQuoteId = Mage::getSingleton('checkout/session')->getQuoteId();
$store = Mage::getSingleton('core/store')->load(Mage::app()->getStore()->getId());
$quote = Mage::getModel('sales/quote')->setStore($store)->load($currenQuoteId);
$billing = $quote->getCustomerTaxvat();
if($billing != "" )
{
return 0;
}
$taxRate = $taxRate / 100;
if ($priceIncludeTax) {
$amount = $price * (1 - 1 / (1 + $taxRate));
} else {
$amount = $price * $taxRate;
}
if ($round) {
return $this->round($amount);
}
return $amount;
}
I was Fixed Below Flow:-
Instead of Using GetQuote In Calculation.php Using Session Variable
Mage/Checkout/Model/Type/Onepage.php
Function Name:- public function saveBilling($data, $customerAddressId)
$assign = $this->getQuote()->getCustomerTaxvat();
if ($assign !="")
{
$this->_checkoutSession->setVatSession($assign);
}
else
{
$this->_checkoutSession->unsVatSession();
}
Add Above code in the onepage.php before return array(); which means last of the function.
Now Below Get Accessing the Session Variable
Mage/Tax/Model/Calculation.php
public function calcTaxAmount($price, $taxRate, $priceIncludeTax = false, $round = true)
{
$sessionaccess = Mage::getModel('checkout/session')->getVatSession();
if($sessionaccess != "")
{
return 0;
}
$taxRate = $taxRate / 100;
if ($priceIncludeTax) {
$amount = $price * (1 - 1 / (1 + $taxRate));
} else {
$amount = $price * $taxRate;
}
if ($round) {
return $this->round($amount);
}
return $amount;
}
ok so im working with this loop and getting information from DB:
for($i0 = 0; $i0 < $total0; $i0 ++) {
$id = $oconecta->retrieve_value($i0, "id_tam_product");
$price = $oconecta->retrieve_value($i0, "price_tam_product");
echo $id; // RESULTS: 312, 313, 314
echo $price; // RESULTS: 180.00, 250.00, 300.00
}
i was wondering how can i get the MAX value for that loop:
echo $id; //RESULTS: 314
echo $price; //RESULTS: 300.00
$maxID = 0;
$maxPrice = 0;
for($i0=0;$i0<$total0;$i0++)
{
$id=$oconecta->retrieve_value($i0,"id_tam_product");
$price=$oconecta->retrieve_value($i0,"price_tam_product");
$maxID = max($id, $maxID);
$maxPrice = max($price, $maxPrice);
}
echo "MAX ID: $maxID - Max PRICE: $maxPrice";
Use the max() function to determine the highest number of a set.
Either you use SQL's MAX() if you can modify your SQL query, or keep the max value in a variable in each loop, and reassign it everytime:
$firstLoop = true;
$maxId = 0;
$maxPrice = 0;
for ($i0 = 0; $i0 < $total0; $i0++)
{
$id = $oconecta->retrieve_value($i0, "id_tam_product");
$price = $oconecta->retrieve_value($i0, "price_tam_product");
if ($firstLoop) {
$firstLoop = false;
$maxId = $id;
$maxPrice = $price;
}
else {
$maxId = max($maxId, $id);
$maxPrice = max($maxPrice, $price);
}
}
(the boolean is here if you have negative values it wouldn't work if $maxPrice and $maxId are initiliazed with 0)