U8ServerService.php 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327
  1. <?php
  2. namespace App\Service;
  3. use App\Model\DDEmployee;
  4. use App\Model\FieldData;
  5. use App\Model\Inventory;
  6. use App\Model\Record;
  7. use App\Model\U8State;
  8. use App\Model\Vendor;
  9. use Illuminate\Support\Facades\DB;
  10. class U8ServerService extends Service
  11. {
  12. protected $databaseService;
  13. /**
  14. * @var string|null
  15. */
  16. protected $error;
  17. /**
  18. * U8ServerService constructor.
  19. */
  20. public function __construct($loginUser = [])
  21. {
  22. $service = new U8DatabaseServerService($loginUser);
  23. $this->databaseService = $service->db;
  24. $this->error = $service->error;
  25. }
  26. /**
  27. * 获取错误信息
  28. *
  29. * @return string|null
  30. */
  31. public function getError()
  32. {
  33. return $this->error;
  34. }
  35. public function purchaseOrder($data, $user){
  36. $qx = $user['qx'];
  37. $order_date = $data['order_date'] ?? [];
  38. $order_date = array_filter($order_date);
  39. $order_number = $data['order_number'] ?? "";
  40. $model = $this->databaseService->table('PO_Pomain as a')
  41. ->leftJoin('Vendor as c', 'c.cVenCode', 'a.cVenCode') // 供应商
  42. ->leftJoin('Department as d', 'd.cDepCode', 'a.cDepCode') // 部门
  43. ->leftJoin('Person as e', 'e.cPersonCode', 'a.cPersonCode')// 业务员
  44. ->leftJoin('PurchaseType as f', 'f.cPTCode', 'a.cPTCode') // 采购类型
  45. ->when(empty($qx), function ($query) use($user){
  46. return $query->where('a.cMaker', $user['name']);
  47. })
  48. ->when(!empty($order_number), function ($query) use($order_number){
  49. return $query->where('a.cPOID', 'LIKE', '%'.$order_number.'%');
  50. })
  51. ->when(!empty($order_date), function ($query) use($order_date){
  52. $start = date('Y-m-d 00:00:00.000', $order_date[0]);
  53. $end = date('Y-m-d 23:59:59.000', $order_date[1]);
  54. return $query->whereBetween('a.dPODate', [$start, $end]);
  55. })
  56. ->where(function ($query) {
  57. $query->where('a.iverifystateex', 0)
  58. ->orWhereNull('a.iverifystateex');
  59. })
  60. ->select(
  61. DB::raw("ISNULL(a.cBusType, '') as business_type"), // 业务类型
  62. DB::raw("ISNULL(CONVERT(varchar(10), a.dPODate, 120), '') as order_date"), // 订单日期
  63. DB::raw("ISNULL(a.cPOID, '') as order_number"), // 订单编号
  64. DB::raw("ISNULL(f.cPTName, '') as purchase_type"), // 采购类型
  65. DB::raw("ISNULL(c.cVenName, '') as supplier_title"), // 供应商
  66. DB::raw("ISNULL(d.cDepName, '') as department_name"), // 部门
  67. DB::raw("ISNULL(e.cPersonName, '') as person_name"), // 业务员
  68. DB::raw("CAST(ISNULL(a.iTaxRate, 0) AS varchar) as tax_rate"), // 税率(数值转字符,null转0或空)
  69. DB::raw("ISNULL(a.cexch_name, '') as currency_name"), // 币种
  70. DB::raw("CAST(ISNULL(a.nflat, 0) AS varchar) as exchange_rate"), // 汇率
  71. DB::raw("ISNULL(a.cMemo, '') as remark"), // 备注
  72. DB::raw("ISNULL(a.cMaker, '') as crt_name") // 制单人
  73. )
  74. ->orderBy('a.POID', 'desc');
  75. $list = $this->limit($model, '', $data);
  76. $list = $this->fillAll($list, U8State::type_one, $user['login_type']);
  77. return [true, $list];
  78. }
  79. public function purchaseOrderDetail($data, $user){
  80. if(empty($data['order_number'])) return [false, '采购单号不能为空'];
  81. $order = $this->databaseService->table('PO_Pomain as a')
  82. ->leftJoin('Vendor as c', 'c.cVenCode', 'a.cVenCode')
  83. ->leftJoin('Department as d', 'd.cDepCode', 'a.cDepCode')
  84. ->leftJoin('Person as e', 'e.cPersonCode', 'a.cPersonCode')
  85. ->leftJoin('PurchaseType as f', 'f.cPTCode', 'a.cPTCode')
  86. ->where('a.cPOID', $data['order_number'])
  87. ->select(
  88. 'a.POID',
  89. DB::raw("ISNULL(a.cBusType, '') as business_type"),
  90. DB::raw("ISNULL(CONVERT(varchar(10), a.dPODate, 120), '') as order_date"),
  91. DB::raw("ISNULL(a.cPOID, '') as order_number"),
  92. DB::raw("ISNULL(f.cPTName, '') as purchase_type"),
  93. DB::raw("ISNULL(c.cVenName, '') as supplier_title"),
  94. DB::raw("ISNULL(d.cDepName, '') as department_name"),
  95. DB::raw("ISNULL(e.cPersonName, '') as person_name"),
  96. DB::raw("CAST(ISNULL(a.iTaxRate, 0) AS varchar) as tax_rate"),
  97. DB::raw("ISNULL(a.cexch_name, '') as currency_name"),
  98. DB::raw("CAST(ISNULL(a.nflat, 0) AS varchar) as exchange_rate"),
  99. DB::raw("ISNULL(a.cMemo, '') as remark"),
  100. DB::raw("ISNULL(a.cMaker, '') as crt_name")
  101. )
  102. ->first();
  103. if(empty($order)) return [false, '采购单不存在'];
  104. $order = (array) $order;
  105. $order = $this->fillDetail($order, U8State::type_one, $user['login_type']);
  106. // 获取明细
  107. $detail = $this->databaseService->table('PO_Podetails as a')
  108. ->leftJoin('Inventory as b', 'b.cInvCode', 'a.cInvCode')
  109. ->leftJoin('ComputationUnit as c', 'c.cComunitCode', 'b.cComUnitCode')
  110. ->where('a.POID', $order['POID'])
  111. ->select(
  112. DB::raw("ISNULL(b.cInvCode, '') as product_code"),
  113. DB::raw("ISNULL(b.cInvName, '') as product_title"),
  114. DB::raw("ISNULL(b.cInvStd, '') as product_std"),
  115. DB::raw("ISNULL(c.cComUnitName, '') as unit_title"),
  116. // 数字转字符串并去除空格,NULL 则返回 '0.00' 或 ''
  117. DB::raw("ISNULL(LTRIM(STR(a.iQuantity, 20, 2)), '0.00') as quantity"),
  118. DB::raw("ISNULL(LTRIM(STR(a.iTaxPrice, 20, 4)), '0.0000') as tax_unit_price"),
  119. DB::raw("ISNULL(LTRIM(STR(a.iUnitPrice, 20, 4)), '0.0000') as unit_price"),
  120. DB::raw("ISNULL(LTRIM(STR(a.iMoney, 20, 2)), '0.00') as amount"),
  121. DB::raw("ISNULL(LTRIM(STR(a.iSum, 20, 2)), '0.00') as tax_amount"),
  122. DB::raw("ISNULL(LTRIM(STR(a.iPerTaxRate, 20, 2)), '0.00') as tax_rate"),
  123. DB::raw("ISNULL(CONVERT(varchar(10), a.dArriveDate, 120), '') as arrive_date"),
  124. DB::raw("ISNULL(b.cEnterprise, '') as factory_name")
  125. )
  126. ->get();
  127. // 转为数组
  128. $order['detail'] = $detail->map(function ($item) {
  129. return (array) $item;
  130. })->toArray();
  131. // 移除内部 ID 避免暴露
  132. unset($order['POID']);
  133. return [true, $order];
  134. }
  135. public function purchaseRequisition($data, $user){
  136. $qx = $user['qx'];
  137. $order_date = $data['order_date'] ?? [];
  138. $order_date = array_filter($order_date);
  139. $order_number = $data['order_number'] ?? "";
  140. $model = $this->databaseService->table('PU_AppVouch as a')
  141. ->leftJoin('Person as c', 'c.cPersonCode', 'a.cPersonCode') // 请购人
  142. ->leftJoin('Department as d', 'd.cDepCode', 'a.cDepCode') // 请购部门
  143. ->leftJoin('PurchaseType as f', 'f.cPTCode', 'a.cPTCode') // 采购类型
  144. ->when(empty($qx), function ($query) use($user){
  145. return $query->where('a.cMaker',$user['name']);
  146. })
  147. ->when(! empty($order_number), function ($query) use($order_number){
  148. return $query->where('a.cCode','LIKE', '%'.$order_number.'%');
  149. })
  150. ->when(! empty($order_date), function ($query) use($order_date){
  151. $start = date('Y-m-d 00:00:00.000', $order_date[0]);
  152. $end = date('Y-m-d 23:59:59.000', $order_date[1]);
  153. return $query->whereBetween('a.dDate', [$start, $end]);
  154. })
  155. ->where(function ($query) {
  156. $query->where('a.iverifystateex', 0)
  157. ->orWhereNull('a.iverifystateex');
  158. })
  159. ->select(
  160. DB::raw("ISNULL(a.cBusType, '') as business_type"), // 业务类型
  161. DB::raw("ISNULL(a.cCode, '') as order_number"), // 单据号
  162. DB::raw("ISNULL(CONVERT(varchar(10), a.dDate, 120), '') as order_date"), // 日期
  163. DB::raw("ISNULL(d.cDepName, '') as department_name"), // 请购部门
  164. DB::raw("ISNULL(c.cPersonName, '') as purchase_name"), // 请购人员
  165. DB::raw("ISNULL(f.cPTName, '') as purchase_type"), // 采购类型
  166. DB::raw("ISNULL(a.cMaker, '') as crt_name") // 制单人
  167. )
  168. ->orderBy('a.ID','desc');
  169. $list = $this->limit($model,'',$data);
  170. $list = $this->fillAll($list, U8State::type_two, $user['login_type']);
  171. return [true , $list];
  172. }
  173. public function purchaseRequisitionDetail($data, $user){
  174. if(empty($data['order_number'])) return [false, '采购请购单号不能为空'];
  175. $order = $this->databaseService->table('PU_AppVouch as a')
  176. ->leftJoin('Person as c', 'c.cPersonCode', 'a.cPersonCode')
  177. ->leftJoin('Department as d', 'd.cDepCode', 'a.cDepCode')
  178. ->leftJoin('PurchaseType as f', 'f.cPTCode', 'a.cPTCode')
  179. ->where('a.cCode', $data['order_number'])
  180. ->select(
  181. 'a.ID', // 用于关联子表
  182. DB::raw("ISNULL(a.cBusType, '') as business_type"),
  183. DB::raw("ISNULL(a.cCode, '') as order_number"),
  184. DB::raw("ISNULL(CONVERT(varchar(10), a.dDate, 120), '') as order_date"),
  185. DB::raw("ISNULL(d.cDepName, '') as department_name"),
  186. DB::raw("ISNULL(c.cPersonName, '') as purchase_name"),
  187. DB::raw("ISNULL(f.cPTName, '') as purchase_type"),
  188. DB::raw("ISNULL(a.cMaker, '') as crt_name")
  189. )
  190. ->first();
  191. if(empty($order)) return [false, '采购请购单不存在'];
  192. $order = (array) $order;
  193. $order = $this->fillDetail($order, U8State::type_two, $user['login_type']);
  194. $detail = $this->databaseService->table('PU_AppVouchs as a')
  195. ->leftJoin('Inventory as b', 'b.cInvCode', 'a.cInvCode')
  196. ->leftJoin('ComputationUnit as c', 'c.cComunitCode', 'b.cComUnitCode')
  197. ->where('a.ID', $order['ID'])
  198. ->select(
  199. DB::raw("ISNULL(b.cInvCode, '') as product_code"), // 存货编码
  200. DB::raw("ISNULL(b.cInvName, '') as product_title"), // 存货名称
  201. DB::raw("ISNULL(b.cInvStd, '') as product_std"), // 规格型号
  202. DB::raw("ISNULL(c.cComUnitName, '') as unit_title"), // 主计量
  203. DB::raw("ISNULL(LTRIM(STR(a.fQuantity, 20, 2)), '0.00') as quantity"), // 数量
  204. DB::raw("ISNULL(LTRIM(STR(a.fUnitPrice, 20, 4)), '0.0000') as unit_price"), // 本币单价
  205. DB::raw("ISNULL(LTRIM(STR(a.iOriSum, 20, 2)), '0.00') as tax_amount"), // 本币价税合计
  206. DB::raw("ISNULL(CONVERT(varchar(10), a.dRequirDate, 120), '') as need_arrived_date"), // 需求日期
  207. DB::raw("ISNULL(CONVERT(varchar(10), a.dArriveDate, 120), '') as suggest_order_date"), // 建议订货日期
  208. DB::raw("ISNULL(b.cEnterprise, '') as factory_name") // 生产企业
  209. )
  210. ->get();
  211. // 转为数组格式
  212. $order['detail'] = $detail->map(function ($item) {
  213. return (array) $item;
  214. })->toArray();
  215. unset($order['ID']); // 隐藏内部ID
  216. return [true, $order];
  217. }
  218. public function purchaseInOrder($data, $user){
  219. $qx = $user['qx'];
  220. $order_date = $data['order_date'] ?? [];
  221. $order_date = array_filter($order_date);
  222. $order_number = $data['order_number'] ?? "";
  223. $model = $this->databaseService->table('RdRecord01 as a')
  224. ->leftJoin('Vendor as c', 'c.cVenCode', 'a.cVenCode') // 供货单位
  225. ->leftJoin('Warehouse as w', 'w.cWhCode', 'a.cWhCode') // 仓库
  226. ->leftJoin('Department as d', 'd.cDepCode', 'a.cDepCode') // 部门
  227. ->leftJoin('Person as p', 'p.cPersonCode', 'a.cPersonCode')// 业务员
  228. ->leftJoin('PurchaseType as pt', 'pt.cPTCode', 'a.cPTCode')// 采购类型
  229. ->leftJoin('Rd_Style as rs', 'rs.cRdCode', 'a.cRdCode') // 入库类别
  230. ->when(empty($qx), function ($query) use($user){
  231. return $query->where('a.cMaker',$user['name']);
  232. })
  233. ->when(! empty($order_number), function ($query) use($order_number){
  234. return $query->where('a.cCode','LIKE', '%'.$order_number.'%');
  235. })
  236. ->when(! empty($order_date), function ($query) use($order_date){
  237. $start = date('Y-m-d 00:00:00.000', $order_date[0]);
  238. $end = date('Y-m-d 23:59:59.000', $order_date[1]);
  239. return $query->whereBetween('a.dDate', [$start, $end]);
  240. })
  241. ->where(function ($query) {
  242. $query->whereNull('a.cHandler')
  243. ->orWhere('a.cHandler', '');
  244. })// 未审核
  245. ->select(
  246. DB::raw("ISNULL(a.cCode, '') as order_number"), // 入库单号
  247. DB::raw("ISNULL(CONVERT(varchar(10), a.dDate, 120), '') as order_date"), // 入库日期
  248. DB::raw("ISNULL(w.cWhName, '') as warehouse_name"), // 仓库
  249. DB::raw("ISNULL(a.cOrderCode, '') as po_number"), // 订单号
  250. DB::raw("ISNULL(a.cARVCode, '') as arrival_number"), // 到货单号
  251. DB::raw("ISNULL(p.cPersonName, '') as person_name"), // 业务员
  252. DB::raw("ISNULL(c.cVenName, '') as supplier_title"), // 供货单位
  253. DB::raw("ISNULL(d.cDepName, '') as department_name"), // 部门
  254. DB::raw("ISNULL(CONVERT(varchar(10), a.dARVDate, 120), '') as arrival_date"), // 到货日期
  255. DB::raw("ISNULL(a.cBusType, '') as business_type"), // 业务类型
  256. DB::raw("ISNULL(pt.cPTName, '') as purchase_type"), // 采购类型
  257. DB::raw("ISNULL(rs.cRdName, '') as rd_style_name"), // 入库类别
  258. DB::raw("ISNULL(CONVERT(varchar(10), a.dVeriDate, 120), '') as audit_date"), // 审核日期
  259. DB::raw("ISNULL(a.cMemo, '') as remark"), // 备注
  260. DB::raw("ISNULL(a.cMaker, '') as crt_name") // 制单人
  261. )
  262. ->orderBy('a.ID','desc');
  263. $list = $this->limit($model,'',$data);
  264. $list = $this->fillAll($list, U8State::type_three, $user['login_type']);
  265. return [true , $list];
  266. }
  267. public function purchaseInOrderDetail($data, $user){
  268. if(empty($data['order_number'])) return [false, '入库单号不能为空'];
  269. $order = $this->databaseService->table('RdRecord01 as a')
  270. ->leftJoin('Vendor as c', 'c.cVenCode', 'a.cVenCode')
  271. ->leftJoin('Warehouse as w', 'w.cWhCode', 'a.cWhCode')
  272. ->leftJoin('Department as d', 'd.cDepCode', 'a.cDepCode')
  273. ->leftJoin('Person as p', 'p.cPersonCode', 'a.cPersonCode')
  274. ->leftJoin('PurchaseType as pt', 'pt.cPTCode', 'a.cPTCode')
  275. ->leftJoin('Rd_Style as rs', 'rs.cRdCode', 'a.cRdCode')
  276. ->where('a.cCode', $data['order_number'])
  277. ->select(
  278. 'a.ID',
  279. DB::raw("ISNULL(a.cCode, '') as order_number"),
  280. DB::raw("ISNULL(CONVERT(varchar(10), a.dDate, 120), '') as order_date"),
  281. DB::raw("ISNULL(w.cWhName, '') as warehouse_name"),
  282. DB::raw("ISNULL(a.cOrderCode, '') as po_number"),
  283. DB::raw("ISNULL(a.cARVCode, '') as arrival_number"),
  284. DB::raw("ISNULL(p.cPersonName, '') as person_name"),
  285. DB::raw("ISNULL(c.cVenName, '') as supplier_title"),
  286. DB::raw("ISNULL(d.cDepName, '') as department_name"),
  287. DB::raw("ISNULL(CONVERT(varchar(10), a.darvdate, 120), '') as arrival_date"),
  288. DB::raw("ISNULL(a.cBusType, '') as business_type"),
  289. DB::raw("ISNULL(pt.cPTName, '') as purchase_type"),
  290. DB::raw("ISNULL(rs.cRdName, '') as rd_style_name"),
  291. DB::raw("ISNULL(CONVERT(varchar(10), a.dVeriDate, 120), '') as audit_date"),
  292. DB::raw("ISNULL(a.cMemo, '') as remark"),
  293. DB::raw("ISNULL(a.cMaker, '') as crt_name")
  294. )
  295. ->first();
  296. if(empty($order)) return [false, '采购入库单不存在'];
  297. $order = (array) $order;
  298. $order = $this->fillDetail($order, U8State::type_three, $user['login_type']);
  299. $detail = $this->databaseService->table('rdrecords01 as a')
  300. ->leftJoin('Inventory as b', 'b.cInvCode', 'a.cInvCode')
  301. ->leftJoin('ComputationUnit as c', 'c.cComunitCode', 'b.cComUnitCode')
  302. ->where('a.ID', $order['ID'])
  303. ->select(
  304. DB::raw("ISNULL(b.cInvCode, '') as product_code"), // 存货编码
  305. DB::raw("ISNULL(b.cInvName, '') as product_title"), // 存货名称
  306. DB::raw("ISNULL(b.cInvStd, '') as product_std"), // 规格型号
  307. DB::raw("ISNULL(c.cComUnitName, '') as unit_title"), // 主计量单位
  308. DB::raw("ISNULL(LTRIM(STR(a.iQuantity, 20, 2)), '0.00') as quantity"), // 数量
  309. DB::raw("ISNULL(LTRIM(STR(a.iUnitCost, 20, 4)), '0.0000') as unit_price"), // 本币单价
  310. DB::raw("ISNULL(LTRIM(STR(a.iPrice, 20, 2)), '0.00') as amount"), // 本币金额
  311. DB::raw("ISNULL(LTRIM(STR(a.iTax, 20, 2)), '0.00') as tax"), // 税额
  312. DB::raw("ISNULL(LTRIM(STR(a.iTaxPrice, 20, 2)), '0.00') as nat_tax"), // 本币税额 (入库单本币税额通常等于原币税额)
  313. DB::raw("ISNULL(LTRIM(STR(a.iSum, 20, 2)), '0.00') as tax_amount") // 本币价税合计
  314. )
  315. ->get();
  316. $detailArr = $detail->map(function ($item) {
  317. return (array) $item;
  318. })->toArray();
  319. // 计算总金额(价税合计之和)
  320. $order['total_amount'] = number_format(array_sum(array_column($detailArr, 'tax_amount')), 2, '.', '');
  321. $order['detail'] = $detailArr;
  322. unset($order['ID']);
  323. return [true, $order];
  324. }
  325. public function inventoryDetail($data, $user){
  326. if(empty($data['order_number'])) return [false, '流水单号不能为空'];
  327. $order = Inventory::where('del_time',0)
  328. ->where('order_number', $data['order_number'])
  329. ->where('login_type', $user['login_type'])
  330. ->first();
  331. if(empty($order)) return [false, '存货不存在'];
  332. $order = $order->toArray() ;
  333. $order['crt_name'] = DDEmployee::where('login_type', $user['login_type'])->where('userid', $order['crt_id'])->value('name') ?? '';
  334. return [true, $order];
  335. }
  336. public function vendorDetail($data, $user){
  337. if(empty($data['order_number'])) return [false, '流水单号不能为空'];
  338. $order = Vendor::where('del_time',0)
  339. ->where('order_number', $data['order_number'])
  340. ->where('login_type', $user['login_type'])
  341. ->first();
  342. if(empty($order)) return [false, '供应商不存在'];
  343. $order = $order->toArray() ;
  344. $order['crt_name'] = DDEmployee::where('login_type', $user['login_type'])->where('userid', $order['crt_id'])->value('name') ?? '';
  345. return [true, $order];
  346. }
  347. public function getOrderDetails($data,$user){
  348. $type = $data['type'];
  349. [$success, $order] = [false, '异常错误'];
  350. if($type == 1){
  351. // 采购单
  352. // [$success, $order] = $this->purchaseOrderDetail($data,$user);
  353. [$success, $order] = (new U8XkyServerService())->purchaseOrderMyDetail($data,$user);
  354. }elseif ($type == 2){
  355. // 采购请购单
  356. // [$success, $order] = $this->purchaseRequisitionDetail($data,$user);
  357. [$success, $order] = (new U8XkyServerService())->purchaseRequisitionMyDetail($data,$user);
  358. }elseif ($type == 3){
  359. // 采购入库
  360. // [$success, $order] = $this->purchaseInOrderDetail($data,$user);
  361. [$success, $order] = (new U8XkyServerService())->purchaseOrderInMyDetail($data,$user);
  362. }elseif ($type == 4){
  363. // 存货
  364. [$success, $order] = $this->inventoryDetail($data,$user);
  365. }elseif ($type == 5){
  366. // 供应商
  367. [$success, $order] = $this->vendorDetail($data,$user);
  368. }
  369. return [$success, $order];
  370. }
  371. private function fillAll($list, $type, $login_type){
  372. if(empty($list['data'])) return $list;
  373. $map = U8State::where('del_time', 0)
  374. ->where('type', $type)
  375. ->where('login_type', $login_type)
  376. ->whereIn('order_number', array_column($list['data'], 'order_number'))
  377. ->pluck('state', 'order_number')
  378. ->toArray();
  379. foreach ($list['data'] as $key => $value){
  380. if(isset($map[$value->order_number])) {
  381. $m = $map[$value->order_number];
  382. $state = $m;
  383. $state_title = Record::state_name[$state];
  384. }else{
  385. $state = Record::state_minus_one;
  386. $state_title = Record::state_name[$state];
  387. }
  388. $list['data'][$key]->state = $state;
  389. $list['data'][$key]->state_title = $state_title;
  390. }
  391. return $list;
  392. }
  393. private function fillDetail($list, $type, $login_type)
  394. {
  395. if (empty($list)) return $list;
  396. // 1. 从 U8State 表中查询 state 和 result
  397. $u8Status = U8State::where('del_time', 0)
  398. ->where('type', $type)
  399. ->where('login_type', $login_type)
  400. ->where('order_number', $list['order_number'])
  401. ->select('state', 'result') // 明确查询需要的字段
  402. ->first();
  403. // 2. 逻辑判断
  404. if ($u8Status) {
  405. $state = $u8Status->state;
  406. $result = $u8Status->result; // 获取你想要的 result 字段
  407. $state_title = Record::state_name[$state] ?? '';
  408. } else {
  409. $state = Record::state_minus_one;
  410. $result = ''; // 或者根据业务给个默认值,如 ''
  411. $state_title = Record::state_name[$state] ?? '';
  412. }
  413. // 3. 注入到 list 中
  414. $list['state'] = $state;
  415. $list['state_title'] = $state_title;
  416. $list['result'] = $result; // 将 result 返回给前端或后续逻辑
  417. return $list;
  418. }
  419. public function stockList($data, $user)
  420. {
  421. try {
  422. $field_list = [];
  423. $employee = DDEmployee::where('userid', $user['userid'])->where('login_type', $user['login_type'])->first();
  424. $qx = $employee['qx'] ?? 0;
  425. if(empty($qx)) {
  426. $field_list = FieldData::where('userid', $user['userid'])
  427. ->where('login_type', $user['login_type'])
  428. ->where('type', FieldData::STATE_ZERO)
  429. ->pluck('key')
  430. ->all();
  431. }
  432. // 1. 构建基础查询:关联现存量表和存货档案表
  433. $query = $this->databaseService->table('CurrentStock as S')
  434. ->select([
  435. 'S.cWhCode', // 仓库编码
  436. 'W.cWhName', // 仓库名称
  437. 'S.cInvCode', // 存货编码
  438. 'I.cInvName', // 存货名称
  439. 'I.cInvStd', // 规格型号
  440. 'I.cInvCCode', // 分类编码
  441. // --- 数量字段对齐你的结构 ---
  442. 'S.iQuantity', // 结存数量 (账面现存量)
  443. 'S.fAvaQuantity', // 可用数量
  444. 'S.fOutQuantity', // 待发货数量 (待出)
  445. 'S.fInQuantity', // 待入库数量 (待入)
  446. 'S.fStopQuantity', // 冻结数量
  447. // --- 批次与日期 ---
  448. 'S.cBatch', // 批号
  449. 'S.dMdate', // 生产日期
  450. 'S.dVDate', // 失效日期
  451. ])
  452. ->join('Inventory as I', 'S.cInvCode', '=', 'I.cInvCode')
  453. ->leftJoin('Warehouse as W', 'S.cWhCode', '=', 'W.cWhCode')
  454. ->leftJoin('InventoryClass as IC', 'I.cInvCCode', '=', 'IC.cInvCCode');
  455. // 2. 过滤条件:存货名称 (支持模糊查询)
  456. if (!empty($data['material_title'])) {
  457. $query->where('I.cInvName', 'like', '%' . $data['material_title'] . '%');
  458. }
  459. // 2. 过滤条件:存货编码 (支持模糊查询)
  460. if (!empty($data['material_code'])) {
  461. $query->where('S.cInvCode', 'like', '%' . $data['material_code'] . '%');
  462. }
  463. // 3. 过滤条件:存货分类 (支持左匹配,即选大类查出所有子类)
  464. if (!empty($data['category_code'])) {
  465. // U8 分类是级次结构,用 like '01%' 可以查出 01 开头的所有子类
  466. $query->where('I.cInvCCode', 'like', $data['category_code'] . '%');
  467. }
  468. // 6. 排序
  469. $query->orderBy('S.cInvCode', 'asc')->orderBy('S.cWhCode', 'asc');
  470. // 7. 调用你定义的分页方法
  471. $columns = ['*'];
  472. $result = $this->limit($query, $columns, $data);
  473. // 注意这里的 &$item,加了 & 符号才能直接修改原数组里的内容
  474. foreach ($result['data'] as &$item) {
  475. $numFields = ['iQuantity', 'fAvaQuantity', 'fOutQuantity', 'fInQuantity', 'fStopQuantity'];
  476. foreach ($numFields as $field) {
  477. if (isset($item->$field)) {
  478. $item->$field = (float)$item->$field;
  479. }
  480. }
  481. $item->dMdate = $item->dMdate ? date('Y-m-d', strtotime($item->dMdate)) : '';
  482. $item->dVDate = $item->dVDate ? date('Y-m-d', strtotime($item->dVDate)) : '';
  483. // 脱敏处理
  484. if (!empty($field_list)) {
  485. foreach ($field_list as $blackField) {
  486. if (isset($item->$blackField)) {
  487. $item->$blackField = '*****';
  488. }
  489. }
  490. }
  491. }
  492. unset($item); // 销毁引用
  493. return [true, $result];
  494. } catch (\Throwable $exception) {
  495. return [false, "查询库存失败: " . $exception->getMessage()];
  496. }
  497. }
  498. public function vendorU8List($data, $user)
  499. {
  500. // 1. 构建基础查询
  501. $query = $this->databaseService->table('Vendor as V')
  502. ->select([
  503. 'V.cVenCode', // 供应商编码
  504. 'V.cVenName', // 供应商名称
  505. 'V.cVenAbbName', // 供应商简称
  506. 'V.cVCCode', // 分类编码
  507. 'VC.cVCName', // 分类名称 (来自 VendorClass)
  508. 'V.cVenAddress', // 地址
  509. 'V.cVenPhone', // 电话
  510. 'V.dVenDevDate', // 发展日期
  511. 'V.cCreatePerson', // 创建人
  512. 'V.bVenTax', // 是否计税
  513. 'V.iId' // 内部ID
  514. ])
  515. // 关联供应商分类表获取分类名称
  516. ->leftJoin('VendorClass as VC', 'V.cVCCode', '=', 'VC.cVCCode');
  517. // 2. 增加搜索逻辑 (可选)
  518. if (!empty($data['keyword'])) {
  519. $keyword = $data['keyword'];
  520. $query->where(function($q) use ($keyword) {
  521. $q->where('V.cVenCode', 'like', "%{$keyword}%")
  522. ->orWhere('V.cVenName', 'like', "%{$keyword}%")
  523. ->orWhere('V.cVenAbbName', 'like', "%{$keyword}%");
  524. });
  525. }
  526. // 3. 排序 (默认按编码排序)
  527. $query->orderBy('V.cVenCode', 'ASC');
  528. // 4. 调用你定义的分页方法
  529. // 注意:limit 方法内部会执行 paginate 并将结果填充到 $data
  530. $columns = ['*']; // select 已经在上面定义过了,这里传 * 即可
  531. $result = $this->limit($query, $columns, $data);
  532. return [true, $result];
  533. }
  534. public function vendorClassTree($data, $user)
  535. {
  536. try {
  537. // 1. 获取所有供应商分类 (表名: VendorClass)
  538. $classes = $this->databaseService->table('VendorClass')
  539. ->select('cVCCode', 'cVCName', 'iVCGrade', 'bVCEnd') // 编码、名称、级次
  540. ->orderBy('cVCCode', 'asc')
  541. ->get();
  542. if ($classes->isEmpty()) {
  543. return [true, []];
  544. }
  545. // 2. 格式化数据,以编码为 Key
  546. $classList = [];
  547. foreach ($classes as $item) {
  548. $classList[$item->cVCCode] = [
  549. 'label' => $item->cVCName,
  550. 'value' => $item->cVCCode, // 前端通常需要 value 字段
  551. 'code' => $item->cVCCode,
  552. 'grade' => $item->iVCGrade,
  553. 'is_end' => $item->bVCEnd,
  554. 'children' => []
  555. ];
  556. }
  557. // 3. 构建引用树
  558. $tree = [];
  559. foreach ($classList as $code => &$node) {
  560. // 获取父级编码
  561. $parentCode = $this->getParentCode($code);
  562. if ($parentCode === null || !isset($classList[$parentCode])) {
  563. // 顶级节点
  564. $tree[] = &$node;
  565. } else {
  566. // 挂载到父节点
  567. $classList[$parentCode]['children'][] = &$node;
  568. }
  569. }
  570. return [true, $tree];
  571. } catch (\Throwable $exception) {
  572. return [false, "获取供应商分类树失败: " . $exception->getMessage()];
  573. }
  574. }
  575. /**
  576. * 辅助函数:根据 U8 编码规则获取父级编码
  577. * U8 的级次通常存储在 GradeDef 表,但通用逻辑是截取末尾
  578. */
  579. private function getParentCode($code)
  580. {
  581. $len = strlen($code);
  582. if ($len <= 2) return null; // 假设第一级是2位,小于等于2位则无父级
  583. // 这里假设级次是 2-2-2-2 (最常见配置)
  584. // 实际生产中,如果级次不固定,建议查询 GradeDef 表
  585. return substr($code, 0, $len - 2);
  586. }
  587. //U8 存货分类树结构
  588. public function inventoryClassTree($data, $user)
  589. {
  590. try {
  591. // 1. 从数据库获取所有存货分类
  592. $classes = $this->databaseService->table('InventoryClass')
  593. ->select('cInvCCode', 'cInvCName', 'iInvCGrade', 'bInvCEnd')
  594. ->orderBy('cInvCCode', 'asc')
  595. ->get();
  596. if ($classes->isEmpty()) return [true, []];
  597. // 2. 将集合转换为数组并以编码作为 Key,方便查找
  598. $classList = [];
  599. foreach ($classes as $item) {
  600. $classList[$item->cInvCCode] = [
  601. 'label' => $item->cInvCName,
  602. 'code' => $item->cInvCCode,
  603. 'grade' => $item->iInvCGrade,
  604. 'is_end' => $item->bInvCEnd,
  605. 'children' => []
  606. ];
  607. }
  608. // 3. 构建树形结构
  609. $tree = [];
  610. foreach ($classList as $code => &$node) {
  611. // 获取当前分类的级次 (U8 逻辑通常根据编码长度判断父级)
  612. // 比如 0101 的父级是 01
  613. $parentCode = $this->getParentCode($code);
  614. if ($parentCode === null || !isset($classList[$parentCode])) {
  615. // 如果没有父级编码,或者父级编码不在列表里,说明是顶级分类
  616. $tree[] = &$node;
  617. } else {
  618. // 将当前节点引用到父节点的 children 数组中
  619. $classList[$parentCode]['children'][] = &$node;
  620. }
  621. }
  622. return [true, $tree];
  623. } catch (\Throwable $exception) {
  624. return [false, "获取分类树失败: " . $exception->getMessage()];
  625. }
  626. }
  627. //U8 计量单位组(带默认主计量单位)
  628. public function getUnitGroups($data, $user)
  629. {
  630. $list = $this->databaseService->select("
  631. SELECT
  632. G.cGroupCode,
  633. G.cGroupName,
  634. G.iGroupType,
  635. U.cComUnitCode,
  636. U.cComUnitName,
  637. U.iNumber
  638. FROM ComputationGroup AS G
  639. OUTER APPLY (
  640. SELECT TOP 1 cComUnitCode, cComUnitName, iNumber
  641. FROM ComputationUnit
  642. WHERE cGroupCode = G.cGroupCode
  643. ORDER BY
  644. bMainUnit DESC, -- 1. 优先主计量
  645. iNumber ASC, -- 2. 序号最小 (若 NULL 会排在最前)
  646. cComUnitCode ASC -- 3. 编码最小
  647. ) AS U
  648. ");
  649. return [true, $list];
  650. }
  651. //U8 计量单位档案
  652. public function getComputationUnitList($data, $user)
  653. {
  654. $list = $this->databaseService->table('ComputationUnit as U')
  655. ->select(
  656. 'U.cComUnitCode', // 单位编码
  657. 'U.cComUnitName', // 单位名称
  658. 'U.cGroupCode', // 所属组编码
  659. 'U.bMainUnit', // 是否主单位
  660. 'U.iNumber' // 排序序号
  661. )
  662. ->orderBy('U.cGroupCode', 'ASC')
  663. ->orderBy('U.iNumber', 'ASC') // 按照你要求的 iNumber 排序
  664. ->get();
  665. return [true, $list];
  666. }
  667. // U8 采购类型
  668. public function getPurchaseTypeList($data, $user)
  669. {
  670. $list = $this->databaseService->table('PurchaseType as P')
  671. // 核心修改:左关联收发类别表 Rd_Style
  672. ->leftJoin('Rd_Style as R', 'R.cRdCode', '=', 'P.cRdCode')
  673. ->select(
  674. 'P.cPTCode', // 采购类型编码 (如: 01, 02)
  675. 'P.cPTName', // 采购类型名称 (如: 国内采购, 国外采购)
  676. 'P.bDefault', // 是否默认值
  677. // 核心修改:查出对应的默认收发类别编码和名称
  678. DB::raw("ISNULL(P.cRdCode, '') as rd_code"),
  679. DB::raw("ISNULL(R.cRdName, '') as rd_name")
  680. )
  681. ->orderBy('P.bDefault', 'DESC')
  682. ->get();
  683. return [true, $list];
  684. }
  685. // 存货档案
  686. public function inventoryU8List($data, $user)
  687. {
  688. // 获取前端传过来的查询参数
  689. $search_code = $data['code'] ?? ""; // 存货编码查询条件
  690. $search_name = $data['name'] ?? ""; // 存货名称查询条件
  691. $model = $this->databaseService->table('Inventory as i')
  692. // 核心修改:关联计量单位表获取单位名称
  693. ->leftJoin('ComputationUnit as u', 'u.cComUnitCode', 'i.cComUnitCode')
  694. // 当编码查询条件不为空时,进行模糊查询
  695. ->when(!empty($search_code), function ($query) use ($search_code) {
  696. return $query->where('i.cInvCode', 'LIKE', '%' . $search_code . '%');
  697. })
  698. // 当名称查询条件不为空时,进行模糊查询
  699. ->when(!empty($search_name), function ($query) use ($search_name) {
  700. return $query->where('i.cInvName', 'LIKE', '%' . $search_name . '%');
  701. })
  702. ->select(
  703. 'i.cInvCode as code', // 存货编码
  704. 'i.cInvName as name', // 存货名称
  705. 'i.cInvStd as size', // 规格型号
  706. 'i.cComUnitCode as unit_code', // 主计量单位编码
  707. 'u.cComUnitName as unit', // 核心修改:主计量单位名称 (如: 个、千克、箱)
  708. 'i.iImpTaxRate as purchase_rate' // 进项税率
  709. )
  710. ->orderBy('i.cInvCode', 'ASC');
  711. // 如果存货档案数据量非常大,建议这里配合分页使用,例如:
  712. $list = $this->limit($model, '', $data);
  713. return [true, $list];
  714. }
  715. // 仓库档案
  716. public function warehouseU8List($data, $user)
  717. {
  718. $list = $this->databaseService->table('Warehouse as W')
  719. ->select(
  720. 'W.cWhCode as code', // 仓库编码 (如: 01, 02)
  721. 'W.cWhName as name' // 仓库名称 (如: 原材料库, 半成品库)
  722. )
  723. ->orderBy('W.cWhCode', 'ASC')
  724. ->get();
  725. return [true, $list];
  726. }
  727. //获取u8请购单
  728. /**
  729. * 获取 U8 请购单列表(带存货明细 detail 字段)
  730. */
  731. public function purchaseRequisitionU8List($data, $user)
  732. {
  733. $order_date = $data['order_date'] ?? [];
  734. $order_date = array_filter($order_date);
  735. $code = $data['code'] ?? "";
  736. // 1. 构建主表查询 Model
  737. $model = $this->databaseService->table('PU_AppVouch as a')
  738. ->when(!empty($code), function ($query) use ($code) {
  739. return $query->where('a.cCode', 'LIKE', '%' . $code . '%');
  740. })
  741. ->when(!empty($order_date), function ($query) use ($order_date) {
  742. $start = date('Y-m-d 00:00:00.000', $order_date[0]);
  743. $end = date('Y-m-d 23:59:59.000', $order_date[1]);
  744. return $query->whereBetween('a.dDate', [$start, $end]);
  745. })
  746. ->where('a.iverifystateex', 2) // 已审核状态
  747. ->whereExists(function ($query) {
  748. $query->select(DB::raw(1))
  749. ->from('PU_AppVouchs as b')
  750. ->whereRaw('b.ID = a.ID')
  751. ->whereRaw('ISNULL(b.fQuantity, 0) > ISNULL(b.iReceivedQTY, 0)');
  752. })
  753. ->select(
  754. 'a.ID as id', // 必须查出主表 ID 用来后续关联子表
  755. DB::raw("ISNULL(a.cBusType, '') as business_type"),
  756. DB::raw("ISNULL(a.cCode, '') as order_number"),
  757. DB::raw("ISNULL(CONVERT(varchar(10), a.dDate, 120), '') as order_date"),
  758. DB::raw("ISNULL(a.cMaker, '') as crt_name")
  759. )
  760. ->orderBy('a.ID', 'desc');
  761. // 2. 获取主表分页列表数据
  762. $list = $this->limit($model, '', $data);
  763. // 如果列表为空,直接返回
  764. $items = $list['data'] ?? [];
  765. if (empty($items)) {
  766. return [true, $list];
  767. }
  768. // 3. 【核心结合】批量提取当前页所有主表的 ID
  769. $mainIds = array_column($items, 'id');
  770. // 4. 一次性批量查出这些主表对应的所有存货明细
  771. $details = $this->databaseService->table('PU_AppVouchs as b')
  772. ->leftJoin('Inventory as i', 'i.cInvCode', 'b.cInvCode')
  773. ->leftJoin('ComputationUnit as u', 'u.cComUnitCode', 'i.cComUnitCode')
  774. ->whereIn('b.ID', $mainIds) // 批量范围查询
  775. ->whereRaw('ISNULL(b.fQuantity, 0) > ISNULL(b.iReceivedQTY, 0)')
  776. ->select(
  777. 'b.AutoID as detail_id',
  778. 'b.ID as main_id', // 核心:用这个字段在内存里与主表归类映射
  779. 'b.ivouchrowno as row_no',
  780. 'b.cInvCode as code',
  781. 'b.dRequirDate as plan_date',
  782. 'b.fTaxPrice as price',
  783. 'b.iPerTaxRate as rate',
  784. 'i.cComUnitCode as unit_code',
  785. 'u.cComUnitName as unit',
  786. DB::raw("ISNULL(i.cInvName, '') as name"),
  787. DB::raw("ISNULL(i.cInvStd, '') as size"),
  788. DB::raw("ISNULL(b.fQuantity, 0) as req_qty"),
  789. DB::raw("ISNULL(b.iReceivedQTY, 0) as order_qty"),
  790. DB::raw("(ISNULL(b.fQuantity, 0) - ISNULL(b.iReceivedQTY, 0)) as available_qty")
  791. )
  792. ->orderBy('b.ivouchrowno', 'asc')
  793. ->get();
  794. // 转化为普通纯数组
  795. $detailList = json_decode(json_encode($details), true);
  796. // 5. 按 main_id 将明细数据分门别类组装成 Map [main_id => [明细数组]]
  797. $detailMap = [];
  798. foreach ($detailList as $detail) {
  799. $detail['price'] = floatval($detail['price']);
  800. $detail['rate'] = floatval($detail['rate']);
  801. $detail['req_qty'] = floatval($detail['req_qty']);
  802. $detail['order_qty'] = floatval($detail['order_qty']);
  803. $detail['available_qty'] = floatval($detail['available_qty']);
  804. $detailMap[$detail['main_id']][] = $detail;
  805. }
  806. // 6. 将明细 Map 塞回主表列表项的 detail 字段中
  807. foreach ($items as &$item) {
  808. $item->detail = $detailMap[$item->id] ?? [];
  809. }
  810. // 重写回原列表结构中
  811. $list['data'] = $items;
  812. return [true, $list];
  813. }
  814. public function purchaseRequisitionU8List1($data, $user){
  815. $order_date = $data['order_date'] ?? [];
  816. $order_date = array_filter($order_date);
  817. $code = $data['code'] ?? "";
  818. $model = $this->databaseService->table('PU_AppVouch as a')
  819. ->when(! empty($code), function ($query) use($code){
  820. return $query->where('a.cCode', 'LIKE', '%'.$code.'%');
  821. })
  822. ->when(! empty($order_date), function ($query) use($order_date){
  823. $start = date('Y-m-d 00:00:00.000', $order_date[0]);
  824. $end = date('Y-m-d 23:59:59.000', $order_date[1]);
  825. return $query->whereBetween('a.dDate', [$start, $end]);
  826. })
  827. ->where('a.iverifystateex', 2)
  828. ->whereExists(function ($query) {
  829. $query->select(DB::raw(1))
  830. ->from('PU_AppVouchs as b')
  831. ->whereRaw('b.ID = a.ID') // 主外键关联
  832. ->whereRaw('ISNULL(b.fQuantity, 0) > ISNULL(b.iReceivedQTY, 0)');
  833. })
  834. ->select(
  835. 'a.ID as id', // 必须把主表ID查出来,后续点击需要用来查子表
  836. DB::raw("ISNULL(a.cBusType, '') as business_type"),
  837. DB::raw("ISNULL(a.cCode, '') as order_number"),
  838. DB::raw("ISNULL(CONVERT(varchar(10), a.dDate, 120), '') as order_date"),
  839. DB::raw("ISNULL(a.cMaker, '') as crt_name")
  840. )
  841. ->orderBy('a.ID', 'desc');
  842. $list = $this->limit($model, '', $data);
  843. return [true, $list];
  844. }
  845. public function getRequisitionDetails($data){
  846. if (empty($data['id'])) return [false, '请购单主表ID不能为空'];
  847. $mainId = $data['id'];
  848. $details = $this->databaseService->table('PU_AppVouchs as b')
  849. ->leftJoin('Inventory as i', 'i.cInvCode', 'b.cInvCode') // 通常明细需要关联存货档案拿名称和规格
  850. ->leftJoin('ComputationUnit as u', 'u.cComUnitCode', 'i.cComUnitCode')
  851. ->where('b.ID', $mainId)
  852. ->whereRaw('ISNULL(b.fQuantity, 0) > ISNULL(b.iReceivedQTY, 0)')
  853. ->select(
  854. 'b.AutoID as detail_id', // 子表行单据唯一标识
  855. 'b.ID as main_id', // 主表ID
  856. 'b.ivouchrowno as row_no', // 行号
  857. 'b.cInvCode as code', // 存货编码
  858. 'b.dRequirDate as plan_date', // 需求日期 | 采购订单里的计划到货日期
  859. 'b.fTaxPrice as price', // 原币含税单价
  860. 'b.iPerTaxRate as rate', // 税率
  861. 'i.cComUnitCode as unit_code', // 主计量单位编码
  862. 'u.cComUnitName as unit', // 核心修改:主计量单位名称 (如: 个、千克、箱)
  863. DB::raw("ISNULL(i.cInvName, '') as name"), // 存货名称
  864. DB::raw("ISNULL(i.cInvStd, '') as size"), // 规格型号
  865. DB::raw("ISNULL(b.fQuantity, 0) as req_qty"), // 请购数量 (fQuantity)
  866. DB::raw("ISNULL(b.iReceivedQTY, 0) as order_qty"), // 累计订货数量 (iReceivedQTY)
  867. DB::raw("(ISNULL(b.fQuantity, 0) - ISNULL(b.iReceivedQTY, 0)) as available_qty") // 剩余数量
  868. )
  869. ->orderBy('b.ivouchrowno', 'asc')
  870. ->get();
  871. // 转化为普通数组返回
  872. $list = json_decode(json_encode($details), true);
  873. return [true, $list];
  874. }
  875. public function getRequisitionDetailsByCode($code = [])
  876. {
  877. if (empty($code)) return [false, '请购单号不能为空'];
  878. // 2. 执行 U8 数据库查询
  879. $list = $this->databaseService->table('PU_AppVouchs as b')
  880. ->join('PU_AppVouch as a', 'a.ID', 'b.ID')
  881. ->leftJoin('Inventory as i', 'i.cInvCode', 'b.cInvCode')
  882. ->whereIn('a.cCode', $code)
  883. // 过滤:只查出【请购数量 > 累计订货数量】即还未订货完的明细
  884. ->whereRaw('ISNULL(b.fQuantity, 0) > ISNULL(b.iReceivedQTY, 0)')
  885. ->select(
  886. 'a.cCode as cappcode', // 顺便把主表单号也查出来,方便前端知道这一行属于哪张请购单
  887. 'b.AutoID as detail_id', // 子表行单据唯一标识 (iAppIds)
  888. 'b.ID as main_id', // 主表ID (cappcodeId)
  889. 'b.ivouchrowno as row_no', // 行号
  890. 'b.cInvCode as material_code', // 存货编码
  891. 'b.dRequirDate as plan_date', // 需求日期 -> 对应采购订单的计划到货日期
  892. DB::raw("ISNULL(i.cInvName, '') as material_name"), // 存货名称
  893. DB::raw("ISNULL(i.cInvStd, '') as material_std"), // 规格型号
  894. DB::raw("ISNULL(b.fQuantity, 0) as req_qty"), // 请购数量
  895. DB::raw("ISNULL(b.iReceivedQTY, 0) as order_qty"), // 累计订货数量
  896. DB::raw("(ISNULL(b.fQuantity, 0) - ISNULL(b.iReceivedQTY, 0)) as available_qty") // 剩余可用数量
  897. )
  898. // 按照主表单号和子表行号升序排序,方便前端按单据分组展示
  899. ->orderBy('a.cCode', 'asc')
  900. ->orderBy('b.ivouchrowno', 'asc')
  901. ->get();
  902. // 3. 转化为普通数组返回
  903. $list = json_decode(json_encode($list), true);
  904. return [true, $list];
  905. }
  906. //获取u8采购订单
  907. /**
  908. * 获取 U8 采购订单列表(带存货明细 detail 字段)
  909. */
  910. public function purchaseOrderU8List($data, $user)
  911. {
  912. // 0 蓝单 1 红单
  913. if (!isset($data['bredvouch'])) return [false, '参照类型(蓝单|红单不能为空)'];
  914. $type = $data['bredvouch'];
  915. $order_date = $data['order_date'] ?? [];
  916. $order_date = array_filter($order_date);
  917. $code = $data['code'] ?? "";
  918. // 1. 构建主表查询 Model
  919. $model = $this->databaseService->table('PO_Pomain as a')
  920. ->leftJoin('Vendor as v', 'v.cVenCode', '=', 'a.cVenCode')
  921. ->when(!empty($code), function ($query) use ($code) {
  922. return $query->where('a.cPOID', 'LIKE', '%' . $code . '%');
  923. })
  924. ->when(!empty($order_date), function ($query) use ($order_date) {
  925. $start = date('Y-m-d 00:00:00.000', $order_date[0]);
  926. $end = date('Y-m-d 23:59:59.000', $order_date[1]);
  927. return $query->whereBetween('a.dPODate', [$start, $end]);
  928. })
  929. ->where('a.iverifystateex', 2) // 已审核
  930. ->whereExists(function ($query) use ($type) {
  931. if ($type == 0) {
  932. $query->select(DB::raw(1))
  933. ->from('PO_Podetails as b')
  934. ->whereRaw('b.POID = a.POID')
  935. ->whereRaw('ISNULL(b.iQuantity, 0) > ISNULL(b.iReceivedQTY, 0)');
  936. } elseif ($type == 1) {
  937. $query->select(DB::raw(1))
  938. ->from('PO_Podetails as b')
  939. ->whereRaw('b.POID = a.POID')
  940. ->whereRaw('ISNULL(b.iReceivedQTY, 0) > 0');
  941. }
  942. })
  943. ->select(
  944. 'a.POID as id', // 对应明细表的 b.POID
  945. DB::raw("ISNULL(a.cBusType, '') as business_type"),
  946. DB::raw("ISNULL(a.cVenCode, '') as supply_code"),
  947. DB::raw("ISNULL(v.cVenName, '') as supply_name"),
  948. DB::raw("ISNULL(a.cexch_name, '') as cexch_name"),
  949. DB::raw("ISNULL(a.nflat, '') as nflat"),
  950. DB::raw("ISNULL(a.iDiscountTaxType, '') as iDiscountTaxType"),
  951. DB::raw("ISNULL(a.cPOID, '') as order_number"),
  952. DB::raw("ISNULL(CONVERT(varchar(10), a.dPODate, 120), '') as order_date"),
  953. DB::raw("ISNULL(a.cMaker, '') as crt_name")
  954. )
  955. ->orderBy('a.POID', 'desc');
  956. // 2. 获取主表分页列表数据
  957. $list = $this->limit($model, '', $data);
  958. $items = $list['data'] ?? [];
  959. if (empty($items)) {
  960. return [true, $list];
  961. }
  962. // 3. 批量提取当前页所有主表 POID
  963. $mainIds = array_column($items, 'id');
  964. // 4. 一次性批量查出这些主表对应的所有子表存货明细(继承蓝单/红单过滤逻辑)
  965. $details = $this->databaseService->table('PO_Podetails as b')
  966. ->leftJoin('Inventory as i', 'i.cInvCode', 'b.cInvCode')
  967. ->leftJoin('ComputationUnit as u', 'u.cComUnitCode', 'i.cComUnitCode')
  968. ->whereIn('b.POID', $mainIds) // 批量范围锁定
  969. ->when(isset($type), function ($query) use ($type) {
  970. if ($type == 0) {
  971. return $query->whereRaw('ISNULL(b.iQuantity, 0) > ISNULL(b.iReceivedQTY, 0)');
  972. } elseif ($type == 1) {
  973. return $query->whereRaw('ISNULL(b.iReceivedQTY, 0) > 0');
  974. }
  975. })
  976. ->select(
  977. 'b.POID as main_id', // 用于在内存中与主表 id 映射
  978. 'b.ID as detail_id', // 子表行单据唯一标识
  979. 'b.ivouchrowno as row_no',
  980. 'b.cInvCode as code',
  981. 'b.iTaxPrice as price',
  982. 'b.iPerTaxRate as rate',
  983. 'i.cComUnitCode as unit_code',
  984. 'i.bInvBatch as pici', // 批次管理
  985. 'i.bInvQuality as baozhiqi', // 保质期管理
  986. 'u.cComUnitName as unit',
  987. DB::raw("ISNULL(i.cInvName, '') as name"),
  988. DB::raw("ISNULL(i.cInvStd, '') as size"),
  989. DB::raw("ISNULL(b.iQuantity, 0) as i_qty"),
  990. DB::raw("ISNULL(b.iReceivedQTY, 0) as in_qty"),
  991. DB::raw($type == 0
  992. ? "(ISNULL(b.iQuantity, 0) - ISNULL(b.iReceivedQTY, 0)) as available_qty"
  993. : "ISNULL(b.iReceivedQTY, 0) as available_qty"
  994. )
  995. )
  996. ->orderBy('b.ivouchrowno', 'asc')
  997. ->get();
  998. // 转化为普通纯数组
  999. $detailList = json_decode(json_encode($details), true);
  1000. // 5. 按 main_id 将明细数据归类到 Map 容器中
  1001. $detailMap = [];
  1002. foreach ($detailList as $detail) {
  1003. $detail['price'] = floatval($detail['price']);
  1004. $detail['rate'] = floatval($detail['rate']);
  1005. $detail['i_qty'] = floatval($detail['i_qty']);
  1006. $detail['in_qty'] = floatval($detail['in_qty']);
  1007. $detail['available_qty'] = floatval($detail['available_qty']);
  1008. $detailMap[$detail['main_id']][] = $detail;
  1009. }
  1010. // 6. 将明细 Map 塞回主表对应的列表项中
  1011. foreach ($items as &$item) {
  1012. $item->detail = $detailMap[$item->id] ?? [];
  1013. }
  1014. // 重写回原分页列表结构
  1015. $list['data'] = $items;
  1016. return [true, $list];
  1017. }
  1018. public function purchaseOrderU8List1($data, $user){
  1019. // 0 蓝单 1 红单
  1020. if(! isset($data['bredvouch'])) return [false, '参照类型(蓝单|红单不能为空)'];
  1021. $type = $data['bredvouch'];
  1022. $order_date = $data['order_date'] ?? [];
  1023. $order_date = array_filter($order_date);
  1024. $code = $data['code'] ?? "";
  1025. $model = $this->databaseService->table('PO_Pomain as a')
  1026. ->leftJoin('Vendor as v', 'v.cVenCode', '=', 'a.cVenCode')
  1027. ->when(! empty($code), function ($query) use($code){
  1028. return $query->where('a.cPOID', 'LIKE', '%'.$code.'%');
  1029. })
  1030. ->when(! empty($order_date), function ($query) use($order_date){
  1031. $start = date('Y-m-d 00:00:00.000', $order_date[0]);
  1032. $end = date('Y-m-d 23:59:59.000', $order_date[1]);
  1033. return $query->whereBetween('a.dPODate', [$start, $end]);
  1034. })
  1035. ->where('a.iverifystateex', 2)
  1036. ->whereExists(function ($query) use($type){
  1037. if($type == 0){
  1038. $query->select(DB::raw(1))
  1039. ->from('PO_Podetails as b')
  1040. ->whereRaw('b.POID = a.POID')
  1041. ->whereRaw('ISNULL(b.iQuantity, 0) > ISNULL(b.iReceivedQTY, 0)');
  1042. }elseif($type == 1){
  1043. $query->select(DB::raw(1))
  1044. ->from('PO_Podetails as b')
  1045. ->whereRaw('b.POID = a.POID')
  1046. ->whereRaw('ISNULL(b.iReceivedQTY, 0) > 0');
  1047. }
  1048. })
  1049. ->select(
  1050. 'a.POID as id',
  1051. DB::raw("ISNULL(a.cBusType, '') as business_type"),
  1052. DB::raw("ISNULL(a.cVenCode, '') as supply_code"),
  1053. DB::raw("ISNULL(v.cVenName, '') as supply_name"),
  1054. DB::raw("ISNULL(a.cexch_name, '') as cexch_name"),
  1055. DB::raw("ISNULL(a.nflat, '') as nflat"),
  1056. DB::raw("ISNULL(a.iDiscountTaxType, '') as iDiscountTaxType"),
  1057. DB::raw("ISNULL(a.cPOID, '') as order_number"),
  1058. DB::raw("ISNULL(CONVERT(varchar(10), a.dPODate, 120), '') as order_date"),
  1059. DB::raw("ISNULL(a.cMaker, '') as crt_name")
  1060. )
  1061. ->orderBy('a.ID', 'desc');
  1062. $list = $this->limit($model, '', $data);
  1063. return [true, $list];
  1064. }
  1065. // 供应商 业务类型 汇率 扣税类别 币种 相同才能一起选
  1066. // 只能同时选择供应商、币种、汇率、扣税类别、业务类型、流程模式相同的行! 这是用友的提示
  1067. // 获取u8采购订单明细
  1068. public function getPurchaseOrderDetails($data){
  1069. if (empty($data['id'])) return [false, '采购订单主表ID不能为空'];
  1070. // 0 蓝单 1 红单
  1071. if(! isset($data['bredvouch'])) return [false, '参照类型(蓝单|红单不能为空)'];
  1072. $type = $data['bredvouch'];
  1073. $mainId = $data['id'];
  1074. $list = $this->databaseService->table('PO_Podetails as b')
  1075. ->leftJoin('Inventory as i', 'i.cInvCode', 'b.cInvCode') // 通常明细需要关联存货档案拿名称和规格
  1076. ->leftJoin('ComputationUnit as u', 'u.cComUnitCode', 'i.cComUnitCode')
  1077. ->where('b.POID', $mainId)
  1078. ->when(!empty($type), function ($query) use($type){
  1079. if($type == 0){
  1080. return $query->whereRaw('ISNULL(b.iQuantity, 0) > ISNULL(b.iReceivedQTY, 0)');
  1081. }elseif($type == 1){
  1082. return $query->whereRaw('ISNULL(b.iReceivedQTY, 0) > 0');
  1083. }
  1084. })
  1085. ->select(
  1086. 'b.POID as main_id', // 主表ID
  1087. 'b.ID as detail_id', // 子表行单据唯一标识
  1088. 'b.ivouchrowno as row_no', // 行号
  1089. 'b.cInvCode as code', // 存货编码
  1090. 'b.iTaxPrice as price', // 原币含税单价
  1091. 'b.iPerTaxRate as rate', // 税率
  1092. 'i.cComUnitCode as unit_code', // 主计量单位编码
  1093. 'i.bInvBatch as pici', // 批次管理
  1094. 'i.bInvQuality as baozhiqi', // 保质期管理
  1095. 'u.cComUnitName as unit', // 核心修改:主计量单位名称 (如: 个、千克、箱)
  1096. DB::raw("ISNULL(i.cInvName, '') as name"), // 存货名称
  1097. DB::raw("ISNULL(i.cInvStd, '') as size"), // 规格型号
  1098. DB::raw("ISNULL(b.iQuantity, 0) as i_qty"), // 采购订单数量 (iQuantity)
  1099. DB::raw("ISNULL(b.iReceivedQTY, 0) as in_qty"), // 累计入库数量 (iReceivedQTY)
  1100. DB::raw("(ISNULL(b.iQuantity, 0) - ISNULL(b.iReceivedQTY, 0)) as available_qty") // 剩余可入库数量
  1101. )
  1102. ->orderBy('b.ivouchrowno', 'asc')
  1103. ->get();
  1104. // 转化为普通数组返回
  1105. // $list = json_decode(json_encode($list), true);
  1106. return [true, $list];
  1107. }
  1108. public function getPurchaseOrderDetailsByCode($code = [])
  1109. {
  1110. if (empty($code)) return [false, '采购订单号不能为空'];
  1111. // 执行 U8 数据库查询
  1112. $list = $this->databaseService->table('PO_Podetails as b')
  1113. ->join('PO_Pomain as a', 'a.POID', '=', 'b.POID')
  1114. ->leftJoin('Inventory as i', 'i.cInvCode', '=', 'b.cInvCode')
  1115. ->whereIn('a.cPOID', $code)
  1116. ->select(
  1117. 'a.cPOID as order_number',
  1118. 'b.ID as detail_id',
  1119. 'b.POID as main_id',
  1120. 'b.ivouchrowno as row_no',
  1121. 'b.cInvCode as cInvCode',
  1122. DB::raw("ISNULL(i.cInvName, '') as name"),
  1123. DB::raw("ISNULL(i.cInvStd, '') as size"),
  1124. DB::raw("ISNULL(i.cComUnitCode, '') as unit"),
  1125. DB::raw("ISNULL(b.iPerTaxRate, 0) as rate"),
  1126. DB::raw("ISNULL(b.iUnitPrice, 0) as price"),
  1127. DB::raw("ISNULL(b.iQuantity, 0) as po_qty"),
  1128. DB::raw("ISNULL(b.iReceivedQTY, 0) as received_qty"),
  1129. DB::raw("(ISNULL(b.iQuantity, 0) - ISNULL(b.iReceivedQTY, 0)) as available_qty"),
  1130. // ==================== 新增:主表强控制联动校验字段 ====================
  1131. DB::raw("ISNULL(a.cBusType, '') as cBusType"), // 业务类型
  1132. DB::raw("ISNULL(a.cVenCode, '') as cVenCode"), // 供应商编码
  1133. DB::raw("ISNULL(a.cexch_name, '人民币') as cexch_name"), // 币种
  1134. DB::raw("CAST(ISNULL(a.nflat, 1.0) AS DECIMAL(10,4)) as nflat"),// 汇率
  1135. DB::raw("ISNULL(a.iDiscountTaxType, 0) as iDiscountTaxType") // 扣税类别
  1136. )
  1137. ->orderBy('a.cPOID', 'asc')
  1138. ->orderBy('b.ivouchrowno', 'asc')
  1139. ->get();
  1140. // 转化为普通数组返回
  1141. $list = json_decode(json_encode($list), true);
  1142. if (empty($list)) {
  1143. return [false, '未查询到对应的采购订单明细数据'];
  1144. }
  1145. // ==================== 核心修改:多单合单严格一致性校验 ====================
  1146. if (count($code) > 1) {
  1147. // 1. 校验供应商
  1148. $venCodes = array_unique(array_column($list, 'cVenCode'));
  1149. if (count($venCodes) > 1) return [false, '只能同时选择【供应商】相同的采购订单进行合并入库!'];
  1150. // 2. 校验业务类型
  1151. $busTypes = array_unique(array_column($list, 'cBusType'));
  1152. if (count($busTypes) > 1) return [false, '只能同时选择【业务类型】相同的采购订单进行合并入库!'];
  1153. // 3. 校验币种
  1154. $exchNames = array_unique(array_column($list, 'cexch_name'));
  1155. if (count($exchNames) > 1) return [false, '只能同时选择【币种】相同的采购订单进行合并入库!'];
  1156. // 4. 校验汇率 (转成 float 排除数据库浮点数末尾 0 的干扰)
  1157. $nflats = array_unique(array_map('floatval', array_column($list, 'nflat')));
  1158. if (count($nflats) > 1) return [false, '只能同时选择【汇率】相同的采购订单进行合并入库!'];
  1159. // 5. 校验扣税类别
  1160. $taxTypes = array_unique(array_column($list, 'iDiscountTaxType'));
  1161. if (count($taxTypes) > 1) return [false, '只能同时选择【扣税类别】相同的采购订单进行合并入库!'];
  1162. }
  1163. // ====================================================================
  1164. return [true, $list];
  1165. }
  1166. }