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

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



Revision: 2848
          http://ledger-smb.svn.sourceforge.net/ledger-smb/?rev=2848&view=rev
Author:   einhverfr
Date:     2010-01-27 05:51:39 +0000 (Wed, 27 Jan 2010)

Log Message:
-----------
Asset fixes

Modified Paths:
--------------
    addons/1.3/assets/trunk/LedgerSMB/DBObject/Asset.pm
    addons/1.3/assets/trunk/LedgerSMB/DBObject/Asset_Report.pm
    addons/1.3/assets/trunk/UI/asset/asset.js
    addons/1.3/assets/trunk/UI/asset/begin_report.html
    addons/1.3/assets/trunk/UI/asset/edit_asset.html
    addons/1.3/assets/trunk/scripts/asset.pl
    addons/1.3/assets/trunk/sql/modules/Assets-tables.sql
    addons/1.3/assets/trunk/sql/modules/Assets.sql

Modified: addons/1.3/assets/trunk/LedgerSMB/DBObject/Asset.pm
===================================================================
--- addons/1.3/assets/trunk/LedgerSMB/DBObject/Asset.pm	2010-01-25 23:23:20 UTC (rev 2847)
+++ addons/1.3/assets/trunk/LedgerSMB/DBObject/Asset.pm	2010-01-27 05:51:39 UTC (rev 2848)
@@ -53,6 +53,10 @@
    @{$self->{departments}} = $self->exec_method(funcname => 'department__list_all');
     @{$self->{asset_accounts}} = $self->exec_method(funcname => 'asset_class__get_asset_accounts');
     @{$self->{dep_accounts}} = $self->exec_method(funcname => 'asset_class__get_dep_accounts');
+    my @dep_methods = $self->exec_method(funcname => 'asset_class__get_dep_methods');
+    for my $dep(@dep_methods){
+        $self->{dep_method}->{$dep->{id}} = $dep;
+    }
     for my $acc (@{$self->{asset_accounts}}){
         $acc->{text} = $acc->{accno} . '--' . $acc->{description};
     }

Modified: addons/1.3/assets/trunk/LedgerSMB/DBObject/Asset_Report.pm
===================================================================
--- addons/1.3/assets/trunk/LedgerSMB/DBObject/Asset_Report.pm	2010-01-25 23:23:20 UTC (rev 2847)
+++ addons/1.3/assets/trunk/LedgerSMB/DBObject/Asset_Report.pm	2010-01-27 05:51:39 UTC (rev 2848)
@@ -17,10 +17,23 @@
 
 sub save {
     my ($self) = @_;
-    my ($ref) = $self->exec_method(funcname => 'asset_report__save');
-    $self->{report_id} = $ref->{id};
-    my ($dep) = $self->exec_method(funcname => 'asset_class__get_dep_method');
-    $self->exec_method(funcname => $dep->{sproc});
+    if ($self->{depreciation}){
+        my ($ref) = $self->exec_method(funcname => 'asset_report__save');
+        $self->{report_id} = $ref->{id};
+        my ($dep) = $self->exec_method(funcname => 'asset_class__get_dep_method');
+        $self->exec_method(funcname => $dep->{sproc});
+    } else {
+       my ($ref) = $self->exec_method(funcname => 'asset_report__begin_disposal');
+       for my $i (1 .. $self->{rowcount}){
+           if ($self->{"asset_$i"} = 1){
+              my $id = $self->{"asset_id_$i"};
+              $self->call_procedure(procname => 'asset_report__dispose',
+                               args => [$self->{report_id}, 
+                                        $id, 
+                                        $self->{"amount_$id"}]);
+          }
+       }
+    }
     $self->{dbh}->commit;
 }
 

Modified: addons/1.3/assets/trunk/UI/asset/asset.js
===================================================================
--- addons/1.3/assets/trunk/UI/asset/asset.js	2010-01-25 23:23:20 UTC (rev 2847)
+++ addons/1.3/assets/trunk/UI/asset/asset.js	2010-01-27 05:51:39 UTC (rev 2848)
@@ -4,10 +4,14 @@
     var old_class = document.getElementById('last-class-id').value;
     var class_dropdown = document.getElementById('asset-class');
     var new_class = class_dropdown.options[class_dropdown.selectedIndex].value;
+    var unit_caption_e = document.getElementById('caption-usablelifegroup');
+    var unit_caption = document.getElementById('unit-label-' + new_class).value;
+
     if (new_class == ""){
         new_class = class_dropdown.options[0].value;
     }
     if (old_class != ""){
+        unit_caption_e.innerHTML = '(' + unit_caption + ')';
         document.getElementById('asset-account-default-' + old_class).value 
         = asset_dropdown.options[asset_dropdown.selectedIndex].value;
         document.getElementById('dep-account-default-' + old_class).value 
@@ -19,6 +23,7 @@
                  document.getElementById('asset-account-default-' + new_class).value);
     set_dropdown(dep_dropdown,
                  document.getElementById('dep-account-default-' + new_class).value);
+
 }
 
 function set_dropdown (selectElement, newValue){

Modified: addons/1.3/assets/trunk/UI/asset/begin_report.html
===================================================================
--- addons/1.3/assets/trunk/UI/asset/begin_report.html	2010-01-25 23:23:20 UTC (rev 2847)
+++ addons/1.3/assets/trunk/UI/asset/begin_report.html	2010-01-27 05:51:39 UTC (rev 2848)
@@ -22,36 +22,6 @@
      label = text("Date")
 } ?>
 </div></div>
-<?lsmb IF depreciation ?>
-     <div class="inputrow" id="exprow"><div class="inputgroup" id="expgrp">
-     <?lsmb PROCESS select element_data = {
-                    name = "exp_account_id"
-                    default_values = [exp_account_id]
-                    options = exp_accounts
-                    label = text('Expense Account') #'
-                    value_attr = 'id'
-     } ?>
-     </div></div>
-<?lsmb ELSE ?>
-     <div class="inputrow" id="gainrow"><div class="inputgroup" id="gaingrp">
-     <?lsmb PROCESS select element_data = {
-                    name = "gain_account_id"
-                    default_values = [gain_account_id]
-                    options = gain_accounts
-                    label = text('Gain Account') #'
-                    value_attr = 'id'
-     } ?>
-     </div></div>
-     <div class="inputrow" id="lossrow"><div class="inputgroup" id="lossgrp">
-     <?lsmb PROCESS select element_data = {
-                    name = "dep_expense_id"
-                    default_values = [loss_account_id]
-                    options = loss_accts
-                    label = text('Loss Account') #'
-                    value_attr = 'id'
-     } ?>
-     </div></div>
-<?lsmb END ?>
 <?lsmb PROCESS input element_data = {
      name = "depreciation"
      type = "hidden"

Modified: addons/1.3/assets/trunk/UI/asset/edit_asset.html
===================================================================
--- addons/1.3/assets/trunk/UI/asset/edit_asset.html	2010-01-25 23:23:20 UTC (rev 2847)
+++ addons/1.3/assets/trunk/UI/asset/edit_asset.html	2010-01-27 05:51:39 UTC (rev 2848)
@@ -23,6 +23,11 @@
        name = "dep_account_default_$a_class.id",
        value = a_class.dep_account_id,
 } ?>
+<?lsmb PROCESS input element_data = {
+       type = "hidden",
+       name = "unit_label_$a_class.id",
+       value = dep_method.${a_class.id}.unit_label,
+} ?>
 <?lsmb- END # For a_class ?>
 <?lsmb PROCESS input element_data = {
        type = "hidden"
@@ -91,7 +96,10 @@
 	type = "text"
 	size = 10
 } ?>
+<span class="caption" id="caption-usablelifegroup">
+(<?lsmb text('Method Default') ?>)
 </span>
+</span>
 <span class="inputgroup" id="salvagevaluegroup">
 <?lsmb PROCESS input element_data = {
 	label = text("Salvage Value:") #"

Modified: addons/1.3/assets/trunk/scripts/asset.pl
===================================================================
--- addons/1.3/assets/trunk/scripts/asset.pl	2010-01-25 23:23:20 UTC (rev 2847)
+++ addons/1.3/assets/trunk/scripts/asset.pl	2010-01-27 05:51:39 UTC (rev 2848)
@@ -321,6 +321,7 @@
        description    =>  $locale->text('Description') ,
        purchase_date  =>  $locale->text('Purchase Date') ,
        purchase_value =>  $locale->text('Purchase Value') ,
+       amount         =>  $locale->text('Amount'),
    };
    my $rows = [];
    my $hiddens = {};
@@ -339,6 +340,12 @@
               purchase_value => $request->format_amount(
                                          amount => $asset->{purchase_value}
                                 ),
+              amount         => {input => { name  => "amount_$asset->{id}",
+                                            type  => 'text',
+                                            class => 'amount',
+                                            value => $request->{"amount_$asset->{id}"},
+                                          },
+                                },
             };
        $hiddens->{"id_$count"} = $asset->{id};
        ++$count;
@@ -356,6 +363,7 @@
        $request->{title} = $locale->text('Asset Depreciation Report');
    } else {
        $request->{title} = $locale->text('Asset Disposal Report');
+       push @$cols, 'amount';
    }
    for $hide (qw(exp_account_id gain_account_id loss_account_id report_date 
                  asset_class rowcount depreciation))

Modified: addons/1.3/assets/trunk/sql/modules/Assets-tables.sql
===================================================================
--- addons/1.3/assets/trunk/sql/modules/Assets-tables.sql	2010-01-25 23:23:20 UTC (rev 2847)
+++ addons/1.3/assets/trunk/sql/modules/Assets-tables.sql	2010-01-27 05:51:39 UTC (rev 2848)
@@ -11,17 +11,19 @@
 	id serial unique not null,
 	method text primary key,
         sproc text not null unique,
+        unit_label text not null,
 	unit_class int not null references asset_unit_class(id) 
 );
 
 comment on column asset_dep_method.method IS 
 $$ These are keyed to specific stored procedures.  Currently only "straight_line" is supported$$;
 
-INSERT INTO asset_dep_method(method, unit_class, sproc) 
-values ('Annual Straight Line', 1, 'asset_dep_straight_line_yr');
+INSERT INTO asset_dep_method(method, unit_class, sproc, unit_label) 
+values ('Annual Straight Line', 1, 'asset_dep_straight_line_yr', 'in years');
 
-INSERT INTO asset_dep_method(method, unit_class, sproc) 
-values ('Whole Month Straight Line', 1, 'asset_dep_straight_line_whl_m');
+INSERT INTO asset_dep_method(method, unit_class, sproc, unit_label) 
+values ('Whole Month Straight Line', 1, 'asset_dep_straight_line_whl_m', 
+'in months');
 
 CREATE TABLE asset_class (
 	id serial not null unique,

Modified: addons/1.3/assets/trunk/sql/modules/Assets.sql
===================================================================
--- addons/1.3/assets/trunk/sql/modules/Assets.sql	2010-01-25 23:23:20 UTC (rev 2847)
+++ addons/1.3/assets/trunk/sql/modules/Assets.sql	2010-01-27 05:51:39 UTC (rev 2848)
@@ -38,12 +38,6 @@
      SELECT ai.id, $3,
             asset_dep__straight_line_base(
                                    ai.usable_life,
-                                   coalesce(
-                                            ai.start_depreciation, 
-                                            ai.purchase_date
-                                   ),
-                                   max(rep.report_date)
-                       ),
                        get_fractional_year(
                                    coalesce(
                                             max(rep.report_date), 
@@ -52,8 +46,7 @@
                                    ),
                                    $2),
                        ai.purchase_value - ai.salvage_value 
-                       - coalesce(sum(repline.amount), 0)
-            ),
+                       - coalesce(sum(repline.amount), 0)),
             ai.department_id, ai.location_id
        FROM asset_item ai
   LEFT JOIN asset_report_line repline ON (repline.asset_id = ai.id)
@@ -65,7 +58,7 @@
     select true;
 $$ language sql;
 
-CREATE OR REPLACE FUNCTION asset_report__generate_gl(in_report_id int)
+CREATE OR REPLACE FUNCTION asset_report__generate_gl(in_report_id int, in_accum_account_id int)
 RETURNS INT AS
 $$
 DECLARE 
@@ -86,7 +79,7 @@
 	GROUP BY asset_report.report_id, asset_item.department_id;
 
 	INSERT INTO acc_trans (trans_id, chart_id, transdate, approved, amount)
-	SELECT gl.id, a.asset_account_id, r.report_date, true, sum(amount)
+	SELECT gl.id, in_accum_account_id, r.report_date, true, sum(amount) * -1
 	FROM asset_report r
 	JOIN asset_report_line l ON (r.id = l.report_id)
 	JOIN asset_item a ON (l.asset_item_id = a.id)
@@ -94,10 +87,10 @@
 		a.department_id = gl.department_id)
 	JOIN asset_class c ON (a.class_id = c.id)
 	WHERE r.id = in_report_id
-	GROUP BY gl.id, a.asset_account_id, r.report_date;
+	GROUP BY gl.id, r.report_date;
 
 	INSERT INTO acc_trans (trans_id, chart_id, transdate, approved, amount)
-	SELECT gl.id, a.dep_account_id, r.report_date, true, sum(amount) * -1
+	SELECT gl.id, a.dep_account_id, r.report_date, true, sum(amount)
 	FROM asset_report r
 	JOIN asset_report_line l ON (r.id = l.report_id)
 	JOIN asset_item a ON (l.asset_item_id = a.id)
@@ -174,7 +167,7 @@
 
 CREATE OR REPLACE FUNCTION asset_class__save
 (in_id int, in_asset_account_id int, in_dep_account_id int, 
-in_method int, in_label text)
+in_method int, in_label text, in_unit_label text)
 RETURNS asset_class AS
 $$
 DECLARE ret_val asset_class;
@@ -386,7 +379,16 @@
 
 END;
 $$ language plpgsql;
+CREATE OR REPLACE FUNCTION asset_report__dispose
+(in_id int, in_asset_id int, in_amount numeric)
+returns bool AS
+$$
+INSERT INTO asset_report_line (report_id, asset_id, amount)
+                     values   ($1, $2, $3);
 
+SELECT TRUE;
+$$ language sql;
+
 CREATE OR REPLACE FUNCTION asset_report__approve
 (in_id int)
 RETURNS asset_report AS
@@ -447,5 +449,142 @@
    HAVING coalesce(max(ar.report_class), 1) = 1
           AND (ai.purchase_value - coalesce(sum(arl.amount), 0) 
                > ai.salvage_value
-               OR not $1)
+               OR $1 is not true)
 $$ language sql;
+
+
+CREATE OR REPLACE FUNCTION asset_report__begin_disposal(in_asset_class int, in_report_date date)
+returns asset_report as $$
+DECLARE retval asset_report;
+
+begin
+
+INSERT INTO asset_report (asset_class, report_date, entered_at, entered_by)
+   VALUES (in_asset_class, in_report_date, now(), person__get_my_entity_id());
+
+SELECT * INTO retval FROM asset_report where id = currval('asset_report_id_seq');
+
+return retval;
+
+end;
+
+$$ language plpgsql;
+
+create or replace function asset_depreciation__approve(in_report_id int, in_expense_acct int)
+returns asset_report
+as $$
+declare retval asset_report;
+begin
+
+UPDATE asset_report 
+   set approved_by = person__get_my_entity_id(),
+       approved_at = now()
+ where id = in_report_id;
+
+INSERT INTO gl (reference, description, approved)
+select 'Asset Report ' || in_id, 'Asset Depreciation Report for ' || report_date,
+       false
+ FROM asset_report where id = in_id;
+
+INSERT INTO acc_trans (amount, chart_id, transdate, approved, trans_id)
+SELECT l.amount, a.dep_account_id, r.report_date, true, currval('id')
+  FROM asset_report r
+  JOIN asset_report_line l ON (r.id = l.report_id)
+  JOIN asset_item a ON (a.id = l.asset_id)
+ WHERE r.id = in_id;
+
+INSERT INTO acc_trans (amount, chart_id, transdate, approved, trans_id)
+SELECT sum(l.amount) * -1, in_expense_acct, r.report_date, approved, 
+       currval('id')
+  FROM asset_report r
+  JOIN asset_report_line l ON (r.id = l.report_id)
+  JOIN asset_item a ON (a.id = l.asset_id)
+ WHERE r.id = in_id
+ GROUP BY r.report_date;
+
+
+return retval;
+
+end;
+$$ language plpgsql;
+
+CREATE OR REPLACE FUNCTION asset_disposal__approve
+(in_report_id int, in_gain_acct int, in_loss_acct int, in_asset_acct int)
+returns asset_report
+as $$
+DECLARE 
+   retval asset_report;
+   iter record;
+begin
+
+UPDATE asset_report 
+   set approved_by = person__get_my_entity_id(),
+       approved_at = now()
+ where id = in_report_id;
+
+INSERT INTO gl (reference, description, approved)
+select 'Asset Report ' || in_id, 'Asset Depreciation Report for ' || report_date,
+       false
+ FROM asset_report where id = in_id;
+
+SELECT * INTO retval FROM asset_report WHERE id = in_id;
+
+-- REMOVING ASSETS FROM ACCOUNT
+insert into acc_trans (trans_id, chart_id, amount, approved, transdate)
+SELECT currval('id'), a.asset_account_id, a.purchase_value, true, r.report_date
+ FROM  asset_item a
+ JOIN  asset_report_line l ON (l.asset_id = a.id)
+ JOIN  asset_report r ON (r.id = l.report_id)
+ WHERE r.id = in_id;
+
+-- REMOVING ACCUM DEP.
+INSERT into acc_trans (trans_id, chart_id, amount, approved, transdate)
+SELECT currval('id', a.dep_account_id, sum(dl.amount) * -1, true, r.report_date)
+ FROM  asset_item a
+ JOIN  asset_report_line l ON (l.asset_id = a.id)
+ JOIN  asset_report r ON (r.id = l.report_id)
+ JOIN  asset_report_line dl ON (l.asset_id = dl.asset_id)
+ JOIN  asset_report dr ON (dl.report_id = dr.id 
+                           and dr.depreciation is true 
+                           and dr.approved_at is not null)
+ WHERE r.id = in_id;
+
+-- INSERT asset change
+INSERT INTO acc_trans (trans_id, chart_id, amount, approved, transdate)
+SELECT currval('id'), in_asset_act, l.amount * -1, true, r.report_date
+ FROM  asset_item a
+ JOIN  asset_report_line l ON (l.asset_id = a.id)
+ JOIN  asset_report r ON (r.id = l.report_id)
+ WHERE r.id = in_id;
+
+-- INSERT GAIN/LOSS
+
+for iter in select sum(dl.amount) as dep, a.purchase_price , l.amount
+     FROM  asset_item a
+     JOIN  asset_report_line l ON (l.asset_id = a.id)
+     JOIN  asset_report r ON (r.id = l.report_id)
+     JOIN  asset_report_line dl ON (l.asset_id = dl.asset_id)
+     JOIN  asset_report dr ON (dl.report_id = dr.id 
+                               and dr.depreciation is true 
+                               and dr.approved_at is not null)
+     WHERE r.id = in_id
+loop
+   IF (iter.dep > iter.purchase_price - iter.amount)
+
+   THEN
+      INSERT INTO acc_trans (trans_id, chart_id, amount, approved, transdate)
+      values (currval('id'), in_loss_acct, 
+              iter.purchase_price - iter.dep - iter.amount, true, 
+              retval.report_date);
+   ELSE
+      INSERT INTO acc_trans (trans_id, chart_id, amount, approved, transdate)
+      values (currval('id'), in_gain_acct, 
+              iter.purchase_price - iter.dep - iter.amount, true, 
+              retval.report_date);
+   END IF;
+
+END LOOP;
+
+return retval;
+end;
+$$ language plpgsql;


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