s/create/new/g; and use fields('table_name')
[freeside.git] / htdocs / edit / process / part_pkg.cgi
1 #!/usr/bin/perl -Tw
2 #
3 # $Id: part_pkg.cgi,v 1.6 1999-01-18 22:47:56 ivan Exp $
4 #
5 # process/part_pkg.cgi: Edit package definitions (process form)
6 #
7 # ivan@sisd.com 97-dec-10
8 #
9 # don't update non-changing records in part_svc (causing harmless but annoying
10 # "Records identical" errors). ivan@sisd.com 98-feb-19
11 #
12 # Changes to allow page to work at a relative position in server
13 #       bmccane@maxbaud.net     98-apr-3
14 #
15 # Added `|| 0 ' when getting quantity off web page ivan@sisd.com 98-jun-4
16 #
17 # lose background, FS::CGI ivan@sisd.com 98-sep-2
18 #
19 # $Log: part_pkg.cgi,v $
20 # Revision 1.6  1999-01-18 22:47:56  ivan
21 # s/create/new/g; and use fields('table_name')
22 #
23 # Revision 1.5  1998/12/30 23:03:29  ivan
24 # bugfixes; fields isn't exported by derived classes
25 #
26 # Revision 1.4  1998/12/17 08:40:24  ivan
27 # s/CGI::Request/CGI.pm/; etc
28 #
29 # Revision 1.3  1998/11/21 07:17:58  ivan
30 # bugfix to work for regular aswell as custom pricing
31 #
32 # Revision 1.2  1998/11/15 13:16:15  ivan
33 # first pass as per-user custom pricing
34 #
35
36 use strict;
37 use CGI;
38 use CGI::Carp qw(fatalsToBrowser);
39 use FS::UID qw(cgisuidsetup);
40 use FS::CGI qw(eidiot popurl);
41 use FS::Record qw(qsearch qsearchs fields);
42 use FS::part_pkg;
43 use FS::pkg_svc;
44 use FS::cust_pkg;
45
46 my($cgi)=new CGI;
47 &cgisuidsetup($cgi);
48
49 my($pkgpart)=$cgi->param('pkgpart');
50
51 my($old)=qsearchs('part_pkg',{'pkgpart'=>$pkgpart}) if $pkgpart;
52
53 my($new)=new FS::part_pkg ( {
54   map {
55     $_, scalar($cgi->param($_));
56   } fields('part_pkg')
57 } );
58
59 local $SIG{HUP} = 'IGNORE';
60 local $SIG{INT} = 'IGNORE';
61 local $SIG{QUIT} = 'IGNORE';
62 local $SIG{TERM} = 'IGNORE';
63 local $SIG{TSTP} = 'IGNORE';
64
65 if ( $pkgpart ) {
66   my($error)=$new->replace($old);
67   eidiot($error) if $error;
68 } else {
69   my($error)=$new->insert;
70   eidiot($error) if $error;
71   $pkgpart=$new->getfield('pkgpart');
72 }
73
74 my($part_svc);
75 foreach $part_svc (qsearch('part_svc',{})) {
76 # don't update non-changing records in part_svc (causing harmless but annoying
77 # "Records identical" errors). ivan@sisd.com 98-jan-19
78   #my($quantity)=$cgi->param('pkg_svc'. $part_svc->getfield('svcpart')),
79   my($quantity)=$cgi->param('pkg_svc'. $part_svc->svcpart) || 0,
80   my($old_pkg_svc)=qsearchs('pkg_svc',{
81     'pkgpart'  => $pkgpart,
82     'svcpart'  => $part_svc->getfield('svcpart'),
83   });
84   my($old_quantity)=$old_pkg_svc ? $old_pkg_svc->quantity : 0;
85   next unless $old_quantity != $quantity; #!here
86   my($new_pkg_svc)=new FS::pkg_svc({
87     'pkgpart'  => $pkgpart,
88     'svcpart'  => $part_svc->getfield('svcpart'),
89     #'quantity' => $cgi->param('pkg_svc'. $part_svc->getfield('svcpart')),
90     'quantity' => $quantity, 
91   });
92   if ($old_pkg_svc) {
93     my($error)=$new_pkg_svc->replace($old_pkg_svc);
94     eidiot($error) if $error;
95   } else {
96     my($error)=$new_pkg_svc->insert;
97     eidiot($error) if $error;
98   }
99 }
100
101 unless ( $cgi->param('pkgnum') && $cgi->param('pkgnum') =~ /^(\d+)$/ ) {
102   print $cgi->redirect(popurl(3). "browse/part_pkg.cgi");
103 } else {
104   my($old_cust_pkg) = qsearchs( 'cust_pkg', { 'pkgnum' => $1 } );
105   my %hash = $old_cust_pkg->hash;
106   $hash{'pkgpart'} = $pkgpart;
107   my($new_cust_pkg) = new FS::cust_pkg \%hash;
108   my $error = $new_cust_pkg->replace($old_cust_pkg);
109   eidiot "Error modifying cust_pkg record: $error\n" if $error;
110   print $cgi->redirect(popurl(3). "view/cust_main.cgi?". $new_cust_pkg->custnum);
111 }
112
113