Advertisement
tjromano

AR

Oct 29th, 2014
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.09 KB | None | 0 0
  1. <?php
  2. $dateStart = $_GET["dateStart"];
  3. $dateEnd = $_GET['dateEnd'];
  4. $sku_prefix = $_GET['sku'];
  5. $results_format = $_GET['format'];
  6. $comm = $_GET['comm'];
  7.  
  8. function startsWith($haystack, $needle)
  9. {
  10.     $length = strlen($needle);
  11.     return (substr($haystack, 0, $length) === $needle);
  12. }
  13.  
  14. if (!$dateStart) { $dateStart = "2013-07-01"; }
  15. if (!$dateEnd) { $dateEnd = "2013-10-01"; }
  16. if (!$comm) { $comm = 15; }
  17.  
  18. /**
  19.  * Compilation includes configuration file
  20.  */
  21. $compilerConfig = 'includes/config.php';
  22. if (file_exists($compilerConfig)) {
  23.     include($compilerConfig);
  24. }
  25.  
  26. $mageFilename = 'app/Mage.php';
  27. require_once $mageFilename;
  28.  
  29. umask(0);
  30. Mage::init();
  31.  
  32. $collection = Mage::getModel('sales/order')->getCollection();
  33. $collection->addFieldToFilter(
  34.      'created_at',
  35.      array('from' => $dateStart, 'to' => $dateEnd)
  36. );
  37.  
  38. echo <<<END
  39. <h3>Artist Report</h3>
  40. <form action="#">
  41. Sku prefix<input type=text name="sku" value="$sku_prefix" size=5>
  42. Date Start: <input type=text name="dateStart" value="$dateStart" size=8>
  43. Date End: <input type=text name="dateEnd" value="$dateEnd" size=8>
  44. Commission %: <input type=text name="comm" value="$comm" size=3 maxlength=2>
  45. <input type=submit value="Search">
  46. </form>
  47.  
  48.  
  49. <div id="resultsTable" style="position:absolute">
  50. <a href="javascript:downloadCSV();">Show CSV data</a>
  51. <table border=1>
  52. <tr bgcolor="gray" align=center><td>Order Number</td><td>Order Date</td><td>Sku<td>Base Price</td><td>sq feet</td><td>Sub-total</td><td>Row Total</td><td bgcolor="white"><u>Order</u> base total</td><td bgcolor="white"><u>Order</u> base discount</td><td bgcolor="white" >Discount %</td><td bgcolor="yellow">Commission</td></tr>
  53. END;
  54.  
  55. $csv_header = "order_id,date,sku,base_price,discount,commission %,commission\n";
  56.  
  57. foreach ($collection as $order) {
  58. $sku_data;
  59. $csv_data;
  60. $_totalData =$order->getData();
  61. $order_id =$order->getRealOrderId();
  62. $oid =$order->getId();
  63.  
  64. $oid_link = "<a href='/index.php/magic_admin/sales_order/view/order_id/$oid/' target='_blank'>$order_id</a>";
  65. # foreach($_totalData as $key => $one) {    print $key . ' ' . $one . '<br>'; }
  66.  
  67.     $created_at =  $_totalData['created_at'];
  68.     $date = substr($created_at,0,10);
  69.  
  70.                 $items = $order->getAllItems();
  71.                 $itemcount=count($items);
  72.         $grand = $_totalData['base_grand_total'];
  73.         $discount = $_totalData['base_discount_amount'];
  74.  
  75.                         foreach ($items as $itemId => $item) {
  76. # $_totalData2 =$item->getData();
  77. # foreach($_totalData2 as $key => $one) {    print $key . ' ' . $one . '<br>'; }
  78.  
  79.                             $sku=$item->getSku();
  80.                             $row_total= $item->getData('row_total') - $item->getData('discount_amount');
  81.                 $base_price = $item->getPrice() * $item->getQtyInvoiced();
  82.                 $discountPercentage = $discount;
  83.                 $dp = $discountPercentage;
  84.                 $commission = $row_total *  $comm/100;
  85.  
  86.                 if ($discount == 0) {
  87.                     $dp = 0;
  88.                 } else if ($dp < 0) {
  89.                     $dp = $discount/ $base_price * -1;
  90.                 }
  91.                 $discountPercentage = number_format($dp*100,0);
  92.                 if ($discountPercentage == 100) {
  93.                     $commission = 0;
  94.                 }
  95.  
  96.  
  97. # FOO - Multiple items per order
  98.  
  99.  
  100.                 $csv = "$order_id,$date,$sku,$row_total,$commission,$itemcount\n";
  101.                 $row = "<tr><td>$oid_link</td>";
  102.                 $row .= "<td>$date&nbsp;</td>";
  103.  
  104.                     $row .= "<td width=151>";
  105.                 $row .="$sku</td><td width=40 align=right>";
  106.                 $row .=number_format($item->getPrice(),2) . '</td><td width=40 align=right>';
  107.                 $row .=number_format($item->getQtyInvoiced(),2) . '</td><td with=40 align=right>';
  108.                 $row .=number_format($base_price,2) . '</td><td align=right>';
  109.                     $row .= number_format($row_total,2)."</td><td align=right>";
  110.                     $row .= number_format($grand,2)."</td><td align=right>";
  111.                     $row .= number_format($discount,2)."</td><td align=right>";
  112.                     $row .= $discountPercentage."</td><td align=right>";
  113.                 $row .=number_format($commission,2).  '</td>';
  114.  
  115.                 if ($sku_prefix) {
  116.                     if  (startsWith($sku,$sku_prefix)) {
  117.                         $sku_data[$item->getSku()] .= $row;
  118.                         $csv_data[$item->getSku()] .= $csv;
  119.                     }
  120.                 } else {
  121.                     $sku_data[$item->getSku()] .= $row;
  122.                     $csv_data[$item->getSku()] .= $csv;
  123.                 }
  124.                         }
  125. }
  126. $keys = array_keys($sku_data);
  127. sort($keys);
  128. $csv_content = $csv_header;;
  129. foreach ($keys as $key){
  130.      echo $sku_data{$key};
  131.      $csv_content .= $csv_data{$key};
  132.  }
  133.  
  134. echo <<<END
  135. </table>
  136. </div>
  137. <div  id="csv_content" style="position:relative;">
  138. <a href="javascript:showResults()">Show Results</a>
  139. <pre>
  140. $csv_content
  141. </div>
  142. </pre>
  143. <script>
  144. function downloadCSV() {
  145. document.getElementById('resultsTable').style.visibility='hidden';
  146. document.getElementById('csv_content').style.visibility='visible';
  147. alert('Please copy and paste the content below into a new text file. Name the file artist_report_date.csv');
  148. }
  149.  
  150. function showResults() {
  151. document.getElementById('resultsTable').style.visibility='visible';
  152. document.getElementById('csv_content').style.visibility='hidden';
  153. }
  154. document.getElementById('csv_content').style.visibility='hidden';
  155. </script>
  156. END;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement