<?php   
    require "commonincludefiles.php";
	
	$action 				= $_REQUEST['action'];
	
	$account_id 			= $_REQUEST['accountcode'];
	$account_name 			= $_REQUEST['accountname'];
	$account_type 			= $_REQUEST['accounttype'];
	$account_effect 		= $_REQUEST['accounteffect'];
	$account_txtopbalance 	= $_REQUEST['txtopbalance'];
	$account_opbalance 		= $_REQUEST['opbal'];
	$account_address 		= $_REQUEST['address'];
	$account_phone 			= $_REQUEST['phone'];
	$account_groupid 		= $_REQUEST['accgroupid'];
	$subgroup_id 			= $_REQUEST['accsubgroupid'];
	
	if ($_SESSION['cCompany_id']!="" && isset($_SESSION['cCompany_id'])) {
		
		$AccountMasterMgmt = new AccountMasterMgmt();
		
		if($action == "save"){
			$AccountMasterMgmt->account_groupid		=$account_groupid;
			$AccountMasterMgmt->account_type		=$account_type;
			$AccountMasterMgmt->account_name		=$account_name;
			$AccountMasterMgmt->account_opbalance	=$account_txtopbalance;
			$AccountMasterMgmt->account_crdr		=$account_opbalance;
			$AccountMasterMgmt->account_baldate   	=date("Y-m-d");
			$AccountMasterMgmt->account_address		=$account_address;
			$AccountMasterMgmt->account_phone		=$account_phone;
			$AccountMasterMgmt->account_effect		=$account_effect;
			$AccountMasterMgmt->subgroup_id			=$subgroup_id;
			$AccountMasterMgmt->add();			
			echo '{"success":"true","msg":"Data has been saved..."}';	
		}
		else if($action == "delete"){
		
			////
			$str_voucherid = "";
			$str_accountid = "";
		
			$qry1 = "SELECT am.account_id 
					FROM ams_accountmaster am
					WHERE am.company_id = '".$_SESSION["cCompany_id"]."' AND am.account_id = $account_id ";
					$result = dbQuery($qry1);
			
			if(dbRows($result)>0){
				$record_account = dbArray($result);
				$str_accountid = $record_account['account_id'];
				if($str_accountid!=""){
					$qry2 = "SELECT group_concat(vm.voucher_id) AS voucher_id
							FROM ams_vouchermaster vm,ams_voucherdetail vd
							WHERE vm.company_id = '".$_SESSION["cCompany_id"]."' 
								AND vd.company_id = '".$_SESSION["cCompany_id"]."' 
								AND vm.voucher_id=vd.voucher_id 
								AND vd.account_id in($str_accountid) ";
					$result = dbQuery($qry2);
					if(dbRows($result)>0){
						$record_voucher = dbArray($result);
						$str_voucherid = $record_voucher['voucher_id'];
					}
				}
			}
			
		
			
			if($str_accountid!=""){
				$qry3 = "delete from ams_accountmasterdetail where account_id in ($str_accountid) And company_id='".$_SESSION["cCompany_id"]."'";
				$qry4 = "delete from ams_accountmaster where account_id in ($str_accountid) And company_id='".$_SESSION["cCompany_id"]."'";
				
				
				dbQuery($qry3);
				dbQuery($qry4);
			}
			
			if($str_voucherid != ""){
				$qry5 = "delete from ams_vouchermaster where voucher_id in ($str_voucherid) And company_id='".$_SESSION["cCompany_id"]."'";
				$qry6 = "delete from ams_voucherdetail where voucher_id in ($str_voucherid) And company_id='".$_SESSION["cCompany_id"]."'";
				dbQuery($qry5);
				dbQuery($qry6);
			}
			echo '{"success":"true","msg":"Data has been deleted..."}';	

			////
		
			//$AccountMasterMgmt->account_id 			=$account_id;
			//$AccountMasterMgmt->delete();
			//echo '{"success":"true","msg":"Data has been deleted..."}';	
		}
		else{ // edit code
		           $account_phone = replaceSpecial($account_phone);
			
			$AccountMasterMgmt->account_id			=$account_id;
			$AccountMasterMgmt->account_groupid		=$account_groupid;
			$AccountMasterMgmt->account_type		=$account_type;
			$AccountMasterMgmt->account_name		=$account_name;
			$AccountMasterMgmt->account_baldate   	=date("Y-m-d");
			$AccountMasterMgmt->account_opbalance	=$account_txtopbalance;
			$AccountMasterMgmt->account_crdr		=$account_opbalance;
			$AccountMasterMgmt->account_address		=$account_address;
			$AccountMasterMgmt->account_phone		=$account_phone;
			$AccountMasterMgmt->account_effect		=$account_effect;
			$AccountMasterMgmt->subgroup_id			=$subgroup_id;
			$AccountMasterMgmt->edit();
			echo '{"success":"true","msg":"Data has been edited..."}';	
		}
	} 
	else {
		echo '{"success":"false","msg":"session expiry"}';
	}
?>