Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
converterdocumentprofile.php
1<?php
5
6
13{
17 protected function getFieldsInfo()
18 {
20 }
21
27 public function resolveParams($documentImport)
28 {
29 if(!($documentImport instanceof DocumentBase))
30 throw new ArgumentException("Document must be instanceof DocumentBase");
31
32 $result = array();
33
34 $params = $documentImport->getFieldValues();
35 foreach($params as $k=>$v)
36 {
37 switch($k)
38 {
39 case 'VERSION':
40 if(!empty($v))
41 $profile['VERSION_1C'] = $v;
42 break;
43 case 'XML_ID':
44 case 'OFICIAL_NAME':
45 case 'FULL_NAME':
46 case 'INN':
47 case 'KPP':
48 case 'OKPO_CODE':
49 case 'EGRPO':
50 case 'OKVED':
51 case 'OKDP':
52 case 'OKOPF':
53 case 'OKFC':
54 //case 'OKPO':
55 if(!empty($v))
56 $profile[$k] = $v;
57 break;
58 case 'ITEM_NAME':
59 if(!empty($v))
60 {
61 $profile[$k] = $v;
62 $profile['AGENT_NAME'] = $v;
63 }
64 break;
65 case 'REGISTRATION_ADDRESS':
66 case 'UR_ADDRESS':
67 case 'ADDRESS':
68 foreach($params[$k] as $name=>$values)
69 {
70 if($name == 'ADDRESS_FIELD')
71 {
72 foreach($values as $nameAddres=>$valuesAddres)
73 {
74 $profile[$k][$nameAddres] = $valuesAddres['VALUE'];
75 }
76 }
77 else
78 {
79 $profile[$k][$name] = $values;
80 }
81 }
82 break;
83 case 'CONTACTS':
84 foreach($params[$k]['CONTACT'] as $name=>$values)
85 {
86 $profile['CONTACT'][$name] = $values['VALUE'];
87 }
88
89 $profile['CONTACT']['EMAIL'] = !empty($profile['CONTACT']['MAIL_NEW'])? $profile['CONTACT']['MAIL_NEW']:null;
90 $profile['CONTACT']['PHONE'] = !empty($profile['CONTACT']['WORK_PHONE_NEW'])? $profile['CONTACT']['WORK_PHONE_NEW']:null;
91
92 break;
93 case 'REPRESENTATIVES':
94 foreach($params[$k]['REPRESENTATIVE'] as $name=>$values)
95 {
96 if($name == 'CONTACT_PERSON')
97 {
98 $profile['CONTACT'][$name] = $values['ITEM_NAME'];
99 }
100 }
101 break;
102 }
103 }
104
105 if(!empty($profile["OKPO_CODE"]))
106 $profile["OKPO"] = $profile["OKPO_CODE"];
107
108 if($profile["OFICIAL_NAME"] <> '' && empty($profile["FULL_NAME"]))
109 $profile["FULL_NAME"] = $profile["OFICIAL_NAME"];
110
111 if($profile["OFICIAL_NAME"] <> '' && $profile["INN"] <> '')
112 $profile["TYPE"] = "UR";
113 elseif($profile["INN"] <> '')
114 $profile["TYPE"] = "IP";
115 else
116 $profile["TYPE"] = "FIZ";
117
118 if(!empty($profile))
119 {
120 $property = array();
121 foreach($profile as $name => $value)
122 {
123 switch($name)
124 {
125 case 'ID':
126 case 'VERSION':
127 case 'ITEM_NAME':
128 case 'AGENT_NAME':
129 case 'OFICIAL_NAME':
130 case 'FULL_NAME':
131 case 'INN':
132 case 'KPP':
133 case 'OKPO_CODE':
134 case 'EGRPO':
135 case 'OKVED':
136 case 'OKDP':
137 case 'OKOPF':
138 case 'OKFC':
139 case 'OKPO':
140 $property[$name] = $value;
141 break;
142 case 'CONTACT':
143 $property["EMAIL"] = $value["MAIL_NEW"];
144 $property["PHONE"] = $value["WORK_PHONE_NEW"];
145 break;
146 case 'REPRESENTATIVE':
147 $property["CONTACT_PERSON"] = $value["CONTACT_PERSON"];
148 break;
149 case 'REGISTRATION_ADDRESS':
150 case 'UR_ADDRESS':
151 foreach($value as $nameProperty => $valueProperty)
152 {
153 if($valueProperty <> '' && empty($property[$nameProperty]))
154 $property[$nameProperty] = $valueProperty;
155 }
156 $property["ADDRESS_FULL"] = $value["PRESENTATION"];
157 $property["INDEX"] = $value["POST_CODE"];
158 break;
159 case 'ADDRESS':
160 foreach($value as $nameProperty => $valueProperty)
161 {
162 if($valueProperty <> '' && empty($property["F_".$nameProperty]))
163 $property["F_".$nameProperty] = $valueProperty;
164 }
165 $property["F_ADDRESS_FULL"] = $value["PRESENTATION"];
166 $property["F_INDEX"] = $value["POST_CODE"];
167 break;
168 }
169 }
170 }
171
172 $result['TRAITS'] = isset($profile)? $profile:array();
173 $result['ORDER_PROPS'] = isset($property)? $property:array();
174
175 return $result;
176 }
177
182 static public function sanitizeFields($entity = null, array &$fields, ISettings $settings)
183 {
184 // TODO: Implement sanitizeFields() method.
185 }
186
187 public function externalize(array $fields)
188 {
189 $result = array();
190 $traits = $fields['TRAITS'];
191 $businessValue = $fields['BUSINESS_VALUE'];
192
193 $availableFields = $this->getFieldsInfo();
194
195 foreach ($availableFields as $k=>$v)
196 {
197 $value='';
198 $replacedFields = $businessValue;
199 switch ($k)
200 {
201 case 'XML_ID':
202 $value = $this->getXmlId($traits);
203 break;
204 case 'ITEM_NAME':
205 $value = $businessValue['AGENT_NAME'];
206 break;
207 case 'INN':
208 $value = $businessValue['INN'];
209 break;
210 case 'KPP':
211 $value = $businessValue['KPP'];
212 break;
213 case 'ADDRESS':
214 $replaceNameFields = array(
215 'PRESENTATION' => 'F_ADDRESS_FULL',
216 'POST_CODE' => 'F_INDEX',
217 'COUNTRY' => 'F_COUNTRY',
218 'REGION' => 'F_REGION',
219 'STATE' => 'F_STATE',
220 'SMALL_CITY' => 'F_TOWN',
221 'CITY' => 'F_CITY',
222 'STREET' => 'F_STREET',
223 'HOUSE' => 'F_HOUSE',
224 'BUILDING' => 'F_BUILDING',
225 'FLAT' => 'F_FLAT');
226 $this->replaceNameFields($replacedFields, $replaceNameFields);
227 $value = $this->externalizeArrayFields($replacedFields, $v);
228 break;
229 case 'CONTACTS':
230 $replaceNameFields = array(
231 'WORK_PHONE_NEW' => 'PHONE',
232 'MAIL_NEW' => 'EMAIL'
233 );
234 $this->replaceNameFields($replacedFields, $replaceNameFields);
235 $value = $this->externalizeArrayFields($replacedFields, $v);
236 break;
237 case 'REPRESENTATIVES':
238 $replaceNameFields = array(
239 'RELATION' => 'CONTACT_PERSON',
240 'ID' => 'CONTACT_PERSON',
241 'ITEM_NAME' => 'CONTACT_PERSON',
242 );
243 $this->replaceNameFields($replacedFields, $replaceNameFields);
244 $value = $this->externalizeArrayFields($replacedFields, $v);
245 break;
246 case 'ROLE':
247 $value = DocumentBase::getLangByCodeField('BUYER');
248 break;
249 }
250 $result[$k] = $value;
251 }
252
253 if($this->isFiz($businessValue))
254 {
255 foreach ($availableFields as $k=>$v)
256 {
257 if(in_array($k, array('XML_ID','ITEM_NAME', 'INN', 'KPP', 'ADDRESS', 'CONTACTS', 'REPRESENTATIVES', 'ROLE')))
258 continue;
259
260 $value='';
261 $replacedFields = $businessValue;
262 switch ($k)
263 {
264 case 'FULL_NAME':
265 $value = $businessValue['FULL_NAME'];
266 break;
267 case 'SURNAME':
268 $value = $businessValue['SURNAME'];
269 break;
270 case 'NAME':
271 $value = $businessValue['NAME'];
272 break;
273 case 'MIDDLE_NAME':
274 $value = $businessValue['SECOND_NAME'];
275 break;
276 case 'BIRTHDAY':
277 $value = $businessValue['BIRTHDAY'];
278 break;
279 case 'SEX':
280 $value = $businessValue['MALE'];
281 break;
282 case 'REGISTRATION_ADDRESS':
283 $replaceNameFields = array(
284 'PRESENTATION'=>'ADDRESS_FULL',
285 'POST_CODE'=>'INDEX',
286 );
287 $this->replaceNameFields($replacedFields, $replaceNameFields);
288 $value = $this->externalizeArrayFields($replacedFields, $v);
289 break;
290 }
291 if(!is_array($value))
292 $this->externalizeField($value, $v);
293
294 $result[$k] = $value;
295
296 }
297 }
298 else
299 {
300 foreach ($availableFields as $k=>$v)
301 {
302 if(in_array($k, array('XML_ID','ITEM_NAME', 'INN', 'KPP', 'ADDRESS', 'CONTACTS', 'REPRESENTATIVES', 'ROLE')))
303 continue;
304
305 $value='';
306 $replacedFields = $businessValue;
307 switch ($k)
308 {
309 case 'UR_ADDRESS':
310 $replaceNameFields = array(
311 'PRESENTATION'=>'ADDRESS_FULL',
312 'POST_CODE'=>'INDEX',
313 );
314 $this->replaceNameFields($replacedFields, $replaceNameFields);
315 $value = $this->externalizeArrayFields($replacedFields, $v);
316 break;
317 case 'OFICIAL_NAME':
318 $value = $businessValue['FULL_NAME'];
319 break;
320 case 'EGRPO':
321 $value = $businessValue['EGRPO'];
322 break;
323 case 'OKVED':
324 $value = $businessValue['OKVED'];
325 break;
326 case 'OKDP':
327 $value = $businessValue['OKDP'];
328 break;
329 case 'OKOPF':
330 $value = $businessValue['OKOPF'];
331 break;
332 case 'OKFC':
333 $value = $businessValue['OKFC'];
334 break;
335 case 'OKPO':
336 case 'OKPO_CODE':
337 $value = $businessValue['OKPO'];
338 break;
339 case 'MONEY_ACCOUNTS':
340 $replaceNameFields = array(
341 'ITEM_NAME'=>'B_NAME',
342 'PRESENTATION'=>'B_ADDRESS_FULL',
343 'POST_CODE'=>'B_INDEX',
344 'COUNTRY'=>'B_COUNTRY',
345 'REGION'=>'B_REGION',
346 'STATE'=>'B_STATE',
347 'SMALL_CITY'=>'B_TOWN',
348 'CITY'=>'B_CITY',
349 'STREET'=>'B_STREET',
350 'HOUSE'=>'B_HOUSE',
351 'BUILDING'=>'B_BUILDING',
352 'FLAT'=>'B_FLAT',
353 );
354 $this->replaceNameFields($replacedFields, $replaceNameFields);
355 $value = $this->externalizeArrayFields($replacedFields, $v);
356 break;
357 }
358 if(!is_array($value))
359 $this->externalizeField($value, $v);
360
361 $result[$k] = $value;
362 }
363 }
364
365 foreach ($availableFields as $k=>$v)
366 {
367 if($k <> 'REK_VALUES')
368 continue;
369
370 $value='';
371 switch ($k)
372 {
373 case 'REK_VALUES':
374 $value=array();
375 foreach($v['FIELDS'] as $name=>$fieldInfo)
376 {
377 $valueRV='';
378 switch($name)
379 {
380 case 'DELIVERY_ADDRESS':
381 $valueRV = isset($result['ADDRESS']['PRESENTATION']) ? $result['ADDRESS']['PRESENTATION']:'';
382 if($valueRV === '')
383 $valueRV = $result[($this->isFiz($businessValue) ? 'REGISTRATION_ADDRESS':'UR_ADDRESS')]['PRESENTATION'];
384 break;
385 }
386 $value[] = $this->externalizeRekvValue($name, $valueRV, $fieldInfo);
387 }
388 break;
389 }
390 $result[$k] = $value;
391 }
392
393 $result = $this->modifyTrim($result);
394
395 return $result;
396 }
397
402 protected function isFiz(array $fields)
403 {
404 return ($fields["IS_FIZ"]=="Y");
405 }
406
412 public function externalizeArrayFields($fields, $fieldsInfo, $key=null)
413 {
414 $result = array();
415 foreach ($fieldsInfo['FIELDS'] as $name=>$fieldInfo)
416 {
417 if($fieldInfo['TYPE'] == 'array')
418 {
419 switch ($name)
420 {
421 case 'ADDRESS_FIELD':
422 case 'CONTACT':
423 case 'REPRESENTATIVE':
424 $value = $this->externalizeArrayFields($fields, $fieldInfo);
425 break;
426 default:
427 $value = $this->externalizeArrayFields($fields, $fieldInfo, $name);
428 }
429 }
430 else
431 {
432 switch ($name)
433 {
434 case 'RELATION':
435 case 'TYPE':
437 break;
438 case 'VALUE':
439 $value = $fields[$key];
440 break;
441 default:
442 $value = $fields[$name];
443 }
444 }
445 if(!is_array($value))
446 $this->externalizeField($value, $fieldInfo);
447
448 $result[$name] = $value;
449 }
450 return $result;
451 }
452
457 private function replaceNameFields(array &$original, array $replace)
458 {
459 foreach ($original as $k=>$v)
460 {
461 $replaceNameField = array_search($k, $replace);
462 if($replaceNameField !== false)
463 {
464 $original[$replaceNameField] = $v;
465 unset($original[$k]);
466 }
467 }
468 }
469
474 private function getXmlId(array $fields)
475 {
476 if($fields['XML_ID'] <> '')
477 {
478 $result = $fields['XML_ID'];
479 }
480 else
481 {
482 $result = htmlspecialcharsbx(mb_substr($fields["ID"]."#".$fields["LOGIN"]."#".$fields["LAST_NAME"]." ".$fields["NAME"]." ".$fields["SECOND_NAME"], 0, 40));
483 \Bitrix\Sale\Exchange\Entity\UserImportBase::updateEmptyXmlId($fields["ID"], $result);
484 }
485 return $result;
486 }
487}
static sanitizeFields($entity=null, array &$fields, ISettings $settings)