Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 12 additions & 26 deletions src/api/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -1688,36 +1688,22 @@ export const apiGetAllRoutesAsync = async () => {


export const apiGetStoreIncomeAsync = async (storeId, from, to) => {
const fromDate = from.toISOString();
const toDate = to.toISOString();
const response = await axios.get(
`${baseApiUrl}/api/Admin/store/${storeId}/income`,
{ params: { from: fromDate, to: toDate } }
);
return response.data;
try {
const response = await axios.get(
`${baseApiUrl}/api/Admin/store/${storeId}/income`,
{
params: { from, to }
}
);
return response.data;
} catch (error) {
console.error(`❌ Error fetching store income for ID ${storeId}:`, error);
throw error;
}
};



export const apiGetAdminProfitAsync = async (from, to, storeIds) => {
const response = await axios.get(`${baseApiUrl}/api/loyalty/admin/profit`, {
params: {
from: from?.toISOString(),
to: to?.toISOString(),
storeIds,
},
paramsSerializer: (params) => {
const query = new URLSearchParams();
if (params.from) query.append('from', params.from);
if (params.to) query.append('to', params.to);
if (params.storeIds) {
params.storeIds.forEach((id) => query.append('storeIds', id));
}
return query.toString();
},
});
return response.data;
};

export const apiFetchDeliveryAddressByIdAsync = async (addressId) => {
const res = await axios.get(
Expand Down
26 changes: 9 additions & 17 deletions src/pages/AnalyticsPage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import {
apiFetchAdViewsAsync,
apiFetchAdConversionsAsync, // From HEAD, for ProductsSummary
apiGetStoreIncomeAsync,
apiGetAdminProfitAsync
apiGetMonthlyStoreRevenueAsync
} from '../api/api.js';
// format and parseISO were in develop but not used in the conflicting part, subMonths is used by both
import { subMonths, format, parseISO } from 'date-fns'; // Added format, parseISO from develop imports
Expand Down Expand Up @@ -328,32 +328,27 @@ const AnalyticsPage = () => {
calculatedProductsChange
);

const adsWithProfitResp = await apiFetchAdsWithProfitAsync();
const adsWithProfit = adsWithProfitResp?.data || [];

const noww= new Date();

const from = new Date(noww.getFullYear(), noww.getMonth(), 1);
const to = new Date(noww.getFullYear(), noww.getMonth() + 1, 0);

const earningsStats = await Promise.all(
stores.map(async (store) => {
try {
const income = await apiGetStoreIncomeAsync(store.id, from, to);
const income = await apiGetMonthlyStoreRevenueAsync(store.id);
console.log("ispis "+income.storeId+" total income "+income.totalIncome+" taxed income "+income.taxedIncome);// 👈 nova funkcija
return {
storeId: income.StoreId,
name: income.StoreName ?? store.name,
storeRevenue: income.TotalIncome ,
adminProfit: income.TaxedIncome,
storeId: income.storeId,
name: income.storeName,
storeRevenue: income.totalIncome,
adminProfit: income.taxedIncome,
taxRate: (store.tax) * 100,
};
} catch (err) {
console.error(`❌ Error fetching income for store ${store.id}`, err);
return {
storeId: store.id,
name: store.name,
storeRevenue: 0,
adminProfit: 0,
storeRevenue: 999,
adminProfit: 999,
taxRate: (store.tax) * 100,
};
}
Expand All @@ -362,9 +357,6 @@ const AnalyticsPage = () => {

setStoreStats(earningsStats);


const adminProfit = await apiGetAdminProfitAsync(from, to, stores.map(s => s.id));
setTotalAdminProfit(adminProfit);

// Other initial data if needed (orders, users - not directly used for KPIs in develop's version)
// const ordersData = await apiFetchOrdersAsync();
Expand Down