{"id":1,"nombre":"Complemento detallista","configuracion":[{"id":"codigo_gln","nombre":"No.Global localización comprador","tipo":"v-text-field","campo_id":null,"campo_nombre":null,"parametros_default":null,"valor_default":null,"show_codigo":null,"multiple":false,"ids":"codigo_gln"}],"estatus":true,"created_at":"2024-06-13T15:41:33.000000Z","updated_at":"2024-09-11T20:11:33.000000Z","database_id":2,"uuid":"c-detallista","contenido":"$row = \\App\\Factura::with(['contacto','detalles','detalles.producto','detalles.unidad','cfdi'])\n ->where('id',$data->id)\n ->where('estatus', true)\n ->first();\n\n $cfdi=$data->cfdi;\n $xml=$data->xml;\n // $xml = new \\DOMDocument(\"1.0\",'utf-8');\n // $xml->formatOutput = true;\n //direcciones del detallista\n $cfdi->setAttribute(\"xsi:schemaLocation\",\"http://www.sat.gob.mx/cfd/4 http://www.sat.gob.mx/sitio_internet/cfd/4/cfdv40.xsd http://www.sat.gob.mx/detallista http://www.sat.gob.mx/sitio_internet/cfd/detallista/detallista.xsd\");\n $cfdi->setAttribute(\"xmlns:cfdi\",\"http://www.sat.gob.mx/cfd/4\");\n $cfdi->setAttribute(\"xmlns:detallista\",\"http://www.sat.gob.mx/detallista\");\n\n $complemento = $xml->createElement(\"cfdi:Complemento\");\n $detallista = $xml->createElement(\"detallista:detallista\");\n $detallista->setAttribute(\"contentVersion\",\"1.3.1\");\n $detallista->setAttribute(\"documentStatus\",\"ORIGINAL\");\n $detallista->setAttribute(\"documentStructureVersion\",\"AMC8.1\");\n $detallista->setAttribute(\"type\",\"SimpleInvoiceType\");\n\n //requerido\n $requestForPaymentIdentification= $xml->createElement(\"detallista:requestForPaymentIdentification\");//especifica al transacción a utilizar\n $entityType = $xml->createElement(\"detallista:entityType\",\"INVOICE\");//especifica el tipo de transacción\n // INVOICE, DEBIT_NOTE, CREDIT_NOTE, LEASE_RECEIPT, HONORARY_RECEIPT, PARTIAL_INVOICE, TRANSPORT_DOCUMENT,AUTO_INVOICE\n $requestForPaymentIdentification->appendChild($entityType);\n $detallista->appendChild($requestForPaymentIdentification);\n\n //opcional\n $specialInstruction = $xml->createElement(\"detallista:specialInstruction\");//Tipo de instrucciones comerciales son enviadas\n $specialInstruction->setAttribute(\"code\",\"ZZZ\");//Código del tipo de instrucciones comerciales que son enviadas\n //AAB, DUT, PUR, ZZZ\n $oUtil = new \\App\\Utilerias\\Util;\n $letras = $oUtil->toInvoice((float)$row->total,2,$row->moneda);\n $importe_text = $letras;\n $detallista_text = $xml->createElement(\"detallista:text\", $importe_text);//Información de texto que aplica a todo el mensaje de la factura\n $specialInstruction->appendChild($detallista_text);\n $detallista->appendChild($specialInstruction);\n\n //requerido\n $orderIdentification = $xml->createElement(\"detallista:orderIdentification\");//Especifica la información sobre la orden de compra que hace referencia a la factura\n $referenceIdentification = $xml->createElement(\"detallista:referenceIdentification\", $row->referencia);//especifica el número de la orden de compra(comprador)\n $referenceIdentification->setAttribute(\"type\", 'ON'); //especifica el código para identificar la orden de compra\n $orderIdentification->appendChild($referenceIdentification);\n $referenceDate = $xml->createElement(\"detallista:ReferenceDate\", $row->fecha);//Especifica la fecha de la orden de compra\n $orderIdentification->appendChild($referenceDate);\n $detallista->appendChild($orderIdentification);\n\n //requerido, pueden ser varios\n $additionalInformation = $xml->createElement(\"detallista:AdditionalInformation\");//epecifica las referencias adicionales a nivel global\n $referenceIdentificationAditional = $xml->createElement(\"detallista:referenceIdentification\", $row->referencia);\n $referenceIdentificationAditional->setAttribute(\"type\", 'ON');\n $additionalInformation->appendChild($referenceIdentificationAditional);\n $detallista->appendChild($additionalInformation);\n\n //opcional\n $deliveryNote = $xml->createElement(\"detallista:DeliveryNote\");\n $referenceIdentification = $xml->createElement(\"detallista:referenceIdentification\", $row->referencia);\n $deliveryNote->appendChild($referenceIdentification);\n $referenceDate = $xml->createElement(\"detallista:ReferenceDate\", $row->fecha);\n $deliveryNote->appendChild($referenceDate);\n $detallista->appendChild($deliveryNote);\n\n //requerido\n if(empty($data->codigo_gln)){\n throw new ErrorException('No hay un codigo de localizacion para el contacto: '.$row->contacto->nombre_completo); \n }\n $buyer = $xml->createElement(\"detallista:buyer\");//Información del comprador\n $gln = $xml->createElement(\"detallista:gln\", $data->codigo_gln);//Número global de localización del comprador\n\n $buyer->appendChild($gln);\n $contactInformation = $xml->createElement(\"detallista:contactInformation\");//Información del contacto de compras\n $personOrDepartmentName = $xml->createElement(\"detallista:personOrDepartmentName\");//Especifica el contactod de compras\n $text = $xml->createElement(\"detallista:text\", $row->contacto_id);//Especifica el contacto de factura\n $personOrDepartmentName->appendChild($text);\n $contactInformation->appendChild($personOrDepartmentName);\n $buyer->appendChild($contactInformation);\n $detallista->appendChild($buyer);\n\n //opcional\n // $seller = $xml->createElement(\"detallista:seller\");\n // $gln = $xml->createElement(\"detallista:gln\", $row->gln);\n // $seller->appendChild($gln);\n // $alternatePartyIdentification = $xml->createElement(\"detallista:alternatePartyIdentification \", $row->contacto_vendedor);\n // $alternatePartyIdentification->setAttribute(\"type\", \"SELLER_ASSIGNED_IDENTIFIER_FOR_A_PARTY\");\n // $seller->appendChild($alternatePartyIdentification);\n // $detallista->appendChild($seller);\n\n //opcional\n // $shipTo = $xml->createElement(\"detallista:shipTo\");\n // $gln = $xml->createElement(\"detallista:gln\", $row->gln);\n // $shipTo->appendChild($gln);\n // $nameAndAddress = $xml->createElement(\"detallista:nameAndAddress\");\n // $name = $xml->createElement(\"detallista:name\", $row->empresa->razon_social);\n // $nameAndAddress->appendChild($name);\n // $streetAddressOne = $xml->createElement(\"detallista:streetAddressOne\", $row->empresa->direccion->vialidad);\n // $nameAndAddress->appendChild($streetAddressOne);\n // $city = $xml->createElement(\"detallista:city\", $row->empresa->direccion->localidad_id);\n // $nameAndAddress->appendChild($city);\n // $postalCode = $xml->createElement(\"detallista:postalCode\", $row->empresa->direccion->codigo_postal);\n // $nameAndAddress->appendChild($postalCode);\n // $shipTo->appendChild($nameAndAddress);\n // $detallista->appendChild($shipTo);\n\n //opcional\n // $invoiceCreator = $xml->createElement(\"detallista:InvoiceCreator\");\n // $gln = $xml->createElement(\"detallista:gln\", $row->gln);\n // $invoiceCreator->appendChild($gln);\n // $alternatePartyIdentification = $xml->createElement(\"detallista:alternatePArtyIdentification\", $row->contacto_vendedor);\n // $alternatePartyIdentification->setAttribute(\"type\", \"SELLER_ASSIGNED_IDENTIFIER_FOR_A_PARTY\");\n // $invoiceCreator->appendChild($alternatePartyIdentification);\n // $nameAndAddress = $xml->createElement(\"detallista:nameAndAddress\");\n // $name = $xml->createElement(\"detallista:name\", $row->razon_social);\n // $nameAndAddress->appendChild($name);\n // $streetAddressOne = $xml->createElement(\"detallista:streetAddressOne\", $row->direccion);\n // $nameAndAddress->appendChild($streetAddressOne);\n // $city = $xml->createElement(\"detallista:city\", $row->ciudad);\n // $nameAndAddress->appendChild($city);\n // $postalCode = $xml->createElement(\"detallista:postalCode\", $row->codigo_postal);\n // $nameAndAddress->appendChild($postalCode);\n // $invoiceCreator->appendChild($nameAndAddress);\n // $detallista->appendChild($invoiceCreator);\n\n //opcional\n // $customs = $xml->createElement(\"detallista:Customs\", $row->ubicacion_aduana);\n // $gln = $xml->createElement(\"detallista:gln\", $row->gln);\n // $customs->appendChild($gln);\n // $detallista->appendChild($customs);\n\n //opcional\n $allowanceCharge = $xml->createElement(\"detallista:allowanceCharge\");//Especifica la información de los cargos o descuentos globales\n $allowanceCharge->setAttribute(\"allowanceChargeType\", \"ALLOWANCE_GLOBAL\");//indicador de cargo o descuento\n //ALLOWANCE_GLOBAL, CHARGE_GLOBAL\n $allowanceCharge->setAttribute(\"settlementType\", \"OFF_INVOICE\");//especificar la inputación del descuento o cargo\n //BILL_BACK, OFF_INVOICE\n $specialServicesType = $xml->createElement(\"detallista:specialServicesType\", \"AJ\");//Opcional- especifica el tipo de descuento o cargo\n $allowanceCharge->appendChild($specialServicesType);\n $monetaryAmountOrPercentage = $xml->createElement(\"detallista:monetaryAmountOrPercentage\"); //especifica la cantidad monetaria o porcentaje del decuento\n $rate = $xml->createElement(\"detallista:rate\");\n $rate->setAttribute(\"base\", \"INVOICE_VALUE\");\n $porcentaje = $xml->createElement(\"detallista:percentage\", 0);\n $rate->appendChild($porcentaje);\n $monetaryAmountOrPercentage->appendChild($rate);\n $allowanceCharge->appendChild($monetaryAmountOrPercentage);\n $detallista->appendChild($allowanceCharge);\n\n //opcional\n foreach($row->detalles as $index=>$prod ){\n $codigo=\\App\\ProductoCodigo::where('razon_social_id',$row->razon_social_id)->where('producto_id',$prod->producto_id)\n ->where('producto_variante_id',$prod->producto_variante_id)->first();\n if(empty($codigo)){\n throw new ErrorException('No hay un codigo EAN para el producto: '.$prod->producto_id); \n }\n $lineItem = $xml->createElement(\"detallista:lineItem\");\n $lineItem->setAttribute(\"number\", $index+1);\n $lineItem->setAttribute(\"type\", \"SimpleInvoiceLineItemType\");\n $tradeItemIdentification = $xml->createElement(\"detallista:tradeItemIdentification\");\n $gtin = $xml->createElement(\"detallista:gtin\", $codigo->codigo_producto);\n $tradeItemIdentification->appendChild($gtin);\n $lineItem->appendChild($tradeItemIdentification);\n $alternateTradeItemIdentification = $xml->createElement(\"detallista:alternateTradeItemIdentification\", $prod->id);\n $alternateTradeItemIdentification->setAttribute(\"type\",\"BUYER_ASSIGNED\");\n $lineItem->appendChild($alternateTradeItemIdentification);\n\n $tradeItemDescriptionInformation = $xml->createElement(\"detallista:tradeItemDescriptionInformation\");\n $tradeItemDescriptionInformation->setAttribute(\"language\",\"ES\");\n\n $descripcion = $prod->producto->descripcion;\n\n // Si la descripción es mayor a 35 caracteres, la corta\n if (strlen($descripcion) > 35) {\n $descripcion = substr($descripcion, 0, 35);\n }\n\n // Crea el elemento longText con la descripción cortada (si aplica)\n $longText = $xml->createElement(\"detallista:longText\", $descripcion);\n $tradeItemDescriptionInformation->appendChild($longText);\n $lineItem->appendChild($tradeItemDescriptionInformation);\n $invoicedQuantity = $xml->createElement(\"detallista:invoicedQuantity\", $prod->cantidad);\n $invoicedQuantity->setAttribute(\"unitOfMeasure\", $prod->unidad->nombre);\n $lineItem->appendChild($invoicedQuantity);\n $grossPrice = $xml->createElement(\"detallista:grossPrice\");\n $amount = $xml->createElement(\"detallista:Amount\", $prod->precio);\n $grossPrice->appendChild($amount);\n $lineItem->appendChild($grossPrice);\n $netPrice = $xml->createElement(\"detallista:netPrice\");\n $amount_netPrice = $xml->createElement(\"detallista:Amount\", $prod->precio);\n $netPrice->appendChild($amount_netPrice);\n $lineItem->appendChild($netPrice);\n $totalLineAmount = $xml->createElement(\"detallista:totalLineAmount\");\n $grossAmount = $xml->createElement(\"detallista:grossAmount\");\n $amount_grossAmount = $xml->createElement(\"detallista:Amount\", $prod->sub_total);\n $grossAmount->appendChild($amount_grossAmount);\n $totalLineAmount->appendChild($grossAmount);\n $netAmount = $xml->createElement(\"detallista:netAmount\");\n $amount_netAmount = $xml->createElement(\"detallista:Amount\", $prod->total);\n $netAmount->appendChild($amount_netAmount);\n $totalLineAmount->appendChild($netAmount);\n $lineItem->appendChild($totalLineAmount);\n $detallista->appendChild($lineItem);\n\n }\n\n $totalAmount = $xml->createElement(\"detallista:totalAmount\");\n $amount = $xml->createElement(\"detallista:Amount\",$row->total);\n $totalAmount->appendChild($amount);\n $detallista->appendChild($totalAmount);\n\n $totalAllowanceCharge = $xml->createElement(\"detallista:TotalAllowanceCharge\");\n $totalAllowanceCharge->setAttribute(\"allowanceOrChargeType\", \"ALLOWANCE\");\n $specialServicesTypeAll = $xml->createElement(\"detallista:specialServicesType\",\"TD\");\n $amountall = $xml->createElement(\"detallista:Amount\",0);\n $totalAllowanceCharge->appendChild($specialServicesTypeAll);\n $totalAllowanceCharge->appendChild($amountall);\n $detallista->appendChild($totalAllowanceCharge);\n\n $complemento->appendChild($detallista);\n $cfdi->appendChild($complemento);\n\t\t$xml->appendChild($cfdi); \n $xmlString = $xml->saveXML();\n\n $jsonString = json_encode($xmlString);\n\n // Devolver el JSON\n echo $jsonString;"}