[IMP] Changed all module categories, limited number of categories
[odoo/odoo.git] / addons / purchase_requisition / wizard / purchase_requisition_partner.py
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 import time
23 from datetime import datetime
24 from dateutil.relativedelta import relativedelta
25 from osv import fields, osv
26 from osv.orm import browse_record, browse_null
27 from tools.translate import _
28
29 class purchase_requisition_partner(osv.osv_memory):
30     _name = "purchase.requisition.partner"
31     _description = "Purchase Requisition Partner"
32     _columns = {
33         'partner_id': fields.many2one('res.partner', 'Partner', required=True,domain=[('supplier', '=', True)]),
34         'partner_address_id':fields.many2one('res.partner.address', 'Address', required=True),
35     }
36
37     def view_init(self, cr, uid, fields_list, context=None):
38         if context is None:
39             context = {}
40         res = super(purchase_requisition_partner, self).view_init(cr, uid, fields_list, context=context)
41         record_id = context and context.get('active_id', False) or False
42         tender = self.pool.get('purchase.requisition').browse(cr, uid, record_id, context=context)
43         if not tender.line_ids:
44             raise osv.except_osv(_('Error!'), _('No Product in Tender'))
45         True
46
47     def onchange_partner_id(self, cr, uid, ids, partner_id):
48         if not partner_id:
49             return {}
50         addr = self.pool.get('res.partner').address_get(cr, uid, [partner_id], ['default'])
51         part = self.pool.get('res.partner').browse(cr, uid, partner_id)
52         return {'value':{'partner_address_id': addr['default']}}
53
54     def create_order(self, cr, uid, ids, context=None):
55         """
56              To Create a purchase orders .
57
58              @param self: The object pointer.
59              @param cr: A database cursor
60              @param uid: ID of the user currently logged in
61              @param ids: the ID or list of IDs
62              @param context: A standard dictionary
63              @return: {}
64
65         """
66         if context is None:
67             context = {}
68         record_ids = context and context.get('active_ids', False)
69         if record_ids:
70             data =  self.read(cr, uid, ids)
71             company = self.pool.get('res.users').browse(cr, uid, uid, context).company_id
72             order_obj = self.pool.get('purchase.order')
73             order_line_obj = self.pool.get('purchase.order.line')
74             partner_obj = self.pool.get('res.partner')
75             tender_line_obj = self.pool.get('purchase.requisition.line')
76             pricelist_obj = self.pool.get('product.pricelist')
77             prod_obj = self.pool.get('product.product')
78             tender_obj = self.pool.get('purchase.requisition')
79             acc_pos_obj = self.pool.get('account.fiscal.position')
80             partner_id = data[0]['partner_id']
81
82             supplier_data = partner_obj.browse(cr, uid, partner_id, context=context)
83
84             address_id = partner_obj.address_get(cr, uid, [partner_id], ['delivery'])['delivery']
85             list_line=[]
86             purchase_order_line={}
87             for tender in tender_obj.browse(cr, uid, record_ids, context=context):
88                 for line in tender.line_ids:
89                     partner_list = sorted([(partner.sequence, partner) for partner in  line.product_id.seller_ids if partner])
90                     partner_rec = partner_list and partner_list[0] and partner_list[0][1] or False
91                     uom_id = line.product_id.uom_po_id and line.product_id.uom_po_id.id or False
92
93                     if tender.date_start:
94                         newdate = datetime.strptime(tender.date_start, '%Y-%m-%d %H:%M:%S') - relativedelta(days=company.po_lead)
95                     else:
96                         newdate = datetime.today() - relativedelta(days=company.po_lead)
97                     delay = partner_rec and partner_rec.delay or 0.0
98                     if delay:
99                         newdate -= relativedelta(days=delay)
100
101                     partner = partner_rec and partner_rec.name or supplier_data
102                     pricelist_id = partner.property_product_pricelist_purchase and partner.property_product_pricelist_purchase.id or False
103                     price = pricelist_obj.price_get(cr, uid, [pricelist_id], line.product_id.id, line.product_qty, False, {'uom': uom_id})[pricelist_id]
104                     product = prod_obj.browse(cr, uid, line.product_id.id, context=context)
105                     location_id = self.pool.get('stock.warehouse').read(cr, uid, [tender.warehouse_id.id], ['lot_input_id'])[0]['lot_input_id'][0]
106
107                     purchase_order_line= {
108                             'name': product.partner_ref,
109                             'product_qty': line.product_qty,
110                             'product_id': line.product_id.id,
111                             'product_uom': uom_id,
112                             'price_unit': price,
113                             'date_planned': newdate.strftime('%Y-%m-%d %H:%M:%S'),
114                             'notes': product.description_purchase,
115                     }
116                     taxes_ids = line.product_id.product_tmpl_id.supplier_taxes_id
117                     taxes = acc_pos_obj.map_tax(cr, uid, partner.property_account_position, taxes_ids)
118                     purchase_order_line.update({
119                             'taxes_id': [(6,0,taxes)]
120                         })
121                     list_line.append(purchase_order_line)
122                 purchase_id = order_obj.create(cr, uid, {
123                             'origin': tender.purchase_ids and tender.purchase_ids[0].origin or tender.name,
124                             'partner_id': partner_id,
125                             'partner_address_id': address_id,
126                             'pricelist_id': pricelist_id,
127                             'location_id': tender.purchase_ids and tender.purchase_ids[0].location_id.id or line.product_id.product_tmpl_id.property_stock_production.id,
128                             'company_id': tender.company_id.id,
129                             'fiscal_position': partner.property_account_position and partner.property_account_position.id or False,
130                             'requisition_id':tender.id,
131                             'notes':tender.description,
132                             'warehouse_id':tender.warehouse_id.id and tender.warehouse_id.id ,
133                             'location_id':location_id,
134                             'company_id':tender.company_id.id,
135                 })
136                 order_ids=[]
137                 for order_line in list_line:
138                     order_line.update({
139                             'order_id': purchase_id
140                         })
141                     order_line_obj.create(cr,uid,order_line)
142         return {'type': 'ir.actions.act_window_close'}
143
144 purchase_requisition_partner()
145
146
147 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: