Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision 415437ab

Von Werner Hahn vor mehr als 8 Jahren hinzugefügt

  • ID 415437ab32d812e4dcc608180caa7345f07065c8
  • Vorgänger 81868e7f
  • Nachfolger 7f94d4ee

Backgroundjob Shoporder->Auftrag->Lieferschein (auslagern) mit debugmeldungen
Positionen werden beim Shopimport schon nach Artikelnummern geordnet - neue Spalte Position.

Conflicts:
SL/Controller/ShopOrder.pm
SL/ShopConnector/Shopware.pm

Unterschiede anzeigen:

SL/BackgroundJob/ShopOrder.pm
1
package SL::BackgroundJob::ShopOrder;
2

  
3
use strict;
4
use warnings;
5

  
6
use parent qw(SL::BackgroundJob::Base);
7

  
8
#
9
# Workflow Dreschflegel Shoporder import -> wo geht automatisch nach Order(Auftrag) und DeliveryOrder (Lieferschein) mit auslagern transferieren
10
#
11

  
12
use SL::DB::ShopOrder;
13
use SL::DB::Order;
14
use SL::DB::DeliveryOrder;
15
use SL::DB::Inventory;
16

  
17
use constant WAITING_FOR_EXECUTION        => 0;
18
use constant CONVERTING_TO_DELIVERY_ORDER => 1;
19
use constant DONE                         => 5;
20

  
21
# Data format:
22
# my $data                  = {
23
#     shop_order_record_ids       => [ 603, 604, 605],
24
#     num_order_created           => 0,
25
#     num_delivery_order_created  => 0,
26
#     conversation_errors         => [ { id => 603 , item => 2, message => "Out of stock"}, ],
27
# };
28
#
29

  
30
sub create_order {
31
  my ( $self ) = @_;
32
  $::lxdebug->dump(0, 'WH: Taskmanager: ', \$self);
33
  my $job_obj = $self->{job_obj};
34
  my $db      = $job_obj->db;
35
  foreach my $shop_order_id (@{ $job_obj->data_as_hash->{shop_order_record_ids} }) {
36
    #my $shop_order = SL::DB::Manager::ShopOrder->find_by( id => $shop_order_id );
37
    my $shop_order = SL::DB::ShopOrder->new(id => $shop_order_id)->load;
38
    die "can't find shoporder with id $shop_order_id" unless $shop_order;
39
    my $customer = SL::DB::Manager::Customer->find_by(id => $::form->{customer});
40
    die "Can't find customer" unless $customer;
41
    my $employee = SL::DB::Manager::Employee->current;
42
    my $items = SL::DB::Manager->ShopOrderItem->get_all( where => [shop_order_id => $shop_order_id] );
43
    $::lxdebug->dump(0, 'WH: CREATE:I ', \$shop_order);
44
    $::lxdebug->dump(0, 'WH: CREATE:II ', \$items);
45

  
46
  }
47
}
48

  
49
sub create_delivery_order {
50
  my ( $self ) = @_;
51
}
52

  
53
sub run {
54
  my ($self, $job_obj) = @_;
55

  
56
  $self->{job_obj}         = $job_obj;
57
  $self->create_order;
58

  
59
  $job_obj->set_data(status => DONE())->save;
60

  
61
  return 1;
62
}
63
1;
SL/BackgroundJob/ShopOrderMassTransfer.pm
1
package SL::BackgroundJob::ShopOrderMassTransfer;
2

  
3
use strict;
4
use warnings;
5

  
6
use parent qw(SL::BackgroundJob::Base);
7

  
8
#
9
# Workflow Dreschflegel Shoporder import -> wo geht automatisch nach Order(Auftrag) und DeliveryOrder (Lieferschein) mit auslagern transferieren
10
#
11

  
12
use SL::DB::ShopOrder;
13
use SL::DB::ShopOrderItem;
14
use SL::DB::Order;
15
use SL::DB::DeliveryOrder;
16
use SL::DB::Inventory;
17
use Sort::Naturally ();
18

  
19
use constant WAITING_FOR_EXECUTION        => 0;
20
use constant CONVERTING_TO_DELIVERY_ORDER => 1;
21
use constant DONE                         => 5;
22

  
23
# Data format:
24
# my $data                  = {
25
#     shop_order_record_ids       => [ 603, 604, 605],
26
#     num_order_created           => 0,
27
#     num_delivery_order_created  => 0,
28
#     conversation_errors         => [ { id => 603 , item => 2, message => "Out of stock"}, ],
29
# };
30
#
31

  
32
sub create_order {
33
  my ( $self ) = @_;
34
  $::lxdebug->dump(0, 'WH: Taskmanager: ', \$self);
35
  my $job_obj = $self->{job_obj};
36
  my $db      = $job_obj->db;
37
  foreach my $shop_order_id (@{ $job_obj->data_as_hash->{shop_order_record_ids} }) {
38
    #my $shop_order = SL::DB::Manager::ShopOrder->find_by( id => $shop_order_id );
39
    my $shop_order = SL::DB::ShopOrder->new(id => $shop_order_id)->load;
40
    die "can't find shoporder with id $shop_order_id" unless $shop_order;
41
    $::lxdebug->dump(0, 'WH: CREATE:I ', \$shop_order);
42
    #TODO Kundenabfrage so ändern, dass es nicht abricht
43
    my $customer = SL::DB::Manager::Customer->find_by(id => $shop_order->{kivi_customer_id});
44
    die "Can't find customer" unless $customer;
45
    my $employee = SL::DB::Manager::Employee->current;
46
    my $items = SL::DB::Manager::ShopOrderItem->get_all( where => [shop_order_id => $shop_order_id],
47
                                                          sort_by => 'partnumber::int' );
48
    $::lxdebug->dump(0, 'WH: CREATE:I ', \$shop_order);
49
    $::lxdebug->dump(0, 'WH: CREATE:II ', \$items);
50

  
51
    # check inventory onhand > 0
52
    my $onhand = 0;
53
    foreach my $item (@$items) {
54
      my $qty = SL::DB::Manager::Part->find_by(partnumber => $item->{partnumber})->onhand; # > 0 ? $onhand = 1 : 0;
55
      $qty >= $item->{quantity} ? $onhand = 1 : 0;
56
      $main::lxdebug->message(0, "WH: STOCK: $qty -- $onhand");
57
      last if $onhand == 0;
58
    }
59
    $main::lxdebug->message(0, "WH:ONHAND: $onhand ");
60
    if ($onhand == 1) {
61
      $shop_order->{shop_order_items} = $items;
62
      $main::lxdebug->dump(0, 'WH: TRANSFER SHOPORDER', \$shop_order);
63

  
64
      my $order = $shop_order->convert_to_sales_order(customer => $customer, employee => $employee);
65
      $order->save;
66
      $shop_order->transferred(1);
67
      $shop_order->transfer_date(DateTime->now_local);
68
      $shop_order->oe_transid($order->id);
69
      $shop_order->save;
70
      $shop_order->link_to_record($order);
71

  
72
      my $delivery_order = $order->convert_to_delivery_order(customer => $customer, employee => $employee);
73
      $delivery_order->save;
74
      $order->link_to_record($delivery_order);
75
      my $delivery_order_items = $delivery_order->{orderitems};
76
      # Lagerentnahme
77
      # entsprechende defaults holen, falls standardlagerplatz verwendet werden soll
78
      $main::lxdebug->dump(0, 'WH: LAGER: ', \$delivery_order_items);
79
      my $test = $::instance_conf->get_transfer_default_use_master_default_bin;
80
      $main::lxdebug->message(0, "WH: TEST $test ");
81
      $main::lxdebug->dump(0, 'WH: KONF ',$::instance_conf);
82
      $main::lxdebug->message(0, "WH:NACH ");
83
      require SL::DB::Inventory;
84
      my $rose_db = SL::DB::Inventory->new->db;
85
      my $dbh = $db->dbh;
86
      my $default_warehouse_id;
87
      my $default_bin_id;
88
      my @parts_ids;
89
      my @transfers;
90
      my @errors;
91
      my $qty;
92
      my @stock_out;
93
      require SL::WH;
94
      require SL::IS;
95
      require SL::DB::DeliveryOrderItemsStock;
96
      foreach my $item (@{$delivery_order_items}) {
97
        my ($err, $wh_id, $bin_id) = IS->_determine_wh_and_bin($dbh, $::instance_conf,
98
                                                           $item->{parts_id},
99
                                                           $item->{qty},
100
                                                           $item->{unit}
101
                                                           );
102
        if (!@{ $err } && $wh_id && $bin_id) {
103
          push @transfers, {
104
            'bestbefore' => undef,
105
            'chargenumber' => '',
106
            'shippingdate' => 'current_date',
107
            'delivery_order_items_stock_id' => undef,
108
            'project_id' => '',
109
            'parts_id'         => $item->{parts_id},
110
            'qty'              => $item->{qty},
111
            'unit'             => $item->{unit},
112
            'transfer_type'    => 'shipped',
113
            'src_warehouse_id' => $wh_id,
114
            'src_bin_id'       => $bin_id,
115
            'comment' => $main::locale->text("Default transfer delivery order"),
116
            'oe_id' => $delivery_order->{id},
117
          };
118
          $main::lxdebug->dump(0, 'WH: ITEM',\$item);
119
          push @stock_out, {
120
            'delivery_order_item_id'  => $item->{id},
121
            'qty'                     => $item->{qty},
122
            'unit'                    => $item->{unit},
123
            'warehouse_id'            => $wh_id,
124
            'bin_id'                  => $bin_id,
125
          };
126

  
127
          push @errors, @{ $err };
128
        }
129
      }
130
      $main::lxdebug->dump(0, 'WH: LAGER II ', \@transfers);
131
      $main::lxdebug->dump(0, 'WH: LAGER III ', \@errors);
132
      if (!@errors) {
133
        WH->transfer(@transfers);
134
        foreach my $stock_out_item(@stock_out) {
135
          $main::lxdebug->dump(0, "WH: LAGER IV ",\$stock_out_item);
136
          my $delivery_order_items_stock = SL::DB::DeliveryOrderItemsStock->new(%$stock_out_item);
137
          $delivery_order_items_stock->save;
138
          $delivery_order->delivered(1);
139
          $delivery_order->save;
140
        }
141
      }
142
    }
143
  }
144
}
145

  
146
sub create_delivery_order {
147
  my ( $self ) = @_;
148
}
149

  
150
sub run {
151
  $main::lxdebug->message(0, "WH: RUN");
152
  my ($self, $job_obj) = @_;
153
  $::lxdebug->dump(0, 'WH: RUN: ', \$self);
154

  
155
  $self->{job_obj}         = $job_obj;
156
  $self->create_order;
157

  
158
  $job_obj->set_data(status => DONE())->save;
159

  
160
  return 1;
161
}
162
1;
SL/Controller/ShopOrder.pm
4 4

  
5 5
use parent qw(SL::Controller::Base);
6 6

  
7
use SL::BackgroundJob::ShopOrder;
7
use SL::BackgroundJob::ShopOrderMassTransfer;
8 8
use SL::System::TaskServer;
9 9
use SL::DB::ShopOrder;
10 10
use SL::DB::ShopOrderItem;
......
44 44
                                                    );
45 45
  $::lxdebug->dump(0, "WH: IMPORTS I ",  \$shop_orders);
46 46
  foreach my $shop_order ( @$shop_orders ) {
47
  $::lxdebug->dump(0, "WH: IMPORTS II ",  $shop_order);
47
    # $::lxdebug->dump(0, "WH: IMPORTS II ",  $shop_order);
48 48
    my %billing_address = ( 'name'     => $shop_order->billing_lastname,
49 49
                            'company'  => $shop_order->billing_company,
50 50
                            'street'   => $shop_order->billing_street,
......
52 52
                            'city'     => $shop_order->billing_city,
53 53
                          );
54 54
    my $b_address = $self->check_address(%billing_address);
55
    if ($b_address) {
56
      $main::lxdebug->message(0, "WH: SAVE ");
57
      $shop_order->{kivi_customer_id} = $b_address->{id};
58
      $shop_order->save;
59
    }
55 60
    $shop_order->{kivi_cv_id} = $b_address;
56 61
  }
57
  $::lxdebug->dump(0, "WH: IMPORTS III ",  \$shop_orders);
62
  #$::lxdebug->dump(0, "WH: IMPORTS III ",  \$shop_orders);
58 63
  $self->render('shop_order/list',
59 64
                title       => t8('ShopOrders'),
60 65
                SHOPORDERS  => $shop_orders,
......
67 72
  my $id = $::form->{id} || {};
68 73
  my $shop_order = SL::DB::Manager::ShopOrder->find_by( id => $id );
69 74
  die "can't find shoporder with id $id" unless $shop_order;
70
$main::lxdebug->dump(0, 'WH:ORDER: ', \$shop_order);
75
#$main::lxdebug->dump(0, 'WH:ORDER: ', \$shop_order);
71 76
  # the different importaddresscheck if there complete in the customer table to prevent duplicats inserts
72 77
  my %customer_address = ( 'name'    => $shop_order->customer_lastname,
73 78
                           'company' => $shop_order->customer_company,
......
141 146
  my ($self) = @_;
142 147
  my @shop_orders =  @{ $::form->{id} || [] };
143 148
  $::lxdebug->dump(0, 'WH: MT II', \@shop_orders);
149
  $::lxdebug->dump(0, 'WH: MT SELF', \$self);
150
  $::lxdebug->dump(0, 'WH: MT FORM', \$::form);
144 151

  
145 152
  my $job                   = SL::DB::BackgroundJob->new(
146 153
    type                    => 'once',
147 154
    active                  => 1,
148
    package_name            => 'ShopOrder',
155
    package_name            => 'ShopOrderMassTransfer',
149 156
  )->set_data(
150
     shop_order_record_ids       => [ 606, 604, 605],
157
     shop_order_record_ids       => [ @shop_orders ],
151 158
     num_order_created           => 0,
152 159
     num_delivery_order_created  => 0,
153 160
     conversation_errors         => [ ],
154 161
   )->update_next_run_at;
155 162

  
156 163
   SL::System::TaskServer->new->wake_up;
157

  
164
$main::lxdebug->message(0, "WH: ACTION");
158 165
}
159 166

  
160 167
sub action_apply_customer {
SL/DB/MetaSetup/ShopOrderItem.pm
12 12
  description   => { type => 'text' },
13 13
  id            => { type => 'serial', not_null => 1 },
14 14
  partnumber    => { type => 'text' },
15
  position      => { type => 'integer' },
15 16
  price         => { type => 'numeric', precision => 15, scale => 5 },
16 17
  quantity      => { type => 'numeric', precision => 25, scale => 5 },
17 18
  shop_id       => { type => 'integer' },
SL/DB/ShopOrder.pm
36 36
  foreach my $items (@{$self->shop_order_items}) {
37 37
    my $item = SL::DB::OrderItem->new;
38 38
    my $part = SL::DB::Manager::Part->find_by( partnumber => $items->{partnumber} );
39

  
39
$main::lxdebug->dump(0, 'WH: SO PART',\$part);
40 40
    $item->assign_attributes(
41 41
        parts_id        => $part->id,
42 42
        description     => $items->description,
43
        position        => $items->position,
43 44
        qty             => $items->quantity,
44 45
        sellprice       => $items->price,
45 46
        unit            => $part->unit,
SL/IS.pm
1445 1445

  
1446 1446
sub _determine_wh_and_bin {
1447 1447
  $::lxdebug->enter_sub(2);
1448

  
1448
  if (@_ == 6) {my $dummy = shift;}
1449
$main::lxdebug->dump(0, 'WH:WAREHOUSE ',\@_);
1449 1450
  my ($dbh, $conf, $part_id, $qty, $unit) = @_;
1450 1451
  my @errors;
1451 1452

  
......
1509 1510
    }
1510 1511
  }
1511 1512

  
1513
  $main::lxdebug->dump(0, "WH: BIN: $bin_id -- $wh_id", \@errors);
1512 1514
  $::lxdebug->leave_sub(2);
1513 1515
  return (\@errors, $wh_id, $bin_id);
1514 1516
}
SL/ShopConnector/Shopware.pm
10 10
use SL::DB::ShopOrder;
11 11
use SL::DB::ShopOrderItem;
12 12
use Data::Dumper;
13
use Sort::Naturally ();
13 14

  
14 15
use Rose::Object::MakeMethods::Generic (
15 16
  'scalar --get_set_init' => [ qw(connector url) ],
......
19 20
  my ($self, $id) = @_;
20 21

  
21 22
  my $url = $self->url;
22
  my $ordnumber = 61544;
23
  my $ordnumber = 61101;
23 24
  # Muss noch angepasst werden
24
  for(my $i=1;$i<=300;$i++) {
25
  for(my $i=1;$i<=20;$i++) {
25 26
    my $data = $self->connector->get("http://$url/api/orders/$ordnumber?useNumberAsId=true");
26 27
    $ordnumber++;
27 28
    $::lxdebug->dump(0, "WH: DATA ", \$data);
......
99 100
    my $id = $insert->id;
100 101
    #$::lxdebug->dump(0, "WH: ID ", $insert->id);
101 102

  
102
    my @positions = @{ $import->{data}->{details} };
103
    #$::lxdebug->dump(0, "WH: POSITIONS ", \@positions);
103
    #my @positions = @{ $import->{data}->{details} };
104
    my @positions = sort { Sort::Naturally::ncmp($a->{"partnumber"}, $b->{"partnumber"}) } @{ $import->{data}->{details} };
105
    # $::lxdebug->dump(0, "WH: POSITIONS ", \@positions);
106
    my $position = 1;
104 107
    foreach my $pos(@positions) {
105 108
      my %pos_columns = ( description => $pos->{articleName},
106
                          id          => $pos->{id},
109
        #     id          => $pos->{id},
107 110
                          partnumber  => $pos->{articleNumber},
108 111
                          price       => $pos->{price},
109 112
                          quantity    => $pos->{quantity},
110
                          #shop_id     => $pos->{articleId},
113
                          position    => $position,
111 114
                          tax_rate    => $pos->{taxRate},
112 115
                          shop_trans_id    => $pos->{articleId},
113 116
                          shop_order_id    => $id,
114 117
                        );
115 118
      my $pos_insert = SL::DB::ShopOrderItem->new(%pos_columns);
116 119
      $pos_insert->save;
120
      $position++;
117 121
      #$::lxdebug->dump(0,"WH: POS ", \%pos_columns);
118 122
    }
123
    # Versandkosten als Position am ende einfügen Dreschflegelspezifisch event. konfigurierbar machen
124
    if (my $shipping = $import->{data}->{dispatch}->{name}) {
125
      my %shipping_partnumbers = (
126
                                  'Auslandsversand Einschreiben' => { 'partnumber' => '900650'},
127
                                  'Auslandsversand'              => { 'partnumber' => '900650'},
128
                                  'Standard Versand'            => { 'partnumber' => '905500'},
129
                                  'Kostenloser Versand'         => { 'partnumber' => '905500'},
130
                                );
131
      $main::lxdebug->message(0, "WH: SHIPPING1: $shipping ");
132
      my %shipping_pos = ( description => $import->{data}->{dispatch}->{name},
133
        #      id          => 0,
134
                           partnumber  => $shipping_partnumbers{$shipping}->{partnumber},
135
                           price       => $import->{data}->{invoiceShipping},
136
                           quantity    => 1,
137
                           position    => $position,
138
                           tax_rate    => 7,
139
                           shop_trans_id  => 0,
140
                           shop_order_id  => $id,
141
                         );
142
      $main::lxdebug->dump(0, 'WH: SHIPPING: ', \%shipping_pos);
143
      my $shipping_pos_insert = SL::DB::ShopOrderItem->new(%shipping_pos);
144
      $shipping_pos_insert->save;
145
    }
119 146
  }
120 147
  # return $import;
121 148
};
SL/WH.pm
47 47

  
48 48
  my ($self, @args) = @_;
49 49

  
50
      $main::lxdebug->dump(0, 'WH: TRANSFER @_',\@_);
51
      $main::lxdebug->dump(0, 'WH: TRANSFER SELF',\$self);
50 52
  if (!@args) {
51 53
    $::lxdebug->leave_sub;
52 54
    return;
......
63 65

  
64 66
  my $objectify = sub {
65 67
    my ($transfer, $field, $class, @find_by) = @_;
68
      $main::lxdebug->dump(0, 'WH: TRANSFER II @_',\@_);
69
      $main::lxdebug->dump(0, 'WH: TRANSFER II TRANSFER',\$transfer);
66 70

  
67 71
    @find_by = (description => $transfer->{$field}) unless @find_by;
68 72

  
......
79 83
  my $db = SL::DB::Inventory->new->db;
80 84
  $db->with_transaction(sub{
81 85
    while (my $transfer = shift @args) {
86
      $main::lxdebug->dump(0, 'WH: WAREHOUSE TRANSFER ',\$transfer);
82 87
      my ($trans_id) = selectrow_query($::form, $::form->get_standard_dbh, qq|SELECT nextval('id')|);
83 88

  
84 89
      my $part          = $objectify->($transfer, 'parts',         'SL::DB::Part');
......
110 115
                              ? $now : $transfer->{shippingdate},
111 116
          map { $_ => $transfer->{$_} } qw(chargenumber bestbefore oe_id delivery_order_items_stock_id invoice_id comment),
112 117
      );
113

  
118
      my $unit_obj = $part->unit_obj;##
119
$main::lxdebug->dump(0, 'WH: WAREHOUSE PART:', \$part);
120
$main::lxdebug->dump(0, "WH: WAREHOUSE I QTY: $qty --", \$unit);
114 121
      if ($unit) {
122
        $main::lxdebug->message(0, "WH: WAREHOUSE $qty -- $unit -- $part->unit_obj");
115 123
        $qty = $unit->convert_to($qty, $part->unit_obj);
116 124
      }
117 125

  
sql/Pg-upgrade2/shop_orders_2.sql
1
-- @tag: shop_orders_2
2
-- @description: Hinzufügen der Spalte Position in der Tabelle shop_order_items
3
-- @depends: release_3_3_0
4
ALTER TABLE shop_order_items ADD COLUMN position integer;
t/background_job/known_jobs.t
7 7
use_ok 'SL::BackgroundJob::Base';
8 8

  
9 9
my @expected_known_job_classes = qw(BackgroundJobCleanup CleanAuthSessions CleanBackgroundJobHistory CreatePeriodicInvoices CsvImport FailedBackgroundJobsReport
10
                                      MassRecordCreationAndPrinting SelfTest Test);
10
                                      MassRecordCreationAndPrinting SelfTest ShopOrderMassTransfer Test);
11 11
is_deeply [ SL::BackgroundJob::Base->get_known_job_classes ], \@expected_known_job_classes, 'get_known_job_classes called as class method';
12 12

  
13 13
my $job = new_ok 'SL::BackgroundJob::Base';
templates/webpages/shop_order/list.html
156 156
    <td>[% shop_order.shop_order_items.size %]<br>[% shop_order.amount_as_number %]<br>[% shop_order.shipping_costs_as_number %]</td>
157 157
    <td>[% IF shop_order.transferred == 1 %]<a href="controller.pl?id=[% shop_order.id %]&action=ShopOrder/show">[% 'Show order' | $T8 %]<br>[% shop_order.transferred_date_as_date %]</a>
158 158
        [% ELSE %]
159
        [% L.checkbox_tag('id[]', checked = transferable,  value=shop_order.id) %]<br>
159
        [% L.checkbox_tag('id[]', checked = '',  value=shop_order.id) %]<br>
160 160
        <a href="controller.pl?id=[% shop_order.id %]&action=ShopOrder/show">[% 'Create order' | $T8 %][% END %]</a></td>
161 161
  </tr>
162 162
  [% END %]

Auch abrufbar als: Unified diff