402 lines
16 KiB
JavaScript
402 lines
16 KiB
JavaScript
(function() {
|
|
CmsBuys = (function() {
|
|
|
|
function CmsBuys(){
|
|
var _this = this;
|
|
|
|
CBase.oModule = CmsBuys;
|
|
|
|
_this.bindBaseActions();
|
|
$(".fadein").fadeIn();
|
|
}
|
|
|
|
CmsBuys.prototype.oValidator = {};
|
|
CmsBuys.prototype.bindBaseActions = function(){
|
|
var _this = this;
|
|
|
|
// Bind Chosen controllers if plugin exists
|
|
if($.fn.chosen){
|
|
|
|
// For chosen to search with LV characters
|
|
String.prototype.replaceLVSymbols = function(){
|
|
return this.replace(/[ēūīāšģļžčņ]/ig, function(m) {
|
|
var d = {
|
|
'ē':'e', 'ū':'u', 'ī':'i', 'ā':'a', 'š':'s', 'ģ':'g',
|
|
'ķ':'k', 'ļ':'l', 'ž':'z', 'č':'c', 'ņ':'n',
|
|
'Ē':'E', 'Ū':'U', 'Ī':'I', 'Ā':'A', 'Š':'S', 'Ģ':'G',
|
|
'Ķ':'K', 'Ļ':'L', 'Ž':'Z', 'Č':'C', 'Ņ':'N'
|
|
};
|
|
if(d[m]) return d[m];
|
|
return m;
|
|
});
|
|
};
|
|
|
|
$("select.chz").chosen({
|
|
width : "100%",
|
|
disable_search : false
|
|
});
|
|
|
|
var jqP = $("#data-cnt-top");
|
|
// Bind clear button
|
|
jqP.find("a.clear_search").click(function(){
|
|
// Clear hidden input val
|
|
jqP.find("li > input").val('');
|
|
|
|
// Clear all drop downs
|
|
jqP.find("select:not(#prod_partner)").each(function(){
|
|
$(this).find("option:not(:first)").remove();
|
|
});
|
|
jqP.find("select#prod_partner").find("option:selected").attr("selected", false);
|
|
jqP.find("select#prod_partner").find("option:first").attr("selected", true);
|
|
jqP.find("select").trigger("chosen:updated");
|
|
|
|
$(".search_buttons > a").fadeOut("fast");
|
|
});
|
|
|
|
_this.prodSearch();
|
|
}
|
|
|
|
|
|
// Bind date picker
|
|
jqP.parent().find("input.date").datepicker({
|
|
dateFormat: 'dd/mm/yy',
|
|
changeMonth: true,
|
|
changeYear: true
|
|
});
|
|
jqP.parent().find("span.dateico").click(function(){
|
|
$(this).parent().find("input").focus();
|
|
});
|
|
};
|
|
|
|
CmsBuys.prototype.prodSearch = function(){
|
|
var _this = this;
|
|
|
|
var timeout = null;
|
|
var oAjaxRef = false;
|
|
|
|
var jqObject = $(".form-data"),
|
|
jqSelects = jqObject.find(".chz");
|
|
|
|
// Add value in drop down search when opened from filter
|
|
jqSelects.not("#prod_partner").on("chosen:showing_dropdown", function (evt, params) {
|
|
var jqDD = params.chosen.dropdown;
|
|
jqDD.find(".chosen-search > input").val(jqDD.closest("li").find("> input").val());
|
|
});
|
|
|
|
// Bind key up in search
|
|
$("#prod_title_chosen").add("#prod_nr_chosen").find(":input").keyup(function(e){
|
|
clearTimeout(timeout);
|
|
|
|
var v = $(this).val();
|
|
var jqP = $(this).closest("li");
|
|
jqP.find("> input").val(v);
|
|
|
|
// If partner selected, return false, we don't do ajax send
|
|
if($("input#search_partner").val().length)
|
|
return false;
|
|
|
|
if(v.length > 1){
|
|
|
|
// If enter pressed, we set time out to zero for instant filter;
|
|
if(e.keyCode == 13)
|
|
e.timeoutMs = 0;
|
|
|
|
timeout = setTimeout(function(){
|
|
|
|
var jqUl = jqP.parent();
|
|
var oD = {};
|
|
jqP.parent().find("> li > input").each(function(){
|
|
oD[$(this).attr("id")] = $(this).val();
|
|
});
|
|
var oData = {
|
|
action : "getDropDownData",
|
|
data : $.toJSON(oD)
|
|
};
|
|
|
|
if(oAjaxRef) oAjaxRef.abort();
|
|
oAjaxRef = CBase.ajax(oData, false, function(d){
|
|
if(d && d.jsonok){
|
|
|
|
// Clear all drop downs
|
|
jqUl.find("select:not(#prod_partner)").each(function(){
|
|
$(this).find("option:not(:first)").remove();
|
|
});
|
|
|
|
// Add options
|
|
if(d.data){
|
|
|
|
var jqProdNr = jqUl.find("#prod_nr");
|
|
var jqProdTitle = jqUl.find("#prod_title");
|
|
|
|
$.each(d.data, function(id, val){
|
|
if(val.nr.length) jqProdNr.append('<option value="'+ val.id +'">' + val.nr + '</option>');
|
|
jqProdTitle.append('<option value="'+ val.id +'">' + val.prod_name + '</option>');
|
|
});
|
|
|
|
}
|
|
|
|
// Update chosen drop downs
|
|
jqUl.find("select").trigger("chosen:updated");
|
|
// Put in search filter value
|
|
jqP.find(".chosen-search > input").val(v);
|
|
|
|
$(".search_buttons > a").fadeIn("fast");
|
|
}
|
|
});
|
|
}, defaultVal(e.timeoutMs, 600));
|
|
}
|
|
|
|
return true;
|
|
});
|
|
|
|
// On select send ajax and load count to class
|
|
var jqDataCnt = $(".data-cnt");
|
|
jqSelects.not("#prod_partner").on("change", function(evt, params) {
|
|
$(this).find("option:selected").attr("selected", false);
|
|
if(params.selected && params.selected.length && !jqDataCnt.find("div#" + params.selected).length){
|
|
CBase.ajax({ action : "loadClassification", id : params.selected}, false, function(d){
|
|
if(d && d.jsonok && $.isDefined(d.html)){
|
|
var jqHtml = $(d.html);
|
|
jqDataCnt.append(jqHtml);
|
|
|
|
jqHtml.attr("id", params.selected);
|
|
_this.newCntBinds(jqHtml, d, params.selected);
|
|
|
|
jqHtml.find("div.prod-title > .ptitle").html(jqObject.find("select#prod_title option[value='"+ params.selected +"']").html());
|
|
|
|
// Set table title widths
|
|
var i = 1;
|
|
jqHtml.find(".list-data ul.dx.sbox:not(:hidden) > li:not(.id)").each(function(){
|
|
var iOWidth = $(this).outerWidth()+12;
|
|
jqHtml.find("ul.header li:nth-child("+i+")").css({width : iOWidth+"px"});
|
|
i++;
|
|
});
|
|
}
|
|
});
|
|
}
|
|
});
|
|
|
|
$("select#prod_partner").on("change", function(evt, params) {
|
|
var jqLi = $(this).closest("li");
|
|
var jqUl = $(this).closest("ul");
|
|
$(".search_buttons > a").fadeIn("fast");
|
|
if(params.selected && params.selected.length){
|
|
CBase.ajax({ action : "getProductsByPartnerId", id : params.selected}, false, function(d){
|
|
if(d && d.jsonok){
|
|
|
|
// Clear all drop downs
|
|
jqSelects.not("#prod_partner").each(function(){
|
|
$(this).find("option:not(:first)").remove();
|
|
});
|
|
|
|
// Add options
|
|
if(d.data){
|
|
var jqProdNr = jqUl.find("#prod_nr");
|
|
var jqProdTitle = jqUl.find("#prod_title");
|
|
|
|
$.each(d.data, function(id, val){
|
|
if(val.nr.length) jqProdNr.append('<option value="'+ val.id +'">' + val.nr + '</option>');
|
|
jqProdTitle.append('<option value="'+ val.id +'">' + val.prod_name + '</option>');
|
|
});
|
|
|
|
}
|
|
|
|
|
|
// Put in search filter value
|
|
jqLi.find("> input").val(params.selected);
|
|
|
|
// Update chosen drop downs
|
|
jqSelects.trigger("chosen:updated");
|
|
// Put in search filter value
|
|
//jqLi.find(".chosen-search > input").val(params.selected);
|
|
|
|
}
|
|
});
|
|
}
|
|
});
|
|
};
|
|
CmsBuys.prototype.newCntBinds = function(jqObj, d, id){
|
|
var _this = this;
|
|
|
|
_this.setupDivisionData(jqObj, d);
|
|
|
|
// Bind delete
|
|
jqObj.find(".tbl-buttons > a.button-cancel").click(function(){
|
|
jqObj.slideUp("fast", function(){
|
|
jqObj.remove();
|
|
});
|
|
});
|
|
|
|
// Bind validator
|
|
if(typeof(Validator) != "undefined" && $.isDefined(CBase.dt.vcfg))
|
|
_this.oValidator[id] = new Validator(CBase.dt.vcfg, "#" + id);
|
|
|
|
// Bind save
|
|
jqObj.find("#button-accept").click(function(){
|
|
_this.sendSaveAction(jqObj)
|
|
});
|
|
|
|
};
|
|
// Function to setup division data
|
|
CmsBuys.prototype.sendSaveAction = function(jqObj){
|
|
var _this = this;
|
|
var id = jqObj.attr("id");
|
|
if(id.length && _this.oValidator[id].isFormValid()) {
|
|
|
|
var oD = {};
|
|
jqObj.find("> div.list-data > ul.sbox:visible").each(function(){
|
|
var $i = $(this).attr("id");
|
|
oD[$i] = {};
|
|
|
|
$(this).find("li").find("input, select").each(function(){
|
|
if($(this).attr("id"))
|
|
oD[$i][$(this).attr("id")] = $(this).val();
|
|
});
|
|
});
|
|
|
|
oD['date'] = $("input#date").val();
|
|
|
|
var oData = {
|
|
action: "selfSave",
|
|
id: id,
|
|
fields: $.toJSON(oD)
|
|
};
|
|
|
|
CBase.ajax(oData, false, function (d) {
|
|
if (d && d.jsonok) {
|
|
|
|
// Add the green save line on title
|
|
jqObj.find(".ptitle").removeClass("er").addClass("saved");
|
|
setTimeout( function() {
|
|
jqObj.find(".ptitle").removeClass("saved");
|
|
}, 700);
|
|
|
|
// Send ajax for load classifications
|
|
CBase.ajax({ action : 'loadClassification', id : id}, false, function(d){
|
|
if(d && $.isDefined(d.html)){
|
|
|
|
var jqHtml = $(d.html);
|
|
|
|
jqObj.find("div.list-data").remove();
|
|
jqObj.find("ul.header").after(jqHtml.find("div.list-data"));
|
|
|
|
// Bind save
|
|
jqObj.find("#button-accept").click(function(){
|
|
_this.sendSaveAction(jqObj)
|
|
});
|
|
|
|
_this.setupDivisionData(jqObj, d);
|
|
|
|
$.each(oD, function(id, data){
|
|
var i = id.substr(1);
|
|
jqObj.find("ul#" + id).find("input#cprice" + i).val(data["cprice" + i]);
|
|
});
|
|
|
|
if(typeof(Validator) != "undefined" && $.isDefined(CBase.dt.vcfg))
|
|
_this.oValidator[id].bindSubs(jqHtml);
|
|
}
|
|
});
|
|
|
|
}else{
|
|
// Add the red error save line on title
|
|
jqObj.find(".ptitle").addClass("er");
|
|
jqObj.find(".ptitle").addClass("error");
|
|
setTimeout( function() {
|
|
jqObj.find(".ptitle").removeClass("error");
|
|
}, 700);
|
|
|
|
|
|
// Show error
|
|
if(d && d.data){
|
|
$.each(d.data, function(row, data){
|
|
$.each(data, function(key, errorMsg){
|
|
jqObj.find("input#" + key).parent().removeClass("v-ok").addClass("v-error").attr("title", errorMsg);
|
|
});
|
|
});
|
|
}
|
|
|
|
}
|
|
});
|
|
}
|
|
};
|
|
CmsBuys.prototype.setupDivisionData = function(jqObj, d){
|
|
var _this = this;
|
|
if(d && $.isDefined(d.data)){
|
|
for(var i in d.data){
|
|
if(d.data.hasOwnProperty(i)){
|
|
var o = _this.addNewCountClassification(jqObj, false);
|
|
|
|
// Disable the row if its not enabled
|
|
if(d.data[i]["d"] != '1') o.addClass("disabled");
|
|
|
|
o.find(".id input").val(i);
|
|
o.find(".count label").html(d.data[i]["c"]);
|
|
|
|
// Add class
|
|
if(parseInt(d.data[i]["c"]) > 0)
|
|
o.find(".count label").addClass("cok");
|
|
if(parseInt(d.data[i]["c"]) < 0)
|
|
o.find(".count label").addClass("ce");
|
|
|
|
// Write data in inputs
|
|
$.each(d.data[i], function(id, val){
|
|
if(o.find("li." + id)){
|
|
var jqLi = o.find("li." + id);
|
|
jqLi.find("input").val(val);
|
|
|
|
// If its count to class and has no val then we hide this li;
|
|
if(jqLi.hasClass("ft") && !val)
|
|
jqLi.css({visibility : "hidden"});
|
|
}
|
|
});
|
|
|
|
o.fadeIn("fast");
|
|
}
|
|
}
|
|
}
|
|
};
|
|
// Function to add new count classification
|
|
CmsBuys.prototype.addNewCountClassification = function(jqObj, show, bEmptRow){
|
|
var _this = this;
|
|
|
|
// Continue normal work
|
|
var o = jqObj.find(".list-data ul.dx:first").clone(true);
|
|
|
|
// Bind blur and fix to price
|
|
o.find("li.price input, li.incoming_price input").blur(function(){
|
|
_this.fix($(this));
|
|
});
|
|
|
|
o.hide();
|
|
|
|
jqObj.find("#buttons").before(o);
|
|
o.after("<br/>");
|
|
|
|
var n = 1;
|
|
while(jqObj.find("#count" + n).length > 0)
|
|
n++;
|
|
|
|
o.attr("id", "t" + n);
|
|
o.find("input, select").each(function(){
|
|
$(this).attr("name", $(this).attr("id") + n);
|
|
$(this).attr("id", $(this).attr("id") + n);
|
|
});
|
|
o.find("label i").html(n);
|
|
o.find(".v-ok, .v-error, .skip").removeClass("v-ok v-error skip");
|
|
|
|
if(typeof(show) !== "undefined" && show)
|
|
o.fadeIn("fast");
|
|
|
|
return o;
|
|
};
|
|
CmsBuys.prototype.fix = function(jqInput){
|
|
var v = jqInput.val();
|
|
v = parseFloat(v.replace(',', '.'));
|
|
jqInput.val((!isNaN(v) ? CBase.formatNumber(v,2,".","") : 0.00));
|
|
};
|
|
|
|
return CmsBuys;
|
|
})();
|
|
|
|
}).call(this);
|