如何使用PHP或MySql生成具有特殊要求的发票号

时间:2012-09-04 14:16:53

标签: php mysql

如何生成发票编号,如下所示:

Invoice Number = MC1200001 

MC : Company name
12 : current year (Flexibel)
00001 : auto increment (Flexibel - auto increment and will reset to 00001 again if year is 2013)

预期结果:

current year 2012 :

MC1200001
MC1200002
MC1200003
.....

year 2013

MC1300001
MC1300002
MC1300003
.....

需要您的想法或解决方案来解决这个问题。

感谢。

4 个答案:

答案 0 :(得分:5)

您可以使用三列来表示数字的不同部分,并使用视图或简单的CONCAT表达式来获取每张发票的整个数字。

然后要获取一个新数字,您将使用一个函数插入一条记录,该函数将返回实际的行ID或连接数。

我会做这样的事情:(为了简洁而留下领先的零)

DROP TABLE IF EXISTS invoces;

CREATE TABLE invoices (
   id INT NOT NULL UNIQUE AUTO_INCREMENT
  ,company CHAR(2)
  ,number INT
  ,fiscal_year INT
 , PRIMARY KEY (company, number, fiscal_year)
);

DROP PROCEDURE IF EXISTS spCreateInvoice;
DELIMITER $$

CREATE PROCEDURE spCreateInvoice
(
   pCompany CHAR(2)
  ,pFiscalYear INT
)
BEGIN
  INSERT INTO invoices (
    company, fiscal_year, number
  ) SELECT
        pCompany
      , pFiscalYear
      , 1+MAX(number) 
    FROM invoices 
    WHERE 
        fiscal_year=pFiscalYear;

  SET @id = LAST_INSERT_ID();

  SELECT CONCAT(i.company, i.number, i.fiscal_year) invoice_number, i.* from invoices i WHERE id = @id;
END;
$$
DELIMITER ;

CALL spCreateInvoice('MC', 12);
CALL spCreateInvoice('MC', 12);
CALL spCreateInvoice('MC', 12);
CALL spCreateInvoice('MC', 12);

CALL spCreateInvoice('MC', 13);
CALL spCreateInvoice('MC', 13);
CALL spCreateInvoice('MC', 13);
CALL spCreateInvoice('MC', 13);

您可以将其作为针对mysql测试数据库的脚本运行,并查看以下输出:

invoice_number  id  company number  fiscal_year
MC012           1   MC      0       12

invoice_number  id  company number  fiscal_year
MC112           2   MC      1       12

invoice_number  id  company number  fiscal_year
MC212           3   MC      2       12

invoice_number  id  company number  fiscal_year
MC312           4   MC      3       12

invoice_number  id  company number  fiscal_year
MC013           5   MC      0       13

invoice_number  id  company number  fiscal_year
MC113           6   MC      1       13

invoice_number  id  company number  fiscal_year
MC213           7   MC      2       13

invoice_number  id  company number  fiscal_year
MC313           8   MC          3   13

答案 1 :(得分:1)

<?php
// after save to database 
$invoiceCode = 'My Company Logo'.date("y").mysql_insert_id() ;
?>

答案 2 :(得分:1)

试试这个..

$ compcode =“MC”;
$ result = $ compcode.date('y')。str_pad($ runningnumber,5,“0”,STR_PAD_LEFT);

答案 3 :(得分:-2)

是的,下面是我的最终解决方案及其工作原理:

<?php
@mysql_connect('localhost','root','') or die('Database error!');
echo "mysql_connect : Connected!</br>";   // if connected show 'Connected!'
echo '</br>';
mysql_select_db("invoice") or die('could not select');

$result4 = mysql_query("SELECT * FROM `order` ORDER BY id desc") or die('could not select');   //Select last transaction
$row4 = mysql_fetch_array($result4);

$invoice4 = $row4['invoice_number'];
$inc_number = $row4['inc_number'];
$year = $row4['year'];

//Setting Invoice Format
$brand = 'GB';
$cur_date = date('y');  // date('y')
$invoice = $brand.$cur_date.'00001';
$customer_id = rand(5487 , 9854);

if($cur_date == $year) {   //if current year equal to last year in transaction 

    if($invoice4 == $invoice && $inc_number == '1') {
        //IF EXIST 

        //add inc_number 
        $inc_number_add = $inc_number + 1; // Increment by 1

        //invoice_number
        $inc = str_pad($inc_number_add, 5, '0', STR_PAD_LEFT); //Format with leading 0 eg: 00001
        $invoice_number_db = $brand.$cur_date.$inc;

        //create new order
        mysql_query("INSERT INTO `order` (customer_id,invoice_number,inc_number,year) VALUES ($customer_id, '$invoice_number_db' ,$inc_number_add,$cur_date)") 
        or die('Cannot Insert into database!');
        echo "<br/>Insert Success!</br>";

    }
    else {
        //IF NOT EXIST 
            mysql_query("INSERT INTO `order` (customer_id,year) VALUES ($customer_id,$cur_date)") or die('Cannot Insert into database!');
            echo "Insert customer_id Success!";

            echo '</br>';

            // Retrieve data again after create
            $result = mysql_query("SELECT * FROM `order` WHERE customer_id='$customer_id'") or die('could not select');
            $row = mysql_fetch_array($result);

            //var
            $id_val = $row['id'];
            $inc_number_add = $inc_number + 1;  // Increment by 1
            $inc = str_pad($inc_number_add, 5, '0', STR_PAD_LEFT); //Format with leading 0 eg: 00001
            $invoice = $brand.$cur_date.$inc;

            //Update invoice
            mysql_query("UPDATE `order` SET invoice_number='$invoice' , inc_number='$inc_number_add' WHERE customer_id=$customer_id") or die('error 89');
            echo "</br>UPDATE invoice_number Success!</br>";
    }

}
    else {

        //IF TODAY IS NEW YEAR RESET INVOICE NUMBER
        mysql_query("INSERT INTO `order` (customer_id,invoice_number,inc_number,year) VALUES ($customer_id, '$invoice' , '1' ,$cur_date)") 
        or die('Cannot Insert into database!');
        echo "<br/>Insert customer_id Success!</br>";

    }

        echo "<br/><br/>FINAL OUTPUT <br/>";
        $result_3 = mysql_query("SELECT * FROM `order` WHERE customer_id='$customer_id' ") or die('could not select');
            $row_3 = mysql_fetch_array($result_3);

            $id3 = $row_3['id'];
            $customer_id3 = $row_3['customer_id'];
            $inc_number = $row_3['inc_number'];
            $invoice3 = $row_3['invoice_number'];

            echo 'id '.$id3.' - ';
            echo 'customer_id '.$customer_id3.' - ';
            echo 'inc_number '.$inc_number.' - ';
            echo 'invoice '.$invoice3. '<br/>';

?>