1 package SL::Controller::PriceSource;
5 use parent qw(SL::Controller::Base);
7 use List::MoreUtils qw(any uniq apply);
8 use SL::Locale::String qw(t8);
11 use Rose::Object::MakeMethods::Generic
13 scalar => [ qw(record_item) ],
14 'scalar --get_set_init' => [ qw(record) ],
17 __PACKAGE__->run_before('check_auth');
23 sub action_price_popup {
26 my $record_item = _make_record_item($::form->{row});
28 if (($old_unit = $record_item->{__additional_form_attributes}{unit_old}) && $old_unit ne $record_item->unit) {
29 # reset unit changes. the way these interact on update breaks stuff
30 $record_item->unit_obj(SL::DB::Manager::Unit->find_by(name => $old_unit));
31 $self->js->val("select[name='unit_$::form->{row}']", $old_unit);
34 $self->render_price_dialog($record_item);
37 sub render_price_dialog {
38 my ($self, $record_item) = @_;
40 my $price_source = SL::PriceSource->new(record_item => $record_item, record => $self->record);
44 'kivi.io.price_chooser_dialog',
45 t8('Available Prices'),
46 $self->render('oe/price_sources_dialog', { output => 0 }, price_source => $price_source)
51 # $self->js->text('#dialog_flash_error_content', join ' ', @errors);
52 # $self->js->show('#dialog_flash_error');
64 $::auth->assert('edit_prices');
71 sub _make_record_item {
75 sales_order => 'OrderItem',
76 purchase_order => 'OrderItem',
77 sales_quotation => 'OrderItem',
78 request_quotation => 'OrderItem',
79 invoice => 'InvoiceItem',
80 purchase_invoice => 'InvoiceItem',
81 credit_note => 'InvoiceItem',
82 purchase_delivery_order => 'DeliveryOrderItem',
83 sales_delivery_order => 'DeliveryOrderItem',
88 $class = 'SL::DB::' . $class;
90 eval "require $class";
92 my $obj = $::form->{"orderitems_id_$row"}
93 ? $class->meta->convention_manager->auto_manager_class_name->find_by(id => $::form->{"orderitems_id_$row"})
96 for my $method (apply { s/_$row$// } grep { /_$row$/ } keys %$::form) {
97 if ($obj->meta->column($method)) {
98 if ($obj->meta->column($method)->isa('Rose::DB::Object::Metadata::Column::Date')) {
99 $obj->${\"$method\_as_date"}($::form->{"$method\_$row"});
100 } elsif ((ref $obj->meta->column($method)) =~ /^Rose::DB::Object::Metadata::Column::(?:Numeric|Float|DoublePrecsion)$/) {
101 $obj->${\"$method\_as_number"}($::form->{"$method\_$row"});
102 } elsif ((ref $obj->meta->column($method)) =~ /^Rose::DB::Object::Metadata::Column::Boolean$/) {
103 $obj->$method(!!$::form->{$method});
105 $obj->$method($::form->{"$method\_$row"});
108 $obj->{__additional_form_attributes}{$method} = $::form->{"$method\_$row"};
112 if ($::form->{"id_$row"}) {
113 $obj->part(SL::DB::Part->load_cached($::form->{"id_$row"}));
120 my ($with_items) = @_;
123 sales_order => 'Order',
124 purchase_order => 'Order',
125 sales_quotation => 'Order',
126 request_quotation => 'Order',
127 purchase_invoice => 'PurchaseInvoice',
128 purchase_delivery_order => 'DeliveryOrder',
129 sales_delivery_order => 'DeliveryOrder',
130 }->{$::form->{type}};
132 if ($::form->{type} =~ /invoice|credit_note/) {
133 $class = $::form->{vc} eq 'customer' ? 'Invoice'
134 : $::form->{vc} eq 'vendor' ? 'PurchaseInvoice'
135 : do { die 'unknown invoice type' };
138 return unless $class;
140 $class = 'SL::DB::' . $class;
142 eval "require $class";
144 my $obj = $::form->{id}
145 ? $class->meta->convention_manager->auto_manager_class_name->find_by(id => $::form->{id})
148 for my $method (keys %$::form) {
149 next unless $obj->can($method);
150 next unless $obj->meta->column($method);
152 if ($obj->meta->column($method)->isa('Rose::DB::Object::Metadata::Column::Date')) {
153 $obj->${\"$method\_as_date"}($::form->{$method});
154 } elsif ((ref $obj->meta->column($method)) =~ /^Rose::DB::Object::Metadata::Column::(?:Numeric|Float|DoublePrecsion)$/) {
155 $obj->${\"$method\_as\_number"}($::form->{$method});
156 } elsif ((ref $obj->meta->column($method)) =~ /^Rose::DB::Object::Metadata::Column::Boolean$/) {
157 $obj->$method(!!$::form->{$method});
159 $obj->$method($::form->{$method});
165 for my $i (1 .. $::form->{rowcount}) {
166 next unless $::form->{"id_$i"};
167 push @items, _make_record_item($i)
170 $obj->items(@items) if @items;
172 $obj->is_sales(!!$obj->customer_id) if $class eq 'SL::DB::DeliveryOrder';