public function rawInsertTransaction(Transaction $transaction)
 {
     $conn = $this->getEntityManager()->getConnection();
     $queryArgs = [$transaction->getVendingMachine()->getId(), $transaction->getNfcTag()->getId(), $transaction->getStudent()->getId(), $transaction->getSyncTransactionId(), $transaction->getSyncTransactionAt()->format('Y-m-d H:i:s'), $transaction->getSyncNfcTagCode(), $transaction->getSyncStudentId(), $transaction->getVendingMachineSerial(), $transaction->getVendingMachineSyncId()];
     $queryArgsNumber = count($queryArgs);
     if (!$queryArgs) {
         return;
     }
     $queryString = "\n            INSERT INTO transactions (\n                vending_machine_id,\n                nfc_tag_id,\n                student_id,\n                sync_transaction_id,\n                sync_transaction_at,\n                sync_nfc_tag_code,\n                sync_student_id,\n                vending_machine_serial,\n                vending_machine_sync_id\n            ) VALUES (" . substr(str_repeat("?,", $queryArgsNumber), 0, -1) . ")";
     $stmt = $conn->prepare($queryString);
     $stmt->execute($queryArgs);
     return $conn->lastInsertId();
 }
Ejemplo n.º 2
0
 /**
  * Add transactions
  *
  * @param \AppBundle\Entity\Transaction\Transaction $transactions
  * @return NfcTag
  */
 public function addTransaction(\AppBundle\Entity\Transaction\Transaction $transactions)
 {
     $transactions->setNfcTag($this);
     $this->transactions[] = $transactions;
     return $this;
 }
Ejemplo n.º 3
0
 public function handleTransactionData(VendingMachine $vendingMachine, $data)
 {
     $nfcTags = new ArrayCollection($this->_manager->getRepository('AppBundle:NfcTag\\NfcTag')->findAvailableByVendingMachine($vendingMachine));
     if ($nfcTags->isEmpty()) {
         // Fallback to all available NFC tags, could signal problem
         $nfcTags = new ArrayCollection($this->_manager->getRepository('AppBundle:NfcTag\\NfcTag')->findAllIndexedByCode());
     }
     if (!($students = $vendingMachine->getStudents())) {
         // Fallback to all available students, could signal problem
         $students = new ArrayCollection($this->_manager->getRepository('AppBundle:Student\\Student')->findAll());
     }
     $transactionsArray = [];
     $banknoteListsArray = [];
     foreach ($data[self::SYNC_DATA][Transaction::getSyncArrayName()] as $value) {
         // KLUDGE: set code to lower case (minor TA architecture failure)
         $value[Transaction::TRANSACTION_NFC_CODE] = mb_strtolower($value[Transaction::TRANSACTION_NFC_CODE], 'UTF-8');
         if ($nfcTags->get($value[Transaction::TRANSACTION_NFC_CODE])) {
             $transaction = (new Transaction())->setSyncTransactionId($value[Transaction::TRANSACTION_SYNC_ID])->setSyncTransactionAt(new DateTime($value[Transaction::TRANSACTION_TRANSACTION_AT]));
             $transaction->setVendingMachine($vendingMachine)->setVendingMachineSerial($vendingMachine->getSerial())->setVendingMachineSyncId($data[self::SYNC_DATA][VendingMachineSync::getSyncArrayName()][0][self::VENDING_MACHINE_SYNC_ID]);
             $transaction->setSyncNfcTagCode($value[Transaction::TRANSACTION_NFC_CODE])->setNfcTag($nfcTags->get($value[Transaction::TRANSACTION_NFC_CODE]) ?: NULL);
             // TRICKY: Setting NFC Tag and Student separately, to preserve purchase history
             // in case if persisted NFC Tag is [unbinded from / binded to other] Student.
             // This is an emerged fallback mechanism, so in early versions of API value
             // could be empty - in that case getting Student from NFC Tag as usual
             if (!empty($value[Transaction::TRANSACTION_STUDENT_ID]) && $students->get($value[Transaction::TRANSACTION_STUDENT_ID])) {
                 $transaction->setSyncStudentId($value[Transaction::TRANSACTION_STUDENT_ID])->setStudent($students->get($value[Transaction::TRANSACTION_STUDENT_ID]) ?: NULL);
             } else {
                 $transaction->setSyncStudentId(NULL)->setStudent($nfcTags->get($value[Transaction::TRANSACTION_NFC_CODE]) ? $nfcTags->get($value[Transaction::TRANSACTION_NFC_CODE])->getStudent() ?: NULL : NULL);
             }
             // Explixitly setting Transaction id as last id from raw insert operation
             $transaction->setId($this->_manager->getRepository('AppBundle:Transaction\\Transaction')->rawInsertTransaction($transaction));
             foreach ($value[Banknote::getSyncArrayName()] as $nestedValue) {
                 $banknotes = new ArrayCollection($this->_manager->getRepository('AppBundle:Banknote\\Banknote')->findAll());
                 $matchingBanknoteCollection = $banknotes->filter(function ($banknote) use($nestedValue) {
                     if ($banknote->getCurrency() == $nestedValue[Banknote::BANKNOTE_CURRENCY] && $banknote->getNominal() == $nestedValue[Banknote::BANKNOTE_NOMINAL]) {
                         return TRUE;
                     }
                 });
                 $banknote = !$matchingBanknoteCollection->isEmpty() ? $matchingBanknoteCollection->first() : NULL;
                 if ($banknote) {
                     $banknoteList = (new BanknoteList())->setTransaction($transaction)->setBanknote($banknote)->setQuantity($nestedValue[Banknote::BANKNOTE_LIST_QUANTITY]);
                     $transaction->addBanknoteList($banknoteList);
                     $banknoteListsArray[] = $banknoteList;
                 } else {
                     $this->_logger->warning("SyncDataHandler: VM `" . $vendingMachine->getSerial() . "` posted contradictory Banknote: " . $nestedValue[Banknote::BANKNOTE_NOMINAL] . " " . $nestedValue[Banknote::BANKNOTE_CURRENCY]);
                 }
             }
             $transaction->setTotalAmount();
             $transactionsArray[] = $transaction;
         } else {
             //Logging value that somehow (!) contains wrong bindings
             $this->_logger->warning("SyncDataHandler: VM `" . $vendingMachine->getSerial() . "` posted contradictory NfcTag `code`: " . json_encode($value));
         }
     }
     // When transactions empty?
     if ($transactionsArray) {
         $this->_manager->getRepository('AppBundle:Transaction\\Transaction')->rawUpdateTransactionsTotalAmounts($transactionsArray);
         // When banknote lists empty?
         if ($banknoteListsArray) {
             $this->_manager->getRepository('AppBundle:Banknote\\BanknoteList')->rawInsertBanknoteLists($banknoteListsArray);
         }
         $studentsArray = [];
         foreach ($transactionsArray as $transaction) {
             // TODO: Some strange stuff in purchases there - checking for contradictionary NFC Tag
             $totalLimit = $transaction->getStudent()->getTotalLimit();
             $totalLimit = bcadd($totalLimit, $transaction->getTotalAmount(), 2);
             $studentsArray[] = ['id' => $transaction->getStudent()->getId(), 'totalLimit' => $totalLimit];
         }
         // When students empty?
         if ($studentsArray) {
             $this->_manager->getRepository('AppBundle:Student\\Student')->rawUpdateStudentsTotalLimits($studentsArray);
         }
     }
     return $data[self::SYNC_DATA][VendingMachineSync::getSyncArrayName()][0][self::VENDING_MACHINE_SYNC_ID];
 }
Ejemplo n.º 4
0
 /**
  * Add transactions
  *
  * @param \AppBundle\Entity\Transaction\Transaction $transactions
  * @return VendingMachine
  */
 public function addTransaction(\AppBundle\Entity\Transaction\Transaction $transactions)
 {
     $transactions->setVendingMachine($this);
     $this->transactions[] = $transactions;
     return $this;
 }