n flamant_erp_order_getter() { global $base_url; global $conf; $sql = <<store_email = null; $discount_amount = 0.0; $total_amount = 0.0; $billing = null; $shipping = null; foreach (array("billing", "shipping") as $bundle) { $query = db_select("field_data_commerce_customer_address", "a"); $query->fields("a"); $query->fields("e", array("field_address_ext_value")); $query->fields("m", array("field_email_address_value")); $query->leftJoin("field_data_field_address_ext", "e", "e.entity_id = a.entity_id AND e.bundle = a.bundle"); $query->join("field_data_field_email_address", "m", "m.entity_id = a.entity_id AND m.bundle = a.bundle"); if ($bundle == "billing") { $query->fields("v", array("field_vat_value")); $query->leftJoin("field_data_field_vat", "v", "v.entity_id = a.entity_id AND v.bundle = a.bundle"); $query->fields("f", array("field_fiscal_code_value")); $query->leftJoin("field_data_field_fiscal_code", "f", "f.entity_id = a.entity_id AND f.bundle = a.bundle"); } $query->condition("a.entity_id", $order->{"commerce_customer_{$bundle}_profile_id"}); $query->condition("a.bundle", $bundle); $result2 = $query->execute()->fetch(); d($result2); if ($bundle == "billing") { $billing = $result2; if ($result2) { if ($result2->commerce_customer_address_data) { $billing->data = unserialize($result2->commerce_customer_address_data); } } } else { $shipping = $result2; if ($result2) { if ($result2->commerce_customer_address_data) { $shipping->data = unserialize($result2->commerce_customer_address_data); } } } } if (empty($billing->commerce_customer_address_organisation_name)) { if (!empty($shipping->commerce_customer_address_organisation_name)) { $billing->commerce_customer_address_organisation_name = $shipping->commerce_customer_address_organisation_name; } } if ($billing) { $billing->company = $billing->commerce_customer_address_organisation_name; } else { $billing = $shipping; } if ($shipping) { $shipping->company = $shipping->commerce_customer_address_organisation_name; } //CHECK SHIPPING $query = db_select("field_data_commerce_line_items", "lir"); $query->fields("li", array("line_item_id", "type", "line_item_label", "quantity", "data")); $query->fields("p", array("commerce_unit_price_amount")); $query->join("commerce_line_item", "li", "li.line_item_id = lir.commerce_line_items_line_item_id"); $query->join("field_data_commerce_unit_price", "p", "p.entity_id = li.line_item_id AND p.entity_type = 'commerce_line_item'"); $query->condition("lir.entity_id", $order->order_id); $query->condition("lir.entity_type", "commerce_order"); $query->condition("li.type", array("product", "shipping", "commerce_discount", "product_discount"), "IN"); $result3 = $query->execute(); foreach ($result3 as $line_item) { if ($line_item->type === "shipping") { $HAS_SHIPPING_ITEM = true; } } if (!$HAS_SHIPPING_ITEM) { continue; } //CHECK EMAIL $email = null; $shipping_changed = false; $billing_changed = false; // d($billing); // d($shipping); // d($order); if (flamant_erp_valid_email($order->mail)) { $email = $order->mail; $HAS_ORDER_EMAIL = true; } if ($shipping) { if (flamant_erp_valid_email($shipping->field_email_address_value)) { $email = $shipping->field_email_address_value; $HAS_SHIPPING_EMAIL = true; } } if($billing) { if (flamant_erp_valid_email($billing->field_email_address_value)) { $email = $billing->field_email_address_value; $HAS_BILLING_EMAIL = true; } } if ($email == null) { continue; } if ($shipping) { if (!$HAS_SHIPPING_EMAIL) { $shipping->field_email_address_value = $email; commerce_customer_profile_save($shipping); } } if($billing) { if (!$HAS_BILLING_EMAIL) { $billing->field_email_address_value = $email; commerce_customer_profile_save($billing); } } if (!$HAS_ORDER_EMAIL) { $order->mail = $email; commerce_order_save($order); } // Is there a shipping line item? // Is there language on order $order->language = $billing->commerce_customer_address_country || $shipping->commerce_customer_address_country; if ($order->language == null) { //TODO: NO LANGUAGE continue; } $order->billing = $billing; $order->shipping = $shipping; array_push($ok_orders, $order); } return $ok_orders; } function flamant_erp_vat_getter() { global $base_url; global $conf; $query = db_select("taxonomy_term_data", "t"); $query->fields("t", array("name")); $query->fields("c", array("field_store_country_value")); $query->fields("p", array("field_vat_percentage_value")); $query->fields("k", array("field_can_invoice_value")); $query->join("taxonomy_vocabulary", "v", "v.vid = t.vid"); $query->join("field_data_field_store_country", "c", "c.entity_id = t.tid AND c.entity_type = 'taxonomy_term' AND c.bundle = v.machine_name"); $query->join("field_data_field_vat_percentage", "p", "p.entity_id = t.tid AND p.entity_type = 'taxonomy_term' AND p.bundle = v.machine_name"); $query->join("field_data_field_can_invoice", "k", "k.entity_id = t.tid AND k.entity_type = 'taxonomy_term' AND k.bundle = v.machine_name"); $query->condition("v.machine_name", "pim_vat"); // $query->condition("k.field_can_invoice_value", 1); $query->orderBy("p.field_vat_percentage_value", "DESC"); $result = $query->execute(); $VAT = array(); $VAT_MAP = array(); foreach ($result as $row) { $VAT[$row->name] = $row->field_can_invoice_value; $cnt = substr($row->name, 0, 2); if ($cnt == "BV") { $VAT["BE"] = $row->field_can_invoice_value; } if ($cnt == "DV") { $VAT["DE"] = $row->field_can_invoice_value; } if ($cnt == "IV") { $VAT["IT"] = $row->field_can_invoice_value; } if ($cnt == "FV") { $VAT["FR"] = $row->field_can_invoice_value; } //MAP if (isset($VAT_MAP[$row->field_store_country_value]) == false) { $VAT_MAP[$row->field_store_country_value] = array(); } $VAT_MAP[$row->field_store_country_value][] = $row->name; } return $VAT_MAP; } function flamant_order_process($order, $VAT_MAP) { $processed = flag_get_flag("processed"); $COUNTRY = array( "BE" => "belgium", "FR" => "france", "NL" => "netherlands", "DE" => "germany", "IT" => "italy", "AT" => "austria", "LU" => "luxembourg", ); $backorder = "N"; $shipping_agent_number = null; $shipping_reference = null; $order->store_email = null; $discount_amount = 0.0; $total_amount = 0.0; $billing = $order->billing; $shipping = $order->shipping; $delivery_type = null; $delivery_time = fla_commerce_get_raw_order_delivery_time($order->order_id); $query = db_select("field_data_commerce_line_items", "lir"); $query->fields("li", array("line_item_id", "type", "line_item_label", "quantity", "data")); $query->fields("p", array("commerce_unit_price_amount")); $query->join("commerce_line_item", "li", "li.line_item_id = lir.commerce_line_items_line_item_id"); $query->join("field_data_commerce_unit_price", "p", "p.entity_id = li.line_item_id AND p.entity_type = 'commerce_line_item'"); $query->condition("lir.entity_id", $order->order_id); $query->condition("lir.entity_type", "commerce_order"); $query->condition("li.type", array("product", "shipping", "commerce_discount", "product_discount"), "IN"); $result3 = $query->execute(); $line_items = array(); foreach ($result3 as $row) { $data = unserialize($row->data); $line_item = array( "Country" => "", "Currency" => "EUR", "From_Name" => null, "Incl_VAT" => "Y", "Item_Description" => null, "Language" => "", "OrderLineReferenceID" => $order->order_id, "Price" => ($row->commerce_unit_price_amount / 100.0), "Product_Code" => null, "Quantity" => (int)($row->quantity), "Recupel" => "N", "Recupel_Type" => null, "Recupel_Value" => "0.00", "Text" => null, "To_Name" => null, "VAT_CODE" => null, ); $country_b = $billing->commerce_customer_address_country; $country = $billing->commerce_customer_address_country; $country_q = $COUNTRY[$country]; if ($row->type == "product") { $product_id = $data["context"]["product_ids"][0]; $country = $billing->commerce_customer_address_country; $country_q = $COUNTRY[$country]; $has_vat_number = $billing->field_vat_value == null ? false : true; // if ($VAT[$country] != 1) { // $country = $COUNTRY["BE"]; // } $query = db_select("commerce_product", "cp"); $query->fields("cp", array("sku", "title")); $query->fields("s", array("commerce_stock_value")); $query->fields("ss", array("field_product_short_summary_value")); $query->fields("t", array("name")); $query->join("field_data_field_product", "n", "n.field_product_product_id = cp.product_id AND n.entity_type = 'node' AND n.bundle = 'hh_product'"); $query->leftJoin("field_data_field_product_short_summary", "ss", "ss.entity_id = n.entity_id AND ss.entity_type = 'node' AND ss.bundle = 'hh_product' AND ss.language = '{$order->field_order_language_value}'"); $query->join("field_data_commerce_stock", "s", "s.entity_id = cp.product_id AND s.entity_type = 'commerce_product'"); $query->leftJoin("field_data_field_{$country_q}_vat", "v", "v.entity_id = cp.product_id AND v.entity_type = 'commerce_product'"); $query->leftJoin("taxonomy_term_data", "t", "t.tid = v.field_{$country_q}_vat_tid"); $query->leftJoin("field_data_field_can_invoice", "vt", "t.tid = vt.entity_id"); $query->condition("cp.product_id", $product_id); $product = $query->execute()->fetch(); $line_item["Item_Description"] = $product->title; if ($product->field_product_short_summary_value != null) { $line_item["Item_Description"] .= ", " . $product->field_product_short_summary_value; } $line_item["Product_Code"] = $product->sku; $line_item["VAT_CODE"] = $product->name; if ($VAT[$country] != 1 && $has_vat_number) { $line_item["VAT_CODE"] = "BVEU"; }else if ($VAT[$country] != 1 && !$has_vat_number) { $line_item["VAT_CODE"] = "BV21"; } if ($line_item["Quantity"] > $product->commerce_stock_value) { $backorder = "Y"; } $line_item["Price"] = ($row->commerce_unit_price_amount / 100.0); $total_amount += $line_item["Quantity"] * $line_item["Price"]; fla_commerce_decrease_product_stock($product_id, $line_item["Quantity"]); } else if ($row->type == "shipping") { switch ($data["shipping_service"]["name"]) { case "bpost_home": case "bpost_world": $delivery_type = "bpost"; $shipping_agent_number = "80001568"; break; case "delivery": case "front_door": case "white_glove": $delivery_type = "idl"; $shipping_agent_number = "80001779"; break; } if ($order->field_selected_store_value != null) { $delivery_type = "store-pickup"; $shipping_agent_number = "FLAMANTSHOP"; $shipping_reference = $order->field_selected_store_value; $order->store_email = $shipping->field_email_address_value; } switch ($row->line_item_label) { case "Home delivery": $line_item["Product_Code"] = "9999900079"; break; case "Front door delivery": $line_item["Product_Code"] = "9999900012"; break; case "White glove delivery": $line_item["Product_Code"] = "9999900080"; break; case "Store pickup": // Bpost case "Pickup": // IDL $line_item["Product_Code"] = "9999900081"; break; default: $line_item["Product_Code"] = "9999900079"; break; } $line_item["Item_Description"] = t($row->line_item_label, array(), array("langcode" => $order->field_order_language_value)); //TODO: fix shipping VAT $line_item["VAT_CODE"] = $VAT_MAP[$shipping->commerce_customer_address_country][0]; if ($VAT[$shipping->commerce_customer_address_country] != 1 && $has_vat_number) { $line_item["VAT_CODE"] = "BVEU"; } else if ($VAT[$shipping->commerce_customer_address_country] != 1 && !$has_vat_number) { $line_item["VAT_CODE"] = "BV21"; } $total_amount += (($row->commerce_unit_price_amount)/ 100.0); } else if ($row->type == "commerce_discount") { $discount_amount += ($row->commerce_unit_price_amount / 100.0); continue; } else if ($row->type == "product_discount") { $country = $shipping->commerce_customer_address_country; $country_q = $COUNTRY[$country]; $has_vat_number = $billing->field_vat_value == null ? false : true; if ($VAT[$country] != 1) { $country = $COUNTRY["BE"]; } $query = db_select("commerce_product", "cp"); $query->fields("cp", array("product_id", "sku", "title")); $query->fields("s", array("commerce_stock_value")); $query->fields("ss", array("field_product_short_summary_value")); $query->fields("t", array("name")); $query->join("field_data_field_product", "n", "n.field_product_product_id = cp.product_id AND n.entity_type = 'node' AND n.bundle = 'hh_product'"); $query->leftJoin("field_data_field_product_short_summary", "ss", "ss.entity_id = n.entity_id AND ss.entity_type = 'node' AND ss.bundle = 'hh_product' AND ss.language = '{$order->field_order_language_value}'"); $query->join("field_data_commerce_stock", "s", "s.entity_id = cp.product_id AND s.entity_type = 'commerce_product'"); $query->leftJoin("field_data_field_{$country_q}_vat", "v", "v.entity_id = cp.product_id AND v.entity_type = 'commerce_product'"); $query->leftJoin("taxonomy_term_data", "t", "t.tid = v.field_{$country_q}_vat_tid"); $query->leftJoin("field_data_field_can_invoice", "vt", "t.tid = vt.entity_id"); $query->condition("cp.sku", $row->line_item_label); $product = $query->execute()->fetch(); $line_item["Item_Description"] = $product->title; if ($product->field_product_short_summary_value != null) { $line_item["Item_Description"] .= ", " . $product->field_product_short_summary_value; } $line_item["Product_Code"] = $product->sku; $line_item["VAT_CODE"] = $product->name; if ($VAT[$country] != 1 && $has_vat_number) { $line_item["VAT_CODE"] = "BVEU"; } if ($line_item["Quantity"] > $product->commerce_stock_value) { $backorder = "Y"; } $total_amount += $line_item["Quantity"] * ($row->commerce_unit_price_amount / 100.0); fla_commerce_decrease_product_stock($product->product_id, $line_item["Quantity"]); } $line_items[] = $line_item; } d($line_items); $delivery_date = fla_commerce_get_raw_order_delivery_time($order->order_id); $scale = 86400; if ($delivery_date["unit"] == "weeks") { $scale *= 7; } else if ($delivery_date["unit"] == "months") { $scale *= 30; } //$transaction = db_transaction(); $placed = ($order->placed != 0) ? $order->placed : $order->changed; $timestamp = time(); try { var_dump("SAVEEE"); // added @ 2019-12-12 by Zdenko // phone number was missing $unpkgData = unserialize($billing->commerce_customer_address_data); $comp_name = empty($billing->company) ? (empty($shipping->comapny) ? null : $shipping->company) : $billing->company; d($billing); d($shipping); db_insert("fla_order_header")->fields(array( "Backorder" => $backorder, "Customer_Email" => $order->mail, "Delivery_Address1" => $shipping->commerce_customer_address_thoroughfare, "Delivery_Address2" => $shipping->commerce_customer_address_premise, "Delivery_Address3" => $shipping->field_address_ext_value ?: null, "Delivery_Country" => $shipping->commerce_customer_address_country, "Delivery_Floor" => $order->field_floor_value, "Delivery_Location" => $shipping->commerce_customer_address_locality, "Delivery_Name" => $shipping->commerce_customer_address_name_line, "Delivery_Zipcode" => $shipping->commerce_customer_address_postal_code, "Invoice_Address1" => $billing->commerce_customer_address_thoroughfare, "Invoice_Address2" => $billing->commerce_customer_address_premise, "Invoice_Address3" => $billing->field_address_ext_value ?: null, "Invoice_Country" => $billing->commerce_customer_address_country, "Invoice_Location" => $billing->commerce_customer_address_locality, "Invoice_Name" => $billing->commerce_customer_address_name_line, "Invoice_Zipcode" => $billing->commerce_customer_address_postal_code, "ItalianTaxNumber" => $billing->field_fiscal_code_value ?: null, "Language" => ($order->field_order_language_value == "nl") ? "BD" : strtoupper($order->field_order_language_value), "MaxDate" => strftime("%Y-%m-%dT00:00:00", $timestamp + ($delivery_date["max"] * $scale)), "MinDate" => strftime("%Y-%m-%dT00:00:00", $timestamp + ($delivery_date["min"] * $scale)), "Mobile_Number" => null, "OrderReferenceID" => $order->order_id, "Order_Comment" => $order->field_comment_value, "Order_DateTime" => strftime("%Y-%m-%dT%H:%M:%S", $placed), "Order_Lines" => count($line_items), "Payment_Method" => ($order->payment_method != null) ? strtoupper(substr($order->payment_method, 8)) : "NONE", "Phone_Number" => $billing->data["phone_number"] ?? $unpkgData["phone_number"], "Shipping_Agent_Number" => $shipping_agent_number, "Shipping_reference" => $shipping_reference, "VAT_Registration_Number" => $billing->field_vat_value ? strtoupper($billing->field_vat_value) : null, "Company_Name" => $comp_name, ))->execute(); watchdog("SAVE AFTER", "asdasd"); // Prilagodimo ceno, da upostevamo popust. $discount = 1 + ($discount_amount / $total_amount); foreach ($line_items as $line_item) { $line_item["Price"] = number_format($line_item["Price"] * $discount, 2, ".", ""); db_insert("fla_order_lines")->fields($line_item)->execute(); watchdog("LINE ITEM", $line_item["Item_Description"]); } } catch (Exception $e) { //$transaction->rollback(); watchdog_exception("flamant_erp.processor EXCEPTION", $e); return; } $processed->flag("flag", $order->order_id, null, true); watchdog("FLAG", $order->order_id); if ($delivery_type == "bpost") { $entity = commerce_order_load($order->order_id); $entity->data["bpost_id"] = $order->order_id; commerce_bpost_webservice_create_order($entity, "commerce_order"); } $data = array( "admin" => false, "order_id" => $order->order_id, "name" => $billing->commerce_customer_address_name_line, "date" => format_date($placed, "short"), "delivery_time" => fla_commerce_format_delivery_time($delivery_time, $order->field_order_language_value), "delivery_method" => $delivery_type, "billing" => $billing, "shipping" => $shipping, "site_name" => "Flamant" ); fla_commerce_send_email("order", $order->mail, $order->field_order_language_value, $data); $data["admin"] = true; $data["delivery_time"] = fla_commerce_format_delivery_time($delivery_time, "nl"); fla_commerce_send_email("order", array($conf["customer_service_email"], "pww@flamant.com"), "nl", $data); if ($shipping_agent_number == "FLAMANTSHOP") { $country = strtolower($shipping->commerce_customer_address_country); if ($country == "be") { $language = "nl"; // TODO: Remove this as there are no stores? } else if ($country == "at") { $language = "de"; } else if ($country == "lu") { // TODO: Get order language? $language = "de"; } else { $language = $country; } $email = $shipping->field_email_address_value; $data["delivery_time"] = fla_commerce_format_delivery_time($delivery_time, $language); if (empty($conf["pickup_in_store_email"]) == false) { $email = $conf["pickup_in_store_email"]; } fla_commerce_send_email("order", $email, $language, $data); } $query = db_select("users", "u"); $query->fields("u", array("uid", "name", "mail", "pass", "login")); $query->condition("u.uid", $order->uid); $user = $query->execute()->fetch(); // Email for new user account. if ($user != null && $user->login == 0) { $reset_url = "user/reset/{$user->uid}/{$timestamp}/" . user_pass_rehash($user->pass, $timestamp, $user->login, $user->uid); fla_commerce_send_email("account", $user->mail, $order->field_order_language_value, array( "site_name" => "Flamant", "reset_url" => "{$b387ase_url}/{$order->field_order_language_value}/{$reset_url}", "login_url" => "{$base_url}/{$order->field_order_language_value}/user", "user_name" => $user->name, )); } } function flamant_erp_processor() { global $base_url; global $conf; $orders = flamant_erp_order_getter(); $VAT_MAP = flamant_erp_vat_getter(); foreach ($orders as $order) { try { flamant_order_process($order, $VAT_MAP); } catch (\Throwable $th) { //throw $th; var_dump($th); } } } function flamant_erp_valid_email($email) { // SET INITIAL RETURN VARIABLES $emailIsValid = FALSE; // MAKE SURE AN EMPTY STRING WASN'T PASSED if (!empty($email)) { // GET EMAIL PARTS $domain = ltrim(stristr($email, '@'), '@') . '.'; $user = stristr($email, '@', TRUE); // VALIDATE EMAIL ADDRESS if ( !empty($user) && !empty($domain) && checkdnsrr($domain) ) {$emailIsValid = TRUE;} } // RETURN RESULT return $emailIsValid; } Illuminazione | Flamant

Attendere prego...