Search code examples
phpmagentomagento-1.9

How to do operations on nested nested arrays


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.


Solution

  • 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;
    }