Hi everyone,
Can anybody guide me to add FROM Date and TO Date in ERPNext Query Report.
Hi everyone,
Can anybody guide me to add FROM Date and TO Date in ERPNext Query Report.
The Report Builder can select Date Between . Would that work?
Add this to report_name.js file
frappe.query_reports[āReport Nameā] = {
āfiltersā: [
{
āfieldnameā: āfrom_dateā,
ālabelā: __(āIn Date Fromā),
āfieldtypeā: āDateā,
ādefaultā: frappe.datetime.get_today(),
āreqdā: 1
},
{
āfieldnameā: āto_dateā,
ālabelā: __(āToā),
āfieldtypeā: āDateā,
ādefaultā: frappe.datetime.get_today(),
āreqdā: 1
}
]
}
SanRam added the client-side filters here, but donāt forget to process this information also server-side in the .py file or else it wonāt do anything.
You can for example set an emtpy directory as the variable āfiltersā
filters = {}
Then you can fill this empty dictionary with the from and to dates like so:
from_date = filters.get('from_date')
to_date = filters.get('to_date')
Now you can use these variables in your report.
report_name.js:
frappe.query_reports[āReport Nameā] = {
āfiltersā: [
{
āfieldnameā: āfrom_dateā,
ālabelā: __(āIn Date Fromā),
āfieldtypeā: āDateā,
ādefaultā: frappe.datetime.get_today(),
āreqdā: 1
},
{
āfieldnameā: āto_dateā,
ālabelā: __(āToā),
āfieldtypeā: āDateā,
ādefaultā: frappe.datetime.get_today(),
āreqdā: 1
}
]
}
then query report:
SELECT ātabTable Nameā.āfield_nameā
FROM ātabTable Nameā
WHERE (DATE(ātabTable Nameā.ādate_fieldā) between %(from_date)s and %(to_date)s)
Hi
How to fetch the transaction date is Today for query report
@ValS -
Thank you for highlighting the server side config. Iāve been trying the following but with no success:
`from future import unicode_literals
import frappe
from frappe import _
from frappe.utils import flt, cint, getdate
from frappe.utils import nowdate
def execute(filters=None):
columns, data = [], []
return columns, data
from_date = filters.get(āfrom_dateā)
to_date = filters.get(āto_dateā)`
Iād appreciate if you could advise where Iām getting it wrong.
Thanks in advance
Is this your full code? Because it seems that you didnāt declare filters as variable first and well, that means you canāt use it in any way.
Thanks for the prompt response.
I greatly appreciate it.
Yes- that is my full .py code.
Thanks for pointing out where I went wrong.
Let me try to get it right and if/when I do- Iāll post the working code here.
There is something wrong with that code.
On line 3, you return from the execute function()
return columns, data
This means that lines 4 and 5 will never be executed.
@crafter @ValS - Thank you for the pointers .
Iāve tried the following but canāt seem to find joy. Could you advise where Iām getting it wrong still:
from future import unicode_literals
import frappe
from frappe import msgprint, _
from frappe.utils import getdate, cstr, flt, fmt_money
def execute(filters=None):
if not filters:
return [], []
if not filters.get("to_date"):
frappe.throw(_("'To Date' is required"))
if not filters.get("from_date"):
frappe.throw(_("'From Date' is required"))
Have a look at my code here for a custom script report :
The file āclient_statement.jsā contains the filter parameters, which includes a start and end date.
The file āclient_statement.pyā contains the handling of the dates.
The dates are used within local functions, but you can see they are referred to by the notation as in the following example code
from_date, to_date = getdate(filters.from_date), getdate(filters.to_date)
@crafter - May you grant me some grace today but I have tried the below and still no success .
May you advise what Iām mssing?
from future import unicode_literals
import frappe
from frappe import msgprint, _
from frappe.utils import getdate, cstr, flt, fmt_money
def execute(filters=None):
if not filters:
return [], []
def get_conditions(filters):
conditions = []
from_date, to_date = getdate(filters.from_date), getdate(filters.to_date)
Do you have any errors when you run this code?
What does the console return, if you print filters?
If it returns nothing, then if not filters will always be true, meaning your script ends at this point, if Iām not mistaken. Iām afraid I canāt help much more beyond that
Thank you for replying.
The console gives the message below.
Uncaught SyntaxError: Invalid or unexpected token
at Object.eval (dom.js:33)
at query_report.js:172
query_report.js:177 Uncaught (in promise) TypeError: Cannot set property 'html_format' of undefined
at query_report.js:177
at request.js:381
at new Promise (<anonymous>)
at Object.frappe.after_ajax (request.js:374)
at query_report.js:175
Can you post your whole .py code AND .js code. Declare the filters in .js just like @SanRam suggested . Right after declaring it in js you should be able to see the filters on your report page after refreshing it. You can then move on to retrieving and manipulating data via server side.
Iām sorry, I canāt be of any more help right now.