<?php $__env->startSection('content'); ?>
<link rel="stylesheet" href="<?php echo e($url); ?>/abserve/css/jquery.dataTables.min.css">
<link rel="stylesheet" href="<?php echo e($url); ?>/abserve/css/dataTables.jqueryui.min.css">
<script type="text/javascript" src="<?php echo e(asset('abserve/js/plugins/chartjs/Chart.min.js')); ?>"></script>
<script src="<?php echo e($url); ?>/abserve/js/jquery.dataTables.min.js" type="text/javascript"></script>
<script src="<?php echo e($url); ?>/abserve/js/dataTables.jqueryui.min.js" type="text/javascript"></script>
<style type="text/css">
input[type="search"]
{
-webkit-appearance :searchfield;
}
label {
padding-left : 16px;
font-weight :normal;
}
.select2-container .select2-choice{
    width: 230px;
}
.theme-config
{
display:none;
}
</style>
<div class="page-content row">
    <div class="page-header">
            <div class="page-title">
            <h3> Product Report <small> Product Report </small></h3>
            </div>
            <ul class="breadcrumb">
            <!--  <li>
            <a href="<?php echo e(URL::to('dashboard')); ?>"> Dashboard </a></li> -->
            <li class="active">Product Report</li>
            </ul> 
          
    </div>
    <div class="page-content-wrapper">  
            <div class="sbox-title"> <h5> <i class="fa fa-table"></i>Product Report </h5>
                    <div>

                    <a href="<?php echo e(url()); ?>/product_report" class="btn btn-xs btn-white tips" title="Clear Search" style="float:  right;"><i class="fa fa-trash-o"></i> </a>

                    </div>
            </div>
    
    <?php if(Auth::check() && Auth::user()->group_id == 1): ?>

        <form action="<?php echo url();?>/weekly_report/index" class="Book_avail">
                <input type="hidden" class="hidden_url" value="<?php echo url();?>/product_report/index">
                <input type="hidden" class="show_from_date" value="<?php echo e($show_from_date); ?>">
                <input type="hidden" class="show_to_date" value="<?php echo e($show_to_date); ?>">

                <div class="sbox-content">     
                <div class="toolbar-line "> 
                <label><input type="text" id="dt1" placeholder="From" class="form-control"></label>
                <label><input type="text" id="dt2" placeholder="To" class="form-control"></label>
                <label>
<select class="site_names select2" id="site" required="required">
<option value="">Select Site</option>
 <?php foreach($site_names as $site): ?>
 <?php $selected='';
 if($site->id == $show_site){
 $selected='selected="selected"';
 $show_site_name = $site->site_name;
    } ?>
<option name="site_option" <?php echo $selected; ?> value="<?php echo e($site->id); ?>"><?php echo e($site->site_name); ?></option>  
    <?php endforeach; ?> 
    </select></label>
    <label>
<select class="product_names select2" id="product">
<option value="">Select Product</option>
 <?php foreach($product_names as $product): ?>
  <?php $selected='';
 if($product->id == $show_product){
 $selected='selected="selected"';
 $show_product_name = $product->name;
    } ?>
<option name="site_option" <?php echo $selected; ?> value="<?php echo e($product->id); ?>"><?php echo e($product->name); ?></option>  
    <?php endforeach; ?> 
    </select></label>
                <label><input type="submit" value="Submit" class="submit_search tips btn btn-sm btn-success"/></label>
                <label><a href="javascript:void(0);" class="downloadfile" title="<?php echo e(Lang::get('core.btn_download')); ?>"><i class="fa fa-download"></i>&nbsp;<?php echo e(Lang::get('core.btn_download')); ?> </a></label>
                <label><a href="javascript:void(0);" class="printfile" title="Print">Print </a></label>
                <?php if($show_clear == '1'): ?>
                <label><a href="<?php echo url();?>/product_report"/>Clear Search</a></label>
                <?php 
                $url_param='';
                $url_param.='from='.$show_from_date;
                $url_param.='&to='.$show_to_date;
                ?>
                <?php endif; ?> 
                </div>
        </form>

        <div id="second">
                <table class="print_show">
                <tr><td>From Date:</td><td><?php echo e($show_from_date); ?></td></tr>
                <tr><td>To Date:</td><td><?php echo e($show_to_date); ?></td></tr>
                <?php if(!empty($show_site)){ ?>
                <tr><td>Site:</td><td><?php echo e($show_site_name); ?></td></tr>
                <?php } ?>
                <?php if(!empty($show_product)){ ?>
                <tr><td>Product:</td><td><?php echo e($show_product_name); ?></td></tr>
                <?php } ?>
                </table>
                <br>
        </div>


                <div class="table-responsive" style="">
                            <table id="example" class="table table-striped"  style="">
                                    <thead>
                                        <tr>
                                        <th>S.No</th>
                                        <th>Product Name</th>
                                        <th>Qty </th>
                                        <th>Rate </th>
                                        <th>Amount  </th>
                                        </tr>
                                    </thead>
                                     <tbody>
                        <?php
                        //( Outwards - Returns )
                        //( Outwards - Returns )

                        if($result_all){
if(!empty($show_from_date)){
$from_date_ex = explode('-',$show_from_date);
$from_date_day = (int)$from_date_ex[0];
$from_date_month = (int)$from_date_ex[1];
$from_date_year = $from_date_ex[2];
if($from_date_day<10){
    $from_date_day='0'.$from_date_day;
} 
if($from_date_month <10){
    $from_date_month ='0'.$from_date_month ;
}
$search_from_date = $from_date_year.'-'.$from_date_month.'-'.$from_date_day;
}
if(!empty($show_to_date)){
$to_date_ex = explode('-',$show_to_date);
$to_date_day = (int)$to_date_ex[0];
$to_date_month = (int)$to_date_ex[1];
$to_date_year = $to_date_ex[2];
if($to_date_day<10){
    $to_date_day='0'.$to_date_day;
} 
if($to_date_month <10){
    $to_date_month ='0'.$to_date_month ;
}
$search_to_date = $to_date_year.'-'.$to_date_month.'-'.$to_date_day;
}
$rec=0;
$tot_product = $tot_amt = $tot_qty=$tot_out_qty=$tot_ret_qty=$tot_out_amt=$tot_ret_amt=0;
foreach ($result_all as $product_id=>$result) {
    $rec++;
    $result['out_qty'] = number_format($result['out_qty'],2,'.','');
    $result['ret_qty'] = number_format($result['ret_qty'],2,'.','');

    $result['out_rate'] = number_format($result['out_rate'],2,'.','');
    $result['ret_rate'] = number_format($result['ret_rate'],2,'.','');

    $result['out_amt'] = number_format($result['out_amt'],2,'.','');
    $result['ret_amt'] = number_format($result['ret_amt'],2,'.','');
    $qty_final = (float)$result['out_qty'] - (float)$result['ret_qty'];

$rate_final = (float)$result['out_rate'];
if(empty($result['out_rate'])){
$rate_final = (float)$result['ret_rate'];
}
     

    $tot_out_qty += $result['out_qty'];
    $tot_ret_qty += $result['ret_qty'];
    $amt_final = (float)$result['out_amt'] - (float)$result['ret_amt'];
    $tot_out_amt += $result['out_amt'];
    $tot_ret_amt += $result['ret_amt'];
if(empty($result['out_qty'])){
    $result['out_qty']=0;
}
if(empty($result['out_amt'])){
    $result['out_amt']=0;
}
if(empty($result['ret_qty'])){
    $result['ret_qty']=0;
}
if(empty($result['ret_amt'])){
    $result['ret_amt']=0;
}
    $tot_qty += $qty_final;
    
    $tot_amt +=$amt_final;

    $tot_product = $rec;
$search_site='';
    if(!empty($show_site)){
$search_site='site:equal:'.$show_site.'|';
    }
    if(!empty($search_from_date)){
$search_date = 'date:between:'.$search_from_date.':'.$search_to_date.'|';
    }

    /*( <a style="text-decoration:none" href="'.url().'/materialoutwards?search='.$search_date.$search_site.'" target="_blank">'.$result['out_qty'].'</a> - <a style="text-decoration:none" href="'.url().'/materialreturns?search=date:between:'.$search_from_date.':'.$search_to_date.'|'.$search_site.'" target="_blank">'.$result['ret_qty'].'</a> )

    ( <a style="text-decoration:none" href="'.url().'/materialoutwards?search='.$search_date.$search_site.'" target="_blank">'.number_format($result['out_amt'],2).'</a> - <a style="text-decoration:none" href="'.url().'/materialreturns?search=date:between:'.$search_from_date.':'.$search_to_date.'|'.$search_site.'" target="_blank">'.number_format($result['ret_amt'],2).'</a> )
    (Outwards - Returns)
    ( {{$tot_out_qty}} - {{ $tot_ret_qty }} )
    (Outwards - Returns)
    ( {{number_format($tot_out_amt,2)}} - {{ number_format($tot_ret_amt,2) }} )

    */
    echo '<tr>
                                                <td>'.$rec.'</td><!-- ID-->
                                                <td><a style="text-decoration:none" href="'.url().'/product_report_date?from='.$show_from_date.'&to='.$show_to_date.'&site='.$show_site.'&product='.$product_id.'" target="_blank">'.$result['name'].'</a></td><!-- Site Name-->
                                                <td>'.$qty_final.'  </td><!-- Material Qty-->
<td>'.number_format($rate_final,2).'  </td><!-- Material Qty-->
                                                <td>'.number_format($amt_final,2).'  </td><!-- Material Cost-->
                                            </tr>';
} }?>
                                               </tbody>
                            </table> </div>
                            <br><br>
                            <ul id="report_all" class="table table-striped"  style="list-style: none;">
                            <li style="list-style: none;height: 20px;clear: both;"></li>
                            <li style="list-style: none;clear: both;"><span class="col-md-3"><b>Total Products:</b></span><span class="col-md-9"><b><?php echo e($tot_product); ?></b></span></li>
                            <li style="list-style: none;clear: both;"><span class="col-md-3"><b>Total Qty:</b></span><span class="col-md-9"><b><?php echo e($tot_qty); ?> </b></span></li>
                            <li style="list-style: none;clear: both;"><span class="col-md-3"><b>Total Amount:</b></span><span class="col-md-9"><b>Rs.<?php echo e(number_format($tot_amt,2)); ?> </b></span></li>
                            </ul>

    <?php endif; ?> 

</div></div>
<script type="text/javascript">
function printData()
{
   var divToPrint=document.getElementById("example");
   var content2=document.getElementById("second");
   var report_all=document.getElementById("report_all");
   newWin= window.open("");
   newWin.document.write(content2.innerHTML+divToPrint.outerHTML+report_all.innerHTML);
   newWin.print();
   newWin.close();
}
    $(document).ready(function () {
        jQuery('.printfile').click( function() {
            printData();
        });


         jQuery('.downloadfile').click( function() {
         /*var site_selcted_name= $( '.site_names option:selected').val();
        var labour_selcted_name= $( '.lab_names option:selected').val();*/

        var from_filter = $('#dt1').val();
        var to_filter = $('#dt2').val();
        var site = $('#site').val();
        var product = $('#product').val();
        var url = '<?php echo url();?>/product_report/downloadfile'; 
       /* if(site_selcted_name== 'select')
        {
            site_selcted_name ='';
        }
        if(labour_selcted_name == 'select')
        {
            labour_selcted_name ='';
        }*/

        // if((from_filter !== '') && (to_filter == ''))
        // {
        //  alert("Please select from and to dates");
        //  return false;

        // }
        if((to_filter !== '') && (from_filter == ''))
        {
            alert("Please select from date");
            return false;

        }

        if(from_filter == '')
        {
            from_filter ='';
        }
        if(to_filter == '')
        {
            to_filter ='';
        }

   url += '?from='+from_filter+'&to='+to_filter+'&site='+site+'&product='+product;
   
window.location.href = url;
 return false;   
         });
        
        /*$('.site_names').select2();
        $('.lab_names').select2();*/
       
 $('#example').DataTable( {
       "aaSorting": [[ 0, "asc" ]],
       "iDisplayLength": 100
    } );

        //$('#example').dataTable();
       /* $('.site_names').select2('val', $('.show_site_name').val());
        $('.lab_names').select2('val', $('.show_labour_name').val());*/
        $('#dt1').val($('.show_from_date').val());
        $('#dt2').val($('.show_to_date').val());


        $("#dt1").datepicker({
           format: 'dd-mm-yyyy',
            minDate: 0,
            onSelect: function (date) {
                var dt2 = $('#dt2');
                var startDate = $(this).datepicker('getDate');
                var minDate = $(this).datepicker('getDate');
                dt2.datepicker('setDate', minDate);
                startDate.setDate(startDate.getDate() + 30);
                //sets dt2 maxDate to the last day of 30 days window
                dt2.datepicker('option', 'maxDate', startDate);
                dt2.datepicker('option', 'minDate', minDate);
                $(this).datepicker('option', 'minDate', minDate);
            }
        });
        $('#dt2').datepicker({
           format: 'dd-mm-yyyy',
        });

        jQuery('.Book_avail').submit( function() {
       /* var site_selcted_name= $( '.site_names option:selected').val();
        var labour_selcted_name= $( '.lab_names option:selected').val();*/

        var from_filter = $('#dt1').val();
        var to_filter = $('#dt2').val();
        var url = $('.hidden_url').val(); 
        var site = $('#site').val();
        var product = $('#product').val();
        /*if(site_selcted_name== 'select')
        {
            site_selcted_name ='';
        }
        if(labour_selcted_name == 'select')
        {
            labour_selcted_name ='';
        }*/

        // if((from_filter !== '') && (to_filter == ''))
        // {
        //  alert("Please select from and to dates");
        //  return false;

        // }
        if((to_filter !== '') && (from_filter == ''))
        {
            alert("Please select from date");
            return false;

        }

        if(from_filter == '')
        {
            from_filter ='';
        }
        if(to_filter == '')
        {
            to_filter ='';
        }

   url += '?from='+from_filter+'&to='+to_filter+'&site='+site+'&product='+product;
   
window.location.href = url;
 return false;
});
    });

    
</script>
<style>
#report_all li{list-style: none;}
.rows.clearfix label {
    font-weight: bold;
}
.print_hide{display:block !important;}
.print_show{display:none;}
</style>

<?php $__env->stopSection(); ?>
<?php echo $__env->make('layouts.app', array_except(get_defined_vars(), array('__data', '__path')))->render(); ?>