Skip to content

Commit

Permalink
add type in customer_type tax
Browse files Browse the repository at this point in the history
  • Loading branch information
avinashRathod02 committed Jan 5, 2022
1 parent 9c44270 commit d198504
Show file tree
Hide file tree
Showing 12 changed files with 87 additions and 48 deletions.
5 changes: 3 additions & 2 deletions src/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -870,8 +870,9 @@
"invalid_postal_code": "Please enter proper zip code."
},
"modal": {
"shipping_address": "Enter Shipping Address",
"shipping": "Enter Shipping Address",
"billing": "Enter Billing Address",
"company_address": "Enter Company Address",
"sales_tax_heading": "The information below is required in order to fetch sales tax."
}
}
}
2 changes: 1 addition & 1 deletion src/navigation/navigation-routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,5 +88,5 @@ export const routes = {

// Modal
COMPANY_ADDRESS_MODAL: 'COMPANY_ADDRESS_MODAL',
SHIPPING_ADDRESS_MODAL: 'CUSTOMER_SHIPPING_ADDRESS_MODAL'
CUSTOMER_ADDRESS_MODAL: 'CUSTOMER_ADDRESS_MODAL'
};
6 changes: 3 additions & 3 deletions src/navigation/navigators/common-navigator.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ import {PaymentModes} from 'screens/payment-modes';
import Webview from 'screens/webview';

import CompanyAddressModal from 'screens/modal/company-address';
import CustomerShippingAddressModal from 'screens/modal/customer-address';
import CustomerAddressModal from 'screens/modal/customer-address';

import {
RecurringInvoices,
Expand Down Expand Up @@ -268,8 +268,8 @@ export const CommonNavigator = (
options={options}
/>
<Stack.Screen
name={routes.SHIPPING_ADDRESS_MODAL}
component={CustomerShippingAddressModal}
name={routes.CUSTOMER_ADDRESS_MODAL}
component={CustomerAddressModal}
options={options}
/>

Expand Down
20 changes: 13 additions & 7 deletions src/screens/estimates/create-estimate/create-estimate.js
Original file line number Diff line number Diff line change
Expand Up @@ -346,9 +346,10 @@ export default class CreateEstimate extends React.Component<IProps, IStates> {
};

onCreateNewCustomer = data => {
const {billing = {}, shipping = {}} = data;
this.customerReference?.changeDisplayValue?.(data);
this.onCustomerSelect(data);
this.fetchSalesTaxRate(taxationTypes.CUSTOMER_LEVEL, data?.shipping ?? {});
this.fetchSalesTaxRate(taxationTypes.CUSTOMER_LEVEL, shipping, billing);
};

navigateToCustomer = () => {
Expand All @@ -365,17 +366,22 @@ export default class CreateEstimate extends React.Component<IProps, IStates> {
};

onCustomerSelect = data => {
const {id, currency, billing = {}, shipping = {}} = data;
data && this.state.hasProvider && this.setState({hasProvider: false});
this.setFormField('exchange_rate', null);
this.setFormField('customer_id', data.id);
this.setExchangeRate(data.currency);
this.fetchNextEstimateNumber(data.id);
this.fetchSalesTaxRate(taxationTypes.CUSTOMER_LEVEL, data?.shipping ?? {});
this.setFormField('customer_id', id);
this.setExchangeRate(currency);
this.fetchNextEstimateNumber(id);
this.fetchSalesTaxRate(taxationTypes.CUSTOMER_LEVEL, shipping, billing);
};

fetchSalesTaxRate = (type = taxationTypes.COMPANY_LEVEL, address = null) => {
fetchSalesTaxRate = (
type = taxationTypes.COMPANY_LEVEL,
shipping = null,
billing = null
) => {
const {dispatch} = this.props;
const params = {form: CREATE_ESTIMATE_FORM, type, address};
const params = {form: CREATE_ESTIMATE_FORM, type, shipping, billing};
dispatch(fetchSalesTaxRate(params));
};

Expand Down
20 changes: 13 additions & 7 deletions src/screens/invoices/create-invoice/create-invoice.js
Original file line number Diff line number Diff line change
Expand Up @@ -316,9 +316,10 @@ export default class CreateInvoice extends React.Component<IProps, IStates> {
};

onCreateNewCustomer = data => {
const {billing = {}, shipping = {}} = data;
this.customerReference?.changeDisplayValue?.(data);
this.onCustomerSelect(data);
this.fetchSalesTaxRate(taxationTypes.CUSTOMER_LEVEL, data?.shipping ?? {});
this.fetchSalesTaxRate(taxationTypes.CUSTOMER_LEVEL, shipping, billing);
};

navigateToCustomer = () => {
Expand All @@ -335,17 +336,22 @@ export default class CreateInvoice extends React.Component<IProps, IStates> {
};

onCustomerSelect = data => {
const {id, currency, billing = {}, shipping = {}} = data;
data && this.state.hasProvider && this.setState({hasProvider: false});
this.setFormField('exchange_rate', null);
this.setFormField('customer_id', data.id);
this.setExchangeRate(data.currency);
this.fetchNextInvoiceNumber(data.id);
this.fetchSalesTaxRate(taxationTypes.CUSTOMER_LEVEL, data?.shipping ?? {});
this.setFormField('customer_id', id);
this.setExchangeRate(currency);
this.fetchNextInvoiceNumber(id);
this.fetchSalesTaxRate(taxationTypes.CUSTOMER_LEVEL, shipping, billing);
};

fetchSalesTaxRate = (type = taxationTypes.COMPANY_LEVEL, address = null) => {
fetchSalesTaxRate = (
type = taxationTypes.COMPANY_LEVEL,
shipping,
billing
) => {
const {dispatch} = this.props;
const params = {form: CREATE_INVOICE_FORM, type, address};
const params = {form: CREATE_INVOICE_FORM, type, shipping, billing};
dispatch(fetchSalesTaxRate(params));
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ import {reduxForm} from 'redux-form';
import CustomerAddress from './customer-address';
import {commonSelector} from 'stores/common/selectors';
import validate from './customer-address-validator';
import {SHIPPING_ADDRESS_FORM} from 'stores/customer/types';
import {CUSTOMER_ADDRESS_FORM} from 'stores/customer/types';
import {loadingSelector} from 'stores/taxation/selectors';

const mapStateToProps = (state, {route}) => ({
...commonSelector(state),
address: route?.params?.address,
parentForm: route?.params?.parentForm,
addressType: route?.params?.address?.addressType,
isSaving: loadingSelector(state),
initialValues: {
state: null,
Expand All @@ -20,7 +21,7 @@ const mapStateToProps = (state, {route}) => ({
}
});

const CustomerAddressForm = reduxForm({form: SHIPPING_ADDRESS_FORM, validate})(
const CustomerAddressForm = reduxForm({form: CUSTOMER_ADDRESS_FORM, validate})(
CustomerAddress
);

Expand Down
10 changes: 10 additions & 0 deletions src/screens/modal/customer-address/customer-address-type.d.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,16 @@ export interface IProps {
* Selected company currency.
*/
currency: any;

/**
* Object of address.
*/
address: Object<any>;

/**
* The type of address for sales tax if sales-tax-type-is customer.
*/
addressType: string;
}

export interface IStates {
Expand Down
16 changes: 8 additions & 8 deletions src/screens/modal/customer-address/customer-address.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React, {Component} from 'react';
import {Field, initialize} from 'redux-form';
import t from 'locales/use-translation';
import {IProps, IStates} from './customer-address-type.d';
import {SHIPPING_ADDRESS_FORM} from 'stores/customer/types';
import {CUSTOMER_ADDRESS_FORM} from 'stores/customer/types';
import {fetchSalesTaxRate} from 'stores/taxation/actions';
import {taxationTypes} from 'stores/taxation/helper';
import {
Expand All @@ -24,23 +24,23 @@ export default class CustomerAddress extends Component<IProps, IStates> {

loadData = () => {
const {dispatch, address} = this.props;
dispatch(initialize(SHIPPING_ADDRESS_FORM, address));
dispatch(initialize(CUSTOMER_ADDRESS_FORM, address));
this.setState({isFetchingInitialData: false});
};

onSave = address => {
const {dispatch, parentForm} = this.props;
const params = {
const {dispatch, parentForm, addressType} = this.props;
let params = {
form: parentForm,
type: taxationTypes.CUSTOMER_LEVEL,
goBack: true,
address
goBack: true
};
params[addressType] = address;
dispatch(fetchSalesTaxRate(params));
};

render() {
const {handleSubmit, isSaving} = this.props;
const {handleSubmit, isSaving, addressType} = this.props;
const {isFetchingInitialData} = this.state;

const bottomAction = (
Expand All @@ -57,7 +57,7 @@ export default class CustomerAddress extends Component<IProps, IStates> {

return (
<ModalLayout
title={t('modal.shipping_address')}
title={t(`modal.${addressType}`)}
sub-title={t('modal.sales_tax_heading')}
bottomAction={bottomAction}
loadingProps={{is: isFetchingInitialData}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -263,9 +263,10 @@ export default class CreateRecurringInvoice extends Component<IProps, IStates> {
};

onCreateNewCustomer = data => {
const {billing = {}, shipping = {}} = data;
this.customerReference?.changeDisplayValue?.(data);
this.onCustomerSelect(data);
this.fetchSalesTaxRate(taxationTypes.CUSTOMER_LEVEL, data?.shipping ?? {});
this.fetchSalesTaxRate(taxationTypes.CUSTOMER_LEVEL, shipping, billing);
};

navigateToCustomer = () => {
Expand All @@ -280,16 +281,26 @@ export default class CreateRecurringInvoice extends Component<IProps, IStates> {
};

onCustomerSelect = data => {
const {id, currency, billing = {}, shipping = {}} = data;
data && this.state.hasProvider && this.setState({hasProvider: false});
this.setFormField('exchange_rate', null);
this.setFormField('customer_id', data.id);
this.setExchangeRate(data.currency);
this.fetchSalesTaxRate(taxationTypes.CUSTOMER_LEVEL, data?.shipping ?? {});
this.setFormField('customer_id', id);
this.setExchangeRate(currency);
this.fetchSalesTaxRate(taxationTypes.CUSTOMER_LEVEL, shipping, billing);
};

fetchSalesTaxRate = (type = taxationTypes.COMPANY_LEVEL, address = null) => {
fetchSalesTaxRate = (
type = taxationTypes.COMPANY_LEVEL,
shipping = null,
billing = null
) => {
const {dispatch} = this.props;
const params = {form: CREATE_RECURRING_INVOICE_FORM, type, address};
const params = {
form: CREATE_RECURRING_INVOICE_FORM,
type,
shipping,
billing
};
dispatch(fetchSalesTaxRate(params));
};

Expand Down
2 changes: 1 addition & 1 deletion src/stores/customer/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ export const SPINNER = 'customer/SPINNER';
export const CUSTOMERS_FORM = 'customer/CUSTOMERS_FORM';
export const CREATE_CUSTOMER_FORM = 'customer/CREATE_CUSTOMER_FORM';
export const ADDRESS_FORM = 'customer/ADDRESS_FORM';
export const SHIPPING_ADDRESS_FORM = 'customer/SHIPPING_ADDRESS_FORM';
export const CUSTOMER_ADDRESS_FORM = 'customer/CUSTOMER_ADDRESS_FORM';

export const FETCH_CUSTOMERS = 'customer/FETCH_CUSTOMERS';
export const FETCH_CUSTOMERS_SUCCESS = 'customer/FETCH_CUSTOMERS_SUCCESS';
Expand Down
4 changes: 3 additions & 1 deletion src/stores/taxation/helper.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import {hasTextLength as hasValue} from '@/constants';
import {find} from 'lodash';

export const salesTax = 'Sales Tax';

export const isFullAddress = address => {
const fields = ['address_street_1', 'city', 'state', 'zip'];
let isValid = true;
Expand All @@ -22,7 +24,7 @@ export const taxationTypes = {

export const setSalesTaxUsFieldValue = values => {
const taxes = values?.taxes ?? [];
let salesTaxUs = find(taxes, {name: 'SalesTaxUs'});
let salesTaxUs = find(taxes, {name: salesTax});
salesTaxUs && (salesTaxUs = {...salesTaxUs, id: salesTaxUs.tax_type_id});
return {salesTaxUs};
};
22 changes: 12 additions & 10 deletions src/stores/taxation/saga.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import * as req from './service';
import {spinner} from './actions';
import {handleError} from '@/utils';
import {hasValue, isBooleanTrue} from '@/constants';
import {isFullAddress, taxationTypes} from './helper';
import {isFullAddress, salesTax, taxationTypes} from './helper';
import {routes, navigation} from '@/navigation';
import {store} from '@/stores';
import {fetchBootstrap} from '../common/actions';
Expand All @@ -27,7 +27,7 @@ function* updateTaxes(form, salesTaxUs) {
initialize(form, {
...formValues,
salesTaxUs: null,
taxes: taxes.filter(tax => tax.name !== 'SalesTaxUs')
taxes: taxes.filter(tax => tax.name !== salesTax)
})
);
return;
Expand All @@ -47,16 +47,16 @@ function* updateTaxes(form, salesTaxUs) {
);
}

function* navigateToAddressScreen(payload, type, address) {
function* navigateToAddressScreen(payload, type, address, addressType) {
const state = yield select();
const formValues = state.form[payload.form]?.values;

let route = null;
let addressInitialValues = address;

if (type === taxationTypes.CUSTOMER_LEVEL) {
route = routes.SHIPPING_ADDRESS_MODAL;
route = routes.CUSTOMER_ADDRESS_MODAL;
addressInitialValues = {
addressType,
customer_id: formValues?.customer_id,
address_street_1: address?.address_street_1,
address_street_2: address?.address_street_2,
Expand Down Expand Up @@ -91,8 +91,8 @@ function* fetchSalesTaxRate({payload}) {
const formValues = state.form[form]?.values;
const selectedCompany = selectedCompanySettingSelector(state);
const type = selectedCompany?.sales_tax_type;
const addressType = selectedCompany?.sales_tax_address_type;
let address = null;

try {
yield put(spinner('isSaving', true));
const isEnabled = isBooleanTrue(selectedCompany?.sales_tax_us_enabled);
Expand All @@ -112,7 +112,7 @@ function* fetchSalesTaxRate({payload}) {
}

if (type === taxationTypes.CUSTOMER_LEVEL) {
address = payload?.address;
address = payload[addressType];
} else {
address = payload?.address ?? currentCompanyAddressSelector(state);
}
Expand All @@ -122,7 +122,7 @@ function* fetchSalesTaxRate({payload}) {
}

if (!isFullAddress(address)) {
yield call(navigateToAddressScreen, payload, type, address);
yield call(navigateToAddressScreen, payload, type, address, addressType);
return;
}

Expand All @@ -132,7 +132,8 @@ function* fetchSalesTaxRate({payload}) {
if (response.error) {
yield call(updateTaxes, form, null);
handleError(response.message);
yield !goBack && call(navigateToAddressScreen, payload, type, address);
yield !goBack &&
call(navigateToAddressScreen, payload, type, address, addressType);
}

if (goBack) {
Expand All @@ -145,7 +146,8 @@ function* fetchSalesTaxRate({payload}) {
} catch (e) {
yield call(updateTaxes, form, null);
handleError(e);
yield !goBack && call(navigateToAddressScreen, payload, type, address);
yield !goBack &&
call(navigateToAddressScreen, payload, type, address, addressType);
} finally {
yield put(spinner('isSaving', false));
}
Expand Down

0 comments on commit d198504

Please sign in to comment.