开发者

Daily Recurrence Invoices Check

My situation as follow:

I'm building up an invoice system, I have almost everything done, except the invoices recurrence. Here is what I'm trying to do without success.

The condition:

My condition to generate a new recurrence incoice is ->

  1. contract must be active.
  2. reference must be equal to PA.
  3. generate invoices only to contract that do not have invoices yet. (How can check it)

I will run the script with cron, and it will run everyday, then I'm going to generate invoices daily if the contract do not have one. If contracts have invoice for this date 开发者_如何学Pythonthen do nothing - exit.

Here I have a piece of code in SQL, that I'm retrieving everybody that has invoices to this date (NOW for month and year).

Some usefull information:

I have 324 active contracts with invoices already generated in the follow query.

SELECT contratos.id, contratos.idcliente, contratos.ativo, invoices.id as inv, invoices.due_date, invoices.contratoid 
FROM contratos 
LEFT JOIN invoices ON invoices.contratoid = contratos.id 
WHERE contratos.ativo = 1 
AND invoices.referencia = 'PA' 
AND YEAR(due_date) = YEAR(CURDATE()) 
AND MONTH(due_date) = MONTH(CURDATE()) 
GROUP BY invoices.id

I have 368 actives contracts in this other query.

SELECT * FROM contracts WHERE active=1

Then I know I have 44 contracts without invoices and I know I need to generate new invocies for the 44 contracts.

My question is how can I retrieve this 44 contracts and show it in my first query, It can be a null (no problem), the important to me id the Contract ID.

If I'm not clear enough please let me know.

Thanks Community.

[EDITED] Summary

  • I run my first query and got results of 324 lines.
  • Then run the second query and compare the result of query1 and query2
  • the lines that does'nt match is the difference and I need to generate invoices to them.

I really would like to do this in my query (one query) and not in my code.


The LEFT JOIN is returning the invoices records being filled with all NULL values. When you are requiring invoices.referencia to = 'PA', that will eliminate all the records that are completely null (negating your outer join).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜