Suppliers.pm 10.6 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# Supplier functions
# Copyright (C) 2007 Nigel Kukard  <nkukard@lbsd.net>
# 
# 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.
# 
# 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.
# 
# 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.

18
19
20



Nigel Kukard's avatar
Nigel Kukard committed
21
package wiaflos::client::cmdline::Suppliers;
22
23

use strict;
24
use warnings;
25
26


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

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


102
103
104
105
106
107
108

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

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

	my $res = soapCall($OUT,"Suppliers","getSuppliers");

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

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


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

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

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

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


	my $detail;
162
	$detail->{'Code'} = $parms->{'code'};
163
	$detail->{'Name'} = $parms->{'name'};
164
	$detail->{'GLAccountNumber'} = $parms->{'account'};
165
166
	$detail->{'RegNumber'} = $parms->{'reg'};
	$detail->{'TaxReference'} = $parms->{'taxref'};
Nigel Kukard's avatar
Nigel Kukard committed
167
	$detail->{'CreateSubAccount'} = $parms->{'createsubaccount'};
168
	my $res = soapCall($OUT,"Suppliers","createSupplier",$detail);
Nigel Kukard's avatar
Nigel Kukard committed
169
170
171
172
173
	
	if ($res->{'Result'} != RES_OK) {
		soapDebug($OUT,$res);
		return $res->{'Result'};
	}
174

Nigel Kukard's avatar
Nigel Kukard committed
175
	return $res->{'Data'};
176
177
178
179
180
181
182
183
184
185
186
}


# Link address 
sub linkAddress
{
	my ($OUT,@args) = @_;
	
	
	my $parms = parseArgs(@args);

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

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

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

	my $detail;
203
	$detail->{'Code'} = $parms->{'supplier'};
204
205
	$detail->{'Type'} = $parms->{'type'};
	$detail->{'Address'} = $parms->{'address'};
206
	my $res = soapCall($OUT,"Suppliers","linkSupplierAddress",$detail);
Nigel Kukard's avatar
Nigel Kukard committed
207
208
209
210
211
	
	if ($res->{'Result'} != RES_OK) {
		soapDebug($OUT,$res);
		return $res->{'Result'};
	}
212

Nigel Kukard's avatar
Nigel Kukard committed
213
	return $res->{'Data'};
214
215
216
217
218
219
220
221
222
223
224
}


# Link phone number 
sub linkPhoneNumber
{
	my ($OUT,@args) = @_;
	
	
	my $parms = parseArgs(@args);

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

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

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


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

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

	return $res->{'Data'};
254
255
256
}


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

	my $parms = parseArgs(@args);

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

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

Nigel Kukard's avatar
Nigel Kukard committed
271
272
273
274
275
276
277
278
	my $res;

	$res = soapCall($OUT,"Suppliers","getSupplier",$detail);
	if ($res->{'Result'} != RES_OK) {
		soapDebug($OUT,$res);
		return $res->{'Result'};
	}
	my $supplier = $res->{'Data'};
Nigel Kukard's avatar
Nigel Kukard committed
279
	
Nigel Kukard's avatar
Nigel Kukard committed
280
281
282
283
284
285
	$res = soapCall($OUT,"Suppliers","getSupplierAddresses",$detail);
	if ($res->{'Result'} != RES_OK) {
		soapDebug($OUT,$res);
		return $res->{'Result'};
	}
	my $addies = $res->{'Data'};
Nigel Kukard's avatar
Nigel Kukard committed
286

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

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

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

");

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

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

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

Nigel Kukard's avatar
Nigel Kukard committed
332
	return RES_OK;
Nigel Kukard's avatar
Nigel Kukard committed
333
334
335
}


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

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

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

	return $res->{'Result'};
357
358
359
}


360

361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
# 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'};
	my $res = soapCall($OUT,"Suppliers","getSupplierGLAccountEntries",$detail);

	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'});
			print $OUT swrite(<<'END', $entry->{'ID'}, $entry->{'TransactionDate'}, $entry->{'Reference'} ? $entry->{'Reference'} : $entry->{'TransactionReference'}, sprintf('%8.2f',$entry->{'Amount'}),sprintf('%8.2f',$balance));
| @<<<<<<<< | @<<<<<<<<< | @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< | @>>>>>>>>>>> | @>>>>>>>>>>>>> |
END
		}
		print $OUT swrite(<<'END');
+===========+============+================================================================================+==============+================+
END
	} else {
		soapDebug($OUT,$res);
	}

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


405
406
407
408
409



1;
# vim: ts=4