2017-06-14 14:54:02 +02:00
|
|
|
# This file is part of Tryton. The COPYRIGHT file at the top level of
|
|
|
|
# this repository contains the full copyright notices and license terms.
|
2019-06-18 15:52:01 +02:00
|
|
|
from trytond.pool import PoolMeta, Pool
|
|
|
|
from trytond.model import fields
|
2019-04-03 17:33:03 +02:00
|
|
|
from trytond.i18n import gettext
|
|
|
|
from trytond.exceptions import UserError
|
2017-06-14 14:54:02 +02:00
|
|
|
|
|
|
|
__all__ = ['Sale']
|
|
|
|
|
|
|
|
|
2018-10-19 00:21:40 +02:00
|
|
|
class Sale(metaclass=PoolMeta):
|
2017-06-14 14:54:02 +02:00
|
|
|
__name__ = 'sale.sale'
|
|
|
|
|
2017-08-02 12:03:28 +02:00
|
|
|
@classmethod
|
|
|
|
def validate(cls, sales):
|
|
|
|
super(Sale, cls).validate(sales)
|
|
|
|
for sale in sales:
|
|
|
|
sale.check_shipment_party()
|
|
|
|
|
|
|
|
def check_shipment_party(self):
|
|
|
|
if self.party.party_sale_payer:
|
2019-04-03 17:33:03 +02:00
|
|
|
raise UserError(
|
|
|
|
gettext('sale_invoice_grouping_shipment_party.msg_error_party_payer',
|
|
|
|
name=self.party.rec_name,
|
|
|
|
))
|
2017-08-02 12:03:28 +02:00
|
|
|
|
2017-06-14 14:54:02 +02:00
|
|
|
@property
|
|
|
|
def _invoice_grouping_fields(self):
|
|
|
|
return super(Sale, self)._invoice_grouping_fields + ('shipment_party',)
|
|
|
|
|
|
|
|
def _get_grouped_invoice_domain(self, invoice):
|
|
|
|
invoice_domain = super(Sale, self)._get_grouped_invoice_domain(invoice)
|
|
|
|
if self.shipment_party:
|
2018-10-19 00:21:40 +02:00
|
|
|
if ('shipment_party', '=', None) in invoice_domain:
|
|
|
|
invoice_domain[
|
|
|
|
invoice_domain.index(('shipment_party', '=', None))
|
|
|
|
] = ('shipment_party', '=', self.shipment_party)
|
|
|
|
else:
|
|
|
|
invoice_domain.append(
|
|
|
|
('shipment_party', '=', self.shipment_party))
|
2017-06-14 14:54:02 +02:00
|
|
|
return invoice_domain
|
|
|
|
|
|
|
|
def _get_invoice_sale(self):
|
|
|
|
invoice = super(Sale, self)._get_invoice_sale()
|
|
|
|
if not hasattr(invoice, 'shipment_party') and self.shipment_party:
|
|
|
|
invoice.shipment_party = self.shipment_party
|
|
|
|
return invoice
|
2017-08-02 12:03:28 +02:00
|
|
|
|
|
|
|
def on_change_shipment_party(self):
|
|
|
|
super(Sale, self).on_change_shipment_party()
|
|
|
|
if self.shipment_party:
|
|
|
|
if self.shipment_party.party_sale_payer:
|
|
|
|
self.party = self.shipment_party.party_sale_payer
|
2017-09-12 13:29:36 +02:00
|
|
|
else:
|
2017-08-02 12:03:28 +02:00
|
|
|
self.party = self.shipment_party
|
|
|
|
self.on_change_party()
|
2019-06-18 15:52:01 +02:00
|
|
|
|
|
|
|
|
|
|
|
@fields.depends('carrier')
|
|
|
|
def on_change_party(self):
|
|
|
|
super(Sale, self).on_change_party()
|
|
|
|
Config = Pool().get('sale.configuration')
|
|
|
|
config = Config(1)
|
|
|
|
if config:
|
|
|
|
config = config.sale_default_party_carrier
|
|
|
|
if config == 'shipment_party' and self.shipment_party:
|
|
|
|
if self.shipment_party.carrier:
|
|
|
|
self.carrier = self.shipment_party.carrier
|