~camptocamp/c2c-rd-addons/8.0a

« back to all changes in this revision

Viewing changes to c2c_stock_accounting/stock.py

  • Committer: ferdinand
  • Date: 2011-04-30 20:34:01 UTC
  • Revision ID: office@chricar.at-20110430203401-eqfv4au4tv3faj93
[ADD] initial

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
##############################################################################
 
3
#
 
4
#    OpenERP, Open Source Management Solution
 
5
#    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
 
6
#    Copyright (C) 2010-2010 Camptocamp Austria (<http://www.camptocamp.at>)
 
7
#
 
8
#    This program is free software: you can redistribute it and/or modify
 
9
#    it under the terms of the GNU Affero General Public License as
 
10
#    published by the Free Software Foundation, either version 3 of the
 
11
#    License, or (at your option) any later version.
 
12
#
 
13
#    This program is distributed in the hope that it will be useful,
 
14
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
16
#    GNU Affero General Public License for more details.
 
17
#
 
18
#    You should have received a copy of the GNU Affero General Public License
 
19
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
20
#
 
21
##############################################################################
 
22
 
 
23
from osv import osv, fields
 
24
import decimal_precision as dp
 
25
 
 
26
import math
 
27
#from _common import rounding
 
28
import re  
 
29
from tools.translate import _
 
30
        
 
31
import sys
 
32
 
 
33
#----------------------------------------------------------
 
34
#  Stock Move INHERIT
 
35
#----------------------------------------------------------
 
36
class stock_move(osv.osv):
 
37
    _inherit = "stock.move"
 
38
 
 
39
    def _compute_move_value_cost(self, cr, uid, ids, name, args, context):
 
40
        if not ids : return {}
 
41
        result = {}
 
42
        for move in self.browse(cr, uid, ids):
 
43
            print >> sys.stderr,'type cost', move.picking_id.type
 
44
            if move.state in ['done','cancel']: return {}
 
45
            if move.purchase_line_id:
 
46
                result[move.id] = move.product_qty * move.purchase_line_id.price_subtotal
 
47
            else:
 
48
                loc_id = str(move.location_id.id)
 
49
                print >> sys.stderr, 'loc_id ',loc_id
 
50
                sql = 'select \
 
51
                 sum( case when location_id = '+loc_id+' then -move_value_cost else 0 end + case when location_dest_id = '+loc_id+' then move_value_cost else 0 end) as sum_amount, \
 
52
                 sum( case when location_id = '+loc_id+' then -product_qty else 0 end + case when location_dest_id     = '+loc_id+' then product_qty else 0 end) as sum_qty \
 
53
                 from stock_move \
 
54
                where product_id = '+ str(move.product_id.id) +' \
 
55
                  and state = \'done\' \
 
56
                  and (location_id = '+loc_id+' or location_dest_id = '+loc_id+')' 
 
57
                if move.prodlot_id:
 
58
                   sql = sql + ' and prodlot_id = ' + str(move.prodlot_id.id )
 
59
                print >> sys.stderr, 'sql ',sql
 
60
                cr.execute(sql)
 
61
                for r in cr.dictfetchall():
 
62
                   sum_amount = r['sum_amount']
 
63
                   sum_qty    = r['sum_qty']
 
64
                   print >> sys.stderr, 'sum ', sum_amount,sum_qty
 
65
                   if sum_qty and sum_qty > 0.0 and sum_amount > 0.0:
 
66
                       avg_price = sum_amount / sum_qty 
 
67
                       result[move.id] = move.product_qty * avg_price
 
68
                   else :
 
69
                       result[move.id] = move.product_qty * move.product_id.standard_price
 
70
                
 
71
        return result
 
72
 
 
73
    def _compute_move_value_sale(self, cr, uid, ids, name, args, context):
 
74
        print >> sys.stderr, 'value_sale'
 
75
        if not ids: return {}
 
76
        result = {}
 
77
        for move in self.browse(cr, uid, ids):
 
78
            if move.state in ['done','cancel']: return {}
 
79
            print >> sys.stderr,'type sale', move.picking_id.type
 
80
            if move.sale_line_id:
 
81
                result[move.id] = move.product_qty * move.sale_line_id.price_subtotal
 
82
                print >> sys.stderr, 'value_sale', result[move.id]
 
83
        return result
 
84
 
 
85
    def _period_id(self, cr, uid, ids, name, arg, context):
 
86
         result = {}
 
87
         for move in self.browse(cr, uid, ids):
 
88
             #period_ids= self.pool.get('account.period').search(cr,uid,[('date_start','<=',move.date),('date_stop','>=',move.date ), ('special','=',False)])
 
89
             period_ids= self.pool.get('account.period').search(cr,uid,[('date_start','<=',move.date),('date_stop','>=',move.date )])
 
90
             
 
91
             if len(period_ids):
 
92
                 result[move.id] = period_ids[0]
 
93
             
 
94
         return result
 
95
 
 
96
    _columns = { 
 
97
        'move_value_cost'    : fields.function(_compute_move_value_cost, method=True, string='Amount', digits_compute=dp.get_precision('Account'),type='float' ,  store=True, \
 
98
                            help="""Product's cost for accounting valuation.""") ,
 
99
        'move_value_sale'    : fields.function(_compute_move_value_sale, method=True, string='Amount Sale', digits_compute=dp.get_precision('Account'),type='float' , store=True, \
 
100
                             help="""Product's sale value for accounting valuation.""") ,
 
101
        'period_id'          : fields.function(_period_id, method=True, string="Period",type='many2one', relation='account.period', store=True, select="1",  ),
 
102
        'price_unit_sale'    : fields.float('Unit Price Sale',  digits_compute=dp.get_precision('Account') ),
 
103
        'analytic_account_id': fields.many2one('account.analytic.account', 'Analytic Account'),
 
104
 
 
105
    }
 
106
 
 
107
 
 
108
    def init(self, cr):
 
109
      # Purchase
 
110
      cr.execute("""
 
111
          update stock_move m set move_value_cost = (select m.product_qty * p.price_unit from purchase_order_line p
 
112
                                   where p.id = m.purchase_line_id) where move_value_cost is null;
 
113
      """)
 
114
      # Sales
 
115
      cr.execute("""
 
116
          update stock_move m set move_value_sale = (select m.product_qty * l.price_unit from sale_order_line l
 
117
                                   where l.id = m.sale_line_id) where move_value_sale is null;
 
118
      """)
 
119
      # other
 
120
      cr.execute("""
 
121
          update stock_move m set move_value_cost = (select m.product_qty * t.standard_price from product_product p, product_template t
 
122
                                   where p.id = m.product_id and t.id = p.product_tmpl_id) where move_value_cost is null;
 
123
      """)
 
124
 
 
125
 
 
126
stock_move()
 
127
 
 
128
 
 
129
 
 
130
#----------------------------------------------------------
 
131
#  Company Config INHERIT
 
132
#----------------------------------------------------------
 
133
 
 
134
#class res_company(osv.osv):
 
135
#    _inherit = "res.company"
 
136
#
 
137
#
 
138
#    _columns = {
 
139
#        'valuation':fields.selection([('manual_periodic', 'Periodical (manual)'),
 
140
#                                        ('real_time','Real Time (automated)'),], 'Inventory Valuation',
 
141
#                                        help="If real-time valuation is enabled for a product, the system will automatically write journal entries corresponding to stock moves." \
 
142
#                                             "The inventory variation account set on the product category will represent the current inventory value, and the stock input and stock output account will hold the counterpart moves for incoming and outgoing products."
 
143
#    }
 
144
 
 
145
#res_company()
 
146
 
 
147
## no connection from product to company !!!!
 
148
#class product_product(osv.osv):
 
149
#    _inherit = "product.product"
 
150
#
 
151
#    def _get_valuation(self, cr, uid, ids, field_name, arg, context=None):
 
152
#         result = {}
 
153
#         for res in self.browse(cr, uid, ids, context):
 
154
#             result[res.id] = 
 
155
#         return result
 
156
#
 
157
#
 
158
#    _columns = {
 
159
#        'valuation':  fields.function(_get_valuation, method=True, string="Valuation",type='char',size=128),
 
160
#
 
161
#    }
 
162
#
 
163
#product_product()