Skip to content

Commit

Permalink
feat:customer_server_side
Browse files Browse the repository at this point in the history
  • Loading branch information
GaturaN committed Aug 29, 2024
1 parent cc00019 commit 8fdfd4c
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 23 deletions.
40 changes: 20 additions & 20 deletions smartbill/smartbill/doctype/customer/customer.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,21 @@ frappe.ui.form.on("Customer", {
frm.toggle_display(
["national_id", "passport_number"],
frm.doc.identification_type !== "Select"
);
// set full name
if (frm.doc.middle_name_optional) {
frm.set_value(
"full_name",
frm.doc.first_name +
" " +
frm.doc.middle_name_optional +
" " +
frm.doc.last_name
);
} else {
frm.set_value("full_name", frm.doc.first_name + " " + frm.doc.last_name);
}
);
// This has been moved to server side
// // set full name
// if (frm.doc.middle_name_optional) {
// frm.set_value(
// "full_name",
// frm.doc.first_name +
// " " +
// frm.doc.middle_name_optional +
// " " +
// frm.doc.last_name
// );
// } else {
// frm.set_value("full_name", frm.doc.first_name + " " + frm.doc.last_name);
// }
},

validate(frm) {
Expand Down Expand Up @@ -55,17 +56,17 @@ frappe.ui.form.on("Customer", {
}
// check if email is valid
if (frm.doc.email) {
if (!frm.doc.email.includes("@")) {
frm.set_value("email","");
frappe.throw("Email is not valid");
}
if (!frm.doc.email.includes("@")) {
frm.set_value("email", "");
frappe.throw("Email is not valid");
}
}
},

date_of_birth(frm) {
// check if date of birth is valid
if (frm.doc.date_of_birth) {
if (Date.parse(frm.doc.date_of_birth) > Date.now()) {
if (Date.parse(frm.doc.date_of_birth) > Date.now()){
frappe.msgprint("Date of birth is not valid");
}
// calculate age in years, months and days
Expand Down Expand Up @@ -96,7 +97,6 @@ frappe.ui.form.on("Customer", {
if (parseInt(frm.doc.age) < 18) {
frm.set_value("date_of_birth", "");
frappe.throw("Must be 18 or older");

}
}
},
Expand Down
85 changes: 82 additions & 3 deletions smartbill/smartbill/doctype/customer/customer.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,88 @@
# Copyright (c) 2024, Gatura Njenga and contributors
# For license information, please see license.txt

# import frappe
import frappe
import re
from datetime import date, datetime, timedelta
from frappe.model.document import Document


class Customer(Document):
pass
def before_save(self):
set_full_name(self)

def validate(self):
valid_email(self)
validate_date_of_birth(self)
set_age(self)
validate_age(self)


# set full_name
def set_full_name(self):
first_name = self.first_name.strip().title()
middle_name = self.middle_name_optional.strip().title() if self.middle_name_optional else ""
last_name = self.last_name.strip().title()

# set all names to be title
self.first_name = first_name
self.middle_name_optional = middle_name if self.middle_name_optional else ""
self.last_name = last_name

# if middle_name is provided include it in full_name
if middle_name:
self.full_name = f"{first_name} {middle_name} {last_name}"
else:
self.full_name = f"{first_name} {last_name}"


# valid email? should contain @ and . after @
def valid_email(self):
pattern = "[a-zA-Z0-9]+@[a-zA-Z0-9]+\.[a-zA-Z]"
e = self.email
if re.search(pattern, e):
return True
else:
frappe.throw("Invalid email address")
# return False


# validate date_of_birth
def validate_date_of_birth(self):
if self.date_of_birth > frappe.utils.nowdate():
frappe.throw("Date of birth cannot be in the future")


# calculate age
def set_age(self):
today = date.today()

# Convert the date_of_birth from string to date object with format 'YYYY-MM-DD'
dob = datetime.strptime(self.date_of_birth, '%Y-%m-%d').date()

age_years = today.year - dob.year
age_months = today.month - dob.month
age_days = today.day - dob.day

if age_months < 0 or (age_months == 0 and age_days < 0):
age_years -= 1
age_months += 12

if age_days < 0:
# Adjust day difference
previous_month_last_day = (today.replace(day=1) - timedelta(days=1)).day
age_days += previous_month_last_day
age_months -= 1

self.age = f"{age_years} years {age_months} months {age_days} days"


# validate age > 18
def validate_age(self):
if self.age:
# Extract the number of years from the age string
age_years = int(self.age.split()[0])

if age_years < 18:
frappe.throw("Must be 18 or older")


0 comments on commit 8fdfd4c

Please sign in to comment.