ifnex/03_WordPress/wp-content/plugins/ifnex-bridge/includes/tracking-form.php
Kazem Alghasi b35e933475 Phase 3.5 — Core Features Complete:
 Multi-Package Support (3.5.1)
- Add shipment_packages migration and ShipmentPackage model
- Add packages() relation to Shipment model
- Update CustomerOrderController to accept packages[] array
- Auto-calculate volumetric weight from dimensions (L*W*H/5000)
- Repeater UI for adding/removing packages
- Real-time summary of total weights

 Invoice Form for PARCEL (3.5.2)
- New step in order form (only for PARCEL type)
- Customs items: description, HS Code, quantity, unit price (USD)
- Auto-calculate total per item and invoice total
- Maximum 9 items
- 5-step order form structure

 PDF Redesign (3.5.4)
- Install picqer/php-barcode-generator
- Redesign AWB with barcode, From/To, Value, Service, Type
- Redesign Invoice with 9-row table, legal declaration
- Redesign Label to A5 landscape with large barcode
- DOC/PARCEL condition (only PARCEL has invoice)
- Barcode as base64 embed

 Technical Fixes (3.5.5)
- Add Enum casts to Shipment model (status, direction, type)
- Fix deselectAfterCompletion -> deselectRecordsAfterCompletion
- Migration for shipment_items (row_number, unit_price)
- Migration to make name nullable
- Fix Enum method names (DocNormal/DocEconomy)

 Tracking Test (3.5.6)
- Update TrackController with status_label
- Redesign tracking page with beautiful timeline
- Status box with colored badge
- Timeline with date, description, location, source
- Carrier mappings section

Documentation:
- Update README, Laravel_README, Roadmap, Checklist
- Add IFNEX_Meeting_Summary.md for client presentation
- Update IFNEX_I18N_Strategy.md (moved to deploy phase)

Phase 3.5 — 90% complete (i18n moved to deploy phase)"
2026-08-29 08:35:34 +03:30

481 lines
18 KiB
PHP

<?php
/**
* IFNEX Tracking Form Handler
* JavaScript و منطق فرم رهگیری
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
add_action('wp_footer', 'ifnex_tracking_form_scripts');
function ifnex_tracking_form_scripts() {
global $post;
if (is_a($post, 'WP_Post') && has_shortcode($post->post_content, 'ifnex_tracking_form')) {
?>
<script>
document.addEventListener('DOMContentLoaded', function() {
var form = document.getElementById('ifnex-tracking-form');
if (!form) return;
form.addEventListener('submit', function(e) {
e.preventDefault();
var input = document.getElementById('ifnex-awb-input');
var resultDiv = document.getElementById('ifnex-tracking-result');
var btn = document.getElementById('ifnex-track-btn');
var awb = input.value.trim();
if (!awb) {
resultDiv.innerHTML = '<p class="ifnex-error">لطفاً شماره بارنامه را وارد کنید.</p>';
resultDiv.style.display = 'block';
return;
}
btn.disabled = true;
btn.textContent = 'در حال جستجو...';
resultDiv.style.display = 'block';
resultDiv.innerHTML = '<p>⏳ در حال جستجو...</p>';
var xhr = new XMLHttpRequest();
xhr.open('POST', '<?php echo esc_url(admin_url("admin-ajax.php")); ?>', true);
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xhr.onload = function() {
btn.disabled = false;
btn.textContent = 'رهگیری';
if (xhr.status === 200) {
try {
var data = JSON.parse(xhr.responseText);
if (data.success) {
resultDiv.innerHTML = data.html;
} else {
resultDiv.innerHTML = '<p class="ifnex-error">' + (data.message || 'خطا') + '</p>';
}
} catch (err) {
resultDiv.innerHTML = '<p class="ifnex-error">پاسخ نامعتبر از سرور.</p>';
}
} else {
resultDiv.innerHTML = '<p class="ifnex-error">خطا در ارتباط با سرور.</p>';
}
};
xhr.onerror = function() {
btn.disabled = false;
btn.textContent = 'رهگیری';
resultDiv.innerHTML = '<p class="ifnex-error">خطا در ارتباط با سرور.</p>';
};
xhr.send('action=ifnex_track_shipment&awb=' + encodeURIComponent(awb));
});
});
</script>
<style>
.ifnex-tracking-container {
max-width: 700px;
margin: 20px auto;
padding: 20px;
font-family: inherit;
}
.ifnex-tracking-form {
margin-bottom: 20px;
}
.ifnex-form-group {
display: flex;
gap: 10px;
}
.ifnex-form-group input {
flex: 1;
padding: 12px 16px;
border: 2px solid #e2e8f0;
border-radius: 8px;
font-size: 16px;
transition: border-color 0.2s;
}
.ifnex-form-group input:focus {
outline: none;
border-color: #f37021;
}
.ifnex-form-group button {
padding: 12px 28px;
background: #f37021;
color: white;
border: none;
border-radius: 8px;
cursor: pointer;
font-size: 16px;
font-weight: bold;
transition: background 0.2s;
}
.ifnex-form-group button:hover:not(:disabled) {
background: #e25f12;
}
.ifnex-form-group button:disabled {
background: #ccc;
cursor: not-allowed;
}
.ifnex-tracking-result {
margin-top: 20px;
}
.ifnex-error {
color: #dc2626;
background: #fef2f2;
padding: 12px 16px;
border-radius: 8px;
border: 1px solid #fecaca;
}
/* ─── Status Box ─── */
.ifnex-status-box {
background: #f8fafc;
border: 1px solid #e2e8f0;
border-radius: 12px;
padding: 20px;
margin-bottom: 20px;
}
.ifnex-status-header {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 15px;
padding-bottom: 15px;
border-bottom: 2px solid #f37021;
}
.ifnex-status-title {
font-size: 18px;
font-weight: bold;
color: #1e293b;
margin: 0;
}
.ifnex-status-badge {
display: inline-block;
padding: 6px 16px;
background: #f37021;
color: white;
border-radius: 20px;
font-size: 13px;
font-weight: bold;
}
.ifnex-status-badge.status-pending_payment { background: #f59e0b; }
.ifnex-status-badge.status-processed { background: #64748b; }
.ifnex-status-badge.status-picked_up { background: #3b82f6; }
.ifnex-status-badge.status-in_transit { background: #8b5cf6; }
.ifnex-status-badge.status-out_for_delivery { background: #06b6d4; }
.ifnex-status-badge.status-delivered { background: #10b981; }
.ifnex-status-badge.status-failed { background: #ef4444; }
.ifnex-status-badge.status-cancelled { background: #ef4444; }
.ifnex-status-badge.status-returned { background: #f59e0b; }
.ifnex-details-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
gap: 12px;
}
.ifnex-detail-item {
display: flex;
flex-direction: column;
}
.ifnex-detail-label {
font-size: 11px;
color: #64748b;
text-transform: uppercase;
margin-bottom: 4px;
font-weight: 600;
}
.ifnex-detail-value {
font-size: 14px;
color: #1e293b;
font-weight: 500;
}
/* ─── Timeline ─── */
.ifnex-timeline-section {
background: white;
border: 1px solid #e2e8f0;
border-radius: 12px;
padding: 20px;
}
.ifnex-timeline-title {
font-size: 16px;
font-weight: bold;
color: #1e293b;
margin: 0 0 20px 0;
padding-bottom: 10px;
border-bottom: 1px solid #e2e8f0;
}
.ifnex-timeline {
position: relative;
padding-right: 30px;
}
.ifnex-timeline::before {
content: '';
position: absolute;
right: 10px;
top: 10px;
bottom: 10px;
width: 2px;
background: #e2e8f0;
}
.ifnex-timeline-item {
position: relative;
padding-bottom: 24px;
padding-right: 20px;
}
.ifnex-timeline-item:last-child {
padding-bottom: 0;
}
.ifnex-timeline-dot {
position: absolute;
right: -25px;
top: 4px;
width: 14px;
height: 14px;
border-radius: 50%;
background: #f37021;
border: 3px solid white;
box-shadow: 0 0 0 2px #f37021;
}
.ifnex-timeline-item:first-child .ifnex-timeline-dot {
background: #10b981;
box-shadow: 0 0 0 2px #10b981;
}
.ifnex-timeline-date {
font-size: 12px;
color: #64748b;
margin-bottom: 4px;
font-weight: 600;
}
.ifnex-timeline-desc {
font-size: 14px;
color: #1e293b;
margin-bottom: 4px;
font-weight: 500;
}
.ifnex-timeline-location {
font-size: 12px;
color: #f37021;
display: inline-flex;
align-items: center;
gap: 4px;
}
.ifnex-timeline-source {
font-size: 10px;
color: #94a3b8;
text-transform: uppercase;
margin-top: 4px;
display: inline-block;
padding: 2px 6px;
background: #f1f5f9;
border-radius: 4px;
}
.ifnex-empty-timeline {
text-align: center;
color: #94a3b8;
padding: 30px;
font-size: 14px;
}
</style>
<?php
}
}
// AJAX Handler
add_action('wp_ajax_ifnex_track_shipment', 'ifnex_track_shipment_ajax');
add_action('wp_ajax_nopriv_ifnex_track_shipment', 'ifnex_track_shipment_ajax');
function ifnex_track_shipment_ajax() {
$awb = isset($_POST['awb']) ? sanitize_text_field($_POST['awb']) : '';
if (empty($awb)) {
wp_send_json(array('success' => false, 'message' => 'شماره بارنامه وارد نشده است.'));
return;
}
$client = new IFNEX_API_Client();
$result = $client->track_shipment($awb);
if (is_wp_error($result)) {
wp_send_json(array('success' => false, 'message' => $result->get_error_message()));
return;
}
// پشتیبانی از هر دو فرمت خروجی API (data.shipment یا مستقیم)
$shipment = $result['data']['shipment'] ?? $result;
$events = $result['data']['tracking_events'] ?? $result['timeline'] ?? array();
$carriers = $result['data']['carrier_mappings'] ?? $result['carrier_mappings'] ?? array();
// ساخت HTML نتیجه
ob_start();
?>
<div class="ifnex-status-box">
<div class="ifnex-status-header">
<h3 class="ifnex-status-title">📦 مرسوله <?php echo esc_html($shipment['awb_no'] ?? ''); ?></h3>
<?php
$status = $shipment['status'] ?? $shipment['status_label'] ?? 'unknown';
$statusLabel = $shipment['status_label'] ?? ucwords(str_replace('_', ' ', $status));
?>
<span class="ifnex-status-badge status-<?php echo esc_attr($status); ?>">
<?php echo esc_html($statusLabel); ?>
</span>
</div>
<div class="ifnex-details-grid">
<div class="ifnex-detail-item">
<span class="ifnex-detail-label">نوع</span>
<span class="ifnex-detail-value"><?php echo esc_html($shipment['type'] ?? '—'); ?></span>
</div>
<div class="ifnex-detail-item">
<span class="ifnex-detail-label">جهت</span>
<span class="ifnex-detail-value"><?php echo esc_html(ucfirst($shipment['direction'] ?? '—')); ?></span>
</div>
<div class="ifnex-detail-item">
<span class="ifnex-detail-label">فرستنده</span>
<span class="ifnex-detail-value"><?php echo esc_html($shipment['sender_name'] ?? $shipment['sender']['name'] ?? '—'); ?></span>
</div>
<div class="ifnex-detail-item">
<span class="ifnex-detail-label">گیرنده</span>
<span class="ifnex-detail-value"><?php echo esc_html($shipment['receiver_name'] ?? $shipment['receiver']['name'] ?? '—'); ?></span>
</div>
<div class="ifnex-detail-item">
<span class="ifnex-detail-label">مبدأ</span>
<span class="ifnex-detail-value"><?php echo esc_html($shipment['from_country'] ?? '—'); ?></span>
</div>
<div class="ifnex-detail-item">
<span class="ifnex-detail-label">مقصد</span>
<span class="ifnex-detail-value"><?php echo esc_html($shipment['to_country'] ?? '—'); ?></span>
</div>
<div class="ifnex-detail-item">
<span class="ifnex-detail-label">وزن</span>
<span class="ifnex-detail-value"><?php echo esc_html($shipment['weight'] ?? '—'); ?> kg</span>
</div>
<div class="ifnex-detail-item">
<span class="ifnex-detail-label">تاریخ ثبت</span>
<span class="ifnex-detail-value"><?php echo esc_html(date('Y/m/d', strtotime($shipment['created_at'] ?? 'now'))); ?></span>
</div>
</div>
</div>
<?php if (!empty($events) && is_array($events)): ?>
<div class="ifnex-timeline-section">
<h4 class="ifnex-timeline-title">🚚 تاریخچه رهگیری</h4>
<div class="ifnex-timeline">
<?php
// مرتب‌سازی از قدیم به جدید (اولین رویداد در بالا)
$sortedEvents = array_reverse($events);
foreach ($sortedEvents as $event):
$date = $event['date'] ?? $event['event_date'] ?? '';
$time = $event['time'] ?? $event['event_time'] ?? '';
$location = $event['location'] ?? '';
$description = $event['description'] ?? $event['event_description'] ?? '';
$source = $event['source'] ?? '';
?>
<div class="ifnex-timeline-item">
<div class="ifnex-timeline-dot"></div>
<div class="ifnex-timeline-date">
<?php
$dateStr = '';
if ($date) {
$dateStr = date('Y/m/d', strtotime($date));
if ($time) $dateStr .= ' - ' . substr($time, 0, 5);
}
echo esc_html($dateStr);
?>
</div>
<?php if ($description): ?>
<div class="ifnex-timeline-desc"><?php echo esc_html($description); ?></div>
<?php endif; ?>
<?php if ($location): ?>
<div class="ifnex-timeline-location">📍 <?php echo esc_html($location); ?></div>
<?php endif; ?>
<?php if ($source): ?>
<span class="ifnex-timeline-source"><?php echo esc_html($source); ?></span>
<?php endif; ?>
</div>
<?php endforeach; ?>
</div>
</div>
<?php else: ?>
<div class="ifnex-timeline-section">
<h4 class="ifnex-timeline-title">🚚 تاریخچه رهگیری</h4>
<div class="ifnex-empty-timeline">
هنوز رویداد ترکینگی ثبت نشده است.
</div>
</div>
<?php endif; ?>
<?php if (!empty($carriers) && is_array($carriers)): ?>
<div class="ifnex-timeline-section" style="margin-top: 16px;">
<h4 class="ifnex-timeline-title">🏢 شرکت‌های حمل</h4>
<?php foreach ($carriers as $carrier): ?>
<div style="padding: 10px; background: #f8fafc; border-radius: 8px; margin-bottom: 8px;">
<strong><?php echo esc_html($carrier['carrier_code'] ?? '—'); ?></strong>
<?php if (!empty($carrier['carrier_tracking_number'])): ?>
| کد رهگیری: <?php echo esc_html($carrier['carrier_tracking_number']); ?>
<?php endif; ?>
<?php if (!empty($carrier['delivery_status'])): ?>
| وضعیت: <?php echo esc_html($carrier['delivery_status']); ?>
<?php endif; ?>
</div>
<?php endforeach; ?>
</div>
<?php endif; ?>
<?php
$html = ob_get_clean();
wp_send_json(array('success' => true, 'html' => $html));
}
// AJAX Handler: موجودی کیف پول
add_action('wp_ajax_ifnex_get_wallet_balance', 'ifnex_get_wallet_balance_ajax');
add_action('wp_ajax_nopriv_ifnex_get_wallet_balance', 'ifnex_get_wallet_balance_ajax');
function ifnex_get_wallet_balance_ajax() {
if (!is_user_logged_in()) {
wp_send_json(array('success' => false, 'message' => 'برای مشاهده موجودی باید وارد شوید.'));
return;
}
$bridge = new IFNEX_User_Bridge();
$balance = $bridge->get_wallet_balance(get_current_user_id());
if (is_wp_error($balance)) {
wp_send_json(array('success' => false, 'message' => $balance->get_error_message()));
return;
}
wp_send_json(array(
'success' => true,
'balance' => $balance['balance'],
'formatted_balance' => number_format($balance['balance']) . ' ریال',
'total_deposited' => number_format($balance['total_deposited']) . ' ریال',
'total_withdrawn' => number_format($balance['total_withdrawn']) . ' ریال',
'is_frozen' => $balance['is_frozen'],
));
}
// AJAX Handler: لیست تراکنش‌ها
add_action('wp_ajax_ifnex_get_transactions', 'ifnex_get_transactions_ajax');
add_action('wp_ajax_nopriv_ifnex_get_transactions', 'ifnex_get_transactions_ajax');
function ifnex_get_transactions_ajax() {
if (!is_user_logged_in()) {
wp_send_json(array('success' => false, 'message' => 'برای مشاهده تراکنش‌ها باید وارد شوید.'));
return;
}
$per_page = isset($_POST['per_page']) ? intval($_POST['per_page']) : 10;
$bridge = new IFNEX_User_Bridge();
$transactions = $bridge->get_transactions(get_current_user_id(), $per_page);
if (is_wp_error($transactions)) {
wp_send_json(array('success' => false, 'message' => $transactions->get_error_message()));
return;
}
wp_send_json(array(
'success' => true,
'transactions' => $transactions['transactions'] ?? array(),
));
}