feat(currency): Integrate official TCMB daily currency rates

This commit is contained in:
cerenX9
2026-07-22 13:26:20 +03:00
parent c0934dcd76
commit b164e9d5f3
5 changed files with 84 additions and 0 deletions

View File

@@ -10,6 +10,7 @@
'data': [
'security/security_groups.xml',
'security/ir.model.access.csv',
'data/ir_cron.xml',
'data/cron_jobs.xml',
'views/contract_views.xml',
'views/maintenance_cost_views.xml',

View File

@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<data>
<record id="ir_cron_update_tcmb_rates" model="ir.cron">
<field name="name">TCMB Kur Güncelleme</field>
<field name="model_id" ref="base.model_res_currency"/>
<field name="state">code</field>
<field name="code">model._update_tcmb_rates()</field>
<field name="interval_number">1</field>
<field name="interval_type">days</field>
<field name="active" eval="True"/>
</record>
</data>
</odoo>

View File

@@ -2840,3 +2840,6 @@ msgstr "🚨 INCOMPATIBLE (%(dev)s km Prohibited Personal Use)"
#: code:addons/mymach_fleet_intelligence/controllers/main.py:0
msgid "🚨 UYUMSUZ ROTA (YÖNETİCİYE ALARM BİLDİRİLDİ)"
msgstr "🚨 INCOMPATIBLE ROUTE (ALARM REPORTED TO MANAGER)"
msgid "TCMB Kur G<>ncelleme"
msgstr "TCMB Currency Update"

View File

@@ -9,3 +9,4 @@ from . import fleet_fine
from . import fleet_handover
from . import periodic_maintenance
from . import fuel_index
from . import res_currency

View File

@@ -0,0 +1,65 @@
from odoo import models, fields, api
import requests
import xml.etree.ElementTree as ET
import logging
_logger = logging.getLogger(__name__)
class ResCurrency(models.Model):
_inherit = 'res.currency'
@api.model
def _update_tcmb_rates(self):
url = "https://www.tcmb.gov.tr/kurlar/today.xml"
try:
response = requests.get(url, timeout=10)
response.raise_for_status()
tree = ET.fromstring(response.content)
company = self.env.company
today = fields.Date.today()
# Create a dict of rates from XML
rates = {}
for currency in tree.findall('Currency'):
code = currency.get('CurrencyCode')
buying = currency.find('ForexBuying')
if code and buying is not None and buying.text:
try:
rates[code] = float(buying.text)
except ValueError:
continue
# Supported codes
target_codes = ['USD', 'EUR', 'GBP', 'CHF']
for code in target_codes:
if code in rates:
# In Odoo, if base is TRY (value=1.0), USD rate = 1 / 33.0
rate_value = 1.0 / rates[code]
curr_id = self.search([('name', '=', code), ('active', 'in', [True, False])], limit=1)
if curr_id:
# Ensure currency is active
if not curr_id.active:
curr_id.write({'active': True})
# Update or create rate
rate_id = self.env['res.currency.rate'].search([
('name', '=', today),
('currency_id', '=', curr_id.id),
('company_id', '=', company.id)
], limit=1)
if rate_id:
rate_id.write({'rate': rate_value})
else:
self.env['res.currency.rate'].create({
'name': today,
'currency_id': curr_id.id,
'rate': rate_value,
'company_id': company.id
})
_logger.info("TCMB rates updated successfully for today.")
except Exception as e:
_logger.error("Failed to update TCMB rates: %s", str(e))