diff --git a/app/controllers/BusinessesController.asp b/app/controllers/BusinessesController.asp index 339ed23..294fe7b 100644 --- a/app/controllers/BusinessesController.asp +++ b/app/controllers/BusinessesController.asp @@ -104,6 +104,109 @@ Class BusinessesController_Class <% End Sub + '------------------------------------------------------------------------------------------------------------------- + ' GET /business/new : blank form to enter a business by hand ("new" is reserved in VBScript, hence the action name) + '------------------------------------------------------------------------------------------------------------------- + Public Sub add() + Dim fv + Set fv = CreateObject("Scripting.Dictionary") + Set fv("_area_ids") = CreateObject("Scripting.Dictionary") + RenderAdd Nothing, fv + End Sub + + ' dup is a matching other business (or Nothing); fv holds the form values to show. + Private Sub RenderAdd(dup, fv) + Dim industries, allAreas, areaSel, csrf, ix, indKey2 + Set industries = BusinessRepository().Industries() + Set allAreas = AreaRepository().All() + Set areaSel = fv("_area_ids") + csrf = Csrf_Token("business") + m_title = "Add business" + %> + + <% + End Sub + + '------------------------------------------------------------------------------------------------------------------- + ' POST /business/create + '------------------------------------------------------------------------------------------------------------------- + Public Sub create() + Dim d, email, phone, indName, indSel, areaIds, areaNew, ai, areaId, areaName, dupMatch, fv, k, newId + + If Not Csrf_Valid("business", F("csrf")) Then + Flash().AddError "Invalid form state. Please try again." + Response.Redirect "/business/new" + End If + + Set d = CreateObject("Scripting.Dictionary") + d("name") = Left(Trim(F("name")), 255) + email = LCase(Left(Trim(F("email")), 255)) + phone = Left(Trim(F("phone")), 40) + d("contact_name") = Left(Trim(F("contact_name")), 150) + d("phone") = phone + d("phone_digits") = ImportFile_PhoneDigits(phone) + d("email") = email + d("website") = Left(Trim(F("website")), 255) + d("address1") = Left(Trim(F("address1")), 255) + d("city") = Left(Trim(F("city")), 100) + d("state") = Left(Trim(F("state")), 50) + d("zip") = Left(Trim(F("zip")), 20) + d("notes") = Left(F("notes"), 10000) + If F("is_customer") = "1" Then d("is_customer") = 1 Else d("is_customer") = 0 + d("first_order_date") = Date_Parse(F("first_order_date")) + + indName = Trim(F("industry_new")) + indSel = F("industry_id") + If Valid_Id(indSel) Then d("industry_id") = CLng(indSel) Else d("industry_id") = Null + + Set areaIds = CreateObject("Scripting.Dictionary") + For ai = 1 To Request.Form("area_ids").Count + If Valid_Id(Request.Form("area_ids")(ai)) Then areaIds(CStr(CLng(Request.Form("area_ids")(ai)))) = True + Next + areaNew = Left(Trim(F("area_new")), 500) + + Set fv = CreateObject("Scripting.Dictionary") + For Each k In d.Keys + fv(k) = d(k) + Next + fv("_industry_new") = indName + fv("_area_new") = areaNew + Set fv("_area_ids") = areaIds + + If Len(d("name")) = 0 Then + Flash().AddError "Business name is required." + RenderAdd Nothing, fv + Exit Sub + End If + If Len(email) > 0 And InStr(email, "@") = 0 Then + Flash().AddError "That email address doesn't look right, so nothing was saved." + RenderAdd Nothing, fv + Exit Sub + End If + + ' Warn when the phone/email already belongs to another business (unless "Save anyway" was pressed). + If F("allow_duplicate") <> "1" Then + Set dupMatch = BusinessRepository().FindDuplicate(0, d("phone_digits"), d("email")) + If Not dupMatch Is Nothing Then + RenderAdd dupMatch, fv + Exit Sub + End If + End If + + If Len(indName) > 0 Then d("industry_id") = BusinessRepository().FindOrCreateIndustry(indName) + + newId = BusinessRepository().Create(d) + + For Each areaName In Split(areaNew, ",") + areaId = AreaRepository().FindOrCreate(areaName) + If Not IsNull(areaId) Then areaIds(CStr(areaId)) = True + Next + AreaRepository().SetForBusiness newId, areaIds + + Flash().Success = "Added " & d("name") & "." + Response.Redirect "/businesses/" & newId + End Sub + '------------------------------------------------------------------------------------------------------------------- ' POST /businesses/:id/update : edit details '------------------------------------------------------------------------------------------------------------------- diff --git a/app/repositories/BusinessRepository.asp b/app/repositories/BusinessRepository.asp index 8059054..e076776 100644 --- a/app/repositories/BusinessRepository.asp +++ b/app/repositories/BusinessRepository.asp @@ -217,6 +217,19 @@ Class BusinessRepository_Class ' Changes '--------------------------------------------------------------------------------------------------------------------- + ' Inserts a business entered by hand (status "new") and returns its id. d holds the same keys as Update. + Public Function Create(d) + Dim rs + Db_Exec "INSERT INTO businesses (name, contact_name, industry_id, phone, phone_digits, email, website, address1, city, state, zip, " & _ + "source, status, is_customer, first_order_date, notes, created_at) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,Now())", _ + Array(Db_PS(d("name")), Db_PS(d("contact_name")), Db_PI(d("industry_id")), Db_PS(d("phone")), Db_PS(d("phone_digits")), Db_PS(d("email")), _ + Db_PS(d("website")), Db_PS(d("address1")), Db_PS(d("city")), Db_PS(d("state")), Db_PS(d("zip")), _ + Db_PS("Added manually"), Db_PS("new"), Db_PI(d("is_customer")), Db_PD(d("first_order_date")), Db_PS(d("notes"))) + Set rs = Db_Query("SELECT @@IDENTITY", Array()) + Create = CLng(rs(0)) + rs.Close + End Function + Public Sub Update(id, d) Db_Exec "UPDATE businesses SET name = ?, contact_name = ?, industry_id = ?, phone = ?, phone_digits = ?, email = ?, " & _ "website = ?, address1 = ?, city = ?, state = ?, zip = ?, notes = ?, is_customer = ?, first_order_date = ? WHERE id = ?", _ diff --git a/app/views/Businesses/add.asp b/app/views/Businesses/add.asp new file mode 100644 index 0000000..05ba300 --- /dev/null +++ b/app/views/Businesses/add.asp @@ -0,0 +1,112 @@ +<% Flash().ShowErrorsIfPresent : Flash().ShowSuccessIfPresent %> + + + +