Skip to content

Commit

Permalink
insurance charts
Browse files Browse the repository at this point in the history
  • Loading branch information
Cuttlas90 committed Jul 22, 2024
1 parent 136dd61 commit 2d1afbc
Show file tree
Hide file tree
Showing 2 changed files with 186 additions and 96 deletions.
238 changes: 142 additions & 96 deletions pages/helper/quarterly_chart.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,102 +12,148 @@ def add_quartely_charts(selected_stock, dollar_toggle):
"""get data and add monthly charts"""
queries = Queries(selected_stock["name"])
if selected_stock["cSecValReal"] in [39, 56, 90]:
st.header('درآمدهای عملیاتی و سود', divider='rainbow')

error, stock_data = vasahm_query(queries.get_quarterly_investment_sell_and_profit(dollar=dollar_toggle))
if error:
st.error(stock_data, icon="🚨")
else:
stock_data_history = pd.DataFrame(stock_data, columns=["row_title",
"value",
"end_to_period"])
if len(stock_data) > 0:
st.header('درآمدهای عملیاتی و سود', divider='rainbow')
stock_data_history = pd.DataFrame(stock_data, columns=["row_title",
"value",
"end_to_period"])

stock_data_history["end_to_period"] = stock_data_history["end_to_period"].astype(str)
# specify the type of selection, here single selection is used
chart2 = alt.Chart(stock_data_history).mark_area(opacity=0.3).encode(
alt.Color('row_title:N', title="سرفصلها"),
alt.Y('value:Q', title="مبلغ (میلیون)").stack(None),
alt.X('end_to_period:N',title="تاریخ")
)
stock_data_history["end_to_period"] = stock_data_history["end_to_period"].astype(str)
# specify the type of selection, here single selection is used
chart2 = alt.Chart(stock_data_history).mark_area(opacity=0.3).encode(
alt.Color('row_title:N', title="سرفصلها"),
alt.Y('value:Q', title="مبلغ (میلیون)").stack(None),
alt.X('end_to_period:N',title="تاریخ")
)

st.altair_chart(chart2, use_container_width=True)
st.altair_chart(chart2, use_container_width=True)

st.header('حاشیه سود خالص', divider='rainbow')

error, stock_data = vasahm_query(queries.get_quarterly_investment_profit_ratio())
if error:
st.error(stock_data, icon="🚨")
else:
stock_data_history = pd.DataFrame(stock_data, columns=["row_title",
"value",
"end_to_period"])
stock_data_history["end_to_period"] = stock_data_history["end_to_period"].astype(str)
pivot_df = stock_data_history.pivot_table(index='end_to_period',
columns='row_title',
values='value',
aggfunc='sum').reset_index()
pivot_df["profit_ratio"] = (pivot_df["سود(زیان) خالص"].astype(float)
/pivot_df["جمع درآمدهای عملیاتی"].astype(float))

chart_product = alt.Chart(pivot_df,
height=600).mark_line().encode(
alt.X('end_to_period:N', title='تاریخ'),
alt.Y('profit_ratio:Q', title="میزان عمکرد").axis(format='%'),
# alt.Color('column_name:N', title='دسته ها'),
)
st.altair_chart(chart_product, use_container_width=True)
if len(stock_data) > 0:
st.header('حاشیه سود خالص', divider='rainbow')
stock_data_history = pd.DataFrame(stock_data, columns=["row_title",
"value",
"end_to_period"])
stock_data_history["end_to_period"] = stock_data_history["end_to_period"].astype(str)
pivot_df = stock_data_history.pivot_table(index='end_to_period',
columns='row_title',
values='value',
aggfunc='sum').reset_index()
pivot_df["profit_ratio"] = (pivot_df["سود(زیان) خالص"].astype(float)
/pivot_df["جمع درآمدهای عملیاتی"].astype(float))

chart_product = alt.Chart(pivot_df).mark_line().encode(
alt.X('end_to_period:N', title='تاریخ'),
alt.Y('profit_ratio:Q', title="میزان عمکرد").axis(format='%'),
# alt.Color('column_name:N', title='دسته ها'),
)
st.altair_chart(chart_product, use_container_width=True)

elif selected_stock["cSecValReal"] in [57]:
st.header('درآمدهای عملیاتی و سود', divider='rainbow')

error, stock_data = vasahm_query(queries.get_quarterly_banking_sell_and_profit(dollar=dollar_toggle))
if error:
st.error(stock_data, icon="🚨")
else:
stock_data_history = pd.DataFrame(stock_data, columns=["row_title",
"value",
"end_to_period"])
if len(stock_data) > 0:
st.header('درآمدهای عملیاتی و سود', divider='rainbow')
stock_data_history = pd.DataFrame(stock_data, columns=["row_title",
"value",
"end_to_period"])

stock_data_history["end_to_period"] = stock_data_history["end_to_period"].astype(str)
# specify the type of selection, here single selection is used
chart2 = alt.Chart(stock_data_history).mark_area(opacity=0.3).encode(
alt.Color('row_title:N', title="سرفصلها"),
alt.Y('value:Q', title="مبلغ (میلیون)").stack(None),
alt.X('end_to_period:N',title="تاریخ")
)
stock_data_history["end_to_period"] = stock_data_history["end_to_period"].astype(str)
# specify the type of selection, here single selection is used
chart2 = alt.Chart(stock_data_history).mark_area(opacity=0.3).encode(
alt.Color('row_title:N', title="سرفصلها"),
alt.Y('value:Q', title="مبلغ (میلیون)").stack(None),
alt.X('end_to_period:N',title="تاریخ")
)

st.altair_chart(chart2, use_container_width=True)
st.altair_chart(chart2, use_container_width=True)

st.header('حاشیه سود خالص', divider='rainbow')

error, stock_data = vasahm_query(queries.get_quarterly_banking_profit_ratio())
if error:
st.error(stock_data, icon="🚨")
else:
stock_data_history = pd.DataFrame(stock_data, columns=["row_title",
"value",
"end_to_period"])
stock_data_history["end_to_period"] = stock_data_history["end_to_period"].astype(str)
pivot_df = stock_data_history.pivot_table(index='end_to_period',
columns='row_title',
values='value',
aggfunc='sum').reset_index()
pivot_df["profit_ratio"] = (pivot_df["سود(زیان) خالص"].astype(float)
/pivot_df["جمع درآمدهای عملیاتی"].astype(float))

chart_product = alt.Chart(pivot_df,
height=600).mark_line().encode(
alt.X('end_to_period:N', title='تاریخ'),
alt.Y('profit_ratio:Q', title="میزان عمکرد").axis(format='%'),
# alt.Color('column_name:N', title='دسته ها'),
)
st.altair_chart(chart_product, use_container_width=True)
if len(stock_data) > 0:
st.header('حاشیه سود خالص', divider='rainbow')
stock_data_history = pd.DataFrame(stock_data, columns=["row_title",
"value",
"end_to_period"])
stock_data_history["end_to_period"] = stock_data_history["end_to_period"].astype(str)
pivot_df = stock_data_history.pivot_table(index='end_to_period',
columns='row_title',
values='value',
aggfunc='sum').reset_index()
pivot_df["profit_ratio"] = (pivot_df["سود(زیان) خالص"].astype(float)
/pivot_df["جمع درآمدهای عملیاتی"].astype(float))

chart_product = alt.Chart(pivot_df).mark_line().encode(
alt.X('end_to_period:N', title='تاریخ'),
alt.Y('profit_ratio:Q', title="میزان عمکرد").axis(format='%'),
# alt.Color('column_name:N', title='دسته ها'),
)
st.altair_chart(chart_product, use_container_width=True)

# elif selected_stock["cSecValReal"] in [58]:
# pass

# elif selected_stock["cSecValReal"] in [66]:
# pass
elif selected_stock["cSecValReal"] in [66]:
error, stock_data = vasahm_query(queries.get_quarterly_insurance_sell_and_profit(dollar=dollar_toggle))
if error:
st.error(stock_data, icon="🚨")
else:
if len(stock_data) > 0:
st.header('درآمدهای عملیاتی و سود', divider='rainbow')
stock_data_history = pd.DataFrame(stock_data, columns=["row_title",
"value",
"end_to_period"])

stock_data_history["end_to_period"] = stock_data_history["end_to_period"].astype(str)
# specify the type of selection, here single selection is used
chart2 = alt.Chart(stock_data_history).mark_area(opacity=0.3).encode(
alt.Color('row_title:N', title="سرفصلها"),
alt.Y('value:Q', title="مبلغ (میلیون)").stack(None),
alt.X('end_to_period:N',title="تاریخ")
)

st.altair_chart(chart2, use_container_width=True)


error, stock_data = vasahm_query(queries.get_quarterly_insurance_profit_ratio())
if error:
st.error(stock_data, icon="🚨")
else:
if len(stock_data) > 0:
st.header('حاشیه سود خالص', divider='rainbow')
stock_data_history = pd.DataFrame(stock_data, columns=["row_title",
"value",
"end_to_period"])
stock_data_history["end_to_period"] = stock_data_history["end_to_period"].astype(str)
pivot_df = stock_data_history.pivot_table(index='end_to_period',
columns='row_title',
values='value',
aggfunc='sum').reset_index()
pivot_df["profit_ratio"] = (pivot_df["سود(زیان) خالص"].astype(float)
/pivot_df["درآمدهای بیمه ای"].astype(float))

chart_product = alt.Chart(pivot_df).mark_line().encode(
alt.X('end_to_period:N', title='تاریخ'),
alt.Y('profit_ratio:Q', title="میزان عمکرد").axis(format='%'),
# alt.Color('column_name:N', title='دسته ها'),
)
st.altair_chart(chart_product, use_container_width=True)

# elif selected_stock["cSecValReal"] in [67]:
# pass
# for current report support by normal
Expand All @@ -119,47 +165,47 @@ def add_quartely_charts(selected_stock, dollar_toggle):
# pass
else:

st.header('درآمدهای عملیاتی و سود', divider='rainbow')

error, stock_data = vasahm_query(queries.get_quarterly_sell_and_profit(dollar=dollar_toggle))
if error:
st.error(stock_data, icon="🚨")
else:
stock_data_history = pd.DataFrame(stock_data, columns=["row_title",
"value",
"end_to_period"])
if len(stock_data) > 0:
st.header('درآمدهای عملیاتی و سود', divider='rainbow')
stock_data_history = pd.DataFrame(stock_data, columns=["row_title",
"value",
"end_to_period"])

stock_data_history["end_to_period"] = stock_data_history["end_to_period"].astype(str)
# specify the type of selection, here single selection is used
chart2 = alt.Chart(stock_data_history).mark_area(opacity=0.3).encode(
alt.Color('row_title:N', title="سرفصلها"),
alt.Y('value:Q', title="مبلغ (میلیون)").stack(None),
alt.X('end_to_period:N',title="تاریخ")
)
stock_data_history["end_to_period"] = stock_data_history["end_to_period"].astype(str)
# specify the type of selection, here single selection is used
chart2 = alt.Chart(stock_data_history).mark_area(opacity=0.3).encode(
alt.Color('row_title:N', title="سرفصلها"),
alt.Y('value:Q', title="مبلغ (میلیون)").stack(None),
alt.X('end_to_period:N',title="تاریخ")
)

st.altair_chart(chart2, use_container_width=True)
st.altair_chart(chart2, use_container_width=True)

st.header('حاشیه سود خالص', divider='rainbow')

error, stock_data = vasahm_query(queries.get_quarterly_profit_ratio())
if error:
st.error(stock_data, icon="🚨")
else:
stock_data_history = pd.DataFrame(stock_data, columns=["row_title",
"value",
"end_to_period"])
stock_data_history["end_to_period"] = stock_data_history["end_to_period"].astype(str)
pivot_df = stock_data_history.pivot_table(index='end_to_period',
columns='row_title',
values='value',
aggfunc='sum').reset_index()
pivot_df["profit_ratio"] = (pivot_df["سود(زیان) خالص"].astype(float)
/pivot_df["درآمدهای عملیاتی"].astype(float))

chart_product = alt.Chart(pivot_df,
height=600).mark_line().encode(
alt.X('end_to_period:N', title='تاریخ'),
alt.Y('profit_ratio:Q', title="میزان عمکرد").axis(format='%'),
# alt.Color('column_name:N', title='دسته ها'),
)
st.altair_chart(chart_product, use_container_width=True)
if len(stock_data) > 0:
st.header('حاشیه سود خالص', divider='rainbow')
stock_data_history = pd.DataFrame(stock_data, columns=["row_title",
"value",
"end_to_period"])
stock_data_history["end_to_period"] = stock_data_history["end_to_period"].astype(str)
pivot_df = stock_data_history.pivot_table(index='end_to_period',
columns='row_title',
values='value',
aggfunc='sum').reset_index()
pivot_df["profit_ratio"] = (pivot_df["سود(زیان) خالص"].astype(float)
/pivot_df["درآمدهای عملیاتی"].astype(float))

chart_product = alt.Chart(pivot_df).mark_line().encode(
alt.X('end_to_period:N', title='تاریخ'),
alt.Y('profit_ratio:Q', title="میزان عمکرد").axis(format='%'),
# alt.Color('column_name:N', title='دسته ها'),
)
st.altair_chart(chart_product, use_container_width=True)
44 changes: 44 additions & 0 deletions pages/helper/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,28 @@ def get_quarterly_banking_sell_and_profit(self, dollar = False):
if dollar:
string = self._dollar_query(string)
return string

def get_quarterly_insurance_sell_and_profit(self, dollar = False):
"""return query to monthly sell value data"""
string = f"""select
row_title,
value,
end_to_period
from
public.all_data
INNER JOIN stocks ON public.all_data.stock_id = stocks.id
INNER JOIN report_list ON public.all_data.report_id = report_list.id
INNER JOIN table_code ON public.all_data.table_id = table_code.id
where
public.all_data.row_title IN ('سود (زیان) ناخالص فعالیتهای بیمه ای','درآمدهای بیمه ای','سود(زیان) خالص')
and stocks.name = '{self.name}'
and (public.report_list.\"letterCode\" = 'ن-۱۰')
and public.all_data.deleted = false
and table_code.sheet_id = 1
"""
if dollar:
string = self._dollar_query(string)
return string

def get_quarterly_profit_ratio(self, dollar = False):
"""return query to monthly sell value data"""
Expand Down Expand Up @@ -666,6 +688,28 @@ def get_quarterly_banking_profit_ratio(self, dollar = False):
if dollar:
string = self._dollar_query(string)
return string

def get_quarterly_insurance_profit_ratio(self, dollar = False):
"""return query to monthly sell value data"""
string = f"""select
row_title,
value,
end_to_period
from
public.all_data
INNER JOIN stocks ON public.all_data.stock_id = stocks.id
INNER JOIN report_list ON public.all_data.report_id = report_list.id
INNER JOIN table_code ON public.all_data.table_id = table_code.id
where
public.all_data.row_title IN ('سود (زیان) ناخالص فعالیتهای بیمه ای','درآمدهای بیمه ای','سود(زیان) خالص')
and stocks.name = '{self.name}'
and (public.report_list.\"letterCode\" = 'ن-۱۰')
and public.all_data.deleted = false
and table_code.sheet_id = 1
"""
if dollar:
string = self._dollar_query(string)
return string

def _dollar_query(self, text):
query_string = f"""WITH
Expand Down

0 comments on commit 2d1afbc

Please sign in to comment.