QC: Querying for invoice detail with consignment breakdown

This post is for the Quantum Control system (inventory software made by Component Control).

Here is my query for getting sales for a particular consignment (so if you want more you will want to drop the two stock*.cnc_auto_key = 970). This works on Firebird (I haven’t tested it on Oracle). I use the “union all” since the credits need to be pulled separately.
970 is the cnc_auto_key of the consignment I am going for. This tends to run a whole lot faster for me than the Invoice Detail Report in Quantum.

I use this in PHP so for the date range you will want to replace the $begin_date and $end_date variables with appropriate dates.

 

 SELECT

 stockouter.pn,
 stockouter.description,
 invc_detail.unit_price,
 invc_detail.unit_cost,
 STOCK_RESERVATIONS.qty_invoiced qty_invoiced,
 (invc_detail.unit_price * STOCK_RESERVATIONS.qty_invoiced) total_price,
 companies.company_name,
 companies.country,
 invc_header.invoice_date
 FROM

 INVC_HEADER INVC_HEADER
 inner join companies on
 (companies.cmp_auto_key = invc_header.cmp_auto_key)

 INNER JOIN INVC_DETAIL INVC_DETAIL ON
     (INVC_DETAIL.INH_AUTO_KEY = INVC_HEADER.INH_AUTO_KEY)

 INNER JOIN SO_DETAIL SO_DETAIL ON
 (SO_DETAIL.SOD_AUTO_KEY = INVC_DETAIL.SOD_AUTO_KEY)

 INNER JOIN STOCK_RESERVATIONS STOCK_RESERVATIONS ON
 (STOCK_RESERVATIONS.SOD_AUTO_KEY = SO_DETAIL.SOD_AUTO_KEY
 and stock_reservations.ind_auto_key = invc_detail.ind_auto_key
 )

 INNER JOIN STOCK stockouter ON
 (stockouter.STM_AUTO_KEY = STOCK_RESERVATIONS.STM_AUTO_KEY)

 WHERE
 invc_header.invoice_date >= '$begin_date'
 and invc_header.invoice_date <= '$end_date'
 and stockouter.cnc_auto_key = 970
 and not invc_detail.route_code = 'M'

 union all

 SELECT
 stockouterc.pn,
 stockouterc.description,
 invc_detailc.unit_price ,
 invc_detailc.unit_cost,
 invc_detailc.qty_ship qty_invoiced,
 invc_detailc.unit_price * invc_detail.qty_ship total_price,
 companiesc.company_name,
 companiesc.country,
 invc_headerc.invoice_date
 FROM
 INVC_HEADER INVC_HEADERc
 inner join companies companiesc on
 (companiesc.cmp_auto_key = invc_headerc.cmp_auto_key)

 INNER JOIN INVC_DETAIL INVC_DETAILc ON
     (INVC_DETAILc.INH_AUTO_KEY = INVC_HEADERc.INH_AUTO_KEY)
 INNER JOIN STOCK stockouterc ON
 (stockouterc.ind_AUTO_KEY = invc_detailc.ind_auto_key)
 WHERE
 invc_headerc.invoice_date >= '$begin_date'
 and invc_headerc.invoice_date <= '$end_date'
 and stockouterc.cnc_auto_key = 970
 and invc_detailc.route_code = 'M'

 ;
I assume you’ll want an inner join to the consignment table on both queries here to pull in the consignment codes (instead of doing it by cnc_auto_key.
If anyone needs that let me know and I’ll update it.
To break it down: the first half of the query basically addresses the fact that for normal invoices you need to connect through stock reservations. And the second half addresses the fact that for credit memos they do not go through stock reservations but instead ind_auto_key on the stocklines.

Leave a Reply

Your email address will not be published. Required fields are marked *

To see if you are a human *