diff options
| author | IT Fixcomart <it@fixcomart.co.id> | 2022-10-13 09:50:34 +0000 |
|---|---|---|
| committer | IT Fixcomart <it@fixcomart.co.id> | 2022-10-13 09:50:34 +0000 |
| commit | 6668257b1c892fdcf366a446587c0aaf31f42651 (patch) | |
| tree | 49557e0332571ecc7db0d42af215476eee880cf2 /indoteknik_api/models | |
| parent | 6029c9e3a0b42a6faef373e6dd2f5bf57cef17a0 (diff) | |
| parent | 867e718b83282fdc7f4a7e77eb386b9b6014d8af (diff) | |
Merged in feature/rest-api (pull request #9)
Feature/rest api
Diffstat (limited to 'indoteknik_api/models')
| -rw-r--r-- | indoteknik_api/models/product_pricelist.py | 22 | ||||
| -rw-r--r-- | indoteknik_api/models/product_template.py | 63 |
2 files changed, 70 insertions, 15 deletions
diff --git a/indoteknik_api/models/product_pricelist.py b/indoteknik_api/models/product_pricelist.py index f40efd5d..4e7f6adf 100644 --- a/indoteknik_api/models/product_pricelist.py +++ b/indoteknik_api/models/product_pricelist.py @@ -41,10 +41,11 @@ class ProductPricelist(models.Model): if price > 0: product = self.env['product.product'].browse(product_id) if product: - for tax in product.taxes_id: - if not tax.price_include: - price *= (100 + tax.amount) / 100 - + product = product.product_tmpl_id + if product.web_tax_id and not product.web_tax_id.price_include: + price *= (100 + product.web_tax_id.amount) / 100 + + price_discount = price for discount in discounts: price_discount *= (100 - discount) / 100 @@ -57,13 +58,12 @@ class ProductPricelist(models.Model): 'discount_percentage': discount_percentage } - def get_lowest_product_variant_price(self, product_id, pricelist_id): + def get_lowest_product_variant_price(self, product_template: dict, pricelist_id: int): """ - @param product_id: id of product.template + @param product_template: dict of product_template @param pricelist_id: id of pricelist which have default price @return price: object """ - product_template = self.env['product.template'].browse(product_id) product_variant_ids = [x.id for x in product_template.product_variant_ids] product = self.env['product.pricelist.item'].search([ ('pricelist_id', '=', pricelist_id), @@ -99,11 +99,3 @@ class ProductPricelist(models.Model): if product_id in pricelist_product_ids: return pricelist.id return False - - # TODO: need testing - def get_active_flash_sale_items(self): - pricelist = self.get_active_flash_sale() - pricelist_item = False - if pricelist: - pricelist_item = [x.product_id for x in pricelist.item_ids] - return pricelist_item diff --git a/indoteknik_api/models/product_template.py b/indoteknik_api/models/product_template.py index e69de29b..aa35d922 100644 --- a/indoteknik_api/models/product_template.py +++ b/indoteknik_api/models/product_template.py @@ -0,0 +1,63 @@ +from odoo import models, fields + + +class ProductTemplate(models.Model): + _inherit = 'product.template' + + def api_single_response(self, product_template, with_detail=False): + base_url = self.env['ir.config_parameter'].get_param('web.base.url') + product_pricelist_default = self.env['ir.config_parameter'].get_param('product.pricelist.default') + product_pricelist_default = int(product_pricelist_default) + data = { + 'id': product_template.id, + 'image': base_url + 'api/image/product.template/image_128/' + str(product_template.id) if product_template.image_128 else '', + 'code': product_template.default_code or '', + 'name': product_template.name, + 'lowest_price': self.env['product.pricelist'].get_lowest_product_variant_price(product_template, product_pricelist_default), + 'variant_total': len(product_template.product_variant_ids), + 'stock_total': product_template.qty_stock_vendor, + 'weight': product_template.weight, + 'manufacture': self.api_manufacture(product_template), + 'categories': self.api_categories(product_template), + } + if with_detail: + detail_data = { + 'image': base_url + 'api/image/product.template/image_512/' + str(product_template.id) if product_template.image_512 else '', + 'variants': [], + 'description': product_template.website_description or '', + } + for variant in product_template.product_variant_ids: + detail_data['variants'].append({ + 'id': variant.id, + 'code': variant.default_code or '', + 'name': variant.display_name, + 'price': self.env['product.pricelist'].compute_price(product_pricelist_default, variant.id), + 'stock': variant.qty_stock_vendor, + 'weight': variant.weight, + }) + data.update(detail_data) + return data + + def api_manufacture(self, product_template): + base_url = self.env['ir.config_parameter'].get_param('web.base.url') + if product_template.x_manufacture: + manufacture = product_template.x_manufacture + return { + 'id': manufacture.id, + 'name': manufacture.x_name, + 'image_promotion_1': base_url + 'api/image/x_manufactures/image_promotion_1/' + str(manufacture.id) if manufacture.image_promotion_1 else '', + 'image_promotion_2': base_url + 'api/image/x_manufactures/image_promotion_2/' + str(manufacture.id) if manufacture.image_promotion_2 else '', + } + return {} + + def api_categories(self, product_template): + if product_template.public_categ_ids: + categories = [] + for category in product_template.public_categ_ids: + categories.append({ + 'id': category.id, + 'name': category.name + }) + return categories + return [] +
\ No newline at end of file |
