Skip to content

Commit

Permalink
adds an option to set date when a product or variant becomes unavailable
Browse files Browse the repository at this point in the history
Includes a few missing translations to czech, improves display of
unavailable items.
  • Loading branch information
Amunak committed Jul 9, 2015
1 parent 021837f commit dad431e
Show file tree
Hide file tree
Showing 17 changed files with 314 additions and 122 deletions.
36 changes: 36 additions & 0 deletions app/migrations/Version20150708104025.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Funlife\Migrations;

use Doctrine\DBAL\Migrations\AbstractMigration;
use Doctrine\DBAL\Schema\Schema;

/**
* Auto-generated Migration: Please modify to your needs!
*/
class Version20150708104025 extends AbstractMigration
{
/**
* @param Schema $schema
*/
public function up(Schema $schema)
{
// this up() migration is auto-generated, please modify it to your needs
$this->abortIf($this->connection->getDatabasePlatform()->getName() != 'mysql', 'Migration can only be executed safely on \'mysql\'.');

$this->addSql('ALTER TABLE sylius_product ADD available_until DATETIME DEFAULT NULL');
$this->addSql('ALTER TABLE sylius_product_variant ADD available_until DATETIME DEFAULT NULL');
}

/**
* @param Schema $schema
*/
public function down(Schema $schema)
{
// this down() migration is auto-generated, please modify it to your needs
$this->abortIf($this->connection->getDatabasePlatform()->getName() != 'mysql', 'Migration can only be executed safely on \'mysql\'.');

$this->addSql('ALTER TABLE sylius_product DROP available_until');
$this->addSql('ALTER TABLE sylius_product_variant DROP available_until');
}
}
8 changes: 8 additions & 0 deletions src/Sylius/Bundle/ProductBundle/Form/Type/VariantType.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ public function buildForm(FormBuilderInterface $builder, array $options)
'time_widget' => 'text',
'label' => 'sylius.form.product_variant.available_on'
))
->add('availableUntil', 'datetime', array(
'date_format' => 'y-M-d',
'date_widget' => 'choice',
'time_widget' => 'text',
'label' => 'sylius.form.product_variant.available_until',
'empty_value' => '-',
'required' => false
))
;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
<gedmo:versioned />
</field>

<field name="availableUntil" column="available_until" type="datetime" nullable="true">
<gedmo:versioned />
</field>

<many-to-one field="archetype" target-entity="Sylius\Component\Product\Model\ArchetypeInterface">
<join-column name="archetype_id" referenced-column-name="id" nullable="true" />
</many-to-one>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

<mapped-superclass name="Sylius\Component\Product\Model\Variant" table="sylius_product_variant">
<field name="availableOn" column="available_on" type="datetime" />
<field name="availableUntil" column="available_until" type="datetime" nullable="true" />
</mapped-superclass>

</doctrine-mapping>
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ sylius:
available_on: Dostupný od
meta_keywords: Meta klíčová slova
meta_description: Meta popis
product_variant:
available_on: Dostupné od
available_until: Dostupné do
archetype:
name: Jméno
properties: Vlastnosti
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ sylius:
meta_description: Meta description
product_variant:
available_on: Available on
available_until: Available until
product_archetype:
name: Name
parent: Parent
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -263,8 +263,11 @@ sylius:
name: Jméno
updated_at: Poslední aktualizace
product:
available_now: Aktuálně dostupný
available_on: Dostupný od
available_on_demand: K dispozici na požádání
available_unlimited: Neomezeno
available_until: Dostupný do
categorization: Kategorizace
create_header: Nový produkt
description: Popis
Expand Down Expand Up @@ -337,6 +340,9 @@ sylius:
shipping_address: Doručovací adresa
username: Uživatelské jméno
variant:
available_on: Dostupnost od
available_unlimited: Neomezená
available_until: Dostupnost do
available_on_demand: K dispozici na požádání
last_update: Poslední aktualizace
options: Možnosti
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -594,8 +594,11 @@ sylius:
updated_at: Last update
product:
add_attribute: Add attribute
available_now: Available now
available_on: Available on
available_on_demand: Available on demand
available_unlimited: Unlimited
available_until: Available until
categorization: Categorization
contain_variants: Contain Variants
create: Create product
Expand Down Expand Up @@ -850,6 +853,10 @@ sylius:
variant:
add_image: Add image
availability: Availability
available_on: Available since
available_unlimited: Unlimited
available_until: Available until
available: Available
available_on_demand: Available on demand
create: Create variant
create_header: New product variant
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
<hr />
<div class="row">
<div class="col-md-6">
{{ form_label(form.masterVariant.availableOn) }}
{{ form_widget(form.masterVariant.availableOn, {'label': false}) }}
{{ form_row(form.masterVariant.availableOn) }}
{{ form_row(form.masterVariant.availableUntil) }}
{{ form_row(form.masterVariant.availableOnDemand) }}
{{ form_row(form.masterVariant.onHand) }}
<hr />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,17 @@
<td>{{ 'sylius.product.name'|trans }}</td>
<td>{{ product.name }}</td>
</tr>
<tr>
<td>{{ 'sylius.product.available_now'|trans }}</td>
<td><span class="label label-{{ product.available ? 'success' : 'danger' }}">{{ product.available ? 'sylius.yes'|trans : 'sylius.no'|trans }}</span></td>
</tr>
<tr>
<td>{{ 'sylius.product.available_on'|trans }}</td>
<td><span class="label label-{{ product.available ? 'success' : 'danger' }}">{{ product.availableOn|date }}</span></td>
<td>{{ product.availableOn|date }}</td>
</tr>
<tr>
<td>{{ 'sylius.product.available_until'|trans }}</td>
<td>{{ product.availableUntil == null ? 'sylius.product.available_unlimited'|trans : product.availableUntil|date }}</td>
</tr>
<tr>
<td>{{ 'sylius.product.options'|trans }}</td>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@
<div class="col-lg-10">
{{ form_row(form.availableOn.date, {'label': false}) }}
{{ form_row(form.availableOn.time, {'label': false}) }}
{{ form_errors(form.availableOn) }}
</div>
</div>
<div class="form-group">
{{ form_label(form.availableUntil) }}
<div class="col-lg-10">
{{ form_row(form.availableUntil.date, {'label': false}) }}
{{ form_row(form.availableUntil.time, {'label': false}) }}
{{ form_errors(form.availableUntil) }}
</div>
</div>
{{ form_row(form.price) }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
<th>id</th>
<th>{{ 'sylius.variant.sku'|trans }}</th>
<th></th>
<th>{{ 'sylius.variant.availability'|trans }}</th>
<th>{{ 'sylius.variant.available_on'|trans }}</th>
<th>{{ 'sylius.variant.available_until'|trans }}</th>
<th>{{ 'sylius.variant.last_update'|trans }}</th>
<th>{{ 'sylius.variant.options'|trans }}</th>
<th>{{ 'sylius.variant.available_on_demand'|trans }}</th>
Expand All @@ -29,7 +30,14 @@
<img class="img-polaroid" src="{{ variant.images.offsetGet(0).path|imagine_filter('sylius_small') }}" />
{% endif %}
</td>
<td><span class="label label-{{ variant.available ? 'success' : 'important' }}">{{ variant.availableOn|date }}</span></td>
<td>
<span class="label label-{{ variant.available ? 'success' : 'important' }}">
{{ variant.availableOn|date }}
</span>
</td>
<td>
{{ variant.availableUntil == null ? 'sylius.variant.available_unlimited'|trans : variant.availableUntil|date }}
</td>
<td>{{ product.updatedAt|date }}</td>
<td>
<ul>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,9 @@
{% endif %}
</div>
<div class="col-md-6 pull-right">
{% if not product.hasVariants and not sylius_inventory_is_available(product.masterVariant) %}
{% if not product.available %}
<span class="label label-warning">{{ 'sylius.product.unavailable'|trans }}</span>
{% elseif not product.hasVariants and not sylius_inventory_is_available(product.masterVariant) %}
<span class="label label-warning">{{ 'sylius.out_of_stock'|trans }}</span>
{% else %}
{{ form_row(form.quantity, {'attr': {'class': 'center-text'}, 'empty_value': '1'}) }}
Expand Down
59 changes: 46 additions & 13 deletions src/Sylius/Component/Product/Model/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Sylius\Component\Product\Model;

use DateTime;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Sylius\Component\Archetype\Model\ArchetypeInterface as BaseArchetypeInterface;
Expand Down Expand Up @@ -42,10 +43,18 @@ class Product extends AbstractTranslatable implements ProductInterface
/**
* Available on.
*
* @var \DateTime
* @var DateTime
*/
protected $availableOn;


/**
* Available until.
*
* @var DateTime
*/
protected $availableUntil;

/**
* Attributes.
*
Expand All @@ -70,21 +79,21 @@ class Product extends AbstractTranslatable implements ProductInterface
/**
* Creation time.
*
* @var \DateTime
* @var DateTime
*/
protected $createdAt;

/**
* Last update time.
*
* @var \DateTime
* @var DateTime
*/
protected $updatedAt;

/**
* Deletion time.
*
* @var \DateTime
* @var DateTime
*/
protected $deletedAt;

Expand All @@ -94,11 +103,12 @@ class Product extends AbstractTranslatable implements ProductInterface
public function __construct()
{
parent::__construct();
$this->availableOn = new \DateTime();
$this->availableOn = new DateTime();
$this->availableUntil = null;
$this->attributes = new ArrayCollection();
$this->variants = new ArrayCollection();
$this->options = new ArrayCollection();
$this->createdAt = new \DateTime();
$this->createdAt = new DateTime();
}

/**
Expand Down Expand Up @@ -222,7 +232,14 @@ public function setMetaDescription($metaDescription)
*/
public function isAvailable()
{
return new \DateTime() >= $this->availableOn;
$now = new DateTime();
return
$now >= $this->availableOn
&& (
$this->availableUntil === null ?
true :
( $now < $this->availableUntil )
);
}

/**
Expand All @@ -236,13 +253,29 @@ public function getAvailableOn()
/**
* {@inheritdoc}
*/
public function setAvailableOn(\DateTime $availableOn = null)
public function setAvailableOn(DateTime $availableOn = null)
{
$this->availableOn = $availableOn;

return $this;
}

/**
* {@inheritdoc}
*/
public function getAvailableUntil()
{
return $this->availableUntil;
}

/**
* {@inheritdoc}
*/
public function setAvailableUntil(DateTime $availableUntil = null)
{
$this->availableUntil = $availableUntil;
return $this;
}

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -499,7 +532,7 @@ public function getCreatedAt()
/**
* {@inheritdoc}
*/
public function setCreatedAt(\DateTime $createdAt)
public function setCreatedAt(DateTime $createdAt)
{
$this->createdAt = $createdAt;

Expand All @@ -517,7 +550,7 @@ public function getUpdatedAt()
/**
* {@inheritdoc}
*/
public function setUpdatedAt(\DateTime $updatedAt)
public function setUpdatedAt(DateTime $updatedAt)
{
$this->updatedAt = $updatedAt;

Expand All @@ -529,7 +562,7 @@ public function setUpdatedAt(\DateTime $updatedAt)
*/
public function isDeleted()
{
return null !== $this->deletedAt && new \DateTime() >= $this->deletedAt;
return null !== $this->deletedAt && new DateTime() >= $this->deletedAt;
}

/**
Expand All @@ -543,7 +576,7 @@ public function getDeletedAt()
/**
* {@inheritdoc}
*/
public function setDeletedAt(\DateTime $deletedAt)
public function setDeletedAt(DateTime $deletedAt)
{
$this->deletedAt = $deletedAt;

Expand Down
Loading

0 comments on commit dad431e

Please sign in to comment.