@extends('layout.default')
@section('content')  
<div class="row show-grid-forms my_content">
    @if (Session::has('success'))
        <div class="alert alert-success" style="width: 1120px;">
            {{ Session::get('success') }}
        </div>
    @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'); 
        ?>
        {!! Form::open(array('id'=>'report_entry_form','class' => 'form-horizontal','role' => 'form','action' => 'ReportController@get_reports','method' => 'POST','accept-charset' => 'ISO-8859-1')) !!}
        {!! 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">
        {!! 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">
        {!! Form::text('date_to','',array('required'=>'required','class'=>'form-control input_date'))!!}
    </div>
    <div class="col-sm-2">
        {!! Form::submit('Submit', array('class' => 'btn btn-primary btn-label-left')) !!}
        {!! Form::close() !!}   
    </div>
</div>

@if(isset($reprt_type))
    @if(!empty($reports)) 
    <?php
    $i=1;
        ?> 
        <div class="box-content my_content">				
            <table class="table">
                <thead>
                    <tr>
                        @if($reprt_type == 'monthly')
                            <th>Month</th>                                   
                        @else
                            <th>Date</th>
                        @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>
                    @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="{{ $i }}">
                                    @if($reprt_type == 'monthly')
                                        {{ $report_date }}                                    
                                    @else
                                        <?php $date = new DateTime($report_date); ?>
                                        {{ $date->format('d F Y') }}
                                    @endif
                                </a>
                            </td>
                            <td>
                                ${{ number_format($total_sales,2) }}
                            </td>
                            <td>
                                ${{ number_format($total_shipping,2)}}
                            </td>
                            <td>
                                ${{ number_format($total_net_cost,2)}}
                            </td>
                            <td>
                                ${{ number_format($total_tax,2)}}
                            </td>
                            <td <?php if($total_profit < 0) echo 'class="loss"'; ?>>
                                ${{ number_format($total_profit,2)}}
                            </td>
                        </tr>
                        <tr class="tr_class" id="tr_class_id_{{ $i }}" style="background-color: #C7D7E3"> 
                            <td colspan="6" style="background-color: #C7D7E3">
                                <div class="box-content my_content report_table_div" id="report_table_{{ $i }}" style="background-color: #C7D7E3">
                                    @foreach ($report_datas as $key=>$report_data)
                                        
                                        <!--  for first time start-->
                                        @if($reprt_type == 'market_place' || $reprt_type == 'monthly')
                                            <!--  for market place start-->
                                            @if(empty($market_place))
                                                <?php $market_place = $report_data['market_place_name']; ?>
                                                <div class="midle {{str_replace(' ', '_', $report_data['market_place_name'])}}">
                                                    <div class="dott">
                                                        Detailed Report of {{ $report_data['market_place_name'] }}
                                                    </div>
                                                </div> 
                                                <table class="table {{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>
                                            @endif
                                            <!--  for market place end-->
                                        @else
                                            <!--  for source name start-->
                                            @if(empty($source_name))
                                                <?php $source_name = $report_data['source_name']; ?>
                                                <div class="midle {{str_replace(' ', '_', $report_data['source_name'])}}">
                                                    <div class="dott">
                                                        Detailed Report of {{ $report_data['source_name'] }}
                                                    </div>
                                                </div> 
                                                <table class="table {{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>
                                            @endif
                                            <!--  for source name end-->
                                        @endif
                                        <!--  for first time end-->
                                         
                                        
                                    @if($reprt_type == 'market_place' || $reprt_type == 'monthly')
                                        <!--  for Every new market place name start-->                                            
                                        @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;">${{number_format($sub_total_sales, 2)}}</td>
                                                <td class="amount_sub_total" style="vertical-align: middle;">${{number_format($sub_total_shipping, 2)}}</td>
                                                <td class="amount_sub_total" style="vertical-align: middle;">${{number_format($semi_total_net_cost, 2)}}</td>
                                                <td class="amount_sub_total" style="vertical-align: middle;">${{number_format($semi_total_tax, 2)}}</td>
                                                <td class="amount_sub_total" style="vertical-align: middle;">${{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;">${{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 {{str_replace(' ', '_', $report_data['market_place_name'])}}">
                                        <div class="dott">
                                            Detailed Report of {{ $report_data['market_place_name'] }}
                                        </div>
                                    </div>
                                    <table class="table {{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>
                                    @endif                                              
                                    <!--  for Every new market place name end-->
                                    @else
                                        @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;">${{number_format($sub_total_sales, 2)}}</td>
                                                <td class="amount_sub_total" style="vertical-align: middle;">${{number_format($sub_total_shipping, 2)}}</td>
                                                <td class="amount_sub_total" style="vertical-align: middle;">${{number_format($semi_total_net_cost, 2)}}</td>
                                                <td class="amount_sub_total" style="vertical-align: middle;">${{number_format($semi_total_tax, 2)}}</td>
                                                <td class="amount_sub_total" style="vertical-align: middle;">${{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;">${{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 {{str_replace(' ', '_', $report_data['source_name'])}}">
                                        <div class="dott">
                                            Detailed Report of {{ $report_data['source_name'] }}
                                        </div>
                                    </div>
                                    <table class="table {{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>
                                        @endif
                                    @endif 
                                        <!--  Common data start-->
                                        <tr>
                                            <td style="width: 150px;">{{ $report_data['order_id'] }}</td>
                                            @if($report_data['refund']>0)                                            
                                                <td style="width: 150px;">{{ $report_data['item_sku'] }}  (R- {{$report_data['refund']}})</td>
                                            @else
                                                <td style="width: 150px;">{{ $report_data['item_sku'] }}</td>
                                            @endif
                                            <td class="amount">${{ number_format($report_data['selling_price']*$report_data['quantity'], 2) }}</td>
                                            <td class="amount">${{ number_format($report_data['shipping_cost'], 2) }}</td>
                                            @if($report_data['refund']>0)
                                                <td></td>
                                                <td></td>
                                                <td></td>
                                            @else
                                                <td class="amount">${{ number_format($report_data['net_cost']*$report_data['quantity'], 2) }}</td>
                                                <td class="amount">${{ number_format($report_data['tax']*$report_data['quantity'], 2) }}</td>
                                                <td class="amount">${{ number_format($report_data['commission']*$report_data['quantity'], 2) }}</td>
                                            @endif
                                            <td style="text-align: center;">{{ $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;">                                                
                                                ${{ number_format($profit, 2) }}                                                        
                                            </td>
                                            @if($reprt_type == 'market_place' || $reprt_type == 'monthly')
                                                <td style="width: 100px;">{{ $report_data['source_name'] }}</td>
                                            @else
                                                <td style="width: 100px;">{{ $report_data['market_place_name'] }}</td>
                                            @endif
                                            <td>
                                                <a title="Edit" href="reports/{{ $report_data['report_id'] }}">
                                                    <img src="{{asset('images/edit.png')}}" height="20" width="20" alt="Edit">
                                                </a>
                                            </td>
                                            <td>
                                                <a title="Delete" href="report_delete/{{ $report_data['report_id'] }}">
                                                    <img src="{{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-->
                                            @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;">${{number_format($sub_total_sales, 2)}}</td>
                                                        <td class="amount_sub_total" style="vertical-align: middle;">${{number_format($sub_total_shipping, 2)}}</td>
                                                        <td class="amount_sub_total" style="vertical-align: middle;">${{number_format($semi_total_net_cost, 2)}}</td>
                                                        <td class="amount_sub_total" style="vertical-align: middle;">${{number_format($semi_total_tax, 2)}}</td>
                                                        <td class="amount_sub_total" style="vertical-align: middle;">${{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;">${{number_format($sub_toal_profit, 2)}}</td>                                                                
                                                        <td></td>
                                                        <td></td>
                                                        <td></td>
                                                    </tr>
                                                <!--  Sub total print end-->
                                            </tbody>                                
                                        </table>
                                        @endif
                                        <!--  Last value of a specific date end-->                                        
                                    @endforeach 
                                    <?php
                                        if($reprt_type == 'market_place' || $reprt_type == 'monthly')    
                                        {
                                            unset($market_place);                                                        
                                        }
                                        else
                                        {
                                            unset($source_name); 
                                        }
                                    ?>
                                </div>
                            </td> 
                        </tr>
                        <?php
                            $i++; 
                        ?>
                    @endforeach
                </tbody>
            </table>
        </div>
        @else
            <div class="midle">
                There is no report
            </div>
        @endif
    @endif
@stop