<?php

use DHL\Entity\AM\ShipmentResponse;
use DHL\Entity\AM\ShipmentRequest;
use DHL\Client\Web as WebserviceClient;
use DHL\Datatype\AM\Piece;
use DHL\Datatype\AM\SpecialService;

require(__DIR__ . '/../../init.php');


class DHL_Shipment {
    private $shpmentRequest;
    private $dhl;
    private $config;
    function __construct(){
        $this->config = require(__DIR__ . '/../../conf/config.php');
        $this->shpmentRequest = new ShipmentRequest();
        $this->dhl = $this->config['dhl'];
    }
   
    public function generateShipment(array $params) {

        $this->shpmentRequest->MessageTime = date('Y-m-d\TH:i:s\Z');
        $this->shpmentRequest->MessageReference = $this->generateMsnRef('1234567890123456789012345678901');
        $this->shpmentRequest->ShippingSystemPlatform = $this->dhl['ShippingSystemPlatform'];
        $this->shpmentRequest->ShippingSystemPlatformVersion = $this->dhl['SoftwareVersion'];
        
        $this->shpmentRequest->Username = $this->dhl['username'];
        $this->shpmentRequest->Password = $this->dhl['pass'];

        $this->shpmentRequest->ShipmentInfo->DropOffType = $this->dhl['DropOffType'];
        $this->shpmentRequest->ShipmentInfo->ServiceType = 'P';
        $this->shpmentRequest->ShipmentInfo->Billing->ShippingPaymentType = $params['ShippingPaymentType'];//'S'; /* S => Shipper R => Recipient */
        $this->shpmentRequest->ShipmentInfo->Billing->ShipperAccountNumber = $this->dhl['shipperAccountNumber'];
        $this->shpmentRequest->ShipmentInfo->Billing->BillingAccountNumber = $this->dhl['billingAccountNumber'];
        
        $this->shpmentRequest->ShipmentInfo->SpecialServices->Service->ServiceType = $this->dhl['ServiceType'];
        $this->shpmentRequest->ShipmentInfo->Currency = 'USD';
        $this->shpmentRequest->ShipmentInfo->UnitOfMeasurement = 'SI';
        $this->shpmentRequest->ShipmentInfo->LabelType = 'PDF';
        $this->shpmentRequest->ShipmentInfo->LabelOptions->RequestDHLCustomsInvoice = 'Y';
        $this->shpmentRequest->ShipmentInfo->LabelOptions->DHLCustomsInvoiceLanguageCode = $this->dhl['LanguageCode'];
        $this->shpmentRequest->ShipmentInfo->LabelOptions->DHLCustomsInvoiceType = 'COMMERCIAL_INVOICE';
        
        $this->shpmentRequest->ShipmentInfo->ShipmentReferences->ShipmentReference->ShipmentReference = $params['content'];
        $this->shpmentRequest->ShipTimestamp =  date('Y-m-d\TH:i:s \G\M\T+01:00',strtotime(date('Y-m-d\TH:i:s'). ' + 1 days'));
        
        $this->shpmentRequest->PaymentInfo = 'DAP';

        $this->shpmentRequest->InternationalDetail->Commodities->Description = $params['content'];
        $this->shpmentRequest->InternationalDetail->Commodities->CustomsValue = '671';
        $this->shpmentRequest->InternationalDetail->Content = 'NON_DOCUMENTS';
        $this->shpmentRequest->InternationalDetail->ExportDeclaration->InvoiceDate = date('Y-m-d');
        $this->shpmentRequest->InternationalDetail->ExportDeclaration->InvoiceNumber = $params['factura'];
        $this->shpmentRequest->InternationalDetail->ExportDeclaration->ExportReasonType = 'PERMANENT';
        $this->shpmentRequest->InternationalDetail->ExportDeclaration->ExportReason = 'Commercial';
        $this->shpmentRequest->InternationalDetail->ExportDeclaration->PlaceOfIncoterm = 'COLOMBIA';
        $this->shpmentRequest->InternationalDetail->ExportDeclaration->ShipmentPurpose = 'COMMERCIAL';
        $this->shpmentRequest->InternationalDetail->ExportDeclaration->DocumentFunction = 'EXPORT';
        $this->shpmentRequest->InternationalDetail->ExportDeclaration->ExportLineItems->ExportLineItem->ItemNumber = '1';
        $this->shpmentRequest->InternationalDetail->ExportDeclaration->ExportLineItems->ExportLineItem->Quantity = '1';
        $this->shpmentRequest->InternationalDetail->ExportDeclaration->ExportLineItems->ExportLineItem->QuantityUnitOfMeasurement = 'PCS';
        $this->shpmentRequest->InternationalDetail->ExportDeclaration->ExportLineItems->ExportLineItem->ItemDescription = $params['item']['descripcion'];
        $this->shpmentRequest->InternationalDetail->ExportDeclaration->ExportLineItems->ExportLineItem->UnitPrice = '39.20';
        $this->shpmentRequest->InternationalDetail->ExportDeclaration->ExportLineItems->ExportLineItem->NetWeight = '0.2';
        $this->shpmentRequest->InternationalDetail->ExportDeclaration->ExportLineItems->ExportLineItem->GrossWeight = '0.2';
        $this->shpmentRequest->InternationalDetail->ExportDeclaration->ExportLineItems->ExportLineItem->ExportReasonType = 'PERMANENT';
        $this->shpmentRequest->InternationalDetail->ExportDeclaration->ExportLineItems->ExportLineItem->ManufacturingCountryCode = 'US';
        $this->shpmentRequest->InternationalDetail->ExportDeclaration->ExportLineItems->ExportLineItem->TaxesPaid = 'N';
        
        $this->shpmentRequest->InternationalDetail->ExportDeclaration->CustomsDocuments->CustomsDocument->CustomsDocumentType = 'INV';
        $this->shpmentRequest->InternationalDetail->ExportDeclaration->CustomsDocuments->CustomsDocument->CustomsDocumentID = 'MyDHLAPI - CUSDOC-001';
        $this->shpmentRequest->InternationalDetail->ExportDeclaration->PackageMarks = ' ';
        //$this->shpmentRequest->InternationalDetail->ExportDeclaration->PayerGSTVAT = 'no';

        $this->shpmentRequest->InternationalDetail->ExportDeclaration->Remarks->Remark->RemarkDescription = $params['content'];
        $this->shpmentRequest->InternationalDetail->ExportDeclaration->TermsOfPayment = '';
        $this->shpmentRequest->InternationalDetail->ExportDeclaration->InvoiceInstructions = 'InvoiceInstructions';
        $this->shpmentRequest->InternationalDetail->ExportDeclaration->InvoiceReferences->InvoiceReference->InvoiceReferenceType = 'CU';
        $this->shpmentRequest->InternationalDetail->ExportDeclaration->InvoiceReferences->InvoiceReference->InvoiceReferenceNumber = $params['factura'];
       
        $this->shpmentRequest->Ship->Shipper->Contact->CompanyName = $this->dhl['CompanyName'];
        $this->shpmentRequest->Ship->Shipper->Contact->PersonName = $this->dhl['PersonName'];
        $this->shpmentRequest->Ship->Shipper->Contact->PhoneNumber = $this->dhl['PhoneNumber'];
        
        $this->shpmentRequest->Ship->Shipper->Address->addStreetLines($this->dhl['AddressLine1']);
        $this->shpmentRequest->Ship->Shipper->Address->City = $this->dhl['City'];
        $this->shpmentRequest->Ship->Shipper->Address->PostalCode = $this->dhl['PostalCode'];
        $this->shpmentRequest->Ship->Shipper->Address->CountryCode = $this->dhl['CountryCode'];
        //$this->shpmentRequest->Ship->Shipper->BusinessPartyTypeCode = 'BU';

        
        $this->shpmentRequest->Ship->Recipient->Contact->PersonName = $params['recipient']['PersonName'];
        $this->shpmentRequest->Ship->Recipient->Contact->CompanyName = $params['recipient']['CompanyName'];
        $this->shpmentRequest->Ship->Recipient->Contact->PhoneNumber = $params['recipient']['PhoneNumber'];
        $this->shpmentRequest->Ship->Recipient->Contact->EmailAddress = $params['recipient']['Email'];
        $this->shpmentRequest->Ship->Recipient->Contact->MobilePhoneNumber = $params['recipient']['PhoneNumber'];
        
        $this->shpmentRequest->Ship->Recipient->Address->addStreetLines($params['recipient']['AddressLine1']);
        $this->shpmentRequest->Ship->Recipient->Address->addStreetLines2($params['recipient']['AddressLine2']);
        $this->shpmentRequest->Ship->Recipient->Address->addStreetLines3($params['recipient']['AddressLine3']);
        $this->shpmentRequest->Ship->Recipient->Address->City = $params['recipient']['City'];
        $this->shpmentRequest->Ship->Recipient->Address->PostalCode = $params['recipient']['PostalCode'];
        $this->shpmentRequest->Ship->Recipient->Address->CountryCode = $params['recipient']['CountryCode'];
        $this->shpmentRequest->Packages->RequestedPackages->addAtributte('number','1');
        $this->shpmentRequest->Packages->RequestedPackages->Weight = '0.4';
        $this->shpmentRequest->Packages->RequestedPackages->PackageContentDescription = $this->dhl['CountryCode'];
        $this->shpmentRequest->Packages->RequestedPackages->Dimensions->Length = '30';
        $this->shpmentRequest->Packages->RequestedPackages->Dimensions->Width = '30';
        $this->shpmentRequest->Packages->RequestedPackages->Dimensions->Height = '30';
        
        $start = microtime(true);
        
        $client = new WebserviceClient('staging');
        $data = $client->call($this->shpmentRequest);
        
        
        return json_encode($data);
        
    }
    private function generateMsnRef($text)
    {
        $count = strlen($text);
        $output = null;
        
            for($i=0; $i < 30 - $count; $i++) {
                $output .= rand(1,9);
            }
        
         return strtoupper ($text).'-'.$output;        
    }
}
 /*$n = new DHL_Shipment();
 $params =array(
     'recipient' => array(
         'PersonName' => 'Yunjie Wang WUMTJ',
         'CompanyName' => 'Yunjie Wang WUMTJ',
         'AddressLine1'=>'261 Quigley Boulevard',
         'AddressLine2'=>'Suite 1 # 525953',
         'AddressLine3'=>'.',
         'City' => 'New Castle Delaware',
         'PostalCode' => '19720-4187',
         'CountryCode' => 'US',
         'CountryName' => 'Usa',
         'PhoneNumber' => '+19123735448',
         'PhoneExtension' => '123',
         'FaxNumber' => '+19123735448', 
         'Telex' => '+19123735448',
         'Email' => 'kidwell.lauren@gmail.com-233500113575b.ct.vtex.com',
     ),
     'item'=> array(
        'descripcion' => 'VESTIDO DE BAÑO ODM'
     ), 
     'content' => '8464-ODM_USA',
     'ShippingPaymentType' => 'S',
     'factura' => '002-0001293123'
 );
 print_r($n->generateShipment($params));
 */


