<?php
 //die("Don't panic, this is a test.");

require_once("connect.php");
require_once("functions.php");
$table = $_REQUEST['t'];
$sm = requireParam("sm");
switch ($table) {
  case 'CUSTOMER':
    $query = "SELECT ID, Name, Address1, Address2, City, State, ZipCode, Phone, PhoneExt, ContactTitle, Contact, ContactPhone, AccountNumber, PriceLevel, Email, Salesman, CreditLimit, DiscountDays, SupplierID, Password, Comments, Active, Language, DisableCheck, LastLogin, CookieHash, CookieExpire, ResetHash, WaiveShipping, SendOrderEmail, SendAmountDueEmail, ShowUPCInvoice, DueDays, DueDaysCalcDate, TaxID1, TaxIDExpiration1, TaxExempt1, TaxID2, TaxIDExpiration2, TaxExempt2, Terms, ShipToAddress1, ShipToAddress2, ShipToCity, ShipToState, ShipToZip, OverdueReminderSentDate FROM CUSTOMER WHERE Salesman = '$sm';";
    break;
  case "SALESMAN" :
    $query = "SELECT ID, FullName, Username, Password, LastLogin, Language, Email, CookieHash, CookieExpire, CommissionRate, SalesCommissionRate, GlobalCommission, CommissionSalesman, Phone, CellProvider, EmailPass, iPhoneConfig, AllowTruckMode, SendInventoryEmail, Active FROM SALESMAN;";
    break;
  case "INVENTORY" :
    $query = "SELECT id, `Line`, `Group`, SKU, Familia, Description, Notes, Qty, AvgCost, Cost, PriceA, PriceB, PriceC, PriceD, PriceE, MSRP, Core, Interchange, Dashless, DashlessInterchange, ProductURL, ImageURL, Category, Subcategory, OnOrder, Location, Taxable1, Taxable2, LocationB, LocationC, LocationD, LocationE, Discontinued, CommissionRate, Brand, VendorSKU, VendorID, Active FROM INVENTORY WHERE Active = 1;";
    break;
  case "PAYMENT_HEAD":
    $sm = requireParam("sm");
    $query = "SELECT ID, PaymentDate, AccountNumber, SalesmanID, CPD, CPDDate, CPDReceipt, CPDReceiptDate, CashAmount, CheckAmount, TransferAmount, ProcessDate, Processed, ProcessUser, Loaded, Notes, Void, OfflinePayment, OfflineID, OfflineDate, Deposited, CollectingUser FROM PAYMENT_HEAD WHERE SalesmanID = '$sm';";
    break;
  case "PAYMENT_DETAIL":
    $sm = requireParam("sm");
    $query = "SELECT PAYMENT_DETAIL.ID, PaymentID, InvoiceID, InvoiceDate, Reference, PartialPayment, SubtotalAmount, DiscountAmount, TaxAmount, PaymentAmount FROM PAYMENT_HEAD JOIN PAYMENT_DETAIL ON(PAYMENT_HEAD.ID = PAYMENT_DETAIL.PaymentID) WHERE SalesmanID = '$sm';";
    break;
  case "ORDER_HEAD":
    $sm = requireParam("sm");
    $query = "SELECT OrderID, AccountNumber, OrderDate, Salesman, SalesmanOrder, WarehouseID, OrderNotes, PONumber, DeliveryMethod, InProcess, InProcessDate, Processed, ProcessedDate, ProcessUser, OnHold, OnHoldDate, DeleteOrder, DeleteOrderDate, HoldDeleteReason, OnHoldDeleteUser, OfflineOrder, OfflineID, OfflineOrderDate, DocumentID FROM ORDER_HEAD WHERE Salesman = '$sm';";
    break;
  case "ORDER_DETAIL":
    $sm = requireParam("sm");
    $query = "SELECT OrderID, `Group`, `Line`, SKU, Description, Notes, OrderQty, AvailableQty, Interchange, Price, Core, ID FROM ORDER_HEAD JOIN ORDER_DETAIL USING(OrderID) WHERE Salesman = '$sm'";
    break;
  case "INVOICE_HEAD":
    $sm = requireParam("sm");
    $query = "SELECT InvoiceID, OrderID, AccountNumber, InvoiceDate, Salesman, SalesmanOrder, WarehouseID, Notes, PONumber, DeliveryMethod, Status, DueDate, SentPicking, Packed, PackingUser, PackageCount, CreationDate, InvoiceUser, OnHold, OnHoldDate, Void, VoidDate, HoldVoidReason, HoldVoidUser, Terms, Subtotal, Tax1Total, Tax2Total, Tax1Rate, Tax2Rate, ShipToName, ShipToAddress1, ShipToAddress2, ShipToCity, ShipToState, ShipToZip, ShipToCountry, BillToName, BillToAddress1, BillToAddress2, BillToCity, BillToState, BillToZip, BillToCountry, DeliveryDate, Delivered, DeliveryUser FROM INVOICE_HEAD;";
    break;
  case "INVOICE_DETAIL":
    $sm = requireParam("sm");
    $query = "SELECT ID, InvoiceID, `Group`, `Line`, SKU, Description, Notes, OrderQty, ShippedQty, AvgCost, Core, Price, Taxable1, Taxable2, Reference, ReferenceType FROM INVOICE_DETAIL;";
    break;
  default:
    // code...
    break;
}
$path= "sync/$sm.$table.syncFile";
if(CSVExport($query, $path)){
  $result = new stdClass();
  $result->Result = true;
  $result->Message = "$table file was successfuly created.";
  $result->Path = $path;
  $result->Table = $table;
  echo json_encode($result);
  header("Access-Control-Allow-Origin: *");
}else{
  $result = new stdClass();
  $result->Result = false;
  $result->Message = "$table there was an error creating the file";
  $result->Path = $path;
  $result->Table = $table;
  echo json_encode($result);
}

function requireParam($param){
  global $con;
  if(isset($_REQUEST[$param])){
    return mysqli_real_escape_string($con,$_REQUEST[$param]);
  }else {
    die("Error! Missing parameter: '$param'");
  }
}
 ?>
