acc_sale.py
44.0 KB
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
# -*- coding: utf-8 -*-
import logging
from odoo import api, SUPERUSER_ID, fields, models, _
from odoo.http import request
import logging
import xlrd
from collections import Counter
import re
import xlwt
import sys,os
import datetime as dt
import pytz
from odoo.tools.misc import DEFAULT_SERVER_DATETIME_FORMAT
from datetime import datetime
# from ..controllers.common import localizeStrTime
from odoo.exceptions import UserError, ValidationError
_logger = logging.getLogger(__name__)
class AccSaleOrder(models.Model):
"""
销售单继承
"""
_inherit = "sale.order"
@api.depends('quotation_line.price_subtotal','discount','tax_id')
def _amount_quo_all(self):
for order in self:
if order.tax_id:
tax_rate = order.tax_id.amount/100
else:
tax_rate = 0
quo_amount_total = 0.0
quo_amount_untaxed = 0.0
quo_amount_tax = 0.0
discount = self.discount
for line in order.quotation_line:
quo_amount_untaxed += line.price_subtotal
quo_amount_tax += line.price_subtotal * tax_rate
order.update({
'quo_amount_untaxed':quo_amount_untaxed,
'quo_amount_tax':quo_amount_tax,
'quo_amount_total': quo_amount_untaxed + quo_amount_tax - discount,
})
title = fields.Char(string=u'标题', required=True)
state = fields.Selection([
('draft', '待确认'),
('certain', '已确认'),
('confirm', '已提交审批'),
('sent', 'Quotation Sent'),
# ('boss', '管理部已审核'),
('sale', 'Sales Order'),
('done', 'Locked'),
('cancel', 'Cancelled'),
], string='Status', readonly=True, copy=False, index=True, track_visibility='onchange', track_sequence=3, default='draft')
crm_sonumber = fields.Char(string=u'老销售单号',readonly=True)
bomupdate_time = fields.Datetime(string=u'物料变更最新操作时间',readonly=True)
partner_ponumber = fields.Char(string=u'客户po号')
charge_person = fields.Many2one('res.users',string=u'负责人',default=lambda self: self.env.user.id,required=True)
contact_id = fields.Many2one('res.partner',string='联系人')
transfer = fields.Char(string='承运人')
delivery_time = fields.Char(string=u'货期',placeholder="填写格式如(1周,1weeks,1天,1days)")
origin_country = fields.Char(string="货物原产国及厂家")
port_shipment = fields.Char(string="发货机场")
destination_port = fields.Char(string="目的机场")
destination_address = fields.Char(string="交货地址")
shipping_method = fields.Char(string="运输方式")
sale_commission = fields.Float(string='销售佣金')
in_country = fields.Boolean(string='是否为国内订单')
is_invoice = fields.Selection([('part', '部分开票'), ('all', '全部开票'), ('noinvoice', '未开票')], string='账单开票情况',readonly=True)
is_makepo = fields.Boolean(string='是否生成采购单',default=True)
purchase_charge_person = fields.Many2one('res.users',string='采购负责人',required=True)
is_purchasing = fields.Boolean(string='是否开始采购',readonly=True)
purchase_date = fields.Datetime(string='开始采购时间',readonly=True)
# is_send = fields.Boolean(string='是否发货',readonly=True)
send_date = fields.Datetime(string='发货时间',readonly=True)
is_pay = fields.Boolean(string='是否收款',readonly=True)
wait_change = fields.Selection([('no', '需变更'), ('yes', '无需变更')],default='yes', string='物料变更',readonly=True)
send_status = fields.Selection([('no', '未发货'), ('yes', '已发货'), ('part', '部分发货')], '发货情况',compute='_compute_sale_state')
transaction_mode = fields.Many2one('transaction.rule',string='交易方式')
transaction_rule = fields.Char(string='交易条款')
delivery_address = fields.Many2one('delivery.address',string='采购交货地址')
tax_id = fields.Many2one('account.tax', string='税', domain=['|', ('active', '=', False), ('active', '=', True)])
discount = fields.Float('折扣金额')
sale_company = fields.Many2one('acc.company',string = '卖方公司')
po_number = fields.Char(string='关联的采购单',readonly=True,copy=False)
before_purchase_id = fields.Many2one('before.purchase',string='待确认生成询价单',copy=False)
origin_sale_order_id = fields.Many2one('sale.order',string='源销售单',copy=False)
sale_order_id = fields.Many2one('sale.order',string='借用销售订单',copy=False)
acc_quotation_id = fields.Many2one('acc.quotation',string='关联报价单',copy=False)
quo_amount_untaxed = fields.Float(string='未含税金额', store=True, readonly=True, compute='_amount_quo_all', track_visibility='always')
quo_amount_tax = fields.Float(string='税', store=True, readonly=True, compute='_amount_quo_all')
quo_amount_total = fields.Float(string='总计', store=True, readonly=True, compute='_amount_quo_all')
# quo_amount_untaxed = fields.Float(string='未含税金额')
# quo_amount_tax = fields.Float(string='税')
# quo_amount_total = fields.Float(string='总计')
bom_line = fields.One2many('sale.bom.line', 'salebom_id', 'Bom Lines',readonly=True)
quotation_line = fields.One2many('quotation.line', 'quotation_id','Quotation Lines')
def search(self, args, offset=0, limit=None, order=None, count=False):
args = args or []
if self._uid == 2:
return super(AccSaleOrder, self).search(args, offset, limit, order, count)
# 管理部门
elif self.env.user.has_group('acct_base.acc_manage_level1_group'):
return super(AccSaleOrder, self).search(args, offset, limit, order, count)
# 销售部经理
elif self.env.user.has_group('acct_base.unovo_it_info_group'):
uids = self.get_department(self._uid)
args.extend([('user_id', 'in', uids)])
return super(AccSaleOrder, self).search(args, offset, limit, order, count)
# 普通员工
else:
args.extend([('user_id', '=', self._uid)])
return super(AccSaleOrder, self).search(args, offset, limit, order, count)
@api.multi
def get_department(self,user_id):
department_ids = []
employee_id = self.env['hr.employee'].search([('user_id', '=', user_id)])
department_id = self.env['hr.department'].search([('manager_id', '=', employee_id.id)])
if department_id:
for did in department_id:
department_ids.append(did.id)
# department_name = department_id.name
# if department_name in ['销售一部','销售二部','销售三部']:
uids = self.make_sale_uid(department_ids,user_id)
return uids
@api.depends('order_line.price_total')
def _amount_all(self):
res = super(AccSaleOrder, self)._amount_all()
self.amount_total = self.amount_untaxed + self.amount_tax - self.discount
return res
@api.multi
def write(self, vals):
# if self.purchase_type == 'office':
# self.check_office_price(vals)
if vals.get('discount'):
amount = self.amount_untaxed + self.amount_tax - vals.get('discount', 0)
vals.update({
"amount_total": amount
})
res = super(AccSaleOrder, self).write(vals)
return res
@api.depends('send_status')
def _compute_sale_state(self):
for order in self:
count = len(order.picking_ids)
if count > 0:
if order.picking_ids and all([x.state in ['done', 'cancel'] for x in order.picking_ids]):
if order.send_status == 'yes':
pass
else:
order.send_status = 'yes'
elif order.picking_ids and all([x.state in ['assigned'] for x in order.picking_ids]):
if order.send_status == 'no':
pass
else:
order.send_status = 'no'
else:
if order.send_status == 'part':
pass
else:
order.send_status = 'part'
else:
order.send_status = 'no'
order.update_isinvoice_state(order)
return True
def update_isinvoice_state(self,order):
invoices = order.mapped('invoice_ids')
if not invoices:
# order.write({'is_invoice':'noinvoice'})
order.is_invoice = 'noinvoice'
else:
invoices_total = 0.0
for invoice in invoices:
invoices_total += invoice.invoice_acc_total
if invoices_total == 0:
order.is_invoice = 'noinvoice'
if invoices_total < order.amount_total and invoices_total != 0:
order.is_invoice = 'part'
if invoices_total == order.amount_total:
order.is_invoice = 'all'
return True
@api.multi
def _fresh_so_state(self):
sale_orders = self.env['sale.order'].search([('state', '=', 'sale')])
# ('date_order', '>', my_date) OR (('state', '=', 'done') AND ('id', '>', my_id))
for order in sale_orders:
_logger.debug('===========%s===============',order.name)
invoices = order.mapped('invoice_ids')
if not invoices:
order.write({'is_invoice':'noinvoice'})
else:
invoices_total = 0.0
for invoice in invoices:
invoices_total += invoice.invoice_acc_total
if invoices_total == 0:
order.write({'is_invoice':'noinvoice'})
if invoices_total < order.amount_total and invoices_total != 0:
order.write({'is_invoice':'part'})
if invoices_total == order.amount_total:
order.write({'is_invoice':'all'})
for order in sale_orders:
cr = self.env.cr
out_qty_sql = """ SELECT
SUM (product_uom_qty) AS product_uom_qty,
sum (qty_delivered) as qty_delivered,
sum (product_uom_qty-qty_delivered) as compare
FROM
sale_order_line
WHERE
order_id = %s"""%(order.id)
cr.execute(out_qty_sql)
result = request.cr.dictfetchall()
if result[0]['qty_delivered'] == 0 and result[0]['compare'] > 0:
order.write({'send_status':'no'})
elif result[0]['qty_delivered'] !=0 and result[0]['compare'] > 0:
order.write({'send_status':'part'})
else:
order.write({'send_status':'yes'})
return True
@api.multi
def make_sale_uid(self,department_ids,user_id):
all_ids = []
cr = self.env.cr
all_total = """ SELECT
ru.id as uid
FROM
hr_employee he
LEFT JOIN res_users ru ON he.user_id = ru. ID
WHERE
department_id in %s """
# cr.execute(all_total)
cr.execute(all_total, (tuple(department_ids),))
result = cr.dictfetchall()
for line in result:
all_ids.append(line['uid'])
all_ids.append(user_id)
return tuple(all_ids)
@api.multi
def bom_record(self,mrp_bom,qty):
vals = {
'salebom_id':self.id,
'mrp_bom_id':mrp_bom.id,
'gen_date':fields.Datetime.now(),
'code':mrp_bom.code,
'product_qty':qty,
'acc_type':mrp_bom.acc_type,
# 'is_active':mrp_bom.active,
}
bom_line = self.env['sale.bom.line'].create(vals)
return True
@api.multi
def check_eco_products(self):
res_line = []
mrp_ecos = []
# add_ids = []
remove_ids = []
if self.bom_line:
for bom in self.bom_line:
mrp_eco = self.env['mrp.eco'].search([('bom_id', '=', bom.mrp_bom_id.id),('state', '=', 'done')])
if mrp_eco:
mrp_eco.write({'is_use':'on'})
mrp_ecos.append(mrp_eco.id)
for change in mrp_eco.bom_change_ids:
if change.change_type == 'add':
# add_ids.append(change.product_id.id)
line_vals = {
'order_id':self.before_purchase_id.id,
'product_id':change.product_id.id,
'product_model':change.product_id.product_tmpl_id.product_model,
'qty':change.new_product_qty * bom.product_qty,
'acc_purchase_price':change.product_id.product_tmpl_id.acc_purchase_price,
'brand':change.product_id.product_tmpl_id.brand,
'acc_code':change.product_id.product_tmpl_id.acc_code,
'partner_code':change.product_id.product_tmpl_id.partner_code
}
res_line.append(line_vals)
if change.change_type == 'update' and change.upd_product_qty > 0:
# add_ids.append(change.product_id.id)
line_vals = {
'order_id':self.before_purchase_id.id,
'product_id':change.product_id.id,
'product_model':change.product_id.product_tmpl_id.product_model,
'qty':change.upd_product_qty * bom.product_qty,
'acc_purchase_price':change.product_id.product_tmpl_id.acc_purchase_price,
'brand':change.product_id.product_tmpl_id.brand,
'acc_code':change.product_id.product_tmpl_id.acc_code,
'partner_code':change.product_id.product_tmpl_id.partner_code
}
res_line.append(line_vals)
if change.change_type == 'remove':
remove_vals = {
'p_id':change.product_id.id,
'product_id':change.product_id.product_tmpl_id.name,
'product_model':change.product_id.product_tmpl_id.product_model,
'qty':change.upd_product_qty * bom.product_qty,
'brand':change.product_id.product_tmpl_id.brand,
'acc_code':change.product_id.product_tmpl_id.acc_code}
remove_ids.append(remove_vals)
if change.change_type == 'update' and change.upd_product_qty < 0:
remove_vals = {
'p_id':change.product_id.id,
'product_id':change.product_id.product_tmpl_id.name,
'product_model':change.product_id.product_tmpl_id.product_model,
'qty':change.upd_product_qty * bom.product_qty,
'brand':change.product_id.product_tmpl_id.brand,
'acc_code':change.product_id.product_tmpl_id.acc_code}
remove_ids.append(remove_vals)
# if not res_line[0]:
# raise ValidationError('没有需要增补的物料!!!')
if not self.bom_line:
raise ValidationError('因之前版本(6.28号之前的单据)未做bom记录,无法进行自动增补!!!')
# products["add"] = res_line
# products["remove"] = remove_ids
return res_line,remove_ids,mrp_ecos
@api.multi
def add_new_bom(self):
before_purchase = self.env['before.purchase'].search([('id', '=', self.before_purchase_id.id)])
res_line,remove_ids,mrp_ecos = self.check_eco_products()
if remove_ids:
remove_products = []
for p in remove_ids:
po_name = self.get_poname(p)
p_str = '物料名称:' + p['product_id'] + ' ' + '型号:' + p['product_model'] + ' ' + '品牌:' + p['brand'] + ' ' + '编码:' + p['acc_code'] + ' ' + '移除数量:' + str(p['qty']) + '采购单号:' + po_name + '<br>'
remove_products.append(p_str)
toaddrs = ['luna.zhang@neotel-technology.com','xue.zhang@neotel.tech','fiona.wu@neotel-technology.com','tina.dong@neotel.tech']
toaddrs.append(self.purchase_charge_person.login)
# toaddrs = ['jie.dong@acctronics.cn']
subjects = "源单据{}所申请物料清单有变动,请及时处理".format(self.name)
message = "源单据{}所申请物料清单,有以下物料从物料清单中移除<br><br>{}".format(self.name,remove_products)
self.env['acc.tools'].send_report_email(subjects,message,toaddrs)
# self.check_res(res_line)
if res_line:
if before_purchase and before_purchase.state == 'draft':
raise ValidationError('该销售订单所关联待确认询价单尚未完成,请完成后再进行物料清单变动操作')
if before_purchase and before_purchase.state == 'done':
new_res_line = []
for line in res_line:
new_line_vals = {
'product_id':line['product_id'],
'product_model':line['product_model'],
'qty':line['qty'],
'acc_purchase_price':line['acc_purchase_price'],
'brand':line['brand'],
'acc_code':line['acc_code'],
'partner_code':line['partner_code'],
}
new_res_line.append((0,0,new_line_vals))
vals = {
'sale_order_id':self.id,
'purchase_company':self.sale_company.id,
'charge_person':self.purchase_charge_person.id,
'delivery_address':self.delivery_address.id,
'order_line':new_res_line
}
bp_obj = self.env['before.purchase'].create(vals)
toaddrs = ['luna.zhang@neotel-technology.com','xue.zhang@neotel.tech']
# toaddrs.append(bp_obj.charge_person.login)
subjects = "待确认询价单{}".format(bp_obj.name)
message = "待确认询价单{}已生成,请及时处理".format(bp_obj.name)
self.env['acc.tools'].send_report_email(subjects,message,toaddrs)
if mrp_ecos:
for ecoid in mrp_ecos:
eco_obj = self.env['mrp.eco'].browse(ecoid)
eco_obj.write({'before_purchase_id':bp_obj.id})
# for remove_id in remove_ids:
# 删除的东西怎么处理?
self.change_bom_record()
self.env.user.notify_success(message='变更成功')
# self.write({'before_purchase_id':bp_obj.id})
#
#
@api.multi
def check_res(self,res_line):
line = res_line
if not line:
raise ValidationError('没有需要增补的物料!!!')
@api.multi
def get_poname(self,p):
product_id = p['p_id']
purchase_orders = self.env['purchase.order'].search([('origin_order', '=', self.id)])
po_ids = [tmp.id for tmp in purchase_orders]
po_line = self.env['purchase.order.line'].search([('order_id', 'in', po_ids),('product_id', '=', product_id)])
if po_line:
# taget_order = self.env['purchase.order'].search([('id', '=', po_line.order_id)])
taget_order = self.env['purchase.order'].browse(po_line[0].order_id.id)
taget_poname = taget_order.name
else:
taget_poname = '无'
return taget_poname
# @api.multi
# def _fresh_now_qty(self):
# excipients = self.env['excipients.product'].search([('is_active', '=', True)])
# for line in excipients:
# p_id = line.product_id.id
# location_id = line.location_id.id
# cr = self.env.cr
# # location_ids = []
# now_qty_sql = """ SELECT
# SUM (quantity-reserved_quantity) AS theory_qty
# FROM
# stock_quant
# WHERE
# location_id = %s
# AND product_id = %s """%(location_id, p_id)
# # now_qty = cr.execute(now_qty_sql, (location_id,p_id))
# cr.execute(now_qty_sql)
# result = request.cr.dictfetchall()
# if result[0]['theory_qty']:
# now_qty = result[0]['theory_qty']
# else:
# now_qty = 0.00
# onway_qty = line.onway_qty()
# line.write({'now_qty':now_qty,'purchase_qty':onway_qty})
# return True
@api.multi
def change_bom_record(self):
for bom_line in self.bom_line:
bom_line.unlink()
for line in self.order_line:
product_tmpl_id = line.product_id.product_tmpl_id.id
qty = line.product_uom_qty
mrp_bom = self.env['mrp.bom'].search([('product_tmpl_id', '=', product_tmpl_id),('active', '=', True)])
if mrp_bom:
self.bom_record(mrp_bom,qty)
now_time = fields.Datetime.now()
self.write({'bomupdate_time':now_time,'wait_change':'yes'})
@api.multi
def check_bom(self,line):
product_tmpl_id = line.product_id.product_tmpl_id.id
mrp_bom = self.env['mrp.bom'].search([('product_tmpl_id', '=', product_tmpl_id),('active', '=', True)])
return mrp_bom
def make_mrp_vals(self,mrp_bom,qty):
res_line = []
for bom_line in mrp_bom.bom_line_ids:
bom_line_vals = {
'product_id':bom_line.product_id.id,
'product_model':bom_line.product_id.product_tmpl_id.product_model,
'qty':bom_line.product_qty * qty,
'acc_purchase_price':bom_line.product_id.product_tmpl_id.acc_purchase_price,
'brand':bom_line.product_id.product_tmpl_id.brand,
'acc_code':bom_line.product_id.product_tmpl_id.acc_code,
'partner_code':bom_line.product_id.product_tmpl_id.partner_code,
}
# res_line = [(0,0,bom_line_vals)]
res_line.append((0,0,bom_line_vals))
return res_line
@api.multi
def create_before_po(self):
res_line = []
for line in self.order_line:
mrp_bom = self.check_bom(line)
if mrp_bom:
qty = line.product_uom_qty
self.bom_record(mrp_bom,qty)
mrp_vals = self.make_mrp_vals(mrp_bom,qty)
res_line += mrp_vals
else:
line_vals = {
'product_id':line.product_id.id,
'product_model':line.product_id.product_tmpl_id.product_model,
'qty':line.product_uom_qty,
'acc_purchase_price':line.product_id.product_tmpl_id.acc_purchase_price,
'brand':line.product_id.product_tmpl_id.brand,
'acc_code':line.product_id.product_tmpl_id.acc_code,
'partner_code':line.product_id.product_tmpl_id.partner_code
}
res_line.append((0,0,line_vals))
vals = {
'sale_order_id':self.id,
'charge_person':self.purchase_charge_person.id,
'delivery_address':self.delivery_address.id,
'purchase_company':self.sale_company.id,
'order_line':res_line
}
bp_obj = self.env['before.purchase'].create(vals)
self.write({'before_purchase_id':bp_obj.id})
# toaddrs = ['jie.dong@acctronics.cn','cissy.shen@acctronics.cn','yuanyuan.lu@acctronics.cn']
# toaddrs = []
# toaddrs.append(bp_obj.charge_person.login)
toaddrs = []
toaddrs.append(bp_obj.charge_person.login)
subjects = "待确认询价单{}".format(bp_obj.name)
message = "待确认询价单{}已生成,请及时处理".format(bp_obj.name)
self.env['acc.tools'].send_report_email(subjects,message,toaddrs)
return True
# def log_field(self):
# 额外重新生成待确认询价单流程
@api.multi
def re_create_before_po(self,sale_order_id,product,qty):
res_line = []
mrp_bom = self.env['mrp.bom'].search([('product_tmpl_id', '=', product.product_tmpl_id.id),('active', '=', True)])
if mrp_bom:
mrp_vals = self.make_mrp_vals(mrp_bom,qty)
res_line += mrp_vals
vals = {
'sale_order_id':sale_order_id.id,
'charge_person':self.purchase_charge_person.id,
'delivery_address':self.delivery_address.id,
'purchase_company':self.sale_company.id,
'order_line':res_line
}
bp_obj = self.env['before.purchase'].create(vals)
self.write({'before_purchase_id':bp_obj.id})
toaddrs = []
toaddrs.append(bp_obj.charge_person.login)
subjects = "待确认询价单{}".format(bp_obj.name)
message = "待确认询价单{}已生成,请及时处理".format(bp_obj.name)
self.env['acc.tools'].send_report_email(subjects,message,toaddrs)
body = "重新生成待确认询价单: %s" % (bp_obj.name)
sale_order_id.message_post(body=body, message_type='comment')
return True
@api.multi
def make_sale_purchase(self,sent_product,sale_order_id):
for product in sent_product:
mrp_production = self.env['mrp.production'].search([('product_id', '=', product.id),('origin', '=', sale_order_id.name)])
if mrp_production:
done_qty = sum([m.qty_done for m in mrp_production.finished_move_line_ids])
trans_qty = self.env['sale.order.line'].search([('order_id', '=', self.id),('product_id', '=', product.id)]).product_uom_qty
if trans_qty > done_qty:
raise ValidationError('借用数量超过借用销售订单的<{}>生产完成数量!!'.format(product.product_tmpl_id.name))
else:
self.re_create_before_po(sale_order_id,product,trans_qty)
new_mrp_production = self.env['mrp.production'].search([('product_id', '=', product.id),('origin', '=', self.name)])
new_mrp_production.write({'origin':sale_order_id.name})
body = "重新生成生产单: %s" % (new_mrp_production.name)
sale_order_id.message_post(body=body, message_type='comment')
else:
raise ValidationError('未找到借用销售单关联的生产单!!')
@api.multi
def action_confirm(self):
res = super(AccSaleOrder, self).action_confirm()
if self.is_makepo == True:
self.create_before_po()
sale_order_id = self.sale_order_id
if sale_order_id:
move_product_ids = []
sale_product_ids = []
for move_line in self.sale_order_id.order_line:
move_product_ids.append(move_line.product_id)
for sale_line in self.order_line:
sale_product_ids.append(sale_line.product_id)
sent_product = [x for x in move_product_ids if x in sale_product_ids]
if sent_product:
self.make_sale_purchase(sent_product,sale_order_id)
else:
raise ValidationError('借用销售单中不存在要借用产品,请确认')
return res
@api.multi
def certain(self):
self.filtered(lambda r: r.state == 'draft').write({'state': 'certain'})
return True
@api.multi
def confirm(self):
self.filtered(lambda r: r.state == 'draft').write({'state': 'confirm'})
# toaddrs = []
# toaddrs.append(self.manage_user.login)
toaddrs = ['luna.zhang@neotel-technology.com']
# toaddrs = ['jie.dong@acctronics.cn','yapeng.dai@acctronics.cn']
subjects = "销售单{}已提交需要您确认,请及时处理".format(self.name)
message = "销售单{}已提交需要您确认,请及时处理".format(self.name)
self.env['acc.tools'].send_report_email(subjects,message,toaddrs)
self.env.user.notify_success(message='提交成功')
return True
# @api.multi
# def boss_accept(self):
# self.filtered(lambda r: r.state == 'confirm').write({'state': 'boss'})
# toaddrs = ['yuanyuan.lu@neotel-technology.com']
# # toaddrs = ['jie.dong@acctronics.cn','yapeng.dai@acctronics.cn']
# subjects = "销售单{}管理部已审批完成,请及时确认".format(self.name)
# message = "销售单{}管理部已审批完成,请及时确认".format(self.name)
# self.env['acc.tools'].send_report_email(subjects,message,toaddrs)
# return True
class AccSaleLine(models.Model):
"""
采购单继承
"""
_inherit = "sale.order.line"
acc_code = fields.Char(related='product_id.acc_code',readonly=True, store=True,string='产品编码')
brand = fields.Char(related='product_id.brand',readonly=True, store=True,string='品牌')
@api.onchange('product_uom_qty', 'product_uom')
def product_uom_change(self):
res = super(AccSaleLine, self).product_uom_change()
# your logic here
for rec in self:
# rec.price_unit = self.product_id.product_tmpl_id.acc_purchase_price
rec.name = self.product_id.product_tmpl_id.product_model
# rec.acc_code = self.product_id.product_tmpl_id.acc_code
# rec.brand = self.product_id.product_tmpl_id.brand
return res
class QuotationLine(models.Model):
"""
报价单明细
"""
_name = 'quotation.line'
# _inherit = ['mail.thread']
_description = "报价单明细"
quotation_id = fields.Many2one('sale.order', 'Order Reference')
product_qty = fields.Float(u'数量', required=True)
product_id = fields.Many2one('product.product',u'物料名称',required=True)
product_model = fields.Char(string='规格型号')
acc_code = fields.Char(string='产品编码')
brand = fields.Char(related='product_id.brand',readonly=True, store=True,string='品牌')
description = fields.Char(string='产品描述')
# 'default_code': fields.char(u'参考编号'),
product_uom = fields.Many2one('uom.uom', string='单位')
price_unit = fields.Float(u'单价')
price_subtotal = fields.Float(compute='_compute_amount',string='小计',store=True)
# price_total = fields.Monetary(compute='_compute_amount', string='总计', store=True)
#
@api.onchange('product_id')
def onchange_product_id(self):
result = {}
if not self.product_id:
return result
self.price_unit = self.product_qty = 0.0
self.product_model = self.product_id.product_tmpl_id.product_model
self.description = self.product_id.product_tmpl_id.product_describe_cn
self.acc_code = self.product_id.product_tmpl_id.acc_code
self.product_uom = self.product_id.product_tmpl_id.uom_id
return result
@api.depends('product_qty', 'price_unit')
def _compute_amount(self):
price_subtotal = 0.00
for line in self:
price_subtotal = line.price_unit * line.product_qty
line.update({
'price_subtotal': price_subtotal,
})
class SaleBomLine(models.Model):
_name = 'sale.bom.line'
_description = "物料清单记录"
salebom_id = fields.Many2one('sale.order', '销售单')
mrp_bom_id = fields.Many2one('mrp.bom',string='物料清单')
code = fields.Char(string='参考')
product_qty = fields.Float(string='数量')
acc_type = fields.Selection([('change', '可变'), ('base', '基础'),('standard','标准')], '内部类型')
gen_date = fields.Datetime(string='时间')
# is_active = fields.Boolean(string='有效')
class AccQuotation(models.Model):
"""
挚锦报价单
"""
_name = 'acc.quotation'
_inherit = ['mail.thread']
_description = "挚锦报价单"
_order = 'gen_date desc'
@api.multi
def unlink(self):
for quotation in self:
if quotation.state == 'done':
raise ValidationError('不能删除已完成的挚锦报价单.')
return super(AccQuotation,self).unlink()
@api.model
def create(self,vals):
if not vals.get('name',''):
last_name = self.env['ir.sequence'].get('acc.quotation') or ''
vals['name'] = "%s"%(last_name)
# amount = vals.get('amount_total',0) - vals.get('discount',0)
# vals.update({
# "amount_total": amount
# })
result = super(AccQuotation,self).create(vals)
return result
@api.depends('accquotation_line.price_subtotal','discount')
def _amount_all(self):
for order in self:
if order.tax_id:
tax_rate = order.tax_id.amount/100
else:
tax_rate = 1
amount_total = 0.0
amount_untaxed = 0.0
amount_tax = 0.0
discount = self.discount
for line in order.accquotation_line:
amount_untaxed += line.price_subtotal
amount_tax += line.price_subtotal * tax_rate
order.update({
'amount_untaxed':amount_untaxed,
'amount_tax':amount_tax,
'amount_total': amount_untaxed + amount_tax - discount,
})
name = fields.Char(string=u'名称',readonly=True,index=True, copy=False, default='New')
pricelist_id = fields.Many2one('product.pricelist',string='价格表',required=True)
title = fields.Char(string=u'标题', required=True)
gen_date = fields.Date(string=u'单据日期',default=lambda self: self._context.get('date', fields.Date.context_today(self)),readonly=True)
last_date = fields.Date(string="最后修改日期",default=lambda self: self._context.get('date', fields.Date.context_today(self)),readonly = True)
customer_state = fields.Selection([('nowcreate', '已创建'),('sent', '已发送'),('refuse', '已拒绝'),('cancel', '已取消')], '报价单状态', copy=False, default='nowcreate')
user_id = fields.Many2one('res.users',string=u'负责人',default=lambda self: self.env.user.id,required=True)
partner_id = fields.Many2one('res.partner',string='客户')
contact_id = fields.Many2one('res.partner',string='联系人')
transfer = fields.Char(string='承运人')
origin_country = fields.Char(string="货物原产国及厂家")
port_shipment = fields.Char(string="发货机场")
destination_port = fields.Char(string="目的机场")
destination_address = fields.Char(string="交货地址")
shipping_method = fields.Char(string="运输方式")
is_createso = fields.Boolean(string='是否已生成销售订单',copy=False,readonly=True)
sale_commission = fields.Float(string='销售佣金')
delivery_time = fields.Char(string=u'货期',placeholder="填写格式如(1周,1weeks,1天,1days)")
in_country = fields.Boolean(string='是否为国内订单')
currency_id = fields.Many2one('res.currency', '币种', required=True,
default=lambda self: self.env.user.company_id.currency_id.id)
crm_lead_id = fields.Many2one('crm.lead',string='商机',readonly=True)
# is_makepo = fields.Boolean(string='是否生成采购单',default=True)
purchase_charge_person = fields.Many2one('res.users',string='采购负责人',required=True)
note1 = fields.Char(string='附注1',placeholder="填写格式如: 以上价格为人民币价格,含13.0%增值税")
note4 = fields.Char(string='备注')
# is_purchasing = fields.Boolean(string='是否开始采购',readonly=True)
# purchase_date = fields.Datetime(string='开始采购时间',readonly=True)
# is_send = fields.Boolean(string='是否发货',readonly=True)
# send_date = fields.Datetime(string='发货时间',readonly=True)
# is_pay = fields.Boolean(string='是否收款',readonly=True)
discount = fields.Float(string='折扣',default=0.00)
ship_fee = fields.Float(string='运费',default=0.00)
state = fields.Selection([('draft', '草稿'),('sent', '已发送'),('done', '完成'),('cancel', '取消')], '状态', default='draft',track_visibility='always')
transaction_mode = fields.Many2one('transaction.rule',string='交易方式')
transaction_rule = fields.Char(string='交易条款')
# validity_date = fields.Date(string='有效期至')
sale_company = fields.Many2one('acc.company',string = '卖方公司')
tax_id = fields.Many2one('account.tax', string='税', domain=['|', ('active', '=', False), ('active', '=', True)])
amount_untaxed = fields.Float(string='未含税金额', store=True, readonly=True, compute='_amount_all', track_visibility='always')
amount_tax = fields.Float(string='税', store=True, readonly=True, compute='_amount_all')
amount_total = fields.Float(string='总计', store=True, readonly=True, compute='_amount_all')
sale_order = fields.Many2one('sale.order',string='关联的销售单',copy=False,readonly=True)
# before_purchase_id = fields.Many2one('before.purchase',string='待确认生成询价单')
accquotation_line = fields.One2many('accquotation.line', 'accquotation_id', 'Quotation Lines',copy=True)
log_line = fields.One2many('log.line', 'log_id', 'Log Lines',readonly=True)
@api.multi
def create_so(self):
if self.state == 'done':
res_line = []
for line in self.accquotation_line:
# mrp_bom = self.check_bom(line)
line_vals = {
'product_id':line.product_id.id,
'product_model':line.product_model,
'acc_code':line.acc_code,
'description':line.description,
'product_uom':line.product_uom.id,
'price_unit':line.price_unit,
'product_qty':line.product_qty
}
res_line.append((0,0,line_vals))
vals = {
'partner_id':self.partner_id.id,
'acc_quotation_id':self.id,
'contact_id':self.contact_id.id,
'pricelist_id':self.pricelist_id.id,
'title':self.title,
'transfer':self.transfer,
'sale_commission':self.sale_commission,
'in_country':self.in_country,
'purchase_charge_person':self.purchase_charge_person.id,
'transaction_mode':self.transaction_mode.id,
'transaction_rule':self.transaction_rule,
'sale_company':self.sale_company.id,
'tax_id':self.tax_id.id,
'discount':self.discount,
# 'quo_amount_untaxed':self.amount_untaxed,
# 'quo_amount_total':self.amount_total,
'quotation_line':res_line
}
so_obj = self.env['sale.order'].create(vals)
self.write({'sale_order':so_obj.id,'is_createso':True})
else:
raise ValidationError('该报价单未确认,请先确认后再创建销售单')
return True
@api.multi
def cancel(self):
self.filtered(lambda r: r.state == 'draft').write({'state': 'cancel','customer_state': 'cancel'})
return True
@api.multi
def certain(self):
self.write({'state':'done'})
return True
@api.multi
def draft_sent(self):
self.filtered(lambda r: r.state == 'sent').write({'state': 'draft','customer_state': 'nowcreate'})
return True
@api.multi
def cancel_draft(self):
self.filtered(lambda r: r.state == 'cancel').write({'state': 'draft','customer_state': 'nowcreate'})
return True
@api.multi
def sent(self):
contact_name = self.contact_id.name
contact_mobile = self.contact_id.mobile
contact_title = self.contact_id.title.name
topic = self.title
if not contact_mobile:
raise ValidationError('联系人未填手机号,请填写联系人手机号')
if not contact_title:
raise ValidationError('联系人称呼未正确配置!')
ret = re.match(r"^1[35678]\d{9}$", contact_mobile)
if not ret:
raise ValidationError('联系人手机号填写格式不正确!')
text = contact_name + contact_title + '您好,' +'“' + topic + '”' + '报价单已发到您邮件,请注意查收,谢谢'
vals = {
'topic':topic,
'user_name':contact_name,
'phone':contact_mobile,
'name':text
}
message = self.env['acc.message.interface'].create(vals)
message.sms_send()
self.filtered(lambda r: r.state == 'draft').write({'state': 'sent','customer_state': 'sent'})
return True
@api.multi
def write(self, vals):
vals['last_date'] = fields.Date.context_today(self)
res = super(AccQuotation, self).write(vals)
# self.last_date = fields.Date.context_today(self)
if vals.get('accquotation_line'):
q_vals = {
'log_id':self.id,
'user_id':self.user_id.id,
'gen_date':fields.Datetime.now(),
'amount_total':self.amount_total,
}
log = self.env['log.line'].create(q_vals)
return res
class AccquotationLine(models.Model):
"""
报价单明细
"""
_name = 'accquotation.line'
# _inherit = ['mail.thread']
_description = "报价单明细"
accquotation_id = fields.Many2one('acc.quotation', 'Order Reference')
product_qty = fields.Float(u'数量', required=True)
product_id = fields.Many2one('product.product',u'物料名称',required=True)
product_model = fields.Char(string='规格型号')
acc_code = fields.Char(string='产品编码')
brand = fields.Char(related='product_id.brand',readonly=True, store=True,string='品牌')
description = fields.Char(string='产品描述')
# 'default_code': fields.char(u'参考编号'),
product_uom = fields.Many2one('uom.uom', string='单位')
price_unit = fields.Float(u'单价')
price_subtotal = fields.Float(compute='_compute_amount',string='小计',store=True)
# price_total = fields.Monetary(compute='_compute_amount', string='总计', store=True)
#
@api.onchange('product_id')
def onchange_product_id(self):
result = {}
if not self.product_id:
return result
self.price_unit = self.product_qty = 0.0
self.product_model = self.product_id.product_tmpl_id.product_model
self.description = self.product_id.product_tmpl_id.product_describe_cn
self.acc_code = self.product_id.product_tmpl_id.acc_code
self.product_uom = self.product_id.product_tmpl_id.uom_id
return result
@api.depends('product_qty', 'price_unit')
def _compute_amount(self):
price_subtotal = 0.00
for line in self:
price_subtotal = line.price_unit * line.product_qty
line.update({
'price_subtotal': price_subtotal,
})
class LogLine(models.Model):
"""
修改日志
"""
_name = 'log.line'
# _inherit = ['mail.thread']
_description = "挚锦报价单明细"
log_id = fields.Many2one('acc.quotation', 'Log')
user_id = fields.Many2one('res.users',u'修改人',required=True)
gen_date = fields.Datetime(string=u'修改日期',default=lambda self: fields.Datetime.now(),readonly=True)
amount_total = fields.Float(string='修改金额')