19
19
#
20
20
###############################################################################
21
21
22
- from openerp import models , fields , api
22
+ from openerp import models , fields , api , _
23
+ from openerp .exceptions import Warning
23
24
24
25
class payment_order (models .Model ):
25
26
_inherit = 'payment.order'
@@ -29,9 +30,17 @@ class payment_order(models.Model):
29
30
30
31
@api .one
31
32
def create_account_move (self ):
33
+ """
34
+ This method is creating an account move from the payment.
35
+ """
36
+ if not self .journal_id :
37
+ raise Warning (_ ('Impossible to create the move,' \
38
+ '\n Please select a journal.' ))
39
+ if self .move_id :
40
+ raise Warning (_ ("There's already a move related to this payment." ))
32
41
move_obj = self .env ['account.move' ]
33
42
move_line_obj = self .env ['account.move.line' ]
34
- if not self .move_id :
43
+ if not self .move_id and self . line_ids :
35
44
today = fields .Date .today ()
36
45
move = move_obj .create ({
37
46
'journal_id' : self .journal_id .id ,
@@ -41,36 +50,109 @@ def create_account_move(self):
41
50
})
42
51
self .move_id = move .id
43
52
reconciles = []
53
+ group_lines = self .journal_id .group_payment_line
54
+ debit_grouped = 0.0
55
+ credit_grouped = 0.0
56
+ credit_account_id = self .journal_id .default_credit_account_id .id
57
+ debit_account_id = self .journal_id .default_debit_account_id .id
44
58
for line in self .line_ids :
45
59
move_line = line .move_line_id
46
- debit_line = move_line_obj .\
60
+ if not move_line :
61
+ raise Warning (_ ('The line %s has not related move' % line .name ))
62
+ debit = move_line .debit
63
+ credit = move_line .credit
64
+ account_id = move_line .account_id .id
65
+ if not account_id :
66
+ if debit :
67
+ account_id = credit_account_id
68
+ else :
69
+ account_id = debit_account_id
70
+ if not account_id :
71
+ raise Warning (_ ("There is at least one line without an " \
72
+ "account defined, so make sure the defaults " \
73
+ "credit and debit account are defined in " \
74
+ "the related Journal." ))
75
+ to_reconcile_line = move_line_obj .\
47
76
create ({
48
77
'partner_id' : move_line .partner_id .id ,
49
78
'name' : move_line .name ,
50
79
'journal_id' : self .journal_id .id ,
51
80
'date' : self .move_id .date ,
52
- 'debit' : move_line . credit ,
53
- 'credit' : 0 ,
54
- 'account_id' : move_line . account_id . id ,
81
+ 'debit' : credit ,
82
+ 'credit' : debit ,
83
+ 'account_id' : account_id ,
55
84
'period_id' : self .move_id .period_id .id ,
56
85
'move_id' : self .move_id .id ,
57
86
})
58
- credit_line = move_line_obj .\
87
+ if not group_lines :
88
+ other_line = move_line_obj .\
89
+ create ({
90
+ 'partner_id' : move_line .partner_id .id ,
91
+ 'name' : move_line .name ,
92
+ 'journal_id' : self .journal_id .id ,
93
+ 'date' : self .move_id .date ,
94
+ 'debit' : debit ,
95
+ 'credit' : credit ,
96
+ 'account_id' : debit and credit_account_id or \
97
+ debit_account_id ,
98
+ 'period_id' : self .move_id .period_id .id ,
99
+ 'move_id' : self .move_id .id ,
100
+ })
101
+ else :
102
+ credit_grouped += credit
103
+ debit_grouped += debit
104
+ reconciles .append (move_line + to_reconcile_line )
105
+ if credit_grouped :
106
+ move_line_obj .\
59
107
create ({
60
- 'partner_id' : move_line . partner_id . id ,
61
- 'name' : move_line . name ,
108
+ 'partner_id' : False ,
109
+ 'name' : _ ( 'Centralized credit' ) ,
62
110
'journal_id' : self .journal_id .id ,
63
111
'date' : self .move_id .date ,
64
112
'debit' : 0 ,
65
- 'credit' : move_line .credit ,
66
- 'account_id' : self .journal_id .\
67
- default_debit_account_id .id ,
113
+ 'credit' : credit_grouped ,
114
+ 'account_id' : debit_account_id ,
115
+ 'period_id' : self .move_id .period_id .id ,
116
+ 'move_id' : self .move_id .id ,
117
+ })
118
+ if debit_grouped :
119
+ move_line_obj .\
120
+ create ({
121
+ 'partner_id' : False ,
122
+ 'name' : _ ('Centralized debit' ),
123
+ 'journal_id' : self .journal_id .id ,
124
+ 'date' : self .move_id .date ,
125
+ 'debit' : debit_grouped ,
126
+ 'credit' : 0 ,
127
+ 'account_id' : credit_account_id ,
68
128
'period_id' : self .move_id .period_id .id ,
69
129
'move_id' : self .move_id .id ,
70
130
})
71
- reconciles .append (move_line + debit_line )
72
131
for rec in reconciles :
73
132
rec .reconcile ('manual' )
74
133
return True
75
134
135
+ @api .one
136
+ def delete_account_move (self ):
137
+ if not self .move_id :
138
+ raise Warning (_ ("There's nothing to delete" ))
139
+ if self .move_id .state != 'draft' :
140
+ raise Warning (_ ("Impossible to delete this move, " \
141
+ "please set it to draft before deleting it" ))
142
+ line_obj = self .env ['account.move.line' ]
143
+ lines = line_obj .search (['|' ,
144
+ ('reconcile_partial_id' , '!=' , False ),
145
+ ('reconcile_id' , '!=' , False ),
146
+ ('move_id' , '=' , self .move_id .id ),
147
+ ])
148
+ reconciles = self .env ['account.move.reconcile' ]
149
+ for line in lines :
150
+ if line .reconcile_id :
151
+ reconciles |= line .reconcile_id
152
+ if line .reconcile_partial_id :
153
+ reconciles |= line .reconcile_partial_id
154
+ reconciles .unlink ()
155
+ self .move_id .unlink ()
156
+ return True
157
+
76
158
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
0 commit comments