Commit a1967744 authored by Cédric Houbart's avatar Cédric Houbart
Browse files

Barcode generation, support for barcode in products, better product form

parent 74cceca4
......@@ -41,5 +41,38 @@ case 'category':
echo file_get_contents(ABSPATH . "/templates/" . $config['template'] . "/img/default_category.png");
}
break;
case 'barcode':
require_once(ABSPATH . "/lib/barcode-master/php-barcode.php");
$font = "./lib/barcode-master/NOTTB___.TTF";
$fontSize = 10; // GD1 in px ; GD2 in point
$marge = 2; // between barcode and hri in pixel
$x = 95; // barcode center
$y = 25; // barcode center
$height = 50; // barcode height in 1D ; module size in 2D
$width = 2; // barcode height in 1D ; not use in 2D
$angle = 0; // rotation in degrees : nb : non horizontable barcode might not be usable because of pixelisation
$code = $_GET['code'];
$type = 'ean13';
$im = imagecreatetruecolor(190, 62);
$black = ImageColorAllocate($im,0x00,0x00,0x00);
$white = ImageColorAllocate($im,0xff,0xff,0xff);
$red = ImageColorAllocate($im,0xff,0x00,0x00);
$blue = ImageColorAllocate($im,0x00,0x00,0xff);
imagefilledrectangle($im, 0, 0, 190, 62, $white);
$data = \Barcode::gd($im, $black, $x, $y, $angle, $type,
array('code'=>$code), $width, $height);
if (isset($font)) {
$box = imagettfbbox($fontSize, 0, $font, $data['hri']);
$len = $box[2] - $box[0];
\Barcode::rotate(-$len / 2, ($data['height'] / 2) + $fontSize + $marge, $angle, $xt, $yt);
imagettftext($im, $fontSize, $angle, $x + $xt, $y + $yt, $black, $font, $data['hri']);
}
header('Content-type: image/gif');
imagegif($im);
imagedestroy($im);
break;
}
?>
......@@ -4,6 +4,7 @@
"Edit": "Edit",
"Delete": "Delete",
"Restore": "Restore",
"Generate": "Generate",
"Save": "Save",
"Changes saved": "Changes saved",
"Unable to save changes": "Unable to save changes",
......@@ -13,6 +14,7 @@
"Product.label": "Name",
"Product.reference": "Reference",
"Product.category": "Category",
"Product.barcode": "Barcode",
"Product.tax_cat": "VAT",
"Product.price_sell": "Sell price",
"Product.price_buy": "Buy price",
......
......@@ -4,6 +4,7 @@
"Edit": "Modifier",
"Delete": "Supprimer",
"Restore": "Restaurer",
"Generate": "Génerer",
"Save": "Enregistrer",
"Changes saved": "Modifications enregistrées",
"Unable to save changes": "Impossible d'enregistrer les modifications",
......@@ -13,6 +14,7 @@
"Product.label": "Désignation",
"Product.reference": "Référence",
"Product.category": "Categorie",
"Product.barcode": "Code barre",
"Product.tax_cat": "TVA",
"Product.price_sell": "Prix de vente",
"Product.price_buy": "Prix d'achat",
......
This diff is collapsed.
......@@ -39,7 +39,7 @@ if (isset($_POST['id'])) {
$img = "";
}
$prd = \Pasteque\Product::__build($_POST['id'], $_POST['reference'], $_POST['label'], $_POST['realsell'], $cat, $taxCat,
FALSE, FALSE, $_POST['price_buy'], NULL, NULL, $img);
FALSE, FALSE, $_POST['price_buy'], NULL, $_POST['barcode'], $img);
if (\Pasteque\ProductsService::update($prd)) {
$message = \i18n("Changes saved");
} else {
......@@ -59,7 +59,7 @@ if (isset($_POST['id'])) {
$img = NULL;
}
$prd = new \Pasteque\Product($_POST['reference'], $_POST['label'], $_POST['realsell'], $cat, $taxCat,
FALSE, FALSE, $_POST['price_buy'], NULL, NULL, $img);
FALSE, FALSE, $_POST['price_buy'], NULL, $_POST['barcode'], $img);
$id = \Pasteque\ProductsService::create($prd);
if ($id !== FALSE) {
$message = \i18n("Product saved. <a href=\"%s\">Go to the product page</a>.", PLUGIN_NAME, \Pasteque\get_module_url_action(PLUGIN_NAME, 'product_edit', array('id' => $id)));
......@@ -93,9 +93,25 @@ if ($error !== NULL) {
<form class="edit" action="<?php echo \Pasteque\get_current_url(); ?>" method="post" enctype="multipart/form-data">
<?php \Pasteque\form_hidden("edit", $product, "id"); ?>
<?php \Pasteque\form_input("edit", "Product", $product, "reference", "string", array("required" => true)); ?>
<fieldset>
<legend><?php \pi18n("Display", PLUGIN_NAME); ?></legend>
<?php \Pasteque\form_input("edit", "Product", $product, "label", "string", array("required" => true)); ?>
<?php \Pasteque\form_input("edit", "Product", $product, "category", "pick", array("model" => "Category")); ?>
<div class="row">
<label for="image"><?php \pi18n("Image"); ?></label>
<div style="display:inline-block">
<input type="hidden" id="clearImage" name="clearImage" value="0" />
<?php if ($product !== NULL && $product->image !== NULL) { ?>
<img id="img" class="image-preview" src="?<?php echo \Pasteque\URL_ACTION_PARAM; ?>=img&w=product&id=<?php echo $product->id; ?>" />
<a id="clear" href="" onClick="javascript:clearImage(); return false;"><?php \pi18n("Delete"); ?></a>
<a style="display:none" id="restore" href="" onClick="javascript:restoreImage(); return false;"><?php \pi18n("Restore"); ?></a><br />
<?php } ?>
<input type="file" name="image" />
</div>
</div>
</fieldset>
<fieldset>
<legend><?php \pi18n("Price", PLUGIN_NAME); ?></legend>
<?php \Pasteque\form_input("edit", "Product", $product, "tax_cat", "pick", array("model" => "TaxCategory")); ?>
<div class="row">
<label for="sellvat"><?php \pi18n("Sell price + taxes", PLUGIN_NAME); ?></label>
......@@ -103,7 +119,7 @@ if ($error !== NULL) {
</div>
<div class="row">
<label for="sell"><?php \pi18n("Product.price_sell"); ?></label>
<input type="hidden" id="realsell" name="realsell" <?php if ($product != NULL) echo 'value=' . $product->price_sell; ?> />
<input type="hidden" id="realsell" name="realsell" <?php if ($product != NULL) echo 'value="' . $product->price_sell. '"'; ?> />
<input id="sell" type="numeric" name="sell" value="<?php echo $price; ?>" />
</div>
<?php \Pasteque\form_input("edit", "Product", $product, "price_buy", "numeric"); ?>
......@@ -111,18 +127,19 @@ if ($error !== NULL) {
<label for="margin"><?php \pi18n("Margin", PLUGIN_NAME); ?></label>
<input id="margin" type="numeric" disabled="true" />
</div>
</fieldset>
<fieldset>
<legend><?php \pi18n("Referencing", PLUGIN_NAME); ?></legend>
<?php \Pasteque\form_input("edit", "Product", $product, "reference", "string", array("required" => true)); ?>
<div class="row">
<label for="image"><?php \pi18n("Image"); ?></label>
<div style="display:inline-block">
<input type="hidden" id="clearImage" name="clearImage" value="0" />
<?php if ($product !== NULL && $product->image !== NULL) { ?>
<img id="img" class="image-preview" src="?<?php echo \Pasteque\URL_ACTION_PARAM; ?>=img&w=product&id=<?php echo $product->id; ?>" />
<a id="clear" href="" onClick="javascript:clearImage(); return false;"><?php \pi18n("Delete"); ?></a>
<a style="display:none" id="restore" href="" onClick="javascript:restoreImage(); return false;"><?php \pi18n("Restore"); ?></a><br />
<?php } ?>
<input type="file" name="image" />
<label for="barcode"><?php \pi18n("Product.barcode"); ?></label>
<div style="display:inline-block; max-width:65%;">
<img id="barcodeImg" src="" />
<input id="barcode" type="text" name="barcode" <?php if ($product != NULL) echo 'value="' . $product->barcode . '"'; ?> />
<a class="btn" href="" onClick="javascript:generateBarcode(); return false;"><?php \pi18n("Generate"); ?></a>
</div>
</div>
</fieldset>
<div class="row actions">
<?php \Pasteque\form_send(); ?>
......@@ -180,5 +197,40 @@ if ($error !== NULL) {
jQuery("#clear").show();
jQuery("#restore").hide();
jQuery("#clearImage").val(0);
}
}
updateBarcode = function() {
var barcode = jQuery("#barcode").val();
var src = "?<?php echo \Pasteque\URL_ACTION_PARAM; ?>=img&w=barcode&code=" + barcode;
jQuery("#barcodeImg").attr("src", src);
}
updateBarcode();
jQuery("#barcode").change(updateBarcode);
generateBarcode = function() {
var first = Math.floor(Math.random() * 9) + 1;
var code = new Array();
code.push(first);
for (var i = 0; i < 11; i++) {
var num = Math.floor(Math.random() * 10);
code.push(num);
}
var checksum = 0;
for (var i = 0; i < code.length; i++) {
var weight = 1;
if (i % 2 == 1) {
weight = 3;
}
checksum = checksum + weight * code[i];
}
checksum = checksum % 10;
if (checksum != 0) {
checksum = 10 - checksum;
}
code.push(checksum);
var barcode = code.join("");
jQuery("#barcode").val(barcode);
updateBarcode();
}
</script>
......@@ -4,6 +4,9 @@
"%d products": "%d products",
"No product found": "No product found",
"Edit a product": "Edit a product",
"Display": "Display",
"Price": "Price",
"Referencing": "Referencing",
"Sell price + taxes": "Sell price + taxes",
"Margin": "Margin",
"Product saved. <a href=\"%s\">Go to the product page</a>.": "Product saved. <a href=\"%s\">Go to the product page</a>.",
......
......@@ -4,6 +4,9 @@
"%d products": "%d produits",
"No product found": "Aucun produit trouvé",
"Edit a product": "Édition d'un produit",
"Display": "Affichage",
"Price": "Prix",
"Referencing": "Referencement",
"Sell price + taxes": "Prix de vente TTC",
"Margin": "Marge",
"Product saved. <a href=\"%s\">Go to the product page</a>.": "Produit enregistré. <a href=\"%s\">Aller à la page du produit</a>.",
......
......@@ -95,6 +95,15 @@ form.edit {
padding: 15px;
}
form.edit fieldset {
border: 1px solid #CCC;
padding: 10px 0px;
margin-top: 15px;
}
form.edit fieldset legend {
font-weight: bold;
margin-left: 20px;
}
.edit .row {
width: 600px;
......@@ -103,7 +112,8 @@ form.edit {
.edit .row label {
display: inline-block;
width: 30%;
width: 27%;
margin-left: 3%;
}
.edit .row input,
.edit .row select {
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment