Просмотр исходного кода

More functionality added and wite space wiped.

mingodad 13 лет назад
Родитель
Сommit
9198f56bcb
2 измененных файлов с 362 добавлено и 155 удалено
  1. 29 30
      SquiLu-ourbiz/ourbiz-fltk.nut
  2. 333 125
      SquiLu-ourbiz/ourbiz.nut

+ 29 - 30
SquiLu-ourbiz/ourbiz-fltk.nut

@@ -340,7 +340,7 @@ class Base_Window extends Fl_Window {
 		_db_map = {};
 		if(_sab == null) _sab = "A";
 	}
-	
+
 	function on_close_delete_cb(sender, udata){
 		if(sender){
 			if(!sender.as_window()) throw(_tr("Only windows can use this callback !"));
@@ -352,7 +352,7 @@ class Base_Window extends Fl_Window {
 			Fl.delete_widget(sender);
 		}
 	}
-	
+
 	function show_help_window(sender=null, udata=null){
 		if(sender) this = sender->window();
 		if(!app_help_window){
@@ -482,7 +482,7 @@ class Base_Window extends Fl_Window {
 			choice->add_item(rec[0].tointeger(), rec[1]);
 		}
 	}
-	
+
 	function insert_group_tree_childs (tree, node, parent, pos, size, data)
 	{
 		local new_node;
@@ -521,7 +521,7 @@ class Base_Window extends Fl_Window {
 		tree->shaded_entry_colors( FL_WHITE, FL_GRAY );
 		tree->selection_follows_hilight( true );
 	}
-	
+
 	function treeLoadChilds (tree, node, parent, _table_name)
 	{
 		local data = [];
@@ -799,7 +799,7 @@ class Edit_Base_Window extends Base_Window {
 	function do_import(){/*fl_alert("do_delete");*/}
 	function do_refresh(){/*fl_alert("do_delete");*/}
 	function do_copy(){/*fl_alert("do_delete");*/}
-	
+
 	function dbUpdater(dbu=null){
 		if(dbu){
 			_dbUpdater = dbu;
@@ -1147,7 +1147,7 @@ dofile("fl-bar-chart.nut");
 dofile("calendar-utils.nut");
 
 class MyBarChart extends BarChartGroup {
-	
+
 	constructor(){
 		base.constructor();
 	}
@@ -1159,7 +1159,7 @@ class MyBarChart extends BarChartGroup {
 			btn_periode_is_days->value() ? "days" :
 			"months";
 	}
-	
+
 	function periodes (){
 		local val = periodes_to_show->value();
 		return val.len() ? val.tointeger() : 0;
@@ -1232,7 +1232,7 @@ class MyListSearchWindow extends ListSearchWindow {
 		win.show();
 		win.do_edit(edit_id);
 	}
-	
+
 	function search_for_me(search_for_str, callee_func){
 		_callee_cb = callee_func;
 		btnSelect->activate();
@@ -1425,7 +1425,7 @@ class OurTreeGroups extends GroupsListEditWindow {
 		on_Change_dbAction();
 		delayed_focus(db_group_description);
 	}
-	
+
 	function on_wrap_notes_cb(sender, udata)
 	{
 		this = sender->window();
@@ -1461,7 +1461,7 @@ class OurDynamicQuery extends DynamicQueryWindow
 {
 	_callee = null;
 	_callee_func = null;
-	
+
 	constructor(){
 		base.constructor();
 		setup_grid(grid, this);
@@ -1525,7 +1525,7 @@ class OurImages extends ImagesListEditWindow {
 		appServer.images_get_list(grid->_data, _search_options);
 		grid->recalc_data();
 	}
-	
+
 	function do_edit(aid){
 		base.do_edit(aid);
 		if(aid) button_show_db_image(btnImage, aid.tointeger(), 0, false, false);
@@ -2118,22 +2118,21 @@ class MyEditProductWindow extends EditProductWindow {
 		_ourBarChart = new MyBarChart();
 		replace_widget_for(tabChartStatistics, _ourBarChart);
 		_ourBarChart->btnShowChart.callback(on_show_chart_cb);
-		
+
 		_ourHistory = new HistoryGroup();
 		replace_widget_for(tabHistory, _ourHistory);
 		local choice =  _ourHistory->history_choice;
-		foreach(opt in _history_options)
-		    choice->add(_tr(opt));
+		foreach(opt in _history_options) choice->add(_tr(opt));
 		choice->value(0);
 		choice.callback(on_history_cb);
-		
+
 		_ourProductKit = new ProductKitGroup();
 		replace_widget_for(tabKit, _ourProductKit);
 		//_ourProductKit->btnShowChart.callback(on_show_chart_cb);
 
 		_ourProductPrices = new ProductPricesGroup();
 		replace_widget_for(productPrices, _ourProductPrices);
-		
+
 		tabsMoreData.callback(tabsMoreData_cb);
 
 		load_aux_data();
@@ -2159,9 +2158,9 @@ class MyEditProductWindow extends EditProductWindow {
 		local sales_tax_data = [], measure_units_data = [], warranty_data = [];
 
 		appServer.get_product_aux_data(
-				    sales_tax_data,
-				    measure_units_data,
-				    warranty_data);
+				sales_tax_data,
+				measure_units_data,
+				warranty_data);
 
 		fill_choice_by_data(db_products_sales_tax_id, sales_tax_data);
 
@@ -2299,7 +2298,7 @@ class ProductsListSearch extends MyListSearchWindow {
 					{
 						btnThumbImage.hide();
 						btnThumbImage.image(null);
-						btnThumbImage.show();						
+						btnThumbImage.show();
 					}
 					_fetching_image = false;
 				}
@@ -2428,12 +2427,11 @@ class MyEditOrderWindow extends EditOrderWindow {
 		chk->label("><");
 		chk->set_visible();
 		chk->tooltip(_tr("Show payment types separately."));
-		
+
 		_ourHistory = new HistoryGroup();
 		replace_widget_for(tabHistory, _ourHistory);
 		local choice =  _ourHistory->history_choice;
-		foreach(opt in _history_options)
-		    choice->add(_tr(opt));
+		foreach(opt in _history_options) choice->add(_tr(opt));
 		choice->value(0);
 		choice.callback(on_history_cb);
 
@@ -2452,14 +2450,14 @@ class MyEditOrderWindow extends EditOrderWindow {
 		appServer.payment_types_get_short_list(data);
 		fill_choice_by_data(db_orders_payment_type_id, data);
 	}
-	
+
 	function get_record_by_id(id){
 		local lines = grid_lines->_data;
 		grid_lines->clear_data_rows();
 		appServer.get_record_and_array(_record, lines, dbUpdater()->table_name, 0, 
 			dbUpdater()->edit_id, "&with_lines=1");
 	}
-	
+
 	function do_edit_delayed(udata){
 		base.do_edit_delayed(udata);
 		local lines = grid_lines->_data;
@@ -2640,6 +2638,7 @@ class MyEditOrderWindow extends EditOrderWindow {
 			_ourBarChart->periodes(), _ourBarChart->periode_type() , paidUpaid);
 		_ourBarChart->show_bar_chart(mydata, paidUpaid, (_sab == "A"));
 	}
+
 	function on_history_cb(sender, udata){
 		this = sender->window();
 		local cursor_wait = fl_cursor_wait();
@@ -2860,7 +2859,7 @@ class OrdersBuysListSearch extends OrdersListSearch {
 		base.constructor(doInitialSearch);
 		label(_tr("Orders/Buys List Search"));
 	}
-	
+
 	function get_edit_window(){
 		return getChildWindow("Order/Buys Edit", MyEditOrderBuysWindow);
 	}
@@ -2922,21 +2921,21 @@ class MyMainWindow extends MainWindow {
 		btnPaymentsSales->callback(cb_btnPaymentsSales);
 		btnEntitiesSales->callback(cb_btnEntitiesSales);
 		btnProductsSales->callback(cb_btnProductsSales);
-		
+
 		btnOrdersBuys->callback(cb_btnOrdersBuys);
 		btnPaymentsBuys->callback(cb_btnPaymentsBuys);
 		btnProductsBuys->callback(cb_btnProductsBuys);
 		btnEntitiesBuys->callback(cb_btnEntitiesBuys);
-		
+
 		btnOrders->callback(cb_btnOrders);
 		btnPayments->callback(cb_btnPayments);
 		btnEntities->callback(cb_btnEntities);
 		btnProducts->callback(cb_btnProducts);
-		
+
 		btnGLGroups->callback(cb_btnGLGroups);
 		btnGLChart->callback(cb_btnGLChart);
 		btnGLTransactions->callback(cb_btnGLTransactions);
-		
+
 		btnOrdersSum->callback(cb_btnOrdersSum);
 		btnSalesTaxRates->callback(cb_btnSalesTaxRates);
 		btnOrderTypes->callback(cb_btnOrderTypes);

+ 333 - 125
SquiLu-ourbiz/ourbiz.nut

@@ -21,15 +21,15 @@ if(globals.get("AT_DEV_DBG", false) || !globals.get("PdfSqlTable", false)) {
 }
 
 function escape_sql_like_search_str(str){
-    if (str && (str.len() > 0)){
-        str = str.gsub("%%", "%%%%");
-        if (str.find(" ") == 0)
-            str = str.gsub("^%s*(.+)%s*$","%1%%");
-        else
-            str = str.gsub("^%s*(.+)%s*$","%%%1%%");
-    }
-    //print("escape_sql_like_search_str +" + str + "+")
-    return str;
+	if (str && (str.len() > 0)){
+		str = str.gsub("%%", "%%%%");
+		if (str.find(" ") == 0)
+			str = str.gsub("^%s*(.+)%s*$","%1%%");
+		else
+			str = str.gsub("^%s*(.+)%s*$","%%%1%%");
+	}
+	//print("escape_sql_like_search_str +" + str + "+")
+	return str;
 }
 
 function mkEmptyWhenZero(tbl, key){
@@ -41,6 +41,34 @@ function mkEmptyWhenZero(tbl, key){
 	else if (tbl[key] == 0) tbl[key] = "";
 }
 
+function WriteSLEArrayFieldNames(str_buf, stmt, col_count)
+{
+	for(local i=0; i<col_count; ++i)
+	{
+		add2sle(str_buf, stmt.col_name(i));
+	}
+}
+
+function WriteSLEArrayFieldValues(str_buf, stmt, col_count)
+{
+	for(local i=0; i < col_count; ++i)
+	{
+		add2sle(str_buf, stmt.asString(i));
+	}
+}
+
+function WriteMap2SLEArray(str_buf, map, doAppend=false)
+{
+	if(!doAppend) str_buf.clear();
+	str_buf.write("[[");
+	foreach(k,v in map) add2sle(str_buf, k);
+	str_buf.writen(SLE_SLEEND, 'c');
+	str_buf.write("][");
+	foreach(k,v in map) add2sle(str_buf, v);
+	str_buf.writen(SLE_SLEEND, 'c');
+	str_buf.write("]]");
+}
+
 constants.rawdelete("TimePeriode");
 enum TimePeriode {is_years = 1, is_months, is_weeks, is_days};
 
@@ -87,9 +115,9 @@ function get_sql_bar_chart_statistics_periodes (periode_count, periode_type){
 
 function getOptionsFromMap(map){
 	local opt = {};
-        opt.search_str <- map.get("search_str", null);
-        opt.select_fields <- map.get("select_fields", null);
-        opt.search_on <- map.get("search_on", "1"); //to make easy on html interface
+	opt.search_str <- map.get("search_str", null);
+	opt.select_fields <- map.get("select_fields", null);
+	opt.search_on <- map.get("search_on", "1"); //to make easy on html interface
 	local CHECK_BOOL = function(field){
 		local value = map.get(field, null);
 		if (value == "1") opt[field] <- "1";
@@ -102,39 +130,39 @@ function getOptionsFromMap(map){
 	}
 	local CHECK_INT = function(field) {CHECK_INT_DFLT(field, 0) };
 
-        CHECK_BOOL("active");
-        CHECK_BOOL("buys");
-        CHECK_BOOL("cdate");
-        CHECK_BOOL("group_set");
-        CHECK_BOOL("inactive");
-        CHECK_BOOL("mdate");
-        CHECK_BOOL("only_prices_older");
-        CHECK_BOOL("order_by_creation");
-        CHECK_BOOL("order_by_modification");
-        CHECK_BOOL("sales");
-        CHECK_INT("entity_id");
-        CHECK_INT("group_id");
-        CHECK_INT("image_id");
-        CHECK_INT("account_id");
-        CHECK_INT("order_id");
-        CHECK_INT("payment_type_id");
-        CHECK_INT("product_id");
-        CHECK_INT_DFLT("query_limit", 50);
-        CHECK_BOOL("contact");
-        CHECK_BOOL("date");
-        CHECK_BOOL("description");
-        CHECK_BOOL("entities");
-        CHECK_BOOL("id");
-        CHECK_BOOL("name");
-        CHECK_BOOL("notes");
-        CHECK_BOOL("phone");
-        CHECK_BOOL("products");
-        CHECK_BOOL("reference");
-        CHECK_BOOL("total");
-        CHECK_BOOL("with_images");
-        CHECK_BOOL("with_headers");
-        CHECK_BOOL("with_accounts");
-        CHECK_BOOL("code");
+	CHECK_BOOL("active");
+	CHECK_BOOL("buys");
+	CHECK_BOOL("cdate");
+	CHECK_BOOL("group_set");
+	CHECK_BOOL("inactive");
+	CHECK_BOOL("mdate");
+	CHECK_BOOL("only_prices_older");
+	CHECK_BOOL("order_by_creation");
+	CHECK_BOOL("order_by_modification");
+	CHECK_BOOL("sales");
+	CHECK_INT("entity_id");
+	CHECK_INT("group_id");
+	CHECK_INT("image_id");
+	CHECK_INT("account_id");
+	CHECK_INT("order_id");
+	CHECK_INT("payment_type_id");
+	CHECK_INT("product_id");
+	CHECK_INT_DFLT("query_limit", 50);
+	CHECK_BOOL("contact");
+	CHECK_BOOL("date");
+	CHECK_BOOL("description");
+	CHECK_BOOL("entities");
+	CHECK_BOOL("id");
+	CHECK_BOOL("name");
+	CHECK_BOOL("notes");
+	CHECK_BOOL("phone");
+	CHECK_BOOL("products");
+	CHECK_BOOL("reference");
+	CHECK_BOOL("total");
+	CHECK_BOOL("with_images");
+	CHECK_BOOL("with_headers");
+	CHECK_BOOL("with_accounts");
+	CHECK_BOOL("code");
 	return opt;
 }
 
@@ -313,18 +341,18 @@ class DB_Entities extends DB_Manager {
 	function past_products_sql(entity_id){
 		return format([==[
 SELECT
-    product_id as 'id|ID|6|R',
-    sell_description as 'sell_description|Product|-1',
-    sum(quantity) as 'quantity|Quantity|8|R',
-    sum(quantity*price) as 'amount|Amount|8|R|M',
-    code as 'order_type_code|Type|4|C'
-    FROM orders_lines as ol, orders as o, order_types as ot, products as p
-    WHERE o.entity_id = %d
-    and ot.id = o.order_type_id
-    and ol.order_id = o.id
-    and p.id = ol.product_id
-    GROUP BY product_id, code
-    ORDER BY 3 desc
+	product_id as 'id|ID|6|R',
+	sell_description as 'sell_description|Product|-1',
+	sum(quantity) as 'quantity|Quantity|8|R',
+	sum(quantity*price) as 'amount|Amount|8|R|M',
+	code as 'order_type_code|Type|4|C'
+FROM orders_lines as ol, orders as o, order_types as ot, products as p
+WHERE o.entity_id = %d
+	and ot.id = o.order_type_id
+	and ol.order_id = o.id
+	and p.id = ol.product_id
+GROUP BY product_id, code
+ORDER BY 3 desc
 ]==], entity_id.tointeger());
 	}
 
@@ -345,13 +373,13 @@ LEFT JOIN order_types as ot
 	ON o.order_type_id = ot.id
 LEFT JOIN payment_types as pt
 	ON o.payment_type_id = pt.id
-    , (
-    SELECT order_id, sum(discount_amt) as  discount_amt, sum(total) as total
-    FROM order_line_calc_view
-    WHERE order_id in(SELECT id FROM orders WHERE entity_id =]==], entity_id, [==[)
-    GROUP BY order_id
-    )  as ott
-    WHERE entity_id =]==], entity_id, " and o.id = ott.order_id");
+	, (
+		SELECT order_id, sum(discount_amt) as  discount_amt, sum(total) as total
+		FROM order_line_calc_view
+		WHERE order_id in(SELECT id FROM orders WHERE entity_id =]==], entity_id, [==[)
+		GROUP BY order_id
+	)  as ott
+	WHERE entity_id =]==], entity_id, " and o.id = ott.order_id");
 		}
 		else
 		{
@@ -397,9 +425,9 @@ group by product_id, description, ]==], strTmp3);
 			mf.write(") as t ")
 		}
 
-	    mf.write(" order by qm, sab")
-	    //debug_print(mf.tostring(), "\n")
-	    return mf.tostring();
+		mf.write(" order by qm, sab")
+		//debug_print(mf.tostring(), "\n")
+		return mf.tostring();
 	}
 
 	function sql_search_list(qs_tbl, post_tbl){
@@ -600,7 +628,7 @@ class My_PDF_Order extends PDF_Order {
 			else if(mime_type == "image/png") mtype = "png";
 			Image("order_logo", my_logo_image, my_logo_image.size(), px, py, pwidth, 0, mtype);
 		}
-        }
+	}
 }
 
 class CalcOrderLine
@@ -662,10 +690,10 @@ class CalcOrderLine
 	function clear_preserving_tax_flags(withFullTax=false)
 	{
 		unit_weight = quantity = weight = price =
-                                              first_total = discount_pct =
-                                                      discount_amt = line_subtotal =
-                                                              sales_tax1_pct = sales_tax1_amt = sales_tax2_pct =
-                                                                      sales_tax2_amt = line_total = 0.0;
+			first_total = discount_pct =
+			discount_amt = line_subtotal =
+			sales_tax1_pct = sales_tax1_amt = sales_tax2_pct =
+			sales_tax2_amt = line_total = 0.0;
 		id = order_id = product_id = 0;
 		price_decimals = 2;
 		if(withFullTax){
@@ -677,10 +705,10 @@ class CalcOrderLine
 	function clear_preserving_tax_flags2(withFullTax=false)
 	{
 		unit_weight = quantity = weight = price =
-                                              first_total = discount_pct =
-                                                      discount_amt = line_subtotal =
-                                                              sales_tax1_pct = sales_tax1_amt = sales_tax2_pct =
-                                                                      sales_tax2_amt = line_total = Decimal(0);
+			first_total = discount_pct =
+			discount_amt = line_subtotal =
+			sales_tax1_pct = sales_tax1_amt = sales_tax2_pct =
+			sales_tax2_amt = line_total = Decimal(0);
 		id = order_id = product_id = 0;
 		price_decimals = 2;
 		if(withFullTax){
@@ -794,13 +822,13 @@ class  CalcOrderTotals
 	function clear()
 	{
 		subtotal_amt = discount_amt = sales_tax1_amt =
-                                          sales_tax2_amt = total_amt = weight_total = 0.0;
+			sales_tax2_amt = total_amt = weight_total = 0.0;
 		lines_count = 0;
 	}
 	function clear2()
 	{
 		subtotal_amt = discount_amt = sales_tax1_amt =
-                                          sales_tax2_amt = total_amt = weight_total = Decimal(0);
+			sales_tax2_amt = total_amt = weight_total = Decimal(0);
 		lines_count = 0;
 	}
 
@@ -971,16 +999,22 @@ class MyCalcOrderTotals
 }
 
 class DB_Orders extends DB_Manager {
+	_calc_line = null;
+	_order_totals = null;
+	
 	constructor(){
 		base.constructor("orders", [
-		"order_type_id", "series", "order_number", "entity_order_number",
-		"entity_id", "entity_name", "entity_address",
-		"entity_phone", "entity_zip", "entity_city",
-		"entity_state", "entity_country", "entity_tax_number",
-		"entity_use_sales_tax2", "entity_sales_tax_exempt",
-		"order_date", "payment_type_id", "notes",
-		"cash", "tags", "order_valid_till_date",
-		"irpf_pct_retention"]);
+			"order_type_id", "series", "order_number", "entity_order_number",
+			"entity_id", "entity_name", "entity_address",
+			"entity_phone", "entity_zip", "entity_city",
+			"entity_state", "entity_country", "entity_tax_number",
+			"entity_use_sales_tax2", "entity_sales_tax_exempt",
+			"order_date", "payment_type_id", "notes",
+			"cash", "tags", "order_valid_till_date",
+			"irpf_pct_retention",
+		]);
+		_calc_line = new CalcOrderLine();
+		_order_totals = CalcOrderTotals();
 	}
 
 	function sql_search_list(qs_tbl, post_tbl){
@@ -1110,6 +1144,194 @@ class DB_Orders extends DB_Manager {
 		else if (qs_tbl.get("print_list", false)){
 		}
 	}
+
+	function calc_order_line_from_map(tbl_qs, rec_map)
+	{
+		_calc_line.reset();
+		local product_id = tbl_qs.get("product_id", 0);
+		local order_id = tbl_qs.get("__id__", 0);
+		if(product_id  && order_id)
+		{
+			local db = getOurbizDB();
+			local stmt;
+			local str =  tbl_qs.get("trigger", false);
+			//printf("%s\n", str.c_str());
+			if(str == "quantity")
+			{
+				local discount, quantity;
+				stmt = db.prepare(format([==[
+select group_order from order_types as ot, orders as o
+where o.id=%d
+and ot.id = o.order_type_id]==], order_id));
+				if(stmt.next_row()){
+					str = stmt.col(0);
+					if(str != "B"){
+						quanity = rec_map.get("quantity", 0);
+						if(quantity > 0)
+						{
+							out_buf.clear();
+							stmt.prepare(discount_by_quantity_get_one(out_sql, product_id, quantity));
+							if(stmt.next_row())
+							{
+								discount = stmt.col(0);
+								//special case when discount is negative
+								//meaning an increase we don't show
+								//it as discount but use it's calculated price
+								if(discount <= 0.0)
+								{
+									discount = 0.0;
+									rec_map.discount_pct <- "0";
+									rec_map.price <- stmt.asString(1);
+								}
+								else
+								{
+									rec_map.discount_pct <- stmt.asString(0);
+								}
+							}
+							else
+							{
+								rec_map.discount_pct <- "0";
+							}
+						}
+					}
+				}
+				else throw("Could not get order type !");
+			}
+			//_calc_line.price_over_weight = ?;
+
+			stmt.prepare(format([==[
+select  o.entity_sales_tax_exempt, o.entity_use_sales_tax2,
+	p.weight, p.sales_tax_id
+from orders as o, products as p 
+where o.id = %d and p.id = %d]==], order_id, product_id));
+			local sales_tax_id = 0;
+			if(stmt.next_row())
+			{
+				_calc_line.tax_exempt = stmt.col(0);
+				_calc_line.use_sales_tax2 = stmt.col(1);
+				//stmt.get(2, _calc_line.unit_weight);
+				rec_map.unit_weight <- stmt.col(2);
+				sales_tax_id = stmt.col(3);
+			}
+			else throw("Could not get sales tax info for product!");
+
+			if(!_calc_line.tax_exempt)
+			{
+				stmt.prepare(format("select  rate1, rate2 from sales_tax_rates where id = %d", sales_tax_id));
+				if(stmt.next_row())
+				{
+					rec_map.sales_tax1_pct <- stmt.col(0);
+					if(_calc_line.use_sales_tax2)
+						rec_map.sales_tax2_pct <- stmt.col(1);
+				}
+				else throw("Could not get sales tax info!");
+			}
+		}
+		_calc_line.calc_map(rec_map);
+	}
+
+	function calc_order_line(tbl_qs, out_buf, rec_map)
+	{
+		calc_order_line_from_map(tbl_qs, rec_map);
+		out_buf.clear();
+		WriteMap2SLEArray(out_buf, rec_map);
+	}
+	
+	function orders_lines_get_one(id)
+	{
+		return format([==[
+SELECT ol.*, p.weight as unit_weight,
+pbs.quantity as xref_order_line_quantity
+FROM orders_lines as ol JOIN products as p
+ON ol.product_id = p.id
+LEFT JOIN products_inventory_pending as pbs
+ON ol.id = pbs.order_line_id
+WHERE ol.id=%d]==], id.tointeger());
+	}
+
+	function order_totals_get_one(id)
+	{
+		return format([==[
+SELECT order_id,
+count(*) AS lines_count,
+sum(weight) AS weight_total,
+sum(subtotal_amt) AS subtotal_amt,
+sum(discount_amt) AS discount_amt,
+sum(sales_tax1_amt) AS sales_tax1_amt,
+sum(sales_tax2_amt) AS sales_tax2_amt,
+sum(total) AS total_amt
+FROM order_line_calc_view
+WHERE order_id =%d]==], id.tointeger());
+	}
+
+	function orders_lines_sql_for_order(order_id)
+	{
+		return format([==[
+select
+id,
+product_id,
+description,
+quantity,
+price,
+quantity * price as 'first_total'
+from orders_lines
+where order_id = %d
+order by id]==], order_id.tointeger());
+	}
+
+	function orders_get_one(out_sql, id)
+	{
+		out_sql.write("SELECT o.*, ott.* FROM orders AS o LEFT JOIN ( ");
+		out_sql.write(order_totals_get_one(id));
+		out_sql.write(" ) AS ott ON o.id = ott.order_id WHERE o.id = ", id);
+	}
+
+	function sql_get_one(tbl_qs) {
+		local id = tbl_qs.get(table_name, 0).tointeger();
+		if(tbl_qs.get("line", false))
+		{
+			orders_lines_get_one(out_sql, id);
+		}
+		else if(tbl_qs.get("line_calculated", false))
+		{
+			local db = getOurbizDB();
+			local stmt = db.prepare(orders_lines_get_one(id));
+			if(!stmt.next_row()) return;
+			local rec_map = stmt.asTable();
+			rec_map.__id__ <- rec_map.order_id;
+			local buf = blob(0, 8192);
+			calc_order_line(tbl_qs, buf, rec_map);
+			return buf;
+		}
+		else if (tbl_qs.get("with_lines", false)){
+			local db = getOurbizDB();
+			local buf = blob(0, 8192);
+
+			orders_get_one(buf, id);
+			local stmt = db.prepare("select * from orders where id=?");
+			stmt.bind(1, id);
+			if(!stmt.next_row()) return;
+
+			_order_totals.calc_order_totals(db, id, _calc_line);
+
+			local col_count = stmt.col_count();
+			buf.clear();
+			buf.write("[[");
+			WriteSLEArrayFieldNames(buf, stmt, col_count);
+			_order_totals.dumpSLEFieldNames(buf);
+			buf.writen(SLE_SLEEND, 'c');
+			buf.write("][");
+			WriteSLEArrayFieldValues(buf, stmt, col_count);
+			_order_totals.dumpSLEFieldValues(buf);
+			buf.writen(SLE_SLEEND, 'c');
+			buf.write("]]");
+
+			stmt.prepare(orders_lines_sql_for_order(id));
+			buf.write(stmt.asSleArray());
+			return buf;
+		}
+		else return base.sql_get_one(tbl_qs);
+	}
 }
 
 db_ourbiz_tables.orders <-  new DB_Orders();
@@ -1328,10 +1550,10 @@ db_ourbiz_tables.images <- new  DB_Images();
 class DB_order_types extends DB_Manager {
 	constructor(){
 		base.constructor("order_types", ["is_active", "code", "description", "description_to_print",
-            "group_order", "is_expense", "is_incoming", "notes", "numbering",
-            "series", "show_prices", "show_sales_tax", "subgroup_order",
-            "with_credit", "with_inventory", "with_payment", "with_ordered",
-            "with_sales_tax", "with_sales_tax_included", "with_quote"]);
+			"group_order", "is_expense", "is_incoming", "notes", "numbering",
+			"series", "show_prices", "show_sales_tax", "subgroup_order",
+			"with_credit", "with_inventory", "with_payment", "with_ordered",
+			"with_sales_tax", "with_sales_tax_included", "with_quote"]);
 	}
 
 	function sql_list(qs_tbl, post_tbl){
@@ -1348,14 +1570,14 @@ class DB_order_types extends DB_Manager {
 		{
 			return [==[
 SELECT
-    id,
-    code,
-    description,
-    with_payment,
-    with_inventory,
-    group_order,
-    subgroup_order,
-    is_active
+	id,
+	code,
+	description,
+	with_payment,
+	with_inventory,
+	group_order,
+	subgroup_order,
+	is_active
 FROM order_types
 ORDER BY group_order,subgroup_order
 ]==];
@@ -1484,9 +1706,9 @@ function product_prices_list_sql (product_id){
 	product_id = product_id.tointeger();
 	return format([==[
 select id,quantity ,
-    markup_pct ,
-    discount_pct ,
-    price, mdate, cdate
+	markup_pct ,
+	discount_pct ,
+	price, mdate, cdate
 from product_prices
 where product_id = %d
 UNION ALL
@@ -1632,25 +1854,7 @@ function ourbizDbGetOne(request){
 
 		gmFile.clear()
 
-		if (tbl == "orders"){
-			if (request.get_var(query_string, "with_lines")){
-				sql = "select * from orders where id=?";
-				local stmt = db.prepare(sql);
-				stmt.bind(1, rec_id);
-				gmFile.write(stmt.asSleArray());
-				stmt.finalize();
-				sql = "select id, product_id, description, quantity, price, 0 as sub_total from orders_lines where order_id=?";
-				stmt = db.prepare(sql);
-				stmt.bind(1, rec_id);
-				gmFile.write(stmt.asSleArray());
-				stmt.finalize();
-				sql = null;
-			}
-			else if (request.get_var(query_string, "line_calculated")){
-				sql = "select * from orders_lines where id=?";
-			}
-		}
-		else if (tbl == "products"){
+		if (tbl == "products"){
 			local product_aux_data = request.get_var(query_string, "product_aux_data");
 			local product_for_edit = request.get_var(query_string, "product_for_edit");
 			if (product_aux_data){
@@ -1688,11 +1892,15 @@ function ourbizDbGetOne(request){
 		else if (db_ourbiz_tables.get(tbl, false)) sql = db_ourbiz_tables[tbl].sql_get_one(tbl_qs);
 
 		if (sql){
-			local stmt = db.prepare(sql);
-			stmt.bind(1, rec_id);
-			data = stmt.asSleArray();
-			//debug_print(rec_id, "\t", sql, "\t", data.len(), "\t", data, "\n")
-			stmt.finalize();
+			if(sql instanceof blob) data = sql.tostring();
+			else
+			{
+				local stmt = db.prepare(sql);
+				stmt.bind(1, rec_id);
+				data = stmt.asSleArray();
+				//debug_print(rec_id, "\t", sql, "\t", data.len(), "\t", data, "\n")
+				stmt.finalize();
+			}
 		}
 		else if (gmFile.len() > 0) data = gmFile.tostring();