private function createMatch(Entity\BankStatement $statement, Entity\Code $code) { $em = $this->getServiceLocator()->get('Doctrine\\ORM\\EntityManager'); $bankaccount = $statement->getBankAccount(); #$statement_format = json_decode($bankaccount->getStatementFormat()); $order = $em->getRepository("ErsBase\\Entity\\Order")->findOneBy(array('Code_id' => $code->getId())); $statement->setStatus('matched'); $em->persist($statement); $match = new Entity\Match(); $match->setBankStatement($statement); $match->setOrder($order); $match->setStatus('active'); $match->setComment('matched by auto-matching'); /* * set andi as admin for auto-matching */ $admin = $em->getRepository("ErsBase\\Entity\\User")->findOneBy(array('id' => 1)); $match->setAdmin($admin); $order->addMatch($match); $em->persist($match); #$statement_amount = (float) $statement->getBankStatementColByNumber($statement_format->amount)->getValue(); $statement_amount = $order->getStatementAmount(); $order_amount = $order->getSum(); if ($order_amount == $statement_amount) { $paid = true; #echo "perfect match!".PHP_EOL; echo "."; } elseif ($order_amount < $statement_amount) { $paid = true; #echo "overpaid, ok!".PHP_EOL; echo "!"; } else { $paid = false; echo "-"; } if ($paid) { #$orderStatus = new Entity\OrderStatus(); #$orderStatus->setOrder($order); #$orderStatus->setValue('paid'); #$order->addOrderStatus($orderStatus); $order->setPaymentStatus('paid'); foreach ($order->getItems() as $item) { $item->setStatus('paid'); $em->persist($item); } $em->persist($order); #$em->persist($orderStatus); } else { foreach ($order->getItems() as $item) { $item->setStatus('ordered'); $em->persist($item); } } $em->flush(); }
public function acceptAction() { $logger = $this->getServiceLocator()->get('Logger'); $param_orders = $this->params()->fromQuery('orders', array()); $param_statements = $this->params()->fromQuery('statements', array()); $params['orders'] = $param_orders; $params['statements'] = $param_statements; $em = $this->getServiceLocator()->get('Doctrine\\ORM\\EntityManager'); $form = new Form\AcceptMatch(); $orders = array(); $order_options = array(); foreach ($param_orders as $order_id) { $orders[] = $em->getRepository("ErsBase\\Entity\\Order")->findOneBy(array('id' => $order_id)); $order_options[] = array('label' => $order_id, 'value' => $order_id); } $form->get('Order_id')->setValueOptions($order_options); $statements = array(); $statement_options = array(); foreach ($param_statements as $statement_id) { $statements[] = $em->getRepository("ErsBase\\Entity\\BankStatement")->findOneBy(array('id' => $statement_id)); $statement_options[] = array('label' => $statement_id, 'value' => $statement_id); } $form->get('BankStatement_id')->setValueOptions($statement_options); $form->get('Admin_id')->setValue($this->zfcUserAuthentication()->getIdentity()->getId()); $request = $this->getRequest(); if ($request->isPost()) { $inputFilter = new InputFilter\AcceptMatch(); #$form->setInputFilter($inputFilter->getInputFilter()); $form->setData($request->getPost()); if ($form->isValid()) { $data = $form->getData(); $logger->info(var_export($data, true)); $status = 'unpaid'; if ($data['half-match'] == null) { $status = 'paid'; } /* * get orders */ $orders = array(); foreach ($data['Order_id'] as $order_id) { $orders[] = $em->getRepository("ErsBase\\Entity\\Order")->findOneBy(array('id' => $order_id)); } $order_sum = 0; foreach ($orders as $order) { /* * calculate prices with payment fees. */ $order_sum += $order->getSum(); } /* * get statements */ $statements = array(); foreach ($data['BankStatement_id'] as $statement_id) { $statements[] = $em->getRepository("ErsBase\\Entity\\BankStatement")->findOneBy(array('id' => $statement_id)); } $statement_sum = 0; foreach ($statements as $statement) { $statement_sum += (double) $statement->getAmount()->getValue(); } /* * do matches and set order to paid */ foreach ($orders as $order) { foreach ($order->getItems() as $item) { if ($status == 'paid') { $item->setStatus('paid'); $em->persist($item); } elseif ($status == 'unpaid') { $item->setStatus('ordered'); $em->persist($item); } } $order->setPaymentStatus($status); $em->persist($order); foreach ($statements as $statement) { $match = new Entity\Match(); $match->setOrder($order); $match->setStatus('active'); $match->setBankStatement($statement); $user = $this->zfcUserAuthentication()->getIdentity(); #$match->setAdminId($this->zfcUserAuthentication()->getIdentity()->getId()); $match->setAdmin($user); $match->setComment($data['comment']); $em->persist($match); $statement->setStatus('matched'); $em->persist($statement); } } $em->flush(); #$params['statements'] = $data['statements']; #$params['orders'] = $data['orders']; return $this->redirect()->toRoute('admin/matching', array('action' => 'manual')); } else { $logger->warn($form->getMessages()); } } return new ViewModel(array('orders' => $orders, 'statements' => $statements, 'form' => $form, 'params' => $params)); }