PageRenderTime 69ms CodeModel.GetById 37ms RepoModel.GetById 1ms app.codeStats 0ms

/test/include/func_common.php

https://github.com/shoitokazu/GNS-idemitsu-Job
PHP | 311 lines | 290 code | 9 blank | 12 comment | 48 complexity | dfc39df2661b2db11b7f34fe0e6855a0 MD5 | raw file
  1. <?php
  2. function return_error($msg="不正なアクセスです。") {
  3. echo "<p>$msg</p>";
  4. include "footer.php";
  5. exit();
  6. }
  7. function debug_print($txt) {
  8. $filename = 'c:\\debug.txt';
  9. if (is_writable($filename)) {
  10. if (!$handle = fopen($filename, 'a')) {
  11. echo "Cannot open file ($filename)";
  12. exit;
  13. }
  14. if (fwrite($handle, $txt) === FALSE) {
  15. echo "Cannot write to file ($filename)";
  16. exit;
  17. }
  18. fclose($handle);
  19. } else {
  20. echo "The file $filename is not writable";
  21. }
  22. }
  23. function limitCompany($where, $table, $only=false) {
  24. if ($where!='') $where .= " and ";
  25. if ($only) {
  26. $where .= "$table.company=".$_SESSION['company'];
  27. } else {
  28. $where .= "($table.company=".$_SESSION['company']." or $table.company=0)";
  29. }
  30. return $where;
  31. }
  32. function limitOffice($where, $table) {
  33. if ($where!='') $where .= " and ";
  34. $where .= "$table.company=".$_SESSION['company'];
  35. $where .= " and $table.office=".$_SESSION['office'];
  36. return $where;
  37. }
  38. function join_customer($table, $base) {
  39. return "($table) left join customer on $base.ccode=customer.ccode and $base.company=customer.company";
  40. }
  41. function join_machine($table, $base) {
  42. return "($table) left join machine on $base.mcode=machine.mcode and $base.company=machine.company";
  43. }
  44. function join_item($table, $base, $only0=false, $name="item1") {
  45. if ($only0) {
  46. $query = "select * from item where company=0";
  47. } else {
  48. $query = "select * from item where company=0 or company=".$_SESSION['company'];
  49. }
  50. return "($table) left join ($query) as $name on $base.icode=$name.icode";
  51. }
  52. function join_data($table, $base_h, $base_d) {
  53. return "($table) left join $base_d on $base_h.ID=$base_d.hid and $base_h.company=$base_d.company";
  54. }
  55. function join_header($table, $base_h, $base_d) {
  56. return "($table) left join $base_h on $base_h.ID=$base_d.hid and $base_h.company=$base_d.company";
  57. }
  58. /*
  59. function join_stock($table, $base) {
  60. return "($table) left join stock on $base.icode=stock.icode and $base.company=stock.company";
  61. }
  62. */
  63. function join_stock($table, $base) {
  64. $q0 = "select icode, sum(num) as num from stock where ".limitCompany("", "stock", true)." group by icode";
  65. $q1 = "select icode, sum(num) as num from mainte_d left join mainte_h on mainte_d.hid=mainte_h.ID where ".limitCompany("item_state=1", "mainte_d", true)." group by icode";
  66. $q2 = "select icode, sum(num-arrival) as num from order_d left join order_h on order_d.hid=order_h.ID where ".limitCompany("order_state<>0 and complete=0", "order_d", true)." group by icode";
  67. $table = "($table) left join ($q0) as stock_all on $base.icode=stock_all.icode";
  68. $table = "($table) left join ($q1) as use_plan on $base.icode=use_plan.icode";
  69. $table = "($table) left join ($q2) as arrival_plan on $base.icode=arrival_plan.icode";
  70. return $table;
  71. }
  72. function join_company($table, $base) {
  73. return "($table) left join environment on $base.company=environment.company";
  74. }
  75. function echo_button_customer($eid) {
  76. echo "<input type='button' value='選択' onClick='openNormalWindow(\"../customer/search.php?select=".$eid."&ccode=\" + document.getElementById(\"$eid\").value);'>";
  77. }
  78. function echo_button_article($eid) {
  79. echo "<input type='button' value='選択' onClick='openNormalWindow(\"../article/search.php?select=".$eid."&acode=\" + document.getElementById(\"$eid\").value);'>";
  80. }
  81. function echo_button_machine($meid, $ceid="") {
  82. echo "<input type='button' value='選択' onClick='openNormalWindow(\"../machine/search.php?select=$meid,$ceid&mcode=\" + document.getElementById(\"$meid\").value";
  83. if ($ceid<>"") {
  84. echo " + \"&machine__ccode=\" + document.getElementById(\"$ceid\").value";
  85. }
  86. echo ");'>";
  87. }
  88. function echo_button_figure($mode, $id, $model="", $type=1) {
  89. $arg = "select=$mode,$id&type=$type";
  90. if ($model!="") $arg .= "&model=$model";
  91. echo '<input type="button" value="図選択" onClick="openNormalWindow(\'../item/fig_list.php?'.$arg.'\');">';
  92. }
  93. function echo_button_item($mode, $id, $model="", $type=1) {
  94. $arg = "select=$mode,$id&type=$type";
  95. if ($model!="") $arg .= "&model=$model";
  96. echo '<input type="button" value="検索選択" onClick="openNormalWindow(\'../item/search.php?'.$arg.'\');">';
  97. }
  98. function echo_button_edit_choices($field) {
  99. echo '<input type="button" value="編集" onClick="openNormalWindow(\'../config/list.php?select=on&field='.$field.'\');">';
  100. }
  101. function echo_button_edit_group($field) {
  102. echo '<input type="button" value="編集" onClick="openNormalWindow(\'../config/list2.php?select=on&field='.$field.'\');">';
  103. }
  104. function echo_select_script($eid) {
  105. ?>
  106. <script language="javaScript">
  107. function setSelectValue(v) {
  108. window.opener.document.getElementById('<?=$eid?>').value = v;
  109. window.opener.document.form1.submit();
  110. window.close()
  111. }
  112. </script>
  113. <?php
  114. }
  115. function echo_sky_header($filename) {
  116. // header("Pragma: no-cache");
  117. // header("Content-Type: application/octet-stream");
  118. header("Content-Type: text/csv");
  119. // header('Content-Type: application/download; name="contents.sky"');
  120. // header("Content-Type: text/plain");
  121. // header("Content-Length: " . filesize($filename));
  122. header('Content-Disposition: attachment; filename="'.$filename.'"');
  123. }
  124. function get_env($field, $company="") {
  125. global $conn;
  126. $table = "environment";
  127. if ($company=="") {
  128. $where = "company=".$_SESSION['company'];
  129. } else {
  130. $where = "company=$company";
  131. }
  132. $sql = "select $field from $table where $where";
  133. $row = db_row($conn, $sql);
  134. return $row[0];
  135. }
  136. function setCode($table, $id, $field="code") {
  137. global $conn;
  138. $where = "ID=$id";
  139. $sql = "select $field from $table where $where";
  140. $row = db_row($conn, $sql);
  141. if ($row[0]=="") {
  142. $fields[0] = $field;
  143. $types [0] = "str";
  144. // $values[0] = date('ymdHis');
  145. $values[0] = "b2".sprintf("%08s", $id);
  146. db_update($conn, $table, $fields, $types, $values, $where, true);
  147. return $values[0];
  148. }
  149. return $row[0];
  150. }
  151. function echo_sub_header($buttons_html="", $submit_value="更新", $del_link="", $id_list="") {
  152. global $select_arg, $select_mode;
  153. echo '<div class="frame_sub"><table width=90%><tr><td height=25 width=30></td>';
  154. if ($submit_value!="") {
  155. echo "<td width=50>";
  156. echo_form_submit($submit_value);
  157. echo "</td> ";
  158. } else {
  159. echo "<td width=50><br></td> ";
  160. }
  161. if ($del_link!="") {
  162. if (is_lock()) $attr=" disabled";
  163. echo "<td width=50>";
  164. echo '<input type="button" value="削除" onClick="if (confirm(\'削除しますか?\')) location.href=\''.$del_link.'\'"'.$attr.'>';
  165. echo "</td> ";
  166. } else {
  167. echo "<td width=50><br></td> ";
  168. }
  169. echo '<td valign=center>';
  170. if ($select_mode) {
  171. // echo '<input type="button" value="決定" onClick="btn_select()">';
  172. echo '<input name="decide" type="submit" value="決定">';
  173. } else {
  174. echo $buttons_html;
  175. }
  176. echo '</td>';
  177. if ($select_arg<>"") $arg = "&".$select_arg;
  178. if (is_array($id_list)) {
  179. echo '<td valign=center align=right><span class="sub_cursor">';
  180. echo ' '.($id_list['pre']=="" ? "(前へ)" : '<a href="?id='.$id_list['pre'].$arg.'">(前へ)</a>');
  181. echo ' '.$id_list['no']."/".$id_list['records'].' ';
  182. echo ' '.($id_list['next']=="" ? "(次へ)" : '<a href="?id='.$id_list['next'].$arg.'">(次へ)</a>');
  183. echo '</span></td>';
  184. } else {
  185. echo '<td><br></td>';
  186. }
  187. echo '</tr></table></div>';
  188. }
  189. function cutDecimal($value, $decimals_mode) {
  190. $v = db_value($value, "cur");
  191. switch ($decimals_mode) {
  192. case 0:
  193. return floor($v);
  194. case 1:
  195. return ceil($v);
  196. case 2:
  197. return round($v);
  198. }
  199. }
  200. function getFilePath() {
  201. global $FILES_PATH;
  202. return $FILES_PATH."/company".$_SESSION['company']."/";
  203. }
  204. function check_order($id) {
  205. $state = DLookUp("order_state", "order_h", "ID=$id");
  206. if ($state==null) return_error("無効な発注伝票です。");
  207. if ($state[0]!=0) return_error("発注済みなので編集できません。");
  208. }
  209. function echo_company_select($value) {
  210. global $conn;
  211. echo "<select name='company'>";
  212. echo '<option value=""></option>';
  213. $list = DListUp("ID,name", "company", "", "sort,ID,name");
  214. foreach ($list as $v) {
  215. if ($v[0] == $value) {
  216. echo "<option value='$v[0]' selected>$v[1]</option>";
  217. } else {
  218. echo "<option value='$v[0]'>$v[1]</option>";
  219. }
  220. }
  221. echo "</select>";
  222. }
  223. function file_delete($id, $hid, $ftype) {
  224. global $debug, $conn;
  225. $table = "file_d";
  226. $where = "ID=$id and hid=$hid and ftype=$ftype";
  227. $where = limitCompany($where, $table, true);
  228. $sql = "select filename from $table where $where";
  229. $row = db_row($conn, $sql);
  230. if ($row==false) return false;
  231. $filepath = getFilePath().$row[0];
  232. if ($debug) echo "unlink : $filepath<br>";
  233. unlink($filepath);
  234. db_delete($conn, $table, $where);
  235. return true;
  236. }
  237. function is_my_company($rs) {
  238. global $conn, $company;
  239. $v = db_result($rs, "company");
  240. if ($v == $company) return true;
  241. return false;
  242. }
  243. function fgetcsv_reg (&$handle, $length = null, $d = ',', $e = '"') {
  244. $d = preg_quote($d);
  245. $e = preg_quote($e);
  246. $_line = "";
  247. while ($eof != true) {
  248. $_line .= (empty($length) ? fgets($handle) : fgets($handle, $length));
  249. $itemcnt = preg_match_all('/'.$e.'/', $_line, $dummy);
  250. if ($itemcnt % 2 == 0) $eof = true;
  251. }
  252. $_csv_line = preg_replace('/(?:\r\n|[\r\n])?$/', $d, trim($_line));
  253. $_csv_pattern = '/('.$e.'[^'.$e.']*(?:'.$e.$e.'[^'.$e.']*)*'.$e.'|[^'.$d.']*)'.$d.'/';
  254. preg_match_all($_csv_pattern, $_csv_line, $_csv_matches);
  255. $_csv_data = $_csv_matches[1];
  256. for($_csv_i=0;$_csv_i<count($_csv_data);$_csv_i++){
  257. $_csv_data[$_csv_i]=preg_replace('/^'.$e.'(.*)'.$e.'$/s','$1',$_csv_data[$_csv_i]);
  258. $_csv_data[$_csv_i]=str_replace($e.$e, $e, $_csv_data[$_csv_i]);
  259. }
  260. return empty($_line) ? false : $_csv_data;
  261. }
  262. function get_schedule_id($uid, $area, $date, $type="") {
  263. global $conn, $debug;
  264. $tmp = $debug;
  265. $debug = false;
  266. $base = "schedule";
  267. $table = $base;
  268. $table = "($table) left join account on $base.uid=account.ID";
  269. if ($area=="") {
  270. $where = "$base.uid=$uid";
  271. } else {
  272. $where = "account.company=".db_value($area, "int");
  273. }
  274. $where .= " and date=".db_value($date, "date");
  275. $where .= " and account.auth<>0";
  276. if ($type<>"") $where .= " and schedule_type=".db_value($type, "int");
  277. $sql = "select $base.ID from $table where $where";
  278. $rs = db_exec($conn, $sql);
  279. while (db_fetch_row($rs)) {
  280. $list[] = db_result($rs, "ID");
  281. }
  282. $table = $base;
  283. $table = "($table) inner join schedule_date on $base.ID=schedule_date.hid";
  284. $table = "($table) inner join schedule_user on $base.ID=schedule_user.hid";
  285. $table = "($table) left join account on schedule_user.uid=account.ID";
  286. if ($area=="") {
  287. $where = "schedule_user.uid=$uid";
  288. } else {
  289. $where = "account.company=".db_value($area, "int");
  290. }
  291. $where .= " and schedule_date.date=".db_value($date, "date");
  292. $where .= " and account.auth<>0";
  293. if ($type<>"") $where .= " and schedule_type=".db_value($type, "int");
  294. $sql = "select $base.ID from $table where $where";
  295. $rs = db_exec($conn, $sql);
  296. while (db_fetch_row($rs)) {
  297. $list[] = db_result($rs, "ID");
  298. }
  299. $debug = $tmp;
  300. return $list;
  301. }
  302. ?>