Пример #1
0
 /**
  * Generates certificate of exellence and
  * triggers an event when the file is generated
  * @param \User\Model\Entity\User $user
  * @param string $examName
  */
 public function generateCertificate($user, $examName)
 {
     $config = $this->services->get('config');
     $pdf = PdfDocument::load($config['pdf']['exam_certificate']);
     // get the first page
     $page = $pdf->pages[0];
     // Extract the AdineKirnberg-Script font included in the PDF sample
     $font = $page->extractFont('AdineKirnberg-Script');
     $page->setFont($font, 80);
     // and write the name of the user with it
     $page->drawText($user->getName(), 200, 280);
     // after that use Time Bold to write the name of the exam
     $font = PdfFont::fontWithName(PdfFont::FONT_TIMES_BOLD);
     $page->setFont($font, 40);
     $page->drawText($examName, 200, 120);
     // We use the png image from the public/images folder
     $imageFile = 'public/images/zf2-logo.png';
     // get the right size to do some calculations
     $size = getimagesize($imageFile);
     // load the image
     $image = \ZendPdf\Image::imageWithPath($imageFile);
     $x = 580;
     $y = 440;
     // and finally draw the image
     $page->drawImage($image, $x, $y, $x + $size[0], $y + $size[1]);
     return $pdf;
 }
Пример #2
0
    /**
     * Sends award certificate
     *
     * @param \User\Model\Entity\User $user
     * @param array $exam
     * @param \ZendPdf\Document $pdf
     */
    public function sendCertificate($user, $exam, $pdf)
    {
        $translator = $this->services->get('translator');
        $mail = new Message();
        $mail->addTo($user->getEmail(), $user->getName());
        $text = 'You are genius!
You answered all the questions correctly.
Therefore we are sending you as a gratitude this free award certificate.

';
        // we create a new mime message
        $mimeMessage = new MimeMessage();
        // create the original body as part
        $textPart = new MimePart($text);
        $textPart->type = "text/plain";
        // add the pdf document as a second part
        $pdfPart = new MimePart($pdf->render());
        $pdfPart->type = 'application/pdf';
        $mimeMessage->setParts(array($textPart, $pdfPart));
        $mail->setBody($mimeMessage);
        $mail->setFrom('*****@*****.**', 'Slavey Karadzhov');
        $mail->setSubject($translator->translate('Congratulations: Here is your award certificate'));
        $transport = $this->services->get('mail-transport');
        $transport->send($mail);
    }
Пример #3
0
 /**
  * Sends email message to user.
  *
  * @return bool True on success, false otherwise
  * @throws \BadMethodCallException When "name" or "email" properties are missing
  *  for the provided User entity
  */
 public function send()
 {
     if (!$this->_user->has('email') || !$this->_user->has('name')) {
         throw new BadMethodCallException(__d('user', 'Missing "name" or "email" property when trying to send the email.'));
     }
     if ($this->config('updateToken') === true) {
         $this->loadModel('User.Users');
         $this->_user->updateToken();
     }
     $subject = $this->_parseVariables((string) $this->subject());
     $body = $this->_parseVariables((string) $this->body());
     if (empty($subject) || empty($body)) {
         return false;
     }
     $sender = new Email($this->config('emailConfig'));
     $sent = false;
     try {
         $sent = $sender->to($this->_user->get('email'), $this->_user->get('name'))->subject($subject)->send($body);
     } catch (\Exception $e) {
         return false;
     }
     return $sent;
 }
 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;
 }
Пример #5
0
 /**
  * Generates a unique token for the given user entity. The generated token is
  * automatically persisted on DB.
  *
  * Tokens are unique within the entire DB and follows the pattern below:
  *
  *     <32-random-letters-and-numbers>
  *
  * @param \User\Model\Entity\User $user The user for which generate the token
  * @return \User\Model\Entity\User The user entity with a the new token property
  * @throws \Cake\Error\FatalErrorException When an invalid user entity was given
  */
 public function updateToken(User $user)
 {
     if (!$user->has('id')) {
         throw new FatalErrorException(__d('user', 'UsersTable::updateToken(), no ID was found for the given entity.'));
     }
     $token = md5(uniqid($user->id, true));
     $count = $this->find()->where(['Users.token' => $token])->limit(1)->count();
     while ($count > 0) {
         $token = str_shuffle(md5(uniqid($user->id, true) . rand(1, 9999)));
         $count = $this->find()->where(['Users.token' => $token])->limit(1)->count();
     }
     $user->set('token', $token);
     $user->set('token_expiration', time() + USER_TOKEN_EXPIRATION);
     $this->updateAll(['token' => $user->get('token'), 'token_expiration' => $user->get('token_expiration')], ['id' => $user->id]);
     return $user;
 }
 public function secendoptionuserajaxAction()
 {
     $oResponse = $this->getResponse();
     $bSuccess = false;
     if ($this->getRequest()->isPost()) {
         $sSecendOption = $this->params()->fromPost('second_opinion');
         $sColumnName = $this->params()->fromPost('column_name');
         $nId = $this->params()->fromPost('id') !== '' ? (int) $this->params()->fromPost('id') : null;
         if ($nId) {
             $oUserEntity = new UserEntity();
             switch ($sSecendOption) {
                 case 'enable_disable':
                     $oUser = $this->getUserTable()->getRow(array('id' => $nId));
                     if (isset($oUser->{$sColumnName})) {
                         if ($oUser->{$sColumnName} === 1) {
                             $oData = new \ArrayObject(array('id' => $nId, $sColumnName => 0));
                             $this->getUserTable()->changeActive($oUserEntity->setOptions($oData));
                             $bSuccess = true;
                         } else {
                             if ($oUser->{$sColumnName} === 0) {
                                 $oData = new \ArrayObject(array('id' => $nId, $sColumnName => 1));
                                 $this->getUserTable()->changeActive($oUserEntity->setOptions($oData));
                                 $bSuccess = true;
                             }
                         }
                     }
                     break;
             }
         }
     }
     $oResponse->setContent(\Zend\Json\Json::encode($bSuccess));
     return $oResponse;
 }
 public function getfineAction()
 {
     $oUserSessionContainer = new UserSessionContainer();
     $oBorrowerForm = $this->getServiceLocator()->get('BorrowerFormService');
     $aExpirationItem = null;
     $aBorrowerFine = null;
     $bSuccess = false;
     $sIsBorrower = $oUserSessionContainer->getIsBorrower();
     $sBorrowerId = $oUserSessionContainer->getBorrowerId();
     if ($sIsBorrower === 1 && $sBorrowerId) {
         if ($this->getRequest()->isPost()) {
             $oBorrowerForm->setData($this->getRequest()->getPost());
             if ($oBorrowerForm->isValid()) {
                 try {
                     $this->getUserTable()->beginTransaction();
                     $oUserEntity = new UserEntity();
                     $oUserParamEntity = new UserParamEntity();
                     $oCartEntity = new CartEntity();
                     $oCartItemEntity = new CartItemEntity();
                     $oItemEntity = new ItemEntity();
                     $aValidatedData = $oBorrowerForm->getData();
                     \Zend\Debug\Debug::dump($oUserSessionContainer->getPreIsInit() === $aValidatedData['key']);
                     if ($oUserSessionContainer->getPreIsInit() === $aValidatedData['key']) {
                         $oTempUserData = new \ArrayObject($oUserSessionContainer->getArrayCopy());
                         $oTempUserData->offsetSet('user_category_id', $this->getUserCategoryTable()->findRow(array('name' => 'borrower')));
                         $oTempUserData->offsetSet('active', 0);
                         $oTempUserData->offsetSet('user_id', $this->getUserTable()->addRow($oUserEntity->setOptions($oTempUserData)));
                         if ($oTempUserData->offsetGet('user_id')) {
                             $oTempUserData->offsetSet('user_param_id', $this->getUserParamTable()->addRow($oUserParamEntity->setOptions($oTempUserData)));
                         }
                         if ($oTempUserData->offsetGet('user_id') && $oTempUserData->offsetGet('user_param_id')) {
                             $oUserSessionContainer->setUserId($oTempUserData->offsetGet('user_id'));
                         } else {
                             throw new \Exception();
                         }
                         $nUserId = $oUserSessionContainer->getUserId();
                         $nCartId = $this->getCartTable()->findRow(array('user_id' => $nUserId));
                         if (!$nCartId) {
                             $nCartId = $this->getCartTable()->addRow($oCartEntity->setOptions(new \ArrayObject(array('user_id' => $nUserId))));
                         }
                         if ($oUserSessionContainer->getExpirationItemSum()) {
                             $sName = 'expiration_item';
                             foreach ($oUserSessionContainer->getExpirationItem() as $nKey => $aItem) {
                                 if (is_numeric($nKey)) {
                                     $oItemData = new \ArrayObject(array('name' => $sName, 'fine_id' => $aItem['loan_id'], 'fine_sequence' => '-- brak danych --', 'item_sequence' => '-- brak danych --', 'doc_number' => '-- brak danych --', 'call_no' => '-- brak danych --', 'item_barcode' => $aItem['barcode'], 'title' => $aItem['title'], 'price_net' => $aItem['loan_fine'], 'price_gross' => $aItem['loan_fine'], 'due_date' => $aItem['due_date']));
                                     $nItemId = $this->getItemTable()->addRow($oItemEntity->setOptions($oItemData));
                                     $oCartItemData = new \ArrayObject(array('cart_id' => $nCartId, 'item_id' => $nItemId, 'user_id' => $nUserId));
                                     $this->getCartItemTable()->addRow($oCartItemEntity->setOptions($oCartItemData));
                                 }
                             }
                         }
                         if ($oUserSessionContainer->getBorrowerFineSum()) {
                             $sName = 'borrower_fine';
                             foreach ($oUserSessionContainer->getBorrowerFine() as $nKey => $aFine) {
                                 if (is_numeric($nKey)) {
                                     $oItemData = new \ArrayObject(array('name' => $sName, 'fine_id' => $aFine['fee_id'], 'fine_sequence' => '-- brak danych --', 'item_sequence' => '-- brak danych --', 'doc_number' => '-- brak danych --', 'call_no' => '-- brak danych --', 'item_barcode' => $aFine['barcode'], 'title' => $aFine['comment'], 'price_net' => $aFine['balance'], 'price_gross' => $aFine['balance'], 'due_date' => null));
                                     $nItemId = $this->getItemTable()->addRow($oItemEntity->setOptions($oItemData));
                                     $oCartItemData = new \ArrayObject(array('cart_id' => $nCartId, 'item_id' => $nItemId, 'user_id' => $nUserId));
                                     $this->getCartItemTable()->addRow($oCartItemEntity->setOptions($oCartItemData));
                                 }
                             }
                         }
                         $this->getUserTable()->commit();
                         $this->redirect()->toRoute('shop_cart/shop_cart_process', array('action' => 'items'), array());
                     }
                 } catch (\Exception $e) {
                     $this->getUserTable()->rollBack();
                     \Zend\Debug\Debug::dump($e->getMessage());
                     exit;
                 }
             }
         } else {
             $oXServer = new XServer($sBorrowerId);
             $aBorrowerData = $oXServer->getBorrowerData();
             if (!isset($aBorrowerData['error'])) {
                 $aBorrower['key'] = $oUserSessionContainer->getPreIsInit();
                 $aBorrower['email_address'] = $oUserSessionContainer->getEmailAddress();
                 $aBorrower['is_borrower'] = 1;
                 $oBorrowerForm->setData($aBorrower);
                 $aExpirationItem = $oUserSessionContainer->getExpirationItem();
                 $aBorrowerFine = $oUserSessionContainer->getBorrowerFine();
                 $nCredit = $oUserSessionContainer->getCredit();
                 $bSuccess = true;
             } else {
                 $sErrorMessage = 'getfine - XServer response: ' . $aBorrowerData['error'];
             }
         }
     } else {
         $sErrorMessage = 'getfine - podaj identyfikator czytelnika';
         $oUserSessionContainer->clear();
     }
     return new ViewModel(array('form' => $oBorrowerForm, 'expiration_item' => $aExpirationItem, 'borrower_fine' => $aBorrowerFine, 'credit' => $nCredit, 'success' => $bSuccess, 'error_message' => $sErrorMessage));
 }
 public function newuserAction()
 {
     $oNewBorrowerForm = $this->getServiceLocator()->get('NewBorrowerFormService');
     $oUserSessionContainer = new UserSessionContainer();
     $nUserId = $oUserSessionContainer->getUserId();
     if ($nUserId) {
         $nCartId = $this->getCartTable()->findRow(array('user_id' => $nUserId));
         if ($nCartId) {
             $this->getCartItemTable()->deleteRow(array('cart_id' => $nCartId));
         }
         $oUserSessionContainer->clear();
     }
     $bSuccess = false;
     if ($this->getRequest()->isPost()) {
         $oNewBorrowerForm->setData($this->getRequest()->getPost());
         if ($oNewBorrowerForm->isValid()) {
             try {
                 $this->getUserTable()->beginTransaction();
                 $oUserEntity = new UserEntity();
                 $oUserParamEntity = new UserParamEntity();
                 $oCartEntity = new CartEntity();
                 $oCartItemEntity = new CartItemEntity();
                 $aValidatedData = $oNewBorrowerForm->getData();
                 if ($oUserSessionContainer->getPreIsInit() === $aValidatedData['key'] && ((int) $aValidatedData['is_student'] === 1 || (int) $aValidatedData['is_employee'] === 1)) {
                     $oUserSessionContainer->setFirstName($aValidatedData['first_name']);
                     $oUserSessionContainer->setLastName($aValidatedData['last_name']);
                     $oUserSessionContainer->setPostCode($aValidatedData['post_code']);
                     $oUserSessionContainer->setEmailAddress($aValidatedData['email_address']);
                     $oUserSessionContainer->setPhoneNumber($aValidatedData['phone_number']);
                     $oUserSessionContainer->setStreet($aValidatedData['street']);
                     $oUserSessionContainer->setCity($aValidatedData['city']);
                     $oUserSessionContainer->setPostCode($aValidatedData['post_code']);
                     $oUserSessionContainer->setEmailAddress($aValidatedData['email_address']);
                     $oUserSessionContainer->setPhoneNumber($aValidatedData['phone_number']);
                     $oUserSessionContainer->setBbarcode($aValidatedData['email_address']);
                     $sTempBtype = $this->getBtypeTable()->getRow(array('id' => $aValidatedData['btype']))->btype;
                     if ($sTempBtype) {
                         $aOrganizationBtype = $oUserSessionContainer->getOrganizationBtype();
                         $bIsRealBtype = false;
                         foreach ($aOrganizationBtype as $oValue) {
                             if ($oValue->btype == $sTempBtype) {
                                 $bIsRealBtype = true;
                             }
                         }
                         if ($bIsRealBtype) {
                             $oUserSessionContainer->setBtype($sTempBtype);
                         } else {
                             $oUserSessionContainer->setBtype('ZA');
                         }
                     } else {
                         $oUserSessionContainer->setBtype('ZA');
                     }
                 } else {
                     if ((int) $aValidatedData['is_borrower'] === 1) {
                         $oUserSessionContainer->setNewBorrowerData($aValidatedData);
                         $oUserSessionContainer->setBtype('ZA');
                         $oUserSessionContainer->setBorrowerNote('Osoba z zewnątrz');
                     }
                 }
                 if ($oUserSessionContainer->getIsInit()) {
                     $oTempUserData = new \ArrayObject($oUserSessionContainer->getArrayCopy());
                     $oTempUserData->offsetSet('user_category_id', $this->getUserCategoryTable()->findRow(array('name' => $oUserSessionContainer->getWho())));
                     $oTempUserData->offsetSet('active', 0);
                     $oTempUserData->offsetSet('user_id', $this->getUserTable()->addRow($oUserEntity->setOptions($oTempUserData)));
                     if ($oTempUserData->offsetGet('user_id')) {
                         $oTempUserData->offsetSet('user_param_id', $this->getUserParamTable()->addRow($oUserParamEntity->setOptions($oTempUserData)));
                     }
                     if ($oTempUserData->offsetGet('user_id') && $oTempUserData->offsetGet('user_param_id')) {
                         $oUserSessionContainer->setUserId($oTempUserData->offsetGet('user_id'));
                     } else {
                         throw new \Exception();
                     }
                     $nItemId = 2;
                     $oData = new \ArrayObject(array('item_id' => $nItemId, 'user_id' => $oUserSessionContainer->getUserId()));
                     $oData->offsetSet('cart_id', $this->getCartTable()->findRow(array('user_id' => $oUserSessionContainer->getUserId())));
                     if ($oData->offsetGet('cart_id')) {
                         $oData->offsetSet('cart_item_id', $this->getCartItemTable()->addRow($oCartItemEntity->setOptions($oData)));
                     } else {
                         $oData->offsetSet('cart_id', $this->getCartTable()->addRow($oCartEntity->setOptions($oData)));
                         $oData->offsetSet('cart_item_id', $this->getCartItemTable()->addRow($oCartItemEntity->setOptions($oData)));
                     }
                     if ($oData->offsetGet('cart_id') && $oData->offsetGet('cart_item_id')) {
                         $bSuccess = true;
                     } else {
                         throw new \Exception();
                     }
                 }
                 if ($bSuccess) {
                     $this->getUserTable()->commit();
                     $this->redirect()->toRoute('shop_cart/shop_cart_process', array('action' => 'items'), array());
                 }
             } catch (\Exception $e) {
                 $this->getUserTable()->rollBack();
             }
         }
     }
     return new ViewModel(array('form' => $oNewBorrowerForm));
 }
 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));
     }
 }