www/js/inv_miscs.js

Fri, 13 Sep 2019 20:24:31 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Fri, 13 Sep 2019 20:24:31 +0200
changeset 488
77f1617b6994
parent 395
463d64cce768
child 514
3c680d1dea35
permissions
-rw-r--r--

Fixed inventory databases race conditions

/*****************************************************************************
 * Copyright (C) 2014-2019
 *
 * Michiel Broek <mbroek at mbse dot eu>
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2, or (at your option) any
 * later version.
 *
 * BrewCloud is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with ThermFerm; see the file COPYING.  If not, write to the Free
 * Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
 *****************************************************************************/


function createDelElements() {
	$('#eventWindow').jqxWindow({
		theme: theme,
		position: { x: 490, y: 210 },
		width: 300,
		height: 175,
		resizable: false,
		isModal: true,
		modalOpacity: 0.4,
		okButton: $('#delOk'),
		cancelButton: $('#delCancel'),
		initContent: function () {
			$('#delOk').jqxButton({ template: "danger", width: '65px', theme: theme });
			$('#delCancel').jqxButton({ template: "success", width: '65px', theme: theme });
			$('#delCancel').focus();
		}
	});
	$('#eventWindow').jqxWindow('hide');
}


$(document).ready(function () {

	function calcTotal(cost, inventory) {

		$('#totval').val(cost * (inventory / 1000));
	}

	function prompts(isweight) {

		if (isweight) {
			$("#pmpt_cost").html('Prijs per kg:');
			$("#pmpt_inventory").html('Voorraad gram:');
		} else {
			$("#pmpt_cost").html('Prijs per liter:');
			$("#pmpt_inventory").html('Voorraad ml:');
		}
	}

	var dataRecord = {},
	url = 'includes/db_inventory_miscs.php',
	// prepare the data
	source = {
		datatype: "json",
		cache: false,
		datafields: [
			{ name: 'record', type: 'number' },
			{ name: 'name', type: 'string' },
			{ name: 'type', type: 'int' },
			{ name: 'use_use', type: 'int' },
			{ name: 'time', type: 'int' },
			{ name: 'amount', type: 'float' },
			{ name: 'amount_is_weight', type: 'int' },
			{ name: 'use_for', type: 'string' },
			{ name: 'notes', type: 'string' },
			{ name: 'always_on_stock', type: 'int' },
			{ name: 'inventory', type: 'float' },
			{ name: 'cost', type: 'float' },
			{ name: 'production_date', type: 'string' },
			{ name: 'tht_date', type: 'string' }
		],
		id: 'record',
		url: url,
		deleterow: function (rowid, commit) {
			// synchronize with the server - send delete command
			var data = "delete=true&" + $.param({ record: rowid });
			$.ajax({
				dataType: 'json',
				url: url,
				cache: false,
				data: data,
				type: "POST",
				success: function (data, status, xhr) {
					commit(true);
					location.reload( true );
				},
				error: function (jqXHR, textStatus, errorThrown) {
					commit(false);
				}
			});
		},
		addrow: function (rowid, rowdata, position, commit) {
			var data = "insert=true&" + $.param(rowdata);
			$.ajax({
				dataType: 'json',
				url: url,
				cache: false,
				data: data,
				type: "POST",
				success: function (data, status, xhr) {
					commit(true);
					location.reload( true );
				},
				error: function(jqXHR, textStatus, errorThrown) {
                                        commit(false);
                                }
                        });
                },
		updaterow: function (rowid, rowdata, commit) {
			var data = "update=true&" + $.param(rowdata);
			$.ajax({
				dataType: 'json',
				url: url,
				cache: false,
				data: data,
				type: "POST",
				success: function (data, status, xhr) {
					commit(true);
					location.reload( true );
				},
				error: function(jqXHR, textStatus, errorThrown) {
					commit(false);
				}
			});
		}
	},
	dataAdapter = new $.jqx.dataAdapter(source),
        editrow = -1;

	// initialize the input fields.
	$("#name").jqxInput({ theme: theme, width: 640, height: 23 });
	$("#notes").jqxInput({ theme: theme, width: 800, height: 100 });

	$("#type").jqxDropDownList({theme: theme,
		source: MiscTypeAdapter,
		valueMember: 'id',
		displayMember: 'nl',
		width: 180,
		height: 23,
		autoDropDownHeight: true
	});
	$("#use_use").jqxDropDownList({
		theme: theme,
		source: MiscUseAdapter,
		valueMember: 'id',
		displayMember: 'nl',
		width: 180,
		height: 23,
		autoDropDownHeight: true
	});
	$("#time").jqxNumberInput( PosInt );
	$("#amount_is_weight").jqxCheckBox({ theme: theme, width: 120, height: 23 });
	$("#use_for").jqxInput({ theme: theme, width: 640, height: 48 });
	$("#always_on_stock").jqxCheckBox({ theme: theme, width: 120, height: 23 });
	$("#inventory").jqxNumberInput( Spin1dec );
	$("#production_date").jqxDateTimeInput( Dateopts );
	$("#cost").jqxNumberInput( Spin2dec );
	$("#tht_date").jqxDateTimeInput( Dateopts );
	$("#totval").jqxNumberInput( Show2dec );

	// initialize jqxGrid
	$("#jqxgrid").jqxGrid({
		width: 1280,
		height: 630,
		source: dataAdapter,
		theme: theme,
		showstatusbar: true,
		renderstatusbar: function (statusbar) {
			var container, addButton, impButton;
			container = $("<div style='overflow: hidden; position: relative; margin: 5px;'></div>");
			addButton = $("<div style='float: right; margin-right: 15px;'><img style='position: relative; margin-top: 2px;' src='images/add.png'/><span style='margin-left: 4px; position: relative; top: -3px;'>Nieuw</span></div>");
			impButton = $("<div style='float: right; margin-right: 50px;'><img style='position: relative; margin-top: 2px;' src='images/add.png'/><span style='margin-left: 4px; position: relative; top: -3px;'>Import</span></div>");
			container.append(addButton);
			container.append(impButton);
			statusbar.append(container);
			addButton.jqxButton({ theme: theme, width: 90, height: 20 });
			impButton.jqxButton({ theme: theme, width: 90, height: 20 });
			// add new row.
			addButton.click(function (event) {
				editrow = -1;
				$("#popupWindow").jqxWindow({ position: { x: 110, y: 30 } });
				$("#name").val('');
				$("#type").val(0);
				$("#use_use").val(0);
				$("#time").val(0);
				$("#amount_is_weight").val(1);
				$("#use_for").val('');
				$("#notes").val('');
				$("#always_on_stock").val(0);
				$("#inventory").val(0);
				$("#cost").val(0);
				$("#production_date").val('');
				$("#tht_date").val('');
				$("#popupWindow").jqxWindow('open');
			});
			impButton.click(function (event) {
				window.location.href = 'import_ingredients.php?select=miscs';
			});
		},
		filterable: true,
		filtermode: 'excel',
		columns: [
			{ text: 'Ingredient naam', datafield: 'name', menu: false },
			{ text: 'Type', datafield: 'type', align: 'left', width: 120,
			  cellsrenderer: function (index, datafield, value, defaultvalue, column, rowdata) {
				return "<span style='margin: 3px; margin-top: 6px; float: left;'>" + MiscTypeData[value].nl + "</span>";
			  }
		       	},
			{ text: 'Gebruik', datafield: 'use_use', align: 'left', width: 120,
			  cellsrenderer: function (index, datafield, value, defaultvalue, column, rowdata) {
				return "<span style='margin: 3px; margin-top: 6px; float: left;'>" + MiscUseData[value].nl + "</span>";
			  }
			},
			{ text: 'Tijd', datafield: 'time', width: 90, align: 'left', menu: false,
			  cellsrenderer: function (index, datafield, value, defaultvalue, column, rowdata) {
				var duration = "";
				if (value > 0) {
					if (value == 1)
						duration = "1 minuut";
					else if (value < 60)
						duration = dataAdapter.formatNumber(value, "f0")+" minuten";
					else if (value == 60)
						duration = "1 uur";
					else if (value < 1440)
						duration = dataAdapter.formatNumber(value / 60, "f0")+" uren";
					else if (value == 1440)
						duration = "1 dag";
					else
						duration = dataAdapter.formatNumber(value / 1440, "f0")+" dagen";
				}
				return "<span style='margin: 3px; margin-top: 6px; float: left;'>" + duration + "</span>";
			  }
		       	},
			{ text: 'Voorraad', datafield: 'inventory', width: 110, menu: false, align: 'right',
			  cellsrenderer: function (index, datafield, value, defaultvalue, column, rowdata) {
				var amount = "";
				if (value > 0) {
					if (rowdata.amount_is_weight)
						amount = dataAdapter.formatNumber(value,"f1")+" gr";
					else
						amount = dataAdapter.formatNumber(value,"f1")+" ml";
				}
				return "<span style='margin: 3px; margin-top: 6px; float: right;'>" + amount + "</span>";
			  }
		       	},
			{ text: 'THT datum', datafield: 'tht_date', width: 100, menu: false, align: 'center', cellsalign: 'center' },
			{ text: '', datafield: 'Edit', columntype: 'button', width: 100, menu: false, align: 'center',
				cellsrenderer: function () {
					return "Wijzig";
				}, buttonclick: function (row) {
					// open the popup window when the user clicks a button.
					editrow = row;
					$("#popupWindow").jqxWindow({ position: { x: 110, y: 30 } });
					// get the clicked row's data and initialize the input fields.
					dataRecord = $("#jqxgrid").jqxGrid('getrowdata', editrow);
					$("#name").val(dataRecord.name);
					$("#type").val(dataRecord.type);
					$("#use_use").val(dataRecord.use_use);
					$("#time").val(dataRecord.time);
					$("#amount_is_weight").val(dataRecord.amount_is_weight);
					$("#use_for").val(dataRecord.use_for);
					$("#notes").val(dataRecord.notes);
					$("#always_on_stock").val(dataRecord.always_on_stock);
					$("#inventory").val(dataRecord.inventory);
					$("#cost").val(dataRecord.cost);
					$("#production_date").val(dataRecord.production_date);
					$("#tht_date").val(dataRecord.tht_date);
					calcTotal(dataRecord.cost, dataRecord.inventory);
					prompts(dataRecord.amount_is_weight);
					// show the popup window.
					$("#popupWindow").jqxWindow('open');
				}
			}
		]
	});

	$("#cost").on('change', function (event) {
		dataRecord.cost = parseFloat(event.args.value);
		calcTotal(dataRecord.cost, dataRecord.inventory);
	});
	$("#inventory").on('change', function (event) {
		dataRecord.inventory = parseFloat(event.args.value);
		calcTotal(dataRecord.cost, dataRecord.inventory);
	});
	$("#amount_is_weight").on('checked', function (event) {
		dataRecord.amount_is_weight = true;
		prompts(true);
	});
	$("#amount_is_weight").on('unchecked', function (event) {
		dataRecord.amount_is_weight = false;
		prompts(false);
	});

	// initialize the popup window and buttons.
	$("#popupWindow").jqxWindow({
		width: 1050,
		height: 550,
		resizable: false,
		theme: theme,
		isModal: true,
		autoOpen: false,
		cancelButton: $("#Cancel"),
		modalOpacity: 0.40
	});
	$("#popupWindow").on('open', function () {
		$("#name").jqxInput('selectAll');
	});
	$("#Delete").jqxButton({ template: "danger", width: '90px', theme: theme });
	$("#Delete").click(function () {
		if (editrow >= 0) {
			// Open a popup to confirm this action.
			$('#eventWindow').jqxWindow('open');
			$("#delOk").click(function () {
				var rowID = $('#jqxgrid').jqxGrid('getrowid', editrow);
				$("#jqxgrid").jqxGrid('deleterow', rowID);
			});
		}
		$("#popupWindow").jqxWindow('hide');
	});
	$("#Cancel").jqxButton({ template: "primary", width: '90px', theme: theme });
	$("#Save").jqxButton({ template: "success", width: '90px', theme: theme });
	// update the edited row when the user clicks the 'Save' button.
	$("#Save").click(function () {
		var row, rowID = -1;
		if (editrow >= 0) {
			rowID = $('#jqxgrid').jqxGrid('getrowid', editrow);
		}
		row = {
			record: rowID,
			name: $("#name").val(),
			type: $("#type").val(),
			use_use: $("#use_use").val(),
			time: parseFloat($("#time").jqxNumberInput('decimal')),
			amount_is_weight: $("#amount_is_weight").val(),
			use_for: $("#use_for").val(),
			notes: $("#notes").val(),
			always_on_stock: $("#always_on_stock").val(),
			inventory: parseFloat($("#inventory").jqxNumberInput('decimal')),
			cost: parseFloat($("#cost").jqxNumberInput('decimal')),
	                production_date: $("#production_date").val(),
	                tht_date: $("#tht_date").val()
		};
		if (editrow >= 0) {
			$('#jqxgrid').jqxGrid('updaterow', rowID, row);
		} else {
			$('#jqxgrid').jqxGrid('addrow', null, row);
		}
		$("#popupWindow").jqxWindow('hide');
	});
	createDelElements();
});

mercurial