from trytond.model import ModelSQL, ModelView, fields
from trytond.pool import PoolMeta
from trytond.exceptions import UserError
from trytond.modules.purchase_trade.purchase import (TRIGGERS)
from trytond.transaction import Transaction

__all__ = ['Party']
__metaclass__ = PoolMeta

class PartyExecution(ModelSQL,ModelView):
    "Party Execution"
    __name__ = 'party.execution'

    party = fields.Many2One('party.party',"Party")
    area = fields.Many2One('country.region',"Area")
    percent = fields.Numeric("% targeted")

class Party(metaclass=PoolMeta):
    __name__ = 'party.party'

    tol_min = fields.Numeric("Tol - in %")
    tol_max = fields.Numeric("Tol + in %")
    wb = fields.Many2One('purchase.weight.basis',"Weight basis")
    association = fields.Many2One('purchase.association',"Association")
    execution = fields.One2Many('party.execution','party',"")

    @classmethod
    def getPartyByName(cls, party, category=None):
        party = party.upper()
        p = cls.search([('name', '=', party)], limit=1)
        if p:
            return p[0]
        else:
            p = cls()
            p.name = party
            cls.save([p])
            return p
    