2013-04-26 90 views
1

我正在使用OpenERP 7的XML-RPC API,我正在验证发票并应用付款,但我无法弄清楚我应该使用哪种模式。OpenERP 7 API发票验证和付款

我已成功创建发票并添加了行。帐户中似乎没有任何东西.invoice和account.payment.term和payment.order似乎也不起作用。

任何帮助,将不胜感激。

回答

2

嗨,我想通过使用PHP自动通过XML-RPC验证付款发票。

此openERP发票工作流程如何适用于付款。

创建发票时,此发票的付款由凭证完成,即模型account.voucher用于将付款应用于发票。因此,当您创建发票并验证发票时,发票将状态更改为打开状态,您现在可以访问“客户付款”菜单并输入客户名称,然后执行account.voucher中定义的onchange_partner_id()函数以进行计算当前输入合作伙伴的所有发票已打开付款。

然后,当您在“付费金额”字段中输入付款时,将执行onchange_payment()函数以分配支付给选定合作伙伴的不同未结发票的金额。 当您点击支付表单上的验证时,openerp会自动支付发票并将其标记为已付款。

因此,这意味着您应该至少了解openerp如何通过研究模块帐户和account_voucher中的python代码来处理发票和付款。为了长话短说,这是我使用的代码。

  • 首先为特定合作伙伴创建发票并进行验证。请确保您的合作伙伴在res.partner模型中设置了有效的电话号码。 (刚开始使用OpenERP的形式创建)

    ** * ** * ** * ** * ** PHP代码 ** * ** * ** * *

    <?php 
    
    error_reporting(1); 
    /* 
    * TODO 
    * Improve on handling of server responses. Success and Failures 
    */ 
    date_default_timezone_set('Europe/Moscow'); 
    include_once("openerp_models.php"); 
    ///////////////////////////////////////////////////////////////////// 
    //confirm that a username and password is set 
    if (isset($_SERVER['PHP_AUTH_USER']) && $_SERVER['PHP_AUTH_PW']) { 
    
        //password and username -- Basic Authentication 
        $username = $_SERVER['PHP_AUTH_USER']; 
        $password = $_SERVER['PHP_AUTH_PW']; 
    
        ///important parameters 
        $phone_no = $_REQUEST['sender_phone']; //get the mobile number. Use this number to get the partner information. 
        $amount = $_REQUEST['amount']; // get amount from url 
        $amount_again = $_REQUEST['amount']; //this will be displayed to the paying client. $amount above is reduced to zero hence 
                  //we create another amount variable to show the customer with success message. 
        $payment_ref = $_REQUEST['k2_transaction_id']; // get this from url 
        $firstname = $_REQUEST['first_name']; 
        $lastname = $_REQUEST['last_name']; 
        $middlename = $_REQUEST['middle_name']; 
        $service_name = $_REQUEST['service_name']; 
        $business_number = $_REQUEST['business_number']; 
        $transaction_reference = $_REQUEST['transaction_reference']; 
        $internal_transaction_id = $_REQUEST['internal_transaction_id']; 
        $transaction_timestamp = $_REQUEST['transaction_timestamp']; 
        $transaction_type = $_REQUEST['transaction_type']; 
        $account_number = $_REQUEST['account_number']; 
        $currency = $_REQUEST['currency']; 
        $signature = $_REQUEST['signature']; 
    
    
    
        //openerp instance, pass login,password,dbname and serverURL 
        $kengen_model = new OpenERPXmlrpc($username, $password, 'copia_training_db', 'http://127.0.0.1:8069/xmlrpc/'); 
        /* TODO UNCOMMENT THIS CODE FOR ACTUAL TESTING WITH KOPOKOPO SERVER 
        * We will then authorize kopokopo to continue with record creation. But before that, 
        * we should make sure that the request is actually coming from kopokopo using the signature value sent. 
        * To learn more see https://app.kopokopo.com/push_api 
        * 
        //set up base string 
        $base_string = "account_number=".$account_number."&amount=".$amount."&business_number=".$business_number."&" 
          . "currency=".$currency."&first_name=".$firstname."&internal_transaction_id=".$internal_transaction_id."&" 
          . "last_name=".$lastname."&middle_name=".$middlename."&sender_phone=".$phone_no."&service_name=".$service_name."&transaction_reference=".$transaction_reference."&" 
          . "transaction_timestamp=".$transaction_timestamp."" 
          . "transaction_type=".$transaction_type." "; 
        //get the symmetric key from table keys.api. This key should be copied from Your KopoKopo Account 
    
        $get_key = $kengen_model->search('keys.api', 'provider_name', '=', 'KopoKopo') ; 
    
        $key_id = $get_key[0]; 
    
        $key_value = '' ; 
        //we read 
          $read_keys = $kengen_model->read('keys.api', [$key_id]); 
          //loop 
          foreach ($read_keys as $keys => $values) { 
          $value = $values->scalarval(); 
          // print_r($value); 
          // print '<br/>'; 
          //$myarray [] = array($value['name']->scalarval()); 
          $key_value = $value['key_value']->scalarval(); 
    
    
         } 
        // print $key_value; exit; 
        //data to use 
        $hash_data = hash_hmac('sha256', $base_string, $key_value, true); 
        //generate our own signature to compare with 
        $signature_to_test = base64_encode($hash_data) ; 
    
        ///test if the parameter $signature passed by KopoKopo is equal to $signature_to_test 
        // print $signature_to_test; 
    
        * TODO, DO the actual testing with Real KEY_VALUE and Request from kopokopo and only allow execution if 
        * $signature_to_test = $signature else return an access denied status 
        */ 
    
        ///////////////////////////////////////// 
        /* We retrieve the payment journal for kopokopo. We will 
        * use the journal code to retrieve the journal id. 
        * The code for kopokopo should be KPKPJ 
        */ 
        $get_jrnl_code = $kengen_model->search('account.journal', 'code', '=', 'KPKPJ') ; 
        $journal_id = $get_jrnl_code[0]; //kopokopo journal 
        /* 
        * We retrieve the account_id to pay to. This code is code for KopoKopo Account 
        * should be static i.e. 2040 is the account unless otherwise specified. 
        */ 
        $get_acc_code = $kengen_model->search('account.account', 'code', '=', '2040') ; 
        $account_id = $get_acc_code[0]; //kopokopo account 
        //for this record to appear on list of customer payment 
        $type = 'receipt'; 
        // 
    
        /* TODO 
        * after a successful login, we must authorize this user. We do this to make sure that 
        * the request if coming from kopokopo server. 
        */ 
    
        //now search for the partner using phone/mobile number parameter 
        $search_partner = $kengen_model->search('res.partner', 'phone', '=', $phone_no); 
        //check to make sure that the customer exists 
        //create an array 
        $ids = array(); 
        //loop through the $search content and assign its contents to the $ids 
        for ($i = 0; $i <= count($search_partner); $i++) { 
         // print $search_partner[$i]; 
         $ids [] = $search_partner[$i]; 
        } 
    
        // if a user exist we continue processing 
        if (count($ids[0]) > 0) { 
         //perform a read, by picking the first item on the array 
         $read = $kengen_model->read('res.partner', [$ids[0]]); 
         // print_r($read); 
         $myarray = null; 
    
         $client_name = ''; 
         //this foreach loop with only loop once. hence just retrieve the client name and amount saved 
         // in openerp 
    
         foreach ($read as $keys => $values) { 
          $value = $values->scalarval(); 
    
          // print '<br/>'; 
          //$myarray [] = array($value['name']->scalarval()); 
          $client_name = $value['name']->scalarval(); 
         } 
         ///////////////////////////////////////////////////// 
         //get invoices, pass partner_id and invoice status which is open for unpaid invoices 
         //get invoices to retrieve the journal id 
         $get_invoices = $kengen_model->getPartnetInvoices('account.voucher', $ids[0], 'open'); 
         $retrieved_pay_account_id = 0; 
         $pay_journal_id = 0; 
         $period_id = 0; 
         foreach ($get_invoices as $keys => $values) { 
          $value = $values->scalarval(); 
    
          $retrieved_pay_account_id = $value[4]->scalarval(); 
          $pay_journal_id = $value[5]->scalarval(); 
          $period_id = $value[6]->scalarval(); 
          $move_id = $value[7]->scalarval(); 
         } 
         // print $retrieved_account_id; 
         ///////////////////////////////////////////////////// 
         //fields to create 
         $account_voucher_fields = array(
          'partner_id' => $ids[0], 
          'amount' => $amount, 
          'reference' => $payment_ref, 
          'journal_id' => $journal_id, 
          'account_id' => $account_id, 
          // 'move_id' => $move_id , 
          // 'journal_id'=> $retrieved_journal_id, 
          'type' => $type); 
         //voucher payment 
          $create_kopokopo_payment = $kengen_model->createRecord($account_voucher_fields, 'account.voucher'); 
         //we get the total amount of invoice available for this customer. 
         $total_invoices_amount = $kengen_model->getPartnetInvoices('account.voucher', $ids[0], 'open'); 
         $invoice_totals = 0; 
         foreach ($total_invoices_amount as $keys => $values) { 
          $value = $values->scalarval(); 
          $invoice_totals += $value[3]->scalarval(); 
          /// 
         } 
    
         // print 'invoice totals is '.$invoice_totals ; 
         //voucher line payment 
         //we will retrieve all invoices available for this partner 
         //get invoices, pass partner_id and invoice status which is open for unpaid invoices 
         $invoices = $kengen_model->getPartnetInvoices('account.voucher', $ids[0], 'open'); 
         //loop through available invoices. Remember that a customer can have more than one open invoice 
         $total_credit = 0; 
         foreach ($invoices as $keys => $values) { 
          $value = $values->scalarval(); 
          $number = $value[1]->scalarval(); 
          $invoice_amount_to_pay = $value[3]->scalarval(); 
          /* 
          * To undestand how this code works look at account.voucher model in openerp 
          * function recompute_voucher_lines(). This openerp function calculates voucher lines given 
          * a particular payment. 
          */ 
          $min_amount = min($invoice_amount_to_pay, $amount); 
          // 
          /* print 'x'; 
          print 'on existing invoices in ELSE IF >>>' . ($min_amount); 
          print '<br/>'; 
          print 'amount_unreconciled is >>>' . ($invoice_amount_to_pay); 
          print '<br/>'; 
          print 'total_credit is >>>' . $amount; 
          print '<br/>'; */ 
          /// 
          //reduce amount paid 
          $amount -= $min_amount; 
          //convert amount into int 
          $amount_to_allocate = intval($min_amount); 
          // print $amount_total ; 
          ///get invoice move line ids 
          $new_number = str_replace('/', '', $number); //convert the invoice line 
          $move_ids = $kengen_model->search('account.move.line', 'ref', '=', $new_number); 
          ///// 
          $account_voucher_line_fields = array(
           'partner_id' => $ids[0], 
           'voucher_id' => $create_kopokopo_payment, //last id inserted in account.voucher 
           'account_id' => $retrieved_pay_account_id, 
           'move_line_id' => $move_ids[0], 
           'amount' => $amount_to_allocate, 
           'type' => 'cr', 
           'name' => $number,); 
          ////// 
           $create_kopokopo_line_payment = $kengen_model->createRecord($account_voucher_line_fields, 
             'account.voucher.line'); 
         } 
         /* 
         * we validate the payment. We access method button_proforma_voucher declared in model account.voucher 
         * This methods validates an invoice payment and does reconcilation process for us if the user has paid 
         * fully else the method reconciles invoice partially. 
         */ 
         $validate_voucher_payment = $kengen_model->call_function_func('account.voucher', 
          'button_proforma_voucher', array($create_kopokopo_payment)); 
    
    
    
         //customer found. Return a json response for KopoKopo 
         $message = "Thank you " . $client_name . " for your payment of Ksh " . $amount_again . ". We value your business"; 
         // see doc @ https://app.kopokopo.com/push_api 
         $success_response = array("status" => "01", "description" => "Accepted", 
          "subscriber_message" => $message); 
         echo json_encode($success_response); 
         return json_encode($success_response); 
        } 
    
        // else we return a json_response with error message 
        else { 
         //customer not found. Return a json response for KopoKopo 
         // see doc @ https://app.kopokopo.com/push_api 
         $error_response = array("status" => "02", "description" => "Account not found"); 
         echo json_encode($error_response); 
         return json_encode($error_response); 
        } 
    } else { 
        header('WWW-Authenticate: Basic realm="Copia ERP"'); 
        header('HTTP/1.0 401 Unauthorized'); 
        print 'Access Denied'; 
        exit(); 
    } 
    
    ?> 
    

    希望这会对你有所帮助。