Tengo una función de exportación que obtiene model data y agrega algunos de sus datos de relación como una matriz debajo de ella.

El problema es que la matriz de datos de relación es incorrecta todo el tiempo.

Ejemplo

real data

one

data that I get in export file

<NMEXML EximID="13037" BranchCode="1450645381" ACCOUNTANTCOPYID="">
  <TRANSACTIONS OnError="CONTINUE">
    <WTRAN operation="Add" REQUESTID="1">
      <TRANSFERID>3</TRANSFERID>
      <TRANSACTIONID/>
      <TRANSFERNO>3</TRANSFERNO>
      <TRANSFERDATE>2020-08-22</TRANSFERDATE>
      <DESCRIPTION>232547544</DESCRIPTION>
      <FROMWHID>8</FROMWHID>
      <TOWHID/>
      <FROMWHADDRESS/>
      <TOWHADDRESS/>
      <ITEMLINE operation="Add">
        <KeyID>1</KeyID>
        <ITEMNO>6951919696</ITEMNO>
        <QUANTITY>1</QUANTITY>
        <ITEMUNIT>Unit</ITEMUNIT>
        <UNITRATIO>1</UNITRATIO>
        <ITEMRESERVED1/>
        <ITEMRESERVED2/>
        <ITEMRESERVED3/>
        <ITEMRESERVED4/>
        <ITEMRESERVED5/>
        <ITEMRESERVED6/>
        <ITEMRESERVED7/>
        <ITEMRESERVED8/>
        <ITEMRESERVED9/>
        <ITEMRESERVED10/>
        <UNITPRICE>2500000</UNITPRICE>
        <QTYCONTROL>0</QTYCONTROL>
        <SNHISTORY operation="Ret">
          <SERIALNUMBER>658532125</SERIALNUMBER>
          <EXPIREDDATE>2020-08-21</EXPIREDDATE>
          <QUANTITY>1</QUANTITY>
          <SNSIGN>-1</SNSIGN>
        </SNHISTORY>
      </ITEMLINE>
      <ITEMLINE operation="Add">
        <KeyID>2</KeyID>
        <ITEMNO>9532880976</ITEMNO>
        <QUANTITY>1</QUANTITY>
        <ITEMUNIT>Pcs</ITEMUNIT>
        <UNITRATIO>1</UNITRATIO>
        <ITEMRESERVED1/>
        <ITEMRESERVED2/>
        <ITEMRESERVED3/>
        <ITEMRESERVED4/>
        <ITEMRESERVED5/>
        <ITEMRESERVED6/>
        <ITEMRESERVED7/>
        <ITEMRESERVED8/>
        <ITEMRESERVED9/>
        <ITEMRESERVED10/>
        <UNITPRICE>50000000</UNITPRICE>
        <QTYCONTROL>0</QTYCONTROL>
        <SNHISTORY operation="Ret">
          <SERIALNUMBER>658532125</SERIALNUMBER>
          <EXPIREDDATE>2020-08-21</EXPIREDDATE>
          <QUANTITY>1</QUANTITY>
          <SNSIGN>-1</SNSIGN>
        </SNHISTORY>
        <SNHISTORY operation="Ret">
          <SERIALNUMBER>454874152</SERIALNUMBER>
          <EXPIREDDATE>2020-08-21</EXPIREDDATE>
          <QUANTITY>1</QUANTITY>
          <SNSIGN>-1</SNSIGN>
        </SNHISTORY>
      </ITEMLINE>
      <ITEMLINE operation="Add">
        <KeyID>3</KeyID>
        <ITEMNO>2124086153</ITEMNO>
        <QUANTITY>1</QUANTITY>
        <ITEMUNIT>Box</ITEMUNIT>
        <UNITRATIO>1</UNITRATIO>
        <ITEMRESERVED1/>
        <ITEMRESERVED2/>
        <ITEMRESERVED3/>
        <ITEMRESERVED4/>
        <ITEMRESERVED5/>
        <ITEMRESERVED6/>
        <ITEMRESERVED7/>
        <ITEMRESERVED8/>
        <ITEMRESERVED9/>
        <ITEMRESERVED10/>
        <UNITPRICE>23658000</UNITPRICE>
        <QTYCONTROL>0</QTYCONTROL>
        <SNHISTORY operation="Ret">
          <SERIALNUMBER>658532125</SERIALNUMBER>
          <EXPIREDDATE>2020-08-21</EXPIREDDATE>
          <QUANTITY>1</QUANTITY>
          <SNSIGN>-1</SNSIGN>
        </SNHISTORY>
        <SNHISTORY operation="Ret">
          <SERIALNUMBER>454874152</SERIALNUMBER>
          <EXPIREDDATE>2020-08-21</EXPIREDDATE>
          <QUANTITY>1</QUANTITY>
          <SNSIGN>-1</SNSIGN>
        </SNHISTORY>
        <SNHISTORY operation="Ret">
          <SERIALNUMBER>232547544</SERIALNUMBER>
          <EXPIREDDATE>2020-08-22</EXPIREDDATE>
          <QUANTITY>1</QUANTITY>
          <SNSIGN>-1</SNSIGN>
        </SNHISTORY>
      </ITEMLINE>
    </WTRAN>
  </TRANSACTIONS>
</NMEXML>

Explicación

ITEMLINE son una matriz de las filas de mi modelo

SNHISTORY son datos de relación del modelo (captura de pantalla de arriba) que necesito que sean matrices también y lo más importante en relaciones correctas.

Como puede ver, mis SNHISTORY están fuera de lugar en ITEMLINE y se imprimen en un extra.

Código

Se comentan SNHISTORY partes para su mejor comprensión

public function internalTransit(Request $request) {
    $internalTransits = Transit::with(['barcodes', 'barcodes.outlet', 'barcodes.product', 'barcodes.product.outer', 'sender', 'receiver', 'fromOutlet', 'toOutlet'])->get();

    //data here
    $array = [];
    foreach($internalTransits as $key => $item) {

        // SNHISTORY data

        // collect the data
        $serials = [];
        $ITEMUNIT = '';
        $UNITPRICE = '';
        foreach($item->barcodes as $barcode) {
            $serials = $barcode['serial_number'] ? $barcode['serial_number'] : $barcode['u_serial_number'];
            $ITEMUNIT = $barcode['product']['unit'];
            $UNITPRICE =$barcode['product']['price'];
        }
        // add data into template
        $shin[$key][] = [
            '_attributes' => [
                'operation' => 'Ret'
            ],
            'SERIALNUMBER' => $serials,
            'EXPIREDDATE' => $item['created_at']->format('Y-m-d'),
            'QUANTITY' => '1',
            'SNSIGN' => '-1',
        ];
        // SNHISTORY data

        $year = Carbon::createFromFormat('Y-m-d H:i:s', $item['created_at'])->year;
        $month = Carbon::createFromFormat('Y-m-d H:i:s', $item['created_at'])->month;


        $timeline[$key][] = [
            '_attributes' => [
                'operation' => 'Add'
            ],
            'KeyID' => $item['id'],
            'ITEMNO' => $item['transNu'],
            'QUANTITY' => '1',
            'ITEMUNIT' => $ITEMUNIT,
            'UNITRATIO' => '1',
            'ITEMRESERVED1' => '',
            'ITEMRESERVED2' => '',
            'ITEMRESERVED3' => '',
            'ITEMRESERVED4' => '',
            'ITEMRESERVED5' => '',
            'ITEMRESERVED6' => '',
            'ITEMRESERVED7' => '',
            'ITEMRESERVED8' => '',
            'ITEMRESERVED9' => '',
            'ITEMRESERVED10' => '',
            'UNITPRICE' => $UNITPRICE,
            'QTYCONTROL' => '0',
            'SNHISTORY' => $shin, // load the SNHISTORY template
        ];

        $array['TRANSACTIONS'] = [
            '_attributes' => [
                'OnError' => 'CONTINUE'
            ],
        ];

        $array['TRANSACTIONS']['WTRAN'] = [
            '_attributes' => [
                'operation' => 'Add',
                'REQUESTID' => '1',
            ],
            'TRANSFERID' => $item['id'],
            'TRANSACTIONID' => '',
            'TRANSFERNO' => $item['id'],
            'TRANSFERDATE' => $item['created_at']->format('Y-m-d'),
            'DESCRIPTION' => $item['description'],
            'FROMWHID' => $month,
            'TOWHID' => '',
            'FROMWHADDRESS' => '',
            'TOWHADDRESS' => '',
        ];

        $array['TRANSACTIONS']['WTRAN']['ITEMLINE'] = $timeline;

    }
    //data here

    $filename = 'internalTransits.xml';
    $result = ArrayToXml::convert($array, [
        'rootElementName' => 'NMEXML',
        '_attributes' => [
            'EximID'=> "13037",
            'BranchCode'=> $barcode['outlet'][0]['code'],
            'ACCOUNTANTCOPYID'=> ""
        ]
    ]);
    Storage::disk('local')->put($filename, $result);
    $fullPath = url('exports', $filename);

    return response()->json([
        'data' => $fullPath
    ]);
}

Pregunta

¿Cómo puedo obtener los datos correctos de la relación y colocarlos en matrices ITEMLINE?

0
mafortis 23 ago. 2020 a las 08:35

1 respuesta

La mejor respuesta

Resuelta

Cambié mi código SNHISTORY a:

$a = $item->barcodes;
$grouped = $a->mapToGroups(function ($item, $key) {
    return [
        'SNHISTORY' => [
            '_attributes' => [
                'operation' => 'Ret'
            ],
            'SERIALNUMBER' => $item['serial_number'] ? $item['serial_number'] : $item['u_serial_number'],
            'EXPIREDDATE' => $item['created_at']->format('Y-m-d'),
            'QUANTITY' => '1',
            'SNSIGN' => '-1',
        ],
        'ITEMUNIT' => $item['product']['unit'],
        'UNITPRICE' => $item['product']['price'],
    ];
});

Y $timeline he colocado $grouped->toArray() como:

$timeline[$key][] = [
    '_attributes' => [
        'operation' => 'Add'
    ],
    'KeyID' => $item['id'],
    'ITEMNO' => $item['transNu'],
    'QUANTITY' => '1',
    'ITEMUNIT' => $barcode['product']['unit'],
    'UNITRATIO' => '1',
    'ITEMRESERVED1' => '',
    'ITEMRESERVED2' => '',
    'ITEMRESERVED3' => '',
    'ITEMRESERVED4' => '',
    'ITEMRESERVED5' => '',
    'ITEMRESERVED6' => '',
    'ITEMRESERVED7' => '',
    'ITEMRESERVED8' => '',
    'ITEMRESERVED9' => '',
    'ITEMRESERVED10' => '',
    'UNITPRICE' => $barcode['product']['price'],
    'QTYCONTROL' => '0',
    'SNHISTORY' => $grouped->toArray(), //here
];

Y el problema resuelto, puedo obtener mi matriz de relaciones modelo con mi plantilla de deseo de la siguiente manera:

<NMEXML EximID="13037" BranchCode="1450645381" ACCOUNTANTCOPYID="">
    <TRANSACTIONS OnError="CONTINUE">
        <WTRAN operation="Add" REQUESTID="1">
            <TRANSFERID>3</TRANSFERID>
            <TRANSACTIONID />
            <TRANSFERNO>3</TRANSFERNO>
            <TRANSFERDATE>2020-08-22</TRANSFERDATE>
            <DESCRIPTION>232547544</DESCRIPTION>
            <FROMWHID>8</FROMWHID>
            <TOWHID />
            <FROMWHADDRESS />
            <TOWHADDRESS />
            <ITEMLINE operation="Add">
                <KeyID>1</KeyID>
                <ITEMNO>6951919696</ITEMNO>
                <QUANTITY>1</QUANTITY>
                <ITEMUNIT>Unit</ITEMUNIT>
                <UNITRATIO>1</UNITRATIO>
                <ITEMRESERVED1 />
                <ITEMRESERVED2 />
                <ITEMRESERVED3 />
                <ITEMRESERVED4 />
                <ITEMRESERVED5 />
                <ITEMRESERVED6 />
                <ITEMRESERVED7 />
                <ITEMRESERVED8 />
                <ITEMRESERVED9 />
                <ITEMRESERVED10 />
                <UNITPRICE>2500000</UNITPRICE>
                <QTYCONTROL>0</QTYCONTROL>
                <SNHISTORY>
                    <SNHISTORY operation="Ret">
                        <SERIALNUMBER>74122681</SERIALNUMBER>
                        <EXPIREDDATE>2020-08-21</EXPIREDDATE>
                        <QUANTITY>1</QUANTITY>
                        <SNSIGN>-1</SNSIGN>
                    </SNHISTORY>
                    <SNHISTORY operation="Ret">
                        <SERIALNUMBER>658532125</SERIALNUMBER>
                        <EXPIREDDATE>2020-08-21</EXPIREDDATE>
                        <QUANTITY>1</QUANTITY>
                        <SNSIGN>-1</SNSIGN>
                    </SNHISTORY>
                </SNHISTORY>
            </ITEMLINE>
            <ITEMLINE operation="Add">
                <KeyID>2</KeyID>
                <ITEMNO>9532880976</ITEMNO>
                <QUANTITY>1</QUANTITY>
                <ITEMUNIT>Pcs</ITEMUNIT>
                <UNITRATIO>1</UNITRATIO>
                <ITEMRESERVED1 />
                <ITEMRESERVED2 />
                <ITEMRESERVED3 />
                <ITEMRESERVED4 />
                <ITEMRESERVED5 />
                <ITEMRESERVED6 />
                <ITEMRESERVED7 />
                <ITEMRESERVED8 />
                <ITEMRESERVED9 />
                <ITEMRESERVED10 />
                <UNITPRICE>50000000</UNITPRICE>
                <QTYCONTROL>0</QTYCONTROL>
                <SNHISTORY>
                    <SNHISTORY operation="Ret">
                        <SERIALNUMBER>454874152</SERIALNUMBER>
                        <EXPIREDDATE>2020-08-21</EXPIREDDATE>
                        <QUANTITY>1</QUANTITY>
                        <SNSIGN>-1</SNSIGN>
                    </SNHISTORY>
                </SNHISTORY>
            </ITEMLINE>
            <ITEMLINE operation="Add">
                <KeyID>3</KeyID>
                <ITEMNO>2124086153</ITEMNO>
                <QUANTITY>1</QUANTITY>
                <ITEMUNIT>Box</ITEMUNIT>
                <UNITRATIO>1</UNITRATIO>
                <ITEMRESERVED1 />
                <ITEMRESERVED2 />
                <ITEMRESERVED3 />
                <ITEMRESERVED4 />
                <ITEMRESERVED5 />
                <ITEMRESERVED6 />
                <ITEMRESERVED7 />
                <ITEMRESERVED8 />
                <ITEMRESERVED9 />
                <ITEMRESERVED10 />
                <UNITPRICE>23658000</UNITPRICE>
                <QTYCONTROL>0</QTYCONTROL>
                <SNHISTORY>
                    <SNHISTORY operation="Ret">
                        <SERIALNUMBER>232547544</SERIALNUMBER>
                        <EXPIREDDATE>2020-08-21</EXPIREDDATE>
                        <QUANTITY>1</QUANTITY>
                        <SNSIGN>-1</SNSIGN>
                    </SNHISTORY>
                </SNHISTORY>
            </ITEMLINE>
        </WTRAN>
    </TRANSACTIONS>
</NMEXML>
0
mafortis 23 ago. 2020 a las 09:11