2 require_once 'Pman/Xtuple/Reports/Base.php';
4 class Pman_Xtuple_Reports_SalesByCountry extends Pman_Xtuple_Reports_Base
10 $date_from = date('Y-m-d', strtotime($_REQUEST['date_from']));
11 $date_to = date('Y-m-d', strtotime($_REQUEST['date_to']));
12 $sd = strtoupper(substr(HTML_FlexyFramework::get()->database,-2));
14 $name = $sd == 'HK' ? 'From Hong Kong' : 'From Signapore';
16 //DB_DataObject::DebugLevel(1);
17 $q = DB_DataObject::factory('custinfo');
22 SUM(qtysold) as qtysold,
23 ROUND(SUM(soldat),2) as soldat_hkd
29 CASE WHEN (addr_country IS NULL OR addr_country = '') THEN
35 ROUND(sum(invcitem_billed),0) as qtysold,
36 sum(invcitem_billed * currtocurr(invchead_curr_id, getcurrid('HKD'), invcitem_custprice, invchead_invcdate)) as soldat
46 invchead_id = invcitem_invchead_id
54 invchead_cust_id = cust_id
59 cntct_id = cust_cntct_id
64 addr_id = cntct_addr_id
70 invchead_invcdate >= '$date_from'
72 invchead_invcdate <= '$date_to'
89 if (!empty($_REQUEST['format']) && $_REQUEST['format'] == 'json' ) {
91 $sd = strtoupper(substr(HTML_FlexyFramework::get()->database,-2));
94 $ret[$q->addr_country] = $q->toArray($sd.'_%s',true);
105 $xtd = json_decode(file_get_contents(
106 "http://localhost/xtuple/sg.php/Xtuple/Reports/SalesByCountry?date_from=$date_from&date_to=$date_to&format=json"));
107 // echo '<PRE>';print_r($xtd);exit;
108 $sg = (array) ($xtd->data);
113 while ($q->fetch()) {
114 $add = $q->toArray('%s', true);
116 $head = array_keys($add);
118 $add['total_sold'] = $add['qtysold'] ;
119 $add['total_hkd'] = $add['soldat_hkd'];
120 $ours[$q->addr_country ] = $add;
126 foreach($sg as $k=>$v) {
130 unset($add['SG_addr_country']);
133 $head = array_merge($head, array_keys( $add ));
136 if (!isset($ours[$k]) ) {
139 $add['addr_country'] = $v->SG_addr_country;
140 $add['qty_sold'] = $add['SG_qtysold'];
141 $add['total_hkd'] = $add['SG_soldat_hkd'];
144 $ours[$k]['total_sold'] += $add['SG_qtysold'];
145 $ours[$k]['total_hkd'] += $add['SG_soldat_hkd'];
147 $ours[$k] = ($ours[$k] + $add);
151 $head[] = 'total_sold';
152 $head[] = 'total_hkd';
155 $ret = array_values($ours);
156 usort($ret, function($a,$b) {
157 return $a['total_hkd'] > $b['total_hkd'] ? 1 : -1;
162 //echo '<PRE>';print_r($head);
163 //echo '<PRE>';print_r($ret);exit;
168 $this->toCsv($ret, $head);