| 1 |
<?php |
| 2 |
|
| 3 |
namespace Moloni\Controllers; |
| 4 |
|
| 5 |
use Moloni\Curl; |
| 6 |
use Moloni\Error; |
| 7 |
use Moloni\Tools; |
| 8 |
use WC_Order; |
| 9 |
|
| 10 |
class OrderCustomer |
| 11 |
{ |
| 12 |
/** |
| 13 |
* @var WC_Order |
| 14 |
*/ |
| 15 |
private $order; |
| 16 |
|
| 17 |
private $customer_id = false; |
| 18 |
private $vat = '999999990'; |
| 19 |
private $email = ''; |
| 20 |
private $name = 'Cliente'; |
| 21 |
private $contactName = ''; |
| 22 |
private $zipCode = '1000-100'; |
| 23 |
private $address = 'Desconhecida'; |
| 24 |
private $city = 'Desconhecida'; |
| 25 |
private $languageId = 1; |
| 26 |
private $countryId = 1; |
| 27 |
|
| 28 |
|
| 29 |
/** |
| 30 |
* List of some invalid vat numbers |
| 31 |
* @var array |
| 32 |
*/ |
| 33 |
private $invalidVats = [ |
| 34 |
'999999999', |
| 35 |
'000000000', |
| 36 |
'111111111' |
| 37 |
]; |
| 38 |
|
| 39 |
/** |
| 40 |
* Documents constructor. |
| 41 |
* @param WC_Order $order |
| 42 |
*/ |
| 43 |
public function __construct($order) |
| 44 |
{ |
| 45 |
$this->order = $order; |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* @return bool|int |
| 50 |
* @throws Error |
| 51 |
*/ |
| 52 |
public function create() |
| 53 |
{ |
| 54 |
$this->vat = $this->getVatNumber(); |
| 55 |
$this->email = $this->order->get_billing_email(); |
| 56 |
|
| 57 |
$values['name'] = $this->getCustomerName(); |
| 58 |
$values['language_id'] = $this->getCustomerLanguageId(); |
| 59 |
$values['address'] = $this->getCustomerBillingAddress(); |
| 60 |
$values['zip_code'] = $this->getCustomerZip(); |
| 61 |
$values['city'] = $this->getCustomerBillingCity(); |
| 62 |
$values['country_id'] = $this->getCustomerCountryId(); |
| 63 |
$values['email'] = $this->order->get_billing_email(); |
| 64 |
$values['phone'] = $this->order->get_billing_phone(); |
| 65 |
$values['contact_name'] = $this->contactName; |
| 66 |
$values['maturity_date_id'] = defined('MATURITY_DATE') ? MATURITY_DATE : ''; |
| 67 |
$values['payment_method_id'] = defined('PAYMENT_METHOD') ? PAYMENT_METHOD : ''; |
| 68 |
$values['salesman_id'] = ''; |
| 69 |
$values['payment_day'] = ''; |
| 70 |
$values['discount'] = ''; |
| 71 |
$values['credit_limit'] = ''; |
| 72 |
$values['delivery_method_id'] = ''; |
| 73 |
$values['field_notes'] = ''; |
| 74 |
|
| 75 |
$customerExists = $this->searchForCustomer(); |
| 76 |
if (!$customerExists) { |
| 77 |
$values['vat'] = $this->vat; |
| 78 |
$values['number'] = self::getCustomerNextNumber(); |
| 79 |
$result = Curl::simple('customers/insert', $values); |
| 80 |
} else { |
| 81 |
$values['customer_id'] = $customerExists['customer_id']; |
| 82 |
$result = Curl::simple('customers/update', $values); |
| 83 |
} |
| 84 |
|
| 85 |
if (isset($result['customer_id'])) { |
| 86 |
$this->customer_id = $result['customer_id']; |
| 87 |
} else { |
| 88 |
throw new Error(__('Atenção, houve um erro ao inserir o cliente.')); |
| 89 |
} |
| 90 |
|
| 91 |
return $this->customer_id; |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* Get the vat number of an order |
| 96 |
* Get it from a custom field and validate if Portuguese |
| 97 |
* @return string |
| 98 |
*/ |
| 99 |
public function getVatNumber() |
| 100 |
{ |
| 101 |
$vat = '999999990'; |
| 102 |
|
| 103 |
if (defined('VAT_FIELD')) { |
| 104 |
$metaVat = trim($this->order->get_meta(VAT_FIELD)); |
| 105 |
if (!empty($metaVat)) { |
| 106 |
$vat = $metaVat; |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
$billingCountry = $this->order->get_billing_country(); |
| 111 |
|
| 112 |
// Do some more verifications if the vat number is Portuguese |
| 113 |
if ($billingCountry === 'PT') { |
| 114 |
// Remove the PT part from the beginning |
| 115 |
if (stripos($vat, strtoupper('PT')) === 0) { |
| 116 |
$vat = str_ireplace('PT', '', $vat); |
| 117 |
} |
| 118 |
|
| 119 |
// Check if the vat is one of this |
| 120 |
if (empty($vat) || in_array($vat, $this->invalidVats, false)) { |
| 121 |
$vat = '999999990'; |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
$this->vat = $vat; |
| 126 |
return $this->vat; |
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* Checks if the company name is set |
| 131 |
* If the order has a company we issue the document to the company |
| 132 |
* And add the name of the person to the contact name |
| 133 |
* @return string |
| 134 |
*/ |
| 135 |
public function getCustomerName() |
| 136 |
{ |
| 137 |
$billingName = $this->order->get_billing_first_name(); |
| 138 |
$billingLastName = $this->order->get_billing_last_name(); |
| 139 |
if (!empty($billingLastName)) { |
| 140 |
$billingName .= ' ' . $this->order->get_billing_last_name(); |
| 141 |
} |
| 142 |
|
| 143 |
$billingCompany = trim($this->order->get_billing_company()); |
| 144 |
if (!empty($billingCompany)) { |
| 145 |
$this->name = $billingCompany; |
| 146 |
$this->contactName = $billingName; |
| 147 |
} elseif (!empty($billingName)) { |
| 148 |
$this->name = $billingName; |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
return $this->name; |
| 153 |
} |
| 154 |
|
| 155 |
/** |
| 156 |
* Create a customer billing a address |
| 157 |
* @return string |
| 158 |
*/ |
| 159 |
public function getCustomerBillingAddress() |
| 160 |
{ |
| 161 |
$billingAddress = trim($this->order->get_billing_address_1()); |
| 162 |
$billingAddress2 = $this->order->get_billing_address_2(); |
| 163 |
if (!empty($billingAddress2)) { |
| 164 |
$billingAddress .= ' ' . trim($billingAddress2); |
| 165 |
} |
| 166 |
|
| 167 |
if (!empty($billingAddress)) { |
| 168 |
$this->address = $billingAddress; |
| 169 |
} |
| 170 |
|
| 171 |
return $this->address; |
| 172 |
} |
| 173 |
|
| 174 |
/** |
| 175 |
* Create a customer billing City |
| 176 |
* @return string |
| 177 |
*/ |
| 178 |
public function getCustomerBillingCity() |
| 179 |
{ |
| 180 |
$billingCity = trim($this->order->get_billing_city()); |
| 181 |
if (!empty($billingCity)) { |
| 182 |
$this->city = $billingCity; |
| 183 |
} |
| 184 |
|
| 185 |
return $this->city; |
| 186 |
} |
| 187 |
|
| 188 |
/** |
| 189 |
* Gets the zip code of a customer |
| 190 |
* If the customer is Portuguese validate the Vat Number |
| 191 |
* @return string |
| 192 |
*/ |
| 193 |
public function getCustomerZip() |
| 194 |
{ |
| 195 |
$zipCode = $this->order->get_billing_postcode(); |
| 196 |
|
| 197 |
if ($this->order->get_billing_country() === 'PT') { |
| 198 |
$zipCode = Tools::zipCheck($zipCode); |
| 199 |
} |
| 200 |
|
| 201 |
$this->zipCode = $zipCode; |
| 202 |
return $this->zipCode; |
| 203 |
} |
| 204 |
|
| 205 |
/** |
| 206 |
* Get the customer next available number for incremental inserts |
| 207 |
* @return int |
| 208 |
* @throws Error |
| 209 |
*/ |
| 210 |
public static function getCustomerNextNumber() |
| 211 |
{ |
| 212 |
$results = Curl::simple('customers/getNextNumber', []); |
| 213 |
if (!empty($results['number'])) { |
| 214 |
return ($results['number']); |
| 215 |
} |
| 216 |
|
| 217 |
return (mt_rand(10000000000, 100000000000)); |
| 218 |
} |
| 219 |
|
| 220 |
/** |
| 221 |
* Get the country_id based on a ISO value |
| 222 |
* @return int |
| 223 |
* @throws Error |
| 224 |
*/ |
| 225 |
public function getCustomerCountryId() |
| 226 |
{ |
| 227 |
$countryCode = $this->order->get_billing_country(); |
| 228 |
$this->countryId = Tools::getCountryIdFromCode($countryCode); |
| 229 |
|
| 230 |
return $this->countryId; |
| 231 |
} |
| 232 |
|
| 233 |
/** |
| 234 |
* If the country of the customer is one of the available we set it to Portuguese |
| 235 |
*/ |
| 236 |
public function getCustomerLanguageId() |
| 237 |
{ |
| 238 |
$this->languageId = in_array($this->countryId, [1]) ? 1 : 2; |
| 239 |
return $this->languageId; |
| 240 |
} |
| 241 |
|
| 242 |
/** |
| 243 |
* Search for a customer based on $this->vat or $this->email |
| 244 |
* @param string|bool $forField |
| 245 |
* @return bool |
| 246 |
* @throws Error |
| 247 |
*/ |
| 248 |
public function searchForCustomer($forField = false) |
| 249 |
{ |
| 250 |
$result = false; |
| 251 |
$search = []; |
| 252 |
$search['exact'] = 1; |
| 253 |
if ($forField && in_array($forField, ['vat', 'email'])) { |
| 254 |
//@todo not important for this plugin |
| 255 |
} else if ($this->vat !== '999999990') { |
| 256 |
$search['vat'] = $this->vat; |
| 257 |
$searchResult = Curl::simple('customers/getByVat', $search); |
| 258 |
if (isset($searchResult[0]['customer_id'])) { |
| 259 |
$result = $searchResult[0]; |
| 260 |
} |
| 261 |
} else if (!empty($this->email)) { |
| 262 |
$search['email'] = $this->email; |
| 263 |
$searchResult = Curl::simple('customers/getByEmail', $search); |
| 264 |
if (isset($searchResult[0]['customer_id'])) { |
| 265 |
$result = $searchResult[0]; |
| 266 |
} |
| 267 |
} |
| 268 |
|
| 269 |
return $result; |
| 270 |
} |
| 271 |
} |
| 272 |
|