Ignore:
Timestamp:
Jan 3, 2015, 5:55:34 PM (10 years ago)
Author:
chronos
Message:
  • Modified: Invoice negative values replaced by Direction column which is used to determine if invoice is in or out invoice.
File:
1 edited

Legend:

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

    r719 r720  
    5050  {
    5151    if(!$this->System->User->CheckPermission('Finance', 'Manage')) return('Nemáte oprávnění');
    52     $SQL = 'SELECT Member.*, MemberPayment.MonthlyTotal AS Monthly, '.
    53       'MemberPayment.Cash AS Cash, '.
    54       '(SELECT GROUP_CONCAT(Service.Name) FROM ServiceCustomerRel LEFT JOIN Service '.
    55       'ON Service.Id=ServiceCustomerRel.Service WHERE ServiceCustomerRel.Customer=Member.Id AND '.
    56       'ServiceCustomerRel.Action IS NULL) AS ServicesNextMonth, '.
    57       'UNIX_TIMESTAMP(Member.BillingPeriodLastDate) AS LastDate, Subject.Name AS SubjectName, '.
    58       'FinanceBillingPeriod.Name AS BillingPeriodName '.
    59       'FROM MemberPayment JOIN Member ON Member.Id=MemberPayment.Member JOIN Subject '.
    60       '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)';
     52    $SQL = 'SELECT `Member`.*, `MemberPayment`.`MonthlyTotal` AS `Monthly`, '.
     53      '`MemberPayment`.`Cash` AS `Cash`, '.
     54      '(SELECT GROUP_CONCAT(`Service`.`Name`) FROM `ServiceCustomerRel` LEFT JOIN `Service` '.
     55      'ON `Service`.`Id`=`ServiceCustomerRel`.`Service` WHERE `ServiceCustomerRel`.`Customer`=`Member`.`Id` AND '.
     56      '`ServiceCustomerRel`.`Action` IS NULL) AS `ServicesNextMonth`, '.
     57      'UNIX_TIMESTAMP(`Member`.`BillingPeriodLastDate`) AS `LastDate`, `Subject`.`Name` AS `SubjectName`, '.
     58      '`FinanceBillingPeriod`.`Name` AS `BillingPeriodName` '.
     59      'FROM `MemberPayment` JOIN `Member` ON `Member`.`Id`=`MemberPayment`.`Member` JOIN `Subject` '.
     60      '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)';
    6363
    6464    $DbResult = $this->Database->query('SELECT COUNT(*) FROM ('.$SQL.') AS T');
     
    100100  }
    101101
    102   function InsertInvoice($Subject, $TimeCreation, $TimeDue, $Items,
     102  function InsertInvoice($Subject, $TimeCreation, $TimeDue, $Direction, $Items,
    103103  $DocumentLine, $PeriodFrom, $PeriodTo)
    104104  {
     
    109109    $SumValue = 0;
    110110    foreach($Items as $Item)
    111         $SumValue = $SumValue + ceil($Item['Price'] * $Item['Quantity']);
     111      $SumValue = $SumValue + ceil($Item['Price'] * $Item['Quantity']);   
    112112    $this->Database->insert('FinanceInvoice', array(
    113           'Subject' => $Subject, 'Time' => TimeToMysqlDateTime($TimeCreation),
    114           'TimeDue' => TimeToMysqlDateTime($TimeDue), 'Value' => $SumValue, 'BillCode' => $BillCode,
    115           'PeriodFrom' => TimeToMysqlDate($PeriodFrom), 'PeriodTo' => TimeToMysqlDate($PeriodTo),
    116           'Generate' => 1, 'DocumentLine' => $DocumentLine));
     113      'Subject' => $Subject, 'Time' => TimeToMysqlDateTime($TimeCreation),
     114      'TimeDue' => TimeToMysqlDateTime($TimeDue), 'Value' => $SumValue,
     115        'Direction' => $Direction, 'BillCode' => $BillCode,
     116      'PeriodFrom' => TimeToMysqlDate($PeriodFrom), 'PeriodTo' => TimeToMysqlDate($PeriodTo),
     117      'Generate' => 1, 'DocumentLine' => $DocumentLine));
    117118    $InvoiceId = $this->Database->insert_id;
    118119    foreach($Items as $Item)
     
    130131
    131132    // Generuj účetní položky
    132     $DbResult = $this->Database->query('SELECT Member.*, MemberPayment.MonthlyTotal AS MonthlyTotal, '.
    133       'UNIX_TIMESTAMP(Member.BillingPeriodLastDate), Subject.Name AS SubjectName,'.
    134       'MemberPayment.MonthlyPlus AS MonthlyPlus '.
    135       'FROM MemberPayment JOIN Member ON Member.Id=MemberPayment.Member '.
    136       'JOIN Subject ON Subject.Id=Member.Subject');
     133    $DbResult = $this->Database->query('SELECT `Member`.*, `MemberPayment`.`MonthlyTotal` AS `MonthlyTotal`, '.
     134      'UNIX_TIMESTAMP(`Member`.`BillingPeriodLastDate`), `Subject`.`Name` AS `SubjectName`,'.
     135      '`MemberPayment`.`MonthlyPlus` AS `MonthlyPlus` '.
     136      'FROM `MemberPayment` JOIN `Member` ON `Member`.`Id`=`MemberPayment`.`Member` '.
     137      'JOIN `Subject` ON `Subject`.`Id`=`Member`.`Subject`');
    137138    while($Member = $DbResult->fetch_assoc())
    138139    {
     
    163164        }
    164165        $PayPerPeriod = $MonthlyTotal * $Period['MonthCount'];
     166        // We can't produce negative invoice except storno invoice.
     167        // TODO: In case of negative invoice it is not sufficient to reverse invoicing direction 
     168        // Other subject should invoice only possitive items. Negative items should be somehow removed.
     169        if($MonthlyTotal >= 0)
     170                $Direction = 1; // Standard out invoice
     171          else {
     172                $Direction = -1; // In case of negative total value generate reverse invoice for other subject
     173                foreach($InvoiceItems as $Index => $Item)
     174                        $InvoiceItems[$Index]['Price'] = -$Item['Price'];
     175          }
     176       
     177       
    165178        if($PayPerPeriod != 0)
    166179        {
     
    168181          $Output .= $TimePeriodText.': '.$MonthlyTotal.' * '.$Period['MonthCount'].' = '.$PayPerPeriod.'<br />';
    169182          $this->InsertInvoice($Member['Subject'], time(), time() + 3600 * 24 * INVOICE_DUE_DAYS,
    170             $InvoiceItems, DOC_LINE_INVOICE_OUT, $Period['From'], $Period['To']);
     183            $Direction, $InvoiceItems, DOC_LINE_INVOICE_OUT, $Period['From'], $Period['To']);
    171184
    172185          $Output .= $this->SendPaymentEmail($Member['Id']);
     
    227240    $Output .= $Finance->LoadTariffs();
    228241    $Output .= $Finance->RecalculateMemberPayment();
    229 
    230     // Celkovy prehled
    231     $DbResult = $this->Database->query('SELECT SUM(Product.SellPrice) AS Price FROM StockSerialNumber '.
    232       'JOIN Product ON StockSerialNumber.Product = Product.Id WHERE (StockSerialNumber.TimeElimination IS NULL) AND (StockSerialNumber.Segment IS NOT NULL)');
    233     $Row = $DbResult->fetch_row();
    234     $TotalDeviceCost = $Row[0];
    235     $Output .= 'Celková cena zařízení: Zařízení('.$TotalDeviceCost;
    236242
    237243    $DbResult = $this->Database->query('SELECT SUM(NetworkDevice) FROM MemberPayment');
     
    279285      $Output .= 'Aktualizuji služby....<br>';
    280286      $this->TableUpdatePeriod('Service');
     287     
    281288      // Update customer service selections
    282289      $Output .= 'Aktualizuji výběr služeb zákazníků....<br>';
     
    343350        '<th style="border-style: solid; border-width: 1px; padding: 1px 5px 1px 5px; text-align: center; font-weight: bold;">Částka [Kč]</th></tr>'."\n";
    344351      $DbResult = $this->Database->query('SELECT T1.* FROM ((SELECT Text, Time, (Value*Direction) AS Value, File FROM FinanceOperation WHERE (Subject='.$Member['Subject'].')) UNION ALL '.
    345         '(SELECT CONCAT(`Text`, (SELECT GROUP_CONCAT(`Description` SEPARATOR "<br/>") FROM `FinanceInvoiceItem` WHERE `FinanceInvoiceItem`.`FinanceInvoice` = `FinanceInvoice`.`Id`)) AS `Text`, Time, -Value as Value, File FROM FinanceInvoice WHERE (Subject='.
     352        '(SELECT CONCAT(`Text`, (SELECT GROUP_CONCAT(`Description` SEPARATOR "<br/>") FROM `FinanceInvoiceItem` WHERE `FinanceInvoiceItem`.`FinanceInvoice` = `FinanceInvoice`.`Id`)) AS `Text`, Time, -(Value*Direction) as Value, File FROM FinanceInvoice WHERE (Subject='.
    346353        $Member['Subject'].')) ORDER BY Time DESC) AS T1 WHERE (T1.Time > "'.$Member['BillingPeriodLastDate'].'")');
    347354      while($DbRow = $DbResult->fetch_assoc())
Note: See TracChangeset for help on using the changeset viewer.