Example #1
0
    def chart_of_accounts(self,id,request):
        """
        Get Chart of Accounts From online
        :param company: Company ID
        :return: Response
        """

        try:
            # login_status = Utils.get_login_status(company)
            # if login_status != LoginInfo.IN_PROGRESS:
            #     message = "Login Authentication Failed"
            #     return Utils.dispatch_failure(request,message)
            company = AccountsUtils.get_company(id)
            secret_keys = Utils.get_access_keys(id)
            # Get xero auth access information form xero connection
            auth_info = AccountingOauth2.objects.filter(company_id=id).values('accessToken', 'accessSecretKey',
                                                                                  'tokenAcitvatedOn', 'tokenExpiryON')
            if len(auth_info) == 0:
                return Utils.dispatch_failure(request, 'NO_TOKEN_AUTHENTICATION')

            for key, value in auth_info[0].items():
                OAUTH_PERSISTENT_SERVER_STORAGE.update({key: value})
            stored_values = OAUTH_PERSISTENT_SERVER_STORAGE

            if len(stored_values) == 0:
                return Utils.dispatch_failure(request, "NO_TOKEN_AUTHENTICATION")

            auth = Utils.get_xero_auth(id)


            if AccountingConfiguration.PRIVATE == secret_keys.type:
                credentials = PrivateCredentials(**auth)
            else:
                credentials = PublicCredentials(**auth)

                if credentials.expired():
                    return Utils.dispatch_failure(request, 'NO_TOKEN_AUTHENTICATION')

            # Enable the access for accessing the reports from xero logged in account.
            xero = Xero(credentials)
            # Resave our verified credentials
            # stored_values = bind_auth_info(credentials, pk)

        except XeroException as e:
            if AccountingConfiguration.PRIVATE == secret_keys.type:
                error = ["%s" % e]
                return Utils.dispatch_failure(request, 'XERO_CONNECTION_ERROR', error)
            else:
                return Utils.dispatch_failure(request, "NO_TOKEN_AUTHENTICATION")
        try:
            chartofaccounts = xero.accounts.all()
            XeroAccountings.save_chart_of_accounts(company, chartofaccounts)
            return Utils.dispatch_success(request,"COA_FETECHED_SUCCESSFULLY")
        except XeroException as e:
            if AccountingConfiguration.PRIVATE == secret_keys.type:
                error = ["%s" % e]
                return Utils.dispatch_failure(request, 'XERO_CONNECTION_ERROR', error)
            else:
                return Utils.dispatch_failure(request, "NO_TOKEN_AUTHENTICATION")
        except Exception as e:
            error = ["%s" % e]
            return Utils.dispatch_failure(request, 'DATA_PARSING_ISSUE', error)
Example #2
0
    def trail_balance(self, pk, request):
        """
        Get Trail Balance From online
        :param company: Company Id
        :return: Response
        """
        try:
            # Checking Token Authentication available
            auth_info = AccountingOauth2.objects.filter(company_id=pk).values('accessToken', 'accessSecretKey',
                                                                             'tokenAcitvatedOn', 'tokenExpiryON')
            secret_keys = Utils.get_access_keys(pk)
            if len(auth_info) == 0:
                return Utils.dispatch_failure(request, "NO_TOKEN_AUTHENTICATION")

            for key, value in auth_info[0].items():
                OAUTH_PERSISTENT_SERVER_STORAGE.update({key: value})
            stored_values = OAUTH_PERSISTENT_SERVER_STORAGE

            if len(stored_values) == 0:
                return Utils.dispatch_failure(request, "NO_TOKEN_AUTHENTICATION")


            # Checking Xero Connection Authentication available
            auth = Utils.get_xero_auth(pk)

            if AccountingConfiguration.PRIVATE == secret_keys.type:
                credentials = PrivateCredentials(**auth)
            else:
                credentials = PublicCredentials(**auth)

                if credentials.expired() or credentials is None:
                    return Utils.dispatch_failure(request, "NO_TOKEN_AUTHENTICATION")

            try:
                xero = Xero(credentials)
                xero.reports.get('TrialBalance')

            except XeroException as e:
                if AccountingConfiguration.PRIVATE == secret_keys.type:
                    error = ["%s" % e]
                    return Utils.dispatch_failure(request, 'XERO_CONNECTION_ERROR', error)
                else:
                    return Utils.dispatch_failure(request, "NO_TOKEN_AUTHENTICATION")
            try:
                meta = CompanyMeta.objects.filter(company_id=pk).first()
                if meta.monthly_reporting_current_period:
                    st = time.time()
                    from portalbackend.lendapi.v1.accounting.tasks import trial_balance_for_period
                    job = group(trial_balance_for_period.s(pk, i) for i in range(0, 23))
                    result = job.apply_async()
                else:
                    return Utils.dispatch_failure(request, 'MISSING_MONTHLY_REPORTING_CURRENT_PERIOD')

                while not result.ready():
                    continue
                return Utils.dispatch_success(request, 'TRIAL_BALANCE_RECEIVED_SUCCESS')
            except Exception as e:
                error = ["%s" % e]
                return Utils.dispatch_failure(request, 'DATA_PARSING_ISSUE', error)
        except Exception as e:
            return Utils.dispatch_failure(request, "INTERNAL_SERVER_ERROR")