DivideBV / Postnl

Library to connect to PostNL's SOAP service called CIF
GNU General Public License v2.0
31 stars 40 forks source link

Multi collo shipment not working new API #72

Closed vkas002 closed 5 years ago

vkas002 commented 5 years ago

Hello,

I am using your POSTNL API for almost an year to generate multiple labels in a single page (Basically 4 labels in one pdf) which was working successfully. Recently I updated the code so that i can upgrade to the new POSTNL API. After updating the code (using the lastest vserion of DivideBV/Postnl ) i am not able to generate multiple column shipment labels.

Below i have shared code. Can you please tell me what is the issue? Thank you.

        $client = new Postnl(
            000000 ,   // Customer number
            'TSRV',     // Customer code
            'Customer name',  // Customer name
            'API key', // Password
            111111,     // Collection location
            'CV0781',   // Globalpack
            false        // Whether to use PostNL's sandbox environment.
        );

        $product_code_delivery = "3085";

        $senderAddress = ComplexTypes\Address::create();
        // fill in address

        $num_collo = 4;
        $package_count = 4;

        $receiverAddress1 = ComplexTypes\Address::create();

        $receiverAddress2 = ComplexTypes\Address::create();

        $receiverAddress3 = ComplexTypes\Address::create();

        $receiverAddress4 = ComplexTypes\Address::create();

        // Parcel 1 of 4.
        $barcode1 = $client->generateBarcodeByDestination($receiverAddress1->getCountrycode());

        $shipment1 = ComplexTypes\Shipment::create()
            ->setBarcode($barcode1)
            ->setDimension(ComplexTypes\Dimension::create()
                ->setWeight(5000) // Weight in g
                ->setWidth(215)   // Width in mm
                ->setLength(305)  // Length in mm
                ->setHeight(280)  // Height in mm
            )
            ->setProductCodeDelivery($product_code_delivery)
            ->setAddresses(new ComplexTypes\ArrayOfAddress([
                $receiverAddress1,
                $senderAddress,
            ]));
        // Set product code, dimension, etc...
        $group1 = ComplexTypes\Group::create()
            ->setGroupSequence(1)
            ->setGroupCount($num_collo)
            ->setGroupType('01')
            ->setMainBarcode($barcode1);
        $shipment1->setGroups(new ComplexTypes\ArrayOfGroup([$group1]));

        // Parcel 2 of 4.
        $barcode2 = $client->generateBarcodeByDestination($receiverAddress2->getCountrycode());
        $shipment2 = ComplexTypes\Shipment::create()
            ->setBarcode($barcode2)
            ->setDimension(ComplexTypes\Dimension::create()
                ->setWeight(5000) // Weight in g
                ->setWidth(215)   // Width in mm
                ->setLength(305)  // Length in mm
                ->setHeight(280)  // Height in mm
            )
            ->setProductCodeDelivery($product_code_delivery)
            ->setAddresses(new ComplexTypes\ArrayOfAddress([
                clone $receiverAddress2,
                clone $senderAddress,
            ]));
        // Set product code, address, dimension, etc...
        $group2 = ComplexTypes\Group::create()
            ->setGroupSequence(2)
            ->setGroupCount($num_collo)
            ->setGroupType('01')
            ->setMainBarcode($barcode2);
        $shipment2->setGroups(new ComplexTypes\ArrayOfGroup([$group2]));

        // Parcel 3 of 4.
        $barcode3 = $client->generateBarcodeByDestination($receiverAddress3->getCountrycode());
        $shipment3 = ComplexTypes\Shipment::create()
            ->setBarcode($barcode3)
            ->setDimension(ComplexTypes\Dimension::create()
                ->setWeight(5000) // Weight in g
                ->setWidth(215)   // Width in mm
                ->setLength(305)  // Length in mm
                ->setHeight(280)  // Height in mm
            )
            ->setProductCodeDelivery($product_code_delivery)
            ->setAddresses(new ComplexTypes\ArrayOfAddress([
                clone $receiverAddress3,
                clone $senderAddress,
            ]));
        // Set product code, address, dimension, etc...
        $group3 = ComplexTypes\Group::create()
            ->setGroupSequence(3)
            ->setGroupCount($num_collo)
            ->setGroupType('01')
            ->setMainBarcode($barcode3);
        $shipment3->setGroups(new ComplexTypes\ArrayOfGroup([$group3]));

        // Parcel 4 of 4.
        $barcode4 = $client->generateBarcodeByDestination($receiverAddress4->getCountrycode());
        $shipment4 = ComplexTypes\Shipment::create()
            ->setBarcode($barcode4)
            ->setDimension(ComplexTypes\Dimension::create()
                ->setWeight(5000) // Weight in g
                ->setWidth(215)   // Width in mm
                ->setLength(305)  // Length in mm
                ->setHeight(280)  // Height in mm
            )
            ->setProductCodeDelivery($product_code_delivery)
            ->setAddresses(new ComplexTypes\ArrayOfAddress([
                clone $receiverAddress4,
                clone $senderAddress,
            ]));
        // Set product code, address, dimension, etc...
        $group4 = ComplexTypes\Group::create()
            ->setGroupSequence(4)
            ->setGroupCount($num_collo)
            ->setGroupType('01')
            ->setMainBarcode($barcode4);
        $shipment4->setGroups(new ComplexTypes\ArrayOfGroup([$group4]));

        $results = $client->generateLabels(new ComplexTypes\ArrayOfShipment([$shipment1,$shipment2,$shipment3,$shipment4]));

        $i=0;

        if(!empty($results)) {
            foreach ($results->getResponseShipments() as $result) {
                $file_n = $_SERVER['DOCUMENT_ROOT'] . '/controller/Export_History/EXPORT_DATA/LABELS/' . date('Y') . '/' . date('m') . '/' . date('d') . '/' . $i . '.pdf';
                $file = new \SplFileObject($file_n, 'w',true);
                $handle = fopen($file, "w");

                if(count($package_count) > 1) {
                    fwrite($handle, $results->getMergedLabels()->getMergedLabel()->getLabels()->getLabel()->getContent());
                }else{
                    fwrite($handle, $results->getResponseShipments()->getResponseShipment()->getLabels()->getLabel()->getContent());
                }

                fclose($handle);
            }
        }
ameenross commented 5 years ago

Do you get an error of any description?

vkas002 commented 5 years ago

No errors. In response, I am getting 4 different label PDF files and I am looking for all four in one PDF (This was working earlier for older version but not working for new API ).

                fwrite($handle, $results->getMergedLabels()->getMergedLabel()->getLabels()->getLabel()->getContent());  

In the above code I was getting 4 labels in one PDF using the Mergelabels attribute but now it contains NULL value.

ameenross commented 5 years ago

Can you call $client->debug() after the generateLabels call and share the output?

ameenross commented 5 years ago

Although I think I already know. The return value of getLabel is an array (with a single Label object). PHP SoapClient supports directly returning the object if it's just one, but it's not desirable and thus it's disabled in 2.x. The new code should simply be:

fwrite($handle, $results->getMergedLabels()->getMergedLabel()->getLabels()->getLabel()[0]->getContent());
vkas002 commented 5 years ago

fwrite($handle, $results->getMergedLabels()->getMergedLabel()->getLabels()->getLabel()[0]->getContent());

This doesn't seems to be working because in response the MergedLabels attribute contains NULL value.

Below i have shared by response.

object(DivideBV\Postnl\ComplexTypes\GenerateLabelResponse)#247 (2) { ["MergedLabels":protected]=> NULL ["ResponseShipments":protected]=> object(DivideBV\Postnl\ComplexTypes\ArrayOfResponseShipment)#248 (1) { ["ResponseShipment":protected]=> array(4) { [0]=> object(DivideBV\Postnl\ComplexTypes\ResponseShipment)#249 (7) { ["Barcode":protected]=> string(13) "3STSRV5090232" ["DownPartnerBarcode":protected]=> NULL ["DownPartnerID":protected]=> NULL ["DownPartnerLocation":protected]=> NULL ["Labels":protected]=> object(DivideBV\Postnl\ComplexTypes\ArrayOfLabel)#250 (1) { ["Label":protected]=> array(1) { [0]=> object(DivideBV\Postnl\ComplexTypes\Label)#251 (3) { ["Content":protected]=> string(14096) "%PDF-1.2 1 0 obj PDF 1 file (I have removed the pdf output. Here i am getting a single label in a pdf) %%EOF " ["Contenttype":protected]=> NULL ["Labeltype":protected]=> string(5) "Label" } } } ["ProductCodeDelivery":protected]=> string(4) "4944" ["Warnings":protected]=> object(DivideBV\Postnl\ComplexTypes\ArrayOfWarning)#252 (1) { ["Warning":protected]=> NULL } } [1]=> object(DivideBV\Postnl\ComplexTypes\ResponseShipment)#253 (7) { ["Barcode":protected]=> string(13) "3STSRV2927219" ["DownPartnerBarcode":protected]=> NULL ["DownPartnerID":protected]=> NULL ["DownPartnerLocation":protected]=> NULL ["Labels":protected]=> object(DivideBV\Postnl\ComplexTypes\ArrayOfLabel)#254 (1) { ["Label":protected]=> array(1) { [0]=> object(DivideBV\Postnl\ComplexTypes\Label)#255 (3) { ["Content":protected]=> string(14061) "%PDF-1.2 1 0 obj PDF 2 file (I have removed the pdf output. Here i am getting a single label in a pdf) %%EOF " ["Contenttype":protected]=> NULL ["Labeltype":protected]=> string(5) "Label" } } } ["ProductCodeDelivery":protected]=> string(4) "4944" ["Warnings":protected]=> object(DivideBV\Postnl\ComplexTypes\ArrayOfWarning)#256 (1) { ["Warning":protected]=> NULL } } [2]=> object(DivideBV\Postnl\ComplexTypes\ResponseShipment)#257 (7) { ["Barcode":protected]=> string(15) "3STSRV901903677" ["DownPartnerBarcode":protected]=> NULL ["DownPartnerID":protected]=> NULL ["DownPartnerLocation":protected]=> NULL ["Labels":protected]=> object(DivideBV\Postnl\ComplexTypes\ArrayOfLabel)#258 (1) { ["Label":protected]=> array(1) { [0]=> object(DivideBV\Postnl\ComplexTypes\Label)#259 (3) { ["Content":protected]=> string(7141) "%PDF-1.2 1 0 obj PDF 3 file (I have removed the pdf output. Here i am getting a single label in a pdf) %%EOF " ["Contenttype":protected]=> NULL ["Labeltype":protected]=> string(5) "Label" } } } ["ProductCodeDelivery":protected]=> string(4) "3085" ["Warnings":protected]=> object(DivideBV\Postnl\ComplexTypes\ArrayOfWarning)#260 (1) { ["Warning":protected]=> NULL } } [3]=> object(DivideBV\Postnl\ComplexTypes\ResponseShipment)#261 (7) { ["Barcode":protected]=> string(15) "3STSRV434265636" ["DownPartnerBarcode":protected]=> NULL ["DownPartnerID":protected]=> NULL ["DownPartnerLocation":protected]=> NULL ["Labels":protected]=> object(DivideBV\Postnl\ComplexTypes\ArrayOfLabel)#262 (1) { ["Label":protected]=> array(1) { [0]=> object(DivideBV\Postnl\ComplexTypes\Label)#263 (3) { ["Content":protected]=> string(7150) "%PDF-1.2 1 0 obj PDF 4 file (I have removed the pdf output. Here i am getting a single label in a pdf) %%EOF

          ["Contenttype":protected]=>
          NULL
          ["Labeltype":protected]=>
          string(5) "Label"
        }
      }
    }
    ["ProductCodeDelivery":protected]=>
    string(4) "3085"
    ["Warnings":protected]=>
    object(DivideBV\Postnl\ComplexTypes\ArrayOfWarning)#264 (1) {
      ["Warning":protected]=>
      NULL
    }
  }
}

} }

ameenross commented 5 years ago

Can you please post the raw XML, using $client->debug()?

vkas002 commented 5 years ago

Sure. Below i have attached the raw xml.

ameenross commented 5 years ago

Please be careful with personal information. You might want to remove that attachment now. The important bit is here:

screenshot from 2019-01-03 12-17-40

You are not passing the correct printer type. Check here under "printer types". You can pass the printer type as the second argument to generateLabels.

ameenross commented 5 years ago

Also, depending on your type of printer, this may or may not work for you. We just used ghostscript to combine the separate labels into a single PDF, one each page.

vkas002 commented 5 years ago

I have set the PrinterType to "GraphicFile|PDF|MergeA" and it seems to be working now. Thanka alot man. Cheers!

ameenross commented 5 years ago

Good to hear. You're welcome!