<commit_before># -*- coding: utf-8 -*-
##############################################################################
#
#    Author: <NAME>. Copyright <NAME>
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as
#    published by the Free Software Foundation, either version 3 of the
#    License, or (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

import logging
from datetime import datetime

from openerp.osv import fields, osv, orm
from openerp import SUPERUSER_ID
from openerp import workflow
from openerp.tools.translate import _

import openerp.addons.decimal_precision as dp

_logger = logging.getLogger(__name__)


class AccountWalletCurrencyLine(osv.osv):

    """
    Object which contain the values of the transaction.
    Theses object can be linked to other object than account.wallet.transaction
    """

    _name = 'account.wallet.currency.line'
    _description = 'Currency line'

    def _get_subtotal(self, cr, uid, ids, field_name, arg, context=None):
        #Compute the total value which will be transfer for the specified currency
        res = {}
        for currency in self.browse(cr, uid, ids, context=context):
            model_obj = self.pool.get(currency.model)
            res[currency.id] = 1.0
            if model_obj.search(cr, uid, [('id', '=', currency.res_id)], context=context):
                transaction = model_obj.browse(cr, uid, currency.res_id, context=context)
                res[currency.id] = transaction.quantity * currency.price_unit
        return res

    _columns = {
        'model': fields.char('Related Document Model', size=128, required=True, select=1),
        'res_id': fields.integer('Related Document ID', required=True, select=1),
        'field': fields.char('Field', size=64, required=True, select=1),
        'price_unit': fields.float('Unit price', required=True, digits_compute=dp.get_precision('Product Price')),
        'currency_id': fields.many2one('res.currency', 'Currency', domain=[('wallet_currency', '=', True)],
                                       required=True, select=1),
        'subtotal': fields.function(_get_subtotal, string='Subtotal', digits_compute=dp.get_precision('Account')),
    }

    _defaults = {
        'model': 'account.wallet.transaction',
        'field': 'currency_ids'
    }

    _sql_constraints = [
        ('object_currency', 'unique(model,res_id,field,currency_id)', 'We can only have one currency per record')
    ]


class AccountWalletTransaction(osv.osv):

    """
    Main object used for transferring currencies, from sender_id to receiver_id.
    It has his own workflow, from draft to done and can be refund.
    The confirm state, used only when there is an external currency (currency whose wallet isn't managed in odoo),
     is used so the receiver can confirm that the send gave his the money.
    """

    def _get_price_char(self, cr, uid, ids, prop, unknow_none, context=None):
        # Compute a char from the currency lines, so we can easily display the transaction value in tree view
        res = {}
        for transaction in self.browse(cr, uid, ids, context=context):
            res[transaction.id] = ''
            for currency in transaction.currency_ids:
                if res[transaction.id] != '':
                    res[transaction.id] += ', '
                res[transaction.id] += str(currency.subtotal) + ' ' + currency.currency_id.symbol
        return res

    def _get_user_role(self, cr, uid, ids, prop, unknow_none, context=None):
        # Control the access rights of the current user
        user_obj = self.pool.get('res.users')
        res = {}
        partner_id = self.pool.get('res.users').browse(cr, uid, uid, context=context).partner_id.id
        for transaction in self.browse(cr, uid, ids, context=context):
            res[transaction.id] = {}
            res[transaction.id]['is_sender'] = False
            res[transaction.id]['is_receiver'] = False
            res[transaction.id]['is_moderator'] = False
            if transaction.sender_id.id == partner_id:
                res[transaction.id]['is_sender'] = True
            if transaction.receiver_id.id == partner_id:
                res[transaction.id]['is_receiver'] = True
            if user_obj.has_group(cr, uid, 'account_wallet.group_account_wallet_moderator'):
                res[transaction.id]['is_sender'] = True
                res[transaction.id]['is_receiver'] = True
                res[transaction.id]['is_moderator'] = True
        return res

    _name = 'account.wallet.transaction'
    _description = 'Transaction'
    _inherit = ['mail.thread']
    _columns = {
        'sender_id': fields.many2one('res.partner', 'Sender', required=True, readonly=True,
                                     states={'draft': [('readonly', False)]}, select=1),
        'receiver_id': fields.many2one('res.partner', 'Receiver', required=True, readonly=True,
                                       states={'draft': [('readonly', False)]}, select=1),
        'description': fields.text('Detail'),
        'total': fields.function(_get_price_char, string='Total', type="char", size=64,
                                 digits_compute=dp.get_precision('Account'), store=True, readonly=True),
        'quantity': fields.float('Exchanged quantity', digits_compute=dp.get_precision('Product Unit of Measure')),
        'uom_id': fields.many2one('product.uom', 'Unit of Measure', ondelete='set null'),
        'currency_ids': fields.one2many(
            'account.wallet.currency.line', 'res_id',
            domain=lambda self: [('model', '=', self._name), ('field', '=', 'currency_ids')],
            auto_join=True, string='Currencies'
        ),
        'already_published': fields.boolean('Already published?'),
        'move_ids': fields.one2many('account.move', 'wallet_transaction_id', 'Moves'),
        'reservation_id': fields.many2one('account.move', 'Reservation move'),
        'invoice_id': fields.many2one('account.move', 'Invoice move'),
        'payment_id': fields.many2one('account.move', 'Payment move'),
        'confirm_id': fields.many2one('account.move', 'Confirmation move'),
        'model_id': fields.many2one('ir.model', 'Model', required=True),
        'model_name': fields.related('model_id', 'model', type='char', size=64, string='Model', readonly=True),
        'is_sender': fields.function(_get_user_role, type="boolean", string="Is sender?", multi='role'),
        'is_receiver': fields.function(_get_user_role, type="boolean", string="Is receiver?", multi='role'),
        'is_moderator': fields.function(_get_user_role, type="boolean", string="Is moderator?", multi='role'),
        'state': fields.selection([
            ('draft', 'Draft'),
            ('confirm', 'Confirm'),
            ('done', 'Closed'),
            ('confirm_refund', 'Confirm Refund'),
            ('cancel', 'Cancelled'),
        ], 'Status', readonly=True, required=True),

    }

    def _default_partner(self, cr, uid, context=None):
        # By default, use the partner linked to the current user
        user_obj = self.pool.get('res.users')
        user = user_obj.browse(cr, uid, uid, context=context)
        return user.partner_id.id

    def _default_currency_ids(self, cr, uid, context=None):
        # When create the transaction, it already contain one line with the default currency
        proxy = self.pool.get('ir.model.data')
        config = proxy.get_object(cr, uid, 'base_community', 'community_settings')
        return [(0, 0, {
            'model': self._name,
            'price_unit': 1.0,
            'currency_id': config.default_currency_id.id
        })]

    def _get_uom_id(self, cr, uid, *args):
        # Return the uom_id by default
        try:
            proxy = self.pool.get('ir.model.data')
            result = proxy.get_object_reference(cr, uid, 'product', 'product_uom_unit')
            return result[1]
        except Exception, ex:
            return False
<commit_msg>[FIX] Resolve an issue with transaction<commit_after># -*- coding: utf-8 -*-
##############################################################################
#
#    Author: <NAME>. Copyright <NAME>
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as
#    published by the Free Software Foundation, either version 3 of the
#    License, or (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

import logging
from datetime import datetime

from openerp.osv import fields, osv, orm
from openerp import SUPERUSER_ID
from openerp import workflow
from openerp.tools.translate import _

import openerp.addons.decimal_precision as dp

_logger = logging.getLogger(__name__)


class AccountWalletCurrencyLine(osv.osv):

    """
    Object which contain the values of the transaction.
    Theses object can be linked to other object than account.wallet.transaction
    """

    _name = 'account.wallet.currency.line'
    _description = 'Currency line'

    def _get_subtotal(self, cr, uid, ids, field_name, arg, context=None):
        #Compute the total value which will be transfer for the specified currency
        res = {}
        for currency in self.browse(cr, uid, ids, context=context):
            model_obj = self.pool.get(currency.model)
            res[currency.id] = 1.0
            if model_obj.search(cr, uid, [('id', '=', currency.res_id)], context=context):
                transaction = model_obj.browse(cr, uid, currency.res_id, context=context)
                res[currency.id] = transaction.quantity * currency.price_unit
        return res

    _columns = {
        'model': fields.char('Related Document Model', size=128, required=True, select=1),
        'res_id': fields.integer('Related Document ID', required=True, select=1),
        'field': fields.char('Field', size=64, required=True, select=1),
        'price_unit': fields.float('Unit price', required=True, digits_compute=dp.get_precision('Product Price')),
        'currency_id': fields.many2one('res.currency', 'Currency', domain=[('wallet_currency', '=', True)],
                                       required=True, select=1),
        'subtotal': fields.function(_get_subtotal, string='Subtotal', digits_compute=dp.get_precision('Account')),
    }

    _defaults = {
        'model': 'account.wallet.transaction',
        'field': 'currency_ids'
    }

    _sql_constraints = [
        ('object_currency', 'unique(model,res_id,field,currency_id)', 'We can only have one currency per record')
    ]


class AccountWalletTransaction(osv.osv):

    """
    Main object used for transferring currencies, from sender_id to receiver_id.
    It has his own workflow, from draft to done and can be refund.
    The confirm state, used only when there is an external currency (currency whose wallet isn't managed in odoo),
     is used so the receiver can confirm that the send gave his the money.
    """

    def _get_price_char(self, cr, uid, ids, prop, unknow_none, context=None):
        # Compute a char from the currency lines, so we can easily display the transaction value in tree view
        res = {}
        for transaction in self.browse(cr, uid, ids, context=context):
            res[transaction.id] = ''
            for currency in transaction.currency_ids:
                if res[transaction.id] != '':
                    res[transaction.id] += ', '
                res[transaction.id] += str(currency.subtotal) + ' ' + currency.currency_id.symbol
        return res

    def _get_user_role(self, cr, uid, ids, prop, unknow_none, context=None):
        # Control the access rights of the current user
        user_obj = self.pool.get('res.users')
        res = {}
        partner_id = self.pool.get('res.users').browse(cr, uid, uid, context=context).partner_id.id
        for transaction in self.browse(cr, uid, ids, context=context):
            res[transaction.id] = {}
            res[transaction.id]['is_sender'] = False
            res[transaction.id]['is_receiver'] = False
            res[transaction.id]['is_moderator'] = False
            if transaction.sender_id.id == partner_id:
                res[transaction.id]['is_sender'] = True
            if transaction.receiver_id.id == partner_id:
                res[transaction.id]['is_receiver'] = True
            if user_obj.has_group(cr, uid, 'account_wallet.group_account_wallet_moderator'):
                res[transaction.id]['is_sender'] = True
                res[transaction.id]['is_receiver'] = True
                res[transaction.id]['is_moderator'] = True
        return res

    _name = 'account.wallet.transaction'
    _description = 'Transaction'
    _inherit = ['mail.thread']
    _columns = {
        'sender_id': fields.many2one('res.partner', 'Sender', required=True, readonly=True,
                                     states={'draft': [('readonly', False)]}, select=1),
        'receiver_id': fields.many2one('res.partner', 'Receiver', required=True, readonly=True,
                                       states={'draft': [('readonly', False)]}, select=1),
        'description': fields.text('Detail'),
        'total': fields.function(_get_price_char, string='Total', type="char", size=64,
                                 digits_compute=dp.get_precision('Account'), store=True, readonly=True),
        'quantity': fields.float('Exchanged quantity', digits_compute=dp.get_precision('Product Unit of Measure')),
        'uom_id': fields.many2one('product.uom', 'Unit of Measure', ondelete='set null'),
        'currency_ids': fields.one2many(
            'account.wallet.currency.line', 'res_id',
            domain=lambda self: [('model', '=', self._name), ('field', '=', 'currency_ids')],
            auto_join=True, string='Currencies'
        ),
        'already_published': fields.boolean('Already published?'),
        'move_ids': fields.one2many('account.move', 'wallet_transaction_id', 'Moves'),
        'reservation_id': fields.many2one('account.move', 'Reservation move'),
        'invoice_id': fields.many2one('account.move', 'Invoice move'),
        'payment_id': fields.many2one('account.move', 'Payment move'),
        'confirm_id': fields.many2one('account.move', 'Confirmation move'),
        'model_id': fields.many2one('ir.model', 'Model', required=True),
        'model_name': fields.related('model_id', 'model', type='char', size=64, string='Model', readonly=True),
        'is_sender': fields.function(_get_user_role, type="boolean", string="Is sender?", multi='role'),
        'is_receiver': fields.function(_get_user_role, type="boolean", string="Is receiver?", multi='role'),
        'is_moderator': fields.function(_get_user_role, type="boolean", string="Is moderator?", multi='role'),
        'state': fields.selection([
            ('draft', 'Draft'),
            ('confirm', 'Confirm'),
            ('done', 'Closed'),
            ('confirm_refund', 'Confirm Refund'),
            ('cancel', 'Cancelled'),
        ], 'Status', readonly=True, required=True),

    }

    def _default_partner(self, cr, uid, context=None):
        # By default, use the partner linked to the current user
        user_obj = self.pool.get('res.users')
        user = user_obj.browse(cr, uid, uid, context=context)
        return user.partner_id.id

    def _default_currency_ids(self, cr, uid, context=None):
        # When create the transaction, it already contain one line with the default currency
        proxy = self.pool.get('ir.model.data')
        config = proxy.get_object(cr, uid, 'base_community', 'community_settings')
        return [(0, 0, {
            'model': self._name,
            'field': 'currency_ids',
            'price_unit': 1.0,
            'currency_id': config.default_currency_id.id
        })]

    def _get_uom_id(self, cr, uid, *args):
        # Return the uom_id by default
        try:
            proxy = self.pool.get('ir.model.data')
            result = proxy.get_object_reference(cr, uid, 'product', 'product_uom_unit')
            return result[1]
        except Exception, ex:
            return False

{'fraction_non_alphanumeric': 0.14348308698954257, 'fraction_numerical': 0.004089501665011392, 'mean_word_length': 2.9735376044568245, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4815599', 'n_tokens_mistral': 4879, 'n_tokens_neox': 4731, 'n_words': 1285}