Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- function custom_commerce_order_presave($order) {
- if(user_is_logged_in() && $order->status == 'pending'){
- $params = _getSAPOrder($order);
- $wsdlFile = '/var/www/html/sites/all/modules/custom/_custom/wsdl/managesalesorderin5.wsdl';
- dsm($params);
- $soapClient = new SoapClient($wsdlFile, array(
- 'login' => '_WEBSHOP',
- 'password' => 'Welcome1',
- 'encoding' => 'UTF-8',
- 'location' => 'https://my338553.sapbydesign.com/sap/bc/srt/scs/sap/managesalesorderin5',
- 'trace' => 1,
- 'cache_wsdl' => WSDL_CACHE_NONE,
- 'style' => SOAP_DOCUMENT,
- 'use' => SOAP_ENC_ARRAY
- ));
- $result = $soapClient->MaintainBundle($params);
- dsm($result);
- }
- }
- /**
- * Transform order from SHOPIFY to SAP format.
- *
- * @param $order
- * @param $shop
- *
- * @return Array - Order to SAP
- */
- function _getSAPOrder($order){
- $currency = $order->commerce_order_total['und'][0]['currency_code'];
- $payments = commerce_payment_transaction_load_multiple(array(), array('order_id' => $order->order_id));
- $payment = !empty($payments) ? array_shift($payments) : NULL;
- switch ($currency) {
- case 'USD':
- $BillToParty = '1000197'; //Ecommerce US
- $SellerParty = '2000'; //For webshop USA
- $SalesUnitParty = 'DS2100'; // Sale of products by US webshop
- $PaymentProcessingCompanyID = '2000'; //PIQ USA Inc
- $HouseBankAccountKeyInternalID = 'STRIPE_USD';
- break;
- case 'EUR':
- $BillToParty = '1000195'; //Ecommerce EUR
- $SellerParty = '1000'; //For Webshop UK and EUR
- $SalesUnitParty = 'DS1100'; //Sale of products by France webshop
- $PaymentProcessingCompanyID = '1000'; //For company PIQ SAS
- $HouseBankAccountKeyInternalID = 'STRIPE_EUR';
- break;
- case 'GBP':
- $BillToParty = '1000196'; //Ecommerce GB
- $SellerParty = '1000'; //For Webshop UK and EUR
- $SalesUnitParty = 'DS1200'; // sale of products by UK webshop
- $PaymentProcessingCompanyID = '1000'; //For company PIQ SAS
- $HouseBankAccountKeyInternalID = '';
- break;
- }
- $billing = commerce_customer_profile_load($order->commerce_customer_billing['und'][0]['profile_id']);
- $billing = $billing->commerce_customer_address['und'][0];
- $orderSAP = [
- 'BasicMessageHeader' => [],
- 'SalesOrder' => [
- 'BuyerID' => $order->order_number,//!!!!!!
- 'PostingDate' => date('Y-m-d\TH:i:sP',$order->created),
- 'DataOriginTypeCode' => '4',
- 'SalesAndServiceBusinessArea' => [
- 'DistributionChannelCode' => 'Z1',
- ],
- 'BillToParty' => [
- 'PartyID' => $BillToParty,
- 'Address' => [
- 'Email' => [
- 'URI' => $order->mail,
- ],
- 'PostalAddress' => [
- 'CountryCode' => $billing['country'],
- 'CityName' => $billing['locality'],
- 'StreetPostalCode' => $billing['postal_code'],
- 'StreetName' => $billing['thoroughfare'],
- 'HouseID' => ''
- ],
- 'DisplayName' => [
- 'FormattedName' => $billing['last_name'] . " " . $billing['first_name']
- ]
- ]
- ],
- 'AccountParty' => [
- 'PartyID' => $BillToParty,
- 'Address' => [
- 'Email' => [
- 'URI' => $order->mail,
- ],
- 'PostalAddress' => [
- 'CountryCode' => $billing['country'],
- 'CityName' => $billing['locality'],
- 'StreetPostalCode' => $billing['postal_code'],
- 'StreetName' => $billing['thoroughfare'],
- 'HouseID' => ''
- ],
- 'DisplayName' => [
- 'FormattedName' => $billing['last_name'] . " " . $billing['first_name']
- ]
- ]
- ],
- 'ProductRecipientParty' => [
- 'PartyID' => $BillToParty,//??,
- 'Address' => [
- 'Email' => [
- 'URI' => $order->mail,
- ],
- 'PostalAddress' => [
- 'CountryCode' => $billing['country'],
- 'CityName' => $billing['locality'],
- 'StreetPostalCode' => $billing['postal_code'],
- 'StreetName' => $billing['thoroughfare'],
- 'HouseID' => ''
- ],
- 'DisplayName' => [
- 'FormattedName' => $billing['last_name'] . " " . $billing['first_name']
- ]
- ],
- ],
- 'EmployeeResponsibleParty' => [
- 'PartyID' => '8000000001 ',
- ],
- 'SellerParty' => [
- 'PartyID' => $SellerParty,
- ],
- 'SalesUnitParty' => [
- 'PartyID' => $SalesUnitParty,
- ],
- 'DeliveryTerms' => [
- 'DeliveryPriorityCode' => '2',
- 'Incoterms' => [
- 'ClassificationCode' => 'DDP',
- 'TransferLocationName' => $billing['locality'],///?????????????
- ]
- ],
- 'PricingTerms' => [
- 'CurrencyCode' => $currency,
- 'GrossAmountIndicator' => 1,
- ]
- ]
- ];
- $itemId = 10;
- foreach ($order->commerce_line_items['und'] as $id => $item) {
- $item = commerce_line_item_load($item['line_item_id']);
- if(in_array($item->type,array('shipping','discount'))){
- continue;
- }
- $product = commerce_product_load($item->commerce_product['und'][0]['product_id']);
- $orderSAP['SalesOrder']['Item'][] = [
- 'ID' => $itemId,
- 'BuyerID' => $order->order_number,//!!!!!,
- 'ItemProduct' => [
- 'ProductID' => $product->field_piq_internal_code['und'][0]['value']
- ],
- 'ItemScheduleLine' => [
- 'Quantity' => [
- '_' => '1',
- 'unitCode' => 'EA',
- ]
- ]
- ];
- $itemId = $itemId + 10;
- }
- /*
- $orderSAP['SalesOrder']['CashDiscountTerms'] = [
- 'Code' => [
- '_' => '1001',
- 'listAgencyID' => ''
- ]
- ];
- */
- $orderSAP['SalesOrder']['PaymentControl'] = [
- '_' => [
- 'PaymentProcessingCompanyID' => $PaymentProcessingCompanyID,
- 'PaymentReferenceID' => $payment->transaction_id,
- 'PaymentReferenceTypeCode' => '5',
- 'PropertyMovementDirectionCode' => '2',
- 'PaymentFormCode' => '20',
- 'PaymentAmount' => [
- '_' => $order->commerce_order_total['und'][0]['amount']/100,
- 'currencyCode' => $currency,
- ],
- 'ExternalPayment' => [
- 'HouseBankAccountKeyInternalID' => $HouseBankAccountKeyInternalID,
- 'PaymentTransactionReferenceID' => $payment->remote_id,
- 'DocumentDate' => date('Y-m-d',$payment->created),
- 'PaymentReferenceID' => $payment->transaction_id,
- 'ValueDate' => date('Y-m-d',$payment->created),
- 'Amount' => [
- '_' => $order->commerce_order_total['und'][0]['amount']/100,
- 'currencyCode' => $currency,
- ]
- ],
- ],
- 'actionCode' => ' ',//????
- ];
- $object = json_decode(json_encode((object) $orderSAP), TRUE);
- return $object;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement