#!/usr/bin/env python
"""
Parse the outstanding charged items from Unicorn generated by the export_unicorn.pl script
and generate data to COPY into a charges staging table
"""

#    Copyright (C) 2009, Dan Scott
#    Thanks to the Robertson Library, UPEI, for funding these scripts. 

#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.

import UnicornImport as ui

if __name__ == '__main__':
   
    # Incoming data: DateDue|DateCharged|DateRecalled|Library|UserBarcode|ItemBarcode
    # Make this accept command-line arguments eventually
    fields_in = ("datedue", "datecharged", "daterecall", "library", "usr", "item")
    
    # This is all that the staging table currently wants
    fields_out = ("datedue", "datecharged", "daterecall", "library", "usr", "item")

    date_fields = ("datedue", "datecharged", "daterecall")
    
    # create our Unicorn reader
    charges = ui.Parser('../export2eg/opencharges.lst', 'charges.sql', fields_in, fields_out, date_fields)
    
    # print out the header
    charges.file_out.write('COPY staging_charges(datedue, datecharged, daterecall, library, usr, item) from STDIN;\n')
    
    charges.dump_fields()
