タイムカードの作成

やりたいこと:

・「出庫」→「入庫」の場合は、繰り返し実行できるのですが、「出庫」→「出勤」→「退勤」→「入庫」を行うと、一回のみしか実行できません。

※レコードの状態によって、ボタンの有効・無効を切り替えております

・可能であれば、レコードの状況に応じて、レコードの追加か更新をしたいのですがこちらは可能なのでしょうか?

 

エラー情報:

特になし。

 

参考ソースコード:

https://developer.cybozu.io/hc/ja/articles/202181714-%E4%B8%80%E6%99%82%E7%9A%84%E3%81%AB%E3%83%87%E3%83%BC%E3%82%BF%E3%82%92%E4%BF%9D%E5%AD%98%E3%81%99%E3%82%8B%E3%81%9F%E3%82%81%E3%81%AB%E3%83%95%E3%82%A3%E3%83%BC%E3%83%AB%E3%83%89%E3%82%92%E4%BD%BF%E3%81%86-%E5%8B%A4%E6%80%A0%E7%AE%A1%E7%90%86%E3%82%A2%E3%83%97%E3%83%AA%E3%82%92%E4%BE%8B%E3%81%AB-

 

現在のソースコード:

(function() {
  // 定数、変数定義、関数定義
  'use strict';

  // サーバーの時間を取得する
  function getUTCDateByServer() {
    var r;
    return (r = new XMLHttpRequest())
      ? (r.open('HEAD', '#', false), r.send(null), new Date(r.getResponseHeader('Date'))) : null;
  }

  // 指定された文字の指定された長さのゼロサプレスを取得する
  function zero_suppress(s, len) {
    var str = '';
    for (var i = 0; i < len; i += 1) {
      str += '0';
    }
    str += s;
    return str.substr(str.length - len, len);
  }

  // 時計に表示する関数
  function set_digitTime() {
    var dt = new Date(getUTCDateByServer());
    var yyyy, MM, dd, hh, mm, ss;
    var DAY = ['SUN', 'MON', 'TUE', 'WED', 'THU', 'FRI', 'SAT'];

    yyyy = dt.getFullYear();
    MM = zero_suppress(dt.getMonth() + 1, 2);
    dd = zero_suppress(dt.getDate(), 2);
    hh = zero_suppress(dt.getHours(), 2);
    mm = zero_suppress(dt.getMinutes(), 2);
    ss = zero_suppress(dt.getSeconds(), 2);

    $('#year')[0].innerHTML = yyyy;
    $('#date')[0].innerHTML = MM + '/' + dd + ' ' + DAY[dt.getDay()];
    $('#time')[0].innerHTML = hh + ':' + mm + ':' + ss;
  }

  // イベントハンドラ実行
  var events = ['app.record.index.show'];
  kintone.events.on(events, function(event) {
    if (event.viewId !== 5522594) {
      return;
    }

    // タイムカード
    // アカウント=ログインユーザー、日付=今日 のレコードをすべて取得
    var appId = kintone.app.getId();
    var user = kintone.getLoginUser();
    var qryInfo = 'アカウント名 = "' + user.code + '" and 日付 = TODAY() order by 出勤 desc limit 1';
    var params = {
      app: appId,
      query: qryInfo,
      fields: [
        '社員番号',
        'アカウント名',
        '日付',
        '出勤',
        '退勤',
        '出庫',
        '入庫',
        '出勤中',
        '方面',
        '目的',
        '確認_出庫',
        '検知器_出庫',
        '酒気帯び_出庫',
        '体調_出庫',
        '発熱_出庫',
        '体温_出庫',
        '指示事項・その他_出庫',
        '確認_入庫',
        '検知器_入庫',
        '酒気帯び_入庫',
        '体調_入庫',
        '発熱_入庫',
        '体温_入庫',
        '指示事項・その他_入庫'
      ]
    };

    kintone.api(kintone.api.url('/k/v1/records', true), 'GET', params, function(resp) {

      // resp['records'] の長さが 0 or 1以上の場合で状態を変える
      // 今日の レコード数 = 0
      if (resp.records.length === 0) {
        $('#syukko').replaceWith("<input id='syukko' type='button' value='運転開始'>");
        $('#syukko').on('click', clickFunction);
      } else {
        // 今日の レコード数 >= 1
        if (resp.records[0]['入庫'].value === null) {
            if (resp.records[0]['出勤中'].value[0] === '出勤中') {
              $('#taikin').replaceWith("<input id='taikin' type='button' value='退勤'>");
              $('#taikin').on('click', clickFunction);
            
              $('#zikangaitaikin').replaceWith("<input id='zikangaitaikin' type='button' value='時間外退勤'>");
    $('#zikangaitaikin').on('click', clickFunction);
            } else {
              $('#nyuuko').replaceWith("<input id='nyuuko' type='button' value='運転終了'>");
              $('#nyuuko').on('click', clickFunction);

              $('#syukkin').replaceWith("<input id='syukkin' type='button' value='出勤'>");
              $('#syukkin').on('click', clickFunction);
            }
        } else {
          $('#syukko').replaceWith("<input id='syukko' type='button' value='運転開始'>");
          $('#syukko').on('click', clickFunction);
        }
      }
    });

    // クリック関数
    function clickFunction() {
      var status = $(this).val();
      var id = $(this).attr('id');
      switch (id) {
        case 'syukkin': syukkin_func(); break;
        case 'taikin': taikin_func(); break;
        case 'zikangaitaikin': zikangaitaikin_func(); break;
        case 'syukko': syukko_func(); break;
        case 'nyuuko': nyuuko_func(); break;
      }
    }

    // 出勤関数
    function syukkin_func() {
      // レコード取得
      var appId = kintone.app.getId();
      var qryInfo = 'アカウント名 = "' + user.code + '" order by レコード番号 desc limit 1';
      var params = {
        app: appId,
        query: qryInfo,
        fields: ['レコード番号']
      };

      kintone.api(kintone.api.url('/k/v1/records', true), 'GET', params, function(resp) {
        // 従業員情報の取得
        var employeeRecId = resp.records[0]['レコード番号'].value;

        // 日時取得
        var dt = new Date(getUTCDateByServer());
        var hh = zero_suppress(dt.getHours(), 2);
        var mm = zero_suppress(dt.getMinutes(), 2);
        var syukkin_time = hh + ':' + mm;
        
        if (parseInt(hh + mm) < 1000) {
          syukkin_time = '09:00';
        }

        var body = {
          app: appId,
          id: employeeRecId,
          record: {
            '出勤': {
              value: syukkin_time
            },
            '出勤中': {
              value: ['出勤中']
            },
          }
        };
        // レコード更新へ
        kintone.api(kintone.api.url('/k/v1/record', true), 'PUT', body).then(location.reload());
      });
    }

    // 退勤関数
    function taikin_func() {
      // レコード取得
      var appId = kintone.app.getId();
      var qryInfo = 'アカウント名 = "' + user.code + '" order by レコード番号 desc limit 1';
      var params = {
        app: appId,
        query: qryInfo,
        fields: [
          'レコード番号',
        ]
      };

      kintone.api(kintone.api.url('/k/v1/records', true), 'GET', params, function(resp) {
        // 従業員情報の取得
        var employeeRecId = resp.records[0]['レコード番号'].value;

        var body = {
          app: appId,
          id: employeeRecId,
          record: {
            '退勤': {
              value: '18:00'
            },
            '出勤中': {
              value: []
            },
          }
        };

        // レコード更新へ
        kintone.api(kintone.api.url('/k/v1/record', true), 'PUT', body).then(location.reload());
      });
    }

    // 時間外退勤関数
    function zikangaitaikin_func() {
      // レコード取得
      var appId = kintone.app.getId();
      var qryInfo = 'アカウント名 = "' + user.code + '" order by レコード番号 desc limit 1';
      var params = {
        app: appId,
        query: qryInfo,
        fields: [
          'レコード番号',
        ]
      };

      kintone.api(kintone.api.url('/k/v1/records', true), 'GET', params, function(resp) {
        // 従業員情報の取得
        var employeeRecId = resp.records[0]['レコード番号'].value;
        // 日時取得
        var dt = new Date(getUTCDateByServer());
        var hh = zero_suppress(dt.getHours(), 2);
        var mm = zero_suppress(dt.getMinutes(), 2);
        var taikin_time = hh + ':' + mm;

        var body = {
          app: appId,
          id: employeeRecId,
          record: {
            '退勤': {
              value: taikin_time
            },
            '出勤中': {
              value: []
            },
          }
        };

        // レコード更新へ
        kintone.api(kintone.api.url('/k/v1/record', true), 'PUT', body).then(location.reload());
      });
    }

    // 出庫関数
    function syukko_func() {
      // ルックアップ先のID取得、従業員情報の取得
      var applookId = kintone.app.getLookupTargetAppId('社員番号');
      var qryInfo = 'アカウント名= "' + user.code + '" order by レコード番号 desc limit 1';
      var params = {
        app: applookId,
        query: qryInfo,
        fields: [
          '社員番号',
          '氏名',
          'アカウント名',
        ]
      };

      kintone.api(kintone.api.url('/k/v1/records', true), 'GET', params, function(resp) {
        // 従業員情報の取得
        var employeeNo = resp.records[0]['社員番号'].value;
        var employeeName = resp.records[0]['氏名'].value;
        var employeeAccount = resp.records[0]['アカウント名'].value;

        // 日時取得
        var dt = new Date(getUTCDateByServer());
        var yyyy = dt.getFullYear();
        var MM = zero_suppress(dt.getMonth() + 1, 2);
        var dd = zero_suppress(dt.getDate(), 2);
        var hh = zero_suppress(dt.getHours(), 2);
        var mm = zero_suppress(dt.getMinutes(), 2);
        var syukkin_date = yyyy + '-' + MM + '-' + dd;
        var syukko_time = hh + ':' + mm;

        // 方面取得
        var direction = "";
        while (direction === "" || direction === null) {
          direction = window.prompt("方面を入力してください", "");
        } 

        // 目的取得
        var purpose = "";
        while (purpose === "" || purpose === null) {
          purpose = window.prompt("目的を入力してください", "打合せ・営業・納品");
        }

        // 確認_出庫取得
        var confirmation = "";
        if ( confirm("対面ですか?") ) {
          confirmation = '対面';
        } else {
          confirmation = '電話';
        }

        // 検知器_出庫取得
        var detector = "";
        if ( confirm("検知器は使用しましたか?") ) {
          detector = '有';
        } else {
          detector = '無';
        }

        // 酒気帯び_出庫取得
        var tipsiness = "";
        if ( confirm("酒気帯びはありませんか?") ) {
          tipsiness = '無';
        } else {
          tipsiness = '有';
        }

        // 体調_出庫取得
        var condition = "";
        if ( confirm("体調は良好ですか?") ) {
          condition = '良';
        } else {
          condition = '不';
        }

        // 発熱_出庫+体温_出庫取得
        var fever = "";
        var temperature = "";
        if ( confirm("発熱はありませんか?") ) {
          fever = '無(36.9°C以下)';
        } else {
          fever = '有(37.0°C以上)';
          while (temperature === "" || temperature === null) {
            temperature = window.prompt("体温を入力してください", "");
          }
          temperature = temperature + '°C';
        }

        // 指示事項・その他_出庫取得
        var other = window.prompt("指示事項・その他を入力してください", "")

        var body = {
          app: appId,
          record: {
            '社員番号': {
              value: employeeNo
            },
            '氏名': {
              value: employeeName
            },
            'アカウント名': {
              value: employeeAccount
            },
            '日付': {
              value: syukkin_date
            },
            '出庫': {
              value: syukko_time
            },
            '方面': {
              value: direction
            },
            '目的': {
              value: purpose
            },
            '確認_出庫': {
              value: [confirmation]
            },
            '検知器_出庫': {
              value: [detector]
            },
            '酒気帯び_出庫': {
              value: [tipsiness]
            },
            '体調_出庫': {
              value: [condition]
            },
            '発熱_出庫': {
              value: [fever]
            },
            '体温_出庫': {
              value: temperature
            },
            '指示事項・その他_出庫': {
              value: other
            }
          }
        };
        // 出退勤アプリに record 登録
        kintone.api(kintone.api.url('/k/v1/record', true), 'POST', body).then(location.reload());
      });
    }

    // 入庫関数
    function nyuuko_func() {
      // レコード取得
      var appId = kintone.app.getId();
      var qryInfo = 'アカウント名 = "' + user.code + '" order by レコード番号 desc limit 1';
      var params = {
        app: appId,
        query: qryInfo,
        fields: [
          'レコード番号',
        ]
      };

      kintone.api(kintone.api.url('/k/v1/records', true), 'GET', params, function(resp) {
        // 従業員情報の取得
        var employeeRecId = resp.records[0]['レコード番号'].value;

        // 日時取得
        var dt = new Date(getUTCDateByServer());
        var hh = zero_suppress(dt.getHours(), 2);
        var mm = zero_suppress(dt.getMinutes(), 2);
        var nyuuko_time = hh + ':' + mm;

        // 確認_入庫取得
        var confirmation = "";
        if ( confirm("対面ですか?") ) {
          confirmation = '対面';
        } else {
          confirmation = '電話';
        }

        // 検知器_入庫取得
        var detector = "";
        if ( confirm("検知器は使用しましたか?") ) {
          detector = '有';
        } else {
          detector = '無';
        }

        // 酒気帯び_入庫取得
        var tipsiness = "";
        if ( confirm("酒気帯びはありませんか?") ) {
          tipsiness = '無';
        } else {
          tipsiness = '有';
        }

        // 体調_入庫取得
        var condition = "";
        if ( confirm("体調は良好ですか?") ) {
          condition = '良';
        } else {
          condition = '不';
        }

        // 発熱_入庫+体温_入庫取得
        var fever = "";
        var temperature = "";
        if ( confirm("発熱はありませんか?") ) {
          fever = '無(36.9°C以下)';
        } else {
          fever = '有(37.0°C以上)';
          while (temperature === "" || temperature === null) {
            temperature = window.prompt("体温を入力してください", "");
          }
          temperature = temperature + '°C';
        }

        // 指示事項・その他_入庫取得
        var other = window.prompt("指示事項・その他を入力してください", "")

        var body = {
          app: appId,
          id: employeeRecId,
          record: {
            '入庫': {
              value: nyuuko_time
            },
            '確認_入庫': {
              value: [confirmation]
            },
            '検知器_入庫': {
              value: [detector]
            },
            '酒気帯び_入庫': {
              value: [tipsiness]
            },
            '体調_入庫': {
              value: [condition]
            },
            '発熱_入庫': {
              value: [fever]
            },
            '体温_入庫': {
              value: temperature
            },
            '指示事項・その他_入庫': {
              value: other
            }
          }
        };

        // レコード更新へ
        kintone.api(kintone.api.url('/k/v1/record', true), 'PUT', body).then(location.reload());
      });
    }

    // 1000ミリ秒ごとにset_digitTime()を繰り返す
    set_digitTime();
    setInterval(set_digitTime, 1000);
  });
})();

Koizumiさん

こんにちは。

 

以下の部分でボタン化するかどうかを切り替えてますので、こちらの条件を変えれば可能と思います。

 

現在のコードですと、出庫が押されたときに新規レコード登録(POST)になります。

それ以外のボタンではレコード更新(PUT)になります。

if (resp.records.length === 0) {
$('#syukko').replaceWith("<input id='syukko' type='button' value='運転開始'>");
$('#syukko').on('click', clickFunction);
} else {
// 今日の レコード数 >= 1
if (resp.records[0]['入庫'].value === null) {
  if (resp.records[0]['出勤中'].value[0] === '出勤中') {
    $('#taikin').replaceWith("<input id='taikin' type='button' value='退勤'>");
    $('#taikin').on('click', clickFunction);
    $('#zikangaitaikin').replaceWith("<input id='zikangaitaikin' type='button' value='時間外退勤'>");
    $('#zikangaitaikin').on('click', clickFunction);
  } else {
    $('#nyuuko').replaceWith("<input id='nyuuko' type='button' value='運転終了'>");
    $('#nyuuko').on('click', clickFunction);
    $('#syukkin').replaceWith("<input id='syukkin' type='button' value='出勤'>");
    $('#syukkin').on('click', clickFunction);
  }
} else {
  $('#syukko').replaceWith("<input id='syukko' type='button' value='運転開始'>");
  $('#syukko').on('click', clickFunction);
}
}

koichiさん、初めまして!

こんにちは。

 

分かりやすくご教授いただき、ありがとうございます!

承知いたしました。

こちらを変更して試してみます。

ご確認お願いします。

ご不明点ありましたらご返信ください。