Documentation

Accounting

Methods

Service is intended for handling of information, associated with money: payer types, amount calculation and etc.

Main use scenario: calculation of total amount for CRM item as per list of bound items with included taxes, discounts and etc.

Several amounts are calculated simultaneously during the calculation process. Calculation process may take some time.

Calculation results are cached inside the service at the item's level to avoid executing identical calculations multiple times.

Cache can be reset using a separate method. Also, the method for calculating without caching is available.

Service instance must be retrieved from container.

Method Description Available from version
public function isTaxMode(): 
bool
Returns true, when service uses tax calculation logic as for document tax.
Starting value is defined in service constructor from CRM settings.
public function setTaxMode(bool $isTaxMode): 
self
Sets tax calculation mode. When $isTaxMode = true, service will handle taxes as a document tax. When false, then its VAT.
public function clearCache(): 
self
Resets cache for items total amount calculations.
public function calculateByItem(Item $item): 
Accounting\Result
Executes total amount calculation for $item and writes data into the result (Accounting\Result).
Calculation total will be cached in the item. System uses the data already available in the cache, if available.
public function calculate
(array $productRows, string $currencyId, int $personTypeId, 
?string $locationId = null): 
?array
Method executes all calculations. Does not use cache.
This method is a wrapper for \CCrmSaleHelper::Calculate().
public function resolvePersonTypeId(Item $item): 
int
Returns payer type depending on item $item bindings.
public function calculatePriceWithoutTax
(float $priceWithTax, float $taxRate): float
  • $priceWithTax - price with tax;
  • $taxRate - tax rate in percentage (from 0 to 100).

Calculates amount without tax. For example, when $priceWithTax = 120, and $taxRate = 20, the result is 100.
public function calculatePriceWithTax
(float $priceWithoutTax, float $taxRate): 
float
  • $priceWithoutTax - price without tax;
  • $taxRate - tax rate in percentage (from 0 to 100).

Calculates total with tax. For example, when $priceWithoutTax = 100, and $taxRate = 20, the result is 120.

Accounting\Result

Calculation for total amount is passed as a separate descendant class Bitrix\Main\Result.

Contains both calculation results and error data.

Error data is rarely used. As a rule, calculation is required and one of result field is retrieved.

At the same time, this scenario operation requires several public methods.

When an error occurred during calculations, these methods return null.

Method Description Available from version
public function getPrice(): ?float
Returns total amount with taxes and discounts.
public function getCurrencyId(): ?string
Returns currency IDs.
public function getPersonTypeId(): ?int
Returns payer type ID.
public function getLocationId(): ?string
Returns location ID.
public function getTaxValue(): ?float
Returns tax amount.

Example

use Bitrix\Crm\Service;

$factory = Service\Container::getInstance()->getFactory(\CCrmOwnerType::Quote);

$item = $factory->createItem();

$productRow = \Bitrix\Crm\ProductRow::createFromArray([
	'PRODUCT_NAME' => 'new product',
	'PRICE' => 999,
	'QUANTITY' => 3,
	'DISCOUNT_RATE' => 20,
]);

$item->addToProductRows($productRow);

$price = Service\Container::getInstance()->getAccounting()->calculateByItem($item)->getPrice();

$item->setOpportunity($price);
© «Bitrix24», 2001-2024