From e38d9c35e0e0918f4dd4331430bdb5e05d01b4a2 Mon Sep 17 00:00:00 2001 From: bcomsugi Date: Mon, 2 Sep 2024 03:54:42 +0700 Subject: [PATCH] SalesOrderAdd and folder QBClass --- QBClass/QBClasses.py | 1235 ++++++++++++++++++++++++++++++++++++++++++ QBClass/__init__.py | 0 QBClass/server.py | 511 +++++++++++++++++ QBClass/utils.py | 142 +++++ main.py | 21 +- 5 files changed, 1908 insertions(+), 1 deletion(-) create mode 100644 QBClass/QBClasses.py create mode 100644 QBClass/__init__.py create mode 100644 QBClass/server.py create mode 100644 QBClass/utils.py diff --git a/QBClass/QBClasses.py b/QBClass/QBClasses.py new file mode 100644 index 0000000..b09c8d8 --- /dev/null +++ b/QBClass/QBClasses.py @@ -0,0 +1,1235 @@ +from .server import baseQBQuery +# from server import baseQBQuery, timing +import pprint +# import timeit +# import xml.dom.minidom +from .utils import timing, cleanIncludeRetElements, makeAList +import json +from typing import Union + +ENUM_DateMacro = ['All', 'Today', 'ThisWeek', 'ThisWeekToDate', 'ThisMonth', 'ThisMonthToDate', 'ThisCalendarQuarter', 'ThisCalendarQuarterToDate', 'ThisFiscalQuarter', + 'ThisFiscalQuarterToDate', 'ThisCalendarYear', 'ThisCalendarYearToDate', 'ThisFiscalYear', 'ThisFiscalYearToDate', 'Yesterday', 'LastWeek', 'LastWeekToDate', 'LastMonth', + 'LastMonthToDate', 'LastCalendarQuarter', 'LastCalendarQuarterToDate', 'LastFiscalQuarter', 'LastFiscalQuarterToDate', 'LastCalendarYear', 'LastCalendarYearToDate', + 'LastFiscalYear', 'LastFiscalYearToDate', 'NextWeek', 'NextFourWeeks', 'NextMonth', 'NextCalendarQuarter', 'NextCalendarYear', 'NextFiscalQuarter', 'NextFiscalYear' + ] + +class ItemInventoryQuery(baseQBQuery): + def __init__(self, *args, **kwargs): + print(f'{args = }') + print(f'{kwargs = }') + super().__init__(*args, **kwargs) + ### Required Variable + self.includeRetElements_allowed = ["ListID", "FullName", "TimeCreated", "TimeModified", "EditSequence", "Name", "IsActive", "ClassRef", "ParentRef", "Sublevel", "BarCodeValue", + "ManufacturerPartNumber", "UnitOfMeasureSetRef", "IsTaxIncluded", "SalesTaxCodeRef", "SalesDesc,", "SalesPrice", "IncomeAccountRef", + "PurchaseDesc", "PurchaseCost", "PurchaseTaxCodeRef", "COGSAccountRef", "PrefVendorRef", "AssetAccountRef", "ReforderPoint", "Max", "QuantityOnHand", + "AcerageCost", "QuantityOnOrder", "QuantityOnSalesOrder", + "ExternalGUID", "DataExtRet", + ] + self.onError = "stopOnError" + self.retName = 'ItemInventoryRet' + self.defaultFilterKey = "ListID" + self.className = "ItemInventoryQuery" + self.classNameRq:str = self.__class__.__name__ + 'Rq' + if 'debug' in kwargs and isinstance(kwargs['debug'], bool): + self.class_debug=kwargs["debug"] + + self.QBDict[self.classNameRq]={} + ### End Required Variable. can put ENUM list below this (eg: self.ENUM_GeneralSummaryReportQuery=[]) + + if 'ListID' in kwargs: + self.QBDict[self.classNameRq]["ListID"]=kwargs['ListID'] + elif 'FullName' in kwargs: + self.QBDict[self.classNameRq]["FullName"]=kwargs['FullName'] + else: + if 'MaxReturned' in kwargs: + self.QBDict[self.classNameRq]["MaxReturned"]=kwargs['MaxReturned'] + if 'ActiveStatus' in kwargs: + self.QBDict[self.classNameRq]["ActiveStatus"]=kwargs['ActiveStatus'] + if 'FromModifiedDate' in kwargs: + self.QBDict[self.classNameRq]["FromModifiedDate"]=kwargs['FromModifiedDate'] + if 'ToModifiedDate' in kwargs: + self.QBDict[self.classNameRq]["ToModifiedDate"]=kwargs['ToModifiedDate'] + if 'MatchCriterion' in kwargs and 'Name' in kwargs: + self.QBDict[self.classNameRq]["NameFilter"]={'MatchCriterion':kwargs['MatchCriterion', 'Name':kwargs['Name']]} + elif 'FromName' in kwargs or 'ToName' in kwargs: #if or then use {'key1':kwargs.get('key1',""), 'key2':kwargs.get('key2', "")} + self.QBDict[self.classNameRq]["NameRangeFilter"]={'FromName':kwargs.get('FromName', ""), 'ToName':kwargs.get('ToName', "")} + + if 'IncludeRetElement' in kwargs: + IRE = cleanIncludeRetElements(self.includeRetElements_allowed, kwargs["IncludeRetElement"]) # IRE->IncludeRetElements cleaned version + print(f"{IRE = }") + if len(IRE)>0: + if self.defaultFilterKey not in IRE: # defaultFilterKey is for BaseClass.count() eg: after instantiate, then print obj.count() + IRE.append(self.defaultFilterKey) + self.QBDict[self.classNameRq]["IncludeRetElement"]=IRE + + if 'OwnerID' in kwargs: # usually value=0 to get to DataExtRet (additional data) + self.QBDict[self.classNameRq]["OwnerID"]=kwargs['OwnerID'] + + # print(self.classNameRq) + # print(self.QBDict) + if self.__class__.__name__==self.className: + self.runCheck() ### running the qbxml connection to get data ### + +class GeneralSummaryReportQuery(baseQBQuery): + def __init__(self, *args, **kwargs): + print(f'{args = }') + print(f'{kwargs = }') + super().__init__( ) + ## Required variable + self.includeRetElements_allowed = ["ReportTitle", "ReportSubtitle", "ReportBasis", "NumRows", "NumColumns", "NumColTitleRows", "ReportData", ] + self.onError = "stopOnError" + self.retName = 'ReportRet' + self.defaultFilterKey = "ListID" + self.className = "GeneralSummaryReportQuery" + self.classNameRq:str = self.__class__.__name__ + 'Rq' + if 'debug' in kwargs and isinstance(kwargs['debug'], bool): + self.class_debug=kwargs["debug"] + + self.QBDict[self.classNameRq]={} #Required + ### End Required variable + self.ENUM_GeneralSummaryReportType = ["BalanceSheetByClass", "BalanceSheetPrevYearComp", "BalanceSheetStandard", "BalanceSheetSummary", "CustomerBalanceSummary", + "ExpenseByVendorSummary", "IncomeByCustomerSummary", "InventoryStockStatusByItem", "InventoryStockStatusByVendor", "IncomeTaxSummary", + "InventoryValuationSummary", "InventoryValuationSummaryBySite", "LotNumberInStockBySite", "PhysicalInventoryWorksheet", "ProfitAndLossByClass", + "ProfitAndLossByJob", "ProfitAndLossPrevYearComp", "ProfitAndLossStandard", "ProfitAndLossYTDComp", "PurchaseByItemSummary", "PurchaseByVendorSummary", + "SalesByCustomerSummary", "SalesByItemSummary", "SalesByRepSummary", "SalesTaxLiability", "SalesTaxRevenueSummary", "SerialNumberInStockBySite", + "TrialBalance", "VendorBalanceSummary"] + + if 'GeneralSummaryReportType' in kwargs: + enum=cleanIncludeRetElements(self.ENUM_GeneralSummaryReportType, kwargs['GeneralSummaryReportType']) + print(enum) + self.QBDict[self.classRq]["GeneralSummaryReportType"]=enum[0] + else: + print("Error -> GeneralSummaryReportType is required") + return + + # if 'DisplayReport_ReportPeriod_FromReportDate' in kwargs or 'DisplayReport_ReportPeriod_ToReportDate' in kwargs: + # self.QBDict[self.classNameRq]['DisplayReport']={'ReportPeriod':{}} + # if 'DisplayReport_ReportPeriod_FromReportDate' in kwargs: + # self.QBDict[self.classNameRq]["DisplayReport"]['ReportPeriod']['FromReportDate']=kwargs['DisplayReport_ReportPeriod_FromReportDate'] + # if 'DisplayReport_ReportPeriod_ToReportDate' in kwargs: + # self.QBDict[self.classNameRq]["DisplayReport"]['ReportPeriod']['ToReportDate']=kwargs['DisplayReport_ReportPeriod_ToReportDate'] + # elif 'DisplayReport_ReportPeriod_ReportDateMacro' in kwargs: + # self.QBDict[self.classNameRq]['DisplayReport']={"ReportDateMacro":kwargs['DisplayReport_ReportPeriod_ReportDateMacro']} + + if 'DisplayReport' in kwargs: + self.QBDict[self.classNameRq]["DisplayReport"]=kwargs['DisplayReport'] + + if 'ReportPeriod_FromReportDate' in kwargs or 'ReportPeriod_ToReportDate' in kwargs: #if or then use {'key1':kwargs.get('key1',""), 'key2':kwargs.get('key2', "")} + self.QBDict[self.classNameRq]["ReportPeriod"]={'FromReportDate':kwargs.get('ReportPeriod_FromReportDate', None), 'ToReportDate':kwargs.get('ReportPeriod_ToReportDate', None)} + elif 'ReportDateMacro' in kwargs: + self.QBDict[self.classNameRq]["ReportDateMacro"]=kwargs['ReportDateMacro'] + + if 'ReportAccountFilter_AccountTypeFilter' in kwargs: + self.QBDict[self.classNameRq]["ReportAccountFilter"]={'AccountTypeFilter':kwargs['ReportAccountFilter_AccountTypeFilter']} + elif 'ReportAccountFilter_ListID' in kwargs: + self.QBDict[self.classNameRq]["ReportAccountFilter"]={'ListID':kwargs['ReportAccountFilter_ListID']} + elif 'ReportAccountFilter_FullName' in kwargs: + self.QBDict[self.classNameRq]["ReportAccountFilter"]={'FullName':kwargs['ReportAccountFilter_FullName']} + elif 'ReportAccountFilter_ListIDWithChildren' in kwargs: + self.QBDict[self.classNameRq]["ReportAccountFilter"]={'ListIDWithChildren':kwargs['ReportAccountFilter_ListIDWithChildren']} + elif 'ReportAccountFilter_FullNameWithChildren' in kwargs: + self.QBDict[self.classNameRq]["ReportAccountFilter"]={'FullNameWithChildren':kwargs['ReportAccountFilter_FullNameWithChildren']} + + if 'ReportEntityFilter_EntityTypeFilter' in kwargs: + self.QBDict[self.classNameRq]["ReportEntityFilter"]={'EntityTypeFilter':kwargs['ReportEntityFilter_EntityTypeFilter']} + elif 'ReportEntityFilter_ListID' in kwargs: + self.QBDict[self.classNameRq]["ReportEntityFilter"]={'ListID':kwargs['ReportEntityFilter_ListID']} + elif 'ReportEntityFilter_FullName' in kwargs: + self.QBDict[self.classNameRq]["ReportEntityFilter"]={'FullName':kwargs['ReportEntityFilter_FullName']} + elif 'ReportEntityFilter_ListIDWithChildren' in kwargs: + self.QBDict[self.classNameRq]["ReportEntityFilter"]={'ListIDWithChildren':kwargs['ReportEntityFilter_ListIDWithChildren']} + elif 'ReportEntityFilter_FullNameWithChildren' in kwargs: + self.QBDict[self.classNameRq]["ReportEntityFilter"]={'FullNameWithChildren':kwargs['ReportEntityFilter_FullNameWithChildren']} + + if 'ReportItemFilter_ItemTypeFilter' in kwargs: + self.QBDict[self.classNameRq]["ReportItemFilter"]={'ItemTypeFilter':kwargs['ReportItemFilter_ItemTypeFilter']} + elif 'ReportItemFilter_ListID' in kwargs: + self.QBDict[self.classNameRq]["ReportItemFilter"]={'ListID':kwargs['ReportItemFilter_ListID']} + elif 'ReportItemFilter_FullName' in kwargs: + self.QBDict[self.classNameRq]["ReportItemFilter"]={'FullName':kwargs['ReportItemFilter_FullName']} + elif 'ReportItemFilter_ListIDWithChildren' in kwargs: + self.QBDict[self.classNameRq]["ReportItemFilter"]={'ListIDWithChildren':kwargs['ReportItemFilter_ListIDWithChildren']} + elif 'ReportItemFilter_FullNameWithChildren' in kwargs: + self.QBDict[self.classNameRq]["ReportItemFilter"]={'FullNameWithChildren':kwargs['ReportItemFilter_FullNameWithChildren']} + + if 'ReportClassFilter_ListID' in kwargs: + self.QBDict[self.classNameRq]["ReportClassFilter"]={'ListID':kwargs['ReportClassFilter_ListID']} + elif 'ReportClassFilter_FullName' in kwargs: + self.QBDict[self.classNameRq]["ReportClassFilter"]={'FullName':kwargs['ReportClassFilter_FullName']} + elif 'ReportClassFilter_ListIDWithChildren' in kwargs: + self.QBDict[self.classNameRq]["ReportClassFilter"]={'ListIDWithChildren':kwargs['ReportClassFilter_ListIDWithChildren']} + elif 'ReportClassFilter_FullNameWithChildren' in kwargs: + self.QBDict[self.classNameRq]["ReportClassFilter"]={'FullNameWithChildren':kwargs['ReportClassFilter_FullNameWithChildren']} + + if 'ReportTxnTypeFilter_ListID' in kwargs: + self.QBDict[self.classNameRq]["ReportTxnTypeFilter"]={'TxnTypeFilter':makeAList(kwargs['ReportTxnTypeFilter_ListID'])} + + if 'ReportModifiedDateRangeFilter_FromReportModifiedDate' in kwargs or 'ReportModifiedDateRangeFilter_ToReportModifiedDate' in kwargs: #if or then use {'key1':kwargs.get('key1',""), 'key2':kwargs.get('key2', "")} + self.QBDict[self.classNameRq]["ReportModifiedDateRangeFilter"]={'FromReportModifiedDate':kwargs.get('ReportModifiedDateRangeFilter_FromReportModifiedDate', None), 'ToReportModifiedDate':kwargs.get('ReportModifiedDateRangeFilter_ToReportModifiedDate', None)} + elif 'ReportModifiedDateRangeMacro' in kwargs: + self.QBDict[self.classNameRq]["ReportModifiedDateRangeMacro"]=cleanIncludeRetElements(ENUM_DateMacro, kwargs['ReportModifiedDateRangeMacro']) + + if 'ReportDetailLevelFilter' in kwargs: + self.QBDict[self.classNameRq]["ReportDetailLevelFilter"]=cleanIncludeRetElements(['All', 'AllExceptSummary', 'SummaryOnly'], kwargs['ReportDetailLevelFilter'], 'All') + if 'ReportPostingStatusFilter' in kwargs: + self.QBDict[self.classNameRq]["ReportPostingStatusFilter"]=cleanIncludeRetElements(['Either', 'NonPosting', 'Posting'], kwargs['ReportPostingStatusFilter']) + if 'SummarizeColumnsBy' in kwargs: + self.ENUM_SummarizeColumnsBy=['Account', 'BalanceSheet', 'Class', 'Customer', 'CustomerType', 'Day', 'Employee', 'FourWeek', 'HalfMonth', 'IncomeStatement', 'ItemDetail', 'ItemType', 'Month', 'Payee', + 'PaymentMethod', 'PayrollItemDetail', 'PayrollYtdDetail', 'Quarter', 'SalesRep', 'SalesTaxCode', 'ShipMethod', 'Terms', 'TotalOnly', 'TwoWeek', 'Vendor', 'VendorType', 'Week', 'Year'] + self.QBDict[self.classNameRq]["SummarizeColumnsBy"]=cleanIncludeRetElements(self.ENUM_SummarizeColumnsBy, kwargs['SummarizeColumnsBy']) + if 'IncludeSubColumns' in kwargs: + self.QBDict[self.classNameRq]["IncludeSubColumns"]=kwargs['IncludeSubColumns'] + if 'ReportCalendar' in kwargs: + self.QBDict[self.classNameRq]["ReportCalendar"]=cleanIncludeRetElements(['CalendarYear', 'FiscalYear', 'TaxYear'], kwargs['ReportCalendar'], default_val='CalendarYear') + if 'ReturnsRows' in kwargs: + self.QBDict[self.classNameRq]["ReturnsRows"]=cleanIncludeRetElements(['ActiveOnly', 'NonZero', 'All'], kwargs['ReturnsRows'], default_val='ActiveOnly') + if 'ReturnColumns' in kwargs: + self.QBDict[self.classNameRq]["ReturnColumns"]=cleanIncludeRetElements(['ActiveOnly', 'NonZero', 'All'], kwargs['ReturnColumns'], default_val='ActiveOnly') + if 'ReportBasis' in kwargs: + self.QBDict[self.classNameRq]["ReportBasis"]=cleanIncludeRetElements(['Accrual', 'Cash', 'None'], kwargs['ReportBasis'], default_val='None') + + if 'IncludeRetElement' in kwargs: + IRE = cleanIncludeRetElements(self.includeRetElements_allowed, kwargs["IncludeRetElement"]) #IRE->IncludeRetElements cleaned version + print(f"{IRE = }") + if len(IRE)>0: + if self.defaultFilterKey not in IRE: #defaultFilterKey is for BaseClass.count() eg: after instantiate, then print obj.count() + IRE.append(self.defaultFilterKey) + self.QBDict[self.classNameRq]["IncludeRetElement"]=IRE + + # print(self.classNameRq) + # print(f'{self.QBDict = }') + if self.__class__.__name__==self.className: + self.runCheck() ### running the qbxml connection to get data ### +import xmltodict +def LineAdd(lineAdd:Union[list, dict])->dict: + if not(isinstance(lineAdd, list) or isinstance(lineAdd, dict)): + raise Exception(f"LineAdd Error. {type(lineAdd)}, should use dictionary or list of dictionary. ") + if isinstance(lineAdd, dict): #convert to listofdict + lineAdd = [lineAdd] + ## process LineAdd dict + resultList = [] + resdict = {} + for xLineAdd in lineAdd: + resdict={'@defMacro':'MACROTYPE'} + resdict['abc']="" + # resdict['ItemRef']={} + if 'ItemRef_ListID' in xLineAdd and xLineAdd['ItemRef_ListID']: + print(":he") + if 'ItemRef' in resdict: + resdict['ItemRef']['ListID']= xLineAdd['ItemRef_ListID'] + else: + resdict['ItemRef']={'ListID': xLineAdd['ItemRef_ListID']} + print(resdict) + if 'ItemRef_FullName' in xLineAdd and xLineAdd['ItemRef_FullName']: + resdict['ItemRef']['FullName']= xLineAdd['ItemRef_FullName'] + if 'Desc' in xLineAdd and xLineAdd['Desc']: + resdict['Desc']= xLineAdd['Desc'] + + resultList.append(resdict) + print(f'{resultList = }') + xyz=xmltodict.unparse({'root':{'LineAdd':resultList}}, pretty=True) + print(xyz) + txt = ''' + + + + 1234 + Sugiarto + + lorem + + ''' + dct = xmltodict.parse(txt) + print(f'{dct = }') + +# x=[{'ItemRef_FullName': 'Sugiarto', 'ItemRef_ListID':1234, 'Desc':"lorem"}, {'ItemRef_FullName': 'Sugiarto', 'ItemRef_ListID':1234, 'Desc':"lorem"}] +# LineAdd(x) + +# print(cleanIncludeRetElements(['ActiveOnly', 'NonZero', 'All'], "nonzero", 'ActiveOnly')) + +class InvoiceAdd(baseQBQuery): + def __init__(self, *args, **kwargs): + print(f'{args = }') + print(f'{kwargs = }') + super().__init__(*args, **kwargs) + # self.includeRetElements_allowed = ['TxnID', 'TimeCreated', 'TimeModified', 'EditSequence', 'TxnNumber', 'CustomerRef', 'ClassRef', 'ARAccontRef', 'TemplateRef'] + self.onError = "stopOnError" + self.retName = 'InvoiceAddRet' + + self.defaultFilterKey = "TxnID" + self.className = "InvoiceAdd" + self.classNameRq:str = self.__class__.__name__ + 'Rq' + self.reqSubName = self.className + if 'debug' in kwargs and isinstance(kwargs['debug'], bool): + self.class_debug=kwargs["debug"] + + self.QBDict[self.classNameRq]={} + + self.isRequiredFieldSatisfied = False + self.QBDict[self.classNameRq][self.__class__.__name__]={'@defMacro':'MACROTYPE'} + if 'CustomerRef_ListID' in kwargs: + if 'CustomerRef' in self.QBDict[self.classNameRq][self.__class__.__name__]: + self.QBDict[self.classNameRq][self.__class__.__name__]["CustomerRef"]['ListID']= kwargs['CustomerRef_ListID'] + else: + self.QBDict[self.classNameRq][self.__class__.__name__]["CustomerRef"]={'ListID': kwargs['CustomerRef_ListID']} + self.isRequiredFieldSatisfied=True + if 'CustomerRef_FullName' in kwargs: + self.QBDict[self.classNameRq][self.__class__.__name__]["CustomerRef"]={'FullName': kwargs['CustomerRef_FullName']} + self.isRequiredFieldSatisfied=True + + if not self.isRequiredFieldSatisfied: + raise Exception("Need CustomerRef FullName and/or ListID") + + if 'ClassRef_ListID' in kwargs: + if 'ClassRef' in self.QBDict[self.classNameRq][self.__class__.__name__]: + self.QBDict[self.classNameRq][self.__class__.__name__]["ClassRef"]['ListID']= kwargs['ClassRef_ListID'] + else: + self.QBDict[self.classNameRq][self.__class__.__name__]["ClassRef"]={'ListID': kwargs['ClassRef_ListID']} + if 'ClassRef_FullName' in kwargs: + self.QBDict[self.classNameRq][self.__class__.__name__]["ClassRef"]={'FullName': kwargs['ClassRef_FullName']} + + if 'ARAccountRef_ListID' in kwargs: + if 'ARAccountRef' in self.QBDict[self.classNameRq][self.__class__.__name__]: + self.QBDict[self.classNameRq][self.__class__.__name__]["ARAccountRef"]['ListID']= kwargs['ARAccountRef_ListID'] + else: + self.QBDict[self.classNameRq][self.__class__.__name__]["ARAccountRef"]={'ListID': kwargs['ARAccountRef_ListID']} + if 'ARAccountRef_FullName' in kwargs: + self.QBDict[self.classNameRq][self.__class__.__name__]["ARAccountRef"]={'FullName': kwargs['ARAccountRef_FullName']} + + if 'TemplateRef_ListID' in kwargs: + if 'TemplateRef' in self.QBDict[self.classNameRq][self.__class__.__name__]: + self.QBDict[self.classNameRq][self.__class__.__name__]["TemplateRef"]['ListID']= kwargs['TemplateRef_ListID'] + else: + self.QBDict[self.classNameRq][self.__class__.__name__]["TemplateRef"]={'ListID': kwargs['TemplateRef_ListID']} + if 'TemplateRef_FullName' in kwargs: + self.QBDict[self.classNameRq][self.__class__.__name__]["TemplateRef"]={'FullName': kwargs['TemplateRef_FullName']} + + if 'TxnDate' in kwargs: + self.QBDict[self.classNameRq][self.__class__.__name__]["TxnDate"]=kwargs['TxnDate'] + if 'RefNumber' in kwargs: + self.QBDict[self.classNameRq][self.__class__.__name__]["RefNumber"]=kwargs['RefNumber'] + if 'BillAddress_Addr1' in kwargs: + self.QBDict[self.classNameRq][self.__class__.__name__]["BillAddress"]={'Addr1': kwargs['BillAddress_Addr1']} + if 'BillAddress_Addr2' in kwargs: + self.QBDict[self.classNameRq][self.__class__.__name__]["BillAddress"]={'Addr2': kwargs['BillAddress_Addr2']} + if 'BillAddress_Addr3' in kwargs: + self.QBDict[self.classNameRq][self.__class__.__name__]["BillAddress"]={'Addr3': kwargs['BillAddress_Addr3']} + if 'BillAddress_Addr4' in kwargs: + self.QBDict[self.classNameRq][self.__class__.__name__]["BillAddress"]={'Addr4': kwargs['BillAddress_Addr4']} + if 'BillAddress_Addr5' in kwargs: + self.QBDict[self.classNameRq][self.__class__.__name__]["BillAddress"]={'Addr5': kwargs['BillAddress_Addr5']} + if 'BillAddress_City' in kwargs: + self.QBDict[self.classNameRq][self.__class__.__name__]["BillAddress"]={'City': kwargs['BillAddress_City']} + if 'BillAddress_State' in kwargs: + self.QBDict[self.classNameRq][self.__class__.__name__]["BillAddress"]={'State': kwargs['BillAddress_State']} + if 'BillAddress_PostalCode' in kwargs: + self.QBDict[self.classNameRq][self.__class__.__name__]["BillAddress"]={'PostalCode': kwargs['BillAddress_PostalCode']} + if 'BillAddress_Country' in kwargs: + self.QBDict[self.classNameRq][self.__class__.__name__]["BillAddress"]={'Country': kwargs['BillAddress_Country']} + if 'BillAddress_Note' in kwargs: + self.QBDict[self.classNameRq][self.__class__.__name__]["BillAddress"]={'Note': kwargs['BillAddress_Note']} + + if 'ShipAddress_Addr1' in kwargs: + self.QBDict[self.classNameRq][self.__class__.__name__]["ShipAddress"]={'Addr1': kwargs['ShipAddress_Addr1']} + if 'ShipAddress_Addr2' in kwargs: + self.QBDict[self.classNameRq][self.__class__.__name__]["ShipAddress"]={'Addr2': kwargs['ShipAddress_Addr2']} + if 'ShipAddress_Addr3' in kwargs: + self.QBDict[self.classNameRq][self.__class__.__name__]["ShipAddress"]={'Addr3': kwargs['ShipAddress_Addr3']} + if 'ShipAddress_Addr4' in kwargs: + self.QBDict[self.classNameRq][self.__class__.__name__]["ShipAddress"]={'Addr4': kwargs['ShipAddress_Addr4']} + if 'ShipAddress_Addr5' in kwargs: + self.QBDict[self.classNameRq][self.__class__.__name__]["ShipAddress"]={'Addr5': kwargs['ShipAddress_Addr5']} + if 'ShipAddress_City' in kwargs: + self.QBDict[self.classNameRq][self.__class__.__name__]["ShipAddress"]={'City': kwargs['ShipAddress_City']} + if 'ShipAddress_State' in kwargs: + self.QBDict[self.classNameRq][self.__class__.__name__]["ShipAddress"]={'State': kwargs['ShipAddress_State']} + if 'ShipAddress_PostalCode' in kwargs: + self.QBDict[self.classNameRq][self.__class__.__name__]["ShipAddress"]={'PostalCode': kwargs['ShipAddress_PostalCode']} + if 'ShipAddress_Country' in kwargs: + self.QBDict[self.classNameRq][self.__class__.__name__]["ShipAddress"]={'Country': kwargs['ShipAddress_Country']} + if 'ShipAddress_Note' in kwargs: + self.QBDict[self.classNameRq][self.__class__.__name__]["ShipAddress"]={'Note': kwargs['ShipAddress_Note']} + + if 'IsPending' in kwargs: + self.QBDict[self.classNameRq][self.__class__.__name__]["IsPending"]=kwargs['IsPending'] + if 'IsFinanceCharge' in kwargs: + self.QBDict[self.classNameRq][self.__class__.__name__]["IsFinanceCharge"]=kwargs['IsFinanceCharge'] + if 'PONumber' in kwargs: + self.QBDict[self.classNameRq][self.__class__.__name__]["PONumber"]=kwargs['PONumber'] + if 'TermsRef_ListID' in kwargs: + if 'TermsRef' in self.QBDict[self.classNameRq][self.__class__.__name__]: + self.QBDict[self.classNameRq][self.__class__.__name__]["TermsRef"]['ListID']= kwargs['TermsRef_ListID'] + else: + self.QBDict[self.classNameRq][self.__class__.__name__]["TermsRef"]={'ListID': kwargs['TermsRef_ListID']} + if 'TermsRef_FullName' in kwargs: + self.QBDict[self.classNameRq][self.__class__.__name__]["TermsRef"]={'FullName': kwargs['TermsRef_FullName']} + + if 'DueDate' in kwargs: + self.QBDict[self.classNameRq][self.__class__.__name__]["DueDate"]=kwargs['DueDate'] + if 'SalesRepRef_ListID' in kwargs: + if 'SalesRepRef' in self.QBDict[self.classNameRq][self.__class__.__name__]: + self.QBDict[self.classNameRq][self.__class__.__name__]["SalesRepRef"]['ListID']= kwargs['SalesRepRef_ListID'] + else: + self.QBDict[self.classNameRq][self.__class__.__name__]["SalesRepRef"]={'ListID': kwargs['SalesRepRef_ListID']} + if 'SalesRepRef_FullName' in kwargs: + self.QBDict[self.classNameRq][self.__class__.__name__]["SalesRepRef"]={'FullName': kwargs['SalesRepRef_FullName']} + + if 'FOB' in kwargs: + self.QBDict[self.classNameRq][self.__class__.__name__]["FOB"]=kwargs['FOB'] + if 'ShipDate' in kwargs: + self.QBDict[self.classNameRq][self.__class__.__name__]["ShipDate"]=kwargs['ShipDate'] + if 'ShipMethodRef_ListID' in kwargs: + if 'ShipMethodRef' in self.QBDict[self.classNameRq][self.__class__.__name__]: + self.QBDict[self.classNameRq][self.__class__.__name__]["ShipMethodRef"]['ListID']= kwargs['ShipMethodRef_ListID'] + else: + self.QBDict[self.classNameRq][self.__class__.__name__]["ShipMethodRef"]={'ListID': kwargs['ShipMethodRef_ListID']} + if 'ShipMethodRef_FullName' in kwargs: + self.QBDict[self.classNameRq][self.__class__.__name__]["ShipMethodRef"]={'FullName': kwargs['ShipMethodRef_FullName']} + + if 'ItemSalesTaxRef_ListID' in kwargs: + if 'ItemSalesTaxRef' in self.QBDict[self.classNameRq][self.__class__.__name__]: + self.QBDict[self.classNameRq][self.__class__.__name__]["ItemSalesTaxRef"]['ListID']= kwargs['ItemSalesTaxRef_ListID'] + else: + self.QBDict[self.classNameRq][self.__class__.__name__]["ItemSalesTaxRef"]={'ListID': kwargs['ItemSalesTaxRef_ListID']} + if 'ItemSalesTaxRef_FullName' in kwargs: + self.QBDict[self.classNameRq][self.__class__.__name__]["ItemSalesTaxRef"]={'FullName': kwargs['ItemSalesTaxRef_FullName']} + + if 'Memo' in kwargs: + self.QBDict[self.classNameRq][self.__class__.__name__]["Memo"]=kwargs['Memo'] + if 'CustomerMsgRef_ListID' in kwargs: + if 'CustomerMsgRef' in self.QBDict[self.classNameRq][self.__class__.__name__]: + self.QBDict[self.classNameRq][self.__class__.__name__]["CustomerMsgRef"]['ListID']= kwargs['CustomerMsgRef_ListID'] + else: + self.QBDict[self.classNameRq][self.__class__.__name__]["CustomerMsgRef"]={'ListID': kwargs['CustomerMsgRef_ListID']} + if 'CustomerMsgRef_FullName' in kwargs: + self.QBDict[self.classNameRq][self.__class__.__name__]["CustomerMsgRef"]={'FullName': kwargs['CustomerMsgRef_FullName']} + + if 'IsToBePrinted' in kwargs: + self.QBDict[self.classNameRq][self.__class__.__name__]["IsToBePrinted"]=kwargs['IsToBePrinted'] + if 'IsToBeEmailed' in kwargs: + self.QBDict[self.classNameRq][self.__class__.__name__]["IsToBeEmailed"]=kwargs['IsToBeEmailed'] + if 'IsTaxIncluded' in kwargs: + self.QBDict[self.classNameRq][self.__class__.__name__]["IsTaxIncluded"]=kwargs['IsTaxIncluded'] + if 'CustomerSalesTaxCodeRef_ListID' in kwargs: + if 'CustomerSalesTaxCodeRef' in self.QBDict[self.classNameRq][self.__class__.__name__]: + self.QBDict[self.classNameRq][self.__class__.__name__]["CustomerSalesTaxCodeRef"]['ListID']= kwargs['CustomerSalesTaxCodeRef_ListID'] + else: + self.QBDict[self.classNameRq][self.__class__.__name__]["CustomerSalesTaxCodeRef"]={'ListID': kwargs['CustomerSalesTaxCodeRef_ListID']} + if 'CustomerSalesTaxCodeRef_FullName' in kwargs: + self.QBDict[self.classNameRq][self.__class__.__name__]["CustomerSalesTaxCodeRef"]={'FullName': kwargs['CustomerSalesTaxCodeRef_FullName']} + + if 'Other' in kwargs: + self.QBDict[self.classNameRq][self.__class__.__name__]["Other"]=kwargs['Other'] + if 'ExchangeRate' in kwargs: + self.QBDict[self.classNameRq][self.__class__.__name__]["ExchangeRate"]=kwargs['ExchangeRate'] + if 'ExternalGUID' in kwargs: + self.QBDict[self.classNameRq][self.__class__.__name__]["ExternalGUID"]=kwargs['ExternalGUID'] + if 'LinkToTxnID' in kwargs: + self.QBDict[self.classNameRq][self.__class__.__name__]["LinkToTxnID"]=makeAList(kwargs['LinkToTxnID']) + + if 'SetCredit_CreditTxnID' in kwargs and 'SetCredit_AppliedAmount' in kwargs: + self.QBDict[self.classNameRq][self.__class__.__name__]["SetCredit"]={'CreditTxnID':{'@useMacro':"MACROTYPE", '#text':kwargs['SetCredit_CreditTxnID']}, 'AppliedAmount': kwargs['SetCredit_AppliedAmount']} + if 'SetCredit_Override' in kwargs: + self.QBDict[self.classNameRq][self.__class__.__name__]["SetCredit"]["Override"]=kwargs['SetCredit_Override'] + #add InvoiceLineAdd kwargs here + + + if 'IncludeRetElement' in kwargs: + self.QBDict[self.classNameRq]["IncludeRetElement"]=kwargs['IncludeRetElement'] + + # print(self.classNameRq) + # print(self.QBDict) + + + +class CustomerQuery(baseQBQuery): + def __init__(self, *args, **kwargs): + print(f'{args = }') + print(f'{kwargs = }') + super().__init__(*args, **kwargs) + self.includeRetElements_allowed = ["ListID", "FullName", "TimeCreated", "TimeModified", "EditSequence", "Name", "IsActive", "ClassRef", "ParentRef", "Sublevel", "CompanyName", "Salutation", + "FirstName", "MiddleName", "LastName", "JobTitle", "BillAddress", "BillAddressBlock", "ShipAddress", "ShipAddressBlock", "ShipToAddress", "Phone", + "AltPhone", "Fax", "Email", "Cc", "Contact", "AltContact", "AdditionalContactRef", "ContactsRet", "CustomerTypeRef", "TermsRef", "SalesRepRef", + "Balance", "TotalBalance", "SalesTaxCodeRef", "ItemSalesTaxRef", "SalesTaxCountry", "ResaleNumber", "AccountNumber", "CreditLimit", + "PreferredPaymentMethodRef", "CreditCardInfo", "JobStatus", "JobStartDate", "JobProjectedEndDate", "JobEndDate", "JobDesc", "JobTypeRef", "Notes", + "AdditionalNotesRet", "PreferredDeliveryMethod", "PriceLevelRef", "ExternalGUID", "TaxRegistrationNumber", "CurrencyRef", "DataExtRet"] + self.onError = "stopOnError" + self.retName = 'CustomerRet' + self.defaultFilterKey = "ListID" + self.className = "CustomerQuery" + self.classNameRq:str = self.__class__.__name__ + 'Rq' + if 'debug' in kwargs and isinstance(kwargs['debug'], bool): + self.class_debug=kwargs["debug"] + + self.QBDict[self.classNameRq]={} + + if 'ListID' in kwargs: + self.QBDict[self.classNameRq]["ListID"]=kwargs['ListID'] + elif 'FullName' in kwargs: + self.QBDict[self.classNameRq]["FullName"]=kwargs['FullName'] + else: + if 'MaxReturned' in kwargs: + self.QBDict[self.classNameRq]["MaxReturned"]=kwargs['MaxReturned'] + if 'ActiveStatus' in kwargs: + self.QBDict[self.classNameRq]["ActiveStatus"]=kwargs['ActiveStatus'] + if 'FromModifiedDate' in kwargs: + self.QBDict[self.classNameRq]["FromModifiedDate"]=kwargs['FromModifiedDate'] + if 'ToModifiedDate' in kwargs: + self.QBDict[self.classNameRq]["ToModifiedDate"]=kwargs['ToModifiedDate'] + if 'MatchCriterion' in kwargs and 'Name' in kwargs: + self.QBDict[self.classNameRq]["NameFilter"]={'MatchCriterion':kwargs['MatchCriterion'], 'Name':kwargs['Name']} + elif 'FromName' in kwargs or 'ToName' in kwargs: + self.QBDict[self.classNameRq]["NameRangeFilter"]={'FromName':kwargs.get('FromName', ""), 'ToName':kwargs.get('ToName', "")} + + if 'Operator' in kwargs and 'Amount' in kwargs: + self.QBDict[self.classNameRq]["TotalBalanceFilter"]={'Operator':kwargs['Operator'], 'Amount':kwargs['Amount']} + if 'IncludeRetElement' in kwargs: + IRE = cleanIncludeRetElements(self.includeRetElements_allowed, kwargs['IncludeRetElement']) + print(f'{IRE = }') + if len(IRE)>0: + if self.defaultFilterKey not in IRE: + IRE.append(self.defaultFilterKey) + self.QBDict[self.classNameRq]["IncludeRetElement"]=IRE + if 'OwnerID' in kwargs: + self.QBDict[self.classNameRq]["OwnerID"]=kwargs['OwnerID'] + + print(self.classNameRq) + print(f'{self.QBDict = }' ) + # print(f'{self.includeRetElements_allowed =}') + if self.__class__.__name__==self.className: + self.runCheck() ### running the qbxml connection to get data ### + + +class TransactionQuery(baseQBQuery): + def __init__(self, *args, **kwargs): + print(f'{args = }') + print(f'{kwargs = }') + super().__init__(*args, **kwargs) + ### Required Variable + self.includeRetElements_allowed = ["TxnType", "TxnID", "TxnLineID", "TimeCreated", "TimeModified", "EntityRef", "AccountRef", "TxnDate", "RefNumber", + "Amount", "CurrencyRef", "ExchangeRate", "AmountInHomeCurrency", "Memo", + ] + self.onError = "stopOnError" + self.retName = 'TransactionRet' + self.defaultFilterKey = "TxnID" + self.className = "TransactionQuery" + self.classNameRq = self.__class__.__name__ + 'Rq' + if 'debug' in kwargs and isinstance(kwargs['debug'], bool): + self.class_debug=kwargs["debug"] + + self.QBDict[self.classNameRq]={} + ### End Required Variable. can put ENUM list below this (eg: self.ENUM_GeneralSummaryReportQuery=[]) + + if 'TxnID' in kwargs: + self.QBDict[self.classNameRq]["TxnID"]=kwargs['TxnID'] + else: + if 'MaxReturned' in kwargs: + if kwargs['MaxReturned'] is None: + if 'TxnID' in kwargs: + MaxReturned = None + elif len(kwargs)>1 : + MaxReturned = None + else: + MaxReturned = 1000 + elif isinstance(kwargs['MaxReturned'], str) and not kwargs['MaxReturned'].isdigit(): + MaxReturned = 0 + elif not isinstance(kwargs['MaxReturned'], int): + MaxReturned = 0 + elif int(kwargs['MaxReturned']) > 1000: + MaxReturned = 1000 + elif int(kwargs['MaxReturned']) > 0: + MaxReturned = kwargs['MaxReturned'] + else: + MaxReturned = 0 + else: + MaxReturned = 0 + self.QBDict[self.classNameRq]["MaxReturned"]=MaxReturned + + if 'RefNumber' in kwargs: + self.QBDict[self.classNameRq]["RefNumber"]=kwargs['RefNumber'] + elif 'RefNumberCaseSensitive' in kwargs: + self.QBDict[self.classNameRq]["RefNumberCaseSensitive"]=kwargs['RefNumberCaseSensitive'] + elif 'RefNumberFilter_MatchCriterion' in kwargs and 'RefNumberFilter_RefNumber' in kwargs: + self.QBDict[self.classNameRq]["RefNumberFilter"]={'MatchCriterion':kwargs['RefNumberFilter_MatchCriterion', 'RefNumber':kwargs['RefNumberFilter_RefNumber']]} + elif 'RefNumberFilter_FromRefNumber' in kwargs or 'RefNumberFilter_ToRefNumber' in kwargs: #if or then use {'key1':kwargs.get('key1',""), 'key2':kwargs.get('key2', "")} + self.QBDict[self.classNameRq]["RefNumberRangeFilter"]={'FromRefNumber':kwargs.get('RefNumberFilter_FromRefNumber', ""), 'ToRefNumber':kwargs.get('RefNumberFilter_ToRefNumber', "")} + + if 'TransactionModifiedDateRangeFilter_FromModifiedDate' in kwargs or 'TransactionModifiedDateRangeFilter_ToModifiedDate' in kwargs: #if or then use {'key1':kwargs.get('key1',""), 'key2':kwargs.get('key2', "")} + self.QBDict[self.classNameRq]["TransactionModifiedDateRangeFilter"]={'FromModifiedDate':kwargs.get('TransactionModifiedDateRangeFilter_FromModifiedDate', ""), 'ToModifiedDate':kwargs.get('TransactionModifiedDateRangeFilter_ToModifiedDate', "")} + elif 'TransactionModifiedDateRangeFilter_DateMacro' in kwargs: + self.QBDict[self.classNameRq]["TransactionModifiedDateRangeFilter"]={'DateMacro':kwargs['TransactionModifiedDateRangeFilter_DateMacro']} + + if 'TransactionDateRangeFilter_FromTxnDate' in kwargs or 'TransactionDateRangeFilter_ToTxnDate' in kwargs: #if or then use {'key1':kwargs.get('key1',""), 'key2':kwargs.get('key2', "")} + self.QBDict[self.classNameRq]["TransactionDateRangeFilter"]={'FromTxnDate':kwargs.get('TransactionDateRangeFilter_FromTxnDate', ""), 'ToTxnDate':kwargs.get('TransactionDateRangeFilter_ToTxnDate', "")} + elif 'TransactionDateRangeFilter_DateMacro' in kwargs: + self.QBDict[self.classNameRq]["TransactionDateRangeFilter"]={'DateMacro':kwargs['TransactionDateRangeFilter_DateMacro']} + + if 'TransactionEntityFilter_EntityTypeFilter' in kwargs: + self.QBDict[self.classNameRq]["TransactionEntityFilter"]={'EntityTypeFilter':kwargs['TransactionEntityFilter_EntityTypeFilter']} + elif 'TransactionEntityFilter_ListID' in kwargs: + self.QBDict[self.classNameRq]["TransactionEntityFilter"]={'ListID':kwargs['TransactionEntityFilter_ListID']} + elif 'TransactionEntityFilter_FullName' in kwargs: + self.QBDict[self.classNameRq]["TransactionEntityFilter"]={'FullName':kwargs['TransactionEntityFilter_FullName']} + elif 'TransactionEntityFilter_ListIDWithChildren' in kwargs: + self.QBDict[self.classNameRq]["TransactionEntityFilter"]={'ListIDWithChildren':kwargs['TransactionEntityFilter_ListIDWithChildren']} + elif 'TransactionEntityFilter_FullNameWithChildren' in kwargs: + self.QBDict[self.classNameRq]["TransactionEntityFilter"]={'FullNameWithChildren':kwargs['TransactionEntityFilter_FullNameWithChildren']} + + if 'TransactionAccountFilter_AccountTypeFilter' in kwargs: + self.QBDict[self.classNameRq]["TransactionAccountFilter"]={'AccountTypeFilter':kwargs['TransactionAccountFilter_AccountTypeFilter']} + elif 'TransactionAccountFilter_ListID' in kwargs: + self.QBDict[self.classNameRq]["TransactionAccountFilter"]={'ListID':kwargs['TransactionAccountFilter_ListID']} + elif 'TransactionAccountFilter_FullName' in kwargs: + self.QBDict[self.classNameRq]["TransactionAccountFilter"]={'FullName':kwargs['TransactionAccountFilter_FullName']} + elif 'TransactionAccountFilter_ListIDWithChildren' in kwargs: + self.QBDict[self.classNameRq]["TransactionAccountFilter"]={'ListIDWithChildren':kwargs['TransactionAccountFilter_ListIDWithChildren']} + elif 'TransactionAccountFilter_FullNameWithChildren' in kwargs: + self.QBDict[self.classNameRq]["TransactionAccountFilter"]={'FullNameWithChildren':kwargs['TransactionAccountFilter_FullNameWithChildren']} + + if 'TransactionItemFilter_ItemTypeFilter' in kwargs: + self.QBDict[self.classNameRq]["TransactionItemFilter"]={'ItemTypeFilter':kwargs['TransactionItemFilter_ItemTypeFilter']} + elif 'TransactionItemFilter_ListID' in kwargs: + self.QBDict[self.classNameRq]["TransactionItemFilter"]={'ListID':kwargs['TransactionItemFilter_ListID']} + elif 'TransactionItemFilter_FullName' in kwargs: + self.QBDict[self.classNameRq]["TransactionItemFilter"]={'FullName':kwargs['TransactionItemFilter_FullName']} + elif 'TransactionItemFilter_ListIDWithChildren' in kwargs: + self.QBDict[self.classNameRq]["TransactionItemFilter"]={'ListIDWithChildren':kwargs['TransactionItemFilter_ListIDWithChildren']} + elif 'TransactionItemFilter_FullNameWithChildren' in kwargs: + self.QBDict[self.classNameRq]["TransactionItemFilter"]={'FullNameWithChildren':kwargs['TransactionItemFilter_FullNameWithChildren']} + + if 'TransactionClassFilter_ListID' in kwargs: + self.QBDict[self.classNameRq]["TransactionClassFilter"]={'ListID':kwargs['TransactionClassFilter_ListID']} + elif 'TransactionClassFilter_FullName' in kwargs: + self.QBDict[self.classNameRq]["TransactionClassFilter"]={'FullName':kwargs['TransactionClassFilter_FullName']} + elif 'TransactionClassFilter_ListIDWithChildren' in kwargs: + self.QBDict[self.classNameRq]["TransactionClassFilter"]={'ListIDWithChildren':kwargs['TransactionClassFilter_ListIDWithChildren']} + elif 'TransactionClassFilter_FullNameWithChildren' in kwargs: + self.QBDict[self.classNameRq]["TransactionClassFilter"]={'FullNameWithChildren':kwargs['TransactionClassFilter_FullNameWithChildren']} + + if 'TransactionTypeFilter_TxnTypeFilter' in kwargs: + self.QBDict[self.classNameRq]["TransactionTypeFilter"]={'TxnTypeFilter':kwargs['TransactionTypeFilter_TxnTypeFilter']} + + if 'TransactionDetailLevelFilter' in kwargs: + self.QBDict[self.classNameRq]["TransactionDetailLevelFilter"]=kwargs['TransactionDetailLevelFilter'] + + if 'TransactionPostingStatusFilter' in kwargs: + self.QBDict[self.classNameRq]["TransactionPostingStatusFilter"]=kwargs['TransactionPostingStatusFilter'] + + if 'TransactionPaidStatusFilter' in kwargs: + self.QBDict[self.classNameRq]["TransactionPaidStatusFilter"]=kwargs['TransactionPaidStatusFilter'] + + if 'CurrencyFilter_ListID' in kwargs: + self.QBDict[self.classNameRq]["CurrencyFilter"]=kwargs['CurrencyFilter_ListID'] + elif 'CurrencyFilter_FullName' in kwargs: + self.QBDict[self.classNameRq]["CurrencyFilter"]=kwargs['CurrencyFilter_FullName'] + + if 'IncludeRetElement' in kwargs: + IRE = cleanIncludeRetElements(self.includeRetElements_allowed, kwargs["IncludeRetElement"]) # IRE->IncludeRetElements cleaned version + print(f"{IRE = }") + if len(IRE)>0: + if self.defaultFilterKey not in IRE: # defaultFilterKey is for BaseClass.count() eg: after instantiate, then print obj.count() + IRE.append(self.defaultFilterKey) + self.QBDict[self.classNameRq]["IncludeRetElement"]=IRE + + + # print(self.classNameRq) + # print(self.QBDict) + if self.__class__.__name__==self.className: + self.runCheck() ### running the qbxml connection to get data ### + + +class SalesOrderAdd(baseQBQuery): + def __init__(self, *args, **kwargs): + print(f'{args = }') + print(f'{kwargs = }') + super().__init__(*args, **kwargs) + # self.includeRetElements_allowed = ['TxnID', 'TimeCreated', 'TimeModified', 'EditSequence', 'TxnNumber', 'CustomerRef', 'ClassRef', 'ARAccontRef', 'TemplateRef'] + self.onError = "stopOnError" + self.retName = 'SalesOrderRet' + + self.defaultFilterKey = "TxnID" + self.className = "SalesOrderAdd" + self.classNameRq:str = self.__class__.__name__ + 'Rq' + self.reqSubName = self.className + if 'debug' in kwargs and isinstance(kwargs['debug'], bool): + self.class_debug=kwargs["debug"] + + self.QBDict[self.classNameRq]={} + + + + self.isRequiredFieldSatisfied = False + if 'requestID' in kwargs: + self.QBDict[self.classNameRq]={'@requestID':kwargs.get('requestID', '1')} + self.QBDict[self.classNameRq][self.__class__.__name__]={'@defMacro':'MACROTYPE'} + if 'CustomerRef_ListID' in kwargs: + if 'CustomerRef' in self.QBDict[self.classNameRq][self.__class__.__name__]: + self.QBDict[self.classNameRq][self.__class__.__name__]["CustomerRef"]['ListID']= kwargs['CustomerRef_ListID'] + else: + self.QBDict[self.classNameRq][self.__class__.__name__]["CustomerRef"]={'ListID': kwargs['CustomerRef_ListID']} + self.isRequiredFieldSatisfied=True + if 'CustomerRef_FullName' in kwargs: + self.QBDict[self.classNameRq][self.__class__.__name__]["CustomerRef"]={'FullName': kwargs['CustomerRef_FullName']} + self.isRequiredFieldSatisfied=True + + if not self.isRequiredFieldSatisfied: + raise Exception("Need CustomerRef FullName and/or ListID") + + if 'ClassRef_ListID' in kwargs: + if 'ClassRef' in self.QBDict[self.classNameRq][self.__class__.__name__]: + self.QBDict[self.classNameRq][self.__class__.__name__]["ClassRef"]['ListID']= kwargs['ClassRef_ListID'] + else: + self.QBDict[self.classNameRq][self.__class__.__name__]["ClassRef"]={'ListID': kwargs['ClassRef_ListID']} + if 'ClassRef_FullName' in kwargs: + self.QBDict[self.classNameRq][self.__class__.__name__]["ClassRef"]={'FullName': kwargs['ClassRef_FullName']} + + # if 'ARAccountRef_ListID' in kwargs: + # if 'ARAccountRef' in self.QBDict[self.classNameRq][self.__class__.__name__]: + # self.QBDict[self.classNameRq][self.__class__.__name__]["ARAccountRef"]['ListID']= kwargs['ARAccountRef_ListID'] + # else: + # self.QBDict[self.classNameRq][self.__class__.__name__]["ARAccountRef"]={'ListID': kwargs['ARAccountRef_ListID']} + # if 'ARAccountRef_FullName' in kwargs: + # self.QBDict[self.classNameRq][self.__class__.__name__]["ARAccountRef"]={'FullName': kwargs['ARAccountRef_FullName']} + + if 'TemplateRef_ListID' in kwargs: + if 'TemplateRef' in self.QBDict[self.classNameRq][self.__class__.__name__]: + self.QBDict[self.classNameRq][self.__class__.__name__]["TemplateRef"]['ListID']= kwargs['TemplateRef_ListID'] + else: + self.QBDict[self.classNameRq][self.__class__.__name__]["TemplateRef"]={'ListID': kwargs['TemplateRef_ListID']} + if 'TemplateRef_FullName' in kwargs: + self.QBDict[self.classNameRq][self.__class__.__name__]["TemplateRef"]={'FullName': kwargs['TemplateRef_FullName']} + + if 'TxnDate' in kwargs: + self.QBDict[self.classNameRq][self.__class__.__name__]["TxnDate"]=kwargs['TxnDate'] + if 'RefNumber' in kwargs: + self.QBDict[self.classNameRq][self.__class__.__name__]["RefNumber"]=kwargs['RefNumber'] + if 'BillAddress_Addr1' in kwargs: + self.QBDict[self.classNameRq][self.__class__.__name__]["BillAddress"]={'Addr1': kwargs['BillAddress_Addr1']} + if 'BillAddress_Addr2' in kwargs: + self.QBDict[self.classNameRq][self.__class__.__name__]["BillAddress"]={'Addr2': kwargs['BillAddress_Addr2']} + if 'BillAddress_Addr3' in kwargs: + self.QBDict[self.classNameRq][self.__class__.__name__]["BillAddress"]={'Addr3': kwargs['BillAddress_Addr3']} + if 'BillAddress_Addr4' in kwargs: + self.QBDict[self.classNameRq][self.__class__.__name__]["BillAddress"]={'Addr4': kwargs['BillAddress_Addr4']} + if 'BillAddress_Addr5' in kwargs: + self.QBDict[self.classNameRq][self.__class__.__name__]["BillAddress"]={'Addr5': kwargs['BillAddress_Addr5']} + if 'BillAddress_City' in kwargs: + self.QBDict[self.classNameRq][self.__class__.__name__]["BillAddress"]={'City': kwargs['BillAddress_City']} + if 'BillAddress_State' in kwargs: + self.QBDict[self.classNameRq][self.__class__.__name__]["BillAddress"]={'State': kwargs['BillAddress_State']} + if 'BillAddress_PostalCode' in kwargs: + self.QBDict[self.classNameRq][self.__class__.__name__]["BillAddress"]={'PostalCode': kwargs['BillAddress_PostalCode']} + if 'BillAddress_Country' in kwargs: + self.QBDict[self.classNameRq][self.__class__.__name__]["BillAddress"]={'Country': kwargs['BillAddress_Country']} + if 'BillAddress_Note' in kwargs: + self.QBDict[self.classNameRq][self.__class__.__name__]["BillAddress"]={'Note': kwargs['BillAddress_Note']} + + if 'ShipAddress_Addr1' in kwargs: + self.QBDict[self.classNameRq][self.__class__.__name__]["ShipAddress"]={'Addr1': kwargs['ShipAddress_Addr1']} + if 'ShipAddress_Addr2' in kwargs: + self.QBDict[self.classNameRq][self.__class__.__name__]["ShipAddress"]={'Addr2': kwargs['ShipAddress_Addr2']} + if 'ShipAddress_Addr3' in kwargs: + self.QBDict[self.classNameRq][self.__class__.__name__]["ShipAddress"]={'Addr3': kwargs['ShipAddress_Addr3']} + if 'ShipAddress_Addr4' in kwargs: + self.QBDict[self.classNameRq][self.__class__.__name__]["ShipAddress"]={'Addr4': kwargs['ShipAddress_Addr4']} + if 'ShipAddress_Addr5' in kwargs: + self.QBDict[self.classNameRq][self.__class__.__name__]["ShipAddress"]={'Addr5': kwargs['ShipAddress_Addr5']} + if 'ShipAddress_City' in kwargs: + self.QBDict[self.classNameRq][self.__class__.__name__]["ShipAddress"]={'City': kwargs['ShipAddress_City']} + if 'ShipAddress_State' in kwargs: + self.QBDict[self.classNameRq][self.__class__.__name__]["ShipAddress"]={'State': kwargs['ShipAddress_State']} + if 'ShipAddress_PostalCode' in kwargs: + self.QBDict[self.classNameRq][self.__class__.__name__]["ShipAddress"]={'PostalCode': kwargs['ShipAddress_PostalCode']} + if 'ShipAddress_Country' in kwargs: + self.QBDict[self.classNameRq][self.__class__.__name__]["ShipAddress"]={'Country': kwargs['ShipAddress_Country']} + if 'ShipAddress_Note' in kwargs: + self.QBDict[self.classNameRq][self.__class__.__name__]["ShipAddress"]={'Note': kwargs['ShipAddress_Note']} + + if 'IsPending' in kwargs: + self.QBDict[self.classNameRq][self.__class__.__name__]["IsPending"]=kwargs['IsPending'] + if 'IsFinanceCharge' in kwargs: + self.QBDict[self.classNameRq][self.__class__.__name__]["IsFinanceCharge"]=kwargs['IsFinanceCharge'] + if 'PONumber' in kwargs: + self.QBDict[self.classNameRq][self.__class__.__name__]["PONumber"]=kwargs['PONumber'] + if 'TermsRef_ListID' in kwargs: + if 'TermsRef' in self.QBDict[self.classNameRq][self.__class__.__name__]: + self.QBDict[self.classNameRq][self.__class__.__name__]["TermsRef"]['ListID']= kwargs['TermsRef_ListID'] + else: + self.QBDict[self.classNameRq][self.__class__.__name__]["TermsRef"]={'ListID': kwargs['TermsRef_ListID']} + if 'TermsRef_FullName' in kwargs: + self.QBDict[self.classNameRq][self.__class__.__name__]["TermsRef"]={'FullName': kwargs['TermsRef_FullName']} + + if 'DueDate' in kwargs: + self.QBDict[self.classNameRq][self.__class__.__name__]["DueDate"]=kwargs['DueDate'] + if 'SalesRepRef_ListID' in kwargs: + if 'SalesRepRef' in self.QBDict[self.classNameRq][self.__class__.__name__]: + self.QBDict[self.classNameRq][self.__class__.__name__]["SalesRepRef"]['ListID']= kwargs['SalesRepRef_ListID'] + else: + self.QBDict[self.classNameRq][self.__class__.__name__]["SalesRepRef"]={'ListID': kwargs['SalesRepRef_ListID']} + if 'SalesRepRef_FullName' in kwargs: + self.QBDict[self.classNameRq][self.__class__.__name__]["SalesRepRef"]={'FullName': kwargs['SalesRepRef_FullName']} + + if 'FOB' in kwargs: + self.QBDict[self.classNameRq][self.__class__.__name__]["FOB"]=kwargs['FOB'] + if 'ShipDate' in kwargs: + self.QBDict[self.classNameRq][self.__class__.__name__]["ShipDate"]=kwargs['ShipDate'] + if 'ShipMethodRef_ListID' in kwargs: + if 'ShipMethodRef' in self.QBDict[self.classNameRq][self.__class__.__name__]: + self.QBDict[self.classNameRq][self.__class__.__name__]["ShipMethodRef"]['ListID']= kwargs['ShipMethodRef_ListID'] + else: + self.QBDict[self.classNameRq][self.__class__.__name__]["ShipMethodRef"]={'ListID': kwargs['ShipMethodRef_ListID']} + if 'ShipMethodRef_FullName' in kwargs: + self.QBDict[self.classNameRq][self.__class__.__name__]["ShipMethodRef"]={'FullName': kwargs['ShipMethodRef_FullName']} + + if 'ItemSalesTaxRef_ListID' in kwargs: + if 'ItemSalesTaxRef' in self.QBDict[self.classNameRq][self.__class__.__name__]: + self.QBDict[self.classNameRq][self.__class__.__name__]["ItemSalesTaxRef"]['ListID']= kwargs['ItemSalesTaxRef_ListID'] + else: + self.QBDict[self.classNameRq][self.__class__.__name__]["ItemSalesTaxRef"]={'ListID': kwargs['ItemSalesTaxRef_ListID']} + if 'ItemSalesTaxRef_FullName' in kwargs: + self.QBDict[self.classNameRq][self.__class__.__name__]["ItemSalesTaxRef"]={'FullName': kwargs['ItemSalesTaxRef_FullName']} + + if 'IsManuallyClosed' in kwargs: #only SalesOrderAdd. not in InvoiceAdd + self.QBDict[self.classNameRq][self.__class__.__name__]["IsManuallyClosed"]=kwargs['IsManuallyClosed'] + if 'Memo' in kwargs: + self.QBDict[self.classNameRq][self.__class__.__name__]["Memo"]=kwargs['Memo'] + if 'CustomerMsgRef_ListID' in kwargs: + if 'CustomerMsgRef' in self.QBDict[self.classNameRq][self.__class__.__name__]: + self.QBDict[self.classNameRq][self.__class__.__name__]["CustomerMsgRef"]['ListID']= kwargs['CustomerMsgRef_ListID'] + else: + self.QBDict[self.classNameRq][self.__class__.__name__]["CustomerMsgRef"]={'ListID': kwargs['CustomerMsgRef_ListID']} + if 'CustomerMsgRef_FullName' in kwargs: + self.QBDict[self.classNameRq][self.__class__.__name__]["CustomerMsgRef"]={'FullName': kwargs['CustomerMsgRef_FullName']} + + if 'IsToBePrinted' in kwargs: + self.QBDict[self.classNameRq][self.__class__.__name__]["IsToBePrinted"]=kwargs['IsToBePrinted'] + if 'IsToBeEmailed' in kwargs: + self.QBDict[self.classNameRq][self.__class__.__name__]["IsToBeEmailed"]=kwargs['IsToBeEmailed'] + if 'IsTaxIncluded' in kwargs: + self.QBDict[self.classNameRq][self.__class__.__name__]["IsTaxIncluded"]=kwargs['IsTaxIncluded'] + if 'CustomerSalesTaxCodeRef_ListID' in kwargs: + if 'CustomerSalesTaxCodeRef' in self.QBDict[self.classNameRq][self.__class__.__name__]: + self.QBDict[self.classNameRq][self.__class__.__name__]["CustomerSalesTaxCodeRef"]['ListID']= kwargs['CustomerSalesTaxCodeRef_ListID'] + else: + self.QBDict[self.classNameRq][self.__class__.__name__]["CustomerSalesTaxCodeRef"]={'ListID': kwargs['CustomerSalesTaxCodeRef_ListID']} + if 'CustomerSalesTaxCodeRef_FullName' in kwargs: + self.QBDict[self.classNameRq][self.__class__.__name__]["CustomerSalesTaxCodeRef"]={'FullName': kwargs['CustomerSalesTaxCodeRef_FullName']} + + if 'Other' in kwargs: + self.QBDict[self.classNameRq][self.__class__.__name__]["Other"]=kwargs['Other'] + if 'ExchangeRate' in kwargs: + self.QBDict[self.classNameRq][self.__class__.__name__]["ExchangeRate"]=kwargs['ExchangeRate'] + if 'ExternalGUID' in kwargs: + self.QBDict[self.classNameRq][self.__class__.__name__]["ExternalGUID"]=kwargs['ExternalGUID'] + + # if 'LinkToTxnID' in kwargs: + # self.QBDict[self.classNameRq][self.__class__.__name__]["LinkToTxnID"]=makeAList(kwargs['LinkToTxnID']) + + # if 'SetCredit_CreditTxnID' in kwargs and 'SetCredit_AppliedAmount' in kwargs: + # self.QBDict[self.classNameRq][self.__class__.__name__]["SetCredit"]={'CreditTxnID':{'@useMacro':"MACROTYPE", '#text':kwargs['SetCredit_CreditTxnID']}, 'AppliedAmount': kwargs['SetCredit_AppliedAmount']} + # if 'SetCredit_Override' in kwargs: + # self.QBDict[self.classNameRq][self.__class__.__name__]["SetCredit"]["Override"]=kwargs['SetCredit_Override'] + + #add InvoiceLineAdd kwargs here + + self.SalesOrderLineAdd = [] + if 'SalesOrderLineAdd' in kwargs: + LineAdd = kwargs.get('SalesOrderLineAdd') + self.isLineAddOk = True + if isinstance(LineAdd, dict ): + LineAdd = [LineAdd] + elif isinstance(LineAdd, list): + pass + else: + self.isLineAddOk = False + + if self.isLineAddOk: #check if each instance in the list are all dictionary. + for eachLineAdd in LineAdd: + if not isinstance(eachLineAdd, dict): + self.isLineAddOk = False + break + else: + print("SalesOrderLineAdd Not OK. has to be lisst of dict or a dict") + + + if self.isLineAddOk: #if everything good + for eachLineAdd in LineAdd: + self.LineAddDict = {} + if 'ItemRef_ListID' in eachLineAdd: + if 'ItemRef' in self.QBDict[self.classNameRq][self.__class__.__name__]: + self.LineAddDict["ItemRef"]['ListID']= eachLineAdd['ItemRef_ListID'] + else: + self.LineAddDict["ItemRef"]={'ListID': eachLineAdd['ItemRef_ListID']} + if 'ItemRef_FullName' in eachLineAdd: + self.LineAddDict["ItemRef"]={'FullName': eachLineAdd['ItemRef_FullName']} + if 'Desc' in eachLineAdd: + self.LineAddDict["Desc"]=eachLineAdd['Desc'] + if 'Quantity' in eachLineAdd: + self.LineAddDict["Quantity"]=eachLineAdd['Quantity'] + if 'UnitOfMeasure' in eachLineAdd: + self.LineAddDict["UnitOfMeasure"]=eachLineAdd['UnitOfMeasure'] + if 'Rate' in eachLineAdd: + self.LineAddDict["Rate"]=eachLineAdd['Rate'] + elif 'RatePercent' in eachLineAdd: + self.LineAddDict["RatePercent"]=eachLineAdd['RatePercent'] + elif 'PriceLevelRef_ListID' in eachLineAdd: + if 'PriceLevelRef' in self.QBDict[self.classNameRq][self.__class__.__name__]: + self.LineAddDict["PriceLevelRef"]['ListID']= eachLineAdd['PriceLevelRef_ListID'] + else: + self.LineAddDict["PriceLevelRef"]={'ListID': eachLineAdd['PriceLevelRef_ListID']} + elif 'PriceLevelRef_FullName' in eachLineAdd: + self.LineAddDict["PriceLevelRef"]={'FullName': eachLineAdd['PriceLevelRef_FullName']} + + if 'Amount' in eachLineAdd: + self.LineAddDict["Amount"]=eachLineAdd['Amount'] + # Others + if 'Other1' in eachLineAdd: + self.LineAddDict["Other1"]=eachLineAdd['Other1'] + if len(self.LineAddDict)>0: + self.SalesOrderLineAdd.append(self.LineAddDict) + + #skip the rest, not too important + else: + print("SalesOrderLineAdd has to be list of dict or a dict") + print(f'{self.SalesOrderLineAdd = }') + if len(self.SalesOrderLineAdd)>0: + self.QBDict[self.classNameRq][self.__class__.__name__]['SalesOrderLineAdd']=self.SalesOrderLineAdd + if 'IncludeRetElement' in kwargs: + self.QBDict[self.classNameRq]["IncludeRetElement"]=kwargs['IncludeRetElement'] + + # print(self.classNameRq) + print(f'{self.QBDict = }') + + +class SalesOrderQuery(baseQBQuery): + def __init__(self, *args, **kwargs): + print(f'{args = }') + print(f'{kwargs = }') + super().__init__(*args, **kwargs) + ### Required Variable + self.includeRetElements_allowed = ["TxnID", "TimeCreated", "TimeModified", "EditSequence", "TxnNumber", "CustomerRef", "ClassRef", "TemplateRef", + "TxnDate", "RefNumber", "BillAddress", "BillAddressBlock", "ShipAddress", "ShipAddressBlock", "PONumber,", "TermsRef", "DueDate", + "SalesRepRef", "FOB", "ShipDate", "ShipMethodRef", "SubTotal", "ItemSalesTaxRef", "SalesTaxPercentage", "SalesTaxTotal", "TotalAmount", + "CurrencyRef", "ExchangeRate", "TotalAmountInHomeCurrency", "IsManuallyClosed", "IsFullyInvoiced", "Memo", "CustomerMsgRef", "IsToBePrinted", + "IsToBeEmailed", "IsTaxIncluded", "CustomerSalesTaxCodeRef", "Other", "ExternalGUID", "LinkedTxn", "SalesOrderLineRet", "SalesOrderLineGroupRet" + ] + self.onError = "stopOnError" + self.retName = 'SalesOrderRet' + self.defaultFilterKey = "TxnID" + self.className = 'SalesOrderQuery' #Hardcoded because for check if self.__class__.__name__==self.className + self.classNameRq:str = self.__class__.__name__ + 'Rq' + if 'debug' in kwargs and isinstance(kwargs['debug'], bool): + self.class_debug=kwargs["debug"] + + self.QBDict[self.classNameRq]={} + ### End Required Variable. can put ENUM list below this (eg: self.ENUM_GeneralSummaryReportQuery=[]) + # self.ENUM_DateMacro = ['All', 'Today', 'ThisWeek', 'ThisWeekToDate', 'ThisMonth', 'ThisMonthToDate', 'ThisCalendarQuarter', 'ThisCalendarQuarterToDate', 'ThisFiscalQuarter', + # 'ThisFiscalQuarterToDate', 'ThisCalendarYear', 'ThisCalendarYearToDate', 'ThisFiscalYear', 'ThisFiscalYearToDate', 'Yesterday', 'LastWeek', 'LastWeekToDate', 'LastMonth', + # 'LastMonthToDate', 'LastCalendarQuarter', 'LastCalendarQuarterToDate', 'LastFiscalQuarter', 'LastFiscalQuarterToDate', 'LastCalendarYear', 'LastCalendarYearToDate', + # 'LastFiscalYear', 'LastFiscalYearToDate', 'NextWeek', 'NextFourWeeks', 'NextMonth', 'NextCalendarQuarter', 'NextCalendarYear', 'NextFiscalQuarter', 'NextFiscalYear' + # ] + self.ENUM_MatchCriterion = ['StartsWith', 'Contains', 'EndsWith'] + + if 'TxnID' in kwargs and kwargs['TxnID'] and len(kwargs['TxnID'])>0: + self.QBDict[self.classNameRq]["TxnID"]=makeAList(kwargs['TxnID']) + elif 'RefNumber' in kwargs and kwargs['RefNumber']: + self.QBDict[self.classNameRq]["RefNumber"]=makeAList(kwargs['RefNumber']) + elif 'RefNumberCaseSensitive' in kwargs and kwargs['RefNumberCaseSensitive']: + self.QBDict[self.classNameRq]["RefNumberCaseSensitive"]=makeAList(kwargs['RefNumberCaseSensitive']) + else: + if 'MaxReturned' in kwargs: + self.QBDict[self.classNameRq]["MaxReturned"]=kwargs['MaxReturned'] + if 'ModifiedDateRangeFilter_FromModifiedDate' in kwargs or 'ModifiedDateRangeFilter_ToModifiedDate' in kwargs: #if or then use {'key1':kwargs.get('key1',""), 'key2':kwargs.get('key2', "")} + self.QBDict[self.classNameRq]["ModifiedDateRangeFilter"]={'FromModifiedDate':kwargs.get('ModifiedDateRangeFilter_FromModifiedDate', ""), 'ToModifiedDate':kwargs.get('ModifiedDateRangeFilter_ToModifiedDate', "")} + elif 'TxnDateRangeFilter_FromTxnDate' in kwargs or 'TxnDateRangeFilter_ToTxnDate' in kwargs: #if or then use {'key1':kwargs.get('key1',""), 'key2':kwargs.get('key2', "")} + self.QBDict[self.classNameRq]["TxnDateRangeFilter"]={'FromTxnDate':kwargs.get('TxnDateRangeFilter_FromTxnDate', ""), 'ToTxnDate':kwargs.get('TxnDateRangeFilter_ToTxnDate', "")} + elif 'TxnDateRangeFilter_DateMacro' in kwargs: + DM=cleanIncludeRetElements(ENUM_DateMacro, kwargs['TxnDateRangeFilter_DateMacro']) + if len(DM)>0: + self.QBDict[self.classNameRq]["TxnDateRangeFilter"]={'DateMacro':DM} + + if 'EntityFilter_ListID' in kwargs: + self.QBDict[self.classNameRq]["EntityFilter"]={'ListID':makeAList(kwargs['EntityFilter_ListID'])} + elif 'EntityFilter_FullName' in kwargs: + self.QBDict[self.classNameRq]["EntityFilter"]={'FullName':makeAList(kwargs['EntityFilter_FullName'])} + elif 'EntityFilter_ListIDWithChildren' in kwargs: + self.QBDict[self.classNameRq]["EntityFilter"]={'ListIDWithChildren':kwargs['EntityFilter_ListIDWithChildren']} + elif 'EntityFilter_FullNameWithChildren' in kwargs: + self.QBDict[self.classNameRq]["EntityFilter"]={'FullNameWithChildren':kwargs['EntityFilter_FullNameWithChildren']} + + if 'RefNumberFilter_MatchCriterion' in kwargs and 'RefNumberFilter_RefNumber' in kwargs: + RNFMC = cleanIncludeRetElements(self.ENUM_MatchCriterion, kwargs['RefNumberFilter_MatchCriterion']) + if len(RNFMC)>0: + self.QBDict[self.classNameRq]["RefNumberFilter"]={'MatchCriterion':RNFMC, 'RefNumber':kwargs['RefNumberFilter_RefNumber']} + elif 'RefNumberRangeFilter_FromRefNumber' in kwargs or 'RefNumberRangeFilter_ToRefNumber' in kwargs: #if or then use {'key1':kwargs.get('key1',""), 'key2':kwargs.get('key2', "")} + self.QBDict[self.classNameRq]["RefNumberRangeFilter"]={'FromRefNumber':kwargs.get('RefNumberRangeFilter_FromRefNumber', ""), 'ToRefNumber':kwargs.get('RefNumberRangeFilter_ToRefNumber', "")} + + if 'CurrencyFilter_ListID' in kwargs: + self.QBDict[self.classNameRq]["CurrencyFilter"]=makeAList(kwargs['CurrencyFilter_ListID']) + elif 'CurrencyFilter_FullName' in kwargs: + self.QBDict[self.classNameRq]["CurrencyFilter"]=makeAList(kwargs['CurrencyFilter_FullName']) + + if 'IncludeLineItems' in kwargs: + self.QBDict[self.classNameRq]["IncludeLineItems"]=kwargs['IncludeLineItems'] + if 'IncludeLinkedTxns' in kwargs: + self.QBDict[self.classNameRq]["IncludeLinkedTxns"]=kwargs['IncludeLinkedTxns'] + if 'IncludeRetElement' in kwargs: + IRE = cleanIncludeRetElements(self.includeRetElements_allowed, kwargs["IncludeRetElement"]) # IRE->IncludeRetElements cleaned version + print(f"{IRE = }") + if len(IRE)>0: + if self.defaultFilterKey not in IRE: # defaultFilterKey is for BaseClass.count() eg: after instantiate, then print obj.count() + IRE.append(self.defaultFilterKey) + self.QBDict[self.classNameRq]["IncludeRetElement"]=IRE + + if 'OwnerID' in kwargs: # usually value=0 to get to DataExtRet (additional data) + self.QBDict[self.classNameRq]["OwnerID"]=kwargs['OwnerID'] + + # print(self.classNameRq) + print(self.QBDict) + if self.__class__.__name__==self.className: + self.runCheck() ### running the qbxml connection to get data ### + + +class InvoiceQuery(baseQBQuery): + def __init__(self, *args, **kwargs): + + print(f'{args = }') + print(f'{kwargs = }') + super().__init__(*args, **kwargs) + ### Required Variable + self.includeRetElements_allowed = ["TxnID", "TimeCreated", "TimeModified", "EditSequence", "TxnNumber", "CustomerRef", "ClassRef", "ARAccountRef", "TemplateRef", + "TxnDate", "RefNumber", "BillAddress", "BillAddressBlock", "ShipAddress", "ShipAddressBlock", "IsPending", "IsFinanceCharge", "PONumber,", "TermsRef", "DueDate", + "SalesRepRef", "FOB", "ShipDate", "ShipMethodRef", "SubTotal", "ItemSalesTaxRef", "SalesTaxPercentage", "SalesTaxTotal", "AppliedAmount", "BalanceRemaining", + "CurrencyRef", "ExchangeRate", "BalanceRemainingInHomeCurrency", "Memo", "IsPaid", "CustomerMsgRef", "IsToBePrinted", + "IsToBeEmailed", "IsTaxIncluded", "CustomerSalesTaxCodeRef", "SuggestedDiscountAmount", "SuggestedDiscountDate", "Other", "ExternalGUID", + "LinkedTxn", "InvoiceLineRet", "InvoiceLineGroupRet", + ] + self.onError = "stopOnError" + self.retName = 'InvoiceRet' + self.defaultFilterKey = "TxnID" + self.className = 'InvoiceQuery' + self.classNameRq:str = self.__class__.__name__ + 'Rq' + if 'debug' in kwargs and isinstance(kwargs['debug'], bool): + self.class_debug=kwargs["debug"] + + self.QBDict[self.classNameRq]={} + ### End Required Variable. can put ENUM list below this (eg: self.ENUM_GeneralSummaryReportQuery=[]) + # self.ENUM_DateMacro = ['All', 'Today', 'ThisWeek', 'ThisWeekToDate', 'ThisMonth', 'ThisMonthToDate', 'ThisCalendarQuarter', 'ThisCalendarQuarterToDate', 'ThisFiscalQuarter', + # 'ThisFiscalQuarterToDate', 'ThisCalendarYear', 'ThisCalendarYearToDate', 'ThisFiscalYear', 'ThisFiscalYearToDate', 'Yesterday', 'LastWeek', 'LastWeekToDate', 'LastMonth', + # 'LastMonthToDate', 'LastCalendarQuarter', 'LastCalendarQuarterToDate', 'LastFiscalQuarter', 'LastFiscalQuarterToDate', 'LastCalendarYear', 'LastCalendarYearToDate', + # 'LastFiscalYear', 'LastFiscalYearToDate', 'NextWeek', 'NextFourWeeks', 'NextMonth', 'NextCalendarQuarter', 'NextCalendarYear', 'NextFiscalQuarter', 'NextFiscalYear' + # ] ## disabled, and move to outside class, on top of page + self.ENUM_MatchCriterion = ['StartsWith', 'Contains', 'EndsWith'] + + if 'TxnID' in kwargs and kwargs['TxnID'] and len(kwargs['TxnID'])>0: + _txnIDs=makeAList(kwargs['TxnID']) + self.QBDict[self.classNameRq]["TxnID"]= makeAList(kwargs['TxnID']) + elif 'RefNumber' in kwargs and kwargs['RefNumber']: + self.QBDict[self.classNameRq]['RefNumber']=makeAList(kwargs['RefNumber']) + elif 'RefNumberCaseSensitive' in kwargs and kwargs['RefNumberCaseSensitive']: + self.QBDict[self.classNameRq]["RefNumberCaseSensitive"]=makeAList(kwargs['RefNumberCaseSensitive']) + else: + if 'MaxReturned' in kwargs: + self.QBDict[self.classNameRq]["MaxReturned"]=kwargs['MaxReturned'] + if 'ModifiedDateRangeFilter_FromModifiedDate' in kwargs or 'ModifiedDateRangeFilter_ToModifiedDate' in kwargs: #if or then use {'key1':kwargs.get('key1',""), 'key2':kwargs.get('key2', "")} + self.QBDict[self.classNameRq]["ModifiedDateRangeFilter"]={'FromModifiedDate':kwargs.get('ModifiedDateRangeFilter_FromModifiedDate', ""), 'ToModifiedDate':kwargs.get('ModifiedDateRangeFilter_ToModifiedDate', "")} + elif 'TxnDateRangeFilter_FromTxnDate' in kwargs or 'TxnDateRangeFilter_ToTxnDate' in kwargs: #if or then use {'key1':kwargs.get('key1',""), 'key2':kwargs.get('key2', "")} + self.QBDict[self.classNameRq]["TxnDateRangeFilter"]={'FromTxnDate':kwargs.get('TxnDateRangeFilter_FromTxnDate', ""), 'ToTxnDate':kwargs.get('TxnDateRangeFilter_ToTxnDate', "")} + elif 'TxnDateRangeFilter_DateMacro' in kwargs: + DM=cleanIncludeRetElements(ENUM_DateMacro, kwargs['TxnDateRangeFilter_DateMacro']) + if len(DM)>0: + self.QBDict[self.classNameRq]["TxnDateRangeFilter"]={'DateMacro':DM} + + if 'EntityFilter_ListID' in kwargs: + # self.QBDict[self.classNameRq]["EntityFilter"]={'ListID':kwargs['EntityFilter_ListID']} + self.QBDict[self.classNameRq]['EntityFilter']={'ListID':makeAList(kwargs['EntityFilter_ListID'])} + elif 'EntityFilter_FullName' in kwargs: + # self.QBDict[self.classNameRq]["EntityFilter"]={'FullName':kwargs['EntityFilter_FullName']} + self.QBDict[self.classNameRq]['EntityFilter']={'FullName':makeAList(kwargs['EntityFilter_FullName'])} + elif 'EntityFilter_ListIDWithChildren' in kwargs: + self.QBDict[self.classNameRq]["EntityFilter"]={'ListIDWithChildren':kwargs['EntityFilter_ListIDWithChildren']} + elif 'EntityFilter_FullNameWithChildren' in kwargs: + self.QBDict[self.classNameRq]["EntityFilter"]={'FullNameWithChildren':kwargs['EntityFilter_FullNameWithChildren']} + + if 'AccountFilter_ListID' in kwargs: + self.QBDict[self.classNameRq]["AccountFilter"]={'ListID':makeAList(kwargs['AccountFilter_ListID'])} + elif 'AccountFilter_FullName' in kwargs: + self.QBDict[self.classNameRq]["AccountFilter"]={'FullName':makeAList(kwargs['AccountFilter_FullName'])} + elif 'AccountFilter_ListIDWithChildren' in kwargs: + self.QBDict[self.classNameRq]["AccountFilter"]={'ListIDWithChildren':kwargs['AccountFilter_ListIDWithChildren']} + elif 'AccountFilter_FullNameWithChildren' in kwargs: + self.QBDict[self.classNameRq]["AccountFilter"]={'FullNameWithChildren':kwargs['AccountFilter_FullNameWithChildren']} + + if 'RefNumberFilter_MatchCriterion' in kwargs and 'RefNumberFilter_RefNumber' in kwargs: + RNFMC = cleanIncludeRetElements(self.ENUM_MatchCriterion, kwargs['RefNumberFilter_MatchCriterion']) + if len(RNFMC)>0: + self.QBDict[self.classNameRq]["RefNumberFilter"]={'MatchCriterion':RNFMC, 'RefNumber':kwargs['RefNumberFilter_RefNumber']} + elif 'RefNumberRangeFilter_FromRefNumber' in kwargs or 'RefNumberRangeFilter_ToRefNumber' in kwargs: #if or then use {'key1':kwargs.get('key1',""), 'key2':kwargs.get('key2', "")} + ##### CAREFULL with the get() coz "" will get anything + self.QBDict[self.classNameRq]["RefNumberRangeFilter"]={'FromRefNumber':kwargs.get('RefNumberRangeFilter_FromRefNumber', ""), 'ToRefNumber':kwargs.get('RefNumberRangeFilter_ToRefNumber', "")} + + if 'CurrencyFilter_ListID' in kwargs: + self.QBDict[self.classNameRq]["CurrencyFilter"]=makeAList(kwargs['CurrencyFilter_ListID']) + elif 'CurrencyFilter_FullName' in kwargs: + self.QBDict[self.classNameRq]["CurrencyFilter"]=makeAList(kwargs['CurrencyFilter_FullName']) + + if 'IncludeLineItems' in kwargs: + print(f'{self.QBDict} = ') + self.QBDict[self.classNameRq]["IncludeLineItems"]=kwargs['IncludeLineItems'] + if 'IncludeLinkedTxns' in kwargs: + self.QBDict[self.classNameRq]["IncludeLinkedTxns"]=kwargs['IncludeLinkedTxns'] + if 'IncludeRetElement' in kwargs: + IRE = cleanIncludeRetElements(self.includeRetElements_allowed, kwargs["IncludeRetElement"]) # IRE->IncludeRetElements cleaned version + print(f"{IRE = }") + if len(IRE)>0: + if self.defaultFilterKey not in IRE: # defaultFilterKey is for BaseClass.count() eg: after instantiate, then print obj.count() + IRE.append(self.defaultFilterKey) + self.QBDict[self.classNameRq]["IncludeRetElement"]=IRE + + if 'OwnerID' in kwargs: # usually value=0 to get to DataExtRet (additional data) + self.QBDict[self.classNameRq]["OwnerID"]=kwargs['OwnerID'] + # print(self.classNameRq) + # print(self.QBDict) + if self.__class__.__name__==self.className: + self.runCheck() ### running the qbxml connection to get data ### + + + +if __name__ == "__main__": + @timing + def invoicequery(): + g=InvoiceQuery(debug=False, MaxReturned = 2, IncludeLineItems="true", IncludeLinkedTxns="true", + RefNumber="24010022")#"2023-08-22") + print(json.dumps(g.filter().all(), indent=3)) + print(g.count()) + + @timing + def salesorderquery(): + g=SalesOrderQuery(debug=False, MaxReturned = 1, TxnID=None, IncludeLineItems="true", IncludeLinkedTxns="true", + )#RefNumber="24010022" )#"B23070857") + print(json.dumps(g.filter().all(), indent=3)) + print(g.count()) + + @timing + def transactionquery(): + g=TransactionQuery(debug=False, MaxReturned=None, TransactionTypeFilter_TxnTypeFilter="Invoice", TransactionEntityFilter_FullName="Abadi Serpong", + TransactionDetailLevelFilter="All", TransactionPaidStatusFilter="Open", TransactionDateRangeFilter_DateMacro="ThisFiscalYear") + # print(g.response_string) + # pprint.pprint({"temp":g.filter(['TxnType', 'TxnID', 'Entityref', 'accountref', 'txndate', 'refnumber'])}) + # print(json.dumps(g.filter(['TxnType', 'TxnID', 'Entityref', 'accountref', 'refnumber','txndate',"amount" ]).all(), indent=3)) + + print(json.dumps(g.filter().all(), indent=3)) + + print(g.count()) + + @timing + def main(): + g= GeneralSummaryReportQuery(debug=False, GeneralSummaryReportType="ProfitAndLossStandard", ReportDateMacro="ThisYear") + print(g, type(g)) + print(type(g.all())) + print(g.all()) + print(g.response_string) + pprint.pprint(g.filter("reportdata").all()) + print(g.count(), g.all()) + + @timing + def iteminventoryquery(): + # g=ItemInventoryQuery() + g=ItemInventoryQuery(debug = False, MaxReturned = 5, IncludeRetElements = "abc")# IncludeRetElement=["FullName", "DataExtRet", "Name", "QuantityOnHand", "QuantityOnSalesOrder", "QuantityOnOrder", "ManufacturerPartNumber"]) #put OwnerID=0 to get DataExtRet + # print("before g.all") + # print(f'{g.all() = }') + # print("after g.all") + # print("") + # pprint.pprint(g.filter(["FullName", "Name", "sublevel"]).all()) + # print(f'{g.filter("fullname") = }') + + # print(f'{g = }') + # pprint.pprint(g.response_string,indent=4 ) + print(g.response_string, type(g.response_string)) + print("before") + # print(g.filter([ "Name", "QuantityOnHand", "QUantityOnSalesOrder", "QuantityonOrder"]).first()) + c = g.filter([ "Name", "QuantityOnHand", "QUantityOnSalesOrder", "QuantityonOrder"]).first() + print("c:",type(c), c) + # pprint.pprint(g.returnRet()) + # print(g.filter("name").firstValue()) + print(g.count()) + print(g.statusOk) + print(g.filter(["name", "manufacturerpartnumber", "quantityonhand"]).getValuesOf(["Name","quantityonhand", "manufacturerpartnumber"])) + + + # print(g.filter(["DataExtRet"]).all()) + + @timing + def customerquery(): + g= CustomerQuery(MaxReturned=3, IncludeRetElement=["fullname", "name", "CompanyName", "BillAddressBlock", "ShipAddressBlock"]) + # g= CustomerQuery(MaxReturned=20, ActiveStatus="ActiveOnly", MatchCriterion="StartsWith", Name="to", IncludeRetElement=["fullname", "name", "billaddressblock", "currencyfilter"]) + # print(g.IncludeRetElements_allowed) + print("init finish") + # print(f'{type(g.all()) = }') + print("before g.all") + print(f'{g.all() = }') + print("after g.all") + pprint.pprint(f'{g.filter(["FullName", "Name"]).all() = }') + print(f'{g.filter() = }') + # pprint.pprint(g.filter(["FullName", "abc", "BillAddressBlock"]).all()) + print(f'{g.filter(["FullName", "abc", "BillAddressBlock"]).all() = }') + print("") + print(f'{g.filter([ "Addr1", "Addr2", "Addr3", "Addr4", "Addr5"]).all() = }') + print(f'{g.filter([ "Addr1", "Addr2", "Addr3", "Addr4", "Addr5"]) = }') + print(f'{g.filter(["fullname", "name"]).lastValue() = }') + + def readxmltodict(): + import xmltodict + filename="ItemInventoryQuery.xml" + filename="InvoiceAdd.xml" + with open(filename, "r") as f: + xml = f.read() + print(xml) + print("") + print("") + with open(filename, "r") as f: + xmllines = f.readlines() + print(xmllines) + varDict = xmltodict.parse(xml) + # print(f"{varDict = }") + print() + f = open(filename) + enumDict ={} + def recursiveDict(varDict, f, enumDict): + # print(varDict) + for dKey in varDict: + print(dKey) + if not isinstance(varDict[dKey], (list, dict)): + if dKey[0]=='@' or dKey[0]=="#": + continue + _ = f.readline() + dKey = dKey.replace("#", "") + print(dKey) + # print(f'{dKey = }, {varDict[dKey]}, {_}') + _strOptional = "" + while not dKey in _: + _ = f.readline() + # print(f'{dKey = }, {varDict[dKey]}, {_}') + if "values:" in _: + enumDict[dKey]=_.split(":")[-1].replace("[DEFAULT]","").replace("-->","").replace(" ","").replace("\n","").split(",") + _ = f.readline() + _strOptional = _.split("--")[1].strip() + varDict[dKey]+=";"+_strOptional + print(f'{varDict[dKey] = }') + elif isinstance(varDict[dKey], dict): + varDict[dKey], enumDict=recursiveDict(varDict[dKey], f, enumDict)##### istirahat dulu ah + return varDict, enumDict + print(f'{varDict = }') + print() + print(recursiveDict(varDict, f, enumDict)) + f.close + + # invoicequery() + salesorderquery() + # transactionquery() + # main() + # iteminventoryquery() + # customerquery() + # readxmltodict() + # g=SalesOrderQuery(MaxReturned=1) + # print(len(None)) diff --git a/QBClass/__init__.py b/QBClass/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/QBClass/server.py b/QBClass/server.py new file mode 100644 index 0000000..7acb771 --- /dev/null +++ b/QBClass/server.py @@ -0,0 +1,511 @@ +import pprint +import xmltodict +import win32com.client +# import xml.etree.ElementTree as ET +import xml.dom.minidom as minidom +from time import time +from .utils import cleanIncludeRetElements +import json + +def timing(f): + # @wraps(f) + def wrap(*args, **kw): + ts = time() + result = f(*args, **kw) + te = time() + print('func:%r args:[%r, %r] took: %2.6f sec' % \ + (f.__name__, args, kw, te-ts)) + return result + return wrap + + +class baseQBQuery: + def __init__(self, *args, **kwargs, ) -> None: + # print(f'{kwargs = }') + # print(f'{args = }') + self.QBXML = None + self.QBDict = {} + self.response_string = None + self.Rs = None + self.varDict = {} + ### start ### variable to be replace with other class init value + self.onError = "continueOnError" + # self.cleanIncludeRetElements = None + self.includeRetElements_allowed = None + self.retName = None + self.defaultFilterKey = None + self.class_debug = False + ### end ### variable to be replace with other class init value + self.listOfDict = self.ListOfDict(None, self.varDict, self.retName, False) + self.requestID = None + self.statusCode = -1 + self.statusMessage = "" + self.statusSeverity = "" + self.statusOk = False + if self.__class__.__name__=="baseQBQuery": + print("baseqbquey same with classname") + else: + print("accessed from child class") + print("basequery is accessed from ", self.__class__.__name__ + "Rq") + + # @timing + def create_QBXML(self): + version = "13.0" + dataDict = { ### Header for qmxml with version attribute + "?qbxml": { + "@version": version, + } + } + # dataDict["?qbxml"]["QBXML"] = {"QBXMLMsgsRq": { ### Simple Example ### + # "@onError": "continueOnError", + # "GeneralSummaryReportQueryRq": { + # "GeneralSummaryReportType": self.GeneralSummaryReportType, + # } + # } + # } + + # dataDict["?qbxml"]["QBXML"] = {"QBXMLMsgsRq": { ### Example with multiple FullName Item ### + # "@onError": "continueOnError", + # "ItemInventoryQueryRq": { + # "FullName": ["TACO:AA:TH-003AA", + # "TACO:AA:TH-010AA"] + # }, + # } + # } + firstKey = str(list(self.QBDict.keys())[0]) + dataDict["?qbxml"]["QBXML"] = {"QBXMLMsgsRq": { ### Example with multiple FullName Item ### + "@onError": self.onError, + firstKey: self.QBDict[firstKey]}} + # print(f'{dataDict = }') +# # QBXML = '' + xmltodict.unparse(dataDict, pretty=True) + self.QBXML = xmltodict.unparse(dataDict, pretty=True).replace("", "").replace(f'version="{version}"', f'version="{version}"?') + print(self.QBXML, type(self.QBXML)) + return self.QBXML + + # @timing + def connect_to_quickbooks(self, qbxml_query=None): + # Connect to Quickbooks + 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 + self.response_string = sessionManager.ProcessRequest(ticket, self.QBXML) + + # Disconnect from Quickbooks + sessionManager.EndSession(ticket) # Close the company file + sessionManager.CloseConnection() # Close the connection + + # Beautify response_string + # print(f'{self.response_string = }') + xml = minidom.parseString(self.response_string.replace("\n", "")) + self.response_string = xml.toprettyxml() + # print(f'{self.response_string = }') + + self.statusOk = self.isDataOK() + return self.statusOk + return self.response_string + + def isDataOK(self): + # print("isdataok") + # QBXML = ET.fromstring(self.response_string) + # print(xmltodict.parse(self.response_string)) + self.varDict = xmltodict.parse(self.response_string) + if self.class_debug: + pprint.pprint("isDataOK", self.varDict) + self.listOfDict.varDict = self.varDict + self.listOfDict.filterKey = ["@requestID"] + self.requestID = self.listOfDict.firstValue().get('@requestID',None) + self.listOfDict.filterKey = ["@statusCode"] + self.statusCode = self.listOfDict.firstValue().get('@statusCode',None) + self.listOfDict.filterKey = ["@statusMessage"] + self.statusMessage = self.listOfDict.firstValue().get('@statusMessage',None) + self.listOfDict.filterKey = ["@statusSeverity"] + self.statusSeverity = self.listOfDict.firstValue().get('@statusSeverity') + self.listOfDict.filterKey = [self.retName] + if self.class_debug: + print(f'isDataOK -> {self.listOfDict.firstValue() = }') + if self.listOfDict.firstValue().get(self.retName,None)==None: + return False + + print(f'{self.statusCode = }, {self.statusMessage = }, {self.statusSeverity = }') + varDict = self.varDict['QBXML']['QBXMLMsgsRs'][self.__class__.__name__+"Rs"] + return True + # isStatusOK=None + + # for _ in self.findKeyInDict("FullName", ): ###berhasil + # print(f'{_ = }') + # for _ in self.gen_dict_extract("@statusMessage", self.varDict): + # print(_) + # if 'Status OK'.lower()==_.lower(): + # print(_) + # isStatusOK = True + # break + # else: + # isStatusOK=False + + # if self.ListOfDict.find_firstListOfDict("@statusMessage")['@statusMessage'].lower()=="status OK".lower(): + # # print(f'{self.retName = }') + # self.Rs = self.find_firstListOfDict(self.retName)[self.retName] + # # self.Rs=self.returnRet(self.varDict) + # # # print(self.findKeyInDict("FullName", )) ###test + # # print(self.findKeyInDict("FullName", self.findKeyInDict("QBXMLMsgsRs1", ))) ###test + # # # print(self.findKeyInDict("@statusMessage", )) ###test + # # for _ in self.findKeyInDict("QBXMLMsgsRs",): ###trial blm berhasil + # # print(f'2{_ = }') + # # print(f'{self.Rs = }') + # # print(type(self.Rs)) + # # print(self.find_firstListOfDict("FullName")['FullName']) + # # print(self.find_firstListOfDict("FullName")) + # # print(self.find_allListOfDict("FullName")) + + def returnRet(self, varDict:dict = None): + if varDict== None: + varDict=self.varDict + # pprint.pprint(self.varDict) + + # print(f'{varDict = }') + varDict = varDict['QBXML']['QBXMLMsgsRs'][self.__class__.__name__+"Rs"] + # print(f'{varDict = }') + + for idx, key in enumerate(varDict): + # print(idx, key, len(varDict)) + if self.retName in key: + return varDict[key] + return None + + def runCheck(self): + # print("runCheck") + if self.varDict: + return True + if self.response_string: + return True + if self.Rs: + return True + if self.QBDict: + self.create_QBXML() + if self.connect_to_quickbooks(): + return True + return False + + def __repr__(self) -> str: + self.all() + # print(f'{self.returnRet() = }') + return self.response_string + + def count(self) -> int: + # objs = self.filter(self.defaultFilterKey).all() + # print(f"{objs = }", type(objs)) + return len(self.filter(self.defaultFilterKey).all()) + + + def filter(self, key=None): + print(f'{key = }') + # print(f'{self.statusOk = }') + if not self.runCheck(): + print("not runcheck") + return self.ListOfDict(["abc"], self.varDict, self.retName, self.includeRetElements_allowed, self.statusOk) + return [] + if isinstance(key, str): + key = [key] + elif isinstance(key, list): + pass + elif isinstance(key, dict): + key = [x for x,y in key.items()] + elif key is None: + # print(f"key is none. {self.retName = }") + return self.ListOfDict(self.retName, self.varDict, self.retName, self.includeRetElements_allowed, self.statusOk)#.firstValue()#[self.retName] + else: + return [] + key = cleanIncludeRetElements(self.includeRetElements_allowed, key) + # print(f'f {key = }') + if key: + return self.ListOfDict(key, self.varDict, self.retName, self.includeRetElements_allowed, self.statusOk) + else: + return self.ListOfDict(["abc"], self.varDict, self.retName, self.includeRetElements_allowed, self.statusOk) + ### dont use this way, better returning class because the value if you assign to variable, the valu will be the last filterKey inputed + ### if return class, every filterKey is an object, different from other filterKey + self.listOfDict.varDict = self.varDict + self.listOfDict.filterKey = key + return self.listOfDict + ### + + def all(self) -> dict: + if not self.runCheck(): + return None + # return self.ListOfDict(None, self.varDict, self.retName).firstValue() + temp = self.ListOfDict(None, self.varDict, self.retName, self.includeRetElements_allowed, self.statusOk).firstValue()[self.retName] + if self.requestID: + temp['requestID']=self.requestID + # print(f'{temp = }') + return temp + # return self.ListOfDict(None, self.varDict, self.retName, self.includeRetElements_allowed, self.statusOk).firstValue()[self.retName] + ### dont use this way + self.listOfDict.varDict = self.varDict + self.listOfDict.filterKey = self.retName + return self.listOfDict + ### + + def to_json(self) -> str: + return json.dumps(self.all()) + + + class ListOfDict: + def __init__(self, key, var, retName, includeRetElements_allowed:list ,statusOk:bool = True) -> None: + # print(f'{key =}, {var =}') + # self.first = self.find_firstListOfDict(key) + if key: + if isinstance(key, str): + self.filterKey = [key] + else: + self.filterKey = key + else: + self.filterKey = [retName] + # print(f"{self.filterKey = }") + self.varDict = var + self.statusOk = statusOk + self._includeRetElements_allowed = includeRetElements_allowed + # print("listofDict") + + + def __repr__(self) -> str: + return str(self.all()) + + # def filter(self, filterKey): + # self.filterKey=filterKey + + + def getValuesOf(self, key:str=None, var:dict=None, dataRetList:list=None) : + if key==None: + key = self.filterKey + elif isinstance(key, str): + key=[key] + elif isinstance(key, list): + pass + else: + raise TypeError(f'{key=} should be string not {type(key)}') + print(key) + key = cleanIncludeRetElements(self._includeRetElements_allowed, key) + print(key) + if len(key)==0: + key = self.filterKey + else: + key = key + # print(f'getvaluesof {key = }') + # for xdct in self.findKeyInDict(var, dataRetList): + # print(f'{xdct = }', type(xdct), self.filterKey[0], key) + lstresult = [] + for x in self.findKeyInDict(var, dataRetList): + templstresult = [] + for y in key: + templstresult.append(x.get(y, "")) + lstresult.append(templstresult) + print(f'{lstresult[-1] =}') + return lstresult + _lst = [x[key] for x in self.findKeyInDict(var, dataRetList)] + # print(_dct) + return _lst + + def all(self, var:dict=None, dataRetList:list=None) -> list: + # print(f'{self.statusOk = }') + if not self.statusOk: + return [] + _lst = [x for x in self.findKeyInDict(var, dataRetList)] + # _lst = [x[self.filterKey] for x in self.findKeyInDict(var, dataRetList)] + # if _lst: + return _lst + # else: + # return [] + + def allOnlyValue(self, var:dict=None, dataRetList:list=None): + if not self.statusOk: + return [] + _lst = [x for x in self.findKeyInDict(var, dataRetList)] + return _lst + + def first(self, var:dict=None, dataRetList:list=None) -> dict: + if not self.statusOk: + return {} + return next(self.findKeyInDict( var, dataRetList), {}) + + def firstValue(self, var:dict=None, dataRetList:list=None) ->dict: + if not self.statusOk: + print("firstValue statusOk is False") + return {} + # return self.first(var, dataRetList)[self.filterKey] + _val=self.first(var, dataRetList) + # print(f'{_val = }') + if _val: + # return _val[self.filterKey] + return _val + else: + return {} + + def last(self, var:dict=None, dataRetList:list=None) -> dict: + if not self.statusOk: + return {} + # *_, last = self.findKeyInDict( var, dataRetList) + _val= self.all(var, dataRetList) + if _val:return _val[-1] + else: return {} + + def lastValue(self, var:dict=None, dataRetList:list=None) -> dict: + if not self.statusOk: + return {} + _val=self.last(var, dataRetList) + # print(f"lastValue {_val =}") + if _val: + # return _val[self.filterKey] + return _val + else: + return {} + + def count(self, var:dict=None, dataRetList:list=None) -> int: + if not self.statusOk: + return 0 + # print(len(self.all())) + return len(self.all()) + + # def findKeyInDict(self, var:dict=None, dataRetList:list=None, ): + # # print("genfinekeys") + # if var==None: + # var=self.varDict + # # print(f"{var = }") + # if dataRetList is None: + # dataRetList = [] + # if isinstance(var, list): + # # print("list var") + # for _ in var: + # yield from self.findKeyInDict( _, ) + # elif isinstance(var, dict): + # # print("dict var") + # if self.filterKey in var: + # dataRetList.append({self.filterKey: var[self.filterKey]}) + # print(f"{dataRetList = }") + # yield {self.filterKey: var[self.filterKey]} + # else: + # # print(f'dict else var={var}') + # for _ in var: + # # print(_) + # yield from self.findKeyInDict(var[_], ) + # return dataRetList + + def findKeyInDict(self, var:dict=None, dataRetList:list=None, ): + # print("genfinekeys") + if var==None: + var=self.varDict + # print(f"{var = }") + if dataRetList is None: + dataRetList = [] + if isinstance(var, list): + # print("list var") + for _ in var: + yield from self.findKeyInDict( _, ) + elif isinstance(var, dict): + # print("dict var") + found = False + tempDct = {} + for fKey in self.filterKey: + # if self.filterKey in var: + if fKey in var: + found = True + tempDct[fKey]=var[fKey] + # print(f'{tempDct = }') + if found: + # dataRetList.append({self.filterKey: var[self.filterKey]}) + dataRetList.append(tempDct) + # print(f"{dataRetList = }") + yield tempDct #{self.filterKey: var[self.filterKey]} + else: + # print(f'dict else var={var}') + for _ in var: + # print(_) + yield from self.findKeyInDict(var[_], ) + return dataRetList + + + # def find_allListOfDict(self, key, var:dict=None, dataRetList:list=None): + # return [x for x in self.findKeyInDict(key, var, dataRetList)] + + # def find_firstListOfDictValue(self, key, var:dict=None, dataRetList:list=None): + # return self.find_firstListOfDict(key, var, dataRetList)[key] + + # def find_firstListOfDict(self, key, var:dict=None, dataRetList:list=None): + # return next(self.findKeyInDict(key, var, dataRetList), None) + + # def findKeyInDict(self, key, var:dict=None, dataRetList:list=None, ): + # # print("genfinekeys") + # if var==None: + # var=self.varDict + # # print(f"{var = }") + # if dataRetList is None: + # dataRetList = [] + # if isinstance(var, list): + # # print("list var") + # for _ in var: + # yield from self.findKeyInDict(key, _, ) + # elif isinstance(var, dict): + # # print("dict var") + # if key in var: + # dataRetList.append({key: var[key]}) + # # print(f"{dataRetList = }") + # yield {key: var[key]} + # else: + # # print(f'dict else var={var}') + # for _ in var: + # # print(_) + # yield from self.findKeyInDict(key, var[_], ) + # return dataRetList + + + #### dont delete. + ### Example of extracting dictionary value by key + def gen_dict_extract(self, key, var:dict=None): ### Utils + if var==None: + var=self.response_string + # print("var") + if hasattr(var,'items'): # hasattr(var,'items') for python 3, hasattr(var,'iteritems') for python 2 + # print("hassattr") + for k, v in var.items(): # var.items() for python 3, var.iteritems() for python 2 + # print(k,v) + if k == key: + yield v + if isinstance(v, dict): + for result in self.gen_dict_extract(key, v): + yield result + elif isinstance(v, list): + for d in v: + for result in self.gen_dict_extract(key, d): + yield result + + def __str__(self, *args, **kwargs) -> str: + # return str(self._get_datarow(self.connect_to_quickbooks(self.create_QBXML()))) + # print("__str__") + return str(self.all()) + return self.__class__.__name__ + return str(self.get_datarow()) + + + # def get_datarow(self, *args): + # return self._get_datarow(self.connect_to_quickbooks(self.create_QBXML())) + + # def get_dict(self, *args): + # return pd.DataFrame(self._get_datarow(self.connect_to_quickbooks(self.create_QBXML()))) + + def status_ok(self, QBXML): + GSRQRs=QBXML.find('.//GeneralSummaryReportQueryRs') + 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 + + +if __name__ == '__main__': + pass \ No newline at end of file diff --git a/QBClass/utils.py b/QBClass/utils.py new file mode 100644 index 0000000..2b4aba1 --- /dev/null +++ b/QBClass/utils.py @@ -0,0 +1,142 @@ + +from time import time +from typing import Union + + +def getItemFullName(shortItem:str)->str: + # def searchitem(SearchItem = '001aba', QTY = 0, dbTable="itemlist", column="FullName"): + FullName = shortItem + return FullName + +def getCustomerFullName(shortCustomerName:str)->str: + # def searchitem(SearchItem = '001aba', QTY = 0, dbTable="itemlist", column="FullName"): + FullName = shortCustomerName + return FullName + +def getRefNumber()->str: + return "refnumber345" + +def prepareData(dt:str): + dtLines = dt.strip().split('\n') + smoothdtLines:list[str] = [] + for dtLine in dtLines: + if dtLine.strip()=='': + continue + smoothdtLines.append(dtLine) + for smIdx, smdtLine in enumerate(smoothdtLines): + itemName, Qty, memoLine = ('', '', '') + if smIdx==0 and ':' in smdtLine: + CustomerName = smdtLine.split(':')[1].strip() + CustomerRef_FullName = getCustomerFullName(CustomerName) + RefNumber = getRefNumber() + continue + elif smIdx==0 and ':' not in smdtLine: + return False + else: #proccess the second till end lines + for splitChar in [':', '=']: + if splitChar in smdtLine: + objects = smdtLine.split(splitChar) + if len(objects)==2: + itemName, Qty = tuple(smdtLine.split(splitChar)) + if len(objects)==3: + itemName, Qty, memoLine = tuple(smdtLine.split(splitChar)) + break + itemName = itemName.strip() + Qty=Qty.strip() + memoLine = memoLine.strip() + print(CustomerRef_FullName, RefNumber, itemName, Qty, memoLine) + ItemRef_FullName = getItemFullName(itemName) + + if memoLine: + print(memoLine) + else: + print("no memo") + + + + + +def makeLinkedTxnList(x:list, key:str=None)->list: + txnIDs=[] + if isinstance(x, dict): + x=[x] + print(f'{type(x)=}') + for y in x: #y = each dict of LinkedTxn, and it has a listofdict or a dict inside + linkedTxns=y.get('LinkedTxn',None) + if linkedTxns is None: + return + if isinstance(linkedTxns, dict): + linkedTxns = [linkedTxns] + for linkedTxn in linkedTxns: + # print(f'{key = }') + if key and linkedTxn.get('TxnType')==key: + txnIDs.append(linkedTxn.get('TxnID',None)) + print(f'{txnIDs = }') + return txnIDs + +def makeAList(x:Union[str , dict , list], dictvalue:bool=False, listofdict:bool=False)->list : + if isinstance(x,str): + x=[x] + elif isinstance(x, list) and not listofdict: #lists of str or int values + pass + elif isinstance(x, list) and not dictvalue: #lists of dict, and get all the keys + res=[] + for z in x: + for k in z: + res.append(k) + return res + elif isinstance(x, list) and dictvalue: #lists of dict, and get all the values + res=[] + for z in x: + for k,v in z.items(): + res.append(v) + return res + elif isinstance(x, list): #lists of str or int values + res=[] + for z in x: + for k in z: + res.append(k) + return res + elif isinstance(x, dict) and not dictvalue: #get a list of keys from dict + x=[k for k in x] + elif isinstance(x, dict): #get a list of values from dict + x=[v for k,v in x.items()] + + return x + +def timing(f): + # @wraps(f) + def wrap(*args, **kw): + ts = time() + result = f(*args, **kw) + te = time() + print('func:%r args:[%r, %r] took: %2.6f sec' % \ + (f.__name__, args, kw, te-ts)) + return result + return wrap + + +def cleanIncludeRetElements(includeRetElements_allowed:list, includeRetElements:Union[list, str], default_val:str=None): + if default_val==None: + default_val = [] + # else: + # default = [default] + iREs = [] + # print(f'{includeRetElements_allowed = }\n{includeRetElements = }') + if isinstance(includeRetElements, str): #if user put 1 str argument in IncludeRetElements, change it to list + includeRetElements = [includeRetElements] + for iRE in includeRetElements: + for iRE_a in includeRetElements_allowed: + if iRE.lower() == iRE_a.lower(): + iREs.append(iRE_a) + break + if len(iREs)>0: + return iREs + else: + return [default_val] + +if __name__=='__main__': + #Example makelist + print(makeAList("hallo"), makeAList({'world':1, 'case':'another'}), makeAList(['here', 'we', 'go']), makeAList(1), makeAList({'world':1, 'case':'another'}, True)) + alistofdict = [{'RefNumber': '0001'}, {'RefNumber': '0022'}, {'RefNumber': '0004'}, {'RefNumber': '0006'}, {'RefNumber': '0008'}, {'RefNumber': '0009'}, {'RefNumber': '0010'}, {'RefNumber': '0015'}, {'RefNumber': '0065'}, {'RefNumber': '0017'}, {'RefNumber': '0019'}, {'RefNumber': '0023'}, {'RefNumber': '0024'}, {'RefNumber': '0025'}, {'RefNumber': '0027'}, {'RefNumber': '0029'}, {'RefNumber': '0030'}, {'RefNumber': '0045'}, {'RefNumber': '0032'}, {'RefNumber': '0034'}, {'RefNumber': '0035'}, {'RefNumber': '0036'}, {'RefNumber': '0040'}, {'RefNumber': '0003'}, {'RefNumber': '0031'}, {'RefNumber': '0033'}, {'RefNumber': '0037'}, {'RefNumber': '0038'}, {'RefNumber': '0039'}, {'RefNumber': '0047'}, {'RefNumber': '0005'}, {'RefNumber': '0007'}, {'RefNumber': '0012'}, {'RefNumber': '0013'}, {'RefNumber': '0014'}, {'RefNumber': '0016'}, {'RefNumber': '0011'}, {'RefNumber': '0021'}, {'RefNumber': '0002'}, {'RefNumber': '0018'}, {'RefNumber': '0020'}, {'RefNumber': '0026'}, {'RefNumber': '0028'}, {'RefNumber': '0041'}, {'RefNumber': '0051'}, {'RefNumber': '0062'}, {'RefNumber': '0042'}, {'RefNumber': '0043'}, {'RefNumber': '0053'}, {'RefNumber': '0055'}, {'RefNumber': '0057'}, {'RefNumber': '0058'}, {'RefNumber': '0059'}, {'RefNumber': '0061'}, {'RefNumber': '0068'}, {'RefNumber': '0044'}, {'RefNumber': '0046'}, {'RefNumber': '0048'}, {'RefNumber': '0049'}, {'RefNumber': '0050'}, {'RefNumber': '0086'}, {'RefNumber': '0054'}, {'RefNumber': '0056'}, {'RefNumber': '0060'}, {'RefNumber': '0052'}, {'RefNumber': '0064'}, {'RefNumber': '0066'}, {'RefNumber': '0070'}, {'RefNumber': '0071'}, {'RefNumber': '0072'}, {'RefNumber': '0075'}, {'RefNumber': '0076'}, {'RefNumber': '0078'}, {'RefNumber': '0080'}, {'RefNumber': '0081'}, {'RefNumber': '0085'}, {'RefNumber': '0067'}, {'RefNumber': '0069'}, {'RefNumber': '0073'}, {'RefNumber': '0074'}, {'RefNumber': '0077'}, {'RefNumber': '0079'}, {'RefNumber': '0082'}, {'RefNumber': '0083'}, {'RefNumber': '0084'}, {'RefNumber': '0087'}, {'RefNumber': '0089'}, {'RefNumber': '0091'}, {'RefNumber': '0092'}, {'RefNumber': '0093'}, {'RefNumber': '0094'}, {'RefNumber': '0095'}, {'RefNumber': '0096'}, {'RefNumber': '0167'}, {'RefNumber': '0088'}, {'RefNumber': '0097'}, {'RefNumber': '0098'}, {'RefNumber': '0099'}, {'RefNumber': '0100'}, {'RefNumber': '0103'}] + print(makeAList(alistofdict,listofdict=True, dictvalue=True)) \ No newline at end of file diff --git a/main.py b/main.py index 5bc6ebb..919e629 100644 --- a/main.py +++ b/main.py @@ -18,6 +18,7 @@ import pdfexcel4DNwithxlrd from ItemInventoryQuery import ItemInventoryQuery from SO_to_Inv import readSO, CustomerQuery import os +from QBClass.QBClasses import SalesOrderAdd import pprint @@ -223,4 +224,22 @@ async def get_generalsalesreport(request: Request): print(f'{datetime.datetime.now()} get_gsr 4 finish -> {type(getdict)}, {getdict}') print("") - return datas1[0], responseRt.get_total(), datas1[1] \ No newline at end of file + return datas1[0], responseRt.get_total(), datas1[1] + + +@app.post('/dasa2/salesorder') +async def salesorderadd(request: Request): + salesorderdict = await request.body() + print("") + try: + salesorderdict = json.loads(salesorderdict) + except: + print('error salesorderadd()') + return {'message':'error getting SalesOrder Data. it is not json'} + print(f'{type(salesorderdict)}, {salesorderdict = }') + + so = SalesOrderAdd(**salesorderdict) + print(so.all()) + return so.all() + +