1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
|
# 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 collections import defaultdict
from sql import Literal
from sql.operators import Equal
from trytond.cache import Cache
from trytond.model import (
DeactivableMixin, Exclude, ModelSQL, ModelView, fields)
from trytond.pool import Pool, PoolMeta
from trytond.pyson import Eval
from trytond.transaction import Transaction
class Many2ManyInactive(fields.Many2Many):
def get(self, ids, model, name, values=None):
with Transaction().set_context(inactive_test=True):
return super().get(ids, model, name, values=values)
def set(self, Model, name, ids, values, *args):
with Transaction().set_context(inactive_test=True):
return super().set(Model, name, ids, values, *args)
class Inactivate(ModelSQL, DeactivableMixin):
@classmethod
def search_domain(cls, domain, active_test=None, tables=None):
context = Transaction().context
if context.get('inactive_test'):
domain = [domain, ('active', '=', False)]
return super().search_domain(
domain, active_test=active_test, tables=tables)
@classmethod
def delete(cls, records):
cls.copy([r for r in records if r.active and r.shop.to_sync],
default={
'active': False,
})
super().delete(records)
class Shop(DeactivableMixin, ModelSQL, ModelView):
"Web Shop"
__name__ = 'web.shop'
name = fields.Char("Name", required=True)
company = fields.Many2One('company.company', "Company", required=True)
currency = fields.Many2One('currency.currency', "Currency", required=True)
language = fields.Many2One(
'ir.lang', "Language",
domain=[
('translatable', '=', True),
])
type = fields.Selection([
(None, ""),
], "Type",
help="The front-end used for the web shop.")
warehouses = fields.Many2Many(
'web.shop-stock.location', 'shop', 'warehouse', "Warehouses",
domain=[
('type', '=', 'warehouse'),
])
guest_party = fields.Many2One(
'party.party', "Guest Party",
context={
'company': Eval('company', -1),
},
depends={'company'})
products = fields.Many2Many(
'web.shop-product.product', 'shop', 'product', "Products",
domain=[
('salable', '=', True),
],
context={
'company': Eval('company', -1),
},
depends={'company'},
help="The list of products to publish.")
products_removed = Many2ManyInactive(
'web.shop-product.product', 'shop', 'product', "Products Removed",
context={
'company': Eval('company', -1),
},
depends={'company'},
help="The list of products to unpublish.")
categories = fields.Many2Many(
'web.shop-product.category', 'shop', 'category', "Categories",
context={
'company': Eval('company', -1),
},
depends={'company'},
help="The list of categories to publish.")
categories_removed = Many2ManyInactive(
'web.shop-product.category', 'shop', 'category', "Categories Removed",
context={
'company': Eval('company', -1),
},
depends={'company'},
help="The list of categories to unpublish.")
_name_cache = Cache('web.shop.name', context=False)
@property
def warehouse(self):
if self.warehouses:
return self.warehouses[0]
@classmethod
def __setup__(cls):
super().__setup__()
t = cls.__table__()
cls._sql_constraints = [
('name_unique', Exclude(t, (t.name, Equal),
where=t.active == Literal(True)),
'web_shop.msg_shop_name_unique'),
]
@classmethod
def default_company(cls):
return Transaction().context.get('company')
@classmethod
def default_currency(cls):
pool = Pool()
Company = pool.get('company.company')
company_id = cls.default_company()
if company_id is not None and company_id >= 0:
company = Company(company_id)
return company.currency.id
@classmethod
def get(cls, name):
shop_id = cls._name_cache.get(name)
if not shop_id:
shop, = cls.search([('name', '=', name)])
cls._name_cache.set(name, shop.id)
else:
shop = cls(shop_id)
return shop
@classmethod
def write(cls, *args):
cls._name_cache.clear()
super().write(*args)
@classmethod
def delete(cls, shops):
cls._name_cache.clear()
super().delete(shops)
@property
def to_sync(self):
return False
def _customer_taxe_rule(self):
pool = Pool()
Configuration = pool.get('account.configuration')
config = Configuration(1)
return config.get_multivalue(
'default_customer_tax_rule', company=self.company.id)
def get_context(self):
pool = Pool()
Date = pool.get('ir.date')
with Transaction().set_context(company=self.company.id):
today = Date.today()
return {
'language': self.language.code if self.language else None,
'company': self.company.id,
'currency': self.currency.id,
'locations': [w.id for w in self.warehouses],
'stock_date_end': today,
'stock_assign': True,
}
def get_products(self, pattern=None):
"Return the list of products with corresponding prices and taxes"
pool = Pool()
Date = pool.get('ir.date')
Product = pool.get('product.product')
Tax = pool.get('account.tax')
if pattern is None:
pattern = {}
with Transaction().set_context(**self.get_context()):
all_products = Product.browse(self.products)
today = Date.today()
customer_tax_rule = self._customer_taxe_rule()
taxes2products = defaultdict(list)
for product in all_products:
taxes = set()
for tax in product.customer_taxes_used:
if customer_tax_rule:
tax_ids = customer_tax_rule.apply(tax, pattern)
if tax_ids:
taxes.update(tax_ids)
continue
taxes.add(tax.id)
if customer_tax_rule:
tax_ids = customer_tax_rule.apply(None, pattern)
if tax_ids:
taxes.update(tax_ids)
taxes2products[tuple(sorted(taxes))].append(product)
prices, taxes = {}, {}
for tax_ids, products in taxes2products.items():
with Transaction().set_context(**self.get_context()):
products = Product.browse(products)
taxes_ = Tax.browse(tax_ids)
with Transaction().set_context(taxes=tax_ids):
prices.update(Product.get_sale_price(products))
for product in products:
price = prices[product.id]
if price is not None:
taxes[product.id] = sum(
t['amount'] for t in Tax.compute(
taxes_, price, 1, today))
else:
taxes[product.id] = None
return all_products, prices, taxes
def get_categories(self):
"Return the list of categories"
pool = Pool()
Category = pool.get('product.category')
with Transaction().set_context(**self.get_context()):
return Category.browse(self.categories)
def get_sale(self, party=None):
pool = Pool()
Sale = pool.get('sale.sale')
if not party:
party = self.guest_party
sale = Sale(party=party)
sale.company = self.company
sale.warehouse = self.warehouse
sale.web_shop = self
sale.on_change_party()
sale.on_change_web_shop()
sale.currency = self.currency
sale.invoice_method = 'order'
sale.shipment_method = 'order'
return sale
class Shop_TaxRuleCountry(metaclass=PoolMeta):
__name__ = 'web.shop'
def get_products(self, pattern=None):
pattern = pattern.copy() if pattern is not None else {}
if (self.warehouse
and self.warehouse.address
and self.warehouse.address.country):
pattern.setdefault(
'from_country', self.warehouse.address.country.id)
else:
pattern.setdefault('from_country')
pattern.setdefault('to_country')
return super().get_products(pattern=pattern)
class Shop_Warehouse(ModelSQL):
"Web Shop - Warehouse"
__name__ = 'web.shop-stock.location'
shop = fields.Many2One(
'web.shop', "Shop", ondelete='CASCADE', required=True)
warehouse = fields.Many2One(
'stock.location', "Warehouse", ondelete='CASCADE', required=True,
domain=[
('type', '=', 'warehouse'),
])
class Shop_Product(Inactivate):
"Web Shop - Product"
__name__ = 'web.shop-product.product'
shop = fields.Many2One(
'web.shop', "Shop", ondelete='CASCADE', required=True)
product = fields.Many2One(
'product.product', "Product", ondelete='RESTRICT', required=True)
class Shop_ProductCategory(Inactivate):
"Web Shop - Product Category"
__name__ = 'web.shop-product.category'
shop = fields.Many2One(
'web.shop', "Shop", ondelete='CASCADE', required=True)
category = fields.Many2One(
'product.category', "Category", ondelete='RESTRICT', required=True)
class ShopAttribute(metaclass=PoolMeta):
__name__ = 'web.shop'
attributes = fields.Many2Many(
'web.shop-product.attribute', 'shop', 'attribute', "Attributes",
help="The list of attributes to publish.")
attributes_removed = Many2ManyInactive(
'web.shop-product.attribute', 'shop', 'attribute',
"Attributes Removed",
help="The list of attributes to unpublish.")
def get_attributes(self):
"Return the list of attributes"
pool = Pool()
Attribute = pool.get('product.attribute')
with Transaction().set_context(**self.get_context()):
return Attribute.browse(self.attributes)
class Shop_Attribute(Inactivate):
"Web Shop - Attribute"
__name__ = 'web.shop-product.attribute'
shop = fields.Many2One(
'web.shop', "Shop", ondelete='CASCADE', required=True)
attribute = fields.Many2One(
'product.attribute', "Attribute", ondelete='RESTRICT', required=True)
class User(metaclass=PoolMeta):
__name__ = 'web.user'
invoice_address = fields.Many2One(
'party.address', "Invoice Address",
domain=['OR',
('party', '=', Eval('party', -1)),
('party', 'in', Eval('secondary_parties', [])),
])
shipment_address = fields.Many2One(
'party.address', "Shipment Address",
domain=['OR',
('party', '=', Eval('party', -1)),
('party', 'in', Eval('secondary_parties', [])),
])
|