1 package SL::Controller::PriceSource;
5 use parent qw(SL::Controller::Base);
7 use List::MoreUtils qw(any uniq apply);
9 use SL::Locale::String qw(t8);
12 use Rose::Object::MakeMethods::Generic
14 scalar => [ qw(record_item) ],
15 'scalar --get_set_init' => [ qw(js record) ],
18 __PACKAGE__->run_before('check_auth');
24 sub action_price_popup {
27 my $record_item = _make_record_item($::form->{row});
29 $self->render_price_dialog($record_item);
32 sub render_price_dialog {
33 my ($self, $record_item) = @_;
35 my $price_source = SL::PriceSource->new(record_item => $record_item, record => $self->record);
39 'kivi.io.price_chooser_dialog',
40 t8('Available Prices'),
41 $self->render('oe/price_sources_dialog', { output => 0 }, price_source => $price_source)
46 # $self->js->text('#dialog_flash_error_content', join ' ', @errors);
47 # $self->js->show('#dialog_flash_error');
50 $self->js->render($self);
59 $::auth->assert('edit_prices');
70 sub _make_record_item {
74 sales_order => 'OrderItem',
75 purchase_order => 'OrderItem',
76 sales_quotation => 'OrderItem',
77 request_quotation => 'OrderItem',
78 invoice => 'InvoiceItem',
79 purchase_invoice => 'InvoiceItem',
80 credit_note => 'InvoiceItem',
81 purchase_delivery_order => 'DeliveryOrderItem',
82 sales_delivery_order => 'DeliveryOrderItem',
87 $class = 'SL::DB::' . $class;
89 eval "require $class";
91 my $obj = $::form->{"orderitems_id_$row"}
92 ? $class->meta->convention_manager->auto_manager_class_name->find_by(id => $::form->{"orderitems_id_$row"})
95 for my $method (apply { s/_$row$// } grep { /_$row$/ } keys %$::form) {
96 next unless $obj->meta->column($method);
97 if ($obj->meta->column($method)->isa('Rose::DB::Object::Metadata::Column::Date')) {
98 $obj->${\"$method\_as_date"}($::form->{"$method\_$row"});
99 } elsif ((ref $obj->meta->column($method)) =~ /^Rose::DB::Object::Metadata::Column::(?:Numeric|Float|DoublePrecsion)$/) {
100 $obj->${\"$method\_as_number"}($::form->{"$method\_$row"});
101 } elsif ((ref $obj->meta->column($method)) =~ /^Rose::DB::Object::Metadata::Column::Boolean$/) {
102 $obj->$method(!!$::form->{$method});
104 $obj->$method($::form->{"$method\_$row"});
108 if ($::form->{"id_$row"}) {
109 $obj->part(SL::DB::Part->load_cached($::form->{"id_$row"}));
116 my ($with_items) = @_;
119 sales_order => 'Order',
120 purchase_order => 'Order',
121 sales_quotation => 'Order',
122 request_quotation => 'Order',
123 purchase_invoice => 'PurchaseInvoice',
124 purchase_delivery_order => 'DeliveryOrder',
125 sales_delivery_order => 'DeliveryOrder',
126 }->{$::form->{type}};
128 if ($::form->{type} =~ /invoice|credit_note/) {
129 $class = $::form->{vc} eq 'customer' ? 'Invoice'
130 : $::form->{vc} eq 'vendor' ? 'PurchaseInvoice'
131 : do { die 'unknown invoice type' };
134 return unless $class;
136 $class = 'SL::DB::' . $class;
138 eval "require $class";
140 my $obj = $::form->{id}
141 ? $class->meta->convention_manager->auto_manager_class_name->find_by(id => $::form->{id})
144 for my $method (keys %$::form) {
145 next unless $obj->can($method);
146 next unless $obj->meta->column($method);
148 if ($obj->meta->column($method)->isa('Rose::DB::Object::Metadata::Column::Date')) {
149 $obj->${\"$method\_as_date"}($::form->{$method});
150 } elsif ((ref $obj->meta->column($method)) =~ /^Rose::DB::Object::Metadata::Column::(?:Numeric|Float|DoublePrecsion)$/) {
151 $obj->${\"$method\_as\_number"}($::form->{$method});
152 } elsif ((ref $obj->meta->column($method)) =~ /^Rose::DB::Object::Metadata::Column::Boolean$/) {
153 $obj->$method(!!$::form->{$method});
155 $obj->$method($::form->{$method});
161 for my $i (1 .. $::form->{rowcount}) {
162 next unless $::form->{"id_$i"};
163 push @items, _make_record_item($i)
166 $obj->items(@items) if @items;
168 $obj->is_sales(!!$obj->customer_id) if $class eq 'SL::DB::DeliveryOrder';