Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
- <?php
- // =============== EDWINS VISMA .DBF READER/CONVERTER =============== //
- set_time_limit(0);
- /* ======================= */
- /* Converter Variables */
- /* ======================= */
- $extension = 3; // 0 = XML, 1 = JSON, 2 = To MYSQL, 3 = DEBUG
- date_default_timezone_set('Europe/Stockholm'); // TIME- zone
- $datetime = date_create()->format('Y-m-d H:i:s'); //TIME- create
- $cpath = "KUND.DBF"; // Path To Customer Database
- $apath = "ART.DBF"; // Path To Article Database
- $opath = "OOF.DBF"; // Path To Order Database
- $oapath = "ARTRAD.DBF"; // Path To Order Article Database
- /* ======================= */
- /* Converter Booleans */
- /* ======================= */
- $customers = "false"; // Export Customers From Visma
- $articles = "false"; // Export Articles From Visma
- $orders = "true"; // Export Orders From Visma
- $invoices = "false"; // Export Invoices From Visma
- $aorders = "true"; // Export Articles In Orders From Visma
- /* ======================= */
- /* Customers Convertion */
- /* ======================= */
- if($customers == "true"){
- /* Preparation */
- switch($extension){
- case 0: // Save to XML file.
- $doc = new DOMDocument('1.0');
- $doc->formatOutput = true;
- $root = $doc->createElement('customers');
- $root = $doc->appendChild($root);
- break;
- case 1: // Save to JSON file.
- $json = [];
- break;
- case 2: // Insert to Database
- $servername = "localhost";
- $username = "";
- $password = "";
- $dbname = "";
- $conn = new mysqli($servername, $username, $password, $dbname);
- if ($conn->connect_error)
- {
- die("Connection failed: " . $conn->connect_error);
- }
- break;
- }
- /* Execution */
- //Opens the Database
- $dbh = dbase_open($cpath, 0)
- or die("Error! Could not open dbase database file '$cpath'.");
- //Loops through Datbase.
- if ($dbh) {
- $record_numbers = dbase_numrecords($dbh);
- for ($i = 1; $i <= $record_numbers; $i++) {
- $row = dbase_get_record_with_names($dbh, $i);
- /* ======================= */
- /*Grab Stuff From Database */
- /* ======================= */
- $id = $row["KUNDNR"];
- $name = utf8_encode($row["NAMN"]);
- $country = $row["LAND"];
- switch($extension){
- case 0: // XML Export Option
- $title = $doc->createElement('customer'); // <CUSTOMER>
- $title = $root->appendChild($title);
- $cid = $doc->createElement('id'); // <CUSTOMER> <ID> N/A </ID></CUSTOMER>
- $cid = $title->appendChild($cid);
- $cname = $doc->createElement('name'); // <CUSTOMER> <NAME> N/A </NAME></CUSTOMER>
- $cname = $title->appendChild($cname);
- $ccountry = $doc->createElement('country'); // <CUSTOMER> <COUNTRY> N/A </COUNTRY></CUSTOMER>
- $ccountry = $title->appendChild($ccountry);
- $idtext = $doc->createTextNode($id); // Makes the ID writeable
- $idtext = $cid->appendChild($idtext); // Writes the ID
- $nametext = $doc->createTextNode($name); // Makes the NAME writeable
- $nametext = $cname->appendChild($nametext); // Writes the NAME
- $countrytext = $doc->createTextNode($country); // Makes the COUNTRY writeable
- $countrytext = $ccountry->appendChild($countrytext); // Writes the COUNTRY
- break;
- case 1: // JSON Export Option
- $json[] = array("id" => $id, "name" => $name, "country" => $country);
- break;
- case 2: // SQL Export Option
- $insert = "INSERT into importtest VALUES ('".$id."','".$row["NAMN"]."','".$country."')";
- $conn->query($insert);
- break;
- }
- }
- }
- /* Wrap up */
- switch($extension){
- case 0: // XML Export Option
- echo 'Wrote: ' . $doc->save("customers.xml") . ' bytes';
- break;
- case 1: // JSON Export Option
- echo json_encode($json[0],JSON_UNESCAPED_UNICODE);
- $fp = fopen('customers.json', 'w');
- fwrite($fp, json_encode($json[0],JSON_UNESCAPED_UNICODE));
- fclose($fp);
- break;
- case 2: // SQL Export Option
- $conn->close();
- break;
- }
- }
- /* ======================= */
- /* Articles Convertion */
- /* ======================= */
- if($articles == "true"){
- switch($extension){
- case 0: // Save to XML file.
- $doc = new DOMDocument('1.0');
- $doc->formatOutput = true;
- $root = $doc->createElement('articles');
- $root = $doc->appendChild($root);
- break;
- case 1: // Save to JSON file.
- $json = [];
- break;
- case 2: // Insert to Database
- $servername = "localhost";
- $username = "";
- $password = "";
- $dbname = "";
- $conn = new mysqli($servername, $username, $password, $dbname);
- if ($conn->connect_error)
- {
- die("Connection failed: " . $conn->connect_error);
- }
- break;
- }
- //Opens the Database
- $dbh = dbase_open($apath, 0)
- or die("Error! Could not open dbase database file '$apath'.");
- //Loops through Datbase.
- if ($dbh) {
- $record_numbers = dbase_numrecords($dbh);
- for ($i = 1; $i <= $record_numbers; $i++) {
- $row = dbase_get_record_with_names($dbh, $i);
- /* ======================= */
- /*Grab Stuff From Database */
- /* ======================= */
- $id = $row["ARTNR"];
- $name = utf8_encode($row["BENAEMN"]);
- $stock = $row["ILAGER"];
- if($row["deleted"] == 1){
- continue;
- }
- switch($extension){
- case 0: // XML Export Option
- $title = $doc->createElement('article'); // <ARTICLE>
- $title = $root->appendChild($title);
- $aid = $doc->createElement('id'); // <ARTICLE> <ID> N/A </ID></ARTICLE>
- $aid = $title->appendChild($aid);
- $aname = $doc->createElement('name'); // <ARTICLE> <NAME> N/A </NAME></ARTICLE>
- $aname = $title->appendChild($aname);
- $astock = $doc->createElement('stock'); // <ARTICLE> <STOCK> N/A </STOCK></ARTICLE>
- $astock = $title->appendChild($astock);
- $a_idtext = $doc->createTextNode($id); // Makes the ID writeable
- $a_idtext = $aid->appendChild($a_idtext); // Writes the ID
- $a_nametext = $doc->createTextNode($name); // Makes the NAME writeable
- $a_nametext = $aname->appendChild($a_nametext); // Writes the NAME
- $a_stock = $doc->createTextNode($stock); // Makes the STOCK writeable
- $a_stock = $astock->appendChild($a_stock); // Writes the STOCK
- break;
- case 1: // JSON Export Option
- $json[] = array("id" => $id, "name" => $name, "stock" => $stock);
- break;
- case 2: // SQL Export Option
- $insert = "UPDATE products SET stock = ".$stock." WHERE id ='".$id."'";
- $addjob = "INSERT into jobs VALUES ('null','articles','".$datetime."')";
- $conn->query($insert);
- $conn->query($addjob);
- break;
- }
- }
- }
- switch($extension){
- case 0: // XML Export Option
- echo 'Wrote: ' . $doc->save("articles.xml") . ' bytes';
- break;
- case 1: // JSON Export Option
- echo json_encode($json[0],JSON_UNESCAPED_UNICODE);
- $fp = fopen('articles.json', 'w');
- fwrite($fp, json_encode($json[0],JSON_UNESCAPED_UNICODE));
- fclose($fp);
- break;
- case 2: // SQL Export Option
- $conn->close();
- break;
- }
- }
- /* ======================= */
- /* OrdersARTICLEConvertion */
- /* ======================= */
- if($aorders == "true"){
- switch($extension){
- case 0: // Save to XML file.
- $orderproducts = [];
- break;
- }
- //Opens the Database
- $dbh = dbase_open($oapath, 0)
- or die("Error! Could not open dbase database file '$oapath'.");
- //Loops through Datbase.
- if ($dbh) {
- $record_numbers = dbase_numrecords($dbh);
- for ($i = 1; $i <= $record_numbers; $i++) {
- $row = dbase_get_record_with_names($dbh, $i);
- /* ======================= */
- /*Grab Stuff From Database */
- /* ======================= */
- $articleid = $row["ARTNR"];
- $orderid = $row["DOKNR"];
- $rowtype = str_replace(' ', '',$row["TYP"]);
- $rowdate = $row["DAT"];
- $quantity = $row["ANTAL1"];
- $rowremoved = $row["MAKUL"];
- $unknown = $row["ARTTR_FL"];
- if($rowremoved == 1){
- continue;
- }
- if($rowtype != "O"){
- continue;
- }
- if($unknown == 1){
- continue;
- }
- $orderproducts[$orderid]["id"][] = $articleid;
- $orderproducts[$orderid]["quantity"][] = $quantity;
- }
- }
- }
- /* ======================= */
- /* Orders Convertion */
- /* ======================= */
- if($orders == "true"){
- switch($extension){
- case 0: // Save to XML file.
- /* $doc = new DOMDocument('1.0');
- $doc->formatOutput = true;
- $root = $doc->createElement('orders');
- $root = $doc->appendChild($root);*/
- $orderXML = new SimpleXMLElement("<orders></orders>");
- $orders = [];
- break;
- case 1: // Save to JSON file.
- $json = [];
- break;
- case 2: // Insert to Database
- $servername = "localhost";
- $username = "";
- $password = "";
- $dbname = "";
- $conn = new mysqli($servername, $username, $password, $dbname);
- if ($conn->connect_error)
- {
- die("Connection failed: " . $conn->connect_error);
- }
- break;
- }
- //Opens the Database
- $dbh = dbase_open($opath, 0)
- or die("Error! Could not open dbase database file '$opath'.");
- //Loops through Datbase.
- if ($dbh) {
- $record_numbers = dbase_numrecords($dbh);
- for ($i = 1; $i <= $record_numbers; $i++) {
- $row = dbase_get_record_with_names($dbh, $i);
- /* ======================= */
- /*Grab Stuff From Database */
- /* ======================= */
- // Grab true or falses to check which orders to grab
- $doktype = str_replace(' ', '',$row["DOKTYP"]); // Is it an order or Invoice? O = Order, F = Invoice
- $delivered = $row["UTSK"]; // Ordererkännande
- $deleted = $row["MAKUL"]; // Deleted Order
- $orderlev = $row["ORDERLEV"]; // Is it delivered? This combined with Orderplock means its locally delivered.
- $listsent = $row["ORDERPLOCK"]; // If the order has been put out on "plock"
- $shipmentinvoice = $row["ORDERFOELJ"]; // If the order has left the stock
- // Grab strings from orders which we will put into database
- $id = $row["DOKNR"]; // Order ID
- $customerid = $row["KUNDNR"]; // Customer ID
- $odate = $row["DATUM1"]; // Order Date
- $odate = date("Y-m-d",strtotime($odate));
- $ldate = $row["DATUM2"]; //Delivery Date
- $orders[$id] = "True";
- ///////////////////////////////
- if($doktype == "F"){// If its an invoice, skip
- continue;
- }
- ///////////////////////////////
- if($shipmentinvoice == 1){// If it left the stock, skip
- continue;
- }
- ///////////////////////////////
- if($deleted == 1){// If its deleted, skip.
- continue;
- }
- ///////////////////////////////
- if($orderlev == 1){
- continue;
- }
- ///////////////////////////////
- if($row["deleted"] == 1){ // If order is deleted, skip.
- continue;
- }
- ///////////////////////////////
- if($listsent == 1){
- $listsent == "True";
- } else {
- $listsent = "False";
- }
- ///////////////////////////////
- if($shipmentinvoice == 1){
- $shipmentinvoice == "True";
- } else {
- $shipmentinvoice = "False";
- }
- ///////////////////////////////
- switch($extension){
- case 0: // XML Export Option
- if($zaorders == "true"){
- $i = 0;
- $productz = count($orderproducts[$id]["id"]);
- }
- $x_order = $orderXML->addChild('order');
- $x_order->addAttribute('id', $id);
- $x_order->addChild('ID', $id);
- $x_order->addChild('CID', $customerid);
- $x_order->addChild('DATE', $odate);
- $x_order->addChild('SHIPPED', $shipmentinvoice);
- $x_order->addChild('PRINTED', $listsent);
- $x_products = $x_order->addChild('products');
- if($zaorders == "true"){
- for($i;$i < $productz;$i++){
- $x_product = $x_products->addChild('product');
- $x_product->addChild('PID', $orderproducts[$id]["id"][$i]);
- $x_product->addChild('QUANTITY', $orderproducts[$id]["quantity"][$i]);
- }
- }
- break;
- case 1: // JSON Export Option
- $json[] = array("id" => $id, "name" => $name, "stock" => $stock);
- break;
- case 2: // SQL Export Option
- $orderinput = "INSERT INTO cordertest (record, id, customerid, date, packed, printpack, handled) VALUES (NULL, ".$id.", ".$customerid.", '".$odate."', '".$shipmentinvoice."', '".$listsent."', 'false');";
- //$addjob = "INSERT into jobs VALUES ('null','articles','".$datetime."')";
- $conn->query($orderinput);
- //$conn->query($addjob);
- break;
- case 3:
- break;
- }
- }
- }
- switch($extension){
- case 0: // XML Export Option
- echo "I got here!";
- echo $orderXML->asXML('order.xml');
- //echo 'Wrote: ' . $orderXML->save("orders.xml") . ' bytes';
- break;
- case 1: // JSON Export Option
- echo json_encode($json[0],JSON_UNESCAPED_UNICODE);
- $fp = fopen('articles.json', 'w');
- fwrite($fp, json_encode($json[0],JSON_UNESCAPED_UNICODE));
- fclose($fp);
- break;
- case 2: // SQL Export Option
- $conn->close();
- break;
- case 3:
- break;
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement