FyySqlServerService.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. <?php
  2. namespace App\Service;
  3. use App\Model\Orders;
  4. use App\Model\SaleOrdersProduct;
  5. use Illuminate\Support\Facades\Config;
  6. use Illuminate\Support\Facades\DB;
  7. use Illuminate\Support\Facades\Log;
  8. use Illuminate\Support\Facades\Redis;
  9. class FyySqlServerService extends Service
  10. {
  11. public $db = null;
  12. public $error = null;
  13. public $host = "";
  14. public $port = 0;
  15. public $database = "";
  16. public $url = "";
  17. public function __construct()
  18. {
  19. $fyy_array = Redis::get('fyy_sql_server');
  20. if(empty($fyy_array)) {
  21. $this->error = 'SQLSERVER设置不存在!';
  22. return;
  23. }
  24. $fyy_array = json_decode($fyy_array,true);
  25. $this->host = $fyy_array['sqlserver_host'] ?? '';
  26. $this->port = $fyy_array['sqlserver_port'] ?? 0;
  27. $this->database = $fyy_array['sqlserver_database'] ?? '';
  28. $this->url = empty($this->host)?: "http://" . $this->host . ":12365/U8Sys/U8API";
  29. if(! $this->db){
  30. $config = [
  31. 'driver' => 'sqlsrv',
  32. 'host' => $fyy_array['sqlserver_host'] ?? '',
  33. 'port' => $fyy_array['sqlserver_port'] ?? '',
  34. 'database' => $fyy_array['sqlserver_database'] ?? '',
  35. 'username' => env('SQLSRV_USERNAME'),
  36. 'password' => env('SQLSRV_PASSWORD'),
  37. ];
  38. // 进行数据库连接
  39. Config::set('database.connections.sqlsrvs', $config);
  40. try {
  41. $pdo = DB::connection('sqlsrvs')->getPdo();
  42. if ($pdo instanceof \PDO) {
  43. // 连接成功的逻辑代码
  44. $this->db = DB::connection('sqlsrvs');
  45. } else {
  46. $this->error = '连接失败!';
  47. return;
  48. }
  49. } catch (\Throwable $e) {
  50. $this->error = $e->getMessage();
  51. }
  52. }
  53. }
  54. private function fakeData(){
  55. $return = [
  56. [
  57. 'out_order_no' => '001',
  58. 'out_order_no_time' => 1685928995,
  59. 'customer_no' => '201603012',
  60. 'customer_name' => '巴大胡',
  61. 'table_header_mark' => '表头备注',
  62. 'product_no' => '产品编码001',
  63. 'product_title' => '产品名称001',
  64. 'product_size' => '产品规格30c',
  65. 'product_unit' => '产品单位米',
  66. 'order_quantity' => 100,
  67. 'product_quantity_on_hand' => 50,
  68. 'technology_material' => '工艺材质',
  69. 'technology_name' => '工艺名称:同命',
  70. 'wood_name' => '木皮名称:梭梭树皮',
  71. 'process_mark' => '加工备注',
  72. 'table_body_mark' => '表体备注',
  73. 'out_crt_man' => '外部人员一',
  74. 'out_checker_man' => '外部人员二',
  75. 'out_checker_time' => 1685928995,
  76. ],
  77. [
  78. 'out_order_no' => '001',
  79. 'out_order_no_time' => 1685928995,
  80. 'customer_no' => '201603012',
  81. 'customer_name' => '巴大胡',
  82. 'table_header_mark' => '表头备注',
  83. 'product_no' => '产品编码002',
  84. 'product_title' => '产品名称002',
  85. 'product_size' => '产品规格30d',
  86. 'product_unit' => '产品单位米',
  87. 'order_quantity' => 100,
  88. 'product_quantity_on_hand' => 50,
  89. 'technology_material' => '工艺材质',
  90. 'technology_name' => '工艺名称:同命',
  91. 'wood_name' => '木皮名称:梭梭树皮',
  92. 'process_mark' => '加工备注',
  93. 'table_body_mark' => '表体备注',
  94. 'out_crt_man' => '外部人员一',
  95. 'out_checker_man' => '外部人员二',
  96. 'out_checker_time' => 1685928995,
  97. ],
  98. [
  99. 'out_order_no' => '002',
  100. 'out_order_no_time' => 1685928995,
  101. 'customer_no' => '201603012',
  102. 'customer_name' => '巴大胡',
  103. 'table_header_mark' => '表头备注',
  104. 'product_no' => '产品编码002',
  105. 'product_title' => '产品名称002',
  106. 'product_size' => '产品规格30d',
  107. 'product_unit' => '产品单位米',
  108. 'order_quantity' => 100,
  109. 'product_quantity_on_hand' => 50,
  110. 'technology_material' => '工艺材质',
  111. 'technology_name' => '工艺名称:同命',
  112. 'wood_name' => '木皮名称:梭梭树皮',
  113. 'process_mark' => '加工备注',
  114. 'table_body_mark' => '表体备注',
  115. 'out_crt_man' => '外部人员一',
  116. 'out_checker_man' => '外部人员二',
  117. 'out_checker_time' => 1685928995,
  118. ],
  119. ];
  120. return $return;
  121. }
  122. public function is_same_month($timestamp1,$timestamp2){
  123. date_default_timezone_set("PRC");
  124. // 格式化时间戳为年份和月份
  125. $year1 = date('Y', $timestamp1);
  126. $month1 = date('m', $timestamp1);
  127. $year2 = date('Y', $timestamp2);
  128. $month2 = date('m', $timestamp2);
  129. if ($year1 === $year2 && $month1 === $month2) {
  130. return true;
  131. } else {
  132. return false;
  133. }
  134. }
  135. //获取数据(点击引入)
  136. public function getDataFromSqlServer($data){
  137. if(! empty($this->error)) return [false,$this->error,''];
  138. if(empty($data['out_order_no_time'][0]) || empty($data['out_order_no_time'][1])) return [false,'制单日期不能为空!',''];
  139. $bool = $this->is_same_month($data['out_order_no_time'][0],$data['out_order_no_time'][1]);
  140. if(! $bool) return [false,'制单日期必须同月!',''];
  141. //查询产品主表副表数据
  142. date_default_timezone_set("PRC");
  143. $start = date('Y-m-d H:i:s.000',$data['out_order_no_time'][0]);
  144. $end = date('Y-m-d H:i:s.000',$data['out_order_no_time'][1]);
  145. $model = $this->db->table('SO_SOMain as a')
  146. ->leftJoin('SO_SODetails as b','b.cSOCode','a.cSOCode')
  147. ->whereBetween('a.dDate',[$start, $end])
  148. ->whereNotNull('a.cVerifier')
  149. ->select('a.cSOCode as out_order_no','a.dDate as out_order_no_time','a.cCusCode as customer_no','a.cCusName as customer_name','a.cMemo as table_header_mark','a.cMaker as out_crt_man','a.cVerifier as out_checker_man','a.dverifydate as out_checker_time','b.cInvCode as product_no','b.iQuantity as order_quantity','b.cDefine28 as technology_material','b.cFree1 as technology_name','b.cFree2 as wood_name','b.cDefine30 as process_mark','b.cMemo as table_body_mark');
  150. if(! empty($data['out_order_no'])) $model->where('a.cSOCode', 'LIKE', '%'.$data['out_order_no'].'%');
  151. if(! empty($data['customer_no'])) $model->where('a.cCusCode', 'LIKE', '%'.$data['customer_no'].'%');
  152. if(! empty($data['customer_name'])) $model->where('a.cCusName', 'LIKE', '%'.$data['customer_name'].'%');
  153. if(! empty($data['table_header_mark'])) $model->where('a.cMemo', 'LIKE', '%'.$data['table_header_mark'].'%');
  154. if(! empty($data['out_crt_man'])) $model->where('a.cMaker', 'LIKE', '%'.$data['out_crt_man'].'%');
  155. if(! empty($data['out_checker_man'])) $model->where('a.cVerifier', 'LIKE', '%'.$data['out_checker_man'].'%');
  156. if(! empty($data['out_checker_time'][0]) && ! empty($data['out_checker_time'][1])) {
  157. $start1 = date('Y-m-d H:i:s.000',$data['out_checker_time'][0]);
  158. $end1 = date('Y-m-d H:i:s.000',$data['out_checker_time'][1]);
  159. $model->whereBetween('a.dverifydate',[$start1, $end1]);
  160. }
  161. $result = $model->get()->toArray();
  162. if(empty($result)) return [false,'暂无数据,更新结束!',''];
  163. list($status,$msg) = $this->orderRule($result);
  164. if(! $status) return [false,$msg,''];
  165. //查询附带的一些信息(比较少)
  166. $product_no = array_column($result,'product_no');
  167. $messageOne = $this->db->table('Inventory as a')
  168. ->join('ComputationUnit as b',function($join){
  169. $join->on('a.cGroupCode','=','b.cGroupCode')
  170. ->on('a.cComUnitCode','=','b.cComUnitCode');
  171. }, null,null,'left')
  172. ->whereIn('a.cInvCode',$product_no)
  173. ->select('a.cInvCode as product_no','a.cInvName as product_title','a.cInvStd as product_size','b.cComUnitName as product_unit')
  174. ->get()->toArray();
  175. $messageMap = array_column($messageOne,null,'product_no');
  176. //现存量查询开始 ---组织查询条件
  177. $args = '';
  178. foreach ($result as $value){
  179. $product = $value->product_no;
  180. $technology_name = $value->technology_name ?? '';
  181. $wood_name = $value->wood_name ?? '';
  182. $args .= "(a.cInvCode = '{$product}' and a.cFree1 = '{$technology_name}' and a.cFree2 = '{$wood_name}') OR ";
  183. }
  184. $args = rtrim($args,'OR ');
  185. $messageTwo = $this->db->table('CurrentStock as a')
  186. ->leftJoin('Warehouse as b','b.cWhCode','a.cWhCode')
  187. ->whereRaw("($args)")
  188. ->where('a.iQuantity','>',0)
  189. ->select('a.iQuantity as product_quantity_on_hand','a.cInvCode as product_no','a.cFree1 as technology_name','a.cFree2 as wood_name','b.cWhName as warehouse_name')
  190. ->get()->toArray();
  191. if(! empty($messageTwo)){
  192. foreach ($messageTwo as $key => $value){
  193. $messageTwo[$key] = (array)$value;
  194. }
  195. }
  196. //现存量查询结束
  197. foreach ($result as $key => $value){
  198. $result[$key]->technology_material = $value->technology_material ?? '';
  199. $result[$key]->technology_name = $value->technology_name ?? '';
  200. $result[$key]->wood_name = $value->wood_name ?? '';
  201. $result[$key]->process_mark = $value->process_mark ?? '';
  202. $result[$key]->table_body_mark = $value->table_body_mark ?? '';
  203. $result[$key]->table_header_mark = $value->table_header_mark ?? '';
  204. $keys = $value->product_no . $value->technology_name . $value->wood_name;
  205. $result[$key]->out_order_no_time = $value->out_order_no_time ? strtotime($value->out_order_no_time) : 0;
  206. $result[$key]->out_checker_time = $value->out_checker_time ? strtotime($value->out_checker_time) : 0;
  207. $result[$key]->product_title = $messageMap[$value->product_no]->product_title ?? '';
  208. $result[$key]->product_size = $messageMap[$value->product_no]->product_size ?? '';
  209. $result[$key]->product_unit = $messageMap[$value->product_no]->product_unit ?? '';
  210. $result[$key] = (array)$value;
  211. }
  212. return [true,$result,$messageTwo];
  213. }
  214. public function orderRule($data){
  215. $result = Orders::where('del_time',0)
  216. ->whereIn('out_order_no',array_column($data,'out_order_no'))
  217. ->select('out_order_no')
  218. ->get()->toArray();
  219. if(! empty($result)) return [false,'查询区间内销售订单号已存在'];
  220. return [true,''];
  221. }
  222. //获取数据(刷新现存量)
  223. public function getDataFromSqlServerForOnHand($data){
  224. if(! empty($this->error)) return [false,$this->error,''];
  225. if(empty($data['id'])) return [false,'数据不能为空!',''];
  226. $product = SaleOrdersProduct::whereIn('id',$data['id'])
  227. ->select('product_no','technology_name','wood_name')
  228. ->get()->toArray();
  229. //现存量查询开始 ---组织查询条件
  230. $args = '';
  231. foreach ($product as $value){
  232. $args .= "(a.cInvCode = '{$value['product_no']}' and a.cFree1 = '{$value['technology_name']}' and a.cFree2 = '{$value['wood_name']}') OR ";
  233. }
  234. $args = rtrim($args,'OR ');
  235. $message = $this->db->table('CurrentStock as a')
  236. ->leftJoin('Warehouse as b','b.cWhCode','a.cWhCode')
  237. ->whereRaw("($args)")
  238. ->where('a.iQuantity','>',0)
  239. ->select('a.iQuantity as product_quantity_on_hand','a.cInvCode as product_no','a.cFree1 as technology_name','a.cFree2 as wood_name','b.cWhName as warehouse_name')
  240. ->get()->toArray();
  241. if(! empty($message)){
  242. foreach ($message as $key => $value){
  243. $message[$key] = (array)$value;
  244. }
  245. }
  246. //现存量查询结束
  247. return [true,$message,$product];
  248. }
  249. //产成品入库单保存接口以及审核
  250. public function U8Rdrecord10Save($data){
  251. if(! empty($this->error)) return [false,$this->error];
  252. date_default_timezone_set("PRC");
  253. list($bool,$msg) = $this->createOrderNumberAboutSCRK();
  254. if(! $bool) return [false,$msg];
  255. //数据
  256. $bodys = [];
  257. foreach ($data as $value){
  258. $bodys[] = [
  259. "cinvcode" => $value["product_no"],
  260. "cposition" => "",
  261. "cbatch" => "",
  262. "iquantity" => $value["quantity"],
  263. "inum" => $value["quantity"],
  264. "iunitcost" => "0",
  265. "iprice" => "0",
  266. "iinvexchrate" => "1.00",
  267. "impoids" => "",
  268. "cmocode" => "",
  269. "imoseq" => "",
  270. "cbmemo" => "",
  271. "cfree1" => "",
  272. "cfree2" => ""
  273. ];
  274. }
  275. $post = [
  276. "password"=>"cloud@123456",
  277. "entity"=>"U8Rdrecord10Save",
  278. "login"=>[
  279. "sAccID"=> "(default)@001",
  280. "sDate"=> date("Y-m-d"),
  281. "sServer"=> '127.0.0.1',
  282. "sUserID"=> "董晓磊",
  283. "sSerial"=> "",
  284. "sPassword"=> ""
  285. ],
  286. "data"=>[
  287. "ccode"=>$msg,
  288. "ddate"=>date("Y-m-d"),
  289. "cmaker"=>"董晓磊",
  290. "dnmaketime"=> date("Y-m-d"),
  291. "IsExamine"=>true,
  292. "bredvouch"=>"0",
  293. "cwhcode"=>"02",
  294. "cdepcode"=>"",
  295. "crdcode"=>"",
  296. "cmemo"=>"",
  297. "bodys"=>$bodys
  298. ]
  299. ];
  300. $return = $this->post_helper($this->url,json_encode($post), ['Content-Type:application/json']);
  301. return [$return['flag'], $return['msg']];
  302. }
  303. //产成品入库单单据号
  304. public function createOrderNumberAboutSCRK(){
  305. date_default_timezone_set("PRC");
  306. $current_month = date('m'); // 获取当前月份
  307. $date1 = date('y') . $current_month ; // 格式 "2307"
  308. $date2 = date('Y') . $current_month; // 格式 "202307"
  309. $record = $this->db->table('VoucherHistory')
  310. ->where('CardNumber','0411')
  311. ->whereRaw("(cSeed = '{$date1}' or cSeed = '{$date2}')")
  312. ->select('cNumber')
  313. ->first();
  314. $record = (array)$record;
  315. if(empty($record)) return [false,'未找到产成品入库当月流水号信息!'];
  316. $tmp = $record['cNumber'] + 1;
  317. if(strlen($tmp) > 4) return [false,'产成品入库当月流水号超过四位数!'];
  318. $number = str_pad($tmp,4,'0',STR_PAD_LEFT);
  319. $number = $tmp . $number;
  320. $ccode = "SCRK" . $date2 . $number;
  321. return [true,$ccode];
  322. }
  323. public function post_helper($url, $data,$header = [],$timeout = 60){
  324. $ch = curl_init();
  325. curl_setopt($ch, CURLOPT_URL, $url);
  326. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  327. curl_setopt($ch, CURLOPT_ENCODING, '');
  328. curl_setopt($ch, CURLOPT_POST, 1);
  329. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
  330. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  331. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  332. curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
  333. if(!is_null($data)) curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  334. $r = curl_exec($ch);
  335. curl_close($ch);
  336. return json_decode($r, true);
  337. }
  338. }