diff --git a/changelogs/unreleased/88462.json b/changelogs/unreleased/88462.json new file mode 100644 index 000000000..f3ef830a4 --- /dev/null +++ b/changelogs/unreleased/88462.json @@ -0,0 +1,5 @@ +{ + "title": "Data fetching: add active company management", + "type": "refactor", + "packages": "sale" +} diff --git a/packages/apps/sale/src/api/cart-api.js b/packages/apps/sale/src/api/cart-api.js index ca7a4cf15..35fff42a3 100644 --- a/packages/apps/sale/src/api/cart-api.js +++ b/packages/apps/sale/src/api/cart-api.js @@ -37,7 +37,7 @@ const createCartCriteria = (searchValue, userId) => { return criteria; }; -export async function searchCart({searchValue, page = 0, userId}) { +export async function searchCart({searchValue, page = 0, userId, companyId}) { return createStandardSearch({ model: 'com.axelor.apps.sale.db.Cart', criteria: createCartCriteria(searchValue, userId), @@ -45,6 +45,7 @@ export async function searchCart({searchValue, page = 0, userId}) { sortKey: 'sale_cart', page: page, provider: 'model', + companyId, }); } diff --git a/packages/apps/sale/src/api/customer-api.js b/packages/apps/sale/src/api/customer-api.js index fb7e58b79..324cc6311 100644 --- a/packages/apps/sale/src/api/customer-api.js +++ b/packages/apps/sale/src/api/customer-api.js @@ -92,14 +92,8 @@ export async function searchCustomer({ userId, categoryId, ), - ...(companyId != null && { - domain: ':company MEMBER OF self.companySet', - domainContext: { - company: { - id: companyId, - }, - }, - }), + companyId, + isCompanyM2M: true, fieldKey: 'sale_customer', sortKey: 'sale_customer', page, diff --git a/packages/apps/sale/src/api/customer-delivery-api.js b/packages/apps/sale/src/api/customer-delivery-api.js index 665734c4d..b622b22bd 100644 --- a/packages/apps/sale/src/api/customer-delivery-api.js +++ b/packages/apps/sale/src/api/customer-delivery-api.js @@ -18,7 +18,7 @@ import {createStandardSearch} from '@axelor/aos-mobile-core'; -export async function fetchCustomerDelivery({saleOrderId}) { +export async function fetchCustomerDelivery({saleOrderId, companyId}) { return createStandardSearch({ model: 'com.axelor.apps.stock.db.StockMove', domain: ':saleOrder MEMBER OF self.saleOrderSet', @@ -30,5 +30,6 @@ export async function fetchCustomerDelivery({saleOrderId}) { numberElementsByPage: 1, page: 0, provider: 'model', + companyId, }); } diff --git a/packages/apps/sale/src/api/product-api.js b/packages/apps/sale/src/api/product-api.js index a3b00af6d..6486081c9 100644 --- a/packages/apps/sale/src/api/product-api.js +++ b/packages/apps/sale/src/api/product-api.js @@ -123,11 +123,8 @@ const createProductCategoryCriteria = searchValue => { return [getSearchCriterias('sale_productCategory', searchValue)]; }; -const createProductCompanyCriteria = (companyId, productId) => { - return [ - {fieldName: 'company.id', operator: '=', value: companyId}, - {fieldName: 'product.id', operator: '=', value: productId}, - ]; +const createProductCompanyCriteria = productId => { + return [{fieldName: 'product.id', operator: '=', value: productId}]; }; const createVariantProductCriteria = (searchValue, parentProductId) => { @@ -194,11 +191,12 @@ export async function fetchProductCompanyConfig({companyId, productId}) { return createStandardSearch({ model: 'com.axelor.apps.base.db.ProductCompany', - criteria: createProductCompanyCriteria(companyId, productId), + criteria: createProductCompanyCriteria(productId), fieldKey: 'sale_productCompany', page: 0, numberElementsByPage: 1, provider: 'model', + companyId, }); } diff --git a/packages/apps/sale/src/api/sale-config-api.js b/packages/apps/sale/src/api/sale-config-api.js index 43e562ea1..39c1d873d 100644 --- a/packages/apps/sale/src/api/sale-config-api.js +++ b/packages/apps/sale/src/api/sale-config-api.js @@ -18,23 +18,14 @@ import {createStandardSearch} from '@axelor/aos-mobile-core'; -const createSaleConfigCriteria = companyId => { - return [ - { - fieldName: 'company.id', - operator: '=', - value: companyId, - }, - ]; -}; - export async function fetchSaleConfig({companyId}) { return createStandardSearch({ model: 'com.axelor.apps.sale.db.SaleConfig', - criteria: createSaleConfigCriteria(companyId), + criteria: [], fieldKey: 'sale_saleConfig', sortKey: 'sale_saleConfig', page: 0, provider: 'model', + companyId, }); } diff --git a/packages/apps/sale/src/api/sale-order-api.js b/packages/apps/sale/src/api/sale-order-api.js index 221bee941..a6255fc2d 100644 --- a/packages/apps/sale/src/api/sale-order-api.js +++ b/packages/apps/sale/src/api/sale-order-api.js @@ -54,6 +54,7 @@ export async function fetchSaleOrder({ statusList, customerId, page = 0, + companyId, }) { return createStandardSearch({ model: 'com.axelor.apps.sale.db.SaleOrder', @@ -62,6 +63,7 @@ export async function fetchSaleOrder({ sortKey: 'sale_saleOrder', page, provider: 'model', + companyId, }); } diff --git a/packages/apps/sale/src/components/atoms/SaleOrderBottomButton/SaleOrderBottomButton.tsx b/packages/apps/sale/src/components/atoms/SaleOrderBottomButton/SaleOrderBottomButton.tsx index aa0089b30..e8fb455f2 100644 --- a/packages/apps/sale/src/components/atoms/SaleOrderBottomButton/SaleOrderBottomButton.tsx +++ b/packages/apps/sale/src/components/atoms/SaleOrderBottomButton/SaleOrderBottomButton.tsx @@ -41,6 +41,7 @@ const SaleOrderBottomButton = ({saleOrder}: SaleOrderBottomButtonProps) => { const dispatch = useDispatch(); const {checkModule} = useModules(); + const {user} = useSelector(state => state.user); const {customerDelivery} = useSelector( (state: any) => state.sale_customerDelivery, ); @@ -59,8 +60,13 @@ const SaleOrderBottomButton = ({saleOrder}: SaleOrderBottomButtonProps) => { ); useEffect(() => { - dispatch((fetchCustomerDelivery as any)({saleOrderId: saleOrder.id})); - }, [dispatch, saleOrder]); + dispatch( + (fetchCustomerDelivery as any)({ + saleOrderId: saleOrder.id, + companyId: user.activeCompany?.id, + }), + ); + }, [dispatch, saleOrder, user.activeCompany?.id]); if (saleOrder.statusSelect === SaleOrder?.statusSelect.Finalized) { return ( diff --git a/packages/apps/sale/src/components/pages/SaleOrderListView/SaleOrderListView.tsx b/packages/apps/sale/src/components/pages/SaleOrderListView/SaleOrderListView.tsx index ee78f46c9..722a17202 100644 --- a/packages/apps/sale/src/components/pages/SaleOrderListView/SaleOrderListView.tsx +++ b/packages/apps/sale/src/components/pages/SaleOrderListView/SaleOrderListView.tsx @@ -45,6 +45,7 @@ const SaleOrderListView = ({ const {SaleOrder} = useTypes(); const {getSelectionItems} = useTypeHelpers(); + const {user} = useSelector(state => state.user); const {loading, moreLoading, isListEnd, saleOrderList} = useSelector( (state: any) => state.sale_saleOrder, ); @@ -73,8 +74,9 @@ const SaleOrderListView = ({ () => ({ statusList: statusList, customerId: customer?.id, + companyId: user.activeCompany?.id, }), - [customer?.id, statusList], + [customer?.id, statusList, user.activeCompany?.id], ); return ( diff --git a/packages/apps/sale/src/screens/cart/ActiveCartScreen.tsx b/packages/apps/sale/src/screens/cart/ActiveCartScreen.tsx index 02e8eafe3..632b27642 100644 --- a/packages/apps/sale/src/screens/cart/ActiveCartScreen.tsx +++ b/packages/apps/sale/src/screens/cart/ActiveCartScreen.tsx @@ -54,6 +54,7 @@ const ActiveCartScreen = ({}) => { const isFocused = useIsFocused(); const {userId} = useSelector((state: any) => state.auth); + const {user} = useSelector(state => state.user); const {mobileSettings} = useSelector((state: any) => state.appConfig); const {activeCart} = useSelector((state: any) => state.sale_cart); const {loading, moreLoading, isListEnd, carLineList} = useSelector( @@ -64,9 +65,14 @@ const ActiveCartScreen = ({}) => { useEffect(() => { if (isFocused) { - dispatch((fetchActiveCart as any)({userId})); + dispatch( + (fetchActiveCart as any)({ + userId: userId, + companyId: user.activeCompany?.id, + }), + ); } - }, [dispatch, isFocused, userId]); + }, [dispatch, isFocused, userId, user.activeCompany?.id]); const handleCartValidation = useCallback( (cart: any) => { diff --git a/packages/core/src/apiProviders/Standard/requests.helper.ts b/packages/core/src/apiProviders/Standard/requests.helper.ts index 8b86b4b60..97eea0bf2 100644 --- a/packages/core/src/apiProviders/Standard/requests.helper.ts +++ b/packages/core/src/apiProviders/Standard/requests.helper.ts @@ -104,7 +104,7 @@ class RequestBuilder { } if (domain != null && domain !== '') { - if (data._domain !== null) { + if (data._domain != null) { data._domain += ` AND ${domain}`; data._domainContext = { ...data._domainContext,