def batch_process(self, request): status_approved = CreditTradeStatus.objects \ .get(status="Approved") credit_trades = CreditTrade.objects.filter( status_id=status_approved.id) for credit_trade in credit_trades: CreditTradeService.approve(credit_trade) return Response(None, status=status.HTTP_200_OK)
def batch_process(self, request): status_approved = CreditTradeStatus.objects \ .get(status="Approved") credit_trades = CreditTrade.objects.filter( status_id=status_approved.id).order_by('id') CreditTradeService.validate_credits(credit_trades) for credit_trade in credit_trades: credit_trade.update_user_id = request.user.id CreditTradeService.approve(credit_trade) return Response( {"message": "Approved Credit Transactions have been processed."}, status=status.HTTP_200_OK)
def approve(self, request, pk=None): """ Marks the Credit Trade as Approved Transfers the Credits Then, marks the Credit Trade as Completed """ credit_trade = self.get_object() credit_trade.trade_effective_date = datetime.date.today() previous_state = credit_trade if credit_trade.compliance_period_id is None: credit_trade.compliance_period_id = \ CreditTradeService.get_compliance_period_id(credit_trade) serializer = self.get_serializer(credit_trade, data=request.data) serializer.is_valid(raise_exception=True) completed_credit_trade = CreditTradeService.approve( credit_trade, request.user ) serializer = self.get_serializer(completed_credit_trade) CreditTradeService.dispatch_notifications(previous_state, completed_credit_trade) return Response(serializer.data, status=status.HTTP_200_OK)
def approve(self, request, pk=None): credit_trade = self.get_object() completed_credit_trade = CreditTradeService.approve(credit_trade) serializer = self.get_serializer(completed_credit_trade) return Response(serializer.data, status=status.HTTP_200_OK)
def batch_process(self, request): """ Call the approve function on multiple Credit Trades """ status_approved = CreditTradeStatus.objects \ .get(status="Recorded") credit_trades = CreditTrade.objects.filter( status_id=status_approved.id).order_by('id') CreditTradeService.validate_credits(credit_trades) for credit_trade in credit_trades: credit_trade.update_user_id = request.user.id CreditTradeService.approve(credit_trade) CreditTradeService.dispatch_notifications(None, credit_trade) return Response( {"message": "Approved credit transactions have been processed."}, status=status.HTTP_200_OK)
def approve(self, request, pk=None): credit_trade = self.get_object() credit_trade.trade_effective_date = datetime.date.today() serializer = self.get_serializer(credit_trade, data=request.data) serializer.is_valid(raise_exception=True) completed_credit_trade = CreditTradeService.approve(credit_trade) serializer = self.get_serializer(completed_credit_trade) return Response(serializer.data, status=status.HTTP_200_OK)
def create_director_transactions(compliance_report, creating_user): """ Validate or Reduce credits when the director accepts a compliance report Always use the snapshot as the basis for calculation, so we don't recompute anything and possibly alter the values :param compliance_report: :return: """ previous_transactions = [] current = compliance_report while current.supplements is not None: current = current.supplements if current.credit_transaction is not None: previous_transactions.append(current.credit_transaction) total_previous_reduction = Decimal(0.0) total_previous_validation = Decimal(0.0) for transaction in previous_transactions: if transaction.type.the_type in ['Credit Validation']: total_previous_validation += transaction.number_of_credits if transaction.type.the_type in ['Credit Reduction']: total_previous_reduction += transaction.number_of_credits if settings.DEVELOPMENT: print('we have {} previous transactions to consider'.format( len(previous_transactions))) print('Total of previous reductions: {}'.format( total_previous_reduction)) print('Total of previous validations: {}'.format( total_previous_validation)) if compliance_report.snapshot is None: raise InvalidStateException() snapshot = compliance_report.snapshot if 'summary' not in snapshot: raise InvalidStateException() if 'lines' not in snapshot['summary']: raise InvalidStateException() lines = snapshot['summary']['lines'] desired_net_credit_balance_change = Decimal(0.0) if Decimal(lines['25']) > Decimal(0): desired_net_credit_balance_change = Decimal(lines['25']) else: if Decimal(lines['25']) < 0 and Decimal(lines['26']) > Decimal(0): desired_net_credit_balance_change = Decimal( lines['26']) * Decimal(-1.0) required_credit_transaction = desired_net_credit_balance_change - \ (total_previous_validation - total_previous_reduction) if settings.DEVELOPMENT: print('line 25 of current report: {}'.format(lines['25'])) print('desired credit balance change: {}'.format( desired_net_credit_balance_change)) print('required transaction to effect change: {}'.format( required_credit_transaction)) if required_credit_transaction > Decimal(0): # do validation for Decimal(lines['25']) credit_transaction = CreditTrade( initiator=Organization.objects.get(id=1), respondent=compliance_report.organization, status=CreditTradeStatus.objects.get(status='Draft'), type=CreditTradeType.objects.get(the_type='Credit Validation'), number_of_credits=required_credit_transaction, compliance_period=compliance_report.compliance_period, create_user=creating_user, update_user=creating_user) credit_transaction.save() credit_transaction.refresh_from_db() CreditTradeService.approve(credit_transaction) compliance_report.credit_transaction = credit_transaction compliance_report.save() CreditTradeService.pvr_notification(None, credit_transaction) else: if required_credit_transaction < Decimal(0): # do_reduction for Decimal(lines['26']) credit_transaction = CreditTrade( initiator=Organization.objects.get(id=1), respondent=compliance_report.organization, status=CreditTradeStatus.objects.get(status='Draft'), type=CreditTradeType.objects.get( the_type='Credit Reduction'), number_of_credits=required_credit_transaction * Decimal(-1.0), compliance_period=compliance_report.compliance_period, create_user=creating_user, update_user=creating_user) credit_transaction.save() credit_transaction.refresh_from_db() CreditTradeService.approve(credit_transaction) compliance_report.credit_transaction = credit_transaction compliance_report.save() CreditTradeService.pvr_notification(None, credit_transaction)