Website : phonehp.ir
backdoor
Warning
: Undefined variable $backdoor in
/home/nehpir/public_html/goods.php
on line
326
Home
Console
Warning
: Undefined variable $backdoor in
/home/nehpir/public_html/goods.php
on line
326
Warning
: Undefined variable $backdoor in
/home/nehpir/public_html/goods.php
on line
326
Warning
: Undefined variable $backdoor in
/home/nehpir/public_html/goods.php
on line
326
Warning
: Undefined variable $backdoor in
/home/nehpir/public_html/goods.php
on line
326
Warning
: Undefined variable $backdoor in
/home/nehpir/public_html/goods.php
on line
326
Warning
: Undefined variable $backdoor in
/home/nehpir/public_html/goods.php
on line
326
Upload
information
Create File
Create Folder
About
Tools
:
/
home
/
nehpir
/
www
/
wp-content
/
plugins
/
woocommersa
/
includes
/
Services
/
Filename :
Profit_Margin_Service.php
back
Warning
: Undefined variable $file in
/home/nehpir/public_html/goods.php
on line
545
Copy
<?php namespace WooCommersa\Services; if (!defined('ABSPATH')) exit; /** * Senior Profit & Margin Service * Handles precision-critical financial calculations for WooCommerce products. */ class Profit_Margin_Service { /** * Calculate all financial metrics for a product * Formula: Margin % = ((Price - EffectiveCost) / Price) * 100 */ public function calculate_metrics($product_id) { $product = wc_get_product($product_id); if (!$product) return false; $settings = get_option('woocommersa_settings', []); $overhead_percent = floatval($settings['profit_overhead_percent'] ?? 0); $price = floatval($product->get_price()); $cost = floatval(get_post_meta($product_id, '_cost_price', true)); // Stock potential $stock = floatval($product->get_stock_quantity() ?: 0); if ($price <= 0) { return [ 'profit' => 0, 'margin' => 0, 'effective_cost' => $cost, 'revenue_potential' => 0, 'profit_potential' => 0 ]; } // Calculation Logic $overhead_value = $price * ($overhead_percent / 100); $effective_cost = $cost + $overhead_value; $profit = $price - $effective_cost; $margin = ($profit / $price) * 100; $metrics = [ 'profit' => round($profit, 2), 'margin' => round($margin, 2), 'effective_cost' => round($effective_cost, 2), 'revenue_potential' => round($price * $stock, 2), 'profit_potential' => round($profit * $stock, 2) ]; // Sync to DB for filtering/sorting performance $this->sync_to_meta($product_id, $metrics); return $metrics; } /** * Persist calculated values to post meta for high-speed SQL queries (sorting/filtering) */ public function sync_to_meta($product_id, $metrics) { update_post_meta($product_id, '_profit', $metrics['profit']); update_post_meta($product_id, '_margin', $metrics['margin']); update_post_meta($product_id, '_profit_potential', $metrics['profit_potential']); } /** * Bulk update margin for a set of products * This is an Architectural rule-based update. */ public function apply_bulk_margin($product_ids, $target_margin_percent) { $affected = 0; foreach ($product_ids as $id) { $product = wc_get_product($id); if (!$product) continue; $cost = floatval(get_post_meta($id, '_cost_price', true)); if ($cost <= 0) continue; $settings = get_option('woocommersa_settings', []); $overhead = floatval($settings['profit_overhead_percent'] ?? 0); // Back-solve for Price based on target Margin: // Margin = (Price - Cost - (Price * OH)) / Price // Margin * Price = Price - Cost - (Price * OH) // Price - (Price * OH) - (Price * Margin) = Cost // Price * (1 - OH - Margin) = Cost // Price = Cost / (1 - OH - Margin) $oh_dec = $overhead / 100; $margin_dec = $target_margin_percent / 100; if ((1 - $oh_dec - $margin_dec) <= 0) continue; // Division by zero or logic error safety $new_price = $cost / (1 - $oh_dec - $margin_dec); $product->set_regular_price(round($new_price, 0)); $product->save(); $this->calculate_metrics($id); // Refetch and re-sync $affected++; } return $affected; } }