From 0b5405c8711b7ba9079f5c4d958b8abdd5ebabb8 Mon Sep 17 00:00:00 2001 From: Praesidiarius Date: Sat, 7 Mar 2020 23:38:01 +0100 Subject: [PATCH] basic shop working --- src/Controller/ApiController.php | 306 ++++++++++++++++++++++++++++++- 1 file changed, 303 insertions(+), 3 deletions(-) diff --git a/src/Controller/ApiController.php b/src/Controller/ApiController.php index ab12469..d85ea88 100644 --- a/src/Controller/ApiController.php +++ b/src/Controller/ApiController.php @@ -20,10 +20,14 @@ use Application\Controller\CoreEntityController; use Application\Model\CoreEntityModel; use http\Exception\RuntimeException; +use OnePlace\Article\Model\ArticleTable; +use OnePlace\Article\Variant\Model\VariantTable; use OnePlace\Basket\Model\BasketTable; use Laminas\View\Model\ViewModel; use Laminas\Db\Adapter\AdapterInterface; use OnePlace\Basket\Position\Model\PositionTable; +use OnePlace\Contact\Address\Model\AddressTable; +use OnePlace\Contact\Model\ContactTable; class ApiController extends CoreEntityController { /** @@ -58,6 +62,7 @@ public function addAction() { $iItemID = $_REQUEST['shop_item_id']; $sItemType = $_REQUEST['shop_item_type']; + $fItemAmount = $_REQUEST['shop_item_amount']; $sShopSessionID = $_REQUEST['shop_session_id']; $oBasket = false; @@ -117,7 +122,8 @@ public function addAction() { $aPosData = [ 'basket_idfs' => $oBasket->getID(), 'article_idfs' => $iItemID, - 'amount' => 1, + 'article_type' => $sItemType, + 'amount' => (float)$fItemAmount, 'price' => 0, 'comment' => '', 'created_by' => 1, @@ -130,7 +136,7 @@ public function addAction() { $oBasketPosTbl->saveSingle($oPos); } - $aResponse = ['state'=>'success','message'=>'item added to basket','oBasket'=>$oBasket]; + $aResponse = ['state'=>'success','message'=> $fItemAmount.' items added to basket','oBasket'=>$oBasket]; echo json_encode($aResponse); return false; @@ -152,7 +158,301 @@ public function getAction() { return false; } - $aResponse = ['state'=>'success','message'=>'open basket found','oBasket'=>$oBasketExists]; + $aPositions = []; + $oPosTbl = CoreEntityController::$oServiceManager->get(PositionTable::class); + $oArticleTbl = CoreEntityController::$oServiceManager->get(ArticleTable::class); + $oVariantTbl = CoreEntityController::$oServiceManager->get(VariantTable::class); + + $oBasketPositions = $oPosTbl->fetchAll(false,['basket_idfs' => $oBasketExists->getID()]); + if(count($oBasketPositions) > 0) { + foreach($oBasketPositions as $oPos) { + switch($oPos->article_type) { + case 'variant': + $oPos->oVariant = $oVariantTbl->getSingle($oPos->article_idfs); + $oPos->oArticle = $oArticleTbl->getSingle($oPos->oVariant->article_idfs); + break; + default: + break; + } + $aPositions[] = $oPos; + } + } + $aResponse = ['state'=>'success','message'=>'open basket found','basket'=>$oBasketExists,'items'=>$aPositions]; + echo json_encode($aResponse); + + return false; + } + + public function checkoutAction() { + $this->layout('layout/json'); + + $sShopSessionID = $_REQUEST['shop_session_id']; + + $oTagDelMet = CoreEntityController::$aCoreTables['core-tag']->select(['tag_key' => 'deliverymethod']); + $aDeliveryMethods = []; + if(count($oTagDelMet) > 0) { + $oTagDelMet = $oTagDelMet->current(); + + $oDeliveryMethodsDB = CoreEntityController::$aCoreTables['core-entity-tag']->select([ + 'entity_form_idfs' => 'basket-single', + 'tag_idfs' => $oTagDelMet->Tag_ID, + ]); + if(count($oDeliveryMethodsDB) > 0) { + foreach($oDeliveryMethodsDB as $oDel) { + $aDeliveryMethods[] = (object)['id' => $oDel->Entitytag_ID,'label' => $oDel->tag_value]; + } + } + } + + try { + $oBasketExists = $this->oTableGateway->getSingle($sShopSessionID,'shop_session_id'); + if($oBasketExists->shop_session_id != $sShopSessionID) { + throw new \RuntimeException('Not really the same basket...'); + } + } catch(\RuntimeException $e) { + $aResponse = ['state'=>'error','message'=>'No matching open basket found']; + echo json_encode($aResponse); + return false; + } + + if($oBasketExists->contact_idfs != 0) { + $oContactTbl = CoreEntityController::$oServiceManager->get(ContactTable::class); + $oAddressTbl = CoreEntityController::$oServiceManager->get(AddressTable::class); + try { + $oContactExists = $oContactTbl->getSingle($oBasketExists->contact_idfs); + } catch (\RuntimeException $e) { + $aResponse = ['state' => 'success', 'message' => 'checkout started', 'basket' => $oBasketExists,'deliverymethods' => $aDeliveryMethods]; + echo json_encode($aResponse); + + return false; + } + + $oAddress = $oAddressTbl->getSingle($oContactExists->getID(),'contact_idfs'); + $oContactExists->address = $oAddress; + + $aResponse = ['state'=>'success','message'=>'checkout started again','basket'=>$oBasketExists,'contact'=>$oContactExists,'deliverymethods' => $aDeliveryMethods]; + echo json_encode($aResponse); + + return false; + } else { + $aResponse = ['state' => 'success', 'message' => 'checkout started', 'basket' => $oBasketExists,'deliverymethods' => $aDeliveryMethods]; + echo json_encode($aResponse); + + return false; + } + } + + public function paymentAction() { + $this->layout('layout/json'); + + $sShopSessionID = $_REQUEST['shop_session_id']; + + try { + $oBasketExists = $this->oTableGateway->getSingle($sShopSessionID,'shop_session_id'); + if($oBasketExists->shop_session_id != $sShopSessionID) { + throw new \RuntimeException('Not really the same basket...'); + } + } catch(\RuntimeException $e) { + $aResponse = ['state'=>'error','message'=>'No matching open basket found']; + echo json_encode($aResponse); + return false; + } + + $oContactTbl = CoreEntityController::$oServiceManager->get(ContactTable::class); + $oAddressTbl = CoreEntityController::$oServiceManager->get(AddressTable::class); + + $aContactData = []; + if(isset($_REQUEST['email'])) { + $aContactData['email_private'] = $_REQUEST['email']; + $aContactData['firstname'] = $_REQUEST['firstname']; + $aContactData['lastname'] = $_REQUEST['lastname']; + $aContactData['phone_private'] = $_REQUEST['phone']; + $aContactData['salutation_idfs'] = $_REQUEST['salutation']; + + try { + $oContactExists = $oContactTbl->getSingle($aContactData['email_private'],'email_private'); + } catch(\RuntimeException $e) { + $oNewContact = $oContactTbl->generateNew(); + $oNewContact->exchangeArray($aContactData); + $iContactID = $oContactTbl->saveSingle($oNewContact); + $oContactExists = $oContactTbl->getSingle($iContactID); + + $aAddressData = [ + 'contact_idfs' => $iContactID, + 'street' => $_REQUEST['street'], + 'zip' => $_REQUEST['zip'], + 'city' => $_REQUEST['city'], + ]; + $oNewAddress = $oAddressTbl->generateNew(); + $oNewAddress->exchangeArray($aAddressData); + $iAddressID = $oAddressTbl->saveSingle($oNewAddress); + + $this->oTableGateway->updateAttribute('contact_idfs',$iContactID,'Basket_ID',$oBasketExists->getID()); + } + } else { + $oContactExists = $oContactTbl->getSingle($oBasketExists->contact_idfs); + } + + if(isset($_REQUEST['deliverymethod'])) { + $iDeliveryMethodID = $_REQUEST['deliverymethod']; + $this->oTableGateway->updateAttribute('deliverymethod_idfs', $iDeliveryMethodID, 'Basket_ID', $oBasketExists->getID()); + } + + $oTagDelMet = CoreEntityController::$aCoreTables['core-tag']->select(['tag_key' => 'paymentmethod']); + $aPaymentMethods = []; + if(count($oTagDelMet) > 0) { + $oTagDelMet = $oTagDelMet->current(); + + $oDeliveryMethodsDB = CoreEntityController::$aCoreTables['core-entity-tag']->select([ + 'entity_form_idfs' => 'basket-single', + 'tag_idfs' => $oTagDelMet->Tag_ID, + ]); + if(count($oDeliveryMethodsDB) > 0) { + foreach($oDeliveryMethodsDB as $oDel) { + $aPaymentMethods[] = (object)[ + 'id' => $oDel->Entitytag_ID, + 'label' => $oDel->tag_value, + 'icon' => $oDel->tag_icon, + ]; + } + } + } + + /** + * Load Payment Method + */ + $aPay = ['id' => 0,'label' => '-','icon' => '']; + $oPaymentMethod = CoreEntityController::$aCoreTables['core-entity-tag']->select([ + 'Entitytag_ID' => $oBasketExists->paymentmethod_idfs, + ]); + if(count($oPaymentMethod) > 0) { + $oPaymentMethod = $oPaymentMethod->current(); + $aPay = [ + 'id' => $oPaymentMethod->Entitytag_ID, + 'label' => $oPaymentMethod->tag_value, + 'icon' => $oPaymentMethod->tag_icon + ]; + } + + $aResponse = [ + 'state' => 'success', + 'message' => 'contact saved', + 'basket' => $oBasketExists, + 'contact' => $oContactExists, + 'paymentmethods' => $aPaymentMethods, + 'paymentmethodselected' => $aPay, + ]; + echo json_encode($aResponse); + + return false; + } + + public function confirmAction() { + $this->layout('layout/json'); + + $sShopSessionID = $_REQUEST['shop_session_id']; + + /** + * Load Basket + */ + try { + $oBasketExists = $this->oTableGateway->getSingle($sShopSessionID,'shop_session_id'); + if($oBasketExists->shop_session_id != $sShopSessionID) { + throw new \RuntimeException('Not really the same basket...'); + } + } catch(\RuntimeException $e) { + $aResponse = ['state' => 'error','message' => 'No matching open basket found']; + echo json_encode($aResponse); + return false; + } + + /** + * Load Contact + */ + $oContactExists = []; + if($oBasketExists->contact_idfs != 0) { + $oContactTbl = CoreEntityController::$oServiceManager->get(ContactTable::class); + $oAddressTbl = CoreEntityController::$oServiceManager->get(AddressTable::class); + try { + $oContactExists = $oContactTbl->getSingle($oBasketExists->contact_idfs); + } catch (\RuntimeException $e) { + $aResponse = ['state' => 'success', 'message' => 'checkout started', 'basket' => $oBasketExists,'deliverymethods' => $aDeliveryMethods]; + echo json_encode($aResponse); + + return false; + } + + $oAddress = $oAddressTbl->getSingle($oContactExists->getID(),'contact_idfs'); + $oContactExists->address = $oAddress; + } + + /** + * Update Payment Method + */ + $iPaymentMethodID = $_REQUEST['paymentmethod']; + $this->oTableGateway->updateAttribute('paymentmethod_idfs',$iPaymentMethodID,'Basket_ID',$oBasketExists->getID()); + + /** + * Load Payment Method + */ + $aPay = ['id' => 0,'label' => '-','icon' => '']; + $oPaymentMethod = CoreEntityController::$aCoreTables['core-entity-tag']->select([ + 'Entitytag_ID' => $oBasketExists->paymentmethod_idfs, + ]); + if(count($oPaymentMethod) > 0) { + $oPaymentMethod = $oPaymentMethod->current(); + $aPay = [ + 'id' => $oPaymentMethod->Entitytag_ID, + 'label' => $oPaymentMethod->tag_value, + 'icon' => $oPaymentMethod->tag_icon + ]; + } + + /** + * Load Delivery Method + */ + $aDelivery = ['id' => 0,'label' => '-','icon' => '']; + $oDeliveryMethod = CoreEntityController::$aCoreTables['core-entity-tag']->select([ + 'Entitytag_ID' => $oBasketExists->deliverymethod_idfs, + ]); + if(count($oDeliveryMethod) > 0) { + $oDeliveryMethod = $oDeliveryMethod->current(); + $aDelivery = [ + 'id' => $oDeliveryMethod->Entitytag_ID, + 'label' => $oDeliveryMethod->tag_value, + 'icon' => $oDeliveryMethod->tag_icon + ]; + } + + $aPositions = []; + $oPosTbl = CoreEntityController::$oServiceManager->get(PositionTable::class); + $oArticleTbl = CoreEntityController::$oServiceManager->get(ArticleTable::class); + $oVariantTbl = CoreEntityController::$oServiceManager->get(VariantTable::class); + + $oBasketPositions = $oPosTbl->fetchAll(false,['basket_idfs' => $oBasketExists->getID()]); + if(count($oBasketPositions) > 0) { + foreach($oBasketPositions as $oPos) { + switch($oPos->article_type) { + case 'variant': + $oPos->oVariant = $oVariantTbl->getSingle($oPos->article_idfs); + $oPos->oArticle = $oArticleTbl->getSingle($oPos->oVariant->article_idfs); + break; + default: + break; + } + $aPositions[] = $oPos; + } + } + + $aResponse = [ + 'state' => 'success', + 'message' => 'paymentmethod saved', + 'basket' => $oBasketExists, + 'paymentmethod' => $aPay, + 'deliverymethod' => $aDelivery, + 'contact' => $oContactExists, + 'positions' => $aPositions, + ]; echo json_encode($aResponse); return false;