~vauxoo/addons-vauxoo/7.0-demo_maintenance-dev_luis

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
# -*- encoding: utf-8 -*-
###########################################################################
#    Module Writen to OpenERP, Open Source Management Solution
#
#    Copyright (c) 2012 Vauxoo - http://www.vauxoo.com
#    All Rights Reserved.
#    info@vauxoo.com
############################################################################
#    Coded by: julio (julio@vauxoo.com)
############################################################################
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as
#    published by the Free Software Foundation, either version 3 of the
#    License, or (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
from osv import osv,fields
from tools.translate import _
import netsvc
import time

class mrp_production(osv.osv):
    _inherit='mrp.production'

    def action_finish(self,cr,uid,ids,context={}):
        res = super(mrp_production, self).action_finish(cr,uid,ids,context=context)
        self.create_move_variation(cr,uid,ids,context=context)
        return res

    def create_move_variation(self,cr,uid,ids,context={}):
        move_obj = self.pool.get('account.move')
        account_moves = []
        for production in self.browse(cr,uid,ids,context=context):
            for prod_variation in production.variation_ids:
                context['type'] = 'consumed'
                if prod_variation.product_id and prod_variation.product_id.valuation == 'real_time' and prod_variation.quantity <> 0:
                    j_id, src_acc, dest_acc, reference_amount = self.get_journal_accounts(cr,uid,prod_variation,production,context=context)
                    account_moves += [(j_id, self.create_account_variation_move_line(cr,uid,prod_variation,src_acc,dest_acc,reference_amount))]

            for prod_variation in production.variation_finished_product_ids:
                context['type'] = 'produced'
                if prod_variation.product_id and prod_variation.product_id.valuation == 'real_time' and prod_variation.quantity <> 0:
                    j_id, src_acc, dest_acc, reference_amount = self.get_journal_accounts(cr,uid,prod_variation,production,context=context)
                    account_moves += [(j_id, self.create_account_variation_move_line(cr,uid,prod_variation,src_acc,dest_acc,reference_amount))]

            if account_moves:
                for j_id,move_lines in account_moves:
                    move_obj.create(cr, uid,
                        {
                         'journal_id': j_id,
                         'line_id': move_lines,
                         'ref': 'PROD: ' + production.name })


        return True

    def get_journal_accounts(self,cr,uid,product,production,context={}):

        if not context:
            context = {}

        src_acc = False
        dest_acc = False

        if context.get('type',False) == 'consumed':
            if product.quantity > 0:
                if production.product_id.property_stock_production.valuation_in_account_id:
                    src_acc = production.product_id.property_stock_production.valuation_in_account_id.id
                if product.product_id.type == 'service':
                    dest_acc = product.product_id.categ_id.property_stock_valuation_account_id and product.product_id.categ_id.property_stock_valuation_account_id.id or False
                elif production.product_id.property_stock_production.variation_in_account_id:
                    dest_acc = production.product_id.property_stock_production.variation_in_account_id.id
                reference_amount = product.cost_variation

            if product.quantity < 0:
                if product.product_id.type == 'service':
                    src_acc = product.product_id.categ_id.property_stock_valuation_account_id and product.product_id.categ_id.property_stock_valuation_account_id.id or False
                elif production.product_id.property_stock_production.variation_in_account_id:
                    src_acc = production.product_id.property_stock_production.variation_in_account_id.id
                if production.product_id.property_stock_production.valuation_in_account_id:
                    dest_acc = production.product_id.property_stock_production.valuation_in_account_id.id
                reference_amount = product.cost_variation*-1

        if context.get('type',False) == 'produced':
            if product.quantity > 0:
                if production.product_id.property_stock_production.valuation_out_account_id:
                    src_acc = production.product_id.property_stock_production.variation_out_account_id.id
                if production.product_id.property_stock_production.variation_out_account_id:
                    dest_acc = production.product_id.property_stock_production.valuation_out_account_id.id
                reference_amount = product.cost_variation
            if product.quantity < 0:
                if production.product_id.property_stock_production.variation_out_account_id:
                    src_acc = production.product_id.property_stock_production.valuation_out_account_id.id
                if production.product_id.property_stock_production.valuation_out_account_id:
                    dest_acc = production.product_id.property_stock_production.variation_out_account_id.id
                reference_amount = product.cost_variation*-1

        journal_id = product.product_id.categ_id.property_stock_journal.id
        if not src_acc or not dest_acc:
            raise osv.except_osv(_('Error!'),  _('There is no account defined for this location: "%s" ') % \
                                    (production.product_id.property_stock_production.name,))

        if not journal_id:
            raise osv.except_osv(_('Error!'), _('There is no journal defined on the product category: "%s" (id: %d)') % \
                                    (product.product_id.categ_id.name, product.product_id.categ_id.id,))

        return journal_id, src_acc, dest_acc, reference_amount

    def create_account_variation_move_line(self, cr, uid, prod_variation, src_account_id, dest_account_id, reference_amount, context=None):
        debit_line_vals = {
                    'name': 'PROD: ' + prod_variation.production_id.name +' - '+ (prod_variation.product_id and prod_variation.product_id.name or ''),
                    'product_id': prod_variation.product_id and prod_variation.product_id.id or False,
                    'quantity': prod_variation.quantity,
 #                   'ref': 'prueba',
                    'date': time.strftime('%Y-%m-%d'),
#                    'partner_id': partner_id,
                    'debit': reference_amount,
                    'account_id': dest_account_id,
        }
        credit_line_vals = {
                    'name': 'PROD: ' + prod_variation.production_id.name +' - '+ (prod_variation.product_id and prod_variation.product_id.name or ''),
                    'product_id': prod_variation.product_id and prod_variation.product_id.id or False,
                    'quantity': prod_variation.quantity,
   #                 'ref': 'prueba',
                    'date': time.strftime('%Y-%m-%d'),
 #                   'partner_id': partner_id,
                    'credit': reference_amount,
                    'account_id': src_account_id,
        }

        return [(0, 0, debit_line_vals), (0, 0, credit_line_vals)]
    
#    def action_production_end(self, cr, uid, ids):
 #       res  = super(mrp_production, self).action_production_end(cr, uid, ids)
  #      self.action_finish(cr, uid, ids)
   #     return res
    
mrp_production()


class stock_move(osv.osv):
    _inherit = 'stock.move'

    def _create_account_move_line(self, cr, uid, move, src_account_id, dest_account_id, reference_amount, reference_currency_id, context=None):
        res = super(stock_move, self)._create_account_move_line(cr, uid, move, src_account_id, dest_account_id, reference_amount, reference_currency_id, context=context)
        for lin in res:
            lin[2]['name'] = move.name +' - '+ (move.product_id and move.product_id.name or '')
        return res

stock_move()