# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
-# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+# MA 02110-1335, USA.
#======================================================================
#
# Inventory invoicing module
use SL::IO;
use SL::TransNumber;
use SL::DB::Default;
+use SL::DB::Draft;
use SL::DB::Tax;
use SL::DB::TaxZone;
use SL::TransNumber;
use Data::Dumper;
use strict;
+use constant PCLASS_OK => 0;
+use constant PCLASS_NOTFORSALE => 1;
+use constant PCLASS_NOTFORPURCHASE => 2;
sub invoice_details {
$main::lxdebug->enter_sub();
# so that they can be sorted in later
my %prepared_template_arrays = IC->prepare_parts_for_printing(myconfig => $myconfig, form => $form);
my @prepared_arrays = keys %prepared_template_arrays;
+ my @separate_totals = qw(non_separate_subtotal);
my $ic_cvar_configs = CVar->get_configs(module => 'IC');
my $project_cvar_configs = CVar->get_configs(module => 'Projects');
push @{ $form->{TEMPLATE_ARRAYS}->{discount_nofmt} }, ($discount != 0) ? $discount * -1 : '';
push @{ $form->{TEMPLATE_ARRAYS}->{p_discount} }, $form->{"discount_$i"};
+ if ( $prepared_template_arrays{separate}[$i - 1] ) {
+ my $pabbr = $prepared_template_arrays{separate}[$i - 1];
+ if ( ! $form->{"separate_${pabbr}_subtotal"} ) {
+ push @separate_totals , "separate_${pabbr}_subtotal";
+ $form->{"separate_${pabbr}_subtotal"} = 0;
+ }
+ $form->{"separate_${pabbr}_subtotal"} += $linetotal;
+ } else {
+ $form->{non_separate_subtotal} += $linetotal;
+ }
+
$form->{total} += $linetotal;
$form->{nodiscount_total} += $nodiscount_linetotal;
$form->{discount_total} += $discount;
$form->{delivery_term}->description_long($form->{delivery_term}->translated_attribute('description_long', $form->{language_id})) if $form->{delivery_term} && $form->{language_id};
$form->{username} = $myconfig->{name};
+ $form->{$_} = $form->format_amount($myconfig, $form->{$_}, 2) for @separate_totals;
$main::lxdebug->leave_sub();
}
'trans_id' => $form->{customer_id});
map { $form->{"vc_cvar_$_->{name}"} = $_->{value} } @{ $custom_variables };
+ if ($form->{cp_id}) {
+ $custom_variables = CVar->get_custom_variables(dbh => $dbh,
+ module => 'Contacts',
+ trans_id => $form->{cp_id});
+ $form->{"cp_cvar_$_->{name}"} = $_->{value} for @{ $custom_variables };
+ }
+
$form->{cp_greeting} = GenericTranslations->get('dbh' => $dbh,
'translation_type' => 'greetings::' . ($form->{cp_gender} eq 'f' ? 'female' : 'male'),
'language_id' => $language_id,
# need the current dbh to get the not yet committed mtime
$form->new_lastmtime('ar', $provided_dbh);
- $form->{name} = $form->{customer};
- $form->{name} =~ s/--\Q$form->{customer_id}\E//;
-
# add shipto
if (!$form->{shipto_id}) {
$form->add_shipto($dbh, $form->{id}, "AR");
do_query($form, $dbh, $query, @orphaned_ids);
}
+ if ($form->{draft_id}) {
+ SL::DB::Manager::Draft->delete_all(where => [ id => delete($form->{draft_id}) ]);
+ }
+
# safety check datev export
if ($::instance_conf->get_datev_check_on_sales_invoice) {
- my $transdate = $::form->{invdate} ? DateTime->from_lxoffice($::form->{invdate}) : undef;
- $transdate ||= DateTime->today;
my $datev = SL::DATEV->new(
- exporttype => DATEV_ET_BUCHUNGEN,
- format => DATEV_FORMAT_KNE,
dbh => $dbh,
trans_id => $form->{id},
);
- $datev->export;
+ $datev->generate_datev_data;
if ($datev->errors) {
die join "\n", $::locale->text('DATEV check returned errors:'), $datev->errors;
# get shipto
$query = qq|SELECT * FROM shipto WHERE (trans_id = ?) AND (module = 'AR')|;
$ref = selectfirst_hashref_query($form, $dbh, $query, $id);
- delete($ref->{id});
- map { $form->{$_} = $ref->{$_} } keys %$ref;
+ $form->{$_} = $ref->{$_} for grep { m{^shipto(?!_id$)} } keys %$ref;
# get printed, emailed
$query = qq|SELECT printed, emailed, spoolfile, formname FROM status WHERE trans_id = ?|;
my $payment_id;
# get customer
+ my $where = '';
+ if ($cid) {
+ $where .= 'AND c.id = ?';
+ push @values, $cid;
+ }
$query =
qq|SELECT
c.id AS customer_id, c.name AS customer, c.discount as customer_discount, c.creditlimit,
FROM customer c
LEFT JOIN business b ON (b.id = c.business_id)
LEFT JOIN currencies cu ON (c.currency_id=cu.id)
- WHERE c.id = ?|;
- push @values, $cid;
+ WHERE 1 = 1 $where|;
$ref = selectfirst_hashref_query($form, $dbh, $query, @values);
delete $ref->{salesman_id} if !$ref->{salesman_id};
}
$sth->finish;
- # setup last accounts used for this customer
- if (!$form->{id} && $form->{type} !~ /_(order|quotation)/) {
- $query =
- qq|SELECT c.id, c.accno, c.description, c.link, c.category
- FROM chart c
- JOIN acc_trans ac ON (ac.chart_id = c.id)
- JOIN ar a ON (a.id = ac.trans_id)
- WHERE a.customer_id = ?
- AND NOT (c.link LIKE '%_tax%' OR c.link LIKE '%_paid%')
- AND a.id IN (SELECT max(a2.id) FROM ar a2 WHERE a2.customer_id = ?)|;
- $sth = prepare_execute_query($form, $dbh, $query, $cid, $cid);
-
- my $i = 0;
- while ($ref = $sth->fetchrow_hashref('NAME_lc')) {
- if ($ref->{category} eq 'I') {
- $i++;
- $form->{"AR_amount_$i"} = "$ref->{accno}--$ref->{description}";
-
- if ($form->{initial_transdate}) {
- my $tax_query =
- qq|SELECT tk.tax_id, t.rate
- FROM taxkeys tk
- LEFT JOIN tax t ON tk.tax_id = t.id
- WHERE (tk.chart_id = ?) AND (startdate <= date(?))
- ORDER BY tk.startdate DESC
- LIMIT 1|;
- my ($tax_id, $rate) =
- selectrow_query($form, $dbh, $tax_query, $ref->{id},
- $form->{initial_transdate});
- $form->{"taxchart_$i"} = "${tax_id}--${rate}";
- }
- }
- if ($ref->{category} eq 'A') {
- $form->{ARselected} = $form->{AR_1} = $ref->{accno};
- }
- }
- $sth->finish;
- $form->{rowcount} = $i if ($i && !$form->{type});
- }
-
$main::lxdebug->leave_sub();
}
FROM taxzone_charts tc
WHERE tc.buchungsgruppen_id = p.buchungsgruppen_id and tc.taxzone_id = ${taxzone_id}) = c3.id)
LEFT JOIN partsgroup pg ON (pg.id = p.partsgroup_id)
- LEFT JOIN parts_classifications pt ON (pt.id = p.classification_id)
+ LEFT JOIN part_classifications pt ON (pt.id = p.classification_id)
LEFT JOIN price_factors pfac ON (pfac.id = p.price_factor_id)
WHERE $where|;
my $sth = prepare_execute_query($form, $dbh, $query, @values);
LIMIT 1| ] );
map { push @{ $_ }, prepare_query($form, $dbh, $_->[0]) } @translation_queries;
- my $has_wrong_pclass = 0;
+ my $has_wrong_pclass = PCLASS_OK;
while (my $ref = $sth->fetchrow_hashref('NAME_lc')) {
if ($mm_by_id{$ref->{id}}) {
$ref->{type_and_classific} = $::request->presenter->type_abbreviation($ref->{part_type}).
$::request->presenter->classification_abbreviation($ref->{classification_id});
if (! $ref->{used_for_sale} ) {
- $has_wrong_pclass = 1;
- next;
+ $has_wrong_pclass = PCLASS_NOTFORSALE ;
+ next;
}
# In der Buchungsgruppe ist immer ein Bestandskonto verknuepft, auch wenn
# es sich um eine Dienstleistung handelt. Bei Dienstleistungen muss das
$_->[1]->finish for @translation_queries;
$form->{is_wrong_pclass} = $has_wrong_pclass;
+ $form->{NOTFORSALE} = PCLASS_NOTFORSALE;
+ $form->{NOTFORPURCHASE} = PCLASS_NOTFORPURCHASE;
foreach my $item (@{ $form->{item_list} }) {
my $custom_variables = CVar->get_custom_variables(module => 'IC',
trans_id => $item->{id},
dbh => $dbh,
);
- $form->{is_wrong_pclass} = 0; # one correct type
+ $form->{is_wrong_pclass} = PCLASS_OK; # one correct type
map { $item->{"ic_cvar_" . $_->{name} } = $_->{value} } @{ $custom_variables };
}
$main::lxdebug->leave_sub();