Commit f406938a authored by Nigel Kukard's avatar Nigel Kukard
Browse files

* Added support for removing of clients & suppliers to admin interface

* Added support for creating sub accounts for clients & suppliers in the relevant journals
* Various bug fixes
* Return more data when returning a GL account
parent 9988e4df
......@@ -281,7 +281,7 @@ my $Suppliers_cmds = {
'command' => 'create',
'regex' => 'create',
'desc' => 'Create a supplier',
'help' => 'create ref="<reference>" name="<supplier name>" [reg="<registration num>"] [taxref="<tax reference number>"] acc="<GL account>" [contactperson="<contact person name>"]',
'help' => 'create ref="<reference>" name="<supplier name>" [reg="<registration num>"] [taxref="<tax reference number>"] acc="<GL account>" [contactperson="<contact person name>"] [createsubacc="<y to create sub account>"]',
'function' => \&ias::client::cmdline::Suppliers::create,
},
{
......@@ -346,35 +346,35 @@ my $Clients_cmds = {
'command' => 'create',
'regex' => 'create',
'desc' => 'Create a client',
'help' => 'create ref="<reference>" name="<client name>" [reg="<registration num>"] [taxref="<tax reference number>"] glacc="<GL account>" [contactperson="<contact person name>"]',
'help' => 'create ref="<reference>" name="<client name>" [reg="<registration num>"] [taxref="<tax reference number>"] glacc="<GL account>" [contactperson="<contact person name>"] [createsubacc="<y to create sub account>"]',
'function' => \&ias::client::cmdline::Clients::create,
},
{
'command' => 'linkaddr',
'regex' => 'linkaddr',
'desc' => 'Link address',
'help' => 'linkaddr client="<item id>" type="<physical or postal>" address="<address>"',
'help' => 'linkaddr client="<client ref>" type="<physical or postal>" address="<address>"',
'function' => \&ias::client::cmdline::Clients::linkAddress,
},
{
'command' => 'linkemail',
'regex' => 'linkemail',
'desc' => 'Link email address',
'help' => 'linkemail client="<item id>" type="<accounts or general>" address="<address>"',
'help' => 'linkemail client="<client ref>" type="<accounts or general>" address="<address>"',
'function' => \&ias::client::cmdline::Clients::linkEmailAddress,
},
{
'command' => 'linkphnum',
'regex' => 'linkphnum',
'desc' => 'Link phone number',
'help' => 'linkphnum client="<item id>" type="<phone or fax>" phnumber="<phone number>" [name="<name>"]',
'help' => 'linkphnum client="<client ref>" type="<phone or fax>" phnumber="<phone number>" [name="<name>"]',
'function' => \&ias::client::cmdline::Clients::linkPhoneNumber,
},
{
'command' => 'remove',
'regex' => 'remove',
'desc' => 'Remove client',
'help' => 'remove client="<item id>"',
'help' => 'remove client="<client ref>"',
'function' => \&ias::client::cmdline::Clients::remove,
},
{
......
......@@ -76,6 +76,10 @@ sub create
return -202;
}
if (!defined($parms->{'createsubacc'})) {
return -202;
}
my $detail;
$detail->{'Ref'} = $parms->{'ref'};
......@@ -83,6 +87,7 @@ sub create
$detail->{'GLAccRef'} = $parms->{'glacc'};
$detail->{'RegNumber'} = $parms->{'reg'};
$detail->{'TaxReference'} = $parms->{'taxref'};
$detail->{'CreateSubAcc'} = $parms->{'createsubacc'};
my $res = soapCall($OUT,"Clients","createClient",$detail);
if ($res == -2) {
......@@ -280,6 +285,34 @@ Email Address: ".$item->{'Type'}."
}
# Remove client
sub remove
{
my ($OUT,@args) = @_;
my $parms = parseArgs(@args);
if (!defined($parms->{'client'})) {
return -202;
}
my $detail;
$detail->{'ClientRef'} = $parms->{'client'};
my $res = soapCall($OUT,"Clients","removeClient",$detail);
if ($res == -2) {
print($OUT " => Database error\n");
} elsif ($res == -3) {
print($OUT " => Parameter problem\n");
} elsif ($res == -4) {
print($OUT " => Supplier not found\n");
}
return $res;
}
......
......@@ -83,6 +83,7 @@ sub create
$detail->{'GLAccRef'} = $parms->{'acc'};
$detail->{'RegNumber'} = $parms->{'reg'};
$detail->{'TaxReference'} = $parms->{'taxref'};
$detail->{'CreateSubAcc'} = $parms->{'createsubacc'};
my $res = soapCall($OUT,"Suppliers","createSupplier",$detail);
if ($res == -2) {
......@@ -241,6 +242,34 @@ Email Address: ".$item->{'Type'}."
}
# Remove supplier
sub remove
{
my ($OUT,@args) = @_;
my $parms = parseArgs(@args);
if (!defined($parms->{'supplier'})) {
return -202;
}
my $detail;
$detail->{'SupplierRef'} = $parms->{'supplier'};
my $res = soapCall($OUT,"Suppliers","removeSupplier",$detail);
if ($res == -2) {
print($OUT " => Database error\n");
} elsif ($res == -3) {
print($OUT " => Parameter problem\n");
} elsif ($res == -4) {
print($OUT " => Supplier not found\n");
}
return $res;
}
......
......@@ -251,6 +251,7 @@ sub getClients
# Optional:
# RegNumber - Company registration number / ID number of person
# TaxReference - Company tax reference number
# CreateSubAcc - Create sub GL account for this client
sub createClient
{
my ($detail) = @_;
......@@ -319,6 +320,33 @@ sub createClient
DBBegin();
# Check if we should create a sub account
if (defined($detail->{'CreateSubAcc'}) && $detail->{'CreateSubAcc'} eq 'y') {
# Get GL account ID
my $tmp;
$tmp->{'GLAccRef'} = $detail->{'GLAccRef'};
my $subGLAcc = ias::server::GL::getAccount($tmp);
if (ref $subGLAcc ne "HASH") {
setError(ias::server::GL::Error());
return $subGLAcc;
}
# Create new account
my $newAcc;
$newAcc->{'Ref'} = ias::server::GL::getNextSubAccRef($tmp);
$newAcc->{'Name'} = $detail->{'Ref'};
$newAcc->{'FinCat'} = $subGLAcc->{'FinCatIdentifier'};
$newAcc->{'RwCat'} = $subGLAcc->{'RwCatIdentifier'};
$newAcc->{'ParentGLAcc'} = $detail->{'GLAccRef'};
$GLAccID = ias::server::GL::createAccount($newAcc);
# Check for error
if ($GLAccID < 1) {
setError(ias::server::GL::Error());
return $GLAccID;
}
}
# Create client
my $sth = DBDo("
INSERT INTO clients
......@@ -779,5 +807,61 @@ sub getPhoneNumbers
}
# Remove client
# Parameters:
# ClientRef - Client reference
sub removeClient
{
my ($detail) = @_;
# Verify client ref
if (!defined($detail->{'ClientRef'}) || $detail->{'ClientRef'} eq "") {
setError("No (or invalid) client ref provided");
return -3;
}
# Check if client exists & pull
my $tmp;
$tmp->{'ClientRef'} = $detail->{'ClientRef'};
my $client = getClient($tmp);
if (ref $client ne "HASH") {
return $client;
}
DBBegin();
# Remove client
my $sth = DBDo("DELETE FROM client_addrs WHERE ClientID = ".DBQuote($client->{'ID'})."");
if (!$sth)
{
setError(ias::server::dblayer::Error());
DBRollback();
return -2;
}
$sth = DBDo("DELETE FROM client_email WHERE ClientID = ".DBQuote($client->{'ID'})."");
if (!$sth)
{
setError(ias::server::dblayer::Error());
DBRollback();
return -2;
}
$sth = DBDo("DELETE FROM clients WHERE ID = ".DBQuote($client->{'ID'})."");
if (!$sth)
{
setError(ias::server::dblayer::Error());
DBRollback();
return -2;
}
DBCommit();
return 0;
}
1;
# vim: ts=4
......@@ -321,6 +321,27 @@ sub checkAccFinCat
}
# Backend function to build account item hash
sub sanitizeRawAccountItem
{
my $rawData = shift;
my $item;
$item->{'ID'} = $rawData->{'ID'};
$item->{'Ref'} = getAccRef($rawData->{'ID'});
$item->{'ParentGLAccID'} = $rawData->{'ParentGLAccID'};
$item->{'Name'} = $rawData->{'Name'};
$item->{'FinCatIdentifier'} = $rawData->{'FinCatIdentifier'};
$item->{'FinCatDescription'} = $rawData->{'FinCatDescription'};
$item->{'RwCatIdentifier'} = $rawData->{'RwCatIdentifier'};
$item->{'RwatDescription'} = $rawData->{'RwCatDescription'};
return $item;
}
# Return an array of general ledger accounts
sub getAccounts
......@@ -332,11 +353,13 @@ sub getAccounts
my $sth = DBSelect("
SELECT
gl_accounts.ID, gl_accounts.Ref, gl_accounts.Name,
gl_fincats.Identifier AS FinCatIdentifier, gl_fincats.Description AS FinCatDescription
gl_fincats.Identifier AS FinCatIdentifier, gl_fincats.Description AS FinCatDescription,
gl_rwcats.Identifier AS RwCatIdentifier, gl_rwcats.Description AS RwCatDescription
FROM
gl_accounts, gl_fincats
gl_accounts, gl_fincats, gl_rwcats
WHERE
gl_fincats.ID = gl_accounts.FinCatID
AND gl_rwcats.ID = gl_accounts.RwCatID
");
if (!$sth)
{
......@@ -346,26 +369,119 @@ sub getAccounts
# Fetch rows
while (my $row = $sth->fetchrow_hashref()) {
my $account;
my $account = sanitizeRawAccountItem($row);
$account->{'ID'} = $row->{'ID'};
push(@accounts,$account);
}
if (!($account->{'Ref'} = getAccRef($row->{'ID'}))) {
DBFreeRes($sth);
return -1;
}
DBFreeRes($sth);
$account->{'ParentGLAccID'} = $row->{'ParentGLAccID'};
$account->{'Name'} = $row->{'Name'};
$account->{'FinCatIdentifier'} = $row->{'FinCatIdentifier'};
$account->{'FinCatDescription'} = $row->{'FinCatDescription'};
return \@accounts;
}
push(@accounts,$account);
# Return a hash containing account details
# Parameters:
# GLAccRef - GL account reference
sub getAccount
{
my ($data) = @_;
# Verify GLAccRef
if (!defined($data->{'GLAccRef'}) || $data->{'GLAccRef'} eq "") {
setError("No (or invalid) GLAccRef provided");
return -3;
}
# Check GL account exists
my $GLAccID;
if (($GLAccID = getGLAccIDFromRef($data->{'GLAccRef'})) < 1) {
return $GLAccID;
}
my $sth = DBSelect("
SELECT
gl_accounts.ID, gl_accounts.Ref, gl_accounts.Name,
gl_fincats.Identifier AS FinCatIdentifier, gl_fincats.Description AS FinCatDescription,
gl_rwcats.Identifier AS RwCatIdentifier, gl_rwcats.Description AS RwCatDescription
FROM
gl_accounts, gl_fincats, gl_rwcats
WHERE
gl_accounts.ID = ".DBQuote($GLAccID)."
AND gl_fincats.ID = gl_accounts.FinCatID
AND gl_rwcats.ID = gl_accounts.RwCatID
");
if (!$sth)
{
setError(ias::server::dblayer::Error());
return -2;
}
# Check we got a result
if ($sth->rows != 1) {
setError("Error finding account");
return -4;
}
# Fetch row
my $row = $sth->fetchrow_hashref();
DBFreeRes($sth);
return \@accounts;
return sanitizeRawAccountItem($row);
}
# Return the next sub account number
# Parameters:
# GLAccRef - GL account reference
sub getNextSubAccRef
{
my ($data) = @_;
# Verify GLAccRef
if (!defined($data->{'GLAccRef'}) || $data->{'GLAccRef'} eq "") {
setError("No (or invalid) GLAccRef provided");
return -3;
}
# Check GL account exists
my $GLAccID;
if (($GLAccID = getGLAccIDFromRef($data->{'GLAccRef'})) < 1) {
return $GLAccID;
}
# Select last account
my $sth = DBSelect("
SELECT
Ref
FROM
gl_accounts
WHERE
ParentGLAccID = ".DBQuote($GLAccID)."
ORDER BY Ref DESC
LIMIT 1
");
if (!$sth)
{
setError(ias::server::dblayer::Error());
return -2;
}
# Check we got a result, if not return 1
if ($sth->rows != 1) {
setError("Error finding account");
return 1;
}
# Fetch row
my $row = $sth->fetchrow_hashref();
DBFreeRes($sth);
return $row->{'Ref'} + 1;
}
......@@ -745,7 +861,7 @@ sub getAccountEntries
# Check GL account exists
my $GLAccID;
if ((my $GLAccID = getGLAccIDFromRef($data->{'GLAccRef'})) < 1) {
if (($GLAccID = getGLAccIDFromRef($data->{'GLAccRef'})) < 1) {
return $GLAccID;
}
......
......@@ -494,7 +494,7 @@ sub createItem
}
# Verify sell price
if (!defined($data->{'SellPrice'}) || $data->{'SellPrice'} < 1) {
if (!defined($data->{'SellPrice'}) || $data->{'SellPrice'} eq "") {
setError("Sell price not provided");
return -3;
}
......
......@@ -198,7 +198,7 @@ sub getSupplier
# Check if supplier exists & pull
my $supplierID;
if (($supplierID = getSupplierIDFromRef($detail->{'SupplierID'})) < 1) {
if (($supplierID = getSupplierIDFromRef($detail->{'SupplierRef'})) < 1) {
return $supplierID;
}
......@@ -248,6 +248,7 @@ sub getSuppliers
# Optional:
# RegNumber - Company registration number / ID number of person
# TaxReference - Company tax reference number
# CreateSubAcc - Create sub GL account for this client
sub createSupplier
{
my ($detail) = @_;
......@@ -313,6 +314,33 @@ sub createSupplier
DBBegin();
# Check if we should create a sub account
if (defined($detail->{'CreateSubAcc'}) && $detail->{'CreateSubAcc'} eq 'y') {
# Get GL account ID
my $tmp;
$tmp->{'GLAccRef'} = $detail->{'GLAccRef'};
my $subGLAcc = ias::server::GL::getAccount($tmp);
if (ref $subGLAcc ne "HASH") {
setError(ias::server::GL::Error());
return $subGLAcc;
}
# Create new account
my $newAcc;
$newAcc->{'Ref'} = ias::server::GL::getNextSubAccRef($tmp);
$newAcc->{'Name'} = $detail->{'Ref'};
$newAcc->{'FinCat'} = $subGLAcc->{'FinCatIdentifier'};
$newAcc->{'RwCat'} = $subGLAcc->{'RwCatIdentifier'};
$newAcc->{'ParentGLAcc'} = $detail->{'GLAccRef'};
$GLAccID = ias::server::GL::createAccount($newAcc);
# Check for error
if ($GLAccID < 1) {
setError(ias::server::GL::Error());
return $GLAccID;
}
}
# Create supplier
my $sth = DBDo("
INSERT INTO suppliers
......@@ -771,6 +799,62 @@ sub getPhoneNumbers
}
# Remove supplier
# Parameters:
# SupplierRef - Supplier reference
sub removeSupplier
{
my ($detail) = @_;
# Verify supplier ref
if (!defined($detail->{'SupplierRef'}) || $detail->{'SupplierRef'} eq "") {
setError("No (or invalid) supplier ref provided");
return -3;
}
# Check if supplier exists & pull
my $tmp;
$tmp->{'SupplierRef'} = $detail->{'SupplierRef'};
my $supplier = getSupplier($tmp);
if (ref $supplier ne "HASH") {
return $supplier;
}
DBBegin();
# Remove supplier
my $sth = DBDo("DELETE FROM supplier_addrs WHERE SupplierID = ".DBQuote($supplier->{'ID'})."");
if (!$sth)
{
setError(ias::server::dblayer::Error());
DBRollback();
return -2;
}
$sth = DBDo("DELETE FROM supplier_email WHERE SupplierID = ".DBQuote($supplier->{'ID'})."");
if (!$sth)
{
setError(ias::server::dblayer::Error());
DBRollback();
return -2;
}
$sth = DBDo("DELETE FROM suppliers WHERE ID = ".DBQuote($supplier->{'ID'})."");
if (!$sth)
{
setError(ias::server::dblayer::Error());
DBRollback();
return -2;
}
DBCommit();
return 0;
}
......
......@@ -108,6 +108,7 @@ sub getClients {
# Optional:
# RegNumber - Company registration number / ID number of person
# TaxReference - Company tax reference number
# CreateSubAcc - Create sub GL account for this client
sub createClient {
my (undef,$data) = @_;
......@@ -141,6 +142,7 @@ sub createClient {
$detail->{'GLAccRef'} = $data->{'GLAccRef'};
$detail->{'RegNumber'} = $data->{'RegNumber'};
$detail->{'TaxReference'} = $data->{'TaxReference'};
$detail->{'CreateSubAcc'} = $data->{'CreateSubAcc'};
my $res = ias::server::Clients::createClient($detail);
return $res;
......@@ -399,6 +401,35 @@ sub getPhoneNumbers {
}
# Remove client
# Parameters:
# ClientRef - Client reference
sub removeClient {
my (undef,$data) = @_;
# Check if client is authorized to access this function
if (isAuthorized("removeClient") < 0) {
return -103;
}
# Check params
if (!defined($data->{'ClientRef'}) || $data->{'ClientRef'} eq "") {
return -101;
}
my $authInfo = getAuthData();
# Do transaction
my $detail;
$detail->{'ClientRef'} = $data->{'ClientRef'};
my $res = ias::server::Suppliers::removeClient($detail);
return $res;
}
......
......@@ -108,6 +108,7 @@ sub getSuppliers {
# Optional:
# RegNumber - Company registration number / ID number of person
# TaxReference - Company tax reference number
# CreateSubAcc - Create sub GL account for this client
sub createSupplier {
my (undef,$data) = @_;
......@@ -141,6 +142,7 @@ sub createSupplier {
$detail->{'GLAccRef'} = $data->{'GLAccRef'};
$detail->{'RegNumber'} = $data->{'RegNumber'};
$detail->{'TaxReference'} = $data->{'TaxReference'};
$detail->{'CreateSubAcc'} = $data->{'CreateSubAcc'};
my $res = ias::server::Suppliers::createSupplier($detail);
return $res;
......@@ -396,6 +398,35 @@ sub getPhoneNumbers {