my $dbh = $form->get_standard_dbh;
my $sth;
- my $query = qq|SELECT date | . conv_dateq($form->{duedate}) . qq| - date | . conv_dateq($form->{invdate}) . qq| AS terms|;
- ($form->{terms}) = selectrow_query($form, $dbh, $query);
-
my (@project_ids);
$form->{TEMPLATE_ARRAYS} = {};
$sortorder = qq|ORDER BY a.oid|;
}
- $query =
+ my $query =
qq|SELECT p.partnumber, p.description, p.unit, a.qty, pg.partsgroup
FROM assembly a
JOIN parts p ON (a.parts_id = p.id)
transdate = ?, orddate = ?, quodate = ?, customer_id = ?,
amount = ?, netamount = ?, paid = ?,
duedate = ?, deliverydate = ?, invoice = ?, shippingpoint = ?,
- shipvia = ?, terms = ?, notes = ?, intnotes = ?,
+ shipvia = ?, notes = ?, intnotes = ?,
currency_id = (SELECT id FROM currencies WHERE name = ?),
department_id = ?, payment_id = ?, taxincluded = ?,
type = ?, language_id = ?, taxzone_id = ?, shipto_id = ?,
conv_date($form->{"invdate"}), conv_date($form->{"orddate"}), conv_date($form->{"quodate"}), conv_i($form->{"customer_id"}),
$amount, $netamount, $form->{"paid"},
conv_date($form->{"duedate"}), conv_date($form->{"deliverydate"}), '1', $form->{"shippingpoint"},
- $form->{"shipvia"}, conv_i($form->{"terms"}), $restricter->process($form->{"notes"}), $form->{"intnotes"},
+ $form->{"shipvia"}, $restricter->process($form->{"notes"}), $form->{"intnotes"},
$form->{"currency"}, conv_i($form->{"department_id"}), conv_i($form->{"payment_id"}), $form->{"taxincluded"} ? 't' : 'f',
$form->{"type"}, conv_i($form->{"language_id"}), conv_i($form->{"taxzone_id"}), conv_i($form->{"shipto_id"}),
conv_i($form->{"employee_id"}), conv_i($form->{"salesman_id"}), conv_i($form->{storno_id}), $form->{"storno"} ? 't' : 'f',
a.invnumber, a.ordnumber, a.quonumber, a.cusordnumber,
a.orddate, a.quodate, a.globalproject_id,
a.transdate AS invdate, a.deliverydate, a.paid, a.storno, a.gldate,
- a.shippingpoint, a.shipvia, a.terms, a.notes, a.intnotes, a.taxzone_id,
+ a.shippingpoint, a.shipvia, a.notes, a.intnotes, a.taxzone_id,
a.duedate, a.taxincluded, (SELECT cu.name FROM currencies cu WHERE cu.id=a.currency_id) AS currency, a.shipto_id, a.cp_id,
a.employee_id, a.salesman_id, a.payment_id,
a.language_id, a.delivery_customer_id, a.delivery_vendor_id, a.type,
my $dateformat = $myconfig->{dateformat};
$dateformat .= "yy" if $myconfig->{dateformat} !~ /^y/;
- my (@values, $duedate, $ref, $query);
-
- if ($form->{invdate}) {
- $duedate = "to_date(?, '$dateformat')";
- push @values, $form->{invdate};
- } else {
- $duedate = "current_date";
- }
+ my (@values, $ref, $query);
my $cid = conv_i($form->{customer_id});
my $payment_id;
- if ($form->{payment_id}) {
- $payment_id = "(pt.id = ?) OR";
- push @values, conv_i($form->{payment_id});
- }
-
# get customer
$query =
qq|SELECT
- c.id AS customer_id, c.name AS customer, c.discount as customer_discount, c.creditlimit, c.terms,
+ c.id AS customer_id, c.name AS customer, c.discount as customer_discount, c.creditlimit,
c.email, c.cc, c.bcc, c.language_id, c.payment_id, c.delivery_term_id,
c.street, c.zipcode, c.city, c.country,
c.notes AS intnotes, c.klass as customer_klass, c.taxzone_id, c.salesman_id, cu.name AS curr,
c.taxincluded_checked, c.direct_debit,
- $duedate + COALESCE(pt.terms_netto, 0) AS duedate,
b.discount AS tradediscount, b.description AS business
FROM customer c
LEFT JOIN business b ON (b.id = c.business_id)
- LEFT JOIN payment_terms pt ON ($payment_id (c.payment_id = pt.id))
LEFT JOIN currencies cu ON (c.currency_id=cu.id)
WHERE c.id = ?|;
push @values, $cid;
$ref = selectfirst_hashref_query($form, $dbh, $query, @values);
delete $ref->{salesman_id} if !$ref->{salesman_id};
+ delete $ref->{payment_id} if $form->{payment_id};
map { $form->{$_} = $ref->{$_} } keys %$ref;
+ if ($form->{payment_id}) {
+ my $reference_date = $form->{invdate} ? DateTime->from_kivitendo($form->{invdate}) : undef;
+ $form->{duedate} = SL::DB::PaymentTerm->new(id => $form->{payment_id})->load->calc_date(reference_date => $reference_date)->to_kivitendo;
+ } else {
+ $form->{duedate} = DateTime->today_local->to_kivitendo;
+ }
+
# use customer currency
$form->{currency} = $form->{curr};