/**
  * @param \PHPUnit_Framework_MockObject_MockObject|LineItem $existingLineItem
  * @param mixed $newNotes
  *
  * @dataProvider lineItemDataProvider
  */
 public function testProcessExistingLineItem($existingLineItem, $newNotes)
 {
     $this->request = Request::create('/', 'PUT');
     $this->addRegistryExpectations($existingLineItem);
     $this->form->expects($this->once())->method('submit')->with($this->request);
     $this->form->expects($this->once())->method('isValid')->will($this->returnValue(true));
     $this->lineItem->expects($this->once())->method('getQuantity')->will($this->returnValue(40));
     $this->lineItem->expects($this->once())->method('getNotes')->will($this->returnValue($newNotes));
     $handler = new LineItemHandler($this->form, $this->request, $this->registry, $this->shoppingListManager);
     $this->assertTrue($handler->process($this->lineItem));
     $this->assertEquals(['savedId' => 123], $handler->updateSavedId([]));
 }
 /**
  * Create line item form
  *
  * @Route(
  *      "/create/{shoppingListId}",
  *      name="orob2b_shopping_list_line_item_create_widget",
  *      requirements={"shoppingListId"="\d+"}
  * )
  * @Template("OroB2BShoppingListBundle:LineItem:widget/update.html.twig")
  * @Acl(
  *      id="orob2b_shopping_list_line_item_create",
  *      type="entity",
  *      class="OroB2BShoppingListBundle:LineItem",
  *      permission="CREATE"
  * )
  * @ParamConverter("shoppingList", class="OroB2BShoppingListBundle:ShoppingList", options={"id" = "shoppingListId"})
  *
  * @param ShoppingList $shoppingList
  *
  * @return array|RedirectResponse
  */
 public function createAction(ShoppingList $shoppingList)
 {
     $lineItem = new LineItem();
     $lineItem->setShoppingList($shoppingList);
     $request = $this->getRequest();
     $form = $this->createForm(LineItemType::NAME, $lineItem);
     $handler = new LineItemHandler($form, $request, $this->getDoctrine());
     $result = $this->get('oro_form.model.update_handler')->handleUpdate($lineItem, $form, null, null, null, $handler);
     if ($request->get('_wid')) {
         $result = $handler->updateSavedId($result);
     }
     return $result;
 }
 /**
  * @param ObjectManager $manager
  * @param ShoppingList $shoppingList
  * @param ProductUnit $unit
  * @param Product $product
  * @param string $referenceName
  */
 protected function createLineItem(ObjectManager $manager, ShoppingList $shoppingList, ProductUnit $unit, Product $product, $referenceName)
 {
     $item = new LineItem();
     $item->setNotes('Test Notes');
     $item->setAccountUser($shoppingList->getAccountUser());
     $item->setOrganization($shoppingList->getOrganization());
     $item->setShoppingList($shoppingList);
     $item->setUnit($unit);
     $item->setProduct($product);
     $item->setQuantity(23.15);
     $manager->persist($item);
     $this->addReference($referenceName, $item);
 }
 /**
  * @param LineItem          $lineItem
  * @param ShoppingList|null $shoppingList
  * @param bool|true         $flush
  */
 public function addLineItem(LineItem $lineItem, ShoppingList $shoppingList, $flush = true)
 {
     $em = $this->managerRegistry->getManagerForClass('OroB2BShoppingListBundle:LineItem');
     $lineItem->setShoppingList($shoppingList);
     /** @var LineItemRepository $repository */
     $repository = $em->getRepository('OroB2BShoppingListBundle:LineItem');
     $duplicate = $repository->findDuplicate($lineItem);
     if ($duplicate instanceof LineItem && $shoppingList->getId()) {
         $duplicate->setQuantity($duplicate->getQuantity() + $lineItem->getQuantity());
     } else {
         $shoppingList->addLineItem($lineItem);
         $em->persist($lineItem);
     }
     if ($flush) {
         $em->flush();
     }
 }
 /**
  * @param \PHPUnit_Framework_MockObject_MockObject|LineItem $existingLineItem
  * @param mixed $newNotes
  *
  * @dataProvider lineItemDataProvider
  */
 public function testProcessExistingLineItem($existingLineItem, $newNotes)
 {
     $this->request->expects($this->once())->method('getMethod')->will($this->returnValue('PUT'));
     $this->form->expects($this->once())->method('submit')->with($this->request);
     $this->form->expects($this->once())->method('isValid')->will($this->returnValue(true));
     $this->lineItem->expects($this->once())->method('getQuantity')->will($this->returnValue(40));
     $this->lineItem->expects($this->once())->method('getNotes')->will($this->returnValue($newNotes));
     $repository = $this->getMockBuilder('OroB2B\\Bundle\\ShoppingListBundle\\Entity\\Repository\\LineItemRepository')->disableOriginalConstructor()->getMock();
     $repository->expects($this->once())->method('findDuplicate')->with($this->lineItem)->will($this->returnValue($existingLineItem));
     $manager = $this->getMock('Doctrine\\Common\\Persistence\\ObjectManager');
     $manager->expects($this->once())->method('getRepository')->with(self::LINE_ITEM_SHORTCUT)->will($this->returnValue($repository));
     $manager->expects($this->never())->method('persist');
     $manager->expects($this->once())->method('flush');
     $this->registry->expects($this->once())->method('getManagerForClass')->with(self::LINE_ITEM_SHORTCUT)->will($this->returnValue($manager));
     $handler = new LineItemHandler($this->form, $this->request, $this->registry);
     $this->assertTrue($handler->process($this->lineItem));
     $this->assertEquals(['savedId' => 123], $handler->updateSavedId([]));
 }
 /**
  * Find line item with the same product and unit
  *
  * @param LineItem $lineItem
  *
  * @return LineItem
  */
 public function findDuplicate(LineItem $lineItem)
 {
     $qb = $this->createQueryBuilder('li')->where('li.product = :product')->andWhere('li.unit = :unit')->andWhere('li.shoppingList = :shoppingList')->setParameter('product', $lineItem->getProduct())->setParameter('unit', $lineItem->getUnit())->setParameter('shoppingList', $lineItem->getShoppingList());
     if ($lineItem->getId()) {
         $qb->andWhere('li.id != :currentId')->setParameter('currentId', $lineItem->getId());
     }
     return $qb->getQuery()->getOneOrNullResult();
 }
 /**
  * @param LineItem $lineItem
  * @param LineItem $existingLineItem
  */
 protected function updateExistingLineItem(LineItem $lineItem, LineItem $existingLineItem)
 {
     $existingLineItem->setQuantity($lineItem->getQuantity() + $existingLineItem->getQuantity());
     $existingLineItemNote = $existingLineItem->getNotes();
     $newNotes = $lineItem->getNotes();
     $notes = trim(implode(' ', [$existingLineItemNote, $newNotes]));
     if ($notes) {
         $existingLineItem->setNotes($notes);
     }
     $this->savedId = $existingLineItem->getId();
 }
 /**
  * @return array
  */
 public function submitDataProvider()
 {
     $product = $this->getProductEntityWithPrecision(1, 'kg', 3);
     $defaultLineItem = new LineItem();
     $defaultLineItem->setProduct($product);
     /** @var ShoppingList $expectedShoppingList */
     $expectedShoppingList = $this->getShoppingList(1, 'Shopping List 1');
     $expectedLineItem = clone $defaultLineItem;
     $expectedLineItem->setQuantity(15.112)->setUnit($product->getUnitPrecision('kg')->getUnit())->setShoppingList($expectedShoppingList);
     return ['New line item with existing shopping list' => ['defaultData' => $defaultLineItem, 'submittedData' => ['shoppingList' => 1, 'quantity' => 15.1119, 'unit' => 'kg', 'shoppingListLabel' => null], 'expectedData' => $expectedLineItem]];
 }
 /**
  * @return array
  */
 public function submitDataProvider()
 {
     $shoppingList = new ShoppingList();
     /** @var Product $expectedProduct */
     $expectedProduct = $this->getProductEntityWithPrecision(1, 'kg', 3);
     $defaultLineItem = new LineItem();
     $defaultLineItem->setShoppingList($shoppingList);
     $expectedLineItem = clone $defaultLineItem;
     $expectedLineItem->setProduct($expectedProduct)->setQuantity('10')->setUnit($expectedProduct->getUnitPrecision('kg')->getUnit())->setNotes('my note');
     $existingLineItem = $this->getEntity('OroB2B\\Bundle\\ShoppingListBundle\\Entity\\LineItem', 2);
     $existingLineItem->setShoppingList($shoppingList)->setProduct($expectedProduct)->setQuantity(5)->setUnit($expectedProduct->getUnitPrecision('kg')->getUnit())->setNotes('my note2');
     $expectedLineItem2 = clone $existingLineItem;
     $expectedLineItem2->setQuantity(15.112)->setUnit($expectedProduct->getUnitPrecision('kg')->getUnit())->setNotes('note1');
     $expectedLineItem3 = clone $existingLineItem;
     $expectedLineItem3->setQuantity(15.112)->setUnit($expectedProduct->getUnitPrecision('kg')->getUnit())->setNotes(null);
     return ['new line item' => ['defaultData' => $defaultLineItem, 'submittedData' => ['product' => 1, 'quantity' => 10, 'unit' => 'kg', 'notes' => 'my note'], 'expectedData' => $expectedLineItem, 'isExisting' => false], 'existing line item' => ['defaultData' => $existingLineItem, 'submittedData' => ['product' => 2, 'quantity' => 15.1119, 'unit' => 'kg', 'notes' => 'note1'], 'expectedData' => $expectedLineItem2, 'isExisting' => true], 'missing product' => ['defaultData' => $existingLineItem, 'submittedData' => ['unit' => 'kg', 'quantity' => 15.1119], 'expectedData' => $expectedLineItem3, 'isExisting' => true]];
 }
 /**
  * @param LineItem $data
  * @param ExecutionContextInterface $context
  */
 public function checkShoppingListLabel($data, ExecutionContextInterface $context)
 {
     if (!$data->getShoppingList()) {
         $context->buildViolation('orob2b.shoppinglist.not_empty')->atPath('shoppingListLabel')->addViolation();
     }
 }