Suppliers.pm 11 KB
Newer Older
1
# Supplier functions
2
# Copyright (C) 2009-2014, AllWorldIT
Nigel Kukard's avatar
Nigel Kukard committed
3
# Copyright (C) 2008, LinuxRulz
Nigel Kukard's avatar
Nigel Kukard committed
4
# Copyright (C) 2007 Nigel Kukard  <nkukard@lbsd.net>
5
#
6
7
8
9
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
10
#
11
12
13
14
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
15
#
16
17
18
19
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

20
21
22



Nigel Kukard's avatar
Nigel Kukard committed
23
package wiaflos::client::cmdline::Suppliers;
24
25

use strict;
26
use warnings;
27
28


29
use wiaflos::constants;
Nigel Kukard's avatar
Nigel Kukard committed
30
31
32
use wiaflos::client::misc;
use wiaflos::client::soap;
use wiaflos::client::reportwriter;
33

Nigel Kukard's avatar
Nigel Kukard committed
34
35
36
# Plugin info
our $pluginInfo = {
	Name 	=> "Suppliers",
37
38
	Menu 	=> [
		# Suppliers main menu option
Nigel Kukard's avatar
Nigel Kukard committed
39
40
41
42
43
		{
			MenuItem 	=> "Suppliers",
			Children 	=> [
				{
					MenuItem 	=> "Create",
44
					Regex		=> "create",
Nigel Kukard's avatar
Nigel Kukard committed
45
					Desc		=> "Create a supplier",
46
					Help		=> 'create code="<supplier code>" name="<supplier name>" [reg="<registration num>"] [taxref="<tax reference number>"] account="<GL account>" [contactperson="<contact person name>"] [createsubaccount="<y to create sub account>"]',
Nigel Kukard's avatar
Nigel Kukard committed
47
48
49
50
					Function	=> \&create,
				},
				{
					MenuItem 	=> "LinkAddr",
51
					Regex		=> "linkaddr(?:ess)?",
Nigel Kukard's avatar
Nigel Kukard committed
52
					Desc		=> "Link in an address",
53
					Help		=> 'linkAddress supplier="<supplier code>" type="<billing or shipping>" address="<address>"',
Nigel Kukard's avatar
Nigel Kukard committed
54
55
					Function	=> \&linkAddress,
				},
Robert Spencer's avatar
Robert Spencer committed
56
57
58
59
60
61
62
#				{
#					MenuItem 	=> "LinkEmail",
#					Regex		=> "linkemail(?:address)?",
#					Desc		=> "Link in an email address",
#					Help		=> 'linkEmailAddress supplier="<supplier code>" type="<accounts or general>" address="<address>"',
#					Function	=> \&linkEmailAddress,
#				},
Nigel Kukard's avatar
Nigel Kukard committed
63
64
				{
					MenuItem 	=> "LinkPhNum",
65
					Regex		=> "link(?:phnum|phonenumber)",
Nigel Kukard's avatar
Nigel Kukard committed
66
					Desc		=> "Link in a phone number",
67
					Help		=> 'linkPhoneNumber supplier="<supplier code>" type="<phone or fax>" number="<phone number>" [name="<name>"]',
Nigel Kukard's avatar
Nigel Kukard committed
68
69
70
71
					Function	=> \&linkPhoneNumber,
				},
				{
					MenuItem 	=> "Remove",
72
					Regex		=> "remove",
Nigel Kukard's avatar
Nigel Kukard committed
73
					Desc		=> "Remove supplier",
74
					Help		=> 'remove supplier="<supplier code>"',
Nigel Kukard's avatar
Nigel Kukard committed
75
76
77
78
					Function	=> \&remove,
				},
				{
					MenuItem 	=> "List",
79
					Regex		=> "list",
Nigel Kukard's avatar
Nigel Kukard committed
80
81
82
83
84
85
					Desc		=> "Display list of suppliers",
					Help		=> 'list',
					Function	=> \&list,
				},
				{
					MenuItem 	=> "Show",
86
					Regex		=> "show",
Nigel Kukard's avatar
Nigel Kukard committed
87
					Desc		=> "Show supplier",
88
					Help		=> 'show supplier="<supplier code>"',
Nigel Kukard's avatar
Nigel Kukard committed
89
90
					Function	=> \&show,
				},
91
92
93
94
				{
					MenuItem 	=> "ShowGL",
					Regex		=> "showgl",
					Desc		=> "Show GL account entries",
Nigel Kukard's avatar
Nigel Kukard committed
95
					Help		=> 'showGL supplier="<supplier code>" [start="<start date>"] [end="<end date>"] [balance-brought-forward="<y|n>"]',
96
97
					Function	=> \&showGLAccountEntries,
				},
Nigel Kukard's avatar
Nigel Kukard committed
98
99
100
101
102
103
			],
		},
	],
};


104
105
106
107
108
109
110

# List suppliers
sub list
{
	my ($OUT,@args) = @_;

	if (@args > 0) {
Nigel Kukard's avatar
Nigel Kukard committed
111
112
		print($OUT "  => ERROR: Too many arguments provided\n");
		return ERR_C_PARAM;
113
114
	}

115
	my $res = soapCall($OUT,"wiaflos/server/api/Suppliers","getSuppliers");
116

Nigel Kukard's avatar
Nigel Kukard committed
117
	if ($res->{'Result'} == RES_OK) {
118
		print $OUT swrite(<<'END', "ID", "Code", "Name", "RegNumber", "TaxRef", "GLAccount");
119
120
121
+========+===========+===============================================+=====================+================+=================+
| @||||| | @|||||||| | @|||||||||||||||||||||||||||||||||||||||||||| | @|||||||||||||||||| | @||||||||||||| | @|||||||||||||| |
+========+===========+===============================================+=====================+================+=================+
122
END
Nigel Kukard's avatar
Nigel Kukard committed
123
		foreach my $item (@{$res->{'Data'}}) {
124
			print $OUT swrite(<<'END', $item->{'ID'}, $item->{'Code'}, $item->{'Name'}, defined($item->{'RegNumber'}) ? $item->{'RegNumber'} : '-', defined($item->{'TaxReference'}) ? $item->{'TaxReference'} : '-', $item->{'GLAccountNumber'});
125
| @<<<<< | @<<<<<<<< | @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< | @<<<<<<<<<<<<<<<<<< | @<<<<<<<<<<<<< | @<<<<<<<<<<<<<< |
126
127
END
			}
128
			print $OUT swrite(<<'END');
129
+========+===========+===============================================+=====================+================+=================+
130
131
END
	} else {
Nigel Kukard's avatar
Nigel Kukard committed
132
		soapDebug($OUT,$res);
133
134
	}

Nigel Kukard's avatar
Nigel Kukard committed
135
	return $res->{'Result'};
136
137
138
139
140
141
142
}


# Create supplier
sub create
{
	my ($OUT,@args) = @_;
143
144


145
146
	my $parms = parseArgs(@args);

147
	if (!defined($parms->{'code'})) {
Nigel Kukard's avatar
Nigel Kukard committed
148
		print($OUT "  => ERROR: Parameter 'code' not defined\n");
149
		return ERR_C_PARAM;
150
151
152
	}

	if (!defined($parms->{'name'})) {
Nigel Kukard's avatar
Nigel Kukard committed
153
		print($OUT "  => ERROR: Parameter 'name' not defined\n");
154
		return ERR_C_PARAM;
155
156
	}

157
	if (!defined($parms->{'account'})) {
Nigel Kukard's avatar
Nigel Kukard committed
158
		print($OUT "  => ERROR: Parameter 'account' not defined\n");
159
		return ERR_C_PARAM;
160
161
162
163
	}


	my $detail;
164
	$detail->{'Code'} = $parms->{'code'};
165
	$detail->{'Name'} = $parms->{'name'};
166
	$detail->{'GLAccountNumber'} = $parms->{'account'};
167
168
	$detail->{'RegNumber'} = $parms->{'reg'};
	$detail->{'TaxReference'} = $parms->{'taxref'};
Nigel Kukard's avatar
Nigel Kukard committed
169
	$detail->{'CreateSubAccount'} = $parms->{'createsubaccount'};
170
	my $res = soapCall($OUT,"wiaflos/server/api/Suppliers","createSupplier",$detail);
171

Nigel Kukard's avatar
Nigel Kukard committed
172
173
174
175
	if ($res->{'Result'} != RES_OK) {
		soapDebug($OUT,$res);
		return $res->{'Result'};
	}
176

Nigel Kukard's avatar
Nigel Kukard committed
177
	return $res->{'Data'};
178
179
180
}


181
# Link address
182
183
184
sub linkAddress
{
	my ($OUT,@args) = @_;
185
186


187
188
	my $parms = parseArgs(@args);

189
	if (!defined($parms->{'supplier'})) {
Nigel Kukard's avatar
Nigel Kukard committed
190
		print($OUT "  => ERROR: Parameter 'supplier' not defined\n");
191
		return ERR_C_PARAM;
192
193
194
	}

	if (!defined($parms->{'type'})) {
Nigel Kukard's avatar
Nigel Kukard committed
195
		print($OUT "  => ERROR: Parameter 'type' not defined\n");
196
		return ERR_C_PARAM;
197
198
199
	}

	if (!defined($parms->{'address'})) {
Nigel Kukard's avatar
Nigel Kukard committed
200
		print($OUT "  => ERROR: Parameter 'address' not defined\n");
201
		return ERR_C_PARAM;
202
203
204
	}

	my $detail;
205
	$detail->{'Code'} = $parms->{'supplier'};
206
207
	$detail->{'Type'} = $parms->{'type'};
	$detail->{'Address'} = $parms->{'address'};
208
	my $res = soapCall($OUT,"wiaflos/server/api/Suppliers","linkSupplierAddress",$detail);
209

Nigel Kukard's avatar
Nigel Kukard committed
210
211
212
213
	if ($res->{'Result'} != RES_OK) {
		soapDebug($OUT,$res);
		return $res->{'Result'};
	}
214

Nigel Kukard's avatar
Nigel Kukard committed
215
	return $res->{'Data'};
216
217
218
}


219
# Link phone number
220
221
222
sub linkPhoneNumber
{
	my ($OUT,@args) = @_;
223
224


225
226
	my $parms = parseArgs(@args);

227
	if (!defined($parms->{'supplier'})) {
Nigel Kukard's avatar
Nigel Kukard committed
228
		print($OUT "  => ERROR: Parameter 'supplier' not defined\n");
229
		return ERR_C_PARAM;
230
231
232
	}

	if (!defined($parms->{'type'})) {
Nigel Kukard's avatar
Nigel Kukard committed
233
		print($OUT "  => ERROR: Parameter 'type' not defined\n");
234
		return ERR_C_PARAM;
235
236
	}

237
	if (!defined($parms->{'number'})) {
Nigel Kukard's avatar
Nigel Kukard committed
238
		print($OUT "  => ERROR: Parameter 'number' not defined\n");
239
		return ERR_C_PARAM;
240
241
242
243
	}


	my $detail;
244
	$detail->{'Code'} = $parms->{'supplier'};
245
	$detail->{'Type'} = $parms->{'type'};
246
	$detail->{'Number'} = $parms->{'number'};
247
	$detail->{'Name'} = $parms->{'name'};
248
	my $res = soapCall($OUT,"wiaflos/server/api/Suppliers","linkSupplierPhoneNumber",$detail);
249

Nigel Kukard's avatar
Nigel Kukard committed
250
251
252
253
254
255
	if ($res->{'Result'} != RES_OK) {
		soapDebug($OUT,$res);
		return $res->{'Result'};
	}

	return $res->{'Data'};
256
257
258
}


Nigel Kukard's avatar
Nigel Kukard committed
259
260
261
262
263
264
265
# Show supplier
sub show
{
	my ($OUT,@args) = @_;

	my $parms = parseArgs(@args);

266
	if (!defined($parms->{'supplier'})) {
267
		return ERR_C_PARAM;
Nigel Kukard's avatar
Nigel Kukard committed
268
269
270
	}

	my $detail;
271
	$detail->{'Code'} = $parms->{'supplier'};
Nigel Kukard's avatar
Nigel Kukard committed
272

Nigel Kukard's avatar
Nigel Kukard committed
273
274
	my $res;

275
	$res = soapCall($OUT,"wiaflos/server/api/Suppliers","getSupplier",$detail);
Nigel Kukard's avatar
Nigel Kukard committed
276
277
278
279
280
	if ($res->{'Result'} != RES_OK) {
		soapDebug($OUT,$res);
		return $res->{'Result'};
	}
	my $supplier = $res->{'Data'};
281

282
	$res = soapCall($OUT,"wiaflos/server/api/Suppliers","getSupplierAddresses",$detail);
Nigel Kukard's avatar
Nigel Kukard committed
283
284
285
286
287
	if ($res->{'Result'} != RES_OK) {
		soapDebug($OUT,$res);
		return $res->{'Result'};
	}
	my $addies = $res->{'Data'};
Nigel Kukard's avatar
Nigel Kukard committed
288

289
	$res = soapCall($OUT,"wiaflos/server/api/Suppliers","getSupplierPhoneNumbers",$detail);
Nigel Kukard's avatar
Nigel Kukard committed
290
291
292
293
294
	if ($res->{'Result'} != RES_OK) {
		soapDebug($OUT,$res);
		return $res->{'Result'};
	}
	my $phones = $res->{'Data'};
Nigel Kukard's avatar
Nigel Kukard committed
295

296
	$res = soapCall($OUT,"wiaflos/server/api/Suppliers","getSupplierEmailAddresses",$detail);
Nigel Kukard's avatar
Nigel Kukard committed
297
298
299
300
301
	if ($res->{'Result'} != RES_OK) {
		soapDebug($OUT,$res);
		return $res->{'Result'};
	}
	my $emailAddies = $res->{'Data'};
302

303
	print($OUT "
304
ID.....: ".$supplier->{'ID'}."
305
Code...: ".$supplier->{'Code'}."
306
307
308
Name...: ".$supplier->{'Name'}."
Reg....: ".(defined($supplier->{'RegNumber'}) ? $supplier->{'RegNumber'} : "-")."
TaxRef.: ".(defined($supplier->{'TaxReference'}) ? $supplier->{'TaxReference'} : "-")."
309
GLAcc..: ".$supplier->{'GLAccountNumber'}."
Nigel Kukard's avatar
Nigel Kukard committed
310
311
312
313

");

foreach my $item (@{$addies}) {
314
	print($OUT "
Nigel Kukard's avatar
Nigel Kukard committed
315
316
317
318
319
320
Address: ".$item->{'Type'}."
".$item->{'Address'}."
");
}

foreach my $item (@{$phones}) {
321
	print($OUT "
Nigel Kukard's avatar
Nigel Kukard committed
322
Phone Number: ".$item->{'Type'}."
Nigel Kukard's avatar
Nigel Kukard committed
323
".$item->{'Number'}."
Nigel Kukard's avatar
Nigel Kukard committed
324
");
325
326
327
}

foreach my $item (@{$emailAddies}) {
328
	print($OUT "
329
330
331
Email Address: ".$item->{'Type'}."
".$item->{'Address'}."
");
Nigel Kukard's avatar
Nigel Kukard committed
332
333
}

Nigel Kukard's avatar
Nigel Kukard committed
334
	return RES_OK;
Nigel Kukard's avatar
Nigel Kukard committed
335
336
337
}


338
339
340
341
# Remove supplier
sub remove
{
	my ($OUT,@args) = @_;
342
343


344
345
346
	my $parms = parseArgs(@args);

	if (!defined($parms->{'supplier'})) {
Nigel Kukard's avatar
Nigel Kukard committed
347
348
		print($OUT "  => ERROR: Parameter 'supplier' not defined\n");
		return ERR_C_PARAM;
349
350
351
	}

	my $detail;
352
	$detail->{'Code'} = $parms->{'supplier'};
353
	my $res = soapCall($OUT,"wiaflos/server/api/Suppliers","removeSupplier",$detail);
Nigel Kukard's avatar
Nigel Kukard committed
354
	if ($res->{'Result'} != RES_OK) {
Nigel Kukard's avatar
Nigel Kukard committed
355
		soapDebug($OUT,$res);
Nigel Kukard's avatar
Nigel Kukard committed
356
357
358
	}

	return $res->{'Result'};
359
360
361
}


362

363
364
365
366
367
368
369
370
371
372
373
374
375
376
# Show supplier GL account entries
sub showGLAccountEntries
{
	my ($OUT,@args) = @_;


	my $parms = parseArgs(@args);

	if (!defined($parms->{'supplier'})) {
		print($OUT "  => ERROR: Parameter 'supplier' not defined\n");
		return ERR_C_PARAM;
	}
	my $detail;
	$detail->{'Code'} = $parms->{'supplier'};
Nigel Kukard's avatar
Nigel Kukard committed
377
378
379
	$detail->{'StartDate'} = $parms->{'start'};
	$detail->{'EndDate'} = $parms->{'end'};
	$detail->{'BalanceBroughtForward'} = $parms->{'balance-brought-forward'};
380
	my $res = soapCall($OUT,"wiaflos/server/api/Suppliers","getSupplierGLAccountEntries",$detail);
381
382
383
384
385
386
387
388
389
390
391
392
393
394

	if ($res->{'Result'} == RES_OK) {
		print $OUT swrite(<<'END', "ID", "Date", "Ref", "Amount", "Balance");
+===========+============+================================================================================+==============+================+
| @|||||||| | @||||||||| | @||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| | @||||||||||| | @||||||||||||| |
+===========+============+================================================================================+==============+================+
END
		# Sort data
		my @sorted = sort {$a->{'TransactionDate'} cmp $b->{'TransactionDate'}} @{$res->{'Data'}};

		my $balance = Math::BigFloat->new(0);

		foreach my $entry (@sorted) {
			$balance->badd($entry->{'Amount'});
Nigel Kukard's avatar
Nigel Kukard committed
395
			print $OUT swrite(<<'END', $entry->{'ID'}, $entry->{'TransactionDate'}, $entry->{'Reference'} ? $entry->{'Reference'} : $entry->{'TransactionReference'}, sprintf('%.2f',$entry->{'Amount'}),sprintf('%.2f',$balance));
396
397
398
399
400
401
402
403
404
405
406
407
408
409
| @<<<<<<<< | @<<<<<<<<< | @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< | @>>>>>>>>>>> | @>>>>>>>>>>>>> |
END
		}
		print $OUT swrite(<<'END');
+===========+============+================================================================================+==============+================+
END
	} else {
		soapDebug($OUT,$res);
	}

	return $res->{'Result'};
}


410
411
412
413
414



1;
# vim: ts=4