|
package SL::Controller::CustomerVendor;
|
|
|
|
use strict;
|
|
use parent qw(SL::Controller::Base);
|
|
|
|
use List::MoreUtils qw(any);
|
|
|
|
use SL::JSON;
|
|
use SL::DBUtils;
|
|
use SL::Helper::Flash;
|
|
use SL::Locale::String;
|
|
use SL::Util qw(trim);
|
|
use SL::VATIDNr;
|
|
use SL::Webdav;
|
|
use SL::ZUGFeRD;
|
|
use SL::Controller::Helper::GetModels;
|
|
use SL::Controller::Helper::ReportGenerator;
|
|
use SL::Controller::Helper::ParseFilter;
|
|
|
|
use SL::DB::AuthGroup;
|
|
use SL::DB::Customer;
|
|
use SL::DB::Vendor;
|
|
use SL::DB::Business;
|
|
use SL::DB::ContactDepartment;
|
|
use SL::DB::ContactTitle;
|
|
use SL::DB::Employee;
|
|
use SL::DB::Greeting;
|
|
use SL::DB::Language;
|
|
use SL::DB::TaxZone;
|
|
use SL::DB::Note;
|
|
use SL::DB::PaymentTerm;
|
|
use SL::DB::Pricegroup;
|
|
use SL::DB::Price;
|
|
use SL::DB::Contact;
|
|
use SL::DB::FollowUp;
|
|
use SL::DB::FollowUpLink;
|
|
use SL::DB::History;
|
|
use SL::DB::Currency;
|
|
use SL::DB::Invoice;
|
|
use SL::DB::PurchaseInvoice;
|
|
use SL::DB::Order;
|
|
|
|
use Data::Dumper;
|
|
|
|
use Rose::Object::MakeMethods::Generic (
|
|
scalar => [ qw(user_has_edit_rights) ],
|
|
'scalar --get_set_init' => [ qw(customer_models vendor_models zugferd_settings) ],
|
|
);
|
|
|
|
# safety
|
|
__PACKAGE__->run_before(
|
|
'_instantiate_args',
|
|
only => [
|
|
'save',
|
|
'save_and_ap_transaction',
|
|
'save_and_ar_transaction',
|
|
'save_and_close',
|
|
'save_and_invoice',
|
|
'save_and_order',
|
|
'save_and_quotation',
|
|
'save_and_rfq',
|
|
'delete',
|
|
'delete_contact',
|
|
'delete_shipto',
|
|
'delete_additional_billing_address',
|
|
]
|
|
);
|
|
|
|
__PACKAGE__->run_before(
|
|
'_load_customer_vendor',
|
|
only => [
|
|
'edit',
|
|
'show',
|
|
'update',
|
|
'ajaj_get_shipto',
|
|
'ajaj_get_additional_billing_address',
|
|
'ajaj_get_contact',
|
|
'ajax_list_prices',
|
|
]
|
|
);
|
|
|
|
# make sure this comes after _load_customer_vendor
|
|
__PACKAGE__->run_before('_check_auth');
|
|
|
|
__PACKAGE__->run_before(
|
|
'_create_customer_vendor',
|
|
only => [
|
|
'add',
|
|
]
|
|
);
|
|
|
|
__PACKAGE__->run_before('normalize_name');
|
|
|
|
my @ADDITIONAL_BILLING_ADDRESS_COLUMNS = qw(name department_1 department_2 contact street zipcode city country gln email phone fax default_address);
|
|
|
|
sub action_add {
|
|
my ($self) = @_;
|
|
|
|
$self->_pre_render();
|
|
|
|
if ($self->{cv}->is_customer) {
|
|
$self->{cv}->assign_attributes(hourly_rate => $::instance_conf->get_customer_hourly_rate);
|
|
$self->{cv}->salesman_id(SL::DB::Manager::Employee->current->id) if !$::auth->assert('customer_vendor_all_edit', 1);
|
|
}
|
|
|
|
$self->render(
|
|
'customer_vendor/form',
|
|
title => ($self->is_vendor() ? $::locale->text('Add Vendor') : $::locale->text('Add Customer')),
|
|
%{$self->{template_args}}
|
|
);
|
|
}
|
|
|
|
sub action_edit {
|
|
my ($self) = @_;
|
|
|
|
$self->_pre_render();
|
|
$self->render(
|
|
'customer_vendor/form',
|
|
title => ($self->is_vendor() ? $::locale->text('Edit Vendor') : $::locale->text('Edit Customer')),
|
|
%{$self->{template_args}}
|
|
);
|
|
}
|
|
|
|
sub action_show {
|
|
my ($self) = @_;
|
|
|
|
if ($::request->type eq 'json') {
|
|
my $cv_hash;
|
|
if (!$self->{cv}) {
|
|
# TODO error
|
|
} else {
|
|
$cv_hash = $self->{cv}->as_tree;
|
|
$cv_hash->{cvars} = $self->{cv}->cvar_as_hashref;
|
|
}
|
|
|
|
$self->render(\ SL::JSON::to_json($cv_hash), { layout => 0, type => 'json', process => 0 });
|
|
}
|
|
}
|
|
|
|
sub _check_ustid_taxnumber_unique {
|
|
my ($self) = @_;
|
|
|
|
my %cfg;
|
|
if ($self->is_vendor()) {
|
|
%cfg = (should_check => $::instance_conf->get_vendor_ustid_taxnummer_unique,
|
|
manager_class => 'SL::DB::Manager::Vendor',
|
|
err_ustid => t8('A vendor with the same VAT ID already exists.'),
|
|
err_taxnumber => t8('A vendor with the same taxnumber already exists.'),
|
|
);
|
|
|
|
} elsif ($self->is_customer()) {
|
|
%cfg = (should_check => $::instance_conf->get_customer_ustid_taxnummer_unique,
|
|
manager_class => 'SL::DB::Manager::Customer',
|
|
err_ustid => t8('A customer with the same VAT ID already exists.'),
|
|
err_taxnumber => t8('A customer with the same taxnumber already exists.'),
|
|
);
|
|
|
|
} else {
|
|
return;
|
|
}
|
|
|
|
my @errors;
|
|
|
|
if ($cfg{should_check}) {
|
|
my $do_clean_taxnumber = sub { my $n = $_[0]; $n //= ''; $n =~ s{[[:space:].-]+}{}g; return $n};
|
|
|
|
my $clean_ustid = SL::VATIDNr->clean($self->{cv}->ustid);
|
|
my $clean_taxnumber = $do_clean_taxnumber->($self->{cv}->taxnumber);
|
|
|
|
if (!($clean_ustid || $clean_taxnumber)) {
|
|
return t8('VAT ID and/or taxnumber must be given.');
|
|
|
|
} else {
|
|
my $clean_number = $clean_ustid;
|
|
if ($clean_number) {
|
|
my $entries = $cfg{manager_class}->get_all(query => ['!id' => $self->{cv}->id, '!ustid' => undef, '!ustid' => ''], select => ['ustid'], distinct => 1);
|
|
if (any { $clean_number eq SL::VATIDNr->clean($_->ustid) } @$entries) {
|
|
push @errors, $cfg{err_ustid};
|
|
}
|
|
}
|
|
|
|
$clean_number = $clean_taxnumber;
|
|
if ($clean_number) {
|
|
my $entries = $cfg{manager_class}->get_all(query => ['!id' => $self->{cv}->id, '!taxnumber' => undef, '!taxnumber' => ''], select => ['taxnumber'], distinct => 1);
|
|
if (any { $clean_number eq $do_clean_taxnumber->($_->taxnumber) } @$entries) {
|
|
push @errors, $cfg{err_taxnumber};
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return join "\n", @errors if @errors;
|
|
return;
|
|
}
|
|
|
|
sub _save {
|
|
my ($self) = @_;
|
|
|
|
my @errors = $self->{cv}->validate;
|
|
if (@errors) {
|
|
flash('error', @errors);
|
|
$self->_pre_render();
|
|
$self->render(
|
|
'customer_vendor/form',
|
|
title => ($self->is_vendor() ? t8('Edit Vendor') : t8('Edit Customer')),
|
|
%{$self->{template_args}}
|
|
);
|
|
$::dispatcher->end_request;
|
|
}
|
|
|
|
$self->{cv}->greeting(trim $self->{cv}->greeting);
|
|
my $save_greeting = $self->{cv}->greeting
|
|
&& $::instance_conf->get_vc_greetings_use_textfield
|
|
&& SL::DB::Manager::Greeting->get_all_count(where => [description => $self->{cv}->greeting]) == 0;
|
|
|
|
$self->{contact}->cp_title(trim($self->{contact}->cp_title));
|
|
my $save_contact_title = $self->{contact}->cp_title
|
|
&& $::instance_conf->get_contact_titles_use_textfield
|
|
&& SL::DB::Manager::ContactTitle->get_all_count(where => [description => $self->{contact}->cp_title]) == 0;
|
|
|
|
$self->{contact}->cp_abteilung(trim($self->{contact}->cp_abteilung));
|
|
my $save_contact_department = $self->{contact}->cp_abteilung
|
|
&& $::instance_conf->get_contact_departments_use_textfield
|
|
&& SL::DB::Manager::ContactDepartment->get_all_count(where => [description => $self->{contact}->cp_abteilung]) == 0;
|
|
|
|
my $db = $self->{cv}->db;
|
|
|
|
$db->with_transaction(sub {
|
|
my $cvs_by_nr;
|
|
if ( $self->is_vendor() ) {
|
|
if ( $self->{cv}->vendornumber ) {
|
|
$cvs_by_nr = SL::DB::Manager::Vendor->get_all(query => [vendornumber => $self->{cv}->vendornumber]);
|
|
}
|
|
} else {
|
|
if ( $self->{cv}->customernumber ) {
|
|
$cvs_by_nr = SL::DB::Manager::Customer->get_all(query => [customernumber => $self->{cv}->customernumber]);
|
|
}
|
|
}
|
|
|
|
foreach my $entry (@{$cvs_by_nr}) {
|
|
if( $entry->id != $self->{cv}->id ) {
|
|
my $msg =
|
|
$self->is_vendor() ? $::locale->text('This vendor number is already in use.') : $::locale->text('This customer number is already in use.');
|
|
|
|
$::form->error($msg);
|
|
}
|
|
}
|
|
|
|
my $ustid_taxnumber_error = $self->_check_ustid_taxnumber_unique;
|
|
$::form->error($ustid_taxnumber_error) if $ustid_taxnumber_error;
|
|
|
|
$self->{cv}->save(cascade => 1);
|
|
|
|
SL::DB::Greeting->new(description => $self->{cv}->greeting)->save if $save_greeting;
|
|
|
|
$self->{contact}->cp_cv_id($self->{cv}->id);
|
|
if( $self->{contact}->cp_name ne '' || $self->{contact}->cp_givenname ne '' ) {
|
|
SL::DB::ContactTitle ->new(description => $self->{contact}->cp_title) ->save if $save_contact_title;
|
|
SL::DB::ContactDepartment->new(description => $self->{contact}->cp_abteilung)->save if $save_contact_department;
|
|
|
|
$self->{contact}->save(cascade => 1);
|
|
}
|
|
|
|
if( $self->{note}->subject ne '' && $self->{note}->body ne '' ) {
|
|
|
|
if ( !$self->{note_followup}->follow_up_date ) {
|
|
$::form->error($::locale->text('Date missing!'));
|
|
}
|
|
if (!$self->{note_followup}->{created_for_employees}) {
|
|
$::form->error($::locale->text('You must chose a user.'));
|
|
}
|
|
|
|
$self->{note}->trans_id($self->{cv}->id);
|
|
$self->{note}->save();
|
|
|
|
if (delete $self->{note_followup}->{not_done}) {
|
|
$self->{note_followup}->done->delete if $self->{note_followup}->done;
|
|
}
|
|
$self->{note_followup}->save();
|
|
|
|
$self->{note_followup_link}->follow_up_id($self->{note_followup}->id);
|
|
$self->{note_followup_link}->trans_id($self->{cv}->id);
|
|
$self->{note_followup_link}->save();
|
|
|
|
SL::Helper::Flash::flash_later('info', $::locale->text('Follow-Up saved.'));
|
|
}
|
|
|
|
$self->{shipto}->trans_id($self->{cv}->id);
|
|
if(any { $self->{shipto}->$_ ne '' } qw(shiptoname shiptodepartment_1 shiptodepartment_2 shiptostreet shiptozipcode shiptocity shiptocountry shiptogln shiptocontact shiptophone shiptofax shiptoemail)) {
|
|
$self->{shipto}->save(cascade => 1);
|
|
}
|
|
|
|
if ($self->is_customer && any { $self->{additional_billing_address}->$_ ne '' } grep { $_ ne 'default_address' } @ADDITIONAL_BILLING_ADDRESS_COLUMNS) {
|
|
$self->{additional_billing_address}->customer_id($self->{cv}->id);
|
|
$self->{additional_billing_address}->save(cascade => 1);
|
|
|
|
# Make sure only one address per customer has "default address" set.
|
|
if ($self->{additional_billing_address}->default_address) {
|
|
SL::DB::Manager::AdditionalBillingAddress->update_all(
|
|
set => { default_address => 0, },
|
|
where => [
|
|
customer_id => $self->{cv}->id,
|
|
'!id' => $self->{additional_billing_address}->id,
|
|
]);
|
|
}
|
|
}
|
|
|
|
my $snumbers = $self->is_vendor() ? 'vendornumber_'. $self->{cv}->vendornumber : 'customernumber_'. $self->{cv}->customernumber;
|
|
SL::DB::History->new(
|
|
trans_id => $self->{cv}->id,
|
|
snumbers => $snumbers,
|
|
employee_id => SL::DB::Manager::Employee->current->id,
|
|
addition => 'SAVED',
|
|
)->save();
|
|
|
|
if ( $::form->{delete_notes} ) {
|
|
foreach my $note_id (@{ $::form->{delete_notes} }) {
|
|
my $note = SL::DB::Note->new(id => $note_id)->load();
|
|
if ( $note->follow_up ) {
|
|
if ( $note->follow_up->follow_up_link ) {
|
|
$note->follow_up->follow_up_link->delete(cascade => 'delete');
|
|
}
|
|
$note->follow_up->delete(cascade => 'delete');
|
|
}
|
|
$note->delete(cascade => 'delete');
|
|
}
|
|
}
|
|
|
|
1;
|
|
}) || die($db->error);
|
|
|
|
}
|
|
|
|
sub action_save {
|
|
my ($self) = @_;
|
|
|
|
$self->_save();
|
|
|
|
my @redirect_params = (
|
|
action => 'edit',
|
|
id => $self->{cv}->id,
|
|
db => ($self->is_vendor() ? 'vendor' : 'customer'),
|
|
);
|
|
|
|
if ( $self->{contact}->cp_id ) {
|
|
push(@redirect_params, contact_id => $self->{contact}->cp_id);
|
|
}
|
|
|
|
if ( $self->{shipto}->shipto_id ) {
|
|
push(@redirect_params, shipto_id => $self->{shipto}->shipto_id);
|
|
}
|
|
|
|
if ( $self->is_customer && $self->{additional_billing_address}->id ) {
|
|
push(@redirect_params, additional_billing_address_id => $self->{additional_billing_address}->id);
|
|
}
|
|
|
|
$self->redirect_to(@redirect_params);
|
|
}
|
|
|
|
sub action_save_and_close {
|
|
my ($self) = @_;
|
|
|
|
$self->_save();
|
|
|
|
my $msg = $self->is_vendor() ? $::locale->text('Vendor saved') : $::locale->text('Customer saved');
|
|
$::form->redirect($msg);
|
|
}
|
|
|
|
sub _transaction {
|
|
my ($self, $script) = @_;
|
|
|
|
$::auth->assert('gl_transactions | ap_transactions | ar_transactions'.
|
|
'| invoice_edit | vendor_invoice_edit | ' .
|
|
' request_quotation_edit | sales_quotation_edit | sales_order_edit | purchase_order_edit');
|
|
|
|
$self->_save();
|
|
|
|
my $name = $::form->escape($self->{cv}->name, 1);
|
|
my $db = $self->is_vendor() ? 'vendor' : 'customer';
|
|
my $action = 'add';
|
|
|
|
if ($::instance_conf->get_feature_experimental_order && 'oe.pl' eq $script) {
|
|
$script = 'controller.pl';
|
|
$action = 'Order/' . $action;
|
|
}
|
|
|
|
my $url = $self->url_for(
|
|
controller => $script,
|
|
action => $action,
|
|
vc => $db,
|
|
$db .'_id' => $self->{cv}->id,
|
|
$db => $name,
|
|
type => $::form->{type},
|
|
callback => $::form->{callback},
|
|
);
|
|
|
|
print $::form->redirect_header($url);
|
|
}
|
|
|
|
sub action_save_and_ar_transaction {
|
|
my ($self) = @_;
|
|
|
|
$main::auth->assert('ar_transactions');
|
|
|
|
$self->_transaction('ar.pl');
|
|
}
|
|
|
|
sub action_save_and_ap_transaction {
|
|
my ($self) = @_;
|
|
|
|
$main::auth->assert('ap_transactions');
|
|
|
|
$self->_transaction('ap.pl');
|
|
}
|
|
|
|
sub action_save_and_invoice {
|
|
my ($self) = @_;
|
|
|
|
if ( $self->is_vendor() ) {
|
|
$::auth->assert('vendor_invoice_edit');
|
|
} else {
|
|
$::auth->assert('invoice_edit');
|
|
}
|
|
|
|
$::form->{type} = 'invoice';
|
|
$self->_transaction($self->is_vendor() ? 'ir.pl' : 'is.pl');
|
|
}
|
|
|
|
sub action_save_and_order {
|
|
my ($self) = @_;
|
|
|
|
if ( $self->is_vendor() ) {
|
|
$::auth->assert('purchase_order_edit');
|
|
} else {
|
|
$::auth->assert('sales_order_edit');
|
|
}
|
|
|
|
$::form->{type} = $self->is_vendor() ? 'purchase_order' : 'sales_order';
|
|
$self->_transaction('oe.pl');
|
|
}
|
|
|
|
sub action_save_and_rfq {
|
|
my ($self) = @_;
|
|
|
|
$::auth->assert('request_quotation_edit');
|
|
|
|
$::form->{type} = 'request_quotation';
|
|
$self->_transaction('oe.pl');
|
|
}
|
|
|
|
sub action_save_and_quotation {
|
|
my ($self) = @_;
|
|
|
|
$::auth->assert('sales_quotation_edit');
|
|
|
|
$::form->{type} = 'sales_quotation';
|
|
$self->_transaction('oe.pl');
|
|
}
|
|
|
|
sub action_delete {
|
|
my ($self) = @_;
|
|
|
|
my $db = $self->{cv}->db;
|
|
|
|
if( !$self->is_orphaned() ) {
|
|
$self->action_edit();
|
|
} else {
|
|
|
|
$db->with_transaction(sub {
|
|
$self->{cv}->delete(cascade => 1);
|
|
|
|
my $snumbers = $self->is_vendor() ? 'vendornumber_'. $self->{cv}->vendornumber : 'customernumber_'. $self->{cv}->customernumber;
|
|
SL::DB::History->new(
|
|
trans_id => $self->{cv}->id,
|
|
snumbers => $snumbers,
|
|
employee_id => SL::DB::Manager::Employee->current->id,
|
|
addition => 'DELETED',
|
|
)->save();
|
|
}) || die($db->error);
|
|
|
|
my $msg = $self->is_vendor() ? $::locale->text('Vendor deleted!') : $::locale->text('Customer deleted!');
|
|
$::form->redirect($msg);
|
|
}
|
|
|
|
}
|
|
|
|
|
|
sub action_delete_contact {
|
|
my ($self) = @_;
|
|
|
|
my $db = $self->{contact}->db;
|
|
|
|
if ( !$self->{contact}->cp_id ) {
|
|
SL::Helper::Flash::flash('error', $::locale->text('No contact selected to delete'));
|
|
} else {
|
|
|
|
: $self->{cv}->is_customer ? t8("You don't have the rights to edit this customer.")
|
|
: t8("You don't have the rights to edit this vendor.");
|
|
|
|
for my $bar ($::request->layout->get('actionbar')) {
|
|
$bar->add(
|
|
combobox => [
|
|
action => [
|
|
t8('Save'),
|
|
submit => [ '#form', { action => "CustomerVendor/save" } ],
|
|
checks => [ 'check_taxzone_and_ustid' ],
|
|
accesskey => 'enter',
|
|