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

SF.net SVN: ledger-smb:[2944] addons/1.3/assets/trunk



Revision: 2944
          http://ledger-smb.svn.sourceforge.net/ledger-smb/?rev=2944&view=rev
Author:   einhverfr
Date:     2010-03-03 19:14:17 +0000 (Wed, 03 Mar 2010)

Log Message:
-----------
Correcting disposal approval issues

Modified Paths:
--------------
    addons/1.3/assets/trunk/scripts/asset.pl
    addons/1.3/assets/trunk/sql/modules/Assets.sql

Modified: addons/1.3/assets/trunk/scripts/asset.pl
===================================================================
--- addons/1.3/assets/trunk/scripts/asset.pl	2010-03-03 17:02:45 UTC (rev 2943)
+++ addons/1.3/assets/trunk/scripts/asset.pl	2010-03-03 19:14:17 UTC (rev 2944)
@@ -457,9 +457,13 @@
     my $hiddens = {};
     my $count = 0;
     my $base_href = "asset.pl?action=report_details&".
-                     "expense_acct=$ar->{expense_acct}&".
-                     "depreciation=$ar->{depreciation}";
+                     "expense_acct=$ar->{expense_acct}";
+    if ($ar->{depreciation}){
+             $base_href .= '&depreciation=1';
+    }
     for my $r (@results){
+        next if (($r->{report_class} != 1 and $ar->{depreciation})
+                 or ($r->{report_class} == 1 and !$ar->{depreciation}));
         $hiddens->{"id_$count"} = $r->{id};
         my $ref = {
               select         => {input => { name    => "report_$count",
@@ -518,6 +522,10 @@
 
 sub report_details {
     my ($request) = @_;
+    if (!$request->{depreciation}){
+       disposal_details($request);
+       exit;
+    }
     my $locale = $request->{_locale};
     my $report = LedgerSMB::DBObject::Asset_Report->new(base => $request);
     $report->get;
@@ -573,6 +581,65 @@
     });
 }
 
+sub disposal_details {
+    my ($request) = @_;
+    my $locale = $request->{_locale};
+    my $report = LedgerSMB::DBObject::Asset_Report->new(base => $request);
+    $report->get;
+    my @cols = qw(tag description start_dep disposed_on dm purchase_value
+                 accum_depreciation adj_basis gain_loss);
+    $report->{title} = $locale->text("Report [_1] on date [_2]", 
+                     $report->{id}, $report->{report_date});
+    my $header = {
+                            tag => $locale->text('Tag'),
+                      start_dep => $locale->text('Dep. Starts'),
+                     disposed_on => $locale->text('Disposal Date'),
+                 purchase_value =>$locale->text('Aquired Value'),
+                             dm =>$locale->text('D M'),
+             accum_depreciation =>$locale->text('Accum. Depreciation'),
+                      adj_basis =>$locale->text('Adjusted Basis'),
+                      gain_loss =>$locale->text('Gain (Loss)'),
+    };
+    my $rows = [];
+    for my $r (@{$report->{report_lines}}){
+        $r->{usable_life} = $report->format_amount({amount => $r->{usable_life}});
+        for my $amt (qw(purchase_value adj_basis)){
+             $r->{$amt} = $report->format_amount({amount => $r->{$amt},
+                                              money  => 1,});
+        }
+        $r->{gain_loss} = $report->format_amount({amount => $r->{gain_loss},
+                                                 money => 1,
+                                               neg_format => '-' } );
+        push @$rows, $r;
+    }
+    my $template = LedgerSMB::Template->new(
+        user =>$request->{_user}, 
+        locale => $request->{_locale},
+        path => 'UI',
+        template => 'form-dynatable',
+        format => 'HTML'
+    );
+    my $buttons = [{
+                   text  => $locale->text('Approve'),
+                   type  => 'submit',
+                   class => 'submit',
+                   name =>  'action',
+                   value => 'approve'
+                   },
+    ];
+    $template->render({form => $report, 
+                    columns => ..hidden.., 
+                    heading => $header,
+                       rows => $rows,
+                    hiddens => $report,
+                    buttons => $buttons
+    });
+}
+
+sub disposal_details_approve {
+    report_details_approve(@_);
+}
+
 sub report_details_approve {
     my ($request) = @_;
     my $report = LedgerSMB::DBObject::Asset_Report->new(base => $request);

Modified: addons/1.3/assets/trunk/sql/modules/Assets.sql
===================================================================
--- addons/1.3/assets/trunk/sql/modules/Assets.sql	2010-03-03 17:02:45 UTC (rev 2943)
+++ addons/1.3/assets/trunk/sql/modules/Assets.sql	2010-03-03 19:14:17 UTC (rev 2944)
@@ -485,8 +485,50 @@
 SELECT TRUE;
 $$ language sql;
 
+CREATE TYPE asset_disposal_report_line 
+AS (
+       id int,
+       tag text,
+       description text,
+       start_dep date,
+       disposed_on date,
+       dm char(1),
+       purchase_value numeric,
+       accum_depreciation numeric,
+       disposal_amt numeric,
+       adj_basis numeric,
+       gain_loss numeric
+);
+
+CREATE OR REPLACE FUNCTION get_disposal_report (in_id int)
+returns setof asset_disposal_report_line AS
+$$
+   SELECT ai.id, ai.tag, ai.description, ai.start_depreciation, r.report_date,
+          'A'::char, ai.purchase_value, 
+          sum (CASE WHEN pr.report_class = 1 THEN prl.amount ELSE 0 END) 
+          as accum_dep,
+          l.amount, 
+          ai.purchase_value - sum(CASE WHEN pr.report_class = 1 
+                                       THEN prl.amount 
+                                       ELSE 0 
+                                   END) as adjusted_basis,
+          l.amount - ai.purchase_value + sum(CASE WHEN pr.report_class = 1
+                                                  THEN prl.amount 
+                                                  ELSE 0 
+                                              END) as gain_loss
+     FROM asset_item ai
+     JOIN asset_report_line l   ON (l.report_id = $1 AND ai.id = l.asset_id)
+     JOIN asset_report r        ON (l.report_id = r.id)
+LEFT JOIN asset_report_line prl ON (prl.report_id <> $1 
+                                   AND ai.id = prl.asset_id)
+LEFT JOIN asset_report pr       ON (prl.report_id = pr.id)
+ GROUP BY ai.id, ai.tag, ai.description, ai.start_depreciation, r.report_date,
+          ai.purchase_value, l.amount
+ ORDER BY ai.id, ai.tag;
+$$ language sql;
+
 CREATE OR REPLACE FUNCTION asset_report__approve
-(in_id int, in_expense_acct int)
+(in_id int, in_expense_acct int, in_gain_acct int, in_loss_acct int)
 RETURNS asset_report AS
 $$
 DECLARE ret_val asset_report;
@@ -497,7 +539,13 @@
          where id = in_id;
 	SELECT * INTO ret_val FROM asset_report WHERE id = in_id;
         if ret_val.dont_approve is not true then 
-                PERFORM asset_report__generate_gl(in_id, in_expense_acct);
+                if retval.report_class = 1 THEN
+                    PERFORM asset_report__generate_gl(in_id, in_expense_acct);
+                ELSIF retval.report_class = 1 THEN
+                    PERFORM asset_report__disposal_gl(
+                                 in_id, in_gain_acct, in_loss_acct);
+                ELSE RAISE EXCEPTION 'Invalid report class';
+                END IF;
         end if;
 	SELECT * INTO ret_val FROM asset_report WHERE id = in_id;
 	RETURN ret_val;
@@ -505,7 +553,48 @@
 $$ language plpgsql;
 revoke execute on function asset_report__approve(int, int) from public;
 
+CREATE OR REPLACE FUNCTION asset_report__disposal_gl
+(in_id int, in_gain_acct int, in_loss_acct int)
+RETURNS bool AS
+$$
+  INSERT 
+    INTO gl (reference, description, transdate, approved)
+  SELECT setting_increment('glnumber'), 'Asset Report ' || asset_report.id,
+		report_date, false
+    FROM asset_report 
+    JOIN asset_report_line ON (asset_report.id = asset_report_line.report_id)
+    JOIN asset_item        ON (asset_report_line.asset_id = asset_item.id)
+   WHERE asset_report.id = in_report_id
+GROUP BY asset_report.id, asset_report.report_date;
 
+  INSERT
+    INTO acc_trans (chart_id, trans_id, amount, approved, transdate)
+  SELECT a.dep_account_id, currval('id')::int, sum(r.accum_depreciation),
+         TRUE, r.disposed_on
+    FROM get_disposal_report(in_id) r
+    JOIN asset_item ai ON (r.id = ai.id)
+GROUP BY a.dep_account_id, r.disposed_on;
+
+  INSERT
+    INTO acc_trans (chart_id, trans_id, amount, approved, transdate)
+  SELECT case when sum(r.gain_loss) > 0 THEN $2 else $3 end,
+         currval('id')::int, sum(r.gain_loss) * -1,
+         TRUE, r.disposed_on
+    FROM get_disposal_report(in_id) r
+    JOIN asset_item ai ON (r.id = ai.id)
+GROUP BY r.disposed_on;
+
+  INSERT
+    INTO acc_trans (chart_id, trans_id, amount, approved, transdate)
+  SELECT a.asset_account_id, currval('id')::int, sum(r.purchase_value),
+         TRUE, r.disposed_on
+    FROM get_disposal_report(in_id) r
+    JOIN asset_item ai ON (r.id = ai.id)
+GROUP BY a.asset_account_id, r.disposed_on;
+
+  SELECT TRUE;
+$$ language sql;
+
 CREATE OR REPLACE FUNCTION asset_item__add_note(in_id int, in_subject text, in_note text)
 RETURNS asset_note AS
 $$


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