
<?php $__env->startSection('content'); ?>  
<div class="row show-grid-forms my_content">
    <?php if(Session::has('success')): ?>
        <div class="alert alert-success" style="width: 1120px;">
            <?php echo e(Session::get('success')); ?>

        </div>
    <?php endif; ?>
    <div class="col-sm-2" style="text-align: right;">
        <b>Get Report By</b>
    </div>
    <div class="col-sm-2">
        <?php 
            $report_type = array(''=>'Select Type','market_place'=>'Market Place','source'=>'Source','monthly'=>'Monthly'); 
        ?>
        <?php echo Form::open(array('id'=>'report_entry_form','class' => 'form-horizontal','role' => 'form','action' => 'ReportController@get_reports','method' => 'POST','accept-charset' => 'ISO-8859-1')); ?>

        <?php echo Form::select('report_type', $report_type, null, ['class' => 'select_dropdown']); ?>

    </div>
    <div class="col-sm-2" style="width: 40px;">
        <b>From</b>
    </div>
    <div class="col-sm-2">
        <?php echo Form::text('date_from','',array('required'=>'required','class'=>'form-control input_date')); ?>

    </div>
    <div class="col-sm-2" style="width: 30px;">
        <b>To</b>
    </div>
    <div class="col-sm-2">
        <?php echo Form::text('date_to','',array('required'=>'required','class'=>'form-control input_date')); ?>

    </div>
    <div class="col-sm-2">
        <?php echo Form::submit('Submit', array('class' => 'btn btn-primary btn-label-left')); ?>

        <?php echo Form::close(); ?>   
    </div>
</div>

<?php if(isset($reprt_type)): ?>
    <?php if(!empty($reports)): ?> 
    <?php
    $i=1;
        ?> 
        <div class="box-content my_content">				
            <table class="table">
                <thead>
                    <tr>
                        <?php if($reprt_type == 'monthly'): ?>
                            <th>Month</th>                                   
                        <?php else: ?>
                            <th>Date</th>
                        <?php endif; ?>
                        <th>Total Sales</th>
                        <th>Total Shipping Cost</th>
                        <th>Total Net Cost</th>
                        <th>Total Tax</th>
                        <th>Total Profit</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach($reports as $report_date =>$report_datas): ?>
                    <?php
                        if($reprt_type == 'market_place' || $reprt_type == 'monthly')    
                        {
                           $market_place;                                   
                        }
                        else
                        {
                           $source_name; 
                        }                        
                        $total_sales =0;
                        $total_profit =0;
                        $total_shipping =0;
                        $total_net_cost = 0;
                        $total_tax = 0;
                        
                        $sub_total_sales =0;
                        $sub_total_shipping = 0;
                        $semi_total_net_cost = 0;
                        $semi_total_tax = 0;
                        $sub_total_com = 0;
                        $sub_toal_profit = 0;
                                                    
                        foreach ($report_datas as $report_data)
                        {
                            $total_sales += ($report_data['selling_price']*$report_data['quantity']);                                
                            if($report_data['refund']>0)
                            {
                                $total_profit += ($report_data['selling_price']*$report_data['quantity']) - $report_data['shipping_cost'] - $report_data['refund'];
                                $total_tax += 0;
                                $total_net_cost += 0;
                            }
                            else
                            {
                                $total_profit +=  ($report_data['selling_price']*$report_data['quantity']) - ($report_data['shipping_cost']+($report_data['net_cost']*$report_data['quantity'])+($report_data['commission']*$report_data['quantity']));
                                $total_tax += $report_data['tax']*$report_data['quantity'];
                                $total_net_cost += $report_data['net_cost']*$report_data['quantity'];
                            }
                            $total_shipping += $report_data['shipping_cost'];
                        }
                    ?>
                        <tr>
                            <td>
                                <a href="" class="total_report_head" id="<?php echo e($i); ?>">
                                    <?php if($reprt_type == 'monthly'): ?>
                                        <?php echo e($report_date); ?>                                    
                                    <?php else: ?>
                                        <?php $date = new DateTime($report_date); ?>
                                        <?php echo e($date->format('d F Y')); ?>

                                    <?php endif; ?>
                                </a>
                            </td>
                            <td>
                                $<?php echo e(number_format($total_sales,2)); ?>

                            </td>
                            <td>
                                $<?php echo e(number_format($total_shipping,2)); ?>

                            </td>
                            <td>
                                $<?php echo e(number_format($total_net_cost,2)); ?>

                            </td>
                            <td>
                                $<?php echo e(number_format($total_tax,2)); ?>

                            </td>
                            <td <?php if($total_profit < 0) echo 'class="loss"'; ?>>
                                $<?php echo e(number_format($total_profit,2)); ?>

                            </td>
                        </tr>
                        <tr class="tr_class" id="tr_class_id_<?php echo e($i); ?>" style="background-color: #C7D7E3"> 
                            <td colspan="6" style="background-color: #C7D7E3">
                                <div class="box-content my_content report_table_div" id="report_table_<?php echo e($i); ?>" style="background-color: #C7D7E3">
                                    <?php foreach($report_datas as $key=>$report_data): ?>
                                        
                                        <!--  for first time start-->
                                        <?php if($reprt_type == 'market_place' || $reprt_type == 'monthly'): ?>
                                            <!--  for market place start-->
                                            <?php if(empty($market_place)): ?>
                                                <?php $market_place = $report_data['market_place_name']; ?>
                                                <div class="midle <?php echo e(str_replace(' ', '_', $report_data['market_place_name'])); ?>">
                                                    <div class="dott">
                                                        Detailed Report of <?php echo e($report_data['market_place_name']); ?>

                                                    </div>
                                                </div> 
                                                <table class="table <?php echo e(str_replace(' ', '_', $report_data['market_place_name'])); ?>">
                                                 <thead>
                                                    <tr>
                                                        <th>Order ID</th>
                                                        <th>SKU</th>
                                                        <th class="amount">Sales</th>
                                                        <th class="amount">Shipping</th>
                                                        <th class="amount">Net Cost</th> 
                                                        <th class="amount">Tax</th>
                                                        <th class="amount">Commission</th>
                                                        <th style="text-align: center;">Quantity</th>
                                                        <th class="amount" style="padding-right: 50px;">Profit</th>
                                                        <th>Source</th>
                                                        <th></th>
                                                        <th></th>
                                                    </tr>
                                                </thead>
                                                <tbody>
                                            <?php endif; ?>
                                            <!--  for market place end-->
                                        <?php else: ?>
                                            <!--  for source name start-->
                                            <?php if(empty($source_name)): ?>
                                                <?php $source_name = $report_data['source_name']; ?>
                                                <div class="midle <?php echo e(str_replace(' ', '_', $report_data['source_name'])); ?>">
                                                    <div class="dott">
                                                        Detailed Report of <?php echo e($report_data['source_name']); ?>

                                                    </div>
                                                </div> 
                                                <table class="table <?php echo e(str_replace(' ', '_', $report_data['source_name'])); ?>">                                        
                                                <thead>
                                                    <tr>
                                                        <th>Order ID</th>
                                                        <th>SKU</th>
                                                        <th class="amount">Sales</th>
                                                        <th class="amount">Shipping</th>
                                                        <th class="amount">Net Cost</th> 
                                                        <th class="amount">Tax</th>
                                                        <th class="amount">Commission</th>
                                                        <th style="text-align: center;">Quantity</th>
                                                        <th class="amount" style="padding-right: 50px;">Profit</th>
                                                        <th>Market Place</th>
                                                        <th></th>
                                                        <th></th>
                                                    </tr>
                                                </thead>
                                                <tbody>
                                            <?php endif; ?>
                                            <!--  for source name end-->
                                        <?php endif; ?>
                                        <!--  for first time end-->
                                         
                                        
                                    <?php if($reprt_type == 'market_place' || $reprt_type == 'monthly'): ?>
                                        <!--  for Every new market place name start-->                                            
                                        <?php if($market_place != $report_data['market_place_name']): ?>
                                            <!--  Sub total print start-->
                                            <tr class="sub_total_row">
                                                <td colspan="2" style="vertical-align: middle;"><b>Sub Total</b></td>
                                                <td class="amount_sub_total" style="vertical-align: middle;">$<?php echo e(number_format($sub_total_sales, 2)); ?></td>
                                                <td class="amount_sub_total" style="vertical-align: middle;">$<?php echo e(number_format($sub_total_shipping, 2)); ?></td>
                                                <td class="amount_sub_total" style="vertical-align: middle;">$<?php echo e(number_format($semi_total_net_cost, 2)); ?></td>
                                                <td class="amount_sub_total" style="vertical-align: middle;">$<?php echo e(number_format($semi_total_tax, 2)); ?></td>
                                                <td class="amount_sub_total" style="vertical-align: middle;">$<?php echo e(number_format($sub_total_com, 2)); ?></td>
                                                <td></td>        
                                                <td class="amount_sub_total <?php if($sub_toal_profit < 0) echo 'loss'; ?>" style="vertical-align: middle;padding-right: 50px;">$<?php echo e(number_format($sub_toal_profit, 2)); ?></td>                                                                
                                                <td></td>
                                                <td></td>
                                                <td></td>
                                            </tr>
                                            <!--  Sub total print end-->
                                            <!--  Sub total initialization start-->
                                            <?php
                                                $market_place = $report_data['market_place_name'];
                                                $sub_total_sales =0;
                                                $sub_total_shipping = 0;
                                                $semi_total_net_cost = 0;
                                                $semi_total_tax = 0;
                                                $sub_total_com = 0;
                                                $sub_toal_profit = 0;
                                            ?>
                                            <!--  Sub total initialization end-->
                                        </tbody>                                
                                    </table>
                                    <br/>
                                    <div class="midle <?php echo e(str_replace(' ', '_', $report_data['market_place_name'])); ?>">
                                        <div class="dott">
                                            Detailed Report of <?php echo e($report_data['market_place_name']); ?>

                                        </div>
                                    </div>
                                    <table class="table <?php echo e(str_replace(' ', '_', $report_data['market_place_name'])); ?>">
                                        <thead>
                                            <tr>
                                                <th>Order ID</th>
                                                <th>SKU</th>
                                                <th class="amount">Sales</th>
                                                <th class="amount">Shipping</th>
                                                <th class="amount">Net Cost</th> 
                                                <th class="amount">Tax</th>
                                                <th class="amount">Commission</th>
                                                <th style="text-align: center;">Quantity</th>
                                                <th class="amount" style="padding-right: 50px;">Profit</th>
                                                <th>Source</th>
                                                <th></th>
                                                <th></th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                    <?php endif; ?>                                              
                                    <!--  for Every new market place name end-->
                                    <?php else: ?>
                                        <?php if($source_name != $report_data['source_name']): ?>
                                            <!--  Sub total print start-->
                                            <tr class="sub_total_row">
                                                <td colspan="2" style="vertical-align: middle;"><b>Sub Total</b></td>
                                                <td class="amount_sub_total" style="vertical-align: middle;">$<?php echo e(number_format($sub_total_sales, 2)); ?></td>
                                                <td class="amount_sub_total" style="vertical-align: middle;">$<?php echo e(number_format($sub_total_shipping, 2)); ?></td>
                                                <td class="amount_sub_total" style="vertical-align: middle;">$<?php echo e(number_format($semi_total_net_cost, 2)); ?></td>
                                                <td class="amount_sub_total" style="vertical-align: middle;">$<?php echo e(number_format($semi_total_tax, 2)); ?></td>
                                                <td class="amount_sub_total" style="vertical-align: middle;">$<?php echo e(number_format($sub_total_com, 2)); ?></td>
                                                <td></td>        
                                                <td class="amount_sub_total <?php if($sub_toal_profit < 0) echo 'loss'; ?>" style="vertical-align: middle;padding-right: 50px;">$<?php echo e(number_format($sub_toal_profit, 2)); ?></td>                                                                
                                                <td></td>
                                                <td></td>
                                                <td></td>
                                            </tr>
                                            <!--  Sub total print end-->
                                            <!--  Sub total initialization start-->
                                            <?php
                                                $source_name = $report_data['source_name'];
                                                $sub_total_sales =0;
                                                $sub_total_shipping = 0;
                                                $semi_total_net_cost = 0;
                                                $semi_total_tax = 0;
                                                $sub_total_com = 0;
                                                $sub_toal_profit = 0;
                                            ?>
                                            <!--  Sub total initialization end-->
                                        </tbody>                                
                                    </table>
                                    <br/>
                                    <div class="midle <?php echo e(str_replace(' ', '_', $report_data['source_name'])); ?>">
                                        <div class="dott">
                                            Detailed Report of <?php echo e($report_data['source_name']); ?>

                                        </div>
                                    </div>
                                    <table class="table <?php echo e(str_replace(' ', '_', $report_data['source_name'])); ?>">
                                        <thead>
                                            <tr>
                                                <th>Order ID</th>
                                                <th>SKU</th>
                                                <th class="amount">Sales</th>
                                                <th class="amount">Shipping</th>
                                                <th class="amount">Net Cost</th> 
                                                <th class="amount">Tax</th>
                                                <th class="amount">Commission</th>
                                                <th style="text-align: center;">Quantity</th>
                                                <th class="amount" style="padding-right: 50px;">Profit</th>
                                                <th>Market Place</th>
                                                <th></th>
                                                <th></th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                        <?php endif; ?>
                                    <?php endif; ?> 
                                        <!--  Common data start-->
                                        <tr>
                                            <td style="width: 150px;"><?php echo e($report_data['order_id']); ?></td>
                                            <?php if($report_data['refund']>0): ?>                                            
                                                <td style="width: 150px;"><?php echo e($report_data['item_sku']); ?>  (R- <?php echo e($report_data['refund']); ?>)</td>
                                            <?php else: ?>
                                                <td style="width: 150px;"><?php echo e($report_data['item_sku']); ?></td>
                                            <?php endif; ?>
                                            <td class="amount">$<?php echo e(number_format($report_data['selling_price']*$report_data['quantity'], 2)); ?></td>
                                            <td class="amount">$<?php echo e(number_format($report_data['shipping_cost'], 2)); ?></td>
                                            <?php if($report_data['refund']>0): ?>
                                                <td></td>
                                                <td></td>
                                                <td></td>
                                            <?php else: ?>
                                                <td class="amount">$<?php echo e(number_format($report_data['net_cost']*$report_data['quantity'], 2)); ?></td>
                                                <td class="amount">$<?php echo e(number_format($report_data['tax']*$report_data['quantity'], 2)); ?></td>
                                                <td class="amount">$<?php echo e(number_format($report_data['commission']*$report_data['quantity'], 2)); ?></td>
                                            <?php endif; ?>
                                            <td style="text-align: center;"><?php echo e($report_data['quantity']); ?></td>
                                            <?php 
                                                if($report_data['refund']>0)
                                                {
                                                    $profit = ($report_data['selling_price']*$report_data['quantity'])-$report_data['shipping_cost']-$report_data['refund']; 
                                                }
                                                else
                                                {
                                                    $profit = ($report_data['selling_price']*$report_data['quantity'])-($report_data['shipping_cost']+($report_data['net_cost']*$report_data['quantity'])+($report_data['commission']*$report_data['quantity'])); 
                                                }    
                                            ?>
                                            <td class="amount <?php if($profit < 0) echo 'loss'; ?>" style="padding-right: 50px;">                                                
                                                $<?php echo e(number_format($profit, 2)); ?>                                                        
                                            </td>
                                            <?php if($reprt_type == 'market_place' || $reprt_type == 'monthly'): ?>
                                                <td style="width: 100px;"><?php echo e($report_data['source_name']); ?></td>
                                            <?php else: ?>
                                                <td style="width: 100px;"><?php echo e($report_data['market_place_name']); ?></td>
                                            <?php endif; ?>
                                            <td>
                                                <a title="Edit" href="reports/<?php echo e($report_data['report_id']); ?>">
                                                    <img src="<?php echo e(asset('images/edit.png')); ?>" height="20" width="20" alt="Edit">
                                                </a>
                                            </td>
                                            <td>
                                                <a title="Delete" href="report_delete/<?php echo e($report_data['report_id']); ?>">
                                                    <img src="<?php echo e(asset('images/trash_icon.png')); ?>" height="20" width="20" alt="Delete">
                                                </a>
                                            </td>
                                        </tr>
                                        <!--  Common data end-->
                                        
                                            <!--  Sub Total calculation end-->    
                                            <?php
                                                $sub_total_sales += $report_data['selling_price']*$report_data['quantity'];
                                                $sub_total_shipping += $report_data['shipping_cost'];
                                                if($report_data['refund']>0)
                                                {
                                                    $semi_total_net_cost += 0;
                                                    $semi_total_tax += 0;
                                                    $sub_total_com += 0;
                                                }
                                                else
                                                {
                                                    $semi_total_net_cost += $report_data['net_cost']*$report_data['quantity'];
                                                    $semi_total_tax += $report_data['tax']*$report_data['quantity'];
                                                    $sub_total_com += $report_data['commission']*$report_data['quantity'];
                                                }
                                                //$sub_toal_profit += $report_data['selling_price']*$report_data['quantity'] -($report_data['shipping_cost']+($report_data['net_cost']*$report_data['quantity'])+($report_data['tax']*$report_data['quantity']+($report_data['commission']*$report_data['quantity'])));
                                                $sub_toal_profit += $profit;
                                            ?>
                                            <!--  Sub Total calculation start-->
                                            
                                            <!--  Last value of a specific date start-->
                                            <?php if(count($report_datas)-1 == $key): ?>
                                                <!--  Sub total print start-->
                                                    <tr class="sub_total_row">
                                                        <td colspan="2" style="vertical-align: middle;"><b>Sub Total</b></td>
                                                        <td class="amount_sub_total" style="vertical-align: middle;">$<?php echo e(number_format($sub_total_sales, 2)); ?></td>
                                                        <td class="amount_sub_total" style="vertical-align: middle;">$<?php echo e(number_format($sub_total_shipping, 2)); ?></td>
                                                        <td class="amount_sub_total" style="vertical-align: middle;">$<?php echo e(number_format($semi_total_net_cost, 2)); ?></td>
                                                        <td class="amount_sub_total" style="vertical-align: middle;">$<?php echo e(number_format($semi_total_tax, 2)); ?></td>
                                                        <td class="amount_sub_total" style="vertical-align: middle;">$<?php echo e(number_format($sub_total_com, 2)); ?></td>
                                                        <td></td>        
                                                        <td class="amount_sub_total <?php if($sub_toal_profit < 0) echo 'loss'; ?>" style="vertical-align: middle;padding-right: 50px;">$<?php echo e(number_format($sub_toal_profit, 2)); ?></td>                                                                
                                                        <td></td>
                                                        <td></td>
                                                        <td></td>
                                                    </tr>
                                                <!--  Sub total print end-->
                                            </tbody>                                
                                        </table>
                                        <?php endif; ?>
                                        <!--  Last value of a specific date end-->                                        
                                    <?php endforeach; ?> 
                                    <?php
                                        if($reprt_type == 'market_place' || $reprt_type == 'monthly')    
                                        {
                                            unset($market_place);                                                        
                                        }
                                        else
                                        {
                                            unset($source_name); 
                                        }
                                    ?>
                                </div>
                            </td> 
                        </tr>
                        <?php
                            $i++; 
                        ?>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
        <?php else: ?>
            <div class="midle">
                There is no report
            </div>
        <?php endif; ?>
    <?php endif; ?>
<?php $__env->stopSection(); ?>
<?php echo $__env->make('layout.default', array_except(get_defined_vars(), array('__data', '__path')))->render(); ?>