<?php
/**
 * Maho
 *
 * @category    design
 * @package     default_default
 * @copyright   Copyright (c) 2006-2020 Magento, Inc. (https://magento.com)
 * @copyright   Copyright (c) 2020-2024 The OpenMage Contributors (https://openmage.org)
 * @copyright   Copyright (c) 2024 Maho (https://mahocommerce.com)
 * @license     https://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
 */

/** @var Mage_Adminhtml_Block_Catalog_Product_Edit_Tab_Inventory $this */
?>
<?php if ($this->isReadonly()):?>
<?php $_readonly = ' disabled="disabled" ';?>
<?php else: ?>
<?php $_readonly = '';?>
<?php endif ?>
<div class="entry-edit">
    <div class="entry-edit-head">
        <h4><?= Mage::helper('catalog')->__('Inventory') ?></h4>
    </div>
    <fieldset>

        <legend><?= Mage::helper('catalog')->__('Inventory') ?></legend>
        <table cellspacing="0" class="form-list" id="table_cataloginventory">
        <tr>
            <td class="label"><label for="inventory_manage_stock"><?= Mage::helper('catalog')->__('Manage Stock') ?></label></td>
            <td class="value"><select id="inventory_manage_stock" name="<?= $this->getFieldSuffix() ?>[stock_data][manage_stock]" class="select" <?= $_readonly ?>>
                <option value="1"><?= Mage::helper('catalog')->__('Yes') ?></option>
                <option value="0"<?php if ($this->getConfigFieldValue('manage_stock') == 0): ?> selected="selected"<?php endif ?>><?= Mage::helper('catalog')->__('No') ?></option>
            </select>
            <input type="hidden" id="inventory_manage_stock_default" value="<?= $this->getDefaultConfigValue('manage_stock') ?>" />

            <?php $_checked = ($this->getFieldValue('use_config_manage_stock') || $this->isNew()) ? 'checked="checked"' : '' ?>
            <input type="checkbox" id="inventory_use_config_manage_stock" name="<?= $this->getFieldSuffix() ?>[stock_data][use_config_manage_stock]" value="1" <?= $_checked ?> onclick="toggleValueElements(this, this.parentNode);" class="checkbox" <?= $_readonly ?>/>
            <label for="inventory_use_config_manage_stock" class="normal"><?= Mage::helper('catalog')->__('Use Config Settings') ?></label>
            <?php if (!$this->isReadonly()):?><script type="text/javascript">toggleValueElements($('inventory_use_config_manage_stock'), $('inventory_use_config_manage_stock').parentNode);</script><?php endif ?></td>
            <td class="value scope-label"><?= Mage::helper('adminhtml')->__('[GLOBAL]') ?></td>
        </tr>

<?php if(!$this->getProduct()->isComposite()): ?>
        <tr>
            <td class="label"><label for="inventory_qty"><?= Mage::helper('catalog')->__('Qty') ?><span class="required">*</span></label></td>
            <td class="value">
                <?php if (!$_readonly):?>
                <input type="hidden" id="original_inventory_qty" name="<?= $this->getFieldSuffix() ?>[stock_data][original_inventory_qty]" value="<?= $this->getFieldValue('qty')*1 ?>"/>
                <?php endif ?>
                <input type="text" class="input-text required-entry validate-number" id="inventory_qty" name="<?= $this->getFieldSuffix() ?>[stock_data][qty]" value="<?= $this->getFieldValue('qty')*1 ?>" <?= $_readonly ?>/>
            </td>
            <td class="value scope-label"><?= Mage::helper('adminhtml')->__('[GLOBAL]') ?></td>
        </tr>

        <tr>
            <td class="label"><label for="inventory_min_qty"><?= Mage::helper('catalog')->__('Qty for Item\'s Status to Become Out of Stock') ?></label></td>
            <td class="value"><input type="text" class="input-text validate-number" id="inventory_min_qty" name="<?= $this->getFieldSuffix() ?>[stock_data][min_qty]" value="<?= $this->getFieldValue('min_qty')*1 ?>" <?= $_readonly ?>/>

            <?php $_checked = ($this->getFieldValue('use_config_min_qty') || $this->isNew()) ? 'checked="checked"' : '' ?>
            <input type="checkbox" id="inventory_use_config_min_qty" name="<?= $this->getFieldSuffix() ?>[stock_data][use_config_min_qty]" value="1" <?= $_checked ?> onclick="toggleValueElements(this, this.parentNode);" <?= $_readonly ?> />
            <label for="inventory_use_config_min_qty" class="normal"><?= Mage::helper('catalog')->__('Use Config Settings') ?></label>
            <?php if (!$this->isReadonly()):?><script type="text/javascript">toggleValueElements($('inventory_use_config_min_qty'), $('inventory_use_config_min_qty').parentNode);</script><?php endif ?></td>
            <td class="value scope-label"><?= Mage::helper('adminhtml')->__('[GLOBAL]') ?></td>
        </tr>

        <tr>
            <td class="label"><label for="inventory_min_sale_qty"><?= Mage::helper('catalog')->__('Minimum Qty Allowed in Shopping Cart') ?></label></td>
            <td class="value"><input type="text" class="input-text validate-number" id="inventory_min_sale_qty" name="<?= $this->getFieldSuffix() ?>[stock_data][min_sale_qty]" value="<?= (bool)$this->getProduct()->getId() ? $this->getFieldValue('min_sale_qty')*1 : Mage::helper('catalog/product')->getDefaultProductValue('min_sale_qty', $this->getProduct()->getTypeId()) ?>" <?= $_readonly ?>/>

            <?php $_checked = ($this->getFieldValue('use_config_min_sale_qty') || $this->isNew()) ? 'checked="checked"' : '' ?>
            <input type="checkbox" id="inventory_use_config_min_sale_qty" name="<?= $this->getFieldSuffix() ?>[stock_data][use_config_min_sale_qty]" value="1" <?= $_checked ?> onclick="toggleValueElements(this, this.parentNode);" class="checkbox" <?= $_readonly ?> />
            <label for="inventory_use_config_min_sale_qty" class="normal"><?= Mage::helper('catalog')->__('Use Config Settings') ?></label>
            <?php if (!$this->isReadonly()):?><script type="text/javascript">toggleValueElements($('inventory_use_config_min_sale_qty'), $('inventory_use_config_min_sale_qty').parentNode);</script><?php endif ?></td>
            <td class="value scope-label"><?= Mage::helper('adminhtml')->__('[GLOBAL]') ?></td>
        </tr>

        <tr>
            <td class="label"><label for="inventory_max_sale_qty"><?= Mage::helper('catalog')->__('Maximum Qty Allowed in Shopping Cart') ?></label></td>
            <td class="value"><input type="text" class="input-text validate-number" id="inventory_max_sale_qty" name="<?= $this->getFieldSuffix() ?>[stock_data][max_sale_qty]" value="<?= $this->getFieldValue('max_sale_qty')*1 ?>" <?= $_readonly ?> />

            <?php $_checked = ($this->getFieldValue('use_config_max_sale_qty') || $this->isNew()) ? 'checked="checked"' : '' ?>
            <input type="checkbox" id="inventory_use_config_max_sale_qty" name="<?= $this->getFieldSuffix() ?>[stock_data][use_config_max_sale_qty]" value="1" <?= $_checked ?> onclick="toggleValueElements(this, this.parentNode);" class="checkbox" <?= $_readonly ?> />
            <label for="inventory_use_config_max_sale_qty" class="normal"><?= Mage::helper('catalog')->__('Use Config Settings') ?></label>
            <?php if (!$this->isReadonly()):?><script type="text/javascript">toggleValueElements($('inventory_use_config_max_sale_qty'), $('inventory_use_config_max_sale_qty').parentNode);</script><?php endif ?></td>
            <td class="value scope-label"><?= Mage::helper('adminhtml')->__('[GLOBAL]') ?></td>
        </tr>

        <?php if($this->canUseQtyDecimals()): ?>
        <tr>
            <td class="label"><label for="inventory_is_qty_decimal"><?= Mage::helper('catalog')->__('Qty Uses Decimals') ?></label></td>
            <td class="value"><select id="inventory_is_qty_decimal" name="<?= $this->getFieldSuffix() ?>[stock_data][is_qty_decimal]" class="select" <?= $_readonly ?>>
                <option value="0"><?= Mage::helper('catalog')->__('No') ?></option>
                <option value="1"<?php if($this->getFieldValue('is_qty_decimal')==1): ?> selected="selected"<?php endif ?>><?= Mage::helper('catalog')->__('Yes') ?></option>
            </select>
            </td>
            <td class="value scope-label"><?= Mage::helper('adminhtml')->__('[GLOBAL]') ?></td>
        </tr>

        <?php if (!$this->isVirtual()) : ?>
        <tr>
            <td class="label"><label for="inventory_is_decimal_divided"><?= Mage::helper('catalog')->__('Can be Divided into Multiple Boxes for Shipping') ?></label></td>
            <td class="value"><select id="inventory_is_decimal_divided" name="<?= $this->getFieldSuffix() ?>[stock_data][is_decimal_divided]" class="select" <?= $_readonly ?>>
                <option value="0"><?= Mage::helper('catalog')->__('No') ?></option>
                <option value="1"<?php if($this->getFieldValue('is_decimal_divided') == 1): ?> selected="selected"<?php endif ?>><?= Mage::helper('catalog')->__('Yes') ?></option>
            </select>
            </td>
            <td class="value scope-label"><?= Mage::helper('adminhtml')->__('[GLOBAL]') ?></td>
        </tr>
        <?php endif ?>

        <?php endif ?>

        <tr>
            <td class="label"><label for="inventory_backorders"><?= Mage::helper('catalog')->__('Backorders') ?></label></td>
            <td class="value"><select id="inventory_backorders" name="<?= $this->getFieldSuffix() ?>[stock_data][backorders]" class="select" <?= $_readonly ?>>
            <?php foreach ($this->getBackordersOption() as $option): ?>
                <?php $_selected = ($option['value'] == $this->getFieldValue('backorders')) ? 'selected="selected"' : '' ?>
                <option value="<?= $option['value'] ?>" <?= $_selected ?>><?= $option['label'] ?></option>
            <?php endforeach ?>
            </select>

            <?php $_checked = ($this->getFieldValue('use_config_backorders') || $this->isNew()) ? 'checked="checked"' : '' ?>
            <input type="checkbox" id="inventory_use_config_backorders" name="<?= $this->getFieldSuffix() ?>[stock_data][use_config_backorders]" value="1" <?= $_checked ?> onclick="toggleValueElements(this, this.parentNode);" class="checkbox" <?= $_readonly ?> />
            <label for="inventory_use_config_backorders" class="normal"><?= Mage::helper('catalog')->__('Use Config Settings') ?></label>
            <?php if (!$this->isReadonly()):?><script type="text/javascript">toggleValueElements($('inventory_use_config_backorders'), $('inventory_use_config_backorders').parentNode);</script><?php endif ?></td>
            <td class="value scope-label"><?= Mage::helper('adminhtml')->__('[GLOBAL]') ?></td>
        </tr>
        <tr>
            <td class="label"><label for="inventory_notify_stock_qty"><?= Mage::helper('catalog')->__('Notify for Quantity Below') ?></label></td>
            <td class="value"><input type="text" class="input-text validate-number" id="inventory_notify_stock_qty" name="<?= $this->getFieldSuffix() ?>[stock_data][notify_stock_qty]" value="<?= $this->getFieldValue('notify_stock_qty')*1 ?>" <?= $_readonly ?>/>

            <?php $_checked = ($this->getFieldValue('use_config_notify_stock_qty') || $this->isNew()) ? 'checked="checked"' : '' ?>
            <input type="checkbox" id="inventory_use_config_notify_stock_qty" name="<?= $this->getFieldSuffix() ?>[stock_data][use_config_notify_stock_qty]" value="1" <?= $_checked ?> onclick="toggleValueElements(this, this.parentNode);" class="checkbox" <?= $_readonly ?>/>
            <label for="inventory_use_config_notify_stock_qty" class="normal"><?= Mage::helper('catalog')->__('Use Config Settings') ?></label>
            <?php if (!$this->isReadonly()):?><script type="text/javascript">toggleValueElements($('inventory_use_config_notify_stock_qty'), $('inventory_use_config_notify_stock_qty').parentNode);</script><?php endif ?></td>
            <td class="value scope-label"><?= Mage::helper('adminhtml')->__('[GLOBAL]') ?></td>
        </tr>
<?php endif ?>
        <tr>
            <td class="label"><label for="inventory_enable_qty_increments"><?= Mage::helper('catalog')->__('Enable Qty Increments') ?></label></td>
            <td class="value"><select id="inventory_enable_qty_increments" name="<?= $this->getFieldSuffix() ?>[stock_data][enable_qty_increments]" class="select" <?= $_readonly ?>>
                <option value="1"><?= Mage::helper('catalog')->__('Yes') ?></option>
                <option value="0"<?php if ($this->getConfigFieldValue('enable_qty_increments') == 0): ?> selected="selected"<?php endif ?>><?= Mage::helper('catalog')->__('No') ?></option>
            </select>
            <input type="hidden" id="inventory_enable_qty_increments_default" value="<?= $this->getDefaultConfigValue('enable_qty_increments') ?>" />

            <?php $_checked = ($this->getFieldValue('use_config_enable_qty_increments') || $this->isNew()) ? 'checked="checked"' : '' ?>
            <input type="checkbox" id="inventory_use_config_enable_qty_increments" name="<?= $this->getFieldSuffix() ?>[stock_data][use_config_enable_qty_increments]" value="1" <?= $_checked ?> onclick="toggleValueElements(this, this.parentNode);" class="checkbox" <?= $_readonly ?>/>
            <label for="inventory_use_config_enable_qty_increments" class="normal"><?= Mage::helper('catalog')->__('Use Config Settings') ?></label>
            <?php if (!$this->isReadonly()):?><script type="text/javascript">toggleValueElements($('inventory_use_config_enable_qty_increments'), $('inventory_use_config_enable_qty_increments').parentNode);</script><?php endif ?></td>
            <td class="value scope-label"><?= Mage::helper('adminhtml')->__('[GLOBAL]') ?></td>
        </tr>
        <tr>
            <td class="label"><label for="inventory_qty_increments"><?= Mage::helper('catalog')->__('Qty Increments') ?></label></td>
            <td class="value">
                <input type="text" class="input-text validate-digits" id="inventory_qty_increments" name="<?= $this->getFieldSuffix() ?>[stock_data][qty_increments]" value="<?= $this->getFieldValue('qty_increments')*1 ?>" <?= $_readonly ?>/>
                <?php $_checked = ($this->getFieldValue('use_config_qty_increments') || $this->isNew()) ? 'checked="checked"' : '' ?>
                <input type="checkbox" id="inventory_use_config_qty_increments" name="<?= $this->getFieldSuffix() ?>[stock_data][use_config_qty_increments]" value="1" <?= $_checked ?> onclick="toggleValueElements(this, this.parentNode);" class="checkbox" <?= $_readonly ?>/>
                <label for="inventory_use_config_qty_increments" class="normal"><?= Mage::helper('catalog')->__('Use Config Settings') ?></label>
                <?php if (!$this->isReadonly()):?><script type="text/javascript">toggleValueElements($('inventory_use_config_qty_increments'), $('inventory_use_config_qty_increments').parentNode);</script><?php endif ?></td>
            <td class="value scope-label"><?= Mage::helper('adminhtml')->__('[GLOBAL]') ?></td>
        </tr>
        <tr>
            <td class="label"><label for="inventory_stock_availability"><?= Mage::helper('catalog')->__('Stock Availability') ?></label></td>
            <td class="value"><select id="inventory_stock_availability" name="<?= $this->getFieldSuffix() ?>[stock_data][is_in_stock]" class="select" <?= $_readonly ?>>
            <?php foreach ($this->getStockOption() as $option): ?>
                <?php $_selected = ($option['value'] == $this->getFieldValue('is_in_stock')) ? 'selected="selected"' : '' ?>
                <option value="<?= $option['value'] ?>" <?= $_selected ?>><?= $option['label'] ?></option>
            <?php endforeach ?>
            </select>
            </td>
            <td class="value scope-label"><?= Mage::helper('adminhtml')->__('[GLOBAL]') ?></td>
        </tr>
        </table>
    </fieldset>
</div>
<script type="text/javascript">
//<![CDATA[
    function changeManageStockOption()
    {
        var manageStock = $('inventory_use_config_manage_stock').checked
            ? $('inventory_manage_stock_default').value
            : $('inventory_manage_stock').value;
        var catalogInventoryNotManageStockFields = {
            inventory_min_sale_qty: true,
            inventory_max_sale_qty: true,
            inventory_enable_qty_increments : true,
            inventory_qty_increments: true
        };

        $$('#table_cataloginventory tr').each(function(el) {
            if (el == $('inventory_manage_stock').up(1)) {
                return;
            }

            for (field in catalogInventoryNotManageStockFields) {
                if ($(field) && ($(field).up(1) == el)) {
                    return;
                }
            }

            el[manageStock == 1 ? 'show' : 'hide']();
        });

        return true;
    }

    function applyEnableQtyIncrements() {
        var enableQtyIncrements = $('inventory_use_config_enable_qty_increments').checked
            ? $('inventory_enable_qty_increments_default').value
            : $('inventory_enable_qty_increments').value;

        $('inventory_qty_increments').up('tr')[enableQtyIncrements == 1 ? 'show' : 'hide']();
    }

    function applyEnableDecimalDivided() {
    <?php if (!$this->isVirtual()) : ?>
        $('inventory_is_decimal_divided').up('tr').hide();
        <?php endif ?>
        $('inventory_qty_increments').removeClassName('validate-digits').removeClassName('validate-number');
        $('inventory_min_sale_qty').removeClassName('validate-digits').removeClassName('validate-number');
        if ($('inventory_is_qty_decimal').value == 1) {
        <?php if (!$this->isVirtual()) : ?>
            $('inventory_is_decimal_divided').up('tr').show();
            <?php endif ?>
            $('inventory_qty_increments').addClassName('validate-number');
            $('inventory_min_sale_qty').addClassName('validate-number');
        } else {
            $('inventory_qty_increments').addClassName('validate-digits');
            $('inventory_min_sale_qty').addClassName('validate-digits');
        }
    }

    Event.observe(window, 'load', function() {
        if ($('inventory_manage_stock') && $('inventory_use_config_manage_stock')) {
            Event.observe($('inventory_manage_stock'), 'change', changeManageStockOption);
            Event.observe($('inventory_use_config_manage_stock'), 'change', changeManageStockOption);
            changeManageStockOption();
        }
        if ($('inventory_enable_qty_increments') && $('inventory_use_config_enable_qty_increments')) {
            Event.observe($('inventory_enable_qty_increments'), 'change', applyEnableQtyIncrements);
            Event.observe($('inventory_use_config_enable_qty_increments'), 'change', applyEnableQtyIncrements);
            applyEnableQtyIncrements();
        }
        if ($('inventory_is_qty_decimal') && $('inventory_qty_increments') && $('inventory_min_sale_qty')) {
            Event.observe($('inventory_is_qty_decimal'), 'change', applyEnableDecimalDivided);
            applyEnableDecimalDivided();
        }
    });
//]]>
</script>
