@php // Define the maximum number of lines per page $maxLines = 14; $allDetails = $salesReturn->details; $pages = []; $currentPageItems = []; $currentLines = 0; $currentPageSubtotal = 0; foreach ($allDetails as $detail) { $maxLength = 46; $itemName = $detail->items->item_name; $prefix = '| BUTTERFLY |'; // Check if the item name starts with the prefix if (str_starts_with($itemName, $prefix)) { // Remove the prefix if it exists $itemName = substr($itemName, strlen($prefix)); // Optional: Trim any leading/trailing whitespace left after removal $itemName = trim($itemName); } $linesNeeded = count(explode("\n", wordwrap($itemName, $maxLength, "\n", false))); if (($currentLines + $linesNeeded) > $maxLines) { $pages[] = [ 'items' => $currentPageItems, 'subtotal' => $currentPageSubtotal ]; $currentPageItems = []; $currentLines = 0; $currentPageSubtotal = 0; } $currentPageItems[] = $detail; $currentLines += $linesNeeded; $currentPageSubtotal += ($detail->qty*$detail->price)-($detail->qty*$detail->price*$detail->disc_percent/100); } if (!empty($currentPageItems)) { $pages[] = [ 'items' => $currentPageItems, 'subtotal' => $currentPageSubtotal ]; } $name=''; $name=$salesReturn->logo==true?'UD.OLIVIA':'SERBAJAYA'; $totalPages = count($pages); @endphp @foreach($pages as $pageNumber => $page)


  {{ str_pad($name, 35) }} {{$salesReturn->customers->customer_name ?? 'N/A'}}
  {{ str_pad('SURABAYA', 45) }} {{$salesReturn->customers->address ?? 'N/A'}}
  {{ str_pad('', 45) }} {{$salesReturn->customers->city ?? 'N/A'}}
  {{ str_pad('', 45) }} TGL RETUR  : {{ \Carbon\Carbon::parse($salesReturn->document_date)->format('d-m-Y') }}

  RETUR PENJUALAN                               {{ substr($salesReturn->sales_return_number,7) }}
 —————————————————————————————————————————————————————————————————————————————————————————————————
 |NO |                   NAMA BARANG                |   QTY| UNIT|    HARGA |DISC %|     SUBTOTAL|
 —————————————————————————————————————————————————————————————————————————————————————————————————
@php
     $count = 0;
     $startingIndex = 0;
     for ($i = 0; $i < $pageNumber; $i++) {
         $startingIndex += count($pages[$i]['items']);
     }
@endphp
@foreach($page['items'] as $index => $detail)
@php
     $maxLength = 46;
     $itemName = $detail->items->item_name;
     $prefix = '| BUTTERFLY |';
     // Check if the item name starts with the prefix
     if (str_starts_with($itemName, $prefix)) {
         // Remove the prefix if it exists
         $itemName = substr($itemName, strlen($prefix));
         // Optional: Trim any leading/trailing whitespace left after removal
         $itemName = trim($itemName);
     }
     $lines = explode("\n", wordwrap($itemName, $maxLength, "\n", false));
     $qty = $detail->qty;
     if($qty!=round($qty)){
        $qty = number_format($detail->qty, 1, ',', '.');
     }else{
        $qty = number_format($detail->qty, 0, ',', '.');
     }
@endphp
@foreach($lines as $lineIndex => $line)
@if($lineIndex == 0)
 |{{ str_pad($startingIndex + $loop->parent->iteration, 3) }}|{{ str_pad($line, 46) }}|{{ str_pad($qty, 6, ' ', STR_PAD_LEFT) }}|{{ str_pad($detail->unit, 5, ' ', STR_PAD_LEFT) }}|{{ str_pad(number_format($detail->price, 0, ',', '.'), 10, ' ', STR_PAD_LEFT) }}|{{ str_pad(number_format($detail->disc_percent, 0, ',', '.'), 6, ' ', STR_PAD_LEFT) }}|{{ str_pad(number_format(($detail->qty*$detail->price)-($detail->qty*$detail->price*$detail->disc_percent/100), 0, ',', '.'), 13, ' ', STR_PAD_LEFT) }}|
@else
 |{{ str_pad('', 3) }}|{{ str_pad($line, 46) }}|{{ str_pad('', 6) }}|{{ str_pad('', 5) }}|{{ str_pad('', 10) }}|{{ str_pad('', 6) }}|{{ str_pad('', 13) }}|
@endif
@php
     $count++;
@endphp
@endforeach
@endforeach
@for($i = 0; $i < ($maxLines - $count); $i++)
 |{{ str_pad('', 3) }}|{{ str_pad('', 46) }}|{{ str_pad('', 6) }}|{{ str_pad('', 5) }}|{{ str_pad('', 10) }}|{{ str_pad('', 6) }}|{{ str_pad('', 13) }}|
@endfor
 —————————————————————————————————————————————————————————————————————————————————————————————————
 |                                                               |SUBTOTAL          {{ str_pad(number_format($page['subtotal'], 0, ',', '.'), 13, ' ', STR_PAD_LEFT) }}|
 —————————————————————————————————————————————————————————————————————————————————————————————————
  {{ str_pad('', 45) }}                  |TOTAL             {{ str_pad(number_format($salesReturn->subtotal, 0, ',', '.'), 13, ' ', STR_PAD_LEFT) }}|
@if($salesReturn->disc_nominal>0||$salesReturn->disc_percent==0)
  {{ str_pad('', 45) }}                  |DISCOUNT          {{ str_pad(number_format($salesReturn->disc_nominal, 0, ',', '.'), 13, ' ', STR_PAD_LEFT) }}|
@endif
@if($salesReturn->disc_percent>0)
  {{ str_pad('', 45) }}                  |DISCOUNT {{str_pad(('('.number_format($salesReturn->disc_percent,1).' %)'), 8, ' ', STR_PAD_LEFT)}} {{ str_pad(number_format(($salesReturn->subtotal*$salesReturn->disc_percent/100), 0, ',', '.'), 13, ' ', STR_PAD_LEFT) }}|
@endif
  {{ str_pad('', 45) }}                  |GRAND TOTAL       {{ str_pad(number_format($salesReturn->total, 0, ',', '.'), 13, ' ', STR_PAD_LEFT) }}|
                                                                 —————————————————————————————————
                                                                                {{ ($pageNumber + 1) }} of {{ $totalPages }}
        
@endforeach Kembali