diff --git a/altinkaya_reports/report/account_invoice_report.py b/altinkaya_reports/report/account_invoice_report.py index 73d319d5..480ff6fd 100644 --- a/altinkaya_reports/report/account_invoice_report.py +++ b/altinkaya_reports/report/account_invoice_report.py @@ -1,5 +1,5 @@ from odoo import models, fields, api, tools - +from datetime import datetime class AccountInvoice(models.Model): _inherit = 'account.invoice' @@ -28,15 +28,17 @@ class AccountInvoiceReport(models.Model): source_id = fields.Many2one('utm.source', string='Marketing Source', readonly=True) campaign_id = fields.Many2one('utm.campaign', string='Marketing Campaign', readonly=True) month_nr = fields.Char('Ay No', readonly=True) + customer_create_date = fields.Date(string='Customer Create Date', readonly=True) + medium_id = fields.Many2one('utm.medium', string='Marketing Medium', readonly=True) def _select(self): return super(AccountInvoiceReport, self)._select() + \ - ", sub.state_id, sub.total_tax as total_tax, sub.price_total_usd as price_total_usd, sub.price_average_usd as price_average_usd, sub.source_id as source_id, sub.campaign_id as campaign_id, sub.month_nr as month_nr" + ", sub.state_id, sub.total_tax as total_tax, sub.price_total_usd as price_total_usd, sub.price_average_usd as price_average_usd, sub.source_id as source_id, sub.campaign_id as campaign_id, sub.month_nr as month_nr, sub.customer_create_date as customer_create_date, sub.medium_id as medium_id" def _sub_select(self): return super(AccountInvoiceReport, self)._sub_select() + \ """, coalesce(partner.state_id, partner_ai.state_id) AS state_id, - partner.source_id,partner.campaign_id, + partner.source_id,partner.campaign_id,partner.medium_id, to_char(ai.date_invoice, 'MM') AS month_nr, SUM(ail.price_subtotal_signed * invoice_type.sign * ai.usd_rate) AS price_total_usd, ail.kdv_amount as total_tax, @@ -47,5 +49,5 @@ def _sub_select(self): END AS price_average_usd""" def _group_by(self): - return super(AccountInvoiceReport, self)._group_by() + ", coalesce(partner.state_id, partner_ai.state_id),partner.source_id,partner.campaign_id,month_nr" + return super(AccountInvoiceReport, self)._group_by() + ", coalesce(partner.state_id, partner_ai.state_id),partner.source_id,partner.campaign_id,partner.medium_id,month_nr" diff --git a/currency_rate_update_tcmb/README.rst b/currency_rate_update_tcmb/README.rst new file mode 100644 index 00000000..edb68034 --- /dev/null +++ b/currency_rate_update_tcmb/README.rst @@ -0,0 +1,75 @@ +============================ +Currency Rate Update: TCMB.gov.tr +============================ + +.. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png + :target: https://odoo-community.org/page/development-status + :alt: Beta +.. |badge2| image:: https://img.shields.io/badge/licence-AGPL--3-blue.png + :target: http://www.gnu.org/licenses/agpl-3.0-standalone.html + :alt: License: AGPL-3 +.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fcurrency-lightgray.png?logo=github + :target: https://github.com/OCA/currency/tree/12.0/currency_rate_update_tcmb + :alt: OCA/currency +.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png + :target: https://translation.odoo-community.org/projects/currency-12-0/currency-12-0-currency_rate_update_tcmb + :alt: Translate me on Weblate +.. |badge5| image:: https://img.shields.io/badge/runbot-Try%20me-875A7B.png + :target: https://runbot.odoo-community.org/runbot/259/12.0 + :alt: Try me on Runbot + +|badge1| |badge2| |badge3| |badge4| |badge5| + +This module adds `TCMB.gov.tr `_ as currency rate provider. Central Bank of the Republic of Turkey. + + +**Table of contents** + +.. contents:: + :local: + + +Bug Tracker +=========== + +Bugs are tracked on `GitHub Issues `_. +In case of trouble, please check there if your issue has already been reported. +If you spotted it first, help us smashing it by providing a detailed and welcomed +`feedback `_. + +Do not contact contributors directly about support or help with technical issues. + +Credits +======= + +Authors +~~~~~~~ + +* Yiğit Budak + +Contributors +~~~~~~~~~~~~ + +* Alexey Pelykh + +Maintainers +~~~~~~~~~~~ + +This module is maintained by the OCA. + +.. image:: https://odoo-community.org/logo.png + :alt: Odoo Community Association + :target: https://odoo-community.org + +OCA, or the Odoo Community Association, is a nonprofit organization whose +mission is to support the collaborative development of Odoo features and +promote its widespread use. + +This module is part of the `OCA/currency `_ project on GitHub. + +You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/currency_rate_update_tcmb/__init__.py b/currency_rate_update_tcmb/__init__.py new file mode 100644 index 00000000..4b76c7b2 --- /dev/null +++ b/currency_rate_update_tcmb/__init__.py @@ -0,0 +1,3 @@ +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). + +from . import models diff --git a/currency_rate_update_tcmb/__manifest__.py b/currency_rate_update_tcmb/__manifest__.py new file mode 100644 index 00000000..4e735180 --- /dev/null +++ b/currency_rate_update_tcmb/__manifest__.py @@ -0,0 +1,23 @@ +# Copyright 2020 Yiğit Budak (https://github.com/yibudak) +# Copyright 2019 Brainbean Apps (https://brainbeanapps.com) +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). + +{ + 'name': 'Currency Rate Update: Turkish Central Bank', + 'version': '12.0.1.0.0', + 'category': 'Financial Management/Configuration', + 'summary': 'Update exchange rates using TCMB.gov.tr', + 'author': + 'Yiğit Budak, ' + 'Odoo Community Association (OCA)', + 'Brainbean Apps' + 'license': 'AGPL-3', + 'installable': True, + 'application': False, + 'depends': [ + 'currency_rate_update', + ], + 'data': [ + 'views/res_currency_rate_provider.xml', + ], +} diff --git a/currency_rate_update_tcmb/models/__init__.py b/currency_rate_update_tcmb/models/__init__.py new file mode 100644 index 00000000..ba3a790b --- /dev/null +++ b/currency_rate_update_tcmb/models/__init__.py @@ -0,0 +1,3 @@ +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). + +from . import res_currency_rate_provider_TCMB diff --git a/currency_rate_update_tcmb/models/res_currency_rate_provider_TCMB.py b/currency_rate_update_tcmb/models/res_currency_rate_provider_TCMB.py new file mode 100644 index 00000000..f8e97685 --- /dev/null +++ b/currency_rate_update_tcmb/models/res_currency_rate_provider_TCMB.py @@ -0,0 +1,140 @@ +# Copyright 2021 Yiğit Budak (https://github.com/yibudak) +# -*- coding: utf-8 -*- + +from datetime import datetime, timedelta, date +from odoo import models, fields, api +from odoo.tools.translate import _ +import logging +from lxml.etree import fromstring +import requests + +_logger = logging.getLogger(__name__) + +TCMB_RATE_TYPES = [ + "ForexBuying", + "ForexSelling", + "BanknoteBuying", + "BanknoteSelling", +] + +class ResCurrencyRateProviderTCMB(models.Model): + _inherit = 'res.currency.rate.provider' + + service = fields.Selection( + selection_add=[('TCMB', 'Central Bank of the Republic of Turkey')], + default="TCMB" + ) + service_rate_type = fields.Selection([ + ('ForexBuying', _('Forex Buy')), + ('ForexSelling', _('Forex Sell')), + ('BanknoteBuying', _('Banknote Buy')), + ('BanknoteSelling', _('Banknote Sell')) + ], string='Service Rate Type', default="ForexBuying", required=True) + + @api.multi + def _get_supported_currencies(self): + self.ensure_one() + if self.service != 'TCMB': + return super()._get_supported_currencies() + + # List of currencies obrained from: + # http://www.tcmb.gov.tr/kurlar/today.xml + return \ + [ + 'USD', 'AUD', 'DKK', 'EUR', 'GBP', 'CHF', 'SEK', 'CAD', + 'KWD', 'NOK', 'SAR', 'JPY', 'BGN', 'RON', 'RUB', 'IRR', + 'CNY', 'PKR', 'QAR', 'XDR', 'TRY' + ] + + @api.multi + def _obtain_rates(self, base_currency, currencies, date_from, date_to): + self.ensure_one() + if self.service != 'TCMB': + return super()._obtain_rates(base_currency, currencies, date_from, date_to) + + invert_calculation = False + if base_currency != 'TRY': + invert_calculation = True + if base_currency not in currencies: + currencies.append(base_currency) + + if 'TRY' in currencies: + currencies.remove('TRY') + + def daterange(start_date, end_date): + for n in range(int((end_date - start_date).days)): + yield start_date + timedelta(n) + + result = {} + if date_from == date_to and date_from == date.today(): + url = 'https://www.tcmb.gov.tr/kurlar/today.xml' + try: + rate_date = date.today().strftime('%Y-%m-%d') + currency_data = self.get_tcmb_currency_data(url, currencies) + result[rate_date] = currency_data + self._action_log_update(rate_date) + except Exception: + _logger.error(_('No currency rate on %s' % (date_from.strftime("%Y-%m-%d")))) + else: + latest_data = None + for single_date in daterange(date_from, date_to): + year = str(single_date.year) + month = '{:02d}'.format(single_date.month) + day = '{:02d}'.format(single_date.day) + url = "https://www.tcmb.gov.tr/kurlar/%s%s/%s%s%s.xml" % (year, month, day, month, year) + try: + rate_date = (single_date + timedelta(days=1)).strftime('%Y-%m-%d') + currency_data = self.get_tcmb_currency_data(url, currencies) + result[rate_date] = currency_data # bir gun oncesinin kurunu al + latest_data = currency_data + self._action_log_update(rate_date) + except Exception: + _logger.error(_('No currency rate on %s' % (single_date.strftime("%Y-%m-%d")))) + rate_date = (single_date + timedelta(days=1)).strftime('%Y-%m-%d') + if latest_data: + result[rate_date] = latest_data # bir gun oncesinin kurunu al + self._action_log_update(rate_date) + continue + + content = result + if invert_calculation: + for k in content.keys(): + base_rate = float(content[k][base_currency]) + for rate in content[k].keys(): + content[k][rate] = str(float(content[k][rate]) / base_rate) + content[k]['TRY'] = str(1.0 / base_rate) + return content + + def rate_retrieve(self, dom, currency, rate_type): + res = {} + xpath_currency_rate = "./Currency[@Kod='%s']/%s" % (currency.upper(), rate_type) + res['rate_currency'] = float( + dom.findall(xpath_currency_rate)[0].text + ) + xpath_rate_ref = "./Currency[@Kod='%s']/Unit" % currency.upper() + res['rate_ref'] = float(dom.findall(xpath_rate_ref)[0].text) + return res + + def get_tcmb_currency_data(self, url, currencies): + response = requests.get(url).text + dom = fromstring(response.encode('utf-8')) + + _logger.debug("TCMB sent a valid XML file") + currency_data = {} + for currency in currencies: + currency_data[currency] = {} + for rate_type in TCMB_RATE_TYPES: + curr_data = self.rate_retrieve(dom, currency, rate_type) + currency_data[currency][rate_type] = curr_data['rate_ref'] / (curr_data['rate_currency'] or 1.0) + + return currency_data + + def _action_log_update(self, rate_date): + self.env['mail.message'].create({ + 'email_from': 'Administrator', + 'author_id': 2, + 'model': 'res.currency.rate.provider', + 'subtype_id': self.env.ref('mail.mt_comment').id, + 'body': "%s Currency Rate Updated at %s" % (rate_date, datetime.now()), + 'res_id': self.id, + }) diff --git a/currency_rate_update_tcmb/readme/CONTRIBUTORS.rst b/currency_rate_update_tcmb/readme/CONTRIBUTORS.rst new file mode 100644 index 00000000..619b321e --- /dev/null +++ b/currency_rate_update_tcmb/readme/CONTRIBUTORS.rst @@ -0,0 +1,2 @@ +* Yiğit Budak +* Alexey Pelykh diff --git a/currency_rate_update_tcmb/readme/DESCRIPTION.rst b/currency_rate_update_tcmb/readme/DESCRIPTION.rst new file mode 100644 index 00000000..3b4efa67 --- /dev/null +++ b/currency_rate_update_tcmb/readme/DESCRIPTION.rst @@ -0,0 +1,4 @@ +This module adds `TCMB.gov.tr `_ as currency rate provider. Central Bank of the Republic of Turkey. + + + diff --git a/currency_rate_update_tcmb/static/description/icon.png b/currency_rate_update_tcmb/static/description/icon.png new file mode 100644 index 00000000..3a0328b5 Binary files /dev/null and b/currency_rate_update_tcmb/static/description/icon.png differ diff --git a/currency_rate_update_tcmb/static/description/index.html b/currency_rate_update_tcmb/static/description/index.html new file mode 100644 index 00000000..fd3c0f83 --- /dev/null +++ b/currency_rate_update_tcmb/static/description/index.html @@ -0,0 +1,420 @@ + + + + + + +Currency Rate Update: TCMB.gov.tr + + + +
+

Currency Rate Update: TCMB.gov.tr

+ + +

Beta License: AGPL-3 OCA/currency Translate me on Weblate Try me on Runbot

+

This module adds TCMB.gov.tr as currency rate provider. Central Bank of the Republic of Turkey.

+

Table of contents

+ +
+

Bug Tracker

+

Bugs are tracked on GitHub Issues. +In case of trouble, please check there if your issue has already been reported. +If you spotted it first, help us smashing it by providing a detailed and welcomed +feedback.

+

Do not contact contributors directly about support or help with technical issues.

+
+
+

Credits

+
+

Authors

+
    +
  • Yiğit Budak
  • +
+
+ +
+

Maintainers

+

This module is maintained by the OCA.

+Odoo Community Association +

OCA, or the Odoo Community Association, is a nonprofit organization whose +mission is to support the collaborative development of Odoo features and +promote its widespread use.

+

This module is part of the OCA/currency project on GitHub.

+

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.

+
+
+
+ + diff --git a/currency_rate_update_tcmb/views/res_currency_rate_provider.xml b/currency_rate_update_tcmb/views/res_currency_rate_provider.xml new file mode 100644 index 00000000..a23021f4 --- /dev/null +++ b/currency_rate_update_tcmb/views/res_currency_rate_provider.xml @@ -0,0 +1,17 @@ + + + + + res.currency.rate.provider.form.tcmb + res.currency.rate.provider + + + + + + + + + diff --git a/short_url_yourls/models/short_url_yourls.py b/short_url_yourls/models/short_url_yourls.py index 6fada65a..45c38f77 100644 --- a/short_url_yourls/models/short_url_yourls.py +++ b/short_url_yourls/models/short_url_yourls.py @@ -75,6 +75,10 @@ def shorten_url(self, url): "format": "json", } + exist_shortened_url = line_obj.search([("long_url", "=", url)], limit=1) + if exist_shortened_url: + return exist_shortened_url.short_url + retries = 0 # Will attempt to get a response from the server 3 times, if fails then returns False is_shortened = False response = False