<?php
/*
 * @author     M2E Pro Developers Team
 * @copyright  M2E LTD
 * @license    Commercial use is forbidden
 */

// @codingStandardsIgnoreFile

/** @var $this Ess_M2ePro_Block_Adminhtml_Amazon_Order_View_Form */
echo $this->getChildHtml('item_edit');

$currency = $this->order->getData('currency');
if (empty($currency)) {
    $currency = $this->order->getMarketplace()->getChildObject()->getDefaultCurrency();
}

?>

<!-- ########################################################################################### -->

<script type="text/javascript">

    M2ePro.url.add(<?php echo json_encode(array(
        'adminhtml_order/getDebugInformation' => $this->getUrl('M2ePro/adminhtml_order/getDebugInformation/', array('id' => $this->getRequest()->getParam('id')))
    )); ?>);

    M2ePro.translator.add(<?php echo json_encode(array(
        'Custom Note' => Mage::helper('M2ePro')->__('Custom Note')
    )); ?>);

    <?php if ($this->getRequest()->getParam('show_merchant_fulfillment')): ?>
    Event.observe(window, 'load', function() {
        AmazonOrderMerchantFulfillmentObj.getPopupAction(<?php echo $this->order->getId(); ?>);
    });
    <?php endif; ?>

    Event.observe(window, 'load', function() {
        OrderNoteObj = new OrderNote('<?php echo $this->getChild('order_note_grid')->getId(); ?>');
    });

</script>

<!-- ########################################################################################### -->

<div class="box-left">
    <div class="entry-edit" id="magento_block_amazon_orders_general" collapseable="no" magento_block="no">

        <div class="entry-edit-head">
            <h4 class="icon-head head-edit-form fieldset-legend">
                <?php echo Mage::helper('M2ePro')->__('Amazon Order');?> # <?php echo $this->order->getData('amazon_order_id'); ?>
                <?php if ($this->order->getChildObject()->isPrime()): ?>
                    <img src="<?php echo $this->getSkinUrl('M2ePro/images/prime.png'); ?>" />
                <?php endif; ?>
                <?php if ($this->order->getChildObject()->isBusiness()): ?>
                    <img width="70px" src="<?php echo $this->getSkinUrl('M2ePro/images/amazon-business.png'); ?>" />
                <?php endif; ?>
                <?php if ($this->order->getChildObject()->getReplacedAmazonOrderId()) : ?>
                    &nbsp;<div class="label-replacement-order">
                        <?php echo Mage::helper('M2ePro')->__('Replacement');?>
                    </div>
                <?php endif; ?>
            </h4>
            <div style="float: right;">
                <?php $url = Mage::helper('M2ePro/Component_Amazon')->getOrderUrl($this->order->getChildObject()->getAmazonOrderId(), $this->order->getMarketplaceId()); ?>
                <a href="<?php echo $url ?>" target="_blank"><?php echo Mage::helper('M2ePro')->__('View On Amazon') ?></a>
            </div>
        </div>

        <div class="fieldset">
            <div class="hor-scroll">

                <table cellspacing="0" class="form-list">
                    <tbody>

                        <tr>
                            <td class="label"><?php echo Mage::helper('M2ePro')->__('Magento Order #');?>:</td>
                            <td class="value">
                                <strong>
                                    <?php if ($this->realMagentoOrderId !== null) { ?>

                                        <?php if ($this->realMagentoOrderId !== null) { ?>
                                            <a href="<?php echo $this->getUrl('adminhtml/sales_order/view/', array('order_id' => $this->order->getData('magento_order_id'))); ?>" target="_blank"><?php echo $this->realMagentoOrderId; ?></a>

                                            <?php if ($this->realMagentoOrderId == $this->order->getChildObject()->getSellerOrderId()) { ?>
                                                &nbsp;<?php echo Mage::helper('M2ePro')->__('(used as Your Seller Order ID)'); ?>
                                            <?php } ?>

                                        <?php } else { ?>
                                            <?php echo $this->order->getData('magento_order_id'); ?>
                                        <?php } ?>
                                    <?php } else { ?>

                                        <?php echo Mage::helper('M2ePro')->__('N/A'); ?>

                                    <?php } ?>
                                </strong>
                            </td>
                        </tr>

                        <tr>
                            <td class="label"><?php echo Mage::helper('M2ePro')->__('Sale Time');?>:</td>
                            <td class="value"><strong><?php echo Mage::helper('core')->formatDate($this->order->getData('purchase_create_date'), 'medium', true); ?></strong></td>
                        </tr>

                        <tr>
                            <td class="label"><?php echo Mage::helper('M2ePro')->__('Update Time');?>:</td>
                            <td class="value"><strong><?php echo Mage::helper('core')->formatDate($this->order->getData('purchase_update_date'), 'medium', true); ?></strong></td>
                        </tr>

                        <?php if ($this->order->getData('reservation_start_date') !== null): ?>
                            <tr>
                                <td class="label"><?php echo Mage::helper('M2ePro')->__('Reservation Time') ?>:</td>
                                <td class="value"><strong><?php echo Mage::helper('core')->formatDate($this->order->getData('reservation_start_date'), 'medium', true); ?></strong></td>
                            </tr>
                        <?php endif; ?>

                        <tr>
                            <td class="label"><?php echo Mage::helper('M2ePro')->__('Order Status'); ?>:</td>
                            <td class="value">
                                <strong>

                                    <?php if ($this->order->getChildObject()->isPending()) { ?>
                                        <span style="color: gray;"><?php echo Mage::helper('M2ePro')->__('Pending') ?></span>
                                    <?php } ?>

                                    <?php if ($this->order->getChildObject()->isCancellationRequested()) { ?>
                                        <span style="color: red;"><?php echo Mage::helper('M2ePro')->__('Unshipped (Cancellation Requested)') ?></span>
                                    <?php } elseif ($this->order->getChildObject()->isUnshipped()) { ?>
                                        <span><?php echo Mage::helper('M2ePro')->__('Unshipped') ?></span>
                                    <?php } ?>

                                    <?php if ($this->order->getChildObject()->isPartiallyShipped()) { ?>
                                        <span><?php echo Mage::helper('M2ePro')->__('Partially Shipped') ?></span>
                                    <?php } ?>

                                    <?php if ($this->order->getChildObject()->isShipped()) { ?>
                                        <span style="color: green;"><?php echo Mage::helper('M2ePro')->__('Shipped') ?></span>
                                    <?php } ?>

                                    <?php if ($this->order->getChildObject()->isUnfulfillable()) { ?>
                                        <span><?php echo Mage::helper('M2ePro')->__('Unfulfillable') ?></span>
                                    <?php } ?>

                                    <?php if ($this->order->getChildObject()->isInvoiceUnconfirmed()) { ?>
                                        <span><?php echo Mage::helper('M2ePro')->__('Invoice Unconfirmed') ?></span>
                                    <?php } ?>

                                    <?php if ($this->order->getChildObject()->isCanceled()) { ?>
                                        <span style="color: red;"><?php echo Mage::helper('M2ePro')->__('Canceled') ?></span>
                                    <?php } ?>

                                    <?php if ($this->order->isSetProcessingLock('update_shipping_status')) { ?>
                                        &nbsp;[<span style="color: gray;"><?php echo Mage::helper('M2ePro')->__('Status Update in Progress...') ?></span>]
                                    <?php } ?>

                                </strong>
                            </td>
                        </tr>

                        <tr>
                            <td class="label"><?php echo Mage::helper('M2ePro')->__('Fulfilled By Amazon') ?>:</td>
                            <td class="value">
                                <strong>

                                    <?php if ($this->order->getChildObject()->isFulfilledByAmazon()): ?><?php echo Mage::helper('M2ePro')->__('Yes') ?><?php else: ?><?php echo Mage::helper('M2ePro')->__('No') ?><?php endif; ?>

                                </strong>
                            </td>
                        </tr>

                        <tr>
                            <td class="label"><?php echo Mage::helper('M2ePro')->__('Account');?>:</td>
                            <td class="value">
                                <strong>

                                    <?php if (!$this->order->getAccount()->getId()) { ?>
                                        <span style="color: red;"><?php echo Mage::helper('M2ePro')->__('Deleted'); ?></span>

                                    <?php } else { ?>

                                        <a href="<?php echo $this->getUrl('*/adminhtml_amazon_account/edit', array('id' => $this->order->getAccount()->getId())); ?>" target="_blank"><?php echo Mage::helper('M2ePro')->escapeHtml($this->order->getAccount()->getTitle()); ?></a>

                                    <?php }?>

                                </strong>
                            </td>
                        </tr>

                    </tbody>
                </table>

            </div>
        </div>

    </div>
</div>

<div class="box-right">
    <div class="entry-edit" id="magento_block_amazon_orders_buyer" collapseable="no" magento_block="no">

        <div class="entry-edit-head">
            <h4 class="icon-head head-edit-form fieldset-legend"><?php echo Mage::helper('M2ePro')->__('Buyer Information');?></h4>
            <?php if (!$this->order->getChildObject()->isPending()): ?>
                <div style="float: right;">
                    <a href="<?php echo $this->getUrl('*/*/editShippingAddress', array('id' => $this->order->getId())) ?>"><?php echo Mage::helper('M2ePro')->__('Edit') ?></a>
                </div>
            <?php endif; ?>
        </div>

        <div class="fieldset">
            <div class="hor-scroll">

                <table cellspacing="0" class="form-list">
                    <tbody>

                        <tr>
                            <td class="label"><?php echo Mage::helper('M2ePro')->__('Customer Name'); ?>:</td>
                            <td class="value">
                                <strong>
                                    <?php if ($this->order->getData('buyer_name') == '') {
                                        echo Mage::helper('M2ePro')->__('N/A');
                                    } else {
                                        echo Mage::helper('M2ePro')->escapeHtml($this->order->getData('buyer_name'));
                                    } ?>
                                </strong>
                            </td>
                        </tr>

                        <?php if ($this->order->getData('buyer_email') != '') { ?>
                            <tr>
                                <td class="label"><?php echo Mage::helper('M2ePro')->__('Buyer Email');?>:</td>
                                <td class="value">
                                    <strong>
                                        &lt;<?php echo Mage::helper('M2ePro')->escapeHtml($this->order->getData('buyer_email')); ?>&gt;
                                    </strong>
                                </td>
                            </tr>
                        <?php } ?>

                        <tr>
                            <td class="label"><?php echo Mage::helper('M2ePro')->__('Shipping Address');?>:</td>
                            <td class="value">
                                <strong>

                                    <?php echo Mage::helper('M2ePro')->escapeHtml($this->shippingAddress['recipient_name']) ?>

                                    <?php if (!empty($this->shippingAddress['company'])) : ?>
                                        <br/><?php echo Mage::helper('M2ePro')->escapeHtml($this->shippingAddress['company']) ?>
                                    <?php endif; ?>

                                    <?php if (!empty($this->shippingAddress['country_code']) || !empty($this->shippingAddress['city'])): ?>

                                        <?php if (!empty($this->shippingAddress['street'][0])): ?>
                                            <br/><?php echo Mage::helper('M2ePro')->escapeHtml($this->shippingAddress['street'][0]); ?>
                                        <?php endif; ?>

                                        <?php if (!empty($this->shippingAddress['street'][1])): ?>
                                            <br/><?php echo Mage::helper('M2ePro')->escapeHtml($this->shippingAddress['street'][1]); ?>
                                        <?php endif; ?>

                                        <?php if (!empty($this->shippingAddress['street'][2])): ?>
                                            <br/><?php echo Mage::helper('M2ePro')->escapeHtml($this->shippingAddress['street'][2]); ?>
                                        <?php endif; ?>

                                        <?php if (!empty($this->shippingAddress['city'])): ?>
                                            <?php
                                                $addressData = array_filter(array(
                                                    $this->shippingAddress['city'],
                                                    $this->shippingAddress['state'],
                                                    $this->shippingAddress['county'],
                                                    $this->shippingAddress['postal_code'],
                                                ));
                                            ?>
                                            <br/><?php echo Mage::helper('M2ePro')->escapeHtml(implode(', ', $addressData)) ?>
                                        <?php endif; ?>

                                        <?php if (!empty($this->shippingAddress['country_name'])): ?>
                                            <br/>
                                            <?php
                                                if (!empty($this->shippingAddress['country_name'])) {
                                                    echo Mage::helper('M2ePro')->escapeHtml($this->shippingAddress['country_name']);
                                                } else {
                                                    echo Mage::helper('M2ePro')->escapeHtml($this->shippingAddress['country_code']);
                                                }
                                            ?>
                                        <?php endif; ?>

                                        <?php if (!empty($this->shippingAddress['phone'])): ?>
                                            <br/><?php echo 'T: ' . Mage::helper('M2ePro')->escapeHtml($this->shippingAddress['phone']); ?>
                                        <?php endif; ?>

                                    <?php endif; ?>

                                </strong>
                            </td>
                        </tr>

                    </tbody>
                </table>

            </div>
        </div>

    </div>
</div>

<br style="clear: both;"/>

<div class="box-left">
    <div class="entry-edit" id="magento_block_amazon_orders_payment" collapseable="no">

        <div class="entry-edit-head">
            <h4 class="icon-head head-edit-form fieldset-legend"><?php echo Mage::helper('M2ePro')->__('Payment & Invoicing Information');?></h4>
            <div class="form-buttons"></div>
        </div>

        <div class="fieldset">
            <div class="hor-scroll">

                <table cellspacing="0" class="form-list">
                    <tbody>
                        <tr>
                            <td class="label"><?php echo Mage::helper('M2ePro')->__('Order Currency');?>:</td>
                            <td class="value">
                                <?php if ($this->order->getData('currency')): ?>
                                    <strong>
                                        <?php echo Mage::helper('M2ePro')->escapeHtml($this->order->getData('currency')); ?>
                                    </strong>

                                    <?php
                                    if (!$this->isCurrencyAllowed()) {
                                        echo '<br/><b>' . Mage::helper('M2ePro')->__('Attention!') . '</b> ' . Mage::helper('M2ePro')->__('Order Currency is not enabled in Magento. Please, enable it in System > Configuration > Currency Setup.');
                                    } elseif (!$this->hasCurrencyConversionRate()) {
                                        echo '<br/><b>' . Mage::helper('M2ePro')->__('Attention!') . '</b> ' . Mage::helper('M2ePro')->__('There\'s no rate for Order Currency in Magento. Please, add Currency convert rate in System > Manage Currency > Rates.');
                                    }
                                    ?>
                                <?php else: ?>
                                <strong><?php echo Mage::helper('M2ePro')->__('N/A'); ?><strong>
                                        <?php endif; ?>

                            </td>
                        </tr>
                        <?php if ($this->order->getChildObject()->canSendMagentoCreditmemo() ||
                            $this->order->getChildObject()->canSendMagentoInvoice()) { ?>
                            <tr>
                                <td class="label"></td>
                                <td class="value">
                                    <?php echo $this->getChildHtml('resend_document') ?>
                                </td>
                            </tr>
                        <?php } ?>
                    </tbody>
                </table>
            </div>
        </div>

    </div>
</div>

<div class="box-right">
    <div class="entry-edit" id="magento_block_amazon_orders_shipping" collapseable="no">

        <div class="entry-edit-head">
            <h4 class="icon-head head-edit-form fieldset-legend"><?php echo Mage::helper('M2ePro')->__('Shipping & Handling Information');?></h4>
            <div class="form-buttons"></div>
        </div>

        <div class="fieldset">
            <div class="hor-scroll">

                <table class="form-list" cellspacing="0">
                    <tbody>
                        <tr>
                            <td class="label"><?php echo Mage::helper('M2ePro')->__('Ship By Date');?>:</td>
                            <td class="value">
                                <strong>
                                    <?php
                                    $shippingDateTo = $this->order->getChildObject()->getShippingDateTo();
                                    if (empty($shippingDateTo)) { ?>
                                        <?php echo Mage::helper('M2ePro')->__('N/A'); ?>
                                    <?php } else { ?>
                                        <?php echo Mage::helper('core')->formatDate($shippingDateTo, 'medium', true); ?>
                                    <?php } ?>
                                </strong>
                            </td>
                        </tr>

                    <?php if ($iossNumber = $this->order->getChildObject()->getIossNumber()): ?>
                        <tr>
                            <td class="label"><?php echo Mage::helper('M2ePro')->__('IOSS Number');?>:</td>
                            <td class="value"><strong><?php echo $iossNumber; ?></strong></td>
                        </tr>
                    <?php endif; ?>

                    <?php if ($taxRegistrationId = $this->order->getChildObject()->getTaxRegistrationId()): ?>
                        <tr>
                            <td class="label"><?php echo Mage::helper('M2ePro')->__('Tax Registration Number');?>:</td>
                            <td class="value"><strong><?php echo $taxRegistrationId; ?></strong></td>
                        </tr>
                    <?php endif; ?>

                        <tr>
                            <td class="label"><?php echo Mage::helper('M2ePro')->__('Shipping Service');?>:</td>
                            <td class="value">
                                <strong>
                                    <?php if ($this->order->getData('shipping_service') === null) { ?>
                                        <?php echo Mage::helper('M2ePro')->__('N/A'); ?>
                                    <?php } else { ?>
                                        <?php echo Mage::helper('M2ePro')->escapeHtml($this->order->getData('shipping_service')); ?>
                                    <?php } ?>
                                </strong>
                            </td>
                        </tr>

                        <tr>
                            <td class="label"><?php echo Mage::helper('M2ePro')->__('Shipping Cost');?>:</td>
                            <td class="value">
                                <strong>
                                    <?php if (!$this->order->getData('shipping_price')) { ?>
                                        <?php echo Mage::helper('M2ePro')->__('N/A'); ?>
                                    <?php } else { ?>
                                        <?php echo Mage::getSingleton('M2ePro/Currency')->formatPrice($currency, $this->order->getData('shipping_price')); ?>
                                    <?php } ?>
                                </strong>
                            </td>
                        </tr>

                        <?php if ($this->getChild('resubmit_shipping_info')): ?>
                            <tr>
                                <td></td>
                                <td class="value">
                                    <?php echo $this->getChildHtml('resubmit_shipping_info') ?>
                                </td>
                            </tr>
                        <?php endif; ?>

                        <?php if (!$this->order->getChildObject()->isMerchantFulfillmentApplied()):?>
                            <?php if ($this->getChildHtml('use_amazons_shipping_services')):?>
                                <tr>
                                    <td></td>
                                    <td>
                                        <?php echo $this->getChildHtml('use_amazons_shipping_services'); ?>
                                        <p class="note">
                                            <span><?php echo Mage::helper('M2ePro')->__('
                                                This Option allows you to specify Settings for
                                                <strong>Amazon\'s Shipping Service Tool</strong> to get the List of available
                                                Shipping Services offered by Amazon. In this case, Amazon guarantees
                                                <strong>fast</strong> and <strong>high quality</strong> delivery.
                                            '); ?></span>
                                        </p>
                                    </td>
                                <tr>
                            <?php endif; ?>
                        <?php else: ?>
                            <tr>
                                <td class="label"><?php echo Mage::helper('M2ePro')->__('Amazon\'s Shipping Services');?>:</td>
                                <td class="value">
                                    <strong>
                                        <a href="javascript://" id="fulfillment_manage_title" onclick="AmazonOrderMerchantFulfillmentObj.getPopupAction(<?php echo $this->order->getId(); ?>);">
                                            <?php echo Mage::helper('M2ePro')->__('Show Details');?>
                                        </a>
                                    </strong>
                                    <p class="note">
                                        <span><?php echo Mage::helper('M2ePro')->__('
                                            This Option allows you to specify Settings for
                                            <strong>Amazon\'s Shipping Service Tool</strong> to get the List of available
                                            Shipping Services offered by Amazon. In this case, Amazon guarantees
                                            <strong>fast</strong> and <strong>high quality</strong> delivery.
                                        '); ?></span>
                                    </p>
                                </td>
                            </tr>
                        <?php endif; ?>

                    </tbody>
                </table>

            </div>
        </div>

    </div>

</div>

<br style="clear: both;"/>

<div class="entry-edit" id="magento_block_amazon_orders_transaction" collapseable="no">
    <div class="entry-edit-head">
        <h4 class="icon-head head-products"><?php echo Mage::helper('M2ePro')->__('Transaction Item(s)'); ?></h4>
    </div>
</div>

<div class="grid np">
    <div class="hor-scroll">
         <?php echo $this->getChildHtml('item'); ?>
    </div>
</div>

<br style="clear: both;"/>

<div class="box-left">

    <div class="entry-edit" id="magento_block_amazon_orders_note" collapseable="no">

        <div class="entry-edit-head">
            <h4 class="icon-head head-edit-form fieldset-legend"><?php echo Mage::helper('M2ePro')->__('Custom Notes');?></h4>
        </div>

        <div class="fieldset">
            <?php echo $this->getChildHtml('order_note_grid'); ?>
            <?php echo $this->getChildHtml('add_note_button'); ?>
        </div>

    </div>

    <div class="entry-edit" id="magento_block_amazon_orders_history" collapseable="no">

        <div class="entry-edit-head">
            <h4 class="icon-head head-edit-form fieldset-legend"><?php echo Mage::helper('M2ePro')->__('Order Log');?></h4>
            <div class="form-buttons"></div>
        </div>

        <a name="order_log"></a>

        <div class="fieldset">
            <?php echo $this->getChildHtml('log'); ?>
        </div>

    </div>

</div>

<div class="box-right entry-edit" collapseable="no">

    <div class="entry-edit-head">
        <h4><?php echo Mage::helper('M2ePro')->__('Order Totals');?></h4>
    </div>

    <div class="order-totals">
        <table cellspacing="0" width="100%">
            <tbody>

                <tr>
                    <td class="label"><?php echo Mage::helper('M2ePro')->__('Subtotal'); ?></td>
                    <td><span class="price"><?php echo Mage::getSingleton('M2ePro/Currency')->formatPrice($currency, $this->order->getChildObject()->getSubtotalPrice());?></span></td>
                </tr>

                <tr>
                    <td class="label"><?php echo Mage::helper('M2ePro')->__('Shipping &amp; Handling'); ?></td>
                    <td><span class="price"><?php echo Mage::getSingleton('M2ePro/Currency')->formatPrice($currency, $this->order->getData('shipping_price')); ?></span></td>
                </tr>

                <tr>
                    <td class="label"><?php echo Mage::helper('M2ePro')->__('Promotions'); ?></td>
                    <td><span class="price"><?php echo Mage::getSingleton('M2ePro/Currency')->formatPrice($currency, $this->order->getChildObject()->getPromotionDiscountAmount() + $this->order->getChildObject()->getShippingDiscountAmount()); ?></span></td>
                </tr>

                <tr>
                    <td class="label"><?php echo Mage::helper('M2ePro')->__('Tax'); ?></td>
                    <td><span class="price"><?php echo Mage::getSingleton('M2ePro/Currency')->formatPrice($currency, $this->order->getChildObject()->getProductPriceTaxAmount() + $this->order->getChildObject()->getShippingPriceTaxAmount()); ?></span></td>
                </tr>

            </tbody>

            <tfoot>

                <tr>
                    <td class="label">
                        <strong><big><?php echo Mage::helper('M2ePro')->__('Grand Total');?></big></strong>
                    </td>
                    <td class="emph">
                        <big>
                            <strong>
                                <span class="price"><?php echo Mage::getSingleton('M2ePro/Currency')->formatPrice($currency, $this->order->getChildObject()->getGrandTotalPrice());?></span>
                            </strong>
                        </big>
                    </td>
                </tr>

                <tr>
                    <td class="label"><small><?php echo Mage::helper('M2ePro')->__('Amount Paid'); ?></small></td>
                    <td><span class="price"><small><?php echo Mage::getSingleton('M2ePro/Currency')->formatPrice($currency, $this->order->getData('paid_amount'));?></small></span></td>
                </tr>

            </tfoot>
        </table>

    </div>
</div>
