<?php
namespace controls;
class Finances
{
  public static function category_delete() {
    global $user;

    if ( !$user )
      return \controls\Users::login_form();

    if ( \factory\Finances::category_delete( \S::get( 'category_id' ) ) ) {
      \S::alert( 'Wybrana kategoria została usunięta.' );
      header( 'Location: /finances/main_view/' );
      exit;
    }
  }

  public static function operation_save()
  {
    global $user;

    if ( !$user )
      return \controls\Users::login_form();

    $response = [ 'status' => 'error', 'msg' => 'Podczas zapisywania operacji wystąpił błąd. Proszę spróbować ponownie.' ];
    $values   = \S::json_to_array( \S::get( 'values' ) );

    \S::set_session( 'operation-date', $values['date'] );

    if ( $id = \factory\Finances::operation_save(
            $values['id'], $values['category_id'], $values['date'], $values['amount'], $values['description'], $values['tags'], \S::get_session( 'finance-group' )
          ) )
      $response = [ 'status' => 'ok', 'msg' => 'Operacja została zapisana.', 'id' => $id ];

    echo json_encode( $response );
    exit;
  }

  public static function operation_delete()
  {
    global $user;

    if ( !$user )
      return \controls\Users::login_form();

    if ( \factory\Finances::operation_delete( \S::get( 'operation-id' ) ) )
    {
      \S::alert( 'Wybrana operacja została usunięta.' );
      header( 'Location: /finances/operations_list/category-id=' . \S::get( 'category-id' ) );
      exit;
    }
  }

  public static function operation_edit()
  {
    global $user;

    if ( !$user )
      return \controls\Users::login_form();

    return \Tpl::view( 'finances/operation-edit', [
              'operation' => \factory\Finances::operation_details( \S::get( 'operation-id' ) ),
              'category_id' => \S::get( 'category-id' ),
              'tags' => \factory\Finances::tags_list( \S::get_session( 'finance-group' ) ),
              'tags_json' => \factory\Finances::tags_json( \S::get_session( 'finance-group' ) ),
              'operation_date' => \S::get_session( 'operation-date' ),
              'clients' => \factory\Finances::clients_list()
            ] );
  }

  public static function category_save()
  {
    global $user;

    if ( !$user )
      return \controls\Users::login_form();

    $response = [ 'status' => 'error', 'msg' => 'Podczas zapisywania kateogrii wystąpił błąd. Proszę spróbować ponownie.' ];
    $values   = \S::json_to_array( \S::get( 'values' ) );

    if ( $id = \factory\Finances::category_save(
            $values['id'], $values['name'], $values['parent_id'], $values['group_id']
          ) )
      $response = [ 'status' => 'ok', 'msg' => 'Kategoria została zapisana.', 'id' => $id ];

    echo json_encode( $response );
    exit;
  }

  public static function category_edit()
  {
    global $user;

    if ( !$user )
      return \controls\Users::login_form();

    return \Tpl::view( 'finances/category-edit', [
              'category' => \factory\Finances::category_details( \S::get( 'id' ) ),
              'group_id' => \S::get_session( 'finance-group' )
            ] );
  }

  public static function operations_list()
  {
    global $user;

    if ( !$user )
      return \controls\Users::login_form();

    $dates = \S::get( 'dates' );

    if ( $dates )
      \S::set_session( 'finance-dates', $dates );

    $dates = \S::get_session( 'finance-dates' );
    $dates = explode( ' - ', $dates );

    $dates[0] ? $date_from = $dates[0] : $date_from = date( 'Y-m-01' );
    $dates[1] ? $date_to = $dates[1] : $date_to = date( 'Y-m-t' );

    $category = \factory\Finances::category_details( \S::get( 'category-id' ) );

    if ( $category['group_id'] == 3 )
    {
      $date_from = '2000-01-01';
      $date_to = '3000-01-0';
    }

    return \Tpl::view( 'finances/operations-list', [
              'category' => $category,
              'operations' => \factory\Finances::operations( \S::get( 'category-id' ), $date_from, $date_to, \S::get_session( 'finance-tag-id' ) ),
              'date_from' => $date_from,
              'date_to' => $date_to
            ] );
  }

  public static function main_view()
  {
    global $user;

    if ( !$user )
      return \controls\Users::login_form();

    if ( \S::get( 'tag-clear' ) )
      unset( $_SESSION['finance-tag-id'] );

    if ( \S::get( 'client_clear' ) )
      unset( $_SESSION['finance_client_id'] );

    if ( \S::get( 'dates' ) )
      \S::set_session( 'finance-dates', \S::get( 'dates' ) );

    if ( \S::get( 'tag-id' ) )
      \S::set_session( 'finance-tag-id', \S::get( 'tag-id' ) );

    if ( \S::get( 'client_id' ) )
      \S::set_session( 'finance_client_id', \S::get( 'client_id' ) );

    if ( \S::get( 'group-id' ) )
      \S::set_session( 'finance-group', \S::get( 'group-id' ) );

    if ( !$group_id = \S::get_session( 'finance-group' ) )
    {
      $group_id = \factory\Finances::default_group();
      \S::set_session( 'finance-group', $group_id );
    }

    $tag_id = \S::get_session( 'finance-tag-id' );
    $client_id = \S::get_session( 'finance_client_id' );
    $dates = \S::get_session( 'finance-dates' );
    $dates = explode( '_-_', $dates );

    $dates[0] ? $date_from = $dates[0] : $date_from = $date_from = date( 'Y-m-01' );
    $dates[1] ? $date_to = $dates[1] : $date_to = date( 'Y-m-t' );

    if ( $group_id == 3 )
    {
      $date_from = '2000-01-01';
      $date_to = '3000-01-0';
    }

    return \Tpl::view( 'finances/main-view', [
              'categories' => \factory\Finances::categories( $date_from, $date_to, $tag_id, null, $group_id ),
              'date_from' => $date_from,
              'date_to' => $date_to,
              'tags' => \factory\Finances::tags_list( $group_id ),
              'tag_id' => $tag_id,
              'groups' => \factory\Finances::groups_list(),
              'group_id' => $group_id,
              'operations_list' => \factory\Finances::operations_list( $date_from, $date_to, $group_id ),
              'wallet_summary' => \factory\Finances::wallet_summary( $group_id ),
              'wallet_summary_this_month' => \factory\Finances::wallet_summary_this_month( $group_id ),
              'wallet_income_this_month' => \factory\Finances::wallet_income_this_month( $group_id ),
              'wallet_expenses_this_month' => \factory\Finances::wallet_expenses_this_month( $group_id )
            ] );
  }
}
