StatSaleClass.php 70 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353
  1. <?php
  2. namespace bizGhs\stat\classes;
  3. use biz\stat\classes\StatCgClass;
  4. use biz\stat\classes\StatStockInClass;
  5. use biz\stat\classes\StatStockOutClass;
  6. use bizGhs\cg\classes\CgRefundClass;
  7. use bizGhs\custom\classes\CustomClass;
  8. use bizGhs\expend\classes\ExpendClass;
  9. use bizGhs\item\classes\ItemClassClass;
  10. use bizGhs\order\classes\OrderItemClass;
  11. use bizGhs\order\classes\PurchaseOrderClass;
  12. use bizGhs\order\classes\PurchaseOrderItemClass;
  13. use bizGhs\order\classes\RefundOrderClass;
  14. use bizGhs\order\classes\StockWastageOrderClass;
  15. use bizGhs\product\classes\ProductClass;
  16. use bizGhs\staff\classes\SalaryClass;
  17. use bizHd\goods\classes\PlantCgClass;
  18. use bizHd\order\classes\OrderClass as HdOrderClass;
  19. use bizGhs\order\classes\OrderClass;
  20. use bizHd\purchase\classes\PurchaseClass;
  21. use bizHd\refund\classes\HdRefundClass;
  22. use common\components\arrayUtil;
  23. use common\components\dateUtil;
  24. use common\components\dict;
  25. use common\components\noticeUtil;
  26. use common\components\util;
  27. use Yii;
  28. use bizGhs\base\classes\BaseClass;
  29. class StatSaleClass extends BaseClass
  30. {
  31. public static $baseFile = '\bizGhs\stat\models\StatSale';
  32. public static function wastage($mainId)
  33. {
  34. $get = Yii::$app->request->get();
  35. $where = [];
  36. $where['mainId'] = $mainId;
  37. $searchTime = isset($get['searchTime']) && !empty($get['searchTime']) ? $get['searchTime'] : 'today';
  38. $startTime = $get['startTime'] ?? '';
  39. $endTime = $get['endTime'] ?? '';
  40. $period = dateUtil::formatTime($searchTime, $startTime, $endTime, true);
  41. $start = $period['startTime'];
  42. $end = $period['endTime'];
  43. $currentStartDate = date('Y-m-d', strtotime($start));
  44. $currentEndDate = date('Y-m-d', strtotime($end));
  45. $currentStartTime = $currentStartDate . ' 00:00:00';
  46. $currentEndTime = $currentEndDate . ' 23:59:59';
  47. $where['addTime'] = ['between', [$currentStartTime, $currentEndTime]];
  48. $list = StockWastageOrderClass::getAllByCondition($where, null, '*');
  49. $totalPrice = 0;
  50. if (!empty($list)) {
  51. foreach ($list as $order) {
  52. $price = $order['price'] ?? 0;
  53. $totalPrice = bcadd($totalPrice, $price, 2);
  54. }
  55. }
  56. $totalPrice = floatval($totalPrice);
  57. return ['totalPrice' => $totalPrice];
  58. }
  59. //结账跟踪 ssh 20230503
  60. public static function OrderClear($mainId)
  61. {
  62. $get = Yii::$app->request->get();
  63. $where = ['mainId' => $mainId, 'status' => ['in', [OrderClass::ORDER_STATUS_UN_SEND, OrderClass::ORDER_STATUS_SENDING, OrderClass::ORDER_STATUS_COMPLETE]]];
  64. $timeStyle = $get['timeStyle'] ?? 0;
  65. if (isset($get['searchTime']) && !empty($get['searchTime'])) {
  66. $searchTime = $get['searchTime'];
  67. $startTime = $get['startTime'] ?? '';
  68. $endTime = $get['endTime'] ?? '';
  69. $period = dateUtil::formatTime($searchTime, $startTime, $endTime, true);
  70. $start = $period['startTime'];
  71. $end = $period['endTime'];
  72. $currentStartDate = date('Y-m-d', strtotime($start));
  73. $currentEndDate = date('Y-m-d', strtotime($end));
  74. $currentStartTime = $currentStartDate . ' 00:00:00';
  75. $currentEndTime = $currentEndDate . ' 23:59:59';
  76. if ($timeStyle == 0) {
  77. $where['payTime'] = ['between', [$currentStartTime, $currentEndTime]];
  78. } else {
  79. $where['clearTime'] = ['between', [$currentStartTime, $currentEndTime]];
  80. }
  81. } else {
  82. $payTime = 'today';
  83. $startTime = $get['startTime'] ?? '';
  84. $endTime = $get['endTime'] ?? '';
  85. $period = dateUtil::formatTime($payTime, $startTime, $endTime, true);
  86. $start = $period['startTime'];
  87. $end = $period['endTime'];
  88. $currentStartDate = date('Y-m-d', strtotime($start));
  89. $currentEndDate = date('Y-m-d', strtotime($end));
  90. $currentStartTime = $currentStartDate . ' 00:00:00';
  91. $currentEndTime = $currentEndDate . ' 23:59:59';
  92. $where['payTime'] = ['between', [$currentStartTime, $currentEndTime]];
  93. }
  94. $customId = $get['customId'] ?? 0;
  95. if (!empty($customId)) {
  96. $where['customId'] = $customId;
  97. }
  98. $unClear = $get['unClear'] ?? 0;
  99. if ($unClear == 1) {
  100. $where['clearId'] = 0;
  101. $where['remainDebtPrice>'] = 0;
  102. }
  103. $ghsOrderList = OrderClass::getAllByCondition($where, null, '*');
  104. return ['list' => $ghsOrderList];
  105. }
  106. //客户下单榜 ssh 20230502
  107. public static function statCustomCg($mainId)
  108. {
  109. $get = Yii::$app->request->get();
  110. $where = [];
  111. $where['mainId'] = $mainId;
  112. $searchTime = isset($get['searchTime']) && !empty($get['searchTime']) ? $get['searchTime'] : 'today';
  113. $startTime = $get['startTime'] ?? '';
  114. $endTime = $get['endTime'] ?? '';
  115. $period = dateUtil::formatTime($searchTime, $startTime, $endTime, true);
  116. $start = $period['startTime'];
  117. $end = $period['endTime'];
  118. $where['time'] = ['between', [$start, $end]];
  119. $currentStartDate = date('Y-m-d', strtotime($start));
  120. $currentEndDate = date('Y-m-d', strtotime($end));
  121. $currentStartTime = $currentStartDate . ' 00:00:00';
  122. $currentEndTime = $currentEndDate . ' 23:59:59';
  123. $where = ['mainId' => $mainId, 'status' => ['in', [OrderClass::ORDER_STATUS_UN_SEND, OrderClass::ORDER_STATUS_SENDING, OrderClass::ORDER_STATUS_COMPLETE]]];
  124. $where['payTime'] = ['between', [$currentStartTime, $currentEndTime]];
  125. $customList = CustomClass::getAllByCondition(['ownMainId' => $mainId], null, 'id,name,py', 'id');
  126. $arr = [];
  127. $ghsOrderList = OrderClass::getAllByCondition($where, null, '*');
  128. $totalAmount = 0;
  129. $totalNum = 0;
  130. if (!empty($ghsOrderList)) {
  131. foreach ($ghsOrderList as $ghsKey => $ghsOrder) {
  132. $actPrice = $ghsOrder['actPrice'] ?? 0;
  133. $orderPrice = $ghsOrder['orderPrice'] ?? 0;
  134. $tkPrice = $ghsOrder['tkPrice'] ?? 0;
  135. $customId = $ghsOrder['customId'] ?? 0;
  136. $status = $ghsOrder['status'] ?? 0;
  137. $book = $ghsOrder['book'] ?? 0;
  138. if ($status == 1 || $status == 5) {
  139. continue;
  140. }
  141. if ($book == 1 && $status == 2) {
  142. continue;
  143. }
  144. $customName = $customList[$customId]['name'] ?? '';
  145. $py = $customList[$customId]['py'] ?? '';
  146. $totalNum++;
  147. $totalAmount = bcadd($totalAmount, $actPrice, 2);
  148. if (isset($arr[$customId])) {
  149. $arr[$customId]['num']++;
  150. $arr[$customId]['amount'] = bcadd($arr[$customId]['amount'], $actPrice, 2);
  151. $arr[$customId]['orderPrice'] = bcadd($arr[$customId]['orderPrice'], $orderPrice, 2);
  152. $arr[$customId]['tkPrice'] = bcadd($arr[$customId]['tkPrice'], $tkPrice, 2);
  153. } else {
  154. $arr[$customId] = [
  155. 'customId' => $customId,
  156. 'customName' => $customName,
  157. 'py' => $py,
  158. 'num' => 1,
  159. 'amount' => $actPrice,
  160. 'levelName' => '',
  161. 'orderPrice' => $orderPrice,
  162. 'tkPrice' => $tkPrice,
  163. ];
  164. }
  165. }
  166. $arr = arrayUtil::arraySort($arr, 'amount');
  167. }
  168. $totalAmount = floatval($totalAmount);
  169. return ['list' => $arr, 'totalAmount' => $totalAmount, 'totalNum' => $totalNum];
  170. }
  171. public static function profile($mainId)
  172. {
  173. //收入:批发开单、零售开单(含门店、美团和微信客服)、出库
  174. //支出:采购、调拨入库、批发退款、零售退款、店租、水电、伙食
  175. $get = Yii::$app->request->get();
  176. $where = [];
  177. $where['mainId'] = $mainId;
  178. $searchTime = isset($get['searchTime']) && !empty($get['searchTime']) ? $get['searchTime'] : 'today';
  179. $startTime = $get['startTime'] ?? '';
  180. $endTime = $get['endTime'] ?? '';
  181. $period = dateUtil::formatTime($searchTime, $startTime, $endTime, true);
  182. $start = $period['startTime'];
  183. $end = $period['endTime'];
  184. $where['time'] = ['between', [$start, $end]];
  185. $currentStartDate = date('Y-m-d', strtotime($start));
  186. $currentEndDate = date('Y-m-d', strtotime($end));
  187. $currentStartTime = $currentStartDate . ' 00:00:00';
  188. $currentEndTime = $currentEndDate . ' 23:59:59';
  189. //出库
  190. $stockOut = StatStockOutClass::getSum($where);
  191. //采购入库
  192. $cgWhere = ['mainId' => $mainId, 'status' => 4];
  193. $cgWhere['entryTime'] = ['between', [$currentStartTime, $currentEndTime]];
  194. $cgList = PurchaseOrderClass::getAllByCondition($cgWhere, null, '*');
  195. $cg = 0;
  196. if (!empty($cgList)) {
  197. foreach ($cgList as $cgKey => $cgItem) {
  198. $actPrice = $cgItem['actPrice'] ?? 0;
  199. $paidPrice = $cgItem['paidPrice'] ?? 0;
  200. if ($actPrice <= 0) {
  201. // 如果$actPrice=0,表示这个采购单已经全部退款,不需要再统计成本,本来$paidPrice要同步清零,但没有,这个问题由徐州邂逅花坊马总,提出的需求发现
  202. $cg = bcadd($cg, 0, 2);
  203. } else {
  204. $total = bcadd($actPrice, $paidPrice, 2);
  205. //物流费用不算成本。意思是说,长短途运费+提货费+本地运费,这四项和打包费一起,参与到平摊花材运费成本的计算,但那四项不参与总的采购成本登记,因为,有另外地方会独立登记运费成本。
  206. //$paidPrice 字段说明,此字段表示收货方已付款金额,包括 $localCharge本地运费、 $pickCharge提货费、 $shortCharge短途运费(到付时算,如果不是到付,不算在$paidPrice里)、$longCharge长途运费(到付时算,如果不是到付,不算在$paidPrice里)
  207. //请搜索关键词 cg_item_cost ,有多处相似情况要考虑
  208. if (isset($cgItem['isCost']) && $cgItem['isCost'] == 0) {
  209. $shortCharge = $cgItem['shortCharge'] ?? 0;
  210. $longCharge = $cgItem['longCharge'] ?? 0;
  211. $pickCharge = $cgItem['pickCharge'] ?? 0;
  212. $localCharge = $cgItem['localCharge'] ?? 0;
  213. $a1 = bcadd($shortCharge, $longCharge, 2);
  214. $a2 = bcadd($pickCharge, $localCharge, 2);
  215. $a = bcadd($a1, $a2, 2);
  216. //注意下面是减号 bcsub
  217. $total = bcsub($total, $a, 2);
  218. }
  219. $cg = bcadd($cg, $total, 2);
  220. }
  221. }
  222. }
  223. $cg = floatval($cg);
  224. $lsCgWhere = ['mainId' => $mainId, 'status' => 4, 'belongCost' => 1];
  225. $lsCgWhere['payTime'] = ['between', [$currentStartTime, $currentEndTime]];
  226. $lsCgList = PurchaseClass::getAllByCondition($lsCgWhere, null, '*');
  227. $lsCgAmount = 0;
  228. if (!empty($lsCgList)) {
  229. foreach ($lsCgList as $lsCgItem) {
  230. $actPrice = $lsCgItem['actPrice'] ?? 0;
  231. $lsCgAmount = bcadd($lsCgAmount, $actPrice, 2);
  232. }
  233. }
  234. $lsCgAmount = floatval($lsCgAmount);
  235. $lzWhere = ['mainId' => $mainId, 'status' => 3];
  236. $lzWhere['entryTime'] = ['between', [$currentStartTime, $currentEndTime]];
  237. $lzCgList = PlantCgClass::getAllByCondition($lzWhere, null, '*');
  238. $lzCgAmount = 0;
  239. if (!empty($lzCgList)) {
  240. foreach ($lzCgList as $lzInfo) {
  241. $actPrice = $lzInfo['actPrice'] ?? 0;
  242. $lzCgAmount = bcadd($actPrice, $lzCgAmount, 2);
  243. }
  244. }
  245. $lzCgAmount = floatval($lzCgAmount);
  246. //调拨入库
  247. $stockIn = StatStockInClass::getSum($where);
  248. $totalOrderNum = 0;
  249. //批发开单
  250. $pfIncome = 0;
  251. $pfSendCost = 0;
  252. $pfPack = 0;
  253. $where = ['mainId' => $mainId, 'status' => ['in', [OrderClass::ORDER_STATUS_UN_SEND, OrderClass::ORDER_STATUS_SENDING, OrderClass::ORDER_STATUS_COMPLETE]]];
  254. $where['payTime'] = ['between', [$currentStartTime, $currentEndTime]];
  255. $ghsOrderList = OrderClass::getAllByCondition($where, null, '*');
  256. if (!empty($ghsOrderList)) {
  257. foreach ($ghsOrderList as $ghsKey => $ghsOrder) {
  258. $actPrice = $ghsOrder['actPrice'] ?? 0;
  259. $sendCost = $ghsOrder['sendCost'] ?? 0;
  260. $refundSendCost = $ghsOrder['refundSendCost'] ?? 0;
  261. $remainSendCost = bcsub($sendCost, $refundSendCost, 2);
  262. $packCost = $ghsOrder['packCost'] ?? 0;
  263. $refundPackCost = $ghsOrder['refundPackCost'] ?? 0;
  264. $remainPackCost = bcsub($packCost, $refundPackCost, 2);
  265. $pfIncome = bcadd($pfIncome, $actPrice, 2);
  266. $pfSendCost = bcadd($pfSendCost, $remainSendCost, 2);
  267. $pfPack = bcadd($pfPack, $remainPackCost, 2);
  268. $totalOrderNum++;
  269. }
  270. }
  271. //零售开单
  272. $lsIncome = 0;
  273. $lsSendCost = 0;
  274. $lsServiceFee = 0;
  275. $hdWhere = ['mainId' => $mainId, 'status' => ['in', [HdOrderClass::ORDER_STATUS_UN_SEND, HdOrderClass::ORDER_STATUS_SENDING, HdOrderClass::ORDER_STATUS_COMPLETE]]];
  276. $hdWhere['payTime'] = ['between', [$currentStartTime, $currentEndTime]];
  277. $hdOrderList = HdOrderClass::getAllByCondition($hdWhere, null, '*');
  278. foreach ($hdOrderList as $hdKey => $hdOrder) {
  279. $actPrice = $hdOrder['mainPay'] ?? 0;
  280. if ($hdOrder['tkPrice'] && $hdOrder['tkPrice'] > 0) {
  281. $actPrice = bcsub($actPrice, $hdOrder['tkPrice'], 2);
  282. }
  283. $cash = $hdOrder['cash'] ?? 0;
  284. $serviceFee = $hdOrder['serviceFee'] ?? 0;
  285. $sendCost = $hdOrder['sendCost'] ?? 0;
  286. $lsIncome = bcadd($actPrice, $lsIncome, 2);
  287. $lsIncome = bcadd($lsIncome, $cash, 2);
  288. //退款取消的先不计算成本
  289. if ($actPrice > 0) {
  290. $lsSendCost = bcadd($lsSendCost, $sendCost, 2);
  291. $lsServiceFee = bcadd($lsServiceFee, $serviceFee, 2);
  292. }
  293. $totalOrderNum++;
  294. }
  295. $condition = ['mainId' => $mainId, 'addTime' => ['between', [$currentStartTime, $currentEndTime]],];
  296. $salary = 0;
  297. $salaryList = SalaryClass::getAllByCondition($condition, null, '*', null, true);
  298. if (!empty($salaryList)) {
  299. foreach ($salaryList as $item) {
  300. $salary = bcadd($salary, $item->amount, 2);
  301. }
  302. }
  303. $condition = [
  304. 'mainId' => $mainId,
  305. 'payTime' => ['between', [$currentStartTime, $currentEndTime]],
  306. 'delStatus' => 0,
  307. ];
  308. $expendList = ExpendClass::getAllByCondition($condition, null, '*', null, true);
  309. $expendTypeMap = dict::getDict('expendTypeMap');
  310. $expendAmount = [];
  311. if (!empty($expendList)) {
  312. foreach ($expendList as $item) {
  313. $type = $item->type ?? 0;
  314. $amount = $item->amount ?? 0;
  315. $name = $expendTypeMap[$type] ?? '暂无';
  316. if (isset($expendAmount[$type]['amount']) == false) {
  317. $expendAmount[$type]['amount'] = 0;
  318. }
  319. $expendAmount[$type]['name'] = $name;
  320. $expendAmount[$type]['amount'] = floatval(bcadd($expendAmount[$type]['amount'], $amount, 2));
  321. }
  322. }
  323. $income = [
  324. ['name' => '批发', 'id' => 'pf', 'amount' => floatval($pfIncome)],
  325. ['name' => '零售', 'id' => 'ls', 'amount' => floatval($lsIncome)],
  326. ['name' => '调拨出库', 'id' => 'allot', 'amount' => floatval($stockOut)],
  327. ];
  328. $expend = [
  329. ['name' => '批发运费', 'id' => 'pfSendCost', 'amount' => floatval($pfSendCost)],
  330. ['name' => '批发包装费', 'id' => 'pfSendCost', 'amount' => floatval($pfPack)],
  331. ['name' => '零售运费', 'id' => 'lsSendCost', 'amount' => floatval($lsSendCost)],
  332. ['name' => '美团手续费', 'id' => 'mtFee', 'amount' => floatval($lsServiceFee)],
  333. ['name' => '花材采购', 'id' => 'cg', 'amount' => floatval($cg)],
  334. ['name' => '绿植资材采购', 'id' => 'cg', 'amount' => floatval($lzCgAmount)],
  335. ['name' => '零售花材采购', 'id' => 'cg', 'amount' => floatval($lsCgAmount)],
  336. ['name' => '调拨入库', 'id' => 'allot', 'amount' => floatval($stockIn)],
  337. ['name' => '员工工资', 'id' => 'salary', 'amount' => floatval($salary)],
  338. ];
  339. if (!empty($expendAmount)) {
  340. foreach ($expendAmount as $it) {
  341. $expend[] = $it;
  342. }
  343. }
  344. $balance = 0;
  345. foreach ($income as $it) {
  346. $amount = $it['amount'] ?? 0;
  347. $balance = bcadd($amount, $balance, 2);
  348. }
  349. foreach ($expend as $it) {
  350. $amount = $it['amount'] ?? 0;
  351. $balance = bcsub($balance, $amount, 2);
  352. }
  353. return ['income' => $income, 'expend' => $expend, 'balance' => floatval($balance), 'totalOrderNum' => $totalOrderNum];
  354. }
  355. //按花材统计收入 ssh 20231111
  356. public static function incomeOutItem($mainId)
  357. {
  358. $get = Yii::$app->request->get();
  359. $export = $get['export'] ?? 0;
  360. $where = [];
  361. $where['mainId'] = $mainId;
  362. $searchTime = isset($get['searchTime']) && !empty($get['searchTime']) ? $get['searchTime'] : 'today';
  363. $startTime = $get['startTime'] ?? '';
  364. $endTime = $get['endTime'] ?? '';
  365. $period = dateUtil::formatTime($searchTime, $startTime, $endTime, true);
  366. $start = $period['startTime'];
  367. $end = $period['endTime'];
  368. $where['time'] = ['between', [$start, $end]];
  369. $currentStartDate = date('Y-m-d', strtotime($start));
  370. $currentEndDate = date('Y-m-d', strtotime($end));
  371. $currentStartTime = $currentStartDate . ' 00:00:00';
  372. $currentEndTime = $currentEndDate . ' 23:59:59';
  373. $s = strtotime($currentStartTime);
  374. $e = strtotime($currentEndTime);
  375. $p = bcsub($e, $s);
  376. $xx = bcmul(86400, 90);
  377. if ($p > $xx) {
  378. util::fail('查询时间不能超过三个月');
  379. }
  380. $productList = ProductClass::getAllByCondition(['mainId' => $mainId], null, 'id,name', 'id');
  381. if (empty($productList)) {
  382. util::fail('没有找到花材');
  383. }
  384. $arr = [];
  385. //打包费和运费
  386. $packFreight = 0;
  387. $onlyRefundCg = 0;
  388. $onlyRefund = 0;
  389. $totalDiscountAmount = 0;
  390. $totalLabourCost = 0;
  391. //采购入库
  392. $tbCgIncome = [];
  393. $tbCgIncomeStaff = [];
  394. $cgWhere = ['mainId' => $mainId, 'status' => 4];
  395. $cgWhere['entryTime'] = ['between', [$currentStartTime, $currentEndTime]];
  396. $cgList = PurchaseOrderClass::getAllByCondition($cgWhere, null, '*');
  397. if (!empty($cgList)) {
  398. foreach ($cgList as $cgKey => $cgItem) {
  399. $packingCharge = $cgItem['packingCharge'] ?? 0;
  400. $shortCharge = $cgItem['shortCharge'] ?? 0;
  401. $longCharge = $cgItem['longCharge'] ?? 0;
  402. $pickCharge = $cgItem['pickCharge'] ?? 0;
  403. $localCharge = $cgItem['localCharge'] ?? 0;
  404. $packFreight = bcadd($packFreight, $packingCharge, 2);
  405. $packFreight = bcadd($packFreight, $shortCharge, 2);
  406. $packFreight = bcadd($packFreight, $longCharge, 2);
  407. $packFreight = bcadd($packFreight, $pickCharge, 2);
  408. $packFreight = bcadd($packFreight, $localCharge, 2);
  409. $tkPrice = $cgItem['tkPrice'] ?? 0;
  410. $orderSn = $cgItem['orderSn'] ?? '';
  411. $cgItemInfoList = PurchaseOrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  412. if (!empty($cgItemInfoList)) {
  413. foreach ($cgItemInfoList as $cgData) {
  414. $itemNum = $cgData->itemNum ?? 0;
  415. $productId = $cgData->productId ?? 0;
  416. $refundNum = $cgData->refundNum ?? 0;
  417. $remainNum = bcsub($itemNum, $refundNum);
  418. $bigPrice = $cgData->bigPrice ?? 0;
  419. $amount = bcmul($remainNum, $bigPrice, 2);
  420. $itemName = $productList[$productId]['name'] ?? '已删除';
  421. $arr[$productId]['name'] = $itemName;
  422. $arr[$productId]['id'] = $productId;
  423. if (isset($arr[$productId]['expend'])) {
  424. $arr[$productId]['expend'] = bcadd($amount, $arr[$productId]['expend'], 2);
  425. $arr[$productId]['outNum'] = bcadd($arr[$productId]['outNum'], $remainNum, 2);
  426. } else {
  427. $arr[$productId]['expend'] = $amount;
  428. $arr[$productId]['outNum'] = $remainNum;
  429. $arr[$productId]['inNum'] = 0;
  430. $arr[$productId]['income'] = 0;
  431. }
  432. }
  433. if ($tkPrice > 0) {
  434. $cgRefundOrderList = CgRefundClass::getAllByCondition(['relateOrderSn' => $orderSn], null, '*', null, true);
  435. if (!empty($cgRefundOrderList)) {
  436. foreach ($cgRefundOrderList as $cgRefundOrder) {
  437. if ($cgRefundOrder->refundType == 2) {
  438. $onlyRefundCg = bcadd($cgRefundOrder->refundPrice, $onlyRefundCg, 2);
  439. }
  440. }
  441. }
  442. }
  443. }
  444. }
  445. }
  446. //批发开单
  447. $pfIncome = 0;
  448. $tbSaleIncome = [];
  449. $tbSaleIncomeStaff = [];
  450. $where = ['mainId' => $mainId, 'status' => ['in', [OrderClass::ORDER_STATUS_UN_SEND, OrderClass::ORDER_STATUS_SENDING, OrderClass::ORDER_STATUS_COMPLETE]]];
  451. $where['payTime'] = ['between', [$currentStartTime, $currentEndTime]];
  452. $ghsOrderList = OrderClass::getAllByCondition($where, null, '*');
  453. if (!empty($ghsOrderList)) {
  454. foreach ($ghsOrderList as $ghsKey => $ghsOrder) {
  455. $book = $ghsOrder['book'] ?? 0;
  456. $status = $ghsOrder['status'] ?? 0;
  457. if ($book == 1 && $status == 2) {
  458. //预订单待发货的不算收入
  459. continue;
  460. }
  461. if ($status == 0 || $status == 5) {
  462. continue;
  463. }
  464. $shopAdminId = $ghsOrder['shopAdminId'] ?? 0;
  465. $shopAdminName = $ghsOrder['shopAdminName'] ?? '';
  466. $orderSn = $ghsOrder['orderSn'] ?? '';
  467. $actPrice = $ghsOrder['actPrice'] ?? 0;
  468. $tkPrice = $ghsOrder['tkPrice'] ?? 0;
  469. $pfIncome = bcadd($pfIncome, $actPrice, 2);
  470. $labourCost = $ghsOrder['labourCost'] ?? 0;
  471. $discountAmount = $ghsOrder['discountAmount'] ?? 0;
  472. $totalLabourCost = bcadd($labourCost, $totalLabourCost, 2);
  473. $totalDiscountAmount = bcadd($totalDiscountAmount, $discountAmount, 2);
  474. $orderItemList = OrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  475. if (!empty($orderItemList)) {
  476. foreach ($orderItemList as $orderItem) {
  477. $refundNum = $orderItem->refundNum ?? 0;
  478. $name = $orderItem->name ?? '';
  479. $productId = $orderItem->productId ?? 0;
  480. $num = $orderItem->xhNum ?? 0;
  481. $remainNum = bcsub($num, $refundNum);
  482. $xhUnitPrice = $orderItem->xhUnitPrice ?? 0;
  483. $amount = bcmul($remainNum, $xhUnitPrice, 2);
  484. $itemName = $productList[$productId]['name'] ?? '已删除';
  485. $arr[$productId]['name'] = $itemName;
  486. $arr[$productId]['id'] = $productId;
  487. if (isset($arr[$productId]['income'])) {
  488. $arr[$productId]['income'] = bcadd($amount, $arr[$productId]['income'], 2);
  489. $arr[$productId]['inNum'] = bcadd($arr[$productId]['inNum'], $remainNum, 2);
  490. } else {
  491. $arr[$productId]['expend'] = 0;
  492. $arr[$productId]['outNum'] = 0;
  493. $arr[$productId]['inNum'] = $remainNum;
  494. $arr[$productId]['income'] = $amount;
  495. }
  496. //单独统计
  497. $belongCost = $orderItem->belongCost ?? 0;
  498. if ($belongCost == 1) {
  499. if (isset($tbSaleIncome[$productId])) {
  500. $tbSaleIncome[$productId]['num'] = bcadd($tbSaleIncome[$productId]['num'], $remainNum);
  501. $tbSaleIncome[$productId]['price'] = bcadd($tbSaleIncome[$productId]['price'], $amount, 2);
  502. } else {
  503. $tbSaleIncome[$productId] = ['name' => $name, 'num' => $remainNum, 'price' => $amount];
  504. }
  505. if (isset($tbSaleIncomeStaff[$productId]) && isset($tbSaleIncomeStaff[$productId][$shopAdminId])) {
  506. $tbSaleIncomeStaff[$productId][$shopAdminId]['num'] = bcadd($tbSaleIncomeStaff[$productId][$shopAdminId]['num'], $remainNum);
  507. $tbSaleIncomeStaff[$productId][$shopAdminId]['price'] = bcadd($tbSaleIncomeStaff[$productId][$shopAdminId]['price'], $amount, 2);
  508. } else {
  509. $tbSaleIncomeStaff[$productId][$shopAdminId] = ['name' => $name, 'num' => $remainNum, 'price' => $amount, 'staffName' => $shopAdminName];
  510. }
  511. }
  512. }
  513. if ($tkPrice > 0) {
  514. $refundList = RefundOrderClass::getAllByCondition(['relateOrderSn' => $orderSn], null, '*', null, true);
  515. if (!empty($refundList)) {
  516. foreach ($refundList as $refund) {
  517. if ($refund->refundType == 1) {
  518. //退货退款前面已经考虑,这里不再处理 OK
  519. } else {
  520. $onlyRefund = bcadd($onlyRefund, $refund->refundPrice, 2);
  521. }
  522. }
  523. }
  524. }
  525. }
  526. }
  527. }
  528. //零售开单
  529. $lsIncome = 0;
  530. $hdWhere = ['mainId' => $mainId, 'status' => ['in', [HdOrderClass::ORDER_STATUS_UN_SEND, HdOrderClass::ORDER_STATUS_SENDING, HdOrderClass::ORDER_STATUS_COMPLETE]]];
  531. $hdWhere['payTime'] = ['between', [$currentStartTime, $currentEndTime]];
  532. $hdOrderList = HdOrderClass::getAllByCondition($hdWhere, null, '*');
  533. foreach ($hdOrderList as $hdKey => $hdOrder) {
  534. $actPrice = $hdOrder['mainPay'] ?? 0;
  535. $cash = $hdOrder['cash'] ?? 0;
  536. $lsIncome = bcadd($actPrice, $lsIncome, 2);
  537. $lsIncome = bcadd($lsIncome, $cash, 2);
  538. $orderSn = $hdOrder['orderSn'] ?? '';
  539. $tkPrice = $hdOrder['tkPrice'] ?? 0;
  540. $orderItemList = \bizHd\order\classes\OrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  541. if (!empty($orderItemList)) {
  542. foreach ($orderItemList as $orderItem) {
  543. $unitPrice = $orderItem->unitPrice ?? 0;
  544. $refundNum = $orderItem->refundNum ?? 0;
  545. $num = $orderItem->num ?? 0;
  546. $productId = $orderItem->itemId ?? 0;
  547. $remainNum = bcsub($num, $refundNum);
  548. $amount = bcmul($remainNum, $unitPrice, 2);
  549. $itemName = $productList[$productId]['name'] ?? '已删除';
  550. $arr[$productId]['name'] = $itemName;
  551. $arr[$productId]['id'] = $productId;
  552. if (isset($arr[$productId]['income'])) {
  553. $arr[$productId]['income'] = bcadd($amount, $arr[$productId]['income'], 2);
  554. $arr[$productId]['inNum'] = bcadd($arr[$productId]['inNum'], $remainNum, 2);
  555. } else {
  556. $arr[$productId]['expend'] = 0;
  557. $arr[$productId]['outNum'] = 0;
  558. $arr[$productId]['inNum'] = $remainNum;
  559. $arr[$productId]['income'] = $amount;
  560. }
  561. }
  562. if ($tkPrice > 0) {
  563. $refundList = HdRefundClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  564. if (!empty($refundList)) {
  565. foreach ($refundList as $refund) {
  566. if ($refund->refundType == 2) {
  567. $onlyRefund = bcadd($onlyRefund, $refund->refundPrice, 2);
  568. }
  569. }
  570. }
  571. }
  572. }
  573. }
  574. $totalIncome = 0;
  575. $totalExpend = 0;
  576. if (!empty($arr)) {
  577. foreach ($arr as $key => $val) {
  578. $income = $val['income'] ?? 0;
  579. $income = floatval($income);
  580. $totalIncome = bcadd($totalIncome, $income, 2);
  581. $expend = $val['expend'] ?? 0;
  582. $expend = floatval($expend);
  583. $totalExpend = bcadd($totalExpend, $expend, 2);
  584. $profit = bcsub($income, $expend, 2);
  585. $arr[$key]['profit'] = floatval($profit);
  586. $arr[$key]['income'] = floatval($income);
  587. $arr[$key]['expend'] = floatval($expend);
  588. }
  589. }
  590. //总成本算上物流打包等费用
  591. $totalExpend = bcadd($totalExpend, $packFreight, 2);
  592. //总收入算上采购仅退款
  593. $totalIncome = bcadd($totalIncome, $onlyRefundCg, 2);
  594. //总收入加上人工费
  595. $totalIncome = bcadd($totalIncome, $totalLabourCost, 2);
  596. //总收入减掉优惠金额
  597. $totalIncome = bcsub($totalIncome, $totalDiscountAmount, 2);
  598. //总成本算上销售仅退款
  599. $totalExpend = bcadd($totalExpend, $onlyRefund, 2);
  600. $totalProfit = bcsub($totalIncome, $totalExpend, 2);
  601. $totalIncome = floatval($totalIncome);
  602. $totalExpend = floatval($totalExpend);
  603. $totalProfit = floatval($totalProfit);
  604. $arr = array_values($arr);
  605. $arr = arrayUtil::arraySort($arr, 'profit');
  606. if ($export == 1) {
  607. $statData = [
  608. ['id' => 0, 'name' => '物流打包费', 'income' => 0, 'expend' => $packFreight, 'profit' => -$packFreight, 'inNum' => 0, 'outNum' => 0],
  609. ['id' => 0, 'name' => '采购仅退款', 'income' => $onlyRefundCg, 'expend' => 0, 'profit' => $onlyRefundCg, 'inNum' => 0, 'outNum' => 0],
  610. ['id' => 0, 'name' => '销售仅退款', 'income' => 0, 'expend' => $onlyRefund, 'profit' => -$onlyRefund, 'inNum' => 0, 'outNum' => 0],
  611. ['id' => 0, 'name' => '附加人工费', 'income' => $totalLabourCost, 'expend' => 0, 'profit' => $totalLabourCost, 'inNum' => 0, 'outNum' => 0],
  612. ['id' => 0, 'name' => '优惠金额', 'income' => -$totalDiscountAmount, 'expend' => 0, 'profit' => -$totalDiscountAmount, 'inNum' => 0, 'outNum' => 0],
  613. ['id' => 0, 'name' => '【汇总金额】', 'income' => $totalIncome, 'expend' => $totalExpend, 'profit' => $totalProfit, 'inNum' => 0, 'outNum' => 0],
  614. ];
  615. $list = array_merge($arr, $statData);
  616. self::exportIncomeOutItem($list, $mainId);
  617. }
  618. util::success(['list' => $arr, 'packFreight' => $packFreight, 'onlyRefundCg' => $onlyRefundCg, 'onlyRefund' => $onlyRefund,
  619. 'totalIncome' => $totalIncome,
  620. 'totalExpend' => $totalExpend,
  621. 'totalProfit' => $totalProfit,
  622. 'tbSaleIncome' => $tbSaleIncome,
  623. 'tbCgIncome' => $tbCgIncome,
  624. 'tbCgIncomeStaff' => $tbCgIncomeStaff,
  625. 'tbSaleIncomeStaff' => $tbSaleIncomeStaff,
  626. 'totalLabourCost' => $totalLabourCost,
  627. 'totalDiscountAmount' => $totalDiscountAmount,
  628. ]);
  629. }
  630. //按分类统计收入 ssh 20231111
  631. public static function classProfile($mainId)
  632. {
  633. $get = Yii::$app->request->get();
  634. $export = $get['export'] ?? 0;
  635. $where = [];
  636. $where['mainId'] = $mainId;
  637. $searchTime = isset($get['searchTime']) && !empty($get['searchTime']) ? $get['searchTime'] : 'today';
  638. $startTime = $get['startTime'] ?? '';
  639. $endTime = $get['endTime'] ?? '';
  640. $period = dateUtil::formatTime($searchTime, $startTime, $endTime, true);
  641. $start = $period['startTime'];
  642. $end = $period['endTime'];
  643. $where['time'] = ['between', [$start, $end]];
  644. $currentStartDate = date('Y-m-d', strtotime($start));
  645. $currentEndDate = date('Y-m-d', strtotime($end));
  646. $currentStartTime = $currentStartDate . ' 00:00:00';
  647. $currentEndTime = $currentEndDate . ' 23:59:59';
  648. $s = strtotime($currentStartTime);
  649. $e = strtotime($currentEndTime);
  650. $p = bcsub($e, $s);
  651. $xx = bcmul(86400, 90);
  652. if ($p > $xx) {
  653. util::fail('查询时间不能超过三个月');
  654. }
  655. $classList = ItemClassClass::getAllByCondition(['mainId' => $mainId], null, 'id,name', 'id');
  656. if (empty($classList)) {
  657. util::fail('没有找到花材');
  658. }
  659. $arr = [];
  660. //打包费和运费
  661. $packFreight = 0;
  662. $onlyRefundCg = 0;
  663. $onlyRefund = 0;
  664. $totalDiscountAmount = 0;
  665. $totalLabourCost = 0;
  666. //采购入库
  667. $tbCgIncome = [];
  668. $tbCgIncomeStaff = [];
  669. $cgWhere = ['mainId' => $mainId, 'status' => 4];
  670. $cgWhere['entryTime'] = ['between', [$currentStartTime, $currentEndTime]];
  671. $cgList = PurchaseOrderClass::getAllByCondition($cgWhere, null, '*');
  672. if (!empty($cgList)) {
  673. foreach ($cgList as $cgKey => $cgItem) {
  674. $packingCharge = $cgItem['packingCharge'] ?? 0;
  675. $shortCharge = $cgItem['shortCharge'] ?? 0;
  676. $longCharge = $cgItem['longCharge'] ?? 0;
  677. $pickCharge = $cgItem['pickCharge'] ?? 0;
  678. $localCharge = $cgItem['localCharge'] ?? 0;
  679. $cgStaffId = $cgItem['cgStaffId'] ?? 0;
  680. $cgStaffName = $cgItem['cgStaffName'] ?? '';
  681. $actPrice = $cgItem['actPrice'] ?? 0;
  682. if ($actPrice <= 0) {
  683. continue;
  684. }
  685. $packFreight = bcadd($packFreight, $packingCharge, 2);
  686. $packFreight = bcadd($packFreight, $shortCharge, 2);
  687. $packFreight = bcadd($packFreight, $longCharge, 2);
  688. $packFreight = bcadd($packFreight, $pickCharge, 2);
  689. $packFreight = bcadd($packFreight, $localCharge, 2);
  690. $tkPrice = $cgItem['tkPrice'] ?? 0;
  691. $orderSn = $cgItem['orderSn'] ?? '';
  692. $cgItemInfoList = PurchaseOrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  693. if (!empty($cgItemInfoList)) {
  694. foreach ($cgItemInfoList as $cgData) {
  695. $itemNum = $cgData->itemNum ?? 0;
  696. $productId = $cgData->productId ?? 0;
  697. $name = $cgData->name ?? '';
  698. $refundNum = $cgData->refundNum ?? 0;
  699. $remainNum = bcsub($itemNum, $refundNum);
  700. $bigPrice = $cgData->bigPrice ?? 0;
  701. $amount = bcmul($remainNum, $bigPrice, 2);
  702. $classId = $cgData->classId;
  703. $className = $classList[$classId]['name'] ?? '已删除';
  704. if ($className == '运费') {
  705. //noticeUtil::push("{$cgData->orderSn}运费项", '15280215347');
  706. }
  707. $arr[$classId]['className'] = $className;
  708. $arr[$classId]['classId'] = $classId;
  709. if (isset($arr[$classId]['expend'])) {
  710. $arr[$classId]['expend'] = bcadd($amount, $arr[$classId]['expend'], 2);
  711. $arr[$classId]['cgNum'] = bcadd($arr[$classId]['cgNum'], $remainNum);
  712. } else {
  713. $arr[$classId]['expend'] = $amount;
  714. $arr[$classId]['income'] = 0;
  715. $arr[$classId]['cgNum'] = $remainNum;
  716. $arr[$classId]['saleNum'] = 0;
  717. }
  718. //单独统计
  719. $belongCost = $cgData->belongCost ?? 0;
  720. if ($belongCost == 1) {
  721. if (isset($tbCgIncome[$productId])) {
  722. $tbCgIncome[$productId]['num'] = bcadd($tbCgIncome[$productId]['num'], $remainNum);
  723. $tbCgIncome[$productId]['price'] = bcadd($tbCgIncome[$productId]['price'], $amount, 2);
  724. } else {
  725. $tbCgIncome[$productId] = ['num' => $remainNum, 'name' => $name, 'price' => $amount];
  726. }
  727. if (isset($tbCgIncomeStaff[$productId]) && isset($tbCgIncomeStaff[$productId][$cgStaffId])) {
  728. $tbCgIncomeStaff[$productId][$cgStaffId]['num'] = bcadd($tbCgIncomeStaff[$productId][$cgStaffId]['num'], $remainNum);
  729. $tbCgIncomeStaff[$productId][$cgStaffId]['price'] = bcadd($tbCgIncomeStaff[$productId][$cgStaffId]['price'], $amount, 2);
  730. } else {
  731. $tbCgIncomeStaff[$productId][$cgStaffId] = ['num' => $remainNum, 'name' => $name, 'price' => $amount, 'staffName' => $cgStaffName,];
  732. }
  733. }
  734. }
  735. if ($tkPrice > 0) {
  736. $cgRefundOrderList = CgRefundClass::getAllByCondition(['relateOrderSn' => $orderSn], null, '*', null, true);
  737. if (!empty($cgRefundOrderList)) {
  738. foreach ($cgRefundOrderList as $cgRefundOrder) {
  739. if ($cgRefundOrder->refundType == 2) {
  740. $onlyRefundCg = bcadd($cgRefundOrder->refundPrice, $onlyRefundCg, 2);
  741. }
  742. }
  743. }
  744. }
  745. }
  746. }
  747. }
  748. //批发开单
  749. $pfIncome = 0;
  750. $tbSaleIncome = [];
  751. $tbSaleIncomeStaff = [];
  752. $where = ['mainId' => $mainId, 'status' => ['in', [OrderClass::ORDER_STATUS_UN_SEND, OrderClass::ORDER_STATUS_SENDING, OrderClass::ORDER_STATUS_COMPLETE]]];
  753. $where['payTime'] = ['between', [$currentStartTime, $currentEndTime]];
  754. $ghsOrderList = OrderClass::getAllByCondition($where, null, '*');
  755. if (!empty($ghsOrderList)) {
  756. foreach ($ghsOrderList as $ghsKey => $ghsOrder) {
  757. $book = $ghsOrder['book'] ?? 0;
  758. $status = $ghsOrder['status'] ?? 0;
  759. if ($book == 1 && $status == 2) {
  760. //预订单待发货的不算收入
  761. continue;
  762. }
  763. if ($status == 0 || $status == 5) {
  764. continue;
  765. }
  766. $shopAdminId = $ghsOrder['shopAdminId'] ?? 0;
  767. $shopAdminName = $ghsOrder['shopAdminName'] ?? '';
  768. $orderSn = $ghsOrder['orderSn'] ?? '';
  769. $actPrice = $ghsOrder['actPrice'] ?? 0;
  770. if ($actPrice <= 0) {
  771. continue;
  772. }
  773. $tkPrice = $ghsOrder['tkPrice'] ?? 0;
  774. $pfIncome = bcadd($pfIncome, $actPrice, 2);
  775. $labourCost = $ghsOrder['labourCost'] ?? 0;
  776. $discountAmount = $ghsOrder['discountAmount'] ?? 0;
  777. $totalLabourCost = bcadd($labourCost, $totalLabourCost, 2);
  778. $totalDiscountAmount = bcadd($totalDiscountAmount, $discountAmount, 2);
  779. $orderItemList = OrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  780. if (!empty($orderItemList)) {
  781. foreach ($orderItemList as $orderItem) {
  782. $refundNum = $orderItem->refundNum ?? 0;
  783. $name = $orderItem->name ?? '';
  784. $productId = $orderItem->productId ?? 0;
  785. $num = $orderItem->xhNum ?? 0;
  786. $remainNum = bcsub($num, $refundNum);
  787. $classId = $orderItem->classId ?? 0;
  788. $className = $classList[$classId]['name'] ?? '已删除';
  789. $xhUnitPrice = $orderItem->xhUnitPrice ?? 0;
  790. $amount = bcmul($remainNum, $xhUnitPrice, 2);
  791. $arr[$classId]['className'] = $className;
  792. $arr[$classId]['classId'] = $classId;
  793. if (isset($arr[$classId]['income'])) {
  794. $arr[$classId]['income'] = bcadd($amount, $arr[$classId]['income'], 2);
  795. $arr[$classId]['saleNum'] = bcadd($arr[$classId]['saleNum'], $remainNum);
  796. } else {
  797. $arr[$classId]['expend'] = 0;
  798. $arr[$classId]['income'] = $amount;
  799. $arr[$classId]['saleNum'] = $remainNum;
  800. $arr[$classId]['cgNum'] = 0;
  801. }
  802. //单独统计
  803. $belongCost = $orderItem->belongCost ?? 0;
  804. if ($belongCost == 1) {
  805. if (isset($tbSaleIncome[$productId])) {
  806. $tbSaleIncome[$productId]['num'] = bcadd($tbSaleIncome[$productId]['num'], $remainNum);
  807. $tbSaleIncome[$productId]['price'] = bcadd($tbSaleIncome[$productId]['price'], $amount, 2);
  808. } else {
  809. $tbSaleIncome[$productId] = ['name' => $name, 'num' => $remainNum, 'price' => $amount];
  810. }
  811. if (isset($tbSaleIncomeStaff[$productId]) && isset($tbSaleIncomeStaff[$productId][$shopAdminId])) {
  812. $tbSaleIncomeStaff[$productId][$shopAdminId]['num'] = bcadd($tbSaleIncomeStaff[$productId][$shopAdminId]['num'], $remainNum);
  813. $tbSaleIncomeStaff[$productId][$shopAdminId]['price'] = bcadd($tbSaleIncomeStaff[$productId][$shopAdminId]['price'], $amount, 2);
  814. } else {
  815. $tbSaleIncomeStaff[$productId][$shopAdminId] = ['name' => $name, 'num' => $remainNum, 'price' => $amount, 'staffName' => $shopAdminName];
  816. }
  817. }
  818. }
  819. if ($tkPrice > 0) {
  820. $refundList = RefundOrderClass::getAllByCondition(['relateOrderSn' => $orderSn], null, '*', null, true);
  821. if (!empty($refundList)) {
  822. foreach ($refundList as $refund) {
  823. if ($refund->refundType == 1) {
  824. //退货退款前面已经考虑,这里不再处理 OK
  825. } else {
  826. $onlyRefund = bcadd($onlyRefund, $refund->refundPrice, 2);
  827. }
  828. }
  829. }
  830. }
  831. }
  832. }
  833. }
  834. //零售开单
  835. $lsIncome = 0;
  836. $hdWhere = ['mainId' => $mainId, 'status' => ['in', [HdOrderClass::ORDER_STATUS_UN_SEND, HdOrderClass::ORDER_STATUS_SENDING, HdOrderClass::ORDER_STATUS_COMPLETE]]];
  837. $hdWhere['payTime'] = ['between', [$currentStartTime, $currentEndTime]];
  838. $hdOrderList = HdOrderClass::getAllByCondition($hdWhere, null, '*');
  839. foreach ($hdOrderList as $hdKey => $hdOrder) {
  840. $actPrice = $hdOrder['mainPay'] ?? 0;
  841. $cash = $hdOrder['cash'] ?? 0;
  842. if ($actPrice <= 0 && $cash <= 0) {
  843. continue;
  844. }
  845. $lsIncome = bcadd($actPrice, $lsIncome, 2);
  846. $lsIncome = bcadd($lsIncome, $cash, 2);
  847. $orderSn = $hdOrder['orderSn'] ?? '';
  848. $tkPrice = $hdOrder['tkPrice'] ?? 0;
  849. $orderItemList = \bizHd\order\classes\OrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  850. if (!empty($orderItemList)) {
  851. foreach ($orderItemList as $orderItem) {
  852. $classId = $orderItem->classId ?? 0;
  853. $unitPrice = $orderItem->unitPrice ?? 0;
  854. $refundNum = $orderItem->refundNum ?? 0;
  855. $num = $orderItem->num ?? 0;
  856. $productId = $orderItem->itemId ?? 0;
  857. $name = $orderItem->name ?? '';
  858. $remainNum = bcsub($num, $refundNum);
  859. $amount = bcmul($remainNum, $unitPrice, 2);
  860. $className = $classList[$classId]['name'] ?? '已删除';
  861. $arr[$classId]['className'] = $className;
  862. $arr[$classId]['classId'] = $classId;
  863. if (isset($arr[$classId]['income'])) {
  864. $arr[$classId]['income'] = bcadd($amount, $arr[$classId]['income'], 2);
  865. $arr[$classId]['saleNum'] = bcadd($arr[$classId]['saleNum'], $remainNum);
  866. } else {
  867. $arr[$classId]['expend'] = 0;
  868. $arr[$classId]['income'] = $amount;
  869. $arr[$classId]['saleNum'] = $remainNum;
  870. $arr[$classId]['cgNum'] = 0;
  871. }
  872. $belongCost = $orderItem->belongCost ?? 0;
  873. if ($belongCost == 1) {
  874. if (isset($tbSaleIncome[$productId])) {
  875. $tbSaleIncome[$productId]['num'] = bcadd($tbSaleIncome[$productId]['num'], $remainNum);
  876. $tbSaleIncome[$productId]['price'] = bcadd($tbSaleIncome[$productId]['price'], $amount, 2);
  877. } else {
  878. $tbSaleIncome[$productId] = ['name' => $name, 'num' => $remainNum, 'price' => $amount];
  879. }
  880. }
  881. }
  882. if ($tkPrice > 0) {
  883. $refundList = HdRefundClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  884. if (!empty($refundList)) {
  885. foreach ($refundList as $refund) {
  886. if ($refund->refundType == 2) {
  887. $onlyRefund = bcadd($onlyRefund, $refund->refundPrice, 2);
  888. }
  889. }
  890. }
  891. }
  892. }
  893. }
  894. $totalIncome = 0;
  895. $totalExpend = 0;
  896. if (!empty($arr)) {
  897. foreach ($arr as $key => $val) {
  898. $income = $val['income'] ?? 0;
  899. $income = floatval($income);
  900. $totalIncome = bcadd($totalIncome, $income, 2);
  901. $expend = $val['expend'] ?? 0;
  902. $expend = floatval($expend);
  903. $totalExpend = bcadd($totalExpend, $expend, 2);
  904. $profit = bcsub($income, $expend, 2);
  905. $arr[$key]['profit'] = floatval($profit);
  906. $arr[$key]['income'] = floatval($income);
  907. $arr[$key]['expend'] = floatval($expend);
  908. }
  909. }
  910. //总成本算上物流打包等费用
  911. $totalExpend = bcadd($totalExpend, $packFreight, 2);
  912. //总收入算上采购仅退款
  913. $totalIncome = bcadd($totalIncome, $onlyRefundCg, 2);
  914. //总收入加上人工费
  915. $totalIncome = bcadd($totalIncome, $totalLabourCost, 2);
  916. //总收入减掉优惠金额
  917. $totalIncome = bcsub($totalIncome, $totalDiscountAmount, 2);
  918. //总成本算上销售仅退款
  919. $totalExpend = bcadd($totalExpend, $onlyRefund, 2);
  920. $totalProfit = bcsub($totalIncome, $totalExpend, 2);
  921. $totalIncome = floatval($totalIncome);
  922. $totalExpend = floatval($totalExpend);
  923. $totalProfit = floatval($totalProfit);
  924. $arr = array_values($arr);
  925. $arr = arrayUtil::arraySort($arr, 'profit');
  926. if ($export == 1) {
  927. $statData = [
  928. ['classId' => 0, 'className' => '物流打包费', 'income' => 0, 'expend' => $packFreight, 'profit' => -$packFreight, 'saleNum' => 0, 'cgNum' => 0],
  929. ['classId' => 0, 'className' => '采购仅退款', 'income' => $onlyRefundCg, 'expend' => 0, 'profit' => $onlyRefundCg, 'saleNum' => 0, 'cgNum' => 0],
  930. ['classId' => 0, 'className' => '销售仅退款', 'income' => 0, 'expend' => $onlyRefund, 'profit' => -$onlyRefund, 'saleNum' => 0, 'cgNum' => 0],
  931. ['classId' => 0, 'className' => '附加人工费', 'income' => $totalLabourCost, 'expend' => 0, 'profit' => $totalLabourCost, 'saleNum' => 0, 'cgNum' => 0],
  932. ['classId' => 0, 'className' => '优惠金额', 'income' => -$totalDiscountAmount, 'expend' => 0, 'profit' => -$totalDiscountAmount, 'saleNum' => 0, 'cgNum' => 0],
  933. ['classId' => 0, 'className' => '【汇总金额】', 'income' => $totalIncome, 'expend' => $totalExpend, 'profit' => $totalProfit, 'saleNum' => 0, 'cgNum' => 0],
  934. ];
  935. $list = array_merge($arr, $statData);
  936. self::exportIncomeClass($list, $mainId);
  937. }
  938. util::success(['list' => $arr, 'packFreight' => $packFreight, 'onlyRefundCg' => $onlyRefundCg, 'onlyRefund' => $onlyRefund,
  939. 'totalIncome' => $totalIncome,
  940. 'totalExpend' => $totalExpend,
  941. 'totalProfit' => $totalProfit,
  942. 'tbSaleIncome' => $tbSaleIncome,
  943. 'tbCgIncome' => $tbCgIncome,
  944. 'tbCgIncomeStaff' => $tbCgIncomeStaff,
  945. 'tbSaleIncomeStaff' => $tbSaleIncomeStaff,
  946. 'totalLabourCost' => $totalLabourCost,
  947. 'totalDiscountAmount' => $totalDiscountAmount,
  948. ]);
  949. }
  950. public static function exportIncomeOutItem($list, $mainId)
  951. {
  952. $phpExcelFile = Yii::getAlias("@vendor/phpoffice/phpexcel/");
  953. require_once($phpExcelFile . 'Classes/PHPExcel.php');
  954. $objPHPExcel = new \PHPExcel();
  955. $objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
  956. ->setLastModifiedBy("Maarten Balliauw")
  957. ->setTitle("Office 2007 XLSX Document")
  958. ->setSubject("Office 2007 XLSX Document")
  959. ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
  960. ->setKeywords("office 2007 openxml php")
  961. ->setCategory("file");
  962. $ghsTitle = '供货商';
  963. $objPHPExcel->getActiveSheet()->getHeaderFooter()->setOddHeader(date('n月j日') . " " . $ghsTitle);
  964. $objPHPExcel->getActiveSheet()->getPageMargins()->setTop(0.7);
  965. $objPHPExcel->getActiveSheet()->getPageMargins()->setBottom(0.1);
  966. $objPHPExcel->getActiveSheet()->getPageMargins()->setLeft(0.5);
  967. $objPHPExcel->getActiveSheet()->getPageMargins()->setRight(0);
  968. $objPHPExcel->getActiveSheet()->getPageMargins()->setHeader(0.1);
  969. $objPHPExcel->getActiveSheet()->getPageMargins()->setFooter(0);
  970. $objPHPExcel->getActiveSheet()->setCellValue('A1', '名称');
  971. $objPHPExcel->getActiveSheet()->setCellValue('B1', '销售收入');
  972. $objPHPExcel->getActiveSheet()->setCellValue('C1', '销售数量');
  973. $objPHPExcel->getActiveSheet()->setCellValue('D1', '采购成本');
  974. $objPHPExcel->getActiveSheet()->setCellValue('E1', '采购数量');
  975. $objPHPExcel->getActiveSheet()->setCellValue('F1', '利润');
  976. //设置宽度
  977. $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(30);
  978. $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(20);
  979. $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(20);
  980. $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(20);
  981. //加粗
  982. $objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setSize(9)->setBold(true);
  983. $objPHPExcel->getActiveSheet()->getStyle('B1')->getFont()->setSize(9)->setBold(true);
  984. $objPHPExcel->getActiveSheet()->getStyle('C1')->getFont()->setSize(9)->setBold(true);
  985. $objPHPExcel->getActiveSheet()->getStyle('D1')->getFont()->setSize(9)->setBold(true);
  986. $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  987. $objPHPExcel->getActiveSheet()->getStyle('B1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  988. $objPHPExcel->getActiveSheet()->getStyle('C1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  989. $objPHPExcel->getActiveSheet()->getStyle('D1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  990. $baseRow = 2;
  991. foreach ($list as $key => $custom) {
  992. $i = $baseRow + $key;
  993. $objPHPExcel->getActiveSheet()->setCellValue('A' . $i, $custom['name']);
  994. $objPHPExcel->getActiveSheet()->setCellValue('B' . $i, $custom['income']);
  995. $objPHPExcel->getActiveSheet()->setCellValue('C' . $i, $custom['inNum']);
  996. $objPHPExcel->getActiveSheet()->setCellValue('D' . $i, $custom['expend']);
  997. $objPHPExcel->getActiveSheet()->setCellValue('E' . $i, $custom['outNum']);
  998. $objPHPExcel->getActiveSheet()->setCellValue('F' . $i, $custom['profit']);
  999. //居左
  1000. $objPHPExcel->getActiveSheet()->getStyle('A' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1001. $objPHPExcel->getActiveSheet()->getStyle('B' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1002. $objPHPExcel->getActiveSheet()->getStyle('C' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1003. $objPHPExcel->getActiveSheet()->getStyle('D' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1004. $objPHPExcel->getActiveSheet()->getStyle('E' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1005. $objPHPExcel->getActiveSheet()->getStyle('F' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1006. }
  1007. $fileName = '收支花材-' . date("m-d");
  1008. $objPHPExcel->getActiveSheet()->setTitle($fileName);
  1009. $objPHPExcel->setActiveSheetIndex(0);
  1010. $dir = './priceTable/' . $mainId;
  1011. if (file_exists($dir) == false) {
  1012. mkdir($dir, 0777, true);
  1013. }
  1014. $date = $fileName;
  1015. $file = $date . '.xls';
  1016. $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
  1017. if (file_exists($dir . '/' . $file)) {
  1018. unlink($dir . '/' . $file);
  1019. }
  1020. $objWriter->save($dir . '/' . $file);
  1021. $fileUrl = Yii::$app->params['ghsHost'] . '/priceTable/' . $mainId . '/' . $file;
  1022. util::success(['file' => $fileUrl, 'shortFile' => $file]);
  1023. }
  1024. public static function exportIncomeClass($list, $mainId)
  1025. {
  1026. $phpExcelFile = Yii::getAlias("@vendor/phpoffice/phpexcel/");
  1027. require_once($phpExcelFile . 'Classes/PHPExcel.php');
  1028. $objPHPExcel = new \PHPExcel();
  1029. $objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
  1030. ->setLastModifiedBy("Maarten Balliauw")
  1031. ->setTitle("Office 2007 XLSX Document")
  1032. ->setSubject("Office 2007 XLSX Document")
  1033. ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
  1034. ->setKeywords("office 2007 openxml php")
  1035. ->setCategory("file");
  1036. $ghsTitle = '供货商';
  1037. $objPHPExcel->getActiveSheet()->getHeaderFooter()->setOddHeader(date('n月j日') . " " . $ghsTitle);
  1038. $objPHPExcel->getActiveSheet()->getPageMargins()->setTop(0.7);
  1039. $objPHPExcel->getActiveSheet()->getPageMargins()->setBottom(0.1);
  1040. $objPHPExcel->getActiveSheet()->getPageMargins()->setLeft(0.5);
  1041. $objPHPExcel->getActiveSheet()->getPageMargins()->setRight(0);
  1042. $objPHPExcel->getActiveSheet()->getPageMargins()->setHeader(0.1);
  1043. $objPHPExcel->getActiveSheet()->getPageMargins()->setFooter(0);
  1044. $objPHPExcel->getActiveSheet()->setCellValue('A1', '名称');
  1045. $objPHPExcel->getActiveSheet()->setCellValue('B1', '销售数量');
  1046. $objPHPExcel->getActiveSheet()->setCellValue('C1', '销售收入');
  1047. $objPHPExcel->getActiveSheet()->setCellValue('D1', '采购数量');
  1048. $objPHPExcel->getActiveSheet()->setCellValue('E1', '采购成本');
  1049. $objPHPExcel->getActiveSheet()->setCellValue('F1', '利润');
  1050. //设置宽度
  1051. $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(20);
  1052. //加粗
  1053. $objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setSize(9)->setBold(true);
  1054. $objPHPExcel->getActiveSheet()->getStyle('B1')->getFont()->setSize(9)->setBold(true);
  1055. $objPHPExcel->getActiveSheet()->getStyle('C1')->getFont()->setSize(9)->setBold(true);
  1056. $objPHPExcel->getActiveSheet()->getStyle('D1')->getFont()->setSize(9)->setBold(true);
  1057. $objPHPExcel->getActiveSheet()->getStyle('E1')->getFont()->setSize(9)->setBold(true);
  1058. $objPHPExcel->getActiveSheet()->getStyle('F1')->getFont()->setSize(9)->setBold(true);
  1059. $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1060. $objPHPExcel->getActiveSheet()->getStyle('B1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1061. $objPHPExcel->getActiveSheet()->getStyle('C1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1062. $objPHPExcel->getActiveSheet()->getStyle('D1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1063. $objPHPExcel->getActiveSheet()->getStyle('E1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1064. $objPHPExcel->getActiveSheet()->getStyle('F1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1065. $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
  1066. $objPHPExcel->getActiveSheet()->getStyle('B1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
  1067. $objPHPExcel->getActiveSheet()->getStyle('C1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
  1068. $objPHPExcel->getActiveSheet()->getStyle('D1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
  1069. $objPHPExcel->getActiveSheet()->getStyle('E1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
  1070. $objPHPExcel->getActiveSheet()->getStyle('F1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
  1071. $baseRow = 2;
  1072. foreach ($list as $key => $custom) {
  1073. $i = $baseRow + $key;
  1074. $objPHPExcel->getActiveSheet()->setCellValue('A' . $i, $custom['className']);
  1075. $objPHPExcel->getActiveSheet()->setCellValue('B' . $i, $custom['saleNum']);
  1076. $objPHPExcel->getActiveSheet()->setCellValue('C' . $i, $custom['income']);
  1077. $objPHPExcel->getActiveSheet()->setCellValue('D' . $i, $custom['cgNum']);
  1078. $objPHPExcel->getActiveSheet()->setCellValue('E' . $i, $custom['expend']);
  1079. $objPHPExcel->getActiveSheet()->setCellValue('F' . $i, $custom['profit']);
  1080. //居左
  1081. $objPHPExcel->getActiveSheet()->getStyle('A' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1082. $objPHPExcel->getActiveSheet()->getStyle('B' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1083. $objPHPExcel->getActiveSheet()->getStyle('C' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1084. $objPHPExcel->getActiveSheet()->getStyle('D' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1085. $objPHPExcel->getActiveSheet()->getStyle('E' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1086. $objPHPExcel->getActiveSheet()->getStyle('F' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1087. }
  1088. $fileName = '收支分类-' . time();
  1089. $objPHPExcel->getActiveSheet()->setTitle($fileName);
  1090. $objPHPExcel->setActiveSheetIndex(0);
  1091. $dir = './priceTable/' . $mainId;
  1092. if (file_exists($dir) == false) {
  1093. mkdir($dir, 0777, true);
  1094. }
  1095. $date = $fileName;
  1096. $file = $date . '.xls';
  1097. $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
  1098. if (file_exists($dir . '/' . $file)) {
  1099. unlink($dir . '/' . $file);
  1100. }
  1101. $objWriter->save($dir . '/' . $file);
  1102. $fileUrl = Yii::$app->params['ghsHost'] . '/priceTable/' . $mainId . '/' . $file;
  1103. util::success(['file' => $fileUrl, 'shortFile' => $file]);
  1104. }
  1105. public static function exportItemSale($list, $mainId)
  1106. {
  1107. $phpExcelFile = Yii::getAlias("@vendor/phpoffice/phpexcel/");
  1108. require_once($phpExcelFile . 'Classes/PHPExcel.php');
  1109. $objPHPExcel = new \PHPExcel();
  1110. $objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
  1111. ->setLastModifiedBy("Maarten Balliauw")
  1112. ->setTitle("Office 2007 XLSX Document")
  1113. ->setSubject("Office 2007 XLSX Document")
  1114. ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
  1115. ->setKeywords("office 2007 openxml php")
  1116. ->setCategory("file");
  1117. $ghsTitle = '供货商';
  1118. $objPHPExcel->getActiveSheet()->getHeaderFooter()->setOddHeader(date('n月j日') . " " . $ghsTitle);
  1119. $objPHPExcel->getActiveSheet()->getPageMargins()->setTop(0.7);
  1120. $objPHPExcel->getActiveSheet()->getPageMargins()->setBottom(0.1);
  1121. $objPHPExcel->getActiveSheet()->getPageMargins()->setLeft(0.5);
  1122. $objPHPExcel->getActiveSheet()->getPageMargins()->setRight(0);
  1123. $objPHPExcel->getActiveSheet()->getPageMargins()->setHeader(0.1);
  1124. $objPHPExcel->getActiveSheet()->getPageMargins()->setFooter(0);
  1125. $objPHPExcel->getActiveSheet()->setCellValue('A1', '名称');
  1126. $objPHPExcel->getActiveSheet()->setCellValue('B1', '总数量');
  1127. $objPHPExcel->getActiveSheet()->setCellValue('C1', '总金额');
  1128. $objPHPExcel->getActiveSheet()->setCellValue('D1', '毛利');
  1129. $objPHPExcel->getActiveSheet()->setCellValue('E1', '毛利率');
  1130. //设置宽度
  1131. $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(30);
  1132. $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(20);
  1133. $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(20);
  1134. $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(20);
  1135. $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(20);
  1136. //加粗
  1137. $objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setSize(9)->setBold(true);
  1138. $objPHPExcel->getActiveSheet()->getStyle('B1')->getFont()->setSize(9)->setBold(true);
  1139. $objPHPExcel->getActiveSheet()->getStyle('C1')->getFont()->setSize(9)->setBold(true);
  1140. $objPHPExcel->getActiveSheet()->getStyle('D1')->getFont()->setSize(9)->setBold(true);
  1141. $objPHPExcel->getActiveSheet()->getStyle('E1')->getFont()->setSize(9)->setBold(true);
  1142. $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1143. $objPHPExcel->getActiveSheet()->getStyle('B1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1144. $objPHPExcel->getActiveSheet()->getStyle('C1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1145. $objPHPExcel->getActiveSheet()->getStyle('D1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1146. $objPHPExcel->getActiveSheet()->getStyle('E1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1147. $baseRow = 2;
  1148. foreach ($list as $key => $custom) {
  1149. $i = $baseRow + $key;
  1150. $objPHPExcel->getActiveSheet()->setCellValue('A' . $i, $custom['name']);
  1151. $objPHPExcel->getActiveSheet()->setCellValue('B' . $i, $custom['num']);
  1152. $objPHPExcel->getActiveSheet()->setCellValue('C' . $i, $custom['amount']);
  1153. $objPHPExcel->getActiveSheet()->setCellValue('D' . $i, $custom['gross']);
  1154. $objPHPExcel->getActiveSheet()->setCellValue('E' . $i, $custom['mll'] . '%');
  1155. //居左
  1156. $objPHPExcel->getActiveSheet()->getStyle('A' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1157. $objPHPExcel->getActiveSheet()->getStyle('B' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1158. $objPHPExcel->getActiveSheet()->getStyle('C' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1159. $objPHPExcel->getActiveSheet()->getStyle('D' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1160. $objPHPExcel->getActiveSheet()->getStyle('E' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1161. }
  1162. $fileName = '花材销量-' . date("m-d");
  1163. $objPHPExcel->getActiveSheet()->setTitle($fileName);
  1164. $objPHPExcel->setActiveSheetIndex(0);
  1165. $dir = './priceTable/' . $mainId;
  1166. if (file_exists($dir) == false) {
  1167. mkdir($dir, 0777, true);
  1168. }
  1169. $date = $fileName;
  1170. $file = $date . '.xls';
  1171. $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
  1172. if (file_exists($dir . '/' . $file)) {
  1173. unlink($dir . '/' . $file);
  1174. }
  1175. $objWriter->save($dir . '/' . $file);
  1176. $fileUrl = Yii::$app->params['ghsHost'] . '/priceTable/' . $mainId . '/' . $file;
  1177. util::success(['file' => $fileUrl, 'shortFile' => $file]);
  1178. }
  1179. public static function send($mainId)
  1180. {
  1181. $get = Yii::$app->request->get();
  1182. $where = [];
  1183. $where['mainId'] = $mainId;
  1184. $searchTime = isset($get['searchTime']) && !empty($get['searchTime']) ? $get['searchTime'] : 'today';
  1185. $startTime = $get['startTime'] ?? '';
  1186. $endTime = $get['endTime'] ?? '';
  1187. $period = dateUtil::formatTime($searchTime, $startTime, $endTime, true);
  1188. $start = $period['startTime'];
  1189. $end = $period['endTime'];
  1190. $where['time'] = ['between', [$start, $end]];
  1191. $currentStartDate = date('Y-m-d', strtotime($start));
  1192. $currentEndDate = date('Y-m-d', strtotime($end));
  1193. $currentStartTime = $currentStartDate . ' 00:00:00';
  1194. $currentEndTime = $currentEndDate . ' 23:59:59';
  1195. $s = strtotime($currentStartTime);
  1196. $e = strtotime($currentEndTime);
  1197. $p = bcsub($e, $s);
  1198. $xx = bcmul(86400, 90);
  1199. if ($p > $xx) {
  1200. util::fail('查询时间不能超过三个月');
  1201. }
  1202. $where = ['mainId' => $mainId, 'status' => ['in', [OrderClass::ORDER_STATUS_UN_SEND, OrderClass::ORDER_STATUS_SENDING, OrderClass::ORDER_STATUS_COMPLETE]]];
  1203. $where['payTime'] = ['between', [$currentStartTime, $currentEndTime]];
  1204. $ghsOrderList = OrderClass::getAllByCondition($where, null, '*');
  1205. $arr = [];
  1206. if (!empty($ghsOrderList)) {
  1207. foreach ($ghsOrderList as $ghsKey => $ghsOrder) {
  1208. $book = $ghsOrder['book'] ?? 0;
  1209. $status = $ghsOrder['status'] ?? 0;
  1210. if ($book == 1 && $status == 2) {
  1211. //预订单待发货的不算收入
  1212. continue;
  1213. }
  1214. if ($status == 0 || $status == 5) {
  1215. continue;
  1216. }
  1217. $orderSn = $ghsOrder['orderSn'] ?? '';
  1218. $actPrice = $ghsOrder['actPrice'] ?? 0;
  1219. if ($actPrice <= 0) {
  1220. continue;
  1221. }
  1222. $sendStaffId = $ghsOrder['sendStaffId'] ?? 0;
  1223. $sendStaffName = isset($ghsOrder['sendStaffName']) && !empty($ghsOrder['sendStaffName']) ? $ghsOrder['sendStaffName'] : '未记名';
  1224. if (isset($arr[$sendStaffId])) {
  1225. $arr[$sendStaffId]['orderNum'] = bcadd($arr[$sendStaffId]['orderNum'], 1);
  1226. $arr[$sendStaffId]['amount'] = bcadd($arr[$sendStaffId]['amount'], $actPrice, 2);
  1227. } else {
  1228. $arr[$sendStaffId] = ['staffName' => $sendStaffName, 'orderNum' => 1, 'itemNum' => 0, 'amount' => $actPrice, 'staffId' => $sendStaffId];
  1229. }
  1230. $orderItemList = OrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  1231. if (!empty($orderItemList)) {
  1232. foreach ($orderItemList as $orderItem) {
  1233. $refundNum = $orderItem->refundNum ?? 0;
  1234. $num = $orderItem->xhNum ?? 0;
  1235. $remainNum = bcsub($num, $refundNum);
  1236. $arr[$sendStaffId]['itemNum'] = bcadd($arr[$sendStaffId]['itemNum'], $remainNum);
  1237. }
  1238. }
  1239. }
  1240. }
  1241. if (!empty($arr)) {
  1242. $arr = array_values($arr);
  1243. }
  1244. return $arr;
  1245. }
  1246. }