/** * Creates a new Document entity. * * @Route("/create/sale/{id}", name="finance_document_ci_create_from_sale") * @Method("GET") * @Template("FlowerFinancesBundle:Document:new.html.twig") */ public function createFromSaleAction(Request $request, Sale $sale) { $document = new Document(); $document->setAccount($sale->getAccount()); $document->setDiscount($sale->getDiscount()); $document->setTax($sale->getTax()); $document->setTotal($sale->getTotal()); $document->setTotalDiscount($sale->getTotalDiscount()); $document->setTotalWithTax($sale->getTotalWithTax()); $type = $this->getDoctrine()->getManager()->getRepository('FlowerFinancesBundle:DocumentType')->findOneBy(array('name' => \Flower\FinancesBundle\Entity\DocumentType::TYPE_CUSTOMER_INVOICE)); $document->setFinanceAccount($sale->getFinanceAccount()); $document->setType($type); $document->setSale($sale); foreach ($sale->getSaleItems() as $saleItem) { $invoiceItem = new DocumentItem(); $invoiceItem->setProduct($saleItem->getProduct()); $invoiceItem->setService($saleItem->getService()); $invoiceItem->setTotal($saleItem->getTotal()); $invoiceItem->setDocument($document); $invoiceItem->setUnitPrice($saleItem->getUnitPrice()); $invoiceItem->setUnits($saleItem->getUnits()); $document->addItem($invoiceItem); } $em = $this->getDoctrine()->getManager(); $em->persist($document); $em->flush(); $sale->setDocument($document); $em->flush(); return $this->redirect($this->generateUrl('finance_document_ci_show', array('id' => $document->getId()))); }
/** * Creates a new Document entity. * * @Route("/create/sale/{id}", name="finance_document_create_from_sale") * @Method("GET") * @Template("FlowerFinancesBundle:Document:new.html.twig") */ public function createFromSaleAction(Request $request, Sale $sale) { $document = new Document(); $document->setAccount($sale->getAccount()); $document->setDiscount($sale->getDiscount()); $document->setTax($sale->getTax()); $document->setTotal($sale->getTotal()); $document->setTotalDiscount($sale->getTotalDiscount()); $document->setTotalWithTax($sale->getTotalWithTax()); $document->setType(Document::TYPE_CUSTOMER_INVOICE); $document->setSale($sale); foreach ($sale->getSaleItems() as $saleItem) { $invoiceItem = new DocumentItem(); $invoiceItem->setProduct($saleItem->getProduct()); $invoiceItem->setService($saleItem->getService()); $invoiceItem->setTotal($saleItem->getTotal()); $invoiceItem->setDocument($document); $invoiceItem->setUnitPrice($saleItem->getUnitPrice()); $invoiceItem->setUnits($saleItem->getUnits()); $document->addItem($invoiceItem); } $em = $this->getDoctrine()->getManager(); $em->persist($document); $em->flush(); $sale->setDocument($document); $em->flush(); return $this->redirect($this->generateUrl('finance_document_show', array('id' => $document->getId()))); }