Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 106 additions & 0 deletions rmax_custom/api/customer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import frappe
from frappe import _
from frappe.utils import cstr
from frappe.core.doctype.user_permission.user_permission import get_permitted_documents


def _get_default_customer_group():
permitted = get_permitted_documents("Customer Group")
return (permitted[0] if permitted else None) or frappe.db.get_single_value("Selling Settings", "customer_group") or "All Customer Groups"


def _get_default_territory():
permitted = get_permitted_documents("Territory")
return (permitted[0] if permitted else None) or frappe.db.get_single_value("Selling Settings", "territory") or "All Territories"


@frappe.whitelist()
def create_customer_with_address(
customer_name,
mobile_no=None,
customer_type="Individual",
email_id=None,
country=None,
default_currency=None,
tax_id=None,
commercial_registration_number=None,
address_type=None,
address_line1=None,
address_line2=None,
building_number=None,
city=None,
state=None,
pincode=None,
district=None
):

if not customer_name:
frappe.throw(_("Customer Name is required"))

if not mobile_no:
frappe.throw(_("Mobile No is required"))

# Prevent duplicate
if frappe.db.exists("Customer", {"customer_name": customer_name}):
frappe.throw(_("Customer already exists"))

# Get company defaults
if not country or not default_currency:
permitted_companies = get_permitted_documents("Company")
company = (permitted_companies[0] if permitted_companies else None) \
or frappe.defaults.get_user_default("company")

if not company:
frappe.throw(_("Please set a default company"))

company_doc = frappe.get_cached_doc("Company", company)

country = country or company_doc.country
default_currency = default_currency or company_doc.default_currency

# Create Customer
customer = frappe.get_doc({
"doctype": "Customer",
"customer_name": customer_name,
"customer_type": customer_type or "Individual",
"customer_group": _get_default_customer_group(),
"territory": _get_default_territory(),
"default_currency": default_currency,
"tax_id": tax_id,
"mobile_no": mobile_no,
"email_id": email_id
})

customer.insert(ignore_permissions=True)

address_name = None

address = frappe.get_doc({
"doctype": "Address",
"address_title": customer_name,
"address_type": address_type or "Billing",
"address_line1": address_line1,
"address_line2": address_line2,
"city": city,
"state": state,
"pincode": pincode,
"country": country,
"is_primary_address": 1,
"is_shipping_address": 1
})
address.append("links", {
"link_doctype": "Customer",
"link_name": customer.name,
"link_title": customer.customer_name
})

address.insert(ignore_permissions=True)
customer.customer_primary_address = address.name
customer.save(ignore_permissions=True)

return {
"customer": customer.name,
"address": address.name,
"message": "Customer and Address created successfully"
}

8 changes: 7 additions & 1 deletion rmax_custom/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
app_include_js = [
"/assets/rmax_custom/js/warehouse_stock_popup.js",
"/assets/rmax_custom/js/sales_invoice_pos_total_popup.js",
"/assets/rmax_custom/js/sales_invoice_popup.js",
"/assets/rmax_custom/js/create_customer.js",

]


Expand All @@ -48,7 +51,10 @@
# page_js = {"page" : "public/js/file.js"}

# include js in doctype views
doctype_js = {"Purchase Receipt" : "public/js/purchase receipt.js"}
doctype_js = {
"Quotation": "rmax_custom/custom_scripts/quotation/quotation.js",
"Purchase Receipt" : "public/js/purchase receipt.js"
}
# doctype_list_js = {"doctype" : "public/js/doctype_list.js"}
# doctype_tree_js = {"doctype" : "public/js/doctype_tree.js"}
# doctype_calendar_js = {"doctype" : "public/js/doctype_calendar.js"}
Expand Down
138 changes: 138 additions & 0 deletions rmax_custom/public/js/create_customer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@

frappe.ui.form.on("Sales Invoice", {
refresh: function (frm) {
add_create_customer_button(frm);

}
});

function add_create_customer_button(frm) {

if (frm.doc.docstatus !== 0) return;
if (!frm.fields_dict.customer) return;

const $field = frm.fields_dict.customer.$wrapper;
const $parent = $field.parent();

if ($parent.find(".create-customer-btn").length) return;

const $btn = $(`
<button type="button"
class="btn btn-sm btn-secondary create-customer-btn"
style="margin-bottom: 5px;">
<i class="fa fa-plus"></i> Create New Customer
</button>
`);

$btn.on("click", function () {
open_create_customer_dialog(frm);
});

$field.before($btn);
}


function open_create_customer_dialog(frm) {

let company = frm.doc.company || frappe.defaults.get_default("company");

frappe.db.get_value("Company", company,
["country", "default_currency"], function(r) {

let country = r.country;
let default_currency = r.default_currency;

let d = new frappe.ui.Dialog({
title: "Create New Customer",
fields: [
{
fieldname: "customer_name",
fieldtype: "Data",
label: "Customer Name",
reqd: 1
},
{
fieldname: "mobile_no",
fieldtype: "Data",
label: "Mobile No",
reqd: 1
},
{
fieldname: "email_id",
fieldtype: "Data",
label: "Email ID"
},
{ fieldtype: "Section Break", label: "Address Details" },
{
fieldname: "address_type",
fieldtype: "Select",
label: "Address Type",
options: "Billing\nShipping",
default: "Billing",
reqd: 1
},
{
fieldname: "address_line1",
fieldtype: "Data",
label: "Address Line 1",
reqd: 1
},
{
fieldname: "address_line2",
fieldtype: "Data",
label: "Address Line 2"
},
{
fieldname: "city",
fieldtype: "Data",
label: "City/Town",
reqd: 1
},
{
fieldname: "country",
fieldtype: "Link",
options: "Country",
label: "Country",
default: country,
reqd: 1
},


],
primary_action_label: "Create Customer",
primary_action(values) {

frappe.call({
method: "rmax_custom.api.customer.create_customer_with_address",
args: {
customer_name: values.customer_name,
mobile_no: values.mobile_no,
email_id: values.email_id || null,
address_type: values.address_type,
address_line1: values.address_line1,
address_line2: values.address_line2 || null,
city: values.city,
country: values.country,
default_currency: default_currency
},
callback: function(r) {
if (r.message) {

frm.set_value("customer", r.message.customer);
frm.refresh_field("customer");

frappe.show_alert({
message: r.message.message,
indicator: "green"
});

d.hide();
}
}
});
}
});

d.show();
});
}
Loading
Loading