datatables yang tidak sesuai dengan database

jadi saya mengalami nominal data kuantiti yang tidak sesuai dengan database yang ada pada bagian tabel pergerakan stock, jadi pada database memiliki nominal nol(0) sedangkan di view tablenya menjadi -1

pergerakan controller php

<?php

namespace App\Http\Controllers;

use App\Exports\PergerakanStockExport;
use App\Kategori;
use App\Produk;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Maatwebsite\Excel\Excel;

class PergerakanStockController extends Controller
{
    public function index()
    {
        $kategori = null;
        $summary = $this->getSummary();

        if (request('kategori_id')) {
            $kategori = Kategori::select(['kategori.id', DB::raw("CONCAT(kategori.nama, ' - ', brand.nama) as nama")])
                ->join('brand', 'kategori.brand_id', '=', 'brand.id')
                ->where('kategori.id', request('kategori_id'))
                ->first();
        }

        return view('pergerakan-stock.index', compact('kategori', 'summary'));
    }

    public function export()
    {
        return (new PergerakanStockExport())->download('pergerakan-stock.xlsx', Excel::XLSX);
    }

    protected function getQuery()
    {
        $start = request('from');
        $end = request('to');

        $query = Produk::selectRaw("
        produk.id,
        IFNULL((
            SELECT SUM(persediaan.kuantitas)
            FROM persediaan
            WHERE persediaan.produk_id = produk.id
            AND DATE(persediaan.created_at) < '{$start}'
            GROUP BY persediaan.produk_id
            LIMIT 1
        ) + (
            SELECT SUM(retur.kuantitas)
            FROM retur
            WHERE retur.produk_id = produk.id
            AND DATE(retur.created_at) < '{$start}'
            GROUP BY retur.produk_id
            LIMIT 1
        ) - (
            SELECT SUM(pengeluaran.kuantitas)
            FROM pengeluaran
            WHERE pengeluaran.produk_id = produk.id
            AND DATE(pengeluaran.created_at) < '{$start}'
            GROUP BY pengeluaran.produk_id
            LIMIT 1
        ) - (
            SELECT SUM(detail_pesanan.kuantitas)
            FROM detail_pesanan
            JOIN pesanan ON detail_pesanan.pesanan_id = pesanan.id
            WHERE detail_pesanan.produk_id = produk.id
            AND status_pesanan_id IN (3, 4, 5)
            AND DATE(pesanan.created_at) < '{$start}'
            GROUP BY detail_pesanan.produk_id
            LIMIT 1
        ), 0) as stock_awal,
        produk.harga_jual * (SELECT stock_awal) as nilai_stock_awal,
        IFNULL((
            SELECT SUM(persediaan.kuantitas)
            FROM persediaan
            WHERE persediaan.produk_id = produk.id
            AND DATE(persediaan.created_at) BETWEEN '{$start}' AND '{$end}'
            GROUP BY persediaan.produk_id
            LIMIT 1
        ), 0) as stock_persediaan,
        produk.harga_jual * (SELECT stock_persediaan) as nilai_stock_persediaan,
        IFNULL((
            SELECT SUM(retur.kuantitas)
            FROM retur
            WHERE retur.produk_id = produk.id
            AND DATE(retur.created_at) BETWEEN '{$start}' AND '{$end}'
            GROUP BY retur.produk_id
            LIMIT 1
        ), 0) as stock_retur,
        produk.harga_jual * (SELECT stock_retur) as nilai_stock_retur,
        IFNULL((
            SELECT SUM(pengeluaran.kuantitas)
            FROM pengeluaran
            WHERE pengeluaran.produk_id = produk.id
            AND DATE(pengeluaran.created_at) BETWEEN '{$start}' AND '{$end}'
            GROUP BY pengeluaran.produk_id
            LIMIT 1
        ), 0) as stock_pengeluaran,
        produk.harga_jual * (SELECT stock_pengeluaran) as nilai_stock_pengeluaran,
        IFNULL((
            SELECT SUM(detail_pesanan.kuantitas)
            FROM detail_pesanan
            JOIN pesanan ON detail_pesanan.pesanan_id = pesanan.id
            WHERE detail_pesanan.produk_id = produk.id
            AND status_pesanan_id IN (3, 4, 5)
            AND DATE(pesanan.created_at) BETWEEN '{$start}' AND '{$end}'
            GROUP BY detail_pesanan.produk_id
            LIMIT 1
        ), 0) as stock_pesanan,
        produk.harga_jual * (SELECT stock_pesanan) as nilai_stock_pesanan,
        (SELECT stock_awal) + (SELECT stock_persediaan) + (SELECT stock_retur) - (SELECT stock_pengeluaran) - (SELECT stock_pesanan) as stock_akhir,
        produk.harga_jual * (SELECT stock_akhir) as nilai_stock_akhir
    ")
            ->join('produk_umum', 'produk.produk_umum_id', 'produk_umum.id')
            ->join('kategori', 'produk_umum.kategori_id', 'kategori.id')
            ->join('warna', 'produk.warna_id', 'warna.id')
            ->join('ukuran', 'produk.ukuran_id', 'ukuran.id')
            ->join('grade', 'produk.grade_id', 'grade.id');

        if (request()->filled('kategori_id')) {
            $query->whereRaw('produk_umum.kategori_id = ' . request('kategori_id'));
        }

        $sql = $query->toSql();

        return preg_replace('/\s+/', ' ', $sql);
    }

    protected function getSummary()
    {
        return DB::select("
            SELECT
                SUM(stock_awal) as stock_awal,
                SUM(nilai_stock_awal) as nilai_stock_awal,
                SUM(stock_persediaan) as stock_persediaan,
                SUM(nilai_stock_persediaan) as nilai_stock_persediaan,
                SUM(stock_retur) as stock_retur,
                SUM(nilai_stock_retur) as nilai_stock_retur,
                SUM(stock_pengeluaran) as stock_pengeluaran,
                SUM(nilai_stock_pengeluaran) as nilai_stock_pengeluaran,
                SUM(stock_pesanan) as stock_pesanan,
                SUM(nilai_stock_pesanan) as nilai_stock_pesanan,
                SUM(stock_akhir) as stock_akhir,
                SUM(nilai_stock_akhir) as nilai_stock_akhir
            FROM ({$this->getQuery()}) as summary
        ")[0];
    }
}

berikut koding view index

@extends('components.layout')

@section('header')
    @include('components.content-header', ['props'=> ['title' => 'Laporan Pergerakan Stock']])
@endsection

@section('content')
<div class="card">
    <div class="card-header">Filter</div>
    <div class="card-body">
        <form id="form-filter">
            <div class="form-row">
                <div class="form-group col-md">
                    <label for="from">Dari</label>
                    <input type="date" name="from" id="from" class="form-control form-control-custom" value="{{ request('from') }}">
                </div>
                <div class="form-group col-md">
                    <label for="to">Sampai</label>
                    <input type="date" name="to" id="to" class="form-control form-control-custom"  value="{{ request('to') }}">
                </div>
                <div class="form-group col-md">
                    <label for="kategori_id">Kategori</label>
                    <select name="kategori_id" id="kategori_id" class="form-control form-control-custom select2" data-url="{{ route('api.select2.kategori') }}" style="width: 100%">
                        <option></option>
                        @if (request('kategori_id'))
                            <option value="{{ $kategori->id }}" selected>{{ $kategori->nama }}</option>
                        @endif
                    </select>
                </div>
            </div>
            <div class="form-group">
                <button type="submit" class="btn btn-custom-primary">Filter</button>
                <a href="{{ route('laporan.pergerakan-stock.index', ['from' => date('Y-m-01'), 'to' => date('Y-m-t')]) }}" class="btn btn-custom-primary">Reset</a>
                <a href="{{ route('laporan.pergerakan-stock.export', ['from' => request('from'), 'to' => request('to'), 'kategori_id' => request('kategori_id')]) }}" class="btn btn-custom-primary btn-export">Export</a>
            </div>
        </form>
    </div>
</div>

<div class="row">
    <div class="col-6 col-md-3 col-lg-2">
        <!-- Info Boxes Style 2 -->
        <div class="info-box mb-3">
            <div class="info-box-content">
                <span class="info-box-text">Stock Awal</span>
                <span class="info-box-number text-right">{{ Formatter::IDRCurrency($summary->stock_awal) }} pcs</span>
                <span class="info-box-number text-right">Rp {{ Formatter::IDRCurrency($summary->nilai_stock_awal) }}</span>
            </div>
            <!-- /.info-box-content -->
        </div>
        <!-- /.info-box -->
    </div>
    <div class="col-6 col-md-3 col-lg-2">
        <!-- Info Boxes Style 2 -->
        <div class="info-box mb-3">
            <div class="info-box-content">
                <span class="info-box-text">Persediaan</span>
                <span class="info-box-number text-right">{{ Formatter::IDRCurrency($summary->stock_persediaan) }} pcs</span>
                <span class="info-box-number text-right">Rp {{ Formatter::IDRCurrency($summary->nilai_stock_persediaan) }}</span>
            </div>
            <!-- /.info-box-content -->
        </div>
        <!-- /.info-box -->
    </div>
    <div class="col-6 col-md-3 col-lg-2">
        <!-- Info Boxes Style 2 -->
        <div class="info-box mb-3">
            <div class="info-box-content">
                <span class="info-box-text">Retur</span>
                <span class="info-box-number text-right">{{ Formatter::IDRCurrency($summary->stock_retur) }} pcs</span>
                <span class="info-box-number text-right">Rp {{ Formatter::IDRCurrency($summary->nilai_stock_retur) }}</span>
            </div>
            <!-- /.info-box-content -->
        </div>
        <!-- /.info-box -->
    </div>
    <div class="col-6 col-md-3 col-lg-2">
        <!-- Info Boxes Style 2 -->
        <div class="info-box mb-3">
            <div class="info-box-content">
                <span class="info-box-text">Pengeluaran</span>
                <span class="info-box-number text-right">{{ Formatter::IDRCurrency($summary->stock_pengeluaran) }} pcs</span>
                <span class="info-box-number text-right">Rp {{ Formatter::IDRCurrency($summary->nilai_stock_pengeluaran) }}</span>
            </div>
            <!-- /.info-box-content -->
        </div>
        <!-- /.info-box -->
    </div>
    <div class="col-6 col-md-3 col-lg-2">
        <!-- Info Boxes Style 2 -->
        <div class="info-box mb-3">
            <div class="info-box-content">
                <span class="info-box-text">Pesanan</span>
                <span class="info-box-number text-right">{{ Formatter::IDRCurrency($summary->stock_pesanan) }} pcs</span>
                <span class="info-box-number text-right">Rp {{ Formatter::IDRCurrency($summary->nilai_stock_pesanan) }}</span>
            </div>
            <!-- /.info-box-content -->
        </div>
        <!-- /.info-box -->
    </div>
    <div class="col-6 col-md-3 col-lg-2">
        <!-- Info Boxes Style 2 -->
        <div class="info-box mb-3">
            <div class="info-box-content">
                <span class="info-box-text">Stock Akhir</span>
                <span class="info-box-number text-right">{{ Formatter::IDRCurrency($summary->stock_akhir) }} pcs</span>
                <span class="info-box-number text-right">Rp {{ Formatter::IDRCurrency($summary->nilai_stock_akhir) }}</span>
            </div>
            <!-- /.info-box-content -->
        </div>
        <!-- /.info-box -->
    </div>
</div>

<div class="card">
    <div class="card-body">
        <table class="table table-hover text-nowrap datatable" style="width: 100%">
            <thead class="bg-custom-primary">
                <tr>
                    <th rowspan="2" width="10">No</th>
                    <th rowspan="2">Produk</th>
                    <th colspan="2" class="text-center">Stock Awal</th>
                    <th colspan="2" class="text-center">Stock Persediaan</th>
                    <th colspan="2" class="text-center">Stock Retur</th>
                    <th colspan="2" class="text-center">Stock Pengeluaran</th>
                    <th colspan="2" class="text-center">Stock Pesanan</th>
                    <th colspan="2" class="text-center">Stock Akhir</th>
                </tr>
                <tr>
                    <th>Kuantitas</th>
                    <th>Nominal</th>
                    <th>Kuantitas</th>
                    <th>Nominal</th>
                    <th>Kuantitas</th>
                    <th>Nominal</th>
                    <th>Kuantitas</th>
                    <th>Nominal</th>
                    <th>Kuantitas</th>
                    <th>Nominal</th>
                    <th>Kuantitas</th>
                    <th>Nominal</th>
                </tr>
            </thead>
        </table>
    </div>
</div>
@endsection

@push('scripts')
<script>
    $(function() {
        var dtUrl = "{!! route('api.datatables.pergerakan-stock', ['from' => request('from'), 'to' => request('to'), 'kategori_id' => request('kategori_id')]) !!}";

        var table = $('.datatable').DataTable({
            processing: true,
            serverSide: true,
            lengthChange: false,
            ajax: dtUrl,
            columns: [
                {
                    data: 'DT_RowIndex',
                    searchable: false,
                    orderable: false,
                },
                {
                    data: 'produk',
                    name: 'produk',
                },
                {
                    data: 'stock_awal',
                    name: 'stock_awal',
                    searchable: false,
                    className: 'text-right',
                    render: function (data, type, row, meta) {
                        return parseInt(data).toLocaleString('id');
                    },
                },
                {
                    data: 'nilai_stock_awal',
                    name: 'nilai_stock_awal',
                    searchable: false,
                    className: 'text-right',
                    render: function (data, type, row, meta) {
                        return parseInt(data).toLocaleString('id');
                    },
                },
                {
                    data: 'stock_persediaan',
                    name: 'stock_persediaan',
                    searchable: false,
                    className: 'text-right',
                    render: function (data, type, row, meta) {
                        return parseInt(data).toLocaleString('id');
                    },
                },
                {
                    data: 'nilai_stock_persediaan',
                    name: 'nilai_stock_persediaan',
                    searchable: false,
                    className: 'text-right',
                    render: function (data, type, row, meta) {
                        return parseInt(data).toLocaleString('id');
                    },
                },
                {
                    data: 'stock_retur',
                    name: 'stock_retur',
                    searchable: false,
                    className: 'text-right',
                    render: function (data, type, row, meta) {
                        return parseInt(data).toLocaleString('id');
                    },
                },
                {
                    data: 'nilai_stock_retur',
                    name: 'nilai_stock_retur',
                    searchable: false,
                    className: 'text-right',
                    render: function (data, type, row, meta) {
                        return parseInt(data).toLocaleString('id');
                    },
                },
                {
                    data: 'stock_pengeluaran',
                    name: 'stock_pengeluaran',
                    searchable: false,
                    className: 'text-right',
                    render: function (data, type, row, meta) {
                        return parseInt(data).toLocaleString('id');
                    },
                },
                {
                    data: 'nilai_stock_pengeluaran',
                    name: 'nilai_stock_pengeluaran',
                    searchable: false,
                    className: 'text-right',
                    render: function (data, type, row, meta) {
                        return parseInt(data).toLocaleString('id');
                    },
                },
                {
                    data: 'stock_pesanan',
                    name: 'stock_pesanan',
                    searchable: false,
                    className: 'text-right',
                    render: function (data, type, row, meta) {
                        return parseInt(data).toLocaleString('id');
                    },
                },
                {
                    data: 'nilai_stock_pesanan',
                    name: 'nilai_stock_pesanan',
                    searchable: false,
                    className: 'text-right',
                    render: function (data, type, row, meta) {
                        return parseInt(data).toLocaleString('id');
                    },
                },
                {
                    data: 'stock_akhir',
                    name: 'stock_akhir',
                    searchable: false,
                    className: 'text-right',
                    render: function (data, type, row, meta) {
                        return parseInt(data).toLocaleString('id');
                    },
                },
                {
                    data: 'nilai_stock_akhir',
                    name: 'nilai_stock_akhir',
                    searchable: false,
                    className: 'text-right',
                    render: function (data, type, row, meta) {
                        return parseInt(data).toLocaleString('id');
                    },
                },
            ],
            order: [[1, 'asc']]
        });
    });
</script>
@endpush
avatar Ja9090
@Ja9090

3 Kontribusi 0 Poin

Dipost 2 tahun yang lalu

Belum ada Jawaban. Jadi yang pertama Jawaban

Login untuk ikut Jawaban