public function handlePurchaseData(VendingMachine $vendingMachine, $data)
 {
     // TODO: This fallback will cause problems, if prices in ProductVendingGroup are different from default
     if (!($products = $vendingMachine->getProducts())) {
         // Fallback to all available products, could signal problem
         $products = new ArrayCollection($this->_manager->getRepository('AppBundle:Product\\Product')->findAll());
     }
     $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());
     }
     $purchasesArray = [];
     foreach ($data[self::SYNC_DATA][Purchase::getSyncArrayName()] as $value) {
         // KLUDGE: set code to lower case (minor TA architecture failure)
         $value[Purchase::PURCHASE_NFC_CODE] = mb_strtolower($value[Purchase::PURCHASE_NFC_CODE], 'UTF-8');
         if ($nfcTags->get($value[Purchase::PURCHASE_NFC_CODE]) && $products->get($value[Purchase::PURCHASE_PRODUCT_ID])) {
             $purchase = (new Purchase())->setSyncPurchaseId($value[Purchase::PURCHASE_SYNC_ID])->setSyncPurchasedAt(new DateTime($value[Purchase::PURCHASE_PURCHASED_AT]));
             $purchase->setVendingMachine($vendingMachine)->setVendingMachineSerial($vendingMachine->getSerial())->setVendingMachineSyncId($data[self::SYNC_DATA][VendingMachineSync::getSyncArrayName()][0][self::VENDING_MACHINE_SYNC_ID]);
             $purchase->setSyncNfcTagCode($value[Purchase::PURCHASE_NFC_CODE])->setNfcTag($nfcTags->get($value[Purchase::PURCHASE_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[Purchase::PURCHASE_STUDENT_ID]) && $students->get($value[Purchase::PURCHASE_STUDENT_ID])) {
                 $purchase->setSyncStudentId($value[Purchase::PURCHASE_STUDENT_ID])->setStudent($students->get($value[Purchase::PURCHASE_STUDENT_ID]));
             } else {
                 $purchase->setSyncStudentId(NULL)->setStudent($nfcTags->get($value[Purchase::PURCHASE_NFC_CODE]) ? $nfcTags->get($value[Purchase::PURCHASE_NFC_CODE])->getStudent() ?: NULL : NULL);
             }
             $purchase->setSyncProductId($value[Purchase::PURCHASE_PRODUCT_ID])->setSyncProductPrice($value[Purchase::PURCHASE_SYNC_PRODUCT_PRICE])->setProduct($products->get($value[Purchase::PURCHASE_PRODUCT_ID]) ? $products->get($value[Purchase::PURCHASE_PRODUCT_ID]) : NULL);
             $purchasesArray[] = $purchase;
         } else {
             //Logging value that somehow (!) contains wrong bindings
             $this->_logger->warning("SyncDataHandler: VM `" . $vendingMachine->getSerial() . "` posted contradictory NfcTag `code` or Product `id`: " . json_encode($value));
         }
     }
     // When purchases empty?
     if ($purchasesArray) {
         $this->_manager->getRepository('AppBundle:Purchase\\Purchase')->rawInsertPurchases($purchasesArray);
         $purchasesAggregated = $this->_manager->getRepository('AppBundle:Purchase\\Purchase')->findSumsByStudentsWithSyncId($vendingMachine, $data[self::SYNC_DATA][VendingMachineSync::getSyncArrayName()][0][self::VENDING_MACHINE_SYNC_ID]);
         $studentsArray = [];
         foreach ($purchasesAggregated as $purchase) {
             if ($nfcTags->get($purchase['code'])->getStudent()) {
                 $totalLimit = $nfcTags->get($purchase['code'])->getStudent()->getTotalLimit();
                 $totalLimit = bcsub($totalLimit, $purchase['price_sum'], 2);
                 $studentsArray[] = ['id' => $nfcTags->get($purchase['code'])->getStudent()->getId(), 'totalLimit' => $totalLimit];
             } else {
                 //Logging NfcTag that somehow (!) is not binded to Student
                 $this->_logger->warning("SyncDataHandler: VM `" . $vendingMachine->getSerial() . "` posted unbinded NfcTag `code`: " . $nfcTags->get($purchase['code'])->getCode());
             }
         }
         // 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];
 }
Example #2
0
 /**
  * Add purchase
  *
  * @param \AppBundle\Entity\Purchase\Purchase $purchase
  * @return NfcTag
  */
 public function addPurchase(\AppBundle\Entity\Purchase\Purchase $purchase)
 {
     $purchase->setNfcTag($this);
     $this->purchases[] = $purchase;
     return $this;
 }
 /**
  * Add purchase
  *
  * @param \AppBundle\Entity\Purchase\Purchase $purchase
  * @return VendingMachine
  */
 public function addPurchase(\AppBundle\Entity\Purchase\Purchase $purchase)
 {
     $purchase->setVendingMachine($this);
     $this->purchases[] = $purchase;
     return $this;
 }