Skip to content

Commit

Permalink
[REF] stock_card_report: use stock.move.line instead of stock move an…
Browse files Browse the repository at this point in the history
…d add lot to the report
  • Loading branch information
alan196 committed Feb 13, 2024
1 parent 209e7cc commit ea903ab
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 12 deletions.
24 changes: 16 additions & 8 deletions stock_card_report/reports/stock_card_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,17 @@ class StockCardView(models.TransientModel):

date = fields.Datetime()
product_id = fields.Many2one(comodel_name="product.product")
product_qty = fields.Float()
qty_done = fields.Float()
product_uom_qty = fields.Float()
product_uom = fields.Many2one(comodel_name="uom.uom")
product_uom_id = fields.Many2one(comodel_name="uom.uom")
reference = fields.Char()
location_id = fields.Many2one(comodel_name="stock.location")
location_dest_id = fields.Many2one(comodel_name="stock.location")
is_initial = fields.Boolean()
product_in = fields.Float()
product_out = fields.Float()
picking_id = fields.Many2one(comodel_name="stock.picking")
lot_id = fields.Many2one(comodel_name="stock.production.lot")

def name_get(self):
result = []
Expand All @@ -41,6 +42,7 @@ class StockCardReport(models.TransientModel):
date_to = fields.Date()
product_ids = fields.Many2many(comodel_name="product.product")
location_id = fields.Many2one(comodel_name="stock.location")
lot_ids = fields.Many2many(comodel_name="stock.production.lot")

# Data fields, used to browse report data
results = fields.Many2many(
Expand All @@ -56,21 +58,26 @@ def _compute_results(self):
locations = self.env["stock.location"].search(
[("id", "child_of", [self.location_id.id])]
)
lots = self.lot_ids or self.env["stock.production.lot"].search([
("product_id", "in", self.product_ids.ids),
])
self._cr.execute(
"""
SELECT move.date, move.product_id, move.product_qty,
move.product_uom_qty, move.product_uom, move.reference,
SELECT move.date, move.product_id, move.qty_done,
move.product_uom_qty, move.product_uom_id, move.reference,
move.location_id, move.location_dest_id,
case when move.location_dest_id in %s
then move.product_qty end as product_in,
then move.qty_done end as product_in,
case when move.location_id in %s
then move.product_qty end as product_out,
then move.qty_done end as product_out,
case when move.date < %s then True else False end as is_initial,
move.picking_id
FROM stock_move move
move.picking_id,
move.lot_id
FROM stock_move_line move
WHERE (move.location_id in %s or move.location_dest_id in %s)
and move.state = 'done' and move.product_id in %s
and CAST(move.date AS date) <= %s
and move.lot_id in %s
ORDER BY move.date, move.reference
""",
(
Expand All @@ -81,6 +88,7 @@ def _compute_results(self):
tuple(locations.ids),
tuple(self.product_ids.ids),
self.date_to,
tuple(lots.ids),
),
)
stock_card_results = self._cr.dictfetchall()
Expand Down
9 changes: 9 additions & 0 deletions stock_card_report/reports/stock_card_report.xml
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
</div>
<div class="act_as_cell" />
<div class="act_as_cell" />
<div class="act_as_cell" />
<div class="act_as_cell right">
<t t-out="'{0:,.3f}'.format(initial)" />
</div>
Expand Down Expand Up @@ -103,6 +104,7 @@
<div class="act_as_cell">Date From</div>
<div class="act_as_cell">Date To</div>
<div class="act_as_cell">Location</div>
<div class="act_as_cell">Lots</div>
</div>
<div class="act_as_row">
<div class="act_as_cell">
Expand All @@ -114,6 +116,9 @@
<div class="act_as_cell">
<span t-field="o.location_id" />
</div>
<div class="act_as_cell">
<span t-field="o.lot_ids" />
</div>
</div>
</div>
</template>
Expand All @@ -123,6 +128,7 @@
<div class="act_as_row labels">
<div class="act_as_cell">Date</div>
<div class="act_as_cell">Reference</div>
<div class="act_as_cell">Lot</div>
<div class="act_as_cell">In</div>
<div class="act_as_cell">Out</div>
<div class="act_as_cell">Balance</div>
Expand All @@ -138,6 +144,9 @@
<div class="act_as_cell left">
<t t-out="product_line.display_name" />
</div>
<div class="act_as_cell left">
<t t-out="product_line.lot_id.name" />
</div>
<div class="act_as_cell right">
<t t-out="'{0:,.3f}'.format(product_line.product_in)" />
</div>
Expand Down
25 changes: 21 additions & 4 deletions stock_card_report/reports/stock_card_report_xlsx.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,18 @@ def _get_ws_params(self, wb, data, product):
"format": FORMATS["format_tcell_center"],
},
},
"4_lot": {
"header": {"value": "Lots"},
"data": {
"value": self._render("lot"),
"format": FORMATS["format_tcell_center"],
},
},
}
initial_template = {
"1_ref": {
"data": {"value": "Initial", "format": FORMATS["format_tcell_center"]},
"colspan": 4,
"colspan": 5,
},
"2_balance": {
"data": {
Expand All @@ -81,17 +88,25 @@ def _get_ws_params(self, wb, data, product):
},
"width": 25,
},
"3_input": {
"3_lot": {
"header": {"value": "Lot"},
"data": {
"value": self._render("lot"),
"format": FORMATS["format_tcell_left"],
},
"width": 25,
},
"4_input": {
"header": {"value": "In"},
"data": {"value": self._render("input")},
"width": 25,
},
"4_output": {
"5_output": {
"header": {"value": "Out"},
"data": {"value": self._render("output")},
"width": 25,
},
"5_balance": {
"6_balance": {
"header": {"value": "Balance"},
"data": {"value": self._render("balance")},
"width": 25,
Expand Down Expand Up @@ -139,6 +154,7 @@ def _stock_card_report(self, wb, ws, ws_params, data, objects, product):
"date_from": objects.date_from or "",
"date_to": objects.date_to or "",
"location": objects.location_id.display_name or "",
"lot": ", ".join(objects.lot_ids.mapped("name")) or "",
},
col_specs="col_specs_filter",
wanted_list="wanted_list_filter",
Expand Down Expand Up @@ -181,6 +197,7 @@ def _stock_card_report(self, wb, ws, ws_params, data, objects, product):
"input": line.product_in or 0,
"output": line.product_out or 0,
"balance": balance,
"lot": line.lot_id.name or "",
},
default_format=FORMATS["format_tcell_amount_right"],
)
4 changes: 4 additions & 0 deletions stock_card_report/wizard/stock_card_report_wizard.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ class StockCardReportWizard(models.TransientModel):
product_ids = fields.Many2many(
comodel_name="product.product", string="Products", required=True
)
lot_ids = fields.Many2many(
comodel_name="stock.production.lot", string="Lots"
)

@api.onchange("date_range_id")
def _onchange_date_range_id(self):
Expand Down Expand Up @@ -55,6 +58,7 @@ def _prepare_stock_card_report(self):
"date_to": self.date_to or fields.Date.context_today(self),
"product_ids": [(6, 0, self.product_ids.ids)],
"location_id": self.location_id.id,
"lot_ids": [(6, 0, self.lot_ids.ids)],
}

def _export(self, report_type):
Expand Down
1 change: 1 addition & 0 deletions stock_card_report/wizard/stock_card_report_wizard_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<group>
<field name="product_ids" widget="many2many_tags" />
<field name="location_id" />
<field name="lot_ids" widget="many2many_tags" domain="[('product_id', 'in', product_ids)]" />
</group>
</group>
<footer>
Expand Down

0 comments on commit ea903ab

Please sign in to comment.