diff --git a/extraScripts/arcwoc/admin/services/class-hesabix-v2-sync-service.php b/extraScripts/arcwoc/admin/services/class-hesabix-v2-sync-service.php index d912fd61..e9ad2b96 100755 --- a/extraScripts/arcwoc/admin/services/class-hesabix-v2-sync-service.php +++ b/extraScripts/arcwoc/admin/services/class-hesabix-v2-sync-service.php @@ -1076,7 +1076,8 @@ class Hesabix_V2_Sync_Service try { $existing_mapping = $this->db->get_mapping('order', $order_id); - $saved = $existing_mapping ? null : $this->db->get_or_start_invoice_create($order_id); + $hid = absint($existing_mapping['hesabix_id'] ?? 0); + $saved = $hid > 0 ? null : $this->db->get_or_start_invoice_create($order_id); if ($saved !== null) { return array( 'success' => true, @@ -1171,27 +1172,22 @@ class Hesabix_V2_Sync_Service $person_id, $gate['factor'], $gate['currency_id'], - $fiscal_dates ? $fiscal_dates : null + $fiscal_dates ? $fiscal_dates : null, + $hid === 0 ), $order ); self::assert_invoice_lines_have_warehouse_when_required($order, $invoice_data); - $existing_mapping = $this->db->get_mapping('order', $order_id); - $hid = null; - if ($existing_mapping && !empty($existing_mapping['hesabix_id'])) { - $hid = absint($existing_mapping['hesabix_id']); - } - return array( 'success' => true, 'order_id' => (int) $order_id, 'order' => $order, 'person_id' => $person_id, 'invoice_data' => $invoice_data, - 'is_new_invoice' => !$existing_mapping, - 'hesabix_invoice_id' => $hid && $hid > 0 ? $hid : null, + 'is_new_invoice' => $hid === 0, + 'hesabix_invoice_id' => $hid > 0 ? $hid : null, 'fiscal_note' => isset($fiscal['note']) ? (string) $fiscal['note'] : '', ); } catch (Exception $e) { @@ -1593,7 +1589,8 @@ class Hesabix_V2_Sync_Service $person_id, $gate['factor'], $gate['currency_id'], - $fiscal_dates ? $fiscal_dates : null + $fiscal_dates ? $fiscal_dates : null, + !$existing_mapping ), $order ); diff --git a/extraScripts/arcwoc/includes/class-hesabix-v2-mapper.php b/extraScripts/arcwoc/includes/class-hesabix-v2-mapper.php index de305994..0324f01c 100755 --- a/extraScripts/arcwoc/includes/class-hesabix-v2-mapper.php +++ b/extraScripts/arcwoc/includes/class-hesabix-v2-mapper.php @@ -545,9 +545,10 @@ class Hesabix_V2_Mapper * @param float|null $amount_factor ضریب مبلغ (مثلاً ۱۰ برای تومان→ریال). * @param int|null $invoice_currency_id شناسهٔ ارز در حسابیکس؛ اگر تهی باشد از تنظیمات/پیش‌فرض حل می‌شود. * @param array|null $date_overrides اختیاری: `document_date` (Y-m-d)، `payment_date_ymd` (Y-m-d برای بخش تاریخ transaction_date پرداخت). + * @param bool $new_sale Map stock-tracked native variants in main units for the first CREATE. * @return array */ - public static function wc_order_to_invoice($order, $person_id, $amount_factor = 1.0, $invoice_currency_id = null, $date_overrides = null) + public static function wc_order_to_invoice($order, $person_id, $amount_factor = 1.0, $invoice_currency_id = null, $date_overrides = null, $new_sale = false) { $f = (float) $amount_factor; if ($f <= 0 || is_nan($f)) { @@ -663,7 +664,7 @@ class Hesabix_V2_Mapper 'line_discount' => 0, 'tax_amount' => $tax_amount, 'line_total' => $line_total, - 'unit' => 'عدد', + 'unit' => $new_sale && $native_variant_id > 0 && !$product->is_virtual() ? 'main' : 'عدد', 'unit_price_source' => 'base', 'discount_type' => 'amount', 'discount_value' => 0,