FyySqlServerService.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654
  1. <?php
  2. namespace App\Service;
  3. use App\Model\BoxDetail;
  4. use App\Model\Employee;
  5. use App\Model\ErrorTable;
  6. use App\Model\Orders;
  7. use App\Model\SaleOrdersProduct;
  8. use Illuminate\Support\Facades\Config;
  9. use Illuminate\Support\Facades\DB;
  10. use Illuminate\Support\Facades\Log;
  11. use Illuminate\Support\Facades\Redis;
  12. class FyySqlServerService extends Service
  13. {
  14. public $db = null;
  15. public $db2 = null;
  16. public $error = null;
  17. public $host = "192.168.0.157";//数据库外网域名
  18. public $host_api = '192.168.0.157';//用友接口外网域名
  19. public $port = 1433;
  20. public $database = "UFDATA_001_2024";
  21. public $url = "";
  22. public $sAccID = "(default)@001";
  23. public $sUserID = "0001";
  24. public $sPassword = "";
  25. public function __construct($user_id = [], $is_db2 = false)
  26. {
  27. try {
  28. //用户信息校验
  29. if (empty($user_id['id'])) {
  30. $this->error = '恒成塑业数据库连接用户参数不能为空!';
  31. return;
  32. }
  33. //获取用友账号密码
  34. $emp = Employee::where('id', $user_id['id'])->select('sqlserver_account', 'sqlserver_password')->first();
  35. if (empty($emp) || empty($emp->sqlserver_account)) {
  36. $this->error = '恒成塑业连接构造失败,未找到账号对应的用友账号信息';
  37. return;
  38. }
  39. $this->host = env('Yongyou_database_ip');
  40. $this->host_api = env('Yongyou_api_ip');
  41. $this->database = env('Yongyou_database');
  42. //映射ip是否通畅
  43. $bool = $this->isHostReachable($this->host);
  44. if(! $bool) {
  45. $this->error = $this->host . "连接不可达,请稍后重新操作!";
  46. return;
  47. }
  48. //用友接口统一登录账号密码
  49. $this->sUserID = $emp->sqlserver_account ?? '';
  50. $this->sPassword = $emp->sqlserver_password ?? '';
  51. $this->url = $this->host_api . "/U8Sys/U8API";
  52. if(!$this->db) {
  53. $config = [
  54. 'driver' => 'sqlsrv',
  55. 'host' => $this->host,
  56. 'port' => $this->port,
  57. 'database' => $this->database,
  58. 'username' => env('SQLSRV_USERNAME'),
  59. 'password' => env('SQLSRV_PASSWORD'),
  60. ];
  61. // 进行数据库连接
  62. Config::set('database.connections.sqlsrvs', $config);
  63. $pdo = DB::connection('sqlsrvs')->getPdo();
  64. if ($pdo instanceof \PDO) {
  65. // 连接成功的逻辑代码
  66. $this->db = DB::connection('sqlsrvs');
  67. } else {
  68. $this->error = '连接失败!';
  69. return;
  70. }
  71. }
  72. if($is_db2){
  73. $config = [
  74. 'driver' => 'sqlsrv',
  75. 'host' => $this->host,
  76. 'port' => $this->port,
  77. 'database' => 'UFSystem',
  78. 'username' => env('SQLSRV_USERNAME'),
  79. 'password' => env('SQLSRV_PASSWORD'),
  80. ];
  81. // 进行数据库连接
  82. Config::set('database.connections.sqlsrvs', $config);
  83. $pdo = DB::connection('sqlsrvs')->getPdo();
  84. if ($pdo instanceof \PDO) {
  85. // 连接成功的逻辑代码
  86. $this->db2 = DB::connection('sqlsrvs');
  87. } else {
  88. $this->error = '连接失败!';
  89. return;
  90. }
  91. }
  92. } catch (\Throwable $e) {
  93. $this->error = $e->getMessage();
  94. }
  95. }
  96. public function is_same_month($timestamp1, $timestamp2)
  97. {
  98. // 格式化时间戳为年份和月份
  99. $year1 = date('Y', $timestamp1);
  100. $month1 = date('m', $timestamp1);
  101. $year2 = date('Y', $timestamp2);
  102. $month2 = date('m', $timestamp2);
  103. if ($year1 === $year2 && $month1 === $month2) {
  104. return true;
  105. } else {
  106. return false;
  107. }
  108. }
  109. //获取数据(点击引入)
  110. public function getDataFromSqlServer($data)
  111. {
  112. if (!empty($this->error)) return [false, $this->error, ''];
  113. if (empty($data['out_order_no_time'][0]) || empty($data['out_order_no_time'][1])) return [false, '制单日期不能为空!', ''];
  114. $bool = $this->is_same_month($data['out_order_no_time'][0], $data['out_order_no_time'][1]);
  115. if (!$bool) return [false, '制单日期必须同月!', ''];
  116. //查询产品主表副表数据
  117. $start = date('Y-m-d H:i:s.000', $data['out_order_no_time'][0]);
  118. $end = date('Y-m-d H:i:s.000', $data['out_order_no_time'][1]);
  119. $model = $this->db->table('SO_SOMain as a')
  120. ->leftJoin('SO_SODetails as b', 'b.cSOCode', 'a.cSOCode')
  121. ->whereBetween('a.dDate', [$start, $end])
  122. ->whereNotNull('a.cVerifier')
  123. ->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', 'b.iTaxUnitPrice as price','b.dPreDate as pre_shipment_time');
  124. if (!empty($data['out_order_no'])) $model->where('a.cSOCode', 'LIKE', '%' . $data['out_order_no'] . '%');
  125. if (!empty($data['customer_no'])) $model->where('a.cCusCode', 'LIKE', '%' . $data['customer_no'] . '%');
  126. if (!empty($data['customer_name'])) $model->where('a.cCusName', 'LIKE', '%' . $data['customer_name'] . '%');
  127. if (!empty($data['table_header_mark'])) $model->where('a.cMemo', 'LIKE', '%' . $data['table_header_mark'] . '%');
  128. if (!empty($data['out_crt_man'])) $model->where('a.cMaker', 'LIKE', '%' . $data['out_crt_man'] . '%');
  129. if (!empty($data['out_checker_man'])) $model->where('a.cVerifier', 'LIKE', '%' . $data['out_checker_man'] . '%');
  130. if (!empty($data['out_checker_time'][0]) && !empty($data['out_checker_time'][1])) {
  131. $start1 = date('Y-m-d H:i:s.000', $data['out_checker_time'][0]);
  132. $end1 = date('Y-m-d H:i:s.000', $data['out_checker_time'][1]);
  133. $model->whereBetween('a.dverifydate', [$start1, $end1]);
  134. }
  135. if (!empty($data['pre_shipment_time'][0]) && !empty($data['pre_shipment_time'][1])) {
  136. $start1 = date('Y-m-d H:i:s.000', $data['pre_shipment_time'][0]);
  137. $end1 = date('Y-m-d H:i:s.000', $data['pre_shipment_time'][1]);
  138. $model->whereBetween('b.dPreDate', [$start1, $end1]);
  139. }
  140. $result = $model->get()->toArray();
  141. if (empty($result)) return [false, '暂无数据,更新结束!', ''];
  142. list($status, $msg) = $this->orderRule($result);
  143. if (empty($msg)) return [false, '暂无数据,更新结束!', ''];
  144. $result = $msg;
  145. //查询附带的一些信息(比较少)
  146. $product_no = array_filter(array_column($result, 'product_no'));
  147. $chunkSize = 1000; // 每个子集的大小
  148. $chunks = array_chunk($product_no, $chunkSize); // 将原始数组拆分成多个较小的子数组
  149. $results = []; // 存储查询结果的数组
  150. foreach ($chunks as $chunk) {
  151. $tmp = $this->db->table('Inventory as a')
  152. ->join('ComputationUnit as b', function ($join) {
  153. $join->on('a.cGroupCode', '=', 'b.cGroupCode')
  154. ->on('a.cComUnitCode', '=', 'b.cComUnitCode');
  155. }, null, null, 'left')
  156. ->whereIn('a.cInvCode', $chunk)
  157. ->select('a.cInvCode as product_no', 'a.cInvName as product_title', 'a.cInvStd as product_size', 'b.cComUnitName as product_unit')
  158. ->get()
  159. ->toArray();
  160. $results = array_merge($results, $tmp); // 将每个子集的结果合并到总结果数组中
  161. }
  162. $messageMap = array_column($results, null, 'product_no');
  163. unset($results);
  164. //现存量查询开始 ---组织查询条件
  165. $args = '';
  166. foreach ($result as $value) {
  167. $product = $value->product_no;
  168. $technology_name = $value->technology_name ?? '';
  169. // $wood_name = $value->wood_name ?? '';
  170. $args .= "(a.cInvCode = '{$product}' and a.cFree1 = '{$technology_name}') OR ";
  171. }
  172. $args = rtrim($args, 'OR ');
  173. $messageTwo = $this->db->table('CurrentStock as a')
  174. ->leftJoin('Warehouse as b', 'b.cWhCode', 'a.cWhCode')
  175. ->whereRaw("($args)")
  176. ->where('a.iQuantity', '>', 0)
  177. ->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')
  178. ->get()->toArray();
  179. if (!empty($messageTwo)) {
  180. foreach ($messageTwo as $key => $value) {
  181. $messageTwo[$key] = (array)$value;
  182. }
  183. }
  184. //现存量查询结束
  185. foreach ($result as $key => $value) {
  186. $result[$key]->technology_material = $value->technology_material ?? '';
  187. $result[$key]->technology_name = $value->technology_name ?? '';
  188. $result[$key]->wood_name = $value->wood_name ?? '';
  189. $result[$key]->process_mark = $value->process_mark ?? '';
  190. $result[$key]->table_body_mark = $value->table_body_mark ?? '';
  191. $result[$key]->table_header_mark = $value->table_header_mark ?? '';
  192. $keys = $value->product_no . $value->technology_name . $value->wood_name;
  193. $result[$key]->out_order_no_time = $value->out_order_no_time ? strtotime($value->out_order_no_time) : 0;
  194. $result[$key]->out_checker_time = $value->out_checker_time ? strtotime($value->out_checker_time) : 0;
  195. $result[$key]->pre_shipment_time = $value->pre_shipment_time ? strtotime($value->pre_shipment_time) : 0;
  196. $result[$key]->product_title = $messageMap[$value->product_no]->product_title ?? '';
  197. $result[$key]->product_size = $messageMap[$value->product_no]->product_size ?? '';
  198. $result[$key]->product_unit = $messageMap[$value->product_no]->product_unit ?? '';
  199. $result[$key] = (array)$value;
  200. }
  201. return [true, $result, $messageTwo];
  202. }
  203. public function orderRule($data)
  204. {
  205. $result = Orders::where('del_time', 0)
  206. ->whereIn('out_order_no', array_column($data, 'out_order_no'))
  207. ->select('out_order_no')
  208. ->get()->toArray();
  209. $out_order_no = array_column($result, 'out_order_no');
  210. if (!empty($out_order_no)) {
  211. foreach ($data as $key => $value) {
  212. if (in_array($value->out_order_no, $out_order_no)) {
  213. unset($data[$key]);
  214. }
  215. }
  216. }
  217. return [true, $data];
  218. }
  219. //获取数据(刷新现存量)
  220. public function getDataFromSqlServerForOnHand($data)
  221. {
  222. if (!empty($this->error)) return [false, $this->error, ''];
  223. if (empty($data['id'])) return [false, '数据不能为空!', ''];
  224. $product = SaleOrdersProduct::whereIn('id', $data['id'])
  225. ->select('product_no', 'technology_name')
  226. ->get()->toArray();
  227. //现存量查询开始 ---组织查询条件
  228. $args = '';
  229. foreach ($product as $value) {
  230. $args .= "(a.cInvCode = '{$value['product_no']}' and a.cFree1 = '{$value['technology_name']}') OR ";
  231. }
  232. $args = rtrim($args, 'OR ');
  233. $message = $this->db->table('CurrentStock as a')
  234. ->leftJoin('Warehouse as b', 'b.cWhCode', 'a.cWhCode')
  235. ->whereRaw("($args)")
  236. ->where('a.iQuantity', '>', 0)
  237. ->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')
  238. ->get()->toArray();
  239. if (!empty($message)) {
  240. foreach ($message as $key => $value) {
  241. $message[$key] = (array)$value;
  242. }
  243. }
  244. //现存量查询结束
  245. return [true, $message, $product];
  246. }
  247. //获取用友账号的人名
  248. public function getYongyouName(){
  249. if (!empty($this->error)) return [false, $this->error, ''];
  250. $str = "";
  251. $model = $this->db->table('UA_User')
  252. ->where('cUser_Id',$this->sUserID)
  253. ->select('cUser_Name')
  254. ->first();
  255. if(! empty($model)) $str = $model->cUser_Name;
  256. return $str;
  257. }
  258. //产成品入库单保存接口以及审核
  259. public function U8Rdrecord10Save($data, $data_detail, $bredvouch = 0)
  260. {
  261. if (! empty($this->error)) return [false, $this->error];
  262. if ($bredvouch) {
  263. $cmemo = '来源:恒成塑业完工操作撤回';
  264. } else {
  265. $cmemo = '来源:恒成塑业包装操作 包装单号:' . $data['order_no'];
  266. }
  267. //数据
  268. $bodys = [];
  269. foreach ($data_detail as $value){
  270. $key = $value['ext_1'] . $value['ext_3'];
  271. if(! isset($bodys[$key])){
  272. $bodys[$key] = [
  273. "cinvcode" => $value["ext_1"],
  274. "cposition" => "",
  275. "cbatch" => "",
  276. "iquantity" => $value["num"],
  277. "inum" => $value["num"],
  278. "iunitcost" => 0,
  279. "iprice" => 0,
  280. "iinvexchrate" => 0,
  281. "impoids" => "",
  282. "cmocode" => "",
  283. "imoseq" => "",
  284. "cbmemo" => "",
  285. "cfree1" => $value['ext_3'], //颜色
  286. "cfree2" => "",
  287. "cdefine28" => "",
  288. ];
  289. }else{
  290. $bodys[$key]['iquantity'] += $value['num'];
  291. $bodys[$key]['inum'] += $value['num'];
  292. }
  293. }
  294. $bodys = array_values($bodys);
  295. $post = [
  296. "password" => "cloud@123456",
  297. "entity" => "U8Rdrecord10Save",
  298. "login" => [
  299. "sAccID" => $this->sAccID,
  300. "sDate" => date("Y-m-d"),
  301. "sServer" => '127.0.0.1',
  302. "sUserID" => $this->sUserID,
  303. "sSerial" => "",
  304. "sPassword" => $this->sPassword
  305. ],
  306. "data" => [
  307. "ccode" => '',
  308. "ddate" => date("Y-m-d"),
  309. "cmaker" => $data['create_name'],
  310. "dnmaketime" => date("Y-m-d"),
  311. "IsExamine" => true,
  312. "chandler" => $data['create_name'],
  313. "dnverifytime" => date("Y-m-d"),
  314. "bredvouch" => $bredvouch,
  315. "cwhcode" => "002",
  316. "cdepcode" => "03",
  317. "crdcode" => "102", //生产入库
  318. "cmemo" => $cmemo,
  319. "cdefine10" => $data['ext_1'] ?? "", //客户名称
  320. "bodys" => $bodys
  321. ]
  322. ];
  323. Log::channel('apiLog')->info('产成品入库:源数据', ["param" => $post]);
  324. $return = $this->post_helper($this->url, json_encode($post), ['Content-Type:application/json'],70);
  325. Log::channel('apiLog')->info('产成品入库:返回结果', ["param" => $return]);
  326. if (empty($return)) return [false, '异常错误,请确认请求接口地址!'];
  327. return [$return['flag'], $return['msg']];
  328. }
  329. //销售出库单保存接口给以及审核
  330. public function U8Rdrecord32Save($data,$create_name, $bredvouch = 0)
  331. {
  332. if (!empty($this->error)) return [false, $this->error];
  333. if ($bredvouch) {
  334. $cmemo = '来源:恒成塑业发货出库操作(撤回)';
  335. } else {
  336. $cmemo = '来源:恒成塑业发货出库操作';
  337. }
  338. foreach ($data as $value) {
  339. $bodys_tmp = [];
  340. foreach ($value['product'] as $v){
  341. $bodys_tmp[] = [
  342. "idlsid" => "",
  343. "cdlcode" => $value['cdlcode_string'],
  344. "dlrowno" => $v['line'],
  345. "cbdlcode" => "",
  346. "cinvcode" => $v['cinvcode'],
  347. "cposition" => "",
  348. "cbatch" => "",
  349. "iquantity" => $v['iquantity'],
  350. "inum" => 0,
  351. "iinvexchrate" => 0,
  352. "iunitcost" => 0,
  353. "iprice" => 0,
  354. "cbmemo" => "",
  355. "cfree1" => $v['cfree1'],
  356. "cfree2" => "",
  357. ];
  358. }
  359. $cmemo = $cmemo . '(发货单信息:' . $value['cdlcode_string'] . ')';
  360. $post_tmp = [
  361. "password" => "cloud@123456",
  362. "entity" => "U8Rdrecord32Save",
  363. "login" => [
  364. "sAccID" => $this->sAccID,
  365. "sDate" => date("Y-m-d"),
  366. "sServer" => '127.0.0.1',
  367. "sUserID" => $this->sUserID,
  368. "sSerial" => "",
  369. "sPassword" => $this->sPassword
  370. ],
  371. "data" => [
  372. "ccode" => '',
  373. "ddate" => date("Y-m-d"),
  374. "cmaker" => $create_name,
  375. "dnmaketime" => date("Y-m-d"),
  376. "IsExamine" => true,
  377. "chandler" => $create_name,
  378. "dnverifytime" => date("Y-m-d"),
  379. "bredvouch" => $bredvouch,
  380. "cdepcode" => "03",
  381. "ccuscode" => $value['customer_code'],
  382. "crdcode" => '202',
  383. "cmemo" => $cmemo,
  384. "cwhcode" => "002",
  385. "bodys" => $bodys_tmp,
  386. ]
  387. ];
  388. }
  389. Log::channel('apiLog')->info('销售出库单:源数据', ["param" => $post_tmp]);
  390. $return = $this->post_helper($this->url, json_encode($post_tmp), ['Content-Type:application/json'], 70);
  391. Log::channel('apiLog')->info('销售出库单:返回结果', ["param" => $return]);
  392. if (empty($return)) return [false, '异常错误,请确认请求接口地址!'];
  393. if (! $return['flag']) return [false, $return['msg']];
  394. return [true, ''];
  395. }
  396. public function getBoxData($data)
  397. {
  398. $boxData = BoxDetail::from('box_detail as a')
  399. ->leftJoin('sale_orders_product as b', 'b.id', 'a.top_id')
  400. ->where('a.del_time', 0)
  401. ->where('a.order_no', $data['order_number']) //包装单号
  402. ->select('a.num as iquantity', 'b.product_no as cinvcode', 'b.technology_name as cfree1', 'b.wood_name as cfree2', 'b.out_order_no as cSOCode');
  403. return $boxData;
  404. }
  405. //获取发货单数据 还没发的
  406. public function getDataFromDispatchList($data)
  407. {
  408. $model = $this->db->table('DispatchList as a')
  409. ->leftJoin('DispatchLists as b', 'b.DLID', 'a.DLID')
  410. ->leftJoin('Inventory as c', 'c.cInvCode', 'b.cInvCode')
  411. ->whereNotNull('a.cVerifier');
  412. // ->whereColumn('b.iQuantity', '>', 'b.fOutQuantity');
  413. //检索条件
  414. if (!empty($data['time'][0]) && !empty($data['time'][1])) {
  415. $model->where('a.dDate', '>=', $data['time'][0]);
  416. $model->where('a.dDate', '<=', $data['time'][1]);
  417. }
  418. if (!empty($data['order_no'])) $model->where('b.cSOcode', $data['order_no']);
  419. if (!empty($data['out_order_no'])) $model->where('b.cSOcode', $data['out_order_no']);
  420. $message = $model->select('a.cDLCode as cdlcode', 'a.DLID as id', 'a.cCusName as customer_name', 'b.cSOCode as csocode', 'a.cDepCode as cdepcode', 'a.cCusCode as cuscode', 'b.iDLsID as idlsid', 'b.cWhCode as cwhcode', 'b.cInvCode as cinvcode', 'b.cInvName as product_title', 'b.cFree1 as cfree1', 'b.cFree2 as cfree2', 'b.cPosition as cposition', 'b.cBatch as cbatch', 'b.iQuantity as iquantity', 'b.iNum as inum', 'b.iInvExchRate as iinvexchrate', 'b.fOutQuantity as out_quantity', 'b.iUnitPrice as iunitcost', 'b.iMoney as imoney', 'b.cDefine28 as technology_material', 'b.cDefine30 as process_mark', 'c.cInvStd as product_size')
  421. ->get()->toArray();
  422. if (!empty($message)) {
  423. foreach ($message as $key => $value) {
  424. // $message[$key]->iquantity = $value->iquantity - $value->out_quantity;
  425. $message[$key] = (array)$value;
  426. }
  427. }
  428. return $message;
  429. }
  430. public function post_helper($url, $data, $header = [], $timeout = 60)
  431. {
  432. $ch = curl_init();
  433. curl_setopt($ch, CURLOPT_URL, $url);
  434. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  435. curl_setopt($ch, CURLOPT_ENCODING, '');
  436. curl_setopt($ch, CURLOPT_POST, 1);
  437. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
  438. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  439. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  440. curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
  441. if (!is_null($data)) curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  442. $r = curl_exec($ch);
  443. curl_close($ch);
  444. return json_decode($r, true);
  445. }
  446. //获取发货单数据 还没发的 分页
  447. public function getDataFromDispatchListPage($data)
  448. {
  449. //检索条件
  450. if (empty($data['time'][0]) || empty($data['time'][1])) return [false, '时间区间不能为空!'];
  451. if(empty($data['page_size'])) $data['page_size'] = 20;
  452. if(empty($data['page_index'])) $data['page_index'] = 1;
  453. $model = $this->db->table('DispatchList as a')
  454. ->leftJoin('DispatchLists as b', 'b.DLID', 'a.DLID')
  455. ->leftJoin('Inventory as c', 'c.cInvCode', 'b.cInvCode')
  456. ->select('a.cDLCode as cdlcode', 'a.DLID as id', 'a.cCusName as customer_name', 'b.cSOCode as csocode', 'a.cDepCode as cdepcode', 'a.cCusCode as cuscode', 'a.dDate as date','b.irowno', 'b.iDLsID as idlsid', 'b.cWhCode as cwhcode', 'b.cInvCode as cinvcode', 'b.cInvName as product_title', 'b.cFree1 as cfree1', 'b.cFree2 as cfree2', 'b.cPosition as cposition', 'b.cBatch as cbatch', 'b.iQuantity as iquantity', 'b.iNum as inum', 'b.iInvExchRate as iinvexchrate', 'b.fOutQuantity as out_quantity', 'b.iUnitPrice as iunitcost', 'b.iMoney as imoney', 'b.cDefine28 as technology_material', 'b.cDefine30 as process_mark', 'c.cInvStd as product_size', DB::raw('(b.iQuantity - b.fOutQuantity) as quantity'), 'a.cMemo as table_header_mark', 'b.cMemo as table_body_mark')
  457. ->whereNotNull('a.cVerifier')
  458. // ->whereColumn('b.iQuantity', '>', 'b.fOutQuantity')
  459. ->where('a.dDate', '>=', $data['time'][0])
  460. ->where('a.dDate', '<=', $data['time'][1]);
  461. if (!empty($data['cdlcode'])) $model->where('a.cDLCode', 'Like', '%' . $data['cdlcode'] . '%');
  462. if (!empty($data['cinvcode'])) $model->where('b.cInvCode', 'Like', '%' . $data['cinvcode'] . '%');
  463. if (!empty($data['product_title'])) $model->where('b.cInvName', 'Like', '%' . $data['product_title'] . '%');
  464. if (!empty($data['product_size'])) $model->where('c.cInvStd', 'Like', '%' . $data['product_size'] . '%');
  465. if (!empty($data['technology_material'])) $model->where('b.cDefine28', 'Like', '%' . $data['technology_material'] . '%');
  466. if (!empty($data['cfree1'])) $model->where('b.cFree1', 'Like', '%' . $data['cfree1'] . '%');
  467. if (!empty($data['cfree2'])) $model->where('b.cFree2', 'Like', '%' . $data['cfree2'] . '%');
  468. if (!empty($data['process_mark'])) $model->where('b.cDefine30', 'Like', '%' . $data['process_mark'] . '%');
  469. $list = $this->limit($model, '', $data);
  470. if (! empty($list['data'])) {
  471. $product_no = array_unique(array_column($list['data'], 'cinvcode'));
  472. $messageMap = $this->db->table('Inventory as a')
  473. ->join('ComputationUnit as b', function ($join) {
  474. $join->on('a.cGroupCode', '=', 'b.cGroupCode')
  475. ->on('a.cComUnitCode', '=', 'b.cComUnitCode');
  476. }, null, null, 'left')
  477. ->whereIn('a.cInvCode', $product_no)
  478. ->pluck('b.cComUnitName as product_unit', 'a.cInvCode')
  479. ->toArray();
  480. $list['data'] = Collect($list['data'])->map(function ($object) {
  481. return (array)$object;
  482. })->toArray();
  483. $new_data = [];
  484. foreach ($list['data'] as $value) {
  485. $unit = $messageMap[$value['cinvcode']] ?? '';
  486. $iquantity = floatval($value['iquantity']);
  487. $out_quantity = floatval($value['out_quantity']);
  488. $quantity = floatval($value['quantity']);
  489. $inum = floatval($value['inum']);
  490. $pro = [
  491. "cinvcode" => $value['cinvcode'],
  492. "product_title" => $value['product_title'],
  493. "cfree1" => $value['cfree1'],
  494. "iquantity" => $iquantity,
  495. "inum" => $inum,
  496. "out_quantity" => $out_quantity,
  497. "product_size" => $value['product_size'] ?? "",
  498. "quantity" => $quantity,
  499. "unit" => $unit,
  500. "line" => $value['irowno'],
  501. ];
  502. if(isset($new_data[$value['cdlcode']])){
  503. $new_data[$value['cdlcode']]['product'][] = $pro;
  504. }else{
  505. $new_data[$value['cdlcode']] = [
  506. "cdlcode" => $value['cdlcode'],
  507. "csocode" => $value['csocode'],
  508. "customer_code" => $value['cuscode'] ?? "",
  509. "customer_name" => $value['customer_name'] ?? "",
  510. "product" => [$pro]
  511. ];
  512. }
  513. }
  514. $list['data'] = array_values($new_data);
  515. }
  516. return $list;
  517. }
  518. //获取发货单数据 做校验
  519. public function getDataFromDispatchListForCheck($data)
  520. {
  521. $model = $this->db->table('DispatchList as a')
  522. ->leftJoin('DispatchLists as b', 'b.DLID', 'a.DLID')
  523. ->select('a.cDLCode as cdlcode', 'a.cCusName as customer_name', 'b.cSOCode as csocode', 'a.cCusCode as cuscode','b.cInvCode as cinvcode', 'b.cInvName as product_title', 'b.cFree1 as cfree1','b.iQuantity as iquantity', 'b.iNum as inum', 'b.iInvExchRate as iinvexchrate', 'b.fOutQuantity as out_quantity', 'b.iUnitPrice as iunitcost', 'b.iMoney as imoney');
  524. if (!empty($data['cdlcode'])) $model->whereIn('a.cDLCode', $data['cdlcode']);
  525. //颜色
  526. if (!empty($data['cfree1'])) $model->where('b.cFree1', 'Like', '%' . $data['cfree1'] . '%');
  527. $list = $model->get()->toArray();
  528. if (! empty($list)) {
  529. $list = Collect($list)->map(function ($object) {
  530. return (array)$object;
  531. })->toArray();
  532. $new_data = [];
  533. foreach ($list as $value) {
  534. $iquantity = floatval($value['iquantity']);
  535. $out_quantity = floatval($value['out_quantity']);
  536. $inum = floatval($value['inum']);
  537. $pro = [
  538. "cinvcode" => $value['cinvcode'],
  539. "product_title" => $value['product_title'],
  540. "cfree1" => $value['cfree1'],
  541. "iquantity" => $iquantity,
  542. "inum" => $inum,
  543. "out_quantity" => $out_quantity,
  544. ];
  545. if(isset($new_data[$value['cdlcode']])){
  546. $new_data[$value['cdlcode']]['product'][] = $pro;
  547. }else{
  548. $new_data[$value['cdlcode']] = [
  549. "cdlcode" => $value['cdlcode'],
  550. "csocode" => $value['csocode'],
  551. "customer_code" => $value['cuscode'] ?? "",
  552. "customer_name" => $value['customer_name'] ?? "",
  553. "product" => [$pro]
  554. ];
  555. }
  556. }
  557. $list = $new_data;
  558. }
  559. return $list;
  560. }
  561. public function recordErrorTable($msg,$user,$data,$time,$type){
  562. // 连接到指定数据库连接
  563. ErrorTable::insert([
  564. 'msg' => $msg,
  565. 'data' => json_encode($data),
  566. 'user_id' => $user['id'],
  567. 'user_operation_time' => $time,
  568. 'type' => $type,
  569. 'order_no' => $data['order_no'] ?? ""
  570. ]);
  571. }
  572. }