Example #1
0
 public function __invoke()
 {
     $this->_oServiceLocator = $this->getServiceLocator();
     $oUserSessionContainer = new UserSessionContainer();
     if ($oUserSessionContainer->getIsInit()) {
         $nUserId = $oUserSessionContainer->getUserId();
         if ($nUserId) {
             $oCart = $this->getCartTable()->getRow(array('user_id' => $nUserId));
             if ($oCart) {
                 $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);
                 }
                 return array('cart' => $this->getCartItemTable()->getCartSum(array('cart_id' => $oCart->id)), 'description' => $sPaymentDescription);
             }
         }
     }
     return null;
 }
 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));
     }
 }
 public function getCartSum(array $aData = null)
 {
     $oUserSessionContainer = new UserSessionContainer();
     if ($oUserSessionContainer->getIsInit()) {
         $aData['cart.user_id'] = $oUserSessionContainer->getUserId();
         $oThat = $this;
         $oResultSet = $this->select(function (Select $oSelect) use($aData, $oThat) {
             $oSelect->columns(array('cart_items_count' => new Expression('SUM(count)'), 'cart_sum_price_net' => new Expression('SUM(count * (price_net)) / 100'), 'cart_sum_price_gross' => new Expression('SUM(count * (price_gross) / 100)')));
             $oSelect->join('cart', 'cart.id = cart_item.cart_id', array('user_id'));
             $oSelect->join('item', 'item.id = cart_item.item_id', array(), $oSelect::JOIN_INNER);
             $oSelect->join('user_param', 'user_param.user_id = cart.user_id', array('credit'), $oSelect::JOIN_INNER);
             if ($aData) {
                 foreach ($aData as $sKey => $mValue) {
                     $oSelect->where(array($sKey => $mValue));
                 }
             }
             $oSelect->limit(1);
         });
         $aResult = $this->getEntities($oResultSet);
         if (count($aResult) === 1) {
             return $aResult[0];
         }
     }
     return null;
 }