From ba0fb69ce21dd281a09d91418785fa87203f9e6c Mon Sep 17 00:00:00 2001 From: =?utf8?q?Sven=20Sch=C3=B6ling?= Date: Tue, 13 Mar 2012 14:05:21 +0100 Subject: [PATCH] sellprice information test commit --- SL/Controller/Base.pm | 4 +- SL/Controller/SellPriceInformation.pm | 196 ++++++++++++++++++ SL/DB/Manager/Order.pm | 15 +- SL/DB/OrderItem.pm | 17 +- .../webpages/oe/sales_price_information.html | 113 ++++++++++ .../price_information/report_bottom.html | 1 + 6 files changed, 337 insertions(+), 9 deletions(-) create mode 100644 SL/Controller/SellPriceInformation.pm create mode 100644 templates/webpages/oe/sales_price_information.html create mode 100644 templates/webpages/price_information/report_bottom.html diff --git a/SL/Controller/Base.pm b/SL/Controller/Base.pm index 285bfcee4..ac09057af 100644 --- a/SL/Controller/Base.pm +++ b/SL/Controller/Base.pm @@ -7,6 +7,8 @@ use parent qw(Rose::Object); use Carp; use IO::File; use List::Util qw(first); +use SL::Request qw(flatten); +use SL::MoreCommon qw(uri_encode); # # public/helper functions @@ -21,7 +23,7 @@ sub url_for { my $controller = delete($params{controller}) || $self->_controller_name; my $action = delete($params{action}) || 'dispatch'; $params{action} = "${controller}/${action}"; - my $query = join('&', map { $::form->escape($_) . '=' . $::form->escape($params{$_}) } keys %params); + my $query = join '&', map { uri_encode($_->[0]) . '=' . uri_encode($_->[1]) } @{ flatten(\%params) }; return "controller.pl?${query}"; } diff --git a/SL/Controller/SellPriceInformation.pm b/SL/Controller/SellPriceInformation.pm new file mode 100644 index 000000000..8255e4140 --- /dev/null +++ b/SL/Controller/SellPriceInformation.pm @@ -0,0 +1,196 @@ +package SL::Controller::SellPriceInformation; + +use strict; +use parent qw(SL::Controller::Base); + +use Clone qw(clone); +use SL::DB::OrderItem; +use SL::Controller::Helper::ParseFilter; +use SL::Controller::Helper::ReportGenerator; + +sub action_list { + my ($self) = @_; + $self->{action} = 'list'; + + my %list_params = ( + sort_by => $::form->{sort_by} || 'reqdate', + sort_dir => $::form->{sort_dir}, + filter => $::form->{filter}, + page => $::form->{page}, + ); + + my $db_args = $self->setup_for_list(%list_params); + $self->{pages} = SL::DB::Manager::OrderItem->paginate(%list_params, args => $db_args, per_page => 5); + + my $bottom = $::form->parse_html_template('price_information/report_bottom', { SELF => $self }); + + $self->prepare_report( + report_generator_options => { + raw_bottom_info_text => $bottom, + controller_class => 'SellPriceInformation', + }, + db_args => $db_args, + ); + + $self->{orderitems} = SL::DB::Manager::OrderItem->get_all(%$db_args); + + $self->list_objects; +} + +# private functions + +sub setup_for_list { + my ($self, %params) = @_; + $self->{filter} = _pre_parse_filter($params{filter}); + + my %args = ( + parse_filter($self->{filter}, + with_objects => [ 'order', 'order.customer', 'part' ], + ), + sort_by => $self->set_sort_params(%params), + page => $params{page}, + ); + +# $args{query} = [ @{ $args{query} || [] } ]; + + return \%args; +} + +sub set_sort_params { + my ($self, %params) = @_; + my $sort_str; + ($self->{sort_by}, $self->{sort_dir}, $sort_str) = + SL::DB::Manager::OrderItem->make_sort_string(%params); + return $sort_str; +} + +sub prepare_report { + my ($self, %params) = @_; + + my $objects = $params{objects} || []; + my $report = SL::ReportGenerator->new(\%::myconfig, $::form); + $self->{report} = $report; + + my @columns = qw(orddate ordnumber customer ship qty sellprice discount amount); + my @visible = qw(orddate ordnumber customer ship qty sellprice discount amount); + my @sortable = qw(orddate ordnumber customer sellprice discount ); + + my %column_defs = ( + orddate => { text => $::locale->text('Date'), + sub => sub { $_[0]->order->orddate_as_date }}, + ordnumber => { text => $::locale->text('Number'), + sub => sub { $_[0]->order->ordnumber }, + obj_link => sub { $self->link_to($_[0]->order) }}, + customer => { text => $::locale->text('Customer'), + sub => sub { $_[0]->order->customer->name }, + obj_link => sub { $self->link_to($_[0]->order->customer) }}, + customer => { text => $::locale->text('Customer'), + sub => sub { $_[0]->order->customer->name }, + obj_link => sub { $self->link_to($_[0]->order->customer) }}, + ship => { text => $::locale->text('Delivered'), + sub => sub { $::form->format_amount(\%::myconfig, $_[0]->shipped_qty) . ' ' . $_[0]->unit }}, + qty => { text => $::locale->text('Qty'), + sub => sub { $_[0]->qty_as_number . ' ' . $_[0]->unit }}, + sellprice => { text => $::locale->text('Sell Price'), + sub => sub { $_[0]->sellprice_as_number }}, + discount => { text => $::locale->text('Discount'), + sub => sub { $_[0]->discount_as_percent . "%" }}, + amount => { text => $::locale->text('Amount'), + sub => sub { $::form->format_amount(\%::myconfig, $_[0]->qty * $_[0]->sellprice * (1 - $_[0]->discount), 2) }}, + ); + + for my $col (@sortable) { + $column_defs{$col}{link} = $self->self_url( + sort_by => $col, + sort_dir => ($self->{sort_by} eq $col ? 1 - $self->{sort_dir} : $self->{sort_dir}), + page => $self->{pages}{cur}, + ); + } + + map { $column_defs{$_}->{visible} = 1 } @visible; + + $report->set_columns(%column_defs); + $report->set_column_order(@columns); + $report->set_options(allow_pdf_export => 0, allow_csv_export => 0); + $report->set_sort_indicator(%params); + $report->set_export_options(@{ $params{report_generator_export_options} || [] }); + $report->set_options( + %{ $params{report_generator_options} || {} }, + output_format => 'HTML', + top_info_text => $::locale->text('Sales Price Information'), + title => $::locale->text('Sales Price information'), + ); + $report->set_options_from_form; + + $self->{report_data} = { + column_defs => \%column_defs, + columns => \@columns, + visible => \@visible, + sortable => \@sortable, + }; +} + +sub list_objects { + my ($self) = @_; + my $column_defs = $self->{report_data}{column_defs}; + for my $obj (@{ $self->{orderitems} || [] }) { + $self->{report}->add_data({ + map { + $_ => { + data => $column_defs->{$_}{sub} ? $column_defs->{$_}{sub}->($obj) + : $obj->can($_) ? $obj->$_ + : $obj->{$_}, + link => $column_defs->{$_}{obj_link} ? $column_defs->{$_}{obj_link}->($obj) : '', + }, + } @{ $self->{report_data}{columns} || {} } + }); + } + + return $self->{report}->generate_with_headers; +} + +sub link_to { + my ($self, $object, %params) = @_; + + return unless $object; + my $action = $params{action} || 'edit'; + + if ($object->isa('SL::DB::Order')) { + my $type = $object->type; + my $vc = $object->is_sales ? 'customer' : 'vendor'; + my $id = $object->id; + + return "oe.pl?action=$action&type=$type&vc=$vc&id=$id"; + } + if ($object->isa('SL::DB::Customer')) { + my $id = $object->id; + return "ct.pl?action=$action&id=$id&db=customer"; + } +} + +sub _pre_parse_filter { + my $filter = clone(shift); + + if ( exists $filter->{order} + && exists $filter->{order}{type}) { + push @{ $filter->{and} }, SL::DB::Manager::Order->type_filter(delete $filter->{order}{type}, "order."), + } + + return $filter; +} + +sub self_url { + my ($self, %params) = @_; + %params = ( + action => $self->{action}, + sort_by => $self->{sort}, + sort_dir => $self->{sort_dir}, + page => $self->{pages}{cur}, + filter => $::form->{filter}, + %params, + ); + + return $self->url_for(%params); +} + +1; diff --git a/SL/DB/Manager/Order.pm b/SL/DB/Manager/Order.pm index fc2b796ba..ff8ece29f 100644 --- a/SL/DB/Manager/Order.pm +++ b/SL/DB/Manager/Order.pm @@ -10,13 +10,14 @@ sub object_class { 'SL::DB::Order' } __PACKAGE__->make_manager_methods; sub type_filter { - my $class = shift; - my $type = lc(shift || ''); - - return (and => [ '!customer_id' => undef, quotation => 1 ]) if $type eq 'sales_quotation'; - return (and => [ '!vendor_id' => undef, quotation => 1 ]) if $type eq 'request_quotation'; - return (and => [ '!customer_id' => undef, or => [ quotation => 0, quotation => undef ] ]) if $type eq 'sales_order'; - return (and => [ '!vendor_id' => undef, or => [ quotation => 0, quotation => undef ] ]) if $type eq 'purchase_order'; + my $class = shift; + my $type = lc(shift || ''); + my $prefix = shift || ''; + + return (and => [ "!${prefix}customer_id" => undef, "${prefix}quotation" => 1 ]) if $type eq 'sales_quotation'; + return (and => [ "!${prefix}vendor_id" => undef, "${prefix}quotation" => 1 ]) if $type eq 'request_quotation'; + return (and => [ "!${prefix}customer_id" => undef, or => [ "${prefix}quotation" => 0, "${prefix}quotation" => undef ] ]) if $type eq 'sales_order'; + return (and => [ "!${prefix}vendor_id" => undef, or => [ "${prefix}quotation" => 0, "${prefix}quotation" => undef ] ]) if $type eq 'purchase_order'; die "Unknown type $type"; } diff --git a/SL/DB/OrderItem.pm b/SL/DB/OrderItem.pm index 6efee0049..5c18c4ce9 100644 --- a/SL/DB/OrderItem.pm +++ b/SL/DB/OrderItem.pm @@ -2,6 +2,9 @@ package SL::DB::OrderItem; use strict; +use List::Util qw(sum); +use SL::AM; + use SL::DB::MetaSetup::OrderItem; use SL::DB::Helper::CustomVariables ( sub_module => 'orderitems', @@ -44,6 +47,15 @@ sub is_price_update_available { return $self->origprice > $self->part->sellprice; } +sub shipped_qty { + my ($self) = @_; + + my $d_orders = $self->order->linked_records(direction => 'to', to => 'SL::DB::DeliveryOrder'); + my @doi = grep { $_->parts_id == $self->parts_id } map { $_->orderitems } @$d_orders; + + return sum(map { AM->convert_unit($_->unit => $self->unit) * $_->qty } @doi); +} + package SL::DB::Manager::OrderItem; use SL::DB::Helper::Paginated; @@ -57,7 +69,10 @@ sub _sort_spec { ordnumber => [ 'order.ordnumber' ], customer => [ 'lower(customer.name)', ], position => [ 'trans_id', 'runningnumber' ], - transdate => [ 'transdate', 'lower(order.reqdate::text)' ], + reqdate => [ 'COALESCE(orderitems.reqdate, order.transdate)' ], + orddate => [ 'order.orddate' ], + sellprice => [ 'sellprice' ], + discount => [ 'discount' ], }, default => [ 'position', 1 ], nulls => { } diff --git a/templates/webpages/oe/sales_price_information.html b/templates/webpages/oe/sales_price_information.html new file mode 100644 index 000000000..115f07757 --- /dev/null +++ b/templates/webpages/oe/sales_price_information.html @@ -0,0 +1,113 @@ +[% USE HTML %] +[% USE LxERP %] +[% USE T8 %] +[%- IF !TABDIALOG %] + + +

[% 'Price information' | $T8 %]

+ +

+ +

+ +

+ + + + + + + + + + + + [%- UNLESS part_info.type == 'service' %] + + + + + [%- END %] +
[% 'Part Number' | $T8 %]:[% HTML.escape(part_info.partnumber) %]
[% 'Description' | $T8 %]:[% HTML.escape(part_info.description) %]
[% 'Stocked' | $T8 %]:[% HTML.escape(LxERP.format_amount_units(stocked, part_info.unit, part_info.unit)) %]
+

+ +[%- END %] + +

+ + [%- SET custom_texts = { + sales_order => { + head => LxERP.t8('Sales Orders'), + empty => LxERP.t8('This part has not been used in a sales order yet.'), + date => LxERP.t8('Order Date'), + number => LxERP.t8('Order Number'), + vc => LxERP.t8('Customer'), + }, + sales_quotation => { + head => LxERP.t8('Quotations'), + empty => LxERP.t8('This part has not been used in a quotation yet.'), + date => LxERP.t8('Quotation Date'), + number => LxERP.t8('Quotation Number'), + vc => LxERP.t8('Customer'), + }, + purchase_order => { + head => LxERP.t8('Purchase Orders'), + empty => LxERP.t8('This part has not been used in a purchase order yet.'), + date => LxERP.t8('Order Date'), + number => LxERP.t8('Order Number'), + vc => LxERP.t8('Vendor'), + }, + request_quotation => { + head => LxERP.t8('RFQs'), + empty => LxERP.t8('This part has not been used in a request quotation yet.'), + date => LxERP.t8('Quotation Date'), + number => LxERP.t8('Quotation Number'), + vc => LxERP.t8('Vendor'), + }, + } + %] + [%- FOREACH type = ['sales_order', 'sales_quotation', 'purchase_order', 'request_quotation'] %] + + + + + + [%- IF !PRICE_INFO.$type.size %] + + + + + [%- ELSE %] + + + + + + + + + + + + + [%- FOREACH row = PRICE_INFO.$type %] + + + + + + + + + + + [%- END %] + [%- END %] + [%- END %] +
[% custom_texts.$type.head %]
[% custom_texts.$type.empty %]
[% custom_texts.$type.date %][% custom_texts.$type.number %][% custom_texts.$type.vc %][% 'Delivered' | $T8 %][% 'Quantity' | $T8 %][% 'Unit price' | $T8 %][% 'Discount' | $T8 %][% 'Line total' | $T8 %]
[% HTML.escape(row.date) %][% HTML.escape(row.number) %][% HTML.escape(row.vc) %][% HTML.escape(LxERP.format_amount(row.ship)) %] [% HTML.escape(row.unit) %][% HTML.escape(LxERP.format_amount(row.qty)) %] [% HTML.escape(row.unit) %][% HTML.escape(LxERP.format_amount(row.sellprice, 2)) %][% HTML.escape(LxERP.format_amount(row.discount * 100)) %][% HTML.escape(LxERP.format_amount(row.linetotal, 2)) %]
+

+ +[%- IF !TABDIALOG %] + + +[%- END %] diff --git a/templates/webpages/price_information/report_bottom.html b/templates/webpages/price_information/report_bottom.html new file mode 100644 index 000000000..587fb06a3 --- /dev/null +++ b/templates/webpages/price_information/report_bottom.html @@ -0,0 +1 @@ +

[% PROCESS 'common/paginate.html' pages=SELF.pages, base_url=SELF.self_url %]

-- 2.20.1