<table class="table table-hover datatable" style="width: 100%" id="tabelku">
<thead>
<tr>
    <th>Kode Barang</th>
    <th>Nama Barang</th>
    <th>Jumlah Diminta</th>
    <th>Jumlah Datang</th>
    <th width="12%">Satuan</th>
    <th>Harga<br>Satuan</th>
    <th>Harga Beli<br>Eceran</th>
    <th>Total Harga</th>
</tr>
</thead>
<tbody>
<?php
$i=1;
foreach ($databarang as $hasil) {
    if ($hasil->harga_eceran_diterima) {
        $hargabeli=$hasil->harga_eceran_diterima;
    } else {
        $hargabeli=$hasil->harga_beli;
    }
    $tot=($hasil->jumlah_diterima*$hasil->konversi_diterima)*$hargabeli;
    ?>
    <tr>
        <td>
            <?php echo $hasil->kode_barang ?>
            <input type="hidden" value="<?php echo $hasil->id_permintaanmeta ?>" name="id<?php echo $i ?>" id="id<?php echo $i ?>"> 
        </td>
        <td><?php echo $hasil->barang ?></td>
        <td><?php echo $hasil->jumlah.' '.$hasil->satuan ?></td>
        <td>
            <input type="number" value="<?php echo $hasil->jumlah_diterima ?>" name="diterima<?php echo $i ?>" id="diterima<?php echo $i ?>" class="form-control" onkeyup="hitungharga(); simpan(<?php echo $i ?>); hitunghargaecer(<?php echo $i ?>)" onchange="simpan(<?php echo $i ?>)">
        </td>
        <td>
            <select name="satuan<?php echo $i ?>" id="satuan<?php echo $i ?>" class="form-control" onchange="hitungharga(); simpan(<?php echo $i ?>); hitunghargaecer(<?php echo $i ?>)">
                <?php
                if (@$arrsatuan[$hasil->kode_barang]) {
                    foreach ($arrsatuan[$hasil->kode_barang] as $key => $value) {
                        $pecahsatuan=explode('|',$value);
                        ?>
                        <option value="<?php echo $value ?>" <?php if (@$pecahsatuan[0]==$hasil->satuan) echo 'selected' ?>><?php echo @$pecahsatuan[0] ?></option>
                        <?php
                    }
                }
                ?>
            </select>
        </td>
        <td>
            <input type="number" value="<?php echo $hasil->harga_satuan_diterima ?>" name="hargabeli<?php echo $i ?>" id="hargabeli<?php echo $i ?>" class="form-control" onkeyup="hitungharga(); simpan(<?php echo $i ?>); hitunghargaecer(<?php echo $i ?>)" onchange="simpan(<?php echo $i ?>)">
        </td>
        <td><input type="number" value="<?php echo $hargabeli ?>" name="hargaecer<?php echo $i ?>" id="hargaecer<?php echo $i ?>" class="form-control" onkeyup="hitungharga(); simpan(<?php echo $i ?>)" onchange="simpan(<?php echo $i ?>)"></td>
        <td><span id="tot<?php echo $i ?>"><?php echo number_format($tot,0,',','.') ?></span></td>
    </tr>
    <?php
    $i++;
}
?>
</tbody>
</table>
<div class="row">
    <div class="col-md-12">
        <h3 id="toha" style="color: black; font-weight: bold;">Total Harga Rp 0</h3>
    </div>
</div>
<script type="text/javascript">
$(document).ready(function() {
    hitungharga();
});

function hitungharga() {
    <?php
    $i=1;
    foreach ($databarang as $hasil) {
        ?>
        var terima<?php echo $i  ?>=$("#diterima<?php echo $i ?>").val();
        var harga<?php echo $i  ?>=$("#hargabeli<?php echo $i ?>").val();
        var satuan<?php echo $i  ?>=$("#satuan<?php echo $i ?>").val();
        <?php
        $i++;
    }
    ?>
    hitung=<?php $i=1; foreach ($databarang as $hasil) {  if ($i==1) { ?> (terima<?php echo $i  ?>*harga<?php echo $i  ?>) <?php } else { ?>+(terima<?php echo $i  ?>*harga<?php echo $i  ?>) <?php } $i++; } ?>;
    rupiah=new Intl.NumberFormat("id-ID",{
            style:"decimal",
            currency:"IDR"
            }).format(hitung);
    $("#toha").html("Total Harga Rp "+rupiah);
}

function hitunghargaecer(i) {
    var satuan=$("#satuan"+i).val();
    var hargabeli=$("#hargabeli"+i).val();
    pecah=satuan.split("|");
    hitung=hargabeli/pecah[1];
    bulat=Math.round(hitung);
    $("#hargaecer"+i).val(bulat);

}
</script>