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 if ($::form->{vc} eq 'customer') {
65 $::auth->assert('sales_edit_prices');
66 } elsif ($::form->{vc} eq 'vendor') {
67 $::auth->assert('purchase_edit_prices');
69 $::auth->assert('no_such_right');
77 sub _make_record_item {
81 sales_order => 'OrderItem',
82 purchase_order => 'OrderItem',
83 sales_quotation => 'OrderItem',
84 request_quotation => 'OrderItem',
85 invoice => 'InvoiceItem',
86 purchase_invoice => 'InvoiceItem',
87 credit_note => 'InvoiceItem',
88 purchase_delivery_order => 'DeliveryOrderItem',
89 sales_delivery_order => 'DeliveryOrderItem',
94 $class = 'SL::DB::' . $class;
96 my %translated_methods = (
97 'SL::DB::OrderItem' => {
99 orderitems_id => 'id',
101 'SL::DB::DeliveryOrderItem' => {
103 delivery_order_items_id => 'id',
105 'SL::DB::InvoiceItem' => {
111 eval "require $class";
113 my $obj = $::form->{"orderitems_id_$row"}
114 ? $class->meta->convention_manager->auto_manager_class_name->find_by(id => $::form->{"orderitems_id_$row"})
117 for my $key (grep { /_$row$/ } keys %$::form) {
119 $method =~ s/_$row$//;
120 $method = $translated_methods{$class}{$method} // $method;
121 my $value = $::form->{$key};
122 if ($obj->meta->column($method)) {
123 if ($obj->meta->column($method)->isa('Rose::DB::Object::Metadata::Column::Date')) {
124 $obj->${\"$method\_as_date"}($value);
125 } elsif ((ref $obj->meta->column($method)) =~ /^Rose::DB::Object::Metadata::Column::(?:Numeric|Float|DoublePrecsion)$/) {
126 $obj->${\"$method\_as_number"}($value);
127 } elsif ((ref $obj->meta->column($method)) =~ /^Rose::DB::Object::Metadata::Column::Boolean$/) {
128 $obj->$method(!!$value);
130 $obj->$method($value);
133 $obj->{__additional_form_attributes}{$method} = $value;
137 if ($::form->{"id_$row"}) {
138 $obj->part(SL::DB::Part->load_cached($::form->{"id_$row"}));
145 my ($with_items) = @_;
148 sales_order => 'Order',
149 purchase_order => 'Order',
150 sales_quotation => 'Order',
151 request_quotation => 'Order',
152 purchase_invoice => 'PurchaseInvoice',
153 purchase_delivery_order => 'DeliveryOrder',
154 sales_delivery_order => 'DeliveryOrder',
155 }->{$::form->{type}};
157 if ($::form->{type} =~ /invoice|credit_note/) {
158 $class = $::form->{vc} eq 'customer' ? 'Invoice'
159 : $::form->{vc} eq 'vendor' ? 'PurchaseInvoice'
160 : do { die 'unknown invoice type' };
163 return unless $class;
165 $class = 'SL::DB::' . $class;
167 eval "require $class";
169 my $obj = $::form->{id}
170 ? $class->meta->convention_manager->auto_manager_class_name->find_by(id => $::form->{id})
173 for my $method (keys %$::form) {
174 next unless $obj->can($method);
175 next unless $obj->meta->column($method);
177 if ($obj->meta->column($method)->isa('Rose::DB::Object::Metadata::Column::Date')) {
178 $obj->${\"$method\_as_date"}($::form->{$method});
179 } elsif ((ref $obj->meta->column($method)) =~ /^Rose::DB::Object::Metadata::Column::(?:Numeric|Float|DoublePrecsion)$/) {
180 $obj->${\"$method\_as\_number"}($::form->{$method});
181 } elsif ((ref $obj->meta->column($method)) =~ /^Rose::DB::Object::Metadata::Column::Boolean$/) {
182 $obj->$method(!!$::form->{$method});
184 $obj->$method($::form->{$method});
190 for my $i (1 .. $::form->{rowcount}) {
191 next unless $::form->{"id_$i"};
192 push @items, _make_record_item($i)
195 $obj->items(@items) if @items;
197 $obj->is_sales(!!$obj->customer_id) if $class eq 'SL::DB::DeliveryOrder';