[IMP]:improved automatic reconcile wizard with partial reconciliation
authorpap (openerp) <pap@tinyerp.co.in>
Fri, 11 Jun 2010 13:07:35 +0000 (18:37 +0530)
committerpap (openerp) <pap@tinyerp.co.in>
Fri, 11 Jun 2010 13:07:35 +0000 (18:37 +0530)
bzr revid: pap@tinyerp.co.in-20100611130735-vnbtqln7porxml1r

addons/account/wizard/account_automatic_reconcile.py
addons/account/wizard/account_automatic_reconcile_view.xml

index 2370c6d..56cbd4c 100644 (file)
@@ -31,15 +31,16 @@ class account_automatic_reconcile(osv.osv_memory):
     _columns = {
         'account_ids': fields.many2many('account.account', 'reconcile_account_rel', 'reconcile_id', 'account_id', 'Account to reconcile', domain = [('reconcile','=',1)], \
                                         help = 'If no account is specified, the reconciliation will be made using every accounts that can be reconcilied'),
-        'writeoff_acc_id': fields.many2one('account.account', 'Account', required=True),
-        'journal_id': fields.many2one('account.journal', 'Journal', required=True),
-        'period_id': fields.many2one('account.period', 'Period', required=True),
+        'writeoff_acc_id': fields.many2one('account.account', 'Account'),
+        'journal_id': fields.many2one('account.journal', 'Journal'),
+        'period_id': fields.many2one('account.period', 'Period'),
         'max_amount': fields.float('Maximum write-off amount'),
         'power': fields.selection([(p, str(p)) for p in range(2, 10)], 'Power', required=True),
         'date1': fields.date('Start of period', required=True),
         'date2': fields.date('End of period', required=True),
         'reconciled': fields.integer('Reconciled transactions', readonly=True),
         'unreconciled': fields.integer('Not reconciled transactions', readonly=True),
+        'allow_write_off': fields.boolean('Allow write off')
     }
 
     def _get_reconciled(self, cr, uid, context={}):
@@ -53,6 +54,7 @@ class account_automatic_reconcile(osv.osv_memory):
         'date2': time.strftime('%Y-%m-%d'),
         'reconciled': _get_reconciled,
         'unreconciled': _get_unreconciled,
+        'allow_write_off' : lambda *a: False
     }
 
     #TODO: cleanup and comment this code... For now, it is awfulllll
@@ -146,18 +148,23 @@ class account_automatic_reconcile(osv.osv_memory):
         if not form['account_ids']:
             raise osv.except_osv(_('UserError'), _('You must select accounts to reconcile'))
         for account_id in form['account_ids']:
+            if not context.get('allow_write_off', False):
+                query = "SELECT partner_id FROM account_move_line WHERE account_id=%s AND reconcile_id IS NULL \
+                AND state <> 'draft' GROUP BY partner_id \
+                HAVING ABS(SUM(debit-credit)) <> %s AND count(*)>0"%(account_id, 0.0)
+#                query="SELECT partner_id FROM account_move_line WHERE account_id=%s AND reconcile_id IS NULL \
+#                AND state <> 'draft' GROUP BY partner_id AND debit = credi"%(account_id)
+                print "query---\n",query
+            else:
+                query = "SELECT partner_id FROM account_move_line WHERE account_id=%s AND reconcile_id IS NULL \
+                AND state <> 'draft' GROUP BY partner_id \
+                HAVING ABS(SUM(debit-credit)) <= %s AND count(*)>0"%(account_id, max_amount or 0.0)
             # reconcile automatically all transactions from partners whose balance is 0
-            cr.execute(
-                "SELECT partner_id " \
-                "FROM account_move_line " \
-                "WHERE account_id=%s " \
-                "AND reconcile_id IS NULL " \
-                "AND state <> 'draft' " \
-                "GROUP BY partner_id " \
-                "HAVING ABS(SUM(debit-credit)) < %s AND count(*)>0",
-                (account_id, max_amount or 0.0))
+            cr.execute(query)
             partner_ids = [id for (id,) in cr.fetchall()]
+            print "\n\npartners--",partner_ids
             for partner_id in partner_ids:
+                print "partner''",partner_id
                 cr.execute(
                     "SELECT id " \
                     "FROM account_move_line " \
@@ -168,63 +175,70 @@ class account_automatic_reconcile(osv.osv_memory):
                     (account_id, partner_id))
                 line_ids = [id for (id,) in cr.fetchall()]
                 if len(line_ids):
-                    move_line_obj.reconcile(cr, uid, line_ids, 'auto', form['writeoff_acc_id'], form['period_id'], form['journal_id'], context)
-                    reconciled += len(line_ids)
-
-            # get the list of partners who have more than one unreconciled transaction
-            cr.execute(
-                "SELECT partner_id " \
-                "FROM account_move_line " \
-                "WHERE account_id=%s " \
-                "AND reconcile_id IS NULL " \
-                "AND state <> 'draft' " \
-                "AND partner_id IS NOT NULL " \
-                "GROUP BY partner_id " \
-                "HAVING count(*)>1",
-                (account_id,))
-            partner_ids = [id for (id,) in cr.fetchall()]
-            #filter?
-            for partner_id in partner_ids:
-                # get the list of unreconciled 'debit transactions' for this partner
-                cr.execute(
-                    "SELECT id, debit " \
-                    "FROM account_move_line " \
-                    "WHERE account_id=%s " \
-                    "AND partner_id=%s " \
-                    "AND reconcile_id IS NULL " \
-                    "AND state <> 'draft' " \
-                    "AND debit > 0",
-                    (account_id, partner_id))
-                debits = cr.fetchall()
+                    reconciled += len(line_ids)                    
+                    if not context.get('allow_write_off', False):
+                        move_line_obj.reconcile_partial(cr, uid, line_ids, 'manual', context={})
+                    else:
+                        move_line_obj.reconcile(cr, uid, line_ids, 'auto', form['writeoff_acc_id'], form['period_id'], form['journal_id'], context)
 
-                # get the list of unreconciled 'credit transactions' for this partner
-                cr.execute(
-                    "SELECT id, credit " \
-                    "FROM account_move_line " \
-                    "WHERE account_id=%s " \
-                    "AND partner_id=%s " \
-                    "AND reconcile_id IS NULL " \
-                    "AND state <> 'draft' " \
-                    "AND credit > 0",
-                    (account_id, partner_id))
-                credits = cr.fetchall()
-
-                (rec, unrec) = self.do_reconcile(cr, uid, credits, debits, max_amount, power, form['writeoff_acc_id'], form['period_id'], form['journal_id'], context)
-                reconciled += rec
-                unreconciled += unrec
-
-            # add the number of transactions for partners who have only one
-            # unreconciled transactions to the unreconciled count
-            partner_filter = partner_ids and 'AND partner_id not in (%s)' % ','.join(map(str, filter(None, partner_ids))) or ''
-            cr.execute(
-                "SELECT count(*) " \
-                "FROM account_move_line " \
-                "WHERE account_id=%s " \
-                "AND reconcile_id IS NULL " \
-                "AND state <> 'draft' " + partner_filter,
-                (account_id,))
-            additional_unrec = cr.fetchone()[0]
-        context.update({'reconciled': reconciled, 'unreconciled': unreconciled + additional_unrec})
+                        # get the list of partners who have more than one unreconciled transaction
+                        cr.execute(
+                            "SELECT partner_id " \
+                            "FROM account_move_line " \
+                            "WHERE account_id=%s " \
+                            "AND reconcile_id IS NULL " \
+                            "AND state <> 'draft' " \
+                            "AND partner_id IS NOT NULL " \
+                            "GROUP BY partner_id " \
+                            "HAVING count(*)>1",
+                            (account_id,))
+                        partner_ids = [id for (id,) in cr.fetchall()]
+                        print "partner_ids---",partner_ids
+                        #filter?
+                        for partner_id in partner_ids:
+                            print "partner_id wid 1 unree--",partner_id
+                            # get the list of unreconciled 'debit transactions' for this partner
+                            cr.execute(
+                                "SELECT id, debit " \
+                                "FROM account_move_line " \
+                                "WHERE account_id=%s " \
+                                "AND partner_id=%s " \
+                                "AND reconcile_id IS NULL " \
+                                "AND state <> 'draft' " \
+                                "AND debit > 0",
+                                (account_id, partner_id))
+                            debits = cr.fetchall()
+            
+                            # get the list of unreconciled 'credit transactions' for this partner
+                            cr.execute(
+                                "SELECT id, credit " \
+                                "FROM account_move_line " \
+                                "WHERE account_id=%s " \
+                                "AND partner_id=%s " \
+                                "AND reconcile_id IS NULL " \
+                                "AND state <> 'draft' " \
+                                "AND credit > 0",
+                                (account_id, partner_id))
+                            credits = cr.fetchall()
+            
+                            (rec, unrec) = self.do_reconcile(cr, uid, credits, debits, max_amount, power, form['writeoff_acc_id'], form['period_id'], form['journal_id'], context)
+                            reconciled += rec
+                            unreconciled += unrec
+            
+                        # add the number of transactions for partners who have only one
+                        # unreconciled transactions to the unreconciled count
+                        partner_filter = partner_ids and 'AND partner_id not in (%s)' % ','.join(map(str, filter(None, partner_ids))) or ''
+                        cr.execute(
+                            "SELECT count(*) " \
+                            "FROM account_move_line " \
+                            "WHERE account_id=%s " \
+                            "AND reconcile_id IS NULL " \
+                            "AND state <> 'draft' " + partner_filter,
+                            (account_id,))
+                        additional_unrec = cr.fetchone()[0]
+                        print "additional_unrec----",additional_unrec, reconciled, unreconciled
+                        unreconciled = unreconciled + additional_unrec
+        context.update({'reconciled': reconciled, 'unreconciled': unreconciled})
         model_data_ids = obj_model.search(cr,uid,[('model','=','ir.ui.view'),('name','=','account_automatic_reconcile_view1')])
         resource_id = obj_model.read(cr, uid, model_data_ids, fields=['res_id'])[0]['res_id']
         return {
index 5afbbed..3e17c1d 100644 (file)
@@ -8,20 +8,28 @@
              <field name="type">form</field>
              <field name="arch" type="xml">
                                <form string="Reconciliation">
+                                       <group width="660" height="550">
                                    <separator string="Options" colspan="4"/>
+                                   <group>
                                    <field name="account_ids" colspan="4" domain="[('reconcile','=',1)]"/>
                                    <field name="date1"/>
                                    <field name="date2"/>
                                    <field name="power"/>
-                                   <separator string="Write-Off Move" colspan="4"/>
-                                   <field name="max_amount"/>
-                                   <field name="writeoff_acc_id"/>
-                                   <field name="journal_id"/>
-                                   <field name="period_id"/>
+                                   <field name="allow_write_off"/>
+                                   </group>
+                                   <newline/>
+                                   <group attrs="{'readonly':[('allow_write_off', '!=', True)]}">
+                                           <separator string="Write-Off Move" colspan="4"/>
+                                           <field name="max_amount"/>
+                                           <field name="writeoff_acc_id" attrs="{ 'required':[('allow_write_off', '=', True)]}"/>
+                                           <field name="journal_id" attrs="{ 'required':[('allow_write_off', '=', True)]}"/>
+                                           <field name="period_id" attrs="{ 'required':[('allow_write_off', '=', True)]}"/>
+                                   </group>
                                    <separator string ="" colspan="4"/>
                                    <group colspan="2" col="4">
                                        <button special="cancel" string="Cancel" icon="gtk-cancel"/>
-                                       <button name="reconcile" string="Reconcile" type="object" icon="gtk-ok"/>
+                                       <button name="reconcile" string="Reconcile" type="object" icon="gtk-ok" context="{'allow_write_off': allow_write_off}"/>
+                                       </group>
                                        </group>
                                </form>
              </field>