52 return \CSaleTax::class;
60 return \CSaleOrderTax::class;
66 public function getTaxList()
68 if ($this->list ===
null)
70 $this->list = $this->loadList();
75 'VALUES' => $this->list,
79 if ($event->getResults())
82 foreach($event->getResults() as $eventResult)
84 if($eventResult->getType() == Main\EventResult::SUCCESS)
86 $eventResultData = $eventResult->getParameters();
87 if (!empty($eventResultData[
'VALUES']))
89 $this->list = $eventResultData[
'VALUES'];
107 $this->isExternal =
true;
126 public function calculate()
132 if (!$order = $this->getOrder())
137 $taxResult = array();
139 $taxList = $this->getTaxList();
141 $taxExempt = static::loadExemptList($order->getUserId());
144 "SITE_ID" => $order->getSiteid(),
145 "PERSON_TYPE_ID" => $order->getPersonTypeId(),
146 "TAX_LOCATION" => $order->getTaxLocation(),
147 "CURRENCY" => $order->getCurrency(),
148 "USE_VAT" => $order->isUsedVat(),
149 "VAT_RATE" => $order->getVatRate(),
150 "VAT_SUM" => $order->getVatSum(),
153 if (is_array($taxExempt))
155 $fields[
'TAX_EXEMPT'] = $taxExempt;
158 if (is_array($taxList) && !empty($taxList))
160 $fields[
'TAX_LIST'] = $taxList;
164 $basket =
$order->getBasket();
170 foreach ($basket as $basketItem)
172 if ($basketItem->getQuantity() == 0)
174 $fields[
'BASKET_ITEMS'][] = $basketItem->getFieldValues();
178 $className = static::getTaxClassName();
179 $className::calculateTax($fields, array());
181 if (!
$order->isUsedVat() && isset($fields[
'TAX_LIST']) && is_array($fields[
'TAX_LIST']))
183 $taxResult[
'TAX_LIST'] = $fields[
'TAX_LIST'];
186 if (array_key_exists(
'TAX_PRICE', $fields) && floatval($fields[
'TAX_PRICE']) >= 0)
188 $taxResult[
'TAX_PRICE'] = $fields[
'TAX_PRICE'];
191 if (array_key_exists(
'VAT_SUM', $fields) && floatval($fields[
'VAT_SUM']) > 0)
193 $taxResult[
'VAT_SUM'] = $fields[
'VAT_SUM'];
196 if (array_key_exists(
'TAX_LIST', $fields))
199 $taxResult[
'TAX_LIST'] = $newTaxList;
201 $this->list = $newTaxList;
204 if (!
$order->isUsedVat() && empty($this->list) && is_array($this->list))
206 $taxResult[
'TAX_PRICE'] = 0;
209 if (!empty($taxResult))
211 $result->setData($taxResult);
221 public function calculateDelivery()
224 $result =
new Result();
229 throw new Main\ObjectNotFoundException(
'Entity "Order" not found');
232 if (
$order->getId() > 0 || (!empty($this->list) && is_array($this->list)))
234 $taxList = $this->getTaxList();
241 $taxExempt = static::loadExemptList(
$order->getUserId());
244 if (!$basket =
$order->getBasket())
246 throw new Main\ObjectNotFoundException(
'Entity "Basket" not found');
250 "TAX_LOCATION" =>
$order->getTaxLocation(),
251 "VAT_SUM" => $basket->getVatSum(),
252 "CURRENCY" =>
$order->getCurrency(),
256 if (!empty($taxExempt))
258 $fields[
'TAX_EXEMPT'] = $taxExempt;
261 if (!empty($taxList))
263 $fields[
'TAX_LIST'] = $taxList;
270 $options[
'COUNT_DELIVERY_TAX'] = ($isDeliveryCalculate ===
true ?
"Y" :
"N");
273 $shipmentCollection =
$order->getShipmentCollection();
274 if (!$shipmentCollection)
276 throw new Main\ObjectNotFoundException(
'Entity "ShipmentCollection" not found');
280 foreach ($shipmentCollection as $shipment)
282 if ($shipment->isSystem())
285 $service = $shipment->getDelivery();
286 if ($service ===
null)
289 $additionalFields = array(
290 "DELIVERY_PRICE" => $shipment->getPrice()
293 $vatRate = $shipment->getVatRate();
296 $additionalFields[
"USE_VAT"] =
true;
297 $additionalFields[
"VAT_RATE"] = $vatRate;
300 $fields = array_merge($fields, $additionalFields);
303 $className = static::getTaxClassName();
304 $className::calculateDeliveryTax($fields, $options);
308 $taxResult = array();
310 if (array_key_exists(
'TAX_PRICE', $fields) && floatval($fields[
'TAX_PRICE']) > 0)
312 $taxResult[
'TAX_PRICE'] = $fields[
'TAX_PRICE'];
315 if (array_key_exists(
'VAT_SUM', $fields) && floatval($fields[
'VAT_SUM']) > 0)
317 $taxResult[
'VAT_SUM'] = $fields[
'VAT_SUM'];
320 if (array_key_exists(
'VAT_DELIVERY', $fields) && floatval($fields[
'VAT_DELIVERY']) > 0)
322 $taxResult[
'VAT_DELIVERY'] = $fields[
'VAT_DELIVERY'];
326 if ($isDeliveryCalculate && array_key_exists(
'TAX_LIST', $fields) && !empty($fields[
'TAX_LIST']) && is_array($fields[
'TAX_LIST']))
329 $this->list = $newTaxList;
332 if (!empty($taxResult))
334 $result->setData($taxResult);
348 $oldTaxList = $this->loadList();
350 $taxIndexList = array();
352 if (!empty($oldTaxList) && is_array($oldTaxList))
354 $oldTaxIndexList = array();
355 foreach ($oldTaxList as $taxOldKey => $taxOldValue)
357 $oldTaxIndexList[$taxOldValue[
'NAME'].
"|".$taxOldValue[
'CODE']] = $taxOldKey;
360 foreach ($taxList as $taxValue)
362 if (array_key_exists($taxValue[
'NAME'].
"|".$taxValue[
'CODE'], $oldTaxIndexList))
364 $taxIndexList[$taxValue[
'NAME'].
"|".$taxValue[
'CODE']] = $oldTaxIndexList[$taxValue[
'NAME'].
"|".$taxValue[
'CODE']];
369 if (!empty($taxList) && is_array($taxList))
371 foreach ($taxList as $taxKey => $taxValue)
373 $taxCode = $taxValue[
'NAME'].
"|".$taxValue[
'CODE'];
374 if (isset($taxIndexList[$taxCode]))
376 $oldTaxKey = $taxIndexList[$taxCode];
378 $oldTaxValueDat = $oldTaxList[$oldTaxKey];
379 foreach($taxValue as $key => $value)
381 if (array_key_exists($key, $oldTaxValueDat))
383 if ($oldTaxValueDat[$key] != $value)
385 $oldTaxList[$oldTaxKey][$key] = $value;
387 if (!in_array($taxCode, $this->changedValues))
389 $this->changedValues[$taxCode] =
true;
395 $oldTaxList[$oldTaxKey][$key] = $value;
396 if (!in_array($taxCode, $this->changedValues))
398 $this->changedValues[$taxCode] =
true;
405 $oldTaxList[] = array(
406 'NAME' => $taxValue[
'NAME'],
407 'IS_PERCENT' => $taxValue[
'IS_PERCENT'],
408 'VALUE' => $taxValue[
'VALUE'],
409 'VALUE_MONEY' => $taxValue[
'VALUE_MONEY'],
410 'APPLY_ORDER' => $taxValue[
'APPLY_ORDER'],
411 'IS_IN_PRICE' => $taxValue[
'IS_IN_PRICE'],
412 'TAX_VAL' => $taxValue[
'TAX_VAL'],
413 'CODE' => $taxValue[
'CODE'],
424 $taxListModify1C = array();
425 foreach($oldTaxList as $taxOrder)
427 if($taxOrder[
'CODE'] ==
'VAT1C')
429 $taxListModify1C[] = $taxOrder;
433 if(count($taxListModify1C)>0)
435 $oldTaxList = $taxListModify1C;
440 $oldTaxList = array();
451 public function save()
456 if (!$order = $this->getOrder())
464 $className = static::getTaxClassName();
465 $className::DoSaveOrderTax($order->getId(), $this->getTaxList(), $errors);
467 if (!empty($errors) && is_array($errors))
469 foreach ($errors as $error)
471 $result->addError(
new Main\Entity\EntityError($error));
480 $orderHistory = $registry->getOrderHistoryClassName();
482 $orderHistory::collectEntityFields(
'TAX',
$order->getId());
492 private static function createTaxObject()
495 $taxClassName = $registry->getTaxClassName();
497 return new $taxClassName();
515 $tax = static::createTaxObject();
516 $tax->order = $order;
518 if ($order->getId() > 0)
524 $tax->list = $tax->getAvailableList();
533 protected function loadList()
535 $resultList = array();
536 $order = $this->getOrder();
538 if ($order->getId() <= 0)
542 $className = static::getOrderTaxClassName();
544 $dbTaxList = $className::GetList(
545 array(
"APPLY_ORDER" =>
"ASC"),
546 array(
"ORDER_ID" => $order->getId())
548 while ($taxList = $dbTaxList->Fetch())
550 $taxList[
'NAME'] = $taxList[
'TAX_NAME'];
551 $resultList[] = $taxList;
554 return (!empty($resultList) ? $resultList : null);
560 $this->list = array();
565 $this->availableList =
null;
570 public function refreshData()
574 if (!$this->isExternal)
576 $this->resetTaxList();
579 $this->resetAvailableTaxList();
582 $r = $this->calculate();
583 if (!$r->isSuccess())
585 $result->addErrors($r->getErrors());
589 $taxResult = $r->getData();
591 $r = $this->calculateDelivery();
592 if (!$r->isSuccess())
594 $result->addErrors($r->getErrors());
597 $taxResult = array_merge($taxResult, $r->getData());
599 $result->setData($taxResult);
608 public static function loadExemptList($userId)
610 $exemptList = array();
612 static $proxyUserGroups = array();
613 static $proxyTaxExemptList = array();
615 if (!empty($proxyUserGroups[$userId]))
617 $userGroups = $proxyUserGroups[
$userId];
621 $userGroups = \CUser::GetUserGroup($userId);
622 $proxyUserGroups[
$userId] = $userGroups;
626 $proxyTaxExemptKey = md5(join(
'|', $userGroups));
628 if (array_key_exists($proxyTaxExemptKey, $proxyTaxExemptList))
630 $exemptList = $proxyTaxExemptList[$proxyTaxExemptKey];
635 $className = static::getTaxClassName();
636 $dbTaxExemptList = $className::GetExemptList(array(
"GROUP_ID" => $userGroups));
637 while ($taxExemptList = $dbTaxExemptList->Fetch())
639 if (!in_array(intval($taxExemptList[
"TAX_ID"]), $exemptList))
640 $exemptList[] = intval($taxExemptList[
"TAX_ID"]);
643 $proxyTaxExemptList[$proxyTaxExemptKey] = $exemptList;
656 if ($this->availableList ===
null)
661 return $this->availableList;
671 $basket =
$order->getBasket();
679 $taxExemptList = static::loadExemptList(
$order->getUserId());
681 $taxRateRes = \CSaleTaxRate::GetList(
682 array(
"APPLY_ORDER" =>
"ASC"),
684 "LID" =>
$order->getSiteId(),
685 "PERSON_TYPE_ID" =>
$order->getPersonTypeId(),
687 "LOCATION_CODE" =>
$order->getTaxLocation(),
690 while ($taxRate = $taxRateRes->GetNext())
692 if (!in_array(intval($taxRate[
"TAX_ID"]), $taxExemptList))
694 if ($taxRate[
"IS_PERCENT"] !=
"Y")
697 $taxRate[
"CURRENCY"] =
$order->getCurrency();
708 "VALUE" =>
$order->getVatRate() * 100,
709 "VALUE_FORMATED" =>
"(".($order->getVatRate() * 100).
"%, ".GetMessage(
"SOA_VAT_INCLUDED").
")",
710 "VALUE_MONEY" =>
$order->getVatSum(),
711 "VALUE_MONEY_FORMATED" => SaleFormatCurrency(
$order->getVatSum(),
$order->getCurrency()),
712 "APPLY_ORDER" => 100,
713 "IS_IN_PRICE" =>
"Y",
726 $this->deliveryTax = ($value ===
true? true :
false);
734 return $this->deliveryTax;
745 if ($this->
isClone() && $cloneEntity->contains($this))
747 return $cloneEntity[$this];
750 $taxClone = clone $this;
751 $taxClone->isClone =
true;
753 if (!$cloneEntity->contains($this))
755 $cloneEntity[$this] = $taxClone;
760 if ($cloneEntity->contains($this->order))
762 $taxClone->order = $cloneEntity[$this->order];
774 return $this->isClone;