source: trunk/Modules/Document/Document.php

Last change on this file was 901, checked in by chronos, 4 years ago
  • Added: Documents section accessible from users panel.
  • Added: Contract model moved to separate module Contract.
  • Added: New action to generate PDF from Contract.
  • Fixed: Allow users to download files only by hash instead of direct id.
  • Added: Support for SHA1 as allowed mysql function.
File size: 7.6 KB
Line 
1<?php
2
3class ModuleDocument extends Module
4{
5 function __construct(System $System)
6 {
7 parent::__construct($System);
8 $this->Name = 'Document';
9 $this->Version = '1.0';
10 $this->Creator = 'Chronos';
11 $this->License = 'GNU/GPLv3';
12 $this->Description = 'Documents support';
13 $this->Models = array(FinanceYear::GetClassName(), DocumentLineCode::GetClassName(), DocumentLine::GetClassName(),
14 DocumentLineSequence::GetClassName());
15 }
16
17 function DoStart(): void
18 {
19 $this->System->FormManager->RegisterClass('DocumentLine', array(
20 'Title' => 'Dokladové řady',
21 'Table' => 'DocumentLine',
22 'DefaultSortColumn' => 'Name',
23 'Items' => array(
24 'Name' => array('Type' => 'String', 'Caption' => 'Jméno', 'Default' => ''),
25 'Shortcut' => array('Type' => 'String', 'Caption' => 'Kód', 'Default' => ''),
26 'Yearly' => array('Type' => 'Boolean', 'Caption' => 'Ročně', 'Default' => 0),
27 'Sequence' => array('Type' => 'TDocumentLineSequenceListLine', 'Caption' => 'Čísleníky', 'Default' => ''),
28 'Codes' => array('Type' => 'TDocumentLineCodeList', 'Caption' => 'Kódy', 'Default' => ''),
29 'Operations' => array('Type' => 'TFinanceOperationGroupListDocumentLine', 'Caption' => 'Skupiny finančních operací', 'Default' => ''),
30 'Invoices' => array('Type' => 'TFinanceInvoiceGroupListDocumentLine', 'Caption' => 'Skupiny faktur', 'Default' => ''),
31 ),
32 ));
33 $this->System->FormManager->RegisterClass('DocumentLineSequence', array(
34 'Title' => 'Čísleníky dokladových řad',
35 'Table' => 'DocumentLineSequence',
36 'DefaultSortColumn' => 'Id',
37 'Items' => array(
38 'DocumentLine' => array('Type' => 'TDocumentLine', 'Caption' => 'Dokladová řada', 'Default' => ''),
39 'FinanceYear' => array('Type' => 'TFinanceYear', 'Caption' => 'Účetní rok', 'Default' => ''),
40 'NextNumber' => array('Type' => 'Integer', 'Caption' => 'Další číslo', 'Default' => '1'),
41 'YearPrefix' => array('Type' => 'Boolean', 'Caption' => 'Rok jako přípona', 'Default' => '1'),
42 ),
43 ));
44 $this->System->FormManager->RegisterClass('DocumentLineCode', array(
45 'Title' => 'Kód dokladových řad',
46 'Table' => 'DocumentLineCode',
47 'DefaultSortColumn' => 'Name',
48 'Items' => array(
49 'DocumentLine' => array('Type' => 'TDocumentLine', 'Caption' => 'Dokladová řada', 'Default' => ''),
50 'Name' => array('Type' => 'String', 'Caption' => 'Název', 'Default' => ''),
51 ),
52 ));
53 $this->System->FormManager->RegisterClass('FinanceYear', array(
54 'Title' => 'Účetní roky',
55 'Table' => 'FinanceYear',
56 'DefaultSortColumn' => 'Year',
57 'DefaultSortOrder' => 1,
58 'Items' => array(
59 'Year' => array('Type' => 'Integer', 'Caption' => 'Rok', 'Default' => ''),
60 'DateStart' => array('Type' => 'Date', 'Caption' => 'První den', 'Default' => ''),
61 'DateEnd' => array('Type' => 'Date', 'Caption' => 'Poslední den', 'Default' => ''),
62 'Closed' => array('Type' => 'Boolean', 'Caption' => 'Uzavřen', 'Default' => 0),
63 'Sequence' => array('Type' => 'TDocumentLineSequenceListYear', 'Caption' => 'Čísleníky', 'Default' => ''),
64 ),
65 //'AfterInsert' => array($this, 'AfterInsertFinanceYear'),
66 ));
67 $this->System->FormManager->RegisterFormType('TDocumentLine', array(
68 'Type' => 'Reference',
69 'Table' => 'DocumentLine',
70 'Id' => 'Id',
71 'Name' => 'Name',
72 'Filter' => '1',
73 ));
74 $this->System->FormManager->RegisterFormType('TDocumentLineCode', array(
75 'Type' => 'Reference',
76 'Table' => 'DocumentLineCode',
77 'Id' => 'Id',
78 'Name' => 'Name',
79 'Filter' => '1',
80 ));
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 }
157}
158
159class DocumentLine extends Model
160{
161 static function GetModelDesc(): ModelDesc
162 {
163 $Desc = new ModelDesc(self::GetClassName());
164 $Desc->AddString('Name');
165 $Desc->AddString('ShortCut');
166 $Desc->AddBoolean('Yearly');
167 return $Desc;
168 }
169}
170
171class DocumentLineCode extends Model
172{
173 static function GetModelDesc(): ModelDesc
174 {
175 $Desc = new ModelDesc(self::GetClassName());
176 $Desc->AddReference('DocumentLine', DocumentLine::GetClassName());
177 $Desc->AddString('Name');
178 return $Desc;
179 }
180}
181
182class DocumentLineSequence extends Model
183{
184 static function GetModelDesc(): ModelDesc
185 {
186 $Desc = new ModelDesc(self::GetClassName());
187 $Desc->AddReference('DocumentLine', DocumentLine::GetClassName());
188 $Desc->AddReference('FinanceYear', FinanceYear::GetClassName());
189 $Desc->AddInteger('NextNumber');
190 $Desc->AddString('YearPrefix');
191 return $Desc;
192 }
193}
194
195class FinanceYear extends Model
196{
197 static function GetModelDesc(): ModelDesc
198 {
199 $Desc = new ModelDesc(self::GetClassName());
200 $Desc->AddInteger('Year');
201 $Desc->AddDate('DateStart');
202 $Desc->AddDate('DateEnd');
203 $Desc->AddBoolean('Closed');
204 return $Desc;
205 }
206}
Note: See TracBrowser for help on using the repository browser.