EdifactController bez FTP-a
Thu Jan 13 2022 20:52:16 GMT+0000 (Coordinated Universal Time)
<?php
namespace Drupal\stormtextil_core\Controller;
use Drupal\commerce_shipping\Entity\Shipment;
use Drupal\commerce_shipping\Entity\ShippingMethod;
use Drupal\commerce_shipping\ShipmentItem;
use Drupal\Core\Controller\ControllerBase;
use Drupal\physical\Weight;
use Drupal\physical\WeightUnit;
use Drupal\profile\Entity\Profile;
use EDI\Parser;
use Drupal\commerce_price\Price;
use Drupal\commerce_order\Entity\OrderItem;
use Drupal\commerce_order\Entity\Order;
class EdifactController extends ControllerBase {
public function parse() {
$parser = new Parser();
$ediFile = file_get_contents('./sites/default/files/1509018E1-000523-1611970.EDI');
$parsedEdiFile = $parser->loadString($ediFile);
$lineItems = [];
$orderItems = [];
$currency = $parsedEdiFile[18][1][1];
foreach ($parsedEdiFile as $ediElement) {
switch($ediElement[0]) {
case 'NAD':
if ($ediElement[1] == 'DP') {
$organization = $ediElement[3][0];
$address_line1 = $ediElement[3][1];
$postal_local = explode(" ", $ediElement[3][2]);
$postal_code = $postal_local[0];
$locality = $postal_local[1];
$country_code = $ediElement[3][3];
} else if ($ediElement[1] == 'BY') {
$billing_address = $ediElement[5];
$billing_city = $ediElement[6];
$billing_company = $ediElement[4];
$billing_zip_code = $ediElement[8];
};
break;
// Name that we are going to use in profile.
case 'CTA':
$name = explode(" ", $ediElement[2][1]);
$given_name = $name[0];
$family_name = $name[1];
break;
case 'TAX':
if (!isset($tax)) {
$tax = $ediElement[5][3] / 100;
break;
}
// Name of shipping method.
case 'ALC':
$shipping_method_name = $ediElement[2];
break;
case 'MOA':
if($ediElement[1][0] == '8') {
$shipping_price = $ediElement[1][1];
}
// Line item variation.
case 'PIA':
if ($ediElement[1] == '5') {
$sku = $ediElement[2][0];
$rawProductVariation = $this->entityTypeManager()
->getStorage('commerce_product_variation')
->loadByProperties([
'sku' => $sku,
]);
$productVariation = reset($rawProductVariation);
$lineItems[$productVariation->getTitle()]['variation'] = $productVariation;
}
break;
// Line item quantity.
case 'QTY':
$quantity = $ediElement[1][1];
$lineItems[$productVariation->getTitle()]['quantity'] = $quantity;
break;
// Line item price.
case 'PRI':
if ($ediElement[1][0] == 'AAA') {
$price = $ediElement[1][1];
$lineItems[$productVariation->getTitle()]['price'] = $price;
}
break;
// Number of line items.
case 'CNT':
$numberOfLineItems = $ediElement[1][1];
break;
}
}
// Creating order item for each line item.
foreach ($lineItems as $lineItem) {
$orderItem = OrderItem::create([
'type' => 'default',
'purchased_entity' => $lineItem['variation'],
'quantity' => $lineItem['quantity'],
'unit_price' => new Price($lineItem['price'], $currency),
'overridden_unit_price' => TRUE,
]);
$orderItem->save();
$orderItems[] = $orderItem;
}
// Creating order.
$order = Order::create([
'type' => 'default',
'mail' => $this->currentUser()->getEmail(),
'uid' => $this->currentUser()->id(),
'store_id' => 1,
'order_items' => [$orderItems[0], $orderItems[1]],
'placed' => \Drupal::time()->getCurrentTime(),
]);
$order->save();
// Creating shipment.
if ($order->get('shipments')->count() == 0) {
$first_shipment = Shipment::create([
'type' => 'default',
'order_id' => $order->id(),
'title' => 'Shipment',
'state' => 'ready'
]);
$first_shipment->save();
}
foreach ($orderItems as $order_item) {
$quantity = $order_item->getQuantity();
$purchased_entity = $order_item->getPurchasedEntity();
if ($purchased_entity->get('weight')->isEmpty()) {
$weight = new Weight(1, WeightUnit::GRAM);
} else {
$weight_item = $purchased_entity->get('weight')->first();
$weight = $weight_item->toMeasurement();
}
// Creating shipment item.
$shipment_item = new ShipmentItem([
'order_item_id' => $order_item ->id(),
'title' => $purchased_entity->label(),
'quantity' => $quantity,
'weight' => $weight->multiply($quantity),
'declared_value' => $order_item->getTotalPrice(),
]);
$first_shipment->addItem($shipment_item);
}
// Creating shipping method.
$shipping_method = ShippingMethod::create([
'stores' => 1,
'name' => $shipping_method_name,
'plugin' => [
'target_plugin_id' => 'flat_rate',
'target_plugin_configuration' => [
'rate_label' =>'Shipping GG',
'rate_amount' => [
'number' => $shipping_price, //TODO: Zameniti sa varijablom
'currency_code' => $currency
]
],
],
'status' => TRUE,
'weight' => 0,
]);
$shipping_method->save();
// Adding newly created shipment method to shipment.
$first_shipment->setShippingMethod($shipping_method);
// Adding amount to shipment.
$shipping_method_plugin = $shipping_method->getPlugin();
$shipping_rates = $shipping_method_plugin->calculateRates($first_shipment);
$shipping_rate = reset($shipping_rates);
$shipping_service = $shipping_rate->getService()->getId();
$amount = reset($shipping_rates)->getAmount();
$first_shipment->setAmount($amount);
$first_shipment->save();
$order->set('shipments', [$first_shipment]);
$order->save();
// Creating profile using data from EDI file.
$profile = Profile::create([
'uid' => $order->getCustomerId(),
'type' => 'customer',
]);
$profile->address->given_name = $given_name;
$profile->set('field_address_1', $name);
$profile->address->family_name = $family_name;
$profile->address->organization = $organization;
$profile->set('field_company', $organization);
$profile->address->country_code = $country_code;
$profile->set('field_new_country', $country_code);
$profile->address->locality = $locality;
$profile->set('field_city', $locality);
$profile->address->postal_code = $postal_code;
$profile->set('field_zip_code', $postal_code);
$profile->address->address_line1 = $address_line1;
$profile->set('field_address_1', $address_line1);
$profile->save();
// Create a billing profile.
if(empty($order->getBillingProfile())) {
$billing_profile = Profile::create([
'type' => 'customer',
'uid' => $order->getCustomerId(),
]);
$billing_profile->save();
$billing_profile->set('field_address_1', $billing_address);
$billing_profile->set('field_city', $billing_city);
$billing_profile->set('field_company', $billing_company);
$billing_profile->set('field_zip_code', $billing_zip_code);
$billing_profile->save();
$order->setBillingProfile($billing_profile);
$order->save();
}
//TODO: Add other data to profile.
$first_shipment->setShippingProfile($profile);
$first_shipment->setShippingService($shipping_service);
$first_shipment->save();
$order->set('shipments', $first_shipment);
// Including tax into price.
$priceWithTax = $order->getTotalPrice()->getNumber() + ($order->getTotalPrice()->getNumber() * $tax);
$order->set('total_price', new Price($priceWithTax, $currency));
$order->save();
// Checking the number of line items.
if ($numberOfLineItems == count($orderItems)) {
$poruka = 'Provera prošla';
} else {
$poruka = 'Provera nije prošla';
}
return [
'#markup' => $poruka,
];
}
}



Comments