Hi Guys,
I'm trying to make 2 function in controller for calculation book return.
1. function for search calculation book return.
2. function for search interval day beetwen 2 dates.
and then I make 1 action method to running 2 function above inside the controller, but all this 3 textbox just return zero value eventhough it's too late to return the book.
Any help could be apriciate.
<input asp-for="Denda" id="Denda" class="form-control" readonly="readonly" />
<input asp-for="Jumlah_Hari_Terlambat" id="Jumlah_Hari_Terlambat" class="form-control" readonly="readonly" />
<input asp-for="Total_Denda" id="Total_Denda" class="form-control" readonly="readonly" />
Model.cs
public class Pengembalian
{
[Key]
[Display(Name = "Kode Kembali")]
[Required(ErrorMessage = "Kode Kembali is required.")]
public string Kode_Kembali { get; set; }
[Display(Name = "Tanggal Pinjam")]
[Required(ErrorMessage = "Tanggal Pinjam is required.")]
public string Tanggal_Pinjam { get; set; }
[Display(Name = "Tanggal Kembali")]
[Required(ErrorMessage = "Tanggal Kembali is required.")]
public string Tanggal_Kembali { get; set; }
[Display(Name = "Tanggal Jatuh Tempo")]
[Required(ErrorMessage = "Tanggal Jatuh Tempo is required.")]
public string Tanggal_Jatuh_Tempo { get; set; }
[Display(Name = "Denda")]
[Required(ErrorMessage = "Denda is required.")]
public string Denda { get; set; }
[Display(Name = "Jumlah Hari Terlambat")]
[Required(ErrorMessage = "Jumlah Hari Terlambat is required.")]
public string Jumlah_Hari_Terlambat { get; set; }
[Display(Name = "Total Denda")]
[Required(ErrorMessage = "Total Denda is required.")]
public string Total_Denda { get; set; }
[Display(Name = "Kode Petugas")]
[Required(ErrorMessage = "Kode Petugas is required.")]
public string Kode_Petugas { get; set; }
[Display(Name = "Nama Petugas")]
[Required(ErrorMessage = "Nama Petugas is required.")]
public string Nama_Petugas { get; set; }
[Display(Name = "Kode Anggota")]
[Required(ErrorMessage = "Kode Anggota is required.")]
public string Kode_Anggota { get; set; }
[Display(Name = "Nama Anggota")]
[Required(ErrorMessage = "Kode Anggota is required.")]
public string Nama_Anggota { get; set; }
[Display(Name = "Kode Pinjam")]
[Required(ErrorMessage = "Kode Pinjam is required.")]
public string Kode_Pinjam { get; set; }
}
Controller.cs
[HttpPost]
public JsonResult Hitung(Pengembalian pengembalianBuku)
{
CariSelisihHariTerlambatBalikinBuku(pengembalianBuku);
CariDenda(pengembalianBuku);
return Json(pengembalianBuku);
}
//function for search calculation book return fine
private void CariDenda(Pengembalian pengembalianBuku)
{
DateTime Tanggal_Kembali = Convert.ToDateTime(pengembalianBuku.Tanggal_Kembali);
DateTime TglJatuhTempo = Convert.ToDateTime(pengembalianBuku.Tanggal_Jatuh_Tempo);
if (Tanggal_Kembali <= TglJatuhTempo)
{
int Denda = 0;
pengembalianBuku.Denda = Convert.ToString(Denda);
}
else if (Tanggal_Kembali > TglJatuhTempo)
{
decimal Denda = 2000;
decimal JmlHariTerlambat = Convert.ToDecimal(pengembalianBuku.Jumlah_Hari_Terlambat);
Denda = Convert.ToDecimal(pengembalianBuku.Denda);
decimal Hasil = JmlHariTerlambat * Denda;
pengembalianBuku.Total_Denda = Convert.ToString(string.Format("{0:0,00.00}", Hasil));
}
}
//Function for search interval days beetwen 2 dates.
private void CariSelisihHariTerlambatBalikinBuku(Pengembalian pengembalianBuku)
{
DateTime TglJatuhTempo = Convert.ToDateTime(pengembalianBuku.Tanggal_Jatuh_Tempo);
DateTime TglPengembalian = Convert.ToDateTime(pengembalianBuku.Tanggal_Kembali);
TimeSpan _TimeSpan = new TimeSpan();
//_TimeSpan = TglPengembalian - TglJatuhTempo;
_TimeSpan = TglPengembalian.Subtract(TglJatuhTempo);
int TotalDays = _TimeSpan.Days;
pengembalianBuku.Jumlah_Hari_Terlambat = Convert.ToString(TotalDays);
}
Ajax
<script type="text/javascript" lang="javascript">
$(document).ready(function () {
$('#BtnHitung').on("click", function () {
$.ajax({
url: "@Url.Action("Hitung", "Pengembalian")",
type: "POST",
contentType: "application/json;charset=UTF-8",
dataType: "json",
success: function (result) {
$('#Denda').val(result.Denda);
$('#Jumlah_Hari_Terlambat').val(result.Jumlah_Hari_Terlambat);
$("#Total_Denda").val(result.Total_Denda);
}
});
});
});
</script>