public function reportAction()
 {
     if ($this->getRequest()->isPost()) {
         $sSessionId = $this->params()->fromPost('session_id');
         $oOrderPayment = $this->getOrderPaymentTable()->getRow(array('session_id' => $sSessionId));
         $sLocationKey = $oOrderPayment->location_key;
         $oPayU = new \Shop\PayU($sLocationKey);
         if ($oPayU->getResponse((array) $this->getRequest()->getPost())) {
             $oPaymentStatusData = new \ArrayObject($oPayU->getPaymentStatus());
             $oOrderPayment = $this->getOrderPaymentTable()->getRow(array('session_id' => $oPaymentStatusData->offsetGet('session_id')));
             if ($oOrderPayment) {
                 try {
                     $this->getOrderPaymentTable()->beginTransaction();
                     $oOrderPaymentEntity = new OrderPaymentEntity();
                     $oResponseEntity = new ResponseEntity();
                     $oUserEntity = new UserEntity();
                     $oUserStd = new \stdClass();
                     $oMail = new \Application\Mail($this->getServiceLocator());
                     $aBorrowerFine = array();
                     $aExpirationItem = array();
                     $nUserId = $oOrderPayment->user_id;
                     if ($nUserId) {
                         $oUserParam = $this->getUserParamTable()->getRow(array('user_id' => $nUserId));
                         foreach ($oUserParam as $sKey => $mValue) {
                             if (in_array($sKey, array('user_id'))) {
                                 continue;
                             }
                             if (in_array($sKey, array('id'))) {
                                 $sKey = 'user_param_id';
                             }
                             $oUserStd->{$sKey} = $mValue;
                         }
                         foreach ($oOrderPayment as $sKey => $mValue) {
                             if (in_array($sKey, array('user_id', 'first_name', 'last_name', 'e-mail address'))) {
                                 continue;
                             }
                             if (in_array($sKey, array('id'))) {
                                 $sKey = 'order_payment_id';
                             }
                             if (in_array($sKey, array('amount'))) {
                                 $sKey = 'user_amount';
                                 $mValue = (int) $mValue / 100;
                             }
                             $oUserStd->{$sKey} = $mValue;
                         }
                         $nTime = time();
                         $oUserStd->issue_user_date = date('d.m.Y, H:i:s', $nTime);
                         $nPaymentStatus = $oPaymentStatusData->offsetGet('status');
                         $sPaymentStatus = $this->translatePaymentStatusAction($nPaymentStatus);
                         $oOrderPaymentData = new \ArrayObject(array('id' => $oOrderPayment->id, 'payment_status' => $nPaymentStatus, 'user_payment_status' => $sPaymentStatus, 'last_update_payment_status_date' => $nTime));
                         $this->getOrderPaymentTable()->editRow($oOrderPaymentEntity->setOptions($oOrderPaymentData, array('id', 'payment_status', 'user_payment_status', 'last_update_payment_status_date')));
                         if ($nPaymentStatus == '1') {
                             //nowa
                             $oMail->fillMessage('sendPayUReport1', $oUserStd);
                         } else {
                             if ($nPaymentStatus == '2') {
                                 //anulowana
                                 $oMail->fillMessage('sendPayUReport2', $oUserStd);
                             } else {
                                 if ($nPaymentStatus == '3') {
                                     //odrzucona
                                     $oMail->fillMessage('sendPayUReport3', $oUserStd);
                                 } else {
                                     if ($nPaymentStatus == '4') {
                                         //rozpoczęta
                                         $oMail->fillMessage('sendPayUReport4', $oUserStd);
                                     } else {
                                         if ($nPaymentStatus == '5') {
                                             //oczekuje na odbiór
                                             $oMail->fillMessage('sendPayUReport5', $oUserStd);
                                         } else {
                                             if ($nPaymentStatus == '7') {
                                                 //odrzucona
                                                 $oMail->fillMessage('sendPayUReport7', $oUserStd);
                                             } else {
                                                 if ($nPaymentStatus == '99') {
                                                     //zakończona
                                                     if (!$this->getOrderPaymentTable()->getRow(array('id' => $oOrderPayment->id))->end) {
                                                         $oOrderPaymentHistory = $this->getOrderPaymentHistoryTable()->getAll(array('filter' => array('order_payment_id' => $oOrderPayment->id)));
                                                         if ($oOrderPaymentHistory) {
                                                             foreach ($oOrderPaymentHistory as $aValue) {
                                                                 $nItemId = $aValue->item_id;
                                                                 $oItem = $this->getItemTable()->getRow(array('id' => $nItemId));
                                                                 if ($oItem->name === 'expiration_item') {
                                                                     $aExpirationItem[] = array('fine_id' => $oItem->fine_id, 'fine_sequence' => $oItem->fine_sequence, 'doc_number' => $oItem->doc_number, 'item_sequence' => $oItem->item_sequence, 'price_net' => $oItem->price_net / 100, 'price_gross' => $oItem->price_gross / 100, 'location_key' => $oItem->item_location_key);
                                                                 } else {
                                                                     if ($oItem->name === 'borrower_fine') {
                                                                         $aBorrowerFine[] = array('fine_id' => $oItem->fine_id, 'fine_sequence' => $oItem->fine_sequence, 'doc_number' => $oItem->doc_number, 'item_sequence' => $oItem->item_sequence, 'price_net' => $oItem->price_net / 100, 'price_gross' => $oItem->price_gross / 100, 'location_key' => $oItem->item_location_key);
                                                                     }
                                                                 }
                                                             }
                                                         }
                                                         $sBorrowerHash = $oUserParam->borrower_hash;
                                                         if ($sBorrowerHash) {
                                                             //$oPushBullet = new \Shop\PushBullet();
                                                             //$oPushBullet->pushNote('*****@*****.**', 'Kara w PW przed usunięciem', 'Kara w PW przed usunięciem');
                                                             $oXServer = new XServer();
                                                             $oXServer->deleteExpirationItem($sBorrowerHash, $aExpirationItem);
                                                             $oXServer->deleteBorrowerFine($sBorrowerHash, $aBorrowerFine);
                                                             $oOrderPaymentData = new \ArrayObject(array('id' => $oOrderPayment->id, 'date_end' => time(), 'end' => 1));
                                                             $oUserData = new \ArrayObject(array('id' => $nUserId, 'active' => 1));
                                                             $this->getOrderPaymentTable()->editRow($oOrderPaymentEntity->setOptions($oOrderPaymentData, array('id', 'date_end', 'end')));
                                                             $this->getUserTable()->changeActive($oUserEntity->setOptions($oUserData));
                                                             $oMail->fillMessage('sendPayUReport99', $oUserStd);
                                                             //$oPushBullet->pushNote('*****@*****.**', 'Kara w PW została usunięta', 'Kara w PW została usunięta');
                                                             //$oPushBullet->pushNote('*****@*****.**', 'Kara w WAT została usunięta', 'Kara w WAT została usunięta');
                                                         }
                                                     }
                                                 }
                                             }
                                         }
                                     }
                                 }
                             }
                         }
                         $oPaymentStatusData->offsetSet('order_payment_id', $oOrderPayment->id);
                         $oPaymentStatusData->offsetSet('date', time());
                         $this->getResponseTable()->addRow($oResponseEntity->setOptions($oPaymentStatusData));
                         $this->getOrderPaymentTable()->commit();
                     }
                 } catch (\Exception $e) {
                     //$oPushBullet = new \Shop\PushBullet();
                     //$oPushBullet->pushNote('*****@*****.**', 'Kara w WAT - wystąpił błąd', 'Kara w WAT - wystąpił błąd' . $e->getLine());
                     $this->getOrderPaymentTable()->rollBack();
                 }
             }
         }
     }
     echo 'OK';
     exit;
 }
 public function itemsAction()
 {
     $bAutoTrigger = null;
     $oUserSessionContainer = new UserSessionContainer();
     $nUserId = $oUserSessionContainer->getUserId();
     $nUserParamId = $this->getUserParamTable()->findRow(array('user_id' => $nUserId));
     if (isset($nUserId) && isset($nUserParamId)) {
         $oCart = $this->getCartTable()->getRow(array('user_id' => $nUserId));
         if (isset($oCart) && $oUserSessionContainer->getIsInit()) {
             $oCartSum = $this->getCartItemTable()->getCartSum();
             $oEditCartItemForm = $this->getServiceLocator()->get('EditCartItemFormService');
             $oNewPaymentForm = $this->getServiceLocator()->get('NewPaymentFormService');
             //$oNewPaymentForm->setData(array('amount' => $oCartSum->credit - $oCartSum->cart_sum_price_gross . ' PLN', 'first_name' => $oUserSessionContainer->getFirstName(), 'last_name' => $oUserSessionContainer->getLastName(), 'email_address' => $oUserSessionContainer->getEmailAddress(), 'phone_number' => $oUserSessionContainer->getPhoneNumber()));
             if ($oCartSum->credit > 0) {
                 $nAmount = $oCartSum->credit - $oCartSum->cart_sum_price_gross;
             } else {
                 $nAmount = $oCartSum->cart_sum_price_gross;
             }
             $oNewPaymentForm->setData(array('amount' => $nAmount . ' PLN', 'first_name' => $oUserSessionContainer->getFirstName(), 'last_name' => $oUserSessionContainer->getLastName(), 'email_address' => $oUserSessionContainer->getEmailAddress(), 'phone_number' => $oUserSessionContainer->getPhoneNumber(), 'location_key' => 'Alma Demo'));
             if ($this->getRequest()->isPost()) {
                 $oNewPaymentForm->setData($this->getRequest()->getPost());
                 if ($oNewPaymentForm->isValid()) {
                     try {
                         $this->getOrderPaymentTable()->beginTransaction();
                         $oOrderPaymentEntity = new OrderPaymentEntity();
                         $oOrderPaymentHistoryEntity = new OrderPaymentHistoryEntity();
                         $oUserEntity = new UserEntity();
                         $oUserParamEntity = new UserParamEntity();
                         $aValidatedData = $oNewPaymentForm->getData();
                         $sEmailAddress = $aValidatedData['email_address'];
                         $sFirstName = $aValidatedData['first_name'];
                         $sLastName = $aValidatedData['last_name'];
                         $sPhoneNumber = $aValidatedData['phone_number'];
                         $sPaymentDescription = '';
                         $oAllCartItems = $this->getCartItemTable()->getAll(array('cart_id' => $oCart->id));
                         if ($oAllCartItems) {
                             foreach ($oAllCartItems as $oItem) {
                                 $sPaymentDescription .= $this->getItemTable()->getRow(array('id' => $oItem->item_id))->name;
                                 if (count($oAllCartItems) > 1) {
                                     $sPaymentDescription .= '; ';
                                 }
                             }
                             $sPaymentDescription = trim($sPaymentDescription);
                         }
                         $aUserData = array('id' => $nUserId, 'user_param_id' => $nUserParamId, 'email_address' => $sEmailAddress, 'first_name' => $sFirstName, 'last_name' => $sLastName, 'phone_number' => $sPhoneNumber);
                         $oUserSessionContainer->setUserData($aUserData);
                         $oUserData = new \ArrayObject($aUserData);
                         $this->getUserTable()->changeEmailAddress($oUserEntity->setOptions($oUserData));
                         $this->getUserParamTable()->changeFirstName($oUserParamEntity->setOptions($oUserData));
                         $this->getUserParamTable()->changeLastName($oUserParamEntity->setOptions($oUserData));
                         $this->getUserParamTable()->changePhoneNumber($oUserParamEntity->setOptions($oUserData));
                         $oPayU = new \Shop\PayU($oUserSessionContainer->getLocationKey());
                         $oPayU->setSessionId();
                         $oPayU->setOrderId();
                         $oPayU->setAmount($nAmount);
                         $oPayU->setUserId($nUserId);
                         $oPayU->setFirstName($sFirstName);
                         $oPayU->setLastName($sLastName);
                         $oPayU->setEmailAddress($sEmailAddress);
                         $oPayU->setStreet('');
                         $oPayU->setPostCode('');
                         $oPayU->setCity('');
                         $oPayU->setPhone('');
                         $oPayU->setClientIp();
                         $oPayU->setDesc('Opłata biblioteczna ' . $oUserSessionContainer->getLocationKey());
                         $oPayU->setLocationKey($oUserSessionContainer->getLocationKey());
                         $oPayU->setSig();
                         $oPayUForm = $this->getServiceLocator()->get('PayUFormService');
                         $oPaymentForm = $oPayUForm->setPayment($oPayU);
                         $oOrderPaymentData = new \ArrayObject($oPayU->getPaymentParam());
                         $oOrderPaymentData->offsetSet('order_payment_id', $this->getOrderPaymentTable()->addRow($oOrderPaymentEntity->setOptions($oOrderPaymentData)));
                         $oAllCartItems = $this->getCartItemTable()->getAll(array('cart_id' => $oCart->id));
                         if ($oAllCartItems) {
                             foreach ($oAllCartItems as $oItem) {
                                 $oOrderPaymentData->offsetSet('item_id', $oItem->item_id);
                                 $this->getOrderPaymentHistoryTable()->addRow($oOrderPaymentHistoryEntity->setOptions($oOrderPaymentData));
                             }
                         }
                         $bAutoTrigger = true;
                         $this->getOrderPaymentTable()->commit();
                     } catch (\Exception $e) {
                         $this->getOrderPaymentTable()->rollBack();
                     }
                 }
             } else {
                 if ($oUserSessionContainer->getIsInit()) {
                     $oPaymentForm = $oNewPaymentForm;
                 }
             }
         }
         return new ViewModel(array('cart_sum' => $oCartSum, 'edit_cart_item_form' => $oEditCartItemForm, 'payment_form' => $oPaymentForm, 'auto_trigger' => $bAutoTrigger));
     }
 }