<?php
namespace App\EventListener;
use App\Model\DataObject\Address;
use Exception;
use Pimcore\Event\Model\ElementEventInterface;
use Pimcore\Event\Model\DataObjectEvent;
use Pimcore\File;
use Pimcore\Logger;
use Pimcore\Model\DataObject\Business;
use Pimcore\Model\DataObject\Coupon;
use Pimcore\Model\DataObject\News;
use Pimcore\Model\DataObject\Price;
use Pimcore\Model\DataObject\Product;
class ObjectListener
{
public function onPreDelete(ElementEventInterface $e)
{
if ($e instanceof DataObjectEvent) {
$obj = $e->getObject();
if($obj instanceof Product){
$this->deleteProduct($obj);
}else if($obj instanceof Price){
$this->deletePrice($obj);
}else if($obj instanceof Coupon){
$this->deleteCoupon($obj);
}
}
}
public function onPreUpdate(ElementEventInterface $e)
{
if ($e instanceof DataObjectEvent) {
$obj = $e->getObject();
if($obj instanceof Address) {
if(!$obj->getLocation()){
$obj->geocode();
}
}else if($obj instanceof News){
$languages = \Pimcore\Tool::getValidLanguages();
foreach($languages as $lang) {
$obj->setSlug(urlencode($obj->getTitle($lang)."-".$obj->getId()), $lang);
}
}else if($obj instanceof Business){
$languages = \Pimcore\Tool::getValidLanguages();
foreach($languages as $lang) {
if(!$obj->getSlug($lang)){
$obj->setSlug(File::getValidFilename($obj->getName($lang).'_'.$obj->getId()), $lang);
}
}
if(!$obj->getImage()){
if(!$obj->getPremium()){
$obj->setImage(\Pimcore\Model\WebsiteSetting::getByName('defaultImageNotPartner')->getData());
} else{
$obj->setImage(\Pimcore\Model\WebsiteSetting::getByName('defaultImage')->getData());
}
}
if(!$obj->getBanner()){
$obj->setBanner(\Pimcore\Model\WebsiteSetting::getByName('defaultBanner')->getData());
}
$this->businessRefUpdater($obj);
}else if($obj instanceof Product){
//$this->createProduct($obj);
}else if($obj instanceof Price){
//$this->createPrice($obj);
}else if($obj instanceof Coupon){
//$this->createCoupon($obj);
}
}
}
private function businessRefUpdater(Business $obj){
$ref = "";
$lang = $obj->getLocale();
switch($obj->getFrame()) {
case 'family':
if($lang == 'fr'){
$ref .= 'FAM';
break;
}elseif($lang == 'nl'){
$ref .= 'GE';
break;
}
case 'collective':
if($lang == 'fr'){
$ref .= 'COLL';
break;
}elseif($lang == 'nl'){
$ref .= 'GR';
break;
}
}
switch($obj->getPrice()){
case 'subsidized':
if($lang == 'fr'){
$ref .= 'SUB';
break;
}elseif($lang == 'nl'){
if($obj->getFrame() == 'family'){
$ref .= 'IK';
break;
} elseif($obj->getFrame() == 'collective'){
$ref .= 'IKG';
break;
}
}
case 'private':
if($lang == 'fr'){
$ref.= 'PRIV';
break;
}elseif($lang == 'nl'){
$ref.= 'VP';
break;
}
}
$obj->setCodeRef($ref);
$ref .= $obj->getId();
$obj->setRef($ref);
}
private function stripe(){
return new \Stripe\StripeClient(\Pimcore\Model\WebsiteSetting::getByName('stripeSecretKey')->getData());
}
private function createProduct(Product $product){
$stripe = $this->stripe();
if(!$product->getStripeId() && $product->isPublished()){
if(!$product->getStripeId()){
$res = $stripe->products->create([
'name' => $product->getName('fr'),
'description' => $product->getDescription(),
'tax_code' => 'txcd_10000000',
'active' => $product->isPublished()
]);
$product->setStripeId($res->id);
}
}
if($product->getStripeId()){
$stripe->products->update(
$product->getStripeId(),
[
'name' => $product->getName('fr'),
'description' => $product->getDescription(),
'active' => $product->isPublished()
]);
}
}
private function deleteProduct(Product $product){
if($product->getStripeId()) {
$stripe = $this->stripe();
try{
$stripe->products->update(
$product->getStripeId(),
[
'active' => false
]
);
}catch(Exception $e){
Logger::err('[product:'.$product->getId().'] unable to delete from stripe');
}
}
}
private function createPrice(Price $price){
$stripe = $this->stripe();
if(!$price->getStripeId() && $price->isPublished()){
$product = $price->getProduct();
$res = $stripe->prices->create([
'currency' => 'EUR',
'product' => $product->getStripeId(),
'unit_amount' => $price->getAmount() * 100,
'active' => $price->isPublished(),
'recurring' => [
'interval' => $price->getInterval(),
'interval_count' => $price->getIntervalNumber()
],
'tax_behavior' => 'inclusive',
]);
$price->setStripeId($res->id);
}
if($price->getStripeId()) {
$stripe->prices->update(
$price->getStripeId(),
[ 'active' => false ]
);
if($price->isPublished()){
$product = $price->getProduct();
$res = $stripe->prices->create([
'currency' => 'EUR',
'product' => $product->getStripeId(),
'unit_amount' => $price->getAmount() * 100,
'active' => $price->isPublished(),
'recurring' => [
'interval' => $price->getInterval(),
'interval_count' => $price->getIntervalNumber()
],
'tax_behavior' => 'inclusive',
]);
$price->setStripeId($res->id);
}
}
}
private function deletePrice(Price $price){
if($price->getStripeId()) {
try{
$stripe = $this->stripe();
$stripe->prices->update(
$price->getStripeId(),
[ 'active' => false ]
);
}catch(Exception $e){
Logger::err('[price:'.$price->getId().'] unable to delete from stripe');
}
}
}
private function createCoupon(Coupon $coupon){
$stripe = $this->stripe();
if($coupon->isPublished()){
$stripeCoupon = [
$coupon->getCouponType() => ($coupon->getCouponType() === 'percent_off' ? $coupon->getReduction() : ($coupon->getReduction() *100)),
'currency' => 'EUR',
'duration' => $coupon->getValidity(),
'duration_in_months' => $coupon->getDuration(),
'name' => $coupon->getName(),
];
if($coupon->getMaxUsage()){
$stripeCoupon['max_redemptions'] = $coupon->getMaxUsage();
}
$res = $stripe->coupons->create($stripeCoupon);
$coupon->setStripeId($res->id);
}
//UPDATE
if($coupon->getCodeStripeId()){
$stripe->promotionCodes->update(
$coupon->getCodeStripeId(),
[ 'active' => false ]
);
if($coupon->isPublished()){
$stripePromoCode = [
'coupon' => $res->id,
'code' => $coupon->getCode(),
'expires_at' => $coupon->getExpireDate()->timestamp,
];
if($coupon->getMaxUsage()){
$stripePromoCode['max_redemptions'] = $coupon->getMaxUsage();
}
$res2 = $stripe->promotionCodes->create($stripePromoCode);
$coupon->setCodeStripeId($res2->id);
}
}
//CREATE
if(!$coupon->getCodeStripeId() && $coupon->isPublished()){
$stripePromoCode = [
'coupon' => $res->id,
'code' => $coupon->getCode(),
'expires_at' => $coupon->getExpireDate()->timestamp,
];
if($coupon->getMaxUsage()){
$stripePromoCode['max_redemptions'] = $coupon->getMaxUsage();
}
$res3 = $stripe->promotionCodes->create($stripePromoCode);
$coupon->setCodeStripeId($res3->id);
}
}
private function deleteCoupon(Coupon $coupon){
if($coupon->getStripeId()) {
try{
$stripe = $this->stripe();
$stripe->coupons->update($coupon->getStripeId(),
[
'active' => false
]
);
$stripe->promotionCodes->update($coupon->getCodeStripeId(),
[ 'active' => false ]
);
}catch(Exception $e){
Logger::err('[coupon:'.$coupon->getId().'] unable to delete from stripe');
}
}
}
}