2019-01-17 19:04:01 +00:00
|
|
|
# Copyright (c) 2018, Frappe Technologies Pvt. Ltd. and contributors
|
|
|
|
# For license information, please see license.txt
|
|
|
|
|
|
|
|
import json
|
2021-01-19 13:31:13 +00:00
|
|
|
|
|
|
|
import frappe
|
|
|
|
from frappe import _
|
|
|
|
from frappe.utils import cstr, nowdate
|
|
|
|
from frappe.utils.data import fmt_money
|
|
|
|
from frappe.utils.jinja import render_template
|
2019-01-17 19:04:01 +00:00
|
|
|
from frappe.utils.pdf import get_pdf
|
|
|
|
from frappe.utils.print_format import read_multi_pdf
|
|
|
|
from PyPDF2 import PdfFileWriter
|
2021-09-02 11:14:59 +00:00
|
|
|
|
2021-01-19 13:31:13 +00:00
|
|
|
from erpnext.accounts.utils import get_fiscal_year
|
2019-01-17 19:04:01 +00:00
|
|
|
|
2021-01-19 13:31:13 +00:00
|
|
|
IRS_1099_FORMS_FILE_EXTENSION = ".pdf"
|
2019-01-17 19:04:01 +00:00
|
|
|
|
2020-08-26 15:25:16 +00:00
|
|
|
|
2021-01-19 13:31:13 +00:00
|
|
|
def execute(filters=None):
|
|
|
|
filters = filters if isinstance(filters, frappe._dict) else frappe._dict(filters)
|
2019-01-17 19:04:01 +00:00
|
|
|
if not filters:
|
|
|
|
filters.setdefault("fiscal_year", get_fiscal_year(nowdate())[0])
|
2020-08-26 15:25:16 +00:00
|
|
|
filters.setdefault("company", frappe.db.get_default("company"))
|
2020-08-26 13:10:11 +00:00
|
|
|
|
2021-01-19 13:31:13 +00:00
|
|
|
region = frappe.db.get_value("Company", filters={"name": filters.company}, fieldname=["country"])
|
|
|
|
|
2020-08-26 13:10:11 +00:00
|
|
|
if region != "United States":
|
2021-01-19 13:31:13 +00:00
|
|
|
return [], []
|
2020-08-26 15:25:16 +00:00
|
|
|
|
2019-01-17 19:04:01 +00:00
|
|
|
columns = get_columns()
|
2021-02-11 05:36:12 +00:00
|
|
|
conditions = ""
|
|
|
|
if filters.supplier_group:
|
|
|
|
conditions += "AND s.supplier_group = %s" % frappe.db.escape(filters.get("supplier_group"))
|
|
|
|
|
2019-01-17 19:04:01 +00:00
|
|
|
data = frappe.db.sql(
|
|
|
|
"""
|
|
|
|
SELECT
|
|
|
|
s.supplier_group as "supplier_group",
|
|
|
|
gl.party AS "supplier",
|
|
|
|
s.tax_id as "tax_id",
|
2019-03-04 07:52:51 +00:00
|
|
|
SUM(gl.debit_in_account_currency) AS "payments"
|
2019-01-17 19:04:01 +00:00
|
|
|
FROM
|
2021-01-19 13:31:13 +00:00
|
|
|
`tabGL Entry` gl
|
|
|
|
INNER JOIN `tabSupplier` s
|
2019-01-17 19:04:01 +00:00
|
|
|
WHERE
|
|
|
|
s.name = gl.party
|
2021-01-19 13:31:13 +00:00
|
|
|
AND s.irs_1099 = 1
|
|
|
|
AND gl.fiscal_year = %(fiscal_year)s
|
|
|
|
AND gl.party_type = "Supplier"
|
2021-02-11 05:36:12 +00:00
|
|
|
AND gl.company = %(company)s
|
|
|
|
{conditions}
|
2021-08-19 08:11:10 +00:00
|
|
|
|
2019-01-17 19:04:01 +00:00
|
|
|
GROUP BY
|
|
|
|
gl.party
|
2021-02-11 05:36:12 +00:00
|
|
|
|
2019-01-17 19:04:01 +00:00
|
|
|
ORDER BY
|
2021-02-11 05:36:12 +00:00
|
|
|
gl.party DESC""".format(
|
|
|
|
conditions=conditions
|
|
|
|
),
|
|
|
|
{"fiscal_year": filters.fiscal_year, "company": filters.company},
|
|
|
|
as_dict=True,
|
|
|
|
)
|
2021-01-19 13:31:13 +00:00
|
|
|
|
2019-01-17 19:04:01 +00:00
|
|
|
return columns, data
|
|
|
|
|
|
|
|
|
|
|
|
def get_columns():
|
|
|
|
return [
|
|
|
|
{
|
|
|
|
"fieldname": "supplier_group",
|
|
|
|
"label": _("Supplier Group"),
|
|
|
|
"fieldtype": "Link",
|
|
|
|
"options": "Supplier Group",
|
|
|
|
"width": 200,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"fieldname": "supplier",
|
|
|
|
"label": _("Supplier"),
|
|
|
|
"fieldtype": "Link",
|
|
|
|
"options": "Supplier",
|
|
|
|
"width": 200,
|
|
|
|
},
|
|
|
|
{"fieldname": "tax_id", "label": _("Tax ID"), "fieldtype": "Data", "width": 200},
|
|
|
|
{"fieldname": "payments", "label": _("Total Payments"), "fieldtype": "Currency", "width": 200},
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
@frappe.whitelist()
|
|
|
|
def irs_1099_print(filters):
|
|
|
|
if not filters:
|
|
|
|
frappe._dict(
|
|
|
|
{
|
|
|
|
"company": frappe.db.get_default("Company"),
|
2021-01-19 13:31:13 +00:00
|
|
|
"fiscal_year": frappe.db.get_default("Fiscal Year"),
|
|
|
|
}
|
|
|
|
)
|
2019-01-17 19:04:01 +00:00
|
|
|
else:
|
|
|
|
filters = frappe._dict(json.loads(filters))
|
2021-01-19 13:31:13 +00:00
|
|
|
|
|
|
|
fiscal_year_doc = get_fiscal_year(fiscal_year=filters.fiscal_year, as_dict=True)
|
|
|
|
fiscal_year = cstr(fiscal_year_doc.year_start_date.year)
|
|
|
|
|
2019-01-17 19:04:01 +00:00
|
|
|
company_address = get_payer_address_html(filters.company)
|
|
|
|
company_tin = frappe.db.get_value("Company", filters.company, "tax_id")
|
2021-01-19 13:31:13 +00:00
|
|
|
|
2019-01-17 19:04:01 +00:00
|
|
|
columns, data = execute(filters)
|
|
|
|
template = frappe.get_doc("Print Format", "IRS 1099 Form").html
|
|
|
|
output = PdfFileWriter()
|
2021-01-19 13:31:13 +00:00
|
|
|
|
2019-01-17 19:04:01 +00:00
|
|
|
for row in data:
|
2021-01-19 13:31:13 +00:00
|
|
|
row["fiscal_year"] = fiscal_year
|
2019-01-17 19:04:01 +00:00
|
|
|
row["company"] = filters.company
|
|
|
|
row["company_tin"] = company_tin
|
|
|
|
row["payer_street_address"] = company_address
|
2021-01-19 13:31:13 +00:00
|
|
|
row["recipient_street_address"], row["recipient_city_state"] = get_street_address_html(
|
|
|
|
"Supplier", row.supplier
|
|
|
|
)
|
2019-01-17 19:04:01 +00:00
|
|
|
row["payments"] = fmt_money(row["payments"], precision=0, currency="USD")
|
|
|
|
pdf = get_pdf(render_template(template, row), output=output if output else None)
|
2021-01-19 13:31:13 +00:00
|
|
|
|
|
|
|
frappe.local.response.filename = (
|
|
|
|
f"{filters.fiscal_year} {filters.company} IRS 1099 Forms{IRS_1099_FORMS_FILE_EXTENSION}"
|
2022-03-28 13:22:46 +00:00
|
|
|
)
|
2019-01-17 19:04:01 +00:00
|
|
|
frappe.local.response.filecontent = read_multi_pdf(output)
|
|
|
|
frappe.local.response.type = "download"
|
|
|
|
|
|
|
|
|
|
|
|
def get_payer_address_html(company):
|
|
|
|
address_list = frappe.db.sql(
|
|
|
|
"""
|
|
|
|
SELECT
|
|
|
|
name
|
|
|
|
FROM
|
|
|
|
tabAddress
|
|
|
|
WHERE
|
|
|
|
is_your_company_address = 1
|
|
|
|
ORDER BY
|
|
|
|
address_type="Postal" DESC, address_type="Billing" DESC
|
|
|
|
LIMIT 1
|
2021-01-19 13:31:13 +00:00
|
|
|
""",
|
|
|
|
{"company": company},
|
|
|
|
as_dict=True,
|
|
|
|
)
|
|
|
|
|
|
|
|
address_display = ""
|
2019-01-17 19:04:01 +00:00
|
|
|
if address_list:
|
|
|
|
company_address = address_list[0]["name"]
|
2021-01-19 13:31:13 +00:00
|
|
|
address_display = frappe.get_doc("Address", company_address).get_display()
|
|
|
|
|
|
|
|
return address_display
|
2019-01-17 19:04:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
def get_street_address_html(party_type, party):
|
|
|
|
address_list = frappe.db.sql(
|
|
|
|
"""
|
|
|
|
SELECT
|
|
|
|
link.parent
|
2021-01-19 13:31:13 +00:00
|
|
|
FROM
|
|
|
|
`tabDynamic Link` link,
|
|
|
|
`tabAddress` address
|
|
|
|
WHERE
|
|
|
|
link.parenttype = "Address"
|
|
|
|
AND link.link_name = %(party)s
|
|
|
|
ORDER BY
|
|
|
|
address.address_type="Postal" DESC,
|
2019-01-17 19:04:01 +00:00
|
|
|
address.address_type="Billing" DESC
|
|
|
|
LIMIT 1
|
2021-01-19 13:31:13 +00:00
|
|
|
""",
|
|
|
|
{"party": party},
|
|
|
|
as_dict=True,
|
|
|
|
)
|
|
|
|
|
|
|
|
street_address = city_state = ""
|
2019-01-17 19:04:01 +00:00
|
|
|
if address_list:
|
|
|
|
supplier_address = address_list[0]["parent"]
|
|
|
|
doc = frappe.get_doc("Address", supplier_address)
|
2021-01-19 13:31:13 +00:00
|
|
|
|
2019-01-17 19:04:01 +00:00
|
|
|
if doc.address_line2:
|
2021-01-19 13:31:13 +00:00
|
|
|
street_address = doc.address_line1 + "<br>\n" + doc.address_line2 + "<br>\n"
|
2019-01-17 19:04:01 +00:00
|
|
|
else:
|
2021-01-19 13:31:13 +00:00
|
|
|
street_address = doc.address_line1 + "<br>\n"
|
|
|
|
|
|
|
|
city_state = doc.city + ", " if doc.city else ""
|
|
|
|
city_state = city_state + doc.state + " " if doc.state else city_state
|
|
|
|
city_state = city_state + doc.pincode if doc.pincode else city_state
|
|
|
|
city_state += "<br>\n"
|
|
|
|
|
|
|
|
return street_address, city_state
|