source: trunk/Modules/Stock/Stock.php

Last change on this file was 923, checked in by chronos, 3 years ago
  • Modified: Improved model definitions.
File size: 16.9 KB
Line 
1<?php
2
3class ModuleStock extends Module
4{
5 function __construct(System $System)
6 {
7 parent::__construct($System);
8 $this->Name = 'Stock';
9 $this->Version = '1.0';
10 $this->Creator = 'Chronos';
11 $this->License = 'GNU/GPLv3';
12 $this->Description = 'Stock and products';
13 $this->Dependencies = array(ModuleUser::GetName(), ModuleCustomer::GetName(), ModuleNetwork::GetName(),
14 ModuleDocument::GetName());
15 $this->Models = array(Product::GetClassName(), StockSerialNumber::GetClassName(), Stock::GetClassName(),
16 StockMoveGroup::GetClassName(), StockMove::GetClassName(), StockMoveItem::GetClassName(),
17 StockItemHistory::GetClassName(), StockMoveItemSerialRel::GetClassName());
18 }
19
20 function DoStart(): void
21 {
22 $this->System->FormManager->RegisterClass('Product', array(
23 'Title' => 'Produkty',
24 'Table' => 'Product',
25 'DefaultSortColumn' => 'Name',
26 'Items' => array(
27 'Manufacturer' => array('Type' => 'TSubject', 'Caption' => 'Výrobce', 'Default' => '', 'Null' => true),
28 'Code' => array('Type' => 'String', 'Caption' => 'Kód', 'Default' => ''),
29 'Name' => array('Type' => 'String', 'Caption' => 'Jméno', 'Default' => ''),
30 'SellPrice' => array('Type' => 'Integer', 'Caption' => 'Prodejní cena', 'Default' => '0', 'Suffix' => 'Kč'),
31 'BuyPrice' => array('Type' => 'Integer', 'Caption' => 'Kupní cena', 'Default' => '0', 'Suffix' => 'Kč'),
32 'VAT' => array('Type' => 'Integer', 'Caption' => 'DPH', 'Default' => '0', 'Suffix' => '%'),
33 'Consumption' => array('Type' => 'Integer', 'Caption' => 'Spotřeba', 'Default' => '', 'Suffix' => 'Watt'),
34 'Supplier' => array('Type' => 'TSubject', 'Caption' => 'Dodavatel', 'Default' => '', 'Null' => true),
35 'UnitOfMeasure' => array('Type' => 'TUnitOfMeasure', 'Caption' => 'Měrná jednotka', 'Default' => '', 'Null' => true),
36 'StockSerialNumbers' => array('Type' => 'TStockSerialNumberListProduct', 'Caption' => 'Položky na skladě', 'Default' => ''),
37 'StockState' => array('Type' => 'Integer', 'Caption' => 'Počet na skladě', 'Default' => '', 'ReadOnly' => true,
38 'SQL' => 'SELECT IFNULL(SUM(`Amount`), 0) FROM `StockMoveItem` WHERE `StockMoveItem`.`Product`=#Id'),
39 'StockMinCount' => array('Type' => 'Integer', 'Caption' => 'Skladové minimum', 'Default' => '0'),
40 'StockMoves' => array('Type' => 'TStockMoveItemListProduct', 'Caption' => 'Pohyby na skladě', 'Default' => ''),
41 'NetworkDevices' => array('Type' => 'TNetworkDeviceListProduct', 'Caption' => 'Síťová zařízení', 'Default' => ''),
42 'StockShortage' => array('Type' => 'TStockState', 'Caption' => 'Stav skladu', 'Default' => '', 'ReadOnly' => true,
43 'SQL' => 'SELECT (IFNULL(SUM(`Amount`), 0) >= `StockMinCount`) FROM `StockMoveItem` WHERE `StockMoveItem`.`Product`=#Id'),
44 ),
45 ));
46
47 $this->System->FormManager->RegisterClass('StockSerialNumber', array(
48 'Title' => 'Skladové položky',
49 'Table' => 'StockSerialNumber',
50 'DefaultSortColumn' => 'Id',
51 'Items' => array(
52 'Stock' => array('Type' => 'TStock', 'Caption' => 'Sklad', 'Default' => ''),
53 'Product' => array('Type' => 'TProduct', 'Caption' => 'Produkt', 'Default' => ''),
54 'TimeEnlistment' => array('Type' => 'Date', 'Caption' => 'Datum zařazení', 'Default' => ''),
55 'TimeElimination' => array('Type' => 'Date', 'Caption' => 'Datum vyřazení', 'Default' => '', 'Null' => true),
56 'SellPrice' => array('Type' => 'Integer', 'Caption' => 'Prodejní cena', 'Default' => '0', 'Suffix' => 'Kč'),
57 'BuyPrice' => array('Type' => 'Integer', 'Caption' => 'Kupní cena', 'Default' => '0', 'Suffix' => 'Kč'),
58 'Amount' => array('Type' => 'Integer', 'Caption' => 'Množství', 'Default' => '1'),
59 'SerialNumber' => array('Type' => 'String', 'Caption' => 'Sériové číslo', 'Default' => '', 'Null' => true),
60 'RegNumber' => array('Type' => 'String', 'Caption' => 'Evidenční číslo', 'Default' => '', 'Null' => true),
61 'Location' => array('Type' => 'TMember', 'Caption' => 'Umístění', 'Default' => '', 'Null' => true),
62 'Esemble' => array('Type' => 'TStockSerialNumber', 'Caption' => 'Celek', 'Default' => ''),
63 'Parts' => array('Type' => 'TStockSerialNumberListStockSerialNumber', 'Caption' => 'Části', 'Default' => ''),
64 'History' => array('Type' => 'TStockItemHistoryListStockSerialNumber', 'Caption' => 'Historie', 'Default' => ''),
65 'Sériové čísla' => array('Type' => 'TStockMoveItemSerialListSerial', 'Caption' => 'Skladové pohyby', 'Default' => ''),
66 ),
67 ));
68 $this->System->FormManager->RegisterClass('Stock', array(
69 'Title' => 'Sklady',
70 'Table' => 'Stock',
71 'DefaultSortColumn' => 'Name',
72 'Items' => array(
73 'Name' => array('Type' => 'String', 'Caption' => 'Jméno', 'Default' => ''),
74 'Location' => array('Type' => 'TMember', 'Caption' => 'Umístění', 'Default' => '', 'Null' => true),
75 'Items' => array('Type' => 'TStockSerialNumberListStock', 'Caption' => 'Položky', 'Default' => ''),
76 'ItemsCount' => array('Type' => 'Integer', 'Caption' => 'Položek', 'Default' => '',
77 'ReadOnly' => true, 'SQL' => '(SELECT COUNT(*) FROM `StockSerialNumber` WHERE '.
78 '(`StockSerialNumber`.`Stock`=#Id) AND (`StockSerialNumber`.`TimeElimination` IS NULL))'),
79 'TotalPrice' => array('Type' => 'Integer', 'Caption' => 'Celková cena', 'Default' => '',
80 'ReadOnly' => true, 'SQL' => '(SELECT SUM(`SellPrice`) FROM `StockSerialNumber` WHERE '.
81 '(`StockSerialNumber`.`Stock`=#Id) AND (`StockSerialNumber`.`TimeElimination` IS NULL))', 'Suffix' => 'Kč'),
82 'Moves' => array('Type' => 'TStockMoveStock', 'Caption' => 'Pohyby', 'Default' => ''),
83 ),
84 ));
85 $this->System->FormManager->RegisterClass('StockMove', array(
86 'Title' => 'Skladový pohyb',
87 'Table' => 'StockMove',
88 'DefaultSortColumn' => 'Time',
89 'Items' => array(
90 'Group' => array('Type' => 'TStockMoveGroup', 'Caption' => 'Skupina', 'Default' => ''),
91 'BillCode' => array('Type' => 'TDocumentLineCode', 'Caption' => 'Označení', 'Default' => '', 'ReadOnly' => true),
92 'Time' => array('Type' => 'DateTime', 'Caption' => 'Čas', 'Default' => ''),
93 'Stock' => array('Type' => 'TStock', 'Caption' => 'Sklad', 'Default' => '', 'Null' => true),
94 'File' => array('Type' => 'TFile', 'Caption' => 'Doklad', 'Default' => '', 'Null' => true),
95 'Price' => array('Type' => 'Float', 'Caption' => 'Cena', 'Default' => '',
96 'ReadOnly' => true, 'Suffix' => 'Kč', 'SQL' => '(SELECT SUM(`StockMoveItem`.`UnitPrice` * `StockMoveItem`.`Amount`) FROM `StockMoveItem` '.
97 'WHERE `StockMoveItem`.`StockMove`=#Id)'),
98 'Items' => array('Type' => 'TStockMoveItemListStockMove', 'Caption' => 'Položky', 'Default' => ''),
99 ),
100 'BeforeInsert' => array($this, 'BeforeInsertStockMove'),
101 //'AfterInsert' => array($this, 'AfterInsertStockMove'),
102 //'BeforeModify' => array($this, 'BeforeModifyStockMove'),
103 ));
104 $this->System->FormManager->RegisterClass('StockMoveItem', array(
105 'Title' => 'Položka skladového pohybu',
106 'Table' => 'StockMoveItem',
107 'DefaultSortColumn' => 'Product',
108 'Items' => array(
109 'StockMove' => array('Type' => 'TStockMove', 'Caption' => 'Skladový pohyb', 'Default' => ''),
110 'Product' => array('Type' => 'TProduct', 'Caption' => 'Produkt', 'Default' => ''),
111 'UnitPrice' => array('Type' => 'Integer', 'Caption' => 'Jednotková cena', 'Default' => '0', 'Suffix' => 'Kč'),
112 'Amount' => array('Type' => 'Integer', 'Caption' => 'Množství', 'Default' => '1'),
113 'Total' => array('Type' => 'Integer', 'Caption' => 'Celkem', 'Default' => '', 'Suffix' => 'Kč',
114 'ReadOnly' => true, 'SQL' => 'ROUND(`UnitPrice` * `Amount`, '.$this->System->Config['Finance']['Rounding'].')'),
115 'Položky pohybů' => array('Type' => 'TStockMoveItemSerialListItem', 'Caption' => 'Vztahy sériových čísel', 'Default' => ''),
116 'Sériové čísla' => array('Type' => 'String', 'Caption' => 'Sériové čísla', 'ReadOnly' => true,
117 'SQL' => 'SELECT GROUP_CONCAT(`StockSerialNumber`.`RegNumber` SEPARATOR ", ") FROM `StockMoveItemSerialRel` '.
118 'LEFT JOIN `StockSerialNumber` ON `StockSerialNumber`.`Id`=`StockMoveItemSerialRel`.`StockSerialNumber` '.
119 'WHERE `StockMoveItemSerialRel`.`StockMoveItem`=#Id'),
120 ),
121 ));
122 $this->System->FormManager->RegisterClass('StockMoveGroup', array(
123 'Title' => 'Skupina skladových pohybů',
124 'Table' => 'StockMoveGroup',
125 'Items' => array(
126 'Name' => array('Type' => 'String', 'Caption' => 'Název', 'Default' => '0'),
127 'DocumentLine' => array('Type' => 'TDocumentLine', 'Caption' => 'Dokladová řada', 'Default' => '0'),
128 'ValueSign' => array('Type' => 'TFinanceValueSign', 'Caption' => 'Znaménko hodnoty', 'Default' => '0'),
129 'Direction' => array('Type' => 'TFinanceDirection', 'Caption' => 'Směr', 'Default' => '0'),
130 'Items' => array('Type' => 'TStockMoveListGroup', 'Caption' => 'Operace', 'Default' => ''),
131 ),
132 ));
133 $this->System->FormManager->RegisterClass('StockItemHistory', array(
134 'Title' => 'Historie skladové položky',
135 'Table' => 'StockItemHistory',
136 'DefaultSortColumn' => 'Time',
137 'Items' => array(
138 'Time' => array('Type' => 'DateTime', 'Caption' => 'Čas', 'Default' => ''),
139 'StockSerialNumber' => array('Type' => 'TStockSerialNumber', 'Caption' => 'Sériové číslo', 'Default' => ''),
140 'Text' => array('Type' => 'Text', 'Caption' => 'Text', 'Default' => ''),
141 ),
142 ));
143 $this->System->FormManager->RegisterClass('StockMoveItemSerialRel', array(
144 'Title' => 'Vztah položky skladových pohybů a sériových čísel',
145 'Table' => 'StockMoveItemSerialRel',
146 'Items' => array(
147 'StockMoveItem' => array('Type' => 'TStockMoveItem', 'Caption' => 'Položky pohybu', 'Default' => ''),
148 'StockSerialNumber' => array('Type' => 'TStockSerialNumber', 'Caption' => 'Sériové číslo', 'Default' => ''),
149 ),
150 ));
151 $this->System->FormManager->RegisterFormType('TStockMoveItemListStockMove', array(
152 'Type' => 'ManyToOne',
153 'Table' => 'StockMoveItem',
154 'Id' => 'Id',
155 'Ref' => 'StockMove',
156 'Filter' => '1',
157 ));
158 $this->System->FormManager->RegisterFormType('TStockMoveItemListProduct', array(
159 'Type' => 'ManyToOne',
160 'Table' => 'StockMoveItem',
161 'Id' => 'Id',
162 'Ref' => 'Product',
163 'Filter' => '1',
164 ));
165 $this->System->FormManager->RegisterFormType('TStockMoveListGroup', array(
166 'Type' => 'ManyToOne',
167 'Table' => 'StockMove',
168 'Id' => 'Id',
169 'Ref' => 'Group',
170 'Filter' => '1',
171 ));
172 $this->System->FormManager->RegisterFormType('TStockMoveStock', array(
173 'Type' => 'ManyToOne',
174 'Table' => 'StockMove',
175 'Id' => 'Id',
176 'Ref' => 'Stock',
177 'Filter' => '1',
178 ));
179 $this->System->FormManager->RegisterFormType('TStockMoveItemSerialListItem', array(
180 'Type' => 'ManyToOne',
181 'Table' => 'StockMoveItemSerialRel',
182 'Id' => 'Id',
183 'Ref' => 'StockMoveItem',
184 'Filter' => '1',
185 ));
186 $this->System->FormManager->RegisterFormType('TStockMoveItemSerialListSerial', array(
187 'Type' => 'ManyToOne',
188 'Table' => 'StockMoveItemSerialRel',
189 'Id' => 'Id',
190 'Ref' => 'StockSerialNumber',
191 'Filter' => '1',
192 ));
193 $this->System->FormManager->RegisterFormType('TStockMove', array(
194 'Type' => 'Reference',
195 'Table' => 'StockMove',
196 'Id' => 'Id',
197 'Name' => '(SELECT `DocumentLineCode`.`Name` FROM `DocumentLineCode` WHERE `Id`=`StockMove`.`BillCode`)',
198 'Filter' => '1',
199 ));
200 $this->System->FormManager->RegisterFormType('TStockMoveItem', array(
201 'Type' => 'Reference',
202 'Table' => 'StockMoveItem',
203 'Id' => 'Id',
204 'Name' => '(SELECT `Name` FROM `Product` WHERE `Product`.`Id`=`StockMoveItem`.`Product`)',
205 'Filter' => '1',
206 ));
207 $this->System->FormManager->RegisterFormType('TProduct', array(
208 'Type' => 'Reference',
209 'Table' => 'Product',
210 'Id' => 'Id',
211 'Name' => 'Name',
212 'Filter' => '1',
213 ));
214 $this->System->FormManager->RegisterFormType('TStock', array(
215 'Type' => 'Reference',
216 'Table' => 'Stock',
217 'Id' => 'Id',
218 'Name' => 'Name',
219 'Filter' => '1',
220 ));
221 $this->System->FormManager->RegisterFormType('TStockMoveGroup', array(
222 'Type' => 'Reference',
223 'Table' => 'StockMoveGroup',
224 'Id' => 'Id',
225 'Name' => 'Name',
226 'Filter' => '1',
227 ));
228 $this->System->FormManager->RegisterFormType('TStockSerialNumber', array(
229 'Type' => 'Reference',
230 'Table' => 'StockSerialNumber',
231 'Id' => 'Id',
232 'Name' => 'RegNumber',
233 'Filter' => '1',
234 ));
235 $this->System->FormManager->RegisterFormType('TStockItemHistoryListStockSerialNumber', array(
236 'Type' => 'ManyToOne',
237 'Table' => 'StockItemHistory',
238 'Id' => 'Id',
239 'Ref' => 'StockSerialNumber',
240 'Filter' => '1',
241 ));
242
243 $this->System->FormManager->RegisterFormType('TStockState', array(
244 'Type' => 'Enumeration',
245 'States' => array('<span style="color:red;">Nedostatek</span>',
246 '<span style="color:green;">Dostatek</span>'),
247 ));
248 }
249
250 function BeforeInsertStockMove(Form $Form): array
251 {
252 $Finance = &ModuleFinance::Cast($this->System->GetModule('Finance'))->Finance;
253 if (array_key_exists('Time', $Form->Values)) $Year = date("Y", $Form->Values['Time']);
254 else $Year = date("Y", $Form->Values['ValidFrom']);
255 $Group = $Finance->GetFinanceGroupById($Form->Values['Group'], 'StockMoveGroup');
256 $Form->Values['BillCode'] = ModuleDocument::Cast($this->System->GetModule('Document'))->GetNextDocumentLineNumberId($Group['DocumentLine'], $Year);
257 return $Form->Values;
258 }
259}
260
261class Product extends Model
262{
263 static function GetModelDesc(): ModelDesc
264 {
265 $Desc = new ModelDesc(self::GetClassName());
266 $Desc->AddReference('Manufacturer', Subject::GetClassName());
267 $Desc->AddString('Code');
268 $Desc->AddString('Name');
269 $Desc->AddInteger('SellPrice');
270 $Desc->AddInteger('BuyPrice');
271 $Desc->AddInteger('VAT');
272 $Desc->AddInteger('Consumption');
273 $Desc->AddReference('Supplier', Subject::GetClassName());
274 $Desc->AddReference('UnitOfMeasure', UnitOfMeasure::GetClassName());
275 $Desc->AddInteger('StockMinCount');
276 return $Desc;
277 }
278}
279
280class StockSerialNumber extends Model
281{
282 static function GetModelDesc(): ModelDesc
283 {
284 $Desc = new ModelDesc(self::GetClassName());
285 $Desc->AddReference('Stock', Stock::GetClassName());
286 $Desc->AddReference('Product', Product::GetClassName());
287 $Desc->AddDate('TimeEnlistment');
288 $Desc->AddDate('TimeElimination');
289 $Desc->AddInteger('SellPrice');
290 $Desc->AddInteger('BuyPrice');
291 $Desc->AddInteger('Amount');
292 $Desc->AddString('SerialNumber');
293 $Desc->AddString('RegNumber');
294 $Desc->AddReference('Location', Member::GetClassName());
295 $Desc->AddReference('Esemble', StockSerialNumber::GetClassName());
296 return $Desc;
297 }
298}
299
300class Stock extends Model
301{
302 static function GetModelDesc(): ModelDesc
303 {
304 $Desc = new ModelDesc(self::GetClassName());
305 $Desc->AddString('Name');
306 $Desc->AddReference('Location', Member::GetClassName());
307 return $Desc;
308 }
309}
310
311class StockMove extends Model
312{
313 static function GetModelDesc(): ModelDesc
314 {
315 $Desc = new ModelDesc(self::GetClassName());
316 $Desc->AddReference('Group', StockMoveGroup::GetClassName());
317 $Desc->AddDateTime('Time');
318 $Desc->AddReference('Stock', Stock::GetClassName());
319 $Desc->AddReference('File', File::GetClassName());
320 return $Desc;
321 }
322}
323
324class StockMoveItem extends Model
325{
326 static function GetModelDesc(): ModelDesc
327 {
328 $Desc = new ModelDesc(self::GetClassName());
329 $Desc->AddReference('StockMove', StockMove::GetClassName());
330 $Desc->AddReference('Product', Product::GetClassName());
331 $Desc->AddInteger('UnitPrice');
332 $Desc->AddInteger('Amount');
333 return $Desc;
334 }
335}
336
337class StockMoveGroup extends Model
338{
339 static function GetModelDesc(): ModelDesc
340 {
341 $Desc = new ModelDesc(self::GetClassName());
342 $Desc->AddString('Name');
343 $Desc->AddReference('DocumentLine', DocumentLine::GetClassName());
344 $Desc->AddInteger('ValueSign');
345 $Desc->AddInteger('Direction');
346 return $Desc;
347 }
348}
349
350class StockItemHistory extends Model
351{
352 static function GetModelDesc(): ModelDesc
353 {
354 $Desc = new ModelDesc(self::GetClassName());
355 $Desc->AddReference('StockSerialNumber', StockSerialNumber::GetClassName());
356 $Desc->AddDateTime('Time');
357 $Desc->AddText('Text');
358 return $Desc;
359 }
360}
361
362class StockMoveItemSerialRel extends Model
363{
364 static function GetModelDesc(): ModelDesc
365 {
366 $Desc = new ModelDesc(self::GetClassName());
367 $Desc->AddReference('StockMoveItem', StockMoveItem::GetClassName());
368 $Desc->AddReference('StockSerialNumber', StockSerialNumber::GetClassName());
369 return $Desc;
370 }
371}
Note: See TracBrowser for help on using the repository browser.