Permalink
Browse files

Add simple API for applying bulk discounts (hard-coded for now)

  • Loading branch information...
1 parent 2ecaa8b commit 059adf4088c5beedb616fd6fc5d1aaa5114cd330 @jimwins committed Jul 19, 2012
Showing with 57 additions and 0 deletions.
  1. +43 −0 api/txn-apply-discounts.php
  2. +14 −0 index.php
@@ -0,0 +1,43 @@
+<?
+include '../scat.php';
+include '../lib/txn.php';
+
+$id= (int)$_REQUEST['txn'];
+if (!$id)
+ die_jsonp("no transaction specified.");
+
+$txn= txn_load($db, $id);
+
+if ($txn['paid']) {
+ die_jsonp("This order is already paid!");
+}
+
+$count= $db->get_one("SELECT ABS(SUM(ordered))
+ FROM txn_line
+ JOIN item ON txn_line.item = item.id
+ WHERE txn = $id
+ AND code LIKE 'MXG-%'");
+if ($count >= 50) {
+ $q= "UPDATE txn_line, item
+ SET txn_line.discount = 40.0, txn_line.discount_type = 'percentage'
+ WHERE txn = $id AND code LIKE 'MXG-%'";
+ $db->query($q)
+ or die_query($db, $q);
+} elseif ($count >= 12) {
+ $q= "UPDATE txn_line, item
+ SET txn_line.discount = 30.0, txn_line.discount_type = 'percentage'
+ WHERE txn = $id AND code LIKE 'MXG-%'";
+ $db->query($q)
+ or die_query($db, $q);
+} else {
+ $q= "UPDATE txn_line, item
+ SET txn_line.discount = item.discount,
+ txn_line.discount_type = item.discount_type
+ WHERE txn = $id AND code LIKE 'MXG-%'";
+ $db->query($q)
+ or die_query($db, $q);
+}
+
+$txn= txn_load_full($db, $id);
+
+echo jsonp($txn);
View
@@ -60,6 +60,7 @@
/* Hide/show some elements from paid invoices. */
#txn.paid .remove,
#txn.paid #pay,
+#txn.paid #bulk,
#txn.paid .choices, #txn.paid .errors
{
display: none;
@@ -703,6 +704,7 @@ function (data) {
<div id="sale-buttons">
<button id="invoice">Invoice</button>
<button id="print">Print</button>
+ <button id="bulk">Bulk</button>
<button id="pay">Pay</button>
<button id="return">Return</button>
</div>
@@ -767,6 +769,18 @@ function (data) {
}
});
});
+$("#bulk").on("click", function() {
+ var txn= $('#txn').data('txn');
+ $.getJSON("api/txn-apply-discounts.php?callback=?",
+ { txn: txn },
+ function (data) {
+ if (data.error) {
+ $.modal(data.error);
+ } else {
+ loadOrder(data);
+ }
+ });
+});
</script>
<style>
#choose-pay-method {

0 comments on commit 059adf4

Please sign in to comment.