dojo.provide("parus.balanceStatusModel");

dojo.require("parus.parus8");
dojo.require("parus.balance");

dojo.require("parus.statusModelRouteDialog");
dojo.require("parus.statusModelNoteDialog");
dojo.require("parus.statusModelCoWorkingDialog");

if (!parus) parus = {};

if (!parus.balance) dojo.mixin(parus, {
  balance: {}
});

dojo.mixin(parus.balance, {

  statusModelColumns: function() {

    var structure = [];

    structure.push({
      field: "ST$SFULLCODE",
      name: "Номер события (УДП)",
      formatter: parus.balance.formatStatusModelFullCodeValue,
      width: "121px",
      sortField: "regexp_substr(dbms_lob.substr(xml_STATUSMODEL, 4000, 1), '<SFULLCODE>.*</SFULLCODE>')"
    });

    structure.push({
      field: "ST$SEVENT_TYPE",
      name: "Статусная модель (УДП)",
      formatter: parus.balance.formatStatusModelEventTypeValue,
      width: "121px",
      sortField: "regexp_substr(dbms_lob.substr(xml_STATUSMODEL, 4000, 1), '<SEVENT_TYPE>.*</SEVENT_TYPE>')"
    });


    structure.push({
      field: "ST$SEVENT_STAT",
      name: "Статус (УДП)",
      formatter: parus.balance.formatStatusModelStatusValue,
      width: "121px",
      sortField: "regexp_substr(dbms_lob.substr(xml_STATUSMODEL, 4000, 1), '<SEVENT_STAT>.*</SEVENT_STAT>')"
    });

    structure.push({
      field: "ST$NCOWORKING",
      name: "Совместное исполнение (УДП)",
      formatter: parus.balance.formatStatusModelCoworkingValue,
      width: "81px",
      sortField: "regexp_substr(dbms_lob.substr(xml_STATUSMODEL, 4000, 1), '<NCOWORKING>.*</NCOWORKING>')"
    });

    structure.push({
      field: "ST$SEXECUTER",
      name: "Исполнитель (УДП)",
      dataType: "1",
      formatter: parus.balance.formatStatusModelExecuterValue,
      width: "181px",
      sortField: "regexp_substr(dbms_lob.substr(xml_STATUSMODEL, 4000, 1), '<SEXECUTER>.*</SEXECUTER>')"
    });

    structure.push({
      field: "ST$NCOEXECUTER",
      name: "Пользователь-соисполнитель (УДП)",
      dataType: "1",
      formatter: parus.balance.formatStatusModelCoExecuterValue,
      width: "81px",
      sortField: "regexp_substr(dbms_lob.substr(xml_STATUSMODEL, 4000, 1), '<NCOEXECUTER>.*</NCOEXECUTER>')"
    });

    structure.push({
      field: "ST$SNOTE",
      name: "Примечание к статусу (УДП)",
      dataType: "1",
      formatter: parus.balance.formatStatusModelNoteValue,
      width: "181px",
      sortField: "regexp_substr(dbms_lob.substr(xml_STATUSMODEL, 4000, 1), '<SNOTE>.*</SNOTE>')"
    });

    return structure;

  },

  getStatusModelValue: function(grid, rowIndex, fieldName) {
    var item = grid.getItem(rowIndex);
    var rowNodes = parus.parus8.rowNodes(parus.parus8.nodeField(item, "XML_STATUSMODEL"));
    if (!rowNodes) return null;
    if (!rowNodes.length) return null;
    return parus.parus8.nodeField(rowNodes[0], fieldName);
  },

  formatStatusModelFullCodeValue: function(data, rowIndex, column) {
    return parus.balance.getStatusModelValue(column.grid, rowIndex, "SFULLCODE");
  },

  formatStatusModelEventTypeValue: function(data, rowIndex, column) {
    return parus.balance.getStatusModelValue(column.grid, rowIndex, "SEVENT_TYPE");
  },

  formatStatusModelStatusValue: function(data, rowIndex, column) {
    return parus.balance.getStatusModelValue(column.grid, rowIndex, "SEVENT_STAT");
  },

  formatStatusModelCoworkingValue: function(data, rowIndex, column) {
    var value = parus.balance.getStatusModelValue(column.grid, rowIndex, "NCOWORKING");
    if (value == null) return null;
    return parus.parus8.formatBoolean(value);
  },

  formatStatusModelExecuterValue: function(data, rowIndex, column) {
    return parus.balance.getStatusModelValue(column.grid, rowIndex, "SEXECUTER");
  },

  formatStatusModelCoExecuterValue: function(data, rowIndex, column) {
    var value = parus.balance.getStatusModelValue(column.grid, rowIndex, "NCOEXECUTER");
    if (value == null) return null;
    return parus.parus8.formatBoolean(value);
  },

  formatStatusModelNoteValue: function(data, rowIndex, column) {
    return parus.balance.getStatusModelValue(column.grid, rowIndex, "SNOTE");
  },

  checkStatusModel: function(grid, rowIndex) {
    var value = this.getStatusModelValue(grid, rowIndex, "SEVENT_TYPE");
    if (value == null || value === "") {
      parus.parus8.errorMessage("Статусная модель не установлена для отчета.");
      return false;
    }
    else return true;
  },

  statusModelGridPass: function(grid) {
    var store = grid.store;
    var index = grid.selection.getSelectedIndex();
    var item = grid.getItem(index);

    if (!this.checkStatusModel(grid, index)) return;

    parus.balance.statusModelPass(store.getValue(item, "RN"));
  },

  statusModelGridSend: function(grid) {
    var store = grid.store;
    var index = grid.selection.getSelectedIndex();
    var item = grid.getItem(index);

    if (!this.checkStatusModel(grid, index)) return;

    parus.balance.statusModelSend(store.getValue(item, "RN"));
  },

  statusModelGridAddNote: function(grid) {
    var store = grid.store;
    var index = grid.selection.getSelectedIndex();
    var item = grid.getItem(index);

    if (!this.checkStatusModel(grid, index)) return;

    parus.balance.statusModelAddNote(store.getValue(item, "RN"));
  },

  statusModelGridSetMark: function(grid) {
    var store = grid.store;
    var index = grid.selection.getSelectedIndex();
    var item = grid.getItem(index);

    if (!this.checkStatusModel(grid, index)) return;

    parus.balance.statusModelSetMark(store.getValue(item, "RN"));
  },

  statusModelGridRemoveMark: function(grid) {
    var store = grid.store;
    var index = grid.selection.getSelectedIndex();
    var item = grid.getItem(index);

    if (!this.checkStatusModel(grid, index)) return;

    parus.balance.statusModelRemoveMark(store.getValue(item, "RN"));
  },

  statusModelGridHistory: function(grid) {
    var store = grid.store;
    var index = grid.selection.getSelectedIndex();
    var item = grid.getItem(index);

    if (!this.checkStatusModel(grid, index)) return;

    parus.balance.statusModelHistory(store.getValue(item, "RN"));
  },

  statusModelGridNotes: function(grid) {
    var store = grid.store;
    var index = grid.selection.getSelectedIndex();
    var item = grid.getItem(index);

    if (!this.checkStatusModel(grid, index)) return;

    parus.balance.statusModelNotes(store.getValue(item, "RN"));
  },

  statusModelGridRoute: function(grid) {
    var store = grid.store;
    var index = grid.selection.getSelectedIndex();
    var item = grid.getItem(index);

    if (!this.checkStatusModel(grid, index)) return;

    parus.balance.statusModelRoute(store.getValue(item, "RN"));
  },

  statusModelGridCoWorking: function(grid) {
    var store = grid.store;
    var index = grid.selection.getSelectedIndex();
    var item = grid.getItem(index);

    if (!this.checkStatusModel(grid, index)) return;

    parus.balance.statusModelCoWorking(store.getValue(item, "RN"));
  },

  getStatusModelEventInfo: function(rn) {
    var response = parus.database.execute("getStatusModelEvent", {
      UNITCODE: "BalanceReports",
      RN: rn
    });

    var eventRN = parus.parus8.nodeField(parus.parus8.rowNodes(response)[0], "NEVENT");
    if (eventRN != null) {
      return {
        event: eventRN,
        eventCode: parus.parus8.nodeField(parus.parus8.rowNodes(response)[0], "SEVENT"),
        eventType: parus.parus8.nodeField(parus.parus8.rowNodes(response)[0], "NEVENT_TYPE"),
        eventTypeCode: parus.parus8.nodeField(parus.parus8.rowNodes(response)[0], "SEVENT_TYPE"),
        eventStatus: parus.parus8.nodeField(parus.parus8.rowNodes(response)[0], "NEVENT_STAT"),
        eventStatusCode: parus.parus8.nodeField(parus.parus8.rowNodes(response)[0], "SEVENT_STAT"),
        initPerson: parus.parus8.nodeField(parus.parus8.rowNodes(response)[0], "SINIT_PERSON"),
        initAuthName: parus.parus8.nodeField(parus.parus8.rowNodes(response)[0], "SINIT_AUTHNAME"),
        clientClient: parus.parus8.nodeField(parus.parus8.rowNodes(response)[0], "SCLIENT_CLIENT"),
        clientPerson: parus.parus8.nodeField(parus.parus8.rowNodes(response)[0], "SCLIENT_PERSON"),
        sendPerson: parus.parus8.nodeField(parus.parus8.rowNodes(response)[0], "SSEND_PERSON"),
        sendUserName: parus.parus8.nodeField(parus.parus8.rowNodes(response)[0], "SSEND_USER_NAME"),
        point: parus.parus8.nodeField(parus.parus8.rowNodes(response)[0], "NPOINT"),
        closed: parus.parus8.nodeField(parus.parus8.rowNodes(response)[0], "NCLOSED"),

        togetherSign: parus.parus8.nodeField(parus.parus8.rowNodes(response)[0], "TOGETHER_SIGN"),
        addNoteOnSend: parus.parus8.nodeField(parus.parus8.rowNodes(response)[0], "ADDNOTE_ONSEND")
      };
    }
    else {
      parus.parus8.errorMessage("Статусная модель не установлена для отчета.");
    }
  },

  statusModelPass: function(rn, onDoneCallback) {
    var event = this.getStatusModelEventInfo(rn);
    if (event == null) return;

    var dictionaryInfo = parus.dictionaries["BalanceStatusModelPassNextStatus"];

    dojo.mixin(dictionaryInfo, {
      dictionary: dictionaryInfo,
      unitCode: dictionaryInfo.unitCode,
      gridConditions: {
        SEVENT_TYPE: event.eventTypeCode,
        SEVENT_STATUS: event.eventStatusCode
      },
      gridOnComplete: function(store, items) {
        if (items.length == 0) {
          parus.parus8.errorMessage("Отсутствуют точки, доступные для перехода из текущей точки маршрута.");
          this.close();
        }
      },
      onApply: dojo.hitch(this, function(dialog) {
        var nextStatusCode = dialog.getValue();

        //var needNoteOnReturn = dialog.getFieldValue("ADDNOTE_ONRET") == "1";???
        var needNoteOnChangeState = dialog.getFieldValue("ADDNOTE_ONCHST") == "1";
        var needNoteOnChangeStateOfPoint = dialog.getFieldValue("NEXT_ADDNOTE_ONCHST") == "1";
        var needNote = /*needNoteOnReturn ||*/
        needNoteOnChangeState || needNoteOnChangeStateOfPoint;

        var sendToDefaultExecutor = dialog.getFieldValue("DEFAULT_EXEC") == "1";

        this._statusModelPassToSpecifiedStatus(rn, event, nextStatusCode, needNote, sendToDefaultExecutor, onDoneCallback);

        return true;
      })
    });

    (new parus.dictionaryDialog(dictionaryInfo)).show();
  },

  _statusModelPassToSpecifiedStatus: function(rn, event, statusCode, needNote, sendToDefaultExecutor, onDoneCallback) {
    var changeStatusProc = dojo.hitch(this, function(executor) {
      this._checkNeedNoteAndExecuteAction(event, needNote, dojo.hitch(this, function(noteHeader, note) {
        this._changeStatusModelEventState(event.event, statusCode, executor, needNote, noteHeader, note);

        parus.balance.refreshGrid(parus.balance.primaryMasterGrid, "RN", rn);

        if (onDoneCallback) onDoneCallback();
      }));
    });

    if (sendToDefaultExecutor) changeStatusProc(null);
    else {
      this._selectStatusModelPointExecutors(event, statusCode, false, dojo.hitch(this, function(selectedExecutor) {
        changeStatusProc(selectedExecutor);
      }));
    }
  },

  statusModelSend: function(rn, event, onDoneCallback) {
    if (!event) {
      event = this.getStatusModelEventInfo(rn);
      if (event == null) return;
    }

    this._selectStatusModelPointExecutors(event, event.eventStatusCode, true, dojo.hitch(this, function(selectedExecutor) {
      if (event.addNoteOnSend == null) {
        parus.parus8.errorMessage("Не удалось получить информацию о точке маршрута.");
        return;
      }
      var needNote = event.addNoteOnSend == "1";

      this._checkNeedNoteAndExecuteAction(event, needNote, dojo.hitch(this, function(noteHeader, note) {
        this._sendStatusModelEvent(event.event, selectedExecutor, needNote, noteHeader, note);

        parus.balance.refreshGrid(parus.balance.primaryMasterGrid, "RN", rn);

        if (onDoneCallback) onDoneCallback();
      }));
    }));
  },

  statusModelAddNote: function(rn, event) {
    if (!event) {
      event = this.getStatusModelEventInfo(rn);
      if (event == null) return;
    }

    (new parus.statusModelNoteDialog({
      event: event.event,
      eventTypeRN: event.eventType,

      onApply: dojo.hitch(this, function(dialog) {
        var rn = dialog.insertNote();
        if (rn) parus.balance.refreshGrid(parus.balance.primaryMasterGrid, "RN", rn);
        return !!rn;
      })
    })).show();
  },

  statusModelSetMark: function(rn, event, onDoneCallback) {
    if (!event) {
      event = this.getStatusModelEventInfo(rn);
      if (event == null) return;
    }

    if (event.togetherSign != "1") {
      parus.parus8.errorMessage("Текущая точка маршрута не предполагает Совместное исполнение.");
      return;
    }

    var response = parus.database.execute("getStatusModelEventPointByStatus", {
      SEVENT_TYPE: event.eventTypeCode,
      SEVENT_STATUS: event.eventStatusCode
    });

    var point = parus.parus8.nodeField(parus.parus8.rowNodes(response)[0], "NPOINT");
    if (point != null) {
      var dictionaryInfo = parus.dictionaries["BalanceStatusModelNextPerformMarks"];

      dojo.mixin(dictionaryInfo, {
        dictionary: dictionaryInfo,
        unitCode: dictionaryInfo.unitCode,
        gridConditions: {
          EVENT: point
        },
        onApply: dojo.hitch(this, function(dialog) {
          var performMark = dialog.getValue();
          var performMarkCode = dialog.getLabel();
          var needNote = dialog.getFieldValue("NOTE_ADD_SIGN") == "1";

          this._checkNeedNoteAndExecuteAction(event, needNote, dojo.hitch(this, function(noteHeader, note) {
            this._setStatusModelEventPerformMark(event.event, performMark, performMarkCode, needNote, noteHeader, note);

            if (onDoneCallback) onDoneCallback();
          }));

          return true;
        })
      });

      (new parus.dictionaryDialog(dictionaryInfo)).show();
    }
  },

  statusModelRemoveMark: function(rn, event, needConfirm, onDoneCallback) {
    if (!event) {
      event = this.getStatusModelEventInfo(rn);
      if (event == null) return;
    }

    if (event.togetherSign != "1") {
      parus.parus8.errorMessage("Текущая точка маршрута не предполагает Совместное исполнение.");
      return;
    }

    if (!needConfirm) {

      parus.database.execute("removeStatusModelEventPerformMark", {
        EVENT: event.event
      });
      if (onDoneCallback) onDoneCallback();

    }
    else {
      (new parus.confirmationDialog({
        message: "Снять отметку об исполнении?",
        onApply: dojo.hitch(this, function() {

          parus.database.execute("removeStatusModelEventPerformMark", {
            EVENT: event.event
          });
          if (onDoneCallback) onDoneCallback();

          return true;
        })
      })).show();
    }
  },

  statusModelHistory: function(rn, event) {
    if (!event) {
      event = this.getStatusModelEventInfo(rn);
      if (event == null) return;
    }

    var dictionaryInfo = parus.dictionaries["BalanceStatusModelHistory"];

    dojo.mixin(dictionaryInfo, {
      dictionary: dictionaryInfo,
      unitCode: dictionaryInfo.unitCode,
      hideCancel: true,
      gridConditions: {
        EVENT: event.event
      }
    });

    (new parus.dictionaryDialog(dictionaryInfo)).show();
  },

  statusModelNotes: function(rn, event, forSpecifiedEventStatusOnly) {
    if (!event) {
      event = this.getStatusModelEventInfo(rn);
      if (event == null) return;
    }

    var isCurrentStatus = forSpecifiedEventStatusOnly == null || forSpecifiedEventStatusOnly == event.eventStatus;

    var dictionaryInfo = parus.dictionaries["BalanceStatusModelNotes"];

    var dialog;

    var menu = new dijit.Menu({
      "class": "balanceMenu"
    });

    if (isCurrentStatus) {
      menu.addChild(new dijit.MenuItem({
        label: "Добавить...",
        itemAction: false,
        defaultIcon: "PCHardwareadaltCustomIcon",
        onClick: dojo.hitch(this, function() {
          (new parus.statusModelNoteDialog({
            event: event.event,
            eventTypeRN: event.eventType,

            onApply: dojo.hitch(this, function(editDialog) {
              var rn = editDialog.insertNote();
              if (rn) parus.balance.refreshGrid(dialog.grid, "RN", rn);
              return !!rn;
            })
          })).show();
        })
      }));

      menu.addChild(new dijit.MenuItem({
        label: "Размножить...",
        itemAction: true,
        defaultIcon: "PCcopyCustomIcon",
        onClick: dojo.hitch(this, function() {
          var store = dialog.grid.store;
          var item = dialog.grid.selection.getFirstSelected();

          (new parus.statusModelNoteDialog({
            event: event.event,
            eventTypeRN: event.eventType,
            noteHeader: store.getValue(item, "NOTE_HEADER"),
            note: store.getValue(item, "NOTE"),

            onApply: dojo.hitch(this, function(editDialog) {
              var rn = editDialog.insertNote();
              if (rn) parus.balance.refreshGrid(dialog.grid, "RN", rn);
              return !!rn;
            })
          })).show();
        })
      }));
    }

    menu.addChild(new dijit.MenuItem({
      label: "Исправить...",
      itemAction: true,
      "default": true,
      defaultIcon: "PCdocumentCustomIcon",
      onClick: dojo.hitch(this, function() {
        var store = dialog.grid.store;
        var item = dialog.grid.selection.getFirstSelected();

        (new parus.statusModelNoteDialog({
          event: event.event,
          RN: store.getValue(item, "RN"),
          noteHeader: store.getValue(item, "NOTE_HEADER"),
          note: store.getValue(item, "NOTE"),

          onApply: dojo.hitch(this, function(editDialog) {
            var updated = editDialog.updateNote();
            if (updated) parus.balance.refreshGrid(dialog.grid, "RN", store.getValue(item, "RN"));
            return updated;
          })
        })).show();
      })
    }));

    menu.addChild(new dijit.MenuItem({
      label: "Удалить",
      itemAction: true,
      defaultIcon: "PCreduceCustomIcon",
      onClick: dojo.hitch(this, function() {

        (new parus.confirmationDialog({
          message: "Удалить запись (записи)?",

          onApply: dojo.hitch(this, function() {
            var store = dialog.grid.store;
            var item = dialog.grid.selection.getFirstSelected();
            for (var i = 0; i < dialog.grid.selection.getSelectedCount(); i++) {
              parus.database.execute("deleteStatusModelNote", {
                RN: store.getValue(item, "RN")
              });
              item = dialog.grid.selection.getNextSelected(item);
            };

            dialog.grid.selection.deselectAll();
            parus.balance.refreshGrid(dialog.grid);
            return true;
          })
        })).show();
      })
    }));

    menu.startup();

    dojo.mixin(dictionaryInfo, {
      dictionary: dictionaryInfo,
      unitCode: dictionaryInfo.unitCode,
      multiselect: true,
      hideCancel: true,
      gridConditions: {
        EVENT: event.event,
        EVENT_STAT: forSpecifiedEventStatusOnly ? forSpecifiedEventStatusOnly : null
      },
      menu: menu
    });

    dialog = new parus.dictionaryDialog(dictionaryInfo);
    dialog.show();
  },

  statusModelRoute: function(rn) {
    var event = this.getStatusModelEventInfo(rn);
    if (event == null) return;

    var dialog = new parus.statusModelRouteDialog({
      documentRN: rn,
      event: event
    });
    dialog.show();
  },

  statusModelCoWorking: function(rn, event, readOnly) {
    if (!event) {
      event = this.getStatusModelEventInfo(rn);
      if (event == null) return;
    }

    if (event.togetherSign != "1") {
      parus.parus8.errorMessage("Текущая точка маршрута не предполагает Совместное исполнение.");
      return;
    }

    var response = parus.database.execute("fillStatusModelPredefinedExecutors", {
      EVENT: event.event,
      EVENT_STAT: event.eventStatus,
      NCLIENT_CLIENT: event.clientClient,
      NCLIENT_PERSON: event.clientPerson,
      NPOINT: event.point
    });

    var coWorkingDate = parus.parus8.nodeField(parus.parus8.rowNodes(response)[0], "COWORKING_DATE");
    if (coWorkingDate != null) {
      (new parus.statusModelCoWorkingDialog({
        documentRN: rn,
        event: event,
        coWorkingDate: coWorkingDate,
        readOnly: !! readOnly,

        onApply: dojo.hitch(this, function(dialog) {
          return true;
        })
      })).show();
    }
  },

  _selectStatusModelPointExecutors: function(event, statusCode, forSend, onSelectCallback) {
    var response = parus.database.execute("fillTempStatusModelPointExecutors", {
      EVENT: event.event,
      SEVENT_TYPE: event.eventTypeCode,
      SEVENT_STAT: statusCode,
      SINIT_PERSON: event.initPerson,
      SINIT_AUTHNAME: event.initAuthName,
      SCLIENT_CLIENT: event.clientClient,
      SCLIENT_PERSON: event.clientPerson,
      SPERSON_CODE: forSend ? event.sendPerson : null,
      SUSER_NAME: forSend ? event.sendUserName : null
    });

    var ident = parus.parus8.nodeField(parus.parus8.rowNodes(response)[0], "IDENT");
    if (ident != null) {

      (new parus.treeDialog({

        title: "Исполнители в точке маршрута",
        key: "BalanceStatusModelExecutors",
        restoreSelected: false,

        model: this._executorsTreeStoreModel(ident),

        getIconClass: function(item, opened) {
          if (parus.parus8.nodeField(item, "ISVALID") != "1") return ("dijitFolderDisabled")
          else return "dijitLeaf";
        },

        onLoad: function() {
          this.catalogTree.showRoot = false;
          this.catalogTree.autoExpand = true;
          this.catalogTree.getIconClass = this.getIconClass;
        },

        onApply: function(dialog) {
          var model = dialog.catalogTree.model;
          var item = dialog.catalogTree.selectedItem;

          if (onSelectCallback) onSelectCallback({
            client: model.getValue(item, "CLIENT_CODE"),
            division: model.getValue(item, "DIVISION_CODE"),
            post: model.getValue(item, "POST_CODE"),
            postInDiv: model.getValue(item, "POST_IN_DIV_CODE"),
            person: model.getValue(item, "PERSON_CODE"),
            staffGroup: model.getValue(item, "STAFFGRP_CODE"),
            userGroup: model.getValue(item, "USER_GROUP_CODE"),
            userName: model.getValue(item, "USER_NAME")
          });

          return true;
        },

        onClose: function() {
          parus.database.execute("clearTempStatusModelPointExecutors", {
            IDENT: ident
          });
        }

      })).show();
    }
  },

  _checkNeedNoteAndExecuteAction: function(event, needNote, actionCallback) {
    if (!needNote) {
      if (actionCallback) actionCallback(null, null);
    }
    else {
      (new parus.statusModelNoteDialog({
        event: event.event,
        eventTypeRN: event.eventType,
        onApply: dojo.hitch(this, function(dialog) {
          if (actionCallback) actionCallback(dialog.noteHeaderEdit.attr("value"), dialog.noteEdit.attr("value"));
          return true;
        })
      })).show();
    }
  },

  _executorsTreeStoreModel: function(ident) {
    return new parus.catalogTreeModel({
      queryid: "BalanceStatusModelPointExecutors",
      onGetConditions: dojo.hitch(this, function(model, parentNode) {
        return {
          IDENT: ident,
          HRN: parentNode ? model.getIdentity(parentNode) : "!*"
        };
      })
    });
  },

  _changeStatusModelEventState: function(event, statusCode, executor, needNote, noteHeader, note) {
    parus.database.execute("changeStatusModelEventState", {
      EVENT: event,
      SEVENT_STAT: statusCode,
      SSEND_CLIENT: executor ? executor.client : null,
      SSEND_DIVISION: executor ? executor.division : null,
      SSEND_POST: executor ? executor.post : null,
      SSEND_PERFORM: executor ? executor.postInDiv : null,
      SSEND_PERSON: executor ? executor.person : null,
      SSEND_STAFFGRP: executor ? executor.staffGroup : null,
      SSEND_USER_GROUP: executor ? executor.userGroup : null,
      SSEND_USER_NAME: executor ? executor.userName : null,
      NEED_NOTE: needNote ? "1" : "0",
      NOTE_HEADER: noteHeader,
      NOTE: note
    });
  },

  _sendStatusModelEvent: function(event, executor, needNote, noteHeader, note) {
    parus.database.execute("sendStatusModelEvent", {
      EVENT: event,
      SSEND_CLIENT: executor ? executor.client : null,
      SSEND_DIVISION: executor ? executor.division : null,
      SSEND_POST: executor ? executor.post : null,
      SSEND_PERFORM: executor ? executor.postInDiv : null,
      SSEND_PERSON: executor ? executor.person : null,
      SSEND_STAFFGRP: executor ? executor.staffGroup : null,
      SSEND_USER_GROUP: executor ? executor.userGroup : null,
      SSEND_USER_NAME: executor ? executor.userName : null,
      NEED_NOTE: needNote ? "1" : "0",
      NOTE_HEADER: noteHeader,
      NOTE: note
    });
  },

  _setStatusModelEventPerformMark: function(event, performMark, performMarkCode, needNote, noteHeader, note) {
    parus.database.execute("setStatusModelEventPerformMark", {
      EVENT: event,
      PERF_MARK_CODE: performMarkCode,
      NEED_NOTE: needNote ? "1" : "0",
      NOTE_HEADER: noteHeader,
      NOTE: note
    });
  }

});