Advertisement
Guest User

Untitled

a guest
Apr 28th, 2016
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.31 KB | None | 0 0
  1. <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
  2. <?php
  3. // =============== EDWINS VISMA .DBF READER/CONVERTER =============== //
  4. set_time_limit(0);
  5.  
  6. /* ======================= */
  7. /* Converter Variables */
  8. /* ======================= */
  9. $extension = 3; // 0 = XML, 1 = JSON, 2 = To MYSQL, 3 = DEBUG
  10. date_default_timezone_set('Europe/Stockholm'); // TIME- zone
  11. $datetime = date_create()->format('Y-m-d H:i:s'); //TIME- create
  12. $cpath = "KUND.DBF"; // Path To Customer Database
  13. $apath = "ART.DBF"; // Path To Article Database
  14. $opath = "OOF.DBF"; // Path To Order Database
  15. $oapath = "ARTRAD.DBF"; // Path To Order Article Database
  16.  
  17. /* ======================= */
  18. /* Converter Booleans */
  19. /* ======================= */
  20. $customers = "false"; // Export Customers From Visma
  21. $articles = "false"; // Export Articles From Visma
  22. $orders = "true"; // Export Orders From Visma
  23. $invoices = "false"; // Export Invoices From Visma
  24. $aorders = "true"; // Export Articles In Orders From Visma
  25.  
  26.  
  27.  
  28. /* ======================= */
  29. /* Customers Convertion */
  30. /* ======================= */
  31. if($customers == "true"){
  32. /* Preparation */
  33. switch($extension){
  34. case 0: // Save to XML file.
  35. $doc = new DOMDocument('1.0');
  36. $doc->formatOutput = true;
  37. $root = $doc->createElement('customers');
  38. $root = $doc->appendChild($root);
  39. break;
  40. case 1: // Save to JSON file.
  41. $json = [];
  42. break;
  43. case 2: // Insert to Database
  44. $servername = "localhost";
  45. $username = "";
  46. $password = "";
  47. $dbname = "";
  48. $conn = new mysqli($servername, $username, $password, $dbname);
  49. if ($conn->connect_error)
  50. {
  51. die("Connection failed: " . $conn->connect_error);
  52. }
  53. break;
  54. }
  55.  
  56. /* Execution */
  57.  
  58. //Opens the Database
  59. $dbh = dbase_open($cpath, 0)
  60. or die("Error! Could not open dbase database file '$cpath'.");
  61.  
  62. //Loops through Datbase.
  63. if ($dbh) {
  64. $record_numbers = dbase_numrecords($dbh);
  65. for ($i = 1; $i <= $record_numbers; $i++) {
  66. $row = dbase_get_record_with_names($dbh, $i);
  67.  
  68. /* ======================= */
  69. /*Grab Stuff From Database */
  70. /* ======================= */
  71. $id = $row["KUNDNR"];
  72. $name = utf8_encode($row["NAMN"]);
  73. $country = $row["LAND"];
  74.  
  75.  
  76. switch($extension){
  77. case 0: // XML Export Option
  78. $title = $doc->createElement('customer'); // <CUSTOMER>
  79. $title = $root->appendChild($title);
  80.  
  81. $cid = $doc->createElement('id'); // <CUSTOMER> <ID> N/A </ID></CUSTOMER>
  82. $cid = $title->appendChild($cid);
  83.  
  84. $cname = $doc->createElement('name'); // <CUSTOMER> <NAME> N/A </NAME></CUSTOMER>
  85. $cname = $title->appendChild($cname);
  86.  
  87. $ccountry = $doc->createElement('country'); // <CUSTOMER> <COUNTRY> N/A </COUNTRY></CUSTOMER>
  88. $ccountry = $title->appendChild($ccountry);
  89.  
  90. $idtext = $doc->createTextNode($id); // Makes the ID writeable
  91. $idtext = $cid->appendChild($idtext); // Writes the ID
  92. $nametext = $doc->createTextNode($name); // Makes the NAME writeable
  93. $nametext = $cname->appendChild($nametext); // Writes the NAME
  94. $countrytext = $doc->createTextNode($country); // Makes the COUNTRY writeable
  95. $countrytext = $ccountry->appendChild($countrytext); // Writes the COUNTRY
  96. break;
  97. case 1: // JSON Export Option
  98. $json[] = array("id" => $id, "name" => $name, "country" => $country);
  99. break;
  100. case 2: // SQL Export Option
  101. $insert = "INSERT into importtest VALUES ('".$id."','".$row["NAMN"]."','".$country."')";
  102. $conn->query($insert);
  103. break;
  104. }
  105. }
  106. }
  107.  
  108. /* Wrap up */
  109. switch($extension){
  110. case 0: // XML Export Option
  111. echo 'Wrote: ' . $doc->save("customers.xml") . ' bytes';
  112. break;
  113. case 1: // JSON Export Option
  114. echo json_encode($json[0],JSON_UNESCAPED_UNICODE);
  115. $fp = fopen('customers.json', 'w');
  116. fwrite($fp, json_encode($json[0],JSON_UNESCAPED_UNICODE));
  117. fclose($fp);
  118. break;
  119. case 2: // SQL Export Option
  120. $conn->close();
  121. break;
  122. }
  123.  
  124. }
  125.  
  126.  
  127. /* ======================= */
  128. /* Articles Convertion */
  129. /* ======================= */
  130. if($articles == "true"){
  131. switch($extension){
  132. case 0: // Save to XML file.
  133. $doc = new DOMDocument('1.0');
  134. $doc->formatOutput = true;
  135. $root = $doc->createElement('articles');
  136. $root = $doc->appendChild($root);
  137. break;
  138. case 1: // Save to JSON file.
  139. $json = [];
  140. break;
  141. case 2: // Insert to Database
  142. $servername = "localhost";
  143. $username = "";
  144. $password = "";
  145. $dbname = "";
  146. $conn = new mysqli($servername, $username, $password, $dbname);
  147. if ($conn->connect_error)
  148. {
  149. die("Connection failed: " . $conn->connect_error);
  150. }
  151. break;
  152. }
  153.  
  154. //Opens the Database
  155. $dbh = dbase_open($apath, 0)
  156. or die("Error! Could not open dbase database file '$apath'.");
  157.  
  158. //Loops through Datbase.
  159. if ($dbh) {
  160. $record_numbers = dbase_numrecords($dbh);
  161. for ($i = 1; $i <= $record_numbers; $i++) {
  162. $row = dbase_get_record_with_names($dbh, $i);
  163.  
  164. /* ======================= */
  165. /*Grab Stuff From Database */
  166. /* ======================= */
  167. $id = $row["ARTNR"];
  168. $name = utf8_encode($row["BENAEMN"]);
  169. $stock = $row["ILAGER"];
  170. if($row["deleted"] == 1){
  171. continue;
  172. }
  173.  
  174.  
  175. switch($extension){
  176. case 0: // XML Export Option
  177. $title = $doc->createElement('article'); // <ARTICLE>
  178. $title = $root->appendChild($title);
  179.  
  180. $aid = $doc->createElement('id'); // <ARTICLE> <ID> N/A </ID></ARTICLE>
  181. $aid = $title->appendChild($aid);
  182.  
  183. $aname = $doc->createElement('name'); // <ARTICLE> <NAME> N/A </NAME></ARTICLE>
  184. $aname = $title->appendChild($aname);
  185.  
  186. $astock = $doc->createElement('stock'); // <ARTICLE> <STOCK> N/A </STOCK></ARTICLE>
  187. $astock = $title->appendChild($astock);
  188.  
  189. $a_idtext = $doc->createTextNode($id); // Makes the ID writeable
  190. $a_idtext = $aid->appendChild($a_idtext); // Writes the ID
  191. $a_nametext = $doc->createTextNode($name); // Makes the NAME writeable
  192. $a_nametext = $aname->appendChild($a_nametext); // Writes the NAME
  193. $a_stock = $doc->createTextNode($stock); // Makes the STOCK writeable
  194. $a_stock = $astock->appendChild($a_stock); // Writes the STOCK
  195. break;
  196. case 1: // JSON Export Option
  197. $json[] = array("id" => $id, "name" => $name, "stock" => $stock);
  198. break;
  199. case 2: // SQL Export Option
  200. $insert = "UPDATE products SET stock = ".$stock." WHERE id ='".$id."'";
  201. $addjob = "INSERT into jobs VALUES ('null','articles','".$datetime."')";
  202. $conn->query($insert);
  203. $conn->query($addjob);
  204. break;
  205. }
  206. }
  207. }
  208.  
  209. switch($extension){
  210. case 0: // XML Export Option
  211. echo 'Wrote: ' . $doc->save("articles.xml") . ' bytes';
  212. break;
  213. case 1: // JSON Export Option
  214. echo json_encode($json[0],JSON_UNESCAPED_UNICODE);
  215. $fp = fopen('articles.json', 'w');
  216. fwrite($fp, json_encode($json[0],JSON_UNESCAPED_UNICODE));
  217. fclose($fp);
  218. break;
  219. case 2: // SQL Export Option
  220. $conn->close();
  221. break;
  222. }
  223.  
  224. }
  225.  
  226.  
  227.  
  228. /* ======================= */
  229. /* OrdersARTICLEConvertion */
  230. /* ======================= */
  231.  
  232. if($aorders == "true"){
  233. switch($extension){
  234. case 0: // Save to XML file.
  235. $orderproducts = [];
  236. break;
  237. }
  238.  
  239. //Opens the Database
  240. $dbh = dbase_open($oapath, 0)
  241. or die("Error! Could not open dbase database file '$oapath'.");
  242.  
  243. //Loops through Datbase.
  244. if ($dbh) {
  245. $record_numbers = dbase_numrecords($dbh);
  246. for ($i = 1; $i <= $record_numbers; $i++) {
  247. $row = dbase_get_record_with_names($dbh, $i);
  248.  
  249. /* ======================= */
  250. /*Grab Stuff From Database */
  251. /* ======================= */
  252. $articleid = $row["ARTNR"];
  253. $orderid = $row["DOKNR"];
  254. $rowtype = str_replace(' ', '',$row["TYP"]);
  255. $rowdate = $row["DAT"];
  256. $quantity = $row["ANTAL1"];
  257. $rowremoved = $row["MAKUL"];
  258. $unknown = $row["ARTTR_FL"];
  259.  
  260. if($rowremoved == 1){
  261. continue;
  262. }
  263. if($rowtype != "O"){
  264. continue;
  265. }
  266. if($unknown == 1){
  267. continue;
  268. }
  269.  
  270. $orderproducts[$orderid]["id"][] = $articleid;
  271. $orderproducts[$orderid]["quantity"][] = $quantity;
  272.  
  273.  
  274.  
  275. }
  276. }
  277. }
  278.  
  279.  
  280.  
  281. /* ======================= */
  282. /* Orders Convertion */
  283. /* ======================= */
  284. if($orders == "true"){
  285. switch($extension){
  286. case 0: // Save to XML file.
  287. /* $doc = new DOMDocument('1.0');
  288. $doc->formatOutput = true;
  289. $root = $doc->createElement('orders');
  290. $root = $doc->appendChild($root);*/
  291. $orderXML = new SimpleXMLElement("<orders></orders>");
  292. $orders = [];
  293. break;
  294. case 1: // Save to JSON file.
  295. $json = [];
  296. break;
  297. case 2: // Insert to Database
  298. $servername = "localhost";
  299. $username = "";
  300. $password = "";
  301. $dbname = "";
  302. $conn = new mysqli($servername, $username, $password, $dbname);
  303. if ($conn->connect_error)
  304. {
  305. die("Connection failed: " . $conn->connect_error);
  306. }
  307. break;
  308. }
  309.  
  310. //Opens the Database
  311. $dbh = dbase_open($opath, 0)
  312. or die("Error! Could not open dbase database file '$opath'.");
  313.  
  314. //Loops through Datbase.
  315. if ($dbh) {
  316. $record_numbers = dbase_numrecords($dbh);
  317. for ($i = 1; $i <= $record_numbers; $i++) {
  318. $row = dbase_get_record_with_names($dbh, $i);
  319.  
  320. /* ======================= */
  321. /*Grab Stuff From Database */
  322. /* ======================= */
  323.  
  324. // Grab true or falses to check which orders to grab
  325. $doktype = str_replace(' ', '',$row["DOKTYP"]); // Is it an order or Invoice? O = Order, F = Invoice
  326. $delivered = $row["UTSK"]; // Ordererkännande
  327. $deleted = $row["MAKUL"]; // Deleted Order
  328. $orderlev = $row["ORDERLEV"]; // Is it delivered? This combined with Orderplock means its locally delivered.
  329. $listsent = $row["ORDERPLOCK"]; // If the order has been put out on "plock"
  330. $shipmentinvoice = $row["ORDERFOELJ"]; // If the order has left the stock
  331.  
  332.  
  333. // Grab strings from orders which we will put into database
  334. $id = $row["DOKNR"]; // Order ID
  335. $customerid = $row["KUNDNR"]; // Customer ID
  336. $odate = $row["DATUM1"]; // Order Date
  337. $odate = date("Y-m-d",strtotime($odate));
  338. $ldate = $row["DATUM2"]; //Delivery Date
  339. $orders[$id] = "True";
  340.  
  341. ///////////////////////////////
  342. if($doktype == "F"){// If its an invoice, skip
  343. continue;
  344. }
  345. ///////////////////////////////
  346. if($shipmentinvoice == 1){// If it left the stock, skip
  347. continue;
  348. }
  349. ///////////////////////////////
  350. if($deleted == 1){// If its deleted, skip.
  351. continue;
  352. }
  353. ///////////////////////////////
  354. if($orderlev == 1){
  355. continue;
  356. }
  357. ///////////////////////////////
  358. if($row["deleted"] == 1){ // If order is deleted, skip.
  359. continue;
  360. }
  361. ///////////////////////////////
  362. if($listsent == 1){
  363. $listsent == "True";
  364. } else {
  365. $listsent = "False";
  366. }
  367. ///////////////////////////////
  368. if($shipmentinvoice == 1){
  369. $shipmentinvoice == "True";
  370. } else {
  371. $shipmentinvoice = "False";
  372. }
  373. ///////////////////////////////
  374.  
  375. switch($extension){
  376. case 0: // XML Export Option
  377. if($zaorders == "true"){
  378. $i = 0;
  379. $productz = count($orderproducts[$id]["id"]);
  380. }
  381. $x_order = $orderXML->addChild('order');
  382. $x_order->addAttribute('id', $id);
  383. $x_order->addChild('ID', $id);
  384. $x_order->addChild('CID', $customerid);
  385. $x_order->addChild('DATE', $odate);
  386. $x_order->addChild('SHIPPED', $shipmentinvoice);
  387. $x_order->addChild('PRINTED', $listsent);
  388. $x_products = $x_order->addChild('products');
  389. if($zaorders == "true"){
  390. for($i;$i < $productz;$i++){
  391. $x_product = $x_products->addChild('product');
  392. $x_product->addChild('PID', $orderproducts[$id]["id"][$i]);
  393. $x_product->addChild('QUANTITY', $orderproducts[$id]["quantity"][$i]);
  394. }
  395. }
  396.  
  397. break;
  398. case 1: // JSON Export Option
  399. $json[] = array("id" => $id, "name" => $name, "stock" => $stock);
  400. break;
  401. case 2: // SQL Export Option
  402. $orderinput = "INSERT INTO cordertest (record, id, customerid, date, packed, printpack, handled) VALUES (NULL, ".$id.", ".$customerid.", '".$odate."', '".$shipmentinvoice."', '".$listsent."', 'false');";
  403. //$addjob = "INSERT into jobs VALUES ('null','articles','".$datetime."')";
  404. $conn->query($orderinput);
  405.  
  406. //$conn->query($addjob);
  407. break;
  408. case 3:
  409. break;
  410. }
  411.  
  412.  
  413.  
  414.  
  415.  
  416.  
  417.  
  418.  
  419.  
  420. }
  421. }
  422.  
  423. switch($extension){
  424. case 0: // XML Export Option
  425. echo "I got here!";
  426. echo $orderXML->asXML('order.xml');
  427. //echo 'Wrote: ' . $orderXML->save("orders.xml") . ' bytes';
  428. break;
  429. case 1: // JSON Export Option
  430. echo json_encode($json[0],JSON_UNESCAPED_UNICODE);
  431. $fp = fopen('articles.json', 'w');
  432. fwrite($fp, json_encode($json[0],JSON_UNESCAPED_UNICODE));
  433. fclose($fp);
  434. break;
  435. case 2: // SQL Export Option
  436. $conn->close();
  437. break;
  438. case 3:
  439.  
  440. break;
  441. }
  442.  
  443. }
  444.  
  445.  
  446.  
  447.  
  448.  
  449. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement