/** * @param Quote|Invoice $object * @param Money $total * * @return mixed */ private function setDiscount($object, Money $total) { if (null !== $object->getDiscount()) { $discount = $total->multiply($object->getDiscount()); $total = $total->subtract($discount); return $total; } return $total; }
/** * Create an invoice from a quote. * * @param Quote $quote * * @return Invoice */ public function createFromQuote(Quote $quote) { $invoice = new Invoice(); $now = Carbon::now(); $invoice->setCreated($now); $invoice->setClient($quote->getClient()); $invoice->setBaseTotal($quote->getBaseTotal()); $invoice->setDiscount($quote->getDiscount()); $invoice->setNotes($quote->getNotes()); $invoice->setTotal($quote->getTotal()); $invoice->setTerms($quote->getTerms()); $invoice->setUsers($quote->getUsers()->toArray()); $invoice->setBalance($invoice->getTotal()); if (null !== $quote->getTax()) { $invoice->setTax($quote->getTax()); } /** @var \CSBill\QuoteBundle\Entity\Item $item */ 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()); } $invoice->addItem($invoiceItem); } $this->create($invoice); $this->accept($invoice); // ?? Do we really want to accept it immediately after creating it? return $invoice; }
/** * @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; }