Files
crm.e5.pl/modules/EcmServices/realizationList.js
2024-04-27 09:23:34 +02:00

1 line
17 KiB
JavaScript

window.onbeforeunload = function() {
removeDocumentReservations(document.getElementById("temp_id").value);
alert('Wprowadzone zmiany nie zostały zapisane');
}
function removeDocumentReservations(temp_id, operation) {
//alert(operation);
url='index.php?module=EcmStockOperations&action=deleteDocumentReservations&temp_doc_id='+temp_id+'&to_pdf=1';
var req=mint.Request();
req.OnSuccess = function() {
if (operation) {
if (operation=='cancel')
window.location="index.php?module=EcmInvoiceOuts&action=index";
if (operation=='list')
window.location="index.php?module=EcmInvoiceOuts&action=index";
}
}
req.Send(url);
}
function getProductQuantity(input,product_id, stock_id,old_qty, row)
{
url='index.php?module=EcmStockStates&action=getProductQuantity&stock_id='+stock_id+'&product_id='+product_id+'&to_pdf=1';
var req=mint.Request();
req.OnLoading=function()
{
input.innerHTML='loading';
}
req.OnSuccess = function()
{
console.log(old_qty);
if (row) {
if (parseFloat(this.responseText) != old_qty) {
row.className='orangeRow';
setTimeout(function() {row.className='selectedRow';}, 1500);
}
}
input.value=this.responseText;
}
req.Send(url);
}
function saveReservation(stock_id,product_id,quantity,temp_doc_id,temp_item_id,date, row){
url='index.php?module=EcmStockOperations&action=saveReservation&stock_id='+stock_id+'&product_id='+product_id+'&temp_doc_id='+temp_doc_id+'&temp_item_id='+temp_item_id+'&date='+date+'&quantity='+quantity+'&to_pdf=1';
var req=mint.Request();
req.OnSuccess = function(){
row.className='greenRow';
setTimeout(function() {row.className='selectedRow';}, 1500);
}
req.Send(url);
}
function removeReservation(cell) {
var temp_item_id = cell.parentNode.getData().temp_item_id;
url='index.php?module=EcmStockOperations&action=deleteReservation&temp_item_id='+temp_item_id+'&to_pdf=1';
var req=mint.Request();
req.OnSuccess = function(){}
req.Send(url);
}
function createEcmInvoiceOut() {
var selected_prod = false;
for(var i=0; i<N.rowCount(); i++) {
if (N.row(i).getData().checked ==1) selected_prod=true;
}
if (selected_prod==false) {alert('Musisz wybrać przynajmniej jeden produkt'); return; }
post = 'temp_id='+document.getElementById("temp_id").value+"&module=EcmServices&action=realizationListSetSession&to_pdf=1";
doRequest('index.php',post+ItemListSave(true),function(result){
window.onbeforeunload = '';
window.location="index.php?module=EcmInvoiceOuts&action=EditView&to_pdf=0&parent_doc_type=EcmServices&temp_id="+document.getElementById("temp_id").value;
}
);
}
function addEvent(object,eventName,do_function) {
if(typeof(object) == "string") object = document.getElementById(object);
if(!object) { alert('No object in function addEvent!'); return; }
if(object.addEventListener) {
object.addEventListener(eventName, do_function, false);
} else {
object.attachEvent('on'+eventName, do_function);
}
}
function doRequest(where,post,doFunction,error) {
this.Display = function(result) { doFunction(result.responseText); }
this.Fail = function(result){ if(error) alert(error);}
YAHOO.util.Connect.asyncRequest('POST',where,{success:this.Display,failure:this.Fail},post);
}
ItemListSave = function(json) {
var data = '';
for(var i=0; i<N.rowCount(); i++) {
if (N.row(i).getData().checked ==1)
data = data + '&p_' + i.toString() + '=' + JSON.stringifyNoSecurity(N.row(i).getData());
}
return data;
}
var ERROR = false;
function ItemListClear() {
while(N.rowCount()>0) N.row(0).deleteRow();
}
var parentFL;
var productFL;
var contactFL;
addEvent(
window,
'load',
function () {
//initialize table
N = new MyTable('itemsTable');
N.divParent = document.getElementById('itemsTableDIV');
N.onCreateRow = function(row) {
row.newPos = false;
row.onSelect = function() {
for(var i=0; i<this.myTable.colCount(); i++) {
this.cells.item(i).style.height = OPT['row_item_height_selected'];
this.cells.item(i).change(!this.newPos);
}
}
row.onDeselect = function() {
for(var i=0; i<this.myTable.colCount(); i++) {
this.cells.item(i).style.height = OPT['row_item_height'];
this.cells.item(i).change(false);
}
}
}
var itd = document.getElementById('itemsTableDIV');
N.onCreateCell = function(cell) {
var i = cell.index;
cell.change = function(select) {};
cell.style.height = OPT['row_item_height'];
if(i == 0) {
cell.setData = function(data) {
if(data.checked) cell.firstChild.checked = data.checked;
};
cell.getData = function(data) {
data.checked = cell.firstChild.checked;
}
cell.select = function() { this.selectNext(); }
var edit = document.createElement('input');
edit.setAttribute('type','checkbox');
edit.setAttribute('tabIndex',1);
edit.className = 'inputs';
cell.appendChild(edit);
}
if(i == 1) {
cell.change = function(select) {
var cn = this.getElementsByTagName('input');
var c;
if(select) {
c = cn[0]; c.name = 'code_p'; c.id = 'code_p'; c.className = 'inputs';// sqsEnabled';
c = cn[1]; c.name = 'id_p'; c.id = 'id_p';
c = cn[2]; c.name = 'temp_item_id_p'; c.id = 'temp_item_id_p';
c = cn[3]; c.name = 'temp_date_p'; c.id = 'temp_date_p';
c = cn[4]; c.name = 'tmp_date_p'; c.id = 'temp_date_p';
c = cn[5]; c.name = 'service_rq_p'; c.id = 'service_rq_p';
c = cn[6]; c.name = 'service_id_p'; c.id = 'service_id_p';
c = cn[7]; c.name = 'service_item_id_p'; c.id = 'service_item_id_p';
}
else {
c = cn[0]; c.name = ''; c.id = ''; c.className = 'inputs';
c = cn[1]; c.name = ''; c.id = '';
c = cn[2]; c.name = ''; c.id = '';
c = cn[3]; c.name = ''; c.id = '';
c = cn[4]; c.name = ''; c.id = '';
c = cn[5]; c.name = ''; c.id = '';
c = cn[6]; c.name = ''; c.id = '';
c = cn[7]; c.name = ''; c.id = '';
}
}
cell.getData = function(data) {
var cn = this.getElementsByTagName('input');
data.code = cn[0].value;
data.id = cn[1].value;
data.temp_item_id = cn[2].value;
data.temp_date = cn[3].value;
data.type = cn[4].value;
data.service_rq = cn[5].value;
data.service_id = cn[6].value;
data.service_item_id = cn[7].value;
}
cell.setData = function(data) {
var cn = this.getElementsByTagName('input');
cn[0].value = ((data.code)?data.code:'');
cn[1].value = ((data.id)?data.id:'');
cn[2].value = ((data.temp_item_id)?data.temp_item_id:'');
cn[3].value = ((data.temp_date)?data.temp_date:'');
cn[4].value = ((data.type)?data.type:'');
cn[5].value = ((data.service_rq)?data.service_rq:'');
cn[6].value = ((data.service_id)?data.service_id:'');
cn[7].value = ((data.service_item_id)?data.service_item_id:'');
}
var edit = '<input type="text" onFocus="this.parentNode.select();" tabindex="1" class="inputs" style="height:16px;" autocomplete="off" readonly="readonly"><br>';
cell.innerHTML = edit;
cell.align = 'right';
var id = document.createElement('input');
id.setAttribute('type','hidden');
cell.appendChild(id);
var temp_item_id = document.createElement('input');
temp_item_id.setAttribute('type','hidden');
temp_item_id.setAttribute('value','');
cell.appendChild(temp_item_id);
var temp_date = document.createElement('input');
temp_date.setAttribute('type','hidden');
temp_date.setAttribute('value','');
cell.appendChild(temp_date);
var type = document.createElement('input');
type.setAttribute('type','hidden');
type.setAttribute('value','');
cell.appendChild(type);
var service_rq = document.createElement('input');
service_rq.setAttribute('type','hidden');
service_rq.setAttribute('value','');
cell.appendChild(service_rq);
var service_id = document.createElement('input');
service_id.setAttribute('type','hidden');
service_id.setAttribute('value','');
cell.appendChild(service_id);
var service_item_id = document.createElement('input');
service_item_id.setAttribute('type','hidden');
service_item_id.setAttribute('value','');
cell.appendChild(service_item_id);
}
//nazwa
if(i == 2) {
cell.change = function(select) {
var c;
if(select) {
c = this.childNodes[0]; c.name = 'name_p'; c.id = 'name_p'; //c.className = 'sqsEnabled';
}
else {
c = this.childNodes[0]; c.name = ''; c.id = ''; c.className = '';
}
}
cell.getData = function(data) {
var cn = this.getElementsByTagName('textarea');
data.name = cn[0].value;
}
cell.setData = function(data) {
var cn = this.getElementsByTagName('textarea');
cn[0].value = ((data.name)?data.name:'');
}
var textarea = '<textarea tabindex="1" onFocus="this.parentNode.select();" style="width:100%;height:100%;" readonly="readonly" class="inputs" onKeyDown="return this.parentNode.myTable.KeyPressed(event,this.parentNode,null,true);"></textarea>';
cell.innerHTML = textarea;
}
//ilość
if(i == 3) {
var old_qty;
cell.change = function(select) {
var c;
if(select) {
c = this.childNodes[0]; c.name = 'quantity_p'; c.id = 'quantity_p';
}
else {
c = this.childNodes[0]; c.name = ''; c.id = '';
}
}
cell.getData = function(data,noAlert) {
var tmp = UserFormatNumberToNumber(this.firstChild.value);
if(tmp == -1) {
ERROR = true;
data.quantity = 0;
this.firstChild.style.color = 'red';
} else {
data.quantity = tmp;
this.firstChild.style.color = 'black';
}
}
cell.setData = function(data) {
if(data.quantity) this.firstChild.value = NumberToUserFormatNumber(data.quantity); else this.firstChild.value = NumberToUserFormatNumber(1);
}
cell.onSelect = function() {
//refresh state
data = cell.parentNode.getData();
old_qty = data.quantity;
if (data.type == 'service') return;
getProductQuantity(cell.parentNode.getElementsByTagName("input")[10],data.id,document.getElementById("stock_id").value,data.stock, cell.parentNode);
setTimeout(function () {
data = cell.parentNode.getData();
cell.parentNode.setData(data);
}, 1500);
}
cell.onDeselect = function() {
ERROR = false;
var data = new Object();
this.getData(data);
if(!ERROR) {
data.quantity = data.quantity;
this.setData(data);
}
var dat = cell.parentNode.getData();
if (dat.type == 'service') return;
if (data.quantity<0) {
data.quantity=0;
this.setData(data);
return;
}
if (data.quantity>0) {
data = cell.parentNode.getData();
getProductQuantity(cell.parentNode.getElementsByTagName("input")[10],data.id,document.getElementById("stock_id").value,data.stock, cell.parentNode);
setTimeout(function() {
data.stock=cell.parentNode.getElementsByTagName("input")[10].value;
cell.parentNode.setData(data);
if ((data.quantity-old_qty) > data.stock) {
cell.parentNode.setData(data);
cell.parentNode.className='redRow';
setTimeout(function() {cell.parentNode.className='selectedRow';}, 1500);
return;
}
//remove current temp reservation
removeReservation(cell);
// save temp reservation
if (data.quantity-data.service_rq > 0)
saveReservation(document.getElementById("stock_id").value,data.id,data.quantity-data.service_rq,document.getElementById("temp_id").value,data.temp_item_id,data.temp_date,cell.parentNode);
//refresh state
getProductQuantity(cell.parentNode.getElementsByTagName("input")[10],data.id,document.getElementById("stock_id").value,data.stock);
setTimeout(function() {
data.stock = cell.parentNode.getElementsByTagName("input")[10].value;
cell.parentNode.setData(data); }, 500);
},500);
}
}
var edit = '<input type="text" tabindex="1" autocomplete="off" class="inputs" style="text-align:right;" onKeyDown="return this.parentNode.myTable.KeyPressed(event,this.parentNode,\'decimalNumber\');" value="'+NumberToUserFormatNumber(1)+'">';
cell.innerHTML = edit;
}
//realizacja zamówienia
if(i == 4) {
cell.change = function(select) {
var c;
if(select) {
c = this.childNodes[0]; c.name = 'service_qty_p'; c.id = 'service_qty_p';
}
else {
c = this.childNodes[0]; c.name = ''; c.id = '';
}
}
cell.getData = function(data,noAlert) {
var tmp = UserFormatNumberToNumber(this.firstChild.value);
if(tmp == -1) {
ERROR = true;
data.service_qty = 0;
this.firstChild.style.color = 'red';
} else {
data.service_qty = tmp;
this.firstChild.style.color = 'black';
}
}
cell.setData = function(data) {
if(data.service_qty) this.firstChild.value = NumberToUserFormatNumber(data.service_qty); else this.firstChild.value = NumberToUserFormatNumber(0);
}
cell.onDeselect = function() {
ERROR = false;
var data = new Object();
this.getData(data);
if(!ERROR) {
data.quantity = data.quantity;
this.setData(data);
}
}
var edit = '<input type="text" tabindex="1" autocomplete="off" class="inputs" style="text-align:right;" onKeyDown="return this.parentNode.myTable.KeyPressed(event,this.parentNode,\'decimalNumber\');" value="'+NumberToUserFormatNumber(0)+'">';
cell.innerHTML = edit;
}
//magazyn
if(i == 5) {
cell.change = function(select) {
var c;
if(select) {
c = this.childNodes[0]; c.name = 'stock_p'; c.id = 'stock_p';
}
else {
c = this.childNodes[0]; c.name = ''; c.id = '';
}
}
cell.getData = function(data,noAlert) {
var tmp = UserFormatNumberToNumber(this.firstChild.value);
if(tmp == -1) {
ERROR = true;
data.stock = 0;
this.firstChild.style.color = 'red';
} else {
data.stock = tmp;
this.firstChild.style.color = 'black';
}
}
cell.setData = function(data) {
var dat = cell.parentNode.getData();
if (dat.type=='service') {this.firstChild.value='usługa'; return;}
if(data.stock) this.firstChild.value = NumberToUserFormatNumber(data.stock); else this.firstChild.value = NumberToUserFormatNumber(0);
}
cell.onDeselect = function() {
ERROR = false;
var data = new Object();
this.getData(data);
if(!ERROR) {
data.stock = data.stock;
this.setData(data);
}
}
var stock;
stock = document.createElement('input');
stock.setAttribute('type','text');
stock.className = 'inputs';
stock.style.textAlign = "right";
stock.cell = cell;
cell.appendChild(stock);
}
}
N.onSetCellData = function(row,cell,data) {
if(cell.innerHTML == '') cell.innerHTML = '&nbsp;';
}
var pl = document.getElementById('position_list').value;
if(pl && pl != '') {
try {
pl = eval(pl);
for(x in pl) {
var pl_row = pl[x]; if(!pl[x].code || pl[x].code == '') {} else N.addRow().setData(pl_row); }
} catch(err) { pl = null; };
}
if(N.rowCount() == 0) N.addRow();
ItemsList = function(json) {
var data = '';
for(var i=0; i<N.rowCount(); i++) {
data = data + '||||' + JSON.stringifyNoSecurity(N.row(i).getData());
}
return data;
}
/*
//create Product
productFL = new FormLoader();
productFL.load('EcmServices','Parents','productFL');
productFL.onResponseData = function(data) {
data.price = data.selling_price;
data.quantity = '1';
N.selectedRow.setData(data);
};
document.PositionList = N;
*/
}
);