Skip to content
Code-Schnipsel Gruppen Projekte
Commit 4504be9c erstellt von Patrick Müller's avatar Patrick Müller
Dateien durchsuchen

temp commit

Übergeordneter 3b243ce9
No related branches found
No related tags found
Keine zugehörigen Merge Requests gefunden
<?php
/**
* This file contains package_quiqqer_coupons_ajax_create
*/
use QUI\ERP\Coupons\Handler;
use QUI\Utils\Security\Orthos;
use QUI\ERP\Coupons\CouponCodeException;
use QUI\ERP\Discount\Handler as DiscountsHandler;
/**
* Create new CouponCode(s)
......@@ -27,16 +25,63 @@ function ($attributes) {
unset($attributes['amount']);
}
$discountIds = [];
// Check required fields
$requiredFields = [
'discountAmount'
];
foreach ($requiredFields as $field) {
if (!isset($attributes[$field])) {
throw new CouponCodeException([
'quiqqer/coupons',
'exception.ajax.create.missing_field'
]);
}
}
// Create discount
switch ($attributes['discountType']) {
case 'percentage':
$discountType = DiscountsHandler::DISCOUNT_TYPE_PERCENT;
break;
if (!empty($attributes['discountIds'])) {
$discountIds = \explode(',', $attributes['discountIds']);
default:
$discountType = DiscountsHandler::DISCOUNT_TYPE_CURRENCY;
}
$Discounts = DiscountsHandler::getInstance();
$NewDiscount = $Discounts->createChild([
'active' => 1,
'discount' => (float)$attributes['discountAmount'],
'discount_type' => $discountType,
]);
$L = QUI::getLocale();
for ($i = 0; $i < $amount; $i++) {
$couponCodes[] = Handler::createCouponCode($discountIds, $attributes);
$NewCouponCode = Handler::createCouponCode([$NewDiscount->getId()], $attributes);
if ($i === 0) {
\QUI\Translator::update(
'quiqqer/discount',
'discount.'.$NewDiscount->getId().'.title',
'quiqqer/discount',
[
'de' => $L->getByLang('de', 'quiqqer/coupons', 'Discount.default_title', [
'couponCode' => $NewCouponCode->getCode()
]),
'en' => $L->getByLang('en', 'quiqqer/coupons', 'Discount.default_title', [
'couponCode' => $NewCouponCode->getCode()
])
]
);
\QUI\Translator::publish('quiqqer/discount');
}
$couponCodes[] = $NewCouponCode;
}
} catch (\QUI\ERP\Coupons\CouponCodeException $Exception) {
} catch (CouponCodeException $Exception) {
QUI::getMessagesHandler()->addError(
QUI::getLocale()->get(
'quiqqer/coupons',
......
<div class="quiqqer-coupons-manager-create">
<form>
<label>
<span>{{labelDiscount}}</span>
<input type="hidden" name="discountIds" data-qui="package/quiqqer/discount/bin/controls/Select"/>
</label>
<div class="quiqqer-coupons-manager-create-settings-btn"></div>
<div class="quiqqer-coupons-manager-create-settings">
<label>
<span>{{labelTitle}}</span>
<input type="text" name="title"/>
</label>
<label>
<span>{{labelCode}}</span>
<input type="text" name="code"/>
</label>
<label>
<span>{{labelReusable}}</span>
<select name="maxUsages">
<option value="oncePerUser" selected>{{labelReusableOncePerUser}}</option>
<option value="once">{{labelReusableOnce}}</option>
<option value="unlimited">{{labelReusableUnlimited}}</option>
</select>
</label>
<label>
<span>{{labelUsers}}</span>
<input type="hidden" name="userIds" data-qui="controls/users/Select"/>
</label>
<label>
<span>{{labelGroups}}</span>
<input type="hidden" name="groupIds" data-qui="controls/groups/Select"/>
</label>
<label>
<span>{{labelDate}}</span>
<input type="date" placeholder="YYYY-MM-DD HH:MM" name="validUntilDate"/>
</label>
<label>
<span>{{labelAmount}}</span>
<input type="number" name="amount" min="1" value="1"/>
</label>
<div class="quiqqer-coupons-manager-create-basic">
<h2>
{{headerBasics}}
</h2>
<table class="data-table data-table-flexbox">
<tbody>
<tr>
<td>
<label class="field-container">
<span class="field-container-item">{{labelTitle}}</span>
<input type="text" class="field-container-field" name="title"/>
</label>
<div class="field-container-item-desc">
{{{descTitle}}}
</div>
</td>
</tr>
<tr>
<td>
<label class="field-container">
<span class="field-container-item">{{labelCode}}</span>
<input type="text" class="field-container-field" name="code"/>
</label>
<div class="field-container-item-desc">
{{{descCode}}}
</div>
</td>
</tr>
<tr>
<td>
<label class="field-container">
<span class="field-container-item">{{labelAmount}}</span>
<input type="number" name="amount" class="field-container-field" min="1" value="1"/>
</label>
</td>
</tr>
</tbody>
</table>
</div>
<div class="quiqqer-coupons-manager-create-advanced">
<div class="quiqqer-coupons-manager-create-advanced-discount">
<h2>
{{headerDiscount}}
</h2>
<table class="data-table data-table-flexbox">
<tbody>
{{#isCreate}}
<tr>
<td>
<label class="field-container">
<span class="field-container-item">{{labelDiscountAmount}}</span>
<input type="number" name="discountAmount" class="field-container-field" required/>
</label>
</td>
</tr>
<tr>
<td>
<label class="field-container">
<span class="field-container-item">{{labelDiscountType}}</span>
<select class="field-container-field" name="discountType">
<option value="flat" selected>{{labelDiscountTypeFlat}}</option>
<option value="percentage">{{labelDiscountTypePercentage}}</option>
</select>
</label>
</td>
</tr>
{{/isCreate}}
{{^isCreate}}
<tr class="quiqqer-coupons-manager-create-advanced-discount-select">
<td>
<label class="field-container">
<span class="field-container-item">{{labelDiscount}}</span>
<input type="hidden"
class="field-container-field"
name="discountIds"
data-qui="package/quiqqer/discount/bin/controls/Select"
data-qui-options-multiple="0"
data-qui-options-max="1"
required
/>
</label>
<div class="field-container-item-desc quiqqer-coupons-manager-create-advanced-discount-buttons">
</div>
</td>
</tr>
{{/isCreate}}
</tbody>
</table>
</div>
<div class="quiqqer-coupons-manager-create-advanced-restrictions">
<h2>
{{headerRestrictions}}
</h2>
<table class="data-table data-table-flexbox">
<tbody>
<tr>
<td>
<label class="field-container">
<span class="field-container-item">{{labelReusable}}</span>
<select class="field-container-field" name="maxUsages">
<option value="oncePerUser" selected>{{labelReusableOncePerUser}}</option>
<option value="once">{{labelReusableOnce}}</option>
<option value="unlimited">{{labelReusableUnlimited}}</option>
</select>
</label>
</td>
</tr>
<tr>
<td>
<label class="field-container">
<span class="field-container-item">{{labelDate}}</span>
<input type="date"
name="validUntilDate"
placeholder="YYYY-MM-DD HH:MM"
class="field-container-field"
/>
</label>
</td>
</tr>
<tr>
<td>
<label class="field-container">
<span class="field-container-item">{{labelUsers}}</span>
<input type="hidden"
name="userIds"
class="field-container-field"
data-qui="controls/users/Select"
/>
</label>
</td>
</tr>
<tr>
<td>
<label class="field-container">
<span class="field-container-item">{{labelGroups}}</span>
<input type="hidden"
name="groupIds"
class="field-container-field"
data-qui="controls/groups/Select"
/>
</label>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</form>
</div>
\ No newline at end of file
......@@ -23,11 +23,6 @@
margin-bottom: 15px;
}
.quiqqer-coupons-manager-create label > span {
float: left;
margin-bottom: 5px;
}
.quiqqer-coupons-manager-create-sendmail span,
.quiqqer-coupons-manager-create-sendmail input {
float: right !important;
......@@ -42,7 +37,7 @@
}
.quiqqer-coupons-manager-create-settings {
display: none;
}
.quiqqer-coupons-manager-create-settings-btn {
......@@ -88,4 +83,28 @@
.quiqqer-coupons-manager-usages-tbl-user {
width: 200px;
}
.quiqqer-coupons-manager-create h2 {
font-size: 18px;
margin-bottom: 10px;
}
.quiqqer-coupons-manager-create-advanced {
align-content: stretch;
display: flex;
flex-direction: row;
float: left;
justify-content: space-between;
width: 100%;
}
.quiqqer-coupons-manager-create-advanced-discount {
padding-right: 10px;
width: 50%;
}
.quiqqer-coupons-manager-create-advanced-restrictions {
padding-left: 10px;
width: 50%;
}
\ No newline at end of file
......@@ -331,13 +331,15 @@ define('package/quiqqer/coupons/bin/backend/controls/Manager', [
* @param {Object} [CouponData] - If omitted create new CouponCode
*/
$showDetails: function (CouponData) {
var self = this;
let Form;
let DiscountSelect;
CouponData = CouponData || false;
var FuncSubmit = function () {
var Content = Popup.getContent();
var Form = Content.getElement('form');
var FuncSubmit = () => {
if (!Form.reportValidity()) {
return;
}
Popup.Loader.show();
......@@ -348,21 +350,48 @@ define('package/quiqqer/coupons/bin/backend/controls/Manager', [
return;
}
self.refresh();
this.refresh();
Popup.close();
});
return;
}
CouponCodes.create(QUIFormUtils.getFormData(Form)).then(function (couponCodeId) {
CouponCodes.create(QUIFormUtils.getFormData(Form)).then((couponCodeId) => {
if (!couponCodeId) {
Popup.Loader.hide();
return;
}
self.refresh();
Popup.close();
require([
'package/quiqqer/translator/bin/classes/Translator'
], (Translator) => {
new Translator().publish(lg).then(() => {
this.refresh();
Popup.close();
}).then(() => {
this.refresh();
Popup.close();
});
});
}).catch(() => {
Popup.Loader.hide();
});
};
var openDiscount = () => {
Popup.Loader.show();
require([
'package/quiqqer/discount/bin/controls/Discounts',
'utils/Panels'
], function (DiscountsManagerPanel, PanelUtils) {
const Panel = new DiscountsManagerPanel();
PanelUtils.openPanelInTasks(Panel).then((PanelOpened) => {
PanelOpened.editChild(parseInt(DiscountSelect.getValue()));
Popup.close();
});
});
};
......@@ -374,22 +403,20 @@ define('package/quiqqer/coupons/bin/backend/controls/Manager', [
title : CouponData ?
QUILocale.get(lg, 'controls.manager.details.popup.title_edit', {code: CouponData.code}) :
QUILocale.get(lg, 'controls.manager.details.popup.title_new'),
maxHeight : 335,
maxWidth : 450,
maxHeight : 1000,
maxWidth : 1000,
events : {
onOpen: function () {
var Content = Popup.getContent();
var Form = Content.getElement('form');
var SubmitBtn = Popup.getButton('submit');
onOpen: (Win) => {
const Content = Popup.getContent();
Form = Content.getElement('form');
Form.addEvent('submit', function (event) {
event.stop();
FuncSubmit();
});
var Settings = Content.getElement('.quiqqer-coupons-manager-create-settings');
var Amount = Content.getElement('input[name="amount"]');
var Code = Content.getElement('input[name="code"]');
const Amount = Content.getElement('input[name="amount"]');
const Code = Content.getElement('input[name="code"]');
Code.addEvent('keyup', function () {
if (Code.value !== '') {
......@@ -400,97 +427,93 @@ define('package/quiqqer/coupons/bin/backend/controls/Manager', [
}
});
new QUIButton({
textimage: 'fa fa-cogs',
text : QUILocale.get(lg, 'controls.Manager.create.settings_btn.show'),
events : {
onClick: function (Btn) {
if (Settings.getStyle('display') === 'none') {
Settings.setStyle('display', 'block');
Btn.setAttribute(
'text',
QUILocale.get(lg, 'controls.Manager.create.settings_btn.hide')
);
Content.getElement('input[name="title"]').focus();
Popup.setAttribute('maxHeight', 850);
Popup.resize();
} else {
Settings.setStyle('display', 'none');
Btn.setAttribute(
'text',
QUILocale.get(lg, 'controls.Manager.create.settings_btn.show')
);
Popup.setAttribute('maxHeight', 335);
Popup.resize();
}
}
}
}).inject(Content.getElement(
'.quiqqer-coupons-manager-create-settings-btn'
));
let EditDiscountBtn = false;
if (CouponData) {
CouponData.discountIds = CouponData.discountIds.join(',');
QUIFormUtils.setDataToForm(CouponData, Form);
EditDiscountBtn = new QUIButton({
'class' : 'optional',
textimage: 'fa fa-percent',
text : QUILocale.get(lg, 'controls.manager.details.popup.btn.open_discount'),
title : QUILocale.get(lg, 'controls.manager.details.popup.btn.open_discount'),
events : {
onClick: openDiscount
},
styles : {
float: 'right'
}
}).inject(
Content.getElement('.quiqqer-coupons-manager-create-advanced-discount-buttons')
);
}
Popup.Loader.show();
Win.Loader.show();
QUI.parse(Content).then(() => {
Win.Loader.hide();
QUI.parse(Content).then(function () {
var DiscountSelect = QUI.Controls.getById(
if (!CouponData) {
return;
}
DiscountSelect = QUI.Controls.getById(
Content.getElement('input[name="discountIds"]').get('data-quiid')
);
DiscountSelect.addEvents({
onChange: function () {
if (DiscountSelect.getValue() === '') {
SubmitBtn.disable();
} else {
SubmitBtn.enable();
}
onAddItem : () => {
EditDiscountBtn.enable();
},
onRemoveItem: () => {
EditDiscountBtn.disable();
}
});
Popup.Loader.hide();
});
}
},
closeButton: true,
content : Mustache.render(templateCreate, {
labelTitle : QUILocale.get(lg, lgPrefix + 'labelTitle'),
labelCode : QUILocale.get(lg, lgPrefix + 'labelCode'),
labelUsers : QUILocale.get(lg, lgPrefix + 'labelUsers'),
labelGroups : QUILocale.get(lg, lgPrefix + 'labelGroups'),
labelDate : QUILocale.get(lg, lgPrefix + 'labelDate'),
labelAmount : QUILocale.get(lg, lgPrefix + 'labelAmount'),
labelReusable : QUILocale.get(lg, lgPrefix + 'labelReusable'),
labelReusableOncePerUser: QUILocale.get(lg, lgPrefix + 'labelReusableOncePerUser'),
labelReusableOnce : QUILocale.get(lg, lgPrefix + 'labelReusableOnce'),
labelReusableUnlimited : QUILocale.get(lg, lgPrefix + 'labelReusableUnlimited'),
labelDiscount : QUILocale.get(lg, lgPrefix + 'labelDiscount')
labelTitle : QUILocale.get(lg, lgPrefix + 'labelTitle'),
labelCode : QUILocale.get(lg, lgPrefix + 'labelCode'),
labelUsers : QUILocale.get(lg, lgPrefix + 'labelUsers'),
labelGroups : QUILocale.get(lg, lgPrefix + 'labelGroups'),
labelDate : QUILocale.get(lg, lgPrefix + 'labelDate'),
labelAmount : QUILocale.get(lg, lgPrefix + 'labelAmount'),
labelReusable : QUILocale.get(lg, lgPrefix + 'labelReusable'),
labelReusableOncePerUser : QUILocale.get(lg, lgPrefix + 'labelReusableOncePerUser'),
labelReusableOnce : QUILocale.get(lg, lgPrefix + 'labelReusableOnce'),
labelReusableUnlimited : QUILocale.get(lg, lgPrefix + 'labelReusableUnlimited'),
labelDiscount : QUILocale.get(lg, lgPrefix + 'labelDiscount'),
headerBasics : QUILocale.get(lg, lgPrefix + 'headerBasics'),
headerDiscount : QUILocale.get(lg, lgPrefix + 'headerDiscount'),
headerRestrictions : QUILocale.get(lg, lgPrefix + 'headerRestrictions'),
descCode : QUILocale.get(lg, lgPrefix + 'descCode'),
descTitle : QUILocale.get(lg, lgPrefix + 'descTitle'),
labelDiscountAmount : QUILocale.get(lg, lgPrefix + 'labelDiscountAmount'),
labelDiscountType : QUILocale.get(lg, lgPrefix + 'labelDiscountType'),
labelDiscountTypeFlat : QUILocale.get(lg, lgPrefix + 'labelDiscountTypeFlat'),
labelDiscountTypePercentage: QUILocale.get(lg, lgPrefix + 'labelDiscountTypePercentage'),
isCreate: !CouponData
})
});
Popup.open();
Popup.addButton(new QUIButton({
name : 'submit',
disabled: true,
text : CouponData ?
name : 'submit',
text : CouponData ?
QUILocale.get(lg, 'controls.manager.details.popup.btn.confirm_text_edit') :
QUILocale.get(lg, 'controls.manager.details.popup.btn.confirm_text_new'),
alt : CouponData ?
alt : CouponData ?
QUILocale.get(lg, 'controls.manager.details.popup.btn.confirm_edit') :
QUILocale.get(lg, 'controls.manager.details.popup.btn.confirm_new'),
title : CouponData ?
title : CouponData ?
QUILocale.get(lg, 'controls.manager.details.popup.btn.confirm_edit') :
QUILocale.get(lg, 'controls.manager.details.popup.btn.confirm_new'),
events : {
events: {
onClick: FuncSubmit
}
}));
......
......@@ -16,7 +16,8 @@
},
"require": {
"quiqqer\/quiqqer": "^1.2|*@dev",
"quiqqer\/discount": "^1|*@dev"
"quiqqer\/discount": "^1|*@dev",
"quiqqer/products": "^1.3|*@dev"
},
"autoload": {
"psr-4": {
......
......@@ -2,6 +2,10 @@
<database>
<global>
<table name="products">
<field type="BIGINT(20) NULL DEFAULT NULL">couponDiscountId</field>
</table>
<table name="quiqqer_coupons">
<field type="BIGINT(20) NOT NULL AUTO_INCREMENT PRIMARY KEY">id</field>
<field type="MEDIUMTEXT NULL">discountIds</field>
......
<?xml version="1.0" encoding="UTF-8"?>
<events>
<event on="onPackageSetup-quiqqer/coupons"
fire="\QUI\ERP\Coupons\Events::onPackageSetup"
/>
<event on="onQuiqqerProductsProductCreate"
fire="\QUI\ERP\Coupons\Events::onQuiqqerProductsProductCreate"
/>
<event on="onQuiqqer::order::orderProcessBasketEnd"
fire="\QUI\ERP\Coupons\Events::templateOrderProcessBasketEnd"
/>
......
......@@ -80,9 +80,34 @@
<de><![CDATA[Gutschein-Code: [code]]]></de>
<en><![CDATA[Coupon code: [code]]]></en>
</locale>
<locale name="fieldCategory.quiqqer-coupons-fields">
<de><![CDATA[Gutschein-Einstellungen]]></de>
<en><![CDATA[Coupn code settings]]></en>
</locale>
</groups>
<groups name="quiqqer/coupons" datatype="php">
<locale name="Discount.default_title">
<de><![CDATA[Coupon-Code [couponCode]]]></de>
<en><![CDATA[Coupon code [couponCode]]]></en>
</locale>
<locale name="Discount.default_title.product">
<de><![CDATA[Produkt "[productTitle]" (#[productId])]]></de>
<en><![CDATA[Product "[productTitle]" (#[productId])]]></en>
</locale>
<locale name="exception.ajax.create.missing_field">
<de><![CDATA[Bitte fülle alle Pflichtfelder aus.]]></de>
<en><![CDATA[Please fill out all required fields.]]></en>
</locale>
<locale name="product_type.CouponProductType.title">
<de><![CDATA[Gutschein]]></de>
<en><![CDATA[Coupon]]></en>
</locale>
<!-- Ajax -->
<locale name="message.ajax.general_error">
<de>
......@@ -181,6 +206,10 @@
<de><![CDATA[Verwendungszweck (optional)]]></de>
<en><![CDATA[Usage (optional)]]></en>
</locale>
<locale name="controls.manager.create.template.descTitle">
<de><![CDATA[Dient der <b>internen</b> Beschreibung des Verwendungszwecks für diesen Gutschein-Code. Kunden sehen diese Beschreibung <b>nicht</b>.]]></de>
<en><![CDATA[Used for <b>internal</b> description of the purpose for this coupon code. Customers will <b>not</b> see this description.]]></en>
</locale>
<locale name="controls.manager.create.template.labelAmount">
<de><![CDATA[Anzahl generierte Gutschein-Codes]]></de>
<en><![CDATA[Amount of generated coupon codes]]></en>
......@@ -194,8 +223,12 @@
<en><![CDATA[Restrict to the following groups (optional)]]></en>
</locale>
<locale name="controls.manager.create.template.labelCode">
<de><![CDATA[Gutschein-Code (optional - wird automatisch generiert, wenn Feld leergelassen wird)]]></de>
<en><![CDATA[Coupon code (optional - is generated automatically if left empty)]]></en>
<de><![CDATA[Gutschein-Code (optional)]]></de>
<en><![CDATA[Coupon code (optional)]]></en>
</locale>
<locale name="controls.manager.create.template.descCode">
<de><![CDATA[Wird dieses Feld leergelassen, wird automatisch ein zufälliger neuer Gutschein-Code generiert.]]></de>
<en><![CDATA[If this field is left blank, a random new coupon code will be generated automatically.]]></en>
</locale>
<locale name="controls.manager.create.template.labelReusable">
<de><![CDATA[Erlaubte Verwendung]]></de>
......@@ -209,6 +242,22 @@
<de><![CDATA[Insgesamt nur einmal einlösbar]]></de>
<en><![CDATA[Only redeemable once in total]]></en>
</locale>
<locale name="controls.manager.create.template.labelDiscountAmount">
<de><![CDATA[Rabatt-Betrag]]></de>
<en><![CDATA[Discount amount]]></en>
</locale>
<locale name="controls.manager.create.template.labelDiscountType">
<de><![CDATA[Rabatt-Typ]]></de>
<en><![CDATA[Discount type]]></en>
</locale>
<locale name="controls.manager.create.template.labelDiscountTypeFlat">
<de><![CDATA[Festbetrag]]></de>
<en><![CDATA[Fixed amount]]></en>
</locale>
<locale name="controls.manager.create.template.labelDiscountTypePercentage">
<de><![CDATA[Prozentual]]></de>
<en><![CDATA[Percentage]]></en>
</locale>
<locale name="controls.manager.create.template.labelReusableUnlimited">
<de><![CDATA[Unbegrenzt einlösbar]]></de>
<en><![CDATA[Unlimited redeemable]]></en>
......@@ -217,6 +266,18 @@
<de><![CDATA[Verknüpfter Rabatt]]></de>
<en><![CDATA[Linked discount]]></en>
</locale>
<locale name="controls.manager.create.template.headerBasics">
<de><![CDATA[Grundeinstellungen]]></de>
<en><![CDATA[Basic settings]]></en>
</locale>
<locale name="controls.manager.create.template.headerDiscount">
<de><![CDATA[Ermäßigung]]></de>
<en><![CDATA[Discount]]></en>
</locale>
<locale name="controls.manager.create.template.headerRestrictions">
<de><![CDATA[Nutzungseinschränkungen]]></de>
<en><![CDATA[Usage restrictions]]></en>
</locale>
<locale name="controls.manager.details.popup.title_new">
<de><![CDATA[Neuen Gutschein-Code erstellen]]></de>
<en><![CDATA[Create new Coupon code]]></en>
......@@ -241,6 +302,10 @@
<de><![CDATA[Hier klicken, um die Änderungen am Gutschein-Code zu speichern]]></de>
<en><![CDATA[Click here to save the changes made to the Coupon code]]></en>
</locale>
<locale name="controls.manager.details.popup.btn.open_discount">
<de><![CDATA[Rabatt konfigurieren]]></de>
<en><![CDATA[Configure discount]]></en>
</locale>
<locale name="controls.manager.tbl.btn.create">
<de><![CDATA[Erstellen]]></de>
<en><![CDATA[Create]]></en>
......
......@@ -23,5 +23,9 @@
<license><![CDATA[GPL-3.0+]]></license>
</copyright>
<provider>
<productType src="\QUI\ERP\Coupons\Products\CouponProductType"/>
</provider>
</package>
</quiqqer>
\ No newline at end of file
<quiqqer>
<products>
<fieldCategories>
<fieldCategory name="quiqqer-coupons-fields">
<title>
<locale group="quiqqer/coupons" var="fieldCategory.quiqqer-coupons-fields"/>
</title>
<icon>fa fa-credit-card</icon>
<fields>
<field>670</field>
<field>671</field>
<field>672</field>
</fields>
</fieldCategory>
</fieldCategories>
</products>
</quiqqer>
\ No newline at end of file
......@@ -3,11 +3,17 @@
namespace QUI\ERP\Coupons;
use QUI;
use QUI\ERP\Order\AbstractOrder;
use QUI\ERP\Products\Handler\Fields;
use QUI\ERP\Products\Interfaces\ProductInterface;
use Quiqqer\Engine\Collector;
use QUI\ERP\Order\Basket\Basket;
use QUI\ERP\Order\Basket\BasketGuest;
use QUI\ERP\Coupons\Handler as CouponsHandler;
use QUI\ERP\Discount\EventHandling as DiscountEvents;
use QUI\ERP\Coupons\Products\CouponProductType;
use QUI\ERP\Coupons\Products\Handler as CouponProductsHandler;
use QUI\ERP\Products\Handler\Products;
/**
* Class Events
......@@ -16,6 +22,21 @@
*/
class Events
{
/**
* quiqqer/quiqqer: onPackageSetup
*
* @param QUI\Package\Package $Package
* @return void
*/
public static function onPackageSetup(QUI\Package\Package $Package)
{
try {
self::createProductFields();
} catch (\Exception $Exception) {
QUI\System\Log::writeException($Exception);
}
}
/**
* Template event quiqqer/order: onQuiqqer::order::orderProcessBasketEnd
*
......@@ -390,4 +411,166 @@ protected static function addCouponToOrder($Order, $coupon)
} catch (\Exception $Exception) {
}
}
/**
* Create all fixed product fields that quiqqer/stock-management provides
*
* @return void
* @throws QUI\Exception
*/
protected static function createProductFields()
{
$fields = [
CouponProductType::PRODUCT_FIELD_ID_TRANSFERABLE => [
'title' => [
'de' => 'Gutschein-Code ist übertragbar',
'en' => 'Coupon code is transferable'
],
'type' => Fields::TYPE_BOOL,
'public' => false,
'standard' => false
],
CouponProductType::PRODUCT_FIELD_ID_GENERATE_PDF => [
'title' => [
'de' => 'Gutschein-Code als PDF bereitstellen',
'en' => 'Provice coupon code as PDF'
],
'type' => Fields::TYPE_BOOL,
'public' => false,
'standard' => false
],
CouponProductType::PRODUCT_FIELD_ID_COUPON_AMOUNT => [
'title' => [
'de' => 'Gutschein-Code Wert',
'en' => 'Coupon code amount'
],
'type' => Fields::TYPE_FLOAT,
'public' => false,
'standard' => false
]
];
$fieldsCreated = false;
foreach ($fields as $fieldId => $field) {
try {
Fields::getField($fieldId);
continue;
} catch (\Exception $Exception) {
// Field does not exist -> create it
}
try {
Fields::createField([
'id' => $fieldId,
'type' => $field['type'],
'titles' => $field['title'],
'workingtitles' => $field['title'],
'systemField' => 0,
'standardField' => !empty($field['standard']) ? 1 : 0,
'publicField' => !empty($field['public']) ? 1 : 0,
'options' => !empty($field['options']) ? $field['options'] : null
]);
} catch (\Exception $Exception) {
QUI\System\Log::writeException($Exception);
continue;
}
$fieldsCreated = true;
}
if ($fieldsCreated) {
QUI\Translator::publish('quiqqer/products');
}
}
/**
* Assign plan product fields to a product
*
* @param ProductInterface $Product
* @return void
*/
public static function onQuiqqerProductsProductCreate(ProductInterface $Product)
{
if (!($Product instanceof CouponProductType)) {
return;
}
$fields = [
CouponProductType::PRODUCT_FIELD_ID_TRANSFERABLE => true,
CouponProductType::PRODUCT_FIELD_ID_GENERATE_PDF => true,
CouponProductType::PRODUCT_FIELD_ID_COUPON_AMOUNT => null
];
foreach ($fields as $fieldId => $value) {
try {
$Field = Fields::getField($fieldId);
$Field->setValue($value);
$Product->addOwnField($Field);
} catch (\Exception $Exception) {
QUI\System\Log::writeException($Exception);
}
}
try {
$Product->update(QUI::getUsers()->getSystemUser());
} catch (\Exception $Exception) {
QUI\System\Log::writeException($Exception);
}
}
/**
* quiqqer/products: onQuiqqerProductsProductSave
*
* Create or update a discount that is associated with a product
*
* @param QUI\ERP\Products\Product\Product $Product
*/
public static function onQuiqqerProductsProductSave(QUI\ERP\Products\Product\Product $Product)
{
if ($Product instanceof CouponProductType) {
try {
CouponProductsHandler::updateProductDiscount($Product);
} catch (\Exception $Exception) {
QUI\System\Log::writeException($Exception);
}
}
}
/**
* quiqqer/order: onQuiqqerOrderCreated
*
* Parse coupon attributes from order and create coupon codes for the buyer.
*
* @param AbstractOrder $Order
* @return void
*/
public static function onQuiqqerOrderCreated(AbstractOrder $Order)
{
/** @var QUI\ERP\Accounting\Article $Article */
foreach ($Order->getArticles() as $Article) {
try {
// Do not parse coupon codes / discounts
if (empty($Article->getId()) || !\is_numeric($Article->getId())) {
continue;
}
$Product = Products::getProduct($Article->getId());
// Only parse download products
if (!($Product->getType() instanceof CouponProductType)) {
continue;
}
CouponProductsHandler::createCouponCodeFromProduct($Product);
} catch (\Exception $Exception) {
if ($Exception->getCode() === 404) {
QUI\System\Log::writeDebugException($Exception);
} else {
QUI\System\Log::writeException($Exception);
}
}
}
}
}
<?php
namespace QUI\ERP\Coupons\Products;
use QUI;
use QUI\ERP\Products\Product\Types\DigitalProduct;
/**
* Class DigitalProduct
*
* Represents a non-physical product that does not require shipping.
*/
class CouponProductType extends DigitalProduct
{
/**
* Special fields for coupon products
*/
const PRODUCT_FIELD_ID_TRANSFERABLE = 670;
const PRODUCT_FIELD_ID_GENERATE_PDF = 671;
const PRODUCT_FIELD_ID_COUPON_AMOUNT = 672;
/**
* @param QUI\Locale $Locale
* @return string
*/
public static function getTypeTitle($Locale = null)
{
if ($Locale === null) {
$Locale = QUI::getLocale();
}
return $Locale->get('quiqqer/coupons', 'product_type.CouponProductType.title');
}
/**
* @param QUI\Locale $Locale
* @return string
*/
public static function getTypeDescription($Locale = null)
{
if ($Locale === null) {
$Locale = QUI::getLocale();
}
return $Locale->get('quiqqer/coupons', 'product_type.CouponProductType.description');
}
}
<?php
namespace QUI\ERP\Coupons\Products;
use QUI;
use QUI\ERP\Order\AbstractOrder;
use QUI\ERP\Products\Product\Product;
use QUI\ERP\Discount\Discount;
use QUI\ERP\Discount\Handler as DiscountHandler;
/**
* Class Handler
*
* Handles coupon code generation from orders / products.
*/
class Handler
{
/**
* Create coupon codes from an order
*
* @param Product $Product
*/
public static function createCouponCodeFromProduct(Product $Product)
{
}
/**
* Create and/or updates the discount that is associated with a coupon code product.
*
* @param Product $Product
* @return void
*
* @throws QUI\Exception
*/
public static function updateProductDiscount(Product $Product): void
{
$Discount = self::getProductDiscount($Product);
if (!$Discount) {
$Discount = self::createProductDiscount($Product);
}
$discountAmount = $Product->getFieldValue(CouponProductType::PRODUCT_FIELD_ID_COUPON_AMOUNT);
if (empty($discountAmount)) {
$discountAmount = $Product->getPrice()->getValue();
}
$Discount->setAttributes([
'discount' => $discountAmount
]);
$Discount->update();
}
/**
* Creates a new discount for all coupon codes generated by the product.
*
* @param Product $Product
* @return Discount
*
* @throws QUI\Exception
*/
protected static function createProductDiscount(Product $Product): Discount
{
$Handler = DiscountHandler::getInstance();
$discountAmount = $Product->getFieldValue(CouponProductType::PRODUCT_FIELD_ID_COUPON_AMOUNT);
if (empty($discountAmount)) {
$discountAmount = $Product->getPrice()->getValue();
}
/** @var Discount $NewDiscount */
$NewDiscount = $Handler->createChild([
'active' => 1,
'discount' => $discountAmount,
'discount_type' => DiscountHandler::DISCOUNT_TYPE_CURRENCY
]);
$L = new QUI\Locale();
$discountTitle = [];
foreach (QUI::availableLanguages() as $lang) {
$L->setCurrent($lang);
$discountTitle[$lang] = $L->get('quiqqer/coupons', 'Discount.default_title.product', [
'productId' => $Product->getId(),
'productTitle' => $Product->getTitle($L)
]);
}
\QUI\Translator::update(
'quiqqer/discount',
'discount.'.$NewDiscount->getId().'.title',
'quiqqer/discount',
$discountTitle
);
\QUI\Translator::publish('quiqqer/discount');
QUI::getDataBase()->update(
QUI\ERP\Products\Utils\Tables::getProductTableName(),
[
'couponDiscountId' => $NewDiscount->getId()
],
[
'id' => $Product->getId()
]
);
return $NewDiscount;
}
/**
* Get the discount that is associated with a coupon code product.
*
* @param Product $Product
* @return Discount|false - Discount or false if no discount created yet
*
* @throws QUI\Exception
*/
public static function getProductDiscount(Product $Product)
{
$result = QUI::getDataBase()->fetch([
'select' => ['couponDiscountId'],
'from' => QUI\ERP\Products\Utils\Tables::getProductTableName(),
'where' => [
'id' => $Product->getId()
]
]);
if (empty($result)) {
return false;
}
/** @var Discount $Discount */
$Discount = DiscountHandler::getInstance()->getChild($result[0]['couponDiscountId']);
return $Discount;
}
}
0% oder .
You are about to add 0 people to the discussion. Proceed with caution.
Bearbeitung dieser Nachricht zuerst beenden!
Bitte registrieren oder zum Kommentieren