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 my %translated_methods = (
91 'SL::DB::OrderItem' => {
93 orderitems_id => 'id',
95 'SL::DB::DeliveryOrderItem' => {
97 delivery_order_items_id => 'id',
99 'SL::DB::InvoiceItem' => {
105 eval "require $class";
107 my $obj = $::form->{"orderitems_id_$row"}
108 ? $class->meta->convention_manager->auto_manager_class_name->find_by(id => $::form->{"orderitems_id_$row"})
111 for my $key (grep { /_$row$/ } keys %$::form) {
113 $method =~ s/_$row$//;
114 $method = $translated_methods{$class}{$method} // $method;
115 my $value = $::form->{$key};
116 if ($obj->meta->column($method)) {
117 if ($obj->meta->column($method)->isa('Rose::DB::Object::Metadata::Column::Date')) {
118 $obj->${\"$method\_as_date"}($value);
119 } elsif ((ref $obj->meta->column($method)) =~ /^Rose::DB::Object::Metadata::Column::(?:Numeric|Float|DoublePrecsion)$/) {
120 $obj->${\"$method\_as_number"}($value);
121 } elsif ((ref $obj->meta->column($method)) =~ /^Rose::DB::Object::Metadata::Column::Boolean$/) {
122 $obj->$method(!!$value);
124 $obj->$method($value);
127 $obj->{__additional_form_attributes}{$method} = $value;
131 if ($::form->{"id_$row"}) {
132 $obj->part(SL::DB::Part->load_cached($::form->{"id_$row"}));
139 my ($with_items) = @_;
142 sales_order => 'Order',
143 purchase_order => 'Order',
144 sales_quotation => 'Order',
145 request_quotation => 'Order',
146 purchase_invoice => 'PurchaseInvoice',
147 purchase_delivery_order => 'DeliveryOrder',
148 sales_delivery_order => 'DeliveryOrder',
149 }->{$::form->{type}};
151 if ($::form->{type} =~ /invoice|credit_note/) {
152 $class = $::form->{vc} eq 'customer' ? 'Invoice'
153 : $::form->{vc} eq 'vendor' ? 'PurchaseInvoice'
154 : do { die 'unknown invoice type' };
157 return unless $class;
159 $class = 'SL::DB::' . $class;
161 eval "require $class";
163 my $obj = $::form->{id}
164 ? $class->meta->convention_manager->auto_manager_class_name->find_by(id => $::form->{id})
167 for my $method (keys %$::form) {
168 next unless $obj->can($method);
169 next unless $obj->meta->column($method);
171 if ($obj->meta->column($method)->isa('Rose::DB::Object::Metadata::Column::Date')) {
172 $obj->${\"$method\_as_date"}($::form->{$method});
173 } elsif ((ref $obj->meta->column($method)) =~ /^Rose::DB::Object::Metadata::Column::(?:Numeric|Float|DoublePrecsion)$/) {
174 $obj->${\"$method\_as\_number"}($::form->{$method});
175 } elsif ((ref $obj->meta->column($method)) =~ /^Rose::DB::Object::Metadata::Column::Boolean$/) {
176 $obj->$method(!!$::form->{$method});
178 $obj->$method($::form->{$method});
184 for my $i (1 .. $::form->{rowcount}) {
185 next unless $::form->{"id_$i"};
186 push @items, _make_record_item($i)
189 $obj->items(@items) if @items;
191 $obj->is_sales(!!$obj->customer_id) if $class eq 'SL::DB::DeliveryOrder';