Skip to content

Commit

Permalink
basic shop working
Browse files Browse the repository at this point in the history
  • Loading branch information
Praesidiarius committed Mar 7, 2020
1 parent 83842a7 commit 0b5405c
Showing 1 changed file with 303 additions and 3 deletions.
306 changes: 303 additions & 3 deletions src/Controller/ApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
/**
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand All @@ -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;
Expand All @@ -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;
Expand Down

0 comments on commit 0b5405c

Please sign in to comment.