1 #=====================================================================
4 # Based on SQL-Ledger Version 2.1.9
5 # Web http://www.lx-office.org
7 #=====================================================================
11 #=====================================================================
16 use List::Util qw(max);
23 use SL::DB::Manager::Customer;
25 my $DEFINITION = <<SQL;
27 Column | Type | Modifiers
28 -------------------+-----------------------------+------------------------------------------
29 id | integer | not null default nextval('id'::regclass)
30 vc_id | integer | not null
33 text_created_for | text |
40 employee_id | integer |
41 employee_position | text |
42 salesman_id | integer |
43 salesman_position | text |
44 itime | timestamp without time zone | default now()
45 mtime | timestamp without time zone |
46 page_created_for | text |
51 "letter_pkey" PRIMARY KEY, btree (id)
52 Foreign-key constraints:
53 "letter_cp_id_fkey" FOREIGN KEY (cp_id) REFERENCES contacts(cp_id)
54 "letter_employee_id_fkey" FOREIGN KEY (employee_id) REFERENCES employee(id)
55 "letter_salesman_id_fkey" FOREIGN KEY (salesman_id) REFERENCES employee(id)
62 # die 'not a setter' if @_;
64 # return unless $self->{customer_id};
66 # # resolve customer_obj
70 my $class = ref $_[0] || $_[0]; shift;
74 $ref = ref $_[0] eq 'HASH' ? $ref : \%params; # don't like it either...
76 my $self = bless $ref, $class;
78 $self->_lastname_used;
79 $self->_resolve_customer;
87 my $dbh = $::form->get_standard_dbh;
88 ($self->{id}) = selectfirst_array_query($::form, $dbh, "select nextval('id')");
90 do_query($::form, $dbh, <<SQL, $self->{id}, $self->{customer_id});
91 INSERT INTO letter (id, vc_id) VALUES (?, ?);
97 my $dbh = $::form->get_standard_dbh;
98 ($self->{draft_id}) = selectfirst_array_query($::form, $dbh, "select nextval('id')");
100 do_query($::form, $dbh, <<SQL, $self->{draft_id}, $self->{customer_id});
101 INSERT INTO letter_draft (id, vc_id) VALUES (?, ?);
107 $::lxdebug->enter_sub;
111 my $dbh = $::form->get_standard_dbh;
112 my ($table, $update_value);
114 if ($params{draft}) {
115 $self->_create_draft unless $self->{draft_id};
116 $table = 'letter_draft';
117 $update_value = 'draft_id';
119 $self->_create unless $self->{id};
121 $update_value = 'id';
124 my %fields = __PACKAGE__->_get_fields;
125 my %field_mappings = __PACKAGE__->_get_field_mappings;
129 my @update_fields = keys %fields;
130 my $set_clause = join ', ', map { "$_ = ?" } @update_fields;
131 my @values = map { _escaper($_)->( $self->{ $field_mappings{$_} || $_ } ) } @update_fields, $update_value;
133 my $query = "UPDATE $table SET $set_clause WHERE id = ?";
135 do_query($::form, $dbh, $query, @values);
139 $::lxdebug->leave_sub;
143 $::lxdebug->enter_sub;
145 my $class = ref $_[0] || $_[0]; shift;
146 my $myconfig = \%main::myconfig;
147 my $form = $main::form;
148 my $dbh = $form->get_standard_dbh($myconfig);
150 my $letter_table = 'letter';
152 $letter_table = 'letter_draft' if $params{draft};
153 %params = %$form if !scalar keys %params;
155 my (@wheres, @values);
156 my $add_token = sub { add_token(\@wheres, \@values, @_) };
158 $add_token->(col => 'letter.id', val => $params{id}, esc => 'id' ) if $params{id};
159 $add_token->(col => 'letter.letternumber', val => $params{letternumber}, esc => 'substr') if $params{letternumber};
160 $add_token->(col => 'vc.name', val => $params{customer}, esc => 'substr') if $params{customer};
161 $add_token->(col => 'vc.id', val => $params{customer_id}, esc => 'id' ) if $params{customer_id};
162 $add_token->(col => 'letter.cp_id', val => $params{cp_id}, esc => 'id' ) if $params{cp_id};
163 $add_token->(col => 'ct.cp_name', val => $params{contact}, esc => 'substr') if $params{contact};
164 $add_token->(col => 'letter.subject', val => $params{subject}, esc => 'substr') if $params{subject};
165 $add_token->(col => 'letter.body', val => $params{body}, esc => 'substr') if $params{body};
166 $add_token->(col => 'letter.date', val => $params{date_from}, method => '>=' ) if $params{date_from};
167 $add_token->(col => 'letter.date', val => $params{date_to}, method => '<=' ) if $params{date_to};
170 SELECT $letter_table.*, vc.name AS customer, vc.id AS customer_id, ct.cp_name AS contact FROM $letter_table
171 LEFT JOIN customer vc ON vc.id = $letter_table.vc_id
172 LEFT JOIN contacts ct ON $letter_table.cp_id = ct.cp_id
176 $query .= ' WHERE ' . join ' AND ', @wheres;
179 my @results = selectall_hashref_query($form, $dbh, $query, @values);
180 my @objects = map { $class->new($_) } @results;
182 $::lxdebug->leave_sub;
188 $::lxdebug->enter_sub;
192 do_query($::form, $::form->get_standard_dbh, <<SQL, $self->{id});
193 DELETE FROM letter WHERE id = ?
196 $::form->get_standard_dbh->commit;
198 $::lxdebug->leave_sub;
202 $::lxdebug->enter_sub;
207 my $form = $main::form;
208 my $myconfig = \%main::myconfig;
209 my $dbh = $form->get_standard_dbh($myconfig);
212 return $main::lxdebug->leave_sub() unless (@draft_ids);
214 my $query = qq|DELETE FROM letter_draft WHERE id IN (| . join(", ", map { "?" } @draft_ids) . qq|)|;
215 do_query($form, $dbh, $query, @draft_ids);
219 $::lxdebug->leave_sub;
226 return if $self->{letternumber}
228 && 1 == scalar __PACKAGE__->find(letternumber => $self->{letternumber});
230 $self->{letternumber} = SL::TransNumber->new(type => 'letter', id => $self->{id}, number => $self->{letternumber})->create_unique;
237 unless ($params{_name_selected}) {
238 $::form->{$_} = $self->{$_} for qw(oldcustomer customer selectcustomer customer_id);
240 if (::check_name('customer')) {
241 $self->_set_customer_from($::form);
244 $self->_set_customer_from($::form);
248 sub _set_customer_from {
252 $self->{$_} = $from->{$_} for qw(oldcustomer customer_id customer selectcustomer);
259 $self->{date} ||= $::form->current_date(\%::myconfig);
264 my $table = 'letter';
265 my $draft = $self->{draft};
266 $table = 'letter_draft' if $draft;
269 return $self unless $self && $self->{id}; # no id? dont load.
271 my %mappings = _get_field_mappings();
272 my $mapped_select = join ', ', '*', map { "$_ AS $mappings{$_}" } keys %mappings;
274 my ($db_letter) = selectfirst_hashref_query($::form, $::form->get_standard_dbh, <<SQL, $self->{id});
275 SELECT $mapped_select FROM $table WHERE id = ?
278 $self->update_from($db_letter);
279 $self->_resolve_customer;
280 $self->set_greetings;
281 $self->{draft_id} = delete $self->{id} if $draft; # set draft if we have one
289 my %fields = $self->_get_fields;
291 $fields{$_} = $src->{$_} for qw{customer_id customer selectcustomer oldcustomer}; # customer stuff
293 $self->{$_} = $src->{$_} for keys %fields;
302 my %fields = $self->_get_fields;
303 my %field_mappings = $self->_get_field_mappings;
306 $form->{$_} = _escaper($_)->( $self->{ $field_mappings{$_} || $_ } );
312 die 'not a setter' if @_;
314 return unless $self->{cp_id};
317 # contacts have a custom variable called "mailing"
318 # it contains either a language code or the string "No"
320 my $custom_variables = CVar->get_custom_variables(
321 module => 'Contacts',
323 trans_id => $self->{cp_id},
326 my ($mailing) = grep { $_->{name} eq 'Mailing' } @$custom_variables;
328 return $mailing->{value} eq 'No' ? undef : $mailing->{value};
332 $::lxdebug->enter_sub;
335 return $::lxdebug->leave_sub if $self->{greeting};
337 # automatically set greetings
338 # greetings depend mainly on contact person
339 # my $contact = $self->_get_contact;
341 $self->{greeting} = $::locale->text('Dear Sir or Madam,');
343 $::lxdebug->leave_sub;
347 # wrapper for form lastname_used
348 # sets customer to last used customer,
349 # also used to initalize customer for new objects
352 return if $self->{customer_id};
354 my $saved_form = save_form($::form);
356 $::form->lastname_used($::form->get_standard_dbh, \%::myconfig, 'customer');
358 $self->{customer_id} = $::form->{customer_id};
359 $self->{customer} = $::form->{customer};
361 restore_form($saved_form);
366 sub _resolve_customer {
367 # used if an object is created with only id.
370 return unless $self->{customer_id} && !$self->{customer};
372 # my ($customer) = CT->find_by_id(cv => 'customer', id => $self->{customer_id});
373 # my ($customer) = CT->find_by_id(cv => 'customer', id => $self->{customer_id});
374 # SL/CVar.pm: : $cfg->{type} eq 'customer' ? (SL::DB::Manager::Customer->find_by(id => 1*$ref->{number_value}) || SL::DB::Customer->new)->name
375 $self->{customer} = SL::DB::Manager::Customer->find_by(id => $self->{customer_id})->name; # || SL::DB::Customer->new)->name
380 sub _get_definition {
384 sub _get_field_mappings {
386 vc_id => 'customer_id',
391 my %fields = _get_definition() =~ /(\w+) \s+ \| \s+ (integer|text|timestamp|numeric|date)/xg;
395 my $field_name = shift;
396 my %fields = __PACKAGE__->_get_fields;
398 for ($fields{$field_name}) {
399 return sub { conv_i(shift) } if /integer/;
400 return sub { shift };