initial fund_debit creation from PO code
authorerickson <erickson@dcc99617-32d9-48b4-a31d-7c20da2025e4>
Thu, 17 Jul 2008 21:24:12 +0000 (21:24 +0000)
committererickson <erickson@dcc99617-32d9-48b4-a31d-7c20da2025e4>
Thu, 17 Jul 2008 21:24:12 +0000 (21:24 +0000)
git-svn-id: svn://svn.open-ils.org/ILS/branches/acq-experiment@10062 dcc99617-32d9-48b4-a31d-7c20da2025e4

Open-ILS/src/perlmods/OpenILS/Application/Acq/Financials.pm

index 1dee711..77106b2 100644 (file)
@@ -557,10 +557,117 @@ sub create_purchase_order {
         $e->allowed('MANAGE_PROVIDER', $provider->owner, $provider);
 
     $e->create_acq_purchase_order($p_order) or return $e->die_event;
+
     $e->commit;
     return $p_order->id;
 }
 
+
+# returns (price, type), where type=1 is usr, type=2 is provider, type=3 is marc
+sub get_li_price {
+    my $li = shift;
+    my $attrs = $li->attributes;
+    my ($marc_price, $usr_estimated, $usr_actual, $prov_estimated, $prov_actual);
+
+    for my $attr (@$attrs) {
+        if($attr->attr_name eq 'price') { # marc attr
+            $marc_price = $attr->attr_value;
+
+        } elsif($attr->attr_name eq 'estimated_price') {
+            $usr_estimated = $attr->attr_value 
+                if $attr->attr_type eq 'lineitem_usr_attr_definition';
+            $prov_estimated = $attr->attr_value 
+                if $attr->attr_type eq 'lineitem_prov_attr_definition';
+
+        } elsif($attr->attr_name eq 'actual_price') {
+            $usr_actual = $attr->attr_value     
+                if $attr->attr_type eq 'lineitem_usr_attr_definition';
+            $prov_actual = $attr->attr_value 
+                if $attr->attr_type eq 'lineitem_prov_attr_definition';
+        }
+    }
+
+    return ($usr_actual, 1) if $usr_actual;
+    return ($prov_actual, 2) if $prov_actual;
+    return ($usr_estimated, 1) if $usr_estimated;
+    return ($prov_estimated, 2) if $prov_estimated;
+    return ($marc_price, 3);
+}
+
+
+__PACKAGE__->register_method(
+       method => 'create_purchase_order_debits',
+       api_name        => 'open-ils.acq.purchase_order.debits.create',
+       signature => {
+        desc => 'Creates debits associated with a PO',
+        params => [
+            {desc => 'Authentication token', type => 'string'},
+            {desc => 'purchase_order whose debits to create', type => 'number'},
+            {desc => 'arguments hash.  Options include: encumbrance=bool', type => 'object'},
+        ],
+        return => {desc => 'The total amount of all created debits, Event on error'}
+    }
+);
+
+sub create_purchase_order_debits {
+    my($self, $conn, $auth, $po_id, $args) = @_;
+    my $e = new_editor(xact=>1, authtoken=>$auth);
+    return $e->die_event unless $e->checkauth;
+    
+    my $total = 0;
+    my $po = $e->retrieve_acq_purchase_order($po_id) or return $e->die_event;
+    # XXX which perms?
+
+    my $o = 0;
+    while(my $li = $e->search_acq_lineitem([
+        {   purchase_order => $po_id}, 
+        {   offset => $o++, 
+            limit => 1,
+            flesh => 1,
+            flesh_fields => {jub => ['attributes']}}])->[0]) {
+
+        my ($price, $ptype) = get_li_price($li);
+        unless($price) {
+            $e->rollback;
+            return OpenILS::Event->new('ACQ_LINEITEM_NO_PRICE', payload => $li->id);
+        }
+
+        unless($li->provider) {
+            $e->rollback;
+            return OpenILS::Event->new('ACQ_LINEITEM_NO_PROVIDER', payload => $li->id);
+        }
+
+        my $oo = 0;
+        while(my $lid = $e->search_acq_lineitem_detail([
+                {  lineitem => $li->id}, 
+                {   offset => $oo++, 
+                    limit => 1,
+                    flesh => 1, 
+                    flesh_fields => {acqlid => ['fund']}}])->[0]) {
+
+            my $debit = Fieldmapper::acq::fund_debut->new;
+            $debit->fund($lid->fund->id);
+            $debit->origin_amount($price);
+            if($ptype == 2) { # price from vendor
+                $debit->origin_currency_type($li->provider->currency_type);
+                $debit->amount(currency_conversion_impl(
+                    $li->provider->currency_type, $lid->fund->currency_type, $price));
+            } else {
+                $debit->origin_currency_type($lid->fund->currency_type);
+                $debit->amount($price);
+            }
+            $debit->encumbrance($args->{encumbrance});
+            $debit->debit_type('purchase');
+            $e->create_acq_fund_debit($debit) or return $e->die_event;
+            $total += $debit->amount;
+        }
+    }
+
+    $e->commit;
+    return $total;
+}
+
+
 __PACKAGE__->register_method(
        method => 'retrieve_all_user_purchase_order',
        api_name        => 'open-ils.acq.purchase_order.user.all.retrieve',