title = "actions/bag-report"
url = "/actions/bag-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;
    }

    $reportDatas = [];
    //dd($reportDatas);

    if($year){

        $bagProds = Romanah\Gokbakja\Models\SewerProduction::select('width', 'height', 'bag_gram', 'color_id')
            ->with('color')
            ->whereMonth('date', $this["month"])
            ->whereYear('date', $year)
            ->groupBy('width')
            ->groupBy('height')
            ->groupBy('bag_gram')
            ->groupBy('color_id')
            ->orderBy('width', 'ASC')
            ->distinct()
            ->get();


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

                $action = Romanah\Gokbakja\Models\BagAction::select('romanah_gokbakja_bag_actions.*')
                        ->with('product')
                        ->whereHas('product', function($qq) use($bagProds, $x, $year){
                            $qq->whereMonth('date', $this["month"])->whereYear('date', $year)->where('width', $bagProds[$x]->width)->where('height', $bagProds[$x]->height)->where('bag_gram', $bagProds[$x]->bag_gram)->where('color_id', $bagProds[$x]->color_id);
                        })
                        ->sum('amount');

                $actionNow = Romanah\Gokbakja\Models\BagAction::select('romanah_gokbakja_bag_actions.*')
                        ->with('product')
                        ->whereHas('product', function($qq) use($bagProds, $x, $year){
                            $qq->where('width', $bagProds[$x]->width)->where('height', $bagProds[$x]->height)->where('bag_gram', $bagProds[$x]->bag_gram)->where('color_id', $bagProds[$x]->color_id);
                        })
                        ->sum('amount');


                $actionBraks = Romanah\Gokbakja\Models\BagAction::select('romanah_gokbakja_bag_actions.*')
                        ->with('product.color')
                        ->whereHas('product', function($qq) use($bagProds, $x, $year){
                            $qq->whereMonth('date', $this["month"])->whereYear('date', $year)->where('width', $bagProds[$x]->width)->where('height', $bagProds[$x]->height)->where('bag_gram', $bagProds[$x]->bag_gram)->where('color_id', $bagProds[$x]->color_id);
                        })
                        ->get();

                $calculateBrak = 0;
                $calculateBrakRepair = 0;

                for ($xx = 0; $xx < count($actionBraks); $xx++) {
                    $calculateBrak += $actionBraks[$xx]->product->defective_bag_qty;
                    $calculateBrakRepair += $actionBraks[$xx]->product->defective_repair_qty;
                }

                $reportDatas[$x]["width"] = $bagProds[$x]->width;
                $reportDatas[$x]["height"] = $bagProds[$x]->height;
                $reportDatas[$x]["color"] = $bagProds[$x]->color->name ?? "";
                $reportDatas[$x]["bag_gram"] = $bagProds[$x]->bag_gram ?? 0;
                $reportDatas[$x]["amount"] = $action;
                $reportDatas[$x]["defective"] = $calculateBrak;
                $reportDatas[$x]["defectiveRepair"] = $calculateBrakRepair;
                $reportDatas[$x]["all"] = $action + $calculateBrakRepair + $calculateBrakRepair;
                $reportDatas[$x]["amountNow"] = $actionNow;

            }

    }else{

        $bagProds = Romanah\Gokbakja\Models\SewerProduction::select('width', 'height', 'bag_gram', 'color_id')
                ->with('color')
                ->whereMonth('date', $this["month"])
                ->whereYear('date', $currentDate->year)
                ->groupBy('width')
                ->groupBy('height')
                ->groupBy('bag_gram')
                ->groupBy('color_id')
                ->orderBy('width', 'ASC')
                ->distinct()
                ->get();


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

                    $action = Romanah\Gokbakja\Models\BagAction::select('romanah_gokbakja_bag_actions.*')
                            ->with('product')
                            ->whereHas('product', function($qq) use($bagProds, $x, $currentDate){
                                $qq->whereMonth('date', $this["month"])->whereYear('date', $currentDate->year)->where('width', $bagProds[$x]->width)->where('height', $bagProds[$x]->height)->where('bag_gram', $bagProds[$x]->bag_gram)->where('color_id', $bagProds[$x]->color_id);
                            })
                            ->sum('amount');


                    $actionNow = Romanah\Gokbakja\Models\BagAction::select('romanah_gokbakja_bag_actions.*')
                            ->with('product')
                            ->whereHas('product', function($qq) use($bagProds, $x, $currentDate){
                                $qq->where('width', $bagProds[$x]->width)->where('height', $bagProds[$x]->height)->where('bag_gram', $bagProds[$x]->bag_gram)->where('color_id', $bagProds[$x]->color_id);
                            })
                            ->sum('amount');

                    $actionBraks = Romanah\Gokbakja\Models\BagAction::select('romanah_gokbakja_bag_actions.*')
                            ->with('product')
                            ->whereHas('product', function($qq) use($bagProds, $x, $currentDate){
                                $qq->whereMonth('date', $this["month"])->whereYear('date', $currentDate->year)->where('width', $bagProds[$x]->width)->where('height', $bagProds[$x]->height)->where('bag_gram', $bagProds[$x]->bag_gram)->where('color_id', $bagProds[$x]->color_id);
                            })
                            ->get();

                    $calculateBrak = 0;
                    $calculateBrakRepair = 0;

                    for ($xx = 0; $xx < count($actionBraks); $xx++) {
                        $calculateBrak += $actionBraks[$xx]->product->defective_bag_qty;
                        $calculateBrakRepair += $actionBraks[$xx]->product->defective_repair_qty;
                    }

                    $reportDatas[$x]["width"] = $bagProds[$x]->width;
                    $reportDatas[$x]["height"] = $bagProds[$x]->height;
                    $reportDatas[$x]["color"] = $bagProds[$x]->color->name ?? "";
                    $reportDatas[$x]["bag_gram"] = $bagProds[$x]->bag_gram ?? 0;
                    $reportDatas[$x]["amount"] = $action;
                    $reportDatas[$x]["defective"] = $calculateBrak;
                    $reportDatas[$x]["defectiveRepair"] = $calculateBrakRepair;
                    $reportDatas[$x]["all"] = ($action + $calculateBrakRepair + $calculateBrak);
                    $reportDatas[$x]["amountNow"] = $actionNow;

                }

    }
   // dd($reportDatas);
    $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/bag-report/1" class="{{ month == 1 ? 'active' : '' }}"><i
                            class="mdi mdi-archive me-2 font-size-16"></i> Ýanwar</a>
                    <a href="/actions/bag-report/2" class="{{ month == 2 ? 'active' : '' }}"><i
                            class="mdi mdi-archive me-2 font-size-16"></i> Fewral</a>
                    <a href="/actions/bag-report/3" class="{{ month == 3 ? 'active' : '' }}"><i
                            class="mdi mdi-archive me-2 font-size-16"></i> Mart</a>
                    <a href="/actions/bag-report/4" class="{{ month == 4 ? 'active' : '' }}"><i
                            class="mdi mdi-archive me-2 font-size-16"></i> Aprel</a>
                    <a href="/actions/bag-report/5" class="{{ month == 5 ? 'active' : '' }}"><i
                            class="mdi mdi-archive me-2 font-size-16"></i> Maý</a>
                    <a href="/actions/bag-report/6" class="{{ month == 6 ? 'active' : '' }}"><i
                            class="mdi mdi-archive me-2 font-size-16"></i> Iýun</a>
                    <a href="/actions/bag-report/7" class="{{ month == 7 ? 'active' : '' }}"><i
                            class="mdi mdi-archive me-2 font-size-16"></i> Iýul</a>
                    <a href="/actions/bag-report/8" class="{{ month == 8 ? 'active' : '' }}"><i
                            class="mdi mdi-archive me-2 font-size-16"></i> Awgust</a>
                    <a href="/actions/bag-report/9" class="{{ month == 9 ? 'active' : '' }}"><i
                            class="mdi mdi-archive me-2 font-size-16"></i> Sentýabr</a>
                    <a href="/actions/bag-report/10" class="{{ month == 10 ? 'active' : '' }}"><i
                            class="mdi mdi-archive me-2 font-size-16"></i> Oktýabr</a>
                    <a href="/actions/bag-report/11" class="{{ month == 11 ? 'active' : '' }}"><i
                            class="mdi mdi-archive me-2 font-size-16"></i> Noýabr</a>
                    <a href="/actions/bag-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>Boýy</th>
                                    <th>Reňki</th>
                                    <th>Öndürilen Haltasy (arassa)</th>
                                    <th>Öndürilen Haltasy (brak)</th>
                                    <th>Sürülmedik haltalar</th>
                                    <th>Jemi öndüren haltasy</th>
                                    <th>Şuwagtky Ýagdaýy</th>
                                    <!--  <th>Настройки</th> -->
                                </tr>
                            </thead>
                            <tbody>
                                {% set allCalc = 0 %}
                                {% set allCalcDefect = 0 %}
                                {% set allCalcDefectRep = 0 %}
                                {% set allAmountSum = 0 %}

                                {% for key, report in reportDatas %}

                                {% set allCalc = (allCalc + report.amount) %}
                                {% set allCalcDefect = (allCalcDefect + report.defective) %}
                                {% set allCalcDefectRep = (allCalcDefectRep + report.defectiveRepair) %}
                                {% set allAmountSum = (allAmountSum + report.all) %}

                                <tr>
                                    <td style="font-weight: bold;">{{key + 1}}</td>
                                    <td><a href="#" style="font-weight: bold;">{{report.bag_gram}}</a></td>
                                    <td>{{report.width}}</td>
                                    <td>{{report.height}}</td>
                                    <td>{{report.color}}</td>
                                    <td><span class="badge badge-soft-success"
                                        style="font-size: 14px;">{{report.amount}}</span></td>
                                    <td><span class="badge badge-soft-danger"
                                        style="font-size: 14px;">{{report.defective}}</span></td>
                                    <td><span class="badge badge-soft-warning"
                                        style="font-size: 14px;">{{report.defectiveRepair}}</span></td>
                                    <td><span class="badge badge-soft-primary"
                                        style="font-size: 14px;">{{report.all}}</span></td>
                                    <td><span class="badge badge-soft-info"
                                        style="font-size: 14px;">{{report.amountNow}}</span></td>
                                </tr>

                                {% endfor %}

                            </tbody>
                            <tfoot>
                                <tr>
                                    <th colspan="5" style="width: 5%;">Jemi</th>

                                    <th>{{allCalc|number_format(2)}}</th>
                                    <th>{{allCalcDefect|number_format(2)}}</th>
                                    <th>{{allCalcDefectRep|number_format(2)}}</th>
                                    <th>{{allAmountSum|number_format(2)}}</th>
                                    <th></th>
                                </tr>
                            </tfoot>
                        </table>

                    </div>

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

            </div>

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


{% partial 'dataTableJs' %}
