FyySqlServerService.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. <?php
  2. namespace App\Service;
  3. use App\Model\BoxDetail;
  4. use App\Model\Employee;
  5. use App\Model\Orders;
  6. use App\Model\SaleOrdersProduct;
  7. use Illuminate\Support\Facades\Config;
  8. use Illuminate\Support\Facades\DB;
  9. use Illuminate\Support\Facades\Log;
  10. use Illuminate\Support\Facades\Redis;
  11. class FyySqlServerService extends Service
  12. {
  13. public $db = null;
  14. public $error = null;
  15. public $host = "192.168.0.157";//数据库外网域名
  16. public $host_api = '192.168.0.157';//用友接口外网域名
  17. public $port = 1433;
  18. public $database = "UFDATA_001_2024";
  19. public $url = "";
  20. public $sAccID = "(default)@001";
  21. public $sUserID = "0001";
  22. public $sPassword = "";
  23. public function __construct($user_id = [])
  24. {
  25. try {
  26. //用户信息校验
  27. if (empty($user_id['id'])) {
  28. $this->error = '福羊羊数据库连接用户参数不能为空!';
  29. return;
  30. }
  31. //获取用友账号密码
  32. $emp = Employee::where('id', $user_id['id'])->select('sqlserver_account', 'sqlserver_password')->first();
  33. if (empty($emp) || empty($emp->sqlserver_account)) {
  34. $this->error = '福羊羊连接构造失败,未找到账号对应的用友账号信息';
  35. return;
  36. }
  37. //用友接口统一登录账号密码
  38. $this->sUserID = $emp->sqlserver_account ?? '';
  39. $this->sPassword = $emp->sqlserver_password ?? '';
  40. $this->url = $this->host_api . "/U8Sys/U8API";
  41. if (!$this->db) {
  42. $config = [
  43. 'driver' => 'sqlsrv',
  44. 'host' => $this->host,
  45. 'port' => $this->port,
  46. 'database' => $this->database,
  47. 'username' => env('SQLSRV_USERNAME'),
  48. 'password' => env('SQLSRV_PASSWORD'),
  49. ];
  50. // 进行数据库连接
  51. Config::set('database.connections.sqlsrvs', $config);
  52. $pdo = DB::connection('sqlsrvs')->getPdo();
  53. if ($pdo instanceof \PDO) {
  54. // 连接成功的逻辑代码
  55. $this->db = DB::connection('sqlsrvs');
  56. } else {
  57. $this->error = '连接失败!';
  58. return;
  59. }
  60. }
  61. } catch (\Throwable $e) {
  62. $this->error = $e->getMessage();
  63. }
  64. }
  65. public function is_same_month($timestamp1, $timestamp2)
  66. {
  67. // 格式化时间戳为年份和月份
  68. $year1 = date('Y', $timestamp1);
  69. $month1 = date('m', $timestamp1);
  70. $year2 = date('Y', $timestamp2);
  71. $month2 = date('m', $timestamp2);
  72. if ($year1 === $year2 && $month1 === $month2) {
  73. return true;
  74. } else {
  75. return false;
  76. }
  77. }
  78. //获取数据(点击引入)
  79. public function getDataFromSqlServer($data)
  80. {
  81. if (!empty($this->error)) return [false, $this->error, ''];
  82. if (empty($data['out_order_no_time'][0]) || empty($data['out_order_no_time'][1])) return [false, '制单日期不能为空!', ''];
  83. $bool = $this->is_same_month($data['out_order_no_time'][0], $data['out_order_no_time'][1]);
  84. if (!$bool) return [false, '制单日期必须同月!', ''];
  85. //查询产品主表副表数据
  86. $start = date('Y-m-d H:i:s.000', $data['out_order_no_time'][0]);
  87. $end = date('Y-m-d H:i:s.000', $data['out_order_no_time'][1]);
  88. $model = $this->db->table('SO_SOMain as a')
  89. ->leftJoin('SO_SODetails as b', 'b.cSOCode', 'a.cSOCode')
  90. ->whereBetween('a.dDate', [$start, $end])
  91. ->whereNotNull('a.cVerifier')
  92. ->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');
  93. if (!empty($data['out_order_no'])) $model->where('a.cSOCode', 'LIKE', '%' . $data['out_order_no'] . '%');
  94. if (!empty($data['customer_no'])) $model->where('a.cCusCode', 'LIKE', '%' . $data['customer_no'] . '%');
  95. if (!empty($data['customer_name'])) $model->where('a.cCusName', 'LIKE', '%' . $data['customer_name'] . '%');
  96. if (!empty($data['table_header_mark'])) $model->where('a.cMemo', 'LIKE', '%' . $data['table_header_mark'] . '%');
  97. if (!empty($data['out_crt_man'])) $model->where('a.cMaker', 'LIKE', '%' . $data['out_crt_man'] . '%');
  98. if (!empty($data['out_checker_man'])) $model->where('a.cVerifier', 'LIKE', '%' . $data['out_checker_man'] . '%');
  99. if (!empty($data['out_checker_time'][0]) && !empty($data['out_checker_time'][1])) {
  100. $start1 = date('Y-m-d H:i:s.000', $data['out_checker_time'][0]);
  101. $end1 = date('Y-m-d H:i:s.000', $data['out_checker_time'][1]);
  102. $model->whereBetween('a.dverifydate', [$start1, $end1]);
  103. }
  104. if (!empty($data['pre_shipment_time'][0]) && !empty($data['pre_shipment_time'][1])) {
  105. $start1 = date('Y-m-d H:i:s.000', $data['pre_shipment_time'][0]);
  106. $end1 = date('Y-m-d H:i:s.000', $data['pre_shipment_time'][1]);
  107. $model->whereBetween('b.dPreDate', [$start1, $end1]);
  108. }
  109. $result = $model->get()->toArray();
  110. if (empty($result)) return [false, '暂无数据,更新结束!', ''];
  111. list($status, $msg) = $this->orderRule($result);
  112. if (empty($msg)) return [false, '暂无数据,更新结束!', ''];
  113. $result = $msg;
  114. //查询附带的一些信息(比较少)
  115. $product_no = array_filter(array_column($result, 'product_no'));
  116. $chunkSize = 1000; // 每个子集的大小
  117. $chunks = array_chunk($product_no, $chunkSize); // 将原始数组拆分成多个较小的子数组
  118. $results = []; // 存储查询结果的数组
  119. foreach ($chunks as $chunk) {
  120. $tmp = $this->db->table('Inventory as a')
  121. ->join('ComputationUnit as b', function ($join) {
  122. $join->on('a.cGroupCode', '=', 'b.cGroupCode')
  123. ->on('a.cComUnitCode', '=', 'b.cComUnitCode');
  124. }, null, null, 'left')
  125. ->whereIn('a.cInvCode', $chunk)
  126. ->select('a.cInvCode as product_no', 'a.cInvName as product_title', 'a.cInvStd as product_size', 'b.cComUnitName as product_unit')
  127. ->get()
  128. ->toArray();
  129. $results = array_merge($results, $tmp); // 将每个子集的结果合并到总结果数组中
  130. }
  131. $messageMap = array_column($results, null, 'product_no');
  132. unset($results);
  133. //现存量查询开始 ---组织查询条件
  134. $args = '';
  135. foreach ($result as $value) {
  136. $product = $value->product_no;
  137. $technology_name = $value->technology_name ?? '';
  138. // $wood_name = $value->wood_name ?? '';
  139. $args .= "(a.cInvCode = '{$product}' and a.cFree1 = '{$technology_name}') OR ";
  140. }
  141. $args = rtrim($args, 'OR ');
  142. $messageTwo = $this->db->table('CurrentStock as a')
  143. ->leftJoin('Warehouse as b', 'b.cWhCode', 'a.cWhCode')
  144. ->whereRaw("($args)")
  145. ->where('a.iQuantity', '>', 0)
  146. ->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')
  147. ->get()->toArray();
  148. if (!empty($messageTwo)) {
  149. foreach ($messageTwo as $key => $value) {
  150. $messageTwo[$key] = (array)$value;
  151. }
  152. }
  153. //现存量查询结束
  154. foreach ($result as $key => $value) {
  155. $result[$key]->technology_material = $value->technology_material ?? '';
  156. $result[$key]->technology_name = $value->technology_name ?? '';
  157. $result[$key]->wood_name = $value->wood_name ?? '';
  158. $result[$key]->process_mark = $value->process_mark ?? '';
  159. $result[$key]->table_body_mark = $value->table_body_mark ?? '';
  160. $result[$key]->table_header_mark = $value->table_header_mark ?? '';
  161. $keys = $value->product_no . $value->technology_name . $value->wood_name;
  162. $result[$key]->out_order_no_time = $value->out_order_no_time ? strtotime($value->out_order_no_time) : 0;
  163. $result[$key]->out_checker_time = $value->out_checker_time ? strtotime($value->out_checker_time) : 0;
  164. $result[$key]->pre_shipment_time = $value->pre_shipment_time ? strtotime($value->pre_shipment_time) : 0;
  165. $result[$key]->product_title = $messageMap[$value->product_no]->product_title ?? '';
  166. $result[$key]->product_size = $messageMap[$value->product_no]->product_size ?? '';
  167. $result[$key]->product_unit = $messageMap[$value->product_no]->product_unit ?? '';
  168. $result[$key] = (array)$value;
  169. }
  170. return [true, $result, $messageTwo];
  171. }
  172. public function orderRule($data)
  173. {
  174. $result = Orders::where('del_time', 0)
  175. ->whereIn('out_order_no', array_column($data, 'out_order_no'))
  176. ->select('out_order_no')
  177. ->get()->toArray();
  178. $out_order_no = array_column($result, 'out_order_no');
  179. if (!empty($out_order_no)) {
  180. foreach ($data as $key => $value) {
  181. if (in_array($value->out_order_no, $out_order_no)) {
  182. unset($data[$key]);
  183. }
  184. }
  185. }
  186. return [true, $data];
  187. }
  188. //获取数据(刷新现存量)
  189. public function getDataFromSqlServerForOnHand($data)
  190. {
  191. if (!empty($this->error)) return [false, $this->error, ''];
  192. if (empty($data['id'])) return [false, '数据不能为空!', ''];
  193. $product = SaleOrdersProduct::whereIn('id', $data['id'])
  194. ->select('product_no', 'technology_name')
  195. ->get()->toArray();
  196. //现存量查询开始 ---组织查询条件
  197. $args = '';
  198. foreach ($product as $value) {
  199. $args .= "(a.cInvCode = '{$value['product_no']}' and a.cFree1 = '{$value['technology_name']}') OR ";
  200. }
  201. $args = rtrim($args, 'OR ');
  202. $message = $this->db->table('CurrentStock as a')
  203. ->leftJoin('Warehouse as b', 'b.cWhCode', 'a.cWhCode')
  204. ->whereRaw("($args)")
  205. ->where('a.iQuantity', '>', 0)
  206. ->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')
  207. ->get()->toArray();
  208. if (!empty($message)) {
  209. foreach ($message as $key => $value) {
  210. $message[$key] = (array)$value;
  211. }
  212. }
  213. //现存量查询结束
  214. return [true, $message, $product];
  215. }
  216. //产成品入库单保存接口以及审核
  217. public function U8Rdrecord10Save($data, $bredvouch = 0)
  218. {
  219. if (!empty($this->error)) return [false, $this->error];
  220. if ($bredvouch) {
  221. $cmemo = '来源:福羊羊完工操作撤回';
  222. } else {
  223. $cmemo = '来源:福羊羊完工操作 派工单号:' . $data['dispatch_no'];
  224. }
  225. //数据
  226. $bodys[] = [
  227. "cinvcode" => $data["product_no"],
  228. "cposition" => "",
  229. "cbatch" => "",
  230. "iquantity" => $data["quantity"],
  231. "inum" => $data["quantity"],
  232. "iunitcost" => $data["price"] * 0.95,
  233. "iprice" => $data["price"] * 0.95 * $data['quantity'],
  234. "iinvexchrate" => "1.00",
  235. "impoids" => "",
  236. "cmocode" => "",
  237. "imoseq" => "",
  238. "cbmemo" => "",
  239. "cfree1" => $data['technology_name'],
  240. "cfree2" => $data['wood_name'],
  241. "cdefine28" => $data['technology_material'],
  242. ];
  243. $post = [
  244. "password" => "cloud@123456",
  245. "entity" => "U8Rdrecord10Save",
  246. "login" => [
  247. "sAccID" => $this->sAccID,
  248. "sDate" => date("Y-m-d"),
  249. "sServer" => '127.0.0.1',
  250. "sUserID" => $this->sUserID,
  251. "sSerial" => "",
  252. "sPassword" => $this->sPassword
  253. ],
  254. "data" => [
  255. "ccode" => '',
  256. "ddate" => date("Y-m-d"),
  257. "cmaker" => $this->sUserID,
  258. "dnmaketime" => date("Y-m-d"),
  259. "IsExamine" => true,
  260. "bredvouch" => $bredvouch,
  261. "cwhcode" => "02",
  262. "cdepcode" => "06",
  263. "crdcode" => "102", //生产入库
  264. "cmemo" => $cmemo,
  265. "cdefine10" => $data['customer_name'], //客户名称
  266. "bodys" => $bodys
  267. ]
  268. ];
  269. file_put_contents('record_ss.txt', json_encode($post) . PHP_EOL, 8);
  270. $return = $this->post_helper($this->url, json_encode($post), ['Content-Type:application/json']);
  271. file_put_contents('record_ss.txt', json_encode($return) . PHP_EOL, 8);
  272. if (empty($return)) return [false, '异常错误,请确认请求接口地址!'];
  273. return [$return['flag'], $return['msg']];
  274. }
  275. //销售出库单保存接口给以及审核
  276. public function U8Rdrecord32Save($data, $bredvouch = 0)
  277. {
  278. if (!empty($this->error)) return [false, $this->error];
  279. if ($bredvouch) {
  280. $cmemo = '来源:福羊羊';
  281. } else {
  282. $cmemo = '来源:福羊羊发货出库操作';
  283. }
  284. $new = [];
  285. foreach ($data as $value) {
  286. $keys = $value['id'] . $value['cwhcode'];
  287. $new[$keys][] = $value;
  288. }
  289. foreach ($new as $value) {
  290. $main_tmp = $value[0];
  291. $bodys_tmp = [];
  292. foreach ($value as $val) {
  293. $bodys_tmp[] = [
  294. "idlsid" => $val['idlsid'],
  295. "cdlcode" => "",
  296. "dlrowno" => "",
  297. "cbdlcode" => "",
  298. "cinvcode" => $val['cinvcode'],
  299. "cposition" => $val['cposition'] ?? "",
  300. "cbatch" => $val['cbatch'] ?? "",
  301. "iquantity" => $val['iquantity'],
  302. "inum" => $val['inum'],
  303. "iinvexchrate" => $val['iinvexchrate'] ?? 0,
  304. "iunitcost" => $val['iunitcost'] * 0.95,
  305. "iprice" => $val['iunitcost'] * 0.95 * $val['iquantity'],
  306. "cbmemo" => "",
  307. "cfree1" => $val['cfree1'],
  308. "cfree2" => $val['cfree2'],
  309. "cdefine28" => $val['technology_material'],
  310. "cdefine30" => $val['process_mark'],
  311. ];
  312. }
  313. $post_tmp = [
  314. "password" => "cloud@123456",
  315. "entity" => "U8Rdrecord32Save",
  316. "login" => [
  317. "sAccID" => $this->sAccID,
  318. "sDate" => date("Y-m-d"),
  319. "sServer" => '127.0.0.1',
  320. "sUserID" => $this->sUserID,
  321. "sSerial" => "",
  322. "sPassword" => $this->sPassword
  323. ],
  324. "data" => [
  325. "ccode" => '',
  326. "ddate" => date("Y-m-d"),
  327. "cmaker" => $this->sUserID,
  328. "dnmaketime" => date("Y-m-d"),
  329. "IsExamine" => true,
  330. "bredvouch" => $bredvouch,
  331. "cwhcode" => $main_tmp['cwhcode'],
  332. "cdepcode" => $main_tmp['cdepcode'],
  333. "ccuscode" => $main_tmp['cuscode'],
  334. "crdcode" => '202',
  335. "cmemo" => $cmemo,
  336. "cdefine10" => $main_tmp['customer_name'], //客户名称
  337. "bodys" => $bodys_tmp,
  338. ]
  339. ];
  340. $return = $this->post_helper($this->url, json_encode($post_tmp), ['Content-Type:application/json']);
  341. file_put_contents('record.txt', json_encode($new) . PHP_EOL . json_encode($post_tmp) . PHP_EOL, 8);
  342. if (empty($return)) return [false, '异常错误,请确认请求接口地址!'];
  343. if (!$return['flag']) return [false, $return['msg']];
  344. }
  345. return [true, ''];
  346. }
  347. public function getBoxData($data)
  348. {
  349. $boxData = BoxDetail::from('box_detail as a')
  350. ->leftJoin('sale_orders_product as b', 'b.id', 'a.top_id')
  351. ->where('a.del_time', 0)
  352. ->where('a.order_no', $data['order_number']) //包装单号
  353. ->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');
  354. return $boxData;
  355. }
  356. //获取发货单数据 还没发的
  357. public function getDataFromDispatchList($data)
  358. {
  359. $model = $this->db->table('DispatchList as a')
  360. ->leftJoin('DispatchLists as b', 'b.DLID', 'a.DLID')
  361. ->leftJoin('Inventory as c', 'c.cInvCode', 'b.cInvCode')
  362. ->whereNotNull('a.cVerifier');
  363. // ->whereColumn('b.iQuantity', '>', 'b.fOutQuantity');
  364. //检索条件
  365. if (!empty($data['time'][0]) && !empty($data['time'][1])) {
  366. $model->where('a.dDate', '>=', $data['time'][0]);
  367. $model->where('a.dDate', '<=', $data['time'][1]);
  368. }
  369. if (!empty($data['order_no'])) $model->where('b.cSOcode', $data['order_no']);
  370. if (!empty($data['out_order_no'])) $model->where('b.cSOcode', $data['out_order_no']);
  371. $message = $model->select('a.cDLCode as cdlcode', 'a.DLID as id', 'a.cDefine10 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')
  372. ->get()->toArray();
  373. if (!empty($message)) {
  374. foreach ($message as $key => $value) {
  375. // $message[$key]->iquantity = $value->iquantity - $value->out_quantity;
  376. $message[$key] = (array)$value;
  377. }
  378. }
  379. return $message;
  380. }
  381. public function post_helper($url, $data, $header = [], $timeout = 60)
  382. {
  383. $ch = curl_init();
  384. curl_setopt($ch, CURLOPT_URL, $url);
  385. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  386. curl_setopt($ch, CURLOPT_ENCODING, '');
  387. curl_setopt($ch, CURLOPT_POST, 1);
  388. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
  389. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  390. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  391. curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
  392. if (!is_null($data)) curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  393. $r = curl_exec($ch);
  394. curl_close($ch);
  395. return json_decode($r, true);
  396. }
  397. //获取发货单数据 还没发的 分页
  398. public function getDataFromDispatchListPage($data)
  399. {
  400. //检索条件
  401. if (empty($data['time'][0]) || empty($data['time'][1])) return [false, '时间不能为空'];
  402. $model = $this->db->table('DispatchList as a')
  403. ->leftJoin('DispatchLists as b', 'b.DLID', 'a.DLID')
  404. ->leftJoin('Inventory as c', 'c.cInvCode', 'b.cInvCode')
  405. ->select('a.cDLCode as cdlcode', 'a.DLID as id', 'a.cDefine10 as customer_name', 'b.cSOCode as csocode', 'a.cDepCode as cdepcode', 'a.cCusCode as cuscode', 'a.dDate as date', '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')
  406. ->whereNotNull('a.cVerifier')
  407. // ->whereColumn('b.iQuantity', '>', 'b.fOutQuantity')
  408. ->where('a.dDate', '>=', $data['time'][0])
  409. ->where('a.dDate', '<=', $data['time'][1]);
  410. if (!empty($data['cdlcode'])) $model->where('a.cDLCode', 'Like', '%' . $data['cdlcode'] . '%');
  411. if (!empty($data['cinvcode'])) $model->where('b.cInvCode', 'Like', '%' . $data['cinvcode'] . '%');
  412. if (!empty($data['product_title'])) $model->where('b.cInvName', 'Like', '%' . $data['product_title'] . '%');
  413. if (!empty($data['product_size'])) $model->where('c.cInvStd', 'Like', '%' . $data['product_size'] . '%');
  414. if (!empty($data['technology_material'])) $model->where('b.cDefine28', 'Like', '%' . $data['technology_material'] . '%');
  415. if (!empty($data['cfree1'])) $model->where('b.cFree1', 'Like', '%' . $data['cfree1'] . '%');
  416. if (!empty($data['cfree2'])) $model->where('b.cFree2', 'Like', '%' . $data['cfree2'] . '%');
  417. if (!empty($data['process_mark'])) $model->where('b.cDefine30', 'Like', '%' . $data['process_mark'] . '%');
  418. $list = $this->limit($model, '', $data);
  419. if (!empty($list['data'])) {
  420. $product_no = array_unique(array_column($list['data'], 'cinvcode'));
  421. $messageMap = $this->db->table('Inventory as a')
  422. ->join('ComputationUnit as b', function ($join) {
  423. $join->on('a.cGroupCode', '=', 'b.cGroupCode')
  424. ->on('a.cComUnitCode', '=', 'b.cComUnitCode');
  425. }, null, null, 'left')
  426. ->whereIn('a.cInvCode', $product_no)
  427. ->pluck('b.cComUnitName as product_unit', 'a.cInvCode')
  428. ->toArray();
  429. foreach ($list['data'] as $key => $value) {
  430. $list['data'][$key] = (array)$value;
  431. $list['data'][$key]['unit'] = $messageMap[$value->cinvcode] ?? '';
  432. $list['data'][$key]['iquantity'] = intval($value->iquantity);
  433. $list['data'][$key]['out_quantity'] = intval($value->out_quantity);
  434. $list['data'][$key]['quantity'] = intval($value->quantity);
  435. }
  436. }
  437. return $list;
  438. }
  439. }