I am using the following query to get a list of items to bill, put into a multiselect listbox where each item can be selected and billed.
I want to add in a column that tells me the value of each item for billing. The way we are set up, an item (quotation) is made up of a bunch of individual drawings. The MySQL that shows me this follows
The logic is this: If the job is a fixed price, print that. If not, then, if it is a hours worked type rate, multiply the total hours worked by the selected hourly rate. If the nincompoops didn't enter the rate, use our normal hourly rate.
If the rate is free, (rate ID 1) then print zero. Anything else is going to be billed by how much time it should have taken. Sum up the hours assigned. Again, if the nincompoops didn't enter the hourly rate, use our normal rate.
Code:
SELECT
q.quote_id AS [SE Ref]
, q.date_rtn_final AS [Date Returned]
, q.add1 AS [Address Line 1]
, IIf(IsNull(c2.company),q.distributor,c2.company) AS Builder [COLOR=green] 'Use name from company table, otherwise use name input into quote table. - latter is for backwards-compatability[/color]
, r.Rate_Name AS [Rate Name]
FROM (((quotations AS q
INNER JOIN rates AS r ON q.Rate_ID = r.Rate_ID)
LEFT JOIN companies AS c2 ON q.build_id = c2.Comp_ID)
LEFT JOIN companies AS c1 ON q.dist_id = c1.Comp_ID)
LEFT JOIN billing AS b ON q.quote_id = b.Quote_ID
WHERE (((q.date_rtn_final) Is Not Null)
AND ((IIf(IsNull([c1].[company]),[q].[distributor],[c1].[company]))=[Forms]![Billing].[dist]) [COLOR=green]'Used to restrict to company names that can get the invoices, )don't want to bill cust A for something that cust B has to pay for) and backwards compatability is needed. May be obsoleted by c1 join[/color]
AND ((q.Billed)=0)
AND ((q.excluded)=0)
AND ((IIf([q].[rate_id]=1,0,IIf([q].[NoWin]=1,IIf([q].[JobWon]=1,1,0),1)))=1) [COLOR=green] 'if it is a no-win no-fee, only show the ones we have won.[/color]
AND ((b.Bill_ID) Is Null)
AND ((q.Rate_ID)<>1
And (q.Rate_ID)<>11
And (q.Rate_ID)<>12))
ORDER BY q.quote_id DESC;
I want to add in a column that tells me the value of each item for billing. The way we are set up, an item (quotation) is made up of a bunch of individual drawings. The MySQL that shows me this follows
Code:
select
q.quote_id
,q.date_rtn_final
,q.add1
,r.rate_name
,if(q.rate_id = 2,q.fix_price,
if(r.hourly=1,(sum(w.hoursworked) * if(q.hrate='0.00',31.25,q.hrate)),
if(q.rate_id = 1,0,
(sum(h.hours) * if(q.hrate='0.00',31.25,q.hrate))
)
)
)
from quotations as q
left join housetype as h on h.quote_id = q.quote_id and h.deleted = 0
left join hoursworked as w on h.ht_id = w.ht_id
left join rates as r on q.rate_id = r.rate_id
group by q.quote_id
The logic is this: If the job is a fixed price, print that. If not, then, if it is a hours worked type rate, multiply the total hours worked by the selected hourly rate. If the nincompoops didn't enter the rate, use our normal hourly rate.
If the rate is free, (rate ID 1) then print zero. Anything else is going to be billed by how much time it should have taken. Sum up the hours assigned. Again, if the nincompoops didn't enter the hourly rate, use our normal rate.