| 1 | from odoo import fields, models, tools |
| 2 | |
| 3 | |
| 4 | class StockAverageCostReport(models.AbstractModel): |
| 5 | _auto = False |
| 6 | _name = 'stock.avco.report' |
| 7 | _description = 'Stock AVCO Justifier' |
| 8 | _order = 'date desc, id desc' |
| 9 | |
| 10 | date = fields.Date(string='Date', required=True) |
| 11 | user_id = fields.Many2one('res.users', string='User', required=True) |
| 12 | company_id = fields.Many2one('res.company', string='Company', required=True) |
| 13 | currency_id = fields.Many2one('res.currency', related='company_id.currency_id', string='Currency') |
| 14 | |
| 15 | product_id = fields.Many2one('product.product', string='Product', required=True) |
| 16 | |
| 17 | reference = fields.Char(string='Reference', required=True) |
| 18 | description = fields.Text(string='Description', required=True) |
| 19 | |
| 20 | res_model_name = fields.Selection([ |
| 21 | ('stock.move', 'Stock Move'), |
| 22 | ('product.value', 'Product Value'), |
| 23 | ], string='Resource Model Name', required=True) |
| 24 | |
| 25 | quantity = fields.Float(string='Added Quantity', required=True) |
| 26 | value = fields.Float(string='Value', required=True) |
| 27 | |
| 28 | added_value = fields.Float(string='Added Value', compute='_compute_cumulative_fields') |
| 29 | total_quantity = fields.Float(string='Total Quantity', compute='_compute_cumulative_fields') |
| 30 | total_value = fields.Float(string='Total Value', compute='_compute_cumulative_fields') |
| 31 | avco_value = fields.Float(string='AVCO Value', compute='_compute_cumulative_fields') |
| 32 | |
| 33 | justification = fields.Text(string='Justification', compute='_compute_justification') |
| 34 | |
| 35 | def init(self): |
| 36 | tools.drop_view_if_exists(self.env.cr, 'stock_avco_report') |
| 37 | query = """ |
| 38 | CREATE OR REPLACE VIEW stock_avco_report AS ( |
| 39 | SELECT |
| 40 | sm.id AS id, |
| 41 | sm.product_id, |
| 42 | sm.date, |
| 43 | picking.user_id, |
| 44 | sm.company_id, |
| 45 | sm.reference, |
| 46 | CASE WHEN sm.is_in THEN sm.value ELSE -sm.value END AS value, |
| 47 | CASE WHEN sm.is_in THEN sm.quantity ELSE -sm.quantity END AS quantity, |
| 48 | 'stock.move' AS res_model_name, |
| 49 | 'Operation' AS description |
| 50 | FROM |
| 51 | stock_move sm |
| 52 | LEFT JOIN |
| 53 | stock_picking picking ON sm.picking_id = picking.id |
| 54 | LEFT JOIN |
| 55 | product_product pp ON sm.product_id = pp.id |
| 56 | LEFT JOIN |
| 57 | product_template pt ON pp.product_tmpl_id = pt.id |
| 58 | LEFT JOIN |
| 59 | product_category pc ON pt.categ_id = pc.id |
| 60 | LEFT JOIN |
| 61 | res_company company ON sm.company_id = company.id |
| 62 | WHERE |
| 63 | sm.state = 'done' |
| 64 | AND (sm.is_in = TRUE OR sm.is_out = TRUE) |
| 65 | -- Ignore moves for standard cost method. Only display the list of cost updates |
| 66 | AND ( |
| 67 | (pt.categ_id IS NOT NULL AND pc.property_cost_method ->> company.id::text IN ('fifo', 'average')) |
| 68 | OR (pt.categ_id IS NULL OR (pc.property_cost_method IS NULL OR pc.property_cost_method ->> company.id::text IS NULL) AND company.cost_method IN ('fifo', 'average')) |
| 69 | ) |
| 70 | UNION ALL |
| 71 | SELECT |
| 72 | -pv.id, |
| 73 | pv.product_id, |
| 74 | pv.date, |
| 75 | pv.user_id, |
| 76 | pv.company_id, |
| 77 | 'Adjustment' AS reference, -- Set a fixed string for the reference |
| 78 | pv.value, |
| 79 | 0 AS quantity, -- Set quantity to 0 as requested, |
| 80 | 'product.value' AS res_model_name, |
| 81 | pv.description |
| 82 | FROM |
| 83 | product_value pv |
| 84 | WHERE |
| 85 | pv.move_id IS NULL |
| 86 | ); |
| 87 | """ |
| 88 | self.env.cr.execute(query) |
| 89 | |
| 90 | def _compute_cumulative_fields(self): |
| 91 | total_records_grouped = self.env['stock.avco.report'].search( |
| 92 | [('product_id', 'in', self.product_id.mapped('id')), ('company_id', 'in', self.company_id.mapped('id'))] |
| 93 | ).grouped(lambda m: (m.product_id, m.company_id)) |
| 94 | for records in self.grouped(lambda m: (m.product_id, m.company_id)).values(): |
| 95 | current_page_records = records.sorted('date, id') |
| 96 | total_records = total_records_grouped.get((records.product_id, records.company_id)).sorted('date, id') |
| 97 | added_value = 0.0 |
| 98 | total_value = 0.0 |
| 99 | total_quantity = 0.0 |
| 100 | avco = 0.0 |
| 101 | for record in total_records: |
| 102 | if record.res_model_name == 'stock.move': |
| 103 | if record.quantity > 0: |
| 104 | added_value = record.value |
| 105 | elif record.quantity < 0: |
| 106 | added_value = avco * record.quantity |
| 107 | total_value += added_value |
| 108 | total_quantity += record.quantity |
| 109 | |
| 110 | elif record.res_model_name == 'product.value': |
| 111 | added_value = (record.value * total_quantity) - total_value |
| 112 | total_value = record.value * total_quantity |
| 113 | |
| 114 | if total_quantity: |
| 115 | avco = total_value / total_quantity |
| 116 | if record in current_page_records: |
| 117 | record.added_value = added_value |
| 118 | record.total_value = total_value |
| 119 | record.total_quantity = total_quantity |
| 120 | record.avco_value = avco |
| 121 | |
| 122 | def _compute_justification(self): |
| 123 | self.justification = False |
| 124 | for record in self: |
| 125 | if record.res_model_name == 'stock.move': |
| 126 | record.justification = self.env['stock.move'].browse(record.id).value_justification |