# connect to database, turn off autocommit
my $dbh = $provided_dbh ? $provided_dbh : $form->dbconnect_noauto($myconfig);
+ $form->{defaultcurrency} = $form->get_default_currency($myconfig);
my ($query, $sth, @values, $project_id);
my ($allocated, $taxrate, $taxamount, $taxdiff, $item);
curr = ?,
department_id = ?,
storno = ?,
+ storno_id = ?,
globalproject_id = ?,
cp_id = ?,
employee_id = ?
conv_date($form->{duedate}), $taxzone_id,
$form->{taxincluded} ? 't' : 'f',
$form->{notes}, $form->{intnotes}, $form->{currency}, conv_i($form->{department_id}),
- $form->{storno} ? 't' : 'f',
+ $form->{storno} ? 't' : 'f', conv_i($form->{storno_id}),
conv_i($form->{globalproject_id}), conv_i($form->{cp_id}),
conv_i($form->{employee_id}),
conv_i($form->{id}));
my ($query, $sth, $ref, $q_invdate);
- $q_invdate = ", current_date AS invdate" unless $form->{id};
+ if (!$form->{id}) {
+ $q_invdate = qq|, COALESCE((SELECT transdate FROM ar WHERE id = (SELECT MAX(id) FROM ar)), current_date) AS invdate|;
+ if ($form->{vendor_id}) {
+ my $vendor_id = $dbh->quote($form->{vendor_id} * 1);
+ $q_invdate .=
+ qq|, COALESCE((SELECT transdate FROM ar WHERE id = (SELECT MAX(id) FROM ar)), current_date) +
+ COALESCE((SELECT pt.terms_netto
+ FROM vendor v
+ LEFT JOIN payment_terms pt ON (v.payment_id = pt.id)
+ WHERE v.id = $vendor_id),
+ 0) AS duedate|;
+ }
+ }
# get default accounts and last invoice number
foreach my $table_column (qw(p.partnumber p.description pg.partsgroup)) {
my $field = (split m{\.}, $table_column)[1];
next unless $form->{"${field}_${i}"};
- $where .= " AND lower(${table_column}) LIKE ?";
+ $where .= " AND lower(${table_column}) LIKE lower(?)";
push @values, '%' . $form->{"${field}_${i}"} . '%';
}