<!-- Content Header (Page header) -->
<section class="content-header">
	<div class="container-fluid">
		<div class="row mb-2">
			<div class="col-sm-12">
				<h1>LAPORAN KAS HARIAN</h1>
			</div>

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

<section class="content">
	<!-- start isi konten -->
	<div class="card">
		<div class="card-body ">
			<div class="container-fluid">

				<div id="flash" data-flash="<?= $this->session->flashdata('success'); ?>"></div>

				<form action="<?= base_url('lapK/addlapJurnalHariancab'); ?>" method="post">

					<div class="row mb-2">

						<div class="col-12 col-lg-6 col-sm-6 mb-2 ">
							<div class="input-group align-center">
								<div class="input-group-prepend">
									<span class="input-group-text"><i class="fas fa-calendar-alt"></i></span>
								</div>
								<input name="tgl_jurhcab_in" readonly="on" value="<?php echo date('m / Y') ?>" class="form-control " type="hidden">

								<input name="tgl_jurhcab" readonly="on" value="<?php echo date('d / m / Y') ?>" class="form-control " type="text">

							</div>
						</div>
						<div class="col-12 col-lg-6 col-sm-6 mb-2 ">
							<div class="input-group align-center">
								<div class="input-group-prepend">
									<span class="input-group-text"><i class="fas fa-file-invoice"></i></span>
								</div>
								<input name="inov_jurhcab" readonly="on" value="<?= $inoicecabjh ?>" class="form-control <?= set_value('inov_jurhcab') ?  'is-invalid' : null  ?>" type="text">
								<?= form_error('inov_jurhcab', '<small class="text-primary">', '</small>') ?>
							</div>
						</div>

						<table class='table table-hover table-sm '>
							<tr>
								<th scope="row" colspan="3">Kas Awal / Down Payment</th>
								<td>
									<input required name="kasawal_jurhcab" id="kashawal" value="" class="form-control " type="text">
								</td>
							</tr>
							<tr>
								<th scope="row" colspan="4">Pendapatan :</th>
								<tbody class=" table-sm " id="cart_tabel">
							</tr>
							<?php foreach ($inovoicharian->result() as $am => $data) : ?>
								<tr id="car_tabel">
									<th scope="row"></th>
									<td>
										<?= $data->sell_tgl_byr ?> - <?= $data->invoice_no ?> [ <?= $data->byr ?> ]
									</td>
									<td>
										<table class=" table-sm">
											<tr>
												<td scope="col" id="total"><?= $data->totbyr ?></td>
											</tr>
										</table>
									</td>
									<td colspan="2">
									</td>
								</tr>
							<?php endforeach ?>

							<tr id="car_tabel">
								</tbody>
								<th scope="row" style="width: 20px" colspan="2"></th>
								<td>
									<b>Total Pendapatan</b>
								</td>
								<td>
									<input name="pen_jurhcab" id="sub_total" readonly="on" value="" class="form-control " type="text">
								</td>
							</tr>
							<tr>
								<th scope="row" colspan="3">Arus Kas setelah Pendapatan</th>
								<td>
									<input name="aksp_jurhcab" id="kawal" readonly="on" value="" class="form-control " type="text">
								</td>
							</tr>
							<tr>
								<th scope="row" colspan="4"><b class="btn btn-outline-secondary btn-sm " type="submit" data-toggle="modal" data-target="#pengeluarancab"><i class='fa fa-plus'></i> Biaya Pengeluaran Harian</b>
								</th>
							</tr>
							<?php foreach ($inpengharian->result() as $am => $data) : ?>
								<tr id="car_tabela">
									<th scope="row"></th>
									<td>
										<a id="btn-hapus" href="<?= site_url('lapK/delpeng/' . $data->id_pengcab) ?>">
											[ <i class="fas fa-trash-alt"></i>
										</a> ]<?= $data->invoice_pengcab ?> : <?= $data->nama_pengcab ?> ( <?= $data->qty_pengcab ?> x <?= $data->hrg_pengcab ?>)
									</td>
									<td>
										<table class=" table-sm">
											<tr>
												<td scope="col" id="totala"><?= $data->tot_pengcab ?></td> <!-- revisi total pengeluaran -->
											</tr>
										</table>
									</td>
									<td colspan="2">
									</td>
								</tr>
							<?php endforeach ?>
							<tr id="car_tabela">
								<th scope="row" style="width: 20px" colspan="2"></th>

								<td>
									<b>Total Pengeluaran Harian</b>
								</td>
								<td>
									<input name="peng_jurhcab" id="sub_totala" readonly="on" value="" class="form-control " type="text">

								</td>
							</tr>
							<tr id="car_tabela">
								<th scope="row" style="width: 20px" colspan="2"></th>

								<td>
									<b>Total Pembelian Sparepart</b>
								</td>
								<td>
									<?= $totpesbrgkasir == 0 ? "
									<input name='pemb_part' id='sub_totalb' readonly value='0' class='form-control' type='text'>
"
										:
										"
<input name='pemb_part' readonly id='sub_totalb' value='$totpesbrgkasir ' class='form-control' type='text'>
"
									?>
								</td>
							</tr>
							<tr>
								<th scope="row" colspan="3">Kas Akhir</th>

								<td>
									<input name="kasakhir_jurhcab" id="kasak" readonly="on" value="" class="form-control " type="text">

								</td>
							</tr>

							<input name="totservis" value="<?= $totservice ?>" class="form-control " type="hidden">


						</table>
						<button class="btn btn-primary w-100 btn-lg  " type="submit">Simpan Laporan Kas Harian</button>
				</form>
			</div>

			<!-- Modal Pengeluaran Cabang-->

			<form action="<?= base_url('lapK/addpencab'); ?>" method="post">
				<div class="modal fade bd-example-modal-lg" id="pengeluarancab" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
					<div class="modal-dialog modal-dialog-centered " role="document">
						<div class="modal-content">

							<div class="modal-body">
								<div class='row'>

									<div class='col-6 col-md-6 col-sm-6' style="font-size: 13px">
										<div class='input-group'>
											<input readonly="on" type='text' class='form-control' value='<?php echo date('d / m / Y') ?>' name="tgl_pengcab" autocomplete="off" required>
											<input readonly="on" type='hidden' class='form-control' value='<?php echo date('m / Y') ?>' name="bln_pengcab" autocomplete="off" required>

										</div>
									</div>

									<div class='col-6 col-md-6 col-sm-6 ' style="font-size: 13px">
										<div class='input-group'>
											<input readonly="on" type='text' class='form-control' value='<?= $inpengcaba ?>' name="invoice_pengcab">
										</div>
									</div>

									<div class='col-lg-12 mt-2 ' style="font-size: 13px">
										<div class='input-group'>
											<input placeholder="Nama Barang" type='text' class='form-control' value='' name="nama_pengcab" autocomplete="off" required>
										</div>
									</div>
									<div class='col-6 col-md-6 col-sm-6 mt-2 ' style="font-size: 13px">
										<div class='input-group'>
											<input placeholder="jumlah" type='text' class='form-control' value='' name="qty_pengcab" autocomplete="off" required>
										</div>
									</div>
									<div class='col-6 col-md-6 col-sm-6 mt-2 ' style="font-size: 13px">
										<div class='input-group'>
											<input placeholder="Harga Satuan" type='text' class='form-control' value='' name="hrg_pengcab" autocomplete="off" required>
										</div>
									</div>

								</div>
							</div>
							<div class="modal-footer">
								<button type="submit" class="btn btn-warning w-100">Tambah Biaya Pengeluaran</button>
							</div>
						</div>
					</div>
				</div>
			</form>

		</div>
	</div>
	<!-- end isi konten -->
</section>

<script src="<?= base_url() ?>/assets/dist/js/jquery-2.1.1.js"></script>
<script>
	function calculate() {
		var kasawal1 = $('#kashawal').val()
		var subtotal = 0;
		$('#car_tabel tr').each(function() {
			subtotal += parseInt($(this).find('#total').text())
		})
		isNaN(subtotal) ? $('#sub_total').val(0) : $('#sub_total').val(subtotal)


		var grand_total = parseInt(subtotal) + parseInt(kasawal1)
		if (isNaN(grand_total)) {
			$('#kawal').val(0)
			//        $('grand_total2').text(0)
		} else {
			$('#kawal').val(grand_total)
			//       $('#grand_total2').text(grand_total)
		}

		var subtotala = 0;
		$('#car_tabela tr').each(function() {
			subtotala += parseInt($(this).find('#totala').text())
		})
		isNaN(subtotala) ? $('#sub_totala').val(0) : $('#sub_totala').val(subtotala)


		var pengpart = $('#sub_totalb').val()

		var kasakhir = parseInt(grand_total) - parseInt(subtotala) - parseInt(pengpart)
		if (isNaN(kasakhir)) {
			$('#kasak').val(0)

		} else {
			$('#kasak').val(kasakhir)
			//       $('#grand_total2').text(grand_total)
		}

		var subtotalbb = 0;
		$('#car_tabelbb tr').each(function() {
			subtotalbb += parseInt($(this).find('#totalbb').text())
		})
		isNaN(subtotalbb) ? $('#subtotalbb').val(0) : $('#subtotalbb').val(subtotalbb)

		var pengrlcab = 0;
		$('#car_totalrlcab tr').each(function() {
			pengrlcab += parseInt($(this).find('#totalrlcab').text())
		})
		isNaN(pengrlcab) ? $('#subpengrlcab').val(0) : $('#subpengrlcab').val(pengrlcab)

		var kasawalbb = $('#kasbb').val()
		var kasaawalrlcab = parseInt(kasawalbb) - parseInt(pengrlcab)
		if (isNaN(kasaawalrlcab)) {
			$('#kasawalbba').val(0)

		} else {
			$('#kasawalbba').val(kasaawalrlcab)
			//       $('#grand_total2').text(grand_total)
		}

		var bagihasil = parseInt(kasaawalrlcab) / 4
		if (isNaN(bagihasil)) {
			$('#invmodal').val(0)

		} else {
			$('#invmodal').val(bagihasil)
			//       $('#grand_total2').text(grand_total)
		}

		var bagihasil = parseInt(kasaawalrlcab) / 4
		if (isNaN(bagihasil)) {
			$('#invmodala').val(0)

		} else {
			$('#invmodala').val(bagihasil)
			//       $('#grand_total2').text(grand_total)
		}

		var bagihasil = parseInt(kasaawalrlcab) / 4
		if (isNaN(bagihasil)) {
			$('#invmodalb').val(0)

		} else {
			$('#invmodalb').val(bagihasil)
			//       $('#grand_total2').text(grand_total)
		}

		var bagihasil = parseInt(kasaawalrlcab) / 4
		if (isNaN(bagihasil)) {
			$('#invmodalc').val(0)

		} else {
			$('#invmodalc').val(bagihasil)
			//       $('#grand_total2').text(grand_total)
		}

		var totalbagihasil = parseInt(kasaawalrlcab)
		if (isNaN(totalbagihasil)) {
			$('#totalbagihasil').val(0)

		} else {
			$('#totalbagihasil').val(totalbagihasil)
			//       $('#grand_total2').text(grand_total)
		}


		var kasakhirrl = parseInt(kasaawalrlcab) - parseInt(totalbagihasil)
		if (isNaN(kasakhirrl)) {
			$('#kasakhirrl').val(0)

		} else {
			$('#kasakhirrl').val(kasakhirrl)
			//       $('#grand_total2').text(grand_total)
		}




		var totalb = parseInt(kasawalbb)
		if (isNaN(totalb)) {
			$('#totalbln').val(0)

		} else {
			$('#totalbln').val(totalb)
			//       $('#grand_total2').text(grand_total)
		}


	}


	$(document).on('keyup mouseup', '#kashawal', function() {
		calculate()
	})

	$(document).ready(function() {
		calculate()
	})
</script>