Skip to content

Commit

Permalink
Implement backend for the Company/Person 'Delete' button
Browse files Browse the repository at this point in the history
  • Loading branch information
ehuelsmann committed Jan 25, 2025
1 parent 65e9e69 commit 721143d
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 12 deletions.
11 changes: 1 addition & 10 deletions UI/Contact/divs/company.html
Original file line number Diff line number Diff line change
Expand Up @@ -133,16 +133,7 @@
value = 'get_by_cc'
class = 'submit'
} %]
[% INCLUDE button element_data = {
text = text('Save')
id = 'company-action-save'
class="submit"
type="submit"
name="__action"
value="save_company"
} %]
[%
IF entity_id.defined AND NOT company.is_used ;
IF may_delete AND entity_id.defined AND NOT company.is_used ;
INCLUDE button element_data = {
text = text('Delete')
id = 'company-action-delete'
Expand Down
2 changes: 1 addition & 1 deletion UI/Contact/divs/person.html
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@
value="save_person"
} %]
[%
IF entity_id.defined AND NOT company.is_used ;
IF may_delete AND entity_id.defined AND NOT company.is_used ;
INCLUDE button element_data = {
text = text('Delete')
id = 'person-action-delete'
Expand Down
12 changes: 12 additions & 0 deletions lib/LedgerSMB/Entity.pm
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,18 @@ sub get{
return __PACKAGE__->new(%$result);
}

=item del()
=cut

sub del {
my ($self) = @_;
my ($result) = __PACKAGE__->call_procedure(funcname => 'entity__delete',
args => [ $self->id ]);

return $result;
}

=back
=head1 LICENSE AND COPYRIGHT
Expand Down
23 changes: 22 additions & 1 deletion lib/LedgerSMB/Scripts/contact.pm
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ This module is the UI controller for the customer, vendor, etc functions; it
=cut

use HTTP::Status qw( HTTP_FOUND );

use LedgerSMB;
use LedgerSMB::Entity::Company;
use LedgerSMB::Entity::Person;
Expand Down Expand Up @@ -61,6 +63,23 @@ for (@pluginmods){
=over
=item delete_entity
=cut

sub delete_entity {
my ($request) = @_;
my $entity =
LedgerSMB::Entity::Company->get_by_cc($request->{control_code});
$entity ||= LedgerSMB::Entity::Person->get_by_cc($request->{control_code});

$entity->del;
return [ HTTP_FOUND,
[ 'Location' => 'reports.pl?__action=start_report&report_name=contact_search' ],
[ '' ]
];
}

=item get_by_cc
Populates the company area with info on the company, pulled up through the
Expand Down Expand Up @@ -167,6 +186,7 @@ sub _main_screen {
$request->{target_div} ||= 'person_div' if defined $person;
$request->{target_div} ||= 'company_div';

my $may_delete = $request->is_allowed_role({ allowed_roles => [ 'contact_delete' ] });
my @all_managers =
map { $_->{label} = "$_->{first_name} $_->{last_name}"; $_ }
($request->call_procedure( funcname => 'employee__all_managers' ),);
Expand Down Expand Up @@ -365,7 +385,8 @@ sub _main_screen {
all_managers => \@all_managers,
default_country => $default_country,
default_language => $default_language,
earn_id => $earn_id
earn_id => $earn_id,
may_delete => $may_delete,
});
}

Expand Down
1 change: 1 addition & 0 deletions sql/modules/BLACKLIST
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ employee__list_managers
employee__save
employee__search
employee_search
entity__delete
entity__delete_bank_account
entity__delete_contact
entity__delete_location
Expand Down
19 changes: 19 additions & 0 deletions sql/modules/Entity.sql
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,25 @@ COMMENT ON FUNCTION entity__get (
$$ Returns a set of (only one) entity record with the entity id.$$;


CREATE OR REPLACE FUNCTION entity__delete(in_id int)
RETURNS boolean
security definer
AS $$
BEGIN
delete from entity where id = in_id;
return found;
END;
$$ language plpgsql;

REVOKE EXECUTE ON FUNCTION entity__delete(in_id int) FROM public;


COMMENT ON FUNCTION entity__delete(in_id int) IS
$$Removes an entity and its master data.

Removal will fail if the function 'entity__is_used()' returns 'true'.
$$;

CREATE OR REPLACE FUNCTION eca__get_entity (
in_credit_id int
) RETURNS setof entity AS $$
Expand Down
1 change: 1 addition & 0 deletions sql/modules/Roles.sql
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,7 @@ SELECT lsmb__create_role('contact_delete',
does not provide any rights.
$DOC$
);
SELECT lsmb__grant_exec('contact_delete', 'entity__delete(in_id int)');
SELECT lsmb__grant_perms('contact_delete', obj, 'DELETE')
FROM unnest(ARRAY['entity'::text, 'company', 'person', 'location',
'entity_credit_account', 'eca_tax', 'entity_note',
Expand Down

0 comments on commit 721143d

Please sign in to comment.