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

19
20
21



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

use strict;
25
use warnings;
26
27


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

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


103
104
105
106
107
108
109

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

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

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

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

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


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

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

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

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


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

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


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

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

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

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

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

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


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

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

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

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


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

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

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


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

	my $parms = parseArgs(@args);

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

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

Nigel Kukard's avatar
Nigel Kukard committed
272
273
274
275
276
277
278
279
	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
280
	
Nigel Kukard's avatar
Nigel Kukard committed
281
282
283
284
285
286
	$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
287

Nigel Kukard's avatar
Nigel Kukard committed
288
289
290
291
292
293
	$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
294

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

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

");

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

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

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

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


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

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

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

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


361

362
363
364
365
366
367
368
369
370
371
372
373
374
375
# 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
376
377
378
	$detail->{'StartDate'} = $parms->{'start'};
	$detail->{'EndDate'} = $parms->{'end'};
	$detail->{'BalanceBroughtForward'} = $parms->{'balance-brought-forward'};
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
	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'});
Nigel Kukard's avatar
Nigel Kukard committed
394
			print $OUT swrite(<<'END', $entry->{'ID'}, $entry->{'TransactionDate'}, $entry->{'Reference'} ? $entry->{'Reference'} : $entry->{'TransactionReference'}, sprintf('%.2f',$entry->{'Amount'}),sprintf('%.2f',$balance));
395
396
397
398
399
400
401
402
403
404
405
406
407
408
| @<<<<<<<< | @<<<<<<<<< | @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< | @>>>>>>>>>>> | @>>>>>>>>>>>>> |
END
		}
		print $OUT swrite(<<'END');
+===========+============+================================================================================+==============+================+
END
	} else {
		soapDebug($OUT,$res);
	}

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


409
410
411
412
413



1;
# vim: ts=4