def is_token_valid(self, pk, request):
     """
     Check the validity of Token
     :param pk: Company ID
     :return: Response of validation
     """
     try:
         company = AccountsUtils.get_company(pk)
         credentials = AccountingOauth2.objects.filter(
             company=company).first()
         if credentials:
             sample_response, status_code = Utils.get_company_info(
                 credentials.accessToken, credentials.realmId)
             if status_code >= 400:
                 bearer = get(credentials.refreshToken)
                 new_credentials = AccountingUtils.updateAccountingSession(
                     company, bearer.accessToken, bearer.refreshToken,
                     credentials.realmId)
                 sample_response, status_code = Utils.get_company_info(
                     new_credentials.accessToken, new_credentials.realmId)
                 if status_code >= 400:
                     return Utils.dispatch_failure(
                         request, 'NO_TOKEN_AUTHENTICATION')
             return Utils.dispatch_success(request,
                                           "AUTHENTICATED_SUCCESSFULLY")
         return Utils.dispatch_failure(request, 'NO_TOKEN_AUTHENTICATION')
     except Exception as e:
         return Utils.dispatch_failure(request, 'NO_TOKEN_AUTHENTICATION')
Beispiel #2
0
    def post(self, request, pk, *args, **kwargs):
        """
        Parses the income statement and balance sheet sheet request, sent in the form of:
         {"IncomeStatement": [{
                "Period": "2016-01",
                "Account": "Value"},{..}]
            }
        """
        try:
            company = AccountsUtils.get_company(pk)

            try:
                entries, error_tags = AccountingUtils.parse_statement(request, company, request.data,
                                                                      FinancialStatementEntry.INCOME_STATEMENT)
                FinancialStatementEntry.objects.bulk_create(entries)
            except Exception as e:
                error = ["%s" % e]
                return Utils.dispatch_failure(request, 'DATA_PARSING_ISSUE', error)

            serializer = FinancialStatementEntrySerializer(entries, many=True)
            if len(serializer.data) > 0:
                return Utils.dispatch_success(request, serializer.data)
            return Utils.dispatch_success(request, 'NO_DATA_CHANGES')
        except Exception as e:
            return Utils.dispatch_failure(request, 'INTERNAL_SERVER_ERROR')
    def trail_balance(self, pk, request):
        """
        Get the trail balance profile from Quick Books
        :param pk: company: Company ID
        :return: Response of trail balance
        """
        try:
            meta = CompanyMeta.objects.filter(company_id=pk).first()

            if meta.monthly_reporting_current_period:
                st = time.time()

                # this will grab the trial balance for the companymeta.monthly_reporting_current_period
                # plus 23 more months worth of history.
                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')

            # note: this bit of code basically turns this into a synchronous call, this is intended behaviour for now
            #       todo: phase 2 we need to add sockets for better communication with UI
            while not result.ready():
                continue

            print('##### CELERY get TB now takes {:.2f}s'.format(time.time() -
                                                                 st))
            return Utils.dispatch_success(request,
                                          'TRIAL_BALANCE_RECEIVED_SUCCESS')
        except Exception as e:
            return Utils.dispatch_failure(request, 'INTERNAL_SERVER_ERROR')
Beispiel #4
0
    def put(self, request, pk, report_identifier, *args, **kwargs):
        """
        Updates a Company's Monthly Report instance Monthly Report Period or Monthly Report ID
        """
        try:
            company = AccountsUtils.get_company(pk)
            if '-' in report_identifier:
                monthly_report = ReportingUtils.get_monthly_report(pk=pk, period=report_identifier)
            else:
                monthly_report = ReportingUtils.get_monthly_report(pk=pk, report_id=report_identifier)
            response_status = status.HTTP_200_OK  # default to OK and override if not

            if monthly_report:
                data = request.data
                data['company'] = company.id
                serializer = MonthlyReportSerializer(monthly_report, data=data,
                                                     context={'request': request, 'company_id': pk}, partial=True)

                if serializer.is_valid():
                    serializer.save()
                    return Utils.dispatch_success(request,serializer.data)
                else:
                    return Utils.dispatch_failure (request,'VALIDATION_ERROR',serializer.errors)

            return Utils.dispatch_failure(request,'MONTHLY_REPORT_NOT_FOUND')
        except Exception as e:
            return Utils.dispatch_failure (request,'INTERNAL_SERVER_ERROR')
    def refresh(self, pk, request):
        """
        Refresh the token
        :param pk: Company ID
        :return: Response
        """
        try:
            company = AccountsUtils.get_company(pk)
            credentials = AccountingUtils.get_credentials_by_company(company)
            refresh_token = credentials.refreshToken

            if refresh_token is None:
                return Utils.dispatch_failure(request,
                                              'NO_TOKEN_AUTHENTICATION')
            bearer = Utils.get_bearer_token_from_refresh_token(refresh_token)
            if bearer is "failure":
                return Utils.dispatch_failure(request,
                                              "NO_TOKEN_AUTHENTICATION")
            if isinstance(bearer, str):
                return Utils.dispatch_success(request, bearer)
            else:

                token_info = AccountingOauth2.objects.filter(
                    company=company).first()
                AccountingUtils.updateAccountingSession(
                    company, bearer.accessToken, bearer.refreshToken,
                    token_info.realmId)
                return Utils.dispatch_success(request, "CREDENTIALS_UPDATED")
        except Exception as e:
            return Utils.dispatch_failure(request, 'NO_TOKEN_AUTHENTICATION')
Beispiel #6
0
    def post(self, request, pk, *args, **kwargs):
        """
        Starts a new monthly report for the current reporting period, and moves next period ahead by 1 month to prepare
        for the next time. Will not create one that already exists, or create another one until the previous one
        is completed, this is a business rule.
        """
        try:
            company = AccountsUtils.get_company(pk)
            response_status = status.HTTP_200_OK
            response = ''

            meta = CompanyMeta.objects.filter(company_id=pk).first()
            current_period = meta.monthly_reporting_current_period
            next_period = meta.monthly_reporting_next_period

            if current_period is None:
                Utils.send_company_meta(request.user,"PERIOD")
                return Utils.dispatch_failure(request,'MISSING_MONTHLY_REPORTING_CURRENT_PERIOD')

            try:
                # does one exist for the current period
                report = MonthlyReport.objects.filter(company=company, period_ending=current_period).first()
                print('######## CURR AND NEXT - start as: c: ', current_period, ' n: ', next_period, ' rpt: ', report)
            except Exception as e:
                error = ["%s" % e]
                return Utils.dispatch_failure (request,'DATA_PARSING_ISSUE', error)


            # if it doesn't exist, or if the previous one has been completed, then it's ok to create a new one
            if not report or report.status == 'Complete':
                # todo: Due date is not relevant, remove it. We allow them to report any time after the end of the current
                # during initial setup, we do not advance the reporting dates. There is no initial setup for Form Entry
                # aka manual reporting
                if not meta.is_initial_setup:
                    print('#### moving period forward for new monthly report')
                    current_period = next_period
                    next_period = next_period + relativedelta(months=+1, day=31)
                    meta.monthly_reporting_current_period = current_period
                    meta.monthly_reporting_next_period = next_period
                    meta.monthly_reporting_current_period_status = 'IN_PROGRESS'
                    meta.save()
                    print('######## CURR AND NEXT - not initial setup, after move forward ', current_period, next_period)

                print('#### creating new monthly report for current period = ', meta.monthly_reporting_current_period)
                report = MonthlyReport(status='In Progress', company_id=pk, period_ending=current_period)
                report.save()

                response = serializers.serialize('json', [report, ])
                # return response
                return Utils.dispatch_success(request,[response])
            else:
                # was using HTTP_304_NOT_MODIFIED, but when this status is returned the JSON message is null which causes
                # problems for the UI
                # 409 causes the UI to stop processing because it thinks theres an error, but there isn't one.
                # so changing to 200 OK instead since it's OK to move forward
                # response_status = status.HTTP_409_CONFLICT
                return Utils.dispatch_success(request,'MONTHLY_REPORT_ALREADY_EXISTS_WITH_INPROGRESS')

        except Exception as e:
            return Utils.dispatch_failure (request,'INTERNAL_SERVER_ERROR')
Beispiel #7
0
    def get(self, request, pk, *args, **kwargs):
        """
        Creates the CoAMap for a company from the CoA in the database
        """
        try:
            company = AccountsUtils.get_company(pk)
            coa = CoA.objects.filter(company=company)
            remap = request.GET.get('remap', None)
            if coa:
                default_mappings = DefaultAccountTagMapping.objects.filter(software__iexact=company.accounting_type)

                if not default_mappings:
                    return Utils.dispatch_failure(request, "OBJECT_RESOURCE_NOT_FOUND")

                # todo: make sure abstract financial statement entry tags are not getting processed in the CoA Mapping
                try:
                    entries = AccountingUtils.create_coa_map(request, company, default_mappings, coa, remap)
                except Exception as e:
                    error = ["%s" % e]
                    return Utils.dispatch_failure(request, 'DATA_PARSING_ISSUE', error)

                if len(entries) > 0:
                    serializer = CoAMapSerializer(entries, many=True)
                    return Utils.dispatch_success(request, serializer.data)
                return Utils.dispatch_success(request, "NO_DATA_CHANGES")
            return Utils.dispatch_failure(request, "OBJECT_RESOURCE_NOT_FOUND")

        except Exception as e:
            return Utils.dispatch_failure(request, 'INTERNAL_SERVER_ERROR')
Beispiel #8
0
 def get(self, request, *args, **kwargs):
     try:
         company_id = request.GET.get('company', None)
         # Check company is exists
         is_valid_company, message = Utils.check_company_exists(company_id)
         if not is_valid_company:
             return Utils.dispatch_failure(request, "RESOURCE_NOT_FOUND")
         return Accounting().get_instance_by_id(company_id).connect(request, company_id)
     except KeyError:
         return redirect(request.META["HTTP_REFERRER"])
     except Exception as e:
         return Utils.dispatch_failure(request, 'INTERNAL_SERVER_ERROR')
Beispiel #9
0
 def delete(self, request, pk, *args, **kwargs):
     try:
         if self.request.user.is_superuser:
             company = AccountsUtils.get_company(pk)
             if TrialBalance.objects.filter(company=company).count():
                 TrialBalance.objects.filter(company=company).delete()
                 return Utils.dispatch_success(request, 'DELETED_SUCCESSFULLY')
             else:
                 return Utils.dispatch_failure(request, "OBJECT_RESOURCE_NOT_FOUND")
         return Utils.dispatch_failure(request, 'UNAUTHORIZED_ACCESS')
     except Exception as e:
         return Utils.dispatch_failure(request, 'INTERNAL_SERVER_ERROR')
Beispiel #10
0
 def get(self, request, pk, *args, **kwargs):
     try:
         # Check company is exists
         is_valid_company, message = Utils.check_company_exists(pk)
         if not is_valid_company:
             return Utils.dispatch_failure(request, 'RESOURCE_NOT_FOUND')
         login_status = Utils.get_login_status(pk)
         if login_status:
             return Utils.dispatch_success(request, login_status.data)
         return Utils.dispatch_failure(request, 'UNAUTHORIZED_ACCESS')
     except Exception as e:
         print(e)
         return Utils.dispatch_failure(request, 'INTERNAL_SERVER_ERROR')
    def chart_of_accounts(self, company, request):
        """
        Get the chart of account profile from Quick Books
        :param company: Company ID
        :return: Response
        """
        try:
            company = AccountsUtils.get_company(company)
            cm = CompanyMeta.objects.filter(company_id=company).first()

            credentials = AccountingUtils.get_credentials_by_company(company)

            if not credentials:
                return Utils.dispatch_failure(request,
                                              "NO_TOKEN_AUTHENTICATION")

            chart_of_accounts_response, status_code = Utils.get_chart_of_accounts(
                credentials.accessToken, credentials.realmId)

            if status_code >= 400:
                print("First Failure")
                bearer = Utils.get_bearer_token_from_refresh_token(
                    credentials.refreshToken)

                if bearer is "failure":
                    return Utils.dispatch_failure(request,
                                                  "NO_TOKEN_AUTHENTICATION")

                new_credentials = AccountingUtils.updateAccountingSession(
                    company, bearer.accessToken, bearer.refreshToken,
                    credentials.realmId)

                chart_of_accounts_response, status_code = Utils.get_chart_of_accounts(
                    new_credentials.accessToken, new_credentials.realmId)
                if status_code >= 400:
                    return Utils.dispatch_failure(request,
                                                  "NO_TOKEN_AUTHENTICATION")

            coas = QuickBooks.save_chart_of_accounts(
                company, chart_of_accounts_response)

            cm.chartofaccounts_last_refresh_date = datetime.datetime.now()
            cm.save()

            serializer = CoASerializer(coas, many=True)

            return Utils.dispatch_success(request, "COA_FETECHED_SUCCESSFULLY")
        except Exception as e:
            return Utils.dispatch_failure(request, 'INTERNAL_SERVER_ERROR')
Beispiel #12
0
 def delete(self, request, pk, *args, **kwargs):
     try:
         if self.request.user.is_superuser:
             company = AccountsUtils.get_company(pk)
             if FinancialStatementEntry.objects.filter(company=company,
                                                       statement_type=FinancialStatementEntry.INCOME_STATEMENT,
                                                       ).count():
                 FinancialStatementEntry.objects.filter(company=company,
                                                        statement_type=FinancialStatementEntry.INCOME_STATEMENT,
                                                        ).delete()
                 return Utils.dispatch_success(request, 'DELETED_SUCCESSFULLY')
             return Utils.dispatch_success(request, "DATA_NOT_FOUND")
         return Utils.dispatch_failure(request, 'UNAUTHORIZED_ACCESS')
     except Exception as e:
         return Utils.dispatch_failure(request, 'INTERNAL_SERVER_ERROR')
Beispiel #13
0
    def post(self, request, pk, *args, **kwargs):
        """
        Creates Trial Balance from Posted json request, follows the format
        {"Header": {"Time": <timestamp>, "StartPeriod: "YYYY-MM-DD", "EndPeriod": "YYYY-MM-DD", "Currency"...},
         "Columns": { "Column": [ {"ColTitle": "Title", "ColType": "AccountType"}, {..}],
         "Rows": {"Row": [ {"ColData": [{"value": "Account", "id": <int>}, {..}], { "ColData"...}]
         }
        More info here: https://developer.intuit.com/docs/api/accounting/trial%20balance
        """
        try:
            company = AccountsUtils.get_company(pk)
            if 'file' in request.data:

                file_type = CSVUtils.file_type(request.FILES['file'])
                if not file_type:
                    return Utils.dispatch_failure(request, "INVALID_FILE_FORMAT")
                request.FILES['file'].seek(0)

                if file_type == 'CSV':
                    print('#--- processing tb csv')
                    try:
                        csv_data = CSVUtils.format_request_csv(request.FILES['file'])
                        tb_data = CSVUtils.process_trial_balance_csv(company, csv_data)
                        if not tb_data:
                            return Utils.dispatch_failure(request, 'INVALID_TB_DATE')
                        if tb_data is "INVALID_CSV":
                            return Utils.dispatch_failure(request, 'INVALID_TB_CSV')
                        serializer = TrialBalanceSerializer(tb_data, many=True)
                        if len(serializer.data) > 0:
                            return Utils.dispatch_success(request, serializer.data)
                        return Utils.dispatch_success(request, 'NO_DATA_CHANGES')
                    except Exception as e:
                        error = ["%s" % e]
                        return Utils.dispatch_failure(request, 'DATA_PARSING_ISSUE', error)
                elif file_type == 'Excel':
                    try:
                        excel_data = CSVUtils.format_request_excel(request.FILES)

                        # wont be found on heroku
                        with contextlib.suppress(FileNotFoundError):
                            os.remove('tmp.xlsx')
                    except Exception as e:
                        error = ["%s" % e]
                        return Utils.dispatch_failure(request, 'DATA_PARSING_ISSUE', error)

                        # return Response({"message": "Sent a CSV"})
            else:
                try:
                    entries = QuickBooks.save_trial_balance(company, request.data)
                    if entries is None:
                        return Utils.dispatch_failure(request, 'INVALID_TB_DATE')
                    TrialBalance.objects.bulk_create(entries)
                except Exception as e:
                    error = ["%s" % e]
                    return Utils.dispatch_failure(request, 'DATA_PARSING_ISSUE', error)
                return Utils.dispatch_success(request, 'TRIAL_BALANCE_SAVE_SUCCESS')
        except Exception as e:
            return Utils.dispatch_failure(request, 'INTERNAL_SERVER_ERROR')
Beispiel #14
0
    def get(self, request, pk, report_identifier):
        """
        Gets a Company's Questionnaire answers for the requested period

        If called with no period specified, it simply returns a blank set of questions. The functionality supports
        the UI which dynamically displays the questions returned by the server.
        """
        try:
            response = None
            response_status = status.HTTP_200_OK

            context = {'request': request,
                       'company': pk,
                       'period': report_identifier}
            print('############### BEFORE INT COMPARISON')
            # todo: logic needs to be cleaned up. questions and has_answers have overlapping functionality that can be
            #       simplified

            questions = ReportingUtils.get_questionnaire_objects(pk, report_identifier)
            has_answers = ReportingUtils.has_answers_for_period(pk, report_identifier)

            if questions and has_answers:
                # return Q n A for requested period.
                serializer = QuestionWithAnswerSerializer(questions, many=True, context=context)
                if len(serializer.data) > 0:
                    return Utils.dispatch_success(request,serializer.data)
                return Utils.dispatch_success(request,'NO_ANSWER_FOUND')
            return Utils.dispatch_success (request, 'DATA_NOT_FOUND')
        except Exception as e:
            return Utils.dispatch_failure(request,'INTERNAL_SERVER_ERROR')
Beispiel #15
0
    def get(self, request, pk):
        """
        Gets a Company's Questionnaire answers
        """
        try:
            response = None
            response_status = status.HTTP_200_OK

            context = {'request': request,
                       'company': pk,
                       }

            # get an empty set of questions
            qs1 = Question.objects.filter(Q(common_to_all_companies=True, show_on_ui=True) |
                                          Q(common_to_all_companies=False,show_on_ui=True,company=pk))\
                                          .all()

            qs1 = ReportingUtils.sanitize_next_questions(qs1)

            serializer = QuestionWithAnswerSerializer(qs1, many=True, context=context)
            if len (serializer.data) > 0:
                return Utils.dispatch_success (request,serializer.data)
            return Utils.dispatch_success (request,'NO_ANSWER_FOUND')
        except Exception as e:
            return Utils.dispatch_failure(request,'INTERNAL_SERVER_ERROR')
Beispiel #16
0
    def get(self, request, pk, report_identifier, *args, **kwargs):
        """
        Gets a Company's Monthly Report instance by Monthly Report Period or Monthly Report ID
        """
        try:
            if '-' in report_identifier:
                monthly_report = ReportingUtils.get_monthly_report(pk=pk, period=report_identifier)
            else:
                monthly_report = ReportingUtils.get_monthly_report(pk=pk, report_id=report_identifier)

            if monthly_report:
                serializer = MonthlyReportSerializer(monthly_report, context={'request': request, 'company_id': pk})
                return Utils.dispatch_success(request,serializer.data)
            return Utils.dispatch_failure(request,'MONTHLY_REPORT_NOT_FOUND')
        except Exception as e:
            return Utils.dispatch_failure (request,'INTERNAL_SERVER_ERROR')
Beispiel #17
0
 def get(self, request, pk, *args, **kwargs):
     """
     Check to see if the Accounting System token is still valid for the company
     """
     try:
         return Accounting().get_instance_by_id(pk).is_token_valid(pk, request)
     except Exception as e:
         return Utils.dispatch_failure(request, 'INTERNAL_SERVER_ERROR')
Beispiel #18
0
    def get(self, request, pk, *args, **kwargs):
        """
        Gets the Balance Sheet Financial Statement Entries from the database
        Range can be specified by adding ?start_date=YYYY-MM-DD&end_date=YYYY-MM-DD

        No date params will return all entries for the specified company.
        end_date param only will provide data for that specific period.
        A start_date without an end_date, it will cause an error.
        """
        try:
            company = AccountsUtils.get_company(pk)

            # todo: query variable is never used, and appears to be unncessary
            query, dates = Utils.validate_query(request, dateformat=True)

            # todo: figure out why order_by isn't workin on these query_sets, ordering added to model as workaround
            if not dates:
                print('########### not dates')
                queryset = FinancialStatementEntry.objects.filter(company=company,
                                                                  statement_type=FinancialStatementEntry.BALANCE_SHEET)
            else:
                if dates[0] == 'ERROR':
                    return Utils.dispatch_failure(request, dates[1])
                elif len(dates) == 2:
                    queryset = FinancialStatementEntry.objects.filter(company=company,
                                                                      period_ending__range=(dates[0], dates[1]),
                                                                      statement_type=FinancialStatementEntry.BALANCE_SHEET,
                                                                      )
                elif len(dates) == 1:
                    print(dates)
                    queryset = FinancialStatementEntry.objects.filter(company=company,
                                                                      period_ending=dates[0],
                                                                      statement_type=FinancialStatementEntry.BALANCE_SHEET,
                                                                      )
                else:
                    print(dates)
                    queryset = FinancialStatementEntry.objects.filter(company=company,
                                                                      statement_type=FinancialStatementEntry.BALANCE_SHEET)

            if queryset:
                serializer = FinancialStatementEntrySerializer(queryset, many=True)
                return Utils.dispatch_success(request, serializer.data)
            return Utils.dispatch_success(request, 'DATA_NOT_FOUND')
        except Exception as e:
            return Utils.dispatch_failure(request, 'INTERNAL_SERVER_ERROR')
Beispiel #19
0
 def get(self, request, pk, *args, **kwargs):
     """
     Gets the trial balance from online and writes to database
     """
     # Check company is exists
     try:
         return Accounting().get_instance_by_id(pk).trail_balance(pk, request)
     except Exception as e:
         return Utils.dispatch_failure(request, 'INTERNAL_SERVER_ERROR')
Beispiel #20
0
    def get(self, request, pk, *args, **kwargs):
        """
        Gets the Income Statement Financial Statement Entries from the database
        Range can be specified by adding ?start_date=YYYY-MM-DD&end_date=YYYY-MM-DD


        No date params will return all entries for the specified company.
        end_date param only will provide data for that specific period.
        A start_date without an end_date, it will cause an error.
        """
        try:
            company = AccountsUtils.get_company(pk)

            query, dates = Utils.validate_query(request, dateformat=True)

            if not dates:
                queryset = FinancialStatementEntry.objects.filter(company=company,
                                                                  statement_type=FinancialStatementEntry.INCOME_STATEMENT)
            else:
                if dates[0] == 'ERROR':
                    return Utils.dispatch_failure(request, dates[1])
                elif len(dates) == 2:
                    queryset = FinancialStatementEntry.objects.filter(company=company,
                                                                      period_ending__range=(dates[0], dates[1]),
                                                                      statement_type=FinancialStatementEntry.INCOME_STATEMENT,
                                                                      )
                elif len(dates) == 1:
                    queryset = FinancialStatementEntry.objects.filter(company=company,
                                                                      period_ending=dates[0],
                                                                      statement_type=FinancialStatementEntry.INCOME_STATEMENT,
                                                                      )
                else:
                    queryset = FinancialStatementEntry.objects.filter(company=company,
                                                                      statement_type=FinancialStatementEntry.INCOME_STATEMENT)

            queryset = queryset.order_by('value')

            if queryset:
                serializer = FinancialStatementEntrySerializer(queryset, many=True)
                return Utils.dispatch_success(request, serializer.data)
            return Utils.dispatch_success(request, 'DATA_NOT_FOUND')

        except Exception as e:
            return Utils.dispatch_failure(request, 'INTERNAL_SERVER_ERROR')
Beispiel #21
0
    def delete(self, request,pk, report_identifier, *args, **kwargs):
        """
        Updates a Company's Monthly Report instance Monthly Report Period or Monthly Report ID
        """
        try:
            # TODO: Can you delete monthly reports? pls confirm
            if self.request.user.is_superuser:
                if '-' in report_identifier:
                    monthly_report = ReportingUtils.get_monthly_report(pk=pk, period=report_identifier)
                else:
                    monthly_report = ReportingUtils.get_monthly_report(pk=pk, report_id=report_identifier)
                if monthly_report:
                    monthly_report.delete()
                    return Utils.dispatch_success(request,'DELETED_SUCCESSFULLY')
                return Utils.dispatch_failure(request, 'MONTHLY_REPORT_NOT_FOUND')
            return Utils.dispatch_failure(request,'UNAUTHORIZED_ACCESS')

        except Exception as e:
            return Utils.dispatch_failure (request,'INTERNAL_SERVER_ERROR')
Beispiel #22
0
 def get(self, request, pk, *args, **kwargs):
     """
     Gets the Chart of accounts from online and writes to database
     """
     # Check company is exists
     try:
         return Accounting().get_instance_by_id(pk).chart_of_accounts(pk, request)
     except Exception as e:
         print(e)
         return Utils.dispatch_failure(request, 'INTERNAL_SERVER_ERROR')
    def disconnect(self, pk, request):
        """
        Disconnect the QuickBooks
        :param pk: Company ID
        :return: Response
        """
        company = AccountsUtils.get_company(pk)

        credentials = AccountingUtils.get_credentials_by_company(company)
        try:
            revoke_response = Utils.revoke_token(credentials.accessToken)
            if "Token is incorrect" in revoke_response:
                return Utils.dispatch_failure(request,
                                              "NO_TOKEN_AUTHENTICATION")
            return Utils.dispatch_success(request, "REVOKE_SUCCESSFULL")

        except Exception as e:
            print(e)
            return Utils.dispatch_failure(request, "NO_TOKEN_AUTHENTICATION")
Beispiel #24
0
    def get(self, request, pk, *args, **kwargs):
        """
        Lists all monthly reports for the specified companies
        """
        try:
            self.queryset = MonthlyReport.objects.filter(company=pk).order_by('period_ending')

            if len (self.queryset) == 0:
                return Utils.dispatch_success(request,'DATA_NOT_FOUND')
            return super(MonthlyReportList, self).get(request)
        except Exception:
            return Utils.dispatch_failure(request, 'INTERNAL_SERVER_ERROR')
Beispiel #25
0
    def post(self, request, pk, *args, **kwargs):
        try:
            company = AccountsUtils.get_company(pk)
            # print('CoAMap POST - request.data ', request.data)
            updated_maps = AccountingUtils.set_coa_map(company, request.data)
            serializer = UpdatedCoAMapSerializer(updated_maps, many=True)
            if len(serializer.data):
                return Utils.dispatch_success(request, serializer.data)
            return Utils.dispatch_success(request, "NO_DATA_CHANGES")

        except Exception as e:
            return Utils.dispatch_failure(request, 'INTERNAL_SERVER_ERROR')
Beispiel #26
0
    def post(self, request, pk, report_identifier):
        """
        Updates or creates a Company's Questionnaire Answers for a given period
        """
        try:

            if '-' in report_identifier:
                monthly_report = ReportingUtils.get_monthly_report(pk=pk, period=report_identifier)
            else:
                monthly_report = ReportingUtils.get_monthly_report(pk=pk, report_id=report_identifier)

            if monthly_report is None:
                return Utils.dispatch_failure(request, 'MISSING_MONTHLY_REPORTING_PREVIOUS_PERIOD')

            answers = []
            context = {'request': request,
                       'company': pk,
                       'period': report_identifier}
            try:
                for item in request.data:
                    # None Values not checked becauseof, To update answer If yes, please provide details is changed to No scenario
                    # if item['answer'] is not None:
                    item['company'] = pk
                    item['monthly_report'] = monthly_report.id
                    exists = ReportingUtils.answer_exists(item)
                    if exists:
                        serializer = CreateAnswerSerializer(exists, data=item, context=context)
                    else:
                        serializer = CreateAnswerSerializer(data=item, context=context)
                    if serializer.is_valid():
                        serializer.save()

            except Exception as e:
                error = ["%s" % e]
                return Utils.dispatch_failure(request,'DATA_PARSING_ISSUE', error)
            questions = ReportingUtils.get_questionnaire_objects(pk, report_identifier)
            serializer = QuestionWithAnswerSerializer(questions, many=True, context=context)
            return Utils.dispatch_success(request,serializer.data)
        except Exception as e:
            return Utils.dispatch_failure (request,'INTERNAL_SERVER_ERROR')
Beispiel #27
0
 def put(self, request, pk, *args, **kwargs):
     """
     Updates verified by user in CoAMap for a company in the database
     """
     try:
         company = AccountsUtils.get_company(pk)
         coamap = CoAMap.objects.filter(company_id=company)
         for entry in coamap:
             entry.verified_by_user = False
             entry.save()
         return Utils.dispatch_success(request, 'COAMAP_UPDATED_SUCCESSFULLY')
     except Exception as e:
         return Utils.dispatch_failure(request, 'INTERNAL_SERVER_ERROR')
Beispiel #28
0
 def mark_coamap_verified(self,request, pk):
     """
     The verified flag on the CoAMap will remain false until the reporting period is signed off on
     so that the mapping exercise can be re-done easily at any point during the period.
     :return:
     """
     try:
         coamap = CoAMap.objects.filter(company_id=pk)
         for entry in coamap:
             # print('############# verified by = true')
             entry.verified_by_user = True
             # print('################ entry.verified_by_user ', entry.verified_by_user)
             entry.save()
     except Exception as e:
         return Utils.dispatch_failure (request,'INTERNAL_SERVER_ERROR')
Beispiel #29
0
    def trail_balance(self, id, request):
        """
        Get Trail Balance From Database
        :param id: Company Id
        :return: Response
        """
        try:
            entries = TrialBalance.objects.filter(company=id)
            serializer = TrialBalanceSerializer(entries, many=True)
            if len(serializer.data) > 0:
                return Utils.dispatch_success(request, serializer.data)
            return Utils.dispatch_success(request, 'NO_DATA_CHANGES')

        except Exception:
            return Utils.dispatch_failure(request, "INTERNAL_SERVER_ERROR")
Beispiel #30
0
    def get(self, request, pk, *args, **kwargs):
        try:
            company = AccountsUtils.get_company (pk)

            query, dates = Utils.validate_query (request, dateformat=True)
            end_date = request.GET.get ('end_date', None)
            split_date = end_date.split ('-')
            report_identifier = split_date[0]+"-"+split_date[1]

            report_date = datetime.datetime.strptime(end_date, "%Y-%m-%d").date()
            report_month = report_date.strftime('%b')
            report_month_fillform = report_date.strftime ('%B')

            if '-' in report_identifier:
                monthly_report = ReportingUtils.get_monthly_report (pk=pk, period=report_identifier)
            else:
                monthly_report = ReportingUtils.get_monthly_report (pk=pk, report_id=report_identifier)

            context = {'request': request,
                       'company': pk,
                       'period': report_identifier}

            # todo: figure out why order_by isn't workin on these query_sets, ordering added to model as workaround
            if not dates:
                print('########### not dates')
                bs_queryset = FinancialStatementEntry.objects.filter (company=company,
                                                                      statement_type=FinancialStatementEntry.BALANCE_SHEET)
                is_queryset = FinancialStatementEntry.objects.filter (company=company,
                                                                      statement_type=FinancialStatementEntry.INCOME_STATEMENT)
            else:
                if dates[0] == 'ERROR':
                    return Utils.dispatch_failure (request, dates[1])
                elif len (dates) == 2:
                    bs_queryset = FinancialStatementEntry.objects.filter (company=company,
                                                                          period_ending__range=(dates[0], dates[1]),
                                                                          statement_type=FinancialStatementEntry.BALANCE_SHEET,
                                                                          )

                    is_queryset = FinancialStatementEntry.objects.filter (company=company,
                                                                          period_ending__range=(dates[0], dates[1]),
                                                                          statement_type=FinancialStatementEntry.INCOME_STATEMENT,
                                                                          )
                elif len (dates) == 1:
                    bs_queryset = FinancialStatementEntry.objects.filter (company=company,
                                                                          period_ending=dates[0],
                                                                          statement_type=FinancialStatementEntry.BALANCE_SHEET,
                                                                          )

                    is_queryset = FinancialStatementEntry.objects.filter (company=company,
                                                                          period_ending=dates[0],
                                                                          statement_type=FinancialStatementEntry.INCOME_STATEMENT,
                                                                          )
                else:
                    bs_queryset = FinancialStatementEntry.objects.filter (company=company,
                                                                          statement_type=FinancialStatementEntry.BALANCE_SHEET)
                    is_queryset = FinancialStatementEntry.objects.filter (company=company,
                                                                          statement_type=FinancialStatementEntry.INCOME_STATEMENT)

            orderlist = {}
            ordered_data = {}
            bslist = {}
            islist = {}
            qalist = {}
            signofflist = {}
            reportlist = {}

            questions = ReportingUtils.get_questionnaire_objects (pk, report_identifier)
            has_answers = ReportingUtils.has_answers_for_period (pk, report_identifier)

            if questions and has_answers:
                # return Q n A for requested period.
                question_and_answer = QuestionWithAnswerSerializer (questions, many=True, context=context)
                if len (question_and_answer.data) > 0:
                    for row in range (0, int (len (question_and_answer.data))):
                        question_text = question_and_answer.data[row]['question_text']
                        answer_data_type = question_and_answer.data[row]['answer_data_type']
                        short_tag = question_and_answer.data[row]['short_tag']
                        answer = question_and_answer.data[row]['answer']['answer']
                        if row is not 0 and qalist[row-1]["answer_data_type"] == "boolean" and answer_data_type == "varchar(255)" or answer_data_type == "varchar(511)":
                            if question_text.split(' ')[1].split(',')[0].lower() != qalist[row - 1]["answer"].lower():
                                answer = None
                        qalist[row] = {"question": question_text, "answer_data_type": answer_data_type, "short_tag": short_tag, "answer":answer, "count": int(len (question_and_answer.data))}
                    ordered_data["QUESTIONNAIRE"] = qalist
            else:
                qalist[0] = {"question": "", "answer_data_type": "","short_tag": "", "answer": "", "count": 0}
                ordered_data["QUESTIONNAIRE"] = qalist
            orderlist['QA'] = ordered_data

            if bs_queryset:
                balance_sheet = FinancialStatementEntrySerializer (bs_queryset, many=True)

                for row in range (0, int (len (balance_sheet.data))):
                    key = balance_sheet.data[row]['fse_tag']["sort_order"]
                    description = balance_sheet.data[row]['fse_tag']['description']
                    value = balance_sheet.data[row]['value']
                    is_total = balance_sheet.data[row]['fse_tag']['is_total_row']
                    bslist[key] = {"description": description, "value": Utils.currencyformat(value), "is_total": is_total}

                ordered_data["BALANCE"] = bslist
            orderlist['BS'] = ordered_data

            if is_queryset:
                income_statement = FinancialStatementEntrySerializer (is_queryset, many=True)
                ordered_data = {"INCOME":{}}
                for row in range (0, int (len (income_statement.data))):
                    key = income_statement.data[row]['fse_tag']["sort_order"]
                    description = income_statement.data[row]['fse_tag']['description']
                    value = float(income_statement.data[row]['value'])
                    is_total = income_statement.data[row]['fse_tag']['is_total_row']
                    islist[key] = {"description": description,"value": Utils.currencyformat(value), "is_total": is_total}

                ordered_data["INCOME"] = islist
            orderlist['IS'] = ordered_data

            if monthly_report:
                monthly_report_serializer = MonthlyReportSerializer (monthly_report, context={'request': request, 'company_id': pk})
                status = monthly_report_serializer.data['status']
                signoff_by_name = monthly_report_serializer.data['signoff_by_name']
                signoff_by_title = monthly_report_serializer.data['signoff_by_title']
                signoff_date = monthly_report_serializer.data['signoff_date']
                signofflist[0] = {"status": status, "signoff_by_name": signoff_by_name, "signoff_by_title": signoff_by_title, 'signoff_date':signoff_date}

                ordered_data["SIGNOFF"] = signofflist
            orderlist['REPORT'] = ordered_data

            reportlist[0] = {"period": report_month+'. '+split_date[0]}
            ordered_data["PERIOD"] = reportlist
            orderlist['REPORT'] = ordered_data

            pdf = AccountingUtils.render_to_pdf('pdf-layout.html', orderlist)
            response = HttpResponse (pdf, content_type='application/pdf')

            if pdf:
                response = HttpResponse (pdf, content_type='application/pdf')
                filename = PREVIOUS_MONTHLY_REPORT_DOWNLOAD_FILE_PREFIX+"%s.pdf" % (report_month_fillform+' '+split_date[0])
                content = "inline; filename='%s'" % (filename)
                download = request.GET.get("download")
                if download:
                    content = "attachment; filename='%s'" % (filename)
                response['Content-Disposition'] = content
                return response
            return Utils.dispatch_success (request, 'DATA_NOT_FOUND')
        except Exception as e:
            print(e)
            return Utils.dispatch_failure (request, 'INTERNAL_SERVER_ERROR')