@extends('layouts.fixed')
@section('title','Stock Transaction report')
@section('css')
    <style>
        body {
            overflow-y: hidden;
            /*font-family: "Times New Roman";*/
            color: #000000;
        }
        .table-bordered td, .table-bordered th {
            border: 0.1rem solid rgba(8, 8, 8, 0.33) !important;
        !important;
        }

        h5.heading-title {
            text-transform: initial;
            font-weight: bold;
            letter-spacing: 1px;
        }

        tr th, tr td {
            line-height: 1.0;
            font-size: 13px;
            padding: 7px !important;

        }

        tr th, span strong, div span {
            text-transform: initial;
            font-weight: bold;
        }

        @media print {
            @page {
                size: A4 landscape;
                padding: 0 !important;
                margin: 0 !important;
            }
            .table-bordered td, .table-bordered th {
                border: 0.1rem solid rgba(8, 8, 8, 0.50) !important;
            }

            body{
                overflow: hidden !important;
                color: #000000;
            }

            div.card-body{
                margin: auto 20px;
            }

            div.card-body{
                padding-top: 0;
            }
            .table .thead-dark th {
                color: #000 !important;
                background-color: #dadada !important;
                /*border-color: #32383e !important;*/
            }
            /*body {*/
            /*-webkit-print-color-adjust: exact;*/
            /*}*/

        }
    </style>
@stop
@section('content')

    <!-- Content Header (Page header) -->
    <section class="content-header">
        <div class="container-fluid">
            <div class="row mb-2">
                <div class="col-sm-6">
                    <h1>Stock Transaction</h1>
                </div>
                <div class="col-sm-6">
                    <ol class="breadcrumb float-sm-right">
                        <li class="breadcrumb-item"><a href="#">Report</a></li>
                        <li class="breadcrumb-item active">Stock Transaction</li>
                    </ol>
                </div>
            </div>
        </div><!-- /.container-fluid -->
    </section>

    <section class="content no-print">
        <div class="container-fluid">
            {{--start --}}
            <div class="col-lg-12 col-sm-8 col-md-6 col-xs-12">
                <div class="card card-primary card-outline">
                    <div class="card-body " style="padding-bottom:0">
                        {{ Form::open(['route'=>'stock.transaction','method'=>'get']) }}

                        <div class="form-row">
                        <div class="form-group col-md-3" >
                                <label>Select Group</label>
                                <select name="group_id" class="form-control select2">
                                        <option value="">Select Group</option>
                                      
                                        @foreach($prodects_groups as $key => $group)
                                            @php
                                                $count_items = 0;
                                                $products = count(App\Product::where('group_id',$key)->get(['id']));
                                               
                                            @endphp
                                            @if($products > 0)
                                                <option value="{{ $key }}">{{ $group }}</option>
                                            @endif
                                        @endforeach
                                    </select>   
                                
                                </div>
                            <div class="form-group col-md-3" >
                                <label>From</label>
                                <input type="" name="from" class="form-control" value="{{date('d-m-Y',strtotime($from)) }}">
                            </div>
                            <div class="form-group col-md-3" >
                                <label>To</label>
                                <input type="" name="to" class="form-control" value="{{date('d-m-Y',strtotime($to)) }}">
                            </div>
                           
                            <div class="form-group col-md-1" style="margin-top: 30px">
                                <button type="submit" class="btn btn-info btn-md btn-block"><i class="fa fa-search"></i>&nbsp;Search</button>
                            </div>
                            <div class="form-group col-md-1" style="margin-top: 30px">
                                <button class="btn btn-success btn-md btn-block" onclick="window.print(); return false;"><i class="fa fa-print"></i>&nbsp;Print</button>
                            </div>

                        </div>
                        {{  Form::close() }}
                    </div>
                </div>
                <!-- /.card -->
            </div>
        </div>{{--end --}}
    </section>

@if(!empty($groups))
    @php
        $total = count($stockTransaction);
        $total_opening_qty = 0;
        $total_opening_price = 0;
        $total_goods_received_qty = 0;
        $total_goods_received_price = 0;
        $total_goods_issue_qty = 0;
        $total_goods_issue_price = 0;
        $total_available_qty = 0;
        $total_closing_qty = 0;
        $total_closing_price = 0;
        
        $pageNo= 1;
        $i=1;
    @endphp
    @foreach(collect($stockTransaction)->chunk(19) as $chunkStocks)
        @php
            $total -=count($chunkStocks);
        @endphp
        <section class="content">
            <div class="col-lg-12">
                <div class="card"><br>
                    <div class="card-body">

                        <div class="row">
                            <div class="col-md-12 text-right" style="margin:2px 30px 0 0">
                                <h6>Page No: {{$pageNo++}}</h6>
                            </div>
                            <div class="col-md-12 text-center">
                                <h3><b>  {{ is_numeric(Auth::user()->active_unit) ? \App\Unit::query()->findOrFail(Auth::user()->active_unit)->name : 'NULL' }}</b></h3>
                                <h5><b>Inventory Transaction</b></h5>
                                {{-- <h5>Transaction Type : Purchase</h5> --}}
                                <h6>Report From {{ dateFormat($from) }} To {{ dateFormat($to) }}</h6>
                            </div>
                        </div>
                        {{-- view start --}}
                        <table class="table table-hover table-bordered table-condensed text-right" width="100%">
                            <thead>
                            <tr class="text-center">
                                <th width="5%" class="text-center">#SL</th>
                                <th width="10%">Group</th>
                                <th width="10%">Opening Qty</th>
                                <th width="10%">Opening Value</th>
                                <th width="10%">Goods Rcvd</th>
                                <th width="10%">Goods Rcvd Value</th>
                                <th width="10%">Goods Issue</th>
                                <th width="10%">Goods Issue Value</th>
                                {{--<th width="8%">Available Qty</th>--}}
                                <th width="7%">Closing Qty</th>
                                <th width="10%">Closing Value</th>
                            </tr>
                            </thead>
                            <tbody>
                            @php

                            @endphp
                            @foreach($chunkStocks as $stock)
                                <tr>
                                    <td class="text-center">{{ $i++ }}</td>
                                   <td class="text-left">
                                       @if(Auth::id()==35)
                                       {{Auth::id()}} <a style="text-decoration: none; color: black;" href="{{ route('indevisual.stock',[$stock['group_id'],$from,$to]) }}">{{ $stock['group_name'] }}</a>
                                       
                                       @else
                                        {{ $stock['group_name'] }}
                                       @endif
                                       
                                       </td>
                                    <td>{{round($stock['opening_qty'],2) }}</td>
                                    <td>{{round($stock['opening_value'],2) }}</td>
                                    <td>{{round($stock['goods_received_qty'],2) }}</td>
                                    <td>{{round($stock['goods_received_value'],2) }}</td>
                                    <td>{{round($stock['goods_issue_qty'],2) }}</td>
                                    <td>{{round($stock['goods_issue_value'],2) }}</td>
{{--                                    <td>{{round($stock['available_qty'],2) }}</td>--}}
                                    <td>{{round($stock['closing_qty'],2) }}</td>
                                    <td>{{ $stock['closing_qty']!=0 ? round($stock['closing_value'],2): 0 }}</td>
                                </tr>
                                @php
                                    // $total_opening_qty += $stockTransaction[$stock->id]['opening_qty'];
                                    $total_opening_qty += $stock['opening_qty'];
                                    $total_opening_price += $stock['opening_value'];
                                    $total_goods_received_qty += $stock['goods_received_qty'];
                                    $total_goods_received_price += $stock['goods_received_value'];
                                    $total_goods_issue_qty += $stock['goods_issue_qty'];
                                    $total_goods_issue_price += $stock['goods_issue_value'];
                                    $total_available_qty += $stock['available_qty'];
                                    $total_closing_qty += $stock['closing_qty'];
                                    $total_closing_price +=  $stock['closing_value'];
                                @endphp
                            @endforeach
                            @if($total  == 0)
                            <tr>
                                <td colspan="2" class="text-right"><strong>Total</strong></td>
                                <td><strong>{{ round($total_opening_qty,2) }}</strong></td>
                                <td><strong>{{ round($total_opening_price,2) }}</strong></td>
                                <td><strong>{{ round($total_goods_received_qty,2) }}</strong></td>
                                <td><strong>{{ round($total_goods_received_price,2) }}</strong></td>
                                <td><strong>{{ round($total_goods_issue_qty,2) }}</strong></td>
                                <td><strong>{{ round($total_goods_issue_price,2) }}</strong></td>
{{--                                <td><strong>{{ round($total_available_qty,2) }}</strong></td>--}}
                                <td><strong>{{ round($total_closing_qty,2) }}</strong></td>
                                <td><strong>{{ $total_closing_qty != 0 ? round($total_closing_price,2) : 0 }}</strong></td>
                            </tr>
                            @endif
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </section>
        <div class="page_break"></div>
    @endforeach

@else
    <section class="content no-print">
        <div class="container-fluid">

            <div class="col-lg-12 col-sm-8 col-md-6 col-xs-12">
                <div class="card card-primary card-outline">
                    <div class="card-body box-profile">
                        <h4 class="text-center">No Data Found</h4>
                    </div>
                </div>
                <!-- /.card -->
            </div>
        </div>
    </section>

@endif


@stop

@section('style')

@stop


@section('plugin')
    <!-- DataTables -->
    <script src="{{ asset('plugins/datatables/jquery.dataTables.js') }}"></script>
    <script src="{{ asset('plugins/datatables/dataTables.bootstrap4.js') }}"></script>
    <script src='https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/select2.min.js'></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-confirm/3.3.4/jquery-confirm.min.js"></script>
@stop

@section('script')
    <!-- page script -->
    <script type="text/javascript">
        $('.select2').select2()
    </script>
@stop
