<?php
class classTransaction
{
	private $transactionID=NULL;
	private $transactionTime=NULL;
	private $locationID=NULL;
	private $visitID=NULL;
	private $wardBedAllotID=NULL;
	private $doctorID=NULL;
	private $patientID=NULL;
	private $supplierID=NULL;
	private $accountID=NULL;
	private $accountHeadID=NULL;
	
	private $receiveInvoiceID=NULL;
	private $saleInvoiceID=NULL;
	private $diagInvoiceID=NULL;
	
	private $transactionType=NULL;
	private $transactionPurpose=NULL;
	private $description=NULL;
	
	private $totalAmount=NULL;
	private $discount=NULL;
	private $amountPayable=NULL;
	private $payment=NULL;
	private $balance=NULL;
	
	private $userID=NULL;
	
	############################# SET FUNCTIONS #####################################3
	
	function setTransactionID($transactionID)
	{
		if($transactionID!='')
		{
			$this->transactionID = $transactionID;
		}
		else
		{
			throw new MyException(111100);
		}
	}
	function setTransactionTime($transactionTime)
	{
		if($transactionTime!='')
		{
			$this->transactionTime = $transactionTime;
		}
		else
		{
			throw new MyException(111101);
		}
	}
	function setLocationID($locationID)
	{
		if($locationID!="")
		{
			$this->locationID = $locationID;
		}
		
	}
	function setVisitID($visitID)
	{
		if($visitID!='')
		{
			$this->visitID = $visitID;
		}
		else
		{
			throw new MyException(111102);
		}
	}
	
	function setWardBedAllotID($wardBedAllotID)
	{
		if($wardBedAllotID!='')
		{
			$this->wardBedAllotID = $wardBedAllotID;
		}
		else
		{
			throw new MyException('Please enter ipd bed allotment ID.');
		}
	}
	
	function setDoctorID($doctorID)
	{
		if($doctorID!='')
		{
			$this->doctorID = $doctorID;
		}
		else
		{
			throw new MyException(111103);
		}
	}
	function setPatientID($patientID)
	{
		if($patientID!='')
		{
			$this->patientID = $patientID;
		}
		else
		{
			throw new MyException(111104);
		}
	}
	function setSupplierID($supplierID)
	{
		if($supplierID!='')
		{
			$this->supplierID = $supplierID;
		}
		else
		{
			throw new MyException(111105);
		}
	}
	function setAccountID($accountID)
	{
		if($accountID!='')
		{
			$this->accountID = $accountID;
		}
		else
		{
			throw new MyException(111106);
		}
	}
	function setAccountHeadID($accountHeadID)
	{
		if($accountHeadID!='')
		{
			$this->accountHeadID = $accountHeadID;
		}
		else
		{
			throw new MyException(111107);
		}
	}
	
	function setReceiveInvoiceID($receiveInvoiceID)
	{
		if($receiveInvoiceID!='')
		{
			$this->receiveInvoiceID = $receiveInvoiceID;
		}
		else
		{
			throw new MyException(111108);
		}
	}
	function setSaleInvoiceID($saleInvoiceID)
	{
		if($saleInvoiceID!='')
		{
			$this->saleInvoiceID = $saleInvoiceID;
		}
		else
		{
			throw new MyException(111109);
		}
	}
	
	function setDiagInvoiceID($diagInvoiceID)
	{
		if($diagInvoiceID!='')
		{
			$this->diagInvoiceID = $diagInvoiceID;
		}
		else
		{
			throw new MyException(111140);
		}
	}
	
	function setTransactionType($transactionType)
	{
		if($transactionType!='')
		{
			$this->transactionType = $transactionType;
		}
		else
		{
			throw new MyException(111110);
		}
	}
	function setTransactionPurpose($transactionPurpose)
	{
		if($transactionPurpose!='')
		{
			$this->transactionPurpose = $transactionPurpose;
		}
		else
		{
			throw new MyException(111111);
		}
	}
	function setDescription($description)
	{
		if($description!='')
		{
			$this->description = $description;
		}
		else
		{
			//throw new MyException(111112);
		}
	}
	
	function setTotalAmount($totalAmount)
	{
		if($totalAmount!='')
		{
			$this->totalAmount = $totalAmount;
		}
		else
		{
			throw new MyException(111113);
		}
	}
	function setDiscount($discount)
	{
		if($discount!='')
		{
			$this->discount = $discount;
		}
		else
		{
			throw new MyException(111114);
		}
	}
	function setAmountPayable($amountPayable)
	{
		if($amountPayable!='')
		{
			$this->amountPayable = $amountPayable;
		}
		else
		{
			throw new MyException(111115);
		}
	}
	function setPayment($payment)
	{
		if($payment!='')
		{
			if(is_numeric($payment))
			{
				$this->payment = $payment;
			}
			else
			{
				throw new MyException(111138);
			}
		}
		else
		{
			throw new MyException(111116);
		}
	}
	function setBalance($balance)
	{
		if($balance!='')
		{
			$this->balance = $balance;
		}
		else
		{
			//throw new MyException(111117);
		}
	}
	
	function setUserID($userID)
	{
		if($userID!='')
		{
			$this->userID = $userID;
		}
		else
		{
			throw new MyException(111118);
		}
	}

	############################# GET FUNCTIONS #####################################
	
	function getTransactionID()
	{
		if($this->transactionID!='')
		{
			return $this->transactionID;
		}
		else
		{
			throw new MyException(111119);
		}
	}
	function getTransactionTime()
	{
		if($this->transactionTime!='')
		{
			return $this->transactionTime;
		}
		else
		{
			throw new MyException(111120);
		}
	}
	function getLocationID()
	{
		if($this->locationID!="")
		{
			return $this->locationID;
		}
		
	}
	function getVisitID()
	{
		if($this->visitID!='')
		{
			return $this->visitID;
		}
		else
		{
			throw new MyException(111121);
		}
	}
	
	function getWardBedAllotID()
	{
		if($this->wardBedAllotID!='')
		{
			return $this->wardBedAllotID ;
		}
		else
		{
			throw new MyException('Please enter ipd bed allotment ID.');
		}
	}
	
	function getDoctorID()
	{
		if($this->doctorID!='')
		{
			return $this->doctorID;
		}
		else
		{
			throw new MyException(111122);
		}
	}
	function getPatientID()
	{
		if($this->patientID!='')
		{
			return $this->patientID;
		}
		else
		{
			throw new MyException(111123);
		}
	}
	function getSupplierID()
	{
		if($this->supplierID!='')
		{
			return $this->supplierID;
		}
		else
		{
			throw new MyException(111124);
		}
	}
	function getAccountID()
	{
		if($this->accountID!='')
		{
			return $this->accountID;
		}
		else
		{
			throw new MyException(111125);
		}
	}
	function getAccountHeadID()
	{
		if($this->accountHeadID!='')
		{
			return $this->accountHeadID;
		}
		else
		{
			throw new MyException(111126);
		}
	}
	
	function getReceiveInvoiceID()
	{
		if($this->receiveInvoiceID!='')
		{
			return $this->receiveInvoiceID;
		}
		else
		{
			throw new MyException(111127);
		}
	}
	function getSaleInvoiceID()
	{
		if($this->saleInvoiceID!='')
		{
			return $this->saleInvoiceID;
		}
		else
		{
			throw new MyException(111128);
		}
	}
	
	function getDiagInvoiceID()
	{
		if($this->diagInvoiceID!='')
		{
			return $this->diagInvoiceID;
		}
		else
		{
			throw new MyException(111141);
		}
	}
	
	function getTransactionType()
	{
		if($this->transactionType!='')
		{
			return $this->transactionType;
		}
		else
		{
			throw new MyException(111129);
		}
	}
	function getTransactionPurpose()
	{
		if($this->transactionPurpose!='')
		{
			return $this->transactionPurpose;
		}
		else
		{
			throw new MyException(111130);
		}
	}
	function getDescription()
	{
		if($this->description!='')
		{
			return $this->description;
		}
		else
		{
			//throw new MyException(111131);
		}
	}
	
	function getTotalAmount()
	{
		if($this->totalAmount!='')
		{
			return $this->totalAmount;
		}
		else
		{
			throw new MyException(111132);
		}
	}
	function getDiscount()
	{
		if($this->discount!='')
		{
			return $this->discount;
		}
		else
		{
			throw new MyException(111133);
		}
	}
	function getAmountPayable()
	{
		if($this->amountPayable!='')
		{
			return $this->amountPayable;
		}
		else
		{
			throw new MyException(111134);
		}
	}
	function getPayment()
	{
		if($this->payment!='')
		{
			return $this->payment;
		}
		else
		{
			throw new MyException(111135);
		}
	}
	function getBalance()
	{
		if($this->balance!='')
		{
			return $this->balance;
		}
		else
		{
			//throw new MyException(111136);
		}
	}
	
	function getUserID()
	{
		if($this->userID!='')
		{
			return $this->userID;
		}
		else
		{
			throw new MyException(111137);
		}
	}
	
	############################# MAIN INSERT FUNCTIONS #####################################
	
	function executeForIncoming()
	{
		if(($this->getPayment()>$this->getAmountPayable())||($this->getPayment()<=0))
		{
			throw new MyException(111139);
		}
		$transact_time = time();
		$supplier_id = $this->getSupplierID();
		$acchead_id = $this->getAccountHeadID();
		$account_id = $this->getAccountID();
		$transact_type = $this->getTransactionType();
		$description = $this->getDescription();
		$amount_payable = $this->getAmountPayable();
		$payment = $this->getPayment();
		
		$balance = $amount_payable-$payment;
		
		$recinvoice_id = $this->getReceiveInvoiceID();
		$user_id = $this->getUserID();
		$location_id = $this->getLocationID();
		
		$query = "INSERT INTO transactions 
		(
			transact_time,
			supplier_id,
			acchead_id,
			account_id,
			recinvoice_id,
			transact_type,
			description,
			amount_payable,
			payment,
			balance,
			user_id,
			location_id
			
		)
		VALUES
		(
			'$transact_time',
			'$supplier_id',
			'$acchead_id',
			'$account_id',
			'$recinvoice_id',
			'$transact_type',
			'$description',
			'$amount_payable',
			'$payment',
			'$balance',
			'$user_id',
			'$location_id'
		)";
		$queryOb = new classQueryGlobal;
		$this->transactionID = $queryOb->check_fetch_query($query,'insert',$server="",$debug_mode="off");
		
		$updateRecInvOb = new classReceiveInvoice;
		$updateRecInvOb->setReceiveInvoiceID($recinvoice_id);
		$updateRecInvOb->setPayment($payment);
		$updateRecInvOb->setBalance($balance);
		$updateRecInvOb->setPaymentStatus('paid');
		$updateRecInvOb->updateReceiveInvoiceAfterTransaction();
	}
	
	function executeForSales()
	{
		if(($this->getPayment()>$this->getAmountPayable())||($this->getPayment()<=0))
		{
			throw new MyException(111139);
		}
		$transact_time = time();
		$patient_id = $this->getPatientID();
		$acchead_id = $this->getAccountHeadID();
		$account_id = $this->getAccountID();
		$transact_type = $this->getTransactionType();
		$description = $this->getDescription();
		$amount_payable = $this->getAmountPayable();
		$payment = $this->getPayment();
		
		$balance = $amount_payable-$payment;
		
		$saleinvoice_id  = $this->getSaleInvoiceID();
		$user_id = $this->getUserID();
		$location_id = $this->getLocationID();
		
		$query = "INSERT INTO transactions 
		(
			transact_time,
			patient_id,
			acchead_id,
			account_id,
			saleinvoice_id,
			transact_type,
			description,
			amount_payable,
			payment,
			balance,
			user_id,
			location_id
		)
		VALUES
		(
			'$transact_time',
			'$patient_id',
			'$acchead_id',
			'$account_id',
			'$saleinvoice_id',
			'$transact_type',
			'$description',
			'$amount_payable',
			'$payment',
			'$balance',
			'$user_id',
			'$location_id'
		)";
		$queryOb = new classQueryGlobal;
		$this->transactionID = $queryOb->check_fetch_query($query,'insert',$server="",$debug_mode="off");
		
		$updateSaleInvOb = new classSaleInvoice;
		$updateSaleInvOb->setSaleInvoiceID($saleinvoice_id);
		$updateSaleInvOb->setPayment($payment);
		$updateSaleInvOb->setBalance($balance);
		$updateSaleInvOb->setPaymentStatus('paid');
		$updateSaleInvOb->updateSaleInvoiceAfterTransaction();
	}
	
	function executeForIncomingBalance()
	{
		if(($this->getPayment()>$this->getAmountPayable())||($this->getPayment()<=0))
		{
			throw new MyException(111139);
		}
		$transact_time = time();
		$supplier_id = $this->getSupplierID();
		$acchead_id = $this->getAccountHeadID();
		$account_id = $this->getAccountID();
		$transact_type = $this->getTransactionType();
		$transaction_purpose = $this->getTransactionPurpose();
		$description = $this->getDescription();
		$amount_payable = $this->getAmountPayable();
		$payment = $this->getPayment();
		
		$balance = $amount_payable-$payment;
		
		$recinvoice_id = $this->getReceiveInvoiceID();
		$user_id = $this->getUserID();
		$location_id = $this->getLocationID();
		
		$query = "INSERT INTO transactions 
		(
			transact_time,
			supplier_id,
			acchead_id,
			account_id,
			recinvoice_id,
			transact_type,
			transaction_purpose,
			description,
			amount_payable,
			payment,
			balance,
			user_id,
			location_id
		)
		VALUES
		(
			'$transact_time',
			'$supplier_id',
			'$acchead_id',
			'$account_id',
			'$recinvoice_id',
			'$transact_type',
			'$transaction_purpose',
			'$description',
			'$amount_payable',
			'$payment',
			'$balance',
			'$user_id',
			'$location_id'
		)";
		$queryOb = new classQueryGlobal;
		$this->transactionID = $queryOb->check_fetch_query($query,'insert',$server="",$debug_mode="off");
		
		$retrieveRecInvOb = new classReceiveInvoice;
		$retrieveRecInvOb->setReceiveInvoiceID($recinvoice_id);
		$inv_detail = $retrieveRecInvOb->retrieveInvoiceDetail();
		
		$payment = $inv_detail[0]['payment']+$payment;
		$balance = $inv_detail[0]['amount_payable']-$payment;
		
		$updateRecInvOb = new classReceiveInvoice;
		$updateRecInvOb->setReceiveInvoiceID($recinvoice_id);
		$updateRecInvOb->setPayment($payment);
		$updateRecInvOb->setBalance($balance);
		$updateRecInvOb->setPaymentStatus('paid');
		$updateRecInvOb->updateReceiveInvoiceAfterTransaction();
	}
	
	function executeForSalesBalance()
	{
		if(($this->getPayment()>$this->getAmountPayable())||($this->getPayment()<=0))
		{
			throw new MyException(111139);
		}
		$transact_time = time();
		$patient_id = $this->getPatientID();
		$acchead_id = $this->getAccountHeadID();
		$account_id = $this->getAccountID();
		$transact_type = $this->getTransactionType();
		$transaction_purpose = $this->getTransactionPurpose();
		$description = $this->getDescription();
		$amount_payable = $this->getAmountPayable();
		$payment = $this->getPayment();
		
		$balance = $amount_payable-$payment;
		
		$saleinvoice_id  = $this->getSaleInvoiceID();
		$user_id = $this->getUserID();
		$location_id = $this->getLocationID();
		
		$query = "INSERT INTO transactions 
		(
			transact_time,
			patient_id,
			acchead_id,
			account_id,
			saleinvoice_id,
			transact_type,
			transaction_purpose,
			description,
			amount_payable,
			payment,
			balance,
			user_id,
			location_id
		)
		VALUES
		(
			'$transact_time',
			'$patient_id',
			'$acchead_id',
			'$account_id',
			'$saleinvoice_id',
			'$transact_type',
			'$transaction_purpose',
			'$description',
			'$amount_payable',
			'$payment',
			'$balance',
			'$user_id',
			'$location_id'
		)";
		$queryOb = new classQueryGlobal;
		$this->transactionID = $queryOb->check_fetch_query($query,'insert',$server="",$debug_mode="off");
		
		$retrieveSaleInvOb = new classSaleInvoice;
		$retrieveSaleInvOb->setSaleInvoiceID($saleinvoice_id);
		$inv_detail = $retrieveSaleInvOb->retrieveInvoiceDetail();
		
		$payment = $inv_detail[0]['payment']+$payment;
		$balance = $inv_detail[0]['amount_payable']-$payment;
		
		$updateSaleInvOb = new classSaleInvoice;
		$updateSaleInvOb->setSaleInvoiceID($saleinvoice_id);
		$updateSaleInvOb->setPayment($payment);
		$updateSaleInvOb->setBalance($balance);
		$updateSaleInvOb->setPaymentStatus('paid');
		$updateSaleInvOb->updateSaleInvoiceAfterTransaction();
		exit;
	}
	
	function executeForOpdPatient()
	{
		if(($this->getPayment()>$this->getAmountPayable())||($this->getPayment()<0))
		{
			throw new MyException(111139);
		}
		$transact_time = time();
		$patient_id = $this->getPatientID();
		$doctor_id = $this->getDoctorID();
		$acchead_id = $this->getAccountHeadID();
		$account_id = $this->getAccountID();
		$transact_type = $this->getTransactionType();
		$amount_payable = $this->getAmountPayable();
		$payment = $this->getPayment();
		$balance = $amount_payable-$payment;
		
		$visit_id  = $this->getVisitID();
		$user_id = $this->getUserID();
		$location_id = $this->getLocationID();
		
		$query = "INSERT INTO transactions 
		(
			transact_time,
			patient_id,
			acchead_id,
			account_id,
			visit_id,
			transact_type,
			doctor_id,
			amount_payable,
			payment,
			balance,
			user_id,
			location_id
		)
		VALUES
		(
			'$transact_time',
			'$patient_id',
			'$acchead_id',
			'$account_id',
			'$visit_id',
			'$transact_type',
			'$doctor_id',
			'$amount_payable',
			'$payment',
			'$balance',
			'$user_id',
			'$location_id'
			
		)";
		$queryOb = new classQueryGlobal;
		$this->transactionID = $queryOb->check_fetch_query($query,'insert',$server="",$debug_mode="off");
		
		
		
		$updateVsisitOb = new classVisits;
		$updateVsisitOb->setVisitID($visit_id);
		$updateVsisitOb->setAmountPayable($amount_payable);
		$updateVsisitOb->setPayment($payment);
		$updateVsisitOb->setBalance($balance);
		$updateVsisitOb->setPaymentStatus('paid');
		$updateVsisitOb->updatePayment();
	}
	
	function executeForOpdPatientBalance()
	{
		if(($this->getPayment()>$this->getAmountPayable())||($this->getPayment()<0))
		{
			throw new MyException(111139);
		}
		$transact_time = time();
		$patient_id = $this->getPatientID();
		$doctor_id = $this->getDoctorID();
		$acchead_id = $this->getAccountHeadID();
		$account_id = $this->getAccountID();
		$transact_type = $this->getTransactionType();
		$transaction_purpose = $this->getTransactionPurpose();
		$amount_payable = $this->getAmountPayable();
		$payment = $this->getPayment();
		$balance = $amount_payable-$payment;
		
		$visit_id  = $this->getVisitID();
		$user_id = $this->getUserID();
		$location_id = $this->getLocationID();
		
		$query = "INSERT INTO transactions 
		(
			transact_time,
			patient_id,
			acchead_id,
			account_id,
			visit_id,
			transact_type,
			transaction_purpose,
			doctor_id,
			amount_payable,
			payment,
			balance,
			user_id,
			location_id
		)
		VALUES
		(
			'$transact_time',
			'$patient_id',
			'$acchead_id',
			'$account_id',
			'$visit_id',
			'$transact_type',
			'$transaction_purpose',
			'$doctor_id',
			'$amount_payable',
			'$payment',
			'$balance',
			'$user_id',
			'$location_id'
			
		)";
		$queryOb = new classQueryGlobal;
		$this->transactionID = $queryOb->check_fetch_query($query,'insert',$server="",$debug_mode="off");
		
		
		
		$updateVsisitOb = new classVisits;
		$updateVsisitOb->setVisitID($visit_id);
		$updateVsisitOb->setPayment($payment);
		$updateVsisitOb->setBalance($balance);
		$updateVsisitOb->setPaymentStatus('paid');
		$updateVsisitOb->updatePaymentAfterBalanceTransaction();
	}
	
	function executeForSupplierRefund()
	{
		if(($this->getPayment()>$this->getAmountPayable())||($this->getPayment()<=0))
		{
			throw new MyException(111139);
		}
		$transact_time = time();
		$acchead_id = $this->getAccountHeadID();
		$account_id = $this->getAccountID();
		$transact_type = $this->getTransactionType();
		$amount_payable = $this->getAmountPayable();
		$payment = $this->getPayment();
		$balance = $amount_payable-$payment;
		$transaction_purpose = $this->getTransactionPurpose();
		$recinvoice_id = $this->getReceiveInvoiceID();
		$supplier_id = $this->getSupplierID();
		$user_id = $this->getUserID();
		$location_id = $this->getLocationID();
		
		$query = "INSERT INTO transactions 
		(
			transact_time,
			supplier_id,
			acchead_id,
			account_id,
			recinvoice_id,
			transact_type,
			transaction_purpose,
			amount_payable,
			payment,
			balance,
			user_id,
			location_id
		)
		VALUES
		(
			'$transact_time',
			'$supplier_id',
			'$acchead_id',
			'$account_id',
			'$recinvoice_id',
			'$transact_type',
			'$transaction_purpose',
			'$amount_payable',
			'$payment',
			'$balance',
			'$user_id',
			'$location_id'
		)";
		$queryOb = new classQueryGlobal;
		$this->transactionID = $queryOb->check_fetch_query($query,'insert',$server="",$debug_mode="off");
	}
	
	function executeForPatientRefund()
	{
		if(($this->getPayment()>$this->getAmountPayable())||($this->getPayment()<=0))
		{
			throw new MyException(111139);
		}
		$transact_time = time();
		$acchead_id = $this->getAccountHeadID();
		$account_id = $this->getAccountID();
		$transact_type = $this->getTransactionType();
		$amount_payable = $this->getAmountPayable();
		$payment = $this->getPayment();
		$balance = $amount_payable-$payment;
		$transaction_purpose = $this->getTransactionPurpose();
		$saleinvoice_id = $this->getSaleInvoiceID();
		$patient_id = $this->getPatientID();
		$user_id = $this->getUserID();
		$location_id = $this->getLocationID();
		
		$query = "INSERT INTO transactions 
		(
			transact_time,
			patient_id,
			acchead_id,
			account_id,
			saleinvoice_id,
			transact_type,
			transaction_purpose,
			amount_payable,
			payment,
			balance,
			user_id,
			location_id
		)
		VALUES
		(
			'$transact_time',
			'$patient_id',
			'$acchead_id',
			'$account_id',
			'$saleinvoice_id',
			'$transact_type',
			'$transaction_purpose',
			'$amount_payable',
			'$payment',
			'$balance',
			'$user_id',
			'$location_id'
		)";
		$queryOb = new classQueryGlobal;
		$this->transactionID = $queryOb->check_fetch_query($query,'insert',$server="",$debug_mode="off");
	}
	
	function executeForPatientRegistrationFee()
	{
		if(($this->getPayment()>$this->getAmountPayable())||($this->getPayment()<=0))
		{
			throw new MyException(111139);
		}
		$transact_time = time();
		$patient_id = $this->getPatientID();
		$acchead_id = $this->getAccountHeadID();
		$account_id = $this->getAccountID();
		$transact_type = $this->getTransactionType();
		$transaction_purpose = $this->getTransactionPurpose();
		$amount_payable = $this->getAmountPayable();
		$payment = $this->getPayment();
		$balance = $amount_payable-$payment;
		$user_id = $this->getUserID();
		$location_id = $this->getLocationID();
		
		$query = "INSERT INTO transactions 
		(
			transact_time,
			patient_id,
			acchead_id,
			account_id,
			transaction_purpose,
			transact_type,
			amount_payable,
			payment,
			balance,
			user_id,
			location_id
		)
		VALUES
		(
			'$transact_time',
			'$patient_id',
			'$acchead_id',
			'$account_id',
			'$transaction_purpose',
			'$transact_type',
			'$amount_payable',
			'$payment',
			'$balance',
			'$user_id',
			'$location_id'
		)";
		$queryOb = new classQueryGlobal;
		$this->transactionID = $queryOb->check_fetch_query($query,'insert',$server="",$debug_mode="off");
	}
	
	function executeForOpdRefund()
	{
		//$old_transaction_detail = $this->retrieveTransactionByTransactionID();
		//$check_payment = $old_transaction_detail[0]['amount_payable'] - $old_transaction_detail[0]['refunded_amount'];
		if(($this->getPayment()>$this->getAmountPayable())||($this->getPayment()<=0))
		{
			throw new MyException(111139);
		}
		$transact_time = time();
		$patient_id = $this->getPatientID();
		$acchead_id = $this->getAccountHeadID();
		$account_id = $this->getAccountID();
		$transact_type = $this->getTransactionType();
		$transaction_purpose = $this->getTransactionPurpose();
		$amount_payable = $this->getAmountPayable();
		$payment = $this->getPayment();
		$balance = $amount_payable-$payment;
		$user_id = $this->getUserID();
		
		$transact_id = $this->getTransactionID();
		$location_id = $this->getLocationID();
		
		
		
		$query = "INSERT INTO transactions 
		(
			transact_time,
			patient_id,
			acchead_id,
			account_id,
			transaction_purpose,
			transact_type,
			amount_payable,
			payment,
			balance,
			user_id,
			location_id
		)
		VALUES
		(
			'$transact_time',
			'$patient_id',
			'$acchead_id',
			'$account_id',
			'$transaction_purpose',
			'$transact_type',
			'$amount_payable',
			'$payment',
			'$balance',
			'$user_id',
			'$location_id'
		)";
		$queryOb = new classQueryGlobal;
		$this->transactionID = $queryOb->check_fetch_query($query,'insert',$server="",$debug_mode="off");
		
		$query = "UPDATE transactions SET refunded_amount = refunded_amount + '$payment' WHERE transact_id = '$transact_id'";
		$queryOb = new classQueryGlobal;
		$queryOb->check_fetch_query($query,'update',$server="",$debug_mode="off");
	}
	
	function executeForDiagnostic()
	{
		if(($this->getPayment()!=$this->getAmountPayable())||($this->getPayment()<=0))
		{
			throw new MyException(111139);
		}
		$transact_time = time();
		$patient_id = $this->getPatientID();
		$acchead_id = $this->getAccountHeadID();
		$account_id = $this->getAccountID();
		$transact_type = $this->getTransactionType();
		$transaction_purpose = $this->getTransactionPurpose();
		$description = $this->getDescription();
		$amount_payable = $this->getAmountPayable();
		$payment = $this->getPayment();
		
		$balance = $amount_payable-$payment;
		
		$diaginvoice_id  = $this->getDiagInvoiceID();
		$user_id = $this->getUserID();
		$location_id = $this->getLocationID();
		
		$query = "INSERT INTO transactions 
		(
			transact_time,
			patient_id,
			acchead_id,
			account_id,
			diaginvoice_id,
			transact_type,
			transaction_purpose,
			description,
			amount_payable,
			payment,
			balance,
			user_id,
			location_id
		)
		VALUES
		(
			'$transact_time',
			'$patient_id',
			'$acchead_id',
			'$account_id',
			'$diaginvoice_id',
			'$transact_type',
			'$transaction_purpose',
			'$description',
			'$amount_payable',
			'$payment',
			'$balance',
			'$user_id',
			'$location_id'
		)";
		$queryOb = new classQueryGlobal;
		$this->transactionID = $queryOb->check_fetch_query($query,'insert',$server="",$debug_mode="off");
		
		$updateDiagInvOb = new classDiagnostic;
		$updateDiagInvOb->setDiagInvoiceID($diaginvoice_id);
		$updateDiagInvOb->setPayment($payment);
		$updateDiagInvOb->setBalance($balance);
		$updateDiagInvOb->setPaymentStatus('paid');
		$updateDiagInvOb->updateDiagInvoiceAfterTransaction();
	}
	
	function executeForDiagRefund()
	{
		if(($this->getPayment()>$this->getAmountPayable())||($this->getPayment()<=0))
		{
			throw new MyException(111139);
		}
		$transact_time = time();
		$acchead_id = $this->getAccountHeadID();
		$account_id = $this->getAccountID();
		$transact_type = $this->getTransactionType();
		$amount_payable = $this->getAmountPayable();
		$payment = $this->getPayment();
		$balance = $amount_payable-$payment;
		$transaction_purpose = $this->getTransactionPurpose();
		$diaginvoice_id = $this->getDiagInvoiceID();
		$patient_id = $this->getPatientID();
		$user_id = $this->getUserID();
		$location_id = $this->getLocationID();
		
		$query = "INSERT INTO transactions 
		(
			transact_time,
			patient_id,
			acchead_id,
			account_id,
			diaginvoice_id,
			transact_type,
			transaction_purpose,
			amount_payable,
			payment,
			balance,
			user_id,
			location_id
		)
		VALUES
		(
			'$transact_time',
			'$patient_id',
			'$acchead_id',
			'$account_id',
			'$diaginvoice_id',
			'$transact_type',
			'$transaction_purpose',
			'$amount_payable',
			'$payment',
			'$balance',
			'$user_id',
			'$location_id'
		)";
		$queryOb = new classQueryGlobal;
		$this->transactionID = $queryOb->check_fetch_query($query,'insert',$server="",$debug_mode="off");
	}
	
	function executeForIpdPatient()
	{
		if(($this->getPayment()>$this->getAmountPayable())||($this->getPayment()<0))
		{
			throw new MyException(111139);
		}
		
		

		$transact_time = time();
		$patient_id = $this->getPatientID();
		$acchead_id = $this->getAccountHeadID();
		$account_id = $this->getAccountID();
		$transact_type = $this->getTransactionType();
		$amount_payable = $this->getAmountPayable();
		$payment = $this->getPayment();
		$balance = $amount_payable-$payment;
		$ipd_bed_allotment_id  = $this->getWardBedAllotID();
		$user_id = $this->getUserID();
		$location_id = $this->getLocationID();
		
		$query = "INSERT INTO transactions 
		(
			transact_time,
			patient_id,
			acchead_id,
			account_id,
			ipd_bed_allotment_id,
			transact_type,
			amount_payable,
			payment,
			balance,
			user_id,
			location_id
		)
		VALUES
		(
			'$transact_time',
			'$patient_id',
			'$acchead_id',
			'$account_id',
			'$ipd_bed_allotment_id',
			'$transact_type',
			'$amount_payable',
			'$payment',
			'$balance',
			'$user_id',
			'$location_id'
			
		)";
		$queryOb = new classQueryGlobal;
		$this->transactionID = $queryOb->check_fetch_query($query,'insert',$server="",$debug_mode="off");
		
		
		
		$updateIpdOb = new classWardMgmt;
		$updateIpdOb->setWardBedAllotID($ipd_bed_allotment_id);
		$updateVsisitOb->setAmountPayable($amount_payable);
		$updateVsisitOb->setPayment($payment);
		$updateVsisitOb->setBalance($balance);
		$updateVsisitOb->setPaymentStatus('paid');
		$updateVsisitOb->updatePayment();
	}
	
	function copyDefaultAccounts($org_id)
	{
		$accounts_array = $this->retrieveDefaultAccounts();
		for($a=0;$a<count($accounts_array);$a++)
		{
			$acchead_id = $accounts_array[$a]['acchead_id'];
			$account_name = $accounts_array[$a]['account_name'];
			$hosp_share = $accounts_array[$a]['hosp_share'];
			$type = $accounts_array[$a]['type'];
			
			$query = "INSERT INTO accounts (acchead_id,account_name,hosp_share,type,org_id) VALUES ('$acchead_id','$account_name','$hosp_share','$type','$org_id')";
			$queryOb = new classQueryGlobal;
			$queryOb->check_fetch_query($query,'insert',$server="",$debug_mode="off");
			
		}
	}
	
	############################# MAIN RETRIEVE FUNCTIONS #####################################
	
	function retrieveAccountHeadByID()
	{
		$acchead_id = $this->getAccountHeadID();
		
		$query = "SELECT * FROM accheads WHERE acchead_id = '$acchead_id'";
		$queryOb = new classQueryGlobal;
		$acchead_array = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
		if(isset($acchead_array[0]))
		{
			return $acchead_array;
		}
	}
	
	function retrieveDefaultAccounts()
	{
		$query = "SELECT * FROM accounts_default";
		$queryOb = new classQueryGlobal;
		$accounts_array = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
		if(isset($accounts_array[0]))
		{
			return $accounts_array;
		}
	}
	
	function retrieveAccountByID()
	{
		$account_id = $this->getAccountID();
		
		$query = "SELECT * FROM accounts WHERE account_id = '$account_id'";
		$queryOb = new classQueryGlobal;
		$accounts_array = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
		if(isset($accounts_array[0]))
		{
			return $accounts_array;
		}
	}
	
	function retrieveAccountHeadAndAccountByID($acchead_id,$account_id)
	{
		
		$query = "SELECT * FROM accheads JOIN accounts ON accheads.acchead_id = accounts.acchead_id WHERE accounts.acchead_id = '$acchead_id' AND accounts.account_id = '$account_id'";
		$queryOb = new classQueryGlobal;
		$accounts_array = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
		if(isset($accounts_array[0]))
		{
			return $accounts_array;
		}
	}
	
	function retrieveTransactionByTransactionID()
	{
		$transact_id = $this->getTransactionID();
		$query = "SELECT * FROM transactions WHERE transact_id = '$transact_id'";
		$queryOb = new classQueryGlobal;
		$transaction_array = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
		if(isset($transaction_array[0]))
		{
			return $transaction_array;
		}
		
	}
	
	function retrieveTransactionByPurpose()
	{
		$transaction_purpose = $this->getTransactionPurpose();
		$location_id = $this->getLocationID();
		$query = "SELECT * FROM transactions WHERE transaction_purpose = '$transaction_purpose' AND location_id = '$location_id' order by transact_time desc";
		$queryOb = new classQueryGlobal;
		$transaction_array = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
		if(isset($transaction_array[0]))
		{
			return $transaction_array;
		}
		
	}
	
	function retrieveTransactionDetailByDateAndPurpose($by_date,$by_purpose,$location_id)
	{
		$query = "SELECT * FROM transactions WHERE transaction_purpose = '$by_purpose' AND location_id = '$location_id'";
		
	
		$startDateArray = explode('-',$by_date);
		$startTime = mktime(0,0,0,$startDateArray[1],$startDateArray[2],$startDateArray[0]);
		
		$endDateArray = explode('-',$by_date);
		$endTime = mktime(23,59,59,$endDateArray[1],$endDateArray[2],$endDateArray[0]);
		
		if($startTime!="")
		{
			if(($endTime!="" && $endTime > $startTime) || $endTime="")
			{
				$query .= " AND transact_time  > $startTime";
			}
		}
		
		if($endTime!="")
		{
			if(($startTime!="" && $startTime < $endTime) || $startTime="")
			{
				$query .= " AND transact_time  <= $endTime";
			}
		}
		
		$query .= " order by transact_time desc";
		
		$queryOb = new classQueryGlobal;
		$transaction_array = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
		if(isset($transaction_array[0]))
		{
			return $transaction_array;
		}
		
	}
	
	function countTransactionByAccountHeadByID()
	{
		$acchead_id = $this->getAccountHeadID();
		$location_id = $this->getLocationID();
		$query = "SELECT count(*) FROM transactions WHERE acchead_id = '$acchead_id' AND transaction_purpose != 'opd_refund' AND location_id = '$location_id' order by transact_time desc ";
		$queryOb = new classQueryGlobal;
		$transaction_array = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
		if(isset($transaction_array[0]['count(*)']))
		{
			return $transaction_array[0]['count(*)'];
		}
		
	}
	
	function retrieveTransactionByAccountHeadByID($limit=NULL)
	{
		$location_id = $this->getLocationID();
		$acchead_id = $this->getAccountHeadID();
		$query = "SELECT * FROM transactions WHERE acchead_id = '$acchead_id' AND transaction_purpose != 'opd_refund' AND location_id = '$location_id' order by transact_time desc limit $limit";
		$queryOb = new classQueryGlobal;
		$transaction_array = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
		if(isset($transaction_array[0]))
		{
			return $transaction_array;
		}
		
	}
	
	function retrieveTransactionDetailByDateAndAccountHeadID($from,$to,$acchead_id,$transaction_type,$location_id)
	{
		$query = "SELECT SUM(payment),accounts.account_id,accounts.account_name FROM accounts LEFT JOIN transactions ON accounts.account_id = transactions.account_id WHERE accounts.acchead_id = '$acchead_id' AND transactions.transact_type = '$transaction_type' AND transactions.location_id = '$location_id'";
		
		$startDateArray = explode('-',$from);
		$startTime = mktime(0,0,0,$startDateArray[1],$startDateArray[2],$startDateArray[0]);
		
		$endDateArray = explode('-',$to);
		$endTime = mktime(23,59,59,$endDateArray[1],$endDateArray[2],$endDateArray[0]);
		
		if($startTime!="")
		{
			if(($endTime!="" && $endTime > $startTime) || $endTime="")
			{
				$query .= " AND transactions.transact_time  > $startTime";
			}
		}
		
		if($endTime!="")
		{
			if(($startTime!="" && $startTime < $endTime) || $startTime="")
			{
				$query .= " AND transactions.transact_time  <= $endTime";
			}
		}
		
		$query .= "  GROUP BY accounts.account_id ORDER by transactions.transact_time desc";
		
		$queryOb = new classQueryGlobal;
		$transaction_array = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="on");
		if(isset($transaction_array[0]))
		{
			return $transaction_array;
		}
		
	}
	
	function retrieveCashReport($from,$to,$acchead_id,$location_id)
	{
		$startDateArray = explode('-',$from);
		$startTime = mktime(0,0,0,$startDateArray[1],$startDateArray[2],$startDateArray[0]);
		
		$endDateArray = explode('-',$to);
		$endTime = mktime(23,59,59,$endDateArray[1],$endDateArray[2],$endDateArray[0]);
		
		$query = "select account_id, debits, credits, credits-debits as balance
		from (SELECT 
		account_id,
		SUM(case when transact_type='Expense' then payment else 0 end) AS debits, 
		SUM(case when transact_type='Income' then payment else 0 end) AS credits
		FROM transactions WHERE acchead_id='$acchead_id' AND location_id = '$location_id' AND transact_time > '$startTime' AND transact_time <= '$endTime'
		GROUP BY account_id ORDER by transact_time desc) a
		where credits-debits<>0 ";
		
		
		$queryOb = new classQueryGlobal;
		$transaction_array = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
		if(isset($transaction_array[0]))
		{
			return $transaction_array;
		}
		
	}
	
	############################# MAIN DELETE FUNCTIONS #####################################
	
	function deleteTransactionByInvoice($invoice_id,$invoice_type)
	{
		$query = "DELETE FROM transactions WHERE $invoice_type = '$invoice_id'";
		$queryOb = new classQueryGlobal;
		$queryOb->check_fetch_query($query,'delete',$server="",$debug_mode="off");
	}
	
}
?>