source: trunk/Modules/Finance/Import.php@ 719

Last change on this file since 719 was 719, checked in by chronos, 10 years ago
  • Added: Support for Hidden items in form types values.
  • Added: Support for Filtered items in form types values.
  • Modified: FinanceOperation Value splitted to (Direction * Value). Direction can be +1 or -1 depends on if money goes out or in company.
  • Added: Menu "Incomes and spends" is now splitted to incomes/spends of bank account or treasury. Direction and document line is filled automatically.
File size: 5.6 KB
Line 
1<?php
2
3class PageFinanceImportPayment extends Page
4{
5 var $FullTitle = 'Import plateb';
6 var $ShortTitle = 'Import plateb';
7 var $ParentClass = 'PageFinance';
8
9 function Show()
10 {
11 if(!$this->System->User->CheckPermission('Finance', 'SubjectList')) return('Nemáte oprávnění');
12 if(array_key_exists('Operation', $_GET))
13 {
14 if($_GET['Operation'] == 'prepare') return($this->Prepare());
15 else if($_GET['Operation'] == 'insert') return($this->Insert());
16 else echo('Neplatná akce');
17 } else
18 {
19 $Output = 'Vložte CSV data z SYLK exportu Poštovní spořitelny';
20 $Output .= '<form action="?Operation=prepare" method="post">';
21 $Output .= '<textarea name="Source" cols="80" rows="20"></textarea><br/>';
22 $Output .= '<input type="submit" value="Analyzovat"/>';
23 $Output .= '</form>';
24 return($Output);
25 }
26 }
27
28 function Prepare()
29 {
30 $Finance = $this->System->Modules['Finance'];
31 $Finance->LoadMonthParameters(0);
32 $Data = explode("\n", $_POST['Source']);
33 foreach($Data as $Key => $Value)
34 {
35 $Value = str_replace('\"', '"', $Value);
36 $Data[$Key] = str_getcsv($Value, ',', '"', "\\");
37 //print_r($Data[$Key]);
38 foreach($Data[$Key] as $Key2 => $Value2)
39 {
40 if(substr($Data[$Key][$Key2], 0, 2) == '\"')
41 $Data[$Key][$Key2] = substr($Data[$Key][$Key2], 2, -2);
42 }
43 }
44 $Header = array(
45 0 => 'datum zaúčtování',
46 1 => 'částka',
47 2 => 'měna',
48 3 => 'zůstatek',
49 4 => 'konstantní symbol',
50 5 => 'variabilní symbol',
51 6 => 'specifický symbol',
52 7 => 'označení operace',
53 8 => 'název protiúčtu',
54 9 => 'protiúčet',
55 10 => 'poznámka',
56 11 => '',
57 );
58 //print_r($Header);
59 //print_r($Data[0]);
60 //print_r($_POST['Source']);
61 //print_r($Data);
62
63 if($Header != $Data[0]) $Output = 'Nekompatibilní struktura CSV';
64 else
65 {
66 array_shift($Data);
67 $Automatic = '';
68 $Manual = '';
69 $Output = '<form action="?Operation=insert" method="post">';
70 $I = 0;
71 foreach($Data as $Key => $Value)
72 {
73 if(count($Value) <= 1) continue;
74 if($Value[9] == '') $Value[5] = 128; // Žádný účet => Poštovní spořitelna
75 $Time = explode('.', $Value[0]);
76 $Time = $Time[2].'-'.$Time[1].'-'.$Time[0];
77 $Money = $Value[1];
78 if(is_numeric($Value[5]))
79 {
80 $Subject = $Value[5] * 1;
81 $DbResult = $this->Database->query('SELECT Id FROM Subject WHERE Id='.$this->Database->real_escape_string($Subject));
82 if($DbResult->num_rows == 0) $Subject = '? ('.($Value[5] * 1).')';
83 } else
84 {
85 $Subject = '? ('.$Value[5].')';
86 }
87 if(!is_numeric($Subject))
88 {
89 $Mode = 'Ručně';
90 $Style = 'style="background-color: LightPink;" ';
91 } else
92 {
93 $Mode = 'Automaticky';
94 $Style = '';
95 }
96
97 if($Money < 0) $Text = 'Platba převodem';
98 else $Text = 'Přijatá platba';
99 $Automatic .= '<tr>'.
100 //'<td>'.$Mode.'</td>'.
101 '<td><input type="text" name="Date'.$I.'" value="'.$Time.'"/></td>'.
102 '<td><input type="text" '.$Style.'name="Subject'.$I.'" value="'.$Subject.'"/></td>'.
103 '<td>'.$Value[8].'</td>'.
104 '<td><input type="text" name="Money'.$I.'" value="'.$Money.'"/></td>'.
105 '<td><input type="text" name="Text'.$I.'" value="'.$Text.'"/></td>'.
106 '<td><input type="text" name="Taxable'.$I.'" value="1"/></td>'.
107 '<td><input type="text" name="Network'.$I.'" value="1"/></td>'.
108 '</tr><tr><td colspan="7">'.implode(', ', $Value).'</td></tr>';
109 $I++;
110 }
111 $Output .= '<table class="WideTable">'.
112 '<tr>'.
113 //'<th>Zpracování</th>'.
114 '<th>Datum</th><th>Var. symbol</th><th>Protiúčet</th><th>Částka [Kč]</th><th>Text</th><th>Zdanitelné</th><th>Síť</th></tr>';
115 $Output .= $Automatic.'</table>';
116 $Output .= '<input type="hidden" name="ItemCount" value="'.$I.'"/>';
117 $Output .= '<input type="submit" value="Zpracovat"/></form>';
118 }
119 return($Output);
120 }
121
122 function InsertMoney($Subject, $Value, $Direction, $Cash, $Taxable, $Time, $Text, $DocumentLine)
123 {
124 $Year = date('Y', $Time);
125 $BillCode = $this->System->Modules['Finance']->GetNextDocumentLineNumber($DocumentLine, $Year);
126 // TODO: Fixed BankAccount=1, allow to select bank account for import
127 $this->Database->insert('FinanceOperation', array('Text' => $Text,
128 'Subject' => $Subject, 'Cash' => $Cash, 'Value' => $Value, 'Direction' => $Direction,
129 'Time' => TimeToMysqlDateTime($Time), 'Taxable' => $Taxable, 'BillCode' => $BillCode,
130 'BankAccount' => 1));
131 }
132
133 function Insert()
134 {
135 $Finance = $this->System->Modules['Finance'];
136 $Finance->LoadMonthParameters(0);
137 $Output = '';
138
139 for($I = $_POST['ItemCount'] - 1; $I >= 0 ; $I--)
140 {
141 // TODO: Use links to database records instead of contants
142 if($_POST['Money'.$I] < 0) {
143 $DocumentLine = 4;
144 $Direction = -1;
145 } else {
146 $DocumentLine = 3;
147 $Direction = 1;
148 }
149 $Date = explode('-', $_POST['Date'.$I]);
150 $Date = mktime(0, 0, 0, $Date[1], $Date[2], $Date[0]);
151 $this->InsertMoney($_POST['Subject'.$I], Abs($_POST['Money'.$I]), $Direction, 0, $_POST['Taxable'.$I], $Date, $_POST['Text'.$I], $DocumentLine);
152 $Output .= $I.', ';
153 $this->System->ModuleManager->Modules['Log']->NewRecord('Finance', 'NewPaymentInserted');
154 }
155 return($Output);
156 }
157}
Note: See TracBrowser for help on using the repository browser.