Files
simrs-jatim/core/Billing.php
2024-04-19 14:04:41 +07:00

351 lines
17 KiB
PHP

<?php
Class Billing {
private $tempatlayanan;
private $nobill;
private $tanggal;
private $shift;
private $nomr;
private $user;
private $idxdaftar;
private $lunas;
private $jmlbayar;
private $ipaddress;
private $carabayar;
private $poliklinik;
private $aps;
private $unit;
private $nonota;
private $tbl_billing;
private $tbl_bayarbilling;
private $col_bill;
private $status_batal;
public function __construct($tempatlayanan,$nomr,$idxdaftar,$tanggal,$lunas, $jmlbayar,$carabayar,$poliklinik,$aps,$unit = 0,$user = '')
{
$this->tanggal = $tanggal;
$this->nomr = $nomr;
$this->shift = (array_key_exists('SHIFT', $_SESSION)) ? $_SESSION['SHIFT'] : 1;
$this->idxdaftar = $idxdaftar;
$this->user = ($_SESSION['NIP'] == '' && $user != '') ? $user : $_SESSION['NIP'];
$this->lunas = $lunas;
$this->jmlbayar = $jmlbayar;
$this->ipaddress = getRealIpAddr();
$this->carabayar = $carabayar;
$this->poliklinik = $poliklinik;
$this->aps = $aps;
$this->unit = $unit;
$this->nonota = lastNoNota();
$this->tempatlayanan = $tempatlayanan;
$this->status_batal = FALSE;
if($this->tempatlayanan == 'irja')
{
$this->tbl_billing = 't_billrajal';
$this->tbl_billingdetil = 't_billrajal_detil';
$this->tbl_bayarbilling = 't_bayarrajal';
$this->col_bill[$this->tbl_billing] = ['kodetarif','nomr','tanggal','shift','nip','qty','idxdaftar','nobill', 'jasa_sarana','jasa_pelayanan','jasa_anastesi','tarifrs','kdpoly','carabayar','aps','kddokter','unit','nonota','tgl_transaksi','kode_ruang'];
$this->col_bill[$this->tbl_bayarbilling] = ['nomr','shift','nip','idxdaftar','nobill',' totjasa_sarana','totjasa_pelayanan','totjasa_anastesi','tottarifrs','carabayar','aps','tglbayar','jambayar','jmbayar','tbp','lunas','status','unit','idxbill'];
}
elseif($this->tempatlayanan == 'irna')
{
$this->tbl_billing = 't_billranap';
$this->tbl_billingdetil = 't_billranap_detil';
$this->tbl_bayarbilling = 't_bayarranap';
$this->col_bill[$this->tbl_billing] = ['kodetarif','nomr','tanggal','shift','nip','qty','idxdaftar','nobill', 'jasa_sarana','jasa_pelayanan','jasa_anastesi','tarifrs','kdpoly','carabayar','aps','kddokter','unit','discount','nott','nonota','tgl_transaksi','kode_ruang'];
$this->col_bill[$this->tbl_bayarbilling] = ['nomr','shift','nip','idxdaftar','nobill',' totjasa_sarana','totjasa_pelayanan','totjasa_anastesi','tottarifrs','carabayar','aps','tglbayar','jambayar','jmbayar','tbp','lunas','status','discount','noruang','nott','tglbilling','idxbill'];
}
elseif($this->tempatlayanan == 'nonregister')
{
$this->tbl_billing = 't_billnonreg';
$this->tbl_billingdetil = 't_billnonreg_detil';
$this->tbl_bayarbilling = 't_bayarnonreg';
$this->col_bill[$this->tbl_billing] = ['kodetarif','no_nonreg','tanggal','shift','nip','qty','idx_nonreg','nobill', 'jasa_sarana','jasa_pelayanan','jasa_anastesi','tarifrs','carabayar','kddokter','unit','nonota','ruang','tgl_transaksi','kode_ruang'];
$this->col_bill[$this->tbl_bayarbilling] = ['no_nonreg','shift','nip','idx_nonreg','nobill',' totjasa_sarana','totjasa_pelayanan','totjasa_anastesi','tottarifrs','carabayar','tglbayar','jambayar','jmbayar','tbp','lunas','status','unit','idxbill'];
}
}
public function simpanBill($nobill = null)
{
if($this->status_batal){
array_push($this->col_bill[$this->tbl_billing],'status');
}
$this->setNobill($nobill);
if($this->nobill != '')
{
$rajal_status = ($this->tempatlayanan == 'irja') ? 1 : (($this->tempatlayanan == 'irna') ? 0 : 2);
$this->nonota = last_nonota_by_bill($this->nobill,$rajal_status);
$result_bill = $this->createBill();
if($result_bill) {
$save_bayar = $this->createBayar();
return $this->nonota;
}
else {
return false;
}
}
else {
return false;
}
}
private function setNobill($nobill = null) {
global $db;
if($this->tempatlayanan == 'nonregister') {
$get_bill = $db->query("SELECT nobill FROM ".$this->tbl_billing." WHERE no_nonreg = '{$this->nomr}' and idx_nonreg = ".$this->idxdaftar." limit 1");
if($get_bill->numRows() > 0) {
$data_bill = $get_bill->fetchFirst();
$this->nobill = $data_bill['nobill'];
}
elseif($nobill != null) {
$this->nobill = $nobill;
}
else {
$PRE_NO = $LASTNOBILL = $xLASTNOBILL = $nomor = null;
$PRE_NO = date('y');
// $PRE_NO = '22';
$sql = $db->query("SELECT nomor FROM m_maxnobill where type = 'bill_nonreg' and SUBSTRING(nomor::varchar(10),1,2) = '".$PRE_NO."'");
if($sql->numRows() > 0){
$row = $sql->fetchAll();
$LASTNOBILL = $row[0]['nomor']; // 18000002
if(substr($LASTNOBILL, 2) == '999999') {
$xLASTNOBILL = '1'.str_pad('0', 6, '0', STR_PAD_LEFT);
}
else {
if(strlen(substr($LASTNOBILL,2)) < 6) {
$xLASTNOBILL = str_pad( (substr($LASTNOBILL,2)+$p), 6, '0', STR_PAD_LEFT);
}
else {
$xLASTNOBILL = str_pad( (substr($LASTNOBILL,2)+$p), strlen(substr($LASTNOBILL,2)), '0', STR_PAD_LEFT);
}
}
$nomor = $PRE_NO.$xLASTNOBILL;
$this->nobill = ($nomor + 1);
}
else {
$this->nobill = $PRE_NO.'000001';
}
// $get_no = $db->query("SELECT nomor from m_maxnobill where type = 'bill_nonreg'");
// $nomor = $get_no->fetchFirst()['nomor'];
// if($nomor == null)
// {
// $this->nobill = date('y').'00000001';
// }
// else {
// $this->nobill = ($nomor + 1);
// }
execute("UPDATE m_maxnobill set nomor = ".$this->nobill." where type = 'bill_nonreg' ");
}
}
else {
$get_bill = $db->query("SELECT nobill FROM ".$this->tbl_billing." WHERE nomr = '{$this->nomr}' and idxdaftar = ".$this->idxdaftar." limit 1");
if($get_bill->numRows() > 0) {
$data_bill = $get_bill->fetchFirst();
$this->nobill = $data_bill['nobill'];
}
elseif($nobill != null) {
$this->nobill = $nobill;
}
else {
$PRE_NO = $LASTNOBILL = $xLASTNOBILL = $nomor = null;
$PRE_NO = date('y');
// $PRE_NO = '22';
$sql = $db->query("SELECT nomor FROM m_maxnobill where type = 'bill_pasien' and SUBSTRING(nomor::varchar(10),1,2) = '".$PRE_NO."'");
if($sql->numRows() > 0){
$row = $sql->fetchAll();
$LASTNOBILL = $row[0]['nomor']; // 18000002
if(substr($LASTNOBILL, 2) == '999999') {
$xLASTNOBILL = '1'.str_pad('0', 6, '0', STR_PAD_LEFT);
}
else {
if(strlen(substr($LASTNOBILL,2)) < 6) {
$xLASTNOBILL = str_pad( (substr($LASTNOBILL,2)+$p), 6, '0', STR_PAD_LEFT);
}
else {
$xLASTNOBILL = str_pad( (substr($LASTNOBILL,2)+$p), strlen(substr($LASTNOBILL,2)), '0', STR_PAD_LEFT);
}
}
$nomor = $PRE_NO.$xLASTNOBILL;
$this->nobill = ($nomor + 1);
}
else {
$this->nobill = $PRE_NO.'000001';
}
// $get_no = $db->query("SELECT nomor from m_maxnobill where type = 'bill_pasien'");
// $nomor = $get_no->fetchFirst()['nomor'];
// if($nomor == null)
// {
// $this->nobill = date('y').'000001';
// }
// else {
// $this->nobill = ($nomor + 1);
// }
execute("UPDATE m_maxnobill set nomor = ".$this->nobill." where type = 'bill_pasien' ");
}
}
}
public function setBatalBill($param) {
global $db;
$this->status_batal = TRUE;
$st_rajal = ($this->tempatlayanan == 'irja' || $this->tempatlayanan == 'nonregister') ? 1 : 0;
$where_idxbill = (is_array($param['idxbill_batal'])) ? " and a.idxbill in (".implode(",",$param['idxbill_batal']).")" : " and a.idxbill = ".$param['idxbill_batal'];
$currtime = date('Y-m-d H:i:s');
$save_m_batal = "INSERT INTO m_batal (kodetarif,nomr,tglbatal,shift,carabayar,nip,tarif,qty,keterangan,kdpoly,kddokter,idbayar,nonota,rajal)
SELECT a.kodetarif, a.nomr,'$currtime',a.shift,a.carabayar,'{$this->user}',tarifrs,qty,'{$param['alasan']}',kdpoly,kddokter,b.idxbayar,{$this->nonota},$st_rajal
FROM {$this->tbl_billing} a
JOIN {$this->tbl_bayarbilling} b ON a.idxbill = b.idxbill WHERE a.nobill = b.nobill ".$where_idxbill;
$sql_batal = execute($save_m_batal);
$where_idxbill2 = (is_array($param['idxbill_batal'])) ? "idxbill in (".implode(",",$param['idxbill_batal']).")" : "idxbill = ".$param['idxbill_batal'] ;
execute("UPDATE {$this->tbl_billing} set status = 'BATAL' where ".$where_idxbill2);
}
private function createBill() {
global $db;
$tgl_transaksi = date('Y-m-d H:i:s');
if($this->tempatlayanan == 'irna') {
$get_kode_ruang = $db->query("SELECT noruang from t_admission where id_admission = {$this->idxdaftar}");
$kode_ruang = $get_kode_ruang->fetchFirst()['noruang'];
}
elseif($this->tempatlayanan == 'irja') {
if($this->aps == 1) {
$kode_ruang = $this->poliklinik;
}
else {
$get_kode_ruang = $db->query("SELECT kdpoly from t_pendaftaran where idxdaftar = {$this->idxdaftar}");
$kode_ruang = $get_kode_ruang->fetchFirst()['kdpoly'];
}
}
elseif($this->tempatlayanan == 'nonregister') {
$get_kode_ruang = $db->query("SELECT ruang from m_nonreg where idx_nonreg = {$this->idxdaftar}");
$kode_ruang = $get_kode_ruang->fetchFirst()['ruang'];
}
$bill_ranap = ($this->tempatlayanan == 'irna') ? "{$this->unit},a.discount,0" : "{$this->unit}";
$build_query = "INSERT INTO ".$this->tbl_billing." (".implode(',',$this->col_bill[$this->tbl_billing]).") ";
if($this->status_batal) {
$build_query .= "SELECT a.kodetarif,'{$this->nomr}','{$this->tanggal}',{$this->shift},'{$this->user}',a.qty,{$this->idxdaftar},{$this->nobill},a.jasa_sarana,a.jasa_pelayanan,a.jasa_anastesi,a.tarif,{$this->poliklinik},{$this->carabayar},{$this->aps},a.kddokter,$bill_ranap,{$this->nonota},'BATAL' FROM tmp_cartbayar a WHERE a.ip = '{$this->ipaddress}'";
}
else {
if($this->tempatlayanan == 'nonregister') {
$build_query .= "SELECT a.kodetarif,'{$this->nomr}','{$this->tanggal}',{$this->shift},'{$this->user}',a.qty,{$this->idxdaftar},{$this->nobill},a.jasa_sarana,a.jasa_pelayanan,a.jasa_anastesi,a.tarif,{$this->carabayar},a.kddokter,$bill_ranap,{$this->nonota},{$this->poliklinik},'$tgl_transaksi' as tgl_transaksi,$kode_ruang as kode_ruang FROM tmp_cartbayar a WHERE a.ip = '{$this->ipaddress}'";
}
else {
$build_query .= "SELECT a.kodetarif,'{$this->nomr}','{$this->tanggal}',{$this->shift},'{$this->user}',a.qty,{$this->idxdaftar},{$this->nobill},a.jasa_sarana,a.jasa_pelayanan,a.jasa_anastesi,a.tarif,{$this->poliklinik},{$this->carabayar},{$this->aps},a.kddokter,$bill_ranap,{$this->nonota},'$tgl_transaksi' as tgl_transaksi,$kode_ruang as kode_ruang FROM tmp_cartbayar a WHERE a.ip = '{$this->ipaddress}' ORDER BY idxbayar";
}
}
if(_DBTYPE_ == 'mysqli') {
$insert_bill = execute($build_query);
$id_bill = $insert_bill->insertid;
}
else {
$insert_bill = execute($build_query.' RETURNING idxbill');
$id_bill = $insert_bill->fetchFirst()['idxbill'];
}
$tmp_cartbayar_detil = $db->query("SELECT * from tmp_cartbayar_detil where idxbayar in ( select idxbayar from tmp_cartbayar where ip = '{$this->ipaddress}') ORDER by idxbayar");
if($tmp_cartbayar_detil->numRows() > 0){
foreach($tmp_cartbayar_detil->fetchAll() as $row) {
$data_bill_detil = [
'id_bill'=>$id_bill,
'kddokter'=>$row['kddokter'],
'kode_tarif'=>$row['kode_tarif'],
'komponen'=>$row['komponen'],
'nilai'=>$row['nilai']
];
if($row['kddokter'] == '')
{
unset($data_bill_detil['kddokter']);
}
$insert_billdetil = "INSERT INTO ".$this->tbl_billingdetil." ".bind_sql($data_bill_detil);
execute($insert_billdetil);
}
}
return $insert_bill;
}
private function createBayar() {
global $db;
// if($this->carabayar > 1)
// {
// $tgl_bayar = date('Y-m-d');
// $jam_bayar = date('H:i:s');
// $user_bayar = $this->user;
// $shift_bayar = $this->shift;
// $tbp_bayar = 0;
// $lunas_bayar = 1;
// $status_bayar = 'LUNAS';
// }
// else {
$jml_bayar = ($this->tempatlayanan == 'irna') ? '0' : '(tarifrs*qty)';
$tgl_bayar = '0001-01-01';
$jam_bayar = '00:00:00';
$user_bayar = '';
$shift_bayar = '0';
$tbp_bayar = '0';
$lunas_bayar = '0';
$status_bayar = ($this->status_batal) ? 'BATAL' : 'TRX';
$field_bayar = ($this->tempatlayanan == 'irna') ? "(discount*qty) as discount,{$this->poliklinik},{$this->unit},'".date('Y-m-d')."'" : "{$this->unit}";
// }
if ($this->tempatlayanan == 'irna') {
$where = "where idxdaftar = $this->idxdaftar and nip = '$this->user' AND idxbill NOT IN ( SELECT idxbill FROM {$this->tbl_bayarbilling} WHERE idxdaftar = $this->idxdaftar and idxbill is not null)";
}
elseif ($this->tempatlayanan == 'irja') {
$where = "where nobill = $this->nobill and idxdaftar = $this->idxdaftar and nip = '$this->user' AND idxbill NOT IN ( SELECT idxbill FROM {$this->tbl_bayarbilling} WHERE nobill = $this->nobill and idxdaftar = $this->idxdaftar and idxbill is not null)";
}
elseif ($this->tempatlayanan == 'nonregister') {
$where = "where nobill = $this->nobill and idx_nonreg = $this->idxdaftar and nip = '$this->user' AND idxbill NOT IN ( SELECT idxbill FROM {$this->tbl_bayarbilling} WHERE nobill = $this->nobill and idx_nonreg = $this->idxdaftar and idxbill is not null)";
}
$build_query = "INSERT INTO {$this->tbl_bayarbilling} (".implode(',', $this->col_bill[$this->tbl_bayarbilling]).") ";
if($this->status_batal) {
$build_query .= "SELECT '{$this->nomr}',$shift_bayar,'$user_bayar',$this->idxdaftar,$this->nobill,(jasa_sarana*-1),(jasa_pelayanan*-1),(jasa_anastesi*-1),(tarifrs*-1),$this->carabayar,$this->aps,'$tgl_bayar','$jam_bayar',$jml_bayar,$tbp_bayar,$lunas_bayar,'$status_bayar',$field_bayar,idxbill FROM {$this->tbl_billing} ".$where;
}
else {
if ($this->tempatlayanan == 'nonregister') {
$build_query .= "SELECT '{$this->nomr}',$shift_bayar,'$user_bayar',$this->idxdaftar,$this->nobill,(jasa_sarana*qty),(jasa_pelayanan*qty),(jasa_anastesi*qty),(tarifrs*qty),$this->carabayar,'$tgl_bayar','$jam_bayar',$jml_bayar,$tbp_bayar,$lunas_bayar,'$status_bayar',$field_bayar,idxbill FROM {$this->tbl_billing} ".$where;
}
else {
$build_query .= "SELECT '{$this->nomr}',$shift_bayar,'$user_bayar',$this->idxdaftar,$this->nobill,(jasa_sarana*qty),(jasa_pelayanan*qty),(jasa_anastesi*qty),(tarifrs*qty),$this->carabayar,$this->aps,'$tgl_bayar','$jam_bayar',$jml_bayar,$tbp_bayar,$lunas_bayar,'$status_bayar',$field_bayar,idxbill FROM {$this->tbl_billing} ".$where." ORDER BY idxbill";
}
}
$insert_bayar = execute($build_query);
if($insert_bayar)
{
// delete temporary
$delete_temp = execute("DELETE FROM tmp_cartbayar_detil where idxbayar in (select idxbayar from tmp_cartbayar where ip = '$this->ipaddress')");
$delete_temp = execute("DELETE FROM tmp_cartbayar where ip = '$this->ipaddress'");
return $insert_bayar;
}
else {
return false;
}
}
}