| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * FACTURA ELECTRÓNICA SUNAT | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * UBL 2.1 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * Version 1.0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * Copyright 2018, Jaime Cruz | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | namespace F72X\Sunat\Document; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use F72X\Company; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use F72X\Tools\TemplateMgr; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use F72X\UblComponent\SchemaNS; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Sabre\Xml\Writer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | class Factura extends SunatDocument { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 20 |  |  |     // Valores predeterminados para una factura | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |     public function __construct() { | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |         $this->InvoiceTypeCode = SunatDocument::DOC_FACTURA; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      * The xmlSerialize method is called during xml writing. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      * @param Writer $writer | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     public function xmlSerialize(Writer $writer) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         $companyRUC  = Company::getRUC(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         $companyName = Company::getCompanyName(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         // SchemaNS::EXT . 'UBLExtensions' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         $UBLExtensions = TemplateMgr::getTpl('UBLExtensions.xml'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         $Signature     = TemplateMgr::getTpl('Signature.xml', [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |                     'RUC'         => $companyRUC, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |                     'companyName' => $companyName | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         $this->writeLineJump($writer); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         $writer->writeRaw($UBLExtensions); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         $writer->write([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |             SchemaNS::CBC . 'UBLVersionID'      => self::UBL_VERSION_ID, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |             SchemaNS::CBC . 'CustomizationID'   => self::CUSTUMIZATION_ID, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |             [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |                 'name' => SchemaNS::CBC . 'ProfileID', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |                 'value' => $this->ProfileID, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |                 'attributes' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |                     'schemeName' => 'SUNAT:Identificador de Tipo de Operación', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |                     'schemeAgencyName' => 'PE:SUNAT', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |                     'schemeURI' => 'urn:pe:gob:sunat:cpe:see:gem:catalogos:catalogo17' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |                 ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |             ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |             SchemaNS::CBC . 'ID' => $this->ID, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |             SchemaNS::CBC . 'IssueDate' => $this->IssueDate->format('Y-m-d'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |             SchemaNS::CBC . 'IssueTime' => $this->IssueDate->format('H:i:s') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         if ($this->DueDate) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |             $writer->write([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |                 SchemaNS::CBC . 'DueDate' => $this->DueDate->format('Y-m-d') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |             ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         $writer->write([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |             [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |                 'name' => SchemaNS::CBC . 'InvoiceTypeCode', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |                 'value' => $this->InvoiceTypeCode, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |                 'attributes' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |                     'listAgencyName' => 'PE:SUNAT', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |                     'listID'         => $this->ProfileID, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |                     'listName'       => 'Tipo de Documento', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |                     'listSchemeURI'  => 'urn:pe:gob:sunat:cpe:see:gem:catalogos:catalogo51', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |                     'listURI'        => 'urn:pe:gob:sunat:cpe:see:gem:catalogos:catalogo01', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |                     'name'           => 'Tipo de Operacion' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |                 ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |             ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         // Write notes | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         foreach ($this->Notes as $InvoiceLine) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |             $writer->write($InvoiceLine); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         $writer->write([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |             [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |                 'name'  => SchemaNS::CBC . 'DocumentCurrencyCode', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |                 'value' => $this->DocumentCurrencyCode, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |                 'attributes' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |                     'listID'            => 'ISO 4217 Alpha', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |                     'listName'          => 'Currency', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |                     'listAgencyName'    => 'United Nations Economic Commission for Europe' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |                 ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |             ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |             SchemaNS::CBC . 'LineCountNumeric'          => $this->LineCountNumeric | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |         // Order Reference | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         if($this->OrderReference){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |             $writer->write([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |                 SchemaNS::CAC . 'OrderReference'            => $this->OrderReference | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |             ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |         // Despatch Document Reference | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |         if($this->DespatchDocumentReference){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |             $writer->write([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |                 SchemaNS::CAC . 'DespatchDocumentReference' => $this->DespatchDocumentReference | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |             ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         // cac:Signature | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |         $this->writeLineJump($writer); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         $writer->writeRaw($Signature); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |         $writer->write([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |             SchemaNS::CAC . 'AccountingSupplierParty'   => $this->AccountingSupplierParty, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |             SchemaNS::CAC . 'AccountingCustomerParty'   => $this->AccountingCustomerParty | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |         // Cargos y descuentos | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |         foreach ($this->AllowanceCharges as $AllowanceCharge) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |             $writer->write([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |                 SchemaNS::CAC . 'AllowanceCharge' => $AllowanceCharge | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |             ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |         $writer->write([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |             SchemaNS::CAC . 'TaxTotal' => $this->TaxTotal | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |         $writer->write([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |             SchemaNS::CAC . 'LegalMonetaryTotal' => $this->LegalMonetaryTotal | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |         // Detalle | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |         foreach ($this->InvoiceLines as $InvoiceLine) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |             $writer->write([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |                 SchemaNS::CAC . 'InvoiceLine' => $InvoiceLine | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |             ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 138 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 139 |  |  |  |