public function registerAction()
 {
     $oRegisterForm = $this->getServiceLocator()->get('RegisterFormService');
     if ($this->getRequest()->isPost()) {
         $oRegisterForm->setData($this->getRequest()->getPost());
         if ($oRegisterForm->isValid()) {
             $this->getUserTable()->beginTransaction();
             $oUserEntity = new UserEntity();
             $oUserParamEntity = new UserParamEntity();
             $oData = new \ArrayObject($oRegisterForm->getData());
             $oData->offsetSet('user_id', $this->getUserTable()->addRow($oUserEntity->setOptions($oData)));
             if ($oData->offsetGet('user_id')) {
                 $oData->offsetSet('user_param_id', $this->getUserParamTable()->addRow($oUserParamEntity->setOptions($oData)));
             }
             if ($oData->offsetGet('user_id') && $oData->offsetGet('user_param_id')) {
                 $this->getUserTable()->commit();
             } else {
                 $this->getUserTable()->rollBack();
             }
         }
     }
     return new ViewModel(array('form' => $oRegisterForm));
 }
 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 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 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));
     }
 }