hedera-web/web/forms/admin/shelves/shelves.js

412 lines
9.9 KiB
JavaScript
Raw Normal View History

2015-09-22 07:20:47 +00:00
Vn.Shelves = new Class
({
Extends: Vn.Module
2015-11-05 07:30:19 +00:00
2015-09-22 07:20:47 +00:00
,activate: function ()
{
this.$('date').value = new Date ();
2015-10-23 23:23:19 +00:00
}
2015-11-05 07:30:19 +00:00
,onConfigChange: function ()
2015-10-23 23:23:19 +00:00
{
2015-11-05 07:30:19 +00:00
var c = this.$('config');
this.$('warehouse').value = c.get ('warehouse_id');
this.$('shelf').value = c.get ('shelf_id');
this.$('reign').value = c.get ('reino_id');
this.$('family').value = c.get ('family_id');
this.$('filter').value = c.get ('name_prefix');
this.$('max-amount').value = c.get ('max_amount');
this.$('show-packing').value = c.get ('show_packing');
this.$('stack').value = c.get ('stack');
this.$('report-title').value = c.get ('name');
2015-09-22 07:20:47 +00:00
}
,onPreviewClick: function ()
{
2015-10-23 23:23:19 +00:00
var batch = new Sql.Batch ();
2015-11-05 07:30:19 +00:00
batch.addParams ({
'shelf': this.$('shelf'),
'wh': this.$('warehouse'),
'date': this.$('date'),
'family': this.$('family'),
'filter': this.$('filter')
});
2015-10-23 23:23:19 +00:00
2015-10-21 23:42:52 +00:00
var report = new Vn.ShelvesReport ({conn: this.conn});
report.setParams (
this.$('report-title').value,
this.$('max-amount').value,
2015-10-23 23:23:19 +00:00
this.$('show-packing').value,
this.$('stack').value,
batch
2015-10-21 23:42:52 +00:00
);
report.open ();
2015-09-22 07:20:47 +00:00
}
});
2015-10-21 23:42:52 +00:00
Vn.ShelvesReport = new Class
({
Extends: Vn.Report
,nItem: -1
2015-11-05 07:30:19 +00:00
,nColors: 5
2015-10-21 23:42:52 +00:00
2015-10-23 23:23:19 +00:00
,setParams: function (title, maxAmount, showPacking, stack, batch)
2015-10-21 23:42:52 +00:00
{
this.title = title;
this.maxAmount = maxAmount;
this.batch = batch;
2015-10-23 23:23:19 +00:00
this.showPacking = showPacking;
this.stack = stack;
2015-10-21 23:42:52 +00:00
}
,open: function ()
{
var query =
2015-11-05 07:30:19 +00:00
'SELECT id, name, width, height, depth, max_height, tray_height, '+
2015-10-21 23:42:52 +00:00
'first_tray_elevation, tray_density, vspacing, hspacing '+
2015-11-05 07:30:19 +00:00
'FROM shelf WHERE id = #shelf; '+
2015-10-23 23:23:19 +00:00
'CALL item_organizer (#wh, #date, #family, #filter)';
2015-10-21 23:42:52 +00:00
this.conn.execQuery (query, this.onQueryExec.bind (this), this.batch);
}
,onQueryExec: function (resultSet)
{
// Fetch query data
var res = resultSet.fetchResult ();
res.next ();
2015-11-05 07:30:19 +00:00
var maxWidth = 170;
var maxHeight = 200;
var scale = maxWidth / res.get ('width');
if (res.get ('max_height') * scale > maxHeight)
scale = maxHeight / res.get ('max_height');
2015-10-21 23:42:52 +00:00
var shelf = this.shelf =
{
width: res.get ('width') * scale
,height: res.get ('height') * scale
2015-11-05 07:30:19 +00:00
,depth: res.get ('depth') * scale
2015-10-21 23:42:52 +00:00
,maxHeight: res.get ('max_height') * scale
,trayHeight: res.get ('tray_height') * scale
,firstTrayElevation: res.get ('first_tray_elevation') * scale
,trayDensity: res.get ('tray_density') * scale
,vspacing: res.get ('vspacing') * scale
,hspacing: res.get ('hspacing') * scale
};
var items = [];
2015-10-23 23:23:19 +00:00
var remainings = [];
2015-10-21 23:42:52 +00:00
var res = resultSet.fetchResult ();
2015-11-05 07:30:19 +00:00
if (res.data.length == 0)
{
Htk.Toast.showError (_('No items found, check that all fields are correct'));
return;
}
var boxScale = scale * 10;
2015-10-21 23:42:52 +00:00
while (res.next ())
2015-11-05 07:30:19 +00:00
if (!this.maxAmount || res.get ('etiquetas') <= this.maxAmount)
2015-10-23 23:23:19 +00:00
{
2015-10-21 23:42:52 +00:00
items.push ({
2015-11-05 07:30:19 +00:00
id: res.get ('Id_Article')
,name: res.get ('Article')
2015-10-23 23:23:19 +00:00
,packing: res.get ('packing')
2015-10-21 23:42:52 +00:00
,amount: res.get ('etiquetas')
2015-11-05 07:30:19 +00:00
,boxHeight: res.get ('z') * boxScale
,boxWidth: res.get ('x') * boxScale
,boxDepth: res.get ('y') * boxScale
2015-10-21 23:42:52 +00:00
});
2015-10-23 23:23:19 +00:00
}
else
{
remainings.push ({
2015-11-05 07:30:19 +00:00
id: res.get ('Id_Article')
,name: res.get ('Article')
2015-10-23 23:23:19 +00:00
,packing: res.get ('packing')
,amount: res.get ('etiquetas')
});
}
2015-10-21 23:42:52 +00:00
// Intializes the allocator
alloc = new Vn.Allocator ();
alloc.items = items;
alloc.shelfFunc = this.drawShelf.bind (this);
alloc.boxFunc = this.drawBox.bind (this);
2015-10-23 23:23:19 +00:00
alloc.stack = this.stack;
2015-10-21 23:42:52 +00:00
alloc.nTrays = Math.ceil (
(shelf.height - shelf.firstTrayElevation) /
(shelf.trayHeight + shelf.trayDensity)
);
2015-11-05 07:30:19 +00:00
alloc.width = shelf.width - shelf.hspacing * 2;
alloc.depth = shelf.depth;
2015-10-21 23:42:52 +00:00
alloc.trayHeight = shelf.trayHeight - shelf.vspacing;
alloc.topTrayHeight = shelf.maxHeight - shelf.vspacing
- shelf.firstTrayElevation - (alloc.nTrays - 1) * shelf.trayHeight;
// Opens the report
2015-11-05 07:30:19 +00:00
if (!this.createWindow ('shelves'))
return;
2015-10-23 23:23:19 +00:00
// Remaining amount
if (remainings.length > 0)
{
var sheet = this.doc.createElement ('div');
sheet.className = 'sheet';
this.doc.body.appendChild (sheet);
var title = this.doc.createElement ('h1');
2015-11-05 07:30:19 +00:00
title.className = 'title';
title.appendChild (this.doc.createTextNode (this.title));
2015-10-23 23:23:19 +00:00
sheet.appendChild (title);
2015-11-05 07:30:19 +00:00
var subtitle = this.doc.createElement ('h2');
subtitle.className = 'subtitle';
subtitle.appendChild (this.doc.createTextNode (_('Pallets')));
sheet.appendChild (subtitle);
2015-10-23 23:23:19 +00:00
var ul = this.doc.createElement ('ul');
sheet.appendChild (ul);
for (var i = 0; i < remainings.length; i++)
{
var li = this.doc.createElement ('li');
ul.appendChild (li);
2015-11-05 07:30:19 +00:00
var span = this.doc.createElement ('span');
span.className = 'item-id';
span.appendChild (this.doc.createTextNode (remainings[i].id.toLocaleString ()));
li.appendChild (span);
2015-10-23 23:23:19 +00:00
var span = this.doc.createElement ('span');
span.className = 'item';
span.appendChild (this.doc.createTextNode (remainings[i].name));
li.appendChild (span);
if (this.showPacking)
span.appendChild (this.doc.createTextNode (' '+ remainings[i].packing));
var span = this.doc.createElement ('span');
span.className = 'amount';
span.appendChild (this.doc.createTextNode (remainings[i].amount));
li.appendChild (span);
}
}
// Draws the shelves
2015-10-21 23:42:52 +00:00
alloc.run ();
2015-11-05 07:30:19 +00:00
this.drawShelfEnding ();
2015-10-21 23:42:52 +00:00
}
2015-11-05 07:30:19 +00:00
,drawShelf: function (allocator, item)
2015-10-21 23:42:52 +00:00
{
var shelf = this.shelf;
var sheet = this.doc.createElement ('div');
sheet.className = 'sheet';
2015-10-23 23:23:19 +00:00
this.doc.body.appendChild (sheet);
2015-10-21 23:42:52 +00:00
// Draws the title
2015-11-05 07:30:19 +00:00
var pageNumber = this.doc.createElement ('h1');
pageNumber.className = 'page-number';
pageNumber.appendChild (this.doc.createTextNode (allocator.currentShelf + 1));
sheet.appendChild (pageNumber);
2015-10-21 23:42:52 +00:00
var title = this.doc.createElement ('h1');
title.className = 'title';
title.appendChild (this.doc.createTextNode (this.title));
sheet.appendChild (title);
2015-11-05 07:30:19 +00:00
var subtitle = this.doc.createElement ('h2');
subtitle.className = 'subtitle';
subtitle.appendChild (this.doc.createTextNode (item.id.toLocaleString ()));
sheet.appendChild (subtitle);
this.drawShelfEnding ();
this.lastSubtitle = subtitle;
2015-10-21 23:42:52 +00:00
// Draws the shelf
var shelfDiv = this.shelfDiv = this.doc.createElement ('div');
shelfDiv.className = 'shelf';
2015-11-05 07:30:19 +00:00
shelfDiv.style.width = this.mm (shelf.width);
shelfDiv.style.height = this.mm (shelf.maxHeight);
2015-10-21 23:42:52 +00:00
sheet.appendChild (shelfDiv);
// Draws trays
var lastTrayY = shelf.firstTrayElevation;
if (shelf.trayHeight > 0)
while (lastTrayY + shelf.trayDensity < shelf.height)
{
var tray = this.doc.createElement ('div');
tray.className = 'tray';
2015-11-05 07:30:19 +00:00
tray.style.width = this.mm (shelf.width);
tray.style.height = this.mm (shelf.trayDensity);
tray.style.bottom = this.mm (lastTrayY);
2015-10-21 23:42:52 +00:00
shelfDiv.appendChild (tray);
lastTrayY += shelf.trayHeight + shelf.trayDensity;
}
}
2015-11-05 07:30:19 +00:00
,drawShelfEnding: function ()
2015-10-21 23:42:52 +00:00
{
2015-11-05 07:30:19 +00:00
if (this.lastSubtitle)
this.lastSubtitle.appendChild (
this.doc.createTextNode (' - '+ this.lastItem.id.toLocaleString ()));
}
,mm: function (size)
{
return size.toFixed (2) +'mm';
2015-10-21 23:42:52 +00:00
}
,drawBox: function (allocator, item, amount)
{
if (item.boxWidth == 0 || item.boxHeight == 0)
return;
var shelf = this.shelf;
var x = allocator.trayX + shelf.hspacing;
var y = allocator.trayY
+ shelf.firstTrayElevation + shelf.trayDensity
+ allocator.currentTray * (shelf.trayHeight + shelf.trayDensity);
var box = this.doc.createElement ('div');
box.className = 'box';
this.shelfDiv.appendChild (box);
2015-11-05 07:30:19 +00:00
box.style.left = this.mm (x);
box.style.bottom = this.mm (y);
box.style.width = this.mm (item.boxWidth);
box.style.height = this.mm (item.boxHeight);
2015-10-21 23:42:52 +00:00
if (amount == 0)
this.nItem++;
var nColor = this.nItem % this.nColors;
Vn.Node.addClass (box, 'color'+ nColor);
if (amount == 0 || allocator.firstShelfBox)
{
2015-11-05 07:30:19 +00:00
var fontSize = item.boxWidth / 5.2;
if (fontSize > item.boxHeight - 1)
fontSize = item.boxHeight - 1;
var boxLabel = this.doc.createElement ('div');
2015-10-23 23:23:19 +00:00
boxLabel.className = 'box-label';
2015-11-05 07:30:19 +00:00
boxLabel.style.fontSize = this.mm (fontSize);
boxLabel.appendChild (this.doc.createTextNode (item.id.toLocaleString ()));
2015-10-21 23:42:52 +00:00
box.appendChild (boxLabel);
}
2015-11-05 07:30:19 +00:00
this.lastItem = item;
2015-10-21 23:42:52 +00:00
}
});
Vn.Allocator = new Class
({
2015-11-05 07:30:19 +00:00
addShelf: function (item)
2015-10-21 23:42:52 +00:00
{
this.currentShelf++;
this.firstShelfBox = true;
if (this.shelfFunc)
2015-11-05 07:30:19 +00:00
this.shelfFunc (this, item);
2015-10-21 23:42:52 +00:00
}
2015-11-05 07:30:19 +00:00
,addTray: function (item)
2015-10-21 23:42:52 +00:00
{
if (this.currentTray <= 0)
{
2015-11-05 07:30:19 +00:00
this.addShelf (item);
2015-10-21 23:42:52 +00:00
this.currentTray = this.nTrays - 1;
}
else
this.currentTray--;
this.trayX = 0;
}
2015-11-05 07:30:19 +00:00
,addColumn: function (item)
2015-10-21 23:42:52 +00:00
{
2015-11-05 07:30:19 +00:00
if (this.trayX + this.columnWidth + item.boxWidth > this.width
2015-10-21 23:42:52 +00:00
|| this.currentTray == -1)
2015-11-05 07:30:19 +00:00
this.addTray (item);
2015-10-21 23:42:52 +00:00
else
this.trayX += this.columnWidth;
this.trayY = 0;
2015-11-05 07:30:19 +00:00
this.columnWidth = item.boxWidth;
this.lastBoxWidth = item.boxWidth;
2015-10-21 23:42:52 +00:00
}
,addBox: function (item, amount)
{
var trayHeight = this.trayHeight;
if (this.currentTray == this.nTrays - 1)
trayHeight = this.topTrayHeight;
if (this.trayY + item.boxHeight > trayHeight
2015-10-23 23:23:19 +00:00
|| item.boxWidth > this.lastBoxWidth
|| (!this.stack && amount == 0))
2015-11-05 07:30:19 +00:00
this.addColumn (item);
2015-10-21 23:42:52 +00:00
if (this.boxFunc)
this.boxFunc (this, item, amount);
this.trayY += item.boxHeight;
if (item.boxWidth < this.lastBoxWidth)
this.lastBoxWidth = item.boxWidth;
}
,run: function ()
{
this.firstShelfBox = false;
this.currentShelf = -1;
this.currentTray = -1;
this.columnWidth = 0;
this.lastBoxWidth = 0;
this.trayX = 0;
this.trayY = 0;
2015-10-23 23:23:19 +00:00
this.remaining = false;
2015-10-21 23:42:52 +00:00
for (var i = 0; i < this.items.length; i++)
{
var item = this.items[i];
2015-11-05 07:30:19 +00:00
var boxIncrement = Math.floor (this.depth / item.boxDepth);
if (boxIncrement < 1)
boxIncrement = 1;
2015-10-21 23:42:52 +00:00
2015-11-05 07:30:19 +00:00
for (var amount = 0; amount < item.amount; amount += boxIncrement)
2015-10-21 23:42:52 +00:00
{
this.addBox (item, amount);
this.firstShelfBox = false;
}
}
return this.currentShelf + 1;
}
});