Customer Statement Query

The following query can used to create to create customer statement, i am still working on the layout

SELECT tA.customer AS customer_acc,
tC.customer_name AS name,
tA.address_line1 AS address_line1,
tA.address_line2 AS address_line2,
tA.city AS city,
tA.country AS country,
tC.default_currency AS curr,
tSI.name AS trx_no,
tSI.creation AS trx_date,
tSI.due_date AS due_date,
tSI.tc_name AS trx_type,
tSI.grand_total AS trx_amount,
tSI.outstanding_amount AS outs_amount
FROM ((tabCustomer tC
LEFT JOIN tabAddress tA
ON ((tC.customer_name = tA.customer_name)))
LEFT JOIN tabSales Invoice tSI
ON ((tA.customer = tSI.customer)))
WHERE (tSI.outstanding_amount IS NOT NULL)
UNION ALL
SELECT tA.customer AS customer_acc,
tC.customer_name AS name,
tA.address_line1 AS address_line1,
tA.address_line2 AS address_line2,
tA.city AS city,
tA.country AS country,
tC.default_currency AS curr,
tE.voucher_no AS trx_no,
tE.creation AS trx_date,
NULL AS due_date,
tE.voucher_type AS trx_type,
(CASE
WHEN (tE.credit <> 0) THEN (tE.credit * -(1))
ELSE tE.debit
END)
AS trx_amount,
(CASE
WHEN (tE.credit <> 0) THEN (tE.credit * -(1))
ELSE tE.debit
END)
AS outs_amount
FROM ((tabGL Entry tE
LEFT JOIN tabCustomer tC ON ((tE.party = tC.name)))
LEFT JOIN tabAddress tA
ON ((tC.customer_name = tA.customer_name)))
WHERE ((tE.party_type = ‘Customer’) AND isnull(tE.against_voucher));

For the Aging

SELECT VCS.customer_acc AS customer_acc,
VCS.name AS customer_name,
VCS.address_line1 AS address_line1,
VCS.address_line2 AS address_line2,
VCS.city AS city,
VCS.country AS country,
VCS.trx_no AS trx_no,
VCS.trx_date AS trx_date,
VCS.trx_type AS trx_type,
VCS.trx_amount AS trx_amount,
VCS.outs_amount AS outs_amount,
ifnull(
(CASE WHEN (VCS.outs_amount < 0) THEN VCS.outs_amount END),
0)
AS Unalloc,
ifnull(
(CASE WHEN (VCS.outs_amount >= 0) THEN VCS.outs_amount END),
0)
AS Func,
(year(curdate()) + month(curdate())) AS curr,
(year(VCS.trx_date) + month(VCS.trx_date)) AS trx,
( (year(curdate()) + month(curdate()))
- (year(VCS.trx_date) + month(VCS.trx_date)))
AS aging,
ifnull(
(CASE
WHEN ( (( (year(curdate()) + month(curdate()))
- (year(VCS.trx_date) + month(VCS.trx_date))) =
0)
AND (VCS.outs_amount >= 0))
THEN
VCS.outs_amount
END),
0)
AS current,
ifnull(
(CASE
WHEN ( (( (year(curdate()) + month(curdate()))
- (year(VCS.trx_date) + month(VCS.trx_date))) =
1)
AND (VCS.outs_amount >= 0))
THEN
VCS.outs_amount
END),
0)
AS age_30,
ifnull(
(CASE
WHEN ( (( (year(curdate()) + month(curdate()))
- (year(VCS.trx_date) + month(VCS.trx_date))) =
2)
AND (VCS.outs_amount >= 0))
THEN
VCS.outs_amount
END),
0)
AS age_60,
ifnull(
(CASE
WHEN ( (( (year(curdate()) + month(curdate()))
- (year(VCS.trx_date) + month(VCS.trx_date))) >=
3)
AND (VCS.outs_amount >= 0))
THEN
VCS.outs_amount
END),
0)
AS age_90abv
FROM VVL_Customer_Statement VCS;

3 Likes

Nice! Send a pull-request once you are done and become an official contributor :scroll:

As for the report, i am trying to create my own report layout, i don’t have the option of editing the layout, if someone can kindly assit

I am totally new to ERPNext, can you please guide me through

https://frappe.github.io/frappe/user/guides/reports-and-printing/how-to-make-query-report.html

Hi,
I m trying to create customer statement as per the initial query post. however, even as system manager (and administrator ?) i get the following message : Only Administrator allowed to create Query / Script Reports.
Can you enlight me on the procedure to follow ?
Thx

Hi Manu,

I have created a query in the db for the same and also designed my report
on Toad SQL for the same.

Regards,

Essajee Murtaza

Server Error:
pymysql.err.ProgrammingError: (1146, “Table ‘nexterp2.tabCustomer tC’ doesn’t exist”)