Ignore:
Timestamp:
Jan 11, 2016, 12:06:08 AM (9 years ago)
Author:
chronos
Message:
  • Modified: Now billing period for invoice generation is calculated as aligned to year boundary and if period is not complete then only valid months are used.
  • Removed: ToDo list and list of task and bugs is available in Trac web system.
  • Modified: BillingPeriodNext was replaced by more general ChangeAction in Member table.
  • Added: Log new monthly finance recalculation report to log table.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Modules/Finance/Manage.php

    r773 r786  
    3939  }
    4040
     41  /* Get first day and last day of given billing period. Periods are aligned with year start/end. */
    4142  function GetBillingPeriod($Period)
    4243  {
     44    $Time = time();
     45    $Year = date('Y', $Time);
     46
    4347    $MonthCount = $this->System->Modules['Finance']->BillingPeriods[$Period]['MonthCount'];
    44     $PeriodFrom = mktime(0, 0, 0, date('n'), 1, date('Y'));
    45     $PeriodTo = mktime(0, 0, 0, date('n') + $MonthCount - 1, date('t', mktime(0, 0, 0, date('n') + $MonthCount - 1, 1, date('Y'))) , date('Y'));
     48    if($MonthCount <= 0) return(array('From' => NULL, 'To' => NULL, 'MonthCount' => 0));
     49    $MonthCurrent = date('n', $Time);
     50
     51    /* Get start and end of aligned period */
     52    $MonthFrom = floor(($MonthCurrent - 1) / $MonthCount) + 1;
     53    $MonthTo = $MonthFrom + $MonthCount - 1;
     54
     55    /* Use period from current month to end month so months before current month are cut out */
     56    $MonthCount = $MonthTo - $MonthCurrent + 1;
     57    $MonthFrom = $MonthCurrent;
     58
     59    /* Get first and last day of period */
     60    $PeriodFrom = mktime(0, 0, 0, $MonthFrom, 1, $Year);
     61    $PeriodTo = mktime(0, 0, 0, $MonthTo, date('t', mktime(0, 0, 0, $MonthTo, 1, $Year)), $Year);
     62
    4663    return(array('From' => $PeriodFrom, 'To' => $PeriodTo, 'MonthCount' => $MonthCount));
    4764  }
     
    5976      'FROM `MemberPayment` JOIN `Member` ON `Member`.`Id`=`MemberPayment`.`Member` JOIN `Subject` '.
    6077      'ON `Subject`.`Id`=`Member`.`Subject` LEFT JOIN `FinanceBillingPeriod` ON '.
    61       '`FinanceBillingPeriod`.`Id`=`Member`.`BillingPeriodNext` WHERE (`Member`.`Blocked` = 0)'.
    62       'AND (`Member`.`BillingPeriodNext` > 1) AND (`MemberPayment`.`MonthlyTotal` != 0)';
     78      '`FinanceBillingPeriod`.`Id`=`Member`.`BillingPeriod` WHERE (`Member`.`Blocked` = 0)'.
     79      'AND (`Member`.`BillingPeriod` > 1) AND (`MemberPayment`.`MonthlyTotal` != 0)';
    6380
    6481    $DbResult = $this->Database->query('SELECT COUNT(*) FROM ('.$SQL.') AS T');
     
    133150    $Output = '';
    134151
    135     // Generuj účetní položky
     152    // Produce accounting items
    136153    $DbResult = $this->Database->query('SELECT `Member`.*, `MemberPayment`.`MonthlyTotal` AS `MonthlyTotal`, '.
    137154      'UNIX_TIMESTAMP(`Member`.`BillingPeriodLastDate`) AS `BillingPeriodLastUnixTime`, `Subject`.`Name` AS `SubjectName`,'.
     
    142159    {
    143160      $Output .= $Member['SubjectName'].': ';
    144       $Period = $this->GetBillingPeriod($Member['BillingPeriodNext']);
    145       if($Period['From'] > $Member['BillingPeriodLastUnixTime'])
    146       {
    147         $this->Database->update('Member', 'Id='.$Member['Id'],
    148           array('BillingPeriod' => $Member['BillingPeriodNext']));
    149         $Member['BillingPeriod'] = $Member['BillingPeriodNext'];
    150       }
    151161      $Period = $this->GetBillingPeriod($Member['BillingPeriod']);
     162
     163      /* Check if need to produce new invoice for customer */
    152164      if(($Period['MonthCount'] > 0) and ($Member['Blocked'] == 0) and
    153165        ($Period['From'] > $Member['BillingPeriodLastUnixTime']))
     
    170182        // TODO: In case of negative invoice it is not sufficient to reverse invoicing direction
    171183        // Other subject should invoice only positive items. Negative items should be somehow removed.
    172         if($MonthlyTotal >= 0) {
     184        if($MonthlyTotal >= 0)
     185        {
    173186          $InvoiceGroupId = INVOICE_GROUP_OUT;
    174187        } else {
     
    178191        // Load invoice group
    179192        $FinanceGroup = $this->System->Modules['Finance']->GetFinanceGroupById($InvoiceGroupId, 'FinanceInvoiceGroup');
    180         foreach($InvoiceItems as $Index => $Item) {
     193        foreach($InvoiceItems as $Index => $Item)
     194        {
    181195          $InvoiceItems[$Index]['Price'] = $Item['Price'] * $FinanceGroup['ValueSign'];
    182196        }
     
    191205
    192206          $Output .= $this->SendPaymentEmail($Member['Id']);
    193         } else $Output .= '<br />';
    194         $this->Database->update('Member', 'Id='.$Member['Id'],
     207        }
     208        /* Update last billing day */
     209        $this->Database->update('Member', '`Id`='.$Member['Id'],
    195210          array('BillingPeriodLastDate' => TimeToMysqlDateTime($Period['To'])));
    196       } else $Output .= '<br />';
     211      }
     212      $Output .= '<br />';
    197213    }
    198214    return($Output);
     
    233249  function ProcessTableUpdates()
    234250  {
     251    // Update customers
     252    $Output = 'Měním zákazníky...<br>';
     253    $this->TableUpdateChanges('Member');
     254
    235255    // Update finance charge
    236256    $Output = 'Měním aktuální parametry sítě...<br>';
     
    288308    if($MonthCurrent != $MonthLast)
    289309    {
    290       $Output .= 'Odečítám měsíční poplatek...<br />';
     310      $Output .= 'Odečítám pravidelný poplatek...<br />';
    291311      $Output .= $this->ProduceInvoices();
    292312
     
    302322
    303323      $Finance->RecalculateMemberPayment();
    304       //CreateMonthlyOverallBill();
    305       //$Finance->RecalculateUsersFinance();
    306324
    307325      // Restart traffic shaping
     
    309327      //flush();
    310328      //$this->GenerateBills();
     329      $this->System->ModuleManager->Modules['Log']->NewRecord('Finance', 'ProcessMonthlyPayment', $Output);
    311330    }
    312331    return($Output);
     
    332351    $MainSubject = $DbResult->fetch_assoc();
    333352
    334     $Period = $this->GetBillingPeriod($Member['BillingPeriodNext']);
     353    $Period = $this->GetBillingPeriod($Member['BillingPeriod']);
    335354
    336355    $DbResult = $this->Database->query('SELECT `FinanceBankAccount`.*, '.
     
    382401      $Output = '';
    383402    } else $Output = 'Uživatel '.$User['Name'].' nemá email.';
     403    return $Output;
    384404  }
    385405
Note: See TracChangeset for help on using the changeset viewer.