Beispiel #1
0
    def eguide_submit_single(self, **kw):
        stock_picking_id = kw.get('stock_picking_id')

        query = "select name, company_id, unsigned_document, signed_document, response_document from stock_picking where id = " + str(
            stock_picking_id)
        request.cr.execute(query)
        stock_picking = request.cr.dictfetchone()

        query = "select res_partner.vat, res_company.api_mode, res_company.sol_ruc, res_company.sol_username, res_company.sol_password, res_company.certs from res_company left join res_partner on res_partner.company_id = res_company.id where res_company.id = " + str(
            stock_picking['company_id']
        ) + " and res_partner.is_company = TRUE and res_company.partner_id = res_partner.id"
        request.cr.execute(query)
        company_fields = request.cr.dictfetchone()

        serieParts = str(stock_picking["name"]).split("-")
        serieConsecutivoString = serieParts[0]
        serieConsecutivo = serieParts[1]

        sunat_data = {
            "secuencia_consecutivo": serieConsecutivo,
            "sunat_sol": {
                "ruc": company_fields["sol_ruc"],
                'usuario': company_fields["sol_username"],
                'clave': company_fields["sol_password"]
            },
            "xml": {
                #"signed":base64.b64decode((stock_picking["signed_document"]))
            },
            "licencia": "081OHTGAVHJZ4GOZJGJV"
        }

        xmlPath = str(os.path.dirname(os.path.abspath(__file__))).replace(
            "controllers", "models") + '/xml'
        SunatService = Service()
        SunatService.setXMLPath(xmlPath)

        if ("T001" in serieConsecutivoString):
            SunatService.fileName = str(company_fields["vat"]) + "-09-" + str(
                serieConsecutivoString) + "-" + str(serieConsecutivo)

        SunatService.initSunatAPI(company_fields["api_mode"], "sendBill")
        sunatResponse = SunatService.processDeliveryGuideFromSignedXML(
            sunat_data)

        if ('status' in sunatResponse):
            if (sunatResponse["status"] == "OK"):
                # save xml documents steps for reference in edocs
                response_document_filename = str("R_") + "-09-" + str(
                    serieConsecutivoString) + "-" + str(
                        serieConsecutivo) + str(".XML")

                api_message = "ESTADO: " + str(
                    sunatResponse["status"]) + "\n" + "REFERENCIA: " + str(
                        sunatResponse["body"]["referencia"]).replace(
                            "'", '"') + "\n" + " " + str(
                                sunatResponse["body"]["description"]).replace(
                                    "'", '"')
                query = "update stock_picking set sunat_request_status = 'OK', api_message = '" + str(
                    api_message) + "', response_document_filename = '" + str(
                        response_document_filename
                    ) + "', sunat_request_type = 'Manual' where id = " + str(
                        stock_picking_id)
                request.cr.execute(query)

                #if(serieConsecutivo=="00000008"):
                #    with open('/home/rockscripts/Documents/data.json', 'w') as outfile:
                #        json.dump(query, outfile)

            else:
                api_message = "ESTADO: " + str(
                    sunatResponse["status"]) + "\n" + " " + str(
                        sunatResponse["body"]).replace(
                            "'", '"') + "\n" + "CÓDIGO ERROR: " + str(
                                sunatResponse["code"]).replace("'", '"')
                query = "update stock_picking set sunat_request_status = 'FAIL', api_message = '" + str(
                    api_message
                ) + "', sunat_request_type = 'Manual' where id = " + str(
                    stock_picking_id)
                request.cr.execute(query)

                #if(serieConsecutivo):
                #    with open('/home/rockscripts/Documents/data.json', 'w') as outfile:
                #        json.dump(serieConsecutivo, outfile)

        else:
            sunatResponse["status"] = "FAIL"
            api_message = "Servidor no disponible temporalmente."
            query = "update stock_picking set sunat_request_status = 'FAIL', api_message = '" + str(
                api_message
            ) + "', sunat_request_type = 'Manual' where id = " + str(
                stock_picking_id)
            request.cr.execute(query)

        response = {
            "sunat_request_status": sunatResponse["status"],
            "api_message": api_message
        }

        return response
Beispiel #2
0
    def edocs_submit_guides(self, **kw):
        
        query = "select nextcall from ir_cron where cron_name = '"+str("sunat_edocs")+"'"
        request.cr.execute(query)
        cron_job_edocs = request.cr.dictfetchone()
        nextcall_datetime = str(cron_job_edocs["nextcall"]).split(" ")
        guide_date_limit = nextcall_datetime[0]
        
        query = "select id, name, company_id, unsigned_document, signed_document, response_document from stock_picking where name like '%T001%' and create_date <= '"+str(guide_date_limit)+"' and (sunat_request_status = '"+str("FAIL")+"' or sunat_request_status = '"+str("No Emitido")+"' or sunat_request_status = '"+str("not_requested")+"') and name like '%T001%'"
        request.cr.execute(query)
        guides_unsubmited = request.cr.dictfetchall()        

        #with open('/odoo_peru_sunat/custom/addons/sunat_fact/models/log.json', 'w') as outfile:
        #    json.dump(query, outfile) 
        
        for guide_unsubmited in guides_unsubmited:               
            query = "select res_partner.vat, res_company.api_mode, res_company.sol_ruc, res_company.sol_username, res_company.sol_password, res_company.certs from res_company left join res_partner on res_partner.company_id = res_company.id where res_company.id = "+str(guide_unsubmited['company_id'])+" and res_partner.is_company = TRUE and res_company.partner_id = res_partner.id"
            
            request.cr.execute(query)
            company_fields = request.cr.dictfetchone()                   

            serieParts = str(guide_unsubmited["name"]).split("-")             
            serieConsecutivoString = serieParts[0]
            serieConsecutivo = serieParts[1]
            
            sunat_data = {
                            "secuencia_consecutivo":serieConsecutivo,
                            "sunat_sol": {
                                            "ruc":company_fields["sol_ruc"],
                                            'usuario':company_fields["sol_username"],
                                            'clave':company_fields["sol_password"]
                                         },
                            "xml":  {
                                        #"signed":base64.b64decode((invoice_fields["signed_document"]))
                                    },
                            "licencia": "081OHTGAVHJZ4GOZJGJV"
                        }            

            xmlPath = str(os.path.dirname(os.path.abspath(__file__))).replace("controllers","models")+'/xml'
            SunatService = Service()
            SunatService.setXMLPath(xmlPath)
            
            if("T001" in serieConsecutivoString):
                SunatService.fileName = str(company_fields["vat"])+"-09-"+str(serieConsecutivoString)+"-"+str(serieConsecutivo)
            
            SunatService.initSunatAPI(company_fields["api_mode"], "sendBill")
            sunatResponse = SunatService.processDeliveryGuideFromSignedXML(sunat_data)
                                    
            if(sunatResponse["status"] == "OK"):
                # save xml documents steps for reference in edocs
                response_document_filename = str("R_")+"-09-"+str(serieConsecutivoString)+"-"+str(serieConsecutivo)+str(".XML")

                api_message = "ESTADO: "+str(sunatResponse["status"])+"\n"+"REFERENCIA: "+str(sunatResponse["body"]["referencia"]).replace("'",'"')+"\n"+" "+str(sunatResponse["body"]["description"]).replace("'",'"')
                query = "update stock_picking set sunat_request_status = 'OK', api_message = '"+str(api_message)+"', response_document_filename = '"+str(response_document_filename)+"', sunat_request_type = 'Automatizada' where id = "+str(guide_unsubmited["id"])
                request.cr.execute(query)
                #if(serieConsecutivo=="00000008"):
                #    with open('/home/rockscripts/Documents/data.json', 'w') as outfile:
                #        json.dump(query, outfile)
            else:
                api_message = "ESTADO: "+str(sunatResponse["status"])+"\n"+" "+str(sunatResponse["body"]).replace("'",'"')+"\n"+"CÓDIGO ERROR: "+str(sunatResponse["code"]).replace("'",'"')
                query = "update stock_picking set sunat_request_status = 'FAIL', api_message = '"+str(api_message)+"', sunat_request_type = 'Automatizada' where id = "+str(guide_unsubmited["id"])
                request.cr.execute(query)