function zen_get_sba_stock_attribute_id($products_id, $attribute_list = array(), $from = 'order')
{
    global $db;
    /*if (isset($attribute_list) && (($k = sizeof($attribute_list)) > 0)) {*/
    /*
      Discussion:
      This section of code should return the one or more stock_ids that encompass
      all of the provided attributes one time only and only those attributes 
      (or if not to be considered a stock related item then can be duplicated).
      By doing so, one can identify that the attributes provided are captured
      and valid for a checkout or other next action.
      
      So to accomplish this: Need to find the Union of variants where those that 
      are stock bearing do not intersect and where all of the attributes are present.
      May be best to identify all of the variants that contain at least the 
      attribute(s) provided.  This can be done currently with 
      zen_get_sba_ids_from_attribute($attribute_id, $products_id, stock_attribute_unique)
      
      
      The union of these should be at least all attributes, if
      that is not true, then an attribute is provided that is not tracked.
      After have a list of all associated variants, need to find the one or
      combination of them that provides a "complete" combination.
    Approach(es) on this... 
    
    Of the attributes provided, determine the number of those attributes that are
      stock based.  With the set of proposed attributes for the product (being
      stock based) test against the proposed attributes against the stored list 
      to see if the current selection is in the stored list.  If it is, then done.
      If the 
    */
    /*      $stock_attribute = zen_get_sba_stock_attribute($products_id, $attribute_list, $from);
          $query = 'select stock_id from ' . TABLE_PRODUCTS_WITH_ATTRIBUTES_STOCK . 
                    ' where `stock_attributes` = :stock_attribute: and products_id = :products_id:';
          $query = $db->bindVars($query, ':stock_attribute:', $stock_attribute, 'string');
          $query = $db->bindVars($query, ':products_id:', $products_id, 'integer');
    
      		$stock_id = $db->Execute($query);
    
          if ($stock_id->RecordCount() > 1) {
            echo 'This is an error situation, as only one record should be returned.  More than one stock id was returned which should not be possible.';
          } else {
            return $stock_id->fields['stock_id'];
          }
        }
        
        return;*/
    $temp_attributes = array();
    $specAttributes = array();
    $stock_attributes_list = array();
    $stock_attributes = '';
    //    $multi = (sizeof($attribute_list) > 1 ? true : false);
    $products_id = zen_get_prid($products_id);
    $stock_attribute = zen_get_sba_stock_attribute($products_id, $attribute_list, $from);
    if (!zen_product_is_sba($products_id)) {
        return NULL;
    }
    $stock_attributes_list = zen_get_sba_attribute_ids($products_id, $attribute_list, $from);
    // Summary of the above, if the product is not tracked by SBA, then return null.
    // Need to evaluate if product is SBA tracked in case the page is posted without the attributes as a separate check.
    if (sizeof($stock_attributes_list) == 1) {
        // 		  	echo '<br />Single Attribute <br />';
        $stock_attributes = $stock_attributes_list[0];
        // create the query to find single attribute stock
        $stock_query = 'select stock_id, quantity as products_quantity from ' . TABLE_PRODUCTS_WITH_ATTRIBUTES_STOCK . ' where products_id = :products_id: and stock_attributes=:stock_attributes:';
        $stock_query = $db->bindVars($stock_query, ':products_id:', $products_id, 'integer');
        $stock_query = $db->bindVars($stock_query, ':stock_attributes:', $stock_attributes, 'passthru');
        $stock_values = $db->Execute($stock_query);
        // return the stock qty for the attribute
        if (!$stock_values->EOF) {
            return array($stock_values->fields['stock_id']);
        } else {
            return false;
        }
    } elseif (sizeof($stock_attributes_list) > 1) {
        // 			echo '<br />Multiple attributes <br />';
        $stockResult = null;
        //This part checks for "attribute combinations" in the SBA table. (Multiple attributes per Stock ID Row, Multiple Attribute types in stock_attributes Field  i.e, 123,321,234)
        $stock_attributes = implode(',', $stock_attributes_list);
        // create the query to find attribute stock
        $stock_query = 'select stock_id, quantity as products_quantity from ' . TABLE_PRODUCTS_WITH_ATTRIBUTES_STOCK . ' where products_id = :products_id: and stock_attributes like :TMPstock_attributes:';
        $stock_query = $db->bindVars($stock_query, ':products_id:', $products_id, 'integer');
        $stock_query = $db->bindVars($stock_query, ':TMPstock_attributes:', $stock_attributes, 'string');
        // get the stock value for the combination
        $stock_values = $db->Execute($stock_query);
        $stockResult = $stock_values->fields['stock_id'];
        if (!$stock_values->EOF && $stock_values->RecordCount() == 1) {
            //return the stock for "attribute combinations"
            return array($stockResult);
        } else {
            //This part is for attributes that are all listed separately in the SBA table for the product
            $stockResult = null;
            $returnedStock = null;
            $i = 0;
            $stockResultArray = array();
            $notAccounted = false;
            foreach ($stock_attributes_list as $eachAttribute) {
                // create the query to find attribute stock
                //echo '<br />Multiple Attributes selected (one attribute type per product)<br />';
                $stock_query = 'select stock_id, quantity as products_quantity from ' . TABLE_PRODUCTS_WITH_ATTRIBUTES_STOCK . ' where products_id = :products_id: and stock_attributes= :eachAttribute:';
                $stock_query = $db->bindVars($stock_query, ':products_id:', $products_id, 'integer');
                $stock_query = $db->bindVars($stock_query, ':eachAttribute:', $eachAttribute, 'passthru');
                // get the stock value for the combination
                $stock_values = $db->Execute($stock_query);
                $stockResult = $stock_values->fields['products_quantity'];
                $stockResultArray[] = $stock_values->fields['stock_id'];
                if ($stockResult->EOF) {
                    $notAccounted = true;
                }
                //special test to account for qty when all attributes are listed seperetly
                if (!zen_not_null($returnedStock) && $i == 0) {
                    //set initial value
                    if ($stock_values->EOF) {
                        $returnedStock = 0;
                    } else {
                        $returnedStock = $stockResult;
                    }
                } elseif ($returnedStock > $stockResult) {
                    //update for each attribute, if qty is lower than the previous one
                    $returnedStock = $stockResult;
                }
                // end if first stock item of attribute
                $i++;
            }
            // end for each attribute.
            /*foreach ($stockResultArray as $stockResult) {
                if (!zen_not_null($stockResult)) {
                  $stockResultArray = false;
                  continue;
                }
              }*/
            if ($notAccounted) {
                return false;
            } else {
                return $stockResultArray;
            }
        }
    }
    return;
}
function zen_get_products_stock($products_id, $attributes = null, $dupTest = null)
{
    global $db;
    $products_id = zen_get_prid($products_id);
    // Need to evaluate if product is SBA tracked in case the page is posted without the attributes as a separate check.
    if ($products_id && (!is_array($attributes) && !zen_not_null($attributes))) {
        //For products without associated attributes, get product level stock quantity
        $stock_query = "select products_quantity \n                      from " . TABLE_PRODUCTS . " \n                      where products_id = :products_id:";
        $stock_query = $db->bindVars($stock_query, ':products_id:', $products_id, 'integer');
        $stock_values = $db->Execute($stock_query);
        return $stock_values->fields['products_quantity'];
    } elseif (is_array($attributes) && sizeof($attributes) > 0) {
        //For products with associated attributes, to do the following:
        //	1. Check if the attribute has been added to the SBA Stock Page.
        //	2. Check if the attribute(s) are listed in seperate rows or are combined into a single row.
        // mc12345678 - The following seems like it could be compressed more/do less searches.  Now that this seems to work, there is some code that can be compressed.
        /* mc12345678 Comment about the $attribute_stock test is really to see if the product is tracked by SBA. */
        // check if any attribute stock values have been set for the product in the SBA table, if not do the else part
        if (zen_product_is_sba($products_id)) {
            // prepare to search for details for the particular attribute combination passed as a parameter
            $stock_attributes_list = array();
            $stock_attributes = '';
            $stock_attributes_list = zen_get_sba_attribute_ids($products_id, $attributes, 'products');
            if (sizeof($stock_attributes_list) == 1 && !$dupTest) {
                // 		  	echo '<br />Single Attribute <br />';
                $stock_attributes = $stock_attributes_list[0];
                // create the query to find single attribute stock
                $stock_query = 'select stock_id, quantity as products_quantity from ' . TABLE_PRODUCTS_WITH_ATTRIBUTES_STOCK . ' where products_id = :products_id: and stock_attributes=:stock_attributes:';
                $stock_query = $db->bindVars($stock_query, ':products_id:', $products_id, 'integer');
                $stock_query = $db->bindVars($stock_query, ':stock_attributes:', $stock_attributes, 'passthru');
                $stock_values = $db->Execute($stock_query);
                // return the stock qty for the attribute
                if (!$stock_values->EOF) {
                    return $stock_values->fields['products_quantity'];
                } else {
                    return false;
                }
            } elseif (sizeof($stock_attributes_list) > 1) {
                // mc12345678 multiple attributes are associated with the product
                //   question is how these relate to the SBA variant.
                // 			echo '<br />Multiple attributes <br />';
                $stockResult = null;
                //This part checks for "attribute combinations" in the SBA table. (Multiple attributes per Stock ID Row, Multiple Attribute types in stock_attributes Field  i.e, 123,321,234)
                $TMPstock_attributes = implode(',', $stock_attributes_list);
                // create the query to find attribute stock
                $stock_query = 'select stock_id, quantity as products_quantity from ' . TABLE_PRODUCTS_WITH_ATTRIBUTES_STOCK . ' where products_id = :products_id: and stock_attributes like :TMPstock_attributes:';
                $stock_query = $db->bindVars($stock_query, ':products_id:', $products_id, 'integer');
                $stock_query = $db->bindVars($stock_query, ':TMPstock_attributes:', $TMPstock_attributes, 'string');
                // get the stock value for the combination
                $stock_values = $db->Execute($stock_query);
                $stockResult = $stock_values->fields['products_quantity'];
                if ($dupTest) {
                    //return the stock for "attribute combinations" with a flag
                    if ($stockResult > 0) {
                        return 'true';
                    }
                    return 'false';
                } elseif (!$stock_values->EOF && $stock_values->RecordCount() == 1) {
                    //return the stock for "attribute combinations"
                    return $stockResult;
                } else {
                    //This part is for attributes that are all listed separately in the SBA table for the product
                    $stockResult = null;
                    $returnedStock = null;
                    $i = 0;
                    $notAccounted = false;
                    foreach ($stock_attributes_list as $eachAttribute) {
                        // create the query to find attribute stock
                        //echo '<br />Multiple Attributes selected (one attribute type per product)<br />';
                        $stock_query = 'select quantity as products_quantity from ' . TABLE_PRODUCTS_WITH_ATTRIBUTES_STOCK . ' where products_id = :products_id: and stock_attributes= :eachAttribute:';
                        $stock_query = $db->bindVars($stock_query, ':products_id:', $products_id, 'integer');
                        $stock_query = $db->bindVars($stock_query, ':eachAttribute:', $eachAttribute, 'passthru');
                        // get the stock value for the combination
                        $stock_values = $db->Execute($stock_query);
                        $stockResult = $stock_values->fields['products_quantity'];
                        if ($stockResult->EOF) {
                            $notAccounted = true;
                        }
                        //special test to account for qty when all attributes are listed seperetly
                        if (!zen_not_null($returnedStock) && $i == 0) {
                            //set initial value
                            if ($stock_values->EOF) {
                                $returnedStock = 0;
                            } else {
                                $returnedStock = $stockResult;
                            }
                        } elseif ($returnedStock > $stockResult) {
                            //update for each attribute, if qty is lower than the previous one
                            $returnedStock = $stockResult;
                        }
                        // end if first stock item of attribute
                        $i++;
                    }
                    // end for each attribute.
                    if ($notAccounted) {
                        return false;
                    } else {
                        return $returnedStock;
                    }
                }
            }
        } else {
            //Used with products that have attributes But the attribute is not listed in the SBA Stock table.
            //Get product level stock quantity
            $stock_query = "select products_quantity from " . TABLE_PRODUCTS . " where products_id = :products_id:";
            $stock_query = $db->bindVars($stock_query, ':products_id:', $products_id, 'integer');
            $stock_values = $db->Execute($stock_query);
            return $stock_values->fields['products_quantity'];
        }
    }
    return;
}
 function updateNotifyOrderProcessingStockDecrementInit(&$callingClass, $notifier, $paramsArray, &$productI, &$i)
 {
     $this->_i = $i;
     $this->_productI = $productI;
     $this->_orderIsSBA = zen_product_is_sba($this->_productI['id']);
     if ($this->_orderIsSBA) {
         // Only take SBA action on SBA tracked product mc12345678 12-18-2015
         $this->_stock_info = zen_get_sba_stock_attribute_info(zen_get_prid($this->_productI['id']), $this->_productI['attributes'], 'order');
         // Sorted comma separated list of the attribute_id.
         // START "Stock by Attributes"
         $attributeList = null;
         $customid = null;
         if (isset($this->_productI['attributes']) and sizeof($this->_productI['attributes']) > 0) {
             foreach ($this->_productI['attributes'] as $attributes) {
                 $attributeList[] = $attributes['value_id'];
             }
             $customid = zen_get_customid($this->_productI['id'], $attributeList);
             // Expects that customid would be from a combination product, not individual attributes on a single product.  Should return an array if the values are individual or a single value if all attributes equal a single product.
             $productI['customid'] = $customid;
             $this->_productI['customid'] = $customid;
             //      $productI['model'] = (zen_not_null($customid) ? $customid : $productI['model']);
             $this->_productI['model'] = $productI['model'];
         }
     }
     // END "Stock by Attributes"
 }