每日重复发票检查

发布于 2024-11-19 20:51:57 字数 1329 浏览 1 评论 0原文

我的情况如下:

我正在建立一个发票系统,我几乎已经完成了所有事情,除了发票重复。这是我正在尝试做的事情,但没有成功。

条件:

我生成新的循环发票的条件是 ->

  1. 合同必须是有效的。
  2. 参考必须等于 PA。
  3. 仅向还没有发票的合同生成发票。 (如何检查)

我将使用 cron 运行脚本,它将每天运行,然后如果合同没有发票,我将每天生成发票。 如果合同有该日期的发票,则不执行任何操作 - 退出。

这里我有一段 SQL 代码,我正在检索迄今为止拥有发票的每个人(现在是月份和年份)。

一些有用的信息:

我有 324 个有效合同,其中已在以下查询中生成了发票。

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

我在另一个查询中有 368 个活跃合约

SELECT * FROM contracts WHERE active=1

然后我知道我有 44 份没有发票的合同并且我知道我需要为这 44 份合同生成新发票。

我的问题是如何检索这 44 个合同并在我的第一个查询中显示它,它可以为空(没问题),对我来说重要的是合同 ID。

如果我不够清楚,请告诉我。

感谢社区。

[已编辑] 摘要

  • 我运行了第一个查询并得到了 324 行结果。
  • 然后运行第二个查询并比较 query1 和 query2 的结果,
  • 不匹配的行是差异,我需要生成 给他们开具发票。

我真的很想在我的查询(一个查询)中而不是在我的代码中执行此操作。

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 then 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

七七 2024-11-26 20:51:58

LEFT JOIN 返回填充有所有 NULL 值的发票记录。当您要求 Invoices.referencia = 'PA' 时,这将消除所有完全为空的记录(否定您的外部联接)。

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文