#!/usr/bin/perl

use strict;
use warnings;
use FindBin qw( $Bin );
use lib "$Bin/../../lib";
use POSIX qw( strftime );
use Data::Dumper;
use App::POS::Config;
use App::POS::Machine::Server;

my $cfg = App::POS::Config->new( file => "/opt/pos/etc/config.ini" );
my $info = $cfg->load_fixed();

my $machine = undef;

if (exists $info->{STATION_NUMBER} && $info->{STATION_NUMBER} == 0) {
  if (! -d "/opt/pos/server/data/export") {
    system("mkdir -p /opt/pos/server/data/export"); 
  }

  $machine = App::POS::Machine::Server->new( config => $cfg );  

  my $db = $machine->database();

  my $date_str = &strftime("%Y-%m-%d", localtime);

  my $r = $db->select(qq{
    SELECT MAX(business_date) AS date
    FROM sales_headers
    WHERE business_date < ?
  }, $date_str);

  if (scalar @$r) {
    my $time_str = strftime("%Y%m%d%H%M%S", localtime(time));
    my $file = "/opt/pos/server/data/export/".$r->[0]->{date}."#".$time_str.$$.".export";
    open(F, "> $file");
    close(F);
  }
}

