自动增加代码点火器中的发票ID

时间:2017-04-15 17:11:59

标签: php mysql codeigniter

我对代码igniter / php很新。

在我使用随机生成的发票编号之前

$invoice_no = rand(9999,9999999999);

但现在我想增加发票号并将当前年份作为前缀添加到其中。但是在某些地方我做错了,因为这段代码执行失败了。有人可以指出我正确的方向。

我的模特是......

function insertInvoice($data)
{
  $this->db->trans_begin();
  $invoice = array();
  if(!empty($data['client_id']))
  {
    $invoice['invoice_client_id'] = $data['client_id'];
  }else{
    $client_data = array(
      'client_name' => $data['customername'],
      'client_address1' => $data['address1']
    );
    $this->db->insert('client_details', $client_data);
    $insert_id = $this->db->insert_id();
    $invoice['invoice_client_id'] = $insert_id;
  }




$query = $this->db->query("SELECT * FROM invoice ORDER BY invoice_id DESC LIMIT 1");
$result = $query->result_array(0);
$result ++;
$curYear = date('Y');
$invoice_no = $curYear . '-' .$result;
$invoice['invoice_no'] = $invoice_no;
$invoice['invoice_subtotal'] = $data['subTotal'];
  $invoice['invoice_tax'] = $data['tax'];
  $invoice['invoice_tax_amount'] = $data['taxAmount'];
  $invoice['invoice_total'] = $data['totalAftertax'];
  $invoice['invoice_total_extra'] = $data['totalextra'];
  $invoice['invoice_rent'] = $data['rent'];
  $invoice['invoice_paid'] = $data['amountPaid'];
  $invoice['invoice_due'] = $data['amountDue'];
  $invoice['invoice_desc'] = $data['notes'];
  $invoice['invoice_items_count'] = $data['item_count'];
  $invoice['invoice_extra_count'] = $data['extra_count'];
  $invoice['invoice_miscellaneous'] = $data['miscellaneous'];
  $this->db->insert('invoice', $invoice);

  $i=1;
  do {
    $items = array(
      'invoice_no' => $invoice_no,
      'item_name' => $data['invoice']['product_name'][$i],
      'item_price' => $data['invoice']['product_price'][$i],
      'item_qty' => $data['invoice']['product_qty'][$i],
      'item_total' => $data['invoice']['total'][$i],
      'item_noof_crate_wait' => $data['invoice']['noof_crate_wait'][$i],
      'item_crate_wait' => $data['invoice']['crate_wait'][$i],
      'item_choot' => $data['invoice']['choot'][$i],
      'item_net_quantity' => $data['invoice']['net_qty'][$i]
      );
    $this->db->insert('invoice_items',$items);
    $i++;
  } while($i<$data['item_count']);
  $j=1;
  do {
    $extraitems = array(
      'invoice_no' => $invoice_no,
      'extra_item_name' => $data['extra']['name'][$j],
      'extra_item_qunatity' => $data['extra']['qty'][$j],
      'extra_item_price' => $data['extra']['price'][$j],
      'extra_item_total' => $data['extra']['total'][$j]
      );
    $this->db->insert('extra_items',$extraitems);
    $j++;
  } while($j<$data['extra_count']);
  if ($this->db->trans_status() === FALSE)
  {
    $this->db->trans_rollback();
    return FALSE;
  }
  else
  {
    $this->db->trans_commit();
    return TRUE;
  }
}

invoice_id是数据库中的主键。

2 个答案:

答案 0 :(得分:1)

您尝试增加结果数组,但您真正需要的是获取并增加字段值。

//you only need one field so ask only for that
$query = $this->db->query("SELECT invoice_id FROM invoice ORDER BY invoice_id DESC LIMIT 1");

//you really should check to make sure $query is set 
// before trying to get a value from it.
//You can add that yourself
//Asked for only one row, so only retrieve one row -> and its contents
$result = $query->row()->invoice_id;
$result ++;
...

答案 1 :(得分:0)

我猜测你正在将#&#34;对象转换为字符串错误&#34;在线$invoice_no = $curYear . '-' .$result; 由于$result包含一个对象,因此您将其用作字符串。打印$result变量以检查如何使用分配给它的数据。