$this->log('putStockUpdate()');
$this->log('putStockUpdate() - New local stock: '.$stock);
$listing = $this->call('item/getItem', array('itemId' => $item_id));
$product_id = $this->getProductId($item_id);
$reserve = $this->getReserve($product_id, $item_id, ($sku != null ? $sku : ''));
if($listing['status'] == 1 ) {
if($reserve != false) {
$this->log('putStockUpdate() - Reserve stock: '.$reserve);
if($stock > $reserve) {
$this->log('putStockUpdate() - Stock is larger than reserve, setting level to reserve');
$stock = $reserve;
}
}
if($sku == null) {
$this->log('putStockUpdate() - Listing stock: '.$listing['qty'].', new stock: '.$stock);
if($stock <= 0) {
if ($this->config->get('ebay_disable_nostock') == 1) {
$this->disableProduct($product_id);
}
$this->endItem($item_id);
return true;
}elseif($listing['qty'] != $stock) {
$this->call('item/reviseStock/', array('itemId' => $item_id, 'stock' => $stock));
$this->log('putStockUpdate() - OK');
return true;
}else{
$this->log('putStockUpdate() - No update needed');
return false;
}
}else{
// Need to loop over current item check if other variants have stock
$variantStock = false;
foreach($listing['variation']['vars'] as $var) {
if(($var['sku'] != $sku) && ($var['qty'] > 0)) {
//other variations have stock
$variantStock = true;
$this->log('Another variation has stock (SKU: '.$var['sku'].')');
break;
}
}
if ($stock <= 0) {
if ($this->config->get('ebay_disable_nostock') == 1) {
$this->disableVariant($product_id, $sku);
}
}
if($variantStock == true || $stock > 0) {
$this->log('putStockUpdate() - Revising item with Item ID "'.$item_id.'" to stock level "'.$stock.'", sku "'.$sku.'"');
$this->call('item/reviseStock/', array('itemId' => $item_id, 'stock' => $stock, 'sku' => $sku));
return true;
}else{
$this->log('putStockUpdate() - Sending end for item, no variants have stock!');
$this->endItem($item_id);
}
}
}else{
$this->removeItemByItemId($item_id);
$this->log('putStockUpdate() - Listing not active, item id: '. $item_id .', status returned: '.$listing['statusActual']);
}