Working : Coupon - update loop

This commit is contained in:
gmorel
2013-09-12 21:34:50 +02:00
parent 58ef39769c
commit 964355e60d
7 changed files with 206 additions and 13 deletions

View File

@@ -25,6 +25,7 @@ namespace Thelia\Constraint;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Thelia\Constraint\Rule\AvailableForEveryoneManager;
use Thelia\Constraint\Rule\AvailableForTotalAmountManager;
use Thelia\Constraint\Rule\CouponRuleInterface;
use Thelia\Constraint\Rule\SerializableRule;
@@ -74,11 +75,22 @@ class ConstraintFactory
*/
public function serializeCouponRuleCollection(CouponRuleCollection $collection)
{
if ($collection->isEmpty()) {
/** @var CouponRuleInterface $ruleNoCondition */
$ruleNoCondition = $this->container->get(
'thelia.constraint.rule.available_for_everyone'
);
$collection->add($ruleNoCondition);
}
$serializableRules = array();
$rules = $collection->getRules();
if ($rules !== null) {
/** @var $rule CouponRuleInterface */
foreach ($rules as $rule) {
// Remove all rule if the "no condition" rule is found
// if ($rule->getServiceId() == 'thelia.constraint.rule.available_for_everyone') {
// return base64_encode(json_encode(array($rule->getSerializableRule())));
// }
$serializableRules[] = $rule->getSerializableRule();
}
}

View File

@@ -34,6 +34,7 @@ use Thelia\Core\Template\Element\LoopResultRow;
use Thelia\Core\Template\Loop\Argument\ArgumentCollection;
use Thelia\Core\Template\Loop\Argument\Argument;
use Thelia\Coupon\Type\CouponInterface;
use Thelia\Model\CouponQuery;
use Thelia\Model\Coupon as MCoupon;
use Thelia\Type;
@@ -98,6 +99,27 @@ class Coupon extends BaseI18nLoop
$coupon->getSerializedRules()
);
/** @var CouponInterface $couponManager */
$couponManager = $this->container->get($coupon->getType());
$couponManager->set(
$this->container->get('thelia.adapter'),
$coupon->getCode(),
$coupon->getTitle(),
$coupon->getShortDescription(),
$coupon->getDescription(),
$coupon->getAmount(),
$coupon->getIsCumulative(),
$coupon->getIsRemovingPostage(),
$coupon->getIsAvailableOnSpecialOffers(),
$coupon->getIsEnabled(),
$coupon->getMaxUsage(),
$coupon->getExpirationDate()
);
$now = time();
$datediff = $coupon->getExpirationDate()->getTimestamp() - $now;
$daysLeftBeforeExpiration = floor($datediff/(60*60*24));
$cleanedRules = array();
/** @var CouponRuleInterface $rule */
foreach ($rules->getRules() as $key => $rule) {
@@ -114,9 +136,13 @@ class Coupon extends BaseI18nLoop
->set("USAGE_LEFT", $coupon->getMaxUsage())
->set("IS_CUMULATIVE", $coupon->getIsCumulative())
->set("IS_REMOVING_POSTAGE", $coupon->getIsRemovingPostage())
->set("IS_AVAILABLE_ON_SPECIAL_OFFERS", $coupon->getIsAvailableOnSpecialOffers())
->set("IS_ENABLED", $coupon->getIsEnabled())
->set("AMOUNT", $coupon->getAmount())
->set("APPLICATION_CONDITIONS", $cleanedRules);
->set("APPLICATION_CONDITIONS", $cleanedRules)
->set("TOOLTIP", $couponManager->getToolTip())
->set("DAY_LEFT_BEFORE_EXPIRATION", $daysLeftBeforeExpiration)
->set("SERVICE_ID", $couponManager->getServiceId());
$loopResult->addRow($loopResultRow);
}

View File

@@ -78,7 +78,14 @@ $(function($){
// Save rules on click
couponManager.onClickSaveRule = function() {
$('#constraint-save-btn').on('click', function () {
if($('#category-rule').val() == 'thelia.constraint.rule.available_for_everyone') {
// @todo translate + modal
var r= confirm("Do you really want to set this coupon available to everyone ?");
if (r == true) {
couponManager.createOrUpdateRuleAjax();
}
}
});
};
couponManager.onClickSaveRule();

View File

@@ -16,7 +16,6 @@
<h1>{intl l='Coupon : '}<small>{$CODE}</small></h1>
</div>
<section class="row">
<div class="col-md-12 general-block-decorator">
@@ -31,9 +30,27 @@
<td>{intl l='Title'}</td>
<td>{$TITLE}</td>
</tr>
<tr>
<td colspan="2">
{if $IS_ENABLED}
<span class="label label-success">
{intl l="Is enabled"}
</span>
{else}
<span class="label label-warning">
{intl l="Is disabled"}
</span>
{/if}
</td>
</tr>
<tr>
<td colspan="2">
{$TOOLTIP}
</td>
</tr>
<tr>
<td>{intl l='Expiration date'}</td>
<td>{$EXPIRATION_DATE}</td>
<td>{$EXPIRATION_DATE} ({$DAY_LEFT_BEFORE_EXPIRATION} {intl l="days left"})</td>
</tr>
<tr>
<td>{intl l='Usage left'}</td>
@@ -43,7 +60,7 @@
{$USAGE_LEFT}
</span>
{else}
<span class="label label-important">
<span class="label label-warning">
0
</span>
{/if}
@@ -62,7 +79,7 @@
{intl l="May be cumulative"}
</span>
{else}
<span class="label label-important">
<span class="label label-warning">
{intl l="Can't be cumulative"}
</span>
{/if}
@@ -71,7 +88,7 @@
<tr>
<td colspan="2">
{if $IS_REMOVING_POSTAGE}
<span class="label label-important">
<span class="label label-warning">
{intl l="Will remove postage"}
</span>
{else}
@@ -81,6 +98,19 @@
{/if}
</td>
</tr>
<tr>
<td colspan="2">
{if $IS_AVAILABLE_ON_SPECIAL_OFFERS}
<span class="label label-warning">
{intl l="Will be available on special offers"}
</span>
{else}
<span class="label label-success">
{intl l="Won't be available on special offers"}
</span>
{/if}
</td>
</tr>
<tr>
<td>{intl l='Amount'}</td>
<td>{$AMOUNT}</td>
@@ -102,7 +132,6 @@
<td>{intl l='Actions'}</td>
<td>
<a href="#url" class="btn btn-default btn-primary btn-medium"><span class="icon-edit icon-white"></span> {intl l='Edit'}</a>
<a href="#url" class="btn btn-default btn-success btn-medium" data-toggle="confirm" data-target="#enable"><span class="glyphicon glyphicon-ok"></span> {intl l='Enabled'}</a>
</td>
</tr>
</tbody>

View File

@@ -112,13 +112,21 @@
}).done(function(data) {
$('#constraint-add-operators-values').html(data);
couponManager.ruleToSave.serviceId = ruleId;
if (ruleId == -1) {
// Placeholder can't be saved
$('#constraint-save-btn').hide();
} else {
$('#constraint-save-btn').show();
}
return callBack();
});
};
// Rules which will be saved
couponManager.rulesToSave = couponManager.initRules();
$('#constraint-save-btn').hide();
});
</script>
{/block}

View File

@@ -202,6 +202,7 @@
<div id="rule-add-type" class="form-group col-md-4">
<label for="categoryRule">{intl l='Rule\'s category :'}</label>
<select name="categoryRule" id="category-rule" class="form-control">
<option value="-1" >{intl l='Please select a rule category'}</option>
{foreach from=$availableRules item=availableRule}
<option value="{$availableRule.serviceId}" data-description="{$availableRule.toolTip}">{$availableRule.name}</option>
{/foreach}

View File

@@ -189,6 +189,116 @@ casper.thenOpen(thelia2_login_coupon_update_url, function() {
this.test.assertSelectorHasText('tbody#constraint-list tr:nth-child(3)', 'If cart total amount is inferior or equal to 401 GBP','8) 4rd rule created found');
});
// Testing deleting all rules
casper.then(function(){
// Click on Delete button
this.click('tbody#constraint-list tr:nth-child(1) .constraint-delete-btn');
});
casper.wait(1000, function() {
this.echo("\nWaiting....");
});
casper.then(function(){
// Click on Delete button
this.click('tbody#constraint-list tr:nth-child(1) .constraint-delete-btn');
});
casper.wait(1000, function() {
this.echo("\nWaiting....");
});
casper.then(function(){
// Click on Delete button
this.click('tbody#constraint-list tr:nth-child(1) .constraint-delete-btn');
});
casper.wait(1000, function() {
this.echo("\nWaiting....");
});
casper.then(function(){
this.capture('tests/functionnal/casperjs/screenshot/coupons/rule-all-deleted.png');
this.test.assertSelectorHasText('tbody#constraint-list tr:nth-child(1)', 'No conditions','9) 1st default rule found');
test.assertDoesntExist('tbody#constraint-list tr:nth-child(2)');
});
// Check if created rule has been well saved
casper.thenOpen(thelia2_login_coupon_update_url, function() {
this.test.comment('Now on : ' + this.getCurrentUrl());
this.capture('tests/functionnal/casperjs/screenshot/coupons/rule-all-deleted-refreshed.png');
this.test.assertSelectorHasText('tbody#constraint-list tr:nth-child(1)', 'No conditions','10) 1st default rule found');
test.assertDoesntExist('tbody#constraint-list tr:nth-child(2)');
});
// Test add no condition rule
casper.then(function(){
this.evaluate(function() {
$('#category-rule').val('thelia.constraint.rule.available_for_x_articles').change();
return true;
});
});
casper.wait(1000, function() {
this.echo("\nWaiting....");
});
// Test Rule updating
casper.then(function(){
this.evaluate(function() {
$('#quantity-operator').val('>').change();
return true;
});
this.sendKeys('input#quantity-value', '4');
this.click('#constraint-save-btn');
});
casper.wait(1000, function() {
this.echo("\nWaiting....");
});
casper.then(function(){
this.capture('tests/functionnal/casperjs/screenshot/coupons/rule-all-deleted.png');
this.test.assertSelectorHasText('tbody#constraint-list tr:nth-child(1)', 'If cart products quantity is superior to 4', '11) 1st default rule found');
test.assertDoesntExist('tbody#constraint-list tr:nth-child(2)');
});
// Check if created rule has been well saved
casper.thenOpen(thelia2_login_coupon_update_url, function() {
this.test.comment('Now on : ' + this.getCurrentUrl());
this.capture('tests/functionnal/casperjs/screenshot/coupons/rule-all-deleted-refreshed.png');
this.test.assertSelectorHasText('tbody#constraint-list tr:nth-child(1)', 'If cart products quantity is superior to 4','12) 1st default rule found');
test.assertDoesntExist('tbody#constraint-list tr:nth-child(2)');
});
casper.then(function(){
this.evaluate(function() {
$('#category-rule').val('thelia.constraint.rule.available_for_everyone').change();
return true;
});
});
casper.wait(1000, function() {
this.echo("\nWaiting....");
});
// Test Rule updating
casper.then(function(){
this.click('#constraint-save-btn');
});
casper.wait(1000, function() {
this.echo("\nWaiting....");
});
casper.then(function(){
this.capture('tests/functionnal/casperjs/screenshot/coupons/rule-all-deleted.png');
this.test.assertSelectorHasText('tbody#constraint-list tr:nth-child(1)', 'No conditions','13) 1st default rule found');
test.assertDoesntExist('tbody#constraint-list tr:nth-child(2)');
});
// Check if created rule has been well saved
casper.thenOpen(thelia2_login_coupon_update_url, function() {
this.test.comment('Now on : ' + this.getCurrentUrl());
this.capture('tests/functionnal/casperjs/screenshot/coupons/rule-all-deleted-refreshed.png');
this.test.assertSelectorHasText('tbody#constraint-list tr:nth-child(1)', 'No conditions','14) 1st default rule found');
test.assertDoesntExist('tbody#constraint-list tr:nth-child(2)');
});
//RUN
casper.run(function() {
this.test.done();