Commit 9f37a2a8 authored by Malcolm Blaney's avatar Malcolm Blaney

New Sell module to provide a point of sale system for non logged in

buyers. Relies on Cart module to provide credit card processing.
More Cart module improvements, including Factory methods so that
Sell module has access to payment methods.
parent 8f19b08d
This diff is collapsed.
<?php
// Dobrado Content Management System
// Copyright (C) 2013 Malcolm Blaney
// Copyright (C) 2014 Malcolm Blaney
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
......@@ -588,32 +588,49 @@ class Payment extends Base {
return array((float)$info, (float)$warning);
}
public function NewUser() {
public function NewUser($user = "", $deposit = true) {
if ($user === "") {
$user = $this->user->name;
}
$mysqli = connect_db();
$query = 'INSERT INTO payment_totals VALUES ("'.$this->user->name.'", 0)';
$query = 'INSERT INTO payment_totals VALUES ("'.$user.'", 0)';
if (!$mysqli->query($query)) {
$this->Log('Payment->NewUser 1: '.$mysqli->error);
}
// Add the deposit amount as a negative value for the new user.
$deposit = 0;
$query = 'SELECT deposit FROM payment_settings WHERE '.
'system_group = "'.$this->user->group.'" OR system_group = "" '.
'ORDER BY system_group DESC';
if ($result = $mysqli->query($query)) {
if ($payment_settings = $result->fetch_assoc()) {
$deposit = (float)$payment_settings["deposit"] * -1;
if ($deposit) {
// Add the deposit amount as a negative value for the new user.
$deposit = 0;
$query = 'SELECT deposit FROM payment_settings WHERE '.
'system_group = "'.$this->user->group.'" OR system_group = "" '.
'ORDER BY system_group DESC';
if ($result = $mysqli->query($query)) {
if ($payment_settings = $result->fetch_assoc()) {
$deposit = (float)$payment_settings["deposit"] * -1;
}
$result->close();
}
$result->close();
}
else {
$this->Log('Payment->NewUser 2: '.$mysqli->error);
}
if ($deposit != 0) {
$query = 'INSERT INTO payments VALUES ("'.$this->user->name.'", '.time().
', "", '.$deposit.', "Automatic deposit deduction for new user.", "")';
if (!$mysqli->query($query)) {
$this->Log('Payment->NewUser 3: '.$mysqli->error);
else {
$this->Log('Payment->NewUser 2: '.$mysqli->error);
}
if ($deposit != 0) {
$query = 'INSERT INTO payments VALUES '.
'("'.$this->user->name.'", '.time().', "", '.$deposit.', '.
'"Automatic deposit deduction for new user.", "")';
if (!$mysqli->query($query)) {
$this->Log('Payment->NewUser 3: '.$mysqli->error);
}
}
}
$mysqli->close();
}
public function Save($user, $time, $total, $method = "cash") {
$mysqli = connect_db();
$query = 'INSERT INTO payments VALUES ("'.$user.'", '.$time.', "", '.
$total.', "'.$method.'", "'.$this->user->name.'") '.
'ON DUPLICATE KEY UPDATE amount = '.$total.', comment = "'.$method.'"';
if (!$mysqli->query($query)) {
$this->Log('Payment->Save: '.$mysqli->error);
}
$mysqli->close();
}
......
......@@ -170,8 +170,7 @@ class Purchase extends Base {
// users having shared accounts, so check who is on the roster.
$roster = new Roster($this->user, $this->owner);
$first = $roster->FirstName();
if ($first == "" || $first == $this->user->name ||
$first == $user_details["first"]) {
if ($first == "" || $first == $this->user->name) {
$first = $user_details["first"];
$thumbnail = $user_details["thumbnail"];
}
......@@ -890,9 +889,12 @@ class Purchase extends Base {
$mysqli->close();
}
public function NewUser() {
public function NewUser($user = "") {
if ($user === "") {
$user = $this->user->name;
}
$mysqli = connect_db();
$query = 'INSERT INTO purchase_totals VALUES ("'.$this->user->name.'", 0)';
$query = 'INSERT INTO purchase_totals VALUES ("'.$user.'", 0)';
if (!$mysqli->query($query)) {
$this->Log("Purchase->NewUser: ".$mysqli->error);
}
......@@ -1102,6 +1104,77 @@ class Purchase extends Base {
return $total - $future - $this->SupplyTotal($this->user->name, $supplies);
}
public function SaveSales($payment_method) {
$mysqli = connect_db();
$time = time();
$user = "";
$values = "";
$total = 0;
$us_data = json_decode($_POST["data"], true);
$payment = new Payment($this->user, $this->owner);
// Get available usernames for this sales data.
$exists = false;
do {
$query = 'SELECT user FROM users WHERE user LIKE "buyer\_'.$time.'%"';
if ($result = $mysqli->query($query)) {
if ($result->num_rows > 0) {
$exists = true;
$time++;
}
$result->close();
}
} while ($exists);
foreach ($us_data as $us_user => $us_values) {
if ($us_user === "") continue;
if (!is_array($us_values)) continue;
$total = 0;
$purchase_count = count($us_values);
if ($purchase_count > 0) {
$user = "buyer_".$time."_";
// The given value for user is just an index.
$user .= $mysqli->escape_string($us_user);
$query = 'INSERT INTO users VALUES ("'.$user.'", "", "", "'.
$this->user->group.'", 0, "", '.$time.', "")';
if (!$mysqli->query($query)) {
$this->Log("Purchase->SaveSales 1: ".$mysqli->error);
}
for ($i = 0; $i < $purchase_count; $i++) {
$name = $mysqli->escape_string($us_values[$i]["name"]);
$supplier = $mysqli->escape_string($us_values[$i]["supplier"]);
$quantity = (float)$mysqli->escape_string($us_values[$i]["quantity"]);
$price = (float)$mysqli->escape_string($us_values[$i]["price"]);
$base_price =
(float)$mysqli->escape_string($us_values[$i]["basePrice"]);
$total += $quantity * $price;
if ($values !== "") $values .= ",";
$values .= '("'.$user.'", '.$time.', "'.$name.'", '.
'"'.$supplier.'", '.$quantity.', '.$price.', '.$base_price.', '.
'"'.$this->user->name.'")';
}
$this->NewUser($user);
$payment->NewUser($user, false);
$total = number_format($total, 2, ".", "");
$payment->Save($user, $time, $total);
}
}
// If a payment method is given, it only applies to the last user.
if (isset($payment_method)) {
$payment->Save($user, $time, $total, $payment_method);
}
if ($values !== "") {
$query = 'INSERT INTO purchase VALUES '.$values;
error_log("query = ".$query);
if (!$mysqli->query($query)) {
$this->Log("Purchase->SaveSales 2: ".$mysqli->error);
}
}
$mysqli->close();
}
// Private functions below here ////////////////////////////////////////////
private function CurrentTime() {
......
This diff is collapsed.
This diff is collapsed.
......@@ -65,23 +65,33 @@ if (!this.dobrado.cart) {
$("#customer-detail-submit").button().click(customerDetail);
$(".cart-item .item-name a").click(cartItem);
var width = 450;
if ($(".cart-payment-message").html() !== "") {
$(".cart-payment-message button").button().click(paymentDone);
$(".cart-payment-message").dialog({
modal: true,
position: { my: "top", at: "top+50", of: "body" },
title: "Payment",
width: 400,
height: 200,
close: paymentDone
});
}
$("#cart-checkout-dialog").dialog({
autoOpen: false,
modal: true,
position: { my: "top", at: "top+50", of: "body" },
title: "Checkout",
width: width,
width: 500,
height: 500
});
width = 720;
$("#cart-editor").dialog({
autoOpen: false,
close: close,
position: { my: "top", at: "top+50", of: "body" },
title: "Cart Editor",
width: width,
width: 720,
height: 500
});
......@@ -89,7 +99,7 @@ if (!this.dobrado.cart) {
autoOpen: false,
position: { my: "top", at: "top+50", of: "body" },
title: "Cart Item",
width: width,
width: 720,
height: 500
});
......@@ -363,17 +373,17 @@ if (!this.dobrado.cart) {
url: location.href,
token: dobrado.token },
function(response) {
if (dobrado.checkResponseError(response, "cart shipping")) {
if (dobrado.checkResponseError(response, "cart checkout")) {
return;
}
var shipping = JSON.parse(response);
var checkout = JSON.parse(response);
$("#cart-checkout-info").html("");
$("#cart-customer-details-form").hide();
$("#cart-summary").html("You have selected the following items:<br>" +
cartTotal(shipping.cost));
$("#cart-checkout-message").html(shipping.content);
$("#cart-payment").button();
cartTotal(checkout.cost));
$("#cart-checkout-message").html(checkout.content);
$("#cart-payment").button().click(clearCart);
});
return false;
}
......@@ -454,8 +464,8 @@ if (!this.dobrado.cart) {
newShipping();
}
if (settings.checkout) {
$("#cart-checkout-email").val(checkout.email);
$("#cart-checkout-currency").val(checkout.currency);
$("#cart-checkout-email").val(settings.checkout.email);
$("#cart-checkout-currency").val(settings.checkout.currency);
}
if (settings.method) {
$.each(settings.method, function(i, method) {
......@@ -532,6 +542,13 @@ if (!this.dobrado.cart) {
}
}
function clearCart() {
cart = {};
if (dobrado.localStorage()) {
localStorage.cart = JSON.stringify(cart);
}
}
function clearItem() {
$("#cart-item-name").val("");
$("#cart-item-image").val("");
......@@ -551,6 +568,20 @@ if (!this.dobrado.cart) {
}
}
function paymentDone() {
$.post("/php/request.php", { request: "cart",
action: "payment-done",
url: location.href,
token: dobrado.token },
function(response) {
if (dobrado.checkResponseError(response, "cart payment done")) {
return;
}
var payment = JSON.parse(response);
location.href = payment.location;
});
}
function previousItem() {
showItem(settings.items[--itemIndex]);
}
......@@ -693,7 +724,7 @@ if (!this.dobrado.cart) {
url: location.href,
token: dobrado.token },
function(response) {
if (dobrado.checkResponseError(response, "cart save shipping")) {
if (dobrado.checkResponseError(response, "cart remove shipping")) {
return;
}
// A new shipping array is returned with the requested value removed.
......
......@@ -57,9 +57,9 @@ resetForm();$("#purchase-form .order").hide();$(purchaseGridId).hide();setFormCo
if($.inArray(user,purchase.users)===-1){alert("If an account was just created for "+user+" please reload the page.");$("#purchase-name-input").val("");return;}
if(purchase.data[user]){if(purchaseGrid&&purchase.data[user].length!==0){purchaseGrid.setData(purchase.data[user]);purchaseGrid.updateRowCount();purchaseGrid.render();purchaseGrid.setSelectedRows([]);$(".grid").show();}}
else{purchase.data[user]=[];}
$(".warning-level").hide();$(".info-level").hide();$("#purchase-form .order").show();if($.inArray(user,purchase.nextWeek)===-1){$("#purchase-next-week-input").prop("checked",false);}
$(".warning-level").hide();$(".info-level").hide();if($.inArray(user,purchase.nextWeek)===-1){$("#purchase-next-week-input").prop("checked",false);}
else{$("#purchase-next-week-input").prop("checked",true);}
var total=0;$.each(purchase.data[user],function(i,item){if(item.date<tomorrow){total+=parseFloat(item.total);}});$("#purchase-form .total").html(total.toFixed(2));if($.inArray(user,purchase.roster)===-1){$(".roster-reminder").hide();}
var total=0;$.each(purchase.data[user],function(i,item){if(item.date<tomorrow){total+=parseFloat(item.total);}});$("#purchase-form .total").html(total.toFixed(2));$("#purchase-form .order").show();if($.inArray(user,purchase.roster)===-1){$(".roster-reminder").hide();}
else{$(".roster-reminder").show();}
var details=purchase.details[user];if(details&&details.first&&details.last&&details.phone){$("#purchase-form .order .name").html(details.first+" "+details.last);}
else{$("#purchase-form .order .name").html(user);$(".purchase-details-user").html(user);$("#purchase-details-first-input").val("");$("#purchase-details-last-input").val("");$("#purchase-details-phone-input").val("");$("#purchase-details-info").html("");if(details){if(details.first){$("#purchase-details-first-input").val(details.first);}
......
......@@ -477,7 +477,6 @@ if (!this.dobrado.purchase) {
}
$(".warning-level").hide();
$(".info-level").hide();
$("#purchase-form .order").show();
// Update the nextWeek checkbox when the user is changed.
if ($.inArray(user, purchase.nextWeek) === -1) {
$("#purchase-next-week-input").prop("checked", false);
......@@ -493,6 +492,7 @@ if (!this.dobrado.purchase) {
}
});
$("#purchase-form .total").html(total.toFixed(2));
$("#purchase-form .order").show();
// Check the list of users who need reminding to volunteer.
if ($.inArray(user, purchase.roster) === -1) {
$(".roster-reminder").hide();
......
This diff is collapsed.
This diff is collapsed.
......@@ -3,7 +3,7 @@
// @licstart The following is the entire license notice
// for the JavaScript code in this page.
//
// Copyright (C) 2013 Malcolm Blaney
// Copyright (C) 2014 Malcolm Blaney
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
......
......@@ -2,7 +2,7 @@
// @licstart The following is the entire license notice
// for the JavaScript code in this page.
//
// Copyright (C) 2013 Malcolm Blaney
// Copyright (C) 2014 Malcolm Blaney
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
......
<?php
// Dobrado Content Management System
// Copyright (C) 2013 Malcolm Blaney
// Copyright (C) 2014 Malcolm Blaney
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment