Skip to content

Commit

Permalink
Optimized different methods, to fit import order command.
Browse files Browse the repository at this point in the history
  • Loading branch information
hollodk committed Feb 16, 2012
1 parent 2578419 commit a6005dc
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 28 deletions.
4 changes: 2 additions & 2 deletions src/Club/LogBundle/Listener/NewOrderListener.php
Expand Up @@ -23,8 +23,8 @@ public function onShopOrder(\Club\ShopBundle\Event\FilterOrderEvent $event)
$log->setLogType('shop');
$log->setLog('Created a new order #'.$order->getId());

$user = $this->security_context->getToken()->getUser();
if ($user instanceOf \Club\UserBundle\Entity\User) $log->setUser($user);
if ($this->security_context->getToken() && $this->security_context->isGranted('IS_AUTHENTICATED_FULLY'))
$log->setUser($this->security_context->getToken()->getUser());

$this->em->persist($log);
$this->em->flush();
Expand Down
6 changes: 3 additions & 3 deletions src/Club/LogBundle/Listener/OrderChangeListener.php
Expand Up @@ -16,15 +16,15 @@ public function __construct($em,$security_context)
public function onOrderChange(\Club\ShopBundle\Event\FilterOrderEvent $event)
{
$order = $event->getOrder();
$user = $this->security_context->getToken()->getUser();

$log = new \Club\LogBundle\Entity\Log();
$log->setEvent('onOrderChange');
$log->setSeverity('informational');
$log->setUser($user);
$log->setLogType('shop');
$log->setLog('Changed order status on order #'.$order->getId());

if ($this->security_context->getToken() && $this->security_context->isGranted('IS_AUTHENTICATED_FULLY'))
$log->setUser($this->security_context->getToken()->getUser());

$this->em->persist($log);
$this->em->flush();
}
Expand Down
33 changes: 29 additions & 4 deletions src/Club/ShopBundle/Command/ImportOrderCommand.php
Expand Up @@ -16,6 +16,7 @@ protected function configure()
->setName('club:import:order')
->setDescription('Import orders')
->addArgument('file', InputArgument::REQUIRED, 'What filename to import')
->addArgument('location', InputArgument::REQUIRED, 'What location to shop from?')
->addArgument('product', InputArgument::REQUIRED, 'What product id to let the users buy?')
->setHelp(<<<EOF
The required filename just has to have a list of member numbers.
Expand All @@ -27,16 +28,40 @@ protected function configure()
protected function execute(InputInterface $input, OutputInterface $output)
{
$em = $this->getContainer()->get('doctrine.orm.entity_manager');
$product = $em->find('ClubShopBundle:Product', $input->getArgument('product'));
$location = $em->find('ClubUserBundle:Location', $input->getArgument('location'));

$fh = fopen($input->getArgument('file'), 'r');
while (!feof($fh)) {
$member_number = trim(fgets($fh, 1024));
if (strlen($member_number) > 0) {

$user = $em->getRepository('ClubUserBundle:User')->findOneBy(array(
'member_number' => $member_number
));
$user = $em->getRepository('ClubUserBundle:User')->findOneBy(array( 'member_number' => $member_number ));
if (!$user) throw new \Exception('No such user: '.$member_number);

if (!$user) throw new \Exception('No such user: '.$member_number);
$order = $this->getContainer()->get('order');
$order->createSimpleOrder($user, $location);
$cart_prod = new \Club\ShopBundle\Entity\CartProduct();
$cart_prod->setType('product');
$cart_prod->setQuantity(1);
$cart_prod->setPrice($product->getPrice());
$cart_prod->setProductName($product->getProductName());
$cart_prod->setProduct($product);

foreach ($product->getProductAttributes() as $attr) {
$product_attr = new \Club\ShopBundle\Entity\CartProductAttribute();
$product_attr->setCartProduct($cart_prod);
$product_attr->setValue($attr->getValue());
$product_attr->setAttributeName($attr->getAttribute());
$cart_prod->addCartProductAttribute($product_attr);
}

$order->addCartProduct($cart_prod);
$order->save();

$status = $em->getRepository('ClubShopBundle:OrderStatus')->getAcceptedStatus();
$order->changeStatus($status);
}
}
$em->flush();
}
Expand Down
10 changes: 4 additions & 6 deletions src/Club/ShopBundle/Controller/AdminOrderController.php
Expand Up @@ -40,14 +40,12 @@ public function editAction($id)
if ($this->getRequest()->getMethod() == 'POST') {
$form->bindRequest($this->getRequest());
if ($form->isValid()) {
$em = $this->getDoctrine()->getEntityManager();
$em->persist($order);
$em->flush();
$data = $form->getData();

$this->get('session')->setFlash('notice',$this->get('translator')->trans('Your changes are saved.'));
$this->get('order')->setOrder($order);
$this->get('order')->changeStatus($data->getOrderStatus());

$event = new \Club\ShopBundle\Event\FilterOrderEvent($order);
$this->get('event_dispatcher')->dispatch(\Club\ShopBundle\Event\Events::onOrderChange, $event);
$this->get('session')->setFlash('notice',$this->get('translator')->trans('Your changes are saved.'));

return $this->redirect($this->generateUrl('admin_shop_order'));
}
Expand Down
1 change: 1 addition & 0 deletions src/Club/ShopBundle/Entity/Product.php
Expand Up @@ -91,6 +91,7 @@ public function __construct()
{
$this->categories = new \Doctrine\Common\Collections\ArrayCollection();
$this->product_attributes = new \Doctrine\Common\Collections\ArrayCollection();
$this->type = 'product';
}

public function __toString()
Expand Down
29 changes: 22 additions & 7 deletions src/Club/ShopBundle/Helper/Order.php
Expand Up @@ -8,12 +8,17 @@ class Order
private $em;
private $event_dispatcher;

public function __construct($em,$event_dispatcher)
public function __construct($em, $event_dispatcher)
{
$this->em = $em;
$this->event_dispatcher = $event_dispatcher;
}

public function setOrder(\Club\ShopBundle\Entity\Order $order)
{
$this->order = $order;
}

public function createSimpleOrder(\Club\UserBundle\Entity\User $user, \Club\UserBundle\Entity\Location $location)
{
$this->order = new \Club\ShopBundle\Entity\Order();
Expand All @@ -26,18 +31,18 @@ public function createSimpleOrder(\Club\UserBundle\Entity\User $user, \Club\User
$this->setCurrency();
}

public function addSimpleProduct($product)
public function addSimpleProduct(\Club\ShopBundle\Entity\CartProduct $product)
{
// will not add product attribute
$prod = new \Club\ShopBundle\Entity\OrderProduct();

$prod->setOrder($this->order);
$prod->setPrice($product['price']);
$prod->setQuantity($product['quantity']);
$prod->setProductName($product['product_name']);
$prod->setType($product['type']);
$prod->setPrice($product->getPrice());
$prod->setQuantity($product->getQuantity());
$prod->setProductName($product->getProductName());
$prod->setType($product->getType());

$this->order->addOrderProduct($prod);

$this->em->persist($prod);
}

Expand Down Expand Up @@ -76,6 +81,16 @@ public function getOrder()
return $this->order;
}

public function changeStatus(\Club\ShopBundle\Entity\OrderStatus $order_status)
{
$this->order->setOrderStatus($order_status);
$this->em->persist($this->order);
$this->em->flush();

$event = new \Club\ShopBundle\Event\FilterOrderEvent($this->order);
$this->event_dispatcher->dispatch(\Club\ShopBundle\Event\Events::onOrderChange, $event);
}

private function setCustomerAddressByUser($user)
{
$address = $this->getAddressByUser($user);
Expand Down
2 changes: 2 additions & 0 deletions src/Club/TODO
@@ -1,4 +1,6 @@
== NOTES TO THIS RELEASE ==
validate change order works
validate team penalty still works
merge migrations into one file
filter with subscription date does not seems to wrk
quick create intervals when creating the field
Expand Down
12 changes: 6 additions & 6 deletions src/Club/TeamBundle/Listener/TeamPenaltyListener.php
Expand Up @@ -33,13 +33,13 @@ public function onTeamPenalty(\Club\TaskBundle\Event\FilterTaskEvent $event)
$participant = $this->em->getRepository('ClubTeamBundle:Participant')->getUserInRange($user->getUser(), $first, $last);
if (!count($participant)) {
if ($this->penalty_enabled) {
$product = new \Club\ShopBundle\Entity\CartProduct();
$product->setPrice($schedule->getPenalty());
$product->setQuantity(1);
$product->setType('team_fee');
$product->setProductName($this->penalty_product_name);

$this->order->createSimpleOrder($user->getUser(),$schedule->getLocation());
$product = array(
'price' => $schedule->getPenalty(),
'quantity' => 1,
'type' => 'product',
'product_name' => $this->penalty_product_name
);
$this->order->addSimpleProduct($product);
$this->order->save();
}
Expand Down

0 comments on commit a6005dc

Please sign in to comment.