
var inventory_audit = [];
var html = '';


// BRANCHES

function get_branches() {
	var GET_MAP_ARRAY = JSCUS_SQL._ACCOUNT._GROUP.CUS_PREPARE_MAPPED_ARRAY_FOR_GET();

	//JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_ACC_GROUP_STATUS', 2);
	JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_ORDER_FIELD', 'ACC_GROUP_DESCRIPTION');
	JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_ORDER_TYPE', 'ASC');

	JSCUS_SQL._ACCOUNT._GROUP.CUS_GET(GET_MAP_ARRAY, function(IS_SUCCESS, RESPONSE_STRING) {
		if(IS_SUCCESS === false) {
			alert("CONNECTION ERROR: \n" + RESPONSE_STRING);
			return;
		}

		var JSON_OBJECT = JSRC_JSON.REF_CREATE(RESPONSE_STRING);

		if(JSRC_JSON.REF_IS_SUCCESS(JSON_OBJECT) === false) {
			alert("REQUEST ERROR: \n" + RESPONSE_STRING); 
			return;
		}

		if(JSRC_JSON.REF_READ(JSON_OBJECT, "JSON_COUNT") <= 0) {
			return; 
		}

		$('select[id="SEARCH_REC_INVAUDIT_ACC_GROUP_ID_CMB"]').empty();
		$('select[id="SEARCH_REC_INVAUDIT_ACC_GROUP_ID_CMB"]').append($('<option>', { 
		        value: 0,
		        text : 'Any Group'
		    }));

		$.each(JSON_OBJECT.ACC_GROUP_ID, function(i, item) {
			$('select[id="SEARCH_REC_INVAUDIT_ACC_GROUP_ID_CMB"]').append($('<option>', { 
		        value: item,
		        text : JSON_OBJECT.ACC_GROUP_DESCRIPTION[i]
		    }));
		});

		$('select[id="SEARCH_REC_INVAUDIT_ACC_GROUP_ID_CMB"]').val(0).trigger('change');

	}); 
}

// LOCATIONS

function get_locations(branch_id) {
	var GET_MAP_ARRAY = JSCUS_SQL._CATEGORY._STORAGE.CUS_PREPARE_MAPPED_ARRAY_FOR_GET();

	JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_CAT_STORAGE_ACC_GROUP_ID', branch_id);
	//JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_CAT_STORAGE_STATUS', 2);

	JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_ORDER_FIELD', 'CAT_STORAGE_DESCRIPTION');
	JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_ORDER_TYPE', 'ASC');

	JSCUS_SQL._CATEGORY._STORAGE.CUS_GET(GET_MAP_ARRAY, function(IS_SUCCESS, RESPONSE_STRING) {
		if(IS_SUCCESS === false) {
			alert("CONNECTION ERROR: \n" + RESPONSE_STRING);
			return;
		}

		var JSON_OBJECT = JSRC_JSON.REF_CREATE(RESPONSE_STRING);

		if(JSRC_JSON.REF_IS_SUCCESS(JSON_OBJECT) === false) {
			alert("REQUEST ERROR: \n" + RESPONSE_STRING); 
			return;
		}

		if(JSRC_JSON.REF_READ(JSON_OBJECT, "JSON_COUNT") <= 0) {
			$('select[name="INVAUDIT_STORAGE"]').empty();
			$('select[name="INVAUDIT_STORAGE"]').append($('<option>', { 
				value: 0,
				text : 'No Storage Location Found'
			}));

			return; 
		}
		else {
			$('select[name="INVAUDIT_STORAGE"]').empty();
			$('select[name="INVAUDIT_STORAGE"]').append($('<option>', { 
				value: 0,
				text : 'Select Storage Location'
			}));

			$.each(JSON_OBJECT.CAT_STORAGE_ID, function(i, item) {
				$('select[name="INVAUDIT_STORAGE"]').append($('<option>', { 
				    value: item,
				    text : JSON_OBJECT.CAT_STORAGE_DESCRIPTION[i]
				}));
			});
		}
	}); 
}


//PRODUCTS 

function get_products(branch_id) {
	if(branch_id == 0 || branch_id == '' || branch_id == null)
		JSOC_DIALOG.VAR_MESSAGE_BOX('Please select branch to load products.', 'Information', 'OK', 'ERROR', function() {});
	else {
		var GET_MAP_ARRAY = JSCUS_SQL._CATEGORY._PRODUCT_BRANCH.CUS_PREPARE_MAPPED_ARRAY_FOR_GET();

		JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_CAT_PRODBRANCH_ACC_GROUP_ID', branch_id);
		JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_CAT_PRODBRANCH_STATUS', 2);

		JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_ORDER_FIELD', 'CAT_PRODUCT_DESCRIPTION');
		JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_ORDER_TYPE', 'ASC');

		JSCUS_SQL._CATEGORY._PRODUCT_BRANCH.CUS_GET(GET_MAP_ARRAY, function(IS_SUCCESS, RESPONSE_STRING) {
			if(IS_SUCCESS === false) {
				alert("CONNECTION ERROR: \n" + RESPONSE_STRING);
				return;
			}

			var JSON_OBJECT = JSRC_JSON.REF_CREATE(RESPONSE_STRING);

			if(JSRC_JSON.REF_IS_SUCCESS(JSON_OBJECT) === false) {
				alert("REQUEST ERROR: \n" + RESPONSE_STRING); 
				return;
			}

			if(JSRC_JSON.REF_READ(JSON_OBJECT, "JSON_COUNT") <= 0) {
				JSOC_DIALOG.VAR_MESSAGE_BOX('No products found on branch selected!', 'Information', 'OK', 'ERROR', function() {});
				return; 
			}
			else {
				$('#INVAUDIT_TBL tbody').empty();

				$.each(JSON_OBJECT['CAT_PRODUCT_ID'], function(i, item) {
					html += '<tr>';
						html += '<th id="PRODUCT_ID_'+item+'">'+JSON_OBJECT['CAT_PRODUCT_DESCRIPTION'][i]+'</th>';
						html += '<td class="text-right"><input type="hidden" class="form-control text-right" name="PRODUCT_ID_'+item+'_SYSTEM_QTY_VALUE" id="PRODUCT_ID_'+item+'_SYSTEM_QTY_VALUE" value="0"><span id="PRODUCT_ID_'+item+'_SYSTEM_QTY">0.00</span></td>';
						html += '<td class="text-right"><input type="number" class="form-control text-right" name="PRODUCT_ID_'+item+'_ACTUAL_QTY" id="PRODUCT_ID_'+item+'_ACTUAL_QTY" value="0"></td>';
						html += '<td class="text-right"><input type="hidden" class="form-control text-right" name="PRODUCT_ID_'+item+'_AUDIT_LINE_ID" id="PRODUCT_ID_'+item+'_AUDIT_LINE_ID" value="0"><input type="hidden" class="form-control text-right" name="PRODUCT_ID_'+item+'_AUDIT_ID" id="PRODUCT_ID_'+item+'_AUDIT_ID" value="0"><span id="PRODUCT_ID_'+item+'_DIFF_QTY">0.00</span></td>';
					html += '</tr>';
				});

				$('#INVAUDIT_TBL tbody').html(html);

				$('input[name$="_ACTUAL_QTY"]').on('change keyup', function (e) {
					calculate_difference($(this).attr('id'));
				});

				$('input[name$="_ACTUAL_QTY"]').keypress(function (e) {
				  	if (e.which == 13)
				    	save_audit_line($(this).attr('id'));
				});
			}
		});
	}
}


// INVENTORY AUDIT

/*function get_inventory_audit(branch_id, location_id) {
	JSOC_DIALOG.VAR_LOADING_SPINNER(true);

	if(parseInt(location_id) !== 0) {
		get_products(parseInt(branch_id));

		var GET_MAP_ARRAY = JSCUS_SQL._RECORD._INVAUDIT.CUS_PREPARE_MAPPED_ARRAY_FOR_GET();

		JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_REC_INVAUDIT_ACC_GROUP_ID', parseInt(branch_id));
		JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_REC_INVAUDIT_CAT_STORAGE_ID', parseInt(location_id));
		JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_REC_INVAUDIT_STATUS', 2);

		JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_ORDER_FIELD', 'REC_INVAUDIT_ID');
		JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_ORDER_TYPE', 'DESC');

		JSCUS_SQL._RECORD._INVAUDIT.CUS_GET(GET_MAP_ARRAY, function(IS_SUCCESS, RESPONSE_STRING) {
			if(IS_SUCCESS === false) {
				alert("CONNECTION ERROR: \n" + RESPONSE_STRING);
				return;
			}

			var JSON_OBJECT = JSRC_JSON.REF_CREATE(RESPONSE_STRING);

			if(JSRC_JSON.REF_IS_SUCCESS(JSON_OBJECT) === false) {
				alert("REQUEST ERROR: \n" + RESPONSE_STRING); 
				return;
			}

			if(JSRC_JSON.REF_READ(JSON_OBJECT, "JSON_COUNT") <= 0) {
				return; 
			}
			else {
				//get audit lines per audit id
				$.each(JSON_OBJECT['REC_INVAUDIT_ID'], function(i, item) {
					var GET_MAP_ARRAY2 = JSCUS_SQL._RECORD._INVENTORY_AUDIT_LINE.CUS_PREPARE_MAPPED_ARRAY_FOR_GET();

					JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY2, 'PARAM_REC_INVAUDIT_LINE_REC_INVAUDIT_ID', item);
					JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY2, 'PARAM_REC_INVAUDIT_LINE_STATUS', 2);

					JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY2, 'PARAM_ORDER_FIELD', 'REC_INVAUDIT_LINE_CAT_PRODUCT_ID');
					JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY2, 'PARAM_ORDER_TYPE', 'DESC');

					JSCUS_SQL._RECORD._INVENTORY_AUDIT_LINE.CUS_GET(GET_MAP_ARRAY2, function(IS_SUCCESS2, RESPONSE_STRING2) {
						if(IS_SUCCESS === false) {
							alert("CONNECTION ERROR: \n" + RESPONSE_STRING2);
							return;
						}

						var JSON_OBJECT2 = JSRC_JSON.REF_CREATE(RESPONSE_STRING2);

						if(JSRC_JSON.REF_IS_SUCCESS(JSON_OBJECT2) === false) {
							alert("REQUEST ERROR: \n" + RESPONSE_STRING2); 
							return;
						}

						if(JSRC_JSON.REF_READ(JSON_OBJECT2, "JSON_COUNT") <= 0) {
							return; 
						}
						else {
							var difference = 0;

							$.each(JSON_OBJECT2['REC_INVAUDIT_LINE_ID'], function(key, value) {

								product = JSON_OBJECT2['REC_INVAUDIT_LINE_CAT_PRODUCT_ID'][key];
								
								system = parseFloat(JSON_OBJECT2['REC_INVAUDIT_LINE_QUANTITY_SYSTEM'][key]) || 0;
								actual = parseFloat(JSON_OBJECT2['REC_INVAUDIT_LINE_QUANTITY_ACTUAL'][key]) || 0;

								difference = system - actual;


								$('#PRODUCT_ID_'+product+'_SYSTEM_QTY').html(JSVC_NUM.VAR_FORMAT(system, 2));
								$('#PRODUCT_ID_'+product+'_SYSTEM_QTY_VALUE').val(system);

								$('#PRODUCT_ID_'+product+'_ACTUAL_QTY').val(actual);
								$('#PRODUCT_ID_'+product+'_DIFF_QTY').html(JSVC_NUM.VAR_FORMAT(difference, 2));
								
								$('#PRODUCT_ID_'+product+'_AUDIT_ID').val(item);
								$('#PRODUCT_ID_'+product+'_AUDIT_LINE_ID').val(value);
							});
						}
					});
				});
			}
		}); 
	}
	else
		$('#INVAUDIT_TBL tbody').empty().html('<tr><td colspan="4" class="text-center">Select Branch and Location to load products and inventory</td></tr>');

	JSOC_DIALOG.VAR_LOADING_SPINNER(false);
}
*/

// CALCULATE DIFFERENCE

/*function calculate_difference(product) {
	var difference = 0;

	product = $('#'+product).attr('id').replace('_ACTUAL_QTY','');

	actual = parseFloat($('#'+product+'_ACTUAL_QTY').val()) || 0;
	system = parseFloat($('#'+product+'_SYSTEM_QTY_VALUE').val()) || 0;

	difference = system - actual;
	$('#'+product+'_DIFF_QTY').html(JSVC_NUM.VAR_FORMAT(difference, 2));
}
*/


// SAVE AUDIT LINE

/*function save_audit_line(product) {
	var difference = 0;

	var SAVE_MAP_ARRAY1 = [];
	var SAVE_MAP_ARRAY2 = [];

	var branch_id = parseInt($('#INVAUDIT_BRANCH').val()) || 1;
	var location_id = parseInt($('#INVAUDIT_STORAGE').val()) || 1;

	var product = $('#'+product).attr('id').replace('_ACTUAL_QTY','');
	var product_id = parseInt(product.replace('PRODUCT_ID_',''));
		
	var inventory_audit_id = parseInt($('#'+product+'_AUDIT_ID').val()) || 0;
	var inventory_audit_line_id = parseInt($('#'+product+'_AUDIT_LINE_ID').val()) || 0;

	var actual = parseFloat($('#'+product+'_ACTUAL_QTY').val()) || 0;

	if(inventory_audit_id === 0) {
		//add new inventory audit
		SAVE_MAP_ARRAY1 = JSCUS_SQL._RECORD._INVAUDIT.CUS_PREPARE_MAPPED_ARRAY_FOR_SAVE();

		JSVC_MAP_ARRAY.VAR_SET(SAVE_MAP_ARRAY1, "PARAM_REC_INVAUDIT_ACC_GROUP_ID", branch_id);
		JSVC_MAP_ARRAY.VAR_SET(SAVE_MAP_ARRAY1, "PARAM_REC_INVAUDIT_CAT_STORAGE_ID", location_id);
		JSVC_MAP_ARRAY.VAR_SET(SAVE_MAP_ARRAY1, "PARAM_REC_INVAUDIT_STATUS", 2);

		JSCUS_SQL._RECORD._INVAUDIT.CUS_SAVE(SAVE_MAP_ARRAY1,  function(IS_SUCCESS1, HTTP_RESPONSE1) {
			if(IS_SUCCESS1 === false) {
				alert("CONNECTION ERROR: \n" + HTTP_RESPONSE1);
				return;
			}

			var JSON_OBJECT1 = JSRC_JSON.REF_CREATE(HTTP_RESPONSE1);

			if(JSRC_JSON.REF_IS_SUCCESS(JSON_OBJECT1) === false) {
				alert("REQUEST ERROR: \n" + HTTP_RESPONSE1); 
				return;
			}
			else {
				inventory_audit_id = JSON_OBJECT1.INSERTED_ID[1];

				SAVE_MAP_ARRAY2 = JSCUS_SQL._RECORD._INVENTORY_AUDIT_LINE.CUS_PREPARE_MAPPED_ARRAY_FOR_SAVE();

				if(inventory_audit_line_id !== 0)
					JSVC_MAP_ARRAY.VAR_SET(SAVE_MAP_ARRAY2, "PARAM_REC_INVAUDIT_LINE_ID", parseInt(inventory_audit_line_id));
				
				JSVC_MAP_ARRAY.VAR_SET(SAVE_MAP_ARRAY2, "PARAM_REC_INVAUDIT_LINE_REC_INVAUDIT_ID", parseInt(inventory_audit_id));
				JSVC_MAP_ARRAY.VAR_SET(SAVE_MAP_ARRAY2, "PARAM_REC_INVAUDIT_LINE_CAT_PRODUCT_ID", product_id);
				JSVC_MAP_ARRAY.VAR_SET(SAVE_MAP_ARRAY2, "PARAM_REC_INVAUDIT_LINE_QUANTITY_ACTUAL", actual);
				JSVC_MAP_ARRAY.VAR_SET(SAVE_MAP_ARRAY2, "PARAM_REC_INVAUDIT_LINE_STATUS", 2);

				JSCUS_SQL._RECORD._INVENTORY_AUDIT_LINE.CUS_SAVE(SAVE_MAP_ARRAY2,  function(IS_SUCCESS2, HTTP_RESPONSE2) {
					if(IS_SUCCESS2 === false) {
						alert("CONNECTION ERROR: \n" + HTTP_RESPONSE2);
						return;
					}

					var JSON_OBJECT2 = JSRC_JSON.REF_CREATE(HTTP_RESPONSE2);

					if(JSRC_JSON.REF_IS_SUCCESS(JSON_OBJECT2) === false) {
						alert("REQUEST ERROR: \n" + HTTP_RESPONSE2); 
						return;
					}	
					else {
						console.log(JSON_OBJECT2);	
						get_inventory_audit(branch_id, location_id);
					}
				});
			}
		});
	}
	else {
		SAVE_MAP_ARRAY2 = JSCUS_SQL._RECORD._INVENTORY_AUDIT_LINE.CUS_PREPARE_MAPPED_ARRAY_FOR_SAVE();

		if(inventory_audit_line_id > 0)
			JSVC_MAP_ARRAY.VAR_SET(SAVE_MAP_ARRAY2, "PARAM_REC_INVAUDIT_LINE_ID", inventory_audit_line_id);
		
		JSVC_MAP_ARRAY.VAR_SET(SAVE_MAP_ARRAY2, "PARAM_REC_INVAUDIT_LINE_REC_INVAUDIT_ID", inventory_audit_id);
		JSVC_MAP_ARRAY.VAR_SET(SAVE_MAP_ARRAY2, "PARAM_REC_INVAUDIT_LINE_CAT_PRODUCT_ID", product_id);
		JSVC_MAP_ARRAY.VAR_SET(SAVE_MAP_ARRAY2, "PARAM_REC_INVAUDIT_LINE_QUANTITY_ACTUAL", actual);
		JSVC_MAP_ARRAY.VAR_SET(SAVE_MAP_ARRAY2, "PARAM_REC_INVAUDIT_LINE_STATUS", 2);

		JSCUS_SQL._RECORD._INVENTORY_AUDIT_LINE.CUS_SAVE(SAVE_MAP_ARRAY2,  function(IS_SUCCESS2, HTTP_RESPONSE2) {
			if(IS_SUCCESS2 === false) {
				alert("CONNECTION ERROR: \n" + HTTP_RESPONSE2);
				return;
			}

			var JSON_OBJECT2 = JSRC_JSON.REF_CREATE(HTTP_RESPONSE2);

			if(JSRC_JSON.REF_IS_SUCCESS(JSON_OBJECT2) === false) {
				alert("REQUEST ERROR: \n" + HTTP_RESPONSE2); 
				return;
			}	
			else {
				console.log(JSON_OBJECT2);	
				get_inventory_audit(branch_id, location_id);
			}
		});
	}
}*/




//run scripts
$(document).ready(function() {
	//PAGE_AUTH_REQUIRES_LOGIN();

	get_branches();

	$('select[id="SEARCH_REC_INVAUDIT_STATUS_CMB"]').on('change', function() {
		if($(this).val() !== '0')
			get_locations($(this).val());
	});

	// $('select[name="INVAUDIT_STORAGE"]').on('change', function() {
	// 	if($('select[name="INVAUDIT_BRANCH"]').val() !== '0')
	// 		get_inventory_audit($('select[name="SEARCH_REC_INVAUDIT_STATUS_CMB"]').val(), $(this).val());
	// });
});