Ignore:
Timestamp:
Sep 21, 2015, 12:12:26 AM (9 years ago)
Author:
chronos
Message:

This solves problem with inconsistency between invoice and invoice items values and inconsistency between selected document line and incorrect positive or negative values.

File:
1 edited

Legend:

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

    r742 r747  
    109109  }
    110110
     111  function GetNextDocumentLineNumberId($Id, $FinanceYear = 0)
     112  {
     113    $Code = $this->GetNextDocumentLineNumber($Id, $FinanceYear);
     114    $this->Database->insert('DocumentLineCode', array('DocumentLine' => $Id, 'Name' => $Code));
     115    return $this->Database->insert_id;
     116  }
     117
    111118  function GetFinanceGroupById($Id, $Table)
    112119  {
     
    125132    while($Member = $DbResult->fetch_assoc())
    126133    {
    127       $DbResult2 = $this->Database->query('SELECT ((SELECT COALESCE(SUM(`Value` * `ValueSign`), 0) FROM `FinanceOperation` '.
    128           'WHERE `Subject`='.$Member['Subject'].') - (SELECT COALESCE(SUM(`Value` * `ValueSign`), 0) FROM `FinanceInvoice` '.
     134      $DbResult2 = $this->Database->query('SELECT ((SELECT COALESCE(SUM(`Value`), 0) FROM `FinanceOperation` '.
     135          'WHERE `Subject`='.$Member['Subject'].') - (SELECT COALESCE(SUM(`Value`), 0) FROM `FinanceInvoice` '.
    129136          'WHERE `Subject`='.$Member['Subject'].')) AS `Cash`');
    130137      $Cash = $DbResult2->fetch_row();
     
    220227        'Cash' => array('Type' => 'Boolean', 'Caption' => 'Hotově', 'Default' => ''),
    221228        'Taxable' => array('Type' => 'Boolean', 'Caption' => 'Zdanitelné', 'Default' => ''),
    222         'Direction' => array('Type' => 'TFinanceDirection', 'Caption' => 'Směr', 'Default' => '0'),
    223         'ValueSign' => array('Type' => 'TFinanceValueSign', 'Caption' => 'Znaménko', 'Default' => '1'),
    224         'Value' => array('Type' => 'Integer', 'Caption' => 'Částka', 'Default' => '0', 'Suffix' => 'Kč'),
     229        'Value' => array('Type' => 'Integer', 'Caption' => 'Částka absolutní', 'Default' => '0', 'Suffix' => 'Kč', 'ReadOnly' => true),
     230        'ValueUser' => array('Type' => 'Integer', 'Caption' => 'Částka', 'Default' => '0', 'Suffix' => 'Kč'),
    225231        'File' => array('Type' => 'TFile', 'Caption' => 'Doklad', 'Default' => '', 'Null' => true),
    226232        'Text' => array('Type' => 'String', 'Caption' => 'Popis', 'Default' => ''),
     
    232238      ),
    233239      'BeforeInsert' => array($this, 'BeforeInsertFinanceOperation'),
     240      'AfterInsert' => array($this, 'AfterInsertFinanceOperation'),
     241      'BeforeModify' => array($this, 'BeforeModifyFinanceOperation'),
    234242    ));
    235243
     
    243251    $this->System->FormManager->Classes['FinanceTreasuryIn']['Items']['Cash']['Filter'] = false;
    244252    $this->System->FormManager->Classes['FinanceTreasuryIn']['Items']['BankAccount']['Hidden'] = true;
     253    $this->System->FormManager->Classes['FinanceTreasuryIn']['Items']['Value']['Hidden'] = true;
    245254
    246255    $this->System->FormManager->RegisterClass('FinanceTreasuryOut', $this->System->FormManager->Classes['FinanceOperation']);
     
    253262    $this->System->FormManager->Classes['FinanceTreasuryIn']['Items']['Cash']['Filter'] = false;
    254263    $this->System->FormManager->Classes['FinanceTreasuryOut']['Items']['BankAccount']['Hidden'] = true;
     264    $this->System->FormManager->Classes['FinanceTreasuryOut']['Items']['Value']['Hidden'] = true;
    255265
    256266    $this->System->FormManager->RegisterClass('FinanceAccountIn', $this->System->FormManager->Classes['FinanceOperation']);
     
    263273    $this->System->FormManager->Classes['FinanceTreasuryIn']['Items']['Cash']['Filter'] = false;
    264274    $this->System->FormManager->Classes['FinanceAccountIn']['Items']['Treasury']['Hidden'] = true;
     275    $this->System->FormManager->Classes['FinanceAccountIn']['Items']['Value']['Hidden'] = true;
    265276
    266277    $this->System->FormManager->RegisterClass('FinanceAccountOut', $this->System->FormManager->Classes['FinanceOperation']);
     
    273284    $this->System->FormManager->Classes['FinanceTreasuryIn']['Items']['Cash']['Filter'] = false;
    274285    $this->System->FormManager->Classes['FinanceAccountOut']['Items']['Treasury']['Hidden'] = true;
     286    $this->System->FormManager->Classes['FinanceAccountOut']['Items']['Value']['Hidden'] = true;
    275287
    276288    $this->System->FormManager->RegisterClass('FinanceOperationGroup', array(
     
    311323        'TimeDue' => array('Type' => 'Date', 'Caption' => 'Čas splatnosti', 'Default' => ''),
    312324        'TimePayment' => array('Type' => 'Date', 'Caption' => 'Čas zaplacení', 'Default' => '', 'Null' => true),
    313         'Direction' => array('Type' => 'TFinanceDirection', 'Caption' => 'Směr', 'Default' => '0'),
    314         'ValueSign' => array('Type' => 'TFinanceValueSign', 'Caption' => 'Znaménko', 'Default' => '1'),
    315         'Value' => array('Type' => 'Integer', 'Caption' => 'Částka', 'Default' => '0', 'Suffix' => 'Kč'),
    316         'File' => array('Type' => 'TFile', 'Caption' => 'Doklad', 'Default' => '', 'Null' => true),
     325        'Value' => array('Type' => 'Integer', 'Caption' => 'Částka absolutní', 'Default' => '0', 'Suffix' => 'Kč', 'ReadOnly' => true),
     326        'ValueUser' => array('Type' => 'Integer', 'Caption' => 'Částka', 'Default' => '0', 'Suffix' => 'Kč', 'ReadOnly' => true,
     327          'SQL' => 'SELECT SUM(CEIL(`Price`*`Quantity`)) FROM `FinanceInvoiceItem` WHERE `FinanceInvoiceItem`.`FinanceInvoice`=#Id'),
    317328        'Generate' => array('Type' => 'Boolean', 'Caption' => 'Generovat', 'Default' => ''),
    318329        'PeriodFrom' => array('Type' => 'Date', 'Caption' => 'Období od', 'Default' => '', 'Null' => true),
     
    325336          'WHERE `FinanceInvoiceOperationRel`.`Invoice`=#Id)'),
    326337      ),
    327       'BeforeInsert' => array($this, 'BeforeInsertFinanceOperation'),
     338      'BeforeInsert' => array($this, 'BeforeInsertFinanceInvoice'),
     339      'AfterInsert' => array($this, 'AfterInsertFinanceInvoice'),
     340      'BeforeModify' => array($this, 'BeforeModifyFinanceInvoice'),
    328341    ));
    329342    $this->System->FormManager->RegisterClass('FinanceInvoiceIn', $this->System->FormManager->Classes['FinanceInvoice']);
     
    332345    $this->System->FormManager->Classes['FinanceInvoiceIn']['Items']['Group']['Hidden'] = true;
    333346    $this->System->FormManager->Classes['FinanceInvoiceIn']['Items']['Group']['Filter'] = true;
     347    $this->System->FormManager->Classes['FinanceInvoiceIn']['Items']['Value']['Hidden'] = true;
    334348
    335349    $this->System->FormManager->RegisterClass('FinanceInvoiceOut', $this->System->FormManager->Classes['FinanceInvoice']);
     
    338352    $this->System->FormManager->Classes['FinanceInvoiceOut']['Items']['Group']['Hidden'] = true;
    339353    $this->System->FormManager->Classes['FinanceInvoiceOut']['Items']['Group']['Filter'] = true;
     354    $this->System->FormManager->Classes['FinanceInvoiceOut']['Items']['Value']['Hidden'] = true;
    340355
    341356    $this->System->FormManager->RegisterClass('FinanceInvoiceGroup', array(
     
    346361        'DocumentLine' => array('Type' => 'TDocumentLine', 'Caption' => 'Dokladová řada', 'Default' => '0'),
    347362        'ValueSign' => array('Type' => 'TFinanceValueSign', 'Caption' => 'Znaménko hodnoty', 'Default' => '0'),
    348         'Direction' => array('Type' => 'TFinanceDirection', 'Caption' => 'Směr', 'Default' => '0'),
     363        'Direction' => array('Type' => 'TFinanceDirection', 'Caption' => 'Směr', 'Default' => '0'),
    349364      ),
    350365    ));
     
    377392          'ReadOnly' => true, 'SQL' => 'CEIL(`Price` * `Quantity`)'),
    378393      ),
     394      'AfterInsert' => array($this, 'AfterInsertFinanceInvoiceItem'),
     395      'AfterModify' => array($this, 'AfterModifyFinanceInvoiceItem'),
     396      'AfterDelete' => array($this, 'AfterModifyFinanceInvoiceItem'),
    379397    ));
    380398    $this->System->FormManager->RegisterClass('FinanceTreasury', array(
     
    386404        'TimeCreate' => array('Type' => 'Date', 'Caption' => 'Čas vytvoření', 'Default' => ''),
    387405        'State' => array('Type' => 'Float', 'Caption' => 'Stav', 'Default' => '',
    388           'ReadOnly' => true, 'Suffix' => 'Kč', 'SQL' => '(SELECT SUM(`FinanceOperation`.`Value` * `FinanceOperation`.`ValueSign`) FROM `FinanceOperation` '.
     406          'ReadOnly' => true, 'Suffix' => 'Kč', 'SQL' => '(SELECT SUM(`FinanceOperation`.`Value`) FROM `FinanceOperation` '.
    389407          'WHERE `FinanceOperation`.`Treasury`=#Id)'),
    390408        'Operations' => array('Type' => 'TFinanceOperationListTreasury', 'Caption' => 'Operace', 'Default' => ''),
     
    417435        'LastImportId' => array('Type' => 'String', 'Caption' => 'Id posledního importu', 'Default' => ''),
    418436        'State' => array('Type' => 'Float', 'Caption' => 'Stav', 'Default' => '',
    419           'ReadOnly' => true, 'Suffix' => 'Kč', 'SQL' => '(SELECT SUM(`FinanceOperation`.`Value` * `FinanceOperation`.`ValueSign`) FROM `FinanceOperation` '.
     437          'ReadOnly' => true, 'Suffix' => 'Kč', 'SQL' => '(SELECT SUM(`FinanceOperation`.`Value`) FROM `FinanceOperation` '.
    420438          'WHERE `FinanceOperation`.`BankAccount`=#Id)'),
    421439        'AutoImport' => array('Type' => 'Boolean', 'Caption' => 'Automaticky stahovat z banky', 'Default' => ''),
     
    551569      else $Year = date("Y", $Form->Values['ValidFrom']);
    552570    $FinanceGroup = $this->System->Modules['Finance']->GetFinanceGroupById($Form->Values['Group'], 'FinanceOperationGroup');
    553     $Form->Values['BillCode'] = $this->System->Modules['Finance']->GetNextDocumentLineNumber($FinanceGroup['DocumentLine'], $Year);
     571    $Form->Values['BillCode'] = $this->System->Modules['Finance']->GetNextDocumentLineNumberId($FinanceGroup['DocumentLine'], $Year);
     572    return($Form->Values);
     573  }
     574
     575  function AfterInsertFinanceOperation($Form, $Id)
     576  {
     577    $FinanceGroup = $this->System->Modules['Finance']->GetFinanceGroupById($Form->Values['Group'], 'FinanceOperationGroup');
     578    $this->Database->query('UPDATE `'.$Form->Definition['Table'].'` SET `Value`= '.
     579      ($Form->Values['ValueUser'] * $FinanceGroup['ValueSign']).' WHERE `Id`='.$Id);
     580    return($Form->Values);
     581  }
     582
     583  function BeforeModifyFinanceOperation($Form, $Id)
     584  {
     585    $FinanceGroup = $this->System->Modules['Finance']->GetFinanceGroupById($Form->Values['Group'], 'FinanceOperationGroup');
     586    $this->Database->query('UPDATE `'.$Form->Definition['Table'].'` SET `Value`= '.
     587      ($Form->Values['ValueUser'] * $FinanceGroup['ValueSign']).' WHERE `Id`='.$Id);
     588    return($Form->Values);
     589  }
     590
     591  function BeforeInsertFinanceInvoice($Form)
     592  {
     593    if(array_key_exists('Time', $Form->Values)) $Year = date("Y", $Form->Values['Time']);
     594      else $Year = date("Y", $Form->Values['ValidFrom']);
     595    $FinanceGroup = $this->System->Modules['Finance']->GetFinanceGroupById($Form->Values['Group'], 'FinanceInvoiceGroup');
     596    $Form->Values['BillCode'] = $this->System->Modules['Finance']->GetNextDocumentLineNumberId($FinanceGroup['DocumentLine'], $Year);
     597    return($Form->Values);
     598  }
     599
     600  function AfterInsertFinanceInvoice($Form, $Id)
     601  {
     602    $FinanceGroup = $this->System->Modules['Finance']->GetFinanceGroupById($Form->Values['Group'], 'FinanceInvoiceGroup');
     603    $DbResult = $this->Database->query(str_replace('#Id', $Id, $Form->Definition['Items']['ValueUser']['SQL']));
     604    $DbRow = $DbResult->fetch_row();
     605    $Sum = $DbRow[0];
     606
     607    $this->Database->query('UPDATE `'.$Form->Definition['Table'].'` SET `Value`= '.
     608      ($Sum * $FinanceGroup['ValueSign']).' WHERE `Id`='.$Id);
     609    return($Form->Values);
     610  }
     611
     612  function BeforeModifyFinanceInvoice($Form, $Id)
     613  {
     614    $FinanceGroup = $this->System->Modules['Finance']->GetFinanceGroupById($Form->Values['Group'], 'FinanceInvoiceGroup');
     615    $DbResult = $this->Database->query(str_replace('#Id', $Id, $Form->Definition['Items']['ValueUser']['SQL']));
     616    $DbRow = $DbResult->fetch_row();
     617    $Sum = $DbRow[0];
     618    $this->Database->query('UPDATE `'.$Form->Definition['Table'].'` SET `Value`= '.
     619      ($Sum * $FinanceGroup['ValueSign']).' WHERE `Id`='.$Id);
     620    return($Form->Values);
     621  }
     622
     623  function AfterInsertFinanceInvoiceItem($Form, $Id)
     624  {
     625    $ParentForm = new Form($this->System->FormManager);
     626    $ParentForm->SetClass('FinanceInvoice');
     627    $ParentForm->LoadValuesFromDatabase($Form->Values['FinanceInvoice']);
     628    $this->AfterInsertFinanceInvoice($ParentForm, $Form->Values['FinanceInvoice']);
     629    return($Form->Values);
     630  }
     631
     632  function AfterModifyFinanceInvoiceItem($Form, $Id)
     633  {
     634    $ParentForm = new Form($this->System->FormManager);
     635    $ParentForm->SetClass('FinanceInvoice');
     636    $ParentForm->LoadValuesFromDatabase($Form->Values['FinanceInvoice']);
     637    $this->BeforeModifyFinanceInvoice($ParentForm, $Form->Values['FinanceInvoice']);
    554638    return($Form->Values);
    555639  }
Note: See TracChangeset for help on using the changeset viewer.