5 use POSIX qw(strftime);
9 sub retrieve_open_invoices {
10 $main::lxdebug->enter_sub();
15 my $myconfig = \%main::myconfig;
16 my $form = $main::form;
18 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
19 my $arap = $params{vc} eq 'customer' ? 'ar' : 'ap';
20 my $vc = $params{vc} eq 'customer' ? 'customer' : 'vendor';
22 my $mandate = $params{vc} eq 'customer' ? " AND COALESCE(vc.mandator_id, '') <> '' AND vc.mandate_date_of_signature IS NOT NULL " : '';
26 SELECT ${arap}.id, ${arap}.invnumber, ${arap}.${vc}_id as vc_id, ${arap}.amount AS invoice_amount, ${arap}.invoice,
27 vc.name AS vcname, vc.language_id, ${arap}.duedate as duedate, ${arap}.direct_debit,
29 COALESCE(vc.iban, '') <> '' AND COALESCE(vc.bic, '') <> '' ${mandate} AS vc_bank_info_ok,
31 ${arap}.amount - ${arap}.paid - COALESCE(open_transfers.amount, 0) AS open_amount
34 LEFT JOIN ${vc} vc ON (${arap}.${vc}_id = vc.id)
35 LEFT JOIN (SELECT sei.ap_id, SUM(sei.amount) AS amount
36 FROM sepa_export_items sei
37 LEFT JOIN sepa_export se ON (sei.sepa_export_id = se.id)
41 AS open_transfers ON (${arap}.id = open_transfers.ap_id)
43 WHERE ${arap}.amount > (COALESCE(open_transfers.amount, 0) + ${arap}.paid)
45 ORDER BY lower(vc.name) ASC, lower(${arap}.invnumber) ASC
48 my $results = selectall_hashref_query($form, $dbh, $query);
50 $main::lxdebug->leave_sub();
56 $main::lxdebug->enter_sub();
61 Common::check_params(\%params, qw(employee bank_transfers vc));
63 my $myconfig = \%main::myconfig;
64 my $form = $main::form;
65 my $arap = $params{vc} eq 'customer' ? 'ar' : 'ap';
66 my $vc = $params{vc} eq 'customer' ? 'customer' : 'vendor';
69 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
71 my ($export_id) = selectfirst_array_query($form, $dbh, qq|SELECT nextval('sepa_export_id_seq')|);
73 qq|INSERT INTO sepa_export (id, employee_id, vc)
76 WHERE login = ?), ?)|;
77 do_query($form, $dbh, $query, $export_id, $params{employee}, $vc);
79 my $q_item_id = qq|SELECT nextval('id')|;
80 my $h_item_id = prepare_query($form, $dbh, $q_item_id);
81 my $c_mandate = $params{vc} eq 'customer' ? ', vc_mandator_id, vc_mandate_date_of_signature' : '';
82 my $p_mandate = $params{vc} eq 'customer' ? ', ?, ?' : '';
85 qq|INSERT INTO sepa_export_items (id, sepa_export_id, ${arap}_id, chart_id,
86 amount, requested_execution_date, reference, end_to_end_id,
87 our_iban, our_bic, vc_iban, vc_bic ${c_mandate})
90 ?, ?, ?, ? ${p_mandate})|;
91 my $h_insert = prepare_query($form, $dbh, $q_insert);
94 qq|SELECT arap.invnumber,
97 LEFT JOIN chart c ON (at.chart_id = c.id)
98 WHERE (at.trans_id = ?)
99 AND (c.link LIKE '%${ARAP}_paid%'))
102 FROM sepa_export_items sei
103 WHERE (sei.ap_id = ?))
107 my $h_reference = prepare_query($form, $dbh, $q_reference);
111 foreach my $transfer (@{ $params{bank_transfers} }) {
112 if (!$transfer->{reference}) {
113 do_statement($form, $h_reference, $q_reference, (conv_i($transfer->{"${arap}_id"})) x 3);
115 my ($invnumber, $num_payments) = $h_reference->fetchrow_array();
118 $transfer->{reference} = "${invnumber}-${num_payments}";
121 $h_item_id->execute();
122 my ($item_id) = $h_item_id->fetchrow_array();
124 my $end_to_end_id = strftime "LXO%Y%m%d%H%M%S", localtime;
125 my $item_id_len = length "$item_id";
126 my $num_zeroes = 35 - $item_id_len - length $end_to_end_id;
127 $end_to_end_id .= '0' x $num_zeroes if (0 < $num_zeroes);
128 $end_to_end_id .= $item_id;
129 $end_to_end_id = substr $end_to_end_id, 0, 35;
131 my @values = ($item_id, $export_id,
132 conv_i($transfer->{"${arap}_id"}), conv_i($transfer->{chart_id}),
133 $transfer->{amount}, conv_date($transfer->{requested_execution_date}),
134 $transfer->{reference}, $end_to_end_id,
135 map { my $pfx = $_; map { $transfer->{"${pfx}_${_}"} } qw(iban bic) } qw(our vc));
137 push @values, $transfer->{vc_mandator_id}, conv_date($transfer->{vc_mandate_date_of_signature}) if $params{vc} eq 'customer';
139 do_statement($form, $h_insert, $q_insert, @values);
143 $h_item_id->finish();
145 $dbh->commit() unless ($params{dbh});
147 $main::lxdebug->leave_sub();
152 sub retrieve_export {
153 $main::lxdebug->enter_sub();
158 Common::check_params(\%params, qw(id vc));
160 my $myconfig = \%main::myconfig;
161 my $form = $main::form;
162 my $vc = $params{vc} eq 'customer' ? 'customer' : 'vendor';
163 my $arap = $params{vc} eq 'customer' ? 'ar' : 'ap';
165 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
167 my ($joins, $columns);
169 if ($params{details}) {
170 $columns = ', arap.invoice';
171 $joins = "LEFT JOIN ${arap} arap ON (se.${arap}_id = arap.id)";
176 CASE WHEN COALESCE(e.name, '') <> '' THEN e.name ELSE e.login END AS employee
178 LEFT JOIN employee e ON (se.employee_id = e.id)
181 my $export = selectfirst_hashref_query($form, $dbh, $query, conv_i($params{id}));
184 my ($columns, $joins);
186 my $mandator_id = $params{vc} eq 'customer' ? ', mandator_id, mandate_date_of_signature' : '';
188 if ($params{details}) {
189 $columns = qq|, arap.invnumber, arap.invoice, arap.transdate AS reference_date, vc.name AS vc_name, vc.${vc}number AS vc_number, c.accno AS chart_accno, c.description AS chart_description ${mandator_id}|;
190 $joins = qq|LEFT JOIN ${arap} arap ON (sei.${arap}_id = arap.id)
191 LEFT JOIN ${vc} vc ON (arap.${vc}_id = vc.id)
192 LEFT JOIN chart c ON (sei.chart_id = c.id)|;
195 $query = qq|SELECT sei.*
197 FROM sepa_export_items sei
199 WHERE sei.sepa_export_id = ?|;
201 $export->{items} = selectall_hashref_query($form, $dbh, $query, conv_i($params{id}));
204 $export->{items} = [];
207 $main::lxdebug->leave_sub();
213 $main::lxdebug->enter_sub();
218 Common::check_params(\%params, qw(id));
220 my $myconfig = \%main::myconfig;
221 my $form = $main::form;
223 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
225 my @ids = ref $params{id} eq 'ARRAY' ? @{ $params{id} } : ($params{id});
226 my $placeholders = join ', ', ('?') x scalar @ids;
227 my $query = qq|UPDATE sepa_export SET closed = TRUE WHERE id IN ($placeholders)|;
229 do_query($form, $dbh, $query, map { conv_i($_) } @ids);
231 $dbh->commit() unless ($params{dbh});
233 $main::lxdebug->leave_sub();
237 $main::lxdebug->enter_sub();
242 my $myconfig = \%main::myconfig;
243 my $form = $main::form;
244 my $vc = $params{vc} eq 'customer' ? 'customer' : 'vendor';
245 my $arap = $params{vc} eq 'customer' ? 'ar' : 'ap';
247 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
250 'id' => [ 'se.id', ],
251 'export_date' => [ 'se.itime', ],
252 'employee' => [ 'e.name', 'se.id', ],
253 'executed' => [ 'se.executed', 'se.id', ],
254 'closed' => [ 'se.closed', 'se.id', ],
257 my %sort_spec = create_sort_spec('defs' => \%sort_columns, 'default' => 'id', 'column' => $params{sortorder}, 'dir' => $params{sortdir});
259 my (@where, @values, @where_sub, @values_sub, %joins_sub);
261 my $filter = $params{filter} || { };
263 foreach (qw(executed closed)) {
264 push @where, $filter->{$_} ? "se.$_" : "NOT se.$_" if (exists $filter->{$_});
267 my %operators = ('from' => '>=',
270 foreach my $dir (qw(from to)) {
271 next unless ($filter->{"export_date_${dir}"});
272 push @where, "se.itime $operators{$dir} ?::date";
273 push @values, $filter->{"export_date_${dir}"};
276 if ($filter->{invnumber}) {
277 push @where_sub, "arap.invnumber ILIKE ?";
278 push @values_sub, '%' . $filter->{invnumber} . '%';
279 $joins_sub{$arap} = 1;
283 push @where_sub, "vc.name ILIKE ?";
284 push @values_sub, '%' . $filter->{vc} . '%';
285 $joins_sub{$arap} = 1;
289 foreach my $type (qw(requested_execution execution)) {
290 foreach my $dir (qw(from to)) {
291 next unless ($filter->{"${type}_date_${dir}"});
292 push @where_sub, "(items.${type}_date IS NOT NULL) AND (items.${type}_date $operators{$dir} ?)";
293 push @values_sub, $filter->{"${type}_date_${_}"};
299 $joins_sub .= " LEFT JOIN ${arap} arap ON (items.${arap}_id = arap.id)" if ($joins_sub{$arap});
300 $joins_sub .= " LEFT JOIN ${vc} vc ON (arap.${vc}_id = vc.id)" if ($joins_sub{vc});
302 my $where_sub = join(' AND ', map { "(${_})" } @where_sub);
304 my $query_sub = qq|se.id IN (SELECT items.sepa_export_id
305 FROM sepa_export_items items
309 push @where, $query_sub;
310 push @values, @values_sub;
313 push @where, 'se.vc = ?';
316 my $where = @where ? ' WHERE ' . join(' AND ', map { "(${_})" } @where) : '';
319 qq|SELECT se.id, se.employee_id, se.executed, se.closed, itime::date AS export_date,
324 CASE WHEN COALESCE(emp.name, '') <> '' THEN emp.name ELSE emp.login END AS name
326 ) AS e ON (se.employee_id = e.id)
328 ORDER BY $sort_spec{sql}|;
330 my $results = selectall_hashref_query($form, $dbh, $query, @values);
332 $main::lxdebug->leave_sub();
338 $main::lxdebug->enter_sub();
343 Common::check_params(\%params, qw(items));
345 my $myconfig = \%main::myconfig;
346 my $form = $main::form;
347 my $vc = $params{vc} eq 'customer' ? 'customer' : 'vendor';
348 my $arap = $params{vc} eq 'customer' ? 'ar' : 'ap';
349 my $mult = $params{vc} eq 'customer' ? -1 : 1;
352 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
354 my @items = ref $params{items} eq 'ARRAY' ? @{ $params{items} } : ($params{items});
357 'get_item' => [ qq|SELECT sei.*
358 FROM sepa_export_items sei
361 'get_arap' => [ qq|SELECT at.chart_id
363 LEFT JOIN chart c ON (at.chart_id = c.id)
365 AND ((c.link LIKE '%:${ARAP}') OR (c.link LIKE '${ARAP}:%') OR (c.link = '${ARAP}'))
368 'add_acc_trans' => [ qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, gldate, source, memo, taxkey, tax_id , chart_link)
369 VALUES (?, ?, ?, ?, current_date, ?, '', 0, (SELECT id FROM tax WHERE taxkey=0 LIMIT 1), (SELECT link FROM chart WHERE id=?))| ],
371 'update_arap' => [ qq|UPDATE ${arap}
375 'finish_item' => [ qq|UPDATE sepa_export_items
376 SET execution_date = ?, executed = TRUE
379 'has_unexecuted' => [ qq|SELECT sei1.id
380 FROM sepa_export_items sei1
381 WHERE (sei1.sepa_export_id = (SELECT sei2.sepa_export_id
382 FROM sepa_export_items sei2
384 AND NOT COALESCE(sei1.executed, FALSE)
387 'do_close' => [ qq|UPDATE sepa_export
388 SET executed = TRUE, closed = TRUE
392 map { unshift @{ $_ }, prepare_query($form, $dbh, $_->[0]) } values %handles;
394 foreach my $item (@items) {
395 my $item_id = conv_i($item->{id});
397 # Retrieve the item data belonging to the ID.
398 do_statement($form, @{ $handles{get_item} }, $item_id);
399 my $orig_item = $handles{get_item}->[0]->fetchrow_hashref();
401 next if (!$orig_item);
403 # Retrieve the invoice's AR/AP chart ID.
404 do_statement($form, @{ $handles{get_arap} }, $orig_item->{"${arap}_id"});
405 my ($arap_chart_id) = $handles{get_arap}->[0]->fetchrow_array();
407 # Record the payment in acc_trans offsetting AR/AP.
408 do_statement($form, @{ $handles{add_acc_trans} }, $orig_item->{"${arap}_id"}, $arap_chart_id, -1 * $mult * $orig_item->{amount}, $item->{execution_date}, '', $arap_chart_id);
409 do_statement($form, @{ $handles{add_acc_trans} }, $orig_item->{"${arap}_id"}, $orig_item->{chart_id}, $mult * $orig_item->{amount}, $item->{execution_date}, $orig_item->{reference},
410 $orig_item->{chart_id});
412 # Update the invoice to reflect the new paid amount.
413 do_statement($form, @{ $handles{update_arap} }, $orig_item->{amount}, $orig_item->{"${arap}_id"});
415 # Update datepaid of invoice. set_datepaid (which has some extra logic)
416 # finds the date from acc_trans, where the payment has already been
417 # recorded above, so we don't need to explicitly pass
418 # $item->{execution_date}
419 IO->set_datepaid(table => "$arap", id => $orig_item->{"${arap}_id"}, dbh => $dbh);
421 # Update the item to reflect that it has been posted.
422 do_statement($form, @{ $handles{finish_item} }, $item->{execution_date}, $item_id);
424 # Check whether or not we can close the export itself if there are no unexecuted items left.
425 do_statement($form, @{ $handles{has_unexecuted} }, $item_id);
426 my ($has_unexecuted) = $handles{has_unexecuted}->[0]->fetchrow_array();
428 if (!$has_unexecuted) {
429 do_statement($form, @{ $handles{do_close} }, $orig_item->{sepa_export_id});
433 map { $_->[0]->finish() } values %handles;
435 $dbh->commit() unless ($params{dbh});
437 $main::lxdebug->leave_sub();