-
Notifications
You must be signed in to change notification settings - Fork 760
/
gncCustomer.c
998 lines (857 loc) · 27.2 KB
/
gncCustomer.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
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
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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
/********************************************************************\
* gncCustomer.c -- the Core Customer Interface *
* *
* 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, contact: *
* *
* Free Software Foundation Voice: +1-617-542-5942 *
* 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 *
* Boston, MA 02110-1301, USA gnu@gnu.org *
* *
\********************************************************************/
/*
* Copyright (C) 2001,2002 Derek Atkins
* Copyright (C) 2003 Linas Vepstas <linas@linas.org>
* Author: Derek Atkins <warlord@MIT.EDU>
*/
#include <config.h>
#include <glib.h>
#include <string.h>
#include <qofinstance-p.h>
#include "gnc-commodity.h"
#include "gncAddressP.h"
#include "gncBillTermP.h"
#include "gncInvoice.h"
#include "gncBusiness.h"
#include "gncCustomer.h"
#include "gncCustomerP.h"
#include "gncJobP.h"
#include "gncTaxTableP.h"
static gint cust_qof_event_handler_id = 0;
static void cust_handle_qof_events (QofInstance *entity, QofEventId event_type,
gpointer user_data, gpointer event_data);
struct _gncCustomer
{
QofInstance inst;
/* The following fields are identical to 'vendor' */
const char * id;
const char * name;
const char * notes;
GncBillTerm * terms;
GncAddress * addr;
gnc_commodity * currency;
GncTaxTable* taxtable;
gboolean taxtable_override;
GncTaxIncluded taxincluded;
gboolean active;
GList * jobs;
gnc_numeric * balance; /* cached customer balance, will not be stored */
/* The following fields are unique to 'customer' */
gnc_numeric credit;
gnc_numeric discount;
GncAddress * shipaddr;
};
struct _gncCustomerClass
{
QofInstanceClass parent_class;
};
static QofLogModule log_module = GNC_MOD_BUSINESS;
#define _GNC_MOD_NAME GNC_ID_CUSTOMER
/* ============================================================== */
/* misc inline funcs */
static inline void mark_customer (GncCustomer *customer);
void mark_customer (GncCustomer *customer)
{
qof_instance_set_dirty(&customer->inst);
qof_event_gen (&customer->inst, QOF_EVENT_MODIFY, NULL);
}
/* ============================================================== */
enum
{
PROP_0,
PROP_NAME,
PROP_PDF_DIRNAME,
PROP_LAST_POSTED,
PROP_PAYMENT_LAST_ACCT,
};
/* GObject Initialization */
G_DEFINE_TYPE(GncCustomer, gnc_customer, QOF_TYPE_INSTANCE)
static void
gnc_customer_init(GncCustomer* cust)
{
}
static void
gnc_customer_dispose(GObject *custp)
{
G_OBJECT_CLASS(gnc_customer_parent_class)->dispose(custp);
}
static void
gnc_customer_finalize(GObject* custp)
{
G_OBJECT_CLASS(gnc_customer_parent_class)->finalize(custp);
}
static void
gnc_customer_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
GncCustomer *cust;
gchar *key;
g_return_if_fail(GNC_IS_CUSTOMER(object));
cust = GNC_CUSTOMER(object);
switch (prop_id)
{
case PROP_NAME:
g_value_set_string(value, cust->name);
break;
case PROP_PDF_DIRNAME:
qof_instance_get_kvp (QOF_INSTANCE (cust), value, 1, OWNER_EXPORT_PDF_DIRNAME);
break;
case PROP_LAST_POSTED:
qof_instance_get_kvp (QOF_INSTANCE (cust), value, 1, LAST_POSTED_TO_ACCT);
break;
case PROP_PAYMENT_LAST_ACCT:
qof_instance_get_kvp (QOF_INSTANCE (cust), value, 2, GNC_PAYMENT, GNC_LAST_ACCOUNT);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
break;
}
}
static void
gnc_customer_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
GncCustomer *cust;
gchar *key;
g_return_if_fail(GNC_IS_CUSTOMER(object));
cust = GNC_CUSTOMER(object);
g_assert (qof_instance_get_editlevel(cust));
switch (prop_id)
{
case PROP_NAME:
gncCustomerSetName(cust, g_value_get_string(value));
break;
case PROP_PDF_DIRNAME:
qof_instance_set_kvp (QOF_INSTANCE (cust), value, 1, OWNER_EXPORT_PDF_DIRNAME);
break;
case PROP_LAST_POSTED:
qof_instance_set_kvp (QOF_INSTANCE (cust), value, 1, LAST_POSTED_TO_ACCT);
break;
case PROP_PAYMENT_LAST_ACCT:
qof_instance_set_kvp (QOF_INSTANCE (cust), value, 2, GNC_PAYMENT, GNC_LAST_ACCOUNT);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
break;
}
}
/** Return display name for this object */
static gchar*
impl_get_display_name(const QofInstance* inst)
{
GncCustomer* cust;
g_return_val_if_fail(inst != NULL, FALSE);
g_return_val_if_fail(GNC_IS_CUSTOMER(inst), FALSE);
cust = GNC_CUSTOMER(inst);
/* XXX internationalization of "Customer" */
return g_strdup_printf("Customer %s", cust->name);
}
/** Does this object refer to a specific object */
static gboolean
impl_refers_to_object(const QofInstance* inst, const QofInstance* ref)
{
GncCustomer* cust;
g_return_val_if_fail(inst != NULL, FALSE);
g_return_val_if_fail(GNC_IS_CUSTOMER(inst), FALSE);
cust = GNC_CUSTOMER(inst);
if (GNC_IS_BILLTERM(ref))
{
return (cust->terms == GNC_BILLTERM(ref));
}
else if (GNC_IS_TAXTABLE(ref))
{
return (cust->taxtable == GNC_TAXTABLE(ref));
}
return FALSE;
}
/** Returns a list of my type of object which refers to an object. For example, when called as
qof_instance_get_typed_referring_object_list(taxtable, account);
it will return the list of taxtables which refer to a specific account. The result should be the
same regardless of which taxtable object is used. The list must be freed by the caller but the
objects on the list must not.
*/
static GList*
impl_get_typed_referring_object_list(const QofInstance* inst, const QofInstance* ref)
{
if (!GNC_IS_BILLTERM(ref) && !GNC_IS_TAXTABLE(ref))
{
return NULL;
}
return qof_instance_get_referring_object_list_from_collection(qof_instance_get_collection(inst), ref);
}
static void
gnc_customer_class_init (GncCustomerClass *klass)
{
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
QofInstanceClass* qof_class = QOF_INSTANCE_CLASS(klass);
gobject_class->dispose = gnc_customer_dispose;
gobject_class->finalize = gnc_customer_finalize;
gobject_class->set_property = gnc_customer_set_property;
gobject_class->get_property = gnc_customer_get_property;
qof_class->get_display_name = impl_get_display_name;
qof_class->refers_to_object = impl_refers_to_object;
qof_class->get_typed_referring_object_list = impl_get_typed_referring_object_list;
g_object_class_install_property
(gobject_class,
PROP_NAME,
g_param_spec_string ("name",
"Customer Name",
"The customer is an arbitrary string "
"assigned by the user which provides the "
"customer name.",
NULL,
G_PARAM_READWRITE));
g_object_class_install_property
(gobject_class,
PROP_PDF_DIRNAME,
g_param_spec_string ("export-pdf-dir",
"Export PDF Directory Name",
"A subdirectory for exporting PDF reports which is "
"appended to the target directory when writing them "
"out. It is retrieved from preferences and stored on "
"each 'Owner' object which prints items after "
"printing.",
NULL,
G_PARAM_READWRITE));
g_object_class_install_property(
gobject_class,
PROP_LAST_POSTED,
g_param_spec_boxed("invoice-last-posted-account",
"Invoice Last Posted Account",
"The last account to which an invoice belonging to "
"this owner was posted.",
GNC_TYPE_GUID,
G_PARAM_READWRITE));
g_object_class_install_property(
gobject_class,
PROP_PAYMENT_LAST_ACCT,
g_param_spec_boxed("payment-last-account",
"Payment Last Account",
"The last account to which an payment belonging to "
"this owner was posted.",
GNC_TYPE_GUID,
G_PARAM_READWRITE));
}
/* Create/Destroy Functions */
GncCustomer *gncCustomerCreate (QofBook *book)
{
GncCustomer *cust;
if (!book) return NULL;
cust = g_object_new (GNC_TYPE_CUSTOMER, NULL);
qof_instance_init_data (&cust->inst, _GNC_MOD_NAME, book);
cust->id = CACHE_INSERT ("");
cust->name = CACHE_INSERT ("");
cust->notes = CACHE_INSERT ("");
cust->addr = gncAddressCreate (book, &cust->inst);
cust->taxincluded = GNC_TAXINCLUDED_USEGLOBAL;
cust->active = TRUE;
cust->jobs = NULL;
cust->balance = NULL;
cust->discount = gnc_numeric_zero();
cust->credit = gnc_numeric_zero();
cust->shipaddr = gncAddressCreate (book, &cust->inst);
if (cust_qof_event_handler_id == 0)
cust_qof_event_handler_id = qof_event_register_handler (cust_handle_qof_events, NULL);
qof_event_gen (&cust->inst, QOF_EVENT_CREATE, NULL);
return cust;
}
void gncCustomerDestroy (GncCustomer *cust)
{
if (!cust) return;
qof_instance_set_destroying(cust, TRUE);
qof_instance_set_dirty (&cust->inst);
gncCustomerCommitEdit (cust);
}
static void gncCustomerFree (GncCustomer *cust)
{
if (!cust) return;
qof_event_gen (&cust->inst, QOF_EVENT_DESTROY, NULL);
CACHE_REMOVE (cust->id);
CACHE_REMOVE (cust->name);
CACHE_REMOVE (cust->notes);
gncAddressBeginEdit (cust->addr);
gncAddressDestroy (cust->addr);
gncAddressBeginEdit (cust->shipaddr);
gncAddressDestroy (cust->shipaddr);
g_list_free (cust->jobs);
g_free (cust->balance);
if (cust->terms)
gncBillTermDecRef (cust->terms);
if (cust->taxtable)
{
gncTaxTableDecRef (cust->taxtable);
}
/* qof_instance_release (&cust->inst); */
g_object_unref (cust);
}
/* ============================================================== */
/* Set Functions */
#define SET_STR(obj, member, str) { \
if (!g_strcmp0 (member, str)) return; \
gncCustomerBeginEdit (obj); \
CACHE_REPLACE(member, str); \
}
void gncCustomerSetID (GncCustomer *cust, const char *id)
{
if (!cust) return;
if (!id) return;
SET_STR(cust, cust->id, id);
mark_customer (cust);
gncCustomerCommitEdit (cust);
}
void gncCustomerSetName (GncCustomer *cust, const char *name)
{
if (!cust) return;
if (!name) return;
SET_STR(cust, cust->name, name);
mark_customer (cust);
gncCustomerCommitEdit (cust);
}
void gncCustomerSetNotes (GncCustomer *cust, const char *notes)
{
if (!cust) return;
if (!notes) return;
SET_STR(cust, cust->notes, notes);
mark_customer (cust);
gncCustomerCommitEdit (cust);
}
void gncCustomerSetTerms (GncCustomer *cust, GncBillTerm *terms)
{
if (!cust) return;
if (cust->terms == terms) return;
gncCustomerBeginEdit (cust);
if (cust->terms)
gncBillTermDecRef (cust->terms);
cust->terms = terms;
if (cust->terms)
gncBillTermIncRef (cust->terms);
mark_customer (cust);
gncCustomerCommitEdit (cust);
}
void gncCustomerSetTaxIncluded (GncCustomer *cust, GncTaxIncluded taxincl)
{
if (!cust) return;
if (taxincl == cust->taxincluded) return;
gncCustomerBeginEdit (cust);
cust->taxincluded = taxincl;
mark_customer (cust);
gncCustomerCommitEdit (cust);
}
void gncCustomerSetActive (GncCustomer *cust, gboolean active)
{
if (!cust) return;
if (active == cust->active) return;
gncCustomerBeginEdit (cust);
cust->active = active;
mark_customer (cust);
gncCustomerCommitEdit (cust);
}
void gncCustomerSetDiscount (GncCustomer *cust, gnc_numeric discount)
{
if (!cust) return;
if (gnc_numeric_equal (discount, cust->discount)) return;
gncCustomerBeginEdit (cust);
cust->discount = discount;
mark_customer (cust);
gncCustomerCommitEdit (cust);
}
void gncCustomerSetCredit (GncCustomer *cust, gnc_numeric credit)
{
if (!cust) return;
if (gnc_numeric_equal (credit, cust->credit)) return;
gncCustomerBeginEdit (cust);
cust->credit = credit;
mark_customer (cust);
gncCustomerCommitEdit (cust);
}
void gncCustomerSetCurrency (GncCustomer *cust, gnc_commodity *currency)
{
if (!cust || !currency) return;
if (cust->currency && gnc_commodity_equal (cust->currency, currency)) return;
gncCustomerBeginEdit (cust);
cust->currency = currency;
mark_customer (cust);
gncCustomerCommitEdit (cust);
}
void gncCustomerSetTaxTableOverride (GncCustomer *customer, gboolean override)
{
if (!customer) return;
if (customer->taxtable_override == override) return;
gncCustomerBeginEdit (customer);
customer->taxtable_override = override;
mark_customer (customer);
gncCustomerCommitEdit (customer);
}
void gncCustomerSetTaxTable (GncCustomer *customer, GncTaxTable *table)
{
if (!customer) return;
if (customer->taxtable == table) return;
gncCustomerBeginEdit (customer);
if (customer->taxtable)
gncTaxTableDecRef (customer->taxtable);
if (table)
gncTaxTableIncRef (table);
customer->taxtable = table;
mark_customer (customer);
gncCustomerCommitEdit (customer);
}
/* Note that JobList changes do not affect the "dirtiness" of the customer */
void gncCustomerAddJob (GncCustomer *cust, GncJob *job)
{
if (!cust) return;
if (!job) return;
if (g_list_index(cust->jobs, job) == -1)
cust->jobs = g_list_insert_sorted (cust->jobs, job,
(GCompareFunc)gncJobCompare);
qof_event_gen (&cust->inst, QOF_EVENT_MODIFY, NULL);
}
void gncCustomerRemoveJob (GncCustomer *cust, GncJob *job)
{
GList *node;
if (!cust) return;
if (!job) return;
node = g_list_find (cust->jobs, job);
if (!node)
{
/* PERR ("split not in account"); */
}
else
{
cust->jobs = g_list_remove_link (cust->jobs, node);
g_list_free_1 (node);
}
qof_event_gen (&cust->inst, QOF_EVENT_MODIFY, NULL);
}
void gncCustomerBeginEdit (GncCustomer *cust)
{
qof_begin_edit (&cust->inst);
}
static void gncCustomerOnError (QofInstance *inst, QofBackendError errcode)
{
PERR("Customer QofBackend Failure: %d", errcode);
gnc_engine_signal_commit_error( errcode );
}
static void gncCustomerOnDone (QofInstance *inst)
{
GncCustomer *cust = (GncCustomer *) inst;
gncAddressClearDirty (cust->addr);
gncAddressClearDirty (cust->shipaddr);
}
static void cust_free (QofInstance *inst)
{
GncCustomer *cust = (GncCustomer *) inst;
gncCustomerFree (cust);
}
void gncCustomerCommitEdit (GncCustomer *cust)
{
if (!qof_commit_edit (QOF_INSTANCE(cust))) return;
qof_commit_edit_part2 (&cust->inst, gncCustomerOnError,
gncCustomerOnDone, cust_free);
}
/* ============================================================== */
/* Get Functions */
const char * gncCustomerGetID (const GncCustomer *cust)
{
if (!cust) return NULL;
return cust->id;
}
const char * gncCustomerGetName (const GncCustomer *cust)
{
if (!cust) return NULL;
return cust->name;
}
GncAddress * gncCustomerGetAddr (const GncCustomer *cust)
{
if (!cust) return NULL;
return cust->addr;
}
static void
qofCustomerSetAddr (GncCustomer *cust, QofInstance *addr_ent)
{
GncAddress *addr;
if (!cust || !addr_ent)
{
return;
}
addr = (GncAddress*)addr_ent;
if (addr == cust->addr)
{
return;
}
if (cust->addr != NULL)
{
gncAddressBeginEdit(cust->addr);
gncAddressDestroy(cust->addr);
}
gncCustomerBeginEdit(cust);
cust->addr = addr;
gncCustomerCommitEdit(cust);
}
static void
qofCustomerSetShipAddr (GncCustomer *cust, QofInstance *ship_addr_ent)
{
GncAddress *ship_addr;
if (!cust || !ship_addr_ent)
{
return;
}
ship_addr = (GncAddress*)ship_addr_ent;
if (ship_addr == cust->shipaddr)
{
return;
}
if (cust->shipaddr != NULL)
{
gncAddressBeginEdit(cust->shipaddr);
gncAddressDestroy(cust->shipaddr);
}
gncCustomerBeginEdit(cust);
cust->shipaddr = ship_addr;
gncCustomerCommitEdit(cust);
}
GncAddress * gncCustomerGetShipAddr (const GncCustomer *cust)
{
if (!cust) return NULL;
return cust->shipaddr;
}
const char * gncCustomerGetNotes (const GncCustomer *cust)
{
if (!cust) return NULL;
return cust->notes;
}
GncBillTerm * gncCustomerGetTerms (const GncCustomer *cust)
{
if (!cust) return NULL;
return cust->terms;
}
GncTaxIncluded gncCustomerGetTaxIncluded (const GncCustomer *cust)
{
if (!cust) return GNC_TAXINCLUDED_USEGLOBAL;
return cust->taxincluded;
}
gnc_commodity * gncCustomerGetCurrency (const GncCustomer *cust)
{
if (!cust) return NULL;
return cust->currency;
}
gboolean gncCustomerGetActive (const GncCustomer *cust)
{
if (!cust) return FALSE;
return cust->active;
}
gnc_numeric gncCustomerGetDiscount (const GncCustomer *cust)
{
if (!cust) return gnc_numeric_zero();
return cust->discount;
}
gnc_numeric gncCustomerGetCredit (const GncCustomer *cust)
{
if (!cust) return gnc_numeric_zero();
return cust->credit;
}
gboolean gncCustomerGetTaxTableOverride (const GncCustomer *customer)
{
if (!customer) return FALSE;
return customer->taxtable_override;
}
GncTaxTable* gncCustomerGetTaxTable (const GncCustomer *customer)
{
if (!customer) return NULL;
return customer->taxtable;
}
GList * gncCustomerGetJoblist (const GncCustomer *cust, gboolean show_all)
{
if (!cust) return NULL;
if (show_all)
{
return (g_list_copy (cust->jobs));
}
else
{
GList *list = NULL, *iterator;
for (iterator = cust->jobs; iterator; iterator = iterator->next)
{
GncJob *j = iterator->data;
if (gncJobGetActive (j))
list = g_list_prepend (list, j);
}
return g_list_reverse (list);
}
}
gboolean gncCustomerIsDirty (GncCustomer *cust)
{
if (!cust) return FALSE;
return (qof_instance_is_dirty(&cust->inst) ||
gncAddressIsDirty (cust->addr) ||
gncAddressIsDirty (cust->shipaddr));
}
/* Other functions */
int gncCustomerCompare (const GncCustomer *a, const GncCustomer *b)
{
if (!a && !b) return 0;
if (!a && b) return 1;
if (a && !b) return -1;
return(strcmp(a->name, b->name));
}
gboolean
gncCustomerEqual(const GncCustomer *a, const GncCustomer *b)
{
if (a == NULL && b == NULL) return TRUE;
if (a == NULL || b == NULL) return FALSE;
g_return_val_if_fail(GNC_IS_CUSTOMER(a), FALSE);
g_return_val_if_fail(GNC_IS_CUSTOMER(b), FALSE);
if (g_strcmp0(a->id, b->id) != 0)
{
PWARN("IDs differ: %s vs %s", a->id, b->id);
return FALSE;
}
if (g_strcmp0(a->name, b->name) != 0)
{
PWARN("Names differ: %s vs %s", a->name, b->name);
return FALSE;
}
if (g_strcmp0(a->notes, b->notes) != 0)
{
PWARN("Notes differ: %s vs %s", a->notes, b->notes);
return FALSE;
}
if (!gncBillTermEqual(a->terms, b->terms))
{
PWARN("Bill terms differ");
return FALSE;
}
if (!gnc_commodity_equal(a->currency, b->currency))
{
PWARN("currencies differ");
return FALSE;
}
if (!gncTaxTableEqual(a->taxtable, b->taxtable))
{
PWARN("tax tables differ");
return FALSE;
}
if (a->taxtable_override != b->taxtable_override)
{
PWARN("Tax table override flags differ");
return FALSE;
}
if (a->taxincluded != b->taxincluded)
{
PWARN("Tax included flags differ");
return FALSE;
}
if (a->active != b->active)
{
PWARN("Active flags differ");
return FALSE;
}
if (!gncAddressEqual(a->addr, b->addr))
{
PWARN("addresses differ");
return FALSE;
}
if (!gncAddressEqual(a->shipaddr, b->shipaddr))
{
PWARN("addresses differ");
return FALSE;
}
if (!gnc_numeric_equal(a->credit, b->credit))
{
PWARN("Credit amounts differ");
return FALSE;
}
if (!gnc_numeric_equal(a->discount, b->discount))
{
PWARN("Discount amounts differ");
return FALSE;
}
/* FIXME: Need to check jobs list
GList * jobs;
*/
return TRUE;
}
/**
* Listen for qof events.
*
* - If the address of a customer has changed, mark the customer as dirty.
* - If a lot related to a customer has changed, clear the customer's
* cached balance as it likely has become invalid.
*
* @param entity Entity for the event
* @param event_type Event type
* @param user_data User data registered with the handler
* @param event_data Event data passed with the event.
*/
static void
cust_handle_qof_events (QofInstance *entity, QofEventId event_type,
gpointer user_data, gpointer event_data)
{
/* Handle address change events */
if ((GNC_IS_ADDRESS (entity) &&
(event_type & QOF_EVENT_MODIFY) != 0))
{
if (GNC_IS_CUSTOMER (event_data))
{
GncCustomer* cust = GNC_CUSTOMER (event_data);
gncCustomerBeginEdit (cust);
mark_customer (cust);
gncCustomerCommitEdit (cust);
}
return;
}
/* Handle lot change events */
if (GNC_IS_LOT (entity))
{
GNCLot *lot = GNC_LOT (entity);
GncOwner lot_owner;
const GncOwner *end_owner = NULL;
GncInvoice *invoice = gncInvoiceGetInvoiceFromLot (lot);
/* Determine the owner associated with the lot */
if (invoice)
/* Invoice lots */
end_owner = gncOwnerGetEndOwner (gncInvoiceGetOwner (invoice));
else if (gncOwnerGetOwnerFromLot (lot, &lot_owner))
/* Pre-payment lots */
end_owner = gncOwnerGetEndOwner (&lot_owner);
if (gncOwnerGetType (end_owner) == GNC_OWNER_CUSTOMER)
{
/* Clear the cached balance */
GncCustomer* cust = gncOwnerGetCustomer (end_owner);
g_free (cust->balance);
cust->balance = NULL;
}
return;
}
}
/* ============================================================== */
/* Package-Private functions */
static const char * _gncCustomerPrintable (gpointer item)
{
// GncCustomer *c = item;
if (!item) return "failed";
return gncCustomerGetName((GncCustomer*)item);
}
static void
destroy_customer_on_book_close(QofInstance *ent, gpointer data)
{
GncCustomer* c = GNC_CUSTOMER(ent);
gncCustomerBeginEdit(c);
gncCustomerDestroy(c);
}
/** Handles book end - frees all customers from the book
*
* @param book Book being closed
*/
static void
gnc_customer_book_end(QofBook* book)
{
QofCollection *col;
col = qof_book_get_collection(book, GNC_ID_CUSTOMER);
qof_collection_foreach(col, destroy_customer_on_book_close, NULL);
}
static QofObject gncCustomerDesc =
{
DI(.interface_version = ) QOF_OBJECT_VERSION,
DI(.e_type = ) _GNC_MOD_NAME,
DI(.type_label = ) "Customer",
DI(.create = ) (gpointer)gncCustomerCreate,
DI(.book_begin = ) NULL,
DI(.book_end = ) gnc_customer_book_end,
DI(.is_dirty = ) qof_collection_is_dirty,
DI(.mark_clean = ) qof_collection_mark_clean,
DI(.foreach = ) qof_collection_foreach,
DI(.printable = ) (const char * (*)(gpointer))gncCustomerGetName,
DI(.version_cmp = ) (int (*)(gpointer, gpointer)) qof_instance_version_cmp,
};
gboolean gncCustomerRegister (void)
{
static QofParam params[] =
{
{ CUSTOMER_ID, QOF_TYPE_STRING, (QofAccessFunc)gncCustomerGetID, (QofSetterFunc)gncCustomerSetID },
{ CUSTOMER_NAME, QOF_TYPE_STRING, (QofAccessFunc)gncCustomerGetName, (QofSetterFunc)gncCustomerSetName },
{ CUSTOMER_NOTES, QOF_TYPE_STRING, (QofAccessFunc)gncCustomerGetNotes, (QofSetterFunc)gncCustomerSetNotes },
{
CUSTOMER_DISCOUNT, QOF_TYPE_NUMERIC, (QofAccessFunc)gncCustomerGetDiscount,
(QofSetterFunc)gncCustomerSetDiscount
},
{
CUSTOMER_CREDIT, QOF_TYPE_NUMERIC, (QofAccessFunc)gncCustomerGetCredit,
(QofSetterFunc)gncCustomerSetCredit
},
{ CUSTOMER_ADDR, GNC_ID_ADDRESS, (QofAccessFunc)gncCustomerGetAddr, (QofSetterFunc)qofCustomerSetAddr },
{ CUSTOMER_SHIPADDR, GNC_ID_ADDRESS, (QofAccessFunc)gncCustomerGetShipAddr, (QofSetterFunc)qofCustomerSetShipAddr },
{
CUSTOMER_TT_OVER, QOF_TYPE_BOOLEAN, (QofAccessFunc)gncCustomerGetTaxTableOverride,
(QofSetterFunc)gncCustomerSetTaxTableOverride
},
{ CUSTOMER_TERMS, GNC_ID_BILLTERM, (QofAccessFunc)gncCustomerGetTerms, (QofSetterFunc)gncCustomerSetTerms },
{ QOF_PARAM_ACTIVE, QOF_TYPE_BOOLEAN, (QofAccessFunc)gncCustomerGetActive, (QofSetterFunc)gncCustomerSetActive },
{ QOF_PARAM_BOOK, QOF_ID_BOOK, (QofAccessFunc)qof_instance_get_book, NULL },
{ QOF_PARAM_GUID, QOF_TYPE_GUID, (QofAccessFunc)qof_instance_get_guid, NULL },
{ NULL },
};
if (!qof_choice_add_class(GNC_ID_INVOICE, GNC_ID_CUSTOMER, INVOICE_OWNER))
{
return FALSE;
}
if (!qof_choice_add_class(GNC_ID_JOB, GNC_ID_CUSTOMER, JOB_OWNER))
{
return FALSE;
}
qof_class_register (_GNC_MOD_NAME, (QofSortFunc)gncCustomerCompare, params);
if (!qof_choice_create(GNC_ID_CUSTOMER))
{
return FALSE;
}
/* temp */
_gncCustomerPrintable(NULL);
return qof_object_register (&gncCustomerDesc);
}
gchar *gncCustomerNextID (QofBook *book)
{
return qof_book_increment_and_format_counter (book, _GNC_MOD_NAME);
}
const gnc_numeric*
gncCustomerGetCachedBalance (GncCustomer *cust)
{
return cust->balance;
}
void gncCustomerSetCachedBalance (GncCustomer *cust, const gnc_numeric *new_bal)
{
if (!new_bal)
{
if (cust->balance)
{
g_free (cust->balance);
cust->balance = NULL;
}
return;
}
if (!cust->balance)
cust->balance = g_new0 (gnc_numeric, 1);
*cust->balance = *new_bal;
}