public function newAction(Request $request, $shopid) { $trades = null; $shop = $this->getRepository('BookkeepEntityBundle:Shop')->find($shopid); if (!($rp = $this->get('session')->get('refundProduct'))) { $rp = new RefundProduct(); $item = new RefundItem(); $rp->getRefundItems()->add($item); $this->get('session')->set('refundProduct', $rp); } $shopTrades = $this->getRepository('BookkeepEntityBundle:ShopTrade')->findBy(array('shop' => $shop)); foreach ($shopTrades as $shopTrade) { if ($shopTrade->getQuantity() == 0) { $this->remove($shopTrade); } $trades[] = $shopTrade->getTrade(); } $rpForm = $this->createForm(new RefundProductType($shop), $rp); if ($request->isMethod('GET')) { return $this->render('BookkeepAdminBundle:Refund:new.html.twig', array('rpForm' => $rpForm->createView(), 'shop' => $shop)); } $rpForm->handleRequest($request); if ($rpForm->isValid()) { $earnedSum = 0; $rp->setManager($this->getUser()); $rp->setShop($shop); $shop = $this->getRepository('BookkeepEntityBundle:Shop')->find($shopid); foreach ($rp->getRefundItems() as $item) { $item->setRefundProduct($rp); $shopTrade = $this->getRepository('BookkeepEntityBundle:ShopTrade')->findOneBy(array('shop' => $shop, 'trade' => $item->getTrade())); if ($item->getRestore()) { if ($shopTrade) { $shopTrade->setQuantity($shopTrade->getQuantity() + $item->getQuantity()); } else { $shopTrade = new ShopTrade(); $shopTrade->setShop($shop); $shopTrade->setTrade($item->getTrade()); $shopTrade->setQuantity($item->getQuantity()); } } $earnedSum = $earnedSum + $item->getPrice() * $item->getQuantity(); } $shop->setCash($shop->getCash() + $earnedSum); $shopTrades = $this->getRepository('BookkeepEntityBundle:ShopTrade')->findBy(array('shop' => $shop)); foreach ($shopTrades as $shopTrade) { if ($shopTrade->getQuantity() == 0) { $this->remove($shopTrade); } } $cashOperation = new CashOperation(); $cashOperation->setShop($shop); $cashOperation->setQuantity($earnedSum)->setType("UP")->setComment("Возвращено {$earnedSum} сом в магазине #{$shop->getId()} при возврате товаров"); $this->save($shop, $cashOperation, $rp); $this->getFlashBag()->add('success', 'Возврат товаров оформлен!'); $this->get('session')->set('refundProduct', null); return $this->render('BookkeepAdminBundle:Refund:new.html.twig', array('rpForm' => $rpForm->createView(), 'shop' => $shop)); } }