createOrder

This commit is contained in:
joshikannan
2024-04-02 15:33:10 +05:30
parent 3a255749bb
commit 7bf192f7f6

View File

@@ -63,6 +63,8 @@ import CardContent from 'themes/overrides/CardContent';
import { PhoneAndroid } from '@mui/icons-material';
import { SearchOutlined, CloseOutlined } from '@ant-design/icons';
import PhoneInput from 'react-phone-number-input/input';
import MyLocationIcon from '@mui/icons-material/MyLocation';
import HighlightOffIcon from '@mui/icons-material/HighlightOff';
function loadScript(src, position, id) {
if (!position) {
@@ -113,130 +115,130 @@ const Createorder1 = () => {
const [tenanatLocoId, setTenanatLocoId] = useState(localStorage.getItem('locationid'));
const [isLocation, setIsLocation] = useState(false);
// ====================================================== || address (pick)|| ======================================================
useEffect(() => {
if (address) {
try {
Geocode.fromAddress(address).then(
(response) => {
if (response.status == 'OK') {
const { lat, lng } = response.results[0].geometry.location;
console.log({ lat, lng });
setLatlong({
lat,
lng
});
console.log(response);
if (response.results[0].address_components) {
let place = response.results[0];
let cityA, zipcodeA, stateA, suburbA;
for (let i = 0; i < place.address_components.length; i++) {
for (let j = 0; j < place.address_components[i].types.length; j++) {
switch (place.address_components[i].types[j]) {
case 'locality':
cityA = place.address_components[i].long_name;
break;
case 'administrative_area_level_1':
stateA = place.address_components[i].long_name;
break;
case 'postal_code':
zipcodeA = place.address_components[i].long_name;
break;
case 'sublocality':
suburbA = place.address_components[i].long_name;
break;
}
}
}
setCity(cityA || '');
setState(stateA || '');
setZipcode(zipcodeA || '');
setSuburb(suburbA || '');
console.log({ lat, lng, cityA, stateA, zipcodeA, suburbA });
setPickCust({
...pickCust,
city: cityA,
state: stateA,
postcode: zipcodeA,
suburb: suburbA,
latitude: lat,
longitude: lng
});
setStartPoint({ latitude: lat, longitude: lng });
}
}
},
(error) => {
console.log(error);
}
);
} catch (err) {
console.log(err);
}
}
}, [address]);
// ====================================================== || address 1 (drop)|| ======================================================
useEffect(() => {
if (address) {
try {
Geocode.fromAddress(address1).then(
(response) => {
if (response.status == 'OK') {
const { lat, lng } = response.results[0].geometry.location;
// // // ====================================================== || address (pick)|| ======================================================
// useEffect(() => {
// if (address) {
// try {
// Geocode.fromAddress(address).then(
// (response) => {
// if (response.status == 'OK') {
// const { lat, lng } = response.results[0].geometry.location;
// console.log({ lat, lng });
// setLatlong({
// lat,
// lng
// });
// console.log(response);
// if (response.results[0].address_components) {
// let place = response.results[0];
// let cityA, zipcodeA, stateA, suburbA;
// for (let i = 0; i < place.address_components.length; i++) {
// for (let j = 0; j < place.address_components[i].types.length; j++) {
// switch (place.address_components[i].types[j]) {
// case 'locality':
// cityA = place.address_components[i].long_name;
// break;
// case 'administrative_area_level_1':
// stateA = place.address_components[i].long_name;
// break;
// case 'postal_code':
// zipcodeA = place.address_components[i].long_name;
// break;
// case 'sublocality':
// suburbA = place.address_components[i].long_name;
// break;
// }
// }
// }
// setCity(cityA || '');
// setState(stateA || '');
// setZipcode(zipcodeA || '');
// setSuburb(suburbA || '');
// console.log({ lat, lng, cityA, stateA, zipcodeA, suburbA });
// setPickCust({
// ...pickCust
// // city: cityA,
// // state: stateA,
// // postcode: zipcodeA,
// // suburb: suburbA
// // latitude: lat,
// // longitude: lng
// });
// // setStartPoint({ latitude: lat, longitude: lng });
// }
// }
// },
// (error) => {
// console.log(error);
// }
// );
// } catch (err) {
// console.log(err);
// }
// }
// }, [address]);
// // // ====================================================== || address 1 (drop)|| ======================================================
// useEffect(() => {
// if (address) {
// try {
// Geocode.fromAddress(address1).then(
// (response) => {
// if (response.status == 'OK') {
// const { lat, lng } = response.results[0].geometry.location;
setLatlong1({
lat,
lng
});
console.log(response);
if (response.results[0].address_components) {
let place = response.results[0];
let cityB, zipcodeB, stateB, suburbB;
for (let i = 0; i < place.address_components.length; i++) {
for (let j = 0; j < place.address_components[i].types.length; j++) {
switch (place.address_components[i].types[j]) {
case 'locality':
cityB = place.address_components[i].long_name;
break;
case 'administrative_area_level_1':
stateB = place.address_components[i].long_name;
break;
case 'postal_code':
zipcodeB = place.address_components[i].long_name;
break;
case 'sublocality':
suburbB = place.address_components[i].long_name;
break;
}
}
}
setCity(cityB || '');
setState(stateB || '');
setZipcode(zipcodeB || '');
setSuburb(suburbB || '');
console.log({ lat, lng, cityB, stateB, zipcodeB, suburbB });
setDropCust({
...dropCust,
city: cityB,
state: stateB,
postcode: zipcodeB,
suburb: suburbB,
latitude: lat,
longitude: lng
});
setEndPoint({ latitude: lat, longitude: lng });
}
}
},
(error) => {
console.log(error);
}
);
} catch (err) {
console.log(err);
}
}
}, [address1]);
// setLatlong1({
// lat,
// lng
// });
// console.log(response);
// if (response.results[0].address_components) {
// let place = response.results[0];
// let cityB, zipcodeB, stateB, suburbB;
// for (let i = 0; i < place.address_components.length; i++) {
// for (let j = 0; j < place.address_components[i].types.length; j++) {
// switch (place.address_components[i].types[j]) {
// case 'locality':
// cityB = place.address_components[i].long_name;
// break;
// case 'administrative_area_level_1':
// stateB = place.address_components[i].long_name;
// break;
// case 'postal_code':
// zipcodeB = place.address_components[i].long_name;
// break;
// case 'sublocality':
// suburbB = place.address_components[i].long_name;
// break;
// }
// }
// }
// setCity(cityB || '');
// setState(stateB || '');
// setZipcode(zipcodeB || '');
// setSuburb(suburbB || '');
// console.log({ lat, lng, cityB, stateB, zipcodeB, suburbB });
// setDropCust({
// ...dropCust
// // city: cityB,
// // state: stateB,
// // postcode: zipcodeB,
// // suburb: suburbB
// // latitude: lat,
// // longitude: lng
// });
// // setEndPoint({ latitude: lat, longitude: lng });
// }
// }
// },
// (error) => {
// console.log(error);
// }
// );
// } catch (err) {
// console.log(err);
// }
// }
// }, [address1]);
if (typeof window !== 'undefined' && !loaded.current) {
if (!document.querySelector('#google-maps')) {
@@ -246,93 +248,92 @@ const Createorder1 = () => {
'google-maps'
);
}
loaded.current = true;
}
const fetch = React.useMemo(
() =>
debounce((request, callback) => {
autocompleteService.current.getPlacePredictions(request, callback);
}, 400),
[]
);
const fetch1 = React.useMemo(
() =>
debounce((request, callback) => {
autocompleteService.current.getPlacePredictions(request, callback);
}, 400),
[]
);
// const fetch = React.useMemo(
// () =>
// debounce((request, callback) => {
// autocompleteService.current.getPlacePredictions(request, callback);
// }, 400),
// []
// );
// const fetch1 = React.useMemo(
// () =>
// debounce((request, callback) => {
// autocompleteService.current.getPlacePredictions(request, callback);
// }, 400),
// []
// );
// ====================================================== || options (pick)|| ======================================================
React.useEffect(() => {
let active = true;
if (!autocompleteService.current && window.google) {
autocompleteService.current = new window.google.maps.places.AutocompleteService();
}
if (!autocompleteService.current) {
return undefined;
}
if (inputValue === '') {
setOptions(value ? [value] : []);
return undefined;
}
fetch({ input: inputValue }, (results) => {
if (active) {
let newOptions = [];
// React.useEffect(() => {
// let active = true;
// if (!autocompleteService.current && window.google) {
// autocompleteService.current = new window.google.maps.places.AutocompleteService();
// }
// if (!autocompleteService.current) {
// return undefined;
// }
// if (inputValue === '') {
// setOptions(value ? [value] : []);
// return undefined;
// }
// fetch({ input: inputValue }, (results) => {
// if (active) {
// let newOptions = [];
if (value) {
newOptions = [value];
}
// if (value) {
// newOptions = [value];
// }
if (results) {
newOptions = [...newOptions, ...results];
}
// if (results) {
// newOptions = [...newOptions, ...results];
// }
setOptions(newOptions);
}
});
// setOptions(newOptions);
// }
// });
return () => {
active = false;
};
}, [value, inputValue, fetch]);
// return () => {
// active = false;
// };
// }, [value, inputValue, fetch]);
// ====================================================== || options1 (drop)|| ======================================================
React.useEffect(() => {
let active = true;
if (!autocompleteService.current && window.google) {
autocompleteService.current = new window.google.maps.places.AutocompleteService();
}
if (!autocompleteService.current) {
return undefined;
}
if (inputValue1 === '') {
setOptions1(value1 ? [value1] : []);
return undefined;
}
fetch1({ input: inputValue1 }, (results) => {
if (active) {
let newOptions = [];
// // ====================================================== || options1 (drop)|| ======================================================
// React.useEffect(() => {
// let active = true;
// if (!autocompleteService.current && window.google) {
// autocompleteService.current = new window.google.maps.places.AutocompleteService();
// }
// if (!autocompleteService.current) {
// return undefined;
// }
// if (inputValue1 === '') {
// setOptions1(value1 ? [value1] : []);
// return undefined;
// }
// fetch1({ input: inputValue1 }, (results) => {
// if (active) {
// let newOptions = [];
if (value1) {
newOptions = [value1];
}
// if (value1) {
// newOptions = [value1];
// }
if (results) {
newOptions = [...newOptions, ...results];
}
// if (results) {
// newOptions = [...newOptions, ...results];
// }
setOptions1(newOptions);
}
});
// setOptions1(newOptions);
// }
// });
return () => {
active = false;
};
}, [value1, inputValue1, fetch1]);
// return () => {
// active = false;
// };
// }, [value1, inputValue1, fetch1]);
const appId = localStorage.getItem('applocationid');
const navigate = useNavigate();
@@ -461,8 +462,8 @@ const Createorder1 = () => {
}
setShowDistance(true);
if (roundedDistance > appLocaRadius) {
opentoast('Service not available at this location', 'error', 2000);
setShowDistance(true);
setOpen4(true);
}
};
@@ -772,106 +773,108 @@ const Createorder1 = () => {
let arr = {};
arr = {
orders: {
orderheaderid: 0,
tenantid: tenant.tenantid,
applocationid: tenant.applolcationid,
locationid: +tenanatLocoId, //main or branch
moduleid: +tenant.moduleid,
cancellled: '',
categoryid: +tenant.categoryid,
subcategoryid: +subCatId,
configid: +localStorage.getItem('configid'),
orderid: '', //
orderdate: dayjs().format('YYYY-MM-DD HH:mm:ss'),
customerid: +pickCust.customerid || 0,
deliveryaddress: dropCust.address || '',
deliverycharge: +totalCharge.toFixed(2) || 0,
deliverycity: dropCust.city || '',
deliverycontactno: dropCust.contactno || '',
deliverycustomer: dropCust.firstname || '',
deliveryid: dropCust.customerid || 0,
deliverylandmark: dropCust.landmark || '',
deliverylat: dropCust.latitude.toString(),
deliverylocation: dropCust.suburb || '',
deliverylocationid: dropCust.deliverylocationid || 0,
deliverylong: dropCust.longitude.toString(),
deliverytime: `${dayjs(startdate).format('YYYY-MM-DD')} ${dayjs(selectedtime.$d).format('HH:mm:ss')}`,
deliverytype: pickCust.customerid !== 0 || dropCust.customerid !== 0 ? 'B' : 'C',
delivered: '',
itemcount: 1,
kms: distance || 0,
locationid: +tenanatLocoId, //main or branch
moduleid: +tenant.moduleid,
orderamount: +totalCharge.toFixed(2) || 0,
ordercharges: 0.0,
orderdate: dayjs().format('YYYY-MM-DD HH:mm:ss'),
orderheaderid: 0,
orderid: '', //
ordernotes: otherinstructions,
orderstatus: 'created',
ordervalue: +totalCharge.toFixed(2) || 0,
partnerid: tenant.partnerid,
paymentstatus: 1,
paymenttype: 42,
pending: '',
pickupaddress: pickCust.address || '',
pickupcity: pickCust.city || '',
pickupcontactno: pickCust.contactno || '',
pickupcustomer: pickCust.firstname || '',
pickuplandmark: pickCust.landmark || '',
pickuplat: pickCust.latitude.toString(),
pickuplocation: pickCust.suburb || '',
pickuplocationid: pickCust.deliverylocationid || 0,
pickuplong: pickCust.longitude.toString(),
pickupaddress: pickCust.address || '',
processing: '',
ready: '',
delivered: '',
cancellled: '',
customerid: +pickCust.customerid || 0,
pickupcustomer: pickCust.firstname || '',
pickupcontactno: pickCust.contactno || '',
pickuplandmark: pickCust.landmark || '',
pickuplocation: pickCust.suburb || '',
pickupcity: pickCust.city || '',
pickuplocationid: pickCust.deliverylocationid || 0,
pickupaddress: pickCust.address || '',
pickuplat: pickCust.latitude.toString(),
pickuplong: pickCust.longitude.toString(),
deliveryid: dropCust.customerid || 0,
deliverycustomer: dropCust.firstname || '',
deliverycontactno: dropCust.contactno || '',
deliverylocationid: dropCust.deliverylocationid || 0,
deliverylat: dropCust.latitude.toString(),
deliverylong: dropCust.longitude.toString(),
deliveryaddress: dropCust.address || '',
deliverylandmark: dropCust.landmark || '',
deliverylocation: dropCust.suburb || '',
deliverycity: dropCust.city || '',
orderamount: +totalCharge.toFixed(2) || 0,
taxamount: 0.0,
ordercharges: 0.0,
ordervalue: +totalCharge.toFixed(2) || 0,
itemcount: 1,
paymenttype: 42,
paymentstatus: 1,
deliverycharge: +totalCharge.toFixed(2) || 0,
ordernotes: otherinstructions,
kms: distance || 0,
smsdelivery: 1,
remarks: '',
tenantuserid: parseInt(localStorage.getItem('userid')),
partnerid: tenant.partnerid
smsdelivery: 1,
subcategoryid: +subCatId,
taxamount: 0.0,
tenantid: tenant.tenantid,
tenantuserid: parseInt(localStorage.getItem('userid'))
},
pickup: {
customerid: pickCust.customerid || 0,
configid: parseInt(localStorage.getItem('configid')),
firstname: pickCust.firstname || '',
address: pickCust.address || '',
applocationid: pickCust.applocationid,
profileimage: '',
dialcode: '+91',
city: pickCust.city || '',
configid: parseInt(localStorage.getItem('configid')),
contactno: pickCust.contactno || '',
customertoken: '',
customerid: pickCust.customerid || 0,
devicetype: '',
deviceid: '',
customertoken: '',
address: pickCust.address || '',
suburb: pickCust.suburb || '',
city: pickCust.city || '',
state: pickCust.state || '',
postcode: pickCust.postcode || '',
landmark: pickCust.landmark || '',
dialcode: '+91',
doorno: pickCust.doorno || '',
email: pickCust.email || '',
firstname: pickCust.firstname || '',
landmark: pickCust.landmark || '',
latitude: pickCust.latitude.toString() || '',
longitude: pickCust.longitude.toString() || '',
tenantid: tenant.tenantid,
email: pickCust.email || '',
primaryaddress: 1
postcode: pickCust.postcode || '',
primaryaddress: 1,
profileimage: '',
state: pickCust.state || '',
suburb: pickCust.suburb || '',
tenantid: tenant.tenantid
},
drop: {
customerid: dropCust.customerid || 0,
configid: parseInt(localStorage.getItem('configid')),
firstname: dropCust.firstname || '',
address: dropCust.address || '',
applocationid: dropCust.applocationid,
profileimage: '',
dialcode: '+91',
city: dropCust.city || '',
configid: parseInt(localStorage.getItem('configid')),
contactno: dropCust.contactno || '',
customertoken: '',
customerid: dropCust.customerid || 0,
devicetype: '',
deviceid: '',
customertoken: '',
address: dropCust.address || '',
suburb: dropCust.suburb || '',
city: dropCust.city || '',
state: dropCust.state || '',
postcode: dropCust.postcode || '',
landmark: dropCust.landmark || '',
dialcode: '+91',
doorno: dropCust.doorno || '',
email: dropCust.email || '',
firstname: dropCust.firstname || '',
landmark: dropCust.landmark || '',
latitude: dropCust.latitude.toString(),
longitude: dropCust.longitude.toString(),
tenantid: tenant.tenantid,
email: dropCust.email || '',
primaryaddress: 1
postcode: dropCust.postcode || '',
primaryaddress: 1,
profileimage: '',
state: dropCust.state || '',
suburb: dropCust.suburb || '',
tenantid: tenant.tenantid
}
};
console.log('createsubmitobj2', arr);
@@ -1058,6 +1061,7 @@ const Createorder1 = () => {
// ============================================= || Google Maps Autocomplete(pick) || =============================================
useEffect(() => {
// Initialize Google Maps Autocomplete
if (inputValue2) {
const autocompleteInput = document.getElementById('addressAuto1');
const autocomplete = new window.google.maps.places.Autocomplete(autocompleteInput, {
// types: ['(cities)'], // You can adjust the types parameter based on your requirements
@@ -1075,11 +1079,14 @@ const Createorder1 = () => {
const place = autocomplete.getPlace();
setInputValue2(`${place.name}, ${place.formatted_address}`);
console.log('new place', place); // Do something with the selected place
console.log('new place lat lng', { lat: place.geometry.location.lat(), lng: place.geometry.location.lng() }); // Do something with the selected place
console.log(' pick (new place) lat lng', { lat: place.geometry.location.lat(), lng: place.geometry.location.lng() }); // Do something with the selected place
// to trigger getDistance
setStartPoint({ latitude: place.geometry.location.lat(), longitude: place.geometry.location.lng() });
setValue(place);
setAddress(`${place.name} ${place.formatted_address}`);
setPickCust({ ...pickCust, address: `${place.name} ${place.formatted_address}` });
const address = {
address: `${place.name} ${place.formatted_address}`,
street_number: '',
route: '',
locality: '',
@@ -1122,12 +1129,24 @@ const Createorder1 = () => {
});
// Use address object as per your requirements
console.log('Address:', address);
setPickCust({
...pickCust,
address: address.address,
doorno: `${address.street_number} ${address.route}`,
suburb: address.sublocality_level_1,
city: address.locality,
postcode: address.postal_code,
latitude: place.geometry.location.lat(),
longitude: place.geometry.location.lng()
});
}, [address, inputValue2, pickCust]);
console.log('Pick Address:', address);
});
}
}, [inputValue2]);
// ============================================= || Google Maps Autocomplete(Drop) || =============================================
useEffect(() => {
if (inputValue3) {
// Initialize Google Maps Autocomplete
const autocompleteInput = document.getElementById('addressAuto2');
const autocomplete = new window.google.maps.places.Autocomplete(autocompleteInput, {
@@ -1146,11 +1165,13 @@ const Createorder1 = () => {
const place = autocomplete.getPlace();
setInputValue3(`${place.name}, ${place.formatted_address}`);
console.log('new place', place); // Do something with the selected place
console.log('new place lat lng', { lat: place.geometry.location.lat(), lng: place.geometry.location.lng() }); // Do something with the selected place
console.log('drop (new place) lat lng', { lat: place.geometry.location.lat(), lng: place.geometry.location.lng() }); // Do something with the selected place
setEndPoint({ latitude: place.geometry.location.lat(), longitude: place.geometry.location.lng() });
setValue1(place);
setAddress1(`${place.name} ${place.formatted_address}`);
setDropCust({ ...pickCust, address: `${place.name} ${place.formatted_address}` });
setDropCust({ ...dropCust, address: `${place.name} ${place.formatted_address}` });
const address = {
address: `${place.name} ${place.formatted_address}`,
street_number: '',
route: '',
locality: '',
@@ -1193,9 +1214,20 @@ const Createorder1 = () => {
});
// Use address object as per your requirements
console.log('Address:', address);
setDropCust({
...dropCust,
address: address.address,
doorno: `${address.street_number} ${address.route}`,
suburb: address.sublocality_level_1,
city: address.locality,
postcode: address.postal_code,
latitude: place.geometry.location.lat(),
longitude: place.geometry.location.lng()
});
}, [address, inputValue3, dropCust]);
console.log('Drop Address:', address);
});
}
}, [inputValue3]);
// ============================================= || gettenantlocations (branches) || =============================================
const gettenantlocations = async () => {
@@ -1240,7 +1272,21 @@ const Createorder1 = () => {
>
<Typography variant="h3">Create Order</Typography>
{tenantLocations.length == 1 ? (
<TextField variant="outlined" label={'Tenent Location'} value={tenantLocations[0].locationname} sx={{ width: 300 }} />
<TextField
variant="outlined"
label={'Business Location'}
value={tenantLocations[0].locationname}
focused
sx={{ width: 300 }}
InputProps={{
style: { color: theme.palette.primary.main },
startAdornment: (
<InputAdornment position="start">
<MyLocationIcon color="primary" />
</InputAdornment>
)
}}
/>
) : (
<Autocomplete
disablePortal
@@ -1552,7 +1598,7 @@ const Createorder1 = () => {
)
}}
variant="outlined"
label="Door No"
label="Door No / Street"
value={pickCust.doorno}
onChange={(e) => {
setPickCust({ ...pickCust, doorno: e.target.value });
@@ -1922,7 +1968,7 @@ const Createorder1 = () => {
<TextField
fullWidth
variant="outlined"
label="Door No"
label="Door No / Street"
InputProps={{
startAdornment: (
<IconButton>
@@ -2123,7 +2169,7 @@ const Createorder1 = () => {
label={dayjs(val).format('hh:mm A')}
onClick={() => {
if (distance > appLocaRadius) {
opentoast('Service not available at this location', 'error', 2000);
setOpen4(true);
} else if (showDistance) {
console.log('selectedtime', val);
setSelectedtime(val);
@@ -2233,9 +2279,19 @@ const Createorder1 = () => {
<Button
disabled={!showDistance}
variant="contained"
sx={{
'&:hover': {
bgcolor: 'white',
color: theme.palette.primary.main,
border: `1px solid ${theme.palette.primary.main}`
}
}}
onClick={() => {
// createsubmitobj1();
setLoading(true);
createsubmitobj2();
setTimeout(() => {
setLoading(false);
}, 1000);
}}
>
Create
@@ -2366,7 +2422,7 @@ const Createorder1 = () => {
setIsCustomerOpen(false);
if (pickordrop === 1) {
console.log('PickupClient', address);
// setPickCust({ ...pickCust, address: address });
setStartPoint({ latitude: address.latitude, longitude: address.longitude });
setPickCust(address);
} else {
@@ -2434,18 +2490,41 @@ const Createorder1 = () => {
</DialogActions>
</Dialog>
{/* <Drawer
anchor={'right'}
<Dialog
// anchor={'right'}
open={open4}
onClose={() => {
setOpen4(false);
}}
>
<Box sx={{ width: { sm: '600px', xs: '100%' } }}>
<GoogleMaps />
</Box>
</Drawer> */}
<DialogTitle sx={{ textAlign: 'center' }}>
<HighlightOffIcon sx={{ fontSize: 60, color: theme.palette.error.main }} />
<Typography variant="h3" sx={{ color: theme.palette.error.main }}>
Error
</Typography>
</DialogTitle>
<DialogContent>
<Typography variant="h4" color={'secondary'}>
Service not available at this location
</Typography>
</DialogContent>
<DialogActions
sx={{
bgcolor: theme.palette.error.main,
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
fontSize: 20,
color: 'white',
cursor: 'pointer'
}}
onClick={() => {
setOpen4(false);
}}
>
Close
</DialogActions>
</Dialog>
</Grid>
</>
);