[PYTHON][VIEW] siren-->siret
[burette/lhc.git] / lhc.py
diff --git a/lhc.py b/lhc.py
index 494773c..6d5398e 100644 (file)
--- a/lhc.py
+++ b/lhc.py
@@ -2,7 +2,7 @@
 ##############################################################################
 #
 #    lhc module for OpenERP, Customize OpenERP for L'Heureux Cyclage Copyright
-#    (C) 2013-2022 L'Heureux Cyclage (<http://www.heureux-cyclage.org>)
+#    (C) 2013-2023 L'Heureux Cyclage (<http://www.heureux-cyclage.org>)
 #
 #    This file is a part of lhc_custom_oe
 #
@@ -26,6 +26,7 @@ from openerp.osv import orm
 from openerp.osv import fields
 from openerp.tools.translate import _
 import openerp.addons.decimal_precision as dp
+import datetime
 
 
 class res_partner(orm.Model):
@@ -41,6 +42,65 @@ class res_partner(orm.Model):
                 res[obj.id] = ''
         return res
 
+    def _bikecoop_activity_dates_check(self, cr, uid, ids, context=None):
+        # TODO: check condition and return boolean accordingly
+        currentDateTime = datetime.datetime.now()
+        date = currentDateTime.date()
+        year = date.strftime("%Y")
+
+        min_date = 1950
+        max_date = int(year)
+
+        for partner in self.browse(cr, uid, ids, context=context):
+            if partner.bikecoop_activity_start == 0 and partner.bikecoop_activity_stop == 0:
+                return True
+            if partner.bikecoop_activity_start < 0 or partner.bikecoop_activity_stop < 0:
+                return False
+            if partner.bikecoop_activity_start == 0 and partner.bikecoop_activity_stop > 0:
+                return False
+            if partner.bikecoop_activity_stop > max_date or partner.bikecoop_activity_stop < min_date:
+                if partner.bikecoop_activity_stop != 0:
+                    return False
+            if partner.bikecoop_activity_start > max_date or partner.bikecoop_activity_start < min_date:
+                if partner.bikecoop_activity_start != 0:
+                    return False
+            if partner.bikecoop_activity_stop < partner.bikecoop_activity_start:
+                if partner.bikecoop_activity_stop != 0:
+                    return False
+            return True
+
+    def _rna_check(self, cr, uid, ids, context=None):
+        """Check if RNA code is well formatted"""
+        for partner in self.browse(cr, uid, ids, context=context):
+            if partner.rna:
+                if len(partner.rna) != 10:
+                    return False
+                if partner.rna[0] != 'W':
+                    return False
+                try:
+                    int(partner.rna[1:10])
+                except ValueError:
+                    return False
+                return True
+            else:
+                return True
+
+
+    def _siret_check(self, cr, uid, ids, context=None):
+        """Check if SIRET code is well formatted"""
+        for partner in self.browse(cr, uid, ids, context=context):
+            if partner.siret:
+                if len(partner.siret) != 14:
+                    return False
+                try:
+                    int(partner.siret)
+                except ValueError:
+                    return False
+                return True
+            else:
+                return True
+
+
     _columns = {
         'usual_contact': fields.boolean(
             'Usual contact',
@@ -60,12 +120,27 @@ class res_partner(orm.Model):
                     lambda self, cr, uid, ids, c={}: ids,
                     ['zip'], 10)},
         ),
+        'email2': fields.char(
+            'Secondary email',
+            size=240,
+            help="""One more email. Warning: there are no email notification
+            with this email. Only the main email can receive notifications."""),
+        'bikecoop_activity_start': fields.integer('Bikecoop activity start year'),
+        'bikecoop_activity_stop': fields.integer('Bikecoop activity end year'),
+        'rna': fields.char('RNA code', size=10),
+        'siret': fields.char('SIRET', size=14),
     }
 
     _defaults = {
         'kit_sent': lambda *a: False,
     }
 
+    _constraints = [
+        (_bikecoop_activity_dates_check, 'Error: Bikecoop activity dates are inconsistent!', ['Bikecoop activity dates']),
+        (_rna_check, 'Error: RNA code must begin with a "W" followed by exactly 9 digits.', ['RNA']),
+        (_siret_check, 'Error: SIRET must take exactly 14 digits.', ['SIRET']),
+    ]
+
 
 class product_template(orm.Model):
     _inherit = 'product.template'
@@ -161,37 +236,25 @@ class event_registration(orm.Model):
         return vals
 
 
-class crossovered_budget_lines(orm.Model):
-    _inherit = 'crossovered.budget.lines'
+class hr_expense_line(orm.Model):
+    _inherit = 'hr.expense.line'
 
-    def _prac_amt(self, cr, uid, ids, context=None):
-        """Calculate pratical amount even if there is no analytic account"""
-        res = {}
-        result = 0.0
-        if context is None:
-            context = {}
-        account_obj = self.pool.get('account.account')
-
-        for line in self.browse(cr, uid, ids, context=context):
-            acc_ids = [x.id for x in line.general_budget_id.account_ids]
-            if not acc_ids:
-                raise osv.except_osv(_('Error!'),_("The Budget '%s' has no accounts!") % ustr(line.general_budget_id.name))
-            acc_ids = account_obj._get_children_and_consol(cr, uid, acc_ids, context=context)
-            date_to = line.date_to
-            date_from = line.date_from
-
-            if line.analytic_account_id.id:
-                res = super(crossovered_budget_lines, self)._prac_amt(cr, uid, ids, context)
-            else:
-                cr.execute("SELECT SUM(credit) - SUM(debit) FROM account_move_line WHERE (date "
-                   "between to_date(%s,'yyyy-mm-dd') AND to_date(%s,'yyyy-mm-dd')) AND "
-                   "account_id=ANY(%s)", (date_from, date_to,acc_ids,))
-                result = cr.fetchone()[0]
-                if result is None:
-                    result = 0.00
-                res[line.id] = result
-
-        return res
+    def onchange_product_id(self, cr, uid, ids, product_id, context=None):
+        """Override to manage default name and ref value based on product
+        properties"""
 
+        vals = super(hr_expense_line, self).onchange_product_id(cr, uid, ids, product_id, context)
+        res = vals['value']
+        if product_id:
+            product = self.pool.get('product.product').browse(cr, uid, product_id, context=context)
+            res['ref'] = ''
+            if product.description:
+                description = product.description.split('|')
+                if len(description) == 1:
+                    res['name'] = description[0]
+                elif len(description) >= 2:
+                    res['name'] = description[0]
+                    res['ref'] = description[1]
+        return {'value': res}
 
 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: