Advertisement
kervi

Untitled

Dec 2nd, 2016
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.96 KB | None | 0 0
  1. <?php
  2.  
  3. function custom_commerce_order_presave($order) {
  4.   if(user_is_logged_in() && $order->status == 'pending'){
  5.  
  6.     $params = _getSAPOrder($order);
  7.     $wsdlFile = '/var/www/html/sites/all/modules/custom/_custom/wsdl/managesalesorderin5.wsdl';
  8.     dsm($params);
  9.     $soapClient = new SoapClient($wsdlFile, array(
  10.       'login'             => '_WEBSHOP',
  11.       'password'          => 'Welcome1',
  12.       'encoding'          => 'UTF-8',
  13.       'location'          => 'https://my338553.sapbydesign.com/sap/bc/srt/scs/sap/managesalesorderin5',
  14.       'trace'             => 1,
  15.       'cache_wsdl'        => WSDL_CACHE_NONE,
  16.       'style'             => SOAP_DOCUMENT,
  17.       'use'               => SOAP_ENC_ARRAY
  18.     ));
  19.     $result = $soapClient->MaintainBundle($params);
  20.     dsm($result);
  21.   }
  22. }
  23.  
  24.  
  25. /**
  26.  * Transform order from SHOPIFY to SAP format.
  27.  *
  28.  * @param $order
  29.  * @param $shop
  30.  *
  31.  * @return Array - Order to SAP
  32.  */
  33. function _getSAPOrder($order){
  34.  
  35.     $currency = $order->commerce_order_total['und'][0]['currency_code'];
  36.  
  37.     $payments = commerce_payment_transaction_load_multiple(array(), array('order_id' =>  $order->order_id));
  38.     $payment = !empty($payments) ? array_shift($payments) : NULL;
  39.  
  40.     switch ($currency) {
  41.       case 'USD':
  42.         $BillToParty = '1000197'; //Ecommerce US
  43.         $SellerParty  = '2000'; //For webshop USA
  44.         $SalesUnitParty  = 'DS2100'; // Sale of products by US webshop
  45.         $PaymentProcessingCompanyID = '2000'; //PIQ USA Inc
  46.         $HouseBankAccountKeyInternalID = 'STRIPE_USD';
  47.         break;
  48.       case 'EUR':
  49.         $BillToParty = '1000195'; //Ecommerce EUR
  50.         $SellerParty  = '1000'; //For  Webshop UK and EUR
  51.         $SalesUnitParty  = 'DS1100'; //Sale of products by France webshop
  52.         $PaymentProcessingCompanyID = '1000'; //For company PIQ SAS
  53.         $HouseBankAccountKeyInternalID = 'STRIPE_EUR';
  54.         break;
  55.       case 'GBP':
  56.         $BillToParty = '1000196'; //Ecommerce GB
  57.         $SellerParty  = '1000'; //For  Webshop UK and EUR
  58.         $SalesUnitParty  = 'DS1200'; // sale of products by UK webshop
  59.         $PaymentProcessingCompanyID = '1000'; //For company PIQ SAS
  60.         $HouseBankAccountKeyInternalID = '';
  61.         break;
  62.     }
  63.  
  64.     $billing = commerce_customer_profile_load($order->commerce_customer_billing['und'][0]['profile_id']);
  65.     $billing = $billing->commerce_customer_address['und'][0];
  66.  
  67.     $orderSAP = [
  68.         'BasicMessageHeader' => [],
  69.         'SalesOrder' => [
  70.             'BuyerID' => $order->order_number,//!!!!!!
  71.             'PostingDate' => date('Y-m-d\TH:i:sP',$order->created),
  72.             'DataOriginTypeCode' => '4',
  73.             'SalesAndServiceBusinessArea' => [
  74.                 'DistributionChannelCode' => 'Z1',
  75.             ],
  76.             'BillToParty' => [
  77.                 'PartyID' => $BillToParty,
  78.                 'Address' => [
  79.                     'Email' => [
  80.                         'URI' => $order->mail,
  81.                     ],
  82.                     'PostalAddress' => [
  83.                         'CountryCode' => $billing['country'],
  84.                         'CityName' => $billing['locality'],
  85.                         'StreetPostalCode' => $billing['postal_code'],
  86.                         'StreetName' => $billing['thoroughfare'],
  87.                         'HouseID' => ''
  88.                     ],
  89.                     'DisplayName' => [
  90.                         'FormattedName' => $billing['last_name'] . " " . $billing['first_name']
  91.                     ]
  92.                 ]
  93.             ],
  94.             'AccountParty' => [
  95.                 'PartyID' => $BillToParty,
  96.                 'Address' => [
  97.                     'Email' => [
  98.                         'URI' => $order->mail,
  99.                     ],
  100.                     'PostalAddress' => [
  101.                         'CountryCode' => $billing['country'],
  102.                         'CityName' => $billing['locality'],
  103.                         'StreetPostalCode' => $billing['postal_code'],
  104.                         'StreetName' => $billing['thoroughfare'],
  105.                         'HouseID' => ''
  106.                     ],
  107.                     'DisplayName' => [
  108.                         'FormattedName' => $billing['last_name'] . " " . $billing['first_name']
  109.                     ]
  110.                 ]
  111.             ],
  112.             'ProductRecipientParty' => [
  113.                 'PartyID' => $BillToParty,//??,
  114.                 'Address' => [
  115.                     'Email' => [
  116.                         'URI' => $order->mail,
  117.                     ],
  118.                     'PostalAddress' => [
  119.                         'CountryCode' => $billing['country'],
  120.                         'CityName' => $billing['locality'],
  121.                         'StreetPostalCode' => $billing['postal_code'],
  122.                         'StreetName' => $billing['thoroughfare'],
  123.                         'HouseID' => ''
  124.                     ],
  125.                     'DisplayName' => [
  126.                         'FormattedName' => $billing['last_name'] . " " . $billing['first_name']
  127.                     ]
  128.                 ],
  129.             ],
  130.             'EmployeeResponsibleParty' => [
  131.                 'PartyID' => '8000000001 ',
  132.             ],
  133.             'SellerParty' => [
  134.                 'PartyID' => $SellerParty,
  135.             ],
  136.             'SalesUnitParty' => [
  137.                 'PartyID' => $SalesUnitParty,
  138.             ],
  139.             'DeliveryTerms' => [
  140.                 'DeliveryPriorityCode' => '2',
  141.                 'Incoterms' => [
  142.                     'ClassificationCode' => 'DDP',
  143.                     'TransferLocationName' => $billing['locality'],///?????????????
  144.                 ]
  145.             ],
  146.             'PricingTerms' => [
  147.                 'CurrencyCode' => $currency,
  148.                 'GrossAmountIndicator' => 1,
  149.             ]
  150.         ]
  151.     ];
  152.  
  153.  
  154.     $itemId = 10;
  155.  
  156.     foreach ($order->commerce_line_items['und'] as $id => $item) {
  157.       $item = commerce_line_item_load($item['line_item_id']);
  158.  
  159.  
  160.       if(in_array($item->type,array('shipping','discount'))){
  161.         continue;
  162.       }
  163.       $product = commerce_product_load($item->commerce_product['und'][0]['product_id']);
  164.       $orderSAP['SalesOrder']['Item'][] = [
  165.           'ID' => $itemId,
  166.           'BuyerID' => $order->order_number,//!!!!!,
  167.           'ItemProduct' => [
  168.               'ProductID' => $product->field_piq_internal_code['und'][0]['value']
  169.           ],
  170.           'ItemScheduleLine' => [
  171.               'Quantity' => [
  172.                   '_' => '1',
  173.                   'unitCode' => 'EA',
  174.               ]
  175.           ]
  176.       ];
  177.       $itemId = $itemId + 10;
  178.     }
  179.     /*
  180.     $orderSAP['SalesOrder']['CashDiscountTerms'] = [
  181.         'Code' => [
  182.             '_' => '1001',
  183.             'listAgencyID' => ''
  184.         ]
  185.     ];
  186.     */
  187.     $orderSAP['SalesOrder']['PaymentControl'] = [
  188.         '_' => [
  189.             'PaymentProcessingCompanyID' => $PaymentProcessingCompanyID,
  190.             'PaymentReferenceID' => $payment->transaction_id,
  191.             'PaymentReferenceTypeCode' => '5',
  192.             'PropertyMovementDirectionCode' => '2',
  193.             'PaymentFormCode' => '20',
  194.             'PaymentAmount' => [
  195.                 '_' => $order->commerce_order_total['und'][0]['amount']/100,
  196.                 'currencyCode' => $currency,
  197.             ],
  198.             'ExternalPayment' => [
  199.                 'HouseBankAccountKeyInternalID' => $HouseBankAccountKeyInternalID,
  200.                 'PaymentTransactionReferenceID' => $payment->remote_id,
  201.                 'DocumentDate' =>  date('Y-m-d',$payment->created),
  202.                 'PaymentReferenceID' => $payment->transaction_id,
  203.                 'ValueDate' => date('Y-m-d',$payment->created),
  204.                 'Amount' => [
  205.                     '_' => $order->commerce_order_total['und'][0]['amount']/100,
  206.                     'currencyCode' => $currency,
  207.                 ]
  208.             ],
  209.         ],
  210.         'actionCode' => ' ',//????
  211.     ];
  212.     $object = json_decode(json_encode((object) $orderSAP), TRUE);
  213.     return $object;
  214. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement