(function(exports){
	'use strict'

	var erpConfig=JSON.parse(fs.read("scripts/erp.json"));
	
	
	function erpCall_ex(query)
	{
		fs.write("tmp/post.bin", JSON.stringify(query));
		
		var result=net.httpPost(
			erpConfig.url,
			"tmp/post.bin",
			"tmp/get.bin",
			"Запрос к серверу...",
			{
				"Content-Type": "application/json",
				"X-API-Key": erpConfig.token
			} );
		
		if (result != 200) throw new Error("ERP POST error");
		
		JSON.parse(fs.read("tmp/get.bin"));
	}
	
	
	function erpCall(query)
	{
		try
		{
			erpCall_ex(query);
			return true;
		} catch (e)
		{
			print("erpCall exception: "+e);
			return false;
		}
	}
	
	
	exports.meter = function(model, hw_rev, fw_rev, serial, value, result) {
		return erpCall( {
			"meter_verification": {
				"model": model,
				"serial": serial,
				"is_defect": false,
				"is_flash": result,
				"fw_version": fw_rev,
				"hw_version": hw_rev,
				"start_value": value,
				"flash_at": ~~(Date.now() / 1000)
			}
		} );
	};
	
	
	exports.pr = function(model, hw_rev, fw_rev, serial, result) {
		return erpCall( {
			"uspd_verification": {
				"model": model,
				"serial": serial,
				"is_defect": false,
				"is_flash": result,
				"fw_version": fw_rev,
				"hw_version": hw_rev,
				"flash_at": ~~(Date.now() / 1000)
			}
		} );
	};
	
	
	exports.uspd = function(model, hw_rev, fw_rev, uuid, result) {
		return erpCall( {
			"uspd_verification": {
				"model": model,
				"uuid": uuid,
				"is_defect": false,
				"is_flash": result,
				"fw_version": fw_rev,
				"hw_version": hw_rev,
				"flash_at": ~~(Date.now() / 1000)
			}
		} );
	};
})(typeof exports === 'undefined' ? this['_erp_js']={} : exports);
