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
/
public_html
/
wp-content
/
plugins
/
woocommersa
/
includes
/
API
/
Filename :
FinanceController.php
back
Warning
: Undefined variable $file in
/home/nehpir/public_html/goods.php
on line
545
Copy
<?php namespace WooCommersa\API; if (!defined('ABSPATH')) exit; class FinanceController extends BaseController { public function __construct() { add_action('rest_api_init', [$this, 'register_routes']); } public function register_routes() { register_rest_route($this->namespace, '/profit-stats', [ 'methods' => 'GET', 'callback' => [$this, 'get_profit_stats'], 'permission_callback' => [$this, 'permissions_check'], ]); register_rest_route($this->namespace, '/profit-margin/bulk-apply', [ 'methods' => 'POST', 'callback' => [$this, 'bulk_apply_margin'], 'permission_callback' => [$this, 'permissions_check'], ]); } public function get_profit_stats() { global $wpdb; $settings = get_option('woocommersa_settings', []); $overhead = floatval($settings['overhead_percent'] ?? 0); // Fetch products with their price, cost, and stock using faster LEFT JOINS // We limit to products and variations to get a full picture $results = $wpdb->get_results(" SELECT p.ID, pm1.meta_value as price, pm2.meta_value as cost, pm3.meta_value as stock FROM {$wpdb->posts} p LEFT JOIN {$wpdb->postmeta} pm1 ON p.ID = pm1.post_id AND pm1.meta_key = '_price' LEFT JOIN {$wpdb->postmeta} pm2 ON p.ID = pm2.post_id AND pm2.meta_key = '_cost_price' LEFT JOIN {$wpdb->postmeta} pm3 ON p.ID = pm3.post_id AND pm3.meta_key = '_stock' WHERE p.post_type IN ('product', 'product_variation') AND p.post_status = 'publish' "); $stats = [ 'avgMargin' => 0, 'totalProfitPotential' => 0, 'valid_items_count' => 0, 'overhead_percent' => $overhead, 'distribution' => [ 'bleeding' => 0, // profit <= 0 'risk' => 0, // margin < 15% (after overhead) 'healthy' => 0, // margin 15-30% 'stars' => 0 // margin > 30% ] ]; $total_margin = 0; $valid_count = 0; foreach ($results as $row) { $price = floatval($row->price); $cost = floatval($row->cost); $stock = max(0, floatval($row->stock)); if ($price <= 0 || $cost <= 0) continue; // Apply overhead consistent with frontend $effective_cost = $cost + ($price * ($overhead / 100)); $profit = $price - $effective_cost; $margin = ($profit / $price) * 100; $stats['totalProfitPotential'] += ($profit * $stock); $total_margin += $margin; $valid_count++; // Distribution Logic matched to frontend thresholds if ($profit <= 0) { $stats['distribution']['bleeding']++; } elseif ($margin < 15) { $stats['distribution']['risk']++; } elseif ($margin <= 30) { $stats['distribution']['healthy']++; } else { $stats['distribution']['stars']++; } } if ($valid_count > 0) { $stats['avgMargin'] = round($total_margin / $valid_count, 1); $stats['valid_items_count'] = $valid_count; } return new \WP_REST_Response($stats, 200); } public function bulk_apply_margin($request) { $params = $request->get_json_params(); $ids = $params['ids'] ?? []; $margin = floatval($params['margin'] ?? 0); if (empty($ids)) return new \WP_REST_Response(['message' => 'No IDs provided'], 400); $service = new \WooCommersa\Services\Profit_Margin_Service(); $count = $service->apply_bulk_margin($ids, $margin); return new \WP_REST_Response([ 'success' => true, 'message' => sprintf(__('%d محصول با مارجین جدید بروزرسانی شد.', 'woocommersa'), $count) ], 200); } }