~camptocamp/c2c-rd-addons/8.0a

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
# -*- coding: utf-8 -*-

#!/usr/bin/python
# -*- coding: utf-8 -*-
##############################################
#
# ChriCar Beteiligungs- und Beratungs- GmbH
# Copyright (C) ChriCar Beteiligungs- und Beratungs- GmbH
# all rights reserved
# created 2009-08-18 23:44:30+02
#
# WARNING: This program as such is intended to be used by professional
# programmers who take the whole responsability of assessing all potential
# consequences resulting from its eventual inadequacies and bugs.
# End users who are looking for a ready-to-use solution with commercial
# garantees and support are strongly adviced to contract a Free Software
# Service Company.
#
# 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/> or
# write to the Free Software Foundation, Inc.,
# 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
###############################################
import time
from osv import fields,osv
import pooler
import sys

class chricar_budget_production(osv.osv):
     _name = "chricar.budget.production"

     _columns = {
          'name'         : fields.char    ('Production Budget', size=64, required=True),
          'fiscalyear_id': fields.many2one('account.fiscalyear', 'Fiscal Year',required=True),
          'budget_version_ids'   : fields.one2many('c2c_budget.version','budget_production_id','Budget Versions '),
      }

     def copy(self, cr, uid, id, default=None, context=None):
        prod = self.browse(cr, uid, id, context=context)
        default = default or {}

        default['is_current'] = False
        if not default.get('name', False):
            v = ' - v' + time.strftime("%Y-%m-%d")
            default['name'] = prod.name + v  
        res = super(chricar_budget_production, self).copy(cr, uid, id, default, context)
        # FIXME - update budget_versions
        #?? ids = self.search(cr, uid, [('parent_id','child_of', [res])])
        #cr.execute('update budget_version name = name ||' %s ' , is_current=False where budget_production_id in ('+','.join(map(str,ids))+')', [v])
        # udpate new current
        # set budget_version_next_id, = new next
        # set budget_version_pervious_id, = new previous
        return res

chricar_budget_production()

  
class chricar_budget(osv.osv):
     _name = "chricar.budget"

     def product_id_change(self, cr, uid, ids,  product, price_unit_id, uom_id):
         prod= self.pool.get('product.product').browse(cr, uid,product)

         pu = prod.price_unit_id.id
         if not price_unit_id:
            price_unit_id = pu

         prod_uom_po = prod.uom_po_id.id
         if not uom_id:
            uom_id = prod_uom_po
         res = {'value': {'price_unit_id':pu,
                          'product_uom': uom,
                         }
               }
         return res


     def _product_qty_line(self, cr, uid, ids, name, args, context=None):
        res = {}
        for line in self.browse(cr, uid, ids, context=context):
            res[line.id] = line.surface * line.yield_qty
        return res

     def _amount_line(self, cr, uid, ids, name, args, context=None):
        res = {}
        for line in self.browse(cr, uid, ids, context=context):
            res[line.id] = (line.surface * line.yield_qty) * line.price  / line.price_unit_id.coefficient
        return res

     def _yield_line(self, cr, uid, ids, name, args, context=None):
        res = {}
        for line in self.browse(cr, uid, ids, context=context):
            if line.surface and line.surface > 0.0:
                res[line.id] = ((line.surface * line.yield_qty) * line.price  / line.price_unit_id.coefficient) / line.surface
            else:
                res[line.id] = 0.0
        return res

     def name_get(self, cr, uid, ids, context=None):
          if not len(ids):
               return []
          reads = self.read(cr, uid, ids, ['product_id','budget_version_id'], context)
          res = []
          for record in reads:
               # FIXME read code from bud version
               #code = record.budget_version_id[0].code
               code = ''
               if not code:
                   code = record['budget_version_id'][1]
               name = record['product_id'][1]
               res.append((record['id'],code + ' ' + name))
          return res
              
     def _harvest(self, cr, uid, ids, name, args, context=None):
        res = {}
        harvest = 0.0
        for line in self.browse(cr, uid, ids, context=context):
            product       = line.product_id.id
            fy_date_start = line.budget_version_id.budget_id.start_date
            fy_date_stop  = line.budget_version_id.budget_id.end_date
            #print >>sys.stderr,'harvest ',product, fy_date_start, fy_date_stop
            cr.execute("""select coalesce(sum(product_qty),0) from stock_move s,
                                                       stock_location l
                                 where state='done'
                                   and l.usage = 'production'
                                   and l.id = s.location_id
                                   and product_id = %d
                                   and to_char(date_expected,'YYYY-MM-DD') between '%s' and '%s'""" % (product,fy_date_start,fy_date_stop))
            harvest = cr.fetchone()
            print >>sys.stderr,'harvest ',product, fy_date_start, fy_date_stop, harvest[0]
            harvest = harvest[0]
            res[line.id]  = harvest
        return res


     def _harvest_net(self, cr, uid, ids, name, args, context=None):
        res = {}
        print >>sys.stderr,'harvest yield net'
        for line in self.browse(cr, uid, ids, context=context):
            harvest_yield = 0.0
            harvest = 0.0
            surface_used  = line.surface
            if line.harvest_soil:
                harvest       = line.harvest * (100 - line.harvest_soil ) /100
            else:
                harvest       = line.harvest
            print >>sys.stderr,'harvest yield 2',surface_used,harvest
            if surface_used and surface_used <> 0.0  and  harvest and harvest <> 0.0:
                 harvest_yield = harvest / surface_used
            res[line.id]  = harvest_yield
        return res

     def _harvest_yield(self, cr, uid, ids, name, args  , context=None):
        res = {}
        print >>sys.stderr,'harvest yield 1'
        for line in self.browse(cr, uid, ids, context=context):
	    harvest_yield = 0.0
	    harvest = 0.0
            surface_used  = line.surface
            harvest       = line.harvest
            print >>sys.stderr,'harvest yield 2',surface_used,harvest
            if surface_used and surface_used <> 0.0  and  harvest and harvest <> 0.0:
                 harvest_yield = harvest / surface_used
            res[line.id]  = harvest_yield
        return res

     def _harvest_yield_diff(self, cr, uid, ids, name, args, context=None):
        res = {}
        print >>sys.stderr,'harvest yield 2'
        for line in self.browse(cr, uid, ids, context=context):
	    harvest_yield_diff = 0.0
            yield_qty  = line.yield_qty
            harvest_net = line.harvest_net
            print >>sys.stderr,'harvest yield 2',yield_qty,harvest_net
            if yield_qty <> 0.0  and harvest_net <> 0.0:
                 harvest_yield_diff =  ((harvest_net / yield_qty) - 1.0) * 100
            res[line.id]  = harvest_yield_diff
        return res


     _columns = {
       'amount'             : fields.function(_amount_line, method=True, string='Total Sales Planned' ,digits=(16,0),
                                    help="Planned Production Quantity * Price"),
       #'budget_version_id'  : fields.integer ('Budget version', required=True),
       'budget_version_id'  : fields.many2one('c2c_budget.version','Budget Version', required=True),
       'location_ids'       : fields.one2many('chricar.budget.surface','budget_id','Location Surface used'),
       'name'               : fields.text    ('Notes'),
       #'partner_id'         : fields.many2one('res.partner','Partner'),
       'price'              : fields.float   ('Price', digits=(16,2),help="Exected average price"),
       'price_unit_id'      : fields.many2one('c2c_product.price_unit','Price Unit', required=True),
       'product_id'         : fields.many2one('product.product','Product', select=True, required=True),
       'product_uom'        : fields.many2one('product.uom', 'Product UOM', required=True),
       'product_qty'        : fields.function(_product_qty_line, method=True, string='Planned Quantity' ,digits=(16,0),
                             help="Surface * Yield"),
       'surface'            : fields.float   ('Surface (ha)', digits=(16,2)),
       'yield_qty'          : fields.float   ('Yield qty/ha', digits=(16,0)),
       'yield_sale'         : fields.function(_yield_line, method=True, string='Sales/ha' ,digits=(16,0),
                              help="Planned Sales / Surface"),
       'date_cultivation'   : fields.date    ('Date Cultivation'),
       'sale_from'          : fields.date    ('Sale start', help="""Sale lines will be created aliquot for not individually planned sales""", required=True),
       'sale_to'            : fields.date    ('Sale end'),
       'harvest_period_id'  : fields.many2one('account.period','Harvest Period',help="In this period the harvest will be evaluated using product cost price", required=True),
       'harvest'            : fields.function(_harvest, method=True, string='Harvest' ,digits=(16,0), help="Harvested qty this year", readonly=True),
       'harvest_soil'       : fields.float   ('Soil %', digits=(16,0),help="The percentag of soil")       ,
       'harvest_net'        : fields.function(_harvest_net, method=True, string='Net/ha' ,digits=(16,0), help="Harvested qty net - without soil this year", readonly=True),
       'harvest_yield'      : fields.function(_harvest_yield, method=True, string='Yield/ha' ,digits=(16,0), help="Harvested yield qty this year", readonly=True),
       'harvest_yield_diff' : fields.function(_harvest_yield_diff, method=True, string='Yield Net Diff' ,digits=(16,2), help="Harvested yield Diff", readonly=True),
}
     _defaults = {
       'budget_version_id' : lambda *a: 1,
}
     _order = "name"

     _sql_constraints = [
        ('surface_0', 'CHECK (surface>0)',  'Surface must be greater than 0 !'),
    ]


     def copy(self, cr, uid, id, default=None, context=None):
        if default is None:
            default = {}
        default = default.copy()
        default.update({'location_ids': []})
        budget = self.browse(cr, uid, id, context=context)
        default['name'] = (budget['name'] or '') + '(copy)'
        return super(chricar_budget, self).copy(cr, uid, id, default, context)


chricar_budget()

class chricar_budget_surface(osv.osv):
     _name = "chricar.budget.surface"

     def _surface_location(self, cr, uid, ids, name, args, context=None):
        res = {}
        for line in self.browse(cr, uid, ids, context=context):
            res[line.id] = line.location_id.capacity
        return res

     def _harvest(self, cr, uid, ids, name, args, context=None):
        res = {}
        
        for line in self.browse(cr, uid, ids, context=context):
	    harvest = 0.0
            product       = line.budget_id.product_id.id
            location      = line.location_id.id
            fy_date_start = line.budget_id.budget_version_id.budget_id.start_date
            fy_date_stop  = line.budget_id.budget_version_id.budget_id.end_date
            print >>sys.stderr,'harvest detail ',product,location, fy_date_start, fy_date_stop
            cr.execute("""select coalesce(sum(product_qty),0)  from stock_move s,
                                                       stock_location l
                                 where state='done'
                                   and l.usage = 'production'
                                   and l.id = s.location_id
                                   and s.product_id = %d
                                   and s.location_id = %d
                                   and to_char(date_planned,'YYYY-MM-DD') between '%s' and '%s'""" % (product,location,fy_date_start,fy_date_stop))
            harvest = cr.fetchone()
            harvest = harvest[0]
            print >>sys.stderr,'harvest detail 2',product,location, fy_date_start, fy_date_stop,harvest
            res[line.id]  = harvest
        return res

     def _harvest_yield(self, cr, uid, ids, name, args, context=None):
        res = {}
        
        for line in self.browse(cr, uid, ids, context=context):
	    harvest_yield_detail = 0.0  
            surface_used_detail  = line.name
            harvest_detail       = line.harvest
            print >>sys.stderr,'harvest yield detail',name,surface_used_detail,harvest_detail
            if surface_used_detail and surface_used_detail <> 0.0 and harvest_detail and harvest_detail <> 0.0:
                 harvest_yield_detail = harvest_detail / surface_used_detail
            res[line.id]  = harvest_yield_detail
        return res


     _columns = {
         'budget_id'          : fields.many2one('chricar.budget','Product', required=True),
         'location_id'        : fields.many2one('stock.location','Location', required=True),
         'name'               : fields.float   ('ha used', digits=(16,2), required=True, help="Surface of field used for this product"),
         'surface_location'   : fields.function(_surface_location, method=True, string='ha total' ,digits=(16,2), help="Total surface of field", readonly=True),
         'product_id'         : fields.many2one('product.product','Seed', select=True ),
         'harvest'            : fields.function(_harvest, method=True, string='Harvest' ,digits=(16,0), help="Harvested qty this year on this location", readonly=True),
         'harvest_yield'      : fields.function(_harvest_yield, method=True, string='Yield' ,digits=(16,0), help="Harvested yield qty this year on this location", readonly=True),
         'prodlot_id'         : fields.many2one('stock.production.lot', 'Production Lot', readonly=True),
}

     def on_change_location(self, cr, uid, ids,location_id,surface,surface_location):
        result ={}
        loc = self.pool.get('stock.location').browse(cr, uid, [int(location_id)])[0]
        surface_location = loc.capacity
        result['surface_location'] = surface_location
        if not surface or surface > surface_location :
            result['name'] = surface_location

        return {'value':result}

chricar_budget_surface()

class product_product(osv.osv):
      _inherit = "product.product"
      _columns = {
          'budget_ids': fields.one2many('chricar.budget','product_id','Budget Products'),
      }

      def copy(self, cr, uid, id, default=None, context=None):
        if default is None:
            default = {}
        default = default.copy()
        default.update({'budget_ids': []})
        return super(product_product, self).copy(cr, uid, id, default, context)


product_product()

#class res_partner(osv.osv):
#      _inherit = "res.partner"
#      _columns = {
#          'budget_ids': fields.one2many('chricar.budget','partner_id','Budget Products'),
#      }
#
#res_partner()

class c2c_budget_version(osv.osv):
      _inherit = "c2c_budget.version"
      _columns = {
          'budget_ids': fields.one2many('chricar.budget','budget_version_id','Budget Products'),
          'budget_production_id': fields.many2one('chricar.budget.production','Budget Produktion'),
      }
c2c_budget_version()