def find_code_value_by_type_and_code( cls, code_type: str, code: str ): """Find code values by code type and code.""" current_app.logger.debug(f'<find_code_value_by_type_and_code : {code_type} - {code}') code_response = {} if cache.get(code_type): filtered_codes = [cd for cd in cache.get(code_type) if cd.get('type') == code or cd.get('code') == code] if filtered_codes: code_response = filtered_codes[0] else: if code_type == CodeValue.ERROR.value: codes_model = ErrorCode.find_by_code(code) error_schema = ErrorCodeSchema() code_response = error_schema.dump(codes_model, many=False) elif code_type == CodeValue.INVOICE_STATUS.value: codes_model = InvoiceStatusCode.find_by_code(code) schema = InvoiceStatusCodeSchema() code_response = schema.dump(codes_model, many=False) elif code_type == CodeValue.CORP_TYPE.value: codes_model = CorpType.find_by_code(code) schema = CorpTypeSchema() code_response = schema.dump(codes_model, many=False) current_app.logger.debug('>find_code_value_by_type_and_code') return code_response
def _get_fee_code(self, corp_type: str, is_staff: bool = False): # pylint: disable=no-self-use """Return BCOL fee code.""" corp_type = CorpType.find_by_code(code=corp_type) return corp_type.bcol_staff_fee_code if is_staff else corp_type.bcol_fee_code
def create_invoice( self, payment_account: PaymentAccount, # pylint: disable=too-many-locals line_items: [PaymentLineItem], invoice: Invoice, **kwargs): """Create Invoice in PayBC.""" current_app.logger.debug('<create_invoice') now = datetime.datetime.now() curr_time = now.strftime('%Y-%m-%dT%H:%M:%SZ') corp_type: CorpTypeModel = CorpTypeModel.find_by_code( kwargs.get('corp_type_code')) invoice_number: str = kwargs.get('invoice_number', None) if invoice_number is None: invoice_number = invoice.id invoice_url = current_app.config.get('PAYBC_BASE_URL') + '/cfs/parties/{}/accs/{}/sites/{}/invs/' \ .format(payment_account.paybc_party, payment_account.paybc_account, payment_account.paybc_site) # Check if random invoice number needs to be generated transaction_num_suffix = secrets.token_hex(10) \ if current_app.config.get('GENERATE_RANDOM_INVOICE_NUMBER', 'False').lower() == 'true' \ else payment_account.corp_number transaction_number = f'{invoice_number}-{transaction_num_suffix}'.replace( ' ', '') invoice = dict(batch_source=PAYBC_BATCH_SOURCE, cust_trx_type=PAYBC_CUST_TRX_TYPE, transaction_date=curr_time, transaction_number=transaction_number[:20], gl_date=curr_time, term_name=PAYBC_TERM_NAME, comments='', lines=[]) index: int = 0 for line_item in line_items: index = index + 1 invoice['lines'].append({ 'line_number': index, 'line_type': PAYBC_LINE_TYPE, 'memo_line_name': corp_type.gl_memo, 'description': line_item.description, 'attribute1': line_item.description, 'unit_price': line_item.total, 'quantity': 1 }) # If there is a service fees add a new line for the service fees. if corp_type.service_fee is not None and corp_type.service_fee.amount > 0: invoice['lines'].append({ 'line_number': index + 1, 'line_type': PAYBC_LINE_TYPE, 'memo_line_name': corp_type.service_gl_memo, 'description': 'Service Fee', 'attribute1': 'Service Fee', 'unit_price': corp_type.service_fee.amount, 'quantity': 1 }) access_token = self.__get_token().json().get('access_token') invoice_response = self.post(invoice_url, access_token, AuthHeaderType.BEARER, ContentType.JSON, invoice) invoice = { 'invoice_number': invoice_response.json().get('invoice_number', None), 'reference_number': invoice_response.json().get('pbc_ref_number', None) } current_app.logger.debug('>create_invoice') return invoice
def _get_fee_code(self, corp_type: str): # pylint: disable=no-self-use """Return BCOL fee code.""" return CorpType.find_by_code(code=corp_type).bcol_fee_code