Add product discount calculation and update handling for dynamic usage logs

- Introduced `getProductDiscountPercentage` method to calculate accurate discounts for users across products, categories, and global settings.
- Adjusted `importUsageLogs` to refine date modifier parameter and enhance `usage log` checks.
- Enhanced `orders_o` to include product discount logic with stricter type casting for consistency.
- Added `updateFieldsWhere` method for selective database updates based on specific conditions.
- Updated `customers_o` to handle updates for existing customers while adding new ones.
This commit is contained in:
Jeppe Bundgaard
2025-07-23 09:31:43 +02:00
parent 16198f49e6
commit f5ba9dd891
5 changed files with 98 additions and 6 deletions
+28
View File
@@ -699,6 +699,34 @@ class users_o extends db
return $discount;
}
/**
* Get the final price of the product for the user
* This includes:
* - Global discount (If the product allows category inheritance of discounts)
* - Category discount (If the product allows category inheritance of discounts)
* - Product discount (If the product has a custom price)
*/
public function getProductDiscountPercentage(int $product_id): int|null
{
self::requireSelected();
// Get the product by ID
$product = (new products_o())->select((int)$product_id);
$doesProductAllowCategoryDiscount = (bool)$product->apply_category_discount->value();
$economic_discount_percentage = $this->getEconomicCustomerDiscountPercentage();
$discount_percentage = $this->getCustomPrice($product_id);
// If the product allows category inheritance of discounts, get the category discounts
if ($doesProductAllowCategoryDiscount) {
// If the category discount is higher than the product discount, use the category discount
if ($economic_discount_percentage > $discount_percentage) {
$discount_percentage = $economic_discount_percentage;
}
}
// If the discount percentage is null, return 0
return $discount_percentage === null ? 0 : (int)$discount_percentage;
}
/**
* Get all custom prices (DISCOUNT) for the user
* @return array The custom prices