public function buildSyncData(VendingMachineSync $vendingMachineSync = NULL)
 {
     $data = [];
     if ($vendingMachineSync) {
         $data[] = $vendingMachineSync->getSyncObjectData();
     }
     $data = [VendingMachineSync::getSyncArrayName() => $data];
     $syncResponse = [self::SYNC_CHECKSUM => $this->_checksum->getDataChecksum($data), self::SYNC_DATA => $data];
     return $syncResponse;
 }
 protected function recordTransactionData(VendingMachineSync $vendingMachineSync, $syncData)
 {
     $vendingMachineSync->setVendingMachineSyncId($syncData[self::SYNC_DATA][self::VENDING_MACHINE_SYNC_ARRAY][0][self::VENDING_MACHINE_SYNC_ID])->setSyncedType(self::VENDING_MACHINE_SYNC_TYPE_TRANSACTIONS);
     return $vendingMachineSync;
 }
 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];
 }
示例#4
0
 /**
  * Add vendingMachineSync
  *
  * @param \AppBundle\Entity\VendingMachine\VendingMachineSync $vendingMachineSync
  * @return VendingMachine
  */
 public function addVendingMachineSync(\AppBundle\Entity\VendingMachine\VendingMachineSync $vendingMachineSync)
 {
     $vendingMachineSync->setVendingMachine($this);
     $this->vendingMachineSyncs[] = $vendingMachineSync;
     return $this;
 }