public function testCreateFromQuote() { $currency = new Currency('USD'); $this->dispatcher->shouldReceive('dispatch')->withAnyArgs(); $client = new Client(); $client->setName('Test Client'); $client->setWebsite('http://example.com'); $client->setCreated(new \DateTime('NOW')); $tax = new Tax(); $tax->setName('VAT'); $tax->setRate('14'); $tax->setType(Tax::TYPE_INCLUSIVE); $item = new Item(); $item->setTax($tax); $item->setDescription('Item Description'); $item->setCreated(new \DateTime('now')); $item->setPrice(new Money(120, $currency)); $item->setQty(10); $item->setTotal(new Money(12 * 10, $currency)); $quote = new Quote(); $quote->setBaseTotal(new Money(123, $currency)); $quote->setDiscount(new Money(12, $currency)); $quote->setNotes('Notes'); $quote->setTax(new Money(432, $currency)); $quote->setTerms('Terms'); $quote->setTotal(new Money(987, $currency)); $quote->setClient($client); $quote->addItem($item); $invoice = $this->manager->createFromQuote($quote); $this->assertSame($quote->getTotal(), $invoice->getTotal()); $this->assertSame($quote->getBaseTotal(), $invoice->getBaseTotal()); $this->assertSame($quote->getDiscount(), $invoice->getDiscount()); $this->assertSame($quote->getNotes(), $invoice->getNotes()); $this->assertSame($quote->getTerms(), $invoice->getTerms()); $this->assertSame($quote->getTax(), $invoice->getTax()); $this->assertSame($client, $invoice->getClient()); $this->assertSame('new', $invoice->getStatus()); $this->assertNotSame($quote->getUuid(), $invoice->getUuid()); $this->assertNull($invoice->getId()); $this->assertCount(1, $invoice->getItems()); /** @var \CSBill\InvoiceBundle\Entity\item[] $invoiceItem */ $invoiceItem = $invoice->getItems(); $this->assertInstanceOf('CSBill\\InvoiceBundle\\Entity\\item', $invoiceItem[0]); $this->assertSame($item->getTax(), $invoiceItem[0]->getTax()); $this->assertSame($item->getDescription(), $invoiceItem[0]->getDescription()); $this->assertInstanceOf('DateTime', $invoiceItem[0]->getCreated()); $this->assertSame($item->getPrice(), $invoiceItem[0]->getPrice()); $this->assertSame($item->getQty(), $invoiceItem[0]->getQty()); }
/** * @param Quote $quote * * @return Quote */ public function duplicate(Quote $quote) { // We don't use 'clone', since cloning aq quote will clone all the item id's and nested values. // We rather set it manually $newQuote = new Quote(); $now = Carbon::now(); $newQuote->setCreated($now); $newQuote->setClient($quote->getClient()); $newQuote->setBaseTotal($quote->getBaseTotal()); $newQuote->setDiscount($quote->getDiscount()); $newQuote->setNotes($quote->getNotes()); $newQuote->setTotal($quote->getTotal()); $newQuote->setTerms($quote->getTerms()); $newQuote->setUsers($quote->getUsers()->toArray()); if (null !== $quote->getTax()) { $newQuote->setTax($quote->getTax()); } foreach ($quote->getItems() as $item) { $invoiceItem = new Item(); $invoiceItem->setCreated($now); $invoiceItem->setTotal($item->getTotal()); $invoiceItem->setDescription($item->getDescription()); $invoiceItem->setPrice($item->getPrice()); $invoiceItem->setQty($item->getQty()); if (null !== $item->getTax()) { $invoiceItem->setTax($item->getTax()); } $newQuote->addItem($invoiceItem); } $this->dispatcher->dispatch(QuoteEvents::QUOTE_PRE_CREATE, new QuoteEvent($newQuote)); $stateMachine = $this->stateMachine->get($newQuote, Graph::GRAPH); if ($stateMachine->can(Graph::TRANSITION_NEW)) { $stateMachine->apply(Graph::TRANSITION_NEW); } $this->entityManager->persist($newQuote); $this->entityManager->flush(); $this->dispatcher->dispatch(QuoteEvents::QUOTE_POST_CREATE, new QuoteEvent($newQuote)); return $newQuote; }