| @@ -9,8 +9,9 @@ | ||
| 9 | 9 | use FluentCart\Api\StoreSettings; |
| 10 | 10 | use FluentCart\App\App; |
| 11 | 11 | use FluentCart\App\Models\CustomerAddresses; |
| 12 | 12 | use FluentCart\App\Models\Order; |
| 13 | +use FluentCart\App\Models\OrderAddress; | |
| 13 | 14 | use FluentCart\App\Models\ShippingMethod; |
| 14 | 15 | use FluentCart\App\Services\Localization\LocalizationManager; |
| 15 | 16 | use FluentCart\App\Services\Renderer\CheckoutFieldsSchema; |
| 16 | 17 | use FluentCart\Framework\Support\Arr; |
| @@ -92,8 +93,26 @@ | ||
| 92 | 93 | } |
| 93 | 94 | } |
| 94 | 95 | } |
| 95 | 96 | |
| 97 | + /** | |
| 98 | + * Copy an existing OrderAddress's meta (company/VAT/legal-reg id/label) onto the | |
| 99 | + * order-address row of the same type already created for $orderId. insertOrderAddresses() | |
| 100 | + * only accepts flat column data, so a parent-order snapshot copy (renewals) must | |
| 101 | + * carry meta over separately. | |
| 102 | + */ | |
| 103 | + public static function copyOrderAddressMeta($orderId, $type, $sourceAddress): void | |
| 104 | + { | |
| 105 | + if (!$sourceAddress || empty($sourceAddress->meta)) { | |
| 106 | + return; | |
| 107 | + } | |
| 108 | + | |
| 109 | + OrderAddress::query() | |
| 110 | + ->where('order_id', $orderId) | |
| 111 | + ->where('type', $type) | |
| 112 | + ->update(['meta' => json_encode($sourceAddress->meta)]); | |
| 113 | + } | |
| 114 | + | |
| 96 | 115 | public static function getIpAddress($anonymize = false) |
| 97 | 116 | { |
| 98 | 117 | static $ipAddress; |
| 99 | 118 | |
| @@ -573,11 +592,22 @@ | ||
| 573 | 592 | |
| 574 | 593 | if ($addressModel && $addressModel->customer_id == $currentCustomer->id) { |
| 575 | 594 | $addressData = $addressModel->getFormattedDataForCheckout($type . '_'); |
| 576 | 595 | |
| 596 | + $businessInfoFields = [$type . '_company_name', $type . '_legal_registration_id']; | |
| 577 | 597 | foreach ($addressData as $key => $value) { |
| 578 | - if (!isset($data[$key]) || $data[$key] === '' || $data[$key] !== $value) { | |
| 598 | + if ($value !== '') { | |
| 599 | + if (in_array($key, $businessInfoFields) && !empty($data[$key])) { | |
| 600 | + continue; | |
| 601 | + } | |
| 579 | 602 | $data[$key] = $value; |
| 603 | + } | |
| 604 | + } | |
| 605 | + | |
| 606 | + if ($type === 'billing') { | |
| 607 | + $vatNumber = Arr::get($addressData, 'billing_vat_number', ''); | |
| 608 | + if ((!isset($data['fct_billing_tax_id']) || $data['fct_billing_tax_id'] === '') && $vatNumber !== '') { | |
| 609 | + $data['fct_billing_tax_id'] = $vatNumber; | |
| 580 | 610 | } |
| 581 | 611 | } |
| 582 | 612 | } |
| 583 | 613 | } |