3 code examples of PHP Oro\Bundle\SalesBundle\Entity\Opportunity extracted from open source projects
/**
* @depends testChangeStatusAffectsLifetime
*/
public function testCustomerChangeShouldUpdateBothCustomersIfValuable(Opportunity $opportunity): Opportunity
{
$em = $this->getEntityManager();
/** @var B2bCustomer $b2bCustomer */
$b2bCustomer = $opportunity->getCustomerAssociation()->getTarget();
$this->assertEquals(100, $b2bCustomer->getLifetime());
$newCustomer = new B2bCustomer();
$newCustomer->setName(uniqid('name'));
$account = $this->getReference('default_account');
$newCustomer->setAccount($account);
$em->persist($newCustomer);
$em->flush();
$this->assertEquals(0, $newCustomer->getLifetime());
$accountCustomer = $this->getAccountCustomerManager()->getAccountCustomerByTarget($newCustomer);
$opportunity->setCustomerAssociation($accountCustomer);
$em->persist($opportunity);
$em->flush();
$em->refresh($b2bCustomer);
$em->refresh($newCustomer);
$this->assertEquals(0, $b2bCustomer->getLifetime());
$this->assertEquals(100, $newCustomer->getLifetime());
return $opportunity;
}
/**
* @depends testCreateAffectsLifetimeIfValuable
*/
public function testChangeStatusAffectsLifetime(Opportunity $opportunity): Opportunity
{
$em = $this->getEntityManager();
$b2bCustomer = $opportunity->getCustomerAssociation()->getTarget();
$enumClass = ExtendHelper::buildEnumValueClassName(Opportunity::INTERNAL_STATUS_CODE);
$opportunity->setStatus($em->getReference($enumClass, 'lost'));
$em->persist($opportunity);
$em->flush();
$em->refresh($b2bCustomer);
$this->assertEquals(0, $b2bCustomer->getLifetime());
$opportunity->setStatus($em->getReference($enumClass, 'won'));
$closeRevenue = MultiCurrency::create(100, 'USD');
$opportunity->setCloseRevenue($closeRevenue);
$em->persist($opportunity);
$em->flush();
$em->refresh($b2bCustomer);
$this->assertEquals(100, $b2bCustomer->getLifetime());
return $opportunity;
}
public function preUpdate(Opportunity $entity, PreUpdateEventArgs $args)
{
if (null === $entity->getStatus()) {
return;
}
if (!$args->hasChangedField('status')) {
return;
}
if ($this->hasWorkflowRestriction($entity)) {
return;
}
$probability = $this->getDefaultProbability($entity->getStatus()->getId());
if (null === $probability) {
return;
}
$oldProbability = $probability;
if (null !== $args->getOldValue('status')) {
$oldProbability = $this->getDefaultProbability($args->getOldValue('status')->getId());
}
// don't change if it's already overwritten
if ($oldProbability !== $entity->getProbability()) {
return;
}
$entity->setProbability($probability);
$this->recomputeChangeSet($args);
}