from appservices.common.util import *
import xml.etree.ElementTree as ET



def getStations(common_url,base_url,xmlurl,username,password):
    data_status={"responseStatus":0,"message":""}

    try:
        headers={
            "Content-Type": "text/xml; charset=utf-8",
            "SOAPAction": base_url+"GetStationsV1",
        }
        data = f"""<?xml version="1.0" encoding="utf-8"?>
        <soap:Envelope xmlns:soap="{xmlurl}" xmlns:tns="{base_url}">
            <soap:Body>
                <tns:GetStations>
                    <tns:username>{username}</tns:username>
                    <tns:password>{password}</tns:password>
                </tns:GetStations>
            </soap:Body>
        </soap:Envelope>"""

        response=requests.post(common_url,headers=headers,data=data.encode("utf-8"))

        root = ET.fromstring(response.text)
        ns_tag = f".//{{{base_url}}}GetStationsResult"
        json_data = root.find(ns_tag)
       
        if json_data is not None:
            result_dict = json.loads(json_data.text)
            if result_dict.get("status")=="success":
                data_status["responseStatus"]=1
                data_status["message"]="Stations retrieved successfully"
                data_status["data"]=result_dict.get("stations")
            else:
                data_status["responseStatus"]=0
                data_status["message"]=result_dict.get("Message")
        else:
            data_status["responseStatus"]=0
            data_status["message"]="Error: Unable to retrieve abhibuses."
        return data_status

    except Exception as e:
        app.logger.error("Error during request preparation: " + traceback.format_exc())
        return {"responseStatus":0,"message":"Error: Unable to retrieve abhibuses."}



def GetStationsV1(common_url,base_url,xmlurl,username,password):
    data_status={"responseStatus":0,"message":""}

    try:
        headers={
            "Content-Type": "text/xml; charset=utf-8",
            "SOAPAction": base_url+"GetStationsV1",
        }
        data = f"""<?xml version="1.0" encoding="utf-8"?>
        <soap:Envelope xmlns:soap="{xmlurl}" xmlns:tns="{base_url}">
            <soap:Body>
                <tns:GetStationsV1>
                    <tns:username>{username}</tns:username>
                    <tns:password>{password}</tns:password>
                </tns:GetStationsV1>
            </soap:Body>
        </soap:Envelope>"""

        response=requests.post(common_url,headers=headers,data=data.encode("utf-8"))

        root = ET.fromstring(response.text)
        ns_tag = f".//{{{base_url}}}GetStationsV1Result"
        json_data = root.find(ns_tag)
       
        if json_data is not None:
            result_dict = json.loads(json_data.text)
            if result_dict.get("status")=="success":
                data_status["responseStatus"]=1
                data_status["message"]="Stations retrieved successfully"
                data_status["data"]=result_dict.get("stations")
            else:
                data_status["responseStatus"]=0
                data_status["message"]=result_dict.get("Message")
        else:
            data_status["responseStatus"]=0
            data_status["message"]="Error: Unable to retrieve abhibuses."
        return data_status

    except Exception as e:
        app.logger.error("Error during request preparation: " + traceback.format_exc())
        return {"responseStatus":0,"message":"Error: Unable to retrieve abhibuses."}



def getOperators(common_url,base_url,xmlurl,username,password):
    data_status={"responseStatus":0,"message":""}

    try:
        headers={
            "Content-Type": "text/xml; charset=utf-8",
            "SOAPAction": base_url+"GetOperators",
        }
        data = f"""<?xml version="1.0" encoding="utf-8"?>
        <soap:Envelope xmlns:soap="{xmlurl}" xmlns:tns="{base_url}">
            <soap:Body>
                <tns:GetOperators>
                    <tns:username>{username}</tns:username>
                    <tns:password>{password}</tns:password>
                </tns:GetOperators>
            </soap:Body>
        </soap:Envelope>"""

        response=requests.post(common_url,headers=headers,data=data.encode("utf-8"))
        print(response.text)
        root = ET.fromstring(response.text)
        ns_tag = f".//{{{base_url}}}GetOperatorsResult"
        json_data = root.find(ns_tag)
       
        if json_data is not None:
            result_dict = json.loads(json_data.text)
            if result_dict.get("status")=="success":
                data_status["responseStatus"]=1
                data_status["message"]="Stations retrieved successfully"
                data_status["data"]=result_dict.get("operatorsInfo")
            else:
                data_status["responseStatus"]=0
                data_status["message"]=result_dict.get("Message")
        else:
            data_status["responseStatus"]=0
            data_status["message"]="Error: Unable to retrieve abhibuses."
        return data_status

    except Exception as e:
        app.logger.error("Error during request preparation: " + traceback.format_exc())
        return {"responseStatus":0,"message":"Error: Unable to retrieve abhibuses."}



def getCancelationPolicy(common_url,base_url,xmlurl,username,password,operatorId,serviceId,sourceStationId,destinationStationId,journeyDate):
    data_status={"responseStatus":0,"message":""}

    try:
        headers={
            "Content-Type": "text/xml; charset=utf-8",
            "SOAPAction": base_url+"GetCancellationPolicyV4",
        }
        data = f"""<?xml version="1.0" encoding="utf-8"?>
        <soap:Envelope xmlns:soap="{xmlurl}" xmlns:tns="{base_url}">
            <soap:Body>
                <tns:GetCancellationPolicyV4>
                    <tns:username>{username}</tns:username>
                    <tns:password>{password}</tns:password>
                    <operatorId>{operatorId}</operatorId>
                    <serviceId>{serviceId}</serviceId>
                    <sourceStationId>{sourceStationId}</sourceStationId>
                    <destinationStationId>{destinationStationId}</destinationStationId>
                    <journeyDate>{journeyDate}</journeyDate>
                </tns:GetCancellationPolicyV4>
            </soap:Body>
        </soap:Envelope>"""

        response=requests.post(common_url,headers=headers,data=data.encode("utf-8"))
        print(response.text)
        root = ET.fromstring(response.text)
        ns_tag = f".//{{{base_url}}}GetCancellationPolicyV4Result"
        json_data = root.find(ns_tag)
       
        if json_data is not None:
            result_dict = json.loads(json_data.text)
            if result_dict.get("status")=="success":
                data_status["responseStatus"]=1
                data_status["message"]="Stations retrieved successfully"
                data_status["data"]=result_dict.get("Cancellationpy").get("Cancellationpy").get("conditions")
            else:
                data_status["responseStatus"]=0
                data_status["message"]=result_dict.get("Message")
        else:
            data_status["responseStatus"]=0
            data_status["message"]="Error: Unable to retrieve abhibuses."
        return data_status

    except Exception as e:
        app.logger.error("Error during request preparation: " + traceback.format_exc())
        return {"responseStatus":0,"message":"Error: Unable to retrieve abhibuses."}

def getAllServicesAvailable(common_url,base_url,xmlurl,username,password,operatorId,serviceId,sourceStationId,destinationStationId,journeyDate):
    data_status={"responseStatus":0,"message":""}

    try:
        headers={
            "Content-Type": "text/xml; charset=utf-8",
            "SOAPAction": base_url+"GetAvailableServicesV4",
        }
        data = f"""<?xml version="1.0" encoding="utf-8"?>
        <soap:Envelope xmlns:soap="{xmlurl}" xmlns:tns="{base_url}">
            <soap:Body>
                <tns:GetAvailableServicesV4>
                    <tns:username>{username}</tns:username>
                    <tns:password>{password}</tns:password>
                    <operatorId>{operatorId}</operatorId>
                    <serviceId>{serviceId}</serviceId>
                    <sourceStationId>{sourceStationId}</sourceStationId>
                    <destinationStationId>{destinationStationId}</destinationStationId>
                    <journeyDate>{journeyDate}</journeyDate>
                </tns:GetAvailableServicesV4>
            </soap:Body>
        </soap:Envelope>"""

        response=requests.post(common_url,headers=headers,data=data.encode("utf-8"))
        print(response.text)
        root = ET.fromstring(response.text)
        ns_tag = f".//{{{base_url}}}GetAvailableServicesV4Result"
        json_data = root.find(ns_tag)
       
        if json_data is not None:
            result_dict = json.loads(json_data.text)
            if result_dict.get("status")=="success":
                data_status["responseStatus"]=1
                data_status["message"]="Stations retrieved successfully"
                data_status["data"]=result_dict.get("services")
            else:
                data_status["responseStatus"]=0
                data_status["message"]=result_dict.get("Message")
        else:
            data_status["responseStatus"]=0
            data_status["message"]="Error: Unable to retrieve Operators."
        return data_status

    except Exception as e:
        app.logger.error("Error during request preparation: " + traceback.format_exc())
        return {"responseStatus":0,"message":"Error: Unable to retrieve abhibuses."}



def getSeatlayout(common_url,base_url,xmlurl,username,password,operatorId,serviceId,sourceStationId,destinationStationId,journeyDate,layoutId,seatFare,isSingleLady,concessionId):
    data_status={"responseStatus":0,"message":""}

    try:
        headers={
            "Content-Type": "text/xml; charset=utf-8",
            "SOAPAction": base_url+"GetServiceSeatingLayoutV4",
        }
        data = f"""<?xml version="1.0" encoding="utf-8"?>
        <soap:Envelope xmlns:soap="{xmlurl}" xmlns:tns="{base_url}">
            <soap:Body>
                <tns:GetServiceSeatingLayoutV4>
                    <tns:username>{username}</tns:username>
                    <tns:password>{password}</tns:password>
                    <operatorId>{operatorId}</operatorId>
                    <serviceId>{serviceId}</serviceId>
                    <sourceStationId>{sourceStationId}</sourceStationId>
                    <destinationStationId>{destinationStationId}</destinationStationId>
                    <journeyDate>{journeyDate}</journeyDate>
                    <layoutId>{layoutId}</layoutId>
                    <seatFare>{seatFare}</seatFare>
                    <isSingleLady>{isSingleLady}</isSingleLady>
                    <concessionId>{concessionId}</concessionId>
                </tns:GetServiceSeatingLayoutV4>
            </soap:Body>
        </soap:Envelope>"""

        response=requests.post(common_url,headers=headers,data=data.encode("utf-8"))
        print(response.text)
        root = ET.fromstring(response.text)
        ns_tag = f".//{{{base_url}}}GetServiceSeatingLayoutV4Result"
        json_data = root.find(ns_tag)
       
        if json_data is not None:
            result_dict = json.loads(json_data.text)
            if result_dict.get("status")=="success":
                data_status["responseStatus"]=1
                data_status["message"]="Stations retrieved successfully"
                data_status["data"]=result_dict.get("seatlayout")
            else:
                data_status["responseStatus"]=0
                data_status["message"]=result_dict.get("Message")
        else:
            data_status["responseStatus"]=0
            data_status["message"]="Error: Unable to retrieve abhibuses."
        return data_status

    except Exception as e:
        app.logger.error("Error during request preparation: " + traceback.format_exc())
        return {"responseStatus":0,"message":"Error: Unable to retrieve abhibuses."}
   


def blockTickets(common_url,base_url,xmlurl,username,password,operatorId,serviceId,sourceStationId,destinationStationId,journeyDate,layoutId,isSingleLady,concessionId,seatFareList,ageList,contactNumber,gendersList,boardingPointID,droppingPointID,seatNumbersList,seatLayoutUniqueId,address,emailId,seatTypesList,seatTypeIds,isAcSeat,additionalInfoLabel,serviceTaxList,namesList):
    data_status={"responseStatus":0,"message":""}

    try:
        headers={
            "Content-Type": "text/xml; charset=utf-8",
            "SOAPAction": base_url+"BlockTicketsV4",
        }
        print("headers:",headers)
        data = f"""<?xml version="1.0" encoding="utf-8"?>
        <soap:Envelope xmlns:soap="{xmlurl}" xmlns:stag="{base_url}">
            <soap:Body>
                <stag:BlockTicketsV4>
                    <stag:username>{username}</stag:username>
                    <stag:password>{password}</stag:password>
                    <stag:operatorId>{operatorId}</stag:operatorId>
                    <stag:journeyDate>{journeyDate}</stag:journeyDate>
                    <stag:serviceId>{serviceId}</stag:serviceId>
                    <stag:layoutId>{layoutId}</stag:layoutId>
                    <stag:sourceStationId>{sourceStationId}</stag:sourceStationId>
                    <stag:destinationStationId>{destinationStationId}</stag:destinationStationId>
                    <stag:boardingPointID>{boardingPointID}</stag:boardingPointID>
                    <stag:droppingPointID>{droppingPointID}</stag:droppingPointID>
                    <stag:address>{address}</stag:address>
                    <stag:city>{address}</stag:city>
                    <stag:contactNumber>{contactNumber}</stag:contactNumber>
                    <stag:emailId>{emailId}</stag:emailId>
                    <stag:namesList>{namesList}</stag:namesList>
                    <stag:gendersList>{gendersList}</stag:gendersList>
                    <stag:ageList>{ageList}</stag:ageList>
                    <stag:seatNumbersList>{seatNumbersList}</stag:seatNumbersList>
                    <stag:seatFareList>{seatFareList}</stag:seatFareList>
                    <stag:seatTypesList>{seatTypesList}</stag:seatTypesList>
                    <stag:seatTypeIds>{seatTypeIds}</stag:seatTypeIds>
                    <stag:isAcSeat>{isAcSeat}</stag:isAcSeat>
                    <stag:serviceTaxList>{serviceTaxList}</stag:serviceTaxList>
                    <stag:seatLayoutUniqueId>{seatLayoutUniqueId}</stag:seatLayoutUniqueId>
                    <stag:isSingleLady>{isSingleLady}</stag:isSingleLady>
                    <stag:concessionId>{concessionId}</stag:concessionId>
                    <stag:additionalInfoLabel>{additionalInfoLabel}</stag:additionalInfoLabel>
                </stag:BlockTicketsV4>
            </soap:Body>
        </soap:Envelope>"""

        print("body:",data)

        response=requests.post(common_url,headers=headers,data=data.encode("utf-8"))
        print(response.text)
        root = ET.fromstring(response.text)
        ns_tag = f".//{{{base_url}}}BlockTicketsV4Result"
        json_data = root.find(ns_tag)
       
        if json_data is not None:
            result_dict = json.loads(json_data.text)
            if result_dict.get("status")=="success":
                data_status["responseStatus"]=1
                data_status["message"]="Blocked Tickets successfully"
                data_status["data"]=result_dict.get("ReferenceNo")
            else:
                data_status["responseStatus"]=0
                data_status["message"]=result_dict.get("message")
        else:
            data_status["responseStatus"]=0
            data_status["message"]="Error: Unable to retrieve block tickets."
        return data_status

    except Exception as e:
        app.logger.error("Error during request preparation: " + traceback.format_exc())
        return {"responseStatus":0,"message":"Error: Unable to block tickets."}

def confirmSeatBooking(common_url,base_url, xmlurl,username, password, operatorId,journeyDate, referenceNo):
    data_status={"responseStatus":0,"message":""}

    try:
        headers={
            "Content-Type": "text/xml; charset=utf-8",
            "SOAPAction": base_url+"ConfirmationSeatBooking",
        }
        data = f"""<?xml version="1.0" encoding="utf-8"?>
        <soap:Envelope xmlns:soap="{xmlurl}" xmlns:tns="{base_url}">
            <soap:Body>
                <tns:ConfirmationSeatBooking>
                    <tns:username>{username}</tns:username>
                    <tns:password>{password}</tns:password>
                    <operatorId>{operatorId}</operatorId>
                    <journeyDate>{journeyDate}</journeyDate>
                    <referenceNo>{referenceNo}</referenceNo>
                   
                </tns:ConfirmationSeatBooking>
            </soap:Body>
        </soap:Envelope>"""

        response=requests.post(common_url,headers=headers,data=data.encode("utf-8"))
        print(response.text)
        root = ET.fromstring(response.text)
        ns_tag = f".//{{{base_url}}}ConfirmationSeatBookingResult"
        json_data = root.find(ns_tag)
       
        if json_data is not None:
            result_dict = json.loads(json_data.text)
            if result_dict.get("status")=="success":
                data_status["responseStatus"]=1
                data_status["message"]="Stations retrieved successfully"
                data_status["data"]=result_dict.get("TicketNo")
            else:
                data_status["responseStatus"]=0
                data_status["message"]=result_dict.get("Message")
        else:
            data_status["responseStatus"]=0
            data_status["message"]="Error: Unable to retrieve abhibuses."
        return data_status

    except Exception as e:
        app.logger.error("Error during request preparation: " + traceback.format_exc())
        return {"responseStatus":0,"message":"Error: Unable to retrieve abhibuses."}


#### "message":"Your ticket already cancelled or cancellation time exceeded"
def preCancellation(common_url,base_url,xmlurl,username, password, ticketNo, phoneNum):
    data_status={"responseStatus":0,"message":""}

    try:
        headers={
            "Content-Type": "text/xml; charset=utf-8",
            "SOAPAction": base_url+"preCancellation",
        }
        data = f"""<?xml version="1.0" encoding="utf-8"?>
        <soap:Envelope xmlns:soap="{xmlurl}" xmlns:stag="{base_url}">
            <soap:Body>
                <stag:PreCancellation>
                    <stag:username>{username}</stag:username>
                    <stag:password>{password}</stag:password>
                    <stag:ticketNo>{ticketNo}</stag:ticketNo>
                    <stag:phoneNum>{phoneNum}</stag:phoneNum>
                </stag:PreCancellation>

            </soap:Body>
        </soap:Envelope>"""

        response=requests.post(common_url,headers=headers,data=data.encode("utf-8"))
        print(response.text)
        root = ET.fromstring(response.text)
        ns_tag = f".//{{{base_url}}}PreCancellationResult"
        json_data = root.find(ns_tag)

        if json_data is not None:
            result_dict = json.loads(json_data.text)
            if result_dict.get("status")=="success":
                data_status["responseStatus"]=1
                data_status["message"]= "Pre-cancellation data retrieved successfully"
                data_status["can_ret_amount"] = result_dict.get("can_ret_amount")
                data_status["Collect_amt"] = result_dict.get("Collect_amt")
                data_status["cancellation_charges"] = result_dict.get("cancellation_charges")
                data_status["return_amount"] = result_dict.get("return_amount")
            else:
                data_status["responseStatus"]=0
                data_status["message"]=result_dict.get("Message")
        else:
            data_status["responseStatus"]=0
            data_status["message"]="Error: Unable to retrieve abhibuses."
        return data_status
    
    except Exception as e:
        app.logger.error("Error during request preparation: " + traceback.format_exc())
        return {"responseStatus":0,"message":"Error: Unable to retrieve abhibuses."}


def CancelTicket(common_url, base_url, xmlurl, username, password, operatorId, phoneNum, ticketNo, cancelSeats,partialCancellation):
    data_status={"responseStatus":0,"message":""}

    try:
        headers={
            "Content-Type": "text/xml; charset=utf-8",
            "SOAPAction": base_url+"CancelTicket",
        }
        data = f"""<?xml version="1.0" encoding="utf-8"?>
        <soap:Envelope xmlns:soap="{xmlurl}" xmlns:tns="{base_url}">
            <soap:Body>
                <tns:CancelTicket>
                    <tns:username>{username}</tns:username>
                    <tns:password>{password}</tns:password>
                    <tns:operatorId>{operatorId}</tns:operatorId>
                    <tns:phoneNum>{phoneNum}</tns:phoneNum>
                    <tns:ticketNo>{ticketNo}</tns:ticketNo>
                    <tns:cancelSeats>{cancelSeats}</tns:cancelSeats>
                    <tns:partialCancellation>{partialCancellation}</tns:partialCancellation>
                </tns:CancelTicket>
            </soap:Body>
        </soap:Envelope>"""

        
        response=requests.post(common_url,headers=headers,data=data.encode("utf-8"))
        print(response.text)
        root = ET.fromstring(response.text)
        ns_tag = f".//{{{base_url}}}CancelTicketResult"
        json_data = root.find(ns_tag)

        if json_data is not None:
            result_dict = json.loads(json_data.text)
            if result_dict.get("status")=="success":
                data_status["responseStatus"]=1
                data_status["message"]="Your ticket is successfully cancelled"
                data_status["return_amount"] = result_dict.get("return_amount")
            else:
                data_status["responseStatus"]=0
                data_status["message"]=result_dict.get("Message")
        else:
            data_status["responseStatus"]=0
            data_status["message"]="Error: Unable to retrieve abhibuses."
        return data_status

    except Exception as e:
        app.logger.error("Error during request preparation: " + traceback.format_exc())
        return {"responseStatus":0,"message":"Error: Unable to retrieve abhibuses."}

def GetticketDetails(common_url, base_url, xmlurl, username, password, ticketNo):
    data_status={"responseStatus":0,"message":""}

    try:
        headers={
            "Content-Type": "text/xml; charset=utf-8",
            "SOAPAction": base_url+"GetTicketDetailsV4",
        }
        data = f"""<?xml version="1.0" encoding="utf-8"?>
        <soap:Envelope xmlns:soap="{xmlurl}" xmlns:tns="{base_url}">
            <soap:Body>
                <tns:GetTicketDetailsV4>
                    <tns:username>{username}</tns:username>
                    <tns:password>{password}</tns:password>
                    <tns:ticketNo>{ticketNo}</tns:ticketNo>
                </tns:GetTicketDetailsV4>

            </soap:Body>
        </soap:Envelope>"""

        response=requests.post(common_url,headers=headers,data=data.encode("utf-8"))
        print(response.text)
        root = ET.fromstring(response.text)
        ns_tag = f".//{{{base_url}}}GetTicketDetailsV4Result"
        json_data = root.find(ns_tag)

        if json_data is not None:
            result_dict = json.loads(json_data.text)
            if result_dict.get("status")=="success":
                data_status["responseStatus"]=1
                data_status["message"]= "Ticket Details retrieved successfully"
                data_status["data"]= result_dict.get("ticketInfo")
            else:
                data_status["responseStatus"]=0
                data_status["message"]=result_dict.get("Message")
        else:
            data_status["responseStatus"]=0
            data_status["message"]="Error: Unable to retrieve abhibuses."
        return data_status
    
    except Exception as e:
        app.logger.error("Error during request preparation: " + traceback.format_exc())
        return {"responseStatus":0,"message":"Error: Unable to retrieve abhibuses."}
 


def GetTicketByReference(common_url, base_url, xmlurl, username, password, reference, operatorId):
    data_status={"responseStatus":0,"message":""}

    try:
        headers={
            "Content-Type": "text/xml; charset=utf-8",
            "SOAPAction": base_url+"GetTicketByReferenceV4",
        }
        data = f"""<?xml version="1.0" encoding="utf-8"?>
        <soap:Envelope xmlns:soap="{xmlurl}" xmlns:tns="{base_url}">
            <soap:Body>
                <tns:GetTicketByReferenceV4>
                    <tns:username>{username}</tns:username>
                    <tns:password>{password}</tns:password>
                    <tns:reference>{reference}</tns:reference>
                    <tns:operatorId>{operatorId}</tns:operatorId>
                </tns:GetTicketByReferenceV4>

            </soap:Body>
        </soap:Envelope>"""

        response=requests.post(common_url,headers=headers,data=data.encode("utf-8"))
        print(response.text)
        root = ET.fromstring(response.text)
        ns_tag = f".//{{{base_url}}}GetTicketByReferenceV4Result"
        json_data = root.find(ns_tag)

        if json_data is not None:
            result_dict = json.loads(json_data.text)
            if result_dict.get("status")=="success":
                data_status["responseStatus"]=1
                data_status["message"]= "Ticket Details retrieved successfully"
                data_status["data"]= result_dict.get("ticketInfo")
            else:
                data_status["responseStatus"]=0
                data_status["message"]=result_dict.get("Message")
        else:
            data_status["responseStatus"]=0
            data_status["message"]="Error: Unable to retrieve abhibuses."
        return data_status
    
    except Exception as e:
        app.logger.error("Error during request preparation: " + traceback.format_exc())
        return {"responseStatus":0,"message":"Error: Unable to retrieve abhibuses."}
 

    
def CheckavailBalance(common_url,base_url,xmlurl,username,password):
    data_status={"responseStatus":0,"message":""}    

    try:
        headers={
            "Content-Type": "text/xml; charset=utf-8",
            "SOAPAction": base_url+"checkavailBalance",
        }
        data = f"""<?xml version="1.0" encoding="utf-8"?>
        <soap:Envelope xmlns:soap="{xmlurl}" xmlns:tns="{base_url}">
            <soap:Body>
                <tns:checkavailBalance>
                    <tns:username>{username}</tns:username>
                    <tns:password>{password}</tns:password>
                </tns:checkavailBalance>
            </soap:Body>
        </soap:Envelope>"""

        
        response=requests.post(common_url,headers=headers,data=data.encode("utf-8"))
        print(response.text)
        root = ET.fromstring(response.text)
        ns_tag = f".//{{{base_url}}}checkavailBalanceResult"
        json_data = root.find(ns_tag)

        if json_data is not None:
            result_dict = json.loads(json_data.text)
            if result_dict.get("status")=="success":
                data_status["responseStatus"]=1
                data_status["message"]= "check avail Balance retrieved successfully"
                data_status["data"]= result_dict.get("availablebalance")
            else:
                data_status["responseStatus"]=0
                data_status["message"]=result_dict.get("Message")
        else:
            data_status["responseStatus"]=0
            data_status["message"]="Error: Unable to retrieve abhibuses."
        return data_status
    
    except Exception as e:
        app.logger.error("Error during request preparation: " + traceback.format_exc())
        return {"responseStatus":0,"message":"Error: Unable to retrieve abhibuses."}
 



def GetFareDetails(common_url,base_url,xmlurl,username,password,operatorId,sourceStationId,destinationStationId,journeyDate,adultSeatNos,childSeatNos,serviceId,concessionId):
    data_status={"responseStatus":0,"message":""}    

    try:
        headers={
            "Content-Type": "text/xml; charset=utf-8",
            "SOAPAction": base_url+"GetFareDetails",
        }
        data = f"""<?xml version="1.0" encoding="utf-8"?>
        <soap:Envelope xmlns:soap="{xmlurl}" xmlns:tns="{base_url}">
            <soap:Body>
                <tns:GetFareDetails>
                    <tns:username>{username}</tns:username>
                    <tns:password>{password}</tns:password>
                    <tns:operatorId>{operatorId}</tns:operatorId>
                    <tns:sourceStationId>{sourceStationId}</tns:sourceStationId>
                    <tns:destinationStationId>{destinationStationId}</tns:destinationStationId>
                    <tns:journeyDate>{journeyDate}</tns:journeyDate>
                    <tns:adultSeatNos>{adultSeatNos}</tns:adultSeatNos>
                    <tns:childSeatNos>{childSeatNos}</tns:childSeatNos>
                    <tns:serviceId>{serviceId}</tns:serviceId>
                    <tns:concessionId>{concessionId}</tns:concessionId>
                </tns:GetFareDetails>
            </soap:Body>
        </soap:Envelope>"""

   
        response=requests.post(common_url,headers=headers,data=data.encode("utf-8"))
        print(response.text)
        root = ET.fromstring(response.text)
        ns_tag = f".//{{{base_url}}}GetFareDetailsResult"
        json_data = root.find(ns_tag)

        if json_data is not None:
            result_dict = json.loads(json_data.text)
            if result_dict.get("status")=="success":
                data_status["responseStatus"]=1
                data_status["message"]= "FareDetails retrieved successfully"
                data_status["data"]= result_dict.get("GetFareDetails")
            else:
                data_status["responseStatus"]=0
                data_status["message"]=result_dict.get("Message")
        else:
            data_status["responseStatus"]=0
            data_status["message"]="Error: Unable to retrieve abhibuses."
        return data_status
    
    except Exception as e:
        app.logger.error("Error during request preparation: " + traceback.format_exc())
        return {"responseStatus":0,"message":"Error: Unable to retrieve abhibuses."}
 

def GetFaresInfo(common_url,base_url,xmlurl,username,password,adultSeatNos,childSeatNos,referenceNo):
    data_status={"responseStatus":0,"message":""}    

    try:
        headers={
            "Content-Type": "text/xml; charset=utf-8",
            "SOAPAction": base_url+"GetFaresInfo",
        }
        data = f"""<?xml version="1.0" encoding="utf-8"?>
        <soap:Envelope xmlns:soap="{xmlurl}" xmlns:tns="{base_url}">
            <soap:Body>
                <tns:GetFaresInfo>
                    <tns:username>{username}</tns:username>
                    <tns:password>{password}</tns:password>
                    <tns:adultSeatNos>{adultSeatNos}</tns:adultSeatNos>
                    <tns:childSeatNos>{childSeatNos}</tns:childSeatNos>
                    <tns:referenceNo>{referenceNo}</tns:referenceNo>
                </tns:GetFaresInfo>
            </soap:Body>
        </soap:Envelope>"""

   
        response=requests.post(common_url,headers=headers,data=data.encode("utf-8"))
        print(response.text)
        root = ET.fromstring(response.text)
        ns_tag = f".//{{{base_url}}}GetFaresInfoResult"
        json_data = root.find(ns_tag)

        if json_data is not None:
            result_dict = json.loads(json_data.text)
            if result_dict.get("status")=="success":
                data_status["responseStatus"]=1
                data_status["message"]= "FaresInfo retrieved successfully"
                data_status["data"]= result_dict.get("GetFareDetails")
            else:
                data_status["responseStatus"]=0
                data_status["message"]=result_dict.get("message")
        else:
            data_status["responseStatus"]=0
            data_status["message"]="Error: Unable to retrieve abhibuses."
        return data_status
    
    except Exception as e:
        app.logger.error("Error during request preparation: " + traceback.format_exc())
        return {"responseStatus":0,"message":"Error: Unable to retrieve abhibuses."}
    
def GetAvailableServices(common_url,base_url,xmlurl,username,password,sourceStationId,destinationStationId,journeyDate):
    data_status={"responseStatus":0,"message":""}    

    try:
        headers={
            "Content-Type": "text/xml; charset=utf-8",
            "SOAPAction": base_url+"GetAvailableServicesV2",
        }
        data = f"""<?xml version="1.0" encoding="utf-8"?>
        <soap:Envelope xmlns:soap="{xmlurl}" xmlns:tns="{base_url}">
            <soap:Body>
                <tns:GetAvailableServicesV2>
                    <tns:username>{username}</tns:username>
                    <tns:password>{password}</tns:password>
                    <tns:sourceStationId>{sourceStationId}</tns:sourceStationId>
                    <tns:destinationStationId>{destinationStationId}</tns:destinationStationId>
                    <tns:journeyDate>{journeyDate}</tns:journeyDate>
                </tns:GetAvailableServicesV2>
            </soap:Body>
        </soap:Envelope>"""

   
        response=requests.post(common_url,headers=headers,data=data.encode("utf-8"))
        print(response.text)
        root = ET.fromstring(response.text)
        ns_tag = f".//{{{base_url}}}GetAvailableServicesV2Result"
        json_data = root.find(ns_tag)

        if json_data is not None:
            result_dict = json.loads(json_data.text)
            if result_dict.get("status")=="success":
                data_status["responseStatus"]=1
                data_status["message"]= "Available Services retrieved successfully"
                data_status["data"]= result_dict.get("services")
            else:
                data_status["responseStatus"]=0
                data_status["message"]=result_dict.get("Message")
        else:
            data_status["responseStatus"]=0
            data_status["message"]="Error: Unable to retrieve abhibuses."
        return data_status
    
    except Exception as e:
        app.logger.error("Error during request preparation: " + traceback.format_exc())
        return {"responseStatus":0,"message":"Error: Unable to retrieve abhibuses."}


def GetServiceSeatingLayout(common_url,base_url,xmlurl,username,password,operatorId,serviceId,sourceStationId,destinationStationId,journeyDate,layoutId,seatFare,isSingleLady,concessionId):
    data_status={"responseStatus":0,"message":""}    

    try:
        headers={
            "Content-Type": "text/xml; charset=utf-8",
            "SOAPAction": base_url+"GetServiceSeatingLayoutV2",
        }
        data = f"""<?xml version="1.0" encoding="utf-8"?>
        <soap:Envelope xmlns:soap="{xmlurl}" xmlns:tns="{base_url}">
            <soap:Body>
                <tns:GetServiceSeatingLayoutV2>
                    <tns:username>{username}</tns:username>
                    <tns:password>{password}</tns:password>
                    <tns:operatorId>{operatorId}</tns:operatorId>
                    <tns:serviceId>{serviceId}</tns:serviceId>
                    <tns:sourceStationId>{sourceStationId}</tns:sourceStationId>
                    <tns:destinationStationId>{destinationStationId}</tns:destinationStationId>
                    <tns:journeyDate>{journeyDate}</tns:journeyDate>
                    <tns:layoutId>{layoutId}</tns:layoutId>
                    <tns:seatFare>{seatFare}</tns:seatFare>
                    <tns:isSingleLady>{isSingleLady}</tns:isSingleLady>
                    <tns:concessionId>{concessionId}</tns:concessionId>
                </tns:GetServiceSeatingLayoutV2>
            </soap:Body>
        </soap:Envelope>"""
   
        response=requests.post(common_url,headers=headers,data=data.encode("utf-8"))
        print(response.text)
        root = ET.fromstring(response.text)
        ns_tag = f".//{{{base_url}}}GetServiceSeatingLayoutV2Result"
        json_data = root.find(ns_tag)

        if json_data is not None:
            result_dict = json.loads(json_data.text)
            if result_dict.get("status")=="success":
                data_status["responseStatus"]=1
                data_status["message"]= "Service Seating Layout retrieved successfully"
                data_status["data"]= result_dict.get("seatlayout")
            else:
                data_status["responseStatus"]=0
                data_status["message"]=result_dict.get("Message")
        else:
            data_status["responseStatus"]=0
            data_status["message"]="Error: Unable to retrieve abhibuses."
        return data_status
    
    except Exception as e:
        app.logger.error("Error during request preparation: " + traceback.format_exc())
        return {"responseStatus":0,"message":"Error: Unable to retrieve abhibuses."}

def GetTicketDetails(common_url,base_url,xmlurl,username,password,ticketNo):
    data_status={"responseStatus":0,"message":""}    

    try:
        headers={
            "Content-Type": "text/xml; charset=utf-8",
            "SOAPAction": base_url+"GetTicketDetailsV2",
        }
        data = f"""<?xml version="1.0" encoding="utf-8"?>
        <soap:Envelope xmlns:soap="{xmlurl}" xmlns:tns="{base_url}">
            <soap:Body>
                <tns:GetTicketDetailsV2>
                    <tns:username>{username}</tns:username>
                    <tns:password>{password}</tns:password>
                    <tns:ticketNo>{ticketNo}</tns:ticketNo>
                </tns:GetTicketDetailsV2>
            </soap:Body>
        </soap:Envelope>"""
   
        response=requests.post(common_url,headers=headers,data=data.encode("utf-8"))
        print(response.text)
        root = ET.fromstring(response.text)
        ns_tag = f".//{{{base_url}}}GetTicketDetailsV2Result"
        json_data = root.find(ns_tag)

        if json_data is not None:
            result_dict = json.loads(json_data.text)
            if result_dict.get("status")=="success":
                data_status["responseStatus"]=1
                data_status["message"]= "Ticket Details retrieved successfully"
                data_status["data"]= result_dict.get("ticketInfo")
            else:
                data_status["responseStatus"]=0
                data_status["message"]=result_dict.get("Message")
        else:
            data_status["responseStatus"]=0
            data_status["message"]="Error: Unable to retrieve abhibuses."
        return data_status
    
    except Exception as e:
        app.logger.error("Error during request preparation: " + traceback.format_exc())
        return {"responseStatus":0,"message":"Error: Unable to retrieve abhibuses."}


def GetTicketDetailsByReference(common_url,base_url,xmlurl,username,password,reference,operatorId):
    data_status={"responseStatus":0,"message":""}    

    try:
        headers={
            "Content-Type": "text/xml; charset=utf-8",
            "SOAPAction": base_url+"GetTicketByReference",
        }
        data = f"""<?xml version="1.0" encoding="utf-8"?>
        <soap:Envelope xmlns:soap="{xmlurl}" xmlns:tns="{base_url}">
            <soap:Body>
                <tns:GetTicketByReference>
                    <tns:username>{username}</tns:username>
                    <tns:password>{password}</tns:password>
                    <tns:reference>{reference}</tns:reference>
                    <tns:operatorId>{operatorId}</tns:operatorId>
                </tns:GetTicketByReference>
            </soap:Body>
        </soap:Envelope>"""
   
        response=requests.post(common_url,headers=headers,data=data.encode("utf-8"))
        print(response.text)
        root = ET.fromstring(response.text)
        ns_tag = f".//{{{base_url}}}GetTicketByReferenceResult"
        json_data = root.find(ns_tag)

        if json_data is not None:
            result_dict = json.loads(json_data.text)
            if result_dict.get("status")=="success":
                data_status["responseStatus"]=1
                data_status["message"]= "Ticket Details retrieved successfully"
                data_status["data"]= result_dict.get("ticketInfo")
            else:
                data_status["responseStatus"]=0
                data_status["message"]=result_dict.get("Message")
        else:
            data_status["responseStatus"]=0
            data_status["message"]="Error: Unable to retrieve abhibuses."
        return data_status
    
    except Exception as e:
        app.logger.error("Error during request preparation: " + traceback.format_exc())
        return {"responseStatus":0,"message":"Error: Unable to retrieve abhibuses."}

def BlockTicketsV2(common_url,base_url,xmlurl,username,password,operatorId,journeyDate,serviceId,layoutId,sourceStationId,destinationStationId,boardingPointID,droppingPointID,address,contactNumber,emailId,namesList,gendersList,ageList,seatNumbersList,seatFareList,seatTypeIds,seatTypesList,isAcSeat,seatLayoutUniqueId,serviceTaxList,isSingleLady,concessionId,additionalInfoLabel):
    data_status={"responseStatus":0,"message":""}    

    try:
        headers={
            "Content-Type": "text/xml; charset=utf-8",
            "SOAPAction": base_url+"BlockTicketsV2",
        }
        data = f"""<?xml version="1.0" encoding="utf-8"?>
        <soap:Envelope xmlns:soap="{xmlurl}" xmlns:tns="{base_url}">
            <soap:Body>
                <tns:BlockTicketsV2>
                    <tns:username>{username}</tns:username>
                    <tns:password>{password}</tns:password>
                    <tns:operatorId>{operatorId}</tns:operatorId>
                    <tns:journeyDate>{journeyDate}</tns:journeyDate>
                    <tns:serviceId>{serviceId}</tns:serviceId>
                    <tns:layoutId>{layoutId}</tns:layoutId>
                    <tns:sourceStationId>{sourceStationId}</tns:sourceStationId>
                    <tns:destinationStationId>{destinationStationId}</tns:destinationStationId>
                    <tns:boardingPointID>{boardingPointID}</tns:boardingPointID>
                    <tns:droppingPointID>{droppingPointID}</tns:droppingPointID>
                    <tns:address>{address}</tns:address>
                    <tns:city>{address}</tns:city>
                    <tns:contactNumber>{contactNumber}</tns:contactNumber>
                    <tns:emailId>{emailId}</tns:emailId>
                    <tns:namesList>{namesList}</tns:namesList>
                    <tns:gendersList>{gendersList}</tns:gendersList>
                    <tns:ageList>{ageList}</tns:ageList>
                    <tns:seatNumbersList>{seatNumbersList}</tns:seatNumbersList>
                    <tns:seatFareList>{seatFareList}</tns:seatFareList>
                    <tns:seatTypesList>{seatTypesList}</tns:seatTypesList>
                    <tns:seatTypeIds>{seatTypeIds}</tns:seatTypeIds>
                    <tns:isAcSeat>{isAcSeat}</tns:isAcSeat>
                    <tns:serviceTaxList>{serviceTaxList}</tns:serviceTaxList>
                    <tns:seatLayoutUniqueId>{seatLayoutUniqueId}</tns:seatLayoutUniqueId>
                    <tns:isSingleLady>{isSingleLady}</tns:isSingleLady>
                    <tns:concessionId>{concessionId}</tns:concessionId>
                    <tns:additionalInfoLabel>{additionalInfoLabel}</tns:additionalInfoLabel>
                </tns:BlockTicketsV2>
            </soap:Body>
        </soap:Envelope>"""
   
        response=requests.post(common_url,headers=headers,data=data.encode("utf-8"))
        print(response.text)
        root = ET.fromstring(response.text)
        ns_tag = f".//{{{base_url}}}BlockTicketsV2Result"
        json_data = root.find(ns_tag)

        if json_data is not None:
            result_dict = json.loads(json_data.text)
            if result_dict.get("status")=="success":
                data_status["responseStatus"]=1
                data_status["message"]= "Blocked Tickets successfully"
                data_status["data"]= result_dict.get("ReferenceNo")
            else:
                data_status["responseStatus"]=0
                data_status["message"]=result_dict.get("Message")
        else:
            data_status["responseStatus"]=0
            data_status["message"]="Error: Unable to retrieve abhibuses."
        return data_status
    
    except Exception as e:
        app.logger.error("Error during request preparation: " + traceback.format_exc())
        return {"responseStatus":0,"message":"Error: Unable to retrieve abhibuses."}





