Pular para o conteúdo
Menu
Esta pergunta foi sinalizada
1 Responder
1439 Visualizações

Dear All,

I'm creating a dynamic tree view using SQL query in which information is pulled from different models. The pulled information is jumbled and there is no linking of the information as the model is varied. For example, if we take SO & SOL, the SO number does not show the same SOL related to that SO number.

py is:


class DmrrReport(models.Model):
_name = "dmrr.report"
_description = "DMRR Report"
_auto = False

# Purchase Order Lines
product_id = fields.Many2one('product.product', string="Product")
description = fields.Text(string="Product Description")
# hsn_code = fields.Char(string="HSN/SAC Code")
product_qty = fields.Float(string="Quantity")
price_unit = fields.Float(string="Unit Price")
price_subtotal = fields.Char(string="Product Value")
# taxes_id = fields.Many2many('account.tax', string="Taxes")
product_uom = fields.Many2one('uom.uom', string="Unit")
price_total = fields.Char(string="Total Purchase Cost")

# Purchase Order
partner_id = fields.Many2one('res.partner', string="Vendor")
job_num = fields.Char(string="Job No.")
po_wo_date = fields.Date(string="PO/WO Date")

#Account Move / Bill Creation
bill_number = fields.Char(string="Bill No.")
invoice_date = fields.Date(string="Bill Date")

# # Stock Picking
stock_challan = fields.Char(string="Challan No.")
challan_date = fields.Date(string="Challan Date")
stock_grn = fields.Char(string="GRN No.")
effective_date = fields.Date(String="Date")
vehicle_no = fields.Char(String="Vehicle No.")
entry_gate_no = fields.Char(String="Entry Gate No.")
origin = fields.Char(String="PO/WO No.")

def init(self):
tools.drop_view_if_exists(self._cr, 'dmrr_report')
self._cr.execute("""
CREATE or REPLACE VIEW dmrr_report AS(
SELECT row_number() OVER () AS id, line.product_id, line.description, line.product_qty,
line.price_unit, line.partner_id, line.job_num, line.stock_challan, line.stock_grn,
line.vehicle_no, line.effective_date, line.entry_gate_no, line.product_uom, line.price_subtotal,
line.price_total, line.bill_number, line.invoice_date, line.origin, line.po_wo_date,
line.challan_date FROM (
SELECT pol.product_id as product_id,
pol.name as description,
pol.product_qty as product_qty,
pol.price_unit as price_unit,
pol.price_subtotal as price_subtotal,
pol.price_total as price_total,
pol.product_uom as product_uom,
po.partner_id as partner_id,
po.job_num as job_num,
po.effective_date as effective_date,
po.date_po as po_wo_date,
st.challan as stock_challan,
st.challan_date as challan_date,
st.grn as stock_grn,
st.vehicle_no as vehicle_no,
st.origin as origin,
st.entry_gate_no as entry_gate_no,
am.ref as bill_number,
am.invoice_date as invoice_date
FROM purchase_order_line pol
JOIN purchase_order po ON (po.id = pol.order_id)
JOIN account_move am ON (am.id = pol.order_id)
JOIN stock_picking st ON (st.id = pol.order_id)
) line
)""")

Thank you in advance

Avatar
Cancelar
Melhor resposta

Your code is a solid start, but if the SO (Sales Order) numbers and SOL (Sales Order Lines) aren't matching up as expected, it's likely because the linking logic in your SQL needs a tweak. Let's straighten this out!

First, the heart of your query is trying to stitch together data from different models: purchase_order_line (POL), purchase_order (PO), account_move (AM), and stock_picking (ST). But, your JOIN conditions seem a bit off, especially linking account_move and stock_picking directly to purchase_order_line using pol.order_id. This linkage assumes account_move.id and stock_picking.id match purchase_order_line.order_id, which is unlikely unless there's a very unique setup in your database.

Here's a more aligned approach, focusing on the purchase side. Since the original question mixes SO (Sales Order) and PO (Purchase Order) terminology, I'll stick to the PO context given your code. If you need to incorporate SO information, the logic would be similar, just adapted to sales models (sale.order, sale.order.line, etc.).

  1. Clarify the Relationships: Ensure that the relationships between purchase_order, purchase_order_line, account_move, and stock_picking are correctly defined. Usually:
    • A purchase_order can be linked to account_move records (bills) through a field like invoice_ids or a similar mechanism, not directly through id matching.
    • stock_picking records are linked to purchase_order records either directly via a field on stock_picking (like purchase_id) or through operational types and source documents.
  2. Adjust the SQL Query: Use proper JOIN conditions that reflect these relationships. You might need to:
    • Join account_move to purchase_order through a field that represents the connection (often through a many2many relationship, which may require an intermediate join).
    • Similarly, adjust the join condition between stock_picking and purchase_order to use the correct relational field.

Odoo Beratung Deutschland

Avatar
Cancelar
Publicações relacionadas Respostas Visualizações Atividade
0
abr. 24
556
1
set. 21
2316
0
out. 21
1691
1
set. 24
1704
1
abr. 24
1191