+sub _get_invoices_for_advance_payment {
+ my ($self, $id, $id_is_from_order) = @_;
+
+ return [] if !$id;
+
+ # Search all related invoices for advance payment.
+ # Case 1:
+ # (order) -> invoice for adv. payment 1 -> invoice for adv. payment 2 -> invoice for adv. payment 3 -> final invoice
+ #
+ # Case 2:
+ # order -> invoice for adv. payment 1
+ # | |`-> invoice for adv. payment 2
+ # | `--> invoice for adv. payment 3
+ # `----> final invoice
+ #
+ # The id is currently that from the last invoice for adv. payment (3 in this example),
+ # that from the final invoice or that from the order.
+
+ my $invoice_obj;
+ my $order_obj;
+ my $links;
+
+ if (!$id_is_from_order) {
+ $invoice_obj = SL::DB::Invoice->load_cached($id*1);
+ $links = $invoice_obj->linked_records(direction => 'from', from => ['Order']);
+ $order_obj = $links->[0];
+ } else {
+ $order_obj = SL::DB::Order->load_cached($id*1);
+ }
+
+ if ($order_obj) {
+ $links = $order_obj ->linked_records(direction => 'to', to => ['Invoice']);
+ } else {
+ $links = $invoice_obj->linked_records(direction => 'from', from => ['Invoice'], recursive => 1);
+ }
+
+ my @related_invoices = grep {'SL::DB::Invoice' eq ref $_ && "invoice_for_advance_payment" eq $_->type} @$links;
+
+ push @related_invoices, $invoice_obj if !$order_obj && "invoice_for_advance_payment" eq $invoice_obj->type;
+
+ return \@related_invoices;
+}
+
+
+sub transfer_out {
+ $::lxdebug->enter_sub;
+
+ my ($self, $form, $dbh) = @_;
+
+ my (@errors, @transfers);
+
+ # do nothing, if transfer default is not requested at all
+ if (!$::instance_conf->get_transfer_default) {
+ $::lxdebug->leave_sub;
+ return \@errors;
+ }
+
+ require SL::WH;
+
+ foreach my $i (1 .. $form->{rowcount}) {
+ next if !$form->{"id_$i"};
+
+ my ($err, $qty, $wh_id, $bin_id, $chargenumber);
+
+ if ($::instance_conf->get_sales_serial_eq_charge && $form->{"serialnumber_$i"}) {
+ my @serials = split(" ", $form->{"serialnumber_$i"});
+ if (scalar @serials != $form->{"qty_$i"}) {
+ push @errors, $::locale->text("Cannot transfer #1 qty with #2 serial number(s)", $form->{"qty_$i"}, scalar @serials);
+ last;
+ }
+ foreach my $serial (@serials) {
+ ($qty, $wh_id, $bin_id, $chargenumber) = WH->get_wh_and_bin_for_charge(chargenumber => $serial);
+ if (!$qty) {
+ push @errors, $::locale->text("Not enough in stock for the serial number #1", $serial);
+ last;
+ }
+ push @transfers, {
+ 'parts_id' => $form->{"id_$i"},
+ 'qty' => 1,
+ 'unit' => $form->{"unit_$i"},
+ 'transfer_type' => 'shipped',
+ 'src_warehouse_id' => $wh_id,
+ 'src_bin_id' => $bin_id,
+ 'chargenumber' => $chargenumber,
+ 'project_id' => $form->{"project_id_$i"},
+ 'invoice_id' => $form->{"invoice_id_$i"},
+ 'comment' => $::locale->text("Default transfer invoice with charge number"),
+ };
+ }
+ $err = []; # error handling uses @errors direct
+ } else {
+ ($err, $wh_id, $bin_id) = _determine_wh_and_bin($dbh, $::instance_conf,
+ $form->{"id_$i"},
+ $form->{"qty_$i"},
+ $form->{"unit_$i"});
+ if (!@{ $err } && $wh_id && $bin_id) {
+ push @transfers, {
+ 'parts_id' => $form->{"id_$i"},
+ 'qty' => $form->{"qty_$i"},
+ 'unit' => $form->{"unit_$i"},
+ 'transfer_type' => 'shipped',
+ 'src_warehouse_id' => $wh_id,
+ 'src_bin_id' => $bin_id,
+ 'project_id' => $form->{"project_id_$i"},
+ 'invoice_id' => $form->{"invoice_id_$i"},
+ 'comment' => $::locale->text("Default transfer invoice"),
+ };
+ }
+ }
+ push @errors, @{ $err };
+ } # end form rowcount
+
+ if (!@errors) {
+ WH->transfer(@transfers);
+ }
+
+ $::lxdebug->leave_sub;
+ return \@errors;
+}
+
+sub _determine_wh_and_bin {
+ $::lxdebug->enter_sub(2);
+
+ my ($dbh, $conf, $part_id, $qty, $unit) = @_;
+ my @errors;
+
+ my $part = SL::DB::Part->new(id => $part_id)->load;
+
+ # ignore service if they are not configured to be transfered
+ if ($part->is_service && !$conf->get_transfer_default_services) {
+ $::lxdebug->leave_sub(2);
+ return (\@errors);
+ }
+
+ # test negative qty
+ if ($qty < 0) {
+ push @errors, $::locale->text("Cannot transfer negative quantities.");
+ return (\@errors);
+ }
+
+ # get/test default bin
+ my ($default_wh_id, $default_bin_id);
+ if ($conf->get_transfer_default_use_master_default_bin) {
+ $default_wh_id = $conf->get_warehouse_id if $conf->get_warehouse_id;
+ $default_bin_id = $conf->get_bin_id if $conf->get_bin_id;
+ }
+ my $wh_id = $part->warehouse_id || $default_wh_id;
+ my $bin_id = $part->bin_id || $default_bin_id;
+
+ # check qty and bin
+ if ($bin_id) {
+ my ($max_qty, $error) = WH->get_max_qty_parts_bin(dbh => $dbh,
+ parts_id => $part->id,
+ bin_id => $bin_id);
+ if ($error == 1) {
+ push @errors, $::locale->text('Part "#1" has chargenumber or best before date set. So it cannot be transfered automatically.',
+ $part->description);
+ }
+ my $form_unit_obj = SL::DB::Unit->new(name => $unit)->load;
+ my $part_unit_qty = $form_unit_obj->convert_to($qty, $part->unit_obj);
+ my $diff_qty = $max_qty - $part_unit_qty;
+ if (!@errors && $diff_qty < 0) {
+ push @errors, $::locale->text('For part "#1" there are missing #2 #3 in the default warehouse/bin "#4/#5".',
+ $part->description,
+ $::form->format_amount(\%::myconfig, -1*$diff_qty),
+ $part->unit_obj->name,
+ SL::DB::Warehouse->new(id => $wh_id)->load->description,
+ SL::DB::Bin->new( id => $bin_id)->load->description);
+ }
+ } else {
+ push @errors, $::locale->text('For part "#1" there is no default warehouse and bin defined.',
+ $part->description);
+ }
+
+ # transfer to special "ignore onhand" bin if requested and default bin does not work
+ if (@errors && $conf->get_transfer_default_ignore_onhand && $conf->get_bin_id_ignore_onhand) {
+ $wh_id = $conf->get_warehouse_id_ignore_onhand;
+ $bin_id = $conf->get_bin_id_ignore_onhand;
+ if ($wh_id && $bin_id) {
+ @errors = ();
+ } else {
+ push @errors, $::locale->text('For part "#1" there is no default warehouse and bin for ignoring onhand defined.',
+ $part->description);
+ }
+ }
+
+ $::lxdebug->leave_sub(2);
+ return (\@errors, $wh_id, $bin_id);
+}
+
+sub _delete_transfers {
+ $::lxdebug->enter_sub;
+
+ my ($dbh, $form, $id) = @_;
+
+ my $query = qq|DELETE FROM inventory WHERE invoice_id
+ IN (SELECT id FROM invoice WHERE trans_id = ?)|;
+
+ do_query($form, $dbh, $query, $id);
+
+ $::lxdebug->leave_sub;