<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
use nota\models\Nota;

// ===============================
// TOTAL SEMUA TRANSAKSI
// ===============================
$total_semua = Nota::find()
    ->where(['shift_id' => $shift->id])
    ->sum('grand_total');

$total_semua = $total_semua ?? 0;

// ===============================
// TOTAL CASH BERSIH (dikurangi kembalian)
// ===============================
$total_cash_bersih = Nota::find()
    ->alias('n')
    ->joinWith('paymentMethod p')
    ->where([
        'n.shift_id' => $shift->id,
        'p.code' => 'TN'
    ])
    ->sum('(n.uang_diterima - n.kembalian)');

$total_cash_bersih = $total_cash_bersih ?? 0;

// ===============================
// TOTAL KEMBALIAN
// ===============================
$total_kembalian = Nota::find()
    ->alias('n')
    ->joinWith('paymentMethod p')
    ->where([
        'n.shift_id' => $shift->id,
        'p.code' => 'TN'
    ])
    ->sum('n.kembalian');

$total_kembalian = $total_kembalian ?? 0;


use nota\models\KasirPengeluaran;

$pengeluarans = KasirPengeluaran::find()
    ->where(['shift_id' => $shift->id])
    ->orderBy(['tanggal' => SORT_DESC])
    ->all();

$total_pengeluaran = KasirPengeluaran::find()
    ->where(['shift_id' => $shift->id])
    ->sum('jumlah');

$total_pengeluaran = $total_pengeluaran ?? 0;


// ===============================
// SALDO SISTEM
// ===============================
$saldo_sistem =
    $shift->modal_awal +
    $total_cash_bersih -
    $total_pengeluaran;

$this->title = 'Tutup Kasir';
?>

<h3>Tutup Kasir</h3>
<hr>

<p><strong>Modal Awal:</strong>
    Rp <?= number_format($shift->modal_awal,0,',','.') ?>
</p>

<hr>

<p><strong>Total Penjualan (Semua):</strong>
    Rp <?= number_format($total_semua,0,',','.') ?>
</p>

<p><strong>↳ Cash Bersih (Masuk Laci):</strong>
    Rp <?= number_format($total_cash_bersih,0,',','.') ?>
</p>

<p><strong>↳ Total Kembalian:</strong>
    Rp <?= number_format($total_kembalian,0,',','.') ?>
</p>

<hr>

<p><strong>Pengeluaran:</strong>
    Rp <?= number_format($total_pengeluaran,0,',','.') ?>
</p>
<?php if($pengeluarans) : ?>

<hr>

<h4>Detail Pengeluaran</h4>

<table class="table table-bordered table-striped">
<thead>
<tr>
<th width="120">Jam</th>
<th>Keterangan</th>
<th width="150">Jumlah</th>
</tr>
</thead>

<tbody>

<?php foreach($pengeluarans as $p) : ?>

<tr>
<td><?= date('H:i', strtotime($p->tanggal)) ?></td>

<td><?= Html::encode($p->keterangan) ?></td>

<td>
Rp <?= number_format($p->jumlah,0,',','.') ?>
</td>
</tr>

<?php endforeach; ?>

</tbody>
</table>

<?php endif; ?>
<button class="btn btn-warning btn-sm" data-toggle="modal" data-target="#modalPengeluaran">
    + Pengeluaran Kas
</button>

<hr>

<p><strong>Saldo Sistem (Uang di Laci):</strong>
    Rp <?= number_format($saldo_sistem,0,',','.') ?>
</p>

<?php $form = ActiveForm::begin(); ?>

<div class="form-group">
    <label>Saldo Akhir Fisik</label>
    <input type="number"
           name="saldo_fisik"
           class="form-control"
           required>
</div>

<div class="form-group">
    <?= Html::submitButton('Tutup Kasir', [
        'class' => 'btn btn-danger'
    ]) ?>
</div>

<?php ActiveForm::end(); ?>

<div class="modal fade" id="modalPengeluaran">
<div class="modal-dialog">
<div class="modal-content">

<div class="modal-header">
<h4 class="modal-title">Input Pengeluaran Kas</h4>
</div>

<div class="modal-body">


<form id="form-pengeluaran" action="<?= \yii\helpers\Url::to(['kasir-shift/pengeluaran']) ?>">

<input type="hidden"
name="<?= Yii::$app->request->csrfParam ?>"
value="<?= Yii::$app->request->csrfToken ?>">

<input type="hidden" name="shift_id" value="<?= $shift->id ?>">

<div class="form-group">
<label>Keterangan</label>
<input type="text" name="keterangan" class="form-control">
</div>

<div class="form-group">
<label>Jumlah</label>
<input type="number" name="jumlah" class="form-control">
</div>

<button type="button" id="btn-simpan-pengeluaran" class="btn btn-success">
Simpan
</button>

</form>

</div>

</div>
</div>
</div>

<hr>
<!-- tampilan ke 2 -->
<h3 class="page-header">Tutup Kasir</h3>

<div class="row">

<!-- ================= RINGKASAN SHIFT ================= -->

<div class="col-md-6">

<div class="panel panel-primary">

<div class="panel-heading">
<strong>Ringkasan Shift</strong>
</div>

<table class="table table-bordered">

<tr>
<td width="60%">Modal Awal</td>
<td class="text-right">
<span class="label label-primary" style="font-size:14px">
Rp <?= number_format($shift->modal_awal,0,',','.') ?>
</span>
</td>
</tr>

<tr>
<td>Total Penjualan</td>
<td class="text-right">
<span class="label label-success" style="font-size:14px">
Rp <?= number_format($total_semua,0,',','.') ?>
</span>
</td>
</tr>

<tr>
<td>Cash Bersih Masuk Laci</td>
<td class="text-right">
<span class="label label-info" style="font-size:14px">
Rp <?= number_format($total_cash_bersih,0,',','.') ?>
</span>
</td>
</tr>

<tr>
<td>Total Kembalian</td>
<td class="text-right">
<span class="label label-default" style="font-size:14px">
Rp <?= number_format($total_kembalian,0,',','.') ?>
</span>
</td>
</tr>

<tr class="warning">
<td><strong>Saldo Sistem</strong></td>
<td class="text-right">
<span class="label label-warning" style="font-size:15px">
Rp <?= number_format($saldo_sistem,0,',','.') ?>
</span>
</td>
</tr>

</table>

</div>

</div>


<!-- ================= PENGELUARAN ================= -->

<div class="col-md-6">

<div class="panel panel-warning">

<div class="panel-heading">
<strong>Pengeluaran Kas</strong>
</div>

<div class="panel-body">

<div class="row" style="margin-bottom:10px;">

<div class="col-md-6">
<strong>Total Pengeluaran :</strong>
Rp <?= number_format($total_pengeluaran,0,',','.') ?>
</div>

<div class="col-md-6 text-right">

<button class="btn btn-warning btn-sm"
data-toggle="modal"
data-target="#modalPengeluaran">

<span class="glyphicon glyphicon-plus"></span>
Tambah Pengeluaran

</button>

</div>

</div>

<?php if($pengeluarans) : ?>

<table class="table table-bordered table-striped table-hover">

<thead style="background:#f9f9f9">

<tr>
<th width="90">Jam</th>
<th>Keterangan</th>
<th width="150" class="text-right">Jumlah</th>
</tr>

</thead>

<tbody>

<?php foreach($pengeluarans as $p) : ?>

<tr>

<td>
<?= date('H:i', strtotime($p->tanggal)) ?>
</td>

<td>
<?= Html::encode($p->keterangan) ?>
</td>

<td class="text-right">
Rp <?= number_format($p->jumlah,0,',','.') ?>
</td>

</tr>

<?php endforeach; ?>

</tbody>

</table>

<?php else : ?>

<div class="alert alert-info text-center">
Belum ada pengeluaran kas
</div>

<?php endif; ?>

</div>
</div>

</div>

</div>


<!-- ================= TUTUP KASIR ================= -->

<div class="panel panel-danger">

<div class="panel-heading">
<strong>Tutup Shift Kasir</strong>
</div>

<div class="panel-body">

<?php $form = ActiveForm::begin(); ?>

<div class="row">

<div class="col-md-4">

<label>Saldo Akhir Fisik</label>

<input type="number"
name="saldo_fisik"
class="form-control input-lg"
required>

</div>

<div class="col-md-3" style="margin-top:25px">

<?= Html::submitButton('Tutup Kasir', [
'class' => 'btn btn-danger btn-lg btn-block'
]) ?>

</div>

</div>

<?php ActiveForm::end(); ?>

</div>

</div>

<script>
$('#btn-simpan-pengeluaran').on('click', function(e){

    e.preventDefault();

    var form = $('#form-pengeluaran');

    console.log(form.serialize());

    $.ajax({

        url: form.attr('action'),
        type: 'POST',
        data: form.serialize(),
        dataType: 'json',

        success: function(res){

            console.log(res);

            if(res.success){

                swal("Berhasil!", "Pengeluaran tersimpan", "success");

                $('#modalPengeluaran').modal('hide');

                setTimeout(function(){
                    location.reload();
                },500);

            }else{

                swal("Gagal!", res.message, "error");

            }

        },

        error: function(xhr){
            console.log(xhr.responseText);
            swal("Error!", "Terjadi kesalahan server.", "error");
        }

    });

});
</script>