[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

SF.net SVN: ledger-smb:[6269] addons/1.3/wxPOS/WXPOS/Invoice.pm



Revision: 6269
          http://sourceforge.net/p/ledger-smb/code/6269
Author:   einhverfr
Date:     2013-11-09 04:19:15 +0000 (Sat, 09 Nov 2013)
Log Message:
-----------
First efforts at Invoice object for posting/printing invoices.

Added Paths:
-----------
    addons/1.3/wxPOS/WXPOS/Invoice.pm

Added: addons/1.3/wxPOS/WXPOS/Invoice.pm
===================================================================
--- addons/1.3/wxPOS/WXPOS/Invoice.pm	                        (rev 0)
+++ addons/1.3/wxPOS/WXPOS/Invoice.pm	2013-11-09 04:19:15 UTC (rev 6269)
@@ -0,0 +1,261 @@
+=head1 NAME 
+
+WXPOS::Invoice - Invoice processing routines for WXPOS.
+
+=head1 SYNPOSIS
+
+  my $invoice = WXPOS::Invoice->from_UI($invoice_widget, $entity_class);
+  $invoice->post;
+  $invoice->print($printer, 'PDF');
+
+=cut
+
+package WXPOS::Invoice;
+use Moo;
+use Carp;
+use WXPOS::State;
+with 'PGObject::Simple::Role';
+
+sub _get_prefix {
+    return 'invoice__';
+}
+
+=head1 DESCRIPTION
+
+This is a module which allows you to manipulate sales and purchase invoices in
+wxpos.  
+
+=head1 PROPERTIES
+
+=head2 id
+
+The id of the invoice, set on posting
+
+=cut
+
+has id => (is => 'rw', required => 0);
+
+=head2 entity_class
+
+1 for AP, 2 for AR
+
+=cut
+
+has entity_class => (is => 'ro', required => 1,
+    isa => sub { croak 'entity_class must be 1 or 2' 
+                  unless ($_[0] eq '1' or $_[0] eq '2'); } );
+
+=head2 invnumber
+
+Invoice number. Not required, is lazily set.
+
+=cut
+
+has invnumber => (is => 'lazy');
+
+=head2 transdate
+
+Invoice date, defaults to 'today'
+
+=cut
+
+has transdate => (is => 'ro', default => 'today');
+
+=head2 amount
+
+Invoice total
+
+=cut
+
+has amount => (is => 'ro', required => 1);
+
+=head2 netamount
+
+Invoice total, sans taxes
+
+=cut
+
+has netamount => (is => 'ro', required => 1);
+
+=head2 paid
+
+Amount paid
+
+=cut
+
+has paid => (is => 'ro', required => 1);
+
+=head2 invoice
+
+Always true, don't set this.
+
+=cut
+
+has invoice => (is => 'ro', default => 1);
+
+=head2 curr
+
+Only set this if using multiple currencies.
+
+=cut
+
+has curr => (is => 'ro');
+
+=head2 entity_credit_account
+
+The eca id of the customer or vendor attached.
+
+=cut
+
+has entity_credit_account => (is => 'ro', default => 1);
+
+=head2 payments
+
+This is a list of hashrefs for the payments.  Each hashref must contain the 
+following keys:  amount, source, and memo.
+
+=cut
+
+has payments => (is => 'ro', required => 1, isa => sub {_isa_payments(@_)});
+
+sub _isa_payments {
+    my ($payments) = @_;
+    foreach my $pmt (@$payments){
+        for my $key (qw(amount source memo)){
+            croak "payment missing key $key" unless $pmt->{$key};
+        }
+    }
+}
+
+=head2 invoice_lines
+
+This is a list of invoice lines.  Each is a has ref which must contain the 
+followign keys, with a few being optional:
+
+=over
+
+=item partnumber
+
+=item description (optional, defaults to part's description)
+
+=item qty
+
+=item sellprice
+
+=item discount (optional, defaults to 0)
+
+=back
+
+=cut
+
+has invoice_lines => (is => 'ro', isa => sub {_isa_invoice_lines(@_)}, 
+                required => 1);
+
+sub _isa_invoice_lines {
+    my ($lines) = @_;
+    foreach my $line (@$lines){
+        for my $key (qw(partnumber qty sellprice)){
+            croak "Invoice line missing key $key" unless $line->{$key};
+        }
+    }
+}
+
+=head1 METHODS
+
+=head2 from_UI 
+
+Generates an invoice object from a UI widget, allowing the UI widget to encapsulate
+and modularize the logic required for object creation.  The object must provide 
+the following interfaces:
+
+=over
+
+=item get_entity_credit_account($uiwidget, $entity_class)
+
+=item get_amounts() 
+
+must return a hashref containing amount, netamount, and paid
+
+=item get_payments() 
+
+must return an array of payment lines (amount, source, memo)
+
+=item get_lines() 
+
+must return an array, minimally partnumber sellprice, and qty fields, and may 
+optionally return description and discount fields as well.
+
+=cut
+
+sub from_UI{
+    my ($self, $widget, $entity_class) = @_;
+    croak 'No entity_class provided' unless $entity_class;
+    croak 'Widget cannot get_entity_credit_account'
+          unless eval {$widget->can('get_entity_credit_account')};
+    croak 'Widget cannot get_amounts'
+          unless eval {$widget->can('get_amounts')};
+    croak 'Widget cannot get_payments'
+          unless eval {$widget->can('get_payments')};
+    croak 'Widget cannot get_lines'
+          unless eval {$widget->can('get_lines')};
+    my %arghash = (entity_class => $entity_class);
+    $arghash{entity_credit_account} = $widget->get_entity_credit_account;
+    my $amts = $widget->get_amounts();
+    for my $key(qw(amount netamount paid)){
+        $arghash{$key} = $amts->{$key};
+    }
+    $arghash{payments} = $widget->get_payments;
+    $arghash{invoice_lines} = $wisget->get_lines;
+    return __PACKAGE__->new(%arghash);
+}
+
+=head2 post
+
+Posts an invoice to the database.
+
+=cut
+
+sub post {
+    my ($self) = @_;
+    my $dbh = $self->_get_dbh;
+    my $sfx;
+
+    if ('1' eq $self->entity_class){
+        $sfx = 'ap';
+    } elsif ('2' eq $self->entity_class){
+        $sfx = 'ar';
+    } 
+
+    $dbh->begin_work;
+    my ($ref) = $self->call_dbmethod(funcname => "begin_$sfx");
+    my ($id) = values (%$ref);
+    $self->id($id);
+    foreach my $line (@{$self->invoice_lines}){
+        $self->call_dbmethod(funcname => "add_item_$sfx", args => $line);
+    }
+    foreach my $pmt (@{$self->payments}){
+        $self->call_dbmethod(funcname => "add_payment_$sfx", args => $pmt);
+    }
+    $self->call_dbmethod(funcname => "finalize_$sfx");
+    $dbh->commit;
+}
+
+=head2 print($printer, $format)
+
+Prints an invoice.
+
+=cut
+
+sub print {
+    # TODO
+}
+
+=head1 COPYRIGHT
+
+Copyright (C) 2013 The LedgerSMB Core Team.  This file may be re-used under the
+terms of the GNU General Public License version 2 or at your option any later 
+version.  Please see the enclosed LICENSE.txt for details.
+
+=cut
+
+1;

This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.


------------------------------------------------------------------------------
November Webinars for C, C++, Fortran Developers
Accelerate application performance with scalable programming models. Explore
techniques for threading, error checking, porting, and tuning. Get the most 
from the latest Intel processors and coprocessors. See abstracts and register
http://pubads.g.doubleclick.net/gampad/clk?id=60136231&iu=/4140/ostg.clktrk
_______________________________________________
Ledger-smb-commits mailing list
..hidden..
https://lists.sourceforge.net/lists/listinfo/ledger-smb-commits