Skip to content
This repository has been archived by the owner on Feb 16, 2021. It is now read-only.

Commit

Permalink
Support for products weight and weight-based shipping price strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
jvelo committed Aug 1, 2013
1 parent 10321e5 commit 75b59db
Show file tree
Hide file tree
Showing 21 changed files with 289 additions and 28 deletions.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
ALTER TABLE purchase_order ADD COLUMN shipping numeric(18,4);
ALTER TABLE purchase_order ADD COLUMN shipping numeric(18,4);
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ public class ProductRepresentation

private BigDecimal price;

private BigDecimal weight;

private Integer stock;

@JsonInclude(JsonInclude.Include.NON_NULL)
Expand Down Expand Up @@ -71,6 +73,7 @@ public ProductRepresentation(Product product, List<EntityReferenceRepresentation
this.description = product.getDescription();
this.onShelf = product.getOnShelf();
this.price = product.getUnitPrice();
this.weight = product.getWeight();
this.stock = product.getStock();

this.href = Resource.API_ROOT_PATH + ProductEntity.PATH + "/" + this.slug;
Expand Down Expand Up @@ -141,6 +144,16 @@ public void setPrice(BigDecimal price)
this.price = price;
}

public BigDecimal getWeight()
{
return weight;
}

public void setWeight(BigDecimal weight)
{
this.weight = weight;
}

public List<AddonRepresentation> getAddons()
{
return addons;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ public Response updateProduct(@PathParam("slug") String slug,
product.setModel(updatedProductRepresentation.getModel());
product.setOnShelf(updatedProductRepresentation.getOnShelf());
product.setPrice(updatedProductRepresentation.getPrice());
product.setWeight(updatedProductRepresentation.getWeight());
product.setStock(updatedProductRepresentation.getStock());
product.setAddons(addonsRepresentationUnmarshaller.unmarshall(updatedProductRepresentation.getAddons()));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import javax.validation.Valid;

import org.mayocat.configuration.Configurable;
import org.mayocat.shop.catalog.model.WeightUnit;

import com.fasterxml.jackson.annotation.JsonProperty;

Expand All @@ -11,7 +12,6 @@
*/
public class ProductsSettings
{

@Valid
@JsonProperty
private Configurable<Boolean> stock = new Configurable<Boolean>(true);
Expand All @@ -20,6 +20,14 @@ public class ProductsSettings
@JsonProperty
private Configurable<Boolean> collections = new Configurable<Boolean>(true);

@Valid
@JsonProperty
private Configurable<Boolean> weight = new Configurable<Boolean>(true);

@Valid
@JsonProperty
private Configurable<WeightUnit> weightUnit = new Configurable<WeightUnit>(WeightUnit.KILOGRAM);

public Configurable<Boolean> getCollections()
{
return this.collections;
Expand All @@ -29,4 +37,14 @@ public Configurable<Boolean> getStock()
{
return stock;
}

public Configurable<Boolean> getWeight()
{
return weight;
}

public Configurable<WeightUnit> getWeightUnit()
{
return weightUnit;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ public class Product extends AbstractLocalizedEntity implements HasAddons, HasMo

private BigDecimal price;

private BigDecimal weight;

private Integer stock;

@DoNotIndex
Expand Down Expand Up @@ -128,6 +130,16 @@ public void setPrice(BigDecimal price)
this.price = price;
}

public BigDecimal getWeight()
{
return weight;
}

public void setWeight(BigDecimal weight)
{
this.weight = weight;
}

@Override
public PerhapsLoaded<List<Addon>> getAddons()
{
Expand Down Expand Up @@ -189,6 +201,7 @@ public boolean equals(Object obj)
&& Objects.equal(this.onShelf, other.onShelf)
&& Objects.equal(this.price, other.price)
&& Objects.equal(this.stock, other.stock)
&& Objects.equal(this.weight, other.weight)
&& Objects.equal(this.featuredImageId, other.featuredImageId);
}

Expand All @@ -201,7 +214,8 @@ public int hashCode()
this.onShelf,
this.price,
this.stock,
this.featuredImageId
this.featuredImageId,
this.weight
);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package org.mayocat.shop.catalog.model;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;

/**
* @version $Id$
*/
public enum WeightUnit
{
KILOGRAM("kg"),
GRAM("g"),
OUNCE("oz"),
POUND("lb");

WeightUnit(String smybol)
{
this.symbol = smybol;
}

private String symbol;

public String getSymbol()
{
return symbol;
}

@JsonValue
public String toJson()
{
return getSymbol();
}

@JsonCreator
public static WeightUnit fromJson(String text)
{
for (WeightUnit unit : values()) {
if (unit.getSymbol().equalsIgnoreCase(text)) {
return unit;
}
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public Product map(int index, ResultSet resultSet, StatementContext statementCon
product.setDescription(resultSet.getString("description"));
product.setOnShelf(resultSet.getBoolean("on_shelf"));
product.setPrice(resultSet.getBigDecimal("price"));
product.setWeight(resultSet.getBigDecimal("weight"));
UUID featuredImageId = (UUID) resultSet.getObject("featured_image_id");
if (featuredImageId != null) {
product.setFeaturedImageId(featuredImageId);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
group ProductDAO;

createProduct() ::= <<
INSERT INTO product (entity_id, position, title, model, on_shelf, description, price, stock, featured_image_id)
INSERT INTO product (entity_id, position, title, model, on_shelf, description, price, weight, stock, featured_image_id)
VALUES (:product.id,
:position,
:product.title,
:product.model,
:product.onShelf,
:product.description,
:product.price,
:product.weight,
:product.stock,
:product.featuredImageId)
>>
Expand All @@ -20,6 +21,7 @@ updateProduct() ::= <<
description = :product.description,
on_shelf = :product.onShelf,
price = :product.price,
weight = :product.weight,
stock = :product.stock,
featured_image_id = :product.featuredImageId
WHERE entity_id = :product.id
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE product ADD COLUMN weight numeric;
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,10 @@ angular.module('product', ['ngResource'])
}
}
});
configurationService.get("catalog", function (catalogConfiguration) {
$scope.hasWeight = catalogConfiguration.products.weight;
$scope.weightUnit = catalogConfiguration.products.weightUnit;
});
}

// Initialize existing product or new product
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,8 +242,9 @@ angular.module('settings', ['ngResource'])
});
});

configurationService.getSettings("catalog.currencies.main", function (mainCurrency) {
$scope.mainCurrency = mainCurrency.value;
configurationService.get("catalog", function(catalogSettings){
$scope.mainCurrency = catalogSettings.currencies.main;
$scope.weightUnit = catalogSettings.products.weightUnit;
});

$scope.loadCarriers();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,11 @@
* duration, pricing, etc.
*/
return {
templateUrl: "partials/directives/carrierSummary.html?v=2", // Defeat browser cache http://git.io/6dPuFQ
templateUrl: "partials/directives/carrierSummary.html?v=3", // Defeat browser cache http://git.io/6dPuFQ
scope: {
carrier: '=',
mainCurrency: '='
mainCurrency: '=',
weightUnit: '='
},
restrict: 'E'
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ <h4>{{carrier.title}}</h4>
</thead>
<tr ng-repeat="rule in carrier.rules">
<td>
<span ng-show="carrier.strategy == 'weight'">{{rule.fromValue}} - {{rule.upToValue}} kg</span>
<span ng-show="carrier.strategy == 'weight'">{{rule.fromValue}} - {{rule.upToValue}} {{weightUnit}}</span>
<span ng-show="carrier.strategy == 'price'">{{rule.fromValue}} - {{rule.upToValue}} {{mainCurrency}}</span>
</td>
<td>{{rule.price}} {{mainCurrency}}</td>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,14 @@ <h3>Price</h3>
</div>
</div>

<div class="block" ng-show="!isNew() && hasWeight">
<h3>Weight</h3>

<div>
<input type="text" placeholder="Weight" ng-model="product.weight" /> {{weightUnit}}
</div>
</div>

<div class="block" ng-show="!isNew()">
<h3>Stock</h3>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,34 @@ <h1 class="">Catalog settings</h1>
ng-model="settings.catalog.products.stock.value"/>
</label>
</div>

<!-- Weight -->
<div ng-show="isVisible('catalog.products.weight')"
ng-class="{default: isDefaultValue('catalog.products.weight')}">

<label class="checkbox">
have weight (keep this checked if you want to calculate shipping costs by weight)
<input type="checkbox"
id="weightEnabled"
ng-model="settings.catalog.products.weight.value"/>
</label>
</div>

<div ng-show="isVisible('catalog.products.weightUnit')"
ng-class="{default: isDefaultValue('catalog.products.weightUnit')}">


<dd>
<label class="checkbox">
weight unit

<select ng-disabled="!settings.catalog.products.weight.value"
ng-model="settings.catalog.products.weightUnit.value"
ng-options="value.unit as (value.label + ' (' + value.unit + ')') for value in [{'unit':'kg','label':'Kilogram'}, {'unit':'g','label':'Gram'}, {'unit':'oz','label':'Ounce'}, {'unit':'lb','label':'Pound'} ]">
</select>
</label>
</dd>
</div>
</div>

</fieldset>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ <h1 class="">Shipping settings</h1>
<div><a ng-click="editCarrier(carrier);">Edit zone</a></div>
<div><a ng-click="deleteCarrier(carrier);">Delete zone</a></div>
</div>
<carrier-summary carrier="carrier" main-currency="mainCurrency"/>
<carrier-summary carrier="carrier" main-currency="mainCurrency" weight-unit="weightUnit"/>
</li>
</ul>

Expand Down Expand Up @@ -172,7 +172,7 @@ <h3>Pricing</h3>
From <input type="text" class="span1" value="{{fromValue(editedCarrier.rules, $index - 1)}}" disabled />
up to <input type="text" class="span1" ng-model="rule.upToValue" />

<span ng-show="editedCarrier.strategy == 'weight'">kg</span>
<span ng-show="editedCarrier.strategy == 'weight'"> {{weightUnit}} </span>
<span ng-show="editedCarrier.strategy == 'price'"> {{mainCurrency}}</span>

<span class="alert-error alert" ng-show="rule.upToValue < fromValue(editedCarrier.rules, $index - 1)">
Expand All @@ -189,7 +189,7 @@ <h3>Pricing</h3>
<td>
From <input type="text" class="span1" value="" disabled/>
up to <input type="text" class="span1" disabled/>
<span ng-show="editedCarrier.strategy == 'weight'">kg</span>
<span ng-show="editedCarrier.strategy == 'weight'"> {{weightUnit}} </span>
<span ng-show="editedCarrier.strategy == 'price'"> {{mainCurrency}}</span>
</td>
<td>
Expand All @@ -202,7 +202,7 @@ <h3>Pricing</h3>
</tr>
<tr ng-show="editedCarrier.strategy == 'weight'" class="hasInput">
<td>
Additional kg
Additional {{weightUnit}}
</td>
<td ng-repeat="carrier in [editedCarrier]">
<!-- trick to have currency being displayed correctly.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package org.mayocat.shop.shipping.strategy;

import java.math.BigDecimal;
import java.util.Map;

import org.mayocat.shop.catalog.model.Purchasable;
import org.mayocat.shop.shipping.StrategyPriceCalculator;
import org.mayocat.shop.shipping.model.Carrier;
import org.mayocat.shop.shipping.model.CarrierRule;

/**
* @version $Id$
*/
public abstract class AbstractValueBasedStrategyPriceCalculator implements StrategyPriceCalculator
{
@Override
public BigDecimal getPrice(Carrier carrier, Map<Purchasable, Long> items)
{
BigDecimal price = BigDecimal.ZERO;
BigDecimal value = getValue(items);

for (CarrierRule rule : carrier.getRules()) {
price = rule.getPrice();
if (value.compareTo(rule.getUpToValue()) <= 0) {
break;
}
}

return price;
}

protected abstract BigDecimal getValue(Map<Purchasable, Long> items);
}
Loading

0 comments on commit 75b59db

Please sign in to comment.