-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathis_demande_achat_fg.py
executable file
·296 lines (259 loc) · 13.1 KB
/
is_demande_achat_fg.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# -*- coding: utf-8 -*-
from openerp import models,fields,api
from openerp.tools.translate import _
from openerp.exceptions import Warning
import datetime
class is_demande_achat_fg(models.Model):
_name='is.demande.achat.fg'
_inherit=['mail.thread']
_order='name desc'
@api.depends('line_ids')
def _compute(self):
uid=self._uid
for obj in self:
montant_total = 0
for line in obj.line_ids:
montant_total+=line.montant
obj.montant_total = montant_total
vsb=False
if obj.state!='brouillon' and (uid==obj.chef_service_id.id or uid==obj.acheteur_id.id):
vsb=True
obj.vers_brouillon_vsb=vsb
vsb=False
if obj.state=='brouillon' and uid==obj.createur_id.id:
vsb=True
obj.vers_validation_rsp_vsb=vsb
vsb=False
if obj.state=='validation_rsp' and uid==obj.chef_service_id.id:
vsb=True
obj.vers_transmis_achat_vsb=vsb
vsb=False
if obj.state=='transmis_achat' and uid==obj.acheteur_id.id:
vsb=True
obj.vers_solde_vsb=vsb
vsb=False
if obj.state=='brouillon' and uid==obj.createur_id.id:
vsb=True
if obj.state=='validation_rsp' and uid==obj.chef_service_id.id:
vsb=True
if obj.state=='transmis_achat' and uid==obj.acheteur_id.id:
vsb=True
obj.vers_annule_vsb=vsb
name = fields.Char("N°DA-FG", readonly=True)
createur_id = fields.Many2one('res.users', 'Demandeur', required=True)
chef_service_id = fields.Many2one('res.users', 'Chef de service', required=True)
date_creation = fields.Date("Date de création", required=True)
acheteur_id = fields.Many2one('res.users', 'Acheteur', required=True)
fournisseur_id = fields.Many2one('res.partner', 'Fournisseur', domain=[('is_company','=',True),('is_fournisseur_da_fg','=',True)])
pricelist_id = fields.Many2one('product.pricelist', "Liste de prix", related='fournisseur_id.property_product_pricelist_purchase', readonly=True)
fournisseur_autre = fields.Char("Fournisseur autre")
delai_livraison = fields.Date("Délai de livraison", required=True)
lieu_livraison_id = fields.Many2one('res.partner', 'Lieu de livraison', domain=[('is_company','=',True)], required=True)
is_incoterm = fields.Many2one('stock.incoterms', "Incoterm / Conditions de livraison", related='fournisseur_id.is_incoterm', readonly=True)
is_lieu = fields.Char("Lieu", related='fournisseur_id.is_lieu', readonly=True)
num_devis = fields.Char("N° du devis")
date_devis = fields.Date("Date du devis")
commentaire = fields.Text("Commentaire")
state = fields.Selection([
('brouillon' , 'Brouillon'),
('validation_rsp', 'Validation responsable'),
('transmis_achat', 'Transmis achat'),
('solde' , 'Soldé'),
('annule' , 'Annulé'),
], "Etat")
line_ids = fields.One2many('is.demande.achat.fg.line' , 'da_id', u"Lignes" , copy=True)
montant_total = fields.Float("Montant Total", compute='_compute', readonly=True, store=True)
order_id = fields.Many2one('purchase.order', 'Commande générée', readonly=True, copy=False)
vers_brouillon_vsb = fields.Boolean('Champ technique', compute='_compute', readonly=True, store=False)
vers_validation_rsp_vsb = fields.Boolean('Champ technique', compute='_compute', readonly=True, store=False)
vers_transmis_achat_vsb = fields.Boolean('Champ technique', compute='_compute', readonly=True, store=False)
vers_solde_vsb = fields.Boolean('Champ technique', compute='_compute', readonly=True, store=False)
vers_annule_vsb = fields.Boolean('Champ technique', compute='_compute', readonly=True, store=False)
@api.multi
def _lieu_livraison_id(self):
user = self.env['res.users'].browse(self._uid)
partner_id = user.company_id.partner_id.id
return partner_id
_defaults = {
'date_creation' : lambda *a: fields.datetime.now(),
'createur_id' : lambda obj, cr, uid, context: uid,
'state' : 'brouillon',
'lieu_livraison_id': lambda self,cr,uid,context: self._lieu_livraison_id(cr,uid,context),
}
@api.multi
def fournisseur_id_on_change(self,fournisseur_id):
res={}
if fournisseur_id:
res['value']={}
partner = self.env['res.partner'].browse(fournisseur_id)
lieu_livraison_id=partner.is_livre_a_id.id
if lieu_livraison_id:
res['value']['lieu_livraison_id']=lieu_livraison_id
return res
@api.model
def create(self, vals):
data_obj = self.env['ir.model.data']
sequence_ids = data_obj.search([('name','=','is_demande_achat_fg_seq')])
if sequence_ids:
sequence_id = data_obj.browse(sequence_ids[0].id).res_id
vals['name'] = self.env['ir.sequence'].get_id(sequence_id, 'id')
obj = super(is_demande_achat_fg, self).create(vals)
return obj
@api.multi
def vers_brouillon_action(self):
for obj in self:
#if obj.acheteur_id.id==self._uid or obj.chef_service_id.id==self._uid:
obj.sudo().state="brouillon"
@api.multi
def vers_validation_rsp_action(self):
for obj in self:
subject=u'['+obj.name+u'] Validation responsable'
email_to=obj.chef_service_id.email
user = self.env['res.users'].browse(self._uid)
email_from = user.email
nom = user.name
base_url = self.env['ir.config_parameter'].sudo().get_param('web.base.url')
url=base_url+u'/web#id='+str(obj.id)+u'&view_type=form&model=is.demande.achat.fg'
body_html=u"""
<p>Bonjour,</p>
<p>"""+nom+""" vient de passer la demande d'achat de frais généraux <a href='"""+url+"""'>"""+obj.name+"""</a> à l'état 'Validation responsable'.</p>
<p>Merci d'en prendre connaissance.</p>
"""
self.envoi_mail(email_from,email_to,subject,body_html)
obj.state="validation_rsp"
@api.multi
def vers_transmis_achat_action(self):
for obj in self:
subject=u'['+obj.name+u'] Transmis achat'
email_to=obj.acheteur_id.email
user = self.env['res.users'].browse(self._uid)
email_from = user.email
nom = user.name
base_url = self.env['ir.config_parameter'].sudo().get_param('web.base.url')
url=base_url+u'/web#id='+str(obj.id)+u'&view_type=form&model=is.demande.achat.fg'
body_html=u"""
<p>Bonjour,</p>
<p>"""+nom+""" vient de passer la demande d'achat de frais généraux <a href='"""+url+"""'>"""+obj.name+"""</a> à l'état 'Transmis achat'.</p>
<p>Merci d'en prendre connaissance.</p>
"""
self.envoi_mail(email_from,email_to,subject,body_html)
obj.state="transmis_achat"
@api.multi
def vers_solde_action(self):
for obj in self:
order_obj = self.env['purchase.order']
order_line_obj = self.env['purchase.order.line']
partner=obj.fournisseur_id
if partner.property_product_pricelist_purchase:
vals={
'partner_id' : partner.id,
'is_livre_a_id' : obj.lieu_livraison_id.id,
'location_id' : partner.is_source_location_id.id,
'fiscal_position' : partner.property_account_position.id,
'payment_term_id' : partner.property_supplier_payment_term.id,
'pricelist_id' : partner.property_product_pricelist_purchase.id,
'currency_id' : partner.property_product_pricelist_purchase.currency_id.id,
'is_num_da' : obj.name,
'is_demandeur_id' : obj.createur_id.id,
'incoterm_id' : partner.is_incoterm.id,
'is_lieu' : partner.is_lieu,
}
order=order_obj.create(vals)
obj.order_id=order.id
if order:
for line in obj.line_ids:
vals={}
res=order_line_obj.onchange_product_id(
order.pricelist_id.id,
line.product_id.id,
line.quantite,
line.uom_id.id,
partner.id,
date_order = False,
fiscal_position_id = partner.property_account_position.id,
date_planned = False,
name = False,
price_unit = line.prix,
state = 'draft'
)
vals=res['value']
vals['order_id'] = order.id
vals['product_id'] = line.product_id.id
vals['date_planned'] = obj.delai_livraison
name=[]
if line.product_id.id:
name.append(line.product_id.is_code+u' - '+line.product_id.name)
if line.designation1:
name.append(line.designation1)
if line.designation2:
name.append(line.designation2)
vals['name']='\n'.join(name)
if 'taxes_id' in vals:
vals.update({'taxes_id': [[6, False, vals['taxes_id']]]})
order_line=order_line_obj.create(vals)
order.wkf_bid_received()
res=order.wkf_confirm_order()
order.action_picking_create()
order.wkf_approve_order()
obj.sudo().state="solde"
@api.multi
def vers_annule_action(self):
for obj in self:
if obj.createur_id.id==self._uid or obj.acheteur_id.id==self._uid or obj.chef_service_id.id==self._uid:
obj.sudo().state="annule"
@api.multi
def envoi_mail(self, email_from,email_to,subject,body_html):
for obj in self:
vals={
'email_from' : email_from,
'email_to' : email_to,
'email_cc' : email_from,
'subject' : subject,
'body_html' : body_html,
}
email=self.env['mail.mail'].create(vals)
if email:
self.env['mail.mail'].send(email)
class is_demande_achat_fg_line(models.Model):
_name='is.demande.achat.fg.line'
_order='da_id,sequence'
@api.depends('product_id','quantite','prix')
def _compute(self):
for obj in self:
if obj.product_id:
obj.uom_id=obj.product_id.uom_po_id
obj.montant=obj.quantite*obj.prix
da_id = fields.Many2one('is.demande.achat.fg', "Demande d'achat", required=True, ondelete='cascade', readonly=True)
sequence = fields.Integer('Ordre')
product_id = fields.Many2one('product.product', 'Article', domain=[('is_category_id.name','=','72')])
designation1 = fields.Char("Désignation 1")
designation2 = fields.Char("Désignation 2")
uom_id = fields.Many2one('product.uom', "Unité d'achat")
quantite = fields.Float("Quantité", digits=(14,4), required=True)
prix = fields.Float("Prix" , digits=(14,4))
montant = fields.Float("Montant", compute='_compute', readonly=True, store=True)
@api.multi
def product_id_on_change(self,parent,product_id,quantite):
cr=self._cr
res={}
fournisseur_id=parent.fournisseur_id
if fournisseur_id:
partner = self.env['res.partner'].browse(fournisseur_id)
pricelist_id=partner.property_product_pricelist_purchase.id
if product_id and pricelist_id:
res['value']={}
product = self.env['product.product'].browse(product_id)
now=datetime.datetime.now().strftime('%Y-%m-%d')
sql="""
select
is_prix_achat("""+str(pricelist_id)+""", """+str(product_id)+""", """+str(quantite)+""", '"""+now+"""')
from product_product
where id="""+str(product_id)+"""
"""
cr.execute(sql)
prix=0
for row in cr.fetchall():
prix=row[0]
if prix>0:
res['value']['prix']=prix
return res