title = "actions/rulon-report"
url = "/actions/rulon-report/:month/:year?"
layout = "platform_main"
is_hidden = 0
==
<?php
function onStart() {
    $currentDate = Carbon\Carbon::now();
    $this["month"] = $this->param("month");
    $year = $this->param("year");



    if($this["month"] == 'default'){
        $this["month"] = $currentDate->month;
    }


    //dd($reportDatas);



    if($year){
        //$startDateOfYear = Carbon\Carbon::now()->startOfYear();
        //$formattedStartDateYear = $startDateOfYear->format('Y-m-d');

        $startDateOfYear = Romanah\Gokbakja\Models\ProductionMachine::orderBy('date', 'ASC')->first();
        $formattedStartDateYear = $startDateOfYear->date->format('Y-m-d');

        $startDate = Carbon\Carbon::create($year, $this["month"], 1);
        $formattedStartDate = $startDate->format('Y-m-d');

        $endMonthDate = $startDate->endOfMonth();
        $formattedEndDate = $endMonthDate->format('Y-m-d');

        $reportDatas = [];


        $rulonProds = Romanah\Gokbakja\Models\ProductionMachine::select('gram', 'width')
        ->whereMonth('date', $this["month"])
        ->whereYear('date', $year)
        ->groupBy('gram')->groupBy('width')
        ->orderBy('gram', 'ASC')
        ->orderBy('width', 'ASC')
        ->distinct()
        ->get();

        for ($x = 0; $x < count($rulonProds); $x++) {

            $action = Romanah\Gokbakja\Models\RulonAction::select('romanah_gokbakja_rulon_action.*')
                    ->with('product')
                    ->whereHas('product', function($qq) use($rulonProds, $x, $formattedStartDateYear, $formattedStartDate){
                    $qq->where('gram', $rulonProds[$x]->gram)
                        ->where('width', $rulonProds[$x]->width)
                        ->whereBetween('date', [$formattedStartDateYear, $formattedStartDate]);
                    })
                    ->sum('amount');


            //dd($rulonProds[$x]->width);

            $endAction = Romanah\Gokbakja\Models\RulonAction::select('romanah_gokbakja_rulon_action.*')
            ->with('product')
            ->whereHas('product', function($qq) use($rulonProds, $x, $formattedStartDateYear, $formattedEndDate){
                $qq->where('gram', $rulonProds[$x]->gram)
                    ->where('width', $rulonProds[$x]->width)
                    ->whereBetween('date', [$formattedStartDateYear, $formattedEndDate]);
            })
            ->sum('amount');


            $actionIn = Romanah\Gokbakja\Models\RulonAction::select('romanah_gokbakja_rulon_action.*')
            ->with('product')
            ->where('type', 'inbox')
            ->whereHas('product', function($qq) use($rulonProds, $x, $year, $formattedStartDate, $formattedEndDate){
                $qq->where('gram', $rulonProds[$x]->gram)
                    ->where('width', $rulonProds[$x]->width)
                    ->whereBetween('date', [$formattedStartDate, $formattedEndDate]);
            })
            ->sum('amount');


            $actionOut = Romanah\Gokbakja\Models\RulonAction::select('romanah_gokbakja_rulon_action.*')
            ->with('product')
            ->where('type', 'outbox')
            ->whereHas('product', function($qq) use($rulonProds, $x, $year, $formattedStartDate, $formattedEndDate){
                $qq->where('gram', $rulonProds[$x]->gram)
                    ->where('width', $rulonProds[$x]->width)
                    ->whereBetween('date', [$formattedStartDate, $formattedEndDate]);
            })
            ->sum('amount');



            $reportDatas[$x]["gram"] = $rulonProds[$x]->gram;
            $reportDatas[$x]["width"] = $rulonProds[$x]->width;
            $reportDatas[$x]["start_month"] = $action;
            $reportDatas[$x]["end_month"] = $endAction;
            $reportDatas[$x]["in"] = $actionIn;
            $reportDatas[$x]["out"] = $actionOut;

        }

        $this['reportDatas'] = $reportDatas;
    }else{
        
        //$startDate = Carbon\Carbon::create($year, $this["month"], 1);
        $startDateOfYear = Romanah\Gokbakja\Models\ProductionMachine::orderBy('date', 'ASC')->first();
        $formattedStartDateYear = $startDateOfYear->date->format('Y-m-d');

       // dd($formattedStartDate);

        $startDate = Carbon\Carbon::create($currentDate->year, $this["month"], 1);
        $formattedStartDate = $startDate->format('Y-m-d');

        

        $endMonthDate = $startDate->endOfMonth();
        $formattedEndDate = $endMonthDate->format('Y-m-d');

        $reportDatas = [];

        $rulonProds = Romanah\Gokbakja\Models\ProductionMachine::select('gram', 'width')
        ->whereMonth('date', $this["month"])
        ->whereYear('date', $currentDate->year)
        ->groupBy('gram')->groupBy('width')
        ->orderBy('gram', 'ASC')
        ->orderBy('width', 'ASC')
        ->distinct()
        ->get();



        for ($x = 0; $x < count($rulonProds); $x++) {

            $action = Romanah\Gokbakja\Models\RulonAction::select('romanah_gokbakja_rulon_action.*')
                    ->with('product')
                    ->whereHas('product', function($qq) use($rulonProds, $x, $formattedStartDateYear, $formattedStartDate){
                    $qq->where('gram', $rulonProds[$x]->gram)
                        ->where('width', $rulonProds[$x]->width)
                        ->whereBetween('date', [$formattedStartDateYear, $formattedStartDate]);
                    })
                    ->sum('amount');


            //dd($rulonProds[$x]->width);

            $endAction = Romanah\Gokbakja\Models\RulonAction::select('romanah_gokbakja_rulon_action.*')
            ->with('product')
            ->whereHas('product', function($qq) use($rulonProds, $x, $formattedStartDateYear, $formattedEndDate){
                $qq->where('gram', $rulonProds[$x]->gram)
                    ->where('width', $rulonProds[$x]->width)
                    ->whereBetween('date', [$formattedStartDateYear, $formattedEndDate]);
            })
            ->sum('amount');


            $actionIn = Romanah\Gokbakja\Models\RulonAction::select('romanah_gokbakja_rulon_action.*')
            ->with('product')
            ->where('type', 'inbox')
            ->whereHas('product', function($qq) use($rulonProds, $x, $year, $formattedStartDate, $formattedEndDate){
                $qq->where('gram', $rulonProds[$x]->gram)
                    ->where('width', $rulonProds[$x]->width)
                    ->whereBetween('date', [$formattedStartDate, $formattedEndDate]);
            })
            ->sum('amount');


            $actionOut = Romanah\Gokbakja\Models\RulonAction::select('romanah_gokbakja_rulon_action.*')
            ->with('product')
            ->where('type', 'outbox')
            ->whereHas('product', function($qq) use($rulonProds, $x, $year, $formattedStartDate, $formattedEndDate){
                $qq->where('gram', $rulonProds[$x]->gram)
                    ->where('width', $rulonProds[$x]->width)
                    ->whereBetween('date', [$formattedStartDate, $formattedEndDate]);
            })
            ->sum('amount');



            $reportDatas[$x]["gram"] = $rulonProds[$x]->gram;
            $reportDatas[$x]["width"] = $rulonProds[$x]->width;
            $reportDatas[$x]["start_month"] = $action;
            $reportDatas[$x]["end_month"] = $endAction;
            $reportDatas[$x]["in"] = $actionIn;
            $reportDatas[$x]["out"] = $actionOut;

        }
        $this['reportDatas'] = $reportDatas;
    }
}
?>
==
{% put styles %}
<link href="{{'assets/libs/datatables.net-bs4/css/dataTables.bootstrap4.min.css'|theme}}" rel="stylesheet"
    type="text/css" />
<link href="{{'assets/libs/datatables.net-buttons-bs4/css/buttons.bootstrap4.min.css'|theme}}" rel="stylesheet"
    type="text/css" />
<link href="{{'assets/libs/datatables.net-select-bs4/css/select.bootstrap4.min.css'|theme}}" rel="stylesheet"
    type="text/css" />
{% endput %}

<div class="container-fluid">

    <!-- start page title -->
    <div class="row">
        <div class="col-12">
            <div class="page-title-box d-flex align-items-center justify-content-between">
                <h4 class="mb-0">Aýlar Boýunça {{year}}</h4>

            </div>
        </div>
    </div>
    <!-- end page title -->

    <div class="row">
        <div class="col-12">
            <!-- Left sidebar -->
            <div class="email-leftbar card">
                <div class="mail-list mt-4">
                    <a href="/actions/rulon-report/1" class="{{ month == 1 ? 'active' : '' }}"><i
                            class="mdi mdi-archive me-2 font-size-16"></i> Ýanwar</a>
                    <a href="/actions/rulon-report/2" class="{{ month == 2 ? 'active' : '' }}"><i
                            class="mdi mdi-archive me-2 font-size-16"></i> Fewral</a>
                    <a href="/actions/rulon-report/3" class="{{ month == 3 ? 'active' : '' }}"><i
                            class="mdi mdi-archive me-2 font-size-16"></i> Mart</a>
                    <a href="/actions/rulon-report/4" class="{{ month == 4 ? 'active' : '' }}"><i
                            class="mdi mdi-archive me-2 font-size-16"></i> Aprel</a>
                    <a href="/actions/rulon-report/5" class="{{ month == 5 ? 'active' : '' }}"><i
                            class="mdi mdi-archive me-2 font-size-16"></i> Maý</a>
                    <a href="/actions/rulon-report/6" class="{{ month == 6 ? 'active' : '' }}"><i
                            class="mdi mdi-archive me-2 font-size-16"></i> Iýun</a>
                    <a href="/actions/rulon-report/7" class="{{ month == 7 ? 'active' : '' }}"><i
                            class="mdi mdi-archive me-2 font-size-16"></i> Iýul</a>
                    <a href="/actions/rulon-report/8" class="{{ month == 8 ? 'active' : '' }}"><i
                            class="mdi mdi-archive me-2 font-size-16"></i> Awgust</a>
                    <a href="/actions/rulon-report/9" class="{{ month == 9 ? 'active' : '' }}"><i
                            class="mdi mdi-archive me-2 font-size-16"></i> Sentýabr</a>
                    <a href="/actions/rulon-report/10" class="{{ month == 10 ? 'active' : '' }}"><i
                            class="mdi mdi-archive me-2 font-size-16"></i> Oktýabr</a>
                    <a href="/actions/rulon-report/11" class="{{ month == 11 ? 'active' : '' }}"><i
                            class="mdi mdi-archive me-2 font-size-16"></i> Noýabr</a>
                    <a href="/actions/rulon-report/12" class="{{ month == 12 ? 'active' : '' }}"><i
                            class="mdi mdi-archive me-2 font-size-16"></i> Dekabr</a>

                </div>



            </div>
            <!-- End Left sidebar -->


            <!-- Right Sidebar -->
            <div class="email-rightbar mb-3">

                <div class="card">




                    <div class="card-body">
                        <table id="datatable-buttons" class="table table-striped table-bordered dt-responsive nowrap"
                            style="border-collapse: collapse; border-spacing: 0; width: 100%;" data-page-length='80'>
                            <thead>
                                <tr>
                                    <th style="width: 5%;">№</th>
                                    <th>Gram</th>
                                    <th>Ini</th>
                                    <th>Aýyň Başyna</th>
                                    <th>Gelen</th>
                                    <th>Giden</th>
                                    <th>Aýyň Soňuna</th>
                                    <!--  <th>Настройки</th> -->
                                </tr>
                            </thead>
                            <tbody>

                                {% for key, report in reportDatas %}
                                <tr>
                                    <td style="font-weight: bold;">{{key + 1}}</td>
                                    <td><a href="#" style="font-weight: bold;">{{report.gram}}</a></td>
                                    <td>{{report.width}}</td>
                                    <td>{{report.start_month|number_format(2)}}</td>
                                    <td style="color: darkgreen;">{{report.in|number_format(2)}}</td>
                                    <td style="color: darkred;">{{report.out == 0 ? '' : report.out|number_format(2)}}</td>
                                    <td>{{report.end_month|number_format(2)}}</td>
                                </tr>
                                {% endfor %}




                            </tbody>
                            <tfoot>
                                <tr>
                                    <th style="width: 5%;">№</th>
                                    <th>Gram</th>
                                    <th>Ini</th>
                                    <th>Aýyň Başyna</th>
                                    <th>Gelen</th>
                                    <th>Giden</th>
                                    <th>Aýyň Soňuna</th>
                                </tr>
                            </tfoot>
                        </table>

                    </div>

                </div> <!-- card -->

            </div>

        </div>
    </div>
    <!-- End row -->
</div>


{% partial 'dataTableJs' %}
