Shop: Woocommerce Debug raus
[kivitendo-erp.git] / SL / ShopConnector / WooCommerce.pm
index f65570d..77aea40 100644 (file)
@@ -9,6 +9,7 @@ use LWP::UserAgent;
 use LWP::Authen::Digest;
 use SL::DB::ShopOrder;
 use SL::DB::ShopOrderItem;
+use SL::DB::PaymentTerm;
 use SL::DB::History;
 use SL::DB::File;
 use Data::Dumper;
@@ -31,10 +32,11 @@ sub get_one_order {
   if($answer->{success}) {
     my $shoporder = $answer->{data};
 
+    $main::lxdebug->dump(0, 'WH: ANSWER ', $answer);
     $dbh->with_transaction( sub{
         #update status on server
         $shoporder->{status} = "processing";
-        my $anser = $self->set_orderstatus($$shoporder->{id}, "fetched");
+        my $answer = $self->set_orderstatus($shoporder->{id}, "completed");
         unless($answer){
           push @errors,($::locale->text('Saving failed. Error message from the server: #1', $answer->message));
           return 0;
@@ -77,17 +79,16 @@ sub get_new_orders {
     "orders",
     undef,
     "get",
-    "&per_page=$otf&status=pending"
+    "&per_page=$otf&status=processing&after=2020-12-31T23:59:59&order=asc"
   );
   my %fetched_orders;
   if($answer->{success}) {
     my $orders = $answer->{data};
     foreach my $shoporder(@{$orders}){
-
       $dbh->with_transaction( sub{
           #update status on server
-          $shoporder->{status} = "processing";
-          my $anser = $self->set_orderstatus($$shoporder->{id}, "fetched");
+          $shoporder->{status} = "completed";
+          my $anwser = $self->set_orderstatus($shoporder->{id}, "completed");
           unless($answer){
             push @errors,($::locale->text('Saving failed. Error message from the server: #1', $answer->message));
             return 0;
@@ -131,16 +132,19 @@ sub import_data_to_shop_order {
   my @positions = sort { Sort::Naturally::ncmp($a->{"sku"}, $b->{"sku"}) } @{ $import->{line_items} };
   my $position = 1;
 
-  my $answer= $self->send_request("taxes");
-  unless ($answer->{success}){ return 0;}
-  my %taxes = map { ($_->{id} => $_) } @{ $answer->{data} };
-
   my $active_price_source = $self->config->price_source;
+  my $tax_included = $self->config->pricetype eq 'brutto' ? 1 : 0;
   #Mapping Positions
   foreach my $pos(@positions) {
-    my $price = $::form->round_amount($pos->{total},2);
-    my $tax_id = $pos->{taxes}[0]->{id};
-    my $tax_rate = $taxes{ $tax_id }{rate};
+    my $tax_rate = $pos->{tax_class} eq "reduced-rate" ? 7 : 19;
+    my $tax_factor = $tax_rate/100+1;
+    my $price = $pos->{price};
+    if ( $tax_included ) {
+      $price = $price * $tax_factor;
+      $price = $::form->round_amount($price,2);
+    } else {
+      $price = $::form->round_amount($price,2);
+    }
     my %pos_columns = ( description          => $pos->{name},
                         partnumber           => $pos->{sku}, # sku has to be a valid value in WooCommerce
                         price                => $price,
@@ -157,6 +161,21 @@ sub import_data_to_shop_order {
   }
   $shop_order->positions($position-1);
 
+  if ( $self->config->shipping_costs_parts_id ) {
+    my $shipping_part = SL::DB::Manager::Part->find_by( id => $self->config->shipping_costs_parts_id);
+    my %shipping_pos = (
+      description    => $import->{data}->{dispatch}->{name},
+      partnumber     => $shipping_part->partnumber,
+      price          => $import->{data}->{invoiceShipping},
+      quantity       => 1,
+      position       => $position,
+      shop_trans_id  => 0,
+      shop_order_id  => $id,
+    );
+    my $shipping_pos_insert = SL::DB::ShopOrderItem->new(%shipping_pos);
+    $shipping_pos_insert->save;
+  }
+
   my $customer = $shop_order->get_customer;
 
   if(ref($customer)){
@@ -175,10 +194,23 @@ sub map_data_to_shoporder {
                                                 );
 
   my $shop_id      = $self->config->id;
+  my $tax_included = $self->config->pricetype;
 
   # Mapping to table shoporders. See https://woocommerce.github.io/woocommerce-rest-api-docs/?shell#order-properties
+    my $d_street;
+    if ( $import->{shipping}->{address_1} ne "" ) {
+      $d_street = $import->{shipping}->{address_1} . ($import->{shipping}->{address_2} ? " " . $import->{shipping}->{address_2} : "");
+    } else {
+      $d_street = $import->{billing}->{address_1} . ($import->{billing}->{address_2} ? " " . $import->{billing}->{address_2} : "");
+    }
+  # Mapping Zahlungsmethoden muss an Firmenkonfiguration angepasst werden
+  my %payment_ids_methods = (
+    # woocommerce_payment_method_title => kivitendo_payment_id
+  );
+  my $default_payment    = SL::DB::Manager::PaymentTerm->get_first();
+  my $default_payment_id = $default_payment ? $default_payment->id : undef;
   my %columns = (
-#billing
+#billing Shop can have different billing addresses, and may have 1 customer_address
     billing_firstname       => $import->{billing}->{first_name},
     billing_lastname        => $import->{billing}->{last_name},
     #address_1 address_2
@@ -195,7 +227,7 @@ sub map_data_to_shoporder {
     #billing_greeting        => "",
     #billing_fax             => "",
     #billing_vat             => "",
-    #billing_company         => "",
+    billing_company         => $import->{billing}->{company},
     #billing_department      => "",
 
 #customer
@@ -222,15 +254,15 @@ sub map_data_to_shoporder {
     #customer_vat            => "",
 
 #shipping
-    delivery_firstname      => $import->{shipping}->{first_name},
-    delivery_lastname       => $import->{shipping}->{last_name},
-    delivery_company        => $import->{shipping}->{company},
+    delivery_firstname      => $import->{shipping}->{first_name} || $import->{billing}->{first_name},
+    delivery_lastname       => $import->{shipping}->{last_name} || $import->{billing}->{last_name},
+    delivery_company        => $import->{shipping}->{company} || $import->{billing}->{company},
     #address_1 address_2
-    delivery_street         => $import->{shipping}->{address_1} . ($import->{shipping}->{address_2} ? " " . $import->{shipping}->{address_2} : ""),
-    delivery_city           => $import->{shipping}->{city},
+    delivery_street         => $d_street,
+    delivery_city           => $import->{shipping}->{city} || $import->{billing}->{city},
     #state ???
-    delivery_zipcode        => $import->{shipping}->{postcode},
-    delivery_country        => $import->{shipping}->{country},
+    delivery_zipcode        => $import->{shipping}->{postcode} || $import->{billing}->{postcode},
+    delivery_country        => $import->{shipping}->{country} || $import->{billing}->{country},
     #delivery_department     => "",
     #delivery_email          => "",
     #delivery_fax            => "",
@@ -255,20 +287,20 @@ sub map_data_to_shoporder {
     #discount_total
     #discount_tax
     #shipping_total
-    shipping_costs          => $import->{shipping_costs},
+    shipping_costs          => $import->{shipping_total},
     #shipping_tax
-    shipping_costs_net      => $import->{shipping_costs} - $import->{shipping_tax},
+    shipping_costs_net      => $import->{shipping_total},
     #cart_tax
     #total
     amount                  => $import->{total},
     #total_tax
     netamount               => $import->{total} - $import->{total_tax},
     #prices_include_tax
-    tax_included            => $import->{prices_include_tax} eq "true" ? 1 : 0,
+    tax_included            => $tax_included,
     #payment_method
-    # ??? payment_id              => $import->{payment_method},
+    payment_id              => $payment_ids_methods{$import->{payment_method}} || $default_payment_id,
     #payment_method_title
-    payment_description     => $import->{payment}->{payment_method_title},
+    payment_description     => $import->{payment_method_title},
     #transaction_id
     shop_trans_id           => $import->{id},
     #date_paid
@@ -405,7 +437,7 @@ sub get_article {
 sub get_categories {
   my ($self) = @_;
 
-  my $answer = $self->send_request("products/categories");
+  my $answer = $self->send_request("products/categories",undef,"get","&per_page=100");
   unless($answer->{success}) {
     return $answer;
   }
@@ -444,8 +476,8 @@ sub get_version {
 
 sub set_orderstatus {
   my ($self,$order_id, $status) = @_;
-  if ($status eq "fetched") { $status =  "processing"; }
-  if ($status eq "completed") { $status = "completed"; }
+  #  if ($status eq "fetched") { $status =  "processing"; }
+  #  if ($status eq "processing") { $status = "completed"; }
   my %new_status = (status => $status);
   my $status_json = SL::JSON::to_json( \%new_status);
   my $answer = $self->send_request("orders/$order_id", $status_json, "put");