<?php
// === HEADER ===
$date = new DateTime($this->data->kasir->tgl);
$tanggal = $date->format('dmy').str_pad($this->data->kasir->no_order, 3, "0", STR_PAD_LEFT).' (' . $date->format('H.i').')';
?>

<!--<h4><b><?php echo strtoupper($this->login['bill_hed'] ?? 'ULEG CABE') ?></b></h4>-->

<table>
	<tr>
		<td style="text-align:center;" colspan="2">
			<img src="<?php echo $this->baseUrl ?>assets/images/LogoBill.bmp" style="width:250px;">
		</td>
	</tr>
	<?php if($this->data->mode != "bill"){ ?>
		<tr><td colspan="2"><?php echo $tanggal; ?></td></tr>
    <?php } ?>
    <tr>
		<td colspan="2">
			<h4 style="border:0;">
				<b>
				<?php if ($this->data->meja->nm_kat == "TAKE AWAY") echo "TAKE AWAY";
				else echo "DINE-IN"; ?> - 
				Meja <?php echo $this->data->meja->nm_merek; ?>
				</b>
			</h4>
		</td>
	</tr>
	<tr><td colspan="2"><?php echo $this->data->meja->nm_kat; ?></td></tr>
	<?php if ($this->data->meja->nm_kat == "TAKE AWAY") { ?>
	<tr><td>PEMBELI</td><td>: <?php echo $this->data->kasir->name; ?></td></tr>
	<?php } ?>
	<tr><td>PETUGAS</td><td>: <?php echo $this->data->kasir->nm_kasir; ?></td></tr>
	<tr><td>NO ORDER</td><td>: <?php echo $this->data->kasir->no_pos; ?></td></tr>
</table>
<hr>
<div class="section-title">
<?php 
	if ($this->data->mode == "bar") echo "BAR";
	else if ($this->data->mode == "dapur") echo "DAPUR";
	else if ($this->data->mode == "checker") echo "SLIP ORDER<br>(CHECKER)";
	else echo "SLIP BILL";
?>
</div>
<hr>
<?php
// === ITEMS ===
$grup = array();
$total = 0;
$totQty = 0;

foreach($this->data->item as $r){
	$grup[$r->nm_app][] = $r;
	$total += $r->total;
}
?>

<?php foreach($grup as $row){ ?>
	<?php foreach($row as $r){ ?>
		<div class="item">
			<?php if($this->data->mode == "bill") { ?>
				<div class="item-name"><?php echo htmlspecialchars($r->nm_inv); ?></div>
				<?php if(!empty($this->data->pak[$r->kd_inv]->kd_inv)){ ?>
    				<div class="item-sub">
    					<?php foreach($this->data->pak[$r->kd_inv]->invd as $i){ ?>
    					    - <?php echo htmlspecialchars($i->nm_invd); ?> x <?php echo $i->qty; ?><br>
    					<?php } ?>
    				</div>
				<?php } ?>
				<div class="item-sub">
					<?php echo $r->qty; ?> x <?php echo number_format($r->harga); ?>
					<span class="text-right" style="float:right;"><?php echo number_format($r->harga * $r->qty); ?></span>
				</div>
			<?php } else { ?>
				<div class="item-name">
					<?php echo $r->qty . ' ' . htmlspecialchars($r->nm_inv); ?> (<?php echo $r->nota; ?>)
				</div>
				<?php if(!empty($this->data->pak[$r->kd_inv]->kd_inv)){ ?>
    				<div class="item-sub">
    					<?php foreach($this->data->pak[$r->kd_inv]->invd as $i){ ?>
    					    - <?php echo htmlspecialchars($i->nm_invd); ?> x <?php echo $i->qty; ?><br>
    					<?php } ?>
    				</div>
				<?php } ?>
				<div class="item-sub">
					<?php echo !empty($r->ket_det) ? '- ' . htmlspecialchars($r->ket_det) : '-'; ?>
				</div>
			<?php } ?>
		</div>
	<?php $totQty += $r->qty; ?>
	<?php } ?>
<?php } ?>

<?php if($this->data->mode == "bill") { ?>
	<div class="totals">
    	<div class="text-center">
       		<?php echo $totQty; ?> items Total : <?php echo $totQty; ?>
    	</div>
		<hr>
    	<table style="width:100%; margin-top:8px; font-size:14px;">
        	<tr>
            	<td style="text-align:right;">SUB TOTAL</td>
            	<td style="text-align:right;">Rp <?php echo number_format($total); ?></td>
       	 	</tr>
        	<?php if(!empty($this->data->bayar)) { ?>
            	<?php if($this->data->kasir->diskg_nilai > 0){ ?>
            	<tr>
            		<td style="text-align:right;">DISKON (<?php echo $this->data->kasir->diskg; ?>%)</td>
            		<td style="text-align:right;">Rp <?php echo number_format($this->data->kasir->diskg_nilai); ?></td>
        		</tr>
            	<?php } ?>
            <tr>
            	<td style="text-align:right;">PPN (<?php echo $this->data->kasir->ppn_p; ?>%)</td>
            	<td style="text-align:right;">Rp <?php echo number_format($this->data->kasir->ppn_nilai); ?></td>
        	</tr>
        	<tr>
            	<td style="text-align:right; font-weight:bold;">GRAND TOTAL</td>
            	<td style="text-align:right; font-weight:bold;">Rp <?php echo number_format($this->data->kasir->gtot); ?></td>
        	</tr>
        	<tr>
            	<td style="text-align:right;">METODE BAYAR</td>
            	<td style="text-align:right;">
            		<?php 
            			if($this->data->bayar->qris > 0){
                    		echo 'QRIS '.$this->data->bayar->nm_qris;
                    	} 
                        else if($this->data->bayar->debet > 0){
                        	echo 'DEBET '.$this->data->bayar->nm_debet;
                        }
						else if($this->data->bayar->credit > 0){
                        	echo 'CREDIT '.$this->data->bayar->nm_credit;
                        }
						else if($this->data->bayar->deliv > 0){
                        	echo 'DELIVERY '.$this->data->bayar->nm_deliv;
                        }
                        else{
                        	echo 'TUNAI';
                        }                          
                    ?>
            	</td>
        	</tr>
            <tr>
            	<td style="text-align:right;">BAYAR</td>
            	<td style="text-align:right;">Rp <?php echo number_format($this->data->bayar->bayar); ?></td>
        	</tr>
        	<tr>
            	<td style="text-align:right;">KEMBALIAN</td>
            	<td style="text-align:right;">Rp <?php echo number_format($this->data->bayar->change); ?></td>
        	</tr>
        	<?php }else{ 
            	$disc = $this->data->disc;
				if ($disc < 0) $disc = 0;
				if ($disc > 100) $disc = 100;
				$subtotal = $total;
				$diskon_nilai = $subtotal * ($disc / 100);
				$dpp = $subtotal - $diskon_nilai;
            	$ppn_nilai = $dpp * $this->data->ppn_p / 100;
				$gtot = $dpp + $ppn_nilai;
            ?>
            <?php if($diskon_nilai > 0){ ?>
            	<tr>
					<td style="text-align:right;">DISKON (<?php echo $disc; ?>%)</td>
					<td style="text-align:right;">Rp <?php echo number_format($diskon_nilai); ?></td>
				</tr>
            <?php } ?>
				<tr>
					<td style="text-align:right;">PB1 (<?php echo $this->data->ppn_p ?>%)</td>
					<td style="text-align:right;">Rp <?php echo number_format($ppn_nilai); ?></td>
				</tr>
				<tr>
					<td style="text-align:right; font-weight:bold;">GRAND TOTAL</td>
					<td style="text-align:right; font-weight:bold;">Rp <?php echo number_format($gtot); ?></td>
				</tr>
            <?php } ?>
    	</table>
	</div>
<?php } elseif ($_GET['copy'] == 'T') { ?>
	<div class="text-center" style="margin-top:15px;font-weight:bold;">#COPY</div>
<?php } ?>
<br><br><br><br><br><br><br><br>&nbsp;