Menampilkan qty berdasarkan perhitungan dari controller

Saya membuat halaman cetak barcode yang mana 1 halaman menampung 4 barcode, pada inputannya user dapat memasukan qty total dan qty perbarcode misal user memasukan qty total adalah 5 dan ingin membuat pebarcode 2 qty maka sistem akan otomatis membuat 3 barcode, yang masing masing qty-nya adalah 2, 2 dan 1.

sejauh ini jumlah barcode sudah sesuai, masalahnya adalah ada pada qty yang ditampilkan yaitu 2 semua (totalnya 6 dan ini salah), seharusnya barcode terakhir adalah 1.

ini controllernya:

public function cetakBarcode(Request $request, $id)
    {
        $bagians = Bagian::get();
        $data = Order::find($id);

        $master_jenis = MasterJenis::find($data->id_jenis_stone);
        $master_stone = MasterStone::find($data->id_nama_stone);

        $jenis_stone = $master_jenis ? $master_jenis->jenis_stone : '';
        $nama_stone = $master_stone ? $master_stone->nama_stone : '';

        $qty_per_barcode = $data->qty_perbarcode ?? 1;
        $barcode_count = $data->qty;

        $barcode_page = [];
        $barcode_index = 1;

        for ($i = 0; $i < $barcode_count; $i++) {
            $barcode_data = $data->no_po . '.' . $jenis_stone . '.' . $nama_stone . '.' . $barcode_index;

            $barcode_qty = $i == $barcode_count - 1 ? $barcode_count % $qty_per_barcode : $qty_per_barcode;
            $barcodes[] = compact('barcode_data', 'barcode_qty');

            if (($i + 1) % $qty_per_barcode == 0 || $i == $barcode_count - 1) {
                $barcode_page[] = $barcodes;
                $barcodes = [];
                $barcode_index++;
            }
        }

        return view('proses-cutting.cetak-barcode', compact('data', 'bagians', 'barcode_page', 'qty_per_barcode'));
    }

dan ini adlah kode view saya:

<body>
    <div class="my-3">
        <div class="row">
            <div class="col-md-12">
                <div class="barcode-wrapper">
                    @foreach($barcode_page as $barcodes)
                    @if($loop->iteration % 4 == 1)
                            <div class="barcode-section">
                </div>
                @endif
                    <div class="barcode mx-3 my-4">
                        <div class="text-center mb-4">
                            {!! DNS1D::getBarcodeSVG($barcodes[0]['barcode_data'], 'C128', 1, 60) !!}
                        </div>
                        <table>
                            <thead>
                                <th>
                                    No PO
                                </th>
                            </thead>
                            <tbody>
                                <tr>
                                    <td>
                                        {{ $data->no_po }}
                                    </td>
                                </tr>
                            </tbody>
                        </table>
                        <table>
                            <thead>
                                <tr>
                                    <th>Item</th>
                                    <th>Qty</th>
                                    <th>Nama Stone</th>
                                    <th>Jenis Stone</th>
                                    <th>Size</th>
                                </tr>
                            </thead>
                            <tbody>
                                <tr> 
                                    <td>{{ $data->item }}</td>
                                    <td>{{ $qty_per_barcode }}</td>
                                    <td>{{ $data->masterStone->nama_stone }}</td>
                                    <td>{{ $data->masterJenis->jenis_stone }}</td>
                                    <td>{{ $data->size }}</td>
                                </tr>
                            </tbody>
                        </table>
                        <table>
                            <thead>
                                <tr>
                                    <th>Order Date</th>
                                    <th>Out Date</th>
                                    <th>Export Date</th>
                                </tr>
                            </thead>
                            <tbody>
                                <tr>
                                    <td>{{ $data->order_date }}</td>
                                    <td>{{ $data->out_date }}</td>
                                    <td>{{ $data->export_date }}</td>
                                </tr>
                            </tbody>
                        </table>
                        <table>
                            <thead>
                                <tr>
                                    <th>Bagian</th>
                                    <th>In</th>
                                    <th>Out</th>
                                    <th>Loss Rate</th>
                                    <th>Worker</th>
                                </tr>
                            </thead>
                            <tbody>
                                <tr>
                                    <td>Cutting</td>
                                    <td></td>
                                    <td></td>
                                    <td></td>
                                    <td></td>
                                </tr>
                                <tr>
                                    <td>Siap</td>
                                    <td></td>
                                    <td></td>
                                    <td></td>
                                    <td></td>
                                </tr>
                                <tr>
                                    <td>CNC PC</td>
                                    <td></td>
                                    <td></td>
                                    <td></td>
                                    <td></td>
                                </tr>
                                <tr>
                                    <td>CNC Mesin</td>
                                    <td></td>
                                    <td></td>
                                    <td></td>
                                    <td></td>
                                </tr>
                                <tr>
                                    <td>Polishing</td>
                                    <td></td>
                                    <td></td>
                                    <td></td>
                                    <td></td>
                                </tr>
                                <tr>
                                    <td>QC</td>
                                    <td></td>
                                    <td></td>
                                    <td></td>
                                    <td></td>
                                </tr>
                            </tbody>
                        </table>
                    </div>
                    @endforeach
                </div>
            </div>
        </div>
    </div>
</body>

dan untuk tambahan informasi saya memasukan inputan qty perbarcode ke kolom dalam database, misal user memasukan qty perbarcode adalah 2 maka semua akan tercetak 2?

tolong berikan pencerahan, saya sudah stuck sekali :(

avatar yuxxeun
@yuxxeun

91 Kontribusi 48 Poin

Diperbarui 3 hari yang lalu

1 Jawaban:

coba buat kek gini di bagian looping-nya mas.

for ($i = 0; $i < $barcode_count; $i++) {
    $barcode_data = $data->no_po . '.' . $jenis_stone . '.' . $nama_stone . '.' . $barcode_index;

    // Hitung kuantitas barcode untuk barcode saat ini
    $barcode_qty = $i < $barcode_count % $qty_per_barcode ? ceil($barcode_count / $qty_per_barcode) : floor($barcode_count / $qty_per_barcode);

    $barcodes[] = compact('barcode_data', 'barcode_qty');

    // Jika sudah mencapai kuantitas perbarcode atau mencapai akhir jumlah barcode, tambahkan ke halaman barcode
    if (($i + 1) % $qty_per_barcode == 0 || $i == $barcode_count - 1) {
        $barcode_page[] = $barcodes;
        $barcodes = [];
        $barcode_index++;
    }
}

aku pakai ceil() untuk memastikan kuantitas barcode terakhir akan mencakup sisa barcode yang tersisa. Jumlah barcode yang dihasilkan per loop adalah $qty_per_barcode, kecuali untuk sisa jumlah barcode terakhir, di mana jumlahnya disesuaikan.

avatar wayosu
@wayosu

1 Kontribusi 0 Poin

Dipost 3 hari yang lalu

Login untuk ikut Jawaban