1b5d45592c2933b23bd4125aa71dbea68569bea5
[odoo/odoo.git] / addons / delivery / wizard / delivery_invoice_onshipping.py
1 ##############################################################################
2 #
3 # Copyright (c) 2005-2006 TINY SPRL. (http://tiny.be) All Rights Reserved.
4 #
5 # WARNING: This program as such is intended to be used by professional
6 # programmers who take the whole responsability of assessing all potential
7 # consequences resulting from its eventual inadequacies and bugs
8 # End users who are looking for a ready-to-use solution with commercial
9 # garantees and support are strongly adviced to contract a Free Software
10 # Service Company
11 #
12 # This program is Free Software; you can redistribute it and/or
13 # modify it under the terms of the GNU General Public License
14 # as published by the Free Software Foundation; either version 2
15 # of the License, or (at your option) any later version.
16 #
17 # This program is distributed in the hope that it will be useful,
18 # but WITHOUT ANY WARRANTY; without even the implied warranty of
19 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 # GNU General Public License for more details.
21 #
22 # You should have received a copy of the GNU General Public License
23 # along with this program; if not, write to the Free Software
24 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
25 #
26 ##############################################################################
27
28 import time
29 import wizard
30 import ir
31 import pooler
32 from osv.osv import except_osv
33 import netsvc
34
35 invoice_form = """<?xml version="1.0"?>
36 <form string="Create invoices">
37         <separator colspan="4" string="Create invoices" />
38         <field name="journal_id"/>
39         <newline/>
40         <field name="group"/>
41         <newline/>
42         <field name="type"/>
43 </form>
44 """
45
46 invoice_fields = {
47         'journal_id' : {
48                 'string':'Destination Journal',
49                 'type':'many2one',
50                 'relation':'account.journal',
51                 'required':True,
52                 'domain':[('type','=','sale')]
53         },
54         'group' : {'string':'Group by partner', 'type':'boolean'}
55 }
56
57 def make_default(val):
58         def fct(obj, cr, uid):
59                 return val
60         return fct
61
62 def _get_type(self, cr, uid, data, context):
63         picking_obj=pooler.get_pool(cr.dbname).get('stock.picking')
64         pick=picking_obj.browse(cr, uid, [data['id']])[0]
65         if pick.loc_move_id:
66                 usage=pick.loc_move_id.usage
67         else:
68                 usage=pick.move_lines[0].location_id.usage
69         if pick.type=='out' and usage=='supplier':
70                 type='in_refund'
71         elif pick.type=='out' and usage=='customer':
72                 type='out_invoice'
73         elif pick.type=='in' and usage=='supplier':
74                 type='in_invoice'
75         elif pick.type=='in' and usage=='customer':
76                 type='out_refund'
77         else:
78                 type='out_invoice'
79         invoice_fields['type']={'string':'Type', 'type':'selection', 'default':make_default(type),
80                 'selection':[
81                         ('out_invoice','Customer Invoice'),
82                         ('in_invoice','Supplier Invoice'),
83                         ('out_refund','Customer Refund'),
84                         ('in_refund','Supplier Refund'),
85                         ], 'required':True}
86         return {}
87
88 def _create_invoice(self, cr, uid, data, context):
89         pool = pooler.get_pool(cr.dbname)
90         picking_obj = pooler.get_pool(cr.dbname).get('stock.picking')
91         res = picking_obj.action_invoice_create(cr, uid, data['ids'],journal_id=data['form']['journal_id'],group=data['form']['group'], type=data['form']['type'], context= context)
92         for pick_id, inv_id in res.items():
93                 pool.get('stock.picking').write(cr, uid, [pick_id], {'invoice_state':'invoiced'})
94                 invoice = pool.get('account.invoice').browse(cr, uid, inv_id, context)
95                 picking = pool.get('stock.picking').browse(cr, uid, pick_id, context)
96                 if not picking.carrier_id:
97                         continue
98                 grid_id = pool.get('delivery.carrier').grid_get(cr, uid, [picking.carrier_id.id], picking.address_id.id, context)
99                 price = pool.get('delivery.grid').get_price_from_picking(cr, uid, grid_id, 
100                         invoice.amount_untaxed, picking.weight,
101                         picking.volume, context) or 0.0
102                 a =  picking.carrier_id.product_id.product_tmpl_id.property_account_income
103                 if not a:
104                         a = picking.carrier_id.product_id.categ_id.property_account_income_categ
105                 account_id =  a[0]
106                 pool.get('account.invoice.line').create(cr, uid, {
107                         'name':picking.carrier_id.name,
108                         'invoice_id': inv_id,
109                         'uos_id': picking.carrier_id.product_id.uos_id.id,
110                         'product_id':picking.carrier_id.product_id.id,
111                         'account_id':account_id,
112                         'price_unit': price,
113                         'quantity': 1,
114                         'invoice_line_tax_id': [(6,0,[x.id for x in (picking.carrier_id.product_id.taxes_id or [])  ])],
115                         'note': '',
116                 })
117         return res
118
119 def _action_open_window(self, cr, uid, data, context):
120         res = _create_invoice(self, cr, uid, data, context)
121         iids = res.values()
122         form = data['form']
123         return {
124                 'domain': "[('id','in', ["+','.join(map(str,iids))+"])]",
125                 'name': 'Invoices',
126                 'view_type': 'form',
127                 'view_mode': 'tree,form',
128                 'res_model': 'account.invoice',
129                 'view_id': False,
130                 'context': "{'type':'out_invoice'}",
131                 'type': 'ir.actions.act_window'
132         }
133
134 class make_invoice_onshipping(wizard.interface):
135         states = {
136                 'init' : {
137                         'actions' : [_get_type],
138                         'result' : {'type' : 'form',
139                                     'arch' : invoice_form,
140                                     'fields' : invoice_fields,
141                                     'state' : [('end', 'Cancel'),('create_invoice', 'Create invoice') ]}
142                 },
143                 'create_invoice' : {
144                         'actions' : [],
145                         'result' : {'type' : 'action', 'action': _action_open_window, 'state' : 'end'}
146                 },
147         }
148 make_invoice_onshipping("delivery.invoice_onshipping")
149
150
151