1

我正在使用 OpenERP 7 的 XML-RPC API,我正在尝试验证发票并申请付款,但我不知道我应该使用什么模型。

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

任何帮助,将不胜感激。

4

1 回答 1

3

嗨,我想出了如何使用 php 通过 XML-RPC 自动验证对发票的付款。

这就是 openERP 发票工作流程如何用于付款。

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

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

因此,这意味着您应该至少了解 openerp 如何通过研究模块 account 和 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();
    }
    
    ?>
    

    希望这会帮助你。

于 2013-12-09T13:35:14.650 回答