U8ServerService.php 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313
  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. $detailMap[$detail['main_id']][] = $detail;
  800. }
  801. // 6. 将明细 Map 塞回主表列表项的 detail 字段中
  802. foreach ($items as &$item) {
  803. $item->detail = $detailMap[$item->id] ?? [];
  804. }
  805. // 重写回原列表结构中
  806. $list['data'] = $items;
  807. return [true, $list];
  808. }
  809. public function purchaseRequisitionU8List1($data, $user){
  810. $order_date = $data['order_date'] ?? [];
  811. $order_date = array_filter($order_date);
  812. $code = $data['code'] ?? "";
  813. $model = $this->databaseService->table('PU_AppVouch as a')
  814. ->when(! empty($code), function ($query) use($code){
  815. return $query->where('a.cCode', 'LIKE', '%'.$code.'%');
  816. })
  817. ->when(! empty($order_date), function ($query) use($order_date){
  818. $start = date('Y-m-d 00:00:00.000', $order_date[0]);
  819. $end = date('Y-m-d 23:59:59.000', $order_date[1]);
  820. return $query->whereBetween('a.dDate', [$start, $end]);
  821. })
  822. ->where('a.iverifystateex', 2)
  823. ->whereExists(function ($query) {
  824. $query->select(DB::raw(1))
  825. ->from('PU_AppVouchs as b')
  826. ->whereRaw('b.ID = a.ID') // 主外键关联
  827. ->whereRaw('ISNULL(b.fQuantity, 0) > ISNULL(b.iReceivedQTY, 0)');
  828. })
  829. ->select(
  830. 'a.ID as id', // 必须把主表ID查出来,后续点击需要用来查子表
  831. DB::raw("ISNULL(a.cBusType, '') as business_type"),
  832. DB::raw("ISNULL(a.cCode, '') as order_number"),
  833. DB::raw("ISNULL(CONVERT(varchar(10), a.dDate, 120), '') as order_date"),
  834. DB::raw("ISNULL(a.cMaker, '') as crt_name")
  835. )
  836. ->orderBy('a.ID', 'desc');
  837. $list = $this->limit($model, '', $data);
  838. return [true, $list];
  839. }
  840. public function getRequisitionDetails($data){
  841. if (empty($data['id'])) return [false, '请购单主表ID不能为空'];
  842. $mainId = $data['id'];
  843. $details = $this->databaseService->table('PU_AppVouchs as b')
  844. ->leftJoin('Inventory as i', 'i.cInvCode', 'b.cInvCode') // 通常明细需要关联存货档案拿名称和规格
  845. ->leftJoin('ComputationUnit as u', 'u.cComUnitCode', 'i.cComUnitCode')
  846. ->where('b.ID', $mainId)
  847. ->whereRaw('ISNULL(b.fQuantity, 0) > ISNULL(b.iReceivedQTY, 0)')
  848. ->select(
  849. 'b.AutoID as detail_id', // 子表行单据唯一标识
  850. 'b.ID as main_id', // 主表ID
  851. 'b.ivouchrowno as row_no', // 行号
  852. 'b.cInvCode as code', // 存货编码
  853. 'b.dRequirDate as plan_date', // 需求日期 | 采购订单里的计划到货日期
  854. 'b.fTaxPrice as price', // 原币含税单价
  855. 'b.iPerTaxRate as rate', // 税率
  856. 'i.cComUnitCode as unit_code', // 主计量单位编码
  857. 'u.cComUnitName as unit', // 核心修改:主计量单位名称 (如: 个、千克、箱)
  858. DB::raw("ISNULL(i.cInvName, '') as name"), // 存货名称
  859. DB::raw("ISNULL(i.cInvStd, '') as size"), // 规格型号
  860. DB::raw("ISNULL(b.fQuantity, 0) as req_qty"), // 请购数量 (fQuantity)
  861. DB::raw("ISNULL(b.iReceivedQTY, 0) as order_qty"), // 累计订货数量 (iReceivedQTY)
  862. DB::raw("(ISNULL(b.fQuantity, 0) - ISNULL(b.iReceivedQTY, 0)) as available_qty") // 剩余数量
  863. )
  864. ->orderBy('b.ivouchrowno', 'asc')
  865. ->get();
  866. // 转化为普通数组返回
  867. $list = json_decode(json_encode($details), true);
  868. return [true, $list];
  869. }
  870. public function getRequisitionDetailsByCode($code = [])
  871. {
  872. if (empty($code)) return [false, '请购单号不能为空'];
  873. // 2. 执行 U8 数据库查询
  874. $list = $this->databaseService->table('PU_AppVouchs as b')
  875. ->join('PU_AppVouch as a', 'a.ID', 'b.ID')
  876. ->leftJoin('Inventory as i', 'i.cInvCode', 'b.cInvCode')
  877. ->whereIn('a.cCode', $code)
  878. // 过滤:只查出【请购数量 > 累计订货数量】即还未订货完的明细
  879. ->whereRaw('ISNULL(b.fQuantity, 0) > ISNULL(b.iReceivedQTY, 0)')
  880. ->select(
  881. 'a.cCode as cappcode', // 顺便把主表单号也查出来,方便前端知道这一行属于哪张请购单
  882. 'b.AutoID as detail_id', // 子表行单据唯一标识 (iAppIds)
  883. 'b.ID as main_id', // 主表ID (cappcodeId)
  884. 'b.ivouchrowno as row_no', // 行号
  885. 'b.cInvCode as material_code', // 存货编码
  886. 'b.dRequirDate as plan_date', // 需求日期 -> 对应采购订单的计划到货日期
  887. DB::raw("ISNULL(i.cInvName, '') as material_name"), // 存货名称
  888. DB::raw("ISNULL(i.cInvStd, '') as material_std"), // 规格型号
  889. DB::raw("ISNULL(b.fQuantity, 0) as req_qty"), // 请购数量
  890. DB::raw("ISNULL(b.iReceivedQTY, 0) as order_qty"), // 累计订货数量
  891. DB::raw("(ISNULL(b.fQuantity, 0) - ISNULL(b.iReceivedQTY, 0)) as available_qty") // 剩余可用数量
  892. )
  893. // 按照主表单号和子表行号升序排序,方便前端按单据分组展示
  894. ->orderBy('a.cCode', 'asc')
  895. ->orderBy('b.ivouchrowno', 'asc')
  896. ->get();
  897. // 3. 转化为普通数组返回
  898. $list = json_decode(json_encode($list), true);
  899. return [true, $list];
  900. }
  901. //获取u8采购订单
  902. /**
  903. * 获取 U8 采购订单列表(带存货明细 detail 字段)
  904. */
  905. public function purchaseOrderU8List($data, $user)
  906. {
  907. // 0 蓝单 1 红单
  908. if (!isset($data['bredvouch'])) return [false, '参照类型(蓝单|红单不能为空)'];
  909. $type = $data['bredvouch'];
  910. $order_date = $data['order_date'] ?? [];
  911. $order_date = array_filter($order_date);
  912. $code = $data['code'] ?? "";
  913. // 1. 构建主表查询 Model
  914. $model = $this->databaseService->table('PO_Pomain as a')
  915. ->leftJoin('Vendor as v', 'v.cVenCode', '=', 'a.cVenCode')
  916. ->when(!empty($code), function ($query) use ($code) {
  917. return $query->where('a.cPOID', 'LIKE', '%' . $code . '%');
  918. })
  919. ->when(!empty($order_date), function ($query) use ($order_date) {
  920. $start = date('Y-m-d 00:00:00.000', $order_date[0]);
  921. $end = date('Y-m-d 23:59:59.000', $order_date[1]);
  922. return $query->whereBetween('a.dPODate', [$start, $end]);
  923. })
  924. ->where('a.iverifystateex', 2) // 已审核
  925. ->whereExists(function ($query) use ($type) {
  926. if ($type == 0) {
  927. $query->select(DB::raw(1))
  928. ->from('PO_Podetails as b')
  929. ->whereRaw('b.POID = a.POID')
  930. ->whereRaw('ISNULL(b.iQuantity, 0) > ISNULL(b.iReceivedQTY, 0)');
  931. } elseif ($type == 1) {
  932. $query->select(DB::raw(1))
  933. ->from('PO_Podetails as b')
  934. ->whereRaw('b.POID = a.POID')
  935. ->whereRaw('ISNULL(b.iReceivedQTY, 0) > 0');
  936. }
  937. })
  938. ->select(
  939. 'a.POID as id', // 对应明细表的 b.POID
  940. DB::raw("ISNULL(a.cBusType, '') as business_type"),
  941. DB::raw("ISNULL(a.cVenCode, '') as supply_code"),
  942. DB::raw("ISNULL(v.cVenName, '') as supply_name"),
  943. DB::raw("ISNULL(a.cexch_name, '') as cexch_name"),
  944. DB::raw("ISNULL(a.nflat, '') as nflat"),
  945. DB::raw("ISNULL(a.iDiscountTaxType, '') as iDiscountTaxType"),
  946. DB::raw("ISNULL(a.cPOID, '') as order_number"),
  947. DB::raw("ISNULL(CONVERT(varchar(10), a.dPODate, 120), '') as order_date"),
  948. DB::raw("ISNULL(a.cMaker, '') as crt_name")
  949. )
  950. ->orderBy('a.ID', 'desc');
  951. // 2. 获取主表分页列表数据
  952. $list = $this->limit($model, '', $data);
  953. $items = $list['data'] ?? [];
  954. if (empty($items)) {
  955. return [true, $list];
  956. }
  957. // 3. 批量提取当前页所有主表 POID
  958. $mainIds = array_column($items, 'id');
  959. // 4. 一次性批量查出这些主表对应的所有子表存货明细(继承蓝单/红单过滤逻辑)
  960. $details = $this->databaseService->table('PO_Podetails as b')
  961. ->leftJoin('Inventory as i', 'i.cInvCode', 'b.cInvCode')
  962. ->leftJoin('ComputationUnit as u', 'u.cComUnitCode', 'i.cComUnitCode')
  963. ->whereIn('b.POID', $mainIds) // 批量范围锁定
  964. ->when(isset($type), function ($query) use ($type) {
  965. if ($type == 0) {
  966. return $query->whereRaw('ISNULL(b.iQuantity, 0) > ISNULL(b.iReceivedQTY, 0)');
  967. } elseif ($type == 1) {
  968. return $query->whereRaw('ISNULL(b.iReceivedQTY, 0) > 0');
  969. }
  970. })
  971. ->select(
  972. 'b.POID as main_id', // 用于在内存中与主表 id 映射
  973. 'b.ID as detail_id', // 子表行单据唯一标识
  974. 'b.ivouchrowno as row_no',
  975. 'b.cInvCode as code',
  976. 'b.iTaxPrice as price',
  977. 'b.iPerTaxRate as rate',
  978. 'i.cComUnitCode as unit_code',
  979. 'i.bInvBatch as pici', // 批次管理
  980. 'i.bInvQuality as baozhiqi', // 保质期管理
  981. 'u.cComUnitName as unit',
  982. DB::raw("ISNULL(i.cInvName, '') as name"),
  983. DB::raw("ISNULL(i.cInvStd, '') as size"),
  984. DB::raw("ISNULL(b.iQuantity, 0) as i_qty"),
  985. DB::raw("ISNULL(b.iReceivedQTY, 0) as in_qty"),
  986. DB::raw("(ISNULL(b.iQuantity, 0) - ISNULL(b.iReceivedQTY, 0)) as available_qty")
  987. )
  988. ->orderBy('b.ivouchrowno', 'asc')
  989. ->get();
  990. // 转化为普通纯数组
  991. $detailList = json_decode(json_encode($details), true);
  992. // 5. 按 main_id 将明细数据归类到 Map 容器中
  993. $detailMap = [];
  994. foreach ($detailList as $detail) {
  995. $detailMap[$detail['main_id']][] = $detail;
  996. }
  997. // 6. 将明细 Map 塞回主表对应的列表项中
  998. foreach ($items as &$item) {
  999. $item->detail = $detailMap[$item->id] ?? [];
  1000. }
  1001. // 重写回原分页列表结构
  1002. $list['data'] = $items;
  1003. return [true, $list];
  1004. }
  1005. public function purchaseOrderU8List1($data, $user){
  1006. // 0 蓝单 1 红单
  1007. if(! isset($data['bredvouch'])) return [false, '参照类型(蓝单|红单不能为空)'];
  1008. $type = $data['bredvouch'];
  1009. $order_date = $data['order_date'] ?? [];
  1010. $order_date = array_filter($order_date);
  1011. $code = $data['code'] ?? "";
  1012. $model = $this->databaseService->table('PO_Pomain as a')
  1013. ->leftJoin('Vendor as v', 'v.cVenCode', '=', 'a.cVenCode')
  1014. ->when(! empty($code), function ($query) use($code){
  1015. return $query->where('a.cPOID', 'LIKE', '%'.$code.'%');
  1016. })
  1017. ->when(! empty($order_date), function ($query) use($order_date){
  1018. $start = date('Y-m-d 00:00:00.000', $order_date[0]);
  1019. $end = date('Y-m-d 23:59:59.000', $order_date[1]);
  1020. return $query->whereBetween('a.dPODate', [$start, $end]);
  1021. })
  1022. ->where('a.iverifystateex', 2)
  1023. ->whereExists(function ($query) use($type){
  1024. if($type == 0){
  1025. $query->select(DB::raw(1))
  1026. ->from('PO_Podetails as b')
  1027. ->whereRaw('b.POID = a.POID')
  1028. ->whereRaw('ISNULL(b.iQuantity, 0) > ISNULL(b.iReceivedQTY, 0)');
  1029. }elseif($type == 1){
  1030. $query->select(DB::raw(1))
  1031. ->from('PO_Podetails as b')
  1032. ->whereRaw('b.POID = a.POID')
  1033. ->whereRaw('ISNULL(b.iReceivedQTY, 0) > 0');
  1034. }
  1035. })
  1036. ->select(
  1037. 'a.POID as id',
  1038. DB::raw("ISNULL(a.cBusType, '') as business_type"),
  1039. DB::raw("ISNULL(a.cVenCode, '') as supply_code"),
  1040. DB::raw("ISNULL(v.cVenName, '') as supply_name"),
  1041. DB::raw("ISNULL(a.cexch_name, '') as cexch_name"),
  1042. DB::raw("ISNULL(a.nflat, '') as nflat"),
  1043. DB::raw("ISNULL(a.iDiscountTaxType, '') as iDiscountTaxType"),
  1044. DB::raw("ISNULL(a.cPOID, '') as order_number"),
  1045. DB::raw("ISNULL(CONVERT(varchar(10), a.dPODate, 120), '') as order_date"),
  1046. DB::raw("ISNULL(a.cMaker, '') as crt_name")
  1047. )
  1048. ->orderBy('a.ID', 'desc');
  1049. $list = $this->limit($model, '', $data);
  1050. return [true, $list];
  1051. }
  1052. // 供应商 业务类型 汇率 扣税类别 币种 相同才能一起选
  1053. // 只能同时选择供应商、币种、汇率、扣税类别、业务类型、流程模式相同的行! 这是用友的提示
  1054. // 获取u8采购订单明细
  1055. public function getPurchaseOrderDetails($data){
  1056. if (empty($data['id'])) return [false, '采购订单主表ID不能为空'];
  1057. // 0 蓝单 1 红单
  1058. if(! isset($data['bredvouch'])) return [false, '参照类型(蓝单|红单不能为空)'];
  1059. $type = $data['bredvouch'];
  1060. $mainId = $data['id'];
  1061. $list = $this->databaseService->table('PO_Podetails as b')
  1062. ->leftJoin('Inventory as i', 'i.cInvCode', 'b.cInvCode') // 通常明细需要关联存货档案拿名称和规格
  1063. ->leftJoin('ComputationUnit as u', 'u.cComUnitCode', 'i.cComUnitCode')
  1064. ->where('b.POID', $mainId)
  1065. ->when(!empty($type), function ($query) use($type){
  1066. if($type == 0){
  1067. return $query->whereRaw('ISNULL(b.iQuantity, 0) > ISNULL(b.iReceivedQTY, 0)');
  1068. }elseif($type == 1){
  1069. return $query->whereRaw('ISNULL(b.iReceivedQTY, 0) > 0');
  1070. }
  1071. })
  1072. ->select(
  1073. 'b.POID as main_id', // 主表ID
  1074. 'b.ID as detail_id', // 子表行单据唯一标识
  1075. 'b.ivouchrowno as row_no', // 行号
  1076. 'b.cInvCode as code', // 存货编码
  1077. 'b.iTaxPrice as price', // 原币含税单价
  1078. 'b.iPerTaxRate as rate', // 税率
  1079. 'i.cComUnitCode as unit_code', // 主计量单位编码
  1080. 'i.bInvBatch as pici', // 批次管理
  1081. 'i.bInvQuality as baozhiqi', // 保质期管理
  1082. 'u.cComUnitName as unit', // 核心修改:主计量单位名称 (如: 个、千克、箱)
  1083. DB::raw("ISNULL(i.cInvName, '') as name"), // 存货名称
  1084. DB::raw("ISNULL(i.cInvStd, '') as size"), // 规格型号
  1085. DB::raw("ISNULL(b.iQuantity, 0) as i_qty"), // 采购订单数量 (iQuantity)
  1086. DB::raw("ISNULL(b.iReceivedQTY, 0) as in_qty"), // 累计入库数量 (iReceivedQTY)
  1087. DB::raw("(ISNULL(b.iQuantity, 0) - ISNULL(b.iReceivedQTY, 0)) as available_qty") // 剩余可入库数量
  1088. )
  1089. ->orderBy('b.ivouchrowno', 'asc')
  1090. ->get();
  1091. // 转化为普通数组返回
  1092. // $list = json_decode(json_encode($list), true);
  1093. return [true, $list];
  1094. }
  1095. public function getPurchaseOrderDetailsByCode($code = [])
  1096. {
  1097. if (empty($code)) return [false, '采购订单号不能为空'];
  1098. // 执行 U8 数据库查询
  1099. $list = $this->databaseService->table('PO_Podetails as b')
  1100. ->join('PO_Pomain as a', 'a.POID', '=', 'b.POID')
  1101. ->leftJoin('Inventory as i', 'i.cInvCode', '=', 'b.cInvCode')
  1102. ->whereIn('a.cPOID', $code)
  1103. ->select(
  1104. 'a.cPOID as order_number',
  1105. 'b.ID as detail_id',
  1106. 'b.POID as main_id',
  1107. 'b.ivouchrowno as row_no',
  1108. 'b.cInvCode as cInvCode',
  1109. DB::raw("ISNULL(i.cInvName, '') as name"),
  1110. DB::raw("ISNULL(i.cInvStd, '') as size"),
  1111. DB::raw("ISNULL(i.cComUnitCode, '') as unit"),
  1112. DB::raw("ISNULL(b.iPerTaxRate, 0) as rate"),
  1113. DB::raw("ISNULL(b.iUnitPrice, 0) as price"),
  1114. DB::raw("ISNULL(b.iQuantity, 0) as po_qty"),
  1115. DB::raw("ISNULL(b.iReceivedQTY, 0) as received_qty"),
  1116. DB::raw("(ISNULL(b.iQuantity, 0) - ISNULL(b.iReceivedQTY, 0)) as available_qty"),
  1117. // ==================== 新增:主表强控制联动校验字段 ====================
  1118. DB::raw("ISNULL(a.cBusType, '') as cBusType"), // 业务类型
  1119. DB::raw("ISNULL(a.cVenCode, '') as cVenCode"), // 供应商编码
  1120. DB::raw("ISNULL(a.cexch_name, '人民币') as cexch_name"), // 币种
  1121. DB::raw("CAST(ISNULL(a.nflat, 1.0) AS DECIMAL(10,4)) as nflat"),// 汇率
  1122. DB::raw("ISNULL(a.iDiscountTaxType, 0) as iDiscountTaxType") // 扣税类别
  1123. )
  1124. ->orderBy('a.cPOID', 'asc')
  1125. ->orderBy('b.ivouchrowno', 'asc')
  1126. ->get();
  1127. // 转化为普通数组返回
  1128. $list = json_decode(json_encode($list), true);
  1129. if (empty($list)) {
  1130. return [false, '未查询到对应的采购订单明细数据'];
  1131. }
  1132. // ==================== 核心修改:多单合单严格一致性校验 ====================
  1133. if (count($code) > 1) {
  1134. // 1. 校验供应商
  1135. $venCodes = array_unique(array_column($list, 'cVenCode'));
  1136. if (count($venCodes) > 1) return [false, '只能同时选择【供应商】相同的采购订单进行合并入库!'];
  1137. // 2. 校验业务类型
  1138. $busTypes = array_unique(array_column($list, 'cBusType'));
  1139. if (count($busTypes) > 1) return [false, '只能同时选择【业务类型】相同的采购订单进行合并入库!'];
  1140. // 3. 校验币种
  1141. $exchNames = array_unique(array_column($list, 'cexch_name'));
  1142. if (count($exchNames) > 1) return [false, '只能同时选择【币种】相同的采购订单进行合并入库!'];
  1143. // 4. 校验汇率 (转成 float 排除数据库浮点数末尾 0 的干扰)
  1144. $nflats = array_unique(array_map('floatval', array_column($list, 'nflat')));
  1145. if (count($nflats) > 1) return [false, '只能同时选择【汇率】相同的采购订单进行合并入库!'];
  1146. // 5. 校验扣税类别
  1147. $taxTypes = array_unique(array_column($list, 'iDiscountTaxType'));
  1148. if (count($taxTypes) > 1) return [false, '只能同时选择【扣税类别】相同的采购订单进行合并入库!'];
  1149. }
  1150. // ====================================================================
  1151. return [true, $list];
  1152. }
  1153. }