文章目录[隐藏]
WordPress小批量定制插件实现工艺成本精细化核算教程
一、前言:为什么需要工艺成本精细化核算
在当今竞争激烈的制造业环境中,小批量定制生产已成为许多企业的核心竞争力。然而,传统的成本核算方法往往难以准确反映小批量定制生产的真实成本,导致定价不合理、利润空间被压缩等问题。
WordPress作为全球最流行的内容管理系统,不仅适用于网站建设,通过定制插件开发,还可以成为企业管理的强大工具。本文将详细介绍如何开发一个WordPress插件,实现小批量定制生产的工艺成本精细化核算。
二、系统设计与数据库结构
2.1 数据库表设计
首先,我们需要设计数据库表来存储工艺成本相关数据。在插件激活时创建这些表:
<?php
/**
* 工艺成本核算插件 - 数据库设置
*/
// 注册激活钩子
register_activation_hook(__FILE__, 'craft_cost_calculator_install');
function craft_cost_calculator_install() {
global $wpdb;
$charset_collate = $wpdb->get_charset_collate();
// 工艺工序表
$process_table = $wpdb->prefix . 'craft_processes';
$process_sql = "CREATE TABLE IF NOT EXISTS $process_table (
id mediumint(9) NOT NULL AUTO_INCREMENT,
process_name varchar(100) NOT NULL,
description text,
base_time decimal(10,2) DEFAULT 0.00 COMMENT '基准工时(小时)',
base_cost decimal(10,2) DEFAULT 0.00 COMMENT '基准成本(元)',
difficulty_factor decimal(3,2) DEFAULT 1.00 COMMENT '难度系数',
created_at datetime DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (id)
) $charset_collate;";
// 材料成本表
$material_table = $wpdb->prefix . 'craft_materials';
$material_sql = "CREATE TABLE IF NOT EXISTS $material_table (
id mediumint(9) NOT NULL AUTO_INCREMENT,
material_name varchar(100) NOT NULL,
unit varchar(20) NOT NULL COMMENT '计量单位',
unit_price decimal(10,2) DEFAULT 0.00 COMMENT '单价',
supplier varchar(200),
notes text,
created_at datetime DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (id)
) $charset_collate;";
// 订单成本核算表
$order_cost_table = $wpdb->prefix . 'craft_order_costs';
$order_cost_sql = "CREATE TABLE IF NOT EXISTS $order_cost_table (
id mediumint(9) NOT NULL AUTO_INCREMENT,
order_id varchar(50) NOT NULL COMMENT '订单编号',
product_name varchar(200) NOT NULL,
quantity int(11) DEFAULT 1 COMMENT '生产数量',
total_process_cost decimal(12,2) DEFAULT 0.00 COMMENT '总工序成本',
total_material_cost decimal(12,2) DEFAULT 0.00 COMMENT '总材料成本',
additional_cost decimal(12,2) DEFAULT 0.00 COMMENT '附加成本',
total_cost decimal(12,2) DEFAULT 0.00 COMMENT '总成本',
profit_margin decimal(5,2) DEFAULT 20.00 COMMENT '利润率(%)',
suggested_price decimal(12,2) DEFAULT 0.00 COMMENT '建议售价',
calculated_at datetime DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (id),
UNIQUE KEY order_id (order_id)
) $charset_collate;";
// 订单工序明细表
$order_process_table = $wpdb->prefix . 'craft_order_processes';
$order_process_sql = "CREATE TABLE IF NOT EXISTS $order_process_table (
id mediumint(9) NOT NULL AUTO_INCREMENT,
order_cost_id mediumint(9) NOT NULL,
process_id mediumint(9) NOT NULL,
actual_time decimal(10,2) DEFAULT 0.00 COMMENT '实际工时',
difficulty_adjustment decimal(3,2) DEFAULT 1.00 COMMENT '难度调整系数',
process_cost decimal(10,2) DEFAULT 0.00 COMMENT '工序成本',
notes text,
PRIMARY KEY (id),
FOREIGN KEY (order_cost_id) REFERENCES $order_cost_table(id) ON DELETE CASCADE,
FOREIGN KEY (process_id) REFERENCES $process_table(id)
) $charset_collate;";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($process_sql);
dbDelta($material_sql);
dbDelta($order_cost_sql);
dbDelta($order_process_sql);
// 插入示例数据
craft_cost_calculator_insert_sample_data();
}
/**
* 插入示例数据
*/
function craft_cost_calculator_insert_sample_data() {
global $wpdb;
$process_table = $wpdb->prefix . 'craft_processes';
// 检查是否已有数据
$count = $wpdb->get_var("SELECT COUNT(*) FROM $process_table");
if ($count == 0) {
$sample_processes = array(
array('切割', '材料切割工序', 0.5, 25.00, 1.0),
array('打磨', '表面打磨处理', 1.2, 40.00, 1.2),
array('焊接', '部件焊接组装', 2.0, 80.00, 1.5),
array('喷漆', '表面喷漆处理', 1.5, 45.00, 1.1),
array('质检', '质量检验测试', 0.8, 30.00, 1.0)
);
foreach ($sample_processes as $process) {
$wpdb->insert(
$process_table,
array(
'process_name' => $process[0],
'description' => $process[1],
'base_time' => $process[2],
'base_cost' => $process[3],
'difficulty_factor' => $process[4]
)
);
}
}
}
?>
三、核心成本计算类实现
3.1 成本计算器类
<?php
/**
* 工艺成本计算器核心类
*/
class CraftCostCalculator {
private $order_id;
private $processes = array();
private $materials = array();
private $additional_costs = 0;
private $quantity = 1;
/**
* 构造函数
* @param string $order_id 订单编号
* @param int $quantity 生产数量
*/
public function __construct($order_id, $quantity = 1) {
$this->order_id = $order_id;
$this->quantity = $quantity;
}
/**
* 添加工序
* @param int $process_id 工序ID
* @param float $actual_time 实际工时
* @param float $difficulty_adjust 难度调整系数
*/
public function add_process($process_id, $actual_time = null, $difficulty_adjust = null) {
global $wpdb;
$process_table = $wpdb->prefix . 'craft_processes';
$process = $wpdb->get_row(
$wpdb->prepare("SELECT * FROM $process_table WHERE id = %d", $process_id)
);
if ($process) {
// 使用实际工时或基准工时
$time = ($actual_time !== null) ? $actual_time : $process->base_time;
// 使用调整系数或默认难度系数
$difficulty = ($difficulty_adjust !== null) ? $difficulty_adjust : $process->difficulty_factor;
// 计算工序成本
$process_cost = $process->base_cost * ($time / $process->base_time) * $difficulty;
$this->processes[] = array(
'process_id' => $process_id,
'process_name' => $process->process_name,
'base_time' => $process->base_time,
'actual_time' => $time,
'base_cost' => $process->base_cost,
'difficulty_factor' => $difficulty,
'process_cost' => $process_cost
);
}
}
/**
* 添加材料
* @param int $material_id 材料ID
* @param float $amount 使用量
*/
public function add_material($material_id, $amount) {
global $wpdb;
$material_table = $wpdb->prefix . 'craft_materials';
$material = $wpdb->get_row(
$wpdb->prepare("SELECT * FROM $material_table WHERE id = %d", $material_id)
);
if ($material) {
$material_cost = $material->unit_price * $amount;
$this->materials[] = array(
'material_id' => $material_id,
'material_name' => $material->material_name,
'unit' => $material->unit,
'unit_price' => $material->unit_price,
'amount' => $amount,
'material_cost' => $material_cost
);
}
}
/**
* 设置附加成本
* @param float $cost 附加成本金额
*/
public function set_additional_cost($cost) {
$this->additional_costs = $cost;
}
/**
* 计算总成本
* @return array 成本计算结果
*/
public function calculate() {
$total_process_cost = 0;
$total_material_cost = 0;
// 计算工序总成本
foreach ($this->processes as $process) {
$total_process_cost += $process['process_cost'];
}
// 计算材料总成本
foreach ($this->materials as $material) {
$total_material_cost += $material['material_cost'];
}
// 计算单件成本
$unit_process_cost = $total_process_cost;
$unit_material_cost = $total_material_cost;
$unit_additional_cost = $this->additional_costs;
$unit_total_cost = $unit_process_cost + $unit_material_cost + $unit_additional_cost;
// 计算批量总成本
$batch_process_cost = $unit_process_cost * $this->quantity;
$batch_material_cost = $unit_material_cost * $this->quantity;
$batch_additional_cost = $unit_additional_cost * $this->quantity;
$batch_total_cost = $unit_total_cost * $this->quantity;
return array(
'unit_costs' => array(
'process' => $unit_process_cost,
'material' => $unit_material_cost,
'additional' => $unit_additional_cost,
'total' => $unit_total_cost
),
'batch_costs' => array(
'process' => $batch_process_cost,
'material' => $batch_material_cost,
'additional' => $batch_additional_cost,
'total' => $batch_total_cost
),
'processes' => $this->processes,
'materials' => $this->materials,
'quantity' => $this->quantity
);
}
/**
* 计算建议售价
* @param float $profit_margin 利润率(%)
* @param float $total_cost 总成本
* @return float 建议售价
*/
public function calculate_suggested_price($profit_margin, $total_cost) {
if ($profit_margin <= 0 || $total_cost <= 0) {
return 0;
}
// 售价 = 成本 / (1 - 利润率)
$price = $total_cost / (1 - ($profit_margin / 100));
return round($price, 2);
}
/**
* 保存计算结果到数据库
* @param array $calculation_result 计算结果
* @param float $profit_margin 利润率
* @return int|false 插入ID或false
*/
public function save_calculation($calculation_result, $profit_margin = 20) {
global $wpdb;
$order_cost_table = $wpdb->prefix . 'craft_order_costs';
$order_process_table = $wpdb->prefix . 'craft_order_processes';
// 计算建议售价
$suggested_price = $this->calculate_suggested_price(
$profit_margin,
$calculation_result['batch_costs']['total']
);
// 插入订单成本主记录
$wpdb->insert(
$order_cost_table,
array(
'order_id' => $this->order_id,
'product_name' => '定制产品-' . $this->order_id,
'quantity' => $this->quantity,
'total_process_cost' => $calculation_result['batch_costs']['process'],
'total_material_cost' => $calculation_result['batch_costs']['material'],
'additional_cost' => $calculation_result['batch_costs']['additional'],
'total_cost' => $calculation_result['batch_costs']['total'],
'profit_margin' => $profit_margin,
'suggested_price' => $suggested_price
)
);
$order_cost_id = $wpdb->insert_id;
// 插入工序明细
foreach ($calculation_result['processes'] as $process) {
$wpdb->insert(
$order_process_table,
array(
'order_cost_id' => $order_cost_id,
'process_id' => $process['process_id'],
'actual_time' => $process['actual_time'],
'difficulty_adjustment' => $process['difficulty_factor'],
'process_cost' => $process['process_cost']
)
);
}
return $order_cost_id;
}
}
?>
四、管理界面实现
4.1 成本计算表单界面
<?php
/**
* 成本计算表单短代码
*/
function craft_cost_calculator_form_shortcode() {
ob_start();
?>
<div class="craft-cost-calculator">
<h2>工艺成本精细化核算</h2>
<form id="craft-cost-form" method="post">
<?php wp_nonce_field('craft_cost_calculate', 'craft_cost_nonce'); ?>
<div class="form-section">
<h3>订单基本信息</h3>
<div class="form-group">
<label for="order_id">订单编号:</label>
<input type="text" id="order_id" name="order_id"
value="ORD<?php echo date('YmdHis'); ?>" required>
</div>
<div class="form-group">
<label for="product_name">产品名称:</label>
<input type="text" id="product_name" name="product_name"
placeholder="请输入产品名称" required>
</div>
<div class="form-group">
<label for="quantity">生产数量:</label>
<input type="number" id="quantity" name="quantity"
value="1" min="1" required>
</div>
</div>
<div class="form-section">
<h3>选择工艺工序</h3>
<div id="process-selection">
<?php
global $wpdb;
$process_table = $wpdb->prefix . 'craft_processes';
$processes = $wpdb->get_results("SELECT * FROM $process_table ORDER BY id");
foreach ($processes as $process) {
?>
<div class="process-item">
<label>
<input type="checkbox" name="processes[]"
value="<?php echo $process->id; ?>"
data-base-time="<?php echo $process->base_time; ?>"
data-base-cost="<?php echo $process->base_cost; ?>">
<?php echo esc_html($process->process_name); ?>
(基准: <?php echo $process->base_time; ?>小时,
¥<?php echo $process->base_cost; ?>)
</label>
<div class="process-details" style="display:none;">
<label>实际工时(小时):
<input type="number" name="process_time[<?php echo $process->id; ?>]"
step="0.1" min="0.1"
value="<?php echo $process->base_time; ?>">
</label>
<label>难度调整系数:
<input type="number" name="process_difficulty[<?php echo $process->id; ?>]"
step="0.1" min="0.5" max="3"
value="<?php echo $process->difficulty_factor; ?>">
</label>
</div>
</div>
<?php
}
?>
</div>
</div>
<div class="form-section">
<h3>材料成本</h3>
<div id="material-selection">
<div class="material-item">
<button type="button" id="add-material">+ 添加材料</button>
</div>
</div>
</div>
<div class="form-section">
<h3>其他成本</h3>
<div class="form-group">
<label for="additional_cost">附加成本(元):</label>
<input type="number" id="additional_cost" name="additional_cost"
step="0.01" min="0" value="0">
<small>如运输、包装等额外费用</small>
</div>
</div>
<div class="form-section">
<h3>利润设置</h3>
<div class="form-group">
<label for="profit_margin">目标利润率(%):</label>
<input type="number" id="profit_margin" name="profit_margin"
step="0.1" min="0" max="100" value="20">
</div>
</div>
<div class="form-actions">
<button type="submit" name="calculate_cost" class="button button-primary">
计算成本
</button>
<button type="button" id="reset-form" class="button button-secondary">
重置表单
</button>
</div>
</form>
<div id="calculation-results" style="display:none;">
<h3>成本核算结果</h3>
<div class="results-section">
<h4>成本明细</h4>
<table class="cost-breakdown">
<thead>
<tr>
<th>成本项目</th>
<th>单件成本(元)</th>
<th>总成本(元)</th>
</tr>
</thead>
<tbody id="cost-details">
<!-- 通过JavaScript动态填充 -->
</tbody>
</table>
</div>
<div class="results-section">
<h4>定价建议</h4>
<div class="pricing-suggestion">
<p>单件成本: ¥<span id="unit-total-cost">0.00</span></p>
<p>批量总成本: ¥<span id="batch-total-cost">0.00</span></p>
<p>建议售价(含<span id="profit-percentage">20</span>%利润):
<strong>¥<span id="suggested-price">0.00</span></strong></p>
</div>
</div>
<div class="form-actions">
<button type="button" id="save-calculation" class="button button-primary">
保存计算结果
</button>
<button type="button" id="print-report" class="button button-secondary">
打印成本报告
</button>
</div>
</div>
</div>
<script>
jQuery(document).ready(function($) {
// 工序选择切换详情显示
$('input[name="processes[]"]').change(function() {
var details = $(this).closest('.process-item').find('.process-details');
if ($(this).is(':checked')) {
details.show();
} else {
details.hide();
}
});
// 添加材料行
var materialIndex = 0;
$('#add-material').click(function() {
materialIndex++;
var materialRow = `
<div class="material-row" data-index="${materialIndex}">
<div class="form-group-inline">
<label>材料名称:
<input type="text" name="materials[${materialIndex}][name]"
placeholder="材料名称" required>
</label>
</div>
<div class="form-group-inline">
<label>单价(元):
<input type="number" name="materials[${materialIndex}][price]"
step="0.01" min="0" value="0" required>
</label>
</div>
<div class="form-group-inline">
<label>数量:
<input type="number" name="materials[${materialIndex}][amount]"
step="0.1" min="0.1" value="1" required>
</label>
</div>
<div class="form-group-inline">
<label>单位:
<input type="text" name="materials[${materialIndex}][unit]"
placeholder="个/米/千克" required>
</label>
</div>
<button type="button" class="remove-material button button-small">删除</button>
</div>`;
$(this).before(materialRow);
});
// 删除材料行
$(document).on('click', '.remove-material', function() {
$(this).closest('.material-row').remove();
});
// 表单提交处理
$('#craft-cost-form').submit(function(e) {
e.preventDefault();
var formData = $(this).serialize();
$.ajax({
url: '<?php echo admin_url('admin-ajax.php'); ?>',
type: 'POST',
data: formData + '&action=calculate_craft_cost',
dataType: 'json',
success: function(response) {
if (response.success) {
displayResults(response.data);
} else {
alert('计算失败: ' + response.message);
}
}
});
});
// 显示计算结果
function displayResults(data) {
// 填充成本明细
var costHtml = '';
costHtml += `<tr><td>工序成本</td><td>¥${data.unit_costs.process.toFixed(2)}</td><td>¥${data.batch_costs.process.toFixed(2)}</td></tr>`;
costHtml += `<tr><td>材料成本</td><td>¥${data.unit_costs.material.toFixed(2)}</td><td>¥${data.batch_costs.material.toFixed(2)}</td></tr>`;
costHtml += `<tr><td>附加成本</td><td>¥${data.unit_costs.additional.toFixed(2)}</td><td>¥${data.batch_costs.additional.toFixed(2)}</td></tr>`;
costHtml += `<tr class="total-row"><td><strong>总成本</strong></td><td><strong>¥${data.unit_costs.total.toFixed(2)}</strong></td><td><strong>¥${data.batch_costs.total.toFixed(2)}</strong></td></tr>`;
$('#cost-details').html(costHtml);
$('#unit-total-cost').text(data.unit_costs.total.toFixed(2));
$('#batch-total-cost').text(data.batch_costs.total.toFixed(2));
$('#profit-percentage').text(data.profit_margin);
$('#suggested-price').text(data.suggested_price.toFixed(2));
$('#calculation-results').show();
$('html, body').animate({
scrollTop: $('#calculation-results').offset().top
}, 500);
}
// 保存计算结果
$('#save-calculation').click(function() {
var orderData = {
action: 'save_craft_cost',
order_id: $('#order_id').val(),
product_name: $('#product_name').val(),
calculation_data: window.lastCalculationData // 假设已存储
};
$.ajax({
url: '<?php echo admin_url('admin-ajax.php'); ?>',
type: 'POST',
data: orderData,
success: function(response) {
if (response.success) {
alert('计算结果已保存!');
}
}
});
});
});
</script>
<style>
.craft-cost-calculator {
max-width: 1000px;
margin: 20px auto;
padding: 20px;
background: #f9f9f9;
border-radius: 8px;
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
}
.form-section {
margin-bottom: 30px;
padding: 20px;
background: white;
border-radius: 5px;
border-left: 4px solid #0073aa;
}
.form-group {
margin-bottom: 15px;
}
.form-group label {
display: block;
margin-bottom: 5px;
font-weight: bold;
}
.form-group input, .form-group select {
width: 100%;
padding: 8px;
border: 1px solid #ddd;
border-radius: 4px;
}
.process-item {
padding: 10px;
border-bottom: 1px solid #eee;
}
.process-details {
margin-top: 10px;
padding: 10px;
background: #f5f5f5;
border-radius: 4px;
}
.material-row {
display: flex;
flex-wrap: wrap;
gap: 10px;
align-items: center;
padding: 10px;
background: #f0f7ff;
margin-bottom: 10px;
border-radius: 4px;
}
.form-group-inline {
flex: 1;
min-width: 150px;
}
.cost-breakdown {
width: 100%;
border-collapse: collapse;
margin: 15px 0;
}
.cost-breakdown th, .cost-breakdown td {
padding: 10px;
border: 1px solid #ddd;
text-align: right;
}
.cost-breakdown th:first-child, .cost-breakdown td:first-child {
text-align: left;
}
.total-row {
background-color: #f0f7ff;
font-weight: bold;
}
.pricing-suggestion {
background: #e7f4e4;
padding: 20px;
border-radius: 5px;
border-left: 4px solid #46b450;
}
.form-actions {
text-align: center;
margin-top: 20px;
padding-top: 20px;
border-top: 1px solid #eee;
}
</style>
<?php
return ob_get_clean();
}
add_shortcode('craft_cost_calculator', 'craft_cost_calculator_form_shortcode');
?>
## 五、AJAX处理与数据保存
### 5.1 AJAX请求处理
<?php
/**
- AJAX处理:成本计算
*/
add_action('wp_ajax_calculate_craft_cost', 'ajax_calculate_craft_cost');
add_action('wp_ajax_nopriv_calculate_craft_cost', 'ajax_calculate_craft_cost');
function ajax_calculate_craft_cost() {
// 验证nonce
if (!wp_verify_nonce($_POST['craft_cost_nonce'], 'craft_cost_calculate')) {
wp_die('安全验证失败');
}
$order_id = sanitize_text_field($_POST['order_id']);
$product_name = sanitize_text_field($_POST['product_name']);
$quantity = intval($_POST['quantity']);
$profit_margin = floatval($_POST['profit_margin']);
$additional_cost = floatval($_POST['additional_cost']);
// 创建计算器实例
$calculator = new CraftCostCalculator($order_id, $quantity);
// 添加工序
if (!empty($_POST['processes'])) {
foreach ($_POST['processes'] as $process_id) {
$process_id = intval($process_id);
$actual_time = isset($_POST['process_time'][$process_id]) ?
floatval($_POST['process_time'][$process_id]) : null;
$difficulty = isset($_POST['process_difficulty'][$process_id]) ?
floatval($_POST['process_difficulty'][$process_id]) : null;
$calculator->add_process($process_id, $actual_time, $difficulty);
}
}
// 添加材料
if (!empty($_POST['materials'])) {
foreach ($_POST['materials'] as $material) {
// 这里简化处理,实际应用中应从材料库选择
$material_cost = floatval($material['price']) * floatval($material['amount']);
// 可以扩展为使用材料ID
}
}
// 设置附加成本
$calculator->set_additional_cost($additional_cost);
// 计算成本
$result = $calculator->calculate();
// 计算建议售价
$suggested_price = $calculator->calculate_suggested_price(
$profit_margin,
$result['batch_costs']['total']
);
// 返回结果
wp_send_json_success(array(
'unit_costs' => $result['unit_costs'],
'batch_costs' => $result['batch_costs'],
'profit_margin' => $profit_margin,
'suggested_price' => $suggested_price,
'order_id' => $order_id,
'product_name' => $product_name
));
}
/**
- AJAX处理:保存计算结果
*/
add_action('wp_ajax_save_craft_cost', 'ajax_save_craft_cost');
function ajax_save_craft_cost() {
global $wpdb;
$order_id = sanitize_text_field($_POST['order_id']);
$product_name = sanitize_text_field($_POST['product_name']);
// 这里简化处理,实际应保存完整计算数据
$order_cost_table = $wpdb->prefix . 'craft_order_costs';
$existing = $wpdb->get_var($wpdb->prepare(
"SELECT COUNT(*) FROM $order_cost_table WHERE order_id = %s",
$order_id
));
if ($existing > 0) {
wp_send_json_error(array('message' => '订单编号已存在'));
}
// 实际应用中应保存完整的计算数据
$wpdb->insert(
$order_cost_table,
array(
'order_id' => $order_id,
'product_name' => $product_name,
'calculated_at' => current_time('mysql')
)
);
wp_send_json_success(array('message' => '保存成功'));
}
?>
## 六、成本报告与数据分析
### 6.1 成本报告生成
<?php
/**
- 生成成本报告PDF
*/
function generate_cost_report_pdf($order_cost_id) {
global $wpdb;
$order_cost_table = $wpdb->prefix . 'craft_order_costs';
$order_process_table = $wpdb->prefix . 'craft_order_processes';
$process_table = $wpdb->prefix . 'craft_processes';
// 获取订单成本数据
$order_cost = $wpdb->get_row($wpdb->prepare(
"SELECT * FROM $order_cost_table WHERE id = %d",
$order_cost_id
));
if (!$order_cost) {
return false;
}
// 获取工序明细
$process_details = $wpdb->get_results($wpdb->prepare(
"SELECT op.*, p.process_name
FROM $order_process_table op
JOIN $process_table p ON op.process_id = p.id
WHERE op.order_cost_id = %d",
$order_cost_id
));
// 生成HTML报告
$html = '
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>工艺成本核算报告 - ' . esc_html($order_cost->order_id) . '</title>
<style>
body { font-family: "Microsoft YaHei", sans-serif; }
.report-header { text-align: center; margin-bottom: 30px; }
.report-title { font-size: 24px; color: #333; }
.order-info { margin: 20px 0; }
.cost-table { width: 100%; border-collapse: collapse; margin: 20px 0; }
.cost-table th, .cost-table td {
border: 1px solid #ddd;
padding: 10px;
text-align: right;
}
.cost-table th:first-child, .cost-table td:first-child { text-align: left; }
.summary { background: #f9f9f9; padding: 20px; margin-top: 30px; }
.footer { text-align: center; margin-top: 50px; color: #666; }
</style>
</head>
<body>
<div class="report-header">
<h1 class="report-title">工艺成本核算报告</h1>
<p>生成时间: ' . date('Y年m月d日 H:i:s') . '</p>
</div>
<div class="order-info">
<h3>订单信息</h3>
<p>订单编号: ' . esc_html($order_cost->order_id) . '</p>
<p>产品名称: ' . esc_html($order_cost->product_name) . '</p>
<p>生产数量: ' . esc_html($order_cost->quantity) . '</p>
<p>核算日期: ' . esc_html($order_cost->calculated_at) . '</p>
</div>
<h3>工序成本明细</h3>
<table class="cost-table">
<thead>
<tr>
<th>工序名称</th>
<th>实际工时(小时)</th>
<th>难度系数</th>
<th>工序成本(元)</th>
</tr>
</thead>
<tbody>';
$total_process_cost = 0;
foreach ($process_details as $process) {
$html .= '
<tr>
<td>' . esc_html($process->process_name) . '</td>
<td>' . number_format($process->actual_time, 2) . '</td>
<td>' . number_format($process->difficulty_adjustment, 2) . '</td>
<td>' . number_format($process->process_cost, 2) . '</td>
</tr>';
$total_process_cost += $process->process_cost;
}
$html .= '
</tbody>
</table>
<div class="summary">
<h3>成本汇总</h3>
<table class="cost-table">
<tr>
<td>工序总成本</td>
<td>¥' . number_format($order_cost->total_process_cost, 2) . '</td>
</tr>
<tr>
<td>材料总成本</td>
<td>¥' . number_format($order_cost->total_material_cost, 2) . '</td>
</tr>
<tr>
<td>附加成本</td>
<td>¥' . number_format($order_cost->additional_cost, 2) . '</td>
</tr>
<tr style="font-weight: bold;">
<td>总成本</td>
<td>¥' . number_format($order_cost->total_cost, 2) . '</td>
</tr>
<tr>
<td>利润率</td>
<td>' . number_format($order_cost->profit_margin, 2) . '%</td>
</tr>
<tr style="font-weight: bold; color


