1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
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.
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.
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/>.
20 ##############################################################################
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 _
29 class purchase_requisition_partner(osv.osv_memory):
30 _name = "purchase.requisition.partner"
31 _description = "Purchase Requisition Partner"
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),
37 def view_init(self, cr, uid, fields_list, context=None):
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'))
47 def onchange_partner_id(self, cr, uid, ids, partner_id):
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']}}
54 def create_order(self, cr, uid, ids, context=None):
56 To Create a purchase orders .
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
68 record_ids = context and context.get('active_ids', False)
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']
82 supplier_data = partner_obj.browse(cr, uid, partner_id, context=context)
84 address_id = partner_obj.address_get(cr, uid, [partner_id], ['delivery'])['delivery']
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
94 newdate = datetime.strptime(tender.date_start, '%Y-%m-%d %H:%M:%S') - relativedelta(days=company.po_lead)
96 newdate = datetime.today() - relativedelta(days=company.po_lead)
97 delay = partner_rec and partner_rec.delay or 0.0
99 newdate -= relativedelta(days=delay)
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]
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,
113 'date_planned': newdate.strftime('%Y-%m-%d %H:%M:%S'),
114 'notes': product.description_purchase,
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)]
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,
137 for order_line in list_line:
139 'order_id': purchase_id
141 order_line_obj.create(cr,uid,order_line)
142 return {'type': 'ir.actions.act_window_close'}
144 purchase_requisition_partner()
147 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: