source: trunk/finance/import.php@ 325

Last change on this file since 325 was 325, checked in by chronos, 14 years ago
  • Přidáno: Import plateb z poštovní spotřitelny přes CSV soubor převedeny z SYLK formátu.
File size: 5.3 KB
Line 
1<?php
2include('../global.php');
3
4class FinanceImportPayment extends Page
5{
6 var $FullTitle = 'Import plateb';
7 var $ShortTitle = 'Import plateb';
8
9 function Show()
10 {
11 if(!$this->System->Modules['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 české 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 InsertMoney($Subject, $Value, $Cash, $Taxable, $Time, $Text, $DocumentLine)
29 {
30
31 $BillCode = $this->System->Modules['Finance']->GetNextDocumentLineNumber($DocumentLine);
32 $this->Database->insert('FinanceOperation', array('Text' => $Text, 'Subject' => $Subject, 'Cash' => $Cash, 'Value' => $Value, 'Time' => TimeToMysqlDateTime($Time), 'Taxable' => $Taxable, 'BillCode' => $BillCode));
33 }
34
35 function Prepare()
36 {
37 $Finance = $this->System->Modules['Finance'];
38 $Data = explode("\n", $_POST['Source']);
39 foreach($Data as $Key => $Value)
40 {
41 $Value = str_replace('\"', '"', $Value);
42 $Data[$Key] = str_getcsv($Value, ',', '"', "\\");
43 //print_r($Data[$Key]);
44 foreach($Data[$Key] as $Key2 => $Value2)
45 {
46 if(substr($Data[$Key][$Key2], 0, 2) == '\"')
47 $Data[$Key][$Key2] = substr($Data[$Key][$Key2], 2, -2);
48 }
49 }
50 $Header = array(
51 0 => 'datum zaúčtování',
52 1 => 'částka',
53 2 => 'měna',
54 3 => 'zůstatek',
55 4 => 'konstantní symbol',
56 5 => 'variabilní symbol',
57 6 => 'specifický symbol',
58 7 => 'označení operace',
59 8 => 'název protiúčtu',
60 9 => 'protiúčet',
61 10 => 'poznámka',
62 11 => '',
63 );
64 //print_r($Header);
65 //print_r($Data[0]);
66 //print_r($_POST['Source']);
67 //print_r($Data);
68
69 if($Header != $Data[0]) $Output = 'Nekompatibilní struktura CSV';
70 else
71 {
72 array_shift($Data);
73 $Automatic = '';
74 $Manual = '';
75 $Output .= '<form action="?Operation=insert" method="post">';
76 $I = 0;
77 foreach($Data as $Key => $Value)
78 {
79 if(count($Value) <= 1) continue;
80 if($Value[9] == '') $Value[5] = 128; // Žádný účet => Poštovní spořitelna
81 if(is_numeric($Value[5]))
82 {
83 $Time = explode('.', $Value[0]);
84 $Time = $Time[2].'-'.$Time[1].'-'.$Time[0];
85 $Money = $Value[1];
86 $Subject = $Value[5] * 1;
87 $Automatic .= '<tr>'.
88 '<td>Automaticky</td>'.
89 '<td><input type="text" name="Date'.$I.'" value="'.$Time.'"/></td>'.
90 '<td><input type="text" name="Subject'.$I.'" value="'.$Subject.'"/></td>'.
91 '<td>'.$Value[8].'</td>'.
92 '<td><input type="text" name="Money'.$I.'" value="'.$Money.'"/></td>'.
93 '<td><input type="text" name="Text'.$I.'" value="Vklad"/></td>'.
94 '<td><input type="text" name="Taxable'.$I.'" value="1"/></td>'.
95 '<td><input type="text" name="Network'.$I.'" value="1"/></td>'.
96 '</tr><tr><td colspan="7">'.implode(', ', $Value).'</td></tr>';
97 } else
98 {
99 $Time = explode('.', $Value[0]);
100 $Time = $Time[2].'-'.$Time[1].'-'.$Time[0];
101 $Money = $Value[1];
102 $Subject = '?';
103 $Automatic .= '<tr>'.
104 '<td>Ručně</td>'.
105 '<td><input type="text" name="Date'.$I.'" value="'.$Time.'"/></td>'.
106 '<td><input type="text" name="Subject'.$I.'" value="'.$Subject.'"/></td>'.
107 '<td>'.$Value[8].'</td>'.
108 '<td><input type="text" name="Money'.$I.'" value="'.$Money.'"/></td>'.
109 '<td><input type="text" name="Text'.$I.'" value="Vklad"/></td>'.
110 '<td><input type="text" name="Taxable'.$I.'" value="1"/></td>'.
111 '<td><input type="text" name="Network'.$I.'" value="1"/></td>'.
112 '</tr><tr><td colspan="7">'.implode(', ', $Value).'</td></tr>';
113 }
114 $I++;
115 }
116 $Output .= '<table class="WideTable">'.
117 '<tr><th>Zpracování</th><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>';
118 $Output .= $Automatic.'</table>';
119 $Output .= '<input type="hidden" name="ItemCount" value="'.$I.'"/>';
120 $Output .= '<input type="submit" value="Zpracovat"/></form>';
121 }
122 return($Output);
123 }
124
125 function Insert()
126 {
127 $Finance = $this->System->Modules['Finance'];
128 $Output = '';
129
130 for($I = 0; $I < $_POST['ItemCount']; $I++)
131 {
132 if($_POST['Money'.$I] < 0) $DocumentLine = 4;
133 else $DocumentLine = 3;
134 $this->InsertMoney($_POST['Subject'.$I], $_POST['Money'.$I], 0, $_POST['Taxable'.$I], $_POST['Date'.$I], $_POST['Text'.$I], $DocumentLine);
135 $Output .= $I.', ';
136 $this->System->Modules['Log']->NewRecord('Finance', 'NewPaymentInserted');
137 }
138 return($Output);
139 }
140}
141
142$System->AddModule(new FinanceImportPayment());
143$System->Modules['FinanceImportPayment']->GetOutput();
144
145?>
Note: See TracBrowser for help on using the repository browser.