Compute available summary (and view)
authorMaxime Bourget <bmx007@gmail.com>
Mon, 3 Jun 2013 21:59:01 +0000 (22:59 +0100)
committerMaxime Bourget <bmx007@gmail.com>
Mon, 3 Jun 2013 21:59:01 +0000 (22:59 +0100)
hooks.php
includes/order_lines.inc
item_schedule.php
order_lines_view.php
sql/clean_sales_order_details.sql

index af125eb867198a322dbdca3069bfd274d8205f79..019b6affb13cdeefce212cc0373f2968d6bf59c2 100644 (file)
--- a/hooks.php
+++ b/hooks.php
@@ -47,6 +47,7 @@ class hooks_order_line_extra extends hooks {
                                                'alter_sales_order_details.sql' => array('sales_order_details','required_date'),
                                                'create_denorm_order_details_queue.sql' => array('denorm_order_details_queue'),
                                                'create_denorm_qoh.sql' => array('denorm_qoh'),
+                                               'create_order_summary_view.sql' => array('order_summary_view'),
         );
 
                                return $this->update_databases($company, $updates, $check_only)
index 92391f6034458ef3efb3c9447e1562e7dca6a111..dc09a6bcf57d12ddd710fbadfdaa24ebf310e22e 100644 (file)
@@ -158,21 +158,23 @@ function get_order_details_extra($customer_id, $location) {
                AND so.debtor_no = $customer_id
        )
        JOIN ".TB_PREF."denorm_qoh qoh ON (stock_id = stk_code AND loc_code = '$location')
-       JOIN ".TB_PREF."denorm_order_details_queue  d ON (d.id = sod.id)
+       LEFT JOIN ".TB_PREF."denorm_order_details_queue  d ON (d.id = sod.id)
        WHERE sod.quantity > qty_sent
        ";
 
        return $sql;
 }
 
-function get_order_summary() {
-       $sql = "SELECT debtor_no, debtor_ref, branch_ref
+function get_order_summary($location) {
+/*
+       $sub = "SELECT debtor_no, debtor_ref, branch_ref, stk_code
        , min(delivery_date)
-       , sum(quantity - qty_sent) as quantity
-       , sum((quantity - qty_sent)*unit_price*(1-discount_percent/100)) as amount
+       , sum(sod.quantity - qty_sent) as quantity
+       , sum((sod.quantity - qty_sent)*unit_price*(1-discount_percent/100)) as amount
        , min(required_date)
        , group_concat(distinct comments separator ';') as order_comment
        , group_concat(distinct comment separator ';') as detail_comment
+       , max(quantity_before) as quantity_before
        FROM ".TB_PREF."sales_order_details sod
        JOIN ".TB_PREF."sales_orders so ON (so.order_no = sod.order_no
                AND so.trans_type = sod.trans_type
@@ -180,8 +182,26 @@ function get_order_summary() {
        )
        NATURAL JOIN ".TB_PREF."debtors_master
        NATURAL JOIN ".TB_PREF."cust_branch
-       WHERE quantity > qty_sent AND ".ST_SALESORDER."
-       GROUP BY debtor_no, branch_code
+       JOIN ".TB_PREF."denorm_order_details_queue  d ON (d.id = sod.id)
+       WHERE sod.quantity > qty_sent AND ".ST_SALESORDER."
+       GROUP BY debtor_no, branch_code, stk_code
+       ";
+*/
+
+       $sub = TB_PREF."order_summary_view";
+
+       $sql = "SELECT debtor_no, debtor_ref, branch_ref
+       , `min(delivery_date)`
+       , sum(sub.quantity) as quantity
+       , sum(sub.amount) as amount
+       , sum(greatest(least(sub.quantity, qoh.quantity - quantity_before), 0))
+       , sum(sub.amount*greatest(least(sub.quantity, qoh.quantity - quantity_before), 0)/sub.quantity)
+       ,  `min(required_date)`
+       , group_concat(distinct order_comment separator ';') as order_comment
+       , group_concat(distinct detail_comment separator ';') as detail_comment
+                               FROM $sub sub
+                               LEFT JOIN ".TB_PREF."denorm_qoh qoh ON (stock_id = stk_code AND loc_code = '$location')
+                               GROUP BY debtor_no, debtor_ref
        ";
 
        return $sql;
index 1f09897f005f180dd831b7042b7c5788616d3951..1937108e1a2b834d893d3ade96c8a01ca312e940 100644 (file)
@@ -49,7 +49,9 @@ else {
        }
 }
 
-if (list_updated('stock_id') || in_ajax()) $Ajax->activate('item_schedule');
+if (list_updated('stock_id') || in_ajax()) $Ajax->activate('item_schedule') ;
+else update_queue_quantities() && update_qoh_for_item();
+
 
 //if (!@$_GET['popup'])
 //     start_form();
index 2f0a14ad7d5ee66b075702e98b4a75c6f590408f..0bbc5e45b9b9b865608db862b5e56d3b24f652bc 100644 (file)
@@ -80,7 +80,7 @@ if($customer_id) {
        );
 }
 else {
-       $sql = get_order_summary();
+       $sql = get_order_summary('DEF');
        $cols = array(
        'customer id' => 'skip',
 _("Customer") => array('ord' => '', 'fun' => 'customer_link'),
@@ -88,6 +88,8 @@ _("Branch") => array('ord' => ''),
 _("Delivery Date") => array('ord' => '', 'type' => 'date'),
 _("Quantity") => array('ord' => '', 'type' => 'qty', 'dec' => 0),
 _("Amount") => array('ord' => '', 'type' => 'amount'),
+_("Avail. Q") => array('ord' => '', 'type' => 'qty', 'dec' => 0),
+_("Avail. A") => array('ord' => '', 'type' => 'amount', 'dec' => 0),
 _("required date") => array('ord' => '', 'type' => 'date'),
 _("Comments") => array('ord' => '', 'fun' => 'aggregate_comment'),
 _("comment 2 ") =>  'skip'
index 9636d2834b34d4991612e5e15455391cf29ff699..2b70ddfea3873c180660c7cd6cab0e7c5406c165 100644 (file)
@@ -5,3 +5,4 @@ ALTER TABlE 0_sales_order_details
 
 DROP TABLE IF EXISTS 0_denorm_order_details_queue;
 DROP TABLE IF EXISTS 0_denorm_qoh;
+DROP VIEW  IF EXISTS 0_order_summary_view;