简体   繁体   中英

Auto increment Invoice ID in Code-igniter

i am very new to code igniter /php .

Before i was using randomly generated invoice number like

$invoice_no = rand(9999,9999999999);

But now i wanted to increment invoice number and add current year as a prefix to it . But somewhere i am doing wrong as this code failed execute . Can some one point me in the right direction .

My model is ...

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 is primary key in DB .

You're attempting to increment the result array but what you really need is to acquire and increment a field value.

//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 ++;
...

I'm guessing you're getting an "Object conversion to String error" on line $invoice_no = $curYear . '-' .$result; $invoice_no = $curYear . '-' .$result; Since $result contains an object and you're using it as a string. Print the $result variable to check how to use the data assigned to it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM