# -*- coding: utf-8 -*-

##Copyright (C) [2005]  [Juergen Hamel, D-32584 Löhne]

##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, write to the
##Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. 

import sys
import os
import os.path
from types import *
import pygtk
pygtk.require('2.0')
import gtk
import gtk.glade
import gobject

import string
import logging
from cuon.Windows.windows import windows
import cPickle
#import cuon.OpenOffice.letter
# localisation
import locale, gettext
locale.setlocale (locale.LC_NUMERIC, '')
import threading
import datetime as DateTime
import SingleListOfInvoice
import SingleInpayment
import cuon.DMS.dms
import cuon.Order.SingleOrder
import cuon.Addresses.SingleAddress

class invoicebookwindow(windows):

    
    def __init__(self, allTables, dicInvoices=None,  newInvoice = False, invoiceid = 0):

        windows.__init__(self)
        self.InvoiceID = invoiceid
        self.singleListOfInvoice = SingleListOfInvoice.SingleListOfInvoice(allTables)
        self.singleResidue = SingleListOfInvoice.SingleListOfInvoice(allTables)
        self.singleInpayment = SingleInpayment.SingleInpayment(allTables)
        self.singleOrder = cuon.Order.SingleOrder.SingleOrder(allTables)
        self.singleAddress = cuon.Addresses.SingleAddress.SingleAddress(allTables)
        self.ModulNumber = self.MN['Invoice']
        
        self.loadGlade('invoiceBook.xml', 'InvoiceMainwindow')
        #self.win1 = self.getWidget('ListOfInvoiceMainwindow')
        #self.setStatusBar()
        self.allTables = allTables

        self.EntriesListOfInvoice = 'list_of_invoices.xml'
        
        self.loadEntries(self.EntriesListOfInvoice)
        
        self.singleListOfInvoice.setEntries(self.getDataEntries(self.EntriesListOfInvoice) )
        self.singleListOfInvoice.setGladeXml(self.xml)
        self.singleListOfInvoice.setTreeFields( ['invoice_number', 'order_number','date_of_invoice','total_amount'] )
        self.singleListOfInvoice.setStore( gtk.ListStore(gobject.TYPE_STRING, gobject.TYPE_STRING, gobject.TYPE_STRING, gobject.TYPE_STRING, gobject.TYPE_UINT) ) 
        self.singleListOfInvoice.setTreeOrder('invoice_number desc,order_number desc')
        self.singleListOfInvoice.setListHeader([_('Invoice-Nr.'), _('Order-Nr.'), _('Date'),_('Amount')])
        self.singleListOfInvoice.setTree(self.xml.get_widget('tree1') )

  
        self.EntriesInpayment = 'inpayment.xml'
        
        self.loadEntries(self.EntriesInpayment)
        
        self.singleInpayment.setEntries(self.getDataEntries(self.EntriesInpayment) )
        self.singleInpayment.setGladeXml(self.xml)
        self.singleInpayment.setTreeFields( ['invoice_number', 'date_of_paid','inpayment','order_id'] )
        self.singleInpayment.setStore( gtk.ListStore(gobject.TYPE_STRING, gobject.TYPE_STRING, gobject.TYPE_STRING, gobject.TYPE_STRING, gobject.TYPE_UINT) ) 
        self.singleInpayment.setTreeOrder('invoice_number,date_of_paid')
        self.singleInpayment.setListHeader([_('Invoice-Nr.'), _('Date'), _('Inpayment'),_('Order-ID')])
        self.singleInpayment.setTree(self.xml.get_widget('tree1') )

        self.EntriesResidue = 'residue.xml'
        
        self.loadEntries(self.EntriesResidue)
        
        self.singleResidue.setEntries(self.getDataEntries(self.EntriesResidue) )
        self.singleResidue.setGladeXml(self.xml)
        self.singleResidue.bDistinct = True
        sResidue = "list_of_invoices.total_amount -  (case when (select sum(in_payment.inpayment) from in_payment where   to_number(in_payment.invoice_number,'999999999') = list_of_invoices.invoice_number and status != 'delete' and client = " + `self.dicUser['client']` + ")  != 0 then (select sum(in_payment.inpayment) from in_payment where   to_number(in_payment.invoice_number,'999999999') = list_of_invoices.invoice_numbe
r and status != 
'delete' and client = " + `self.dicUser['client']` + ") else 0 end) "
        self.singleResidue.setTreeFields( ['list_of_invoices.invoice_number as invoice_number', 'list_of_invoices.order_number as order_number','list_of_invoices.date_of_invoice as date_of_invoice','list_of_invoices.total_amount as total_amount', sResidue + " as residue "] )
        self.singleResidue.setStore( gtk.ListStore(gobject.TYPE_STRING, gobject.TYPE_STRING, gobject.TYPE_STRING, gobject.TYPE_STRING, gobject.TYPE_STRING, gobject.TYPE_UINT) ) 
        self.singleResidue.setTreeOrder('invoice_number')
        self.singleResidue.setListHeader([_('Invoice-Nr.'),_('Order-Nr.'), _('Date'),_('Amount'),_('Residue')])
        self.singleResidue.setTree(self.xml.get_widget('tree1') )
        self.singleResidue.sWhere = " where  " + sResidue + " > 0.01 "
  

        # set values for comboBox

          

        # Menu-items
        self.initMenuItems()

        # Close Menus for Tab
        self.addEnabledMenuItems('tabs','list_of_invoices1')
        self.addEnabledMenuItems('tabs','inpayment1')
        self.addEnabledMenuItems('tabs','residue1')
  

               
        # seperate Menus
        self.addEnabledMenuItems('ListOfInvoice','list_of_invoices1')
        self.addEnabledMenuItems('Inpayment','inpayment1')
        #self.addEnabledMenuItems('Residue','residue')
          

        # enabledMenues for ListOfInvoice
        
        self.addEnabledMenuItems('editListOfInvoice','new', self.dicUserKeys['new'])
        self.addEnabledMenuItems('editListOfInvoice','delete', self.dicUserKeys['delete'])
        #self.addEnabledMenuItems('editListOfInvoice','print', self.dicUserKeys['print'])
        self.addEnabledMenuItems('editListOfInvoice','edit', self.dicUserKeys['edit'])

        # enabledMenues for Inpayment
        
        self.addEnabledMenuItems('editInpayment','inpayment_new1', self.dicUserKeys['new'])
        self.addEnabledMenuItems('editInpayment','inpayment_delete1', self.dicUserKeys['delete'])
        #self.addEnabledMenuItems('editInpayment', 'inpayment_print1', self.dicUserKeys['print'])
        self.addEnabledMenuItems('editInpayment','inpayment_edit1', self.dicUserKeys['edit'])
        
        # enabledMenues for Residue
        
##        self.addEnabledMenuItems('editInpayment','inpayment_new1', self.dicUserKeys['new'])
##        self.addEnabledMenuItems('editInpayment','inpayment_delete1', self.dicUserKeys['delete'])
##        self.addEnabledMenuItems('editInpayment', 'inpayment_print1', self.dicUserKeys['print'])
##        self.addEnabledMenuItems('editInpayment','inpayment_edit1', self.dicUserKeys['edit'])

        # enabledMenues for Save 
        self.addEnabledMenuItems('editSave','save', self.dicUserKeys['save'])
        self.addEnabledMenuItems('editSave','inpayment_save1', self.dicUserKeys['save'])
    
        if self.InvoiceID > 0:
                self.singleListOfInvoice.sWhere = ' where id = ' + `self.InvoiceID`

        # tabs from notebook
        self.tabListOfInvoice = 0
        self.tabInpayment = 1
        self.tabResidue = 2
    
        self.win1.add_accel_group(self.accel_group)
        
        print 'self tab changed'
        

        self.tabChanged()
        

    def checkClient(self):
        pass
        
    #Menu File
              
    def on_quit1_activate(self, event):
        print "exit ListOfInvoice V1"
        self.closeWindow()        

    


    #Menu ListOfInvoice
  
    def on_save_activate(self, event):
        print  "save invoicebook v2"
        self.singleListOfInvoice.save()
        self.setEntriesEditable(self.EntriesListOfInvoice, False)
        self.tabChanged()
        
    def on_new_activate(self, event):
        self.out( "new invoiceb