Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: exchange rate for ccp #706

Closed
wants to merge 7 commits into from
Closed
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 24 additions & 24 deletions apps/quickbooks_online/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -727,15 +727,6 @@ def purchase_object_payload(self, purchase_object, line, payment_type, account_r
'Line': line,
}

# Add exchange rate for foreign currency transactions
if general_settings.is_multi_currency_allowed and purchase_object.currency != qbo_credentials.currency and qbo_credentials.currency:
exchange_rate = self.connection.exchange_rates.get_by_source(source_currency_code=purchase_object.currency)
purchase_object_payload['ExchangeRate'] = exchange_rate['Rate'] if "Rate" in exchange_rate else 1

if isinstance(purchase_object, CreditCardPurchase):
purchase_object.exchange_rate = purchase_object_payload['ExchangeRate']
purchase_object.save(update_fields=['exchange_rate'])

if general_settings.import_tax_codes:
if general_settings.is_tax_override_enabled:
tax_details = self.get_override_tax_details(line)
Expand Down Expand Up @@ -1057,25 +1048,34 @@ def __construct_credit_card_purchase_lineitems(self, credit_card_purchase_lineit

def __construct_credit_card_purchase(self, credit_card_purchase: CreditCardPurchase, credit_card_purchase_lineitems: List[CreditCardPurchaseLineitem]) -> Dict:
"""
Create a credit_card_purchase
:param credit_card_purchase: credit_card_purchase object extracted from database
:return: constructed credit_card_purchase
Create a credit card purchase
:param credit_card_purchase: credit card purchase object extracted from database
:return: constructed credit card purchase
"""
general_mappings = GeneralMapping.objects.filter(workspace_id=self.workspace_id).first()

line = self.__construct_credit_card_purchase_lineitems(credit_card_purchase_lineitems, general_mappings)
credit = False
general_settings = WorkspaceGeneralSettings.objects.filter(workspace_id=self.workspace_id).first()
qbo_home_currency = QBOCredential.objects.get(workspace_id=self.workspace_id).currency
fyle_home_currency = credit_card_purchase.currency

credit_card_purchase_payload = {
'DocNumber': credit_card_purchase_lineitems[0].expense.expense_number,
'PaymentType': 'CreditCard',
'AccountRef': {'value': credit_card_purchase.ccc_account_id},
'EntityRef': {'value': credit_card_purchase.entity_id},
'DepartmentRef': {'value': credit_card_purchase.department_id},
'TxnDate': credit_card_purchase.transaction_date,
'CurrencyRef': {'value': credit_card_purchase.currency},
'PrivateNote': credit_card_purchase.private_note,
'Credit': False,
'Line': self.__construct_credit_card_purchase_lineitems(credit_card_purchase_lineitems)
anishfyle marked this conversation as resolved.
Show resolved Hide resolved
}

for i in range(len(line)):
if line[i]['Amount'] < 0:
credit = True
tax_amount = line[i][credit_card_purchase_lineitems[i].detail_type]['TaxAmount']
line[i]['Amount'] = abs(line[i]['Amount'])
line[i][credit_card_purchase_lineitems[i].detail_type]['TaxAmount'] = abs(tax_amount) if tax_amount else None
if general_settings.is_multi_currency_allowed and fyle_home_currency != qbo_home_currency and qbo_home_currency:
exchange_rate = self.connection.exchange_rates.get_by_source(source_currency_code=fyle_home_currency)
credit_card_purchase_payload['ExchangeRate'] = exchange_rate['Rate'] if "Rate" in exchange_rate else 1

credit_card_purchase_payload = self.purchase_object_payload(credit_card_purchase, line, account_ref=credit_card_purchase.ccc_account_id, payment_type='CreditCard', doc_number=credit_card_purchase.credit_card_purchase_number, credit=credit)
credit_card_purchase.exchange_rate = credit_card_purchase_payload['ExchangeRate']
credit_card_purchase.save(update_fields=['exchange_rate'])

logger.info("| Payload for Credit Card Purchase creation | Content: {{WORKSPACE_ID: {} EXPENSE_GROUP_ID: {} CREDIT_CARD_PURCHASE_PAYLOAD: {}}}".format(self.workspace_id, credit_card_purchase.expense_group.id, credit_card_purchase_payload))
return credit_card_purchase_payload

def post_credit_card_purchase(self, credit_card_purchase: CreditCardPurchase, credit_card_purchase_lineitems: List[CreditCardPurchaseLineitem]):
Expand Down
Loading