| 1 | <?php
|
|---|
| 2 |
|
|---|
| 3 | class PageFinanceImportPayment extends Page
|
|---|
| 4 | {
|
|---|
| 5 | function __construct(System $System)
|
|---|
| 6 | {
|
|---|
| 7 | parent::__construct($System);
|
|---|
| 8 | $this->Title = 'Import plateb';
|
|---|
| 9 | $this->ParentClass = 'PageFinance';
|
|---|
| 10 | }
|
|---|
| 11 |
|
|---|
| 12 | function Show(): string
|
|---|
| 13 | {
|
|---|
| 14 | if (!ModuleUser::Cast($this->System->GetModule('User'))->User->CheckPermission('Finance', 'SubjectList')) return 'Nemáte oprávnění';
|
|---|
| 15 | if (array_key_exists('Operation', $_GET))
|
|---|
| 16 | {
|
|---|
| 17 | if ($_GET['Operation'] == 'prepare') return $this->Prepare();
|
|---|
| 18 | else if ($_GET['Operation'] == 'insert') return $this->Insert();
|
|---|
| 19 | else echo('Neplatná akce');
|
|---|
| 20 | } else
|
|---|
| 21 | {
|
|---|
| 22 | $Output = 'Vložte CSV data z SYLK exportu Poštovní spořitelny';
|
|---|
| 23 | $Output .= '<form action="?Operation=prepare" method="post">';
|
|---|
| 24 | $Output .= '<textarea name="Source" cols="80" rows="20"></textarea><br/>';
|
|---|
| 25 | $Output .= '<input type="submit" value="Analyzovat"/>';
|
|---|
| 26 | $Output .= '</form>';
|
|---|
| 27 | return $Output;
|
|---|
| 28 | }
|
|---|
| 29 | }
|
|---|
| 30 |
|
|---|
| 31 | function Prepare(): string
|
|---|
| 32 | {
|
|---|
| 33 | $Finance = &ModuleFinance::Cast($this->System->GetModule('Finance'))->Finance;
|
|---|
| 34 | $Finance->LoadMonthParameters(0);
|
|---|
| 35 | $Data = explode("\n", $_POST['Source']);
|
|---|
| 36 | foreach ($Data as $Key => $Value)
|
|---|
| 37 | {
|
|---|
| 38 | $Value = str_replace('\"', '"', $Value);
|
|---|
| 39 | $Data[$Key] = str_getcsv($Value, ',', '"', "\\");
|
|---|
| 40 | foreach ($Data[$Key] as $Key2 => $Value2)
|
|---|
| 41 | {
|
|---|
| 42 | if (substr($Data[$Key][$Key2], 0, 2) == '\"')
|
|---|
| 43 | $Data[$Key][$Key2] = substr($Data[$Key][$Key2], 2, -2);
|
|---|
| 44 | }
|
|---|
| 45 | }
|
|---|
| 46 | $Header = array(
|
|---|
| 47 | 0 => 'datum zaúčtování',
|
|---|
| 48 | 1 => 'částka',
|
|---|
| 49 | 2 => 'měna',
|
|---|
| 50 | 3 => 'zůstatek',
|
|---|
| 51 | 4 => 'konstantní symbol',
|
|---|
| 52 | 5 => 'variabilní symbol',
|
|---|
| 53 | 6 => 'specifický symbol',
|
|---|
| 54 | 7 => 'označení operace',
|
|---|
| 55 | 8 => 'název protiúčtu',
|
|---|
| 56 | 9 => 'protiúčet',
|
|---|
| 57 | 10 => 'poznámka',
|
|---|
| 58 | 11 => '',
|
|---|
| 59 | );
|
|---|
| 60 |
|
|---|
| 61 | if ($Header != $Data[0]) {
|
|---|
| 62 | $Output = 'Nekompatibilní struktura CSV';
|
|---|
| 63 | print_r($Header);
|
|---|
| 64 | print_r($Data[0]);
|
|---|
| 65 | }
|
|---|
| 66 | else
|
|---|
| 67 | {
|
|---|
| 68 | array_shift($Data);
|
|---|
| 69 | $Automatic = '';
|
|---|
| 70 | $Output = '<form action="?Operation=insert" method="post">';
|
|---|
| 71 | $I = 0;
|
|---|
| 72 | foreach ($Data as $Key => $Value)
|
|---|
| 73 | {
|
|---|
| 74 | if (count($Value) <= 1) continue;
|
|---|
| 75 | if ($Value[9] == '') $Value[5] = 128; // Žádný účet => Poštovní spořitelna
|
|---|
| 76 | $Time = explode('.', $Value[0]);
|
|---|
| 77 | $Time = $Time[2].'-'.$Time[1].'-'.$Time[0];
|
|---|
| 78 | $Money = $Value[1];
|
|---|
| 79 | if (is_numeric($Value[5]))
|
|---|
| 80 | {
|
|---|
| 81 | $Subject = $Value[5] * 1;
|
|---|
| 82 | $DbResult = $this->Database->query('SELECT Id FROM Subject WHERE Id='.$this->Database->real_escape_string($Subject));
|
|---|
| 83 | if ($DbResult->num_rows == 0) $Subject = '? ('.($Value[5] * 1).')';
|
|---|
| 84 | } else
|
|---|
| 85 | {
|
|---|
| 86 | $Subject = '? ('.$Value[5].')';
|
|---|
| 87 | }
|
|---|
| 88 | if (!is_numeric($Subject))
|
|---|
| 89 | {
|
|---|
| 90 | $Mode = 'Ručně';
|
|---|
| 91 | $Style = 'style="background-color: LightPink;" ';
|
|---|
| 92 | } else
|
|---|
| 93 | {
|
|---|
| 94 | $Mode = 'Automaticky';
|
|---|
| 95 | $Style = '';
|
|---|
| 96 | }
|
|---|
| 97 |
|
|---|
| 98 | if ($Money < 0) $Text = 'Platba převodem';
|
|---|
| 99 | else $Text = 'Přijatá platba';
|
|---|
| 100 | $Automatic .= '<tr>'.
|
|---|
| 101 | //'<td>'.$Mode.'</td>'.
|
|---|
| 102 | '<td><input type="text" name="Date'.$I.'" value="'.$Time.'"/></td>'.
|
|---|
| 103 | '<td><input type="text" '.$Style.'name="Subject'.$I.'" value="'.$Subject.'"/></td>'.
|
|---|
| 104 | '<td>'.$Value[8].'</td>'.
|
|---|
| 105 | '<td><input type="text" name="Money'.$I.'" value="'.$Money.'"/></td>'.
|
|---|
| 106 | '<td><input type="text" name="Text'.$I.'" value="'.$Text.'"/></td>'.
|
|---|
| 107 | '<td><input type="text" name="Taxable'.$I.'" value="1"/></td>'.
|
|---|
| 108 | '<td><input type="text" name="Network'.$I.'" value="1"/></td>'.
|
|---|
| 109 | '</tr><tr><td colspan="7">'.implode(', ', $Value).'</td></tr>';
|
|---|
| 110 | $I++;
|
|---|
| 111 | }
|
|---|
| 112 | $Output .= '<table class="WideTable">'.
|
|---|
| 113 | '<tr>'.
|
|---|
| 114 | //'<th>Zpracování</th>'.
|
|---|
| 115 | '<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>';
|
|---|
| 116 | $Output .= $Automatic.'</table>';
|
|---|
| 117 | $Output .= '<input type="hidden" name="ItemCount" value="'.$I.'"/>';
|
|---|
| 118 | $Output .= '<input type="submit" value="Zpracovat"/></form>';
|
|---|
| 119 | }
|
|---|
| 120 | return $Output;
|
|---|
| 121 | }
|
|---|
| 122 |
|
|---|
| 123 | function InsertMoney(string $Subject, string $Value, string $Cash, string $Taxable, string $Time, string $Text, array $Group): void
|
|---|
| 124 | {
|
|---|
| 125 | $Year = date('Y', $Time);
|
|---|
| 126 | $BillCode = ModuleDocument::Cast($this->System->GetModule('Document'))->GetNextDocumentLineNumberId($Group['DocumentLine'], $Year);
|
|---|
| 127 | // TODO: Fixed BankAccount=1, allow to select bank account for import
|
|---|
| 128 | $this->Database->insert('FinanceOperation', array('Text' => $Text,
|
|---|
| 129 | 'Subject' => $Subject, 'Cash' => $Cash, 'ValueUser' => $Value, 'Value' => $Value * $Group['ValueSign'],
|
|---|
| 130 | 'Time' => TimeToMysqlDateTime($Time), 'Taxable' => $Taxable, 'BillCode' => $BillCode,
|
|---|
| 131 | 'BankAccount' => 1, 'Group' => $Group['Id']));
|
|---|
| 132 | // TODO: Update Value
|
|---|
| 133 | }
|
|---|
| 134 |
|
|---|
| 135 | function Insert(): string
|
|---|
| 136 | {
|
|---|
| 137 | $Finance = &ModuleFinance::Cast($this->System->GetModule('Finance'))->Finance;
|
|---|
| 138 | $Finance->LoadMonthParameters(0);
|
|---|
| 139 | $Output = '';
|
|---|
| 140 |
|
|---|
| 141 | for ($I = $_POST['ItemCount'] - 1; $I >= 0 ; $I--)
|
|---|
| 142 | {
|
|---|
| 143 | if ($_POST['Money'.$I] < 0)
|
|---|
| 144 | {
|
|---|
| 145 | $FinanceGroup = $Finance->GetFinanceGroupById(OPERATION_GROUP_ACCOUNT_OUT, 'FinanceOperationGroup');
|
|---|
| 146 | } else
|
|---|
| 147 | {
|
|---|
| 148 | $FinanceGroup = $Finance->GetFinanceGroupById(OPERATION_GROUP_ACCOUNT_OUT, 'FinanceOperationGroup');
|
|---|
| 149 | }
|
|---|
| 150 | $Date = explode('-', $_POST['Date'.$I]);
|
|---|
| 151 | $Date = mktime(0, 0, 0, $Date[1], $Date[2], $Date[0]);
|
|---|
| 152 | $this->InsertMoney($_POST['Subject'.$I], Abs($_POST['Money'.$I]),
|
|---|
| 153 | 0, $_POST['Taxable'.$I], $Date, $_POST['Text'.$I], $FinanceGroup);
|
|---|
| 154 | $Output .= $I.', ';
|
|---|
| 155 | ModuleLog::Cast($this->System->GetModule('Log'))->NewRecord('Finance', 'NewPaymentInserted');
|
|---|
| 156 | }
|
|---|
| 157 | return $Output;
|
|---|
| 158 | }
|
|---|
| 159 | }
|
|---|