~unifield-team/unifield-wm/us-826

« back to all changes in this revision

Viewing changes to account_invoice_split/invoice.py

  • Committer: jf
  • Date: 2014-06-18 08:26:21 UTC
  • Revision ID: jfb@tempo-consulting.fr-20140618082621-cptq430pbwqo6z1t
Tags: pilot3.1b2
UFTP-249 [FIX] Reference field not imported in Unifield when importing register lines
lp:~unifield-team/unifield-wm/UFTP-78 2000..2001

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/env python
2
 
#-*- encoding:utf-8 -*-
3
 
##############################################################################
4
 
#
5
 
#    OpenERP, Open Source Management Solution
6
 
#    Copyright (C) 2011 TeMPO Consulting, MSF. All Rights Reserved
7
 
#    Developer: Olivier DOSSMANN
8
 
#
9
 
#    This program is free software: you can redistribute it and/or modify
10
 
#    it under the terms of the GNU Affero General Public License as
11
 
#    published by the Free Software Foundation, either version 3 of the
12
 
#    License, or (at your option) any later version.
13
 
#
14
 
#    This program is distributed in the hope that it will be useful,
15
 
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 
#    GNU Affero General Public License for more details.
18
 
#
19
 
#    You should have received a copy of the GNU Affero General Public License
20
 
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 
#
22
 
##############################################################################
23
 
 
24
 
from osv import osv
25
 
from osv import fields
26
 
from tools.translate import _
27
 
 
28
 
class account_invoice_line(osv.osv):
29
 
    _name = 'account.invoice.line'
30
 
    _inherit = 'account.invoice.line'
31
 
 
32
 
    def copy(self, cr, uid, id, default=None, context=None):
33
 
        if not context:
34
 
            context = {}
35
 
        if not default:
36
 
            default = {}
37
 
 
38
 
        new_id = super(account_invoice_line, self).copy(cr, uid, id, default, context)
39
 
 
40
 
        if 'split_it' in context:
41
 
            purchase_lines_obj = self.pool.get('purchase.order.line')
42
 
            sale_lines_obj = self.pool.get('sale.order.line')
43
 
 
44
 
            if purchase_lines_obj:
45
 
                purchase_line_ids = purchase_lines_obj.search(cr, uid, [('invoice_lines', 'in', [id])])
46
 
                if purchase_line_ids:
47
 
                    purchase_lines_obj.write(cr, uid, purchase_line_ids, {'invoice_lines': [(4, new_id)]})
48
 
 
49
 
            if sale_lines_obj:
50
 
                sale_lines_ids =  sale_lines_obj.search(cr, uid, [('invoice_lines', 'in', [id])])
51
 
                if sale_lines_ids:
52
 
                    sale_lines_obj.write(cr, uid,  sale_lines_ids, {'invoice_lines': [(4, new_id)]})
53
 
        
54
 
        return new_id
55
 
 
56
 
account_invoice_line()
57
 
 
58
 
 
59
 
class account_invoice(osv.osv):
60
 
    _name = 'account.invoice'
61
 
    _description = 'Account invoice'
62
 
 
63
 
    _inherit = 'account.invoice'
64
 
 
65
 
    def copy(self, cr, uid, id, default=None, context=None):
66
 
        if not context:
67
 
            context = {}
68
 
        if not default:
69
 
            default = {}
70
 
 
71
 
        if 'register_line_ids' not in default:
72
 
            default['register_line_ids'] = []
73
 
 
74
 
        new_id = super(account_invoice, self).copy(cr, uid, id, default, context)
75
 
 
76
 
        if 'split_it' in context:
77
 
            purchase_obj = self.pool.get('purchase.order')
78
 
            sale_obj = self.pool.get('sale.order')
79
 
 
80
 
            if purchase_obj:
81
 
                # attach new invoice to PO
82
 
                purchase_ids = purchase_obj.search(cr, uid, [('invoice_ids', 'in', [id])], context=context)
83
 
                if purchase_ids:
84
 
                    purchase_obj.write(cr, uid, purchase_ids, {'invoice_ids': [(4, new_id)]}, context=context)
85
 
            if sale_obj:
86
 
                # attach new invoice to SO
87
 
                sale_ids = sale_obj.search(cr, uid, [('invoice_ids', 'in', [id])], context=context)
88
 
                if sale_ids:
89
 
                    sale_obj.write(cr, uid, sale_ids, {'invoice_ids': [(4, new_id)]}, context=context)
90
 
 
91
 
        return new_id
92
 
 
93
 
    def button_split_invoice(self, cr, uid, ids, context=None):
94
 
        """
95
 
        Launch the split invoice wizard to split an invoice in two elements.
96
 
        """
97
 
        # Some verifications
98
 
        if not context:
99
 
            context={}
100
 
        if isinstance(ids, (int, long)):
101
 
            ids = [ids]
102
 
        # Prepare some value
103
 
        wiz_lines_obj = self.pool.get('wizard.split.invoice.lines')
104
 
        inv_lines_obj = self.pool.get('account.invoice.line')
105
 
        # Creating wizard
106
 
        wizard_id = self.pool.get('wizard.split.invoice').create(cr, uid, {'invoice_id': ids[0]}, context=context)
107
 
        # Add invoices_lines into the wizard
108
 
        invoice_line_ids = self.pool.get('account.invoice.line').search(cr, uid, [('invoice_id', '=', ids[0])], context=context)
109
 
        # Some other verifications
110
 
        if not len(invoice_line_ids):
111
 
            raise osv.except_osv(_('Error'), _('No invoice line in this invoice or not enough elements'))
112
 
        for invl in inv_lines_obj.browse(cr, uid, invoice_line_ids, context=context):
113
 
            wiz_lines_obj.create(cr, uid, {'invoice_line_id': invl.id, 'product_id': invl.product_id.id, 'quantity': invl.quantity, 
114
 
                'price_unit': invl.price_unit, 'description': invl.name, 'wizard_id': wizard_id}, context=context)
115
 
        # Return wizard
116
 
        if wizard_id:
117
 
            return {
118
 
                'name': "Split Invoice",
119
 
                'type': 'ir.actions.act_window',
120
 
                'res_model': 'wizard.split.invoice',
121
 
                'target': 'new',
122
 
                'view_mode': 'form,tree',
123
 
                'view_type': 'form',
124
 
                'res_id': [wizard_id],
125
 
                'context':
126
 
                {
127
 
                    'active_id': ids[0],
128
 
                    'active_ids': ids,
129
 
                    'wizard_id': wizard_id,
130
 
                }
131
 
            }
132
 
        return False
133
 
 
134
 
account_invoice()
135
 
 
136
 
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: