首页 / 教程文章 / WordPress柔性供应链软件开发入门与实战教程

WordPress柔性供应链软件开发入门与实战教程

WordPress柔性供应链软件开发入门与实战教程

引言:为什么选择WordPress开发供应链系统?

在当今快速变化的商业环境中,企业需要灵活、可扩展的供应链解决方案。传统的大型ERP系统虽然功能强大,但往往价格昂贵、实施周期长且难以定制。WordPress作为全球最流行的内容管理系统,凭借其强大的插件生态、友好的开发环境和较低的学习门槛,成为开发轻量级柔性供应链系统的理想选择。

本教程将引导您从零开始,在WordPress平台上开发一个基础的柔性供应链管理系统,涵盖产品管理、库存跟踪、订单处理和供应商协作等核心功能。

环境准备与基础配置

1.1 开发环境搭建

首先,我们需要搭建本地开发环境。推荐使用XAMPP、MAMP或Local by Flywheel等集成环境。

<?php
/**
 * 检查WordPress环境要求
 * 此代码应放在插件主文件中
 */
function check_environment_requirements() {
    $errors = array();
    
    // 检查PHP版本
    if (version_compare(PHP_VERSION, '7.4', '<')) {
        $errors[] = '需要PHP 7.4或更高版本,当前版本:' . PHP_VERSION;
    }
    
    // 检查WordPress版本
    global $wp_version;
    if (version_compare($wp_version, '5.6', '<')) {
        $errors[] = '需要WordPress 5.6或更高版本,当前版本:' . $wp_version;
    }
    
    // 检查必要扩展
    $required_extensions = ['mysqli', 'json', 'curl'];
    foreach ($required_extensions as $ext) {
        if (!extension_loaded($ext)) {
            $errors[] = '需要PHP扩展:' . $xt;
        }
    }
    
    // 如果有错误,显示并停止激活
    if (!empty($errors)) {
        foreach ($errors as $error) {
            echo '<div class="error"><p>' . esc_html($error) . '</p></div>';
        }
        deactivate_plugins(plugin_basename(__FILE__));
        return false;
    }
    
    return true;
}
register_activation_hook(__FILE__, 'check_environment_requirements');
?>

1.2 创建自定义插件

在wp-content/plugins目录下创建新文件夹"flexible-supply-chain",并创建主插件文件。

<?php
/**
 * Plugin Name: 柔性供应链管理系统
 * Plugin URI:  https://yourwebsite.com/
 * Description: 基于WordPress的柔性供应链管理解决方案
 * Version:     1.0.0
 * Author:      您的名称
 * License:     GPL v2 or later
 * Text Domain: flexible-supply-chain
 */

// 防止直接访问
if (!defined('ABSPATH')) {
    exit;
}

// 定义插件常量
define('FSC_VERSION', '1.0.0');
define('FSC_PLUGIN_DIR', plugin_dir_path(__FILE__));
define('FSC_PLUGIN_URL', plugin_dir_url(__FILE__));

// 初始化插件
class FlexibleSupplyChain {
    
    private static $instance = null;
    
    public static function get_instance() {
        if (null === self::$instance) {
            self::$instance = new self();
        }
        return self::$instance;
    }
    
    private function __construct() {
        $this->init_hooks();
    }
    
    private function init_hooks() {
        // 注册激活和停用钩子
        register_activation_hook(__FILE__, array($this, 'activate'));
        register_deactivation_hook(__FILE__, array($this, 'deactivate'));
        
        // 初始化
        add_action('init', array($this, 'init'));
        
        // 管理菜单
        add_action('admin_menu', array($this, 'add_admin_menu'));
        
        // 加载文本域
        add_action('plugins_loaded', array($this, 'load_textdomain'));
    }
    
    public function activate() {
        // 创建数据库表
        $this->create_database_tables();
        
        // 设置默认选项
        $this->set_default_options();
        
        // 刷新重写规则
        flush_rewrite_rules();
    }
    
    public function deactivate() {
        // 清理临时数据
        flush_rewrite_rules();
    }
    
    public function init() {
        // 注册自定义文章类型和分类法
        $this->register_post_types();
    }
    
    public function load_textdomain() {
        load_plugin_textdomain(
            'flexible-supply-chain',
            false,
            dirname(plugin_basename(__FILE__)) . '/languages'
        );
    }
    
    public function add_admin_menu() {
        // 添加管理菜单
        add_menu_page(
            __('供应链管理', 'flexible-supply-chain'),
            __('供应链', 'flexible-supply-chain'),
            'manage_options',
            'supply-chain',
            array($this, 'render_dashboard'),
            'dashicons-networking',
            30
        );
    }
    
    public function render_dashboard() {
        include FSC_PLUGIN_DIR . 'templates/dashboard.php';
    }
    
    private function create_database_tables() {
        global $wpdb;
        
        $charset_collate = $wpdb->get_charset_collate();
        
        // 库存表
        $table_inventory = $wpdb->prefix . 'fsc_inventory';
        $sql_inventory = "CREATE TABLE IF NOT EXISTS $table_inventory (
            id bigint(20) NOT NULL AUTO_INCREMENT,
            product_id bigint(20) NOT NULL,
            warehouse_id bigint(20) NOT NULL,
            quantity int(11) NOT NULL DEFAULT 0,
            reorder_level int(11) DEFAULT 10,
            last_updated datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
            PRIMARY KEY (id),
            KEY product_id (product_id),
            KEY warehouse_id (warehouse_id)
        ) $charset_collate;";
        
        // 订单表
        $table_orders = $wpdb->prefix . 'fsc_orders';
        $sql_orders = "CREATE TABLE IF NOT EXISTS $table_orders (
            id bigint(20) NOT NULL AUTO_INCREMENT,
            order_number varchar(50) NOT NULL,
            customer_id bigint(20) DEFAULT NULL,
            status varchar(50) DEFAULT 'pending',
            total_amount decimal(10,2) DEFAULT 0.00,
            created_at datetime DEFAULT CURRENT_TIMESTAMP,
            updated_at datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
            PRIMARY KEY (id),
            UNIQUE KEY order_number (order_number),
            KEY customer_id (customer_id),
            KEY status (status)
        ) $charset_collate;";
        
        require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
        dbDelta($sql_inventory);
        dbDelta($sql_orders);
    }
    
    private function set_default_options() {
        $default_options = array(
            'inventory_alert_threshold' => 10,
            'default_warehouse' => 1,
            'currency' => 'CNY',
            'low_stock_notification' => 1,
        );
        
        foreach ($default_options as $key => $value) {
            if (false === get_option('fsc_' . $key)) {
                add_option('fsc_' . $key, $value);
            }
        }
    }
    
    private function register_post_types() {
        // 注册产品自定义文章类型
        register_post_type('fsc_product',
            array(
                'labels' => array(
                    'name' => __('产品', 'flexible-supply-chain'),
                    'singular_name' => __('产品', 'flexible-supply-chain'),
                ),
                'public' => true,
                'has_archive' => true,
                'supports' => array('title', 'editor', 'thumbnail', 'custom-fields'),
                'menu_icon' => 'dashicons-products',
                'rewrite' => array('slug' => 'products'),
            )
        );
        
        // 注册供应商自定义文章类型
        register_post_type('fsc_supplier',
            array(
                'labels' => array(
                    'name' => __('供应商', 'flexible-supply-chain'),
                    'singular_name' => __('供应商', 'flexible-supply-chain'),
                ),
                'public' => true,
                'has_archive' => true,
                'supports' => array('title', 'editor', 'thumbnail'),
                'menu_icon' => 'dashicons-businessperson',
                'rewrite' => array('slug' => 'suppliers'),
            )
        );
    }
}

// 启动插件
FlexibleSupplyChain::get_instance();
?>

核心功能模块开发

2.1 产品与库存管理

库存管理是供应链系统的核心。我们将创建一个库存管理类来处理库存操作。

<?php
/**
 * 库存管理类
 * 处理库存相关操作:入库、出库、查询、预警
 */
class FSC_Inventory_Manager {
    
    private $db;
    
    public function __construct() {
        global $wpdb;
        $this->db = $wpdb;
        $this->table = $wpdb->prefix . 'fsc_inventory';
    }
    
    /**
     * 更新库存数量
     * @param int $product_id 产品ID
     * @param int $warehouse_id 仓库ID
     * @param int $quantity 数量(正数表示入库,负数表示出库)
     * @param string $operation_type 操作类型:purchase, sale, adjustment, return
     * @return bool|int 成功返回true,失败返回false
     */
    public function update_stock($product_id, $warehouse_id, $quantity, $operation_type = 'adjustment') {
        
        // 验证参数
        if (!is_numeric($product_id) || !is_numeric($warehouse_id) || !is_numeric($quantity)) {
            return false;
        }
        
        // 检查现有库存
        $current_stock = $this->get_stock_level($product_id, $warehouse_id);
        
        // 如果是出库操作,检查库存是否充足
        if ($quantity < 0 && abs($quantity) > $current_stock) {
            $this->log_low_stock_event($product_id, $warehouse_id, $current_stock);
            return false; // 库存不足
        }
        
        // 计算新库存
        $new_stock = $current_stock + $quantity;
        
        // 检查记录是否存在
        $existing = $this->db->get_row($this->db->prepare(
            "SELECT id FROM {$this->table} WHERE product_id = %d AND warehouse_id = %d",
            $product_id, $warehouse_id
        ));
        
        if ($existing) {
            // 更新现有记录
            $result = $this->db->update(
                $this->table,
                array('quantity' => $new_stock),
                array('product_id' => $product_id, 'warehouse_id' => $warehouse_id),
                array('%d'),
                array('%d', '%d')
            );
        } else {
            // 插入新记录
            $result = $this->db->insert(
                $this->table,
                array(
                    'product_id' => $product_id,
                    'warehouse_id' => $warehouse_id,
                    'quantity' => $new_stock,
                    'reorder_level' => get_option('fsc_inventory_alert_threshold', 10)
                ),
                array('%d', '%d', '%d', '%d')
            );
        }
        
        if ($result !== false) {
            // 记录库存变更历史
            $this->log_inventory_change($product_id, $warehouse_id, $quantity, $operation_type, $current_stock, $new_stock);
            
            // 检查是否需要补货预警
            $this->check_reorder_level($product_id, $warehouse_id, $new_stock);
            
            return true;
        }
        
        return false;
    }
    
    /**
     * 获取库存水平
     * @param int $product_id 产品ID
     * @param int $warehouse_id 仓库ID(可选,不传则返回所有仓库总和)
     * @return int 库存数量
     */
    public function get_stock_level($product_id, $warehouse_id = null) {
        if ($warehouse_id) {
            $query = $this->db->prepare(
                "SELECT quantity FROM {$this->table} WHERE product_id = %d AND warehouse_id = %d",
                $product_id, $warehouse_id
            );
            $result = $this->db->get_var($query);
            return $result ? intval($result) : 0;
        } else {
            $query = $this->db->prepare(
                "SELECT SUM(quantity) FROM {$this->table} WHERE product_id = %d",
                $product_id
            );
            $result = $this->db->get_var($query);
            return $result ? intval($result) : 0;
        }
    }
    
    /**
     * 记录库存变更历史
     */
    private function log_inventory_change($product_id, $warehouse_id, $change, $operation_type, $old_stock, $new_stock) {
        $log_table = $this->db->prefix . 'fsc_inventory_logs';
        
        $this->db->insert(
            $log_table,
            array(
                'product_id' => $product_id,
                'warehouse_id' => $warehouse_id,
                'change_amount' => $change,
                'operation_type' => $operation_type,
                'old_stock' => $old_stock,
                'new_stock' => $new_stock,
                'user_id' => get_current_user_id(),
                'created_at' => current_time('mysql')
            ),
            array('%d', '%d', '%d', '%s', '%d', '%d', '%d', '%s')
        );
    }
    
    /**
     * 检查补货水平
     */
    private function check_reorder_level($product_id, $warehouse_id, $current_stock) {
        $threshold = $this->db->get_var($this->db->prepare(
            "SELECT reorder_level FROM {$this->table} WHERE product_id = %d AND warehouse_id = %d",
            $product_id, $warehouse_id
        ));
        
        if ($current_stock <= $threshold) {
            $this->send_reorder_alert($product_id, $warehouse_id, $current_stock, $threshold);
        }
    }
    
    /**
     * 发送补货预警
     */
    private function send_reorder_alert($product_id, $warehouse_id, $current_stock, $threshold) {
        $product = get_post($product_id);
        $warehouse = get_post($warehouse_id);
        
        $subject = sprintf(__('库存预警: %s 库存不足', 'flexible-supply-chain'), $product->post_title);
        $message = sprintf(
            __('产品 "%s" 在仓库 "%s" 中的库存已低于安全水平。当前库存: %d,安全库存: %d。请及时补货。', 'flexible-supply-chain'),
            $product->post_title,
            $warehouse->post_title,
            $current_stock,
            $threshold
        );
        
        // 获取管理员邮箱
        $admin_email = get_option('admin_email');
        
        // 发送邮件
        wp_mail($admin_email, $subject, $message);
        
        // 记录到系统日志
        error_log('库存预警: ' . $message);
    }
    
    /**
     * 获取低库存产品列表
     * @return array 低库存产品数组
     */
    public function get_low_stock_products() {
        $query = $this->db->prepare(
            "SELECT i.*, p.post_title as product_name, w.post_title as warehouse_name 
             FROM {$this->table} i
             LEFT JOIN {$this->db->posts} p ON i.product_id = p.ID
             LEFT JOIN {$this->db->posts} w ON i.warehouse_id = w.ID
             WHERE i.quantity <= i.reorder_level AND p.post_status = 'publish'"
        );
        
        return $this->db->get_results($query, ARRAY_A);
    }
}
?>

2.2 订单处理系统

订单处理是供应链的另一核心功能。以下是简化的订单处理类:

<?php
/**
 * 订单管理类
 * 处理订单创建、更新、状态变更等操作
 */
class FSC_Order_Manager {
    
    private $db;
    
    public function __construct() {
        global $wpdb;
        $this->db = $wpdb;
        $this->orders_table = $wpdb->prefix . 'fsc_orders';
        $this->order_items_table = $wpdb->prefix . 'fsc_order_items';
    }
    
    /**
     * 创建新订单
     * @param array $order_data 订单数据
     * @param array $items 订单项
     * @return int|false 成功返回订单ID,失败返回false
     */
    public function create_order($order_data, $items = array()) {
        
        // 验证必填字段
        $required_fields = ['customer_name', 'customer_email', 'total_amount'];
        foreach ($required_fields as $field) {
            if (empty($order_data[$field])) {
                return false;
            }
        }
        
        // 生成订单号
        $order_number = $this->generate_order_number();
        
        // 准备订单数据
        $order_data_db = array(
            'order_number' => $order_number,
            'customer_id' => $order_data['customer_id'] ?? 0,
            'customer_name' => sanitize_text_field($order_data['customer_name']),
            'customer_email' => sanitize_email($order_data['customer_email']),
            'customer_phone' => sanitize_text_field($order_data['customer_phone'] ?? ''),
            'shipping_address' => sanitize_textarea_field($order_data['shipping_address'] ?? ''),
            'billing_address' => sanitize_textarea_field($order_data['billing_address'] ?? ''),
            'status' => 'pending',
            'total_amount' => floatval($order_data['total_amount']),
            'notes' => sanitize_textarea_field($order_data['notes'] ?? ''),
            'created_at' => current_time('mysql'),
            'updated_at' => current_time('mysql')
        );
        
        // 插入订单
        $result = $this->db->insert(
            $this->orders_table,
            $order_data_db,
            array('%s', '%d', '%s', '%s', '%s', '%s', '%s', '%s', '%f', '%s', '%s', '%s')
        );
        
        if ($result === false) {
            return false;
        }
        
        $order_id = $this->db->insert_id;
        
        // 添加订单项
        if (!empty($items)) {
            foreach ($items as $item) {
                $this->add_order_item($order_id, $item);
            }
        }
        
        // 记录订单创建日志
        $this->log_order_event($order_id, 'order_created', __('订单已创建', 'flexible-supply-chain'));
        
        // 发送订单确认邮件
        $this->send_order_confirmation($order_id);
        
        return $order_id;
    }
    
    /**
     * 生成唯一订单号
     * @return string 订单号
     */
    private function generate_order_number() {
        $prefix = 'ORD';
        $date = date('Ymd');
        $random = strtoupper(substr(md5(uniqid()), 0, 6));
        
        return $prefix . '-' . $date . '-' . $random;
    }
    
    /**
     * 添加订单项
     */
    private function add_order_item($order_id, $item_data) {
        $item_data_db = array(
            'order_id' => $order_id,
            'product_id' => intval($item_data['product_id']),
            'product_name' => sanitize_text_field($item_data['product_name']),
            'quantity' => intval($item_data['quantity']),
            'unit_price' => floatval($item_data['unit_price']),
            'total_price' => floatval($item_data['quantity'] * $item_data['unit_price']),
            'created_at' => current_time('mysql')
        );
        
        return $this->db->insert(
            $this->order_items_table,
            $item_data_db,
            array('%d', '%d', '%s', '%d', '%f', '%f', '%s')
        );
    }
    
    /**
     * 更新订单状态
     * @param int $order_id 订单ID
     * @param string $new_status 新状态
     * @param string $notes 备注
     * @return bool 成功返回true,失败返回false
     */
    public function update_order_status($order_id, $new_status, $notes = '') {
        
        $valid_statuses = ['pending', 'processing', 'on_hold', 'shipped', 'delivered', 'cancelled', 'refunded'];
        
        if (!in_array($new_status, $valid_statuses)) {
            return false;
        }
        
        // 获取当前状态
        $current_status = $this->db->get_var($this->db->prepare(
            "SELECT status FROM {$this->orders_table} WHERE id = %d",
            $order_id
        ));
        
        // 如果状态相同,不更新
        if ($current_status === $new_status) {
            return true;
        }
        
        // 更新订单状态
        $result = $this->db->update(
            $this->orders_table,
            array(
                'status' => $new_status,
                'updated_at' => current_time('mysql')
            ),
            array('id' => $order_id),
            array('%s', '%s'),
            array('%d')
        );
        
        if ($result !== false) {
            // 记录状态变更
            $status_labels = array(
                'pending' => __('待处理', 'flexible-supply-chain'),
                'processing' => __('处理中', 'flexible-supply-chain'),
                'shipped' => __('已发货', 'flexible-supply-chain'),
                'delivered' => __('已送达', 'flexible-supply-chain'),
                'cancelled' => __('已取消', 'flexible-supply-chain')
            );
            
            $message = sprintf(
                __('订单状态从 "%s" 变更为 "%s"', 'flexible-supply-chain'),
                $status_labels[$current_status] ?? $current_status,
                $status_labels[$new_status] ?? $new_status
            );
            
            if (!empty($notes)) {
                $message .= ' - ' . $notes;
            }
            
            $this->log_order_event($order_id, 'status_changed', $message);
            
            // 如果订单发货,更新库存
            if ($new_status === 'shipped') {
                $this->deduct_inventory_for_order($order_id);
            }
            
            // 发送状态更新通知
            $this->send_status_notification($order_id, $new_status);
            
            return true;
        }
        
        return false;
    }
    
    /**
     * 为发货订单扣减库存
     */
    private function deduct_inventory_for_order($order_id) {
        global $fsc_inventory;
        
        // 获取订单项
        $items = $this->db->get_results($this->db->prepare(
            "SELECT product_id, quantity FROM {$this->order_items_table} WHERE order_id = %d",
            $order_id
        ), ARRAY_A);
        
        // 默认仓库
        $default_warehouse = get_option('fsc_default_warehouse', 1);
        
        foreach ($items as $item) {
            // 扣减库存(负数为出库)
            $fsc_inventory->update_stock(
                $item['product_id'],
                $default_warehouse,
                -$item['quantity'],
                'sale'
            );
        }
    }
    
    /**
     * 记录订单事件
     */
    private function log_order_event($order_id, $event_type, $message) {
        $logs_table = $this->db->prefix . 'fsc_order_logs';
        
        $this->db->insert(
            $logs_table,
            array(
                'order_id' => $order_id,
                'event_type' => $event_type,
                'message' => $message,
                'user_id' => get_current_user_id(),
                'created_at' => current_time('mysql')
            ),
            array('%d', '%s', '%s', '%d', '%s')
        );
    }
    
    /**
     * 发送订单确认邮件
     */
    private function send_order_confirmation($order_id) {
        $order = $this->get_order($order_id);
        
        if (!$order) {
            return false;
        }
        
        $to = $order['customer_email'];
        $subject = sprintf(__('订单确认 #%s', 'flexible-supply-chain'), $order['order_number']);
        
        // 构建邮件内容
        $message = $this->get_order_email_template($order, 'confirmation');
        
        // 设置HTML内容类型
        add_filter('wp_mail_content_type', function() {
            return 'text/html';
        });
        
        // 发送邮件
        wp_mail($to, $subject, $message);
        
        // 恢复内容类型
        remove_filter('wp_mail_content_type', function() {
            return 'text/html';
        });
    }
    
    /**
     * 获取订单详情
     */
    public function get_order($order_id) {
        $query = $this->db->prepare(
            "SELECT * FROM {$this->orders_table} WHERE id = %d",
            $order_id
        );
        
        return $this->db->get_row($query, ARRAY_A);
    }
}
?>

前端界面与用户交互

3.1 创建供应链仪表板

<?php
/**
 * 供应链仪表板模板
 * 文件路径: templates/dashboard.php
 */
if (!defined('ABSPATH')) {
    exit;
}

global $fsc_inventory, $fsc_orders;
?>
<div class="wrap fsc-dashboard">
    <h1><?php _e('供应链管理仪表板', 'flexible-supply-chain'); ?></h1>
    
    <div class="fsc-stats-container">
        <!-- 统计卡片 -->
        <div class="fsc-stat-card">
            <h3><?php _e('总库存价值', 'flexible-supply-chain'); ?></h3>
            <div class="stat-value"><?php echo $this->get_total_inventory_value(); ?></div>
            <div class="stat-trend">↑ 5% <?php _e('较上月', 'flexible-supply-chain'); ?></div>
        </div>
        
        <div class="fsc-stat-card">
            <h3><?php _e('待处理订单', 'flexible-supply-chain'); ?></h3>
            <div class="stat-value"><?php echo $this->get_pending_orders_count(); ?></div>
            <div class="stat-trend">↓ 2% <?php _e('较上周', 'flexible-supply-chain'); ?></div>
        </div>
        
        <div class="fsc-stat-card">
            <h3><?php _e('低库存产品', 'flexible-supply-chain'); ?></h3>
            <div class="stat-value"><?php echo count($fsc_inventory->get_low_stock_products()); ?></div>
            <div class="stat-trend warning">⚠️ <?php _e('需要关注', 'flexible-supply-chain'); ?></div>
        </div>
        
        <div class="fsc-stat-card">
            <h3><?php _e('本月销售额', 'flexible-supply-chain'); ?></h3>
            <div class="stat-value"><?php echo $this->get_monthly_sales(); ?></div>
            <div class="stat-trend">↑ 12% <?php _e('较上月', 'flexible-supply-chain'); ?></div>
        </div>
    </div>
    
    <!-- 库存预警部分 -->
    <div class="fsc-section">
        <h2><?php _e('库存预警', 'flexible-supply-chain'); ?></h2>
        <?php $low_stock_products = $fsc_inventory->get_low_stock_products(); ?>
        <?php if (!empty($low_stock_products)): ?>
            <table class="wp-list-table widefat fixed striped">
                <thead>
                    <tr>
                        <th><?php _e('产品名称', 'flexible-supply-chain'); ?></th>
                        <th><?php _e('仓库', 'flexible-supply-chain'); ?></th>
                        <th><?php _e('当前库存', 'flexible-supply-chain'); ?></th>
                        <th><?php _e('安全库存', 'flexible-supply-chain'); ?></th>
                        <th><?php _e('操作', 'flexible-supply-chain'); ?></th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($low_stock_products as $product): ?>
                    <tr>
                        <td><?php echo esc_html($product['product_name']); ?></td>
                        <td><?php echo esc_html($product['warehouse_name']); ?></td>
                        <td class="stock-low"><?php echo intval($product['quantity']); ?></td>
                        <td><?php echo intval($product['reorder_level']); ?></td>
                        <td>
                            <button class="button button-small" onclick="reorderProduct(<?php echo $product['product_id']; ?>)">
                                <?php _e('立即补货', 'flexible-supply-chain'); ?>
                            </button>
                        </td>
                    </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        <?php else: ?>
            <p><?php _e('暂无低库存产品。', 'flexible-supply-chain'); ?></p>
        <?php endif; ?>
    </div>
    
    <!-- 最近订单 -->
    <div class="fsc-section">
        <h2><?php _e('最近订单', 'flexible-supply-chain'); ?></h2>
        <?php $recent_orders = $this->get_recent_orders(10); ?>
        <table class="wp-list-table widefat fixed striped">
            <thead>
                <tr>
                    <th><?php _e('订单号', 'flexible-supply-chain'); ?></th>
                    <th><?php _e('客户', 'flexible-supply-chain'); ?></th>
                    <th><?php _e('金额', 'flexible-supply-chain'); ?></th>
                    <th><?php _e('状态', 'flexible-supply-chain'); ?></th>
                    <th><?php _e('下单时间', 'flexible-supply-chain'); ?></th>
                    <th><?php _e('操作', 'flexible-supply-chain'); ?></th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($recent_orders as $order): ?>
                <tr>
                    <td><?php echo esc_html($order['order_number']); ?></td>
                    <td><?php echo esc_html($order['customer_name']); ?></td>
                    <td><?php echo number_format($order['total_amount'], 2); ?></td>
                    <td>
                        <span class="order-status status-<?php echo esc_attr($order['status']); ?>">
                            <?php echo $this->get_status_label($order['status']); ?>
                        </span>
                    </td>
                    <td><?php echo date('Y-m-d H:i', strtotime($order['created_at'])); ?></td>
                    <td>
                        <a href="<?php echo admin_url('admin.php?page=supply-chain-orders&action=view&id=' . $order['id']); ?>" class="button button-small">
                            <?php _e('查看', 'flexible-supply-chain'); ?>
                        </a>
                    </td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</div>

<style>
.fsc-dashboard {
    padding: 20px;
}

.fsc-stats-container {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
    gap: 20px;
    margin-bottom: 30px;
}

.fsc-stat-card {
    background: white;
    border: 1px solid #ccd0d4;
    border-radius: 4px;
    padding: 20px;
    box-shadow: 0 1px 3px rgba(0,0,0,0.1);
}

.fsc-stat-card h3 {
    margin-top: 0;
    color: #646970;
    font-size: 14px;
    text-transform: uppercase;
}

.stat-value {
    font-size: 32px;
    font-weight: bold;
    color: #2271b1;
    margin: 10px 0;
}

.stat-trend {
    font-size: 12px;
    color: #646970;
}

.stat-trend.warning {
    color: #d63638;
}

.fsc-section {
    background: white;
    border: 1px solid #ccd0d4;
    border-radius: 4px;
    padding: 20px;
    margin-bottom: 20px;
}

.fsc-section h2 {
    margin-top: 0;
    border-bottom: 1px solid #ccd0d4;
    padding-bottom: 10px;
}

.stock-low {
    color: #d63638;
    font-weight: bold;
}

.order-status {
    padding: 3px 8px;
    border-radius: 3px;
    font-size: 12px;
    font-weight: bold;
}

.status-pending {
    background: #f0c33c;
    color: #000;
}

.status-processing {
    background: #2271b1;
    color: white;
}

.status-shipped {
    background: #00a32a;
    color: white;
}

.status-delivered {
    background: #646970;
    color: white;
}
</style>

<script>
function reorderProduct(productId) {
    if (confirm('<?php _e('确定要为此产品创建补货订单吗?', 'flexible-supply-chain'); ?>')) {
        // AJAX请求创建补货订单
        jQuery.post(ajaxurl, {
            action: 'fsc_create_reorder',
            product_id: productId,
            nonce: '<?php echo wp_create_nonce('fsc_reorder_nonce'); ?>'
        }, function(response) {
            if (response.success) {
                alert('<?php _e('补货订单已创建!', 'flexible-supply-chain'); ?>');
                location.reload();
            } else {
                alert('<?php _e('创建失败:', 'flexible-supply-chain'); ?>' + response.data);
            }
        });
    }
}
</script>

3.2 供应商管理界面

<?php
/**
 * 供应商管理类
 * 处理供应商信息、绩效评估和协作
 */
class FSC_Supplier_Manager {
    
    private $db;
    
    public function __construct() {
        global $wpdb;
        $this->db = $wpdb;
    }
    
    /**
     * 评估供应商绩效
     * @param int $supplier_id 供应商ID
     * @return array 绩效评分
     */
    public function evaluate_supplier_performance($supplier_id) {
        
        $performance = array(
            'delivery_score' => 0,
            'quality_score' => 0,
            'price_score' => 0,
            'communication_score' => 0,
            'overall_score' => 0
        );
        
        // 计算交货准时率
        $delivery_stats = $this->calculate_delivery_stats($supplier_id);
        $performance['delivery_score'] = $delivery_stats['on_time_rate'] * 100;
        
        // 计算质量合格率
        $quality_stats = $this->calculate_quality_stats($supplier_id);
        $performance['quality_score'] = $quality_stats['quality_rate'] * 100;
        
        // 价格竞争力(基于市场平均价比较)
        $price_stats = $this->calculate_price_stats($supplier_id);
        $performance['price_score'] = $price_stats['competitiveness_score'];
        
        // 沟通响应评分
        $communication_stats = $this->calculate_communication_stats($supplier_id);
        $performance['communication_score'] = $communication_stats['response_score'];
        
        // 综合评分(加权平均)
        $weights = array(
本文来自网络,不代表柔性供应链服务中心立场,转载请注明出处:https://mall.org.cn/5769.html

EXCHANGES®作者

上一篇
下一篇

为您推荐

发表回复

联系我们

联系我们

18559313275

在线咨询: QQ交谈

邮箱: vip@exchanges.center

工作时间:周一至周五,9:00-17:30,节假日休息
返回顶部