Skip to content

Commit

Permalink
fix[CU-1qeukn]: wrong data in manager stats & invoice
Browse files Browse the repository at this point in the history
Signed-off-by: Shivansh Saini <shivanshs9@gmail.com>
  • Loading branch information
shivanshs9 committed Dec 31, 2020
1 parent 27672cb commit ab07085
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,58 +17,53 @@ class ShopInvoiceViewModel(application: Application) : BaseViewModel(application
private var mPrevStatsResults: LiveData<Resource<RestaurantAdminStatsModel>>? = null
private val mStatsResults = createNetworkLiveData<RestaurantAdminStatsModel>()

val restaurantSessions: LiveData<Resource<List<RestaurantSessionModel>>>
get() = mResults

val restaurantStats: LiveData<Resource<RestaurantAdminStatsModel>>
get() = mStatsResults
val restaurantSessions: LiveData<Resource<List<RestaurantSessionModel>>> = mResults
val restaurantStats: LiveData<Resource<RestaurantAdminStatsModel>> = mStatsResults

var doFetchStats: Boolean = false

fun fetchShopSessions(restaurantId: Long) {
fun fetchShopSessions(restaurantId: Long = mShopPk) {
this.mShopPk = restaurantId
mPrevResults = mShopInvoiceRepository.getRestaurantSessions(restaurantId)
mResults.addSource(mPrevResults!!) { mResults.value = it }
if (mPrevResults != null)
mResults.removeSource(mPrevResults!!)
mPrevResults = if (fromDate == null && toDate == null) mShopInvoiceRepository.getRestaurantSessions(restaurantId)
else mShopInvoiceRepository.getRestaurantSessions(restaurantId, fromDate, toDate)
mResults.addSource(mPrevResults!!, mResults::setValue)
}

fun fetchShopStats(restaurantId: Long) {
this.mShopPk = restaurantId
mPrevStatsResults = mShopInvoiceRepository.getRestaurantAdminStats(restaurantId)
mStatsResults.addSource(mPrevStatsResults!!) { mStatsResults.value = it }
}

fun setShopPk(restaurantId: Long) {
this.mShopPk = restaurantId
}

private fun filterRestaurantSessions(fromDate: String?, toDate: String?) {
if (mPrevResults != null)
mResults.removeSource(mPrevResults!!)
mPrevResults = mShopInvoiceRepository.getRestaurantSessions(mShopPk, fromDate, toDate)
mResults.addSource(mPrevResults!!) { mResults.value = it }
mStatsResults.addSource(mPrevStatsResults!!, mStatsResults::setValue)
}

private fun filterRestaurantStats(fromDate: String?, toDate: String?) {
if (mPrevStatsResults != null)
mStatsResults.removeSource(mPrevStatsResults!!)
mPrevStatsResults = mShopInvoiceRepository.getRestaurantAdminStats(mShopPk, fromDate, toDate)
mStatsResults.addSource(mPrevStatsResults!!) { mStatsResults.value = it }
mStatsResults.addSource(mPrevStatsResults!!, mStatsResults::setValue)
}

fun filterFrom(fromDate: String) {
@JvmOverloads
fun filterFrom(fromDate: String, doFetch: Boolean = true) {
this.fromDate = fromDate
filterRestaurantSessions(fromDate, toDate)
if (doFetchStats) filterRestaurantStats(fromDate, toDate)
if (doFetch) {
fetchShopSessions()
if (doFetchStats) filterRestaurantStats(fromDate, toDate)
}
}

fun filterTo(toDate: String) {
@JvmOverloads
fun filterTo(toDate: String, doFetch: Boolean = true) {
this.toDate = toDate
filterRestaurantSessions(fromDate, toDate)
if (doFetchStats) filterRestaurantStats(fromDate, toDate)
if (doFetch) {
fetchShopSessions()
if (doFetchStats) filterRestaurantStats(fromDate, toDate)
}
}

override fun updateResults() {
fetchShopSessions(mShopPk)
fetchShopSessions()
if (doFetchStats) fetchShopStats(mShopPk)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -262,13 +262,13 @@ interface WebApiService {
fun deleteRestaurantMember(@Path("shop_id") shopId: Long, @Path("user_id") userId: Long): Call<ObjectNode>

@GET("sessions/restaurants/{restaurant_id}/")
fun getRestaurantSessionsById(@Path("restaurant_id") restaurantId: Long, @Query("checked_in_after") checkedOutAfter: String?, @Query("checked_in_before") checkedOutBefore: String?): Call<List<RestaurantSessionModel>>
fun getRestaurantSessionsById(@Path("restaurant_id") restaurantId: Long, @Query("checked_out_after") checkedOutAfter: String?, @Query("checked_out_before") checkedOutBefore: String?): Call<List<RestaurantSessionModel>>

@GET("sessions/restaurants/{restaurant_id}/scheduled/")
fun getScheduledSessionsById(@Path("restaurant_id") restaurantId: Long): Call<List<ShopScheduledSessionModel>>

@GET("restaurants/{restaurant_id}/stats/admin")
fun getRestaurantAdminStats(@Path("restaurant_id") restaurantId: Long, @Query("checked_in_after") checkedOutAfter: String?, @Query("checked_in_before") checkedOutBefore: String?): Call<RestaurantAdminStatsModel>
fun getRestaurantAdminStats(@Path("restaurant_id") restaurantId: Long, @Query("checked_out_after") checkedOutAfter: String?, @Query("checked_out_before") checkedOutBefore: String?): Call<RestaurantAdminStatsModel>

@GET("sessions/manage/{session_id}/detail/")
fun getShopSessionDetailById(@Path("session_id") sessionId: Long): Call<ShopSessionDetailModel>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,11 @@ class ManagerWorkActivity : BaseAccountActivity(), LiveOrdersInteraction {

private fun setupObservers(shopId: Long) {
mViewModel.fetchRestaurantData(shopId)
Utils.getCurrentFormattedDateInvoice().also { currDate ->
mShopViewModel.filterFrom(currDate, doFetch = false)
mShopViewModel.filterTo(currDate, doFetch = false)
}
mShopViewModel.fetchShopSessions(shopId)
mShopViewModel.filterFrom(Utils.getCurrentFormattedDateInvoice())
mShopViewModel.filterTo(Utils.getCurrentFormattedDateInvoice())
}

override fun setLiveOrdersActivation(isActivated: Boolean) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ class ManagerInvoiceFragment : BaseFragment(), ShopInvoiceInteraction {
@BindView(R.id.rv_shop_invoice_sessions)
internal lateinit var rvSessions: RecyclerView


private val mAdapter: ShopInvoiceSessionAdapter by lazy {
ShopInvoiceSessionAdapter(this)
}
private val mViewModel: ShopInvoiceViewModel by activityViewModels()

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
initRefreshScreen(R.id.sr_manager_invoice)
setupUi()
Expand All @@ -49,9 +49,11 @@ class ManagerInvoiceFragment : BaseFragment(), ShopInvoiceInteraction {

override fun updateScreen() {
super.updateScreen()
mViewModel.filterFrom(Utils.getCurrentFormattedDateInvoice())
mViewModel.filterTo(Utils.getCurrentFormattedDateInvoice())
mViewModel.updateResults()
Utils.getCurrentFormattedDateInvoice().also { currDate ->
mViewModel.filterFrom(currDate, doFetch = false)
mViewModel.filterTo(currDate, doFetch = false)
}
mViewModel.fetchShopSessions()
}

override fun onClickSession(data: RestaurantSessionModel?) {
Expand All @@ -65,4 +67,4 @@ class ManagerInvoiceFragment : BaseFragment(), ShopInvoiceInteraction {
companion object {
fun newInstance() = ManagerInvoiceFragment()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class ManagerStatsFragment : BaseFragment() {
}
it.forEachIndexed { idx, data ->
statsMenuAnalysisItemRevenue {
id(idx)
id("trending_items.$idx")
data(data)
}
}
Expand All @@ -84,7 +84,7 @@ class ManagerStatsFragment : BaseFragment() {
}
it.forEachIndexed { idx, data ->
statsMenuAnalysisItemRevenue {
id(idx)
id("poor_items.$idx")
data(data)
}
}
Expand All @@ -98,7 +98,7 @@ class ManagerStatsFragment : BaseFragment() {
}
it.forEachIndexed { idx, data ->
statsMenuAnalysisGroupRevenue {
id(idx)
id("trending_groups.$idx")
index(idx + 1)
data(data)
}
Expand Down

0 comments on commit ab07085

Please sign in to comment.