Changeset 901 for trunk/Modules/Document
- Timestamp:
- Feb 17, 2021, 9:27:32 PM (4 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Modules/Document/Document.php
r899 r901 80 80 )); 81 81 } 82 83 static function Cast(Module $Module): ModuleDocument 84 { 85 if ($Module instanceof ModuleDocument) 86 { 87 return $Module; 88 } 89 throw new Exception('Expected ModuleDocument type but '.gettype($Module)); 90 } 91 92 function GetFinanceYear(int $Year): array 93 { 94 if ($Year == 0) 95 { 96 // Get latest year 97 $DbResult = $this->Database->select('FinanceYear', '*', '1 ORDER BY `Year` DESC LIMIT 1'); 98 } else $DbResult = $this->Database->select('FinanceYear', '*', '`Year`='.$Year); 99 if ($DbResult->num_rows == 0) 100 { 101 if ($Year == date('Y')) 102 { 103 $this->CreateFinanceYear($Year); 104 $DbResult = $this->Database->select('FinanceYear', '*', '`Year`='.$Year); 105 } else throw new Exception('Rok '.$Year.' nenalezen'); 106 } 107 $FinanceYear = $DbResult->fetch_assoc(); 108 if ($FinanceYear['Closed'] == 1) 109 throw new Exception('Rok '.$FinanceYear['Year'].' je již uzavřen. Nelze do něj přidávat položky.'); 110 return $FinanceYear; 111 } 112 113 function GetNextDocumentLineNumber(string $Id, int $FinanceYear = 0): string 114 { 115 $FinanceYear = $this->GetFinanceYear($FinanceYear); 116 117 $DbResult = $this->Database->query('SELECT `Shortcut`, `Id` FROM `DocumentLine` WHERE `Id`='.$Id); 118 $DocumentLine = $DbResult->fetch_assoc(); 119 120 $DbResult = $this->Database->query('SELECT * FROM `DocumentLineSequence` WHERE '. 121 '`DocumentLine`='.$Id.' AND `FinanceYear`='.$FinanceYear['Id']); 122 $Sequence = $DbResult->fetch_assoc(); 123 124 if ($Sequence['YearPrefix'] == 1) 125 { 126 $Result = $DocumentLine['Shortcut'].$Sequence['NextNumber'].'/'.$FinanceYear['Year']; 127 } else $Result = $DocumentLine['Shortcut'].$Sequence['NextNumber']; 128 129 $this->Database->query('UPDATE `DocumentLineSequence` SET `NextNumber` = `NextNumber` + 1 '. 130 'WHERE (`DocumentLine`='.$Id.') AND (`FinanceYear`='.$FinanceYear['Id'].')'); 131 return $Result; 132 } 133 134 function GetNextDocumentLineNumberId(string $Id, int $FinanceYear = 0): int 135 { 136 $Code = $this->GetNextDocumentLineNumber($Id, $FinanceYear); 137 $this->Database->insert('DocumentLineCode', array('DocumentLine' => $Id, 'Name' => $Code)); 138 return $this->Database->insert_id; 139 } 140 141 function CreateFinanceYear(int $Year) 142 { 143 $StartTime = mktime(0, 0, 0, 1, 1, $Year); 144 $EndTime = mktime(0, 0, 0, 12, 31, $Year); 145 $this->Database->insert('FinanceYear', array('Year' => $Year, 146 'DateStart' => TimeToMysqlDate($StartTime), 'DateEnd' => TimeToMysqlDate($EndTime), 'Closed' => 0)); 147 $YearId = $this->Database->insert_id; 148 149 // Create DocumentLineSequence from previous 150 $DbResult = $this->Database->select('DocumentLine', 'Id', '`Yearly` = 1'); 151 while ($DbRow = $DbResult->fetch_assoc()) 152 { 153 $this->Database->insert('DocumentLineSequence', array('FinanceYear' => $YearId, 154 'NextNumber' => 1, 'YearPrefix' => 1, 'DocumentLine' => $DbRow['Id'])); 155 } 156 } 82 157 } 83 158
Note:
See TracChangeset
for help on using the changeset viewer.