Skip to content

Instantly share code, notes, and snippets.

@nipotan
Last active October 23, 2020 04:41
Show Gist options
  • Save nipotan/6b808aa1ea18d30e4af4ecb43683b115 to your computer and use it in GitHub Desktop.
Save nipotan/6b808aa1ea18d30e4af4ecb43683b115 to your computer and use it in GitHub Desktop.
ジョブカン勤怠管理からマネーフォワードクラウド勤怠に打刻データを移す
#!/usr/bin/env perl
use strict;
use warnings;
use utf8;
use Text::CSV_XS;
use File::Basename;
use File::Spec;
# 下記 URL から従業員別にダウンロードした CSV ファイルを
# https://ssl.jobcan.jp/client/raw-adit
#
# ここからインポートできる形式に変換します
# https://attendance.moneyforward.com/admin/settings/importers/attendances_csv_importers/new
my $jc_csv_file = $ARGV[0];
die "Usage: $0 <JOBCAN_CSV_FILE>\n" unless $jc_csv_file && -f $jc_csv_file;
my $mf_csv_file = jc2mf($jc_csv_file);
print "Output: $mf_csv_file\n";
sub jc2mf {
my $jc_csv_file = shift;
my $mf_csv_file = mf_csv_name($jc_csv_file);
my $csv = Text::CSV_XS->new({ binary => 1, always_quote => 1 });
open my $read, '<:utf8', $jc_csv_file or die $!;
open my $write, '>:utf8', $mf_csv_file or die $!;
my @header_cols = qw(
従業員番号 苗字 名前 打刻所属日 打刻日 打刻時間 打刻種別
);
# 1 行目は見出し
$csv->getline($read); # ジョブカンの見出しは読み飛ばす
$csv->say($write, \@header_cols); # マネフォの見出しを出力
my $type;
my $work_day;
while (my $row = $csv->getline($read)) {
toggle_type($type); # 出勤 <-> 退勤
$work_day = $row->[2] if $type eq '出勤'; # should 出勤 on 労働日
my $mf_row = mf_line($row, $type, $work_day);
$csv->say($write, $mf_row);
}
close $write;
close $read;
return $mf_csv_file;
}
sub mf_line {
my($row, $type, $work_day) = @_;
my($code, $name, $date, $time) = @$row;
my($sei, $mei) = split /\s+/, $name, 2;
# zerofill されてない日時を zerofill してあげる
$work_day = sprintf '%d/%02d/%02d', split m{/}, $work_day;
$date = sprintf '%d/%02d/%02d', split m{/}, $date;
$time = sprintf '%02d:%02d', split m{:}, $time;
return [$code, $sei, $mei, $work_day, $date, $time, $type];
}
# 出勤, 退勤, 出勤, 退勤の順になってる。はず。
sub toggle_type {
$_[0] = defined $_[0] && $_[0] eq '出勤' ? '退勤' : '出勤';
}
sub mf_csv_name {
my $csv = shift;
my $dir = dirname($csv);
my $file = basename($csv);
$file =~ s/\A(jobcan)?/moneyforward/;
return File::Spec->catfile($dir, $file);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment