var selectedInvoiceItemData = { data: null };

const SaleType = {
    IsSale: 'T',
    NotSale: 'F',
    Inventory: 'I',
    Wholesale: 'W'
};

function createOrderIdentsTableRows($element, data, decimalPlacesPrice, decimalPlacesQuntity, isVatObligee, orderType, isB2B, isRetailSale = false, showSaleCalc = true, editSaleCalc = true) {
    let timeStart = "";
    let timeEnd = "";
    let duration = "";
    let worker = "";
    let fixedAsset = "";
    let fixedAssetName = "";
    var priceWithMargin;

    if (data.Worker) worker = data.Worker.trim();
    if (data.StartTime) timeStart = getTime(data.StartTime);
    if (data.EndTime) timeEnd = getTime(data.EndTime);
    if (data.PrstTime) duration = convertMinutesToTime(data.PrstTime);
    if (data.FixedAsset) {
        fixedAsset = data.FixedAsset;
        fixedAssetName = getFixedAssetName(fixedAsset, allFixedAssets);
    }

    var identName = paw.utils.value.isSet(data.Name) ? data.Name.replace(/"/g, '&quot;') : data.Name;
    var customSelectClass = "custom-select-" + data.AnNo;
    var $row = $('<tr role="row" id="position-' + data.AnNo + '"></tr>');
    $row.append('<td><input id="anNo-' + data.AnNo + '" type="text" value="' + data.AnNo + '" class="form-control" readonly data-dtp="Write" data-dtp-state="disabled" /></td>');
    $row.append('<td ' + (isB2B ? "" : 'onclick="redirectToIdent(&quot;' + data.Ident + '&quot;)"') + '><input id="id-' + data.AnNo + '" type="text" value="' + data.Ident + '" title="' + data.Ident + '" class="form-control" readonly data-dtp="Write" data-dtp-state="disabled" /></td>');
    $row.append('<td><input id="name-' + data.AnNo + '" type="text" value="' + identName + '" title="' + identName + '" class="form-control" onblur="changeIdentData(' + data.AnNo + ')" autocomplete="off" ' + (isB2B ? "readonly" : "") + ' data-dtp="Write" data-dtp-state="disabled" /></td>');
    $row.append('<td><input id="quantity-' + data.AnNo + '" type="text" value="' + data.Quantity + '" onblur="changeIdentQuantity(' + data.AnNo + ')" step="' + decimalPlacesQuntity + '" min="0" class="form-control numberInput invoice-input editableInput text-right required" autocomplete="off" data-dtp="Write" data-dtp-state="disabled" /></td>');
    $row.append('<td><select id="measurementUnit-' + data.AnNo + '" value="' + data.UM + '" onchange="changeIdentData(' + data.AnNo + ')" class="form-control custom-select ' + customSelectClass + '" autocomplete="off" ' + (isB2B ? "readonly" : "") + ' data-dtp="Write" data-dtp-state="disabled">' + $("#measUnitOptions").html() + '</select></td>');

    if (showSaleCalc !== 'False') {
        if (orderType !== $("#OrderTypeBuyerEnum").val())
            $row.append('<td><input id="price-' + data.AnNo + '" type="text" value="' + data.Price + '" data-val="' + data.Price + '" onblur="changeIdentPrice(' + data.AnNo + ')" step="' + decimalPlacesPrice + '" min="0" class="form-control numberInput invoice-input editableInput text-right required editSaleCalc" autocomplete="off" ' + (isRetailSale === true ? "disabled" : "") + ' data-dtp="Write" data-dtp-state="disabled" /></td>');
        else
            $row.append('<td><div class="inputSelectContainer"><select id="selectedItemPriceSelect-' + data.AnNo + '" input-id="price-' + data.AnNo + '" value="' + data.Price + '"' +
                ' onchange="document.getElementById(' + "'price-" + data.AnNo + "'" + ').value=this.options[this.selectedIndex].value;changeIdentPrice(' + data.AnNo + ');formatNumberInput($(document.getElementById(' + "'price-" + data.AnNo + "'" + ')));"' +
                ' class="form-control custom-select editSaleCalc ' + customSelectClass + ' inputSelect"' + (editSaleCalc === 'False' ? "readonly" : "") +
                ' input-onblur="changeIdentPrice(' + data.AnNo + ')" input-step="' + decimalPlacesPrice + '" min="0" input-val="' + data.Price + '"' +
                (isB2B || isRetailSale ? ' input-disabled="true"' : '') +
                ' input-class="form-control numberInput invoice-input editableInput text-right inputOnSelect required editSaleCalc" autocomplete="off" ' + (isB2B || isRetailSale ? "readonly" : "") + ' data-dtp="Write" data-dtp-state="disabled" /></div></td> ');

        $row.append('<td><input id="rabate1-' + data.AnNo + '" type="text" value="' + data.Rebate1 + '" onblur="changeRebate(' + data.AnNo + ', 1)" error="' + '@Loc.RabateAmountValidation' + '" step="' + decimalPlacesPrice + '" class="form-control numberInput invoice-input editableInput text-right required editSaleCalc" autocomplete="off" data-dtp="Write" data-dtp-state="disabled" /></td>');
        $row.append('<td><input id="rabate2-' + data.AnNo + '" type="text" value="' + data.Rebate2 + '" onblur="changeRebate(' + data.AnNo + ', 2)" error="' + '@Loc.RabateAmountValidation' + '" step="' + decimalPlacesPrice + '" class="form-control numberInput invoice-input editableInput text-right required editSaleCalc" autocomplete="off" data-dtp="Write" data-dtp-state="disabled" /></td>');
        $row.append('<td><input id="rabate3-' + data.AnNo + '" type="text" value="' + data.Rebate3 + '" onblur="changeRebate(' + data.AnNo + ', 3)" error="' + '@Loc.RabateAmountValidation' + '" step="' + decimalPlacesPrice + '" class="form-control numberInput invoice-input editableInput text-right required editSaleCalc" autocomplete="off" data-dtp="Write" data-dtp-state="disabled" /></td>');
        $row.append('<td><input id="rabate-' + data.AnNo + '" type="text" value="' + data.Rebate + '" onblur="changeIdentData(' + data.AnNo + ')" error="' + '@Loc.RabateAmountValidation' + '" step="' + decimalPlacesPrice + '" data-min="0" data-max="100" class="form-control numberInput invoice-input editableInput text-right required editSaleCalc" autocomplete="off" ' + (isB2B ? "readonly" : "") + ' data-dtp="Write" data-dtp-state="disabled" readonly /></td>');
        $row.append('<td><select id="vat-' + data.AnNo + '" value="' + data.VATCode + '" onchange="changeIdentTax(' + data.AnNo + ')" class="form-control custom-select editSaleCalc ' + customSelectClass + '" autocomplete="off" ' + (isB2B ? "readonly" : "") + ' data-dtp="Write" data-dtp-state="disabled">' + $("#taxOptions").html() + '</select></td>');

        if (orderType === $("#OrderTypeBuyerEnum").val())
            priceWithMargin = data.LastPrice + (data.LastPrice / 100 * toNumber($("#AcctMinProfMarginOrder").val()));
        $row.append('<td><input id="value-' + data.AnNo + '" type="text" value="' + data.Value + '" onblur="onChangeIdentValue(' + data.AnNo + ')" step="' + decimalPlacesPrice + '" min="0" class="form-control numberInput invoice-input text-right editSaleCalc ' + (data.Value < priceWithMargin && orderType === $("#OrderTypeBuyerEnum").val() ? 'text-danger' : '') + '" autocomplete="off" ' + (isB2B ? "readonly" : "") + ' data-dtp="Write" data-dtp-state="disabled" /></td>');

        $row.append('<td class="' + (isB2B ? "hidden" : "") + '"><input id="lastPrice-' + data.AnNo + '" type="text" value="' + data.LastPrice + '" step="' + decimalPlacesPrice + '" min="0" class="form-control numberInput invoice-input text-right editSaleCalc" autocomplete="off" readonly data-dtp="Write" data-dtp-state="disabled" /></td>');
        $row.append('<td><input id="totalPrice-' + data.AnNo + '" type="text" value="' + data.TotalPrice + '" onblur="changeIdentTotalPrice(' + data.AnNo + ')" class="form-control numberInput invoice-input text-right editSaleCalc" step="' + decimalPlacesPrice + '" autocomplete="off" ' + (isB2B ? "readonly" : "") + ' data-dtp="Write" data-dtp-state="disabled" /></td>');
    }
    $row.append('<td><input id="qtyDispDoc-' + data.AnNo + '" type="text" value="' + data.QtyDispDoc + '" onblur="changeIdentData(' + data.AnNo + ')" step="' + decimalPlacesPrice + '" min="0" class="form-control numberInput invoice-input editableInput text-right required" autocomplete="off" ' + (isB2B ? "readonly" : "") + ' data-dtp="Write" data-dtp-state="disabled" /></td>');
    $row.append('<td><input id="deliveryDeadline-' + data.AnNo + '" type="text" value="' + (data.DeliveryDeadline ? data.DeliveryDeadline.format('DD.MM.YYYY') : "") + '" onchange="changeIdentData(' + data.AnNo + ')" class="form-control datepicker invoice-input editableInput text-right required" autocomplete="off" ' + (isB2B ? "readonly" : "") + ' data-dtp="Write" data-dtp-state="disabled" /></td>');

    $row.append('<td class="' + (isB2B ? "hidden" : "") + '"><select id="costDrv-' + data.AnNo + '" data-search-width="150" class="form-control select2withAPI form-select2 costDrvSelect" allow-clear="true"' +
        ' onchange="changeIdentData(' + data.AnNo + ')" data-url="' + $('#GetCostDrvUrl').val() + '" data-dtp="Write" data-dtp-state="disabled"><option value="' + (data.CostDrv !== null ? data.CostDrv : "") + '" selected>' + (data.CostDrv !== null ? data.CostDrv : "") + '</option>' +
        '</select></td>');

    $row.append('<td class="' + (isB2B ? "hidden" : "") + '"><select id="department-' + data.AnNo + '" value="' + data.Department + '" onchange="changeIdentData(' + data.AnNo + ')" class="form-control custom-select ' + customSelectClass + '  element-editable" data-dtp="Write" data-dtp-state="disabled">' + $("#departmentOptions").html() + '</select></td>');

    $row.append(`<td  class="` + (isB2B ? "hidden" : "") + `"><div class="dropdown color-picker-dropdown" data-toggle="dropdown" data-dtp="Write" data-dtp-state="disabled">
                    <p class="dropdown-toggle fa fa-chevron-down" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                        <div class="dropdown color-picker-box" id="input-main-color-box-${data.AnNo}" style="background-color: ${data.ColorCode}"></div>
                        <input type="hidden" id="colorCode-${data.AnNo}" value="${data.ColorCode}" />
                    </p>
                    <div class="dropdown-menu dropdown-menu-right rounded-0 box-shadow" data-row=${data.AnNo}>
                        <div class="row">` + $("#mainColors").html() + `</div>
                    </div>
                </div></td>`);
    $row.append('<td class="' + (isB2B ? "hidden" : "") + '"><input id="startTime-' + data.AnNo + '" type="time" value="' + timeStart + '" class="form-control" onchange="handleTimeInputs(' + data.AnNo + ', \`start\`)" autocomplete="off" data-dtp="Write" data-dtp-state="disabled" /></td>');
    $row.append('<td class="' + (isB2B ? "hidden" : "") + '"><input id="duration-' + data.AnNo + '" type="time" value="' + duration + '" class="form-control" onchange="handleTimeInputs(' + data.AnNo + ', \`duration\`)" autocomplete="off" data-dtp="Write" data-dtp-state="disabled" />' +
        '<input id="prstTime-' + data.AnNo + '" value="' + data.PrstTime + '" hidden data-dtp="Write" data-dtp-state="disabled" /></td>' +
        '<input id="prstUMTime-' + data.AnNo + '" value="' + data.PrstUMTime + '" hidden data-dtp="Write" data-dtp-state="disabled" /></td>');
    $row.append('<td class="' + (isB2B ? "hidden" : "") + '"><input id="endTime-' + data.AnNo + '" type="time" value="' + timeEnd + '" class="form-control" onchange="handleTimeInputs(' + data.AnNo + ', \`end\`)" autocomplete="off" data-dtp="Write" data-dtp-state="disabled" /></td>');
    $row.append('<td class="' + (isB2B ? "hidden" : "") + '"><select id="worker-' + data.AnNo + '" data-search-width="150" data-dropdown-height="200" class="form-control select2withAPI form-select2" allow-clear="true"' +
        ' onchange="changeIdentData(' + data.AnNo + ')" data-url="' + $('#GetWorkersUrl').val() + '" data-dtp="Write" data-dtp-state="disabled"><option value="' + (worker !== "" ? worker : "") + '" selected>' + (worker !== "" ? getFullName(worker, allWorkers) : "") + '</option>' +
        '</select></td>');
    $row.append('<td class="' + (isB2B ? "hidden" : "") + '"><select id="fixedAsset-' + data.AnNo + '" data-search-width="150" data-dropdown-height="200" class="form-control select2withAPI form-select2" allow-clear="true"' +
        ' onchange="changeIdentData(' + data.AnNo + ')" data-url="' + $('#GetFixedAssetsUrl').val() + '" data-dtp="Write" data-dtp-state="disabled"><option value="' + (fixedAsset !== "" ? fixedAsset : "") + '" selected>' + (fixedAssetName ? fixedAssetName : "") + '</option>' +
        '</select></td>');

    $row.append('<td><input id="packQty-' + data.AnNo + '" type="text" value="' + data.PackQty + '" onblur="changeIdentPackQty(' + data.AnNo + ')" step="' + decimalPlacesQuntity + '" min="0" class="form-control numberInput invoice-input editableInput text-right" autocomplete="off" data-dtp="Write" data-dtp-state="disabled" /></td>');
    if (orderType === $("#OrderTypeBuyerEnum").val() && showSaleCalc !== 'False')
        $row.append('<td><input id="retailPrice-' + data.AnNo + '" type="text" value="' + data.RetailPrice + '" onchange="changeIdentRetailPrice(' + data.AnNo + ')" class="form-control numberInput invoice-input text-right editSaleCalc" step="' + decimalPlacesPrice + '" autocomplete="off" ' + (isRetailSale === true ? "" : "disabled") + ' data-dtp="Write" data-dtp-state="disabled" /></td>');

    var $actions = $('<td class="actions-cell"></td>');
    var dropdownActions = '';

    // Part list
    if (data.SetItemType && (data.SetItemType === "P" || data.SetItemType === "U" || data.SetItemType === "G" || data.SetItemType === "H")) {
        dropdownActions =
            '<li class="dropdown-item">' +
            '<button type="button" class="btn btn-link" onclick="checkPartListModal(' + data.AnNo + ', ' + toNumber(data.QtySeries) + ')">' +
            '<i class="fa fa-solid fa-cube"></i>' +
            '<span class="m-l-xs">' + $("#SpecificationLoc").val() + '</span>' +
            '</button>' +
            '</li>';
    }

    // Serial numbers
    if (data.SerialNo && data.SerialNo !== "N" && orderType !== $("#OrderTypeBuyerEnum").val()) {
        dropdownActions +=
            '<li class="dropdown-item">' +
            '<button type="button" class="btn btn-link" onclick="checkIdentSerialNo(' + data.AnNo + ', \`O\`, null, null, \`true\`)">' +
            '<i class="serialNo-icon"></i>' +
            '<span class="m-l-xs">' + $("#SerialNumbersLoc").val() + '</span>' +
            '</button>' +
            '</li>';
    } else if (data.SerialNo && data.SerialNo !== "N" && orderType === $("#OrderTypeBuyerEnum").val()) {
        dropdownActions +=
            '<li class="dropdown-item">' +
            '<button type="button" class="btn btn-link" onclick="checkIdentSerialNoIssue(' + data.AnNo + ', \`O\`, null, null, \`true\`)">' +
            '<i class="serialNo-icon"></i>' +
            '<span class="m-l-xs">' + $("#SerialNumbersLoc").val() + '</span>' +
            '</button>' +
            '</li>';
    }

    $actions.append(
        '<div class="table-expand-button m-r-sm"><span data-toggle="dropdown" aria-expanded="true">' +
        '<i class="fas fa-ellipsis-h clickable-icon"></i>' +
        '</span>' +
        '<ul class="dropdown-menu">' +
        (dropdownActions && dropdownActions != '' ? dropdownActions : '') +
        '<li class="dropdown-item">' +
        '<button id="noteModal-' + data.AnNo + '" onclick="openNoteModal(' + data.AnNo + ')" type="button" class="identNote btn btn-link ' + (data.Note && data.Note.trim() !== "" ? "btn-active" : "") + '" data-dtp="Write" data-dtp-state="disabled">' +
        '<input id="note-' + data.AnNo + '" value="' + data.Note + '" class="hidden" autocomplete="off" data-dtp="Write" data-dtp-state="disabled">' +
        '<input id="acKey-' + data.AnNo + '" value="' + data.AcKey + '" class="hidden" autocomplete="off" data-dtp="Write" data-dtp-state="disabled" />' +
        '<input id="vatPercent-' + data.AnNo + '" value="' + data.VAT + '" class="hidden" data-dtp="Write" data-dtp-state="disabled" />' +
        '<input id="serialNo-' + data.AnNo + '" value="' + data.SerialNo + '" class="hidden" data-dtp="Write" data-dtp-state="disabled" />' +
        '<input id="dateDue-' + data.AnNo + '" value="' + data.DateDue + '" class="hidden" data-dtp="Write" data-dtp-state="disabled" />' +
        '<input id="qid-' + data.AnNo + '" value="' + data.Qid + '" class="hidden" data-dtp="Write" data-dtp-state="disabled" />' +
        '<i class="fa fa-comment"></i>' +
        '<span class="m-l-xs">' + $("#NoteLoc").val() + '</span>' +
        '</button>' +
        '</li>' +
        '<li role="separator" class="divider"></li>' +
        '<li class="dropdown-item">' +
        '<button type="button" id="identRemove-' + data.AnNo + '" onclick="removeIdentFromInvoice(' + data.AnNo + ')" class="btn btn-link editableInput" data-dtp="Delete" data-dtp-state="disabled">' +
        '<i class="far fa-trash-alt"></i>' +
        '<span class="m-l-xs">' + $("#DeleteLoc").val() + '</span>' +
        '</button>' +
        '</li>' +
        '</ul></div>'
    );

    $row.append($actions);

    $element.append($row);
    $('#vat-' + data.AnNo).val(data.VATCode);
    setDropdownSelectedValue("measurementUnit-" + data.AnNo, data.UM);
    setDropdownSelectedValue("department-" + data.AnNo, data.Department);
    $('#deliveryDeadline-' + data.AnNo).datepicker()
        .on('click', function (e) {
            $(this).select();
        }).on('changeDate', function () {
            $(this).datepicker('hide');
        });

    if (orderType === $("#OrderTypeBuyerEnum").val()) {
        if (!data.PricesForSelect) {
            var pricesForItem = [];
            pricesForItem.push({ Id: data.SetItemRTPrice, Text: formatDisplay($("#DefaultDecimalPlacesPrice").val(), data.SetItemRTPrice) + " - " + $("#SellingPriceLoc").val(), Rabate: 0, Selected: data.Price === data.SetItemRTPrice });
            pricesForItem.push({ Id: data.SetItemWSPrice, Text: formatDisplay($("#DefaultDecimalPlacesPrice").val(), data.SetItemWSPrice) + " - " + $("#WSPriceLoc").val(), Rabate: 0, Selected: data.Price === data.SetItemWSPrice });
            pricesForItem.push({ Id: data.SetItemWSPrice2, Text: formatDisplay($("#DefaultDecimalPlacesPrice").val(), data.SetItemWSPrice2) + " - " + $("#WS2PriceLoc").val(), Rabate: 0, Selected: data.Price === data.SetItemWSPrice2 });
            pricesForItem.push({ Id: data.SetItemPrStPrice, Text: formatDisplay($("#DefaultDecimalPlacesPrice").val(), data.SetItemPrStPrice) + " - " + $("#PrStPriceLoc").val(), Rabate: 0, Selected: data.Price === data.SetItemPrStPrice });
            pricesForItem.push({ Id: data.SetItemContractPrice, Text: formatDisplay($("#DefaultDecimalPlacesPrice").val(), data.SetItemContractPrice) + " - " + $("#ContractPriceLoc").val(), Rabate: 0, Selected: data.Price === data.SetItemContractPrice });
            data.PricesForSelect = pricesForItem;
        }
        addSelectOptions(data.PricesForSelect, "selectedItemPriceSelect-" + data.AnNo);
    }

    $("." + customSelectClass).customSelect();
    setInputSelectFocus();
    $element[0].scrollIntoView(false);
    handleTimeInputs(data.AnNo, true);
    initializeSelect2WithAPI("#costDrv-" + data.AnNo);
    if (editSaleCalc === 'False')
        $('.editSaleCalc').prop('readonly', true);

    return $row;
}

function createIdentsTableRows($element, data, saleType, decimalPlacesPrice, decimalPlacesQuntity, isVatObligee, roundT, roundPrice, loc, showSaleCalc = "True", editSaleCalc = "True") {
    var vatCode = (saleType === SaleType.IsSale || saleType === SaleType.Wholesale) ? data.VATCode : data.VATCodeTR;
    var customSelectClass = "custom-select-" + data.AnNo;
    var $row = $('<tr role="row" id="position-' + data.AnNo + '"></tr>');
    $row.append('<td><input id="anNo-' + data.AnNo + '" type="text" value="' + data.AnNo + '" class="form-control" readonly data-dtp="Write" data-dtp-state="disabled" /></td>');
    $row.append('<td onclick="redirectToIdent(&quot;' + data.Ident + '&quot;)"><input id="id-' + data.AnNo + '" type="text" value="' + data.Ident + '" title="' + data.Ident + '" class="form-control" readonly data-dtp="Write" data-dtp-state="disabled" /></td>');
    $row.append('<td><input id="name-' + data.AnNo + '" type="text" value="' + data.Name.replace(/"/g, '&quot;') + '" title="' + data.Name.replace(/"/g, '&quot;') + '" class="form-control element-editable" onblur="changeIdentData(' + data.AnNo + ')" autocomplete="off" data-dtp="Write" data-dtp-state="disabled" /></td>');
    $row.append('<td><input id="quantity-' + data.AnNo + '" type="text" value="' + data.Quantity + '" onblur="' + (saleType === SaleType.Wholesale || saleType === SaleType.NotSale ? 'changeIdentQuantity(' + data.AnNo + ')' : 'changeIdentData(' + data.AnNo + ')') + '" step="' + decimalPlacesQuntity + '" min="0" class="form-control numberInput invoice-input editableInput text-right required" autocomplete="off" data-dtp="Write" data-dtp-state="disabled" /></td>');
    $row.append('<td><select id="measurementUnit-' + data.AnNo + '" value="' + data.MeasurementUnit + '" onchange="changeIdentData(' + data.AnNo + ')" class="form-control custom-select ' + customSelectClass + '  element-editable" data-dtp="Write" data-dtp-state="disabled">' + $("#measUnitOptions").html() + '</select></td>');

    if (showSaleCalc !== 'False') {
        if (saleType !== SaleType.IsSale && saleType !== SaleType.Wholesale)
            $row.append('<td><input id="price-' + data.AnNo + '" type="text" value="' + data.Price + '" data-val="' + data.Price + '" onblur="changeIdentPrice(' + data.AnNo + ')" step="' + decimalPlacesPrice + '" min="0" class="form-control numberInput invoice-input editableInput text-right required editSaleCalc" autocomplete="off" data-dtp="Write" data-dtp-state="disabled" /></td>');
        else
            $row.append('<td><div class="inputSelectContainer"><select id="selectedItemPriceSelect-' + data.AnNo + '" input-id="price-' + data.AnNo + '" value="' + data.Price + '"' +
                ' onchange="document.getElementById(' + "'price-" + data.AnNo + "'" + ').value=this.options[this.selectedIndex].value;changeIdentData(' + data.AnNo + ');formatNumberInput($(document.getElementById(' + "'price-" + data.AnNo + "'" + ')));"' +
                ' class="form-control custom-select ' + customSelectClass + ' inputSelect "' + (editSaleCalc === 'False' ? "readonly" : "") +
                ' input-onblur="changeIdentPrice(' + data.AnNo + ')" input-step="' + decimalPlacesPrice + '" min="0" input-val="' + data.Price + '"' +
                ' input-class="form-control numberInput invoice-input editableInput text-right inputOnSelect required editSaleCalc" autocomplete="off" data-dtp="Write" data-dtp-state="disabled" /></div></td> ');
    }
    if (saleType === SaleType.NotSale && showSaleCalc !== 'False') {
        $row.append('<td><input id="priceCurrency-' + data.AnNo + '" type="text" value="' + data.PriceCurrency + '" onblur="changeIdentPriceCurrency(' + data.AnNo + ')" step="' + decimalPlacesPrice + '" min="0" class="form-control numberInput invoice-input editableInput text-right required editSaleCalc" autocomplete="off" data-dtp="Write" data-dtp-state="disabled" /></td>');
    }
    if ((saleType === SaleType.Wholesale || saleType === SaleType.NotSale) && showSaleCalc !== 'False') {
        $row.append('<td><input id="rabate1-' + data.AnNo + '" type="text" value="' + data.Rabate1 + '" onblur="changeRebate(' + data.AnNo + ', 1)" error="' + '@Loc.RabateAmountValidation' + '" step="' + decimalPlacesPrice + '" class="form-control numberInput invoice-input editableInput text-right required rabateInput editSaleCalc" autocomplete="off" data-dtp="Write" data-dtp-state="disabled" /></td>');
        $row.append('<td><input id="rabate2-' + data.AnNo + '" type="text" value="' + data.Rabate2 + '" onblur="changeRebate(' + data.AnNo + ', 2)" error="' + '@Loc.RabateAmountValidation' + '" step="' + decimalPlacesPrice + '" class="form-control numberInput invoice-input editableInput text-right required editSaleCalc" autocomplete="off" data-dtp="Write" data-dtp-state="disabled" /></td>');
        $row.append('<td><input id="rabate3-' + data.AnNo + '" type="text" value="' + data.Rabate3 + '" onblur="changeRebate(' + data.AnNo + ', 3)" error="' + '@Loc.RabateAmountValidation' + '" step="' + decimalPlacesPrice + '" class="form-control numberInput invoice-input editableInput text-right required editSaleCalc" autocomplete="off" data-dtp="Write" data-dtp-state="disabled" /></td>');
    }
    if (showSaleCalc !== 'False') {
        $row.append('<td><input id="rabate-' + data.AnNo + '" type="text" value="' + data.Rabate + '" onblur="changeIdentData(' + data.AnNo + ')" error="' + '@Loc.RabateAmountValidation' + '" step="' + decimalPlacesPrice + '" data-min="0" data-max="100" class="form-control numberInput invoice-input editableInput text-right required editSaleCalc" autocomplete="off" data-dtp="Write" data-dtp-state="disabled" ' + (saleType === SaleType.Wholesale || saleType === SaleType.NotSale ? "readonly" : "") + ' /></td>');
        if (isVatObligee !== "False") {
            $row.append('<td><select id="vat-' + data.AnNo + '" value="' + vatCode + '" onchange="changeIdentTax(' + data.AnNo + ')" class="form-control custom-select ' + customSelectClass + ' element-editable identVat editSaleCalc" data-dtp="Write" data-dtp-state="disabled">' + $("#taxOptions").html() + '</select></td>');
        }
        $row.append('<td><input id="priceForValue-' + data.AnNo + '" type="text" value="' + data.PriceForValue + '" step="' + decimalPlacesPrice + '" min="0" class="form-control numberInput invoice-input editableInput text-right editSaleCalc" readonly data-dtp="Write" data-dtp-state="disabled" /></td>');
        $row.append('<td><input id="basisForTax-' + data.AnNo + '" type="text" value="' + data.BasisForTax + '" step="' + decimalPlacesPrice + '" data-round="' + (roundT ? roundT : "") + '" min="0" class="form-control numberInput invoice-input editableInput text-right required editSaleCalc" autocomplete="off" readonly data-dtp="Write" data-dtp-state="disabled" /></td>');
        if (isVatObligee !== "False") {
            $row.append('<td><input id="taxPrice-' + data.AnNo + '" type="text" value="' + data.TaxPrice + '" step="' + decimalPlacesPrice + '" data-round="' + (roundT ? roundT : "") + '" min="0" class="form-control numberInput invoice-input editableInput text-right required element-editable editSaleCalc" readonly data-dtp="Write" data-dtp-state="disabled" /></td>');
        }
        if (saleType !== SaleType.Inventory)
            $row.append('<td><input id="totalPrice-' + data.AnNo + '" type="text" value="' + data.TotalPrice + '" onblur="changeIdentTotalPrice(' + data.AnNo + ')" class="form-control numberInput invoice-input text-right editSaleCalc" step="' + decimalPlacesPrice + '" data-round="' + (roundT ? roundT : "") + '" autocomplete="off" data-dtp="Write" data-dtp-state="disabled" /></td>');
        else
            $row.append('<td><input id="totalPrice-' + data.AnNo + '" type="text" value="' + data.TotalPrice + '" class="form-control numberInput invoice-input text-right editSaleCalc" step="' + decimalPlacesPrice + '" autocomplete="off" readonly data-dtp="Write" data-dtp-state="disabled" /></td>');
        if (saleType === SaleType.NotSale) {
            $row.append('<td><input id="valueCurrency-' + data.AnNo + '" type="text" value="' + data.ValueCurrency + '" onblur="changeIdentValueCurrency(' + data.AnNo + ')" step="' + decimalPlacesPrice + '" class="form-control numberInput invoice-input editableInput text-right required editSaleCalc" autocomplete="off" data-dtp="Write" data-dtp-state="disabled" /></td>');
        }
    }
    $row.append('<td><select id="costDrv-' + data.AnNo + '" data-search-width="150" class="form-control select2withAPI form-select2 costDrvSelect" allow-clear="true"' +
        ' onchange="changeIdentData(' + data.AnNo + ')" data-url="' + $('#GetCostDrvUrl').val() + '" data-dtp="Write" data-dtp-state="disabled"><option value="' + (data.CostDrv !== null ? data.CostDrv : "") + '" selected>' + (data.CostDrv !== null ? data.CostDrv : "") + '</option>"/>' +
        '</select></td>');

    if (saleType === SaleType.Wholesale && loc === $("#MeLoc").val())
        $row.append('<td><select id="exemptFromVATSType-' + data.AnNo + '" value="' + data.ExemptFromVATSType + '" onchange="changeIdentData(' + data.AnNo + ')" class="form-control custom-select ' + customSelectClass + '  element-editable" data-dtp="Write" data-dtp-state="disabled">' + $("#taxExemptOptions").html() + '</select></td>');

    if (saleType === SaleType.Wholesale && showSaleCalc !== 'False') {
        var priceRebate = data.Price - (data.Price * data.Rabate / 100);
        $row.append('<td><input id="retailPrice-' + data.AnNo + '" type="text" value="' + data.RTPrice + '" class="form-control numberInput invoice-input text-right editSaleCalc" step="' + decimalPlacesPrice + '" data-round="' + (roundT ? roundT : "") + '" autocomplete="off" readonly data-dtp="Write" data-dtp-state="disabled" /></td>');
        $row.append('<td><input id="priceRebate-' + data.AnNo + '" type="text" value="' + priceRebate + '" onblur="changePriceRebate(' + data.AnNo + ')" class="form-control numberInput invoice-input text-right editSaleCalc" step="' + decimalPlacesPrice + '" data-round="' + (roundT ? roundT : "") + '" autocomplete="off" data-dtp="Write" data-dtp-state="disabled" /></td>');
    }

    $row.append('<td><select id="department-' + data.AnNo + '" value="' + data.Department + '" onchange="changeIdentData(' + data.AnNo + ')" class="form-control custom-select ' + customSelectClass + '  element-editable" data-dtp="Write" data-dtp-state="disabled">' + $("#departmentOptions").html() + '</select></td>');

    var $actions = $('<td class="actions-cell"></td>');
    var dropdownActions = '';

    // Part list
    if (data.SetItemType && (saleType === SaleType.Wholesale || saleType === SaleType.NotSale) && (data.SetItemType === "P" || data.SetItemType === "U" || data.SetItemType === "G"
        || data.SetItemType === "H" || data.SetItemType === "C")) {
        dropdownActions =
            '<li class="dropdown-item">' +
            '<button type="button" class="btn btn-link" onclick="checkPartListModal(' + data.AnNo + ', \'' + data.SetItemType + '\')">' +
            '<i class="fa fa-solid fa-cube"></i>' +
            '<span class="m-l-xs">' + $("#SpecificationLoc").val() + '</span>' +
            '</button>' +
            '</li>';
    }

    // Serial numbers
    if (data.SerialNo && data.SerialNo !== "N" && saleType === SaleType.NotSale) {
        dropdownActions +=
            '<li class="dropdown-item">' +
            '<button type="button" class="btn btn-link" onclick="checkIdentSerialNo(' + data.AnNo + ', \`M\`, null, null, \`true\`)">' +
            '<i class="serialNo-icon"></i>' +
            '<span class="m-l-xs">' + $("#SerialNumbersLoc").val() + '</span>' +
            '</button>' +
            '</li>';
    } else if (data.SerialNo && data.SerialNo !== "N" && saleType === SaleType.Wholesale) {
        dropdownActions +=
            '<li class="dropdown-item">' +
            '<button type="button" class="btn btn-link" onclick="' + (data.Quantity >= 0 ? "checkIdentSerialNoIssue" : "checkIdentSerialNo") + '(' + data.AnNo + ', \`M\`, null, null, \`true\`)">' +
            '<i class="serialNo-icon"></i>' +
            '<span class="m-l-xs">' + $("#SerialNumbersLoc").val() + '</span>' +
            '</button>' +
            '</li>';
    }

    $actions.append(
        '<div class="table-expand-button m-r-sm"><span data-toggle="dropdown" aria-expanded="true">' +
        '<i class="fas fa-ellipsis-h clickable-icon"></i>' +
        '</span>' +
        '<ul class="dropdown-menu">' +
        (dropdownActions && dropdownActions != '' ? dropdownActions : '') +
        '<li class="dropdown-item">' +
        '<button id="noteModal-' + data.AnNo + '" onclick="openNoteModal(' + data.AnNo + ')" type="button" class="identNote btn btn-link ' + (data.Note && data.Note.trim() !== "" ? "btn-active" : "") + '" data-dtp="Write" data-dtp-state="disabled">' +
        '<input id="note-' + data.AnNo + '" value="' + data.Note + '" class="hidden" data-dtp="Write" data-dtp-state="disabled" />' +
        '<input id="acKey-' + data.AnNo + '" value="' + data.AcKey + '" class="hidden" data-dtp="Write" data-dtp-state="disabled" />' +
        '<input id="vatPercent-' + data.AnNo + '" value="' + data.VAT + '" class="hidden" data-dtp="Write" data-dtp-state="disabled" />' +
        '<input id="serialNo-' + data.AnNo + '" value="' + data.SerialNo + '" class="hidden" data-dtp="Write" data-dtp-state="disabled" />' +
        '<input id="dateDue-' + data.AnNo + '" value="' + data.DateDue + '" class="hidden" data-dtp="Write" data-dtp-state="disabled" />' +
        '<input id="qtyOld-' + data.AnNo + '" value="0" class="hidden" data-dtp="Write" data-dtp-state="disabled" />' +
        '<input id="qid-' + data.AnNo + '" value="' + data.Qid + '" class="hidden" data-dtp="Write" data-dtp-state="disabled" />' +
        '<i class="fa fa-comment"></i>' +
        '<span class="m-l-xs">' + $("#NoteLoc").val() + '</span>' +
        '</button>' +
        '</li>' +
        '<li role="separator" class="divider"></li>' +
        '<li class="dropdown-item">' +
        (saleType !== SaleType.Inventory ?
            ('<button type="button" id="identRemove-' + data.AnNo + '" onclick="removeIdentFromInvoice(' + data.AnNo + ')" class="btn btn-link editableInput" data-dtp="Delete" data-dtp-state="disabled">' +
                '<i class="far fa-trash-alt"></i>' +
                '<span class="m-l-xs">' + $("#DeleteLoc").val() + '</span>' +
                '</button>') : '') +
        '</li>' +
        '</ul></div>'
    );

    $row.append($actions);

    $element.append($row);
    $('#vat-' + data.AnNo).val(vatCode);
    setDropdownSelectedValue("measurementUnit-" + data.AnNo, data.MeasurementUnit);
    setDropdownSelectedValue("department-" + data.AnNo, data.Department);
    if (saleType === SaleType.Wholesale && loc === $("#MeLoc").val())
        setDropdownSelectedValue("exemptFromVATSType-" + data.AnNo, data.ExemptFromVATSType);

    if (saleType === SaleType.IsSale || saleType === SaleType.Wholesale) {
        if (!data.PricesForSelect) {
            var pricesForItem = [];
            pricesForItem.push({ Id: data.SetItemRTPrice, Text: formatDisplay($("#DefaultDecimalPlacesPrice").val(), data.SetItemRTPrice) + " - " + $("#SellingPriceLoc").val(), Rabate: 0, Selected: data.Price === data.SetItemRTPrice });
            pricesForItem.push({ Id: data.SetItemWSPrice, Text: formatDisplay($("#DefaultDecimalPlacesPrice").val(), data.SetItemWSPrice) + " - " + $("#WSPriceLoc").val(), Rabate: 0, Selected: data.Price === data.SetItemWSPrice });
            pricesForItem.push({ Id: data.SetItemWSPrice2, Text: formatDisplay($("#DefaultDecimalPlacesPrice").val(), data.SetItemWSPrice2) + " - " + $("#WS2PriceLoc").val(), Rabate: 0, Selected: data.Price === data.SetItemWSPrice2 });
            pricesForItem.push({ Id: data.SetItemPrStPrice, Text: formatDisplay($("#DefaultDecimalPlacesPrice").val(), data.SetItemPrStPrice) + " - " + $("#PrStPriceLoc").val(), Rabate: 0, Selected: data.Price === data.SetItemPrStPrice });
            pricesForItem.push({ Id: data.SetItemContractPrice, Text: formatDisplay($("#DefaultDecimalPlacesPrice").val(), data.SetItemContractPrice) + " - " + $("#ContractPriceLoc").val(), Rabate: 0, Selected: data.Price === data.SetItemContractPrice });
            data.PricesForSelect = pricesForItem;
        }
        addSelectOptions(data.PricesForSelect, "selectedItemPriceSelect-" + data.AnNo);
    }

    $("." + customSelectClass).customSelect();
    setInputSelectFocus();
    if (editSaleCalc === 'False')
        $('.editSaleCalc').prop('readonly', true);

    return $row;
}

function createTransferIdentsTableRows($element, data, saleType, decimalPlacesPrice, decimalPlacesQuntity, isVatObligee) {
    var customSelectClass = "custom-select-" + data.AnNo;
    var $row = $('<tr role="row" id="position-' + data.AnNo + '"></tr>');
    $row.append('<td><input id="anNo-' + data.AnNo + '" type="text" value="' + data.AnNo + '" class="form-control" readonly data-dtp="Write" data-dtp-state="disabled" /></td>');
    $row.append('<td onclick="redirectToIdent(&quot;' + data.Ident + '&quot;)"><input id="id-' + data.AnNo + '" type="text"  value="' + data.Ident + '" title="' + data.Ident + '" class="form-control" readonly data-dtp="Write" data-dtp-state="disabled" /></td>');
    $row.append('<td><input id="name-' + data.AnNo + '" type="text" value="' + data.Name.replace(/"/g, '&quot;') + '" title="' + data.Name.replace(/"/g, '&quot;') + '" class="form-control element-editable" onblur="changeIdentData(' + data.AnNo + ')" autocomplete="off" data-dtp="Write" data-dtp-state="disabled" /></td>');
    $row.append('<td><input id="quantity-' + data.AnNo + '" type="text" value="' + data.Quantity + '" onblur="changeIdentQuantity(' + data.AnNo + ')" step="' + decimalPlacesQuntity + '" min="0" class="form-control numberInput invoice-input editableInput text-right required" autocomplete="off" data-dtp="Write" data-dtp-state="disabled" /></td>');
    $row.append('<td><select id="measurementUnit-' + data.AnNo + '" value="' + data.MeasurementUnit + '" onchange="changeIdentData(' + data.AnNo + ')" class="form-control custom-select ' + customSelectClass + ' element-editable" data-dtp="Write" data-dtp-state="disabled">' + $("#measUnitOptions").html() + '</select></td>');
    $row.append('<td><input id="price-' + data.AnNo + '" type="text" value="' + data.Price + '" data-val="' + data.Price + '" onblur="changeIdentPrice(' + data.AnNo + ')" step="' + decimalPlacesPrice + '" min="0" class="form-control numberInput invoice-input editableInput text-right required" autocomplete="off" data-dtp="Write" data-dtp-state="disabled" /></td>');
    $row.append('<td><input id="rabate-' + data.AnNo + '" type="text" value="' + data.Rabate + '" onblur="changeIdentData(' + data.AnNo + ')" error="' + '@Loc.RabateAmountValidation' + '" step="' + decimalPlacesPrice + '" data-min="0" data-max="100" class="form-control numberInput invoice-input editableInput text-right required" autocomplete="off" data-dtp="Write" data-dtp-state="disabled" /></td>');
    if (isVatObligee !== "False") {
        $row.append('<td><select id="vat-' + data.AnNo + '" value="' + data.VATCode + '" onchange="changeIdentTax(' + data.AnNo + ')" class="form-control custom-select ' + customSelectClass + ' element-editable" data-dtp="Write" data-dtp-state="disabled">' + $("#taxOptions").html() + '</select></td>');
    }
    $row.append('<td><input id="priceRebate-' + data.AnNo + '" type="text" value="' + data.PriceRebate + '" onblur="changeIdentData(' + data.AnNo + ')" step="' + decimalPlacesPrice + '" min="0" class="form-control numberInput invoice-input editableInput text-right" readonly data-dtp="Write" data-dtp-state="disabled" /></td>');
    $row.append('<td><input id="priceForValue-' + data.AnNo + '" type="text" value="' + data.PriceForValue + '" onblur="changeIdentData(' + data.AnNo + ')" step="' + decimalPlacesPrice + '" min="0" class="form-control numberInput invoice-input editableInput text-right" readonly data-dtp="Write" data-dtp-state="disabled" /></td>');
    $row.append('<td><input id="value-' + data.AnNo + '" type="text" value="' + data.Value + '" onblur="changeIdentData(' + data.AnNo + ')" step="' + decimalPlacesPrice + '" min="0" class="form-control numberInput invoice-input editableInput text-right required" autocomplete="off" readonly data-dtp="Write" data-dtp-state="disabled" /></td>');

    $row.append('<td><select id="costDrv-' + data.AnNo + '" data-search-width="150" class="form-control select2withAPI form-select2 costDrvSelect" allow-clear="true"' +
        ' onchange="changeIdentData(' + data.AnNo + ')" data-url="' + $('#GetCostDrvUrl').val() + '" data-dtp="Write" data-dtp-state="disabled"><option value="' + (data.CostDrv !== null ? data.CostDrv : "") + '" selected>' + (data.CostDrv !== null ? data.CostDrv : "") + '</option>"/>' +
        '</select></td>');

    $row.append('<td><select id="department-' + data.AnNo + '" value="' + data.Department + '" onchange="changeIdentData(' + data.AnNo + ')" class="form-control custom-select ' + customSelectClass + '  element-editable" data-dtp="Write" data-dtp-state="disabled">' + $("#departmentOptions").html() + '</select></td>');

    var $actions = $('<td class="actions-cell"></td>');
    $actions.append(
        '<button id="noteModal-' + data.AnNo + '" onclick="openNoteModal(' + data.AnNo + ')" type="button" class="identNote btn btn-link ' + (data.Note && data.Note.trim() !== "" ? "btn-active" : "") + '" data-dtp="Write" data-dtp-state="disabled">' +
        '<i class="fa fa-comment"></i></button>' +
        '<input id="note-' + data.AnNo + '" value="' + data.Note + '" class="hidden" data-dtp="Write" data-dtp-state="disabled" />' +
        '<input id="acKey-' + data.AnNo + '" value="' + data.AcKey + '" class="hidden" data-dtp="Write" data-dtp-state="disabled" />' +
        '<input id="vatPercent-' + data.AnNo + '" value="' + data.VAT + '" class="hidden" data-dtp="Write" data-dtp-state="disabled" />' +
        '<input id="serialNo-' + data.AnNo + '" value="' + data.SerialNo + '" class="hidden" data-dtp="Write" data-dtp-state="disabled" />' +
        '<input id="dateDue-' + data.AnNo + '" value="' + data.DateDue + '" class="hidden" data-dtp="Write" data-dtp-state="disabled" />' +
        '<input id="qtyOld-' + data.AnNo + '" value="0" class="hidden" data-dtp="Write" data-dtp-state="disabled" />'
    );

    if (data.SerialNo && data.SerialNo !== "N")
        $actions.append('<span onclick="' + (data.Quantity >= 0 ? "checkIdentSerialNoIssue" : "checkIdentSerialNo") + '(' + data.AnNo + ', \`M\`, null, null, \`true\`)" class="btn serialNo-icon"></span>');

    $actions.append('<button type="button" id="identRemove-' + data.AnNo + '" onclick="removeIdentFromInvoice(' + data.AnNo + ')" class="btn btn-link editableInput" data-dtp="Delete" data-dtp-state="disabled"><span class="far fa-trash-alt"></span></button>');

    $row.append($actions);

    $element.append($row);
    $('#vat-' + data.AnNo).val(saleType === SaleType.IsSale ? data.VATCode : data.VATCodeTR);
    setDropdownSelectedValue("measurementUnit-" + data.AnNo, data.MeasurementUnit);
    setDropdownSelectedValue("department-" + data.AnNo, data.Department);
    $("." + customSelectClass).customSelect();
    setInputSelectFocus();
    $element[0].scrollIntoView(false);
    return $row;
}

function redirectToIdent(ident) {
    window.location = $("#IdentEditUrl").val() + "?id=" + ident;
}

function createAdvancePaymentIdentsTableRows($element, data, saleType, decimalPlacesPrice, decimalPlacesQuntity, isVatObligee, showSaleCalc = "True", editSaleCalc = "True") {
    var customSelectClass = "custom-select-" + data.AnNo;
    var $row = $('<tr role="row" id="position-' + data.AnNo + '"></tr>');
    $row.append('<td><input id="anNo-' + data.AnNo + '" type="text" value="' + data.AnNo + '" class="form-control" readonly data-dtp="Write" data-dtp-state="disabled" /></td>');
    $row.append('<td onclick="redirectToIdent(&quot;' + data.Ident + '&quot;)"><input id="id-' + data.AnNo + '" type="text" value="' + data.Ident + '" title="' + data.Ident + '" class="form-control" readonly data-dtp="Write" data-dtp-state="disabled" /></td>');
    $row.append('<td><input id="name-' + data.AnNo + '" type="text" value="' + data.Name.replace(/"/g, '&quot;') + '" title="' + data.Name.replace(/"/g, '&quot;') + '" class="form-control element-editable" onblur="changeIdentData(' + data.AnNo + ')" autocomplete="off" data-dtp="Write" data-dtp-state="disabled" /></td>');
    $row.append('<td><input id="quantity-' + data.AnNo + '" type="text" value="' + data.Quantity + '" onblur="changeIdentQuantity(' + data.AnNo + ')" step="' + decimalPlacesQuntity + '" min="0" class="form-control numberInput invoice-input editableInput text-right required" autocomplete="off" data-dtp="Write" data-dtp-state="disabled" /></td>');

    if (saleType === SaleType.IsSale) {
        $row.append('<td><select id="measurementUnit-' + data.AnNo + '" value="' + data.MeasurementUnit + '" onchange="changeIdentData(' + data.AnNo + ')" class="form-control custom-select element-editable" data-dtp="Write" data-dtp-state="disabled">' + $("#measUnitOptions").html() + '</select></td>');
    }

    if (showSaleCalc !== 'False') {
        $row.append('<td><input id="price-' + data.AnNo + '" type="text" value="' + data.Price + '" onblur="changeIdentData(' + data.AnNo + ')" step="' + decimalPlacesPrice + '" min="0" class="form-control numberInput invoice-input editableInput text-right required editSaleCalc" autocomplete="off" data-dtp="Write" data-dtp-state="disabled" /></td>');
        $row.append('<td><input id="rabate1-' + data.AnNo + '" type="text" value="' + data.Rabate1 + '" onblur="changeRebate(' + data.AnNo + ', 1)" error="' + '@Loc.RabateAmountValidation' + '" step="' + decimalPlacesPrice + '" class="form-control numberInput invoice-input editableInput text-right required rabateInput editSaleCalc" autocomplete="off" data-dtp="Write" data-dtp-state="disabled" /></td>');
        $row.append('<td><input id="rabate2-' + data.AnNo + '" type="text" value="' + data.Rabate2 + '" onblur="changeRebate(' + data.AnNo + ', 2)" error="' + '@Loc.RabateAmountValidation' + '" step="' + decimalPlacesPrice + '" class="form-control numberInput invoice-input editableInput text-right required editSaleCalc" autocomplete="off" data-dtp="Write" data-dtp-state="disabled" /></td>');
        $row.append('<td><input id="rabate3-' + data.AnNo + '" type="text" value="' + data.Rabate3 + '" onblur="changeRebate(' + data.AnNo + ', 3)" error="' + '@Loc.RabateAmountValidation' + '" step="' + decimalPlacesPrice + '" class="form-control numberInput invoice-input editableInput text-right required editSaleCalc" autocomplete="off" data-dtp="Write" data-dtp-state="disabled" /></td>');
        $row.append('<td><input id="rabate-' + data.AnNo + '" type="text" value="' + data.Rabate + '" onblur="changeIdentData(' + data.AnNo + ')" error="' + '@Loc.RabateAmountValidation' + '" step="' + decimalPlacesPrice + '" data-min="0" data-max="100" class="form-control numberInput invoice-input editableInput text-right required editSaleCalc" autocomplete="off" data-dtp="Write" data-dtp-state="disabled" /></td>');
        if (isVatObligee !== "False") {
            $row.append('<td><select id="vat-' + data.AnNo + '" value="' + data.VATCode + '" onchange="onChangeIdentVat(' + data.AnNo + ')" class="form-control element-editable custom-select editSaleCalc ' + customSelectClass + '" data-dtp="Write" data-dtp-state="disabled">' + $("#taxOptions").html() + '</select></td>');
        }
        $row.append('<td><input id="totalPrice-' + data.AnNo + '" type="text" value="' + data.SalePrice + '" onchange="onChangeIdentTotalPrice(' + data.AnNo + ')" class="form-control numberInput invoice-input text-right editSaleCalc" step="' + decimalPlacesPrice + '" autocomplete="off" data-dtp="Write" data-dtp-state="disabled" /></td>');
    }

    $row.append('<td><select id="costDrv-' + data.AnNo + '" data-search-width="150" class="form-control select2withAPI form-select2 costDrvSelect" allow-clear="true"' +
        ' onchange="changeIdentData(' + data.AnNo + ')" data-url="' + $('#GetCostDrvUrl').val() + '" data-dtp="Write" data-dtp-state="disabled"><option value="' + (data.CostDrv !== null ? data.CostDrv : "") + '" selected>' + (data.CostDrv !== null ? data.CostDrv : "") + '</option>"/>' +
        '</select></td>');

    $row.append('<td><select id="department-' + data.AnNo + '" value="' + data.Department + '" onchange="changeIdentData(' + data.AnNo + ')" class="form-control custom-select ' + customSelectClass + '  element-editable" data-dtp="Write" data-dtp-state="disabled">' + $("#departmentOptions").html() + '</select></td>');

    var $actions = $('<td class="actions-cell"></td>');
    $actions.append(
        '<button id="noteModal-' + data.AnNo + '" onclick="openNoteModal(' + data.AnNo + ')" type="button" class="identNote btn btn-link ' + (data.Note && data.Note.trim() !== "" ? "btn-active" : "") + '" data-dtp="Write" data-dtp-state="disabled">' +
        '<i class="fa fa-comment"></i></button>' +
        '<input id="note-' + data.AnNo + '" value="' + data.Note + '" class="hidden" data-dtp="Write" data-dtp-state="disabled" />' +
        '<input id="acKey-' + data.AnNo + '" value="' + data.AcKey + '" class="hidden" data-dtp="Write" data-dtp-state="disabled" />' +
        '<input id="vatPercent-' + data.AnNo + '" value="' + data.VAT + '" class="hidden" data-dtp="Write" data-dtp-state="disabled" />' +
        '<input id="priceForValue-' + data.AnNo + '" type="text" value="' + data.PriceForValue + '" step="' + decimalPlacesPrice + '" min="0" class="form-control numberInput invoice-input text-right hidden" readonly data-dtp="Write" data-dtp-state="disabled" />' +
        '<input id="basisForTax-' + data.AnNo + '" type="text" value="' + data.BasisForTax + '" step="' + decimalPlacesPrice + '" min="0" class="form-control numberInput invoice-input text-right hidden" autocomplete="off" readonly data-dtp="Write" data-dtp-state="disabled" />' +
        '<input id="taxPrice-' + data.AnNo + '" type="text" value="' + data.TaxPrice + '" step="' + decimalPlacesPrice + '" min="0" class="form-control numberInput invoice-input text-right hidden" readonly data-dtp="Write" data-dtp-state="disabled" />'
    );
    $actions.append('<button type="button" id="identRemove-' + data.AnNo + '" onclick="removeIdentFromInvoice(' + data.AnNo + ')" class="btn btn-link editableInput" data-dtp="Delete" data-dtp-state="disabled"><span class="far fa-trash-alt"></span></button>');

    $row.append($actions);

    $element.append($row);
    $('#vat-' + data.AnNo).val(saleType === SaleType.IsSale ? data.VATCode : data.VATCodeTR);
    if (saleType === SaleType.IsSale)
        setDropdownSelectedValue("measurementUnit-" + data.AnNo, data.MeasurementUnit);
    setDropdownSelectedValue("department-" + data.AnNo, data.Department);
    $("." + customSelectClass).customSelect();
    setInputSelectFocus();
    $element[0].scrollIntoView(false);
    if (editSaleCalc === 'False')
        $('.editSaleCalc').prop('readonly', true);

    return $row;
}

function createTableRowElement(element, id, className, value, readonly, role, type, onclick, onblur, appendToTd, step, selectOptions) {
    var el = document.createElement(element);
    el.autocomplete = "off";

    if (selectOptions)
        el.innerHTML = selectOptions;

    if (id) el.id = id;
    if (className) el.className = className;
    if (readonly) el.readOnly = readonly;
    if (role) el.role = role;
    if (type) el.type = type;
    if (step) el.step = step;
    if (onblur) el.addEventListener("blur", (event) => onblur());
    if (onclick) el.addEventListener("click", (event) => onclick());
    if (value || value === 0) {
        el.value = value;
        el.title = value;
    }

    if (appendToTd) {
        var td = document.createElement('td');
        td.appendChild(el);

        return td;
    } else
        return el;
}

function handleTimeInputs(rowNum, changedValue) {
    let startTime = $("#startTime-" + rowNum).val();
    let duration = $("#duration-" + rowNum).val();
    let endTime = $("#endTime-" + rowNum).val();
    let startMinutes = 0;
    let durationMinutes = 0;

    if (changedValue == "start" || changedValue == "duration") {
        startMinutes = convertTimeToMinutes(startTime);
        durationMinutes = convertTimeToMinutes(duration);
        endTime = convertMinutesToTime(startMinutes + durationMinutes);
        $("#prstTime-" + rowNum).val(durationMinutes);
        $("#endTime-" + rowNum).val(endTime);
    }
    else if (changedValue == "end") {
        startMinutes = convertTimeToMinutes(startTime);
        endMinutes = convertTimeToMinutes(endTime);
        durationMinutes = endMinutes - startMinutes;
        duration = convertMinutesToTime(durationMinutes);
        $("#prstTime-" + rowNum).val(durationMinutes);
        $("#duration-" + rowNum).val(duration);
    }

    setChangedIdentData(rowNum);
}