def _process_cfs_refund(cls, invoice: InvoiceModel): """Process refund in CFS.""" if invoice.payment_method_code in ([ PaymentMethod.DIRECT_PAY.value, PaymentMethod.DRAWDOWN.value ]): cls._publish_to_mailer(invoice) payment: PaymentModel = PaymentModel.find_payment_for_invoice( invoice.id) payment.payment_status_code = PaymentStatus.REFUNDED.value payment.flush() else: # Create credit memo in CFS if the invoice status is PAID. # Don't do anything is the status is APPROVED. if invoice.invoice_status_code == InvoiceStatus.APPROVED.value: return cfs_account: CfsAccountModel = CfsAccountModel.find_effective_by_account_id( invoice.payment_account_id) line_items: List[PaymentLineItemModel] = [] for line_item in invoice.payment_line_items: line_items.append(PaymentLineItemModel.find_by_id( line_item.id)) cms_response = CFSService.create_cms(line_items=line_items, cfs_account=cfs_account) # TODO Create a payment record for this to show up on transactions, when the ticket comes. # Create a credit with CM identifier as CMs are not reported in payment interface file # until invoice is applied. CreditModel(cfs_identifier=cms_response.get('credit_memo_number'), is_credit_memo=True, amount=invoice.total, remaining_amount=invoice.total, account_id=invoice.payment_account_id).save()
def _process_cfs_refund(cls, invoice: InvoiceModel): """Process refund in CFS.""" if invoice.payment_method_code == PaymentMethod.DIRECT_PAY.value: cls._publish_to_mailer(invoice) payment: PaymentModel = PaymentModel.find_payment_for_invoice( invoice.id) payment.payment_status_code = PaymentStatus.REFUNDED.value payment.flush() else: # Create credit memo in CFS. # TODO Refactor this when actual task is done. This is just a quick fix for CFS UAT - Dec 2020 cfs_account: CfsAccountModel = CfsAccountModel.find_effective_by_account_id( invoice.payment_account_id) line_items: List[PaymentLineItemModel] = [] for line_item in invoice.payment_line_items: line_items.append(PaymentLineItemModel.find_by_id( line_item.id)) cms_response = CFSService.create_cms(line_items=line_items, cfs_account=cfs_account) # TODO Create a payment record for this to show up on transactions, when the ticket comes. # Create a credit with CM identifier as CMs are not reported in payment interface file # until invoice is applied. CreditModel(cfs_identifier=cms_response.get('credit_memo_number'), is_credit_memo=True, amount=invoice.total, remaining_amount=invoice.total, account_id=invoice.payment_account_id).save()
def _refund_and_create_credit_memo(invoice: InvoiceModel): # Create credit memo in CFS if the invoice status is PAID. # Don't do anything is the status is APPROVED. current_app.logger.info( f'Creating credit memo for invoice : {invoice.id}, {invoice.invoice_status_code}' ) if invoice.invoice_status_code == InvoiceStatus.APPROVED.value \ and InvoiceReferenceModel.find_reference_by_invoice_id_and_status( invoice.id, InvoiceReferenceStatus.ACTIVE.value) is None: return cfs_account: CfsAccountModel = CfsAccountModel.find_effective_by_account_id( invoice.payment_account_id) line_items: List[PaymentLineItemModel] = [] for line_item in invoice.payment_line_items: line_items.append(PaymentLineItemModel.find_by_id(line_item.id)) cms_response = CFSService.create_cms(line_items=line_items, cfs_account=cfs_account) # TODO Create a payment record for this to show up on transactions, when the ticket comes. # Create a credit with CM identifier as CMs are not reported in payment interface file # until invoice is applied. CreditModel(cfs_identifier=cms_response.get('credit_memo_number'), is_credit_memo=True, amount=invoice.total, remaining_amount=invoice.total, account_id=invoice.payment_account_id).flush() # Add up the credit amount and update payment account table. payment_account: PaymentAccountModel = PaymentAccountModel.find_by_id( invoice.payment_account_id) payment_account.credit = (payment_account.credit or 0) + invoice.total current_app.logger.info( f'Updating credit amount to {payment_account.credit} for account {payment_account.auth_account_id}' ) payment_account.flush()