+
+# TODO: both of these are makeshift so that price sources can operate on rdbo objects. if
+# this ever gets rewritten in controller style, throw this out
+sub _make_record_item {
+ my ($row) = @_;
+
+ my $class = {
+ sales_order => 'OrderItem',
+ purchase_order => 'OrderItem',
+ sales_quotation => 'OrderItem',
+ request_quotation => 'OrderItem',
+ invoice => 'InvoiceItem',
+ credit_note => 'InvoiceItem',
+ purchase_invoice => 'InvoiceItem',
+ purchase_delivery_order => 'DeliveryOrderItem',
+ sales_delivery_order => 'DeliveryOrderItem',
+ }->{$::form->{type}};
+
+ return unless $class;
+
+ $class = 'SL::DB::' . $class;
+
+ eval "require $class";
+
+ my $obj = $::form->{"orderitems_id_$row"}
+ ? $class->meta->convention_manager->auto_manager_class_name->find_by(id => $::form->{"orderitems_id_$row"})
+ : $class->new;
+
+ for my $method (apply { s/_$row$// } grep { /_$row$/ } keys %$::form) {
+ next unless $obj->meta->column($method);
+ if ($obj->meta->column($method)->isa('Rose::DB::Object::Metadata::Column::Date')) {
+ $obj->${\"$method\_as_date"}($::form->{"$method\_$row"});
+ } elsif ((ref $obj->meta->column($method)) =~ /^Rose::DB::Object::Metadata::Column::(?:Numeric|Float|DoublePrecsion)$/) {
+ $obj->${\"$method\_as_number"}($::form->{"$method\_$row"});
+ } elsif ((ref $obj->meta->column($method)) =~ /^Rose::DB::Object::Metadata::Column::Boolean$/) {
+ $obj->$method(!!$::form->{$method});
+ } else {
+ $obj->$method($::form->{"$method\_$row"});
+ }
+ }
+
+ if ($::form->{"id_$row"}) {
+ $obj->part(SL::DB::Part->load_cached($::form->{"id_$row"}));
+ }
+
+ return $obj;
+}
+
+sub _make_record {
+ my $class = {
+ sales_order => 'Order',
+ purchase_order => 'Order',
+ sales_quotation => 'Order',
+ request_quotation => 'Order',
+ purchase_delivery_order => 'DeliveryOrder',
+ sales_delivery_order => 'DeliveryOrder',
+ }->{$::form->{type}};
+
+ if ($::form->{type} =~ /invoice|credit_note/) {
+ $class = $::form->{vc} eq 'customer' ? 'Invoice'
+ : $::form->{vc} eq 'vendor' ? 'PurchaseInvoice'
+ : do { die 'unknown invoice type' };
+ }
+
+ return unless $class;
+
+ $class = 'SL::DB::' . $class;
+
+ eval "require $class";
+
+ my $obj = $::form->{id}
+ ? $class->meta->convention_manager->auto_manager_class_name->find_by(id => $::form->{id})
+ : $class->new;
+
+ for my $method (keys %$::form) {
+ next unless $obj->can($method);
+ next unless $obj->meta->column($method);
+
+ if ($obj->meta->column($method)->isa('Rose::DB::Object::Metadata::Column::Date')) {
+ $obj->${\"$method\_as_date"}($::form->{$method});
+ } elsif ((ref $obj->meta->column($method)) =~ /^Rose::DB::Object::Metadata::Column::(?:Numeric|Float|DoublePrecsion)$/) {
+ $obj->${\"$method\_as_number"}($::form->{$method});
+ } elsif ((ref $obj->meta->column($method)) =~ /^Rose::DB::Object::Metadata::Column::Boolean$/) {
+ $obj->$method(!!$::form->{$method});
+ } else {
+ $obj->$method($::form->{$method});
+ }
+ }
+
+ my @items;
+ for my $i (1 .. $::form->{rowcount}) {
+ next unless $::form->{"id_$i"};
+ push @items, _make_record_item($i);
+ }
+
+ $obj->items(@items) if @items;
+ $obj->is_sales(!!$obj->customer_id) if $class eq 'SL::DB::DeliveryOrder';
+
+ return $obj;
+}