diff --git a/ledger/payments/api.py b/ledger/payments/api.py index 7e585179a..8a3865d2a 100755 --- a/ledger/payments/api.py +++ b/ledger/payments/api.py @@ -18,7 +18,9 @@ from ledger.payments.bpoint.models import BpointTransaction, BpointToken from ledger.payments.cash.models import CashTransaction, Region, District, DISTRICT_CHOICES, REGION_CHOICES from ledger.payments.models import TrackRefund, LinkedInvoice, OracleAccountCode, RefundFailed, OracleInterfaceSystem, PaymentTotal +from ledger.payments import models as payment_models from ledger.payments.utils import systemid_check, update_payments, ledger_payment_invoice_calulations +from ledger.payments import utils as payments_utils from ledger.payments.invoice import utils as invoice_utils from ledger.payments.facade import bpoint_facade from ledger.payments.reports import generate_items_csv, generate_trans_csv, generate_items_csv_allocated @@ -768,40 +770,49 @@ class ReportCreateView(views.APIView): def get(self,request,format=None): try: http_status = status.HTTP_200_OK - #parse and validate data - report = None - data = { - "start":request.GET.get('start'), - "end":request.GET.get('end'), - "banked_start":request.GET.get('banked_start',None), - "banked_end":request.GET.get('banked_end',None), - "system":request.GET.get('system'), - "items": request.GET.get('items', False), - "region": request.GET.get('region'), - "district": request.GET.get('district') - } - serializer = ReportSerializer(data=data) - serializer.is_valid(raise_exception=True) - filename = 'report-{}-{}'.format(str(serializer.validated_data['start']),str(serializer.validated_data['end'])) - # Generate Report - if serializer.validated_data['items']: - report = generate_items_csv(systemid_check(serializer.validated_data['system']), - serializer.validated_data['start'], - serializer.validated_data['end'], - serializer.validated_data['banked_start'], - serializer.validated_data['banked_end'], - district = serializer.validated_data['district']) - else: - report = generate_trans_csv(systemid_check(serializer.validated_data['system']) - ,serializer.validated_data['start'], - serializer.validated_data['end'], - district = serializer.validated_data['district']) - if report: - response = HttpResponse(FileWrapper(report), content_type='text/csv') - response['Content-Disposition'] = 'attachment; filename="{}.csv"'.format(filename) - return response - else: - raise serializers.ValidationError('No report was generated.') + system = request.GET.get('system') + + ois = payment_models.OracleInterfaceSystem.objects.filter(system_id=system) + if ois.count() > 0: + isp = payments_utils.get_oracle_interface_system_permissions(system,request.user.email) + if isp["reports_access"] is True or isp["all_access"] is True: + + #parse and validate data + report = None + data = { + "start":request.GET.get('start'), + "end":request.GET.get('end'), + "banked_start":request.GET.get('banked_start',None), + "banked_end":request.GET.get('banked_end',None), + "system":request.GET.get('system'), + "items": request.GET.get('items', False), + "region": request.GET.get('region'), + "district": request.GET.get('district') + } + serializer = ReportSerializer(data=data) + serializer.is_valid(raise_exception=True) + filename = 'report-{}-{}'.format(str(serializer.validated_data['start']),str(serializer.validated_data['end'])) + # Generate Report + if serializer.validated_data['items']: + report = generate_items_csv(systemid_check(serializer.validated_data['system']), + serializer.validated_data['start'], + serializer.validated_data['end'], + serializer.validated_data['banked_start'], + serializer.validated_data['banked_end'], + district = serializer.validated_data['district']) + else: + report = generate_trans_csv(systemid_check(serializer.validated_data['system']) + ,serializer.validated_data['start'], + serializer.validated_data['end'], + district = serializer.validated_data['district']) + if report: + response = HttpResponse(FileWrapper(report), content_type='text/csv') + response['Content-Disposition'] = 'attachment; filename="{}.csv"'.format(filename) + return response + else: + raise serializers.ValidationError('No report was generated.') + else: + raise serializers.ValidationError('Forbidden Access.') except serializers.ValidationError: raise except Exception as e: @@ -815,39 +826,49 @@ def get(self,request,format=None): try: http_status = status.HTTP_200_OK #parse and validate data - report = None - data = { - "start":request.GET.get('start'), - "end":request.GET.get('end'), - "banked_start":request.GET.get('banked_start',None), - "banked_end":request.GET.get('banked_end',None), - "system":request.GET.get('system'), - "items": request.GET.get('items', False), - "region": request.GET.get('region'), - "district": request.GET.get('district') - } - serializer = ReportSerializer(data=data) - serializer.is_valid(raise_exception=True) - filename = 'report-{}-{}'.format(str(serializer.validated_data['start']),str(serializer.validated_data['end'])) - # Generate Report - if serializer.validated_data['items']: - report = generate_items_csv_allocated(systemid_check(serializer.validated_data['system']), - serializer.validated_data['start'], - serializer.validated_data['end'], - serializer.validated_data['banked_start'], - serializer.validated_data['banked_end'], - district = serializer.validated_data['district']) - else: - report = generate_trans_csv(systemid_check(serializer.validated_data['system']) - ,serializer.validated_data['start'], - serializer.validated_data['end'], - district = serializer.validated_data['district']) - if report: - response = HttpResponse(FileWrapper(report), content_type='text/csv') - response['Content-Disposition'] = 'attachment; filename="{}.csv"'.format(filename) - return response - else: - raise serializers.ValidationError('No report was generated.') + system = request.GET.get('system') + + ois = payment_models.OracleInterfaceSystem.objects.filter(system_id=system) + if ois.count() > 0: + isp = payments_utils.get_oracle_interface_system_permissions(system,request.user.email) + if isp["reports_access"] is True or isp["all_access"] is True: + + report = None + data = { + "start":request.GET.get('start'), + "end":request.GET.get('end'), + "banked_start":request.GET.get('banked_start',None), + "banked_end":request.GET.get('banked_end',None), + "system":request.GET.get('system'), + "items": request.GET.get('items', False), + "region": request.GET.get('region'), + "district": request.GET.get('district') + } + serializer = ReportSerializer(data=data) + serializer.is_valid(raise_exception=True) + filename = 'report-{}-{}'.format(str(serializer.validated_data['start']),str(serializer.validated_data['end'])) + # Generate Report + if serializer.validated_data['items']: + report = generate_items_csv_allocated(systemid_check(serializer.validated_data['system']), + serializer.validated_data['start'], + serializer.validated_data['end'], + serializer.validated_data['banked_start'], + serializer.validated_data['banked_end'], + district = serializer.validated_data['district']) + else: + report = generate_trans_csv(systemid_check(serializer.validated_data['system']) + ,serializer.validated_data['start'], + serializer.validated_data['end'], + district = serializer.validated_data['district']) + if report: + response = HttpResponse(FileWrapper(report), content_type='text/csv') + response['Content-Disposition'] = 'attachment; filename="{}.csv"'.format(filename) + return response + else: + raise serializers.ValidationError('No report was generated.') + else: + raise serializers.ValidationError('Forbidden Access.') + except serializers.ValidationError: raise except Exception as e: diff --git a/ledger/payments/models.py b/ledger/payments/models.py index fc989e562..8dfc41976 100755 --- a/ledger/payments/models.py +++ b/ledger/payments/models.py @@ -118,6 +118,7 @@ class OracleInterfacePermission(models.Model): ('view_ledger_tools', 'View Ledger Payment Tools'), ('manage_ledger_tool', 'Manage Ledger Payment Tools'), ('view_payment_totals', 'View Payment Totals'), + ('reports_access', 'Reports Access') ) system = models.ForeignKey(OracleInterfaceSystem,related_name='oracle_interface_permission_recipients') diff --git a/ledger/payments/utils.py b/ledger/payments/utils.py index bcbc57e4c..1cbcac588 100755 --- a/ledger/payments/utils.py +++ b/ledger/payments/utils.py @@ -1160,6 +1160,7 @@ def get_oracle_interface_system_permissions(system_id, email): 'view_ledger_tools' : False, 'manage_ledger_tool' : False, 'view_payment_totals' : False, + 'reports_access' : False, } ois = OracleInterfaceSystem.objects.filter(system_id=system_id) @@ -1174,6 +1175,8 @@ def get_oracle_interface_system_permissions(system_id, email): system_interface_permssions['manage_ledger_tool'] = True if oisp.access_type == 'view_payment_totals': system_interface_permssions['view_payment_totals'] = True + if oisp.access_type == 'reports_access': + system_interface_permssions['reports_access'] = True return system_interface_permssions diff --git a/ledgergw/api.py b/ledgergw/api.py index 06c3a29fd..2409507e7 100644 --- a/ledgergw/api.py +++ b/ledgergw/api.py @@ -1681,15 +1681,12 @@ def process_refund(request,apikey): response = HttpResponse(json.dumps(jsondata), content_type='application/json') return response - - def ip_check(request): ledger_json = {} ipaddress = ledgerapi_utils.get_client_ip(request) jsondata = {'status': 200, 'ipaddress': str(ipaddress)} return HttpResponse(json.dumps(jsondata), content_type='application/json') - class SettlementReportView(views.APIView): renderer_classes = (JSONRenderer,) @@ -1698,26 +1695,67 @@ def get(self, request, format=None): http_status = status.HTTP_200_OK # parse and validate data system = request.GET.get('system') - report = None - data = { - "date": request.GET.get('date'), - } - serializer = SettlementReportSerializer(data=data) - serializer.is_valid(raise_exception=True) - filename = 'Settlement Report-{}'.format(str(serializer.validated_data['date'])) - # Generate Report - report = reports.booking_bpoint_settlement_report(serializer.validated_data['date'],system) - if report: - response = HttpResponse(FileWrapper(report), content_type='text/csv') - response['Content-Disposition'] = 'attachment; filename="{}.csv"'.format(filename) - return response - else: - raise serializers.ValidationError('No report was generated.') + + ois = payment_models.OracleInterfaceSystem.objects.filter(system_id=system) + if ois.count() > 0: + isp = payments_utils.get_oracle_interface_system_permissions(system,self.request.user.email) + if isp["reports_access"] is True or isp["all_access"] is True: + report = None + data = { + "date": request.GET.get('date'), + } + serializer = SettlementReportSerializer(data=data) + serializer.is_valid(raise_exception=True) + filename = 'Settlement Report-{}'.format(str(serializer.validated_data['date'])) + # Generate Report + report = reports.booking_bpoint_settlement_report(serializer.validated_data['date'],system) + if report: + response = HttpResponse(FileWrapper(report), content_type='text/csv') + response['Content-Disposition'] = 'attachment; filename="{}.csv"'.format(filename) + return response + else: + raise serializers.ValidationError('No report was generated.') + else: + raise serializers.ValidationError('Forbidden Access.') except serializers.ValidationError: raise except Exception as e: traceback.print_exc() +class ItemisedTransactionReportView(views.APIView): + renderer_classes = (JSONRenderer,) + + def get(self, request, format=None): + try: + http_status = status.HTTP_200_OK + # parse and validate data + system = request.GET.get('system') + ois = payment_models.OracleInterfaceSystem.objects.filter(system_id=system) + if ois.count() > 0: + isp = payments_utils.get_oracle_interface_system_permissions(system,self.request.user.email) + if isp["reports_access"] is True or isp["all_access"] is True: + + report = None + data = { + "date": request.GET.get('date'), + } + serializer = SettlementReportSerializer(data=data) + serializer.is_valid(raise_exception=True) + filename = 'Itemised-Transaction-Report-{}'.format(str(serializer.validated_data['date'])) + # Generate Report + report = reports.itemised_transaction_report(serializer.validated_data['date'],system) + if report: + response = HttpResponse(FileWrapper(report), content_type='text/csv') + response['Content-Disposition'] = 'attachment; filename="{}.csv"'.format(filename) + return response + else: + raise serializers.ValidationError('No report was generated.') + else: + raise serializers.ValidationError('Forbidden Access.') + except serializers.ValidationError: + raise + except Exception as e: + traceback.print_exc() class RefundsReportView(views.APIView): renderer_classes = (JSONRenderer,) @@ -1728,21 +1766,29 @@ def get(self, request, format=None): # parse and validate data report = None system = request.GET.get('system') - data = { - "start": request.GET.get('start'), - "end": request.GET.get('end'), - } - serializer = ReportSerializer(data=data) - serializer.is_valid(raise_exception=True) - filename = 'Refunds Report-{}-{}'.format(str(serializer.validated_data['start']), str(serializer.validated_data['end'])) - # Generate Report - report = reports.booking_refunds(serializer.validated_data['start'], serializer.validated_data['end'],system) - if report: - response = HttpResponse(FileWrapper(report), content_type='text/csv') - response['Content-Disposition'] = 'attachment; filename="{}.csv"'.format(filename) - return response - else: - raise serializers.ValidationError('No report was generated.') + + ois = payment_models.OracleInterfaceSystem.objects.filter(system_id=system) + if ois.count() > 0: + isp = payments_utils.get_oracle_interface_system_permissions(system,self.request.user.email) + if isp["reports_access"] is True or isp["all_access"] is True: + + data = { + "start": request.GET.get('start'), + "end": request.GET.get('end'), + } + serializer = ReportSerializer(data=data) + serializer.is_valid(raise_exception=True) + filename = 'Refunds Report-{}-{}'.format(str(serializer.validated_data['start']), str(serializer.validated_data['end'])) + # Generate Report + report = reports.booking_refunds(serializer.validated_data['start'], serializer.validated_data['end'],system) + if report: + response = HttpResponse(FileWrapper(report), content_type='text/csv') + response['Content-Disposition'] = 'attachment; filename="{}.csv"'.format(filename) + return response + else: + raise serializers.ValidationError('No report was generated.') + else: + raise serializers.ValidationError('Forbidden Access.') except serializers.ValidationError: raise except Exception as e: @@ -1756,17 +1802,24 @@ class OracleJob(views.APIView): def get(self, request, format=None): try: system = request.GET.get('system') - ois = payment_models.OracleInterfaceSystem.objects.filter(integration_type='bpoint_api', enabled=True, system_id=system) - data = { - "date": request.GET.get("date"), - "override": request.GET.get("override") - } - - serializer = OracleSerializer(data=data) - serializer.is_valid(raise_exception=True) - ledgergw_utils.oracle_integration(serializer.validated_data['date'].strftime('%Y-%m-%d'), serializer.validated_data['override'], system, ois[0].system_name) - data = {'successful': True} - return Response(data) + ois = payment_models.OracleInterfaceSystem.objects.filter(system_id=system) + if ois.count() > 0: + isp = payments_utils.get_oracle_interface_system_permissions(system,request.user.email) + if isp["reports_access"] is True or isp["all_access"] is True: + + ois = payment_models.OracleInterfaceSystem.objects.filter(integration_type='bpoint_api', enabled=True, system_id=system) + data = { + "date": request.GET.get("date"), + "override": request.GET.get("override") + } + + serializer = OracleSerializer(data=data) + serializer.is_valid(raise_exception=True) + ledgergw_utils.oracle_integration(serializer.validated_data['date'].strftime('%Y-%m-%d'), serializer.validated_data['override'], system, ois[0].system_name) + data = {'successful': True} + return Response(data) + else: + raise serializers.ValidationError('Forbidden Access.') except serializers.ValidationError: print(traceback.print_exc()) raise @@ -2197,9 +2250,9 @@ def update_ledger_oracle_invoice(request,apikey): ledger_user_json = {} print ('update_ledger_oracle_invoice') if ledgerapi_models.API.objects.filter(api_key=apikey,active=1).count(): - print ("YES 1") + if ledgerapi_utils.api_allow(ledgerapi_utils.get_client_ip(request),apikey) is True: - print ("YES 2") + ois_obj = {} org_array = [] data = json.loads(request.POST.get('data', "{}")) diff --git a/ledgergw/reports.py b/ledgergw/reports.py index 2bd2fc3c1..f263c5ec2 100644 --- a/ledgergw/reports.py +++ b/ledgergw/reports.py @@ -10,7 +10,7 @@ from datetime import timedelta from django.db.models import Q, Min #from oscar.apps.order.models import Order -from ledger.order.models import Order +from ledger.order.models import Order, Line as OrderLine def outstanding_bookings(): @@ -433,6 +433,37 @@ def booking_bpoint_settlement_report(_date,system): except: raise + +def itemised_transaction_report(_date,system): + try: + invoices = Invoice.objects.filter(settlement_date=_date,reference__startswith=system) + + strIO = StringIO() + fieldnames = ['Invoice Number','Order Number','Oracle code','Description','Settlement Date','Quantity','Tax Incl','Tax Excl'] + + writer = csv.writer(strIO) + writer.writerow(fieldnames) + for i in invoices: + print (i.reference) + try: + # invoice = Invoice.objects.get(reference=b.crn1) + order_obj = Order.objects.filter(number=i.order_number) + order_lines = OrderLine.objects.filter(order=order_obj[0]) + for ol in order_lines: + writer.writerow([i.reference,i.order_number,ol.oracle_code,ol.title, i.settlement_date.strftime('%d/%m/%Y'),ol.quantity, ol.unit_price_incl_tax, ol.unit_price_excl_tax]) + except Invoice.DoesNotExist: + pass + except Exception as e: + print ("ERROR: Generate Itemisation Transaction Report") + print (e) + + strIO.flush() + strIO.seek(0) + return strIO + except: + raise + + def bookings_report(_date): try: bpoint, cash = [], [] diff --git a/ledgergw/templates/ledgergw/web/reports.html b/ledgergw/templates/ledgergw/web/reports.html index 3bedcb2ad..5eda34423 100644 --- a/ledgergw/templates/ledgergw/web/reports.html +++ b/ledgergw/templates/ledgergw/web/reports.html @@ -258,8 +258,8 @@