~eduardo-bayardo-bias/openobject-addons/addons_interface

« back to all changes in this revision

Viewing changes to concrete_sale_order/wizard/stock_invoice_onshipping.py

  • Committer: Eduardo Bayardo
  • Date: 2017-08-15 21:49:35 UTC
  • Revision ID: eduardo.bayardo@bias.com.mx-20170815214935-94uhog6hvq6em5mj
set correct date at sale order validation (sock.move, stock.picking)... Create Invoice defaults from pickings

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
##############################################################################
 
3
#
 
4
#    OpenERP, Open Source Management Solution
 
5
#    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
 
6
#
 
7
#    This program is free software: you can redistribute it and/or modify
 
8
#    it under the terms of the GNU Affero General Public License as
 
9
#    published by the Free Software Foundation, either version 3 of the
 
10
#    License, or (at your option) any later version.
 
11
#
 
12
#    This program is distributed in the hope that it will be useful,
 
13
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
#    GNU Affero General Public License for more details.
 
16
#
 
17
#    You should have received a copy of the GNU Affero General Public License
 
18
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
19
#
 
20
##############################################################################
 
21
 
 
22
from osv import fields, osv
 
23
from tools.translate import _
 
24
import time
 
25
 
 
26
class stock_invoice_concrete(osv.osv_memory):
 
27
 
 
28
    def _get_journal_id(self, cr, uid, context=None):
 
29
        if context is None:
 
30
            context = {}
 
31
 
 
32
        model = context.get('active_model')
 
33
        if not model or model != 'stock.picking':
 
34
            return []
 
35
 
 
36
        model_pool = self.pool.get(model)
 
37
        journal_obj = self.pool.get('account.journal')
 
38
        res_ids = context and context.get('active_ids', [])
 
39
        vals = []
 
40
        browse_picking = model_pool.browse(cr, uid, res_ids, context=context)
 
41
        
 
42
        for pick in browse_picking:
 
43
            if not pick.move_lines:
 
44
                continue
 
45
            src_usage = pick.move_lines[0].location_id.usage
 
46
            dest_usage = pick.move_lines[0].location_dest_id.usage
 
47
            type = pick.type
 
48
            if type == 'out' and dest_usage == 'supplier':
 
49
                journal_type = ('purchase_refund',)
 
50
            elif type == 'out' and dest_usage == 'customer':
 
51
                journal_type = ('sale',)
 
52
            elif type == 'in' and src_usage == 'supplier':
 
53
                journal_type = ('purchase',)
 
54
            elif type == 'in' and src_usage == 'customer':
 
55
                journal_type = ('sale_refund',)
 
56
            else:
 
57
                journal_type = ('sale','sale_refund','purchase','purchase_refund')
 
58
                
 
59
            value = journal_obj.search(cr, uid, [('type','in',journal_type )])
 
60
            for jr_type in journal_obj.browse(cr, uid, value, context=context):
 
61
                t1 = jr_type.id,jr_type.name
 
62
                if t1 not in vals:
 
63
                    vals.append(t1)
 
64
        if not vals:
 
65
            raise osv.except_osv(_('Warning !'), _('Either there are no moves linked to the picking or Accounting Journals are misconfigured!'))
 
66
        
 
67
        return vals
 
68
 
 
69
 
 
70
    _name = "stock.invoice.concrete"
 
71
    _description = "Stock Invoice Concrete"
 
72
 
 
73
    _columns = {
 
74
        'journal_id': fields.selection(_get_journal_id, 'Destination Journal',required=True),
 
75
        'group': fields.boolean("Group by partner"),
 
76
        'invoice_date': fields.date('Invoiced date'),
 
77
    }
 
78
    _defaults = {
 
79
        'journal_id': 1,
 
80
        'group': True,
 
81
        'invoice_date': lambda *a: time.strftime('%Y-%m-%d'),
 
82
    }
 
83
 
 
84
    def view_init(self, cr, uid, fields_list, context=None):
 
85
        if context is None:
 
86
            context = {}
 
87
        res = super(stock_invoice_concrete, self).view_init(cr, uid, fields_list, context=context)
 
88
        pick_obj = self.pool.get('stock.picking')
 
89
        count = 0
 
90
        active_ids = context.get('active_ids',[])
 
91
        for pick in pick_obj.browse(cr, uid, active_ids, context=context):
 
92
            if pick.invoice_state != '2binvoiced':
 
93
                count += 1
 
94
        if len(active_ids) == 1 and count:
 
95
            raise osv.except_osv(_('Warning !'), _('This picking list does not require invoicing.'))
 
96
        if len(active_ids) == count:
 
97
            raise osv.except_osv(_('Warning !'), _('None of these picking lists require invoicing.'))
 
98
        return res
 
99
 
 
100
    def open_invoice(self, cr, uid, ids, context=None):
 
101
        if context is None:
 
102
            context = {}
 
103
        invoice_ids = []
 
104
        data_pool = self.pool.get('ir.model.data')
 
105
        res = self.create_invoice(cr, uid, ids, context=context)
 
106
        invoice_ids += res.values()
 
107
        inv_type = context.get('inv_type', False)
 
108
        action_model = False
 
109
        action = {}
 
110
        if not invoice_ids:
 
111
            raise osv.except_osv(_('Error'), _('No Invoices were created'))
 
112
        if inv_type == "out_invoice":
 
113
            action_model,action_id = data_pool.get_object_reference(cr, uid, 'account', "action_invoice_tree1")
 
114
        elif inv_type == "in_invoice":
 
115
            action_model,action_id = data_pool.get_object_reference(cr, uid, 'account', "action_invoice_tree2")
 
116
        elif inv_type == "out_refund":
 
117
            action_model,action_id = data_pool.get_object_reference(cr, uid, 'account', "action_invoice_tree3")
 
118
        elif inv_type == "in_refund":
 
119
            action_model,action_id = data_pool.get_object_reference(cr, uid, 'account', "action_invoice_tree4")
 
120
        if action_model:
 
121
            action_pool = self.pool.get(action_model)
 
122
            action = action_pool.read(cr, uid, action_id, context=context)
 
123
            action['domain'] = "[('id','in', ["+','.join(map(str,invoice_ids))+"])]"
 
124
        return action
 
125
 
 
126
    def create_invoice(self, cr, uid, ids, context=None):
 
127
        journal_mapping_type_inv = {
 
128
            'sale': 'out_invoice',
 
129
            'purchase': 'in_invoice',
 
130
            'sale_refund': 'out_refund',
 
131
            'purchase_refund': 'in_refund',
 
132
        }
 
133
        if context is None:
 
134
            context = {}
 
135
        picking_pool = self.pool.get('stock.picking')
 
136
        onshipdata_obj = self.read(cr, uid, ids, ['journal_id', 'group', 'invoice_date'])
 
137
        journal_pool = self.pool.get('account.journal')
 
138
        if context.get('new_picking', False):
 
139
            onshipdata_obj['id'] = onshipdata_obj.new_picking
 
140
            onshipdata_obj[ids] = onshipdata_obj.new_picking
 
141
        context['date_inv'] = onshipdata_obj[0]['invoice_date']
 
142
        active_ids = context.get('active_ids', [])
 
143
        active_picking = picking_pool.browse(cr, uid, context.get('active_id',False), context=context)
 
144
        journal_type = journal_pool.browse(cr, uid, onshipdata_obj[0]['journal_id']).type
 
145
        inv_type = journal_mapping_type_inv[journal_type]
 
146
        context['inv_type'] = inv_type
 
147
        res = picking_pool.action_invoice_create(cr, uid, active_ids,
 
148
              journal_id = onshipdata_obj[0]['journal_id'],
 
149
              group = onshipdata_obj[0]['group'],
 
150
              type = inv_type,
 
151
              context=context)
 
152
        return res
 
153
 
 
154
stock_invoice_concrete()
 
155
 
 
156
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: