<?php
/**
 * Marketplace - Component Marketplace 
 * ******************************************************************
 *
 * PHP version 7.0
 *
 * @category   Component
 * @package    Joomla
 * @author     WebKul software private limited <support@webkul.com>
 * @copyright  2010 WebKul software private limited
 * @license    http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
 * @version    GIT:5.2
 * @filesource http://store.webkul.com
 * @link       Technical Support:  webkul.uvdesk.com
 * ******************************************************************
 */
defined('_JEXEC') or die('Restricted access');
JHTML::_('behavior.tooltip'); 
JHtml::addIncludePath(JPATH_COMPONENT.'/helpers/html');
JHtml::_('bootstrap.tooltip');
JHtml::_('behavior.multiselect');
JHtml::_('behavior.modal');
JHtml::_('dropdown.init');
JHtml::_('formbehavior.chosen', 'select'); 
$user = JFactory::getUser();
$userId = $user->get('id');
$listOrder = $this->escape($this->state->get('list.ordering'));
$listDirn = $this->escape($this->state->get('list.direction'));
$canOrder = $user->authorise('core.edit.state', 'com_marketplace.product');
$params = (isset($this->state->params)) ? $this->state->params : new JObject;
$params = JComponentHelper::getParams('com_languages');

$doc=JFactory::getDocument();
if (!class_exists('MarketplaceHelper')) {
    include JPATH_ADMINISTRATOR.DIRECTORY_SEPARATOR.'components'.
    DIRECTORY_SEPARATOR.'com_marketplace'.DIRECTORY_SEPARATOR.
    'helpers'.DIRECTORY_SEPARATOR.'marketplace.php';
}
$mpHelp=new MarketplaceHelper();
$modProfFE=$mpHelp->getMarketModelS('Profile');
$doc->addStyleDeclaration(
    '
.icon-updatecommision::before {
 content: url("'.JURI::root(). 'administrator/components/com_marketplace/assets/images/reload_commision.png");

}
.icon-commisions {
    background-image: url("components/com_marketplace/assets/images/sprite-icon.png");
    background-repeat: no-repeat;
    background-position: -39px -93px !important;
    vertical-align: middle;
    width: 20px;
    height: 20px;
    margin-right: 0px!important;
}
'
);
$app=JFactory::getApplication()->input;
$setval=$app->get('wk_set');
if (isset($setval)) {
    JFactory::getDocument()->addScriptDeclaration(
        'window.parent.href="'
        .JURI::root().
        'administrator/index.php?option=com_marketplace&view=commisions"'
    );
}
$redirect=$app->get('wk_redirect');
$pass=$app->get('pass');
if (isset($redirect)) {
    JFactory::getApplication()->redirect(
        'index.php?option=com_marketplace&view=commisions', 
        JText::_('COMMISION_UPDATED_SUCCESS')
    );
}
if ($pass) {
    JFactory::getApplication()->enqueueMessage(
        'Only Numeric Value Allowed For Commission', 'error'
    );
    JFactory::getApplication()->redirect(
        'index.php?option=com_marketplace&view=commisions'
    );
    
}
$test=$app->get('wk_set');
if (isset($test)) {
    $ids=unserialize($app->get('ids', '', 'raw'));
    $script="
    window.addEvent('domready', function () {
        SqueezeBox.initialize();
        SqueezeBox.open('index.php?option=com_marketplace&view=commisions&layout=update_commision&tmpl=component&ids="
            .urlencode(serialize($ids)).
            "', {handler: 'iframe', size: {x: 500, y: 300}});
            SqueezeBox.addEvent('onClose', function () {
                window.location = 'index.php?option=com_marketplace&view=commisions';
        });
    });
    ";
    $doc->addScriptDeclaration($script);
}

$style="
    .comm_det_head {
        text - transform: capitalize;
        font - weight: bold;
    }

    .comm_det {
        margin: 20 px;
    }

    .pay_mode {
        float: left;
    }

    # pay_payment {
        float: left;
    }

    # modal - header {
        display: none;
    }

    # payment_wire_pop {
        display: none;
        position: fixed;
        position: absolute;
        height: 300 px;
        width: 350 px;
        background: #FFFFFF;
        left: 300 px;
        top: 150 px;
        z - index: 100;
        margin - left: 300 px;
        border - radius: 5 px;
        padding: 15 px;
        font - size: 15 px;
        - moz - box - shadow: 0 0 5 px# E7284A;
        - webkit - box - shadow: 0 0 5 px# E7284A;
        box - shadow: 0 0 5 px# E7284A;
    }

    .payment_wire_pop {
        background: none repeat scroll 0 0# FFFFFF;
        border - radius: 5 px;
        box - shadow: 0 0 5 px# E7284A;
        display: none;
        font - size: 15 px;
        height: 300 px;
        left: 300 px;
        margin - left: 300 px;
        /* padding: 15px; */
        position: fixed;
        top: 150 px;
        width: 350 px;
        z - index: 100;
    }

    .comm_det input {
        width: 94 %;
    }

    .payment_wire {
        background: #d2d2d2;
        /*Sample*/
        width: 100 %;
        height: 100 %;
    }

    a: hover {
        cursor: pointer;
        text - decoration: none;
        color: black;
    }
    /* This is for the positioning of the Close Link */

    .popupBoxClose {
        font - size: 20 px;
        line - height: 15 px;
        right: -10 px;
        top: -10 px;
        position: absolute;
        color: red;
        font - weight: bold;
        height: 20 px;
        width: 25 px;
        text - align: center;
        padding - top: 6 px;
        border - radius: 50 %;
        background: rgb(204, 204, 204);
        background: rgba(204, 204, 204, .7);
    }

    .pay_head {
        position: relative;
        top: 0;
        left: 0;
        background: #428BCA;
        margin: 0;
        padding: 10px;
        color: white;
        border-radius: 5px 5px 0px 0;
    }

    .modal {
        overflow: hidden;
    }

    .btn.hasTooltip.js-stools-btn-filter {
        display: none;
    }

    .v_align {
        vertical-align: top!important;
    }
";
$doc->addStyleDeclaration($style);
?>
<form action="index.php?option=com_marketplace&view=commisions" method="post" name="adminForm" id="adminForm">
    <?php if (!empty($this->sidebar)) : ?>
    <div id="j-sidebar-container" class="span2">
        <?php echo $this->sidebar; ?>
    </div>
    <div id="j-main-container" class="span10">
<?php else : ?>
        <div id="j-main-container">
<?php endif; ?>
<?php
echo JLayoutHelper::render('joomla.searchtools.default', array('view' => $this));?>
          <table class="table table-striped table-bordered" id="articleList">
            <thead>
                <tr bgcolor="#6994C4" style="color:#FFFFFF;vertical-align:top">
                    <th width="1%" class="center v_align">
                        <input type="checkbox" name="checkall-toggle" value="" title="<?php echo JText::_('JGLOBAL_CHECK_ALL'); ?>" onclick="Joomla.checkAll(this)"
                        />
                    </th>
                    <th width="10%" class="center v_align">
                        <?php echo JHTML::_(
                            'searchtools.sort',  'CUSTOMER_NAME', 'us.username',
                            $listDirn, $listOrder
                        ); ?>
                    </th>
                    <th width="15%" class="center v_align">
                        <?php echo JHTML::_(
                            'searchtools.sort',  'CUSTOMER_EMAIL', 'us.email',
                            $listDirn, $listOrder
                        ); ?>
                    </th>
                    <th width="10%" class="center v_align">
                        <?php echo JHtml::_(
                            'searchtools.sort', 'COMMISIONS_S', 'a.commision',
                            $listDirn, $listOrder
                        ); ?>
                    </th>
                    <th width="1%" class="center v_align">
                    </th>
                    <th width="10%" class="center v_align">
                        <?php echo JHtml::_(
                            'searchtools.sort', 'TOTAL_SALES', 'a.total_sales',
                            $listDirn, $listOrder
                        ); ?>
                    </th>
                    <th width="10%" class="center v_align">
                        <?php echo JHtml::_(
                            'searchtools.sort', 'AMOUNT_RECEIVED', 'a.amount_recieved',
                            $listDirn, $listOrder
                        ); ?>
                    </th>
                    <th width="10%" class="center v_align">
                        <?php echo JHtml::_(
                            'searchtools.sort', 'AMOUNT_REMAIN', 'a.amount_remain',
                            $listDirn, $listOrder
                        ); ?>
                    </th>
                    <th width="10%" class="center v_align">
                        <?php echo JHtml::_(
                            'searchtools.sort', 'LAST_PAY_AMOUNT', 'a.amount_paid',
                            $listDirn, $listOrder
                        ); ?>
                    </th>

                    <th width="10%" class="center v_align">
                        <?php echo JText::_('ACTION'); ?>
                    </th>
                    <th cwidth="20%" class="center v_align">
                        <?php echo JText::_('CUSTOMER_SINCE'); ?>
                    </th>
                    <th width="1%" class="center v_align">
                        <?php echo JHTML::_(
                            'searchtools.sort',  'ID', 'a.commision_id',
                            $listDirn, $listOrder
                        ); ?>
                    </th>
                </tr>
            </thead>
                <tfoot>
                    <tr>
                        <td colspan="12">
                            <?php echo $this->pagination->getListFooter(); ?>
                        </td>
                    </tr>
                </tfoot>
                <tbody>
        <?php
        $params = JComponentHelper::getParams('com_marketplace');
        $comm_amount = trim($params->get('commision_amount'));
        $paypal_shop_mode = trim($params->get('paypal_shop_mode'));
        $model=$this->getModel('commisions');
        $vendor_currencies_output =$model->getCurrencySymbol();
        $currency_symbol = $vendor_currencies_output['currency_symbol']; 
        $vendor_currency_code_3 = $vendor_currencies_output['currency_code_3']; 
        $k = 0;
        $wk_amt_count = count($this->items);
        if (count($this->items)) {
            $n=count($this->items);
            foreach ($this->items as $k => $item) {
                $comm_type=$item->commision_type;
                $checked    = JHTML::_('grid.id', $k, $item->commision_id);
                $rate=0;;
                $comm=0;
                $cm_amt=0;;
                $comm=$model->getCurrencyDetails($item->seller_id);
                $data_c=count($comm);
                $total_order=$model->getTotalOrder($item->seller_id);
                $total_count=count($total_order);
                $comm->total_sales=0.0;
                if (count($total_order)) {
                    foreach ($total_order as $key => $value) {
                        if ($value->virtuemart_real_order_id=='r') {
                            $comm->total_sales+=
                                $value->virtuemart_product_total_amount;
                            $rate
                                =$value->virtuemart_seller_commision_rate;
                            $cm_amt+=
                                ($value->virtuemart_product_total_amount*$rate)/100;
                            $cm_amt=number_format((float)$cm_amt, 2, '.', '');
                        } else {
                            $comm->total_sales+=
                            $value->virtuemart_product_total_amount;
                            $rate=$value->virtuemart_seller_commision_rate;
                            $cm_amt+=$rate*$value->virtuemart_product_quantity;
                            $cm_amt=number_format((float)$cm_amt, 2, '.', ''); 
                        }
                    }
                }
                $cm_amt
                    =$comm->total_sales-$comm->amount_remain-$comm->amount_recieved;
                $cm_amt=number_format((float)$cm_amt, 2, '.', '');
                $amount_remain = $item->amount_remain;
                ?>
            <tr class="<?php echo "row" . $k; ?>">
                <td>
                    <?php  echo $checked; ?>
                </td>  
                <td><a href="index.php?option=com_marketplace&view=marketplace&layout=edit_seller&seller_id=<?php
                    echo $item->seller_id;
                    ?>"><?php
                    echo JFactory::getUser($item->seller_id)->username;
                    ?></a> 
                </td>
                <td>
                    <?php echo $this->escape($item->email); ?>
                </td>
                <td>     
                    <?php
                    if ($comm_type=='r') {
                        $sign='%';
                    } else {
                        $sign=$currency_symbol;
                    }
                    echo $this->escape($item->commision).' '.$sign;
                    ?>
                </td>
                <td>
                    <a class="modal" rel="{handler: 'iframe', size: {x: 500, y: 300}}" href="<?php
                    echo JURI::root()
                    ?>administrator/index.php?option=com_marketplace&view=commisions&layout=update_commision&tmpl=component&seller_id=<?php echo $item->seller_id ?>&commision_id=<?php
                    echo $this->escape($item->commision_id);
                    ?>">
                    <img style="min-width:27px;min-height:27px;" title="Update Commision" height="25px" width="25px" src="<?php echo JURI::root()."administrator/components/com_marketplace/assets/images/commition-update-icon.png" ?>"/></a>
                </td>
                <td>
                    <?php
                    echo $this->escape($item->total_sales).' '.$currency_symbol;
                    ?>
                </td>
                <td>
                    <?php
                    echo $this->escape($item->amount_recieved).' '.$currency_symbol;
                    ?>
                </td>
                <td>
                    <?php
                    echo $this->escape($amount_remain).' '.$currency_symbol;
                    ?>
                </td>
                <td>
                    <?php
                    echo $this->escape($item->amount_paid).' '.$currency_symbol;
                    ?>
                </td>
                <td>
                    <?php 
                    if (isset($item->payment_mode)) {
                    ?>
                        <span class="pay_mode">
                <?php
                $commision_amount=$comm_amount;
                if ($item->payment_mode!='') {
                    $business_email=array();
                    $business_email=explode(',', $item->payment_desc);
                    $pay_modes=explode(',', $item->payment_mode);
                    $ix=0;
                    $xcv=-1;
                    $pay_meth=$modProfFE->getPaymentInfo();
                    foreach ($pay_modes as $key1 => $value) {
                        if ($value=="") {
                            continue;
                        }
                        $xcv++;
                        $pay_option_finals=$value;
                        foreach ($pay_meth as $pay_data) {
                            if ($pay_data->payment_element."-".$pay_data->virtuemart_paymentmethod_id == $pay_option_finals) {
                                $pay_name=$pay_data->payment_name;
                                break;
                            }
                        }
                        $pay_name=strlen($pay_name)<10?$pay_name:substr($pay_name, 0, 10)."...";
                        $success_url= urlencode(JURI::root().'administrator/index.php?option=com_marketplace&view=commisions&layout=success&amount='.$commision_amount.'&seller_id='.$item->seller_id.'&pay_mode='.$pay_option_finals.'&totalAmountToPay='.$amount_remain.'&commision_amount='.$commision_amount);
            
                        $cancel_url= urlencode(JURI::root().'administrator/index.php?option=com_marketplace&view=commisions&layout=cancel');

                        $ix++;
                        if ((strpos($pay_option_finals, 'paypal')!==false)) {
                            $seller_name_payment=$this->escape(
                                $item->first_name
                                ." ".$item->last_name
                            )." ".JText::_("SELLER_PAYMENT");
                ?>
                                        <div class="pay_paypal">
                                            <p>
                        <?php
                        if ($paypal_shop_mode == 's') { 
                            $shop_mode = "sandbox"; 
                            if ($amount_remain >= $commision_amount) {
                                $commision_amount=$commision_amount;
                        ?>
                                                        <a class="jgrid" href="https://www.<?php echo $shop_mode; ?>.paypal.com/cgi-bin/webscr?cmd=_xclick&business=<?php echo $business_email[$xcv];?>&currency_code=<?php echo $vendor_currency_code_3;?>&item_name=<?php echo urlencode($seller_name_payment);?>&custom=752&amount=<?php echo $commision_amount;?>&return=<?php echo $success_url;?>&cancel_return=<?php echo $cancel_url;?>" id="<?php ?>" title="<?php echo JText::_('PAY_PAYPAL');?>">    
                                                        <?php echo $pay_name; ?>
                                                        </a>
                        <?php
                            } else {
                                $commision_amount = $amount_remain;
                        ?>
                                                        <a class="jgrid" href="https://www.<?php echo $shop_mode; ?>.paypal.com/cgi-bin/webscr?cmd=_xclick&business=<?php echo $business_email[$xcv];?>&currency_code=<?php echo $vendor_currency_code_3;?>&item_name=<?php echo $seller_name_payment;?>&custom=752&amount=<?php echo $commision_amount;?>&return=<?php echo $success_url;?>&cancel_return=<?php echo $cancel_url;?>" title="<?php echo JText::_('PAY_PAYPAL');?>">    
                                                        <?php echo $pay_name; ?>
                                                        </a>
                        <?php
                            }
                        } elseif ($paypal_shop_mode == 'p') { 
                            if ($amount_remain >= $commision_amount) {
                                $commision_amount=$commision_amount;
                        ?>
                                                        <a class="jgrid" href="https://www.paypal.com/cgi-bin/webscr?cmd=_xclick&business=<?php echo $business_email[$xcv];?>&currency_code=<?php echo $vendor_currency_code_3;?>&item_name=<?php echo $seller_name_payment;?>&custom=752&amount=<?php echo $commision_amount;?>&return=<?php echo $success_url;?>&cancel_return=<?php echo $cancel_url;?>" title="<?php echo JText::_('PAY_PAYPAL');?>">    
                                                        <?php echo $pay_name; ?>
                                                        </a>
                        <?php
                            } else {
                                $commision_amount = $amount_remain;
                        ?>
                                                        <a class="jgrid" href="https://www.paypal.com/cgi-bin/webscr?cmd=_xclick&business=<?php echo $business_email[$xcv];?>&currency_code=<?php echo $vendor_currency_code_3;?>&item_name=<?php echo $seller_name_payment;?>&custom=752&amount=<?php echo $commision_amount;?>&return=<?php echo $success_url;?>&cancel_return=<?php echo $cancel_url;?>" title="<?php echo JText::_('PAY_PAYPAL');?>">    
                                                        <?php echo $pay_name; ?>
                                                        </a>
                        <?php
                            }
                        }
                        ?>

                        <!-- 
                            End Condition for Paypal Shop Mode and Commision Amount
                        -->
                                            </p>
                                        </div>
                        <?php }
                        if ((strpos($pay_option_finals, 'standard')!==false) ) {
                            $data=new StdClass();
                            $data->pay_option_finals=$pay_option_finals;
                            $data->business_email=$business_email[$xcv];
                            $data->amount_remain=$amount_remain;
                            $data->cm_amt=$cm_amt;
                            $data->commision_amount=$commision_amount;
                            $dataStr=urlencode(json_encode($data));
                        ?> 
                                        <div class="pay_wire" id="standard_pay<?php echo $ix; ?>">
                                            <p>
                                                <a class="modal" rel="{handler: 'iframe', size: {x: 300, y: 300}}" href="<?php echo JURI::root()?>administrator/index.php?option=com_marketplace&view=commisions&layout=standard&tmpl=component&seller_id=<?php echo $item->seller_id."&data=".$dataStr; ?>" title="<?php echo JText::_('PAY_STANDARD');?>">
                                                <!--<a class="jgrid payment_wire_amount" id="<?php echo $item->seller_id.$xcv; ?>" href="javascript:void(0);" title="<?php echo JText::_('PAY_STANDARD');?>">-->    
                                                    <?php echo $pay_name; ?>
                                                </a>
                                                
                                            </p>
                                        </div>
                        <?php
                        } 
                        if ($value=='remitasplit') {
                            $modalurl=JRoute::_('index.php?option=com_marketplace&tmpl=component&view=commisions&layout=payremita&sellerid='.$item->seller_id, false); 
                        ?>
                                        <div class="remitapaynow" id="remita_pay">        
                                            <a href="<?php echo $modalurl?>" rel="{handler: 'iframe', size: {x: 600, y: 500}}" class="modal">
                                                <?php echo JText::_('Remita'); ?>
                                             </a>    
                                        </div>
                        <?php
                        }
                    } 
                }
                ?>
                    </span>
                <?php
                    }
                ?>
                </td>
                <td >
                    <?php 
                        echo $cm_amt." ".$this->currencySymbol;
                    ?>
                </td>

                <td>
                    <?php echo (int) $item->commision_id; ?>
                </td>
            </tr>
            <?php
            $k = 1 - $k;
            }
        ?>

    <!---*************   End For Loop  *************************---->
        
    <?php
        } else {
            echo "<tr ><td colspan='12'><div class='alert alert-info'>".
            "<strong>Info!</strong>".JText::_('NO_RECORD_FOUND')."</div></td></tr>";
        }
    ?>
        </tbody>

        </table>
      <input type="hidden" name="task" value="" />
      <input type="hidden" name="boxchecked" value="0" />
    <?php
    echo JHTML::_('form.token');
    ?>
 
</form>
<script type="text/javascript">
    jQuery(document).ready(function () {
        jQuery('.popupBoxClose').on('click', function () {
            unloadPopupBox();
        });
        jQuery('.payment_wire_amount').on('click', function (e) {
            var id = jQuery(this).attr('id');
            jQuery('#xyz' + id).fadeIn("slow");
            jQuery(".payment_wire").css({ // this is just for style
                "opacity": "0.3"
            });
            e.preventDefault();
        });
        function unloadPopupBox() { // TO Unload the Popupbox
            jQuery('.payment_wire_pop').fadeOut("slow");
            jQuery(".payment_wire").css({ // this is just for style        
                "opacity": "1"
            });
        }
        function loadPopupBox(idx) { // To Load the Popupbox
        }
    });
</script>
