trytondo-sale_order/sale_order.py
2025-01-25 22:14:29 -05:00

84 lines
2.5 KiB
Python

# This file is part of Tryton. The COPYRIGHT file at the top level of
# this repository contains the full copyright notices and license terms.
from trytond.model import ModelView, ModelSQL, fields
from trytond.pool import Pool
from trytond.transaction import Transaction
from trytond.modules.currency.fields import Monetary
from trytond.modules.product import price_digits
from decimal import Decimal
class SaleOrder(ModelView, ModelSQL):
"Sale Order"
__name__ = 'sale.order'
party = fields.Many2One(
'party.party', "Party", required=True
)
pickup_location = fields.Selection(
[("on_site", "On Site"),
("at_home", "At Home")], 'Pickup Location'
)
lines = fields.One2Many(
'order.line', 'order', 'Lines'
)
class OrderLine(ModelView, ModelSQL):
"Order Line"
__name__ = 'order.line'
company = fields.Many2One(
'company.company', "Company", required=True)
order = fields.Many2One(
'sale.order', "Sale"
)
currency = fields.Many2One(
'currency.currency', 'Currency', required=True)
product = fields.Many2One(
'product.product', 'Product', required=True
)
unit = fields.Many2One(
'product.uom', 'Unit')
product_uom_category = fields.Function(
fields.Many2One('product.uom.category', 'Product UOM Category'),
'on_change_with_product_uom_category'
)
quantity = fields.Float(
"Quantity", digits=('unit')
)
unitprice = Monetary(
"Unit Price", digits=price_digits, currency='currency'
)
total_amount = fields.Function(
Monetary("Total Amount", currency='currency', digits='currency'),
'on_change_with_total_amount'
)
@staticmethod
def default_company():
return Transaction().context.get('company')
@classmethod
def default_currency(cls, **pattern):
pool = Pool()
Company = pool.get('company.company')
company = pattern.get('company')
if not company:
company = cls.default_company()
if company:
return Company(company).currency.id
@fields.depends('product')
def on_change_with_product_uom_category(self, name=None):
if self.product:
return self.product.default_uom.category.id
return None
@fields.depends('quantity', 'unitprice')
def on_change_with_total_amount(self, name=None):
if self.unitprice and self.quantity:
total_amount = self.unitprice * Decimal(self.quantity)
return total_amount