You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

248 lines
9.5 KiB

  1. <?php
  2. /*
  3. * 添加供应商
  4. * author:张帅
  5. */
  6. class add_supply
  7. {
  8. //添加主要流程
  9. function execAddSupply($supplier_name, $area_id, $company_name, $purchase, $sett_type, $sett_frequency, $account_bank, $account_num, $account_name, $link_info)
  10. {
  11. global $pdo;
  12. $sql = "SELECT
  13. supplier_name
  14. FROM
  15. base_supplier
  16. WHERE
  17. cancel_flag = 0
  18. AND supplier_name = '" . $supplier_name . "' AND supplier_type=187 ";
  19. writeLog("check base_supplier:" . $sql);
  20. $result = $pdo->query($sql);
  21. $check_name = $result->fetchAll(PDO::FETCH_ASSOC);
  22. if(count($check_name) > 0)
  23. {
  24. $json['code'] = '1';
  25. $json['info'] = '用户名已存在';
  26. echo json_encode($json);
  27. exit();
  28. }
  29. //1.获取采购信息详情数组
  30. $purchase_array = $this->getPurchaseInfo($purchase);
  31. //2.获取联系人详情数组
  32. $link_array = $this->getLinkInfo($link_info);
  33. //3.判断必填数据是否为空
  34. $judgeData = $this->judgeData($supplier_name, $area_id, $company_name, $sett_type, $sett_frequency);
  35. if ($judgeData['code'] == '1') {
  36. echo json_encode($judgeData);
  37. exit();
  38. }
  39. //4.向数据库中中添加数据
  40. $res = $this->addPdoSupply($supplier_name, $area_id, $company_name, $purchase_array, $sett_type, $sett_frequency, $account_bank, $account_num, $account_name, $link_array);
  41. echo json_encode($res);
  42. exit();
  43. }
  44. //获取采购信息详情数组
  45. function getPurchaseInfo($purchase)
  46. {
  47. if ($purchase == '') {
  48. $purchase_array = array();
  49. return $purchase_array;
  50. }
  51. $purchase = substr($purchase, 1, strlen($purchase) - 2);
  52. $purchase = explode("}{", $purchase);
  53. $i = 0;
  54. foreach ($purchase as $item => $value) {
  55. $value = explode(",", $value);
  56. $status = 0;
  57. foreach ($value as $k => $v) {
  58. if (empty($v)) {
  59. $status = 1;
  60. break;
  61. }
  62. }
  63. if ($status == 0) {
  64. $purchase_array[$i]['product_type'] = $value[0];
  65. $purchase_array[$i]['purchaser_name'] = $value[1];
  66. $i++;
  67. }
  68. }
  69. return $purchase_array;
  70. }
  71. //获取联系人详情数组
  72. function getLinkInfo($link_info)
  73. {
  74. if ($link_info == '') {
  75. $link_array = array();
  76. return $link_array;
  77. }
  78. $link_info = explode(",", $link_info);
  79. $key = $_COOKIE['memcache'];
  80. $link_memcache = get_memcache('ZHANGS_LINK' . $key);
  81. $i = 0;
  82. foreach ($link_info as $item => $value) {
  83. $status = 0;
  84. if (empty($link_memcache[$value]['link_name']) || empty($link_memcache[$value]['contact_name'])) {
  85. $status = 1;
  86. }
  87. if ($status == 0) {
  88. $link_array[$i] = $link_memcache[$value];
  89. $i++;
  90. }
  91. }
  92. return $link_array;
  93. }
  94. //检查必填数据是否为空
  95. function judgeData($supplier_name, $area_id, $company_name, $sett_type, $sett_frequency)
  96. {
  97. if (empty($supplier_name)) {
  98. $json['code'] = '1';
  99. $json['info'] = '供应商名称不能为空';
  100. } elseif (empty($area_id)) {
  101. $json['code'] = '1';
  102. $json['info'] = '供应商区域不能为空';
  103. } elseif (empty($company_name)) {
  104. $json['code'] = '1';
  105. $json['info'] = '供应商公司不能为空';
  106. } elseif (empty($sett_type)) {
  107. $json['code'] = '1';
  108. $json['info'] = '结算方式不能为空';
  109. } elseif (empty($sett_frequency)) {
  110. $json['code'] = '1';
  111. $json['info'] = '结算周期名称不能为空';
  112. } else {
  113. $json['code'] = '0';
  114. $json['info'] = '成功';
  115. }
  116. return ($json);
  117. }
  118. //在数据库中添加供应商信息
  119. function addPdoSupply($supplier_name, $area_id, $company_name, $purchase_array, $sett_type, $sett_frequency, $account_bank, $account_num, $account_name, $link_array)
  120. {
  121. global $pdo;
  122. $user_id = getUserId();//$_COOKIE['user_id'];
  123. //运营主体
  124. $main_corp_id_sql = "select main_corp_id from base_user where id = " . $user_id . ' and cancel_flag = 0 limit 1';
  125. $result = $pdo->query($main_corp_id_sql);
  126. $main_corp_id = $result->fetchAll(PDO::FETCH_ASSOC);
  127. $main_corp_id = $main_corp_id[0]['main_corp_id'];
  128. $time = date('Y-m-d H:i:s', time());
  129. try {
  130. $pdo->beginTransaction();
  131. //1.添加base_supplier表
  132. $supplier_sql = "INSERT INTO base_supplier (
  133. create_user_id,
  134. create_time,
  135. supplier_type,
  136. supplier_name,
  137. area_id,
  138. company_name,
  139. is_disabled,
  140. sett_type,
  141. sett_frequency,
  142. account_bank,
  143. account_num,
  144. account_name,
  145. main_corp_id
  146. )
  147. VALUES
  148. (" . $user_id . ",'" . $time . "',187,'" . $supplier_name . "'," . $area_id . ",'" . $company_name . "',0," . $sett_type . "," . $sett_frequency . ",'" . $account_bank . "','" . $account_num . "','" . $account_name . "'," . $main_corp_id . ")";
  149. writeLog("INSERT INTO base_supplier:" . $supplier_sql);
  150. $result = $pdo->exec($supplier_sql);
  151. if (!$result) {
  152. throw new Exception("db die");
  153. }
  154. $supplier_id = $pdo->lastInsertId();
  155. //2.添加base_supplier_purchase表
  156. if(count($purchase_array) > 0)
  157. {
  158. $purchase_sql = "INSERT INTO base_supplier_purchase (
  159. create_user_id,
  160. create_time,
  161. supplier_id,
  162. product_type,
  163. purchaser_name
  164. )
  165. VALUES ";
  166. foreach ($purchase_array as $key => $v) {
  167. $purchase_sql_array[] = "(" . $user_id . ",'" . $time . "'," . $supplier_id . "," . $v['product_type'] . ",'" . $v['purchaser_name'] . "')";
  168. }
  169. $purchase_sql .= implode(",", $purchase_sql_array);
  170. writeLog("INSERT INTO base_supplier_purchase:" . $purchase_sql);
  171. $result = $pdo->exec($purchase_sql);
  172. if (!$result) {
  173. throw new Exception("db die");
  174. }
  175. }
  176. //3.添加base_supplier_link表
  177. if(count($link_array) > 0)
  178. {
  179. $link_sql = "INSERT INTO base_supplier_link (
  180. create_user_id,
  181. create_time,
  182. supplier_id,
  183. link_name,
  184. contact_name,
  185. contact_mobile,
  186. contact_telphone,
  187. fax,
  188. email,
  189. remark
  190. )
  191. VALUES ";
  192. foreach ($link_array as $key => $v) {
  193. $link_sql_array[] = "(" . $user_id . ",'" . $time . "'," . $supplier_id . ",'" . $v['link_name'] . "','" . $v['contact_name'] . "','" . $v['contact_mobile'] . "','" . $v['contact_telphone'] . "','" . $v['fax'] . "','" . $v['email'] . "','" . $v['remark'] . "')";
  194. }
  195. $link_sql .= implode(",", $link_sql_array);
  196. writeLog("INSERT INTO base_supplier_link:" . $link_sql);
  197. $result = $pdo->exec($link_sql);
  198. if (!$result) {
  199. throw new Exception("db die");
  200. }
  201. }
  202. $pdo->commit();
  203. //4.删除缓存
  204. $key = $_COOKIE['memcache'];
  205. delete_memcache('ZHANGS_LINK' . $key);
  206. $json['code'] = '0';
  207. $json['info'] = '添加数据成功';
  208. } catch (PDOException $ex) {
  209. $pdo->rollBack();
  210. $json['code'] = '1';
  211. $json['info'] = '添加失败';
  212. }
  213. return $json;
  214. }
  215. }
  216. $supplier_name = trim($_POST['supplier_name']);//供应商名称
  217. $area_id = trim($_POST['area_id']);//所属区域市id
  218. $company_name = trim($_POST['company_name']);//公司名称
  219. $purchase = trim($_POST['purchase']);//采购详情
  220. $sett_type = trim($_POST['sett_type']);//结算方式 (授信:275,预付:288,单结:292)
  221. $sett_frequency = trim($_POST['sett_frequency']);//结算周期(日结:293,周结:294,月结:295)
  222. $account_bank = trim($_POST['account_bank']);//开户银行
  223. $account_num = trim($_POST['account_num']);//银行账号
  224. $account_name = trim($_POST['account_name']);//账号名称
  225. $link_info = trim($_POST['link_info']);//联系人详情
  226. $add_supply = new add_supply();
  227. $add_supply->execAddSupply($supplier_name, $area_id, $company_name, $purchase, $sett_type, $sett_frequency, $account_bank, $account_num, $account_name, $link_info);
  228. ?>