/** * Load member and associated address record data into the form fields. * * @param int $fromMember * @param int $mergeStrategy * @param null $fieldList * @return $this|Form * @internal param array|DataObject $action */ public function loadDataFrom($fromMember, $mergeStrategy = 0, $fieldList = NULL) { if ($fromMember instanceof Member) { $shippingAddress = $fromMember->ShippingAddress(); if (!$shippingAddress) { $shippingAddress = new Address_Shipping(['FirstName' => $fromMember->FirstName, 'Surname' => $fromMember->Surname]); $shippingAddress->Default = true; $shippingAddress->write(); $fromMember->ShippingAddresses()->add($shippingAddress); } $billingAddress = $fromMember->BillingAddress(); if (!$billingAddress) { $billingAddress = new Address_Billing(array('FirstName' => $fromMember->FirstName, 'Surname' => $fromMember->Surname)); $billingAddress->Default = true; $billingAddress->write(); $fromMember->BillingAddresses()->add($billingAddress); } $formData = $fromMember->toMap(); foreach ($shippingAddress->toMap() as $fieldName => $value) { $formData["ShippingAddress.{$fieldName}"] = $shippingAddress->{$fieldName}; } foreach ($billingAddress->toMap() as $fieldName => $value) { $formData["BillingAddress.{$fieldName}"] = $billingAddress->{$fieldName}; } $formData = self::filter_data('update', $formData); parent::loadDataFrom($formData, $mergeStrategy); } return $this; }
/** * Adds phone numbers from Order to shipping and billing address records. * * @param $order */ protected function updateAddresses($order) { $data = $order->toMap(); // this is for matching existing address, we don't match on phone numbers though $shippingAddress = Address_Shipping::create(array('MemberID' => $this->owner->Member()->ID, 'FirstName' => $data['ShippingFirstName'], 'Surname' => $data['ShippingSurname'], 'Company' => $data['ShippingCompany'], 'Address' => $data['ShippingAddress'], 'AddressLine2' => $data['ShippingAddressLine2'], 'City' => $data['ShippingCity'], 'PostalCode' => $data['ShippingPostalCode'], 'State' => $data['ShippingState'], 'CountryName' => $data['ShippingCountryName'], 'CountryCode' => $data['ShippingCountryCode'], 'RegionName' => isset($data['ShippingRegionName']) ? $data['ShippingRegionName'] : null, 'RegionCode' => isset($data['ShippingRegionCode']) ? $data['ShippingRegionCode'] : null)); $newShipping = array_filter($shippingAddress->toMap()); $billingAddress = Address_Billing::create(array('MemberID' => $this->owner->Member()->ID, 'FirstName' => $data['BillingFirstName'], 'Surname' => $data['BillingSurname'], 'Company' => $data['BillingCompany'], 'Address' => $data['BillingAddress'], 'AddressLine2' => $data['BillingAddressLine2'], 'City' => $data['BillingCity'], 'PostalCode' => $data['BillingPostalCode'], 'State' => $data['BillingState'], 'CountryName' => $data['BillingCountryName'], 'CountryCode' => $data['BillingCountryCode'], 'RegionName' => isset($data['BillingRegionName']) ? $data['ShippingRegionName'] : null, 'RegionCode' => isset($data['BillingRegionCode']) ? $data['ShippingRegionCode'] : null)); $newBilling = array_filter($billingAddress->toMap()); foreach ($this->owner->Member()->ShippingAddresses() as $address) { $existing = array_filter($address->toMap()); $result = array_intersect_assoc($existing, $newShipping); //If no difference, then match is found $diff = array_diff_assoc($newShipping, $result); $match = empty($diff); if ($match) { $address->StreakPhone = $order->ShippingStreakPhone; $address->StreakMobile = $order->ShippingStreakMobile; $address->write(); } } foreach ($this->owner->Member()->BillingAddresses() as $address) { $existing = array_filter($address->toMap()); $result = array_intersect_assoc($existing, $newBilling); $diff = array_diff_assoc($newBilling, $result); $match = empty($diff); if ($match) { $address->StreakPhone = $order->BillingStreakPhone; $address->StreakMobile = $order->BillingStreakMobile; $address->write(); } } }
public function createAddresses($order) { //Find identical addresses //If none exist then create a new address and set it as default //Default is not used when comparing $data = $order->toMap(); // Set Firstname/Surname Fields on Member table if (!$this->owner->FirstName) { $this->owner->FirstName = $data['ShippingFirstName']; $this->owner->write(); } if (!$this->owner->Surname) { $this->owner->Surname = $data['ShippingSurname']; $this->owner->write(); } $shippingAddress = Address_Shipping::create(array('MemberID' => $this->owner->ID, 'FirstName' => $data['ShippingFirstName'], 'Surname' => $data['ShippingSurname'], 'Company' => $data['ShippingCompany'], 'Address' => $data['ShippingAddress'], 'AddressLine2' => $data['ShippingAddressLine2'], 'City' => $data['ShippingCity'], 'PostalCode' => $data['ShippingPostalCode'], 'State' => $data['ShippingState'], 'CountryName' => $data['ShippingCountryName'], 'CountryCode' => $data['ShippingCountryCode'], 'RegionName' => isset($data['ShippingRegionName']) ? $data['ShippingRegionName'] : null, 'RegionCode' => isset($data['ShippingRegionCode']) ? $data['ShippingRegionCode'] : null)); $billingAddress = Address_Billing::create(array('MemberID' => $this->owner->ID, 'FirstName' => $data['BillingFirstName'], 'Surname' => $data['BillingSurname'], 'Company' => $data['BillingCompany'], 'Address' => $data['BillingAddress'], 'AddressLine2' => $data['BillingAddressLine2'], 'City' => $data['BillingCity'], 'PostalCode' => $data['BillingPostalCode'], 'State' => $data['BillingState'], 'CountryName' => $data['BillingCountryName'], 'CountryCode' => $data['BillingCountryCode'], 'RegionName' => isset($data['BillingRegionName']) ? $data['ShippingRegionName'] : null, 'RegionCode' => isset($data['BillingRegionCode']) ? $data['ShippingRegionCode'] : null)); //Look for identical existing addresses //TODO when a match is made then make that matched address the default now $match = false; foreach ($this->owner->ShippingAddresses() as $address) { $existing = $address->toMap(); $new = $shippingAddress->toMap(); $result = array_intersect_assoc($existing, $new); //If no difference, then match is found $diff = array_diff_assoc($new, $result); $match = empty($diff); } if (!$match) { $shippingAddress->Default = true; $shippingAddress->write(); } $match = false; foreach ($this->owner->BillingAddresses() as $address) { $existing = $address->toMap(); $new = $billingAddress->toMap(); $result = array_intersect_assoc($existing, $new); $diff = array_diff_assoc($new, $result); $match = empty($diff); } if (!$match) { $billingAddress->Default = true; $billingAddress->write(); } }