-use Test::More tests => 7;
+use Test::More tests => 18;
use strict;
use Support::TestSetup;
use Test::Exception;
use DateTime;
+use Rose::DB::Object::Helpers qw(forget_related);
use SL::DB::BackgroundJob;
clear_up();
+########################################
+# two time recordings, one order linked with project_id
+########################################
my $part = new_service(partnumber => 'Serv1', unit => 'Std')->save;
my $project = create_project(projectnumber => 'p1', description => 'Project 1');
my $customer = new_customer()->save;
my @time_recordings;
push @time_recordings, new_time_recording(
start_time => DateTime->new(year => 2021, month => 4, day => 19, hour => 10, minute => 5),
- end_time => DateTime->new(year => 2021, month => 4, day => 19, hour => 11, minute => 10),
+ end_time => DateTime->new(year => 2021, month => 4, day => 19, hour => 11, minute => 5),
customer => $customer,
project => $project,
part => $part,
)->save;
push @time_recordings, new_time_recording(
start_time => DateTime->new(year => 2021, month => 4, day => 19, hour => 12, minute => 5),
- end_time => DateTime->new(year => 2021, month => 4, day => 19, hour => 14, minute => 10),
+ end_time => DateTime->new(year => 2021, month => 4, day => 19, hour => 14, minute => 5),
customer => $customer,
project => $project,
part => $part,
)->save;
-# two time recordings, one order linked with project_id
my %data = (
link_project => 1,
project_id => $project->id,
my $linked_items = $sales_order->items->[0]->linked_records(to => 'DeliveryOrderItem');
is(scalar @$linked_items, 1, 'one delivery order item linked to order item');
-is($linked_items->[0]->qty*1, 3.16, 'qty in delivery order');
+is($linked_items->[0]->qty*1, 3, 'qty in delivery order');
+is($linked_items->[0]->base_qty*1, 3, 'base_qty in delivery order');
+
+# reload order and orderitems to get changes to deliverd and ship
+Rose::DB::Object::Helpers::forget_related($sales_order, 'orderitems');
+$sales_order->load;
+
+ok($sales_order->delivered, 'related order is delivered');
+is($sales_order->items->[0]->ship*1, 3, 'ship in related order');
+
+clear_up();
+
+
+########################################
+# two time recordings, one order linked with project_id
+# unit in order is 'min', but part is 'Std'
+########################################
+$part = new_service(partnumber => 'Serv1', unit => 'Std')->save;
+$project = create_project(projectnumber => 'p1', description => 'Project 1');
+$customer = new_customer()->save;
+
+$sales_order = create_sales_order(
+ save => 1,
+ customer => $customer,
+ globalproject => $project,
+ taxincluded => 0,
+ orderitems => [ create_order_item(part => $part, qty => 180, unit => 'min', sellprice => 70), ]
+);
+
+@time_recordings = ();
+push @time_recordings, new_time_recording(
+ start_time => DateTime->new(year => 2021, month => 4, day => 19, hour => 10, minute => 10),
+ end_time => DateTime->new(year => 2021, month => 4, day => 19, hour => 11, minute => 10),
+ customer => $customer,
+ project => $project,
+ part => $part,
+)->save;
+push @time_recordings, new_time_recording(
+ start_time => DateTime->new(year => 2021, month => 4, day => 19, hour => 12, minute => 10),
+ end_time => DateTime->new(year => 2021, month => 4, day => 19, hour => 14, minute => 10),
+ customer => $customer,
+ project => $project,
+ part => $part,
+)->save;
+
+# two time recordings, one order linked with project_id
+%data = (
+ link_project => 1,
+ project_id => $project->id,
+ from_date => '01.04.2021',
+ to_date => '30.04.2021',
+);
+$db_obj = SL::DB::BackgroundJob->new();
+$db_obj->set_data(%data);
+$job = SL::BackgroundJob::ConvertTimeRecordings->new;
+$ret = $job->run($db_obj);
+
+$linked_dos = $sales_order->linked_records(to => 'DeliveryOrder');
+$linked_items = $sales_order->items->[0]->linked_records(to => 'DeliveryOrderItem');
+is($linked_items->[0]->qty*1, 3, 'different units: qty in delivery order');
+is($linked_items->[0]->base_qty*1, 3, 'different units: base_qty in delivery order');
+
+# reload order and orderitems to get changes to deliverd and ship
+Rose::DB::Object::Helpers::forget_related($sales_order, 'orderitems');
+$sales_order->load;
+
+ok($sales_order->delivered, 'different units: related order is delivered');
+is($sales_order->items->[0]->ship*1, 180, 'different units: ship in related order');
+
+clear_up();
+
+
+########################################
+# two time recordings, one order linked with project_id
+# unit in order is 'Std', but part is 'min'
+########################################
+$part = new_service(partnumber => 'Serv1', unit => 'min')->save;
+$project = create_project(projectnumber => 'p1', description => 'Project 1');
+$customer = new_customer()->save;
+
+$sales_order = create_sales_order(
+ save => 1,
+ customer => $customer,
+ globalproject => $project,
+ taxincluded => 0,
+ orderitems => [ create_order_item(part => $part, qty => 2, unit => 'Std', sellprice => 70), ]
+);
+
+@time_recordings = ();
+push @time_recordings, new_time_recording(
+ start_time => DateTime->new(year => 2021, month => 4, day => 19, hour => 10, minute => 10),
+ end_time => DateTime->new(year => 2021, month => 4, day => 19, hour => 11, minute => 10),
+ customer => $customer,
+ project => $project,
+ part => $part,
+)->save;
+push @time_recordings, new_time_recording(
+ start_time => DateTime->new(year => 2021, month => 4, day => 19, hour => 12, minute => 10),
+ end_time => DateTime->new(year => 2021, month => 4, day => 19, hour => 13, minute => 10),
+ customer => $customer,
+ project => $project,
+ part => $part,
+)->save;
+
+# two time recordings, one order linked with project_id
+%data = (
+ link_project => 1,
+ project_id => $project->id,
+ from_date => '01.04.2021',
+ to_date => '30.04.2021',
+);
+$db_obj = SL::DB::BackgroundJob->new();
+$db_obj->set_data(%data);
+$job = SL::BackgroundJob::ConvertTimeRecordings->new;
+$ret = $job->run($db_obj);
+
+$linked_dos = $sales_order->linked_records(to => 'DeliveryOrder');
+$linked_items = $sales_order->items->[0]->linked_records(to => 'DeliveryOrderItem');
+is($linked_items->[0]->qty*1, 2, 'different units 2: qty in delivery order');
+is($linked_items->[0]->base_qty*1, 120, 'different units 2: base_qty in delivery order');
+
+# reload order and orderitems to get changes to deliverd and ship
+Rose::DB::Object::Helpers::forget_related($sales_order, 'orderitems');
+$sales_order->load;
+
+ok($sales_order->delivered, 'different units 2: related order is delivered');
+is($sales_order->items->[0]->ship*1, 2, 'different units 2: ship in related order');
clear_up();
+
+########################################
+
$::locale = $old_locale;
1;