<?php
include('../config/config.php');
include('../classes/classProduct.php');
include("../classes/classUserRegistration.php");
include('../classes/classReceiveInvoice.php');
include('../classes/classSaleInvoice.php');
include("../classes/classPatient.php");
include("../classes/classImages.php");
include("../classes/classDoctor.php");
include('../classes/classTransaction.php');
include('../classes/classVisits.php');
include('../classes/classProductClosingReconcilation.php');
include('../classes/classDiagnostic.php');
include('../classes/classDiagnosticLiquids.php');
include('../classes/classWardMgmt.php');
include('../classes/classAppointment.php');
include('../classes/classStoreItem.php');
include('../classes/classStoreItemReceiveInvoice.php');
include('../classes/classStoreItemSaleInvoice.php');
include('../classes/classAccounts.php');
include('../classes/classPanel.php');

/*echo '<pre>';
//print_r($_FILES);
print_r($_POST);
echo '</pre>';
exit;
*/

############################# Add And Update User  #################################################################

try
{
	if(isset($_POST['regUser'])&&$_POST['regUser']=="yes")
	{
		$user = new classUserRegistration;
		$user->setUserName($_POST['userName']);
		$user->setPassword($_POST['password']);
		$user->setUserRoleID($_POST['userrole_id']);
		$user->setLocationID($_POST['location_id']);
		$user->setFirstName($_POST['fname']);
		$user->setLastname($_POST['lname']);
		$user->setPhoneNumber($_POST['phone']);
		$user->setEmailAddress($_POST['email']);
		$user->setCreatedBy($_SESSION['security']['user_id']);
		$user->setOrganizationID($_SESSION['security']['org_id']);
		
		################## check doctor select or not ################
		if($_POST['userrole_id']==5)
		{
			$docOb = new classDoctor;
			if(isset($_POST['doctor_id'][0]))
			{
				$docOb->setDoctorID($_POST['doctor_id']);	
			}
			else
			{
				$docOb->setDoctorID('');	
			}
		}
		
		$user->execute();
		
		################## insert doctor assistant relationship ################
		if($_POST['userrole_id']==5)
		{
			$docOb->setAssistantID($user->getUserID());
			$docOb->insertDoctorAssistantRelationship();
		}
		
		if($_POST['userrole_id']==4||$_POST['userrole_id']==2)
		{
			$docOb = new classDoctor;
			$docOb->setCreatedBy($_SESSION['security']['user_id']);
			$docOb->setUserID($user->getUserID());
			$docOb->setFirstName($_POST['fname']);
			$docOb->setLastName($_POST['lname']);
			$docOb->setPhoneNumber($_POST['phone']);
			$docOb->setEmailAddress($_POST['email']);
			$docOb->setFee("1500");
			$docOb->setHospitalShare("0");
			$docOb->setLocationID($_POST['location_id']);
			$docOb->insertDoctorBasicInfo();
		}
		
		
		
		$redirect_url = 'mgmtuser.php';
		die("$redirect_url");
	}
	
	if(isset($_POST['updateUser'])&&$_POST['updateUser']=="yes")
	{
		$user = new classUserRegistration;
		$user->setUserName($_POST['userName']);
		//$user->setPassword($_POST['password']);
		$user->setUserRoleID($_POST['userrole_id']);
		$user->setLocationID($_POST['location_id']);
		$user->setFirstName($_POST['fname']);
		$user->setLastname($_POST['lname']);
		$user->setPhoneNumber($_POST['phone']);
		$user->setEmailAddress($_POST['email']);
		$user->setCreatedBy($_SESSION['security']['user_id']);
		$user->setOrganizationID($_SESSION['security']['org_id']);
		$user->setUserID($_POST['user_id']);
		$user->updateUser();
		
		################## check doctor select or not ################
		if($_POST['userrole_id']==5)
		{
			$docOb = new classDoctor;
			if(isset($_POST['doctor_id'][0]))
			{
				$docOb->setDoctorID($_POST['doctor_id']);	
			}
			else
			{
				$docOb->setDoctorID('');	
			}
			
			$docOb->setAssistantID($_POST['user_id']);
			$docOb->insertDoctorAssistantRelationship();
		}
		
		$redirect_url = 'mgmtuser.php';
		die("$redirect_url");
	}
	
	if(isset($_POST['updateUserProfile'])&&$_POST['updateUserProfile']=="yes")
	{
		$userProfileOb = new classUserRegistration;
		$userProfileOb->setFirstName($_POST['fname']);
		$userProfileOb->setLastname($_POST['lname']);
		$userProfileOb->setPhoneNumber($_POST['phone']);
		$userProfileOb->setEmailAddress($_POST['email']);
		$userProfileOb->setUserID($_POST['user_id']);
		$userProfileOb->updateUserProfile();
		
		$redirect_url = 'profile.php';
		die("$redirect_url");
	}
	
	if(isset($_POST['updateUserPwd'])&&$_POST['updateUserPwd']=="yes")
	{
		$userPwdOb = new classUserRegistration;
		$userPwdOb->setPassword($_POST['password']);
		$userPwdOb->setUserID($_POST['user_id']);
		$userPwdOb->updateUserPwd();
		$redirect_url = $_SERVER['HTTP_REFERER'];
		
		die("$redirect_url");
	}
	
}
catch (MyException $e)
{
	$error_message = $e->getUserMsg();
	die("$error_message");
}			

############################# Add Update Doctor #################################################################

if(isset($_POST['regDoctor'])&&$_POST['regDoctor']=='yes')
{
	try
	{
		
		$docRegOb = new classDoctor;
		
		$docRegOb->setCreatedBy($_SESSION['security']['user_id']);
		$docRegOb->setUserID("0");
		$docRegOb->setLocationID($_SESSION['security']['location_id']);
		
		$docRegOb->setJoinDate(strtotime($_POST['join_date']));
		$docRegOb->setTitle($_POST['title']);
		$docRegOb->setFirstName($_POST['fname']);
		$docRegOb->setLastName($_POST['lname']);
		$docRegOb->setDOB($_POST['dob']);
		$docRegOb->setGender($_POST['gender']);
		$docRegOb->setNIC($_POST['cnic']);
		$docRegOb->setAddress($_POST['address']);
		$docRegOb->setCity($_POST['city']);
		$docRegOb->setPhoneNumber($_POST['phone']);
		$docRegOb->setEmailAddress($_POST['email']);
		
		$docRegOb->setSpecialization($_POST['specialization']);
		$docRegOb->setDegree($_POST['degree']);
		$docRegOb->setGraduationYear($_POST['grad_year']);
		$docRegOb->setLicenseNumber($_POST['license_no']);
		
		$docRegOb->setTimings($_POST['timings']);
		$docRegOb->setFee($_POST['fee']);
		$docRegOb->setHospitalShare($_POST['hosp_share']);
		
		$docRegOb->execute();
		
		$redirect_url = 'mgmtdoctor.php';
		die("$redirect_url");
	}
	catch (MyException $e)
	{
		$error_message = $e->getUserMsg();
		die("$error_message");
	}
}

if(isset($_POST['updateDoctor'])&&$_POST['updateDoctor']=='yes')
{
	try
	{
		
		$docRegOb = new classDoctor;
		
		$docRegOb->setCreatedBy($_SESSION['security']['user_id']);
		$docRegOb->setUserID("0");
		$docRegOb->setLocationID($_SESSION['security']['location_id']);
		
		$docRegOb->setJoinDate(strtotime($_POST['join_date']));
		$docRegOb->setTitle($_POST['title']);
		$docRegOb->setFirstName($_POST['fname']);
		$docRegOb->setLastName($_POST['lname']);
		$docRegOb->setDOB($_POST['dob']);
		$docRegOb->setGender($_POST['gender']);
		$docRegOb->setNIC($_POST['cnic']);
		$docRegOb->setAddress($_POST['address']);
		$docRegOb->setCity($_POST['city']);
		$docRegOb->setPhoneNumber($_POST['phone']);
		$docRegOb->setEmailAddress($_POST['email']);
		
		$docRegOb->setSpecialization($_POST['specialization']);
		$docRegOb->setDegree($_POST['degree']);
		$docRegOb->setGraduationYear($_POST['grad_year']);
		$docRegOb->setLicenseNumber($_POST['license_no']);
		
		$docRegOb->setTimings($_POST['timings']);
		$docRegOb->setFee($_POST['fee']);
		$docRegOb->setHospitalShare($_POST['hosp_share']);
		
		$docRegOb->setDoctorID($_POST['doctor_id']);
		
		$docRegOb->updateDoctor();
		
		$redirect_url = 'mgmtdoctor.php';
		die("$redirect_url");
	}
	catch (MyException $e)
	{
		$error_message = $e->getUserMsg();
		die("$error_message");
	}
}


############################# Add Manufacturer ##########################################################

try
{
	if(isset($_POST['add_manf'])&&$_POST['add_manf']=='Add_Manufacturer')
	{
		$addManfOb = new classProduct;	
		$addManfOb->setManufacturerName($_POST['comp_name']);
		$addManfOb->setManufacturerPhone($_POST['phone']);
		$addManfOb->setManufacturerAddress($_POST['address']);
		$addManfOb->setOrganizationID($_SESSION['security']['org_id']);
		$addManfOb->insertManufacturer();
		
		$redirect_url = 'addprod.php';
		die("$redirect_url");
	}
	
	if(isset($_POST['add_manf'])&&$_POST['add_manf']=='Add_Manufacturer_store')
	{
		$addManfOb = new classProduct;	
		$addManfOb->setManufacturerName($_POST['comp_name']);
		$addManfOb->setManufacturerPhone($_POST['phone']);
		$addManfOb->setManufacturerAddress($_POST['address']);
		$addManfOb->setOrganizationID($_SESSION['security']['org_id']);
		$addManfOb->insertManufacturer();
		
		$redirect_url = 'addstoreitem.php';
		die("$redirect_url");
	}
	
	if(isset($_POST['update_manf'])&&$_POST['update_manf']=='Update_Manufacturer')
	{
		$updateManfOb = new classProduct;	
		$updateManfOb->setManufacturerName($_POST['comp_name']);
		$updateManfOb->setManufacturerPhone($_POST['phone']);
		$updateManfOb->setManufacturerAddress($_POST['address']);
		$updateManfOb->setManufacturerID($_POST['manf_id']);
		$updateManfOb->updateManufacturer();
		
		$redirect_url = 'addprod.php';
		die("$redirect_url");
	}
}
catch (MyException $e)
{
	$error_message = $e->getUserMsg();
	die("$error_message");
}	

############################# Add Supplier ##########################################################

try
{
	if(isset($_POST['add_supp'])&&$_POST['add_supp']=='Add_Supplier')
	{
		$addSuppOb = new classProduct;
		$addSuppOb->setSupplierName($_POST['supp_name']);	
		$addSuppOb->setSupplierPhone($_POST['supp_phone']);
		$addSuppOb->setSupplierCompanyName($_POST['comp_name']);
		$addSuppOb->setSupplierCompanyPhone($_POST['comp_phone']);
		$addSuppOb->setSupplierCompanyAddress($_POST['comp_address']);
		$addSuppOb->setLocationID($_SESSION['security']['location_id']);
		$addSuppOb->insertSupplier();
		
		$redirect_url = 'addprod.php';
		die("$redirect_url");
	}
	if(isset($_POST['update_supp'])&&$_POST['update_supp']=='Update_Supplier')
	{
		$updateSuppOb = new classProduct;
		$updateSuppOb->setSupplierName($_POST['supp_name']);	
		$updateSuppOb->setSupplierPhone($_POST['supp_phone']);
		$updateSuppOb->setSupplierCompanyName($_POST['comp_name']);
		$updateSuppOb->setSupplierCompanyPhone($_POST['comp_phone']);
		$updateSuppOb->setSupplierCompanyAddress($_POST['supp_address']);
		$updateSuppOb->setSupplierID($_POST['supplier_id']);
		$updateSuppOb->updateSupplier();
		
		$redirect_url = 'mgmtsupplier.php';
		die("$redirect_url");
	}
}
catch (MyException $e)
{
	$error_message = $e->getUserMsg();
	die("$error_message");
}	

############################# Add Product ###########################################################

try
{
	if(isset($_POST['add_prod'])&&$_POST['add_prod']=='Add_Product')
	{
		$addProdOb = new classProduct;
		
		$addProdOb->setProductName($_POST['prod_name']);	
		$addProdOb->setManufacturerID($_POST['manf_id']);
		$addProdOb->setProductCategory($_POST['prod_category']);
		$addProdOb->setProductType($_POST['medicine_type']);
		//$addProdOb->setProductGroup($_POST['medicine_group']);
		$addProdOb->setProductReorderQty($_POST['prod_reorder_qty']);
		
		$addProdOb->setProductBoxQty($_POST['box_qty']);
		$addProdOb->setProductLeafPerBoxQty($_POST['leaf_qty_box']);
		$addProdOb->setProductUnitPerBoxQty($_POST['unit_qty_box']);
		
		$addProdOb->setProductBoxRetailPrice($_POST['retail_price_box']);
		$addProdOb->setProductLeafRetailPrice($_POST['retail_price_leaf']);
		$addProdOb->setProductUnitRetailPrice($_POST['retail_price_unit']);
		
		$addProdOb->setActive("1");
		
		$addProdOb->setLocationID($_SESSION['security']['location_id']);
		$addProdOb->setOrganizationID($_SESSION['security']['org_id']);
		
		
		if(isset($_POST['product_id'])&&$_POST['product_id']!='')
		{
			$addProdOb->setProductID($_POST['product_id']);
			$addProdOb->setSupplierID(@$_POST['supplier_ids']);
			$addProdOb->setProductBoxTradePrice($_POST['trade_price_box']);
			$addProdOb->setProductLeafTradePrice($_POST['trade_price_leaf']);
			$addProdOb->setProductUnitTradePrice($_POST['trade_price_unit']);
			
			$addProdOb->updateProduct();
		}
		else
		{
			$addProdOb->setSupplierID($_POST['supplier_id']);
			$addProdOb->setProductBoxTradePrice("0");
			$addProdOb->setProductLeafTradePrice("0");
			$addProdOb->setProductUnitTradePrice("0");
			
			$addProdOb->insertProduct();
		}
		
		$_SESSION['temp']['prod_id'] = $addProdOb->getProductID();
		
		$redirect_url = 'addprod.php';
		die("$redirect_url");
		
	}
}
catch (MyException $e)
{
	$error_message = $e->getUserMsg();
	die("$error_message");
}

############################# Add Organization ###########################################################	

try
{
	if(isset($_POST['add_org'])&&$_POST['add_org']=='Add_organization')
	{
		$insertOrgOB = new classUser;
		$insertOrgOB->setOrganizationName($_POST['org_name']);
		
		$insertOrgOB->setNoOfLocations($_POST['no_of_locations']);
		
		
		if(isset($_POST['redirect_url']))
		{
			$insertLocOB = new classUser;
			$insertLocOB->setLocationName($_POST['location_name']);
			
			$insertOrgOB->insertOrganization($_POST);
			/*$accOb = new classTransaction;
			$accOb->copyDefaultAccounts($insertOrgOB->getOrganizationID());*/
			
			$insertLocOB->setOrganizationID($insertOrgOB->getOrganizationID());
			$insertLocOB->setLocCity('');
			$insertLocOB->setLocAddress('');
			$insertLocOB->setLocPhone('');
			$insertLocOB->setLocContactPerson('');
			$insertLocOB->setPatientRegFee('');
			$insertLocOB->insertLocation();
			
			
			$redirect_url = $_POST['redirect_url'];
			
		}
		else
		{
			$insertOrgOB->insertOrganization($_POST);
			/*$accOb = new classTransaction;
			$accOb->copyDefaultAccounts($insertOrgOB->getOrganizationID());*/
			
			$redirect_url = 'mgmtorg.php';	
		}
		
		die("$redirect_url");
	}
	
	if(isset($_POST['update_org'])&&$_POST['update_org']=='Update_organization')
	{
		$insertOrgOB = new classUser;
		$insertOrgOB->setOrganizationName($_POST['org_name']);
		$insertOrgOB->setOrganizationID($_POST['org_id']);
		$insertOrgOB->setNoOfLocations($_POST['no_of_locations']);
		$insertOrgOB->updateOrganizationInfo($_POST);
		
		$redirect_url = 'mgmtorg.php';
		die("$redirect_url");
	}
}
catch (MyException $e)
{
	$error_message = $e->getUserMsg();
	die("$error_message");
}	

############################# Add Location ###########################################################	

try
{
	if(isset($_POST['add_loc'])&&$_POST['add_loc']=='Add_location')
	{
		$insertLocOB = new classUser;
		$insertLocOB->setLocationName($_POST['location_name']);
		$insertLocOB->setOrganizationID($_POST['org_id']);
		$insertLocOB->setOrganizationName($_POST['org_name']);
		$insertLocOB->setLocCity($_POST['city']);
		$insertLocOB->setLocAddress($_POST['address']);
		$insertLocOB->setLocPhone($_POST['phone']);
		$insertLocOB->setLocContactPerson($_POST['contact_person']);
		$insertLocOB->setPatientRegFee($_POST['patient_reg_fee']);
		
		$insertLocOB->insertLocation();
		$insertLocOB->updateOrganization();
		
		$redirect_url = 'mgmtlocation.php';
		die("$redirect_url");
	}
	
	if(isset($_POST['update_loc'])&&$_POST['update_loc']=='Update_location')
	{
		$insertLocOB = new classUser;
		$insertLocOB->setLocationName($_POST['location_name']);
		$insertLocOB->setLocationID($_POST['location_id']);
		$insertLocOB->setLocCity($_POST['city']);
		$insertLocOB->setLocAddress($_POST['address']);
		$insertLocOB->setLocPhone($_POST['phone']);
		$insertLocOB->setLocContactPerson($_POST['contact_person']);
		$insertLocOB->setPatientRegFee($_POST['patient_reg_fee']);
		$insertLocOB->updateLocation();
		
		$redirect_url = 'mgmtlocation.php';
		die("$redirect_url");
	}
}
catch (MyException $e)
{
	$error_message = $e->getUserMsg();
	die("$error_message");
}	

############################# Calculate Receive Item Detail ###########################################	

if(isset($_GET['receive_items'])&&isset($_GET['packing_type'])&&isset($_GET['prod_id']))
{
	try
	{
		
		$recitems_id = $_GET['recitems_id'];
		$packing_type = $_GET['packing_type'];
		
		if(isset($_GET['packing_qty'])&&$_GET['packing_qty']!='')
		{
			$packing_qty = $_GET['packing_qty'];
		}
		else
		{
			$packing_qty = "0";
		}
		$discount = $_GET['discount'];
		$expiry_date = $_GET['expiry_date'];
		
		$updateRecItemsOb = new classReceiveInvoice;
		$updateRecItemsOb->setPackingType($packing_type);
		$updateRecItemsOb->setReceivedPackingQty($packing_qty);
		$updateRecItemsOb->setDiscount($discount);
		$updateRecItemsOb->setReceiveItemID($recitems_id);
		$updateRecItemsOb->setProductExpiryDate($expiry_date);
		$updateRecItemsOb->setProductID($_GET['prod_id']);
		$updateRecItemsOb->setSupplierID($_SESSION['temp']['supplier_id']);
		$updateRecItemsOb->setReceiveInvoiceID($_SESSION['temp']['invoice_id']);
		$updateRecItemsOb->setLocationID($_SESSION['security']['location_id']);
		
		
		$updateRecItemsOb->updateReceiveItemsInInvoice();
		
		$retrieveInvoiceItemOb = new classReceiveInvoice;
		$retrieveInvoiceItemOb->setReceiveItemID($recitems_id);
		$invoice_items_array = $retrieveInvoiceItemOb->retrieveInvoiceItemsDetailByItemID();
		
		$retrieveInvoiceOb = new classReceiveInvoice;
		$retrieveInvoiceOb->setReceiveInvoiceID($_SESSION['temp']['invoice_id']);
		$invoice_array = $retrieveInvoiceOb->retrieveInvoiceDetail();
		
		$invoice_amount = $invoice_array[0]['invoice_amount'];
		$amount_payable = $invoice_array[0]['amount_payable'];
		
		$trade_price = $invoice_items_array[0]['trade_price'];
		$qty_received = $invoice_items_array[0]['qty_received'];
		$unitqty_recieved = $invoice_items_array[0]['unitqty_recieved'];
		$total = $total = $trade_price*$qty_received;
		$net_total = $invoice_items_array[0]['net_total'];
		$expiry_date = $invoice_items_array[0]['expiry_date'];
		
		
		die("$trade_price&$total&$net_total&$expiry_date&$invoice_amount&$amount_payable");
	}
	catch (MyException $e)
	{
		$error_message = $e->getUserMsg();
		die("$error_message");
	}	
	
}

############################# UPDATE RECEIVE INVOICE DISCOUNT ###########################################	

if(isset($_GET['invoice_discount']))
{
	try
	{
		$invoice_discount = $_GET['invoice_discount'];
		$updateInvDiscountOb = new classReceiveInvoice;
		$updateInvDiscountOb->setReceiveInvoiceID($_SESSION['temp']['invoice_id']);
		$updateInvDiscountOb->setPayment($updateInvDiscountOb->retrieveInvoiceItemsTotal());
		$updateInvDiscountOb->setDiscount($invoice_discount);
		$updateInvDiscountOb->updateReceiveInvoice();
	}
	catch (MyException $e)
	{
		$error_message = $e->getUserMsg();
		die("$error_message");
	}	
}

############################# Add Patient #################################################################

if(isset($_POST['save_patient'])&&$_POST['save_patient']=='save_patient')
{
	try
	{
		$patientRegistrationOb = new classPatient;
		$patientRegistrationOb->setRegDate(strtotime($_POST['reg_date']));
		$patientRegistrationOb->setPatientID($_POST['patient_id']);
		$patientRegistrationOb->setLocationID($_SESSION['security']['location_id']);
		$patientRegistrationOb->setFirstName($_POST['patient_fname']);	
		$patientRegistrationOb->setLastName($_POST['patient_lname']);
		$patientRegistrationOb->setDOB($_POST['dob']);
		$patientRegistrationOb->setGender($_POST['gender']);
		$patientRegistrationOb->setNIC($_POST['nic']);
		$patientRegistrationOb->setAddress($_POST['address']);
		$patientRegistrationOb->setArea(@$_POST['area']);
		$patientRegistrationOb->setCity($_POST['city']);
		$patientRegistrationOb->setPhoneNumber($_POST['phone']);
		$patientRegistrationOb->setEmail(@$_POST['email']);
		
		$patientRegistrationOb->setAttendentName($_POST['parent_name']);
		$patientRegistrationOb->setAttendentPhoneNumber($_POST['parent_phone']);
		$patientRegistrationOb->setAttendentNIC(@$_POST['parent_nic']);
		
		$patientRegistrationOb->setBroughtToByName($_POST['emer_name']);
		$patientRegistrationOb->setBroughtToByPhoneNumber($_POST['emer_phone']);
		$patientRegistrationOb->setBroughtToByNIC($_POST['emer_nic']);
		
		$patientRegistrationOb->setReferringDoctor($_POST['ref_doctor']);
		$patientRegistrationOb->setBloodGroup($_POST['blood_group']);
		$patientRegistrationOb->setMedicalHistory($_POST['med_history']);
		$patientRegistrationOb->setFamilyHistory(@$_POST['fam_history']);
		$patientRegistrationOb->setSocialHistory(@$_POST['soc_history']);
		$patientRegistrationOb->setAllergies($_POST['allergies']);
		$patientRegistrationOb->setFeePerSession($_POST['fee_per_session']);
		
		$patientRegistrationOb->setPanelID($_POST['panel_id']);
		
		$patientRegistrationOb->executeHireStaff();
		
		if(isset($_SESSION['temp']['image_id']))
		{
			$imageOb = new classImages;
			$imageOb->setPatientAutoID($patientRegistrationOb->getPatientAutoID());
			$imageOb->setImageID($_POST['image_id']);
			$imageOb->updatePatientAutoID();
			unset($_SESSION['temp']['image_id']);
			
		}
		
		$redirect_url = 'mgmtpatients.php';
		die("$redirect_url");
	}
	catch (MyException $e)
	{
		$error_message = $e->getUserMsg();
		die("$error_message");
	}
}

if(isset($_POST['update_patient'])&&$_POST['update_patient']=='update_patient')
{
	try
	{
		$patientRegistrationOb = new classPatient;
		$patientRegistrationOb->setRegDate(strtotime($_POST['reg_date']));
		$patientRegistrationOb->setPatientAutoID($_POST['patient_auto_id']);
		$patientRegistrationOb->setPatientID($_POST['patient_id']);
		$patientRegistrationOb->setLocationID($_SESSION['security']['location_id']);
		$patientRegistrationOb->setFirstName($_POST['patient_fname']);	
		$patientRegistrationOb->setLastName($_POST['patient_lname']);
		$patientRegistrationOb->setDOB($_POST['dob']);
		$patientRegistrationOb->setGender($_POST['gender']);
		$patientRegistrationOb->setNIC($_POST['nic']);
		$patientRegistrationOb->setAddress($_POST['address']);
		$patientRegistrationOb->setArea(@$_POST['area']);
		$patientRegistrationOb->setCity($_POST['city']);
		$patientRegistrationOb->setPhoneNumber($_POST['phone']);
		$patientRegistrationOb->setEmail(@$_POST['email']);
		
		
		$patientRegistrationOb->setAttendentName($_POST['parent_name']);
		$patientRegistrationOb->setAttendentPhoneNumber($_POST['parent_phone']);
		$patientRegistrationOb->setAttendentNIC(@$_POST['parent_nic']);
		
		$patientRegistrationOb->setBroughtToByName($_POST['emer_name']);
		$patientRegistrationOb->setBroughtToByPhoneNumber($_POST['emer_phone']);
		$patientRegistrationOb->setBroughtToByNIC($_POST['emer_nic']);
		
		$patientRegistrationOb->setReferringDoctor($_POST['ref_doctor']);
		$patientRegistrationOb->setBloodGroup($_POST['blood_group']);
		$patientRegistrationOb->setMedicalHistory($_POST['med_history']);
		$patientRegistrationOb->setFamilyHistory(@$_POST['fam_history']);
		$patientRegistrationOb->setSocialHistory(@$_POST['soc_history']);
		$patientRegistrationOb->setAllergies($_POST['allergies']);
		$patientRegistrationOb->setFeePerSession($_POST['fee_per_session']);
		
		$patientRegistrationOb->setPanelID($_POST['panel_id']);
		
		$patientRegistrationOb->updatePatient();
		
		if(isset($_SESSION['temp']['image_id']))
		{
			
			/*$delimageOb = new classImages;
			$delimageOb->setPatientAutoID($patientRegistrationOb->getPatientAutoID());
			$delimageOb->deleteImages();*/
			
			$imageOb = new classImages;
			$imageOb->setPatientAutoID($patientRegistrationOb->getPatientAutoID());
			$imageOb->setImageID($_POST['image_id']);
			$imageOb->updatePatientAutoID();
			
			unset($_SESSION['temp']['image_id']);
		
		}
		$redirect_url = 'mgmtpatients.php';
		die("$redirect_url");
	}
	catch (MyException $e)
	{
		$error_message = $e->getUserMsg();
		die("$error_message");
	}
}

if(isset($_POST['add_patient_basic'])&&$_POST['add_patient_basic']=='yes')
{
	try
	{
		$patientRegBasicOb = new classPatient;
		$patientRegBasicOb->setLocationID($_SESSION['security']['location_id']);
		$patientRegBasicOb->setFirstName($_POST['patient_fname']);	
		$patientRegBasicOb->setLastName($_POST['patient_lname']);
		$patientRegBasicOb->setGender($_POST['gender']);
		$patientRegBasicOb->setAge($_POST['age']);
		$patientRegBasicOb->setAddress($_POST['address']);
		$patientRegBasicOb->setPhoneNumber($_POST['phone']);
		$patientRegBasicOb->setRelationShipName($_POST['relationship_name']);
		$patientRegBasicOb->setFeePerSession($_POST['fee_per_session']);
		$patientRegBasicOb->executeBasicInfo();
		if(isset($_POST['redirect_url'])&&$_POST['redirect_url']=='mgmtappoint')
		{
			$p_name = ucwords($_POST['patient_fname'].' '.$_POST['patient_lname']);
			$p_id = $patientRegBasicOb->getPatientAutoID();
			
			$redirect_url = $p_name.'&'.$p_id;
		}
		else
		{
			$redirect_url = 'diagtestinv.php?patient_type='.$patientRegBasicOb->getPatientAutoID().'&reffered_by='.$_POST['reffered_by'];
			
		}
		
		die("$redirect_url");
		
	}
	catch (MyException $e)
	{
		$error_message = $e->getUserMsg();
		die("$error_message");
	}
}


############################# Calculate Sale Item Detail ###########################################	

if(isset($_GET['sale_items'])&&isset($_GET['packing_type'])&&isset($_GET['prod_id']))
{
	
	try
	{
		
		$saleitems_id = $_GET['saleitems_id'];
		$packing_type = $_GET['packing_type'];
		
		if(isset($_GET['packing_qty'])&&$_GET['packing_qty']!='')
		{
			$packing_qty = $_GET['packing_qty'];
		}
		else
		{
			$packing_qty = "0";
		}
		$discount = $_GET['discount'];
		$expiry_date = "0";
		
		$updateSaleItemsOb = new classSaleInvoice;
		$updateSaleItemsOb->setPackingType($packing_type);
		$updateSaleItemsOb->setSalePackingQty($packing_qty);
		$updateSaleItemsOb->setDiscount($discount);
		$updateSaleItemsOb->setSaleItemID($saleitems_id);
		$updateSaleItemsOb->setProductExpiryDate($expiry_date);
		$updateSaleItemsOb->setProductID($_GET['prod_id']);
		$updateSaleItemsOb->setPatientID($_SESSION['temp']['patient_id']);
		$updateSaleItemsOb->setSaleInvoiceID($_SESSION['temp']['invoice_id']);
		$updateSaleItemsOb->setLocationID($_SESSION['security']['location_id']);
		
		
		$updateSaleItemsOb->updateSaleItemsInInvoice();
		
		$retrieveInvoiceItemOb = new classSaleInvoice;
		$retrieveInvoiceItemOb->setSaleItemID($saleitems_id);
		$invoice_items_array = $retrieveInvoiceItemOb->retrieveInvoiceItemsDetailByItemID();
		
		$retrieveInvoiceOb = new classSaleInvoice;
		$retrieveInvoiceOb->setSaleInvoiceID($_SESSION['temp']['invoice_id']);
		$invoice_array = $retrieveInvoiceOb->retrieveInvoiceDetail();
		
		$invoice_amount = $invoice_array[0]['invoice_amount'];
		$amount_payable = $invoice_array[0]['amount_payable'];
		
		$retail_price = $invoice_items_array[0]['retail_price'];
		
		$qty = $invoice_items_array[0]['qty'];
		$unitsaleqty = $invoice_items_array[0]['unitsaleqty'];
		
		$total = $retail_price*$qty;
		$net_total = $invoice_items_array[0]['net_total'];
		$expiry_date = '';
		
		
		die("$retail_price&$total&$net_total&$expiry_date&$invoice_amount&$amount_payable");
	}
	catch (MyException $e)
	{
		$error_message = $e->getUserMsg();
		die("$error_message");
	}	
	
}

############################# UPDATE Sale INVOICE DISCOUNT ###########################################	

if(isset($_GET['sale_invoice_discount']))
{
	try
	{
		$invoice_discount = $_GET['sale_invoice_discount'];
		$updateInvDiscountOb = new classSaleInvoice;
		$updateInvDiscountOb->setSaleInvoiceID($_SESSION['temp']['invoice_id']);
		$updateInvDiscountOb->setPayment($updateInvDiscountOb->retrieveInvoiceItemsTotal());
		$updateInvDiscountOb->setDiscount($invoice_discount);
		$updateInvDiscountOb->updateSaleInvoice();
	}
	catch (MyException $e)
	{
		$error_message = $e->getUserMsg();
		die("$error_message");
	}	
}

################## Retrieve all Patients  ####################################

if(isset($_POST['search_patient'])||isset($_GET['view_all_patients']))
{
	try
	{
		if(isset($_POST['search_patient'])&&$_POST['search_patient']=='Search')
		{
			$retrievePatientOb = new classPatient;
			$patients_array = $retrievePatientOb->searchPatient($_POST['by_id'],$_POST['by_name'],$_POST['by_phone'],$_SESSION['security']['location_id']);
		}
		elseif(isset($_GET['view_all_patients'])&&$_GET['view_all_patients']=='yes')
		{
			$retrievePatientOb = new classPatient;
			$retrievePatientOb->setLocationID($_SESSION['security']['location_id']);
			$patients_array = $retrievePatientOb->retrievePatientByLocationID();
		}
		
		if(isset($patients_array[0]['patient_auto_id'])&&$patients_array[0]['patient_auto_id']!='')
		{
			for($p=0;$p<count($patients_array);$p++)
			{
				if($p%2==0){ $class = 'gradeX';}else{$class = 'gradeC';}
				
				$pid = $patients_array[$p]['patient_auto_id'];
				
				$imageRetrieveOb = new classImages;
				$imageRetrieveOb->setPatientAutoID($pid);
				$image_id = $imageRetrieveOb->fetchImageOfPatient();
				
				if((isset($_POST['refferer'])&&$_POST['refferer']=='mgmtbeds')||(isset($_GET['refferer'])&&$_GET['refferer']=='mgmtbeds'))
				{
					$onsubmit = 'false';
					$class_of_button = 'select_patient btn btn-primary';
				}
				else
				{
					$onsubmit = 'true';
					$class_of_button = 'btn btn-primary';
				}
				?>
				
				<tr class="<?php echo $class;?>">
				
					<td align="center">
					<form action="" method="post" onsubmit="return <?php echo $onsubmit;?>">
						<input type="submit" name="select_patient" value="Select" id="<?php echo $p;?>" class="<?php echo $class_of_button;?>">
						<input type="hidden" name="patient_type" id="patient_type_<?php echo $p;?>" value="<?php echo $pid;?>">
						<input type="hidden" name="patient_name" id="patient_name_<?php echo $p;?>" value="<?php echo $patients_array[$p]['patient_fname'].' '.$patients_array[$p]['patient_lname'];?>">
					</form>
					</td>
					
					<td align="center">
					<?php
					if(isset($image_id)&&$image_id!='')
					{
						?>
						<img src="../images/patient_images/<?php echo $image_id;?>.jpg" width="30" height="30">
						<?php
					}
					else
					{
						?>
						<img src="../images/photo.png" width="30" height="30">
						<?php
					}
					?>
					</td>
					<td align="center"><?php echo $patients_array[$p]['patient_id'];?></td>
					<td align="center"><?php echo  date('d M Y',strtotime($patients_array[$p]['dob']));?></td>
					<td align="left"><?php echo $patients_array[$p]['patient_fname'].' '.$patients_array[$p]['patient_lname'];?></td>
					<td align="center"><?php echo $patients_array[$p]['phone'];?></td>
					<td align="center"><?php echo date('d M Y',$patients_array[$p]['reg_date']);?></td>
				   
				</tr>
				
				<?php
			}
		}
		else
		{
			?>
			 <tr><td colspan="7">No Entry Found!</tr>
             <?php
		}
	}
	catch (MyException $e)
	{
		$error_message = $e->getUserMsg();
		die("$error_message");
	}
}
	
################## Make Transaction  ####################################

if(isset($_POST['trx_incoming_prod'])&&$_POST['trx_incoming_prod']=='yes')
{
	try
	{
		$trxIncOb = new classTransaction;
		$trxIncOb->setAccountHeadID($_POST['acchead_id']);
		$trxIncOb->setAccountID($_POST['account_id']);
		$trxIncOb->setTransactionType($_POST['type']);
		$trxIncOb->setAmountPayable($_POST['amount_payable']);
		$trxIncOb->setPayment($_POST['payment_amount']);
		$trxIncOb->setDescription($_POST['description']);
		$trxIncOb->setSupplierID($_POST['supplier_id']);
		$trxIncOb->setReceiveInvoiceID($_POST['recinvoice_id']);
		$trxIncOb->setUserID($_SESSION['security']['user_id']);
		$trxIncOb->setLocationID($_SESSION['security']['location_id']);
		$trxIncOb->executeForIncoming();
		
		$redirect_url = 'mgmtincoming.php';
		die("$redirect_url");
	}
	catch (MyException $e)
	{
		$error_message = $e->getUserMsg();
		die("$error_message");
	}
	
}

if(isset($_POST['trx_sales_prod'])&&$_POST['trx_sales_prod']=='yes')
{
	try
	{
		$trxIncOb = new classTransaction;
		$trxIncOb->setAccountHeadID($_POST['acchead_id']);
		$trxIncOb->setAccountID($_POST['account_id']);
		$trxIncOb->setTransactionType($_POST['type']);
		$trxIncOb->setAmountPayable($_POST['amount_payable']);
		$trxIncOb->setPayment($_POST['payment_amount']);
		$trxIncOb->setDescription($_POST['description']);
		$trxIncOb->setPatientID($_POST['patient_id']);
		$trxIncOb->setSaleInvoiceID($_POST['saleinvoice_id']);
		$trxIncOb->setUserID($_SESSION['security']['user_id']);
		$trxIncOb->setLocationID($_SESSION['security']['location_id']);
		$trxIncOb->executeForSales();
		
		$redirect_url = 'mgmtsales.php';
		die("$redirect_url");
	}
	catch (MyException $e)
	{
		$error_message = $e->getUserMsg();
		die("$error_message");
	}
	
}

if(isset($_POST['trx_incoming_balance'])&&$_POST['trx_incoming_balance']=='yes')
{
	try
	{
		$trxIncOb = new classTransaction;
		$trxIncOb->setAccountHeadID($_POST['acchead_id']);
		$trxIncOb->setAccountID($_POST['account_id']);
		$trxIncOb->setTransactionType($_POST['type']);
		$trxIncOb->setTransactionPurpose('balance');
		$trxIncOb->setAmountPayable($_POST['amount_payable']);
		$trxIncOb->setPayment($_POST['payment_amount']);
		$trxIncOb->setDescription($_POST['description']);
		$trxIncOb->setSupplierID($_POST['supplier_id']);
		$trxIncOb->setReceiveInvoiceID($_POST['recinvoice_id']);
		$trxIncOb->setUserID($_SESSION['security']['user_id']);
		$trxIncOb->setLocationID($_SESSION['security']['location_id']);
		$trxIncOb->executeForIncomingBalance();
		
		$redirect_url = $_SERVER['HTTP_REFERER'];
		die("$redirect_url");
	}
	catch (MyException $e)
	{
		$error_message = $e->getUserMsg();
		die("$error_message");
	}
	
}

if(isset($_POST['trx_sales_balance'])&&$_POST['trx_sales_balance']=='yes')
{
	try
	{
		$trxIncOb = new classTransaction;
		$trxIncOb->setAccountHeadID($_POST['acchead_id']);
		$trxIncOb->setAccountID($_POST['account_id']);
		$trxIncOb->setTransactionType($_POST['type']);
		$trxIncOb->setTransactionPurpose('balance');
		$trxIncOb->setAmountPayable($_POST['amount_payable']);
		$trxIncOb->setPayment($_POST['payment_amount']);
		$trxIncOb->setDescription($_POST['description']);
		$trxIncOb->setPatientID($_POST['patient_id']);
		$trxIncOb->setSaleInvoiceID($_POST['saleinvoice_id']);
		$trxIncOb->setUserID($_SESSION['security']['user_id']);
		$trxIncOb->setLocationID($_SESSION['security']['location_id']);
		$trxIncOb->executeForSalesBalance();
		
		$redirect_url = $_SERVER['HTTP_REFERER'];
		die("$redirect_url");
	}
	catch (MyException $e)
	{
		$error_message = $e->getUserMsg();
		die("$error_message");
	}
	
}

if(isset($_POST['trx_opd_patient'])&&$_POST['trx_opd_patient']=='yes')
{
	try
	{
		$trxIncOb = new classTransaction;
		$trxIncOb->setAccountHeadID($_POST['acchead_id']);
		$trxIncOb->setAccountID($_POST['account_id']);
		$trxIncOb->setTransactionType($_POST['type']);
		$trxIncOb->setAmountPayable($_POST['amount_payable']);
		$trxIncOb->setPayment($_POST['payment_amount']);
		$trxIncOb->setPatientID($_POST['patient_id']);
		$trxIncOb->setDoctorID($_POST['doctor_id']);
		$trxIncOb->setVisitID($_POST['visit_id']);
		$trxIncOb->setUserID($_SESSION['security']['user_id']);
		$trxIncOb->setLocationID($_SESSION['security']['location_id']);
		$trxIncOb->executeForOpdPatient();
		
		$redirect_url = $_SERVER['HTTP_REFERER'];
		die("$redirect_url");
	}
	catch (MyException $e)
	{
		$error_message = $e->getUserMsg();
		die("$error_message");
	}
	
}

if(isset($_POST['trx_opd_balance'])&&$_POST['trx_opd_balance']=='yes')
{
	try
	{
		$trxIncOb = new classTransaction;
		$trxIncOb->setAccountHeadID($_POST['acchead_id']);
		$trxIncOb->setAccountID($_POST['account_id']);
		$trxIncOb->setTransactionType($_POST['type']);
		$trxIncOb->setTransactionPurpose('balance');
		$trxIncOb->setAmountPayable($_POST['amount_payable']);
		$trxIncOb->setPayment($_POST['payment_amount']);
		$trxIncOb->setPatientID($_POST['patient_id']);
		$trxIncOb->setDoctorID($_POST['doctor_id']);
		$trxIncOb->setVisitID($_POST['visit_id']);
		$trxIncOb->setUserID($_SESSION['security']['user_id']);
		$trxIncOb->setLocationID($_SESSION['security']['location_id']);
		$trxIncOb->executeForOpdPatientBalance();
		
		$redirect_url = $_SERVER['HTTP_REFERER'];
		die("$redirect_url");
	}
	catch (MyException $e)
	{
		$error_message = $e->getUserMsg();
		die("$error_message");
	}
	
}

if(isset($_POST['trx_patient_reg_fee'])&&$_POST['trx_patient_reg_fee']=='yes')
{
	try
	{
		$trxIncOb = new classTransaction;
		$trxIncOb->setAccountHeadID($_POST['acchead_id']);
		$trxIncOb->setAccountID($_POST['account_id']);
		$trxIncOb->setTransactionType($_POST['type']);
		$trxIncOb->setTransactionPurpose('patient_reg_fee');
		$trxIncOb->setAmountPayable($_POST['amount_payable']);
		$trxIncOb->setPayment($_POST['payment_amount']);
		$trxIncOb->setPatientID($_POST['patient_id']);
		$trxIncOb->setUserID($_SESSION['security']['user_id']);
		$trxIncOb->setLocationID($_SESSION['security']['location_id']);
		$trxIncOb->executeForPatientRegistrationFee();
		
		$patientOb = new classPatient;
		$patientOb->updatePatientRegistrationFeeStatus($_POST['payment_amount'],$_POST['patient_id']);
		
		$redirect_url = 'mgmtpatients.php';
		die("$redirect_url");
	}
	catch (MyException $e)
	{
		$error_message = $e->getUserMsg();
		die("$error_message");
	}
	
}
	
################## Closing Items  ####################################

if(isset($_GET['prod_closing'])&&$_GET['prod_closing']=='yes')
{
	try
	{
		
		$closing_packing_type = $_GET['closing_packing_type'];
		$closing_packing_qty = $_GET['closing_packing_qty'];
		$product_id = $_GET['product_id'];
		
		$insertItemClosingOb = new classProductClosingReconcilation;
		$insertItemClosingOb->setProductID($product_id);
		$insertItemClosingOb->setClosingItemPacking($closing_packing_type);
		$insertItemClosingOb->setClosingItemCount($closing_packing_qty);
		$insertItemClosingOb->setClosingItemDate(time());
		$insertItemClosingOb->setLocationID($_SESSION['security']['location_id']);
		$insertItemClosingOb->setClosingReportID($_SESSION['temp']['productclosing_id']);
		$insertItemClosingOb->insertItemClosing();
		
		$closing_item_detail = $insertItemClosingOb->retrieveClosingItemByProductIDAndClosingReportID();
		$unit_phy_qtyinstock = $closing_item_detail[0]['unit_phy_qtyinstock'];
		
		
		######## retrieve product detail ########
		$singleProdOb = new classProduct;
		$singleProdOb->setProductID($product_id);
		$prod_detail = $singleProdOb->retrieveProductByProductID();
		
		if($closing_packing_type=='Unit/Tablets') 
		{
			$unit_per_pack = 1;
		} 
		elseif($closing_packing_type=='Leaf') 
		{
			$unit_per_pack = $prod_detail[0]['unitperbox']/$prod_detail[0]['leafperbox'];
		} 
		elseif($closing_packing_type=='Box/Pack') 
		{
			$unit_per_pack = $prod_detail[0]['unitperbox'];
		}
		
		die("$unit_phy_qtyinstock&$unit_per_pack");
	}
	catch (MyException $e)
	{
		$error_message = $e->getUserMsg();
		die("$error_message");
	}	
	
}

############################# Add Medicine labtest and procedure ###########################################################	

try
{
	if(isset($_POST['add_medicine'])&&$_POST['add_medicine']=='Add_medicine')
	{
		$insertMedOB = new classVisits;
		$insertMedOB->setOrganizationID($_SESSION['security']['org_id']);
		$insertMedOB->setMedicineName($_POST['medicine_name']);
		$insertMedOB->setMedicineType($_POST['medicine_type']);
		$insertMedOB->setMedicineGroup($_POST['medicine_group']);
		$insertMedOB->insertMedicine();

		
		$redirect_url = $_SERVER['HTTP_REFERER'];
		die("$redirect_url");
	}
	
	if(isset($_POST['update_medicine'])&&$_POST['update_medicine']=='Update_medicine')
	{
		$updateMedOB = new classVisits;
		$updateMedOB->setMedicineName($_POST['medicine_name']);
		$updateMedOB->setMedicineID($_POST['product_id']);
		$updateMedOB->setMedicineType($_POST['medicine_type']);
		$updateMedOB->setMedicineGroup($_POST['medicine_group']);
		$updateMedOB->updateMedicine();

		
		$redirect_url = $_SERVER['HTTP_REFERER'];
		die("$redirect_url");
	}
	
	if(isset($_POST['add_labtest'])&&$_POST['add_labtest']=='Add_labtest')
	{
		$insertLabTesOB = new classVisits;
		$insertLabTesOB->setOrganizationID($_SESSION['security']['org_id']);
		$insertLabTesOB->setLabTestName($_POST['labtest_name']);
		$insertLabTesOB->insertLabTest();
		$redirect_url = $_SERVER['HTTP_REFERER'];
		//$redirect_url = 'addpresc.php';
		die("$redirect_url");
	}
	if(isset($_POST['update_labtest'])&&$_POST['update_labtest']=='Update_labtest')
	{
		$updatetLabTesOB = new classVisits;
		$updatetLabTesOB->setLabTestID($_POST['labtest_id']);
		$updatetLabTesOB->setLabTestName($_POST['labtest_name']);
		$updatetLabTesOB->updateLabTest();
		$redirect_url = $_SERVER['HTTP_REFERER'];
		//$redirect_url = 'mgmtpresctests.php';
		die("$redirect_url");
	}
	
	if(isset($_POST['add_procedure'])&&$_POST['add_procedure']=='Add_procedure')
	{
		$insertProcOB = new classVisits;
		$insertProcOB->setOrganizationID($_SESSION['security']['org_id']);
		$insertProcOB->setProcedureName($_POST['procedure_name']);
		$insertProcOB->insertProcedure();
		$redirect_url = $_SERVER['HTTP_REFERER'];
		//$redirect_url = 'addpresc.php';
		die("$redirect_url");
	}
	if(isset($_POST['update_procedure'])&&$_POST['update_procedure']=='Update_procedure')
	{
		$updateProcOB = new classVisits;
		$updateProcOB->setProcedureID($_POST['procedure_id']);
		$updateProcOB->setProcedureName($_POST['procedure_name']);
		$updateProcOB->updateProcedure();

		$redirect_url = 'mgmtprescprocedures.php';
		die("$redirect_url");
	}
	
}
catch (MyException $e)
{
	$error_message = $e->getUserMsg();
	die("$error_message");
}

############################# Activate Products ###########################################################	


if((isset($_POST['activate_products'])&&$_POST['activate_products']=='activate_products')&&isset($_POST['active_prod']))
{
	try
	{
		foreach($_POST['active_prod'] as $key => $value)
		{
			$prodStatusOb = new classProduct;
			$prodStatusOb->setProductID($key);
			$prodStatusOb->setActive($value);
			$prodStatusOb->updateProductStatus();
		}
		
		$redirect_url = 'pharmdel.php';
		die("$redirect_url");
	}
	catch (MyException $e)
	{
		$error_message = $e->getUserMsg();
		die("$error_message");
	}
}

############################# Calculate Refund  Receive Item Detail ###########################################	

if(isset($_GET['refund_receive_items'])&&isset($_GET['refund_qty'])&&isset($_GET['recitems_id']))
{
	try
	{
		
		$recitems_id = $_GET['recitems_id'];
		$refund_qty = $_GET['refund_qty'];
		$product_id = $_GET['prod_id'];
		
		$refundRecItemOb = new classReceiveInvoice;
		$refundRecItemOb->setRefundQty($refund_qty);
		$refundRecItemOb->setReceiveItemID($recitems_id);
		$refundRecItemOb->setProductID($product_id);
		$refundRecItemOb->setSupplierID($_SESSION['temp']['supplier_id']);
		$refundRecItemOb->setLocationID($_SESSION['security']['location_id']);
		$refundRecItemOb->refundReceiveItemsInInvoice();
		
		$retrieveInvoiceItemOb = new classReceiveInvoice;
		$retrieveInvoiceItemOb->setReceiveItemID($recitems_id);
		$invoice_items_array = $retrieveInvoiceItemOb->retrieveInvoiceItemsDetailByItemID();
		
		$tranOb = new classTransaction;
		$account_data = $tranOb->retrieveAccountHeadAndAccountByID(3,6);	
	
		$trxIncOb = new classTransaction;
		$trxIncOb->setAccountHeadID($account_data[0]['acchead_id']);
		$trxIncOb->setAccountID($account_data[0]['account_id']);
		$trxIncOb->setTransactionType($account_data[0]['type']);
		$trxIncOb->setAmountPayable($invoice_items_array[0]['refund_amount']);
		$trxIncOb->setPayment($invoice_items_array[0]['refund_amount']);
		$trxIncOb->setTransactionPurpose('refund');
		$trxIncOb->setSupplierID($_SESSION['temp']['supplier_id']);
		$trxIncOb->setReceiveInvoiceID($_SESSION['temp']['invoice_id']);
		$trxIncOb->setUserID($_SESSION['security']['user_id']);
		$trxIncOb->setLocationID($_SESSION['security']['location_id']);
		$trxIncOb->executeForSupplierRefund();
		
		$retrieveInvoiceOb = new classReceiveInvoice;
		$retrieveInvoiceOb->setReceiveInvoiceID($_SESSION['temp']['invoice_id']);
		$sumOfRefundItems = $retrieveInvoiceOb->retrieveSumOfRefundItems();
		$invoice_array = $retrieveInvoiceOb->retrieveInvoiceDetail();
		$amount_payable = $sumOfRefundItems-($sumOfRefundItems*$invoice_array[0]['discount']/100);
		
		$trade_price = $invoice_items_array[0]['trade_price'];
		$refund_qty = $invoice_items_array[0]['refund_qty'];
		$discount = $invoice_items_array[0]['discount'];
		$total = $trade_price*$refund_qty;
		$net_total = $total-($total*$discount/100);
		
		
		die("$total&$net_total&$sumOfRefundItems&$amount_payable");
	}
	catch (MyException $e)
	{
		$error_message = $e->getUserMsg();
		die("$error_message");
	}	
	
}

############################# Calculate Refund  Sale Item Detail ###########################################	

if(isset($_GET['refund_sale_items'])&&isset($_GET['refund_qty'])&&isset($_GET['saleitems_id']))
{
	try
	{
		
		$saleitems_id = $_GET['saleitems_id'];
		$refund_qty = $_GET['refund_qty'];
		$product_id = $_GET['prod_id'];
		
		$refundSaleItemOb = new classSaleInvoice;
		$refundSaleItemOb->setRefundQty($refund_qty);
		$refundSaleItemOb->setSaleItemID($saleitems_id);
		$refundSaleItemOb->setProductID($product_id);
		$refundSaleItemOb->setPatientID($_SESSION['temp']['patient_id']);
		$refundSaleItemOb->setLocationID($_SESSION['security']['location_id']);
		$refundSaleItemOb->refundSaleItemsInInvoice();
		
		$retrieveInvoiceItemOb = new classSaleInvoice;
		$retrieveInvoiceItemOb->setSaleItemID($saleitems_id);
		$invoice_items_array = $retrieveInvoiceItemOb->retrieveInvoiceItemsDetailByItemID();
		
		$tranOb = new classTransaction;
		$account_data = $tranOb->retrieveAccountHeadAndAccountByID(3,5);	
	
		$trxIncOb = new classTransaction;
		$trxIncOb->setAccountHeadID($account_data[0]['acchead_id']);
		$trxIncOb->setAccountID($account_data[0]['account_id']);
		$trxIncOb->setTransactionType($account_data[0]['type']);
		$trxIncOb->setAmountPayable($invoice_items_array[0]['refund_amount']);
		$trxIncOb->setPayment($invoice_items_array[0]['refund_amount']);
		$trxIncOb->setTransactionPurpose('refund');
		$trxIncOb->setPatientID($_SESSION['temp']['patient_id']);
		$trxIncOb->setSaleInvoiceID($_SESSION['temp']['invoice_id']);
		$trxIncOb->setUserID($_SESSION['security']['user_id']);
		$trxIncOb->setLocationID($_SESSION['security']['location_id']);
		$trxIncOb->executeForPatientRefund();
		
		$retrieveInvoiceOb = new classSaleInvoice;
		$retrieveInvoiceOb->setSaleInvoiceID($_SESSION['temp']['invoice_id']);
		$sumOfRefundItems = $retrieveInvoiceOb->retrieveSumOfRefundItems();
		$invoice_array = $retrieveInvoiceOb->retrieveInvoiceDetail();
		$amount_payable = $sumOfRefundItems-($sumOfRefundItems*$invoice_array[0]['discount']/100);
		
		$retail_price = $invoice_items_array[0]['retail_price'];
		$refund_qty = $invoice_items_array[0]['refund_qty'];
		$discount = $invoice_items_array[0]['discount'];
		$total = $retail_price*$refund_qty;
		$net_total = $total-($total*$discount/100);
		
		
		die("$total&$net_total&$sumOfRefundItems&$amount_payable");
	}
	catch (MyException $e)
	{
		$error_message = $e->getUserMsg();
		die("$error_message");
	}	
	
}

############################# opd Refund transaction ###########################################	

if(isset($_POST['trx_opd_refund'])&&$_POST['trx_opd_refund']=='yes')
{
	try
	{
		$trxOpdRefundOb = new classTransaction;
		$trxOpdRefundOb->setAccountHeadID($_POST['acchead_id']);
		$trxOpdRefundOb->setAccountID($_POST['account_id']);
		$trxOpdRefundOb->setTransactionType($_POST['type']);
		$trxOpdRefundOb->setTransactionPurpose('opd_refund');
		$trxOpdRefundOb->setAmountPayable($_POST['amount_payable']);
		$trxOpdRefundOb->setPayment($_POST['payment_amount']);
		$trxOpdRefundOb->setPatientID($_POST['patient_id']);
		$trxOpdRefundOb->setUserID($_SESSION['security']['user_id']);
		$trxOpdRefundOb->setTransactionID($_POST['transact_id']);
		$trxOpdRefundOb->setLocationID($_SESSION['security']['location_id']);
		$trxOpdRefundOb->executeForOpdRefund();
		
		$redirect_url = 'opdtransact.php';
		die("$redirect_url");
	}
	catch (MyException $e)
	{
		$error_message = $e->getUserMsg();
		die("$error_message");
	}
	
}




############################# Calculate Sale Item Detail ###########################################	

if(isset($_GET['diag_items'])&&isset($_GET['discount'])&&isset($_GET['diagitems_id']))
{
	
	try
	{
		$diagitems_id = $_GET['diagitems_id'];
		$discount = $_GET['discount'];
		$specimen_no = $_GET['specimen_no'];
		
		$updateDiagItemsOb = new classDiagnostic;
		$updateDiagItemsOb->setDiscount($discount);
		$updateDiagItemsOb->setDiagItemID($diagitems_id);
		$updateDiagItemsOb->setDiagnosisSpecimenNo($specimen_no);
		$updateDiagItemsOb->updateDiagItemsInInvoice();
		
		$retrieveInvoiceItemOb = new classDiagnostic;
		$retrieveInvoiceItemOb->setDiagItemID($diagitems_id);
		$invoice_items_array = $retrieveInvoiceItemOb->retrieveInvoiceItemsDetailByItemID();
		
		$retrieveInvoiceOb = new classDiagnostic;
		$retrieveInvoiceOb->setDiagInvoiceID($_SESSION['temp']['diaginvoice_id']);
		$invoice_array = $retrieveInvoiceOb->retrieveInvoiceDetail();
		
		$invoice_amount = $invoice_array[0]['invoice_amount'];
		$amount_payable = $invoice_array[0]['amount_payable'];
		
		$price = $invoice_items_array[0]['price'];
		$net_total = $invoice_items_array[0]['net_total'];
		
		
		
		die("$net_total&$invoice_amount&$amount_payable");
	}
	catch (MyException $e)
	{
		$error_message = $e->getUserMsg();
		die("$error_message");
	}	
	
}

############################# UPDATE DIAGNOSIS INVOICE DISCOUNT ###########################################	

if(isset($_GET['diag_invoice_discount']))
{
	try
	{
		$invoice_discount = $_GET['diag_invoice_discount'];
		$updateInvDiscountOb = new classDiagnostic;
		$updateInvDiscountOb->setDiagInvoiceID($_SESSION['temp']['diaginvoice_id']);
		$updateInvDiscountOb->setPayment($updateInvDiscountOb->retrieveInvoiceItemsTotal());
		$updateInvDiscountOb->setDiscount($invoice_discount);
		$updateInvDiscountOb->updateDiagInvoice();
	}
	catch (MyException $e)
	{
		$error_message = $e->getUserMsg();
		die("$error_message");
	}	
}
############################# UPDATE DIAGNOSIS INVOICE DISCOUNT ###########################################	

if(isset($_GET['reffered_by'])&&$_GET['reffered_by']!='')
{
	try
	{
		$updateRefOb = new classDiagnostic;
		$updateRefOb->setDiagInvoiceID($_GET['diaginvoice_id']);
		$updateRefOb->setReferringBy($_GET['reffered_by']);
		$updateRefOb->updateRefferedByInDiagInvoice();
	}
	catch (MyException $e)
	{
		$error_message = $e->getUserMsg();
		die("$error_message");
	}	
}

############################# UPDATE DIAGNOSIS INVOICE DISCOUNT ###########################################	

if(isset($_GET['reffered_to'])&&$_GET['reffered_to']!='')
{
	try
	{
		$updateRefOb = new classDiagnostic;
		$updateRefOb->setDiagInvoiceID($_GET['diaginvoice_id']);
		$updateRefOb->setReferringTo($_GET['reffered_to']);
		$updateRefOb->updateRefferedToInDiagInvoice();
	}
	catch (MyException $e)
	{
		$error_message = $e->getUserMsg();
		die("$error_message");
	}	
}
		

############################# diagnostic transaction ###########################################	

if(isset($_POST['trx_diagnostic'])&&$_POST['trx_diagnostic']=='yes')
{
	try
	{
		$trxDiagOb = new classTransaction;
		$trxDiagOb->setAccountHeadID($_POST['acchead_id']);
		$trxDiagOb->setAccountID($_POST['account_id']);
		$trxDiagOb->setTransactionType($_POST['type']);
		$trxDiagOb->setTransactionPurpose('diagnostic_fee');
		$trxDiagOb->setAmountPayable($_POST['amount_payable']);
		$trxDiagOb->setPayment($_POST['payment_amount']);
		$trxDiagOb->setDescription($_POST['description']);
		$trxDiagOb->setPatientID($_POST['patient_id']);
		$trxDiagOb->setDiagInvoiceID($_POST['diaginvoice_id']);
		$trxDiagOb->setUserID($_SESSION['security']['user_id']);
		$trxDiagOb->setLocationID($_SESSION['security']['location_id']);
		$trxDiagOb->executeForDiagnostic();
		
		$redirect_url = 'mgmttestsbydate.php';
		die("$redirect_url");
	}
	catch (MyException $e)
	{
		$error_message = $e->getUserMsg();
		die("$error_message");
	}
	
}

############################# Refund  Diagnostics Item Detail ###########################################	

if(isset($_GET['refund_diagnostic_items'])&&isset($_GET['diagitems_id']))
{
	try
	{
		
		$diagitems_id = $_GET['diagitems_id'];
		
		$refundDiagItemOb = new classDiagnostic;
		$refundDiagItemOb->setDiagItemID($diagitems_id);
		$refundDiagItemOb->refunddiagItemsInInvoice();
		
		$retrieveInvoiceItemOb = new classDiagnostic;
		$retrieveInvoiceItemOb->setDiagItemID($diagitems_id);
		$invoice_items_array = $retrieveInvoiceItemOb->retrieveInvoiceItemsDetailByItemID();
		
		$tranOb = new classTransaction;
		$account_data = $tranOb->retrieveAccountHeadAndAccountByID(11,10);	
	
		$trxOb = new classTransaction;
		$trxOb->setAccountHeadID($account_data[0]['acchead_id']);
		$trxOb->setAccountID($account_data[0]['account_id']);
		$trxOb->setTransactionType($account_data[0]['type']);
		$trxOb->setAmountPayable($invoice_items_array[0]['refund_amount']);
		$trxOb->setPayment($invoice_items_array[0]['refund_amount']);
		$trxOb->setTransactionPurpose('refund');
		$trxOb->setPatientID($_SESSION['temp']['patient_id']);
		$trxOb->setDiagInvoiceID($_SESSION['temp']['diaginvoice_id']);
		$trxOb->setUserID($_SESSION['security']['user_id']);
		$trxOb->setLocationID($_SESSION['security']['location_id']);
		$trxOb->executeForDiagRefund();
		
		$retrieveInvoiceOb = new classDiagnostic;
		$retrieveInvoiceOb->setDiagInvoiceID($_SESSION['temp']['diaginvoice_id']);
		$sumOfRefundItems = $retrieveInvoiceOb->retrieveSumOfRefundItems();
		$invoice_array = $retrieveInvoiceOb->retrieveInvoiceDetail();
		$amount_payable = $sumOfRefundItems-($sumOfRefundItems*$invoice_array[0]['discount']/100);
		
		die("$sumOfRefundItems&$amount_payable");
	}
	catch (MyException $e)
	{
		$error_message = $e->getUserMsg();
		die("$error_message");
	}	
	
}

############################# update diagnostic results ###########################################

if(isset($_POST['labtest_result'])&&$_POST['labtest_result']=='yes')
{
	try
	{
		$updateDiagResultOb = new classDiagnostic;
		$updateDiagResultOb->setDiagItemID($_POST['diagitems_id']);
		$updateDiagResultOb->setDiagnosisResult($_POST['test_result']);
		$updateDiagResultOb->updateDiagItemsResult();
		
		$redirect_url = $_POST['redirect_url'];
		die("$redirect_url");
	}
	catch (MyException $e)
	{
		$error_message = $e->getUserMsg();
		die("$error_message");
	}	
	
}

if(isset($_POST['procedure_result'])&&$_POST['procedure_result']=='yes')
{
	try
	{
		$imageOb = new classImages;
		$imageOb->setImageName($_FILES['test_result_image']['name']);
		$imageOb->setImageSize($_FILES['test_result_image']['size']);
		$imageOb->setImageType($_FILES['test_result_image']['type']);
		$imageOb->execute();
		
		$image_id= $imageOb->getImageID();
		$path = '../images/diagnosis_images/'.$image_id.'.jpg';
		move_uploaded_file($_FILES['test_result_image']['tmp_name'],$path);
		
		$imageOb->setDiagItemID($_POST['diagitems_id']);
		$imageOb->setImageID($image_id);
		$imageOb->updateDiagItemID();
		
		$updateDiagResultOb = new classDiagnostic;
		$updateDiagResultOb->setDiagItemID($_POST['diagitems_id']);
		$updateDiagResultOb->setDiagnosisResult($_POST['remarks']);
		$updateDiagResultOb->setDiagnosisResultUnit(" ");
		$updateDiagResultOb->updateDiagItemsResult();
		
		$redirect_url = $_POST['redirect_url'];
		die("$redirect_url");
	}
	catch (MyException $e)
	{
		$error_message = $e->getUserMsg();
	}
}

############################# Add labtest and procedure details ###########################################################	

try
{
	if(isset($_POST['add_labtest_details'])&&$_POST['add_labtest_details']=='Add_labtest_details')
	{
		$insertLabTesOB = new classDiagnostic;
		$insertLabTesOB->setOrganizationID($_SESSION['security']['org_id']);
		$insertLabTesOB->setLabTestName($_POST['labtest_name']);
		$insertLabTesOB->setLabtestAbbrivation($_POST['labtest_abb']);
		$insertLabTesOB->setLabTestNormalValue($_POST['labtest_normal_value']);
		$insertLabTesOB->setLabTestUnit($_POST['labtest_unit']);
		$insertLabTesOB->setLabTestCost($_POST['labtest_cost']);
		$insertLabTesOB->setLabTestNotes($_POST['labtest_notes']);
		$insertLabTesOB->setLabTestPriorityID($_POST['labtest_number']);
		
		$insertLabTesOB->insertLabTest();

		$redirect_url = 'mgmtlabtests.php';
		die("$redirect_url");
	}
	
	if(isset($_POST['update_labtest_details'])&&$_POST['update_labtest_details']=='Update_labtest_details')
	{
		$updateLabTesOB = new classDiagnostic;
		$updateLabTesOB->setLabTestID($_POST['labtest_id']);
		$updateLabTesOB->setOrganizationID($_SESSION['security']['org_id']);
		$updateLabTesOB->setLabTestName($_POST['labtest_name']);
		$updateLabTesOB->setLabtestAbbrivation($_POST['labtest_abb']);
		$updateLabTesOB->setLabTestNormalValue($_POST['labtest_normal_value']);
		$updateLabTesOB->setLabTestUnit($_POST['labtest_unit']);
		$updateLabTesOB->setLabTestCost($_POST['labtest_cost']);
		$updateLabTesOB->setLabTestNotes($_POST['labtest_notes']);
		$updateLabTesOB->setLabTestPriorityID($_POST['labtest_number']);
		$updateLabTesOB->updateLabTest();

		$redirect_url = 'mgmtlabtests.php';
		die("$redirect_url");
	}
	
	if(isset($_POST['add_procedure_details'])&&$_POST['add_procedure_details']=='Add_procedure_details')
	{
		$insertProcOB = new classDiagnostic;
		$insertProcOB->setOrganizationID($_SESSION['security']['org_id']);
		$insertProcOB->setProcedureName($_POST['procedure_name']);
		$insertProcOB->setProcedureType($_POST['procedure_type']);
		$insertProcOB->setProcedureCost($_POST['procedure_cost']);
		$insertProcOB->insertProcedure();

		$redirect_url = 'diagradiotests.php';
		die("$redirect_url");
	}
	
}
catch (MyException $e)
{
	$error_message = $e->getUserMsg();
	die("$error_message");
}

############################# Add labtest groups ###########################################################	

if(isset($_POST['add_labtest_group'])&&$_POST['add_labtest_group']=='Add_labtest_group')
{
	try
	{
		$insertLabTestGroupOB = new classDiagnostic;
		$insertLabTestGroupOB->setOrganizationID($_SESSION['security']['org_id']);
		$insertLabTestGroupOB->setLabTestGroupName($_POST['group_name']);
		$insertLabTestGroupOB->setLabTestGroupCost($_POST['group_cost']);
		$insertLabTestGroupOB->setLabTestID($_POST['labtest_id']);
		$insertLabTestGroupOB->insertLabTestGroups();
		
		
		$insertLabTestGroupOB->updateGroupIDInLabtest();
	
		$redirect_url = 'mgmttestgroups.php';
		die("$redirect_url");
	}
	catch (MyException $e)
	{
		$error_message = $e->getUserMsg();
		die("$error_message");
	}

}

if(isset($_POST['update_labtest_group'])&&$_POST['update_labtest_group']=='Update_labtest_group')
{
	try
	{
		$updateLabTestGroupOB = new classDiagnostic;
		$updateLabTestGroupOB->setOrganizationID($_SESSION['security']['org_id']);
		$updateLabTestGroupOB->setLabTestGroupID($_POST['group_id']);
		$updateLabTestGroupOB->setLabTestGroupName($_POST['group_name']);
		$updateLabTestGroupOB->setLabTestGroupCost($_POST['group_cost']);
		$updateLabTestGroupOB->updateLabTestGroups();
		
		$redirect_url = 'mgmttestgroups.php';
		die("$redirect_url");
	}
	catch (MyException $e)
	{
		$error_message = $e->getUserMsg();
		die("$error_message");
	}

}


if(isset($_POST['update_labtests_in_group'])&&$_POST['update_labtests_in_group']=='Update_labtests_in_group')
{
	try
	{
		$updateLabTestInGroupOB = new classDiagnostic;
		$updateLabTestInGroupOB->setLabTestGroupID($_POST['group_id']);
		$updateLabTestInGroupOB->setLabTestID($_POST['labtest_id']);
		$updateLabTestInGroupOB->updateGroupIDInLabtest();
	
		$redirect_url = 'mgmttestgroups.php';
		die("$redirect_url");
	}
	catch (MyException $e)
	{
		$error_message = $e->getUserMsg();
		die("$error_message");
	}

}

############################# Retrieve labtest of group ###########################################################

if(isset($_GET['group_id'])&&$_GET['group_id']!='')
{
	$group_id = $_GET['group_id'];
	$labtestOb = new classDiagnostic;
	$labtestOb->setLabTestGroupID($group_id);
	$group_tests = $labtestOb->retrieveLabTestByGroupID();
	?>
    
	<input name="group_id" id="group_id" type="hidden" class="btn btn-primary" value="<?php echo $group_id;?>"/>
    <table class="display table table-bordered table-striped" id="dynamic-table">
    
    <thead>
        <tr>
            <th width="5%" align="center"><input type="checkbox" name="selecctall" id="selecctall" value="" class="btn btn-primary" style="margin-left:7px;"></th>
            <th width="95%">Test Name</th>
        </tr>
    </thead>
            
    <tbody>
  
	<?php
	for($gt=0;$gt<count($group_tests);$gt++)
	{
		$diagnosis_id = $group_tests[$gt]['labtest_id'];
		$labtest_name = $group_tests[$gt]['labtest_name'];
		if($gt%2==0){$class = 'gradeX';}else{$class = 'gradeC';}
		?>
        <tr class="<?php echo $class;?>">
        	<td align="center"><input type="checkbox" name="labtest_ids[]" value="<?php echo $diagnosis_id;?>" class="btn btn-primary checkboxes"></td>
            <td align="left"><?php echo $labtest_name;?></td>
        </tr>
        <?php	
	}
	?>
    </tbody>
    
    </table>
   
	<?php
	
}

############################# Add And Update ipd wards ###########################################################

if(isset($_POST['add_ipdward'])&&$_POST['add_ipdward']=='Add_ipdward')
{
	try
	{
		$insertWardOb = new classWardMgmt;
		$insertWardOb->setLocationID($_SESSION['security']['location_id']);
		$insertWardOb->setWardNo($_POST['ward_no']);
		$insertWardOb->setWardType($_POST['ward_type']);
		$insertWardOb->setStaffID($_POST['ward_incharge']);
		$insertWardOb->insertWard();
		
		$redirect_url = 'mgmtwards.php';
		die("$redirect_url");
	}
	catch (MyException $e)
	{
		$error_message = $e->getUserMsg();
		die("$error_message");
	}
	
	
	
	
}

if(isset($_POST['update_ipdward'])&&$_POST['update_ipdward']=='Update_ipdward')
{
	try
	{
		$insertWardOb = new classWardMgmt;
		$insertWardOb->setLocationID($_SESSION['security']['location_id']);
		$insertWardOb->setWardNo($_POST['ward_no']);
		$insertWardOb->setWardID($_POST['ipd_ward_id']);
		$insertWardOb->setWardType($_POST['ward_type']);
		$insertWardOb->setStaffID($_POST['ward_incharge']);
		$insertWardOb->updateWard();
		
		$redirect_url = 'mgmtwards.php';
		die("$redirect_url");
	}
	catch (MyException $e)
	{
		$error_message = $e->getUserMsg();
		die("$error_message");
	}
	
	
	
	
}


############################# Add And Update ipd ward beds ###########################################################

if(isset($_POST['add_ipdwardbed'])&&$_POST['add_ipdwardbed']=='Add_ipdwardbed')
{
	try
	{
		$insertWardBedOb = new classWardMgmt;
		$insertWardBedOb->setWardBedNo($_POST['bed_no']);
		$insertWardBedOb->setWardBedFee($_POST['bed_fee']);
		$insertWardBedOb->setWardBedType($_POST['bed_type']);
		$insertWardBedOb->setWardID($_POST['ward_id']);
		$insertWardBedOb->setWardBedStatus('Empty');
		$insertWardBedOb->insertWardBed();
		
		$redirect_url = 'mgmtwards.php';
		die("$redirect_url");
	}
	catch (MyException $e)
	{
		$error_message = $e->getUserMsg();
		die("$error_message");
	}
	
	
	
	
}

if(isset($_POST['update_ipdwardbed'])&&$_POST['update_ipdwardbed']=='Update_ipdwardbed')
{
	try
	{
		$updateWardBedOb = new classWardMgmt;
		$updateWardBedOb->setWardBedID($_POST['bed_id']);
		$updateWardBedOb->setWardBedNo($_POST['bed_no']);
		$updateWardBedOb->setWardBedFee($_POST['bed_fee']);
		$updateWardBedOb->setWardBedType($_POST['bed_type']);
		$updateWardBedOb->setWardID($_POST['ward_id']);
		//$updateWardBedOb->setWardBedStatus('Empty');
		$updateWardBedOb->updateWardBed();
		
		$redirect_url = 'mgmtwards.php';
		die("$redirect_url");
	}
	catch (MyException $e)
	{
		$error_message = $e->getUserMsg();
		die("$error_message");
	}
}

############################# Allot ipd ward beds ###########################################################

if(isset($_POST['allocate_ipdwardbed'])&&$_POST['allocate_ipdwardbed']=='Allocate_ipdwardbed')
{
	try
	{
		$allotWardBedOb = new classWardMgmt;
		$allotWardBedOb->setLocationID($_SESSION['security']['location_id']);
		$allotWardBedOb->setPatientID($_POST['patient_id']);
		$allotWardBedOb->setWardBedID($_POST['bed_id']);
		$allotWardBedOb->setWardID($_POST['ward_id']);
		$allotWardBedOb->setWardBedStatus($_POST['bed_status']);
		$allotWardBedOb->setWardBedAllotTime($_POST['allocation_date_due'].' '.$_POST['allocation_time_due']);
		$allotWardBedOb->setWardBedDischargeDueTime($_POST['discharge_date_due'].' '.$_POST['discharge_time_due']);
		
		if($_POST['allocate_type']=='allocate_reserved')
		{
			$allotWardBedOb->setWardBedAllotID($_POST['ipd_bed_allotment_id']);
			$allotWardBedOb->updateAllotmentWardBed();
		}
		else
		{
			$allotWardBedOb->allocateWardBed();
		}
		
		
		$redirect_url = 'mgmtadmissions.php';
		die("$redirect_url");
	}
	catch (MyException $e)
	{
		$error_message = $e->getUserMsg();
		die("$error_message");
	}
	
	
}

############################# Allot ipd ward beds ###########################################################

if(isset($_POST['transfer_ipdwardbed'])&&$_POST['transfer_ipdwardbed']=='Transfer_ipdwardbed')
{
	try
	{
		$transferWardBedOb = new classWardMgmt;
		$transferWardBedOb->setWardBedID($_POST['bed_id']);
		$transferWardBedOb->setWardID($_POST['ward_id']);
		$transferWardBedOb->setWardBedAllotID($_POST['ipd_bed_allotment_id']);
		$transferWardBedOb->TransferAllocation();
		
		$redirect_url = 'mgmtadmissions.php';
		die("$redirect_url");
	}
	catch (MyException $e)
	{
		$error_message = $e->getUserMsg();
		die("$error_message");
	}
	
	
}

############################# RETRIEVE BEDS OF WARD ###########################################################

if(isset($_GET['showwardbeds'])&&$_GET['showwardbeds']=='yes')
{
	try
	{
		$retWardBedOb = new classWardMgmt;
		$retWardBedOb->setWardID($_GET['ajax_ward_id']);
		$bed_dropdown= $retWardBedOb->retrieveEmptybedOfWard();
		if(count($bed_dropdown)>0)
		{
			?>
            <option value="">Select Bed</option>
            <?php
			for($bdd=0;$bdd<count($bed_dropdown);$bdd++)
			{
				?>
                <option value="<?php echo $bed_dropdown[$bdd]['ipd_bed_id']?>"><?php echo $bed_dropdown[$bdd]['ipd_bed_no'];?></option>
                <?php
			}
		}
		else
		{
			?>
            <option value="">Select Bed</option>
            <?php
		}
	}
	catch (MyException $e)
	{
		$error_message = $e->getUserMsg();
		die("$error_message");
	}
	
	
}

############################# Discharge ipd ward beds ###########################################################

if(isset($_POST['discharge_ipdwardbed'])&&$_POST['discharge_ipdwardbed']=='Discharge_ipdwardbed')
{
	try
	{
		$disWardBedOb = new classWardMgmt;
		$disWardBedOb->setPatientID($_POST['patient_id']);
		$disWardBedOb->setWardBedID($_POST['bed_id']);
		$disWardBedOb->setWardBedAllotID($_POST['ipd_bed_allotment_id']);
		$disWardBedOb->setProvisionalDiagnosis($_POST['prov_diagnosis']);
		$disWardBedOb->setReferedTo($_POST['refered_to']);
		$disWardBedOb->setFinalDiagnosis($_POST['final_diagnosis']);
		$disWardBedOb->setResult($_POST['result']);
		$disWardBedOb->setDischargingDoctor($_POST['discharging_doctor']);
		$disWardBedOb->setDischargingUnit($_POST['discharging_unit']);
		$disWardBedOb->setDischargingFollowUpTime($_POST['discharging_followuptime']);
		//$disWardBedOb->setWardBedDischargeTime($_POST['discharge_date'].' '.$_POST['dis_time']);
		$disWardBedOb->dischargeFromBed();
		
		$redirect_url = 'mgmtinpatients.php';
		die("$redirect_url");
	}
	catch (MyException $e)
	{
		$error_message = $e->getUserMsg();
		die("$error_message");
	}
	
	
}


############################# Update Discount Payment ipd ward beds #######################

if(isset($_GET['ipd_invoice_discount'])&&$_GET['ipd_invoice_discount']!='')
{
	try
	{
		$discountOb = new classWardMgmt;
		$discountOb->setWardBedAdmissionNo($_GET['ipd_bed_admission_no']);
		$discountOb->setDiscount($_GET['ipd_invoice_discount']);
		$discountOb->updatePaymentsAfterDiscount();
		
		$redirect_url = $_SERVER['HTTP_REFERER'];
		die("$redirect_url");
	}
	catch (MyException $e)
	{
		$error_message = $e->getUserMsg();
		die("$error_message");
	}	
}

if(isset($_POST['ipd_trx'])&&$_POST['ipd_trx']=='yes')
{
	try
	{
		$trxIncOb = new classTransaction;
		$trxIncOb->setAccountHeadID($_POST['acchead_id']);
		$trxIncOb->setAccountID($_POST['account_id']);
		$trxIncOb->setTransactionType($_POST['type']);
		$trxIncOb->setAmountPayable($_POST['amount_payable']);
		$trxIncOb->setPayment($_POST['payment_amount']);
		$trxIncOb->setPatientID($_POST['patient_id']);
		$trxIncOb->setIpdDischargeBillID($_POST['ipd_discharge_bill_id']);
		$trxIncOb->setUserID($_SESSION['security']['user_id']);
		$trxIncOb->setLocationID($_SESSION['security']['location_id']);
		$trxIncOb->executeForIpdPatient();
		
		$redirect_url = 'mgmtinpatienthistory.php';
		die("$redirect_url");
	}
	catch (MyException $e)
	{
		$error_message = $e->getUserMsg();
		die("$error_message");
	}
	
}


############################# ipd Balance transaction ###########################################	

if(isset($_POST['trx_ipd_balance'])&&$_POST['trx_ipd_balance']=='yes')
{
	try
	{
		$trxIncOb = new classTransaction;
		$trxIncOb->setAccountHeadID($_POST['acchead_id']);
		$trxIncOb->setAccountID($_POST['account_id']);
		$trxIncOb->setTransactionType($_POST['type']);
		$trxIncOb->setTransactionPurpose('balance');
		$trxIncOb->setAmountPayable($_POST['amount_payable']);
		$trxIncOb->setPayment($_POST['payment_amount']);
		$trxIncOb->setDescription($_POST['description']);
		$trxIncOb->setPatientID($_POST['patient_id']);
		$trxIncOb->setWardBedAllotID($_POST['ipd_bed_allotment_id']);
		$trxIncOb->setUserID($_SESSION['security']['user_id']);
		$trxIncOb->setLocationID($_SESSION['security']['location_id']);
		$trxIncOb->executeForIpdBalance();
		
		$redirect_url = $_SERVER['HTTP_REFERER'];
		die("$redirect_url");
	}
	catch (MyException $e)
	{
		$error_message = $e->getUserMsg();
		die("$error_message");
	}
	
}

############################# ipd Refund transaction ###########################################	

if(isset($_POST['trx_ipd_refund'])&&$_POST['trx_ipd_refund']=='yes')
{
	try
	{
		$trxOpdRefundOb = new classTransaction;
		$trxOpdRefundOb->setAccountHeadID($_POST['acchead_id']);
		$trxOpdRefundOb->setAccountID($_POST['account_id']);
		$trxOpdRefundOb->setTransactionType($_POST['type']);
		$trxOpdRefundOb->setTransactionPurpose('ipd_refund');
		$trxOpdRefundOb->setAmountPayable($_POST['amount_payable']);
		$trxOpdRefundOb->setPayment($_POST['payment_amount']);
		$trxOpdRefundOb->setPatientID($_POST['patient_id']);
		$trxOpdRefundOb->setUserID($_SESSION['security']['user_id']);
		$trxOpdRefundOb->setTransactionID($_POST['transact_id']);
		$trxOpdRefundOb->setLocationID($_SESSION['security']['location_id']);
		$trxOpdRefundOb->executeForIpdRefund();
		
		$redirect_url = 'ipdtransact.php';
		die("$redirect_url");
	}
	catch (MyException $e)
	{
		$error_message = $e->getUserMsg();
		die("$error_message");
	}
	
}

############################# INSERT blood cross match results ###########################################

if(isset($_POST['blood_crossmatcth_report'])&&$_POST['blood_crossmatcth_report']!='')
{
	try
	{
		$insertBCMResultOb = new classDiagnostic;
		$insertBCMResultOb->setDiagItemID($_POST['diagitems_id']);
		$insertBCMResultOb->setDiagInvoiceID($_POST['diaginvoice_id']);
		
		$insertBCMResultOb->setDonorName($_POST['donor_name']);
		$insertBCMResultOb->setBloodGroup($_POST['blood_group']);
		
		$insertBCMResultOb->setHepatitisBAntigen($_POST['hb_antigen']);
		$insertBCMResultOb->setHepatitisCAntigen($_POST['hc_antigen']);
		$insertBCMResultOb->setHIV($_POST['hiv']);
		$insertBCMResultOb->setMP($_POST['mp']);
		$insertBCMResultOb->setVDRL($_POST['vdrl']);
		
		$insertBCMResultOb->setRecipientName($_POST['recipient_name']);
		$insertBCMResultOb->setRecipientBloodGroup($_POST['recipient_bg']);
		
		$insertBCMResultOb->setRemarks($_POST['remarks']);
		
		if($_POST['bcm_id']=='')
		{
			$insertBCMResultOb->insertBloodCrossMatch();
		}
		else
		{
			$insertBCMResultOb->updateBloodCrossMatch();
		}
		
		$updateDiagResultOb = new classDiagnostic;
		$updateDiagResultOb->setDiagItemID($_POST['diagitems_id']);
		$updateDiagResultOb->setDiagnosisResult($_POST['blood_group']);
		$updateDiagResultOb->updateDiagItemsResult();
		
		$redirect_url = $_POST['redirect_url'];
		die("$redirect_url");
	}
	catch (MyException $e)
	{
		$error_message = $e->getUserMsg();
		die("$error_message");
	}	
	
}

############################# Allot ipd ward beds ###########################################################

if(isset($_POST['insert_appointment'])&&$_POST['insert_appointment']=='Insert_appointment')
{
	try
	{
		$allotAppOb = new classAppointment;
		$allotAppOb->setDoctorID($_POST['doctor_id']);
		$allotAppOb->setPatientID($_POST['patient_id']);
		$allotAppOb->setAppTimeIn($_POST['app_timein']);
		$allotAppOb->setAppTimeOut($_POST['app_timeout']);
		$allotAppOb->setAppPurpose($_POST['app_purpose']);
		
		$allotAppOb->setAppType($_POST['app_type']);
		$allotAppOb->setLocationID($_SESSION['security']['location_id']);
		$allotAppOb->setAppDate($_POST['app_date']);
		
		$allotAppOb->insertAppoitment();
		
		$date = $_POST['app_date'];
		$redirect_url = "mgmtappoint.php?date=$date";
		die("$redirect_url");
	}
	catch (MyException $e)
	{
		$error_message = $e->getUserMsg();
		die("$error_message");
	}
	
	
}


################## Retrieve all Appointments  ####################################

if((isset($_POST['search_appointments'])&&$_POST['search_appointments']=='Search')||isset($_GET['search_appointments'])&&$_GET['search_appointments']=='Search')
{
	if(isset($_GET['search_appointments']))
	{
		$_POST['doctor_id'] = $_GET['doctor_id'];
		$_POST['app_date'] = $_GET['app_date'];
		
	}
	
	try
	{
		$retrieveAppOb = new classAppointment;
		$retrieveAppOb->setLocationID($_SESSION['security']['location_id']);
		$retrieveAppOb->setDoctorID($_POST['doctor_id']);
		$retrieveAppOb->setAppDate($_POST['app_date']);
		$app_array = $retrieveAppOb->retrieveDoctorAppointmentsByDate();
		
		if(isset($app_array[0]['app_id'])&&$app_array[0]['app_id']!='')
		{
			for($app=0;$app<count($app_array);$app++)
			{
				if($app%2==0){ $class = 'gradeX';}else{$class = 'gradeC';}
				
				$pid = $app_array[$app]['patient_id'];
				
				$retrievePatientOb = new classPatient;
				$retrievePatientOb->setPatientAutoID($pid);
				$patient_info = $retrievePatientOb->retrievePatientByPatientID();
				
				$onsubmit = 'true';
				$class_of_button = 'btn btn-primary';
				
				?>
				
				<tr class="<?php echo $class;?>">
				
					<td align="center">
					<form action="" method="post" onsubmit="return <?php echo $onsubmit;?>">
						<input type="submit" name="select_patient" value="Select" id="<?php echo $app;?>" class="<?php echo $class_of_button;?>">
						<input type="hidden" name="patient_type" id="patient_type_<?php echo $app;?>" value="<?php echo $pid;?>">
						<input type="hidden" name="patient_name" id="patient_name_<?php echo $app;?>" value="<?php echo $patient_info[0]['patient_fname'].' '.$patient_info[0]['patient_lname'];?>">
					</form>
					</td>
					
					<td align="center"><?php echo $app_array[$app]['app_id'];?></td>
					<td align="center"><?php echo  date('h:i A',$app_array[$app]['app_timein']);?></td>
					<td align="center"><?php echo  date('h:i A',$app_array[$app]['app_timeout']);?></td>
					<td align="left"><?php echo $patient_info[0]['patient_fname'].' '.$patient_info[0]['patient_lname'];?></td>
					<td align="center"><?php echo $patient_info[0]['phone'];?></td>
					<td align="center"><?php echo date('d M Y',$patient_info[0]['reg_date']);?></td>
				   
				</tr>
				
				<?php
			}
		}
		else
		{
			?>
            <tr><td colspan="7">No Entry Found!</tr>
			<?php
			
		}
	}
	catch (MyException $e)
	{
		$error_message = $e->getUserMsg();
		die("$error_message");
	}
}


################## INSERT UPDATE LIQUID  ####################################

if(isset($_POST['add_labtest_liquids'])&&$_POST['add_labtest_liquids']=='Add_labtest_liquids')
{
	try
	{
		$insertLiqOb = new classDiagnosticLiquids;
		$insertLiqOb->setLocationID($_SESSION['security']['location_id']);
		$insertLiqOb->setLiquidName($_POST['liquid_name']);
		$insertLiqOb->setAmountPerBottle($_POST['amount_per_bottle']);
		$insertLiqOb->setPricePerBottle($_POST['price_per_bottle']);
		$insertLiqOb->setQtyUsedPerTest($_POST['test_per_bottle']);
		$insertLiqOb->setLabTestID(@$_POST['labtest_id']);
		$insertLiqOb->insertLiquid();
		
		$redirect_url = 'mgmtlabliquids.php';
		die("$redirect_url");
	}
	catch (MyException $e)
	{
		$error_message = $e->getUserMsg();
		die("$error_message");
	}
}

if(isset($_POST['update_labtest_liquids'])&&$_POST['update_labtest_liquids']=='Update_labtest_liquids')
{
	try
	{
		$updateLiqOb = new classDiagnosticLiquids;
		$updateLiqOb->setLocationID($_SESSION['security']['location_id']);
		$updateLiqOb->setLiquidName($_POST['liquid_name']);
		$updateLiqOb->setAmountPerBottle($_POST['amount_per_bottle']);
		$updateLiqOb->setPricePerBottle($_POST['price_per_bottle']);
		$updateLiqOb->setQtyUsedPerTest($_POST['test_per_bottle']);
		$updateLiqOb->setLabTestID(@$_POST['labtest_id']);
		$updateLiqOb->setLiquidID($_POST['liquid_id']);
		$updateLiqOb->updateLiquid();
		
		$redirect_url = 'mgmtlabliquids.php';
		die("$redirect_url");
	}
	catch (MyException $e)
	{
		$error_message = $e->getUserMsg();
		die("$error_message");
	}
}


################## INSERT LIQUID RECEIVING  ####################################

if(isset($_POST['receive_labtest_liquids'])&&$_POST['receive_labtest_liquids']=='Receive_labtest_liquids')
{
	try
	{
		$insertLiqRecOb = new classDiagnosticLiquids;
		$insertLiqRecOb->setUserID($_SESSION['security']['user_id']);
		$insertLiqRecOb->setLiquidID($_POST['liquid_id']);
		$insertLiqRecOb->setQty($_POST['no_of_bottles']);
		$insertLiqRecOb->insertLiquidReceiving();
		
		$redirect_url = 'mgmtlabliquids.php';
		die("$redirect_url");
	}
	catch (MyException $e)
	{
		$error_message = $e->getUserMsg();
		die("$error_message");
	}
}

################## INSERT LIQUID RECEIVING  ####################################

if(isset($_POST['reset_labtest_liquids'])&&$_POST['reset_labtest_liquids']=='Reset_labtest_liquids')
{
	try
	{
		$resetLiqOb = new classDiagnosticLiquids;
		$resetLiqOb->setLiquidID($_POST['liquid_id']);
		$resetLiqOb->setActualQty($_POST['actual_qty']);
		$resetLiqOb->setExpectedQty($_POST['expected_qty']);
		$resetLiqOb->setTestPerformed($_POST['no_of_test_performed']);
		$resetLiqOb->setQty($_POST['no_of_bottle_rec']);
		$resetLiqOb->resetLiquid();
		
		$redirect_url = 'mgmtlabliquids.php';
		die("$redirect_url");
	}
	catch (MyException $e)
	{
		$error_message = $e->getUserMsg();
		die("$error_message");
	}
}

################## INSERT UPDATE MEDICAL ISSUES  ####################################

if(isset($_POST['insert_medical_issues'])&&$_POST['insert_medical_issues']=='Insert_medical_issues')
{
	try
	{
		$insertMedIssOb = new classVisits;
		$insertMedIssOb->setMedicalIssueName($_POST['medical_issue_name']);
		$insertMedIssOb->setOrganizationID($_SESSION['security']['org_id']);
		$insertMedIssOb->insertMedicalIssues();
		
		$redirect_url = 'mgmtmedissues.php';
		die("$redirect_url");
	}
	catch (MyException $e)
	{
		$error_message = $e->getUserMsg();
		die("$error_message");
	}
}
if(isset($_POST['update_medical_issues'])&&$_POST['update_medical_issues']=='Update_medical_issues')
{
	try
	{
		$updateMedIssOb = new classVisits;
		$updateMedIssOb->setMedicalIssueID($_POST['medical_issue_id']);
		$updateMedIssOb->setMedicalIssueName($_POST['medical_issue_name']);
		$updateMedIssOb->updateMedicalIssues();
		
		$redirect_url = 'mgmtmedissues.php';
		die("$redirect_url");
	}
	catch (MyException $e)
	{
		$error_message = $e->getUserMsg();
		die("$error_message");
	}
}

################## UPDATE MEDICAL ISSUES IN VISIT  ####################################

if(isset($_GET['change_visit_medissue'])&&$_GET['change_visit_medissue']=='yes')
{
	try
	{
		$changeMedIssOb = new classVisits;
		$changeMedIssOb->setVisitID($_GET['visit_id']);
		$changeMedIssOb->setMedicalIssueID($_GET['med_issue_id']);
		$changeMedIssOb->updateMedicalIssueInVisit();
	}
	catch (MyException $e)
	{
		$error_message = $e->getUserMsg();
		die("$error_message");
	}
}

################## INSERT UPDATE DOSAGE  ####################################

if(isset($_POST['insert_dosage'])&&$_POST['insert_dosage']=='Insert_dosage')
{
	try
	{
		$insertDoseOb = new classVisits;
		$insertDoseOb->setDosage($_POST['dosage']);
		$insertDoseOb->setOrganizationID($_SESSION['security']['org_id']);
		$insertDoseOb->insertDosage();
		
		$redirect_url = 'mgmtdosage.php';
		die("$redirect_url");
	}
	catch (MyException $e)
	{
		$error_message = $e->getUserMsg();
		die("$error_message");
	}
}
if(isset($_POST['update_dosage'])&&$_POST['update_dosage']=='Update_dosage')
{
	try
	{
		$updateDoseOb = new classVisits;
		$updateDoseOb->setDosageID($_POST['dosage_id']);
		$updateDoseOb->setDosage($_POST['dosage']);
		$updateDoseOb->updateDosageName();
		
		$redirect_url = 'mgmtdosage.php';
		die("$redirect_url");
	}
	catch (MyException $e)
	{
		$error_message = $e->getUserMsg();
		die("$error_message");
	}
}


################## INSERT UPDATE DIETS  ####################################

if(isset($_POST['insert_diet'])&&$_POST['insert_diet']=='insert_diet')
{
	try
	{
		$insertDietOb = new classVisits;
		$insertDietOb->setDiet($_POST['diet_name']);
		$insertDietOb->setOrganizationID($_SESSION['security']['org_id']);
		$insertDietOb->insertDiet();
		
		$redirect_url = 'mgmtdiets.php';
		die("$redirect_url");
	}
	catch (MyException $e)
	{
		$error_message = $e->getUserMsg();
		die("$error_message");
	}
}

if(isset($_POST['insert_diet_presc'])&&$_POST['insert_diet_presc']=='insert_diet_presc')
{
	try
	{
		$insertDietOb = new classVisits;
		$insertDietOb->setDiet($_POST['diet_name']);
		$insertDietOb->setOrganizationID($_SESSION['security']['org_id']);
		$insertDietOb->insertDiet();
		
		$redirect_url = $_SERVER['HTTP_REFERER'];
		die("$redirect_url");
	}
	catch (MyException $e)
	{
		$error_message = $e->getUserMsg();
		die("$error_message");
	}
}

if(isset($_POST['update_diet'])&&$_POST['update_diet']=='update_diet')
{
	try
	{
		$updateDietOb = new classVisits;
		$updateDietOb->setDietID($_POST['diet_id']);
		$updateDietOb->setDiet($_POST['diet_name']);
		$updateDietOb->updateDietName();
		  
		$redirect_url = 'mgmtdiets.php';
		die("$redirect_url");
	}
	catch (MyException $e)
	{
		$error_message = $e->getUserMsg();
		die("$error_message");
	}
}


################## INSERT UPDATE DEXERCISE  ####################################

if(isset($_POST['insert_exercise'])&&$_POST['insert_exercise']=='insert_exercise')
{
	try
	{
		$insertExerciseOb = new classVisits;
		$insertExerciseOb->setExercise($_POST['exercise_name']);
		$insertExerciseOb->setOrganizationID($_SESSION['security']['org_id']);
		$insertExerciseOb->insertExercise();
		
		$redirect_url = 'mgmtexercise.php';
		die("$redirect_url");
	}
	catch (MyException $e)
	{
		$error_message = $e->getUserMsg();
		die("$error_message");
	}
}

if(isset($_POST['insert_exercise_presc'])&&$_POST['insert_exercise_presc']=='insert_exercise_presc')
{
	try
	{
		$insertExerciseOb = new classVisits;
		$insertExerciseOb->setExercise($_POST['exercise_name']);
		$insertExerciseOb->setOrganizationID($_SESSION['security']['org_id']);
		$insertExerciseOb->insertExercise();
		
		$redirect_url = $_SERVER['HTTP_REFERER'];
		die("$redirect_url");
	}
	catch (MyException $e)
	{
		$error_message = $e->getUserMsg();
		die("$error_message");
	}
}

if(isset($_POST['update_exercise'])&&$_POST['update_exercise']=='update_exercise')
{
	try
	{
		$updateExerciseOb = new classVisits;
		$updateExerciseOb->setExerciseID($_POST['exercise_id']);
		$updateExerciseOb->setExercise($_POST['exercise_name']);
		$updateExerciseOb->updateExerciseName();
		  
		$redirect_url = 'mgmtexercise.php';
		die("$redirect_url");
	}
	catch (MyException $e)
	{
		$error_message = $e->getUserMsg();
		die("$error_message");
	}
}


################## INSERT UPDATE DRESSING  ####################################

if(isset($_POST['insert_dressing'])&&$_POST['insert_dressing']=='Insert_dressing')
{
	try
	{
		$insertDressOb = new classVisits;
		$insertDressOb->setDressing($_POST['dressing']);
		$insertDressOb->setOrganizationID($_SESSION['security']['org_id']);
		$insertDressOb->insertDressing();
		
		$redirect_url = 'mgmtdressing.php';
		die("$redirect_url");
	}
	catch (MyException $e)
	{
		$error_message = $e->getUserMsg();
		die("$error_message");
	}
}

if(isset($_POST['insert_dressing_presc'])&&$_POST['insert_dressing_presc']=='Insert_dressing_presc')
{
	try
	{
		$insertDressOb = new classVisits;
		$insertDressOb->setDressing($_POST['dressing']);
		$insertDressOb->setOrganizationID($_SESSION['security']['org_id']);
		$insertDressOb->insertDressing();
		
		$redirect_url = 'addpresc.php';
		die("$redirect_url");
	}
	catch (MyException $e)
	{
		$error_message = $e->getUserMsg();
		die("$error_message");
	}
}

if(isset($_POST['update_dressing'])&&$_POST['update_dressing']=='Update_dressing')
{
	try
	{
		$updateDressOb = new classVisits;
		$updateDressOb->setDressingID($_POST['dressing_id']);
		$updateDressOb->setDressing($_POST['dressing']);
		$updateDressOb->updateDressingName();
		
		$redirect_url = 'mgmtdressing.php';
		die("$redirect_url");
	}
	catch (MyException $e)
	{
		$error_message = $e->getUserMsg();
		die("$error_message");
	}
}


################## INSERT UPDATE SERVICES  ####################################

if(isset($_POST['add_opd_service'])&&$_POST['add_opd_service']=='Add_opd_service')
{
	try
	{
		$insertServOb = new classVisits;
		$insertServOb->setServiceName($_POST['service_name']);
		$insertServOb->setServicePrice($_POST['service_price']);
		$insertServOb->setServiceType($_POST['service_type']);
		$insertServOb->setOrganizationID($_SESSION['security']['org_id']);
		$insertServOb->insertServices();
		
		$redirect_url = $_SERVER['HTTP_REFERER'];
		die("$redirect_url");
	}
	catch (MyException $e)
	{
		$error_message = $e->getUserMsg();
		die("$error_message");
	}
}



if(isset($_POST['update_opd_service'])&&$_POST['update_opd_service']=='Update_opd_service')
{
	try
	{
		$updateServOb = new classVisits;
		$updateServOb->setServiceName($_POST['service_name']);
		$updateServOb->setServicePrice($_POST['service_price']);
		$updateServOb->setServiceID($_POST['service_id']);
		$updateServOb->setServiceType($_POST['service_type']);
		$updateServOb->updateServices();
		
		$redirect_url = $_SERVER['HTTP_REFERER'];
		die("$redirect_url");
	}
	catch (MyException $e)
	{
		$error_message = $e->getUserMsg();
		die("$error_message");
	}
}

################## UPDATE PRESC TYPE  ####################################

if(isset($_POST['update_presc_type'])&&$_POST['update_presc_type']=='Update_presc_type')
{
	try
	{
		$updatePrescTypeOb = new classVisits;
		$updatePrescTypeOb->updatePrescType($_POST['visit_id'],$_POST['presc_type']);
		
		$redirect_url = $updatePrescTypeOb->retrievePrescType($_POST['visit_id']);
		die("$redirect_url");
	}
	catch (MyException $e)
	{
		$error_message = $e->getUserMsg();
		die("$error_message");
	}
}



############################# Add Store Item ###########################################################

try
{
	if(isset($_POST['add_store_item'])&&$_POST['add_store_item']=='Add_Store_Item')
	{
		$addStoreItemOb = new classStoreItem;
		
		$addStoreItemOb->setStoreItemName($_POST['storeitem_name']);	
		$addStoreItemOb->setManufacturerID($_POST['manf_id']);
		$addStoreItemOb->setStoreItemCategory($_POST['storeitem_category']);
		$addStoreItemOb->setStoreItemReorderQty($_POST['storeitem_reorder_qty']);
		$addStoreItemOb->setOrganizationID($_SESSION['security']['org_id']);
		$addStoreItemOb->setLocationID($_SESSION['security']['location_id']);
		
		if(isset($_POST['storeitem_id'])&&$_POST['storeitem_id']!='')
		{
			$addStoreItemOb->setStoreItemID($_POST['storeitem_id']);
			$addStoreItemOb->updateStoreItem();
		}
		else
		{
			$addStoreItemOb->insertStoreItem();
		}
		
		$redirect_url = 'addstoreitem.php';
		die("$redirect_url");
		exit;
		
	}
}
catch (MyException $e)
{
	$error_message = $e->getUserMsg();
	die("$error_message");
}


############################# Calculate Receive Item Detail ###########################################	

if(isset($_GET['receive_storeitems'])&&isset($_GET['storeitem_qty'])&&isset($_GET['storeitem_id']))
{
	try
	{
		
		$recitems_id = $_GET['recitems_id'];
		$storeitem_qty = $_GET['storeitem_qty'];
		$expiry_date = $_GET['expiry_date'];
		$storeitem_id = $_GET['storeitem_id'];
		
		$updateRecStoreItemsOb = new classStoreItemReceiveInvoice;
		$updateRecStoreItemsOb->setReceivedQty($storeitem_qty);
		$updateRecStoreItemsOb->setReceiveItemID($recitems_id);
		$updateRecStoreItemsOb->setExpiryDate($expiry_date);
		$updateRecStoreItemsOb->setStoreItemID($storeitem_id);
		$updateRecStoreItemsOb->setLocationID($_SESSION['security']['location_id']);
		$updateRecStoreItemsOb->updateReceiveItemsInInvoice();
		
		//die("");
	}
	catch (MyException $e)
	{
		$error_message = $e->getUserMsg();
		die("$error_message");
	}	
	
}


############################# Calculate Sale Item Detail ###########################################	

if(isset($_GET['sale_storeitems'])&&isset($_GET['storeitemsale_qty'])&&isset($_GET['storeitem_id']))
{
	try
	{
		
		$saleitems_id = $_GET['saleitems_id'];
		$storeitemsale_qty = $_GET['storeitemsale_qty'];
		$storeitem_id = $_GET['storeitem_id'];
		
		$updateSaleStoreItemsOb = new classStoreItemSaleInvoice;
		$updateSaleStoreItemsOb->setSaleQty($storeitemsale_qty);
		$updateSaleStoreItemsOb->setSaleItemID($saleitems_id);
		$updateSaleStoreItemsOb->setStoreItemID($storeitem_id);
		$updateSaleStoreItemsOb->setLocationID($_SESSION['security']['location_id']);
		$updateSaleStoreItemsOb->updateSaleItemsInInvoice();
		
		//die("");
	}
	catch (MyException $e)
	{
		$error_message = $e->getUserMsg();
		die("$error_message");
	}	
	
}

############################# Activate Store Items ###########################################################	


if((isset($_POST['activate_storeitems'])&&$_POST['activate_storeitems']=='activate_storeitems')&&isset($_POST['active_items']))
{
	try
	{
		foreach($_POST['active_items'] as $key => $value)
		{
			$storeItemStatusOb = new classStoreItem;
			$storeItemStatusOb->setStoreItemID($key);
			$storeItemStatusOb->setActive($value);
			$storeItemStatusOb->updateStoreItemStatus();
		}
		
		$redirect_url = 'otdel.php';
		die("$redirect_url");
	}
	catch (MyException $e)
	{
		$error_message = $e->getUserMsg();
		die("$error_message");
	}
}


############################# Verify Store Items ###########################################################	


if(isset($_POST['verify_rec_invoice'])&&$_POST['verify_rec_invoice']=='yes')
{
	try
	{
		
		$verifyRecInvOb = new classStoreItemReceiveInvoice;
		$verifyRecInvOb->setReceiveInvoiceID($_POST['recinvoice_id']);
		$verifyRecInvOb->setVerifiedByName($_POST['verify_by_name']);
		$verifyRecInvOb->setVerifiedByID($_SESSION['security']['user_id']);
		$verifyRecInvOb->verfiyReceiveInvoice();
		
		
		$redirect_url = 'mgmtreceiving.php';
		die("$redirect_url");
	}
	catch (MyException $e)
	{
		$error_message = $e->getUserMsg();
		die("$error_message");
	}
}

############################# Delivere Store Items ###########################################################	


if(isset($_POST['delivere_sale_invoice'])&&$_POST['delivere_sale_invoice']=='yes')
{
	try
	{
		
		$delSaleInvOb = new classStoreItemSaleInvoice;
		$delSaleInvOb->setSaleInvoiceID($_POST['saleinvoice_id']);
		$delSaleInvOb->setDelivereByName($_POST['delivere_by_name']);
		$delSaleInvOb->setDelivereByID($_SESSION['security']['user_id']);
		$delSaleInvOb->delivereSaleInvoice();
		
		
		$redirect_url = 'mgmtrequisitions.php';
		die("$redirect_url");
	}
	catch (MyException $e)
	{
		$error_message = $e->getUserMsg();
		die("$error_message");
	}
}

############################# Refund Sale Item Detail ###########################################	

if(isset($_GET['refund_storeitems'])&&isset($_GET['storeitemsale_refund_qty'])&&isset($_GET['storeitem_id']))
{
	try
	{
		
		$saleitems_id = $_GET['saleitems_id'];
		$storeitemsale_refund_qty = $_GET['storeitemsale_refund_qty'];
		$storeitem_id = $_GET['storeitem_id'];
		
		$refundSaleStoreItemsOb = new classStoreItemSaleInvoice;
		$refundSaleStoreItemsOb->setRefundQty($storeitemsale_refund_qty);
		$refundSaleStoreItemsOb->setSaleItemID($saleitems_id);
		$refundSaleStoreItemsOb->setStoreItemID($storeitem_id);
		$refundSaleStoreItemsOb->setLocationID($_SESSION['security']['location_id']);
		$refundSaleStoreItemsOb->refundSaleItemsInInvoice();
		
	}
	catch (MyException $e)
	{
		$error_message = $e->getUserMsg();
		die("$error_message");
	}	
	
}



############################# Adjustment Store Item Detail ###########################################	

if(isset($_POST['adjustment_store_items'])&&$_POST['adjustment_store_items']=='yes')
{
	try
	{
		$adjStoreItemsOb = new classStoreItem;
		$adjStoreItemsOb->setStoreItemAdjustedQty($_POST['adjusted_qty']);
		$adjStoreItemsOb->setStoreItemID($_POST['storeitem_id']);
		$adjStoreItemsOb->setLocationID($_SESSION['security']['location_id']);
		$adjStoreItemsOb->setUserID($_SESSION['security']['user_id']);
		$adjStoreItemsOb->insertStoreItemAdjustments();
		
		$redirect_url = 'mgmtstore.php';
		die("$redirect_url");
	
	}
	catch (MyException $e)
	{
		$error_message = $e->getUserMsg();
		die("$error_message");
	}	
	
}


############################# ADD ACCOUNT HEADS ###########################################	

if(isset($_POST['add_acchead'])&&$_POST['add_acchead']=='Add_Acchead')
{
	try
	{
		$addAccountOb = new classAccounts;
		$addAccountOb->setOrganizationID($_SESSION['security']['org_id']);
		$addAccountOb->setAccountHeadNumber($_POST['acchead_no']);
		$addAccountOb->setAccountHeadName($_POST['acchead_name']);
		$addAccountOb->setAccountHeadIdentifier($_POST['acchead_identifier']);
		if(isset($_POST['acchead_blocked']))
		{
			$acchead_blocked = 1;
		}
		else
		{
			$acchead_blocked = 0;
		}
		$addAccountOb->setAccountHeadBlocked($acchead_blocked);
		$addAccountOb->insertAccountHeads();
		
		
		$redirect_url = 'mgmtaccounts.php';
		die("$redirect_url");
	
	}
	catch (MyException $e)
	{
		$error_message = $e->getUserMsg();
		die("$error_message");
	}	
	
}

if(isset($_POST['update_acchead'])&&$_POST['update_acchead']=='Update_Acchead')
{
	
	try
	{
		$updateAccountOb = new classAccounts;
		$updateAccountOb->setOrganizationID($_SESSION['security']['org_id']);
		$updateAccountOb->setAccountHeadNumber($_POST['acchead_no']);
		$updateAccountOb->setAccountHeadName($_POST['acchead_name']);
		$updateAccountOb->setAccountHeadIdentifier($_POST['acchead_identifier']);
		if(isset($_POST['acchead_blocked']))
		{
			$acchead_blocked = 1;
		}
		else
		{
			$acchead_blocked = 0;
		}
		$updateAccountOb->setAccountHeadBlocked($acchead_blocked);
		$updateAccountOb->setAccountHeadID($_POST['acchead_id']);
		$updateAccountOb->updateAccountHeads();
		
		
		$redirect_url = 'mgmtaccounts.php';
		die("$redirect_url");
	
	}
	catch (MyException $e)
	{
		$error_message = $e->getUserMsg();
		die("$error_message");
	}	
	
}

############################# Add And Update Panel  #################################################################


try
{
	if(isset($_POST['add_pan'])&&$_POST['add_pan']=='Add_Panel')
	{
		$addPanelOb = new classPanel;
		$addPanelOb->setPanelType($_POST['type']);
		$addPanelOb->setPanelCompany($_POST['company']);
		$addPanelOb->setCompanyDiscount($_POST['discount']);
		$addPanelOb->setOrganizationID($_SESSION['security']['org_id']);
		$addPanelOb->insertPanel();
		
		$redirect_url = 'mgmtpanels.php';
		die("$redirect_url");
	}
	if(isset($_POST['update_pan'])&&$_POST['update_pan']=='Update_Panel')
	{
		$updatePanelOb = new classPanel;
		$updatePanelOb->setPanelType($_POST['type']);
		$updatePanelOb->setPanelCompany($_POST['company']);
		$updatePanelOb->setCompanyDiscount($_POST['discount']);
		$updatePanelOb->setPanelID($_POST['panel_id']);
		$updatePanelOb->setOrganizationID($_SESSION['security']['org_id']);
		$updatePanelOb->updatePanel();
		
		$redirect_url = 'mgmtpanels.php';
		die("$redirect_url");
	}
}
catch (MyException $e)
{
	$error_message = $e->getUserMsg();
	die("$error_message");
}	

		

?>
<script>
$(".select_patient").click(function() {
	
	var id = this.id;
	//alert(id);
	patient_name = $("#patient_name_"+id).val();
	patient_id = $("#patient_type_"+id).val();

	$("#patient_name").html(patient_name);
	$("#patient_id").val(patient_id);
	
	$('#mask,.select_patient-popup').fadeOut(300 , function() {
		  
		$('#mask').remove();  
	});  
});

$(document).ready(function() {
    $('#selecctall').click(function(event) {  //on click
        if(this.checked) { // check select status
            $('.checkboxes').each(function() { //loop through each checkbox
                this.checked = true;  //select all checkboxes with class "checkbox1"              
            });
        }else{
            $('.checkboxes').each(function() { //loop through each checkbox
                this.checked = false; //deselect all checkboxes with class "checkbox1"                      
            });        
        }
    });
   
});

</script>
   
