Hi, can you help me pls.
Is it possible to update SalesOrderItem while doing calculations.
My aim is to reduce the price for vat amount, while I change taxRate to zero.
I'm doing custom calculation following the manual https://docs.espocrm.com/development...tions/#problem
To change List price I'm trying to get OrderItems and set new price.
I think that entity manager didn't save the data:
the full code according to the question here:
Thank you
Is it possible to update SalesOrderItem while doing calculations.
My aim is to reduce the price for vat amount, while I change taxRate to zero.
I'm doing custom calculation following the manual https://docs.espocrm.com/development...tions/#problem
To change List price I'm trying to get OrderItems and set new price.
I think that entity manager didn't save the data:
Code:
foreach ($OrderItems as $OrderItem) {
if($OrderItem->get('id') == $o->id) {
$OrderItem->set('listPrice',$o->unitPrice);
$OrderItem->set('unitPrice',$o->unitPrice);
$OrderItem->set('discount','0');
$this->entityManager->saveEntity($OrderItem);
$log_response = date("Y-m-d H:i:s") . ' $OrderItem-> ' . $OrderItem->get('id') . ' and $o-> ' . $o->id;
file_put_contents('data/logs/calculateItems-' . date("Y-m-d") . '.log', $log_response . PHP_EOL, FILE_APPEND | LOCK_EX);
$log_response = date("Y-m-d H:i:s") . ' listPrice ' . $OrderItem->get('listPrice'). ' unitPrice ' . $OrderItem->get('unitPrice');
file_put_contents('data/logs/calculateItems-' . date("Y-m-d") . '.log', $log_response . PHP_EOL, FILE_APPEND | LOCK_EX);
}
Code:
<?php
/* CUSTOM CALCULATION*/
namespace Espo\Custom\Repositories;
use Espo\ORM\Entity;
use Espo\ORM\EntityManager;
use Espo\Core\ORM\Repositories\RDB;
class SalesOrder extends \Espo\Modules\Sales\Repositories\SalesOrder
{
protected function calculateItems(\Espo\ORM\Entity $entity, array $itemptions = array())
{
parent::calculateItems($entity, $itemptions);
$log_response = date("Y-m-d H:i:s") . ' START ' ;
file_put_contents('data/logs/calculateItems-' . date("Y-m-d") . '.log', $log_response . PHP_EOL, FILE_APPEND | LOCK_EX);
$itemList = $entity->get('itemList');
$amount = 0.0;
$OrderItems = $this->entityManager->getRDBRepository('SalesOrder')->getRelation($entity, 'items')->find();
// $OrderItems = $this->getEntityManager()->getRepository('SalesOrder')->getRelation($entity, 'items')->find();
foreach ($itemList as $o) {
$log_response = date("Y-m-d H:i:s") . ' TAX RATE is ' . $o->taxRate;
file_put_contents('data/logs/calculateItems-' . date("Y-m-d") . '.log', $log_response . PHP_EOL, FILE_APPEND | LOCK_EX);
if($o->taxRate != 0) {
$o->unitPrice = $o->listPrice / (1 + $o->taxRate / 100.0);
$itemTaxAmount = $o->unitPrice * $o->taxRate / 100.0;
$taxAmount += $o->unitPrice * $o->quantity * $o->taxRate / 100.0;
$amount += $o->quantity * ($o->unitPrice + $itemTaxAmount);
}
if($o->taxRate == 0) {
$o->unitPrice = $o->listPrice / 1.21;
$log_response = date("Y-m-d H:i:s") . 'TAX RATE ' . $o->taxRate;
file_put_contents('data/logs/calculateItems-' . date("Y-m-d") . '.log', $log_response . PHP_EOL, FILE_APPEND | LOCK_EX);
$o->listPrice = $o->unitPrice;
$itemTaxAmount = 0;
$taxAmount += $o->unitPrice * $o->quantity * $o->taxRate / 100.0;
$amount += $o->quantity * ($o->unitPrice + $itemTaxAmount);
foreach ($OrderItems as $OrderItem) {
if($OrderItem->get('id') == $o->id) {
$OrderItem->set('listPrice',$o->unitPrice);
$OrderItem->set('unitPrice',$o->unitPrice);
$OrderItem->set('discount','0');
$this->entityManager->saveEntity($OrderItem);
$log_response = date("Y-m-d H:i:s") . ' $OrderItem-> ' . $OrderItem->get('id') . ' and $o-> ' . $o->id;
file_put_contents('data/logs/calculateItems-' . date("Y-m-d") . '.log', $log_response . PHP_EOL, FILE_APPEND | LOCK_EX);
$log_response = date("Y-m-d H:i:s") . ' listPrice ' . $OrderItem->get('listPrice'). ' unitPrice ' . $OrderItem->get('unitPrice');
file_put_contents('data/logs/calculateItems-' . date("Y-m-d") . '.log', $log_response . PHP_EOL, FILE_APPEND | LOCK_EX);
}
}
}
}
$entity->set('taxAmount', $taxAmount);
$entity->set('amount', $amount);
}
}
Thank you
