U8ServerService.php 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398
  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. // 部门档案(树形结构返回)
  728. public function departmentU8List($data, $user)
  729. {
  730. // 1. 从 U8 数据库中获取所有启用的部门(按编码升序)
  731. $list = $this->databaseService->table('Department as D')
  732. ->select(
  733. 'D.cDepCode as code', // 部门编码
  734. 'D.cDepName as name' // 部门名称
  735. )
  736. ->orderBy('D.cDepCode', 'ASC')
  737. ->get();
  738. // 转换为标准数组
  739. $departments = json_decode(json_encode($list), true);
  740. if (empty($departments)) {
  741. return [true, []];
  742. }
  743. // 2. 构建树形结构
  744. $tree = $this->buildDepartmentTree($departments);
  745. return [true, $tree];
  746. }
  747. /**
  748. * 递归/双指针法构建部门树
  749. */
  750. private function buildDepartmentTree(array $departments)
  751. {
  752. $tree = [];
  753. $referMap = [];
  754. // 将所有节点放入临时 Map 中,方便通过 Code 快速查找,并初始化 children 容器
  755. foreach ($departments as $dep) {
  756. $dep['children'] = [];
  757. $referMap[$dep['code']] = $dep;
  758. }
  759. foreach ($referMap as $code => &$node) {
  760. // 寻找当前部门的父级编码
  761. $parentCode = $this->getDParentCode($code, $referMap);
  762. if ($parentCode && isset($referMap[$parentCode])) {
  763. // 如果找到了父级,就将当前节点挂载到父级的 children 下
  764. $referMap[$parentCode]['children'][] = &$node;
  765. } else {
  766. // 如果没有父级(代表是一级部门),直接放入根节点数组中
  767. $tree[] = &$node;
  768. }
  769. }
  770. return $tree;
  771. }
  772. private function getDParentCode($code, $referMap)
  773. {
  774. $len = strlen($code);
  775. if ($len <= 1) return null;
  776. // 从当前编码的倒数第一位开始向前裁剪,寻找第一个在 Map 中存在的上级编码
  777. for ($i = $len - 1; $i > 0; $i--) {
  778. $parentCandidate = substr($code, 0, $i);
  779. if (isset($referMap[$parentCandidate])) {
  780. return $parentCandidate;
  781. }
  782. }
  783. return null;
  784. }
  785. //获取u8请购单
  786. /**
  787. * 获取 U8 请购单列表(带存货明细 detail 字段)
  788. */
  789. public function purchaseRequisitionU8List($data, $user)
  790. {
  791. $order_date = $data['order_date'] ?? [];
  792. $order_date = array_filter($order_date);
  793. $code = $data['code'] ?? "";
  794. // 1. 构建主表查询 Model
  795. $model = $this->databaseService->table('PU_AppVouch as a')
  796. ->when(!empty($code), function ($query) use ($code) {
  797. return $query->where('a.cCode', 'LIKE', '%' . $code . '%');
  798. })
  799. ->when(!empty($order_date), function ($query) use ($order_date) {
  800. $start = date('Y-m-d 00:00:00.000', $order_date[0]);
  801. $end = date('Y-m-d 23:59:59.000', $order_date[1]);
  802. return $query->whereBetween('a.dDate', [$start, $end]);
  803. })
  804. ->where('a.iverifystateex', 2) // 已审核状态
  805. ->whereExists(function ($query) {
  806. $query->select(DB::raw(1))
  807. ->from('PU_AppVouchs as b')
  808. ->whereRaw('b.ID = a.ID')
  809. ->whereRaw('ISNULL(b.fQuantity, 0) > ISNULL(b.iReceivedQTY, 0)');
  810. })
  811. ->select(
  812. 'a.ID as id', // 必须查出主表 ID 用来后续关联子表
  813. DB::raw("ISNULL(a.cBusType, '') as business_type"),
  814. DB::raw("ISNULL(a.cCode, '') as order_number"),
  815. DB::raw("ISNULL(CONVERT(varchar(10), a.dDate, 120), '') as order_date"),
  816. DB::raw("ISNULL(a.cMaker, '') as crt_name")
  817. )
  818. ->orderBy('a.ID', 'desc');
  819. // 2. 获取主表分页列表数据
  820. $list = $this->limit($model, '', $data);
  821. // 如果列表为空,直接返回
  822. $items = $list['data'] ?? [];
  823. if (empty($items)) {
  824. return [true, $list];
  825. }
  826. // 3. 【核心结合】批量提取当前页所有主表的 ID
  827. $mainIds = array_column($items, 'id');
  828. // 4. 一次性批量查出这些主表对应的所有存货明细
  829. $details = $this->databaseService->table('PU_AppVouchs as b')
  830. ->leftJoin('Inventory as i', 'i.cInvCode', 'b.cInvCode')
  831. ->leftJoin('ComputationUnit as u', 'u.cComUnitCode', 'i.cComUnitCode')
  832. ->whereIn('b.ID', $mainIds) // 批量范围查询
  833. ->whereRaw('ISNULL(b.fQuantity, 0) > ISNULL(b.iReceivedQTY, 0)')
  834. ->select(
  835. 'b.AutoID as detail_id',
  836. 'b.ID as main_id', // 核心:用这个字段在内存里与主表归类映射
  837. 'b.ivouchrowno as row_no',
  838. 'b.cInvCode as code',
  839. 'b.dRequirDate as plan_date',
  840. 'b.fTaxPrice as price',
  841. 'b.iPerTaxRate as rate',
  842. 'i.cComUnitCode as unit_code',
  843. 'u.cComUnitName as unit',
  844. DB::raw("ISNULL(i.cInvName, '') as name"),
  845. DB::raw("ISNULL(i.cInvStd, '') as size"),
  846. DB::raw("ISNULL(b.fQuantity, 0) as req_qty"),
  847. DB::raw("ISNULL(b.iReceivedQTY, 0) as order_qty"),
  848. DB::raw("(ISNULL(b.fQuantity, 0) - ISNULL(b.iReceivedQTY, 0)) as available_qty")
  849. )
  850. ->orderBy('b.ivouchrowno', 'asc')
  851. ->get();
  852. // 转化为普通纯数组
  853. $detailList = json_decode(json_encode($details), true);
  854. // 5. 按 main_id 将明细数据分门别类组装成 Map [main_id => [明细数组]]
  855. $detailMap = [];
  856. foreach ($detailList as $detail) {
  857. $detail['price'] = floatval($detail['price']);
  858. $detail['rate'] = floatval($detail['rate']);
  859. $detail['req_qty'] = floatval($detail['req_qty']);
  860. $detail['order_qty'] = floatval($detail['order_qty']);
  861. $detail['available_qty'] = floatval($detail['available_qty']);
  862. $detailMap[$detail['main_id']][] = $detail;
  863. }
  864. // 6. 将明细 Map 塞回主表列表项的 detail 字段中
  865. foreach ($items as &$item) {
  866. $item->detail = $detailMap[$item->id] ?? [];
  867. }
  868. // 重写回原列表结构中
  869. $list['data'] = $items;
  870. return [true, $list];
  871. }
  872. public function purchaseRequisitionU8List1($data, $user){
  873. $order_date = $data['order_date'] ?? [];
  874. $order_date = array_filter($order_date);
  875. $code = $data['code'] ?? "";
  876. $model = $this->databaseService->table('PU_AppVouch as a')
  877. ->when(! empty($code), function ($query) use($code){
  878. return $query->where('a.cCode', 'LIKE', '%'.$code.'%');
  879. })
  880. ->when(! empty($order_date), function ($query) use($order_date){
  881. $start = date('Y-m-d 00:00:00.000', $order_date[0]);
  882. $end = date('Y-m-d 23:59:59.000', $order_date[1]);
  883. return $query->whereBetween('a.dDate', [$start, $end]);
  884. })
  885. ->where('a.iverifystateex', 2)
  886. ->whereExists(function ($query) {
  887. $query->select(DB::raw(1))
  888. ->from('PU_AppVouchs as b')
  889. ->whereRaw('b.ID = a.ID') // 主外键关联
  890. ->whereRaw('ISNULL(b.fQuantity, 0) > ISNULL(b.iReceivedQTY, 0)');
  891. })
  892. ->select(
  893. 'a.ID as id', // 必须把主表ID查出来,后续点击需要用来查子表
  894. DB::raw("ISNULL(a.cBusType, '') as business_type"),
  895. DB::raw("ISNULL(a.cCode, '') as order_number"),
  896. DB::raw("ISNULL(CONVERT(varchar(10), a.dDate, 120), '') as order_date"),
  897. DB::raw("ISNULL(a.cMaker, '') as crt_name")
  898. )
  899. ->orderBy('a.ID', 'desc');
  900. $list = $this->limit($model, '', $data);
  901. return [true, $list];
  902. }
  903. public function getRequisitionDetails($data){
  904. if (empty($data['id'])) return [false, '请购单主表ID不能为空'];
  905. $mainId = $data['id'];
  906. $details = $this->databaseService->table('PU_AppVouchs as b')
  907. ->leftJoin('Inventory as i', 'i.cInvCode', 'b.cInvCode') // 通常明细需要关联存货档案拿名称和规格
  908. ->leftJoin('ComputationUnit as u', 'u.cComUnitCode', 'i.cComUnitCode')
  909. ->where('b.ID', $mainId)
  910. ->whereRaw('ISNULL(b.fQuantity, 0) > ISNULL(b.iReceivedQTY, 0)')
  911. ->select(
  912. 'b.AutoID as detail_id', // 子表行单据唯一标识
  913. 'b.ID as main_id', // 主表ID
  914. 'b.ivouchrowno as row_no', // 行号
  915. 'b.cInvCode as code', // 存货编码
  916. 'b.dRequirDate as plan_date', // 需求日期 | 采购订单里的计划到货日期
  917. 'b.fTaxPrice as price', // 原币含税单价
  918. 'b.iPerTaxRate as rate', // 税率
  919. 'i.cComUnitCode as unit_code', // 主计量单位编码
  920. 'u.cComUnitName as unit', // 核心修改:主计量单位名称 (如: 个、千克、箱)
  921. DB::raw("ISNULL(i.cInvName, '') as name"), // 存货名称
  922. DB::raw("ISNULL(i.cInvStd, '') as size"), // 规格型号
  923. DB::raw("ISNULL(b.fQuantity, 0) as req_qty"), // 请购数量 (fQuantity)
  924. DB::raw("ISNULL(b.iReceivedQTY, 0) as order_qty"), // 累计订货数量 (iReceivedQTY)
  925. DB::raw("(ISNULL(b.fQuantity, 0) - ISNULL(b.iReceivedQTY, 0)) as available_qty") // 剩余数量
  926. )
  927. ->orderBy('b.ivouchrowno', 'asc')
  928. ->get();
  929. // 转化为普通数组返回
  930. $list = json_decode(json_encode($details), true);
  931. return [true, $list];
  932. }
  933. public function getRequisitionDetailsByCode($code = [])
  934. {
  935. if (empty($code)) return [false, '请购单号不能为空'];
  936. // 2. 执行 U8 数据库查询
  937. $list = $this->databaseService->table('PU_AppVouchs as b')
  938. ->join('PU_AppVouch as a', 'a.ID', 'b.ID')
  939. ->leftJoin('Inventory as i', 'i.cInvCode', 'b.cInvCode')
  940. ->whereIn('a.cCode', $code)
  941. // 过滤:只查出【请购数量 > 累计订货数量】即还未订货完的明细
  942. ->whereRaw('ISNULL(b.fQuantity, 0) > ISNULL(b.iReceivedQTY, 0)')
  943. ->select(
  944. 'a.cCode as cappcode', // 顺便把主表单号也查出来,方便前端知道这一行属于哪张请购单
  945. 'b.AutoID as detail_id', // 子表行单据唯一标识 (iAppIds)
  946. 'b.ID as main_id', // 主表ID (cappcodeId)
  947. 'b.ivouchrowno as row_no', // 行号
  948. 'b.cInvCode as material_code', // 存货编码
  949. 'b.dRequirDate as plan_date', // 需求日期 -> 对应采购订单的计划到货日期
  950. DB::raw("ISNULL(i.cInvName, '') as material_name"), // 存货名称
  951. DB::raw("ISNULL(i.cInvStd, '') as material_std"), // 规格型号
  952. DB::raw("ISNULL(b.fQuantity, 0) as req_qty"), // 请购数量
  953. DB::raw("ISNULL(b.iReceivedQTY, 0) as order_qty"), // 累计订货数量
  954. DB::raw("(ISNULL(b.fQuantity, 0) - ISNULL(b.iReceivedQTY, 0)) as available_qty") // 剩余可用数量
  955. )
  956. // 按照主表单号和子表行号升序排序,方便前端按单据分组展示
  957. ->orderBy('a.cCode', 'asc')
  958. ->orderBy('b.ivouchrowno', 'asc')
  959. ->get();
  960. // 3. 转化为普通数组返回
  961. $list = json_decode(json_encode($list), true);
  962. return [true, $list];
  963. }
  964. //获取u8采购订单
  965. /**
  966. * 获取 U8 采购订单列表(带存货明细 detail 字段)
  967. */
  968. public function purchaseOrderU8List($data, $user)
  969. {
  970. // 0 蓝单 1 红单
  971. if (!isset($data['bredvouch'])) return [false, '参照类型(蓝单|红单不能为空)'];
  972. $type = $data['bredvouch'];
  973. $order_date = $data['order_date'] ?? [];
  974. $order_date = array_filter($order_date);
  975. $code = $data['code'] ?? "";
  976. // 1. 构建主表查询 Model
  977. $model = $this->databaseService->table('PO_Pomain as a')
  978. ->leftJoin('Vendor as v', 'v.cVenCode', '=', 'a.cVenCode')
  979. ->when(!empty($code), function ($query) use ($code) {
  980. return $query->where('a.cPOID', 'LIKE', '%' . $code . '%');
  981. })
  982. ->when(!empty($order_date), function ($query) use ($order_date) {
  983. $start = date('Y-m-d 00:00:00.000', $order_date[0]);
  984. $end = date('Y-m-d 23:59:59.000', $order_date[1]);
  985. return $query->whereBetween('a.dPODate', [$start, $end]);
  986. })
  987. ->where('a.iverifystateex', 2) // 已审核
  988. ->whereExists(function ($query) use ($type) {
  989. if ($type == 0) {
  990. $query->select(DB::raw(1))
  991. ->from('PO_Podetails as b')
  992. ->whereRaw('b.POID = a.POID')
  993. ->whereRaw('ISNULL(b.iQuantity, 0) > ISNULL(b.iReceivedQTY, 0)');
  994. } elseif ($type == 1) {
  995. $query->select(DB::raw(1))
  996. ->from('PO_Podetails as b')
  997. ->whereRaw('b.POID = a.POID')
  998. ->whereRaw('ISNULL(b.iReceivedQTY, 0) > 0');
  999. }
  1000. })
  1001. ->select(
  1002. 'a.POID as id', // 对应明细表的 b.POID
  1003. DB::raw("ISNULL(a.cBusType, '') as business_type"),
  1004. DB::raw("ISNULL(a.cVenCode, '') as supply_code"),
  1005. DB::raw("ISNULL(v.cVenName, '') as supply_name"),
  1006. DB::raw("ISNULL(a.cexch_name, '') as cexch_name"),
  1007. DB::raw("ISNULL(a.nflat, '') as nflat"),
  1008. DB::raw("ISNULL(a.iDiscountTaxType, '') as iDiscountTaxType"),
  1009. DB::raw("ISNULL(a.cPOID, '') as order_number"),
  1010. DB::raw("ISNULL(CONVERT(varchar(10), a.dPODate, 120), '') as order_date"),
  1011. DB::raw("ISNULL(a.cMaker, '') as crt_name")
  1012. )
  1013. ->orderBy('a.POID', 'desc');
  1014. // 2. 获取主表分页列表数据
  1015. $list = $this->limit($model, '', $data);
  1016. $items = $list['data'] ?? [];
  1017. if (empty($items)) {
  1018. return [true, $list];
  1019. }
  1020. // 3. 批量提取当前页所有主表 POID
  1021. $mainIds = array_column($items, 'id');
  1022. // 4. 一次性批量查出这些主表对应的所有子表存货明细(继承蓝单/红单过滤逻辑)
  1023. $details = $this->databaseService->table('PO_Podetails as b')
  1024. ->leftJoin('Inventory as i', 'i.cInvCode', 'b.cInvCode')
  1025. ->leftJoin('ComputationUnit as u', 'u.cComUnitCode', 'i.cComUnitCode')
  1026. ->whereIn('b.POID', $mainIds) // 批量范围锁定
  1027. ->when(isset($type), function ($query) use ($type) {
  1028. if ($type == 0) {
  1029. return $query->whereRaw('ISNULL(b.iQuantity, 0) > ISNULL(b.iReceivedQTY, 0)');
  1030. } elseif ($type == 1) {
  1031. return $query->whereRaw('ISNULL(b.iReceivedQTY, 0) > 0');
  1032. }
  1033. })
  1034. ->select(
  1035. 'b.POID as main_id', // 用于在内存中与主表 id 映射
  1036. 'b.ID as detail_id', // 子表行单据唯一标识
  1037. 'b.ivouchrowno as row_no',
  1038. 'b.cInvCode as code',
  1039. 'b.iTaxPrice as price',
  1040. 'b.iPerTaxRate as rate',
  1041. 'i.cComUnitCode as unit_code',
  1042. 'i.bInvBatch as pici', // 批次管理
  1043. 'i.bInvQuality as baozhiqi', // 保质期管理
  1044. 'u.cComUnitName as unit',
  1045. DB::raw("ISNULL(i.cInvName, '') as name"),
  1046. DB::raw("ISNULL(i.cInvStd, '') as size"),
  1047. DB::raw("ISNULL(b.iQuantity, 0) as i_qty"),
  1048. DB::raw("ISNULL(b.iReceivedQTY, 0) as in_qty"),
  1049. DB::raw($type == 0
  1050. ? "(ISNULL(b.iQuantity, 0) - ISNULL(b.iReceivedQTY, 0)) as available_qty"
  1051. : "ISNULL(b.iReceivedQTY, 0) as available_qty"
  1052. )
  1053. )
  1054. ->orderBy('b.ivouchrowno', 'asc')
  1055. ->get();
  1056. // 转化为普通纯数组
  1057. $detailList = json_decode(json_encode($details), true);
  1058. // 5. 按 main_id 将明细数据归类到 Map 容器中
  1059. $detailMap = [];
  1060. foreach ($detailList as $detail) {
  1061. $detail['price'] = floatval($detail['price']);
  1062. $detail['rate'] = floatval($detail['rate']);
  1063. $detail['i_qty'] = floatval($detail['i_qty']);
  1064. $detail['in_qty'] = floatval($detail['in_qty']);
  1065. $detail['available_qty'] = floatval($detail['available_qty']);
  1066. $detailMap[$detail['main_id']][] = $detail;
  1067. }
  1068. // 6. 将明细 Map 塞回主表对应的列表项中
  1069. foreach ($items as &$item) {
  1070. $item->detail = $detailMap[$item->id] ?? [];
  1071. }
  1072. // 重写回原分页列表结构
  1073. $list['data'] = $items;
  1074. return [true, $list];
  1075. }
  1076. public function purchaseOrderU8List1($data, $user){
  1077. // 0 蓝单 1 红单
  1078. if(! isset($data['bredvouch'])) return [false, '参照类型(蓝单|红单不能为空)'];
  1079. $type = $data['bredvouch'];
  1080. $order_date = $data['order_date'] ?? [];
  1081. $order_date = array_filter($order_date);
  1082. $code = $data['code'] ?? "";
  1083. $model = $this->databaseService->table('PO_Pomain as a')
  1084. ->leftJoin('Vendor as v', 'v.cVenCode', '=', 'a.cVenCode')
  1085. ->when(! empty($code), function ($query) use($code){
  1086. return $query->where('a.cPOID', 'LIKE', '%'.$code.'%');
  1087. })
  1088. ->when(! empty($order_date), function ($query) use($order_date){
  1089. $start = date('Y-m-d 00:00:00.000', $order_date[0]);
  1090. $end = date('Y-m-d 23:59:59.000', $order_date[1]);
  1091. return $query->whereBetween('a.dPODate', [$start, $end]);
  1092. })
  1093. ->where('a.iverifystateex', 2)
  1094. ->whereExists(function ($query) use($type){
  1095. if($type == 0){
  1096. $query->select(DB::raw(1))
  1097. ->from('PO_Podetails as b')
  1098. ->whereRaw('b.POID = a.POID')
  1099. ->whereRaw('ISNULL(b.iQuantity, 0) > ISNULL(b.iReceivedQTY, 0)');
  1100. }elseif($type == 1){
  1101. $query->select(DB::raw(1))
  1102. ->from('PO_Podetails as b')
  1103. ->whereRaw('b.POID = a.POID')
  1104. ->whereRaw('ISNULL(b.iReceivedQTY, 0) > 0');
  1105. }
  1106. })
  1107. ->select(
  1108. 'a.POID as id',
  1109. DB::raw("ISNULL(a.cBusType, '') as business_type"),
  1110. DB::raw("ISNULL(a.cVenCode, '') as supply_code"),
  1111. DB::raw("ISNULL(v.cVenName, '') as supply_name"),
  1112. DB::raw("ISNULL(a.cexch_name, '') as cexch_name"),
  1113. DB::raw("ISNULL(a.nflat, '') as nflat"),
  1114. DB::raw("ISNULL(a.iDiscountTaxType, '') as iDiscountTaxType"),
  1115. DB::raw("ISNULL(a.cPOID, '') as order_number"),
  1116. DB::raw("ISNULL(CONVERT(varchar(10), a.dPODate, 120), '') as order_date"),
  1117. DB::raw("ISNULL(a.cMaker, '') as crt_name")
  1118. )
  1119. ->orderBy('a.ID', 'desc');
  1120. $list = $this->limit($model, '', $data);
  1121. return [true, $list];
  1122. }
  1123. // 供应商 业务类型 汇率 扣税类别 币种 相同才能一起选
  1124. // 只能同时选择供应商、币种、汇率、扣税类别、业务类型、流程模式相同的行! 这是用友的提示
  1125. // 获取u8采购订单明细
  1126. public function getPurchaseOrderDetails($data){
  1127. if (empty($data['id'])) return [false, '采购订单主表ID不能为空'];
  1128. // 0 蓝单 1 红单
  1129. if(! isset($data['bredvouch'])) return [false, '参照类型(蓝单|红单不能为空)'];
  1130. $type = $data['bredvouch'];
  1131. $mainId = $data['id'];
  1132. $list = $this->databaseService->table('PO_Podetails as b')
  1133. ->leftJoin('Inventory as i', 'i.cInvCode', 'b.cInvCode') // 通常明细需要关联存货档案拿名称和规格
  1134. ->leftJoin('ComputationUnit as u', 'u.cComUnitCode', 'i.cComUnitCode')
  1135. ->where('b.POID', $mainId)
  1136. ->when(!empty($type), function ($query) use($type){
  1137. if($type == 0){
  1138. return $query->whereRaw('ISNULL(b.iQuantity, 0) > ISNULL(b.iReceivedQTY, 0)');
  1139. }elseif($type == 1){
  1140. return $query->whereRaw('ISNULL(b.iReceivedQTY, 0) > 0');
  1141. }
  1142. })
  1143. ->select(
  1144. 'b.POID as main_id', // 主表ID
  1145. 'b.ID as detail_id', // 子表行单据唯一标识
  1146. 'b.ivouchrowno as row_no', // 行号
  1147. 'b.cInvCode as code', // 存货编码
  1148. 'b.iTaxPrice as price', // 原币含税单价
  1149. 'b.iPerTaxRate as rate', // 税率
  1150. 'i.cComUnitCode as unit_code', // 主计量单位编码
  1151. 'i.bInvBatch as pici', // 批次管理
  1152. 'i.bInvQuality as baozhiqi', // 保质期管理
  1153. 'u.cComUnitName as unit', // 核心修改:主计量单位名称 (如: 个、千克、箱)
  1154. DB::raw("ISNULL(i.cInvName, '') as name"), // 存货名称
  1155. DB::raw("ISNULL(i.cInvStd, '') as size"), // 规格型号
  1156. DB::raw("ISNULL(b.iQuantity, 0) as i_qty"), // 采购订单数量 (iQuantity)
  1157. DB::raw("ISNULL(b.iReceivedQTY, 0) as in_qty"), // 累计入库数量 (iReceivedQTY)
  1158. DB::raw("(ISNULL(b.iQuantity, 0) - ISNULL(b.iReceivedQTY, 0)) as available_qty") // 剩余可入库数量
  1159. )
  1160. ->orderBy('b.ivouchrowno', 'asc')
  1161. ->get();
  1162. // 转化为普通数组返回
  1163. // $list = json_decode(json_encode($list), true);
  1164. return [true, $list];
  1165. }
  1166. public function getPurchaseOrderDetailsByCode($code = [])
  1167. {
  1168. if (empty($code)) return [false, '采购订单号不能为空'];
  1169. // 执行 U8 数据库查询
  1170. $list = $this->databaseService->table('PO_Podetails as b')
  1171. ->join('PO_Pomain as a', 'a.POID', '=', 'b.POID')
  1172. ->leftJoin('Inventory as i', 'i.cInvCode', '=', 'b.cInvCode')
  1173. ->whereIn('a.cPOID', $code)
  1174. ->select(
  1175. 'a.cPOID as order_number',
  1176. 'b.ID as detail_id',
  1177. 'b.POID as main_id',
  1178. 'b.ivouchrowno as row_no',
  1179. 'b.cInvCode as cInvCode',
  1180. DB::raw("ISNULL(i.cInvName, '') as name"),
  1181. DB::raw("ISNULL(i.cInvStd, '') as size"),
  1182. DB::raw("ISNULL(i.cComUnitCode, '') as unit"),
  1183. DB::raw("ISNULL(b.iPerTaxRate, 0) as rate"),
  1184. DB::raw("ISNULL(b.iUnitPrice, 0) as price"),
  1185. DB::raw("ISNULL(b.iQuantity, 0) as po_qty"),
  1186. DB::raw("ISNULL(b.iReceivedQTY, 0) as received_qty"),
  1187. DB::raw("(ISNULL(b.iQuantity, 0) - ISNULL(b.iReceivedQTY, 0)) as available_qty"),
  1188. // ==================== 新增:主表强控制联动校验字段 ====================
  1189. DB::raw("ISNULL(a.cBusType, '') as cBusType"), // 业务类型
  1190. DB::raw("ISNULL(a.cVenCode, '') as cVenCode"), // 供应商编码
  1191. DB::raw("ISNULL(a.cexch_name, '人民币') as cexch_name"), // 币种
  1192. DB::raw("CAST(ISNULL(a.nflat, 1.0) AS DECIMAL(10,4)) as nflat"),// 汇率
  1193. DB::raw("ISNULL(a.iDiscountTaxType, 0) as iDiscountTaxType") // 扣税类别
  1194. )
  1195. ->orderBy('a.cPOID', 'asc')
  1196. ->orderBy('b.ivouchrowno', 'asc')
  1197. ->get();
  1198. // 转化为普通数组返回
  1199. $list = json_decode(json_encode($list), true);
  1200. if (empty($list)) {
  1201. return [false, '未查询到对应的采购订单明细数据'];
  1202. }
  1203. // ==================== 核心修改:多单合单严格一致性校验 ====================
  1204. if (count($code) > 1) {
  1205. // 1. 校验供应商
  1206. $venCodes = array_unique(array_column($list, 'cVenCode'));
  1207. if (count($venCodes) > 1) return [false, '只能同时选择【供应商】相同的采购订单进行合并入库!'];
  1208. // 2. 校验业务类型
  1209. $busTypes = array_unique(array_column($list, 'cBusType'));
  1210. if (count($busTypes) > 1) return [false, '只能同时选择【业务类型】相同的采购订单进行合并入库!'];
  1211. // 3. 校验币种
  1212. $exchNames = array_unique(array_column($list, 'cexch_name'));
  1213. if (count($exchNames) > 1) return [false, '只能同时选择【币种】相同的采购订单进行合并入库!'];
  1214. // 4. 校验汇率 (转成 float 排除数据库浮点数末尾 0 的干扰)
  1215. $nflats = array_unique(array_map('floatval', array_column($list, 'nflat')));
  1216. if (count($nflats) > 1) return [false, '只能同时选择【汇率】相同的采购订单进行合并入库!'];
  1217. // 5. 校验扣税类别
  1218. $taxTypes = array_unique(array_column($list, 'iDiscountTaxType'));
  1219. if (count($taxTypes) > 1) return [false, '只能同时选择【扣税类别】相同的采购订单进行合并入库!'];
  1220. }
  1221. // ====================================================================
  1222. return [true, $list];
  1223. }
  1224. }