1
0
forked from test/crm

fix: allow creating or selecting org & contact from create deal modal from deal listview

This commit is contained in:
Shariq Ansari 2024-04-17 00:32:33 +05:30
parent 49ef15883d
commit 9cde4bb5a0
4 changed files with 412 additions and 268 deletions

View File

@ -230,3 +230,78 @@ def set_primary_contact(deal, contact):
deal.save()
return True
def create_organization(doc):
if not doc.get("organization_name"):
return
existing_organization = frappe.db.exists("CRM Organization", {"organization_name": doc.get("organization_name")})
if existing_organization:
return existing_organization
organization = frappe.new_doc("CRM Organization")
organization.update(
{
"organization_name": doc.get("organization_name"),
"website": doc.get("website"),
"territory": doc.get("territory"),
"industry": doc.get("industry"),
"annual_revenue": doc.get("annual_revenue"),
}
)
organization.insert(ignore_permissions=True)
return organization.name
def contact_exists(doc):
email_exist = frappe.db.exists("Contact Email", {"email_id": doc.get("email")})
mobile_exist = frappe.db.exists("Contact Phone", {"phone": doc.get("mobile_no")})
doctype = "Contact Email" if email_exist else "Contact Phone"
name = email_exist or mobile_exist
if name:
return frappe.db.get_value(doctype, name, "parent")
return False
def create_contact(doc):
existing_contact = contact_exists(doc)
if existing_contact:
return existing_contact
contact = frappe.new_doc("Contact")
contact.update(
{
"first_name": doc.get("first_name"),
"last_name": doc.get("last_name"),
"salutation": doc.get("salutation"),
"company_name": doc.get("organization") or doc.get("organization_name"),
}
)
if doc.get("email"):
contact.append("email_ids", {"email_id": doc.get("email"), "is_primary": 1})
if doc.get("mobile_no"):
contact.append("phone_nos", {"phone": doc.get("mobile_no"), "is_primary_mobile_no": 1})
contact.insert(ignore_permissions=True)
contact.reload() # load changes by hooks on contact
return contact.name
@frappe.whitelist()
def create_deal(args):
deal = frappe.new_doc("CRM Deal")
contact = args.get("contact")
if not contact and (args.get("first_name") or args.get("last_name") or args.get("email") or args.get("mobile_no")):
contact = create_contact(args)
deal.update({
"organization": args.get("organization") or create_organization(args),
"contacts": [{"contact": contact, "is_primary": 1}] if contact else [],
"deal_owner": args.get("deal_owner"),
"status": args.get("status"),
})
deal.insert(ignore_permissions=True)
return deal.name

View File

@ -0,0 +1,330 @@
<template>
<Dialog
v-model="show"
:options="{
size: '3xl',
title: __('Create Deal'),
}"
>
<template #body-content>
<div class="mb-4 grid grid-cols-3 gap-4">
<div class="flex items-center gap-3 text-sm text-gray-600">
<div>{{ __('Choose Existing Organization') }}</div>
<Switch v-model="chooseExistingOrganization" />
</div>
<div class="flex items-center gap-3 text-sm text-gray-600">
<div>{{ __('Choose Existing Contact') }}</div>
<Switch v-model="chooseExistingContact" />
</div>
</div>
<div class="flex flex-col gap-4">
<div v-for="section in allFields" :key="section.section">
<div class="grid grid-cols-3 gap-4 border-t pt-4">
<div v-for="field in section.fields" :key="field.name">
<div class="mb-2 text-sm text-gray-600">{{ field.label }}</div>
<FormControl
v-if="field.type === 'select'"
type="select"
class="form-control"
:options="field.options"
v-model="deal[field.name]"
:placeholder="__(field.placeholder)"
>
<template v-if="field.prefix" #prefix>
<IndicatorIcon :class="field.prefix" />
</template>
</FormControl>
<Link
v-else-if="field.type === 'link'"
class="form-control"
:value="deal[field.name]"
:doctype="field.doctype"
@change="(v) => (deal[field.name] = v)"
:placeholder="__(field.placeholder)"
:onCreate="field.create"
/>
<Link
v-else-if="field.type === 'user'"
class="form-control"
:value="getUser(deal[field.name]).full_name"
:doctype="field.doctype"
@change="(v) => (deal[field.name] = v)"
:placeholder="__(field.placeholder)"
:hideMe="true"
>
<template #prefix>
<UserAvatar class="mr-2" :user="deal[field.name]" size="sm" />
</template>
<template #item-prefix="{ option }">
<UserAvatar class="mr-2" :user="option.value" size="sm" />
</template>
<template #item-label="{ option }">
<Tooltip :text="option.value">
<div class="cursor-pointer">
{{ getUser(option.value).full_name }}
</div>
</Tooltip>
</template>
</Link>
<FormControl
v-else
type="text"
:placeholder="__(field.placeholder)"
v-model="deal[field.name]"
/>
</div>
</div>
</div>
</div>
<ErrorMessage class="mt-4" v-if="error" :message="__(error)" />
</template>
<template #actions>
<div class="flex flex-row-reverse gap-2">
<Button
variant="solid"
:label="__('Create')"
:loading="isDealCreating"
@click="createDeal"
/>
</div>
</template>
</Dialog>
</template>
<script setup>
import IndicatorIcon from '@/components/Icons/IndicatorIcon.vue'
import UserAvatar from '@/components/UserAvatar.vue'
import Link from '@/components/Controls/Link.vue'
import { usersStore } from '@/stores/users'
import { statusesStore } from '@/stores/statuses'
import { createToast } from '@/utils'
import { Tooltip, Switch, createResource } from 'frappe-ui'
import { computed, ref, reactive, onMounted } from 'vue'
import { useRouter } from 'vue-router'
const { getUser } = usersStore()
const { getDealStatus, statusOptions } = statusesStore()
const show = defineModel()
const router = useRouter()
const error = ref(null)
const deal = reactive({
organization: '',
organization_name: '',
website: '',
industry: '',
territory: '',
contact: '',
first_name: '',
last_name: '',
email: '',
mobile_no: '',
status: '',
deal_owner: '',
})
const isDealCreating = ref(false)
const chooseExistingContact = ref(false)
const chooseExistingOrganization = ref(false)
const allFields = computed(() => {
let fields = []
if (chooseExistingOrganization.value) {
fields.push({
section: 'Select Organization',
fields: [
{
label: 'Organization',
name: 'organization',
type: 'link',
placeholder: 'Frappé Technologies',
doctype: 'CRM Organization',
},
],
})
} else {
fields.push({
section: 'Organization Details',
fields: [
{
label: 'Organization Name',
name: 'organization_name',
type: 'data',
placeholder: 'Frappé Technologies',
},
{
label: 'Website',
name: 'website',
type: 'data',
placeholder: 'https://frappe.io',
},
{
label: 'No of Employees',
name: 'no_of_employees',
type: 'select',
options: [
{ label: __('1-10'), value: '1-10' },
{ label: __('11-50'), value: '11-50' },
{ label: __('51-200'), value: '51-200' },
{ label: __('201-500'), value: '201-500' },
{ label: __('501-1000'), value: '501-1000' },
{ label: __('1001-5000'), value: '1001-5000' },
{ label: __('5001-10000'), value: '5001-10000' },
{ label: __('10001+'), value: '10001+' },
],
placeholder: '1-10',
},
{
label: 'Territory',
name: 'territory',
type: 'link',
doctype: 'CRM Territory',
placeholder: 'India',
},
{
label: 'Annual Revenue',
name: 'annual_revenue',
type: 'data',
placeholder: '9,999,999',
},
{
label: 'Industry',
name: 'industry',
type: 'link',
doctype: 'CRM Industry',
placeholder: 'Technology',
},
],
})
}
if (chooseExistingContact.value) {
fields.push({
section: 'Select Contact',
fields: [
{
label: 'Contact',
name: 'contact',
type: 'link',
placeholder: 'John Doe',
doctype: 'Contact',
},
],
})
} else {
fields.push({
section: 'Contact Details',
fields: [
{
label: 'Salutation',
name: 'salutation',
type: 'link',
doctype: 'Salutation',
placeholder: 'Mr',
},
{
label: 'First Name',
name: 'first_name',
type: 'data',
placeholder: 'John',
},
{
label: 'Last Name',
name: 'last_name',
type: 'data',
placeholder: 'Doe',
},
{
label: 'Email',
name: 'email',
type: 'data',
placeholder: 'john@doe.com',
},
{
label: 'Mobile No',
name: 'mobile_no',
type: 'data',
placeholder: '+91 1234567890',
},
{
label: 'Gender',
name: 'gender',
type: 'link',
doctype: 'Gender',
placeholder: 'Male',
},
],
})
}
fields.push({
section: 'Deal Details',
fields: [
{
label: 'Status',
name: 'status',
type: 'select',
options: statusOptions('deal', (field, value) => (deal[field] = value)),
prefix: getDealStatus(deal.status).iconColorClass,
},
{
label: 'Deal Owner',
name: 'deal_owner',
type: 'user',
placeholder: 'Deal Owner',
doctype: 'User',
},
],
})
return fields
})
function createDeal() {
createResource({
url: 'crm.fcrm.doctype.crm_deal.crm_deal.create_deal',
params: { args: deal },
auto: true,
validate() {
error.value = null
if (deal.website && !deal.website.startsWith('http')) {
deal.website = 'https://' + deal.website
}
if (deal.annual_revenue) {
deal.annual_revenue = deal.annual_revenue.replace(/,/g, '')
if (isNaN(deal.annual_revenue)) {
error.value = __('Annual Revenue should be a number')
return error.value
}
}
if (deal.mobile_no && isNaN(deal.mobile_no.replace(/[-+() ]/g, ''))) {
error.value = __('Mobile No should be a number')
return error.value
}
if (deal.email && !deal.email.includes('@')) {
error.value = __('Invalid Email')
return error.value
}
isDealCreating.value = true
},
onSuccess(name) {
isDealCreating.value = false
show.value = false
router.push({ name: 'Deal', params: { dealId: name } })
},
})
}
onMounted(() => {
if (!deal.status) {
deal.status = computed(() => getDealStatus().name)
}
if (!deal.deal_owner) {
deal.deal_owner = getUser().email
}
})
</script>
<style scoped>
:deep(.form-control select) {
padding-left: 2rem;
}
</style>

View File

@ -1,190 +0,0 @@
<template>
<div class="flex flex-col gap-4">
<div v-for="section in allFields" :key="section.section">
<div class="grid grid-cols-3 gap-4">
<div v-for="field in section.fields" :key="field.name">
<div class="mb-2 text-sm text-gray-600">{{ field.label }}</div>
<FormControl
v-if="field.type === 'select'"
type="select"
class="form-control"
:options="field.options"
v-model="newDeal[field.name]"
>
<template v-if="field.prefix" #prefix>
<IndicatorIcon :class="field.prefix" />
</template>
</FormControl>
<FormControl
v-else-if="field.type === 'email'"
type="email"
v-model="newDeal[field.name]"
/>
<Link
v-else-if="field.type === 'link'"
class="form-control"
:value="newDeal[field.name]"
:doctype="field.doctype"
@change="(e) => field.change(e)"
:placeholder="field.placeholder"
:onCreate="field.create"
/>
<Link
v-else-if="field.type === 'user'"
class="form-control"
:value="getUser(newDeal[field.name]).full_name"
:doctype="field.doctype"
@change="(e) => field.change(e)"
:placeholder="field.placeholder"
:hideMe="true"
>
<template #prefix>
<UserAvatar class="mr-2" :user="newDeal[field.name]" size="sm" />
</template>
<template #item-prefix="{ option }">
<UserAvatar class="mr-2" :user="option.value" size="sm" />
</template>
<template #item-label="{ option }">
<Tooltip :text="option.value">
<div class="cursor-pointer">
{{ getUser(option.value).full_name }}
</div>
</Tooltip>
</template>
</Link>
<FormControl
v-else
type="text"
:placeholder="field.placeholder"
v-model="newDeal[field.name]"
/>
</div>
</div>
</div>
</div>
<OrganizationModal
v-model="showOrganizationModal"
v-model:organization="_organization"
:options="{
redirect: false,
afterInsert: (doc) => (newDeal.organization = doc.name),
}"
/>
</template>
<script setup>
import IndicatorIcon from '@/components/Icons/IndicatorIcon.vue'
import UserAvatar from '@/components/UserAvatar.vue'
import Link from '@/components/Controls/Link.vue'
import OrganizationModal from '@/components/Modals/OrganizationModal.vue'
import { usersStore } from '@/stores/users'
import { statusesStore } from '@/stores/statuses'
import { Tooltip } from 'frappe-ui'
import { computed, onMounted, ref } from 'vue'
const { getUser } = usersStore()
const { getDealStatus, statusOptions } = statusesStore()
const props = defineProps({
newDeal: {
type: Object,
required: true,
},
})
const showOrganizationModal = ref(false)
const _organization = ref({})
const allFields = computed(() => {
return [
{
section: 'Deal Details',
fields: [
{
label: 'Salutation',
name: 'salutation',
type: 'link',
doctype: 'Salutation',
placeholder: 'Mr.',
change: (data) => (props.newDeal.salutation = data),
},
{
label: 'First Name',
name: 'first_name',
type: 'data',
placeholder: 'John',
},
{
label: 'Last Name',
name: 'last_name',
type: 'data',
placeholder: 'Doe',
},
{
label: 'Email',
name: 'email',
type: 'data',
placeholder: 'john@doe.com',
},
{
label: 'Mobile No',
name: 'mobile_no',
type: 'data',
placeholder: '+91 1234567890',
},
],
},
{
section: 'Other Details',
fields: [
{
label: 'Organization',
name: 'organization',
type: 'link',
placeholder: 'Frappé Technologies',
doctype: 'CRM Organization',
change: (data) => (props.newDeal.organization = data),
create: (value, close) => {
_organization.value.organization_name = value
showOrganizationModal.value = true
close()
},
},
{
label: 'Status',
name: 'status',
type: 'select',
options: statusOptions(
'deal',
(field, value) => (props.newDeal[field] = value)
),
prefix: getDealStatus(props.newDeal.status).iconColorClass,
},
{
label: 'Deal Owner',
name: 'deal_owner',
type: 'user',
placeholder: 'Deal Owner',
doctype: 'User',
change: (data) => (props.newDeal.deal_owner = data),
},
],
},
]
})
onMounted(() => {
if (!props.newDeal.status) {
props.newDeal.status = getDealStatus(props.newDeal.status).name
}
if (!props.newDeal.deal_owner) {
props.newDeal.deal_owner = getUser().email
}
})
</script>
<style scoped>
:deep(.form-control select) {
padding-left: 2rem;
}
</style>

View File

@ -11,7 +11,7 @@
<Button
variant="solid"
:label="__('Create')"
@click="showNewDialog = true"
@click="showDealModal = true"
>
<template #prefix><FeatherIcon name="plus" class="h-4" /></template>
</Button>
@ -49,31 +49,12 @@
>
<DealsIcon class="h-10 w-10" />
<span>{{ __('No Deals Found') }}</span>
<Button :label="__('Create')" @click="showNewDialog = true">
<Button :label="__('Create')" @click="showDealModal = true">
<template #prefix><FeatherIcon name="plus" class="h-4" /></template>
</Button>
</div>
</div>
<Dialog
v-model="showNewDialog"
:options="{
size: '3xl',
title: __('New Deal'),
}"
>
<template #body-content>
<NewDeal :newDeal="newDeal" />
</template>
<template #actions="{ close }">
<div class="flex flex-row-reverse gap-2">
<Button
variant="solid"
:label="__('Save')"
@click="createNewDeal(close)"
/>
</div>
</template>
</Dialog>
<DealModal v-model="showDealModal" />
</template>
<script setup>
@ -81,7 +62,7 @@ import CustomActions from '@/components/CustomActions.vue'
import DealsIcon from '@/components/Icons/DealsIcon.vue'
import LayoutHeader from '@/components/LayoutHeader.vue'
import DealsListView from '@/components/ListViews/DealsListView.vue'
import NewDeal from '@/components/NewDeal.vue'
import DealModal from '@/components/Modals/DealModal.vue'
import ViewControls from '@/components/ViewControls.vue'
import { usersStore } from '@/stores/users'
import { organizationsStore } from '@/stores/organizations'
@ -92,11 +73,9 @@ import {
timeAgo,
formatNumberIntoCurrency,
formatTime,
createToast,
} from '@/utils'
import { createResource, Breadcrumbs } from 'frappe-ui'
import { useRouter } from 'vue-router'
import { ref, computed, reactive } from 'vue'
import { Breadcrumbs } from 'frappe-ui'
import { ref, computed } from 'vue'
const breadcrumbs = [{ label: __('Deals'), route: { name: 'Deals' } }]
@ -104,9 +83,8 @@ const { getUser } = usersStore()
const { getOrganization } = organizationsStore()
const { getDealStatus } = statusesStore()
const router = useRouter()
const dealsListView = ref(null)
const showDealModal = ref(false)
// deals data is loaded in the ViewControls component
const deals = ref({})
@ -197,53 +175,4 @@ const rows = computed(() => {
return _rows
})
})
// New Deal
const showNewDialog = ref(false)
let newDeal = reactive({
organization: '',
status: '',
email: '',
mobile_no: '',
deal_owner: '',
})
const createDeal = createResource({
url: 'frappe.client.insert',
makeParams(values) {
return {
doc: {
doctype: 'CRM Deal',
...values,
},
}
},
})
function createNewDeal(close) {
createDeal
.submit(newDeal, {
validate() {
if (!newDeal.first_name) {
createToast({
title: __('Error creating deal'),
text: __('First name is required'),
icon: 'x',
iconClasses: 'text-red-600',
})
return __('First name is required')
}
},
onSuccess(data) {
router.push({
name: 'Deal',
params: {
dealId: data.name,
},
})
},
})
.then(close)
}
</script>