843a32f689ea29735d29581eb9461065b97f154c
[kivitendo-erp.git] / bin / mozilla / invoice_io.pl
1 #=====================================================================
2 # LX-Office ERP
3 # Copyright (C) 2004
4 # Based on SQL-Ledger Version 2.1.9
5 # Web http://www.lx-office.org
6 #############################################################################
7 # Veraendert 2005-01-05 - Marco Welter <mawe@linux-studio.de> - Neue Optik  #
8 #############################################################################
9 # SQL-Ledger, Accounting
10 # Copyright (c) 1998-2002
11 #
12 #  Author: Dieter Simader
13 #   Email: dsimader@sql-ledger.org
14 #     Web: http://www.sql-ledger.org
15 #
16 #
17 # This program is free software; you can redistribute it and/or modify
18 # it under the terms of the GNU General Public License as published by
19 # the Free Software Foundation; either version 2 of the License, or
20 # (at your option) any later version.
21 #
22 # This program is distributed in the hope that it will be useful,
23 # but WITHOUT ANY WARRANTY; without even the implied warranty of
24 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25 # GNU General Public License for more details.
26 # You should have received a copy of the GNU General Public License
27 # along with this program; if not, write to the Free Software
28 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 #
30 #######################################################################
31 #
32 # common routines used in is, ir but not in oe
33 #
34 #######################################################################
35
36 use CGI;
37 use List::Util qw(max);
38
39 use SL::Common;
40 use SL::CT;
41 use SL::IC;
42
43 require "bin/mozilla/common.pl";
44
45 use strict;
46
47 # any custom scripts for this one
48 if (-f "bin/mozilla/custom_invoice_io.pl") {
49   eval { require "bin/mozilla/custom_invoice_io.pl"; };
50 }
51 if (-f "bin/mozilla/$main::form->{login}_invoice_io.pl") {
52   eval { require "bin/mozilla/$main::form->{login}_invoice_io.pl"; };
53 }
54
55 1;
56
57 # end of main
58
59 # this is for our long dates
60 # $locale->text('January')
61 # $locale->text('February')
62 # $locale->text('March')
63 # $locale->text('April')
64 # $locale->text('May ')
65 # $locale->text('June')
66 # $locale->text('July')
67 # $locale->text('August')
68 # $locale->text('September')
69 # $locale->text('October')
70 # $locale->text('November')
71 # $locale->text('December')
72
73 # this is for our short month
74 # $locale->text('Jan')
75 # $locale->text('Feb')
76 # $locale->text('Mar')
77 # $locale->text('Apr')
78 # $locale->text('May')
79 # $locale->text('Jun')
80 # $locale->text('Jul')
81 # $locale->text('Aug')
82 # $locale->text('Sep')
83 # $locale->text('Oct')
84 # $locale->text('Nov')
85 # $locale->text('Dec')
86 use SL::IS;
87 use SL::PE;
88 use SL::AM;
89 use Data::Dumper;
90
91 sub set_pricegroup {
92   $main::lxdebug->enter_sub();
93
94   my $form     = $main::form;
95   my %myconfig = %main::myconfig;
96   my $locale   = $main::locale;
97
98   my $rowcount = shift;
99   for my $j (1 .. $rowcount) {
100     my $pricegroup_old = $form->{"pricegroup_old_$j"};
101     if ($form->{PRICES}{$j}) {
102       my $len    = 0;
103       my $prices = '<option value="--">' . $locale->text("none (pricegroup)") . '</option>';
104       my $price  = 0;
105       foreach my $item (@{ $form->{PRICES}{$j} }) {
106
107         #$price = $form->round_amount($myconfig,  $item->{price}, 5);
108         #$price = $form->format_amount($myconfig, $item->{price}, 2);
109         my $price         = $item->{price};
110         my $pricegroup_id = $item->{pricegroup_id};
111         my $pricegroup    = $item->{pricegroup};
112
113         # build drop down list for pricegroups
114         $prices .=
115           qq|<option value="$price--$pricegroup_id"$item->{selected}>$pricegroup</option>\n|;
116
117         $len += 1;
118
119         #        map {
120         #               $form->{"${_}_$j"} =
121         #               $form->format_amount(\%myconfig, $form->{"${_}_$j"})
122         #              } qw(sellprice price_new price_old);
123
124         # set new selectedpricegroup_id and prices for "Preis"
125         if ($item->{selected} && ($pricegroup_id != 0)) {
126           $form->{"pricegroup_old_$j"} = $pricegroup_id;
127           $form->{"price_new_$j"}      = $price;
128           # edit: don't change the sellprice here
129           # $form->{"sellprice_$j"}      = $price;   # this must only be updated for existing articles, not new ones
130         }
131         if ($pricegroup_id == 0) {
132           $form->{"price_new_$j"} = $form->{"sellprice_$j"};
133         }
134       }
135       $form->{"prices_$j"} = $prices;
136     }
137   }
138   $main::lxdebug->leave_sub();
139 }
140
141 sub display_form {
142   $main::lxdebug->enter_sub();
143
144   my $form     = $main::form;
145   my %myconfig = %main::myconfig;
146
147   $main::auth->assert('part_service_assembly_edit   | vendor_invoice_edit       | sales_order_edit    | invoice_edit |' .
148                 'request_quotation_edit       | sales_quotation_edit      | purchase_order_edit | '.
149                 'purchase_delivery_order_edit | sales_delivery_order_edit');
150
151   relink_accounts();
152   retrieve_partunits() if ($form->{type} =~ /_delivery_order$/);
153
154   my $new_rowcount = $form->{"rowcount"} * 1 + 1;
155   $form->{"project_id_${new_rowcount}"} = $form->{"globalproject_id"};
156
157   $form->language_payment(\%myconfig);
158
159   # if we have a display_form
160   if ($form->{display_form}) {
161     call_sub($form->{"display_form"});
162     ::end_of_request();
163   }
164
165   Common::webdav_folder($form);
166
167   #   if (   $form->{print_and_post}
168   #       && $form->{second_run}
169   #       && ($form->{action} eq "display_form")) {
170   #     for (keys %$form) { $old_form->{$_} = $form->{$_} }
171   #     $old_form->{rowcount}++;
172   #
173   #     #$form->{rowcount}--;
174   #     #$form->{rowcount}--;
175   #
176   #     $form->{print_and_post} = 0;
177   #
178   #     &print_form($old_form);
179   #     ::end_of_request();
180   #   }
181   #
182   #   $form->{action}   = "";
183   #   $form->{resubmit} = 0;
184   #
185   #   if ($form->{print_and_post} && !$form->{second_run}) {
186   #     $form->{second_run} = 1;
187   #     $form->{action}     = "display_form";
188   #     $form->{rowcount}--;
189   #     my $rowcount = $form->{rowcount};
190   #
191   #     # get pricegroups for parts
192   #     IS->get_pricegroups_for_parts(\%myconfig, \%$form);
193   #
194   #     # build up html code for prices_$i
195   #     set_pricegroup($rowcount);
196   #
197   #     $form->{resubmit} = 1;
198   #
199   #   }
200   &form_header;
201
202   {
203     no strict 'refs';
204
205     my $numrows    = ++$form->{rowcount};
206     my $subroutine = "display_row";
207
208     if ($form->{item} =~ /(part|service)/) {
209       #set preisgruppenanzahl
210       $numrows    = $form->{price_rows};
211       $subroutine = "price_row";
212
213       &{$subroutine}($numrows);
214
215       $numrows    = ++$form->{makemodel_rows};
216       $subroutine = "makemodel_row";
217     }
218     if ($form->{item} eq 'assembly') {
219       $numrows    = $form->{price_rows};
220       $subroutine = "price_row";
221
222       &{$subroutine}($numrows);
223
224       $numrows    = ++$form->{makemodel_rows};
225       $subroutine = "makemodel_row";
226
227       # assemblies are built from components, they aren't purchased from a vendor
228       # also the lastcost_$i from makemodel conflicted with the component lastcost_$i 
229       # so we don't need the makemodel rows for assemblies 
230       # create makemodel rows
231       # &{$subroutine}($numrows);
232
233       $numrows    = ++$form->{assembly_rows};
234       $subroutine = "assembly_row";
235     }
236
237     # create rows
238     &{$subroutine}($numrows) if $numrows;
239   }
240
241   &form_footer;
242
243   $main::lxdebug->leave_sub();
244 }