-2

以下のような請求書番号を生成する方法:

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

4 に答える 4

5

3つの列を使用して数値のさまざまな部分を表し、ビューまたは単純な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
于 2012-09-04T14:38:06.913 に答える
1
<?php
// after save to database 
$invoiceCode = 'My Company Logo'.date("y").mysql_insert_id() ;
?>
于 2012-09-04T14:21:31.167 に答える
1

これを試して..

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

于 2014-01-01T10:50:42.763 に答える
-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/>';

?>
于 2012-09-05T04:57:22.053 に答える