# HG changeset patch
# User Sylvain <syt@logilab.fr>
# Date 1229618727 -3600
#      Thu Dec 18 17:45:27 2008 +0100
# Node ID 92a91e7a89364decf10f110aa7e32957e0a9c927
# Parent  eaa65c0bdafbb7310a0a87dc9a8e4278ff15ce61
backport missing stuff :(

diff --git a/MANIFEST.in b/MANIFEST.in
--- a/MANIFEST.in
+++ b/MANIFEST.in
@@ -1,4 +1,5 @@
 include *.py
 
+recursive-include views *.py
 recursive-include i18n *.pot *.po
-recursive-include migration *.sql *.py 
+recursive-include migration *.sql *.py depends.map
diff --git a/__pkginfo__.py b/__pkginfo__.py
--- a/__pkginfo__.py
+++ b/__pkginfo__.py
@@ -34,15 +34,9 @@
 
 try:
     data_files = [
-        # common files
         [THIS_TEMPLATE_DIR, [fname for fname in glob('*.py') if fname != 'setup.py']],
-
-        # client (web) files
         [join(THIS_TEMPLATE_DIR, 'i18n'),  listdir('i18n')],
-        # Note: here, you'll need to add views' subdirectories if you want
-        # them to be included in the debian package
-        
-        # server files
+        [join(THIS_TEMPLATE_DIR, 'views'), listdir('views')],
         [join(THIS_TEMPLATE_DIR, 'migration'), listdir('migration')],
         ]
 except OSError:
diff --git a/debian/changelog b/debian/changelog
--- a/debian/changelog
+++ b/debian/changelog
@@ -1,6 +1,18 @@
+cubicweb-fresh (0.1.2-1) unstable; urgency=low
+
+  * new upstream release
+
+ -- Adrien.DiMascio <Adrien.DiMascio@logilab.fr>  Tue, 29 Jul 2008 19:35:09 +0200
+
+cubicweb-fresh (0.1.1-1) unstable; urgency=low
+
+  * new upstream release
+
+ -- Adrien.DiMascio <Adrien.DiMascio@logilab.fr>  Mon, 28 Jul 2008 18:57:46 +0200
+
 cubicweb-fresh (0.1.0-1) unstable; urgency=low
 
   * initial release
 
- -- 
+ -- Adrien.DiMascio <Adrien.DiMascio@logilab.fr>  Tue, 10 Jun 2008 16:14:04 +0200
 
diff --git a/debian/control b/debian/control
--- a/debian/control
+++ b/debian/control
@@ -9,8 +9,8 @@
 
 Package: cubicweb-fresh
 Architecture: all
-Conflicts: erudi-fresh, erudi-fresh-client, erudi-fresh-server, erudi-fresh-comp
-Replaces: erudi-fresh, erudi-fresh-client, erudi-fresh-server, erudi-fresh-comp
+Conflicts: cubicweb-fresh, erudi-fresh-client, erudi-fresh-server, erudi-fresh-comp
+Replaces: cubicweb-fresh, erudi-fresh-client, erudi-fresh-server, erudi-fresh-comp
 Depends: cubicweb-common (>= 2.99.0)
 Description: expense reporting application
  CubicWeb is a entities / relations bases knowledge management system
diff --git a/entities.py b/entities.py
--- a/entities.py
+++ b/entities.py
@@ -14,3 +14,11 @@
     __rtags__ = {
         'paid_by' : 'primary',
         }
+
+    @property
+    def workcase(self):
+        rql = 'Any R WHERE E has_lines EL, EL eid %(el)s, E spent_for W, W ref R'
+        rset = self.req.execute(rql, {'el': self.eid})
+        if rset:
+            return rset[0][0]
+        return None
diff --git a/i18n/en.po b/i18n/en.po
--- a/i18n/en.po
+++ b/i18n/en.po
@@ -1,7 +1,36 @@
 msgid ""
 msgstr ""
-"Content-Type: text/plain; charset=ISO-8859-1\n"
+"Project-Id-Version: 2.0\n"
+"POT-Creation-Date: 2006-01-12 17:35+CET\n"
+"PO-Revision-Date: 2008-06-09 17:56+0200\n"
+"Last-Translator: Logilab\n"
+"Language-Team: English <devel@logilab.fr.org>\n"
+"MIME-Version: 1.0\n"
+"Content-Type: text/plain; charset=UTF-8\n"
 "Content-Transfer-Encoding: 8bit\n"
 "Generated-By: pygettext.py 1.5\n"
 "Plural-Forms: nplurals=2; plural=(n > 1);\n"
 
+msgid "accounting entry view"
+msgstr "accounting entry view"
+
+msgid "actions_accaction"
+msgstr "action which generates accounting entries"
+
+# add related box generated message
+msgid "actions_accaction_description"
+msgstr "action which generates accounting entries"
+
+msgid "generate accounting entries"
+msgstr "generate accounting entries"
+
+# schema pot file, generated on 2008-04-14 11:36:25
+#
+# singular and plural forms for each entity type
+# subject and object forms for each relation type
+# (no object form for final relation types)
+msgid "spent_for"
+msgstr "spent for"
+
+msgid "spent_for_object"
+msgstr "was paid"
diff --git a/i18n/fr.po b/i18n/fr.po
--- a/i18n/fr.po
+++ b/i18n/fr.po
@@ -2,12 +2,35 @@
 msgstr ""
 "Project-Id-Version: 2.0\n"
 "POT-Creation-Date: 2006-01-12 17:35+CET\n"
-"PO-Revision-Date: 2008-02-15 12:55+0100\n"
+"PO-Revision-Date: 2008-06-09 17:56+0200\n"
 "Last-Translator: Logilab\n"
 "Language-Team: French <devel@logilab.fr.org>\n"
 "MIME-Version: 1.0\n"
-"Content-Type: text/plain; charset=ISO-8859-15\n"
+"Content-Type: text/plain; charset=UTF-8\n"
 "Content-Transfer-Encoding: 8bit\n"
 "Generated-By: pygettext.py 1.5\n"
 "Plural-Forms: nplurals=2; plural=(n > 1);\n"
 
+msgid "accounting entry view"
+msgstr "écriture comptabilité"
+
+msgid "actions_accaction"
+msgstr "action permettant de générer les écritures"
+
+# add related box generated message
+msgid "actions_accaction_description"
+msgstr "action permettant de générer les écritures"
+
+msgid "generate accounting entries"
+msgstr "générer les écritures comptables"
+
+# schema pot file, generated on 2008-04-14 11:36:25
+#
+# singular and plural forms for each entity type
+# subject and object forms for each relation type
+# (no object form for final relation types)
+msgid "spent_for"
+msgstr "dépensé pour"
+
+msgid "spent_for_object"
+msgstr "s'est fait payer"
diff --git a/test/test_fresh.py b/test/test_fresh.py
--- a/test/test_fresh.py
+++ b/test/test_fresh.py
@@ -1,16 +1,14 @@
 """template automatic tests"""
 
-from logilab.common.testlib import TestCase, unittest_main
+from logilab.common.testlib import unittest_main
 
-class DefaultTC(TestCase):
-    def test_something(self):
-        self.skip('this component has no test')
+from cubicweb.devtools.htmlparser import SaxOnlyValidator
+from cubicweb.devtools.testlib import AutomaticWebTest
 
-## uncomment the import if you want to activate automatic test for your
-## template
-
-# from cubicweb.devtools.testlib import AutomaticWebTest
-
+AutomaticWebTest.validators.update({
+    'accexpense': SaxOnlyValidator,
+    'accentry': SaxOnlyValidator,
+    })
 
 if __name__ == '__main__':
     unittest_main()
diff --git a/views.py b/views.py
deleted file mode 100644
--- a/views.py
+++ /dev/null
@@ -1,14 +0,0 @@
-"""Specific controllers for CRM.
-
-:organization: Logilab
-:copyright: 2003-2007 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
-:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
-"""
-__docformat__ = "restructuredtext en"
-
-from cubicweb.web.views.basecontrollers import JSonController
-
-def js_get_concerned_by(self):
-    return self.req.execute('DISTINCT Any W,R WHERE W ref R ORDERBY R').rows
-
-JSonController.js_get_concerned_by = js_get_concerned_by
diff --git a/views/__init__.py b/views/__init__.py
new file mode 100644
--- /dev/null
+++ b/views/__init__.py
@@ -0,0 +1,1 @@
+"""template-specific forms/views/actions/components"""
diff --git a/views/accounting.py b/views/accounting.py
new file mode 100644
--- /dev/null
+++ b/views/accounting.py
@@ -0,0 +1,69 @@
+"""accounting views for fresh template
+
+:organization: Logilab
+:copyright: 2008 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
+:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
+"""
+__docformat__ = "restructuredtext en"
+
+
+from logilab.mtconverter import html_escape
+
+from cubicweb.common.view import EntityView
+
+
+class ExpenseAccountingXmlView(EntityView):
+    id = 'accexpense'
+    title = _('accounting entry view')
+    templatable = False
+    content_type = 'text/xml'
+    accepts = ('Expense',)
+
+    def cell_call(self, row, col):
+        entity = self.entity(row, col)
+        self.wview('accentry', entity.related('has_lines'))
+
+
+class ExpenseLineAccountingEntryXmlView(EntityView):
+    id = 'accentry'
+    title = _('accounting entry view')
+    templatable = False
+    content_type = 'text/xml'
+    accepts = ('ExpenseLine',)
+        
+    def call(self):
+        """display a list of entities by calling their <item_vid> view
+        """
+        self.w(u'<?xml version="1.0" encoding="%s"?>\n' % self.req.encoding)
+        self.w(u'<ecritures>\n')
+        for i in xrange(self.rset.rowcount):
+            self.cell_call(i, 0)
+        self.w(u'</ecritures>\n')
+    
+    def cell_call(self, row, col):
+        entity = self.complete_entity(row, col)
+        self.w(u'  <ecriture date="%s">\n' % entity.diem.strftime('%Y-%m-%d'))
+        self.w(u'    <libelle>%s</libelle>\n' % html_escape(entity.dc_long_title()))
+        amount = int(entity.euro_amount()*100) / 100.0
+        taxes = int(entity.taxes*100) / 100.0
+        self.w(u'    <credit compte="%s" montant="%.2f" />\n' % (
+            entity.paid_by[0].account, amount))
+        if entity.taxes:
+            # XXX hardcoded account for VAT
+            self.w(u'    <debit compte="44566" montant="%.2f" />\n' % entity.taxes)
+        taxfree = amount - taxes
+        accounts = list(entity.paid_for)
+        debit_quotient = int(taxfree*100) / len(accounts)
+        debit_remainder = int(taxfree*100) % len(accounts)
+        for account in accounts:
+            if debit_remainder > 0:
+                debit = (debit_quotient + 1) / 100.0
+                debit_remainder -= 1
+            else:
+                debit = debit_quotient / 100.0
+            self.w(u'    <debit compte="%s" montant="%.2f" />\n' %
+                   (account.account, debit))
+        if entity.workcase:
+            self.w(u'    <groupe>%s</groupe>\n' % entity.workcase)
+        self.w(u'  </ecriture>\n')
+
diff --git a/views/actions.py b/views/actions.py
new file mode 100644
--- /dev/null
+++ b/views/actions.py
@@ -0,0 +1,19 @@
+"""specific actions for fresh template
+
+:organization: Logilab
+:copyright: 2008 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
+:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
+"""
+__docformat__ = "restructuredtext en"
+
+from cubicweb.web.action import EntityAction
+
+class AccountingAction(EntityAction):
+    accepts = ('Expense',)
+    id = 'accaction'
+    title = _('generate accounting entries')
+    
+    def url(self):
+        entity = self.entity(0, 0)
+        return entity.build_url(vid='accexpense')
+
diff --git a/views/controllers.py b/views/controllers.py
new file mode 100644
--- /dev/null
+++ b/views/controllers.py
@@ -0,0 +1,14 @@
+"""Specific controllers for CRM.
+
+:organization: Logilab
+:copyright: 2003-2007 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
+:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
+"""
+__docformat__ = "restructuredtext en"
+
+from cubicweb.web.views.basecontrollers import JSonController
+
+def js_get_concerned_by(self):
+    return self.req.execute('DISTINCT Any W,R WHERE W ref R ORDERBY R').rows
+
+JSonController.js_get_concerned_by = js_get_concerned_by