from xml.etree import cElementTree as eTree

from application.interactor import OrderInteractor

NOT_ELECTRONIC_TYPE_ID = (0, 9)


def cancel_non_eft_transactions(
    pos_id,     # type: str
    order_id,   # type: str
    order,      # type: eTree.Element
):
    # type: (...) -> None

    tenders = order.findall("TenderHistory/Tender")
    non_electronic_tenders = [
        tender
        for tender in tenders
        if int(tender.get("electronicType") or tender.get("eletronicType")) in NOT_ELECTRONIC_TYPE_ID
    ]

    order_interactor = OrderInteractor(pos_id=pos_id)
    for tender in non_electronic_tenders:
        tender_amount = float(tender.get('tenderAmount'))
        if tender_amount == 0:
            continue

        order_interactor.clear_order_tender(
            order_tender_id=tender.get("tenderId"),
            order_id=order_id or order.get("orderId"),
        )
