销售订单行中移动行的分配数量

时间:2018-09-06 09:56:56

标签: odoo odoo-8 odoo-9

我想在sales.order.line中显示分配的每个产品数量。当我有1个销售订单行时,它可以工作,但如果我有更多销售订单,则所有产品的数量相同。但是我的代码很糟糕,因为有1个以上的动作,所以我该怎么做呢?

reserved_qty = fields.Float(string='Reserved Qty', compute='_get_qtys') 


@api.depends('order_id.picking_id.move_lines.state')
    def _get_qtys(self):
        for line in self:
            domain = [('id', '=', line.order_id.picking_id.move_lines.id),
                      ('product_id', '=', line.product_id.id)
                      ]
            movez = self.env['stock.move'].search(domain)
            line.not_fulfiled_qty = line.product_uom_qty - line.qty_delivered
            if line.order_id.picking_id:
                for picking in line.order_id.picking_id:
                    for move in picking.move_lines:
                        if move.state == 'assigned':
                            line.reserved_qty = movez.product_uom_qty

更新:

如果找到了可以正常工作的解决方案,但仍然有一个问题,是否可以有多个采购ID?在某些情况下会出现单例错误吗?

 
@api.depends('order_id.picking_id.move_lines.state')
    def _get_qtys(self):
        for line in self:
            line.not_fulfiled_qty = line.product_uom_qty - line.qty_delivered
            if line.order_id.picking_id:
                for move in line.procurement_ids.move_ids:
                    if move.state == 'assigned':
                        line.reserved_qty = move.product_uom_qty

1 个答案:

答案 0 :(得分:1)

即使有更多的采购,也只需更改代码以处理这种情况:

$apns_port = 2195;
$apns_url = 'gateway.push.apple.com';
$pathCk = $this->container->get('kernel')->locateResource('@MainAdminBundle/Controller/client.pem');
$passphrase = '123';
if (!empty($apns_url) && $apns_url != "") {                 
    $data = json_decode($message);
    $response = $response1 = "";                  
    $payload['job_id'] = $data->code;
    $code = $data->code;
    $detail =  $data->detail;                    
    $ctx = stream_context_create();
    stream_context_set_option($ctx, 'ssl', 'local_cert', $pathCk);
    stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
    $apns = stream_socket_client('ssl://' . $apns_url . ':' . $apns_port, $err, $errstr, 60, STREAM_CLIENT_CONNECT | STREAM_CLIENT_PERSISTENT, $ctx);
    stream_set_blocking($apns, 0);                  
    foreach ($apns_user_list as $key => $val) {//                      
        $device = $val['apns_regid']; 
        if ( $apns != NULL && $apns != '' && !empty($apns) && $apns!= false) {
            $payload['aps'] = array(
                'alert' => $data->detail,
                'badge' => 1,
                'sound' => 'default',
                'response' => $data->response
            );
            $final_payload = json_encode($payload);
            $apnsMessage = chr(0) . pack('n', 32) . pack('H*', str_replace(' ', '', $device)) . pack('n', strlen($final_payload)) . $final_payload;
            $result = '';  
            try {
                $result = fwrite($apns, $apnsMessage , strlen($apnsMessage));
                $resp = $this->checkAppleErrorResponse($apns); 
                var_dump($resp);
                exit;
            } catch (\Exception $e) {
                fclose($apns);
                $result = $e->getMessage();
                echo('<br>Error sending Device: ' . $device );
                echo('<br>Error sending payload: ' . $e->getMessage());
                $current .= '<br>Error sending Device: ' . $device ;
                $current .= '<br>Error sending payload: ' . $e->getMessage() ;
                $ctx = stream_context_create();
                stream_context_set_option($ctx, 'ssl', 'local_cert', $pathCk);
                stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
                $apns = stream_socket_client('ssl://' . $apns_url . ':' . $apns_port, $err, $errstr, 60, STREAM_CLIENT_CONNECT | STREAM_CLIENT_PERSISTENT, $ctx);
                stream_set_blocking($apns, 0);
                echo "in catch";
                exit;
            }
        }
    }
}
// FUNCTION to check if there is an error response from Apple Returns TRUE if there was and FALSE if there was not
public function checkAppleErrorResponse($fp) {
    //byte1=always 8, byte2=StatusCode, bytes3,4,5,6=identifier(rowID). 
    // Should return nothing if OK.
    //NOTE: Make sure you set stream_set_blocking($fp, 0) or else fread will pause your script and wait 
    // forever when there is no response to be sent. 
    $apple_error_response = fread($fp, 6);
    if ($apple_error_response) {
        // unpack the error response (first byte 'command" should always be 8)
        $error_response = unpack('Ccommand/Cstatus_code/Nidentifier', $apple_error_response);
        if ($error_response['status_code'] == '0') {
            $error_response['status_code'] = '0-No errors encountered';
        } else if ($error_response['status_code'] == '1') {
            $error_response['status_code'] = '1-Processing error';
        } else if ($error_response['status_code'] == '2') {
            $error_response['status_code'] = '2-Missing device token';
        } else if ($error_response['status_code'] == '3') {
            $error_response['status_code'] = '3-Missing topic';
        } else if ($error_response['status_code'] == '4') {
            $error_response['status_code'] = '4-Missing payload';
        } else if ($error_response['status_code'] == '5') {
            $error_response['status_code'] = '5-Invalid token size';
        } else if ($error_response['status_code'] == '6') {
            $error_response['status_code'] = '6-Invalid topic size';
        } else if ($error_response['status_code'] == '7') {
            $error_response['status_code'] = '7-Invalid payload size';
        } else if ($error_response['status_code'] == '8') {
            $error_response['status_code'] = '8-Invalid token';
        } else if ($error_response['status_code'] == '255') {
            $error_response['status_code'] = '255-None (unknown)';
        } else {
            $error_response['status_code'] = $error_response['status_code'] . '-Not listed';
        }
        echo '<br><b>+ + + + + + ERROR</b> Response Command:<b>' . $error_response['command'] . '</b>&nbsp;&nbsp;&nbsp;Identifier:<b>' . $error_response['identifier'] . '</b>&nbsp;&nbsp;&nbsp;Status:<b>' . $error_response['status_code'] . '</b><br>';

        echo 'Identifier is the rowID (index) in the database that caused the problem, and Apple will disconnect you from server. To continue sending Push Notifications, just start at the next rowID after this Identifier.<br>';
        return true;
    }
    return false;
}
相关问题