import xml.etree.ElementTree as ET
import win32com.client
import xmltodict
import pprint
import datetime
import pandas as pd
from datetime import date
import timeit

class SalesOrderQuery:
    def __init__(self,  **kwargs) -> None:
        # print(f'kwargs:{kwargs}')
        # print(args)
        # self.SalesOrderList=[]
        self.DN = kwargs['DN'] if 'DN' in kwargs else {}
        self.Reuse = kwargs['Reuse'] if 'Reuse' in kwargs else None
        
        self.SalesOrderList= kwargs['SalesOrderList'] if 'SalesOrderList' in kwargs else []
        
        self.SalesOrderType = kwargs['SalesOrderType'] if 'SalesOrderType' in kwargs else 'SalesByCustomerSummary'
        self.IncludeLineItems = kwargs['IncludeLineItems'] if 'IncludeLineItems' in kwargs else 'true'
        self.IncludeRetElement = kwargs['IncludeRetElement'] if 'IncludeRetElement' in kwargs else []
        self.TxnDateRangeFilter = kwargs['TxnDateRangeFilter'] if 'TxnDateRangeFilter' in kwargs else None
        self.DateMacro = None
        if 'DateMacro' in kwargs:
            if kwargs['DateMacro'] in ['All', 'Today', 'ThisWeek', 'ThisWeekToDate', 'ThisMonth', 'ThisMonthToDate', 'ThisQuarter', 'ThisQuarterToDate', 'ThisYear', 'ThisYearToDate', 'Yesterday', 'LastWeek', 'LastWeekToDate', 'LastMonth', 'LastMonthToDate', 'LastQuarter', 'LastQuarterToDate', 'LastYear', 'LastYearToDate', 'NextWeek', 'NextFourWeeks', 'NextMonth', 'NextQuarter', 'NextYear']:
                self.DateMacro = kwargs['DateMacro']
        self.FromTxnDate =  self.validate_date(kwargs['FromTxnDate']) if 'FromTxnDate' in kwargs else None
        self.ToTxnDate = self.validate_date(kwargs['ToTxnDate']) if 'ToTxnDate' in kwargs else None
        self.ReportEntityFilter = kwargs['ReportEntityFilter'] if 'ReportEntityFilter' in kwargs else None
        self.FullName = kwargs['FullName'] if 'FullName' in kwargs else None
        # print(self.DateMacro, self.TxnDateRangeFilter, self.FromTxnDate, self.ToTxnDate)
        # print(self.Reuse)
        # if not self.Reuse:
        #     self.dfDN, self.ext_doc_no_list = self.get_ext_doc_no_list(self.DN)
        #     self.RefNumber = kwargs['RefNumber'] if 'RefNumber' in kwargs else self.ext_doc_no_list
        #     self.get_sales_order_header()

    def create_sub_element(self, ET, parentNode, thisNode, text="\n", whiteSpace = 0, attrib =None):
        if type(attrib) is not dict:
            attrib = {}
        ele = ET.SubElement(parentNode, thisNode)
        for x in attrib:
            ele.set(x, attrib[x])
        ele.text = text
        tail = "\n"
        for x in range(whiteSpace):
            tail = tail + " "
        ele.tail = tail
        return ele

    def create_QBXML(self):
        root = ET.Element("QBXML")
        root.tail = "\n"
        root.text = "\n  "
        QBXMLMsgsRq = ET.SubElement(root, "QBXMLMsgsRq")
        # QBXMLMsgsRq.set("onError", "continueOnError")
        QBXMLMsgsRq.set("onError", "stopOnError")
        QBXMLMsgsRq.tail = "\n"
        QBXMLMsgsRq.text = "\n    "
        SalesOrderQueryRq = self.create_sub_element(ET, QBXMLMsgsRq, "SalesOrderQueryRq","\n      " )
        # SalesOrderType = self.create_sub_element(ET, SalesOrderQueryRq, 'SalesOrderType', self.SalesOrderType)
        if self.FullName:
            EntityFilter = self.create_sub_element(ET, SalesOrderQueryRq, 'EntityFilter', "\n        ")
            FullName = self.create_sub_element(ET, EntityFilter, "FullName", self.FullName, 6)
        # print(f'refnumber:{self.RefNumber}')
        # if self.RefNumber:
        #     for rn in self.RefNumber:
        #         RefNumber = self.create_sub_element(ET, SalesOrderQueryRq, "RefNumber", f'{rn}', 4)
        if self.DateMacro:
            TxnDateRangeFilter = self.create_sub_element(ET, SalesOrderQueryRq, "TxnDateRangeFilter", "\n     ",)
            SalesOrderType = self.create_sub_element(ET, TxnDateRangeFilter, "DateMacro", self.DateMacro)
			# SalesOrderType = self.create_sub_element(ET, SalesOrderQueryRq, "DateMacro", self.DateMacro)

        elif type(self.FromTxnDate) is datetime.date or type(self.ToTxnDate) is datetime.date:
            TxnDateRangeFilter = self.create_sub_element(ET, SalesOrderQueryRq, "TxnDateRangeFilter", "\n     ",)
            if type(self.FromTxnDate) is datetime.date:
                FromTxnDate = self.create_sub_element(ET, TxnDateRangeFilter, "FromTxnDate", self.FromTxnDate.strftime('%Y-%m-%d'),4)
            if type(self.ToTxnDate) is datetime.date:
                ToTxnDate = self.create_sub_element(ET, TxnDateRangeFilter, "ToTxnDate", self.ToTxnDate.strftime('%Y-%m-%d'))
        if self.IncludeLineItems:
            IncludeLineItems = self.create_sub_element(ET, SalesOrderQueryRq, "IncludeLineItems", self.IncludeLineItems, 4)
        if len(self.IncludeRetElement)>0:
            for x in self.IncludeRetElement:
                IncludeRetElement = self.create_sub_element(ET, SalesOrderQueryRq, "IncludeRetElement", x, 4)
        mydata = ET.tostring(root, encoding = "unicode")

        qbxml_query = """<?xml version="1.0" encoding="utf-8"?>\n"""
        qbxml_query = qbxml_query + """<?qbxml version="13.0"?>"""
        qbxml_query = qbxml_query + "\n" + mydata
        print(f'create_QBXML->qbxml_query: {qbxml_query}')
        return qbxml_query

    def create_invoiceadd_QBXML(self):
        root = ET.Element("QBXML")
        root.tail = "\n"
        root.text = "\n  "
        QBXMLMsgsRq = ET.SubElement(root, "QBXMLMsgsRq")
        QBXMLMsgsRq.set("onError", "stopOnError")
        QBXMLMsgsRq.tail = "\n"
        QBXMLMsgsRq.text = "\n    "
        InvoiceAddRq = self.create_sub_element(ET, QBXMLMsgsRq, "InvoiceAddRq", "\n      ",2 )
        InvoiceAdd = self.create_sub_element(ET, InvoiceAddRq, "InvoiceAdd", "\n        ",4 )
        CustomerRef = self.create_sub_element(ET, InvoiceAdd, "CustomerRef", "\n          ",8 )
        FullName = self.create_sub_element(ET, CustomerRef, "FullName", "TACO", 8 )
        today = str(date.today())
        TxnDate = self.create_sub_element(ET, InvoiceAdd, "TxnDate", self.DN['TxnDate'], 8 )
        RefNumber = self.create_sub_element(ET, InvoiceAdd, "RefNumber", self.DN['DNRefNum'], 8 )
        Memo = self.create_sub_element(ET, InvoiceAdd, "Memo", self.DN['Memo'], 10 )
        for salesorder in self.SalesOrderList:
            POTxnId = salesorder['TxnID']
            print(f'create_invoiceadd_QBXML->POTxnID: {POTxnId}')
            for itemline in salesorder['SalesOrderLineRet']:
                if 'DNQuantity' in itemline:
                    ItemLineAdd = self.create_sub_element(ET, InvoiceAdd, "ItemLineAdd", "\n            ", 10 )
                    Quantity = self.create_sub_element(ET, ItemLineAdd, "Quantity", str(itemline['DNQuantity'] ), 12 )
                    UnitOfMeasure = self.create_sub_element(ET, ItemLineAdd, "UnitOfMeasure", str(itemline['UOM']), 12 )
                    LinkToTxn = self.create_sub_element(ET, ItemLineAdd, "LinkToTxn", "\n              ",10 )
                    TxnID = self.create_sub_element(ET, LinkToTxn, "TxnID", POTxnId,14 )
                    TxnLineID = self.create_sub_element(ET, LinkToTxn, "TxnLineID", itemline['TxnLineID'],12 )
        mydata = ET.tostring(root, encoding = "unicode")

        qbxml_query = """<?xml version="1.0" encoding="utf-8"?>\n"""
        qbxml_query = qbxml_query + """<?qbxml version="13.0"?>"""
        qbxml_query = qbxml_query + "\n" + mydata
        # print(f'create_invoiceadd_QBXML->Create_Invoiceadd_QBXML: {qbxml_query}')
        # print(f"replyfrom qbxml:{self.connect_to_quickbooks(qbxml_query)}")

        return qbxml_query


    def connect_to_quickbooks(self, qbxml_query):
        # Connect to Quickbooks
        # sessionManager = win32com.client.Dispatch("QBXMLRP2.RequestProcessor")
        # sessionManager.OpenConnection('', 'DASA')
        # enumfodnc= win32com.client.Dispatch('QBXMLRP2.RequestProcessor')
        # print(enumfodnc)
        # print(enumfodnc.qbFileOpenDoNotCare)
        sessionManager = win32com.client.Dispatch("QBXMLRP2.RequestProcessor")
        sessionManager.OpenConnection('', 'DASA2')
        # ticket = sessionManager.BeginSession("z:\\DBW Bogor.qbw", 2)

        ticket = sessionManager.BeginSession("", 2)

        # Send query and receive response
        response_string = sessionManager.ProcessRequest(ticket, qbxml_query)

        # Disconnect from Quickbooks
        sessionManager.EndSession(ticket)     # Close the company file
        sessionManager.CloseConnection()      # Close the connection
        print (response_string)
        return response_string

    def __str__(self, *args) -> str:
        # return str(self._get_datarow(self.connect_to_quickbooks(self.create_QBXML())))
        # print("__str__")
        return str(self.get_sales_order_header())
        # return "hello"

    def get_sales_order_header(self, *args):
        return self. _get_sales_order_header(self.connect_to_quickbooks(self.create_QBXML()))

    def status_ok(self, QBXML): #for InvoiceAddRS

        tree = ET.fromstring(QBXML)

        GSRQRs = tree.find(".//InvoiceAddRs")
        # print(f"GSRQRs:{GSRQRs}")
        status_code = GSRQRs.attrib #.get('statusCode')
        # print(GSRQRs.attrib)
        # print(GSRQRs.attrib['statusCode'])
        status=GSRQRs.attrib.get('statusMessage')
        
        print(f'status={status}')
        if 'OK' in status:
            return True, status_code
        else:
            return False, status_code


    def _get_sales_order_header(self, response_string):
        print('_get_sales_order_header')
        # print(f'responsestring:{response_string}')
        QBXML = ET.fromstring(response_string)
        datadict = {}
        SalesOrderdict = {}
        _SalesOrderlist = []
        SalesOrderRets = QBXML.findall('.//SalesOrderRet')
        print(SalesOrderRets)
        for SalesOrderRet in SalesOrderRets:
            RefNumber = SalesOrderRet.find('RefNumber').text
            # Memo = SalesOrderRet.find('Memo').text
            CustomerFullName = SalesOrderRet.find('CustomerRef/FullName').text
            TxnID = SalesOrderRet.find('TxnID').text
            TotalAmount = SalesOrderRet.find('TotalAmount').text
            IsFullyInvoiced = SalesOrderRet.find('IsFullyInvoiced').text
            IsManuallyClosed = SalesOrderRet.find('IsManuallyClosed').text
            # print(CustomerFullName, TxnID, TotalAmount)
            SalesOrderdict = {'RefNumber':RefNumber, 'CustomerFullName':CustomerFullName, 'TxnID':TxnID,
                                 'TotalAmount':TotalAmount, 'IsFullyInvoiced':IsFullyInvoiced, 'IsManuallyClosed':IsManuallyClosed, 'SalesOrderLineRet':[]}
            SalesOrderLineRet = SalesOrderRet.findall('SalesOrderLineRet')
            # print(len(SalesOrderLineRet))
            if len(SalesOrderLineRet) > 0:
                for SalesOrderLineRet in SalesOrderLineRet:
                    pass
                    TxnLineID = SalesOrderLineRet.find('TxnLineID').text
                    ItemFullName = SalesOrderLineRet.find('ItemRef/FullName').text
                    Quantity = SalesOrderLineRet.find('Quantity').text
                    UnitOfMeasure = SalesOrderLineRet.find('UnitOfMeasure').text
                    Rate = float(SalesOrderLineRet.find('Rate').text)
                    Amount = float(SalesOrderLineRet.find('Amount').text)
                    Invoiced = SalesOrderLineRet.find('Invoiced').text
                    # IsBilled = SalesOrderLineRet.find('IsBilled').text
                    LineIsManuallyClosed = SalesOrderLineRet.find('IsManuallyClosed').text
                    # print(TxnLineID, ItemFullName)
                    BackOrdered = float(Quantity) - float(Invoiced)
                    if BackOrdered:
                        SalesOrderLinedict = {'TxnLineID':TxnLineID,
                                                 'ItemFullName':ItemFullName,
                                                 'Quantity':Quantity,
                                                 'UOM':UnitOfMeasure,
                                                 'Rate':Rate,
                                                 'Amount':Amount,
                                                 'BackOrdered':BackOrdered,
                                                 'Invoiced':Invoiced,
                                                #  'IsBilled':IsBilled,
                                                 'LineIsManuallyClosed':LineIsManuallyClosed,
                                                 }
                        SalesOrderdict['SalesOrderLineRet'].append(SalesOrderLinedict)
            _SalesOrderlist.append(SalesOrderdict)
        # print(_SalesOrderlist)
        self.SalesOrderList=_SalesOrderlist
        print(f'_get_sales_order_header->Salesorderlist: {self.SalesOrderList}')
        # return SalesOrderlist

    def addDNQtyToSalesOrderList(self, _dict:dict):
        _bolfoundrefnum=False
        _bol_dictisadded=False
        Error_msg = None
        for poidx, _po in enumerate(self.SalesOrderList):
            if _po['RefNumber']==_dict['RefNum']:
                _bolfoundrefnum=True
                if len(_po['SalesOrderLineRet'])>0:
                    for polineidx, _poline in enumerate(_po['SalesOrderLineRet']):
                        pass
                        if _poline['ItemFullName']==_dict['FullName']:
                            if _poline['UOM'].upper()==_dict['UOM'].upper():
                                # first do UOM in _dict convert treatment
                                QuantityIn_dict = _dict['Quantity']
                                if _dict['UOM'].upper().startswith('ROLL_'):
                                    print(f"addDNQtyToSalesOrderList->DNqty:{_dict['Quantity']}, Roll_:{_dict['UOM'].split('_')[1]}")
                                    QuantityIn_dict = _dict['Quantity'] * int(_dict['UOM'].split("_")[1])
                                    pass
                                elif _dict['UOM'].upper() == 'BOX' and _dict['Item No'].upper() == "CUTTER": 
                                    print("addDNQtyToSalesOrderList->cutter")
                                elif _dict['UOM'].upper() == 'BOX' and (_dict['Item No'].upper().startswith("EB-") or _dict['Item No'].upper().startswith("TA-")):
                                    print("addDNQtyToSalesOrderList->LEM")
                                    if _dict['Item No'].split("-")[1].endswith("1006"):
                                        QuantityIn_dict = QuantityIn_dict * 12
                                    elif _dict['Item No'].split("-")[1].endswith("1025"):
                                        QuantityIn_dict = QuantityIn_dict * 6
                                    elif _dict['Item No'].split("-")[1].endswith("1100"):
                                        pass
                                        print("1100 lem")
                                elif _dict['UOM'].upper() == 'BOX' and (_dict['Item No'].upper().startswith("TL-") or _dict['Item No'].upper().startswith("TFL-")):
                                    print("addDNQtyToSalesOrderList->Lock")
                                    QuantityIn_dict = QuantityIn_dict * 20
                                elif _dict['UOM'].upper() == 'BOX' and _dict['Item No'].upper() == "EDG-TRIMMER": 
                                    QuantityIn_dict = QuantityIn_dict * 12 #coz box of 12


                                if _poline['BackOrdered']>=QuantityIn_dict:
                                    self.SalesOrderList[poidx]['SalesOrderLineRet'][polineidx]['DNQuantity']=float(QuantityIn_dict)
                                    _bol_dictisadded = True
                                else:
                                    print(f"{_poline['ItemFullName']} BackOrdered < Qty in DN {_poline['BackOrdered']}<{QuantityIn_dict}")
                                    Error_msg = f"BackOrdered < Qty in DN {_poline['BackOrdered']}<{QuantityIn_dict}"
                            else:
                                # print(f"UOM different {_poline['UOM']} <> {_dict['UOM']}")
                                Error_msg = f"UOM different {_poline['UOM']} <> {_dict['UOM']}"
                        else:
                            # print("errorpoline <>DN")
                            Error_msg = f"poline[ItemFullName] <> DN[FullName];  {_poline['ItemFullName']} <> {_dict['FullName']}, maybe there are 2 same namefromtaco in QB"
                else:
                    pass
                    # print(f"this refnum {_dict['RefNum']} have no QB PO Return Line")
                    Error_msg = f"this refnum {_dict['RefNum']} have no QB PO Return Line"

        # print (_bol_dictisadded, Error_msg)
        return _bol_dictisadded, Error_msg


    def prepareInvoice(self, df:pd.DataFrame = None):
        # print(df)
        _bolAllDNareOk = True
        _notindflist=[]
        _yescounter = 0
        _nocounter = 0
        if df is not None:
            _dflist = df.to_dict('records')
            # print(_dflist)

        else:
            _dflist = self.dfDN.to_dict('records')
        # print(self.dfDN)
        # print(f'_dflist:{_dflist}')
        for idx, xdf in enumerate(_dflist):
            _boladdDN, _Errormsg = self.addDNQtyToSalesOrderList(xdf)
            # print(f'prepareInvoice->_Errormsg:{_Errormsg}')
            if _boladdDN:
                _dflist[idx]['ADDED']=True
            elif _Errormsg:
                _dflist[idx]['ERROR']=_Errormsg
        for xdf in (_dflist):
            if 'ADDED' not in xdf:
                # print (f"prepareInvoice->not added: {xdf['Item No']}")
                print (f"prepareInvoice->not added: {xdf}")

                _notindflist.append(xdf)
                _nocounter+=1
                _bolAllDNareOk = False
            else:
                print (f"ADDED: {xdf['Item No']}")
                
        print(f'{len(_dflist) - _nocounter} of {len(_dflist)} are added')
        return _bolAllDNareOk, _notindflist


    def validate_date(self, date_text):
        if date_text is None:
            return None
        try:
            return datetime.datetime.strptime(date_text, '%Y-%m-%d').date()
        except ValueError:
            return None
            # raise ValueError("Incorrect data format, should be YYYY-MM-DD")

    def get_ext_doc_no_list(self, dndict=None):
        if dndict:
            dnlist = dndict['lines']
        else:
            dnlist = self.DN
        df = pd.DataFrame(dnlist)
        # print(df)
        df['RefNum']= df['No.SO/Ext.Doc.No.'].apply(lambda x: "L"+ x.split("L-")[1])
        # print(df)
        ext_doc_no=df['RefNum'].unique().tolist()
        if len(ext_doc_no)>0:
            return df, ext_doc_no
        else:
            return df, None

    def get_open_so(self, response_string):
        QBXML = ET.fromstring(response_string)
        _OpenSalesOrderlist = []
        SalesOrderRets = QBXML.findall('.//SalesOrderRet')
        # print(f'SalesOrderRets count:{len(SalesOrderRets)}')
        for SalesOrderRet in SalesOrderRets:
            IsFullyInvoiced = SalesOrderRet.find('IsFullyInvoiced').text
            IsManuallyClosed = SalesOrderRet.find('IsManuallyClosed').text
            if IsFullyInvoiced=='false' and IsManuallyClosed=='false':
                _OpenSalesOrderlist.append(SalesOrderRet.find('TxnID').text)
                # RefNumber = SalesOrderRet.find('RefNumber').text
                # Memo = SalesOrderRet.find('Memo').text
                # CustomerFullName = SalesOrderRet.find('CustomerRef/FullName').text
                # TxnID = SalesOrderRet.find('TxnID').text
                # TotalAmount = SalesOrderRet.find('TotalAmount').text
                # IsFullyReceived = SalesOrderRet.find('IsFullyReceived').text
                # IsManuallyClosed = SalesOrderRet.find('IsManuallyClosed').text
        # print(_OpenSalesOrderlist)
        return _OpenSalesOrderlist

    def create_open_sales_order_qbxml(self, txnid:list, IncludeLineItems=True):
        root = ET.Element("QBXML")
        root.tail = "\n"
        root.text = "\n  "
        QBXMLMsgsRq = ET.SubElement(root, "QBXMLMsgsRq")
        # QBXMLMsgsRq.set("onError", "continueOnError")
        QBXMLMsgsRq.set("onError", "stopOnError")
        QBXMLMsgsRq.tail = "\n"
        QBXMLMsgsRq.text = "\n    "
        SalesOrderQueryRq = self.create_sub_element(ET, QBXMLMsgsRq, "SalesOrderQueryRq","\n      " )
        if len(txnid)>0:
            for x in txnid:
                TxnID = self.create_sub_element(ET, SalesOrderQueryRq, "TxnID", x, 6 )
        else:
            return None
        if IncludeLineItems:
            IncludeLineItems = self.create_sub_element(ET, SalesOrderQueryRq, "IncludeLineItems", 'true', 4)
        # if len(self.IncludeRetElement)>0:
        #     for x in self.IncludeRetElement:
        #         IncludeRetElement = self.create_sub_element(ET, SalesOrderQueryRq, "IncludeRetElement", x, 4)
        mydata = ET.tostring(root, encoding = "unicode")

        qbxml_query = """<?xml version="1.0" encoding="utf-8"?>\n"""
        qbxml_query = qbxml_query + """<?qbxml version="13.0"?>"""
        qbxml_query = qbxml_query + "\n" + mydata
        print(f'create_QBXML->qbxml_query: {qbxml_query}')
        return qbxml_query


    def get_open_sales_order(self, txnid:list):
        print(txnid, type(txnid))
        print(self.connect_to_quickbooks(self.create_open_sales_order_qbxml(txnid)))
        self._get_sales_order_header(self.connect_to_quickbooks(self.create_open_sales_order_qbxml(txnid)))

print('### SalesOrder ###')
if __name__ == '__main__':
    starttime = timeit.default_timer()
    ini=SalesOrderQuery(FullName= '999 HPL', IncludeRetElement = ['TxnID', 'TimeCreated', 'TimeModified','TxnNumber', 'CustomerRef', 'IsManuallyClosed', 'IsFullyInvoiced'])
    print(ini.create_QBXML())
    response_string = ini.connect_to_quickbooks(ini.create_QBXML())
    open_sales_orders = ini.get_open_so(response_string)
    print(f'{open_sales_orders = }')
    print(ini.get_open_sales_order(open_sales_orders))
    
    print("The time difference is :", timeit.default_timer() - starttime)