-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feature/add-switch-send-files' of https://github.com/we…
…ni-ai/weni-integrations-engine into develop
- Loading branch information
Showing
20 changed files
with
968 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
marketplace/services/vtex/business/rules/calculate_by_weight_co.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
from .interface import Rule | ||
from marketplace.services.vtex.utils.data_processor import FacebookProductDTO | ||
from typing import Union | ||
|
||
|
||
class CalculateByWeightCO(Rule): | ||
def apply(self, product: FacebookProductDTO, **kwargs) -> bool: | ||
if self._calculates_by_weight(product): | ||
unit_multiplier = self._get_multiplier(product) | ||
weight = self._get_weight(product) * unit_multiplier | ||
|
||
# 10% increase for specific categories | ||
if self._is_increased_price_category(product) and weight >= 500: | ||
increase_factor = 1.10 # 10% | ||
product.price *= unit_multiplier * increase_factor | ||
product.sale_price *= unit_multiplier * increase_factor | ||
else: | ||
product.price *= unit_multiplier | ||
product.sale_price *= unit_multiplier | ||
|
||
price_per_kg = 0 | ||
if weight > 0: | ||
formatted_price = float(f"{product.sale_price / 100:.2f}") | ||
price_per_kg = formatted_price / unit_multiplier | ||
|
||
product.description = ( | ||
f"{product.title} - Aprox. {self._format_grams(weight)}, " | ||
f"Precio por KG: COP {self._format_price(price_per_kg)}" | ||
) | ||
product.title = f"{product.title} Unidad" | ||
|
||
return True | ||
|
||
def _is_increased_price_category(self, product: FacebookProductDTO) -> bool: | ||
categories = [ | ||
"carne y pollo", | ||
"carne res", | ||
"pescados y mariscos", | ||
"pescado congelado", | ||
] | ||
product_categories = { | ||
k: v.lower() | ||
for k, v in product.product_details["ProductCategories"].items() | ||
} | ||
|
||
return any(category in product_categories.values() for category in categories) | ||
|
||
def _get_multiplier(self, product: FacebookProductDTO) -> float: | ||
return product.product_details.get("UnitMultiplier", 1.0) | ||
|
||
def _get_weight(self, product: FacebookProductDTO) -> float: | ||
return product.product_details["Dimension"]["weight"] | ||
|
||
def _calculates_by_weight(self, product: FacebookProductDTO) -> bool: | ||
all_categories = [ | ||
"carne y pollo", | ||
"carne res", | ||
"pescados y mariscos", | ||
"pescado congelado", | ||
"verduras", | ||
"frutas", | ||
] | ||
product_categories = { | ||
k: v.lower() | ||
for k, v in product.product_details["ProductCategories"].items() | ||
} | ||
|
||
for category in all_categories: | ||
if category in product_categories.values(): | ||
return True | ||
|
||
return False | ||
|
||
def _format_price(self, price: Union[int, float]) -> str: | ||
return f"{price:.2f}" | ||
|
||
def _format_grams(self, value: float) -> str: | ||
if 0 < value < 1: | ||
grams = int(value * 1000) | ||
else: | ||
grams = int(value) | ||
|
||
if grams > 999: | ||
formatted = f"{grams:,}".replace(",", ".") | ||
else: | ||
formatted = str(grams) | ||
|
||
return f"{formatted}g" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
from .interface import Rule | ||
from typing import Union | ||
from marketplace.services.vtex.utils.data_processor import FacebookProductDTO | ||
|
||
|
||
class CurrencyCOP(Rule): | ||
def apply(self, product: FacebookProductDTO, **kwargs) -> bool: | ||
product.price = self.format_price(product.price) | ||
product.sale_price = self.format_price(product.sale_price) | ||
return True | ||
|
||
@staticmethod | ||
def format_price(price: Union[int, float]) -> str: | ||
formatted_price = f"{price / 100:.2f} COP" | ||
return formatted_price |
Oops, something went wrong.