Inventory.pm 11.3 KB
Newer Older
1
# Inventory 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


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

use strict;
25
use warnings;
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
32


Nigel Kukard's avatar
Nigel Kukard committed
33
34
35
# Plugin info
our $pluginInfo = {
	Name 	=> "Inventory",
36
37
	Menu 	=> [
		# Inventory main menu option
Nigel Kukard's avatar
Nigel Kukard committed
38
39
40
41
42
		{
			MenuItem 	=> "Inventory",
			Children 	=> [
				{
					MenuItem 	=> "Create",
43
					Regex		=> "create",
Nigel Kukard's avatar
Nigel Kukard committed
44
					Desc		=> "Create inventory item",
45
46
47
					Help		=> 'create code="<product code>" type="<product or service>" [mode="<bulk or track for products>"] '.
							'description="<description>" incomeaccount="<income account>" [assetaccount="<asset account>"] '.
							'[expenseaccount="<expense account>"] sellprice="<sell price>" taxtype="<tax type>" [unit="<unit>"] '.
Nigel Kukard's avatar
Nigel Kukard committed
48
49
50
51
52
							'taxmode="<price is incl or excl>" [discountable="<yes or no>"]',
					Function	=> \&create,
				},
				{
					MenuItem 	=> "Remove",
53
					Regex		=> "remove",
Nigel Kukard's avatar
Nigel Kukard committed
54
					Desc		=> "Remove inventory item",
55
					Help		=> 'remove item="<product code>"',
Nigel Kukard's avatar
Nigel Kukard committed
56
57
					Function	=> \&remove,
				},
58
59
				{
					MenuItem 	=> "Update",
60
					Regex		=> "update",
61
					Desc		=> "Update inventory item",
62
					Help		=> 'update item="<product code>" [description="<description>"] [sellprice="<sell price>"] [discountable="<yes or no>"] [unit="<unit>"] [incomeaccount="<income account>"] [assetaccount="<asset account>"] [expenseaccount="<expense account>"] [taxtype="<tax type id>"]',
63
64
					Function	=> \&update,
				},
Nigel Kukard's avatar
Nigel Kukard committed
65
66
				{
					MenuItem 	=> "List",
67
					Regex		=> "list",
Nigel Kukard's avatar
Nigel Kukard committed
68
69
70
71
					Desc		=> "Display list of inventory items",
					Help		=> 'list',
					Function	=> \&list,
				},
72
73
74
75
				{
					MenuItem 	=> "Show",
					Regex		=> "show",
					Desc		=> "Show inventory item",
76
					Help		=> 'show item="<product code>"',
77
78
					Function	=> \&show,
				},
79
80
81
82
				{
					MenuItem 	=> "ShowMovement",
					Regex		=> "showmovement",
					Desc		=> "Show inventory item movement",
Robert Spencer's avatar
Robert Spencer committed
83
					Help		=> 'showmovement item="<product code>" [serial="<serial number>"]',
84
85
					Function	=> \&showMovement,
				},
86
87
88
89
90
91
92
93
				{
					MenuItem 	=> "LinkExpense",
					Regex		=> "linkexpense",
					Desc		=> "Link inventory item to expense account",
					Help		=> 'linkExpense date="<YYYY-MM-DD>" item="<product code>" reference="<refernce>" '.
							'[serial="<serial number>"] qty="<qty>" account="<GL expense account>"',
					Function	=> \&linkExpense,
				},
Nigel Kukard's avatar
Nigel Kukard committed
94
95
96
97
98
99
100
			],
		},
	],
};



101
102
103
104
105
106
107

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

	if (@args > 0) {
108
		return ERR_C_PARAM;
109
110
	}

111
	my $res = soapCall($OUT,"wiaflos/server/api/Inventory","getInventory");
112

Nigel Kukard's avatar
Nigel Kukard committed
113
	if ($res->{'Result'} == RES_OK) {
114
		print $OUT swrite(<<'END', "ID", "Code", "Type", "Mode", "Description", "Income", "Asset", "Expense", "SellPrice");
115
+=======+===========+=========+========+==============================================+===============+==============+=============+===========+
116
| @|||| | @||||||||| | @|||||| | @||||| | @||||||||||||||||||||||||||||||||||||||||||| | @|||||||||||| | @||||||||||| | @|||||||||| | @|||||||| |
117
+=======+===========+=========+========+==============================================+===============+==============+=============+===========+
118
END
119
120
		foreach my $item (@{$res->{'Data'}}) {
			print $OUT swrite(<<'END', $item->{'ID'}, $item->{'Code'}, $item->{'Type'}, defined($item->{'Mode'}) ? $item->{'Mode'} : '-', $item->{'Description'}, $item->{'GLIncomeAccountNumber'} ? $item->{'GLIncomeAccountNumber'} : '-', defined($item->{'GLAssetAccountNumber'}) ? $item->{'GLAssetAccountNumber'} : '-', defined($item->{'GLExpenseAccountNumber'}) ? $item->{'GLExpenseAccountNumber'} : '-', sprintf('%.2f',$item->{'SellPrice'}));
121
| @<<<< | @<<<<<<<<< | @|||||| | @||||| | @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< | @<<<<<<<<<<<< | @<<<<<<<<<<< | @<<<<<<<<<< | @>>>>>>>> |
122
END
123
124
		}
		print $OUT swrite(<<'END');
125
+=======+===========+=========+========+==============================================+===============+==============+=============+===========+
126
127
END
	} else {
Nigel Kukard's avatar
Nigel Kukard committed
128
		soapDebug($OUT,$res);
129
130
	}

Nigel Kukard's avatar
Nigel Kukard committed
131
	return $res->{'Result'};
132
133
134
}


135
# Create inventory item
136
137
138
sub create
{
	my ($OUT,@args) = @_;
139
140


141
142
	my $parms = parseArgs(@args);

143
	if (!defined($parms->{'code'})) {
144
		print($OUT "  => ERROR: Parameter 'code' not defined\n");
145
		return ERR_C_PARAM;
146
147
148
	}

	if (!defined($parms->{'type'})) {
149
		print($OUT "  => ERROR: Parameter 'type' not defined\n");
150
		return ERR_C_PARAM;
151
152
	}

153
	if (!defined($parms->{'description'})) {
154
		print($OUT "  => ERROR: Parameter 'description' not defined\n");
155
		return ERR_C_PARAM;
156
157
	}

158
	if (!defined($parms->{'incomeaccount'})) {
159
		print($OUT "  => ERROR: Parameter 'incomeaccount' not defined\n");
160
		return ERR_C_PARAM;
161
162
163
	}

	if (!defined($parms->{'sellprice'})) {
164
		print($OUT "  => ERROR: Parameter 'sellprice' not defined\n");
165
		return ERR_C_PARAM;
166
167
168
	}

	if (!defined($parms->{'taxtype'})) {
169
		print($OUT "  => ERROR: Parameter 'taxtype' not defined\n");
170
		return ERR_C_PARAM;
171
172
173
	}

	if (!defined($parms->{'taxmode'})) {
174
		print($OUT "  => ERROR: Parameter 'taxmode' not defined\n");
175
		return ERR_C_PARAM;
176
177
178
	}

	my $detail;
179
	$detail->{'Code'} = $parms->{'code'};
180
	$detail->{'Type'} = $parms->{'type'};
181
	$detail->{'Mode'} = $parms->{'mode'};
182
	$detail->{'Description'} = $parms->{'description'};
183
184
185
	$detail->{'GLIncomeAccountNumber'} = $parms->{'incomeaccount'};
	$detail->{'GLAssetAccountNumber'} = $parms->{'assetaccount'};
	$detail->{'GLExpenseAccountNumber'} = $parms->{'expenseaccount'};
186
187
188
	$detail->{'SellPrice'} = $parms->{'sellprice'};
	$detail->{'TaxTypeID'} = $parms->{'taxtype'};
	$detail->{'TaxMode'} = $parms->{'taxmode'};
189
	$detail->{'Unit'} = $parms->{'unit'};
190
	$detail->{'Discountable'} = $parms->{'discoutable'};
191
	my $res = soapCall($OUT,"wiaflos/server/api/Inventory","createInventoryItem",$detail);
192
193
194
195
196
197
	if ($res->{'Result'} != RES_OK) {
		soapDebug($OUT,$res);
		return $res->{'Result'};
	}

	return $res->{'Data'};
198
199
200
201
202
203
204
}


# Remove inventory item
sub remove
{
	my ($OUT,@args) = @_;
205
206


207
208
	my $parms = parseArgs(@args);

209
	if (!defined($parms->{'item'})) {
210
		print($OUT "  => ERROR: Parameter 'item' not defined\n");
211
		return ERR_C_PARAM;
212
213
214
	}

	my $detail;
215
	$detail->{'Code'} = $parms->{'item'};
216
	my $res = soapCall($OUT,"wiaflos/server/api/Inventory","removeInventoryItem",$detail);
217
218
219
220
221
	if ($res->{'Result'} != RES_OK) {
		soapDebug($OUT,$res);
	}

	return $res->{'Result'};
222
223
224
}


225
# Update inventory item
226
227
228
sub update
{
	my ($OUT,@args) = @_;
229
230


231
232
233
	my $parms = parseArgs(@args);

	if (!defined($parms->{'item'})) {
234
		print($OUT "  => ERROR: Parameter 'item' not defined\n");
235
		return ERR_C_PARAM;
236
237
238
	}

	my $detail;
239
	$detail->{'Code'} = $parms->{'item'};
240
	$detail->{'Description'} = $parms->{'description'};
241
242
243
	$detail->{'GLIncomeAccountNumber'} = $parms->{'incomeaccount'};
	$detail->{'GLAssetAccountNumber'} = $parms->{'assetaccount'};
	$detail->{'GLExpenseAccountNumber'} = $parms->{'expenseaccount'};
244
	$detail->{'SellPrice'} = $parms->{'sellprice'};
245
	$detail->{'TaxTypeID'} = $parms->{'taxtype'};
246
247
	$detail->{'Unit'} = $parms->{'unit'};
	$detail->{'Discountable'} = $parms->{'discoutable'};
248
	my $res = soapCall($OUT,"wiaflos/server/api/Inventory","updateInventoryItem",$detail);
249
250
251
252
	if ($res->{'Result'} != RES_OK) {
		soapDebug($OUT,$res);
	}
	return $res->{'Result'};
253
254
255
}


256
257
258
259
260
261
262
# Show inventory item
sub show
{
	my ($OUT,@args) = @_;


	my $parms = parseArgs(@args);
263

264
265
266
267
268
269
270
	if (!defined($parms->{'item'})) {
		print($OUT "  => ERROR: Parameter 'item' not defined\n");
		return ERR_C_PARAM;
	}

	my $detail;
	$detail->{'Code'} = $parms->{'item'};
271
	my $res = soapCall($OUT,"wiaflos/server/api/Inventory","getInventoryItem",$detail);
272
273
274
275

	if ($res->{'Result'} == RES_OK) {
		my $data = $res->{'Data'};

276
		printf $OUT <<'END',$data->{'Code'},$data->{'Type'},$data->{'Mode'},$data->{'Description'},$data->{'GLIncomeAccountNumber'},$data->{'GLAssetAccountNumber'},$data->{'GLExpenseAccountNumber'},defined($data->{'Unit'}) ? $data->{'Unit'} : '-',$data->{'SellPrice'},$data->{'TaxTypeID'},$data->{'TaxMode'},$data->{'Discountable'};
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
Product Info
============
Product Code......: %s
Type..............: %s
Mode..............: %s
Description.......: %s
GLIncomeAccount...: %s
GLAssetAccount....: %s
GLExpenseAccount..: %s
Unit..............: %s
SellPrice.........: %s
TaxTypeID.........: %s
TaxMode...........: %s
Discountable......: %s

END

	} else {
		soapDebug($OUT,$res);
	}

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



303
304
305
306
307
308
309
# Show inventory item movement
sub showMovement
{
	my ($OUT,@args) = @_;


	my $parms = parseArgs(@args);
310

311
312
313
314
315
316
317
318
	if (!defined($parms->{'item'})) {
		print($OUT "  => ERROR: Parameter 'item' not defined\n");
		return ERR_C_PARAM;
	}

	my $detail;
	$detail->{'Code'} = $parms->{'item'};
	$detail->{'SerialNumber'} = $parms->{'serial'};
319
	my $res = soapCall($OUT,"wiaflos/server/api/Inventory","getInventoryItemMovement",$detail);
320
321
322
323
324
325
326
327
328
329
330
331

	if ($res->{'Result'} == RES_OK) {
		my $data = $res->{'Data'};

		print $OUT swrite(<<'END', "Timestamp", "GLTrans", "SerialNumber", "QtyChange", "Price");
Product Movement
+============+===========+=========================+===============+==============+
| @||||||||| | @|||||||| | @|||||||||||||||||||||| | @|||||||||||| | @||||||||||| |
+============+===========+=========================+===============+==============+
END
		foreach my $change (@{$data}) {
			print $OUT swrite(<<'END', $change->{'GLTransactionDate'}, $change->{'GLTransactionID'}, defined($change->{'SerialNumber'}) ? $change->{'SerialNumber'} : '-', $change->{'QtyChange'}, $change->{'Price'});
332
| @||||||||| | @|||||||| | @|||||||||||||||||||||| | @>>>>>>>>>>>> | @>>>>>>>>>>> |
333
334
335
336
337
338
339
340
341
342
343
344
345
END
		}
		print $OUT swrite(<<'END');
+============+===========+=========================+===============+==============+
END
	} else {
		soapDebug($OUT,$res);
	}

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


346
347
348
349
# Link inventory item as expense to GL account
sub linkExpense
{
	my ($OUT,@args) = @_;
350
351


352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
	my $parms = parseArgs(@args);

	if (!defined($parms->{'date'})) {
		print($OUT "  => ERROR: Parameter 'date' not defined\n");
		return ERR_C_PARAM;
	}

	if (!defined($parms->{'item'})) {
		print($OUT "  => ERROR: Parameter 'item' not defined\n");
		return ERR_C_PARAM;
	}

	if (!defined($parms->{'reference'})) {
		print($OUT "  => ERROR: Parameter 'reference' not defined\n");
		return ERR_C_PARAM;
	}

	if (!defined($parms->{'account'})) {
		print($OUT "  => ERROR: Parameter 'account' not defined\n");
		return ERR_C_PARAM;
	}

	my $detail;
	$detail->{'Date'} = $parms->{'date'};
	$detail->{'Code'} = $parms->{'item'};
	$detail->{'Reference'} = $parms->{'reference'};
	$detail->{'GLAccountNumber'} = $parms->{'account'};
	$detail->{'SerialNumber'} = $parms->{'serial'};
380
	my $res = soapCall($OUT,"wiaflos/server/api/Inventory","linkInventoryItemToExpenseAccount",$detail);
381
382
383
384
385
386
387
388
389
	if ($res->{'Result'} != RES_OK) {
		soapDebug($OUT,$res);
		return $res->{'Result'};
	}

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


390

391
392
393

1;
# vim: ts=4