数据建模草稿/报价/订单/发票

发布于 2024-11-02 01:36:29 字数 909 浏览 5 评论 0原文

我目前正在开发一个小项目,需要对以下场景进行建模:

场景

  1. 客户打电话来,他想要一辆新车的报价。
  2. 销售代表登记客户信息。
  3. 销售代表在系统中创建报价,并将一个项目添加到报价中(汽车)。
  4. 销售代表通过电子邮件将报价发送给客户。
  5. 客户接受报价,此时报价不再是报价而是订单。
  6. 销售代表检查订单,一切正常,他为订单开具发票。现在订单不再是订单,而是发票。

想法

我需要一些帮助来找出建模的理想方法,但我有一些想法。

  1. 我认为草稿/报价/发票基本上都是订单。
  2. 草稿/报价/发票需要单独的唯一编号(id),因此我正在考虑为所有这些编号建立单独的表格。

模型

这是我的数据模型v.1.0,请告诉我你的想法。

数据模型 v.1.0 担忧

不过,我对此模型有一些担忧:

  1. 草稿/报价/发票的订单行上可能有不同的项目和价格。在此模型中,所有草稿/报价/发票都连接到同一订单以及订单行,因此不可能有单独的报价行/草稿行/发票行。也许我应该为此创建新表,但是基本上相同的信息将存储在多个表中,这也不好。
  2. 有时两个或多个报价会成为一张发票,这个模型将如何处理这个问题?

如果您对如何更好地建模有任何建议,请告诉我!

编辑:数据模型 v.1.4 在此处输入图像描述

Im currently working on a small project in which I need to model the following scenario:

Scenario

  1. Customer calls, he want an quote on a new car.
  2. Sales rep. register customer information.
  3. Sales rep. create a quote in the system, and add a item to the quote (the car).
  4. Sales rep. send the quote to the customer on email.
  5. Customer accept the quote, and the quote is now not longer a quote but an order.
  6. Sales rep. check the order, everything is OK and he invoice the order. The order is now not longer an order, but an invoice.

Thoughts

I need a bit of help finding out the ideal way to model this, but I have some thoughts.

  1. I'm thinking that both draft/quote/invoice is basically an order.
  2. Draft/quote/invoice need seperate unique numbers(id's) so there for i'm thinking separate tables for all of them.

Model

This is my data model v.1.0, please let me know what you think.

Data model v.1.0
Concerns

I however have som concerns regarding this model:

  1. Draft/quote/invoice might have different items and prices on the order lines. In this model all draft/quote/invoice is connected to the same order and also order lines, making it impossible to have separate quote lines/draft lines/invoice lines. Maybe I shall make new tables for this, but then basically the same information would be stored in multiple tables, and that is not good either.
  2. Sometimes two or more quotes become an invoice, how would this model take care of this?

If you have any tips on how to model this better, please let me know!

EDIT: Data model v.1.4
enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

沒落の蓅哖 2024-11-09 01:36:29

看起来您已经对所有其他事物(报价、订单、草稿、发票)进行了建模,其结构与所有其他事物相同。如果是这种情况,那么您可以将所有相似的属性“推”到一个表中。

create table statement (
    stmt_id integer primary key,
    stmt_type char(1) not null check (stmt_type in ('d', 'q', 'o', 'i')),
    stmt_date date not null default current_date,
    customer_id integer not null  -- references customer (customer_id)
);

create table statement_line_items (
    stmt_id integer not null references statement (stmt_id),
    line_item_number integer not null,
    -- other columns for line items
    primary key (stmt_id, line_item_number)
);

我认为这适用于您所描述的模型,但我认为从长远来看,通过将它们建模为超类型/子类型,您会得到更好的服务。所有子类型共有的列被“向上”推入超类型;每个子类型都有一个单独的表,用于存储该子类型特有的属性。

这个问题及其接受的答案(和评论)说明了博客评论的超类型/子类型设计。 另一个问题与个人和组织有关。 另一个 与人员配备和电话号码有关。

稍后。 。 .

这还没有完成,但我已经没时间了。我知道它不包括行项目。可能还漏掉了别的东西。

-- "Supertype". Comments appear above the column they apply to.
create table statement (
  -- Autoincrement or serial is ok here.
  stmt_id integer primary key,    
  stmt_type char(1) unique check (stmt_type in ('d','q','o','i')),
  -- Guarantees that only the order_st table can reference rows having
  -- stmt_type = 'o', only the invoice_st table can reference rows having
  -- stmt_type = 'i', etc.
  unique (stmt_id, stmt_type),
  stmt_date date not null default current_date,
  cust_id integer not null -- references customers (cust_id)
);

-- order "subtype"
create table order_st (
  stmt_id integer primary key,
  stmt_type char(1) not null default 'o' check (stmt_type = 'o'),
  -- Guarantees that this row references a row having stmt_type = 'o'
  -- in the table "statement".
  unique (stmt_id, stmt_type),
  -- Don't cascade deletes. Don't even allow deletes. Every order given
  -- an order number must be maintained for accountability, if not for
  -- accounting. 
  foreign key (stmt_id, stmt_type) references statement (stmt_id, stmt_type) 
    on delete restrict,
  -- Autoincrement or serial is *not* ok here, because they can have gaps. 
  -- Database must account for each order number.
  order_num integer not null,  
  is_canceled boolean not null 
    default FALSE
);

-- Write triggers, rules, whatever to make this view updatable.
-- You build one view per subtype, joining the supertype and the subtype.
-- Application code uses the updatable views, not the base tables.    
create view orders as 
select t1.stmt_id, t1.stmt_type, t1.stmt_date, t1.cust_id,
       t2.order_num, t2.is_canceled
from statement t1
inner join order_st t2 on (t1.stmt_id = t2.stmt_id);

It looks like you've modeled every one of these things--quote, order, draft, invoice--as structurally identical to all the others. If that's the case, then you can "push" all the similar attributes up into a single table.

create table statement (
    stmt_id integer primary key,
    stmt_type char(1) not null check (stmt_type in ('d', 'q', 'o', 'i')),
    stmt_date date not null default current_date,
    customer_id integer not null  -- references customer (customer_id)
);

create table statement_line_items (
    stmt_id integer not null references statement (stmt_id),
    line_item_number integer not null,
    -- other columns for line items
    primary key (stmt_id, line_item_number)
);

I think that will work for the model you've described, but I think you'll be better served in the long run by modeling these as a supertype/subtype. Columns common to all subtypes get pushed "up" into the supertype; each subtype has a separate table for the attributes unique to that subtype.

This SO question and its accepted answer (and comments) illustrate a supertype/subtype design for blog comments. Another question relates to individuals and organizations. Yet another relating to staffing and phone numbers.

Later . . .

This isn't complete, but I'm out of time. I know it doesn't include line items. Might have missed something else.

-- "Supertype". Comments appear above the column they apply to.
create table statement (
  -- Autoincrement or serial is ok here.
  stmt_id integer primary key,    
  stmt_type char(1) unique check (stmt_type in ('d','q','o','i')),
  -- Guarantees that only the order_st table can reference rows having
  -- stmt_type = 'o', only the invoice_st table can reference rows having
  -- stmt_type = 'i', etc.
  unique (stmt_id, stmt_type),
  stmt_date date not null default current_date,
  cust_id integer not null -- references customers (cust_id)
);

-- order "subtype"
create table order_st (
  stmt_id integer primary key,
  stmt_type char(1) not null default 'o' check (stmt_type = 'o'),
  -- Guarantees that this row references a row having stmt_type = 'o'
  -- in the table "statement".
  unique (stmt_id, stmt_type),
  -- Don't cascade deletes. Don't even allow deletes. Every order given
  -- an order number must be maintained for accountability, if not for
  -- accounting. 
  foreign key (stmt_id, stmt_type) references statement (stmt_id, stmt_type) 
    on delete restrict,
  -- Autoincrement or serial is *not* ok here, because they can have gaps. 
  -- Database must account for each order number.
  order_num integer not null,  
  is_canceled boolean not null 
    default FALSE
);

-- Write triggers, rules, whatever to make this view updatable.
-- You build one view per subtype, joining the supertype and the subtype.
-- Application code uses the updatable views, not the base tables.    
create view orders as 
select t1.stmt_id, t1.stmt_type, t1.stmt_date, t1.cust_id,
       t2.order_num, t2.is_canceled
from statement t1
inner join order_st t2 on (t1.stmt_id = t2.stmt_id);
从此见与不见 2024-11-09 01:36:29

应该有一个表“quotelines”,它类似于“orderlines”。同样,您应该有一个“invoicelines”表。所有这些表都应该有一个“价格”字段(名义上是零件的默认价格)和一个“折扣”字段。您还可以向“报价”、“订单”和“发票”表添加“折扣”字段,以处理现金折扣或特别优惠等事务。不管你写什么,最好有单独的表格,因为报价中的数量和价格可能与客户实际订购的不匹配,而且也可能与你实际提供的数量不同。

我不确定“草稿”表是什么 - 您可能可以将“草稿”表和“发票”表结合起来,因为它们包含相同的信息,其中一个字段包含发票的状态 - 草稿或最终发票。将发票数据与订单数据分开非常重要,因为您可能将根据您的收入(发票)纳税。

“报价”、“订单”和“发票”都应该有一个字段(外键)来保存销售代表的值;该字段将指向不存在的“SalesRep”表。您还可以在“客户”表中添加“销售代表”字段,该字段指向客户的默认代表。该值将被复制到“报价”表中,尽管如果与默认代表不同的代表给出报价,则可以更改该值。同样,当根据报价发出订单以及根据订单生成发票时,应复制此字段。

我可能可以添加更多内容,但这完全取决于您想要制作的系统的复杂程度和详细程度。如果汽车根据其选项进行配置并相应定价,您可能需要添加某种形式的“物料清单”。

There should be a table "quotelines", which would be similar to "orderlines". Similarly, you should have an 'invoicelines' table. All these tables should have a 'price' field (which nominally will be the part's default price) along with a 'discount' field. You could also add a 'discount' field to the 'quotes', 'orders' and 'invoices' tables, to handle things like cash discounts or special offers. Despite what you write, it is good to have separate tables, as the amount and price in the quote may not match what the customer actually orders, and again it may not be the same amount that you actually supply.

I'm not sure what the 'draft' table is - you could probably combine the 'draft' and 'invoices' tables as they hold the same information, with one field containing the status of the invoice - draft or final. It is important to separate your invoice data from order data, as presumably you will be paying taxes according to your income (invoices).

'Quotes', 'Orders' and 'Invoices' should all have a field (foreign key) which holds the value of the sales rep; this field would point to the non-existent 'SalesRep' table. You could also add a 'salesrep' field in the 'customers' table, which points to the default rep for the customer. This value would be copied into the 'quotes' table, although it could be changed if a different rep to the default gave the quote. Similarly, this field should be copied when an order is made from a quote, and an invoice from an order.

I could probably add much more, but it all depends on how complex and detailed a system you want to make. You might need to add some form of 'bill of materials' if the cars are configured according to their options and priced accordingly.

厌味 2024-11-09 01:36:29

向 line_items 添加新列(例如:Status assmallint)

当 quote_line 变为 order_line 时,将您选择的位设置为从 0 到 3 到 1。

但是当 qty 更改时,添加带有新 qte 的新行并保持最后一行不变。

卡德。

Add a new column to line_items ( ex:Status as smallint)

When a quote_line becomes an order_line then set bit you choose from 0 to 3 to 1.

But when qty changes then add a new line with new qte and keep last line unchanged.

Kad.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文