public function newAction(Request $request)
 {
     $organization = $this->getRepository('BookkeepEntityBundle:Organization')->findOneBy(array("title" => $this->container->get('session')->get('organization')));
     if (!($pr = $this->get('session')->get('prRegistration'))) {
         $pr = new ProductRegistration();
         $item = new ProductRegistrationTradeItem();
         $pr->getTradeItems()->add($item);
         $this->get('session')->set('prRegistration', $pr);
     }
     $prForm = $this->createForm(new PrType($this->getUser(), $organization), $pr);
     if ($request->isMethod('GET')) {
         return $this->render('BookkeepAdminBundle:ProductRegistration:new.html.twig', array('prForm' => $prForm->createView()));
     }
     $prForm->handleRequest($request);
     if ($prForm->isValid()) {
         $amountSpent = 0;
         $pr->setManager($this->getUser());
         $shop = $pr->getShop();
         foreach ($pr->getTradeItems() as $item) {
             $item->setProductRegistration($pr);
             $amountSpent += $item->getQuantity() * $item->getPrice();
             $shopTrade = $this->getRepository('BookkeepEntityBundle:ShopTrade')->findOneBy(array('shop' => $shop, 'trade' => $item->getTrade()));
             if ($shopTrade) {
                 $shopTrade->setQuantity($shopTrade->getQuantity() + $item->getQuantity());
                 $this->save($shopTrade);
             } else {
                 $shopTrade = new ShopTrade();
                 $shopTrade->setShop($shop);
                 $shopTrade->setTrade($item->getTrade());
                 $shopTrade->setQuantity($item->getQuantity());
                 $this->save($shopTrade);
             }
         }
         $pr->setCost($amountSpent);
         $shop->setCash($shop->getCash() - $amountSpent);
         $cashOperation = new CashOperation();
         $cashOperation->setShop($shop);
         $cashOperation->setQuantity($amountSpent)->setType("DOWN")->setComment("Списано {$amountSpent} сом при оформлении прихода в магазине #{$shop->getId()}");
         $this->save($shop, $cashOperation, $pr);
         $this->getFlashBag()->add('success', 'Приход товаров оформлен!');
         $this->get('session')->set('prRegistration', null);
         return $this->render('BookkeepAdminBundle:ProductRegistration:new.html.twig', array('prForm' => $prForm->createView()));
     }
 }
Exemplo n.º 2
0
 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));
     }
 }