/** * 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; }
/** * Emails a quote to the customers. * * @param Quote $quote * * @return int If the email was successfully sent */ public function sendQuote(Quote $quote) { $htmlTemplate = $this->getTemplate('CSBillQuoteBundle:Email:quote.html.twig', ['quote' => $quote]); $textTemplate = $this->getTemplate('CSBillQuoteBundle:Email:quote.txt.twig', ['quote' => $quote]); $subject = $this->getSubject('quote.email_subject', $quote->getId()); $users = []; foreach ($quote->getUsers() as $user) { /* @var \CSBill\ClientBundle\Entity\Contact $user */ $users[(string) $user->getEmail()] = $user->getFirstName() . ' ' . $user->getLastName(); } $event = new QuoteEvent(); $event->setQuote($quote); $bcc = (string) $this->settings->get('quote.bcc_address'); $sent = $this->sendMessage($subject, $users, $htmlTemplate, $textTemplate, $event, $bcc); return $sent; }
/** * Emails a quote to the customers. * * @param Quote $quote * * @return int If the email was successfully sent */ public function sendQuote(Quote $quote) { // TODO : this needs to come from settings or somewhere so it can be extended $htmlTemplate = $this->getTemplate('CSBillQuoteBundle:Email:quote.html.twig', array('quote' => $quote)); $textTemplate = $this->getTemplate('CSBillQuoteBundle:Email:quote.txt.twig', array('quote' => $quote)); $subject = $this->getSubject('quote.email_subject', $quote->getId()); $users = array(); foreach ($quote->getUsers() as $user) { /* @var \CSBill\ClientBundle\Entity\Contact $user */ $users[(string) $user->getPrimaryDetail('email')] = $user->getFirstname() . ' ' . $user->getLastname(); } $event = new QuoteEvent(); $event->setQuote($quote); $bcc = (string) $this->settings->get('quote.bcc_address'); $sent = $this->sendMessage($subject, $users, $htmlTemplate, $textTemplate, $event, $bcc); return $sent; }