← Back to team overview

credativ team mailing list archive

[Merge] lp:~sylvain-legal/openupgrade-addons/7.0-account_with_cashbox into lp:openupgrade-addons

 

Sylvain LE GAL (GRAP) has proposed merging lp:~sylvain-legal/openupgrade-addons/7.0-account_with_cashbox into lp:openupgrade-addons.

Commit message:
[ADD] migration for account_cashbox_line.

Requested reviews:
  OpenUpgrade Committers (openupgrade-committers)

For more details, see:
https://code.launchpad.net/~sylvain-legal/openupgrade-addons/7.0-account_with_cashbox/+merge/183767

Add the migration script for cashbox.
-- 
https://code.launchpad.net/~sylvain-legal/openupgrade-addons/7.0-account_with_cashbox/+merge/183767
Your team OpenUpgrade Committers is requested to review the proposed merge of lp:~sylvain-legal/openupgrade-addons/7.0-account_with_cashbox into lp:openupgrade-addons.
=== modified file 'account/migrations/7.0.1.1/openupgrade_analysis_work.txt'
--- account/migrations/7.0.1.1/openupgrade_analysis_work.txt	2013-07-24 12:41:33 +0000
+++ account/migrations/7.0.1.1/openupgrade_analysis_work.txt	2013-09-05 20:10:08 +0000
@@ -1,22 +1,37 @@
 ---Fields in module 'account'---
-### TODO: migrate cashbox opening and closing items
+# 'opening_details_ids' replacing 'starting_details_ids' : one2many field based on account_cashbox_line.banck_statement_id (many2one) Nothing to do for the object account_banck_statement
+account      / account.bank.statement   / starting_details_ids (one2many): DEL relation: account.cashbox.line
+account      / account.bank.statement   / opening_details_ids (one2many): NEW relation: account.cashbox.line
+
+# 'closing_details_ids' replacing 'ending_details_ids' : one2many field based on account_cashbox_line.banck_statement_id (many2one) Nothing to do for the object account_banck_statement
+account      / account.bank.statement   / ending_details_ids (one2many) : DEL relation: account.cashbox.line
 account      / account.bank.statement   / closing_details_ids (one2many): NEW relation: account.cashbox.line
+
+# new field one2many based on account_cashbox_line.banck_statement_id (many2one) Nothing to do for the object account_banck_statement
 account      / account.bank.statement   / details_ids (one2many)        : NEW relation: account.cashbox.line
-account      / account.bank.statement   / ending_details_ids (one2many) : DEL relation: account.cashbox.line
-account      / account.bank.statement   / opening_details_ids (one2many): NEW relation: account.cashbox.line
-account      / account.bank.statement   / starting_details_ids (one2many): DEL relation: account.cashbox.line
+
+### The account_cashbox_line concept changes a lot. 
+# Use case : in the cashbox : at the opening state, there is 1 '1 euro coin' and at the closing state, 3 '1 euro coin'. 
+# in 6.1 : there is 2 lines in account_cashbox_line. One with starting_id linked to the account_bank_statement (number=1), the other with the ending_id linked to the account_bank_statement (number=3)
+# in 7.0 : there is one line with number_opening = 1 and number_closing = 3 
+# -> Migration script will delete all the "ending" and merge the data to the "starting" line. (If the starting line exists)
 account      / account.cashbox.line     / bank_statement_id (many2one)  : NEW relation: account.bank.statement
 account      / account.cashbox.line     / ending_id (many2one)          : DEL relation: account.bank.statement
+account      / account.cashbox.line     / starting_id (many2one)        : DEL relation: account.bank.statement
 account      / account.cashbox.line     / number (integer)              : DEL 
 account      / account.cashbox.line     / number_closing (integer)      : NEW 
 account      / account.cashbox.line     / number_opening (integer)      : NEW 
-account      / account.cashbox.line     / starting_id (many2one)        : DEL relation: account.bank.statement
+
+### Ignore new fields
 account      / account.journal          / cash_control (boolean)        : NEW 
+account      / account.journal          / with_last_closing_balance (boolean): NEW 
 account      / account.journal          / cashbox_line_ids (one2many)   : NEW relation: account.journal.cashbox.line
-account      / account.journal          / with_last_closing_balance (boolean): NEW 
+account      / account.journal          / internal_account_id (many2one): NEW relation: account.account
+
+### Ignore new fields (new table)
 account      / account.journal.cashbox.line / journal_id (many2one)         : NEW relation: account.journal, required: required
 account      / account.journal.cashbox.line / pieces (float)                : NEW 
-account      / account.journal          / internal_account_id (many2one): NEW relation: account.account
+
 
 ### Ignore, no existing data to push as messages
 account      / account.bank.statement   / message_ids (one2many)        : NEW relation: mail.message

=== modified file 'account/migrations/7.0.1.1/post-migration.py'
--- account/migrations/7.0.1.1/post-migration.py	2013-07-24 12:41:33 +0000
+++ account/migrations/7.0.1.1/post-migration.py	2013-09-05 20:10:08 +0000
@@ -18,10 +18,12 @@
 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 #
 ##############################################################################
-
+import logging
 from openerp import pooler, SUPERUSER_ID
 from openerp.openupgrade import openupgrade, openupgrade_70
 
+logger = logging.getLogger('OpenUpgrade')
+
 def migrate_invoice_addresses(cr, pool):
     # Contact id takes precedence over old partner id
     openupgrade_70.set_partner_id_from_partner_address_id(
@@ -75,12 +77,101 @@
             cr, SUPERUSER_ID, reconcile_ids,
             {'opening_reconciliation': True})
 
+def merge_account_cashbox_line(cr, pool):
+    # Check an unmanaged case by the migration script
+    cr.execute("""
+        select count(*) as quantity2 from (
+        SELECT count(*) as quantity1
+        FROM account_cashbox_line
+        GROUP BY %s, %s, pieces) as tmp
+        WHERE quantity1 > 1
+        """%(
+            openupgrade.get_legacy_name('starting_id'), 
+            openupgrade.get_legacy_name('ending_id'), 
+            ))
+    count = cr.fetchone()[0]
+    if count>0:
+        logger.error('Some duplicated datas in account_cashbox_line (%s). This case is not covered.' %(count))
+
+    cashboxline_obj = pool.get('account.cashbox.line')
+    # Getting all the row from cashbox_line (type "ending")
+    cr.execute("""
+        SELECT id as id_end, pieces, %s as ending_id, %s as number
+        FROM account_cashbox_line 
+        WHERE %s is not NULL AND bank_statement_id is NULL
+        """ %(
+            openupgrade.get_legacy_name('ending_id'), 
+            openupgrade.get_legacy_name('number'), 
+            openupgrade.get_legacy_name('ending_id'), 
+            ))
+    for (id_end, pieces, ending_id, number) in cr.fetchall():
+        # Check if there is some corresping cashbox_line (type "starting") 
+        cr.execute("""
+            SELECT id, %s
+            FROM account_cashbox_line
+            WHERE %s=%s AND pieces=%s
+            """ %(
+                openupgrade.get_legacy_name('number'),
+                openupgrade.get_legacy_name('starting_id'),
+                ending_id,
+                pieces,
+                ))
+
+        if cr.rowcount==0: 
+            # "ending" cashbox_line becomes normal.
+            cashboxline_obj.write(
+                cr, SUPERUSER_ID, [id_end],
+                {
+                    'number_opening': 0,
+                    'number_closing': number,
+                    'bank_statement_id': ending_id,
+                })
+
+        elif cr.rowcount==1:
+            row = cr.fetchone()
+            # "starting" cashbox_line becomes normal with data of "ending" cashbox_line
+            cashboxline_obj.write(
+                cr, SUPERUSER_ID, [row[0]],
+                {
+                    'number_opening': row[1],
+                    'number_closing': number,
+                    'bank_statement_id': ending_id,
+                })
+            # delete the "ending" cashbox_line
+            cashboxline_obj.unlink(cr, SUPERUSER_ID, [id_end])
+            
+        elif cr.rowcount>1:
+            # there is duplicated datas in the 6.1 Database
+            pass
+
+    # Getting all the row from cashbox_line (type "starting") they didn't change
+    cr.execute("""
+        SELECT id as id_start, %s as starting_id, %s as number
+        FROM account_cashbox_line 
+        WHERE %s is not NULL AND bank_statement_id is NULL
+        """ %(
+            openupgrade.get_legacy_name('starting_id'), 
+            openupgrade.get_legacy_name('number'), 
+            openupgrade.get_legacy_name('starting_id'), 
+            ))
+
+    for (id_start, starting_id, number) in cr.fetchall():
+        cashboxline_obj.write(
+            cr, SUPERUSER_ID, [id_start],
+            {
+                'number_opening': number,
+                'number_closing': 0,
+                'bank_statement_id': starting_id,
+            })
+
+
 @openupgrade.migrate()
 def migrate(cr, version):
     pool = pooler.get_pool(cr.dbname)
     migrate_invoice_addresses(cr, pool)
     migrate_invoice_names(cr, pool)
     lock_closing_reconciliations(cr, pool)
+    merge_account_cashbox_line(cr, pool)
     openupgrade.load_xml(
         cr, 'account',
         'migrations/7.0.1.1/data.xml')

=== modified file 'account/migrations/7.0.1.1/pre-migration.py'
--- account/migrations/7.0.1.1/pre-migration.py	2013-09-03 16:38:16 +0000
+++ account/migrations/7.0.1.1/pre-migration.py	2013-09-05 20:10:08 +0000
@@ -32,6 +32,12 @@
         [
         ('name', None),
         ('note', None),
+        ],
+    'account_cashbox_line':
+        [
+        ('ending_id', None),
+        ('starting_id', None),
+        ('number', None),
         ]
 }
 


Follow ups